Overview
GLRMask is a grammar-constrained decoding library for high-throughput LLM serving. It is optimized for fast mask computation with extremely low tail latency, even for complex grammars.
GLRMask supports general context-free grammars, including ambiguous ones. It accepts JSON Schema, Lark, and GLRMask’s native format: GLRM.
Benchmarks
Time between masks
Time between masks
· µs| Statistic | GLRMask | GLRMaskdynamic | llguidance | XGrammar |
|---|---|---|---|---|
| mean | 3.2 | 30.9 | 25.3 | 35.9 |
| 2.9 | 9.0 | 11.4 | 9.1 | |
| 5.1 | 87.5 | 40.6 | 38.2 | |
| 10.1 | 212.0 | 267.8 | 534.0 | |
| 17.0 | 657.4 | 1,070.7 | 2,736.8 | |
| 25.2 | 1,825.9 | 3,627.2 | 7,901.3 | |
| 43.8 | 4,731.5 | 8,276.3 | 12,660.3 |
Time to first mask
· ms| Statistic | GLRMask | GLRMaskdynamic | llguidance | XGrammar |
|---|---|---|---|---|
| mean | 37.3 | 5.8 | 1.3 | 84.5 |
| p50 | 16.8 | 4.1 | 0.9 | 44.3 |
| p90 | 75.0 | 9.5 | 2.1 | 203.2 |
| p99 | 306.4 | 43.4 | 13.3 | 834.3 |
| max | 1,350.1 | 138.0 | 48.6 | 4,066.2 |
GLRMask’s dynamic mode trades mask-generation speed for lower cold-start latency. It is based heavily on llguidance.
Measurement details
The displayed engines use one AWS c7i.16xlarge run (Intel Xeon Platinum 8488C, Linux), the Llama 3.1 8B Instruct vocabulary and the system allocator. TTFM compares 7,948 commonly built schemas from the 9,558-schema JSONSchemaBench corpus (Geng et al.). TBM compares 10,668 complete, correct valid trajectories accepted across the complete campaign: 1,570,228 token positions per engine. Failed, incorrect and incomplete trajectories are excluded. The original campaign cohort is fixed for every displayed arm. One wall-clock pass; sequential cases, CPU affinity 0–31 and a 32-thread compiler budget.
GLRMask 3d65c7a; llguidance 1.9.1; XGrammar 0.2.8 (97787376). These are frozen-source measurements, preceding the later strict schema-policy revision a306051. No verified later-policy eligibility map accompanies this run; no inferred filtering is applied. One XGrammar schema hit the memory limit; six llguidance runtime-error examples remain unknown. Fixture agreement does not establish full-language equivalence. Exact provenance and exclusions.
Full-run outcomes
| Mode | Build OK | Build errors | Runtime-error examples |
|---|---|---|---|
| GLRMask | 8546 | 1012 | 0 |
| GLRMask dynamic | 8547 | 1011 | 0 |
| llguidance | 8332 | 1226 | 6 |
| XGrammar | 9419 | 138 | 0 |
Time between masks
Time between masks
· µs| Statistic | GLRMask | GLRMaskdynamic | llguidance | XGrammar |
|---|---|---|---|---|
| mean | 17.2 | 1,126.5 | 1,152.4 | 116,561.8 |
| 18.4 | 26.5 | 938.6 | 47,894.1 | |
| 30.1 | 3,582.2 | 2,661.0 | 621,935.7 | |
| 46.7 | 4,100.1 | 3,389.5 | 679,617.9 | |
| 63.6 | 4,617.6 | 3,761.8 | 805,342.2 | |
| 69.3 | 4,814.8 | 3,819.2 | 825,890.1 | |
| 69.6 | 4,849.1 | 3,825.7 | 826,881.4 |
Time to first mask
· ms| Statistic | GLRMask | GLRMaskdynamic | llguidance | XGrammar |
|---|---|---|---|---|
| JavaScript | 5,593.7 | 109.0 | 8.9 | 4,175.5 |
GLRMask’s dynamic mode trades mask-generation speed for lower cold-start latency. It is based heavily on llguidance.
Measurement details
GLRMask 3d65c7a; llguidance 1.9.1; XGrammar 0.2.8 (97787376). The same hardware, vocabulary, allocator and timing protocol; one shared JavaScript grammar and fresh state per fixture. TBM compares 27 complete, correct valid trajectories: 2,370 positions per engine. TTFM is one original-process cold observation per engine, not 55 independent builds. XGrammar timed out on the final valid fixture after 120 seconds; only the 24 unattempted negatives continued separately. The second XGrammar cold observation is not averaged into TTFM. The original fixed campaign exclusions remain unchanged. Exact provenance and exclusions.
How it works
Getting started
Installation
python -m pip install glrmask
Usage
GLRMask has four core types:
Grammar: a grammar, written as source text or built from an expression.Module: a grammar plus its extern bindings and any precompiled work. Can be saved and loaded.Constraint: a grammar compiled for a vocabulary, ready to use.ConstraintState: the state of one generated sequence.
Python quickstart
import glrmask
vocab = glrmask.Vocab.from_id_to_bytes({0: b"hello", 1: b" ", 2: b"world"})
grammar = glrmask.Grammar.from_lark('start: "hello" " " "world"')
constraint = grammar.compile(vocab)
state = constraint.start()
for token_id, expected in [(0, [True, False, False]),
(1, [False, True, False]),
(2, [False, False, True])]:
assert state.mask().tolist() == expected
assert state.commit_token(token_id)
assert state.is_accepting()
Rust quickstart
use glrmask::{Grammar, Vocab};
fn main() -> glrmask::Result<()> {
let vocab = Vocab::new(vec![
(0, b"hello".to_vec()), (1, b" ".to_vec()), (2, b"world".to_vec()),
]);
let grammar = Grammar::from_lark(r#"start: "hello" " " "world""#)?;
let constraint = grammar.compile(&vocab)?;
let mut state = constraint.start();
for token_id in 0..3 {
assert_eq!(state.mask()[0], 1 << token_id);
assert!(state.commit_token(token_id));
}
assert!(state.is_accepting());
Ok(())
}
Full guide · Python API reference · Rust API reference
Native bindings: C++ documentation JavaScript / TypeScript documentation.