using System;
using LLama.Native;
namespace LLama.Sampling;
///
/// Base class for implementing custom sampling pipelines. This provides a helpful framework for implementing `ISamplingPipeline`.
///
public abstract class BaseSamplingPipeline
: ISamplingPipeline
{
///
/// Grammar to constrain valid tokens
///
public SafeLLamaGrammarHandle? Grammar { get; set; }
///
public LLamaToken Sample(SafeLLamaContextHandle ctx, ReadOnlySpan logits, ReadOnlySpan lastTokens)
{
// Apply processing to raw logit values
logits = ProcessLogits(ctx, logits, lastTokens);
// Process token data array to select a final token
var candidates = LLamaTokenDataArray.Create(logits);
candidates.ApplyGrammar(ctx, Grammar);
return ProcessTokenDataArray(ctx, candidates, lastTokens);
}
///
public virtual void Accept(SafeLLamaContextHandle ctx, LLamaToken token)
{
Grammar?.AcceptToken(ctx, token);
}
///
/// Process the raw logit values
///
/// The context being sampled from
/// The logits produced by the model
/// A list of tokens recently returned by the model
protected abstract ReadOnlySpan ProcessLogits(SafeLLamaContextHandle ctx, ReadOnlySpan logits, ReadOnlySpan lastTokens);
///
/// Process the LLamaTokenDataArray and select a single token
///
/// The context being sampled from
/// The LLamaTokenDataArray data produced by the model
/// A list of tokens recently returned by the model
///
protected abstract LLamaToken ProcessTokenDataArray(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, ReadOnlySpan lastTokens);
///
public virtual void Reset()
{
}
///
public abstract ISamplingPipeline Clone();
///
public virtual void Dispose()
{
GC.SuppressFinalize(this);
}
}