Text Generation
Transformers
PyTorch
constrained-decoding
reachability
logit-processor
structured-generation
grammar-masking
dfa
fsm
Instructions to use uuugi/gclm-constrained-decoding with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use uuugi/gclm-constrained-decoding with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="uuugi/gclm-constrained-decoding")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("uuugi/gclm-constrained-decoding", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use uuugi/gclm-constrained-decoding with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "uuugi/gclm-constrained-decoding" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "uuugi/gclm-constrained-decoding", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/uuugi/gclm-constrained-decoding
- SGLang
How to use uuugi/gclm-constrained-decoding with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "uuugi/gclm-constrained-decoding" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "uuugi/gclm-constrained-decoding", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "uuugi/gclm-constrained-decoding" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "uuugi/gclm-constrained-decoding", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use uuugi/gclm-constrained-decoding with Docker Model Runner:
docker model run hf.co/uuugi/gclm-constrained-decoding
File size: 10,676 Bytes
102c78c 44810a8 102c78c 44810a8 6afdcc7 44810a8 b984b6d 44810a8 6afdcc7 44810a8 bc69298 44810a8 bc69298 44810a8 94c3e52 44810a8 b984b6d 44810a8 b984b6d 44810a8 b984b6d 94c3e52 44810a8 b984b6d 44810a8 b984b6d 44810a8 bc69298 44810a8 bc69298 44810a8 bc69298 44810a8 bc69298 44810a8 bc69298 94c3e52 44810a8 bc69298 44810a8 bc69298 44810a8 6afdcc7 44810a8 cc3b4ca 44810a8 6afdcc7 44810a8 6afdcc7 44810a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | ---
license: mit
tags:
- constrained-decoding
- reachability
- logit-processor
- structured-generation
- grammar-masking
- dfa
- fsm
- pytorch
- transformers
pipeline_tag: text-generation
---
# Goal-Conditioned Reachability Logit Masker (GCLM)
[](https://github.com/uuuugi/Goal-Conditioned-Reachability-Logit-Masker)
[](paper.pdf)
[](https://huggingface.co/uuugi/gclm-constrained-decoding)
[](https://www.python.org/downloads/)
[](https://pytorch.org/)
[](LICENSE)
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`**](paper.pdf) | π» **GitHub**: [**uuuugi/Goal-Conditioned-Reachability-Logit-Masker**](https://github.com/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)
```
| Feature | Standard Forward DFA (Outlines / SGLang) | **GCLM (Ours)** |
| :--- | :--- | :--- |
| **Masking Basis** | Current state validity (`s_curr -> s'`) | **Time-bounded backward reachability** (`s_curr -> s' ->* S_goal` in `β€ T_rem - 1` steps) |
| **Dead-End Traps** | β May enter valid forward branches that lead to dead-ends | β
**Preemptively masked** before entering trap |
| **Token Budget Exceeded** | β Outputs truncated/broken syntax when budget ends | β
**Forces early syntax closure** before budget exhaustion |
| **Per-Token Overhead** | O(1) table lookup | **Strict O(1) vectorized PyTorch lookup (< 0.1ms)** |
| **Complexity Scaling** | Scales with active state transitions | **Zero runtime dependence on state count (S)** |
---
## π 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:
```python
# 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`:
```python
# 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.
| Token Budget (T_max) | Vanilla Sampling | Forward DFA (Outlines Style) | **GCLM (Ours)** | Latency / Sample (GCLM) |
| :--- | :---: | :---: | :---: | :---: |
| **T_max = 6 tokens** | 0.0% | 30.0% | **100.0%** | **615.90 ms** (Fastest, early closure) |
| **T_max = 10 tokens** | 0.0% | 70.0% | **100.0%** | **1,086.02 ms** |
| **T_max = 16 tokens** | 0.0% | 85.0% | **100.0%** | **992.39 ms** |
---
### 2. Strict Budget JSON Schema Parsing Benchmark
> Complex nested JSON schema tested across 500 trials per budget.
| Budget (T_max) | Vanilla | Forward DFA (Outlines Style) | **GCLM (Ours)** | Key Insight |
| :--- | :---: | :---: | :---: | :--- |
| **T_max = 4** | 2.4% | 55.4% | **100.0%** | **Forces safe `{}` closure when fields cannot finish** |
| **T_max = 6** | 2.4% | 45.6% | **100.0%** | Prunes deep nested object paths |
| **T_max = 8** | 2.2% | 65.2% | **100.0%** | Eliminates dangling commas |
| **T_max = 16** | 1.4% | 91.8% | **100.0%** | Complete 100% parse rate across all budgets |
---
### 3. Multi-Step Agent Tool-Calling & Action Budget Benchmark
> ReAct-style multi-tool workflow evaluating goal completion within action limits.
| Action Budget | Vanilla | Forward DFA | **GCLM (Ours)** | Key Finding |
| :--- | :---: | :---: | :---: | :--- |
| **3 Actions** | 0.00% | 16.80% | **100.00%** | Dynamically forces 3-step shortest path |
| **4 Actions** | 0.00% | 33.20% | **100.00%** | Prunes unfinishable deep search subtrees |
| **8 Actions** | 0.60% | 65.20% | **100.00%** | **Completely avoids infinite retry trap loops** |
---
### 4. FSM Complexity & Strict O(1) Runtime Scaling
> Scaling state count `|S|` from 10 to 10,000 (1,000x increase). Plot saved as `paper_figure_scaling.png`.
| Vocabulary Size (V) | State Count (S) | Offline BFS Time | Memory Footprint | Online Latency per Token |
| :--- | :---: | :---: | :---: | :---: |
| **V = 32,000 (LLaMA)** | S = 10 | 29.55 ms | 2.44 MB | **388.72 Β΅s** |
| V = 32,000 | S = 100 | 240.10 ms | 24.42 MB | **335.10 Β΅s** |
| V = 32,000 | S = 1,000 | 2,111.82 ms | 244.19 MB | **340.84 Β΅s** |
| V = 32,000 | **S = 10,000** | 25,790.14 ms | 2.44 GB | **356.29 Β΅s** (O(1) verified) |
| **V = 151,643 (Qwen2.5)** | S = 10 | 159.29 ms | 11.57 MB | **601.92 Β΅s** |
| V = 151,643 | **S = 10,000** | 147,702.79 ms | 11.56 GB | **666.22 Β΅s** (O(1) verified) |
---
## π Quick Start
### 1. Installation
**From Hugging Face:**
```bash
git clone https://huggingface.co/uuugi/gclm-constrained-decoding
cd gclm-constrained-decoding
pip install -r requirements.txt
```
**From GitHub:**
```bash
git clone https://github.com/uuuugi/Goal-Conditioned-Reachability-Logit-Masker.git
cd Goal-Conditioned-Reachability-Logit-Masker
pip install -r requirements.txt
```
### 2. Basic Usage with Hugging Face Transformers
```python
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
```bash
# 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`**](paper.pdf)
π» **GitHub Repository**: [**uuuugi/Goal-Conditioned-Reachability-Logit-Masker**](https://github.com/uuuugi/Goal-Conditioned-Reachability-Logit-Masker)
π€ **Hugging Face Model**: [**uuugi/gclm-constrained-decoding**](https://huggingface.co/uuugi/gclm-constrained-decoding)
```bibtex
@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`](https://orcid.org/0009-0007-5612-5602)
---
## π License
MIT License
|