Vectorizing the Trie: Efficient Constrained Decoding for LLM-based Generative Retrieval on Accelerators

Zhengyang Su, Isay Katsman, Y. Wang, Ruining He, Lukasz Heldt, Raghunandan H. Keshavan, Shao-Chuan Wang, Xinyang Yi, Mingyan Gao, Onkar Dalal, Lichan Hong, Ed Chi, Ningren Han

Abstract

Generative retrieval has emerged as a powerful paradigm for LLM-based recommendation. However, industrial recommender systems often benefit from restricting the output space to a constrained subset of items based on business logic (e.g. enforcing content freshness or product category), which standard autoregressive decoding cannot natively support. Moreover, existing constrained decoding methods that make use of prefix trees (Tries) incur severe latency penalties on hardware accelerators (TPUs/GPUs). In this work, we introduce STATIC (Sparse Transition Matrix-Accelerated Trie Index for Constrained Decoding), an efficient and scalable constrained decoding technique designed specifically for high-throughput LLM-based generative retrieval on TPUs/GPUs. By flattening the prefix tree into a static Compressed Sparse Row (CSR) matrix, we transform irregular tree traversals into fully vectorized sparse matrix operations, unlocking massive efficiency gains on hardware accelerators. We deploy STATIC on a large-scale industrial video recommendation platform serving billions of users. STATIC produces significant product metric impact with minimal latency overhead (0.033 ms per step and 0.25% of inference time), achieving a 948x speedup over a CPU trie implementation and a 47-1033x speedup over a hardware-accelerated binary-search baseline. Furthermore, the runtime overhead of STATIC remains extremely low across a wide range of practical configurations. To the best of our knowledge, STATIC enables the first production-scale deployment of strictly constrained generative retrieval. In addition, evaluation on academic benchmarks demonstrates that STATIC can considerably improve cold-start performance for generative retrieval. Our code is available at https://github.com/youtube/static-constraint-decoding.

What Problem Does This Paper Address?

In modern recommendation systems, Generative Retrieval (GR) is the new frontier. Instead of using traditional "embedding-based" search, we represent every item as a sequence of tokens (a Semantic ID) and train a Transformer to decode these IDs autoregressively.

However, industrial systems need constraints. You might want to only recommend videos uploaded in the last 7 days ("freshness") or items currently in stock. Standard LLM decoding doesn't know these rules.

Concept of a restricted vocabulary within a larger semantic ID space.

The traditional solution is a Trie (a prefix tree of all valid IDs), but Tries are "pointer-chasing" structures. They require jumping to random memory addresses, which is the "kryptonite" of hardware accelerators like GPUs and TPUs that crave contiguous, predictable data. Previous attempts to fix this either offloaded work to the CPU (too slow) or used binary search (which scales poorly with large vocabularies).

Methods

The authors propose STATIC (Sparse Transition Matrix-Accelerated Trie Index for Constrained Decoding). The core idea is to stop thinking of a Trie as a tree and start thinking of it as a state transition matrix.

The STATIC pipeline: from restricted vocabulary to sparse matrix representation and constrained decoding.
  1. Trie Flattening: They convert the prefix tree into a Compressed Sparse Row (CSR) matrix. This transforms irregular tree traversals into vectorized sparse matrix lookups.
  2. Vectorized Node Transition Kernel (VNTK): They designed a custom, branch-free kernel. Instead of "if-else" logic (which slows down GPUs), it uses "speculative slicing" and bitmasks to identify valid next tokens in a single, static computation graph.
  3. Hybrid Lookups: For the first one or two levels of the tree (where almost every prefix is valid and the data is dense), they use a fast dense tensor mask. For deeper, sparser levels, they switch to the CSR matrix.
  4. Hardware-Native Design: The entire process is designed to be compatible with XLA (Google's compiler for TPUs) and PyTorch Inductor, ensuring it runs entirely "on-device" without costly round-trips to the CPU.

Datasets

The paper evaluates STATIC in two primary contexts:

  • YouTube Production Data: A massive-scale dataset of 100 million to 1 billion videos. The constraints involved a "freshness" subset of ~20 million high-quality videos uploaded in the last 7 days.
  • Amazon Reviews: Used as an academic benchmark to test cold-start performance across categories like "Beauty," "Sports & Outdoors," and "Toys & Games."

Key Insights and Ideas

  • The "Memory Wall" is the Real Enemy: In LLM inference, the bottleneck isn't how fast you can calculate; it's how fast you can move data from memory to the chip. STATIC achieves $O(1)$ I/O complexity relative to the constraint set size, meaning it fetches all necessary data in one "coalesced" move.
  • Tries as Linear Algebra: By reformulating tree traversal as sparse matrix operations, the authors bridge the gap between classical data structures and modern ML compilers.
  • Solving the "Validity Gap": Generative models often "hallucinate" IDs that don't exist. Constrained decoding doesn't just enforce business rules; it ensures the model only speaks in valid item IDs, which is critical for the model's utility.
  • Cold-Start via Constraints: A surprising insight is that constrained decoding can fix the "cold-start" problem. By forcing the model to only generate IDs from a set of new items it hasn't seen during training, you can achieve non-trivial recommendation performance on brand-new content.

Key Results

  • Massive Speedups: STATIC achieved a 948x speedup over a standard CPU-based trie and was 47x to 1033x faster than the previous state-of-the-art hardware-accelerated binary search method (PPV).
  • Negligible Overhead: In a 3-billion parameter model, STATIC added only 0.033 ms of latency per decoding step, accounting for just 0.25% of total inference time.
  • Real-World Impact: When deployed on YouTube's Home Feed, it led to a +5.1% increase in 7-day fresh video views and a +0.15% increase in Click-Through Rate (CTR).
  • Scalability: The system scales linearly with the "branching factor" of the tree and remains efficient even as the number of constraints grows to 100 million items.

Limitations and Future Work

  • Static Construction: Currently, the sparse transition matrix is built offline. If the "valid" set of items changes every second (e.g., a video is deleted), the system needs a way to perform dynamic sparse updates without a full rebuild.
  • Memory Footprint: While efficient, the matrix for 20 million items still takes about 1.5 GB of HBM (High-Bandwidth Memory). Scaling to billions of items might eventually require sharding the matrix across multiple chips.
  • Offline/Online Sync: The paper notes that future work is needed to develop "real-time inventory changes" to handle the highly dynamic nature of web-scale content.

Summary updated

BibTeX
@misc{su2026vectorizing,
  author = {Zhengyang Su and Isay Katsman and Y. Wang and Ruining He and Lukasz Heldt and Raghunandan H. Keshavan and Shao-Chuan Wang and Xinyang Yi and Mingyan Gao and Onkar Dalal and Lichan Hong and Ed Chi and Ningren Han},
  title = {{Vectorizing the Trie: Efficient Constrained Decoding for LLM-based Generative Retrieval on Accelerators}},
  year = {2026},
  eprint = {2602.22647},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2602.22647},
}
Figure