Skip to content

Commit

Permalink
Refactored sampler handles to be much simpler, never exposing ownersh…
Browse files Browse the repository at this point in the history
…ip of the individual stages.
  • Loading branch information
martindevans committed Oct 2, 2024
1 parent 5804cbd commit 5e85435
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 300 deletions.
19 changes: 1 addition & 18 deletions LLama.Examples/Examples/BatchedExecutorBoolQ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public ConversationRunner(BatchedExecutor executor, string sys, string question,
{
_executor = executor;
_decoder = new StreamingTokenDecoder(executor.Context);
_sampler = new GreedySamplingWithGrammarPipeline { Grammar = AnswerGrammar };
_sampler = new GreedySamplingPipeline { Grammar = AnswerGrammar };

// Make sure question ends with question mark
if (!question.EndsWith('?'))
Expand Down Expand Up @@ -244,21 +244,4 @@ public void Dispose()
_sampler.Dispose();
}
}

/// <summary>
/// A sampling pipeline which always selects the most likely token (after applying a grammar)
/// </summary>
public class GreedySamplingWithGrammarPipeline
: BaseSamplingPipeline
{
public required Grammar Grammar { get; init; }

protected override SafeLLamaSamplerChainHandle CreateChain(SafeLLamaContextHandle context)
{
var chain = SafeLLamaSamplerHandle.CreateChain(LLamaSamplerChainParams.Default());
chain.Add(SafeLLamaSamplerHandle.CreateGrammar(context.ModelHandle, Grammar.Gbnf, Grammar.Root));
chain.Add(SafeLLamaSamplerHandle.CreateGreedySampler());
return chain;
}
}
}
Loading

0 comments on commit 5e85435

Please sign in to comment.