TL;DR
FactorSpec transforms complex relational constraints (like those in trip planning or tool calls) into a high-speed speculative drafter for LLMs. By offloading exact feasibility checks to parallel worker processes and using factor graphs, it achieves up to 1.43x faster decoding and a massive 13.9x increase in valid structured outputs compared to standard syntax-only baselines.
What Problem Does This Paper Address?
LLMs are increasingly tasked with generating structured data—JSON records, tool calls, or complex plans. While current serving engines (like SGLang and vLLM) use grammar masks to ensure syntactic correctness (e.g., "is this valid JSON?"), they struggle with relational correctness.
For example, in a trip itinerary, an LLM might generate a syntactically perfect JSON object that is logically impossible because it schedules a stay in a city without a connecting flight. The authors found that models like Qwen3-8B only produce valid itineraries for about 10% of problems in the NATURAL PLAN benchmark. Standard speculative decoding methods (like n-gram drafters) don't help here because they don't "understand" the underlying rules of the data.
Methods
FactorSpec's core innovation is treating the constraint itself as a speculative drafter.
- Factor Graph Compilation: It compiles a JSON Schema and its associated relational constraints into a factor graph. This allows for an exact feasibility check: the system can determine precisely which tokens are "certain" (the only valid choice given previous tokens) and which are "possible."
- Lossless Speculative Drafting: Instead of guessing, FactorSpec drafts every token that the constraints determine as certain. This is "lossless" because these tokens must be accepted by a valid decoder.
- Parallel Constraint Service: Relational checks (like ensuring all cities in a list are different) are computationally heavier than simple grammar masks. FactorSpec offloads this work to a pool of worker processes that run on the CPU while the GPU is executing the LLM's forward pass.
- Per-Request Verify Widths: Unlike standard engines that use a fixed draft length for a whole batch, FactorSpec dynamically adjusts the "verification width" for each request based on how many "certain" tokens it has drafted.
Datasets
The authors evaluated FactorSpec across six diverse workloads:
- NATURAL PLAN: Includes Trip, Meeting, and Calendar planning tasks where fields are highly interdependent.
- Tool-Calling: Includes a synthetic Ledger task, $\tau$-bench retail, and BFCL multi-turn calls, requiring the model to reference specific identifiers (orders, items, files) declared in the prompt.
Key Insights and Ideas
- Constraints are Drafters: The "aha moment" is realizing that a strict constraint isn't just a filter to be applied at the end; it's a source of information that can proactively propose tokens, speeding up the model.
- Hiding Latency: By moving the heavy lifting of constraint satisfaction (which can take milliseconds) to worker processes that overlap with GPU time, the cost of "exactness" becomes virtually free in terms of system throughput.
- Handling State Explosion: Traditional grammars (automata) struggle with joint relations like "all-different" because the number of states explodes. Factor graphs provide a much more compact and efficient way to represent these dependencies.
Key Results
- Speedup: FactorSpec decodes up to 1.43x faster than speculators tuned specifically for the workload.
- Efficiency: It yields up to 10.5 tokens per verify step on planning tasks, significantly outperforming n-gram drafters.
- Validity: Because it enforces exact constraints, it serves up to 13.9x more valid records per GPU-second than syntax-only baselines.
- Low Overhead: The exact check's cost is effectively hidden; the system maintains throughput parity with simple grammar engines like XGrammar while providing much stronger guarantees.
Limitations and Future Work
- Finite Domains: Currently, the system requires variables to have finite domains (e.g., a specific list of cities or a bounded range of days) to build the factor graph.
- Manual Specification: Users must explicitly declare the relations in the JSON Schema, which requires effort and domain knowledge.
- Hardware Scaling: As GPUs get faster and forward-pass times shrink, the system may require more CPU worker processes to keep the constraint checking "hidden" behind the GPU latency.
- Future Direction: The authors suggest extending this to more dynamic relations and potentially automating the extraction of relations from natural language prompts.
Summary updated