pub struct ConstraintDecoder { /* private fields */ }Expand description
The Constraint Decoder - makes forbidden tokens PHYSICALLY IMPOSSIBLE
This is not a filter. This is not a guard. This modifies the REALITY of what the model can produce.
Implementations§
Source§impl ConstraintDecoder
impl ConstraintDecoder
Sourcepub fn new(rules: &[String], vocab_size: usize) -> Self
pub fn new(rules: &[String], vocab_size: usize) -> Self
Create a new constraint decoder from sealed rules
The forbidden space is computed ONCE at initialization. This is the “hard-wiring” - baked into the system.
Sourcepub fn apply_constraints(&self, logits: &mut [f32])
pub fn apply_constraints(&self, logits: &mut [f32])
Apply constraints to logits - THE CORE OF DIAMOND
This is where the magic happens:
- Forbidden tokens get logit = -∞
- After softmax: P(forbidden) = 0.0
- The model CANNOT generate these tokens
Before: [0.2, 0.3, 0.5, ...] ← Any token possible
After: [0.2, -∞, 0.5, ...] ← Token 1 IMPOSSIBLE
Softmax: [0.28, 0.0, 0.72, ...] ← P(token1) = 0.0Sourcepub fn apply_constraints_with_context(
&self,
logits: &mut [f32],
context: &[u32],
)
pub fn apply_constraints_with_context( &self, logits: &mut [f32], context: &[u32], )
Apply constraints with context (for sequence-based constraints)
Sourcepub fn check_sequence(&self, tokens: &[u32]) -> Option<String>
pub fn check_sequence(&self, tokens: &[u32]) -> Option<String>
Check if a token sequence violates constraints
Returns None if valid, Some(violation) if invalid. But in Diamond, this should NEVER return Some - violations are impossible by construction.
Sourcepub fn space_hash(&self) -> [u8; 32]
pub fn space_hash(&self) -> [u8; 32]
Get the forbidden space hash (for attestation)
Sourcepub fn stats(&self) -> ForbiddenSpaceStats
pub fn stats(&self) -> ForbiddenSpaceStats
Get statistics about the forbidden space
Auto Trait Implementations§
impl Freeze for ConstraintDecoder
impl RefUnwindSafe for ConstraintDecoder
impl Send for ConstraintDecoder
impl Sync for ConstraintDecoder
impl Unpin for ConstraintDecoder
impl UnwindSafe for ConstraintDecoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more