module
sampler
ion7.core.SamplerBuilder
ion7.core.Sampler
Functions
SamplerBuilder.new
Start a new chain. Returns a fluent builder ; chain method calls (`:top_k(...)`, `:dist()`, ...) and finalise with `:build()`.
SamplerBuilder:greedy
Pick the highest-probability token at every step.
SamplerBuilder:dist
Sample from the (post-temperature) probability distribution.
SamplerBuilder:temperature
Apply a temperature to the logits. `0` = greedy ; `1` = unmodified.
SamplerBuilder:temp
Alias : shorter spelling of `:temperature`.
SamplerBuilder:temperature_dynamic
Dynamic temperature : final temperature drawn from `[base-range, base+range]`.
SamplerBuilder:temp_dynamic
Alias : shorter spelling of `:temperature_dynamic`.
SamplerBuilder:top_k
Top-K filtering : keep only the K most probable tokens.
SamplerBuilder:top_p
Nucleus (top-P) filtering : keep tokens up to cumulative probability `p`.
SamplerBuilder:min_p
Min-P filtering : keep tokens whose probability is at least `p × max_prob`.
SamplerBuilder:top_n_sigma
Top-N-sigma : keep tokens within `n` standard deviations of the maximum logit. Newer (2025) alternative to top-k / top-p.
SamplerBuilder:typical
Locally-typical sampling : selects tokens whose information content matches the expected entropy of the distribution. More natural output than top-p alone for free-form generation.
SamplerBuilder:xtc
XTC (Exclude Top Choices) : with probability `probability`, drop candidates above `threshold` to encourage diversity.
SamplerBuilder:mirostat
Mirostat v1 : adaptive sampling that targets a constant entropy.
SamplerBuilder:mirostat_v2
Mirostat v2 : simpler than v1, no M parameter.
SamplerBuilder:penalties
Repetition / frequency / presence penalties.
SamplerBuilder:dry
DRY (Don't Repeat Yourself) repetition sampler.
SamplerBuilder:grammar
Constrain output to a GBNF grammar.
SamplerBuilder:grammar_lazy
Lazy grammar : the grammar only kicks in once one of the trigger conditions is met. Useful for "free-form prose, then JSON when the model emits `{`" workflows (CRANE-style).
SamplerBuilder:logit_bias
Logit bias : `{ [token_id] = delta_logit, ... }`.
SamplerBuilder:adaptive_p
Adaptive-p sampler : exponentially-decayed probability threshold.
SamplerBuilder:custom
Plug a Lua-implemented `CustomSampler` into the chain. The builder pins the wrapper so its trampolines outlive the chain.
SamplerBuilder:infill
Fill-in-the-Middle sampler. Requires a `vocab` cdata or `Vocab`.
SamplerBuilder:reasoning_budget
Reasoning-budget sampler : caps the token count inside `
SamplerBuilder:build
Materialise the chain into a `Sampler` instance ready to use.
raises — When any sampler init returns NULL.
Sampler._wrap
Internal : wrap a raw chain pointer into a Sampler instance.
Sampler:sample
Sample the next token from the logits at position `idx`. When called on a CHAIN (the typical case), `llama_sampler_sample` internally calls `llama_sampler_accept` on every step. Do NOT call `Sampler:accept(token)` separately afterwards — that would be a double-accept and would corrupt grammar-state and DRY history.
Sampler:accept
Manually notify the chain that a token was accepted. Only useful for sub-samplers obtained via `Sampler:get(i)` ; not needed when you call `:sample` on the chain itself.
Sampler:reset
Reset every sub-sampler's state (penalty history, mirostat, grammar).
Sampler:seed
Sampler:name
Sampler:n
Sampler:get
Get the sub-sampler at index `i`. Returned handle is NOT GC-managed (the chain owns it).
Sampler:remove
Remove the sub-sampler at index `i` and return it. Caller takes ownership — the returned Sampler is NOT GC-managed.
Sampler:clone
Deep-clone the chain. The clone is independently GC-managed.
Sampler:perf
Read sampler perf counters into a Lua table.
Sampler:perf_print
Print sampler perf to stderr.
Sampler:perf_reset
Reset sampler perf counters.
Sampler:free
Free the chain immediately. Idempotent. Disarms `ffi.gc` so the GC does not double-free later.
Sampler.chain
Entry point of the fluent API.
Sampler.default
Pre-built balanced sampler : penalties + top-k 40 + top-p 0.95 + min-p 0.05 + temp 0.8 + dist. Suitable for most chat workloads.
Sampler.greedy
Pure greedy sampler. Deterministic, ideal for grammar-constrained structured output where any randomness would break parsing.
Sampler.creative
More adventurous defaults : higher temp, mild repetition control.