Skip to main content

Module constrained_decoding

Module constrained_decoding 

Source
Expand description

Grammar-constrained decoding for token-by-token generation.

This module provides the TokenConstraint trait and concrete implementations that restrict which tokens the model can emit at each decoding step:

The ConstrainedSampler wraps a crate::sampling_advanced::SamplerChain and applies a mask to logits before sampling so that only valid continuations are drawn.

§Example

use oxibonsai_runtime::constrained_decoding::{ConstrainedSamplerBuilder, TokenConstraint};

let mut sampler = ConstrainedSamplerBuilder::new(128, 42)
    .with_json_constraint();
assert!(!sampler.is_complete());

§Module structure

Phase 30B split the monolithic constrained_decoding.rs (1966 lines) into focused sub-modules; all external crate::constrained_decoding::* access paths are preserved through the re-exports below.

Structs§

AllowListConstraint
A constraint that forces the generated token sequence to exactly match one of a finite set of allowed token-id sequences (e.g., multiple-choice answers).
ConstrainedSampler
Wraps a crate::sampling_advanced::SamplerChain with a TokenConstraint.
ConstrainedSamplerBuilder
Ergonomic builder for ConstrainedSampler.
JsonConstraint
Constrains generation to syntactically valid JSON.
LengthConstraint
A constraint that enforces hard minimum and maximum token-count limits.
NoConstraint
A passthrough constraint that places no restriction on the vocabulary.
RegexConstraint
Constrains generation to strings that match a regular expression.
SequenceConstraint
A constraint that forces the generated output to reproduce a specific, pre-determined token sequence.

Enums§

ConstraintError
Errors that can arise when building or running a token constraint.
JsonParseState
Internal parser state for JsonConstraint.

Traits§

TokenConstraint
A constraint that restricts which tokens are valid at each decoding step.