| name | static-constrained-decoding |
| title | Vectorizing the Trie: Efficient Constrained Decoding for LLM-based Generative Retrieval |
| version | 0.0.2 |
| engine | skillxiv-v0.0.2-claude-opus-4.6 |
| license | MIT |
| url | https://arxiv.org/abs/2602.22647 |
| keywords | ["Constrained Decoding","Trie Vectorization","Generative Retrieval","Inference Optimization","GPU Acceleration"] |
| description | STATIC converts prefix trees into sparse matrices for vectorized constrained decoding, achieving 948x speedup over CPU and enabling production-scale recommendation systems. |
Technique: Sparse Matrix Vectorization for Constrained Decoding
Generative retrieval systems output item IDs from language models to recommend products, videos, or content. However, these systems must respect business constraints: items must be fresh, appropriate for user age, in stock, or in specific categories. Traditional prefix-tree (trie) based constrained decoding incurs massive latency penalties on GPUs because tree traversal is inherently sequential—each token generation requires tree navigation, incompatible with hardware vectorization.
STATIC (Sparse Transition Matrix-Accelerated Trie Index for Constrained Decoding) solves this by flattening trie structures into Compressed Sparse Row (CSR) matrices. This converts sequential tree traversal into vectorized sparse matrix operations that GPUs can parallelize, achieving production-scale performance with negligible overhead.
Core Concept
The core insight: prefix trees are transition structures that can be represented as sparse matrices. Rather than traversing a tree sequentially, convert it to a static sparse matrix where each row represents valid next-token constraints. During generation, each step becomes a sparse matrix multiplication instead of tree node lookup.
This enables hardware-native parallelization: GPUs excel at sparse matrix operations but struggle with tree traversal. By changing the representation, STATIC unlocks GPU efficiency for constrained decoding.
Architecture Overview
- Trie to CSR Conversion: Flatten constraint tree into static sparse matrix
- Sparse Matrix Ops: Use GPU-native sparse-matrix-multiply for token filtering
- Stateless Decoding: No tree state needed during generation; matrix row index suffices
- Minimal Overhead: Single sparse operation per token (0.033 ms on GPU)
- Production Deployment: Tested at scale on video recommendation platform
Implementation Steps
STATIC involves converting constraint tries to sparse matrices and using them during inference. Here's how to implement it:
Build a constraint trie from a set of allowed item IDs. In practice, this represents catalog items you can recommend:
import numpy as np
from scipy.sparse import csr_matrix
class TrieToSparseMatrix:
def __init__(self, vocab_size):
.vocab_size = vocab_size
.trie = {}
.row_idx =
.rows = []
.cols = []
.data = []
():
tokenized = .tokenize(item_id_str)
.insert_sequence(tokenized)
():
current = .trie
token token_sequence:
token current:
current[token] = {}
current = current[token]
current[] =
():
[(c) c item_id_str[:]]
():
state_to_idx = {}
state_idx =
queue = [(, .trie)]
state_to_idx[] =
state_idx =
queue:
state_name, state_dict = queue.pop()
current_idx = state_to_idx[state_name]
token, next_state state_dict.items():
token == :
(next_state, ):
state_name_next =
state_name_next state_to_idx:
state_to_idx[state_name_next] = state_idx
state_idx +=
queue.append((state_name_next, next_state))
next_idx = state_to_idx[state_name_next]
.rows.append(current_idx)
.cols.append(token)
.data.append(next_idx)
csr = csr_matrix(
(.data, (.rows, .cols)),
shape=(state_idx, .vocab_size),
dtype=np.int32
)
csr, state_to_idx
():
[(c) % .vocab_size c text]