liblloyal 1.0.0
Composable primitives for llama.cpp inference
Loading...
Searching...
No Matches
sampler.hpp File Reference

Token Sampling Operations. More...

#include "common.hpp"
#include "logits.hpp"
#include "tokenizer.hpp"
#include <cstdint>
#include <ctime>
#include <llama/llama.h>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <vector>

Go to the source code of this file.

Classes

struct  lloyal::detail::is_optional< T >
 Type trait to detect std::optional<T> More...
 
struct  lloyal::detail::is_optional< std::optional< T > >
 

Namespaces

namespace  lloyal
 JSON Schema to Grammar Converter (Header-Only)
 
namespace  lloyal::detail
 
namespace  lloyal::sampler
 

Concepts

concept  lloyal::SamplingParamsLike
 C++20 concept: Any type with sampling parameter fields.
 

Functions

template<class X , class T >
constexpr T lloyal::detail::as_value (const X &x, T def)
 Extract value from either T or std::optional<T> with fallback.
 
llama_token lloyal::sampler::greedy (llama_context *ctx, const llama_vocab *vocab)
 Greedy sampling: Select token with highest probability.
 
template<SamplingParamsLike P>
llama_token lloyal::sampler::sample_with_params (llama_context *ctx, const llama_vocab *vocab, const P &params, llama_sampler *grammarSampler=nullptr)
 Sample with configurable parameters (template accepts any SamplingParams type)
 
llama_token lloyal::sampler::greedy (llama_context *ctx, const llama_model *model)
 Greedy sampling with automatic vocab extraction.
 
template<SamplingParamsLike P>
llama_token lloyal::sampler::sample_with_params (llama_context *ctx, const llama_model *model, const P &params, llama_sampler *grammarSampler=nullptr)
 Parameterized sampling with automatic vocab extraction.
 
template<SamplingParamsLike P>
llama_sampler * lloyal::sampler::create_chain (const P &params)
 Create a persistent sampler chain from parameters.
 
llama_sampler * lloyal::sampler::clone_chain (llama_sampler *chain)
 Clone a sampler chain.
 
void lloyal::sampler::reseed_chain (llama_sampler *chain, uint32_t new_seed)
 Reseed the dist sampler in a chain.
 
void lloyal::sampler::free_chain (llama_sampler *chain)
 Free a sampler chain.
 
void lloyal::sampler::apply (llama_sampler *chain, llama_token_data_array *cur_p)
 Apply a sampler chain to a candidate array.
 
void lloyal::sampler::accept (llama_sampler *chain, llama_token token)
 Accept a token into the sampler chain.
 

Detailed Description

Token Sampling Operations.

Wraps llama.cpp sampling APIs with configurable sampling strategies. Uses C++20 concepts for generic parameter handling.

Architecture:

  • Concept-constrained templates accept any Nitrogen-generated SamplingParams type
  • No struct duplication or adapters required
  • Supports greedy, temperature, top-k, top-p, min-p, grammar-constrained sampling

Definition in file sampler.hpp.