| Crates.io | torg-mask |
| lib.rs | torg-mask |
| version | 0.2.1 |
| created_at | 2025-12-14 21:13:42.856799+00 |
| updated_at | 2025-12-28 03:54:16.893141+00 |
| description | LLM logit masking for TØR-G constrained decoding |
| homepage | |
| repository | https://github.com/arkavo-ai/torg |
| max_upload_size | |
| id | 1985119 |
| size | 38,686 |
LLM logit masking for TØR-G constrained decoding.
This crate provides utilities for integrating TØR-G's valid_next_tokens() with LLM inference engines. By masking invalid tokens during decoding, LLMs are guaranteed to produce syntactically correct boolean circuits.
use torg_mask::{ConstrainedDecoder, MaskGenerator, TokenMapping};
// Create a token mapping
// In production, map to actual unused token IDs in your LLM's vocabulary
let mapping = TokenMapping::sequential(256);
let generator = MaskGenerator::new(mapping.clone(), vocab_size);
let mut decoder = ConstrainedDecoder::new(generator);
// Decode loop
while !decoder.is_complete() {
// Get mask for valid tokens
let mask = decoder.next_mask();
// Apply mask to LLM logits
mask.apply_to_logits(&mut logits);
// Sample from masked distribution
let token_id = sample(&logits);
// Feed token to decoder
decoder.feed_token(token_id)?;
}
// Get the constructed graph
let graph = decoder.finish()?;
The TokenMapping struct maps TØR-G's 9 fixed tokens plus Id tokens to LLM vocabulary IDs:
// Sequential mapping for testing (IDs 0-264)
let mapping = TokenMapping::sequential(256);
// Custom mapping for production
let mapping = TokenMapping::builder()
.or(50256) // Map Or to vocab ID 50256
.nor(50257)
.xor(50258)
.node_start(50259)
.node_end(50260)
.input_decl(50261)
.output_decl(50262)
.true_token(50263)
.false_token(50264)
.id_base(50265) // Id(n) maps to 50265 + n
.id_count(256)
.build();
The LogitMask struct efficiently applies masks to logit vectors:
let mask = generator.generate(&valid_tokens);
// Check if a token is allowed
if mask.is_allowed(token_id) { ... }
// Apply mask in-place (O(vocab_size + allowed_count))
mask.apply_to_logits(&mut logits);
// Get allowed token IDs
for &id in mask.allowed_ids() { ... }
Licensed under either of Apache License, Version 2.0 or MIT license at your option.