uuugi/gclm-constrained-decoding
Goal-Conditioned Reachability Logit Masker (GCLM)
     
An ultra-fast, strictly O(1) runtime Goal-Conditioned Reachability Logit Masking Engine for Large Language Models. GCLM mathematically guarantees that an LLM will strictly reach designated goal/accepting states within a fixed token budget (T_max), fundamentally preventing dead-end traps and truncated syntax failures.
๐ Paper: **Read / Download `paper.pdf`** | ๐ป GitHub: **uuuugi/Goal-Conditioned-Reachability-Logit-Masker**
๐ก Key Differences: GCLM vs. Forward DFA Maskers (Outlines / SGLang)
[Traditional Forward DFA (Outlines / SGLang)]
Start (A) โโโ Token X โโโโถ [Valid Branch D] โโโ Token Y โโโโถ [Dead-End / Truncated Trap โ]
(Only checks if transition exists from current state)
[GCLM: Time-Bounded Backward Reachability (Ours)]
Start (A) โโโ Token X (Masked to -inf โ)
โโโ Token B โโโโถ State C โโโโถ Goal / Closing '}' โ
(Preemptively prunes any branch that cannot reach Goal in <= T_rem steps)๐ Mathematical Formulation
1. Offline Backward BFS Table Builder
Given an FSM (S, ฮฃ, ฮด, s_0, S_goal) and maximum token budget T_max, we precompute a reachability tensor R of shape (T_max + 1, |S|) via vectorized backward BFS:
# Base Step (t = 0):
R[0, s] = 1 if (s in S_goal) else 0
# Vectorized Backward BFS (for t = 1 ... T_max):
R[t, s] = R[t-1, s] OR (โ v โ V such that ฮด(s, v) >= 0 and R[t-1, ฮด(s, v)] == 1)2. Strict O(1) Runtime Logits Masking
At decoding step k with remaining token budget T_rem = T_max - k:
# Step 1: Vectorized check for valid transitions within remaining budget
ValidTokens(v) = (ฮด(s_curr, v) >= 0) AND R[min(T_rem - 1, T_max), clamp(ฮด(s_curr, v), 0)]
# Step 2: In-place O(1) logit masking
Logits[v] = Logits[v] if ValidTokens(v) == 1 else -inf๐ Repository Structure
gclm_project/
โโโ core/
โ โโโ __init__.py
โ โโโ fsm_builder.py # Transitions tensor & vectorized backward BFS reachability table
โ โโโ logit_processor.py # Hugging Face LogitsProcessor compatible O(1) in-place masker
โ โโโ compiler.py # Tokenizer-aware grammar/pattern compiler
โโโ benchmarks/
โ โโโ synthetic_deadend.py # Experiment 1: Dead-end trap avoidance benchmark
โ โโโ json_budget_bench.py # Experiment 2: Real-world strict budget JSON benchmark
โ โโโ tool_calling_bench.py # Experiment 3: Multi-step agent action budget benchmark
โ โโโ scaling_bench.py # Experiment 4: Complexity scaling (|S|=10~10,000) & plot generator
โ โโโ real_model_bench.py # Experiment 5: Real lightweight LLM (Qwen2.5) E2E benchmark
โ โโโ latency_bench.py # Per-token runtime overhead benchmark
โโโ examples/
โ โโโ run_generation.py # Live interactive generation demo with Transformers
โโโ tests/
โ โโโ test_fsm_builder.py # Unit tests for BFS reachability & multi-goal
โ โโโ test_logit_processor.py # Unit tests for batch masking & state progression
โโโ paper_figure_scaling.png # Publication-ready 300-DPI scaling figure
โโโ requirements.txt
โโโ README.md๐ Comprehensive Experimental Results
1. Real Lightweight LLM End-to-End Benchmark (Qwen2.5-0.5B)
Tested on real model weights generating JSON responses under strict token limits.
2. Strict Budget JSON Schema Parsing Benchmark
Complex nested JSON schema tested across 500 trials per budget.
3. Multi-Step Agent Tool-Calling & Action Budget Benchmark
ReAct-style multi-tool workflow evaluating goal completion within action limits.
4. FSM Complexity & Strict O(1) Runtime Scaling
Scaling state count|S|from 10 to 10,000 (1,000x increase). Plot saved aspaper_figure_scaling.png.
๐ Quick Start
1. Installation
From Hugging Face:
git clone https://huggingface.co/uuugi/gclm-constrained-decoding
cd gclm-constrained-decoding
pip install -r requirements.txtFrom GitHub:
git clone https://github.com/uuuugi/Goal-Conditioned-Reachability-Logit-Masker.git
cd Goal-Conditioned-Reachability-Logit-Masker
pip install -r requirements.txt2. Basic Usage with Hugging Face Transformers
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, LogitsProcessorList
from core.fsm_builder import ReachabilityFSM
from core.logit_processor import GoalReachabilityLogitsProcessor
# 1. Load model and tokenizer
model_id = "Qwen/Qwen2.5-0.5B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
vocab_size = model.config.vocab_size
max_budget = 15
# 2. Define FSM & Goal state
fsm = ReachabilityFSM(num_states=5, vocab_size=vocab_size)
fsm.add_transition(from_state=0, token_id=101, to_state=1)
fsm.add_transition(from_state=1, token_id=102, to_state=2)
fsm.set_goal_states([2])
# 3. Precompute reachability table (one-time offline step)
fsm.build_reachability(max_steps=max_budget)
# 4. Attach GCLM to Hugging Face LogitsProcessorList
gclm_processor = GoalReachabilityLogitsProcessor(fsm=fsm, max_budget=max_budget)
logits_processors = LogitsProcessorList([gclm_processor])
# 5. Generate with guaranteed reachability
inputs = tokenizer("Your prompt here", return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=max_budget,
logits_processor=logits_processors
)
print(tokenizer.decode(outputs[0]))๐งช Reproducing Experiments
# Run Unit Tests
python -m pytest tests/ -v
# Run Experiment 1: Synthetic Dead-End Benchmark
python -m benchmarks.synthetic_deadend
# Run Experiment 2: Strict Budget JSON Benchmark
python -m benchmarks.json_budget_bench
# Run Experiment 3: Agent Tool-Calling Benchmark
python -m benchmarks.tool_calling_bench
# Run Experiment 4: Scaling Benchmark & Generate Paper Plots
python -m benchmarks.scaling_bench
# Run Experiment 5: Real Lightweight LLM Benchmark (Qwen2.5)
python -m benchmarks.real_model_bench --model Qwen/Qwen2.5-0.5B๐ Paper & Citation
๐ Paper PDF: **Download `paper.pdf`** ๐ป GitHub Repository: **uuuugi/Goal-Conditioned-Reachability-Logit-Masker** ๐ค Hugging Face Model: **uuugi/gclm-constrained-decoding**
@article{an2026gclm,
title={Goal-Conditioned Reachability Logit Masker: Guaranteed Goal Satisfaction for Constrained LLM Generation in O(1) Time},
author={An, ByeongUk},
journal={arXiv preprint},
year={2026}
}Author: ByeongUk An Email: hhjjkk7186@gmail.com ORCID: `0009-0007-5612-5602`
๐ License
MIT License
