Skip to content

Commit c1783d4

Browse files
authored
Merge pull request #1282 from SerialKicked/fix-grammar-topk
Fixes Extended grammar optimization mode would crash if TopK sampling is set to zero.
2 parents 13eada9 + 7515dc2 commit c1783d4

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

LLama/Sampling/DefaultSamplingPipeline.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ public override LLamaToken Sample(SafeLLamaContextHandle ctx, int index)
246246
}
247247

248248
// Extended optimization : Apply the grammar to the TopK tokens and check if the selected token is valid
249-
if (GrammarOptimization == GrammarOptimizationMode.Extended)
249+
// Only run if TopK > 0
250+
if (GrammarOptimization == GrammarOptimizationMode.Extended && TopK > 0)
250251
{
251252
// Calculate a safe TopK value
252253
var safeTopK = Math.Min(TopK, nativeAll.Data.Length);

0 commit comments

Comments
 (0)