torg-mask
LLM logit masking for TØR-G constrained decoding.
Overview
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.
Features
- Token Mapping: Bidirectional mapping between TØR-G tokens and LLM vocab IDs
- Logit Masking: Efficient O(vocab_size + allowed_count) mask application
- Constrained Decoder: High-level orchestrator for decode loops
Usage
use ;
// Create a token mapping
// In production, map to actual unused token IDs in your LLM's vocabulary
let mapping = sequential;
let generator = new;
let mut decoder = new;
// Decode loop
while !decoder.is_complete
// Get the constructed graph
let graph = decoder.finish?;
Token Mapping
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 = sequential;
// Custom mapping for production
let mapping = builder
.or // Map Or to vocab ID 50256
.nor
.xor
.node_start
.node_end
.input_decl
.output_decl
.true_token
.false_token
.id_base // Id(n) maps to 50265 + n
.id_count
.build;
Logit Masking
The LogitMask struct efficiently applies masks to logit vectors:
let mask = generator.generate;
// Check if a token is allowed
if mask.is_allowed
// Apply mask in-place (O(vocab_size + allowed_count))
mask.apply_to_logits;
// Get allowed token IDs
for &id in mask.allowed_ids
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.