---
title: Trie-Based Parallel Decoding
url: https://www.emergentmind.com/topics/trie-based-parallel-decoding
type: topic
---

# Trie-Based Parallel Decoding

Trie-based parallel decoding is a method for efficient beam search in Transformer-based sequence generation that addresses the high memory footprint of conventional batch-based approaches by exploiting prefix sharing among candidate sequences. The central innovation is to represent all active beam hypotheses in a prefix tree (trie), allowing key-value (KV) caches for attention to be shared across beams with common prefixes. This results in substantial reductions in GPU memory usage during sequence decoding, especially in large-scale language model deployments or memory-constrained environments, without significant compromise in throughput or output quality [2502.00085].

## 1. Formal Structure and Core Principles

Let \(B\) denote the beam size, \(L\) the generation length, and \(D\) the size (in floating-point elements) of the KV cache per token. The trie structure \(\mathcal{T} = (N, E, \ell)\) comprises a set of nodes \(N\), directed edges \(E \subseteq N\times N\) labeled by tokens via \(\ell: E \rightarrow \mathcal{V}\), where \(\mathcal{V}\) is the vocabulary. The root node encodes the initial prompt. A directed path in the trie corresponds to a valid partial hypothesis (sequence of tokens), associating each beam’s current prefix with a unique trie node. Formally, at step \(i\), a bijection \(\phi_i:\{1,\ldots,B\} \to L_i \subseteq N\) maps each beam to its current trie node (which spells out the beam's prefix).

This data structure allows the sharing of decoder state: any beams that traverse the same path up to a given prefix also share the corresponding KV cache, as their forward computations up to that token sequence are identical.

## 2. Algorithmic Workflow

The decoding process (as formalized in the referenced paper) proceeds as follows:

1. **Trie and Cache Initialization:** The trie is initialized with the prompt as the root node. A mapping from trie nodes to KV caches is maintained on the GPU.
2. **Gather Distinct Prefixes:** At each step, collect active leaf nodes (distinct prefixes among all beams), denoted \(N_{\text{prefix}}\).
3. **Batch Forward Pass:** Construct a batch of KV caches associated with each distinct prefix and form an attention mask that constraints each prefix to attend within its branch.
4. **Compute Next Token Distributions:** For each distinct prefix, the model predicts logits for the next token. The log-probabilities are used to enumerate all expanded candidates.
5. **Beam Candidate Selection:** For each node, select the top-\(B\) token expansions, then globally prune to the top \(B\) hypotheses across all nodes.
6. **Trie and Cache Update:** New trie nodes and KV-caches are created only for surviving expansions. Cache storage is thus limited to nodes on active paths.
7. **Garbage Collection:** At prescribed intervals \(g\), subtrees not reachable from surviving leaves are pruned, and their associated KV caches are deleted to free memory.

Core data structures and routines include dictionaries for trie nodes to caches, functions for constructing attention masks respecting the trie topology, and pruning mechanisms for memory management.

## 3. Memory Complexity and Sharing Efficiency

For standard batch-based beam search, each of the \(B\) beams maintains its own full KV cache, yielding total memory consumption:
\[
M_{\text{batch}} = B \times L \times D
\]

In the trie-based approach, only unique prefixes are cached:
\[
M_{\text{trie}} \approx \overline{N_{\mathrm{prefix}}} \times L \times D
\]
where \(\overline{N_{\mathrm{prefix}}}\) is the average number of distinct active prefixes over decoding. Empirical results confirm \(\overline{N_{\mathrm{prefix}}} \ll B\) for typical tasks and beams, as many beams share dominating prefixes (especially at early or intermediate stages of generation).

The memory reduction ratio is
\[
\text{Reduction} = \frac{\overline{N_{\mathrm{prefix}}}}{B} \ll 1
\]
For instance, if on average only 3 distinct prefixes remain out of 15 beams, memory usage drops by 80%.

## 4. Computational Complexity and Parallelization

Classic sequential beam search issues one model call per beam per step, for total computational cost:
\[
O(B \times L \times T_{\mathrm{LLM}})
\]
where \(T_{\mathrm{LLM}}\) is the cost of a single-sequence forward pass.

Batch-based beam search compacts all \(B\) beams into a batch, reducing model calls but retaining high memory usage:
\[
\text{Compute cost per step} \approx O(B \times T_{\mathrm{LLM}})
\]

Trie-based parallel decoding maintains the same parallelism, but the effective batch size is only the number of unique prefixes \(N_{\mathrm{prefix}}\):
\[
\text{Compute cost per step} \approx O(N_{\mathrm{prefix}} \times T_{\mathrm{LLM}})
\]
Typically, \(N_{\mathrm{prefix}} \ll B\), so computational efficiency is at least as high as batch decoding, occasionally better due to reduced compute on shared prefixes. Practical results indicate near-ideal speedups proportional to \(\tfrac{B}{N_{\mathrm{prefix}}}\) [2502.00085].

## 5. Empirical Evaluation

Empirical results (reproducing key findings from Table 1 of the original work) show that trie-based decoding achieves drastic GPU memory savings per token, with minimal impact on throughput and output quality.

| Dataset    | Beam size | Method | Mem/Tok (MB) | Tok/Sec |
|:-----------|:----------|:-------|:-------------|:--------|
| CNN/DM     | 3         | Batch  | 4.06         | 7.50    |
| CNN/DM     | 3         | Trie   | 1.51         | 8.01    |
| CNN/DM     | 9         | Batch  | 11.99        | 5.25    |
| CNN/DM     | 9         | Trie   | 1.51         | 6.73    |
| HumanEval  | 3         | Batch  | 3.40         | 7.91    |
| HumanEval  | 3         | Trie   | 1.51         | 7.85    |
| HumanEval  | 15        | Batch  | 16.16        | 6.13    |
| HumanEval  | 15        | Trie   | 3.31         | 6.21    |

- Peak memory reduction of up to 92% per token.
- Throughput (Tok/Sec) remains within 5–10% of conventional batch-based decoding.
- Output quality (ROUGE-L for CNN/DM and pass@1 for HumanEval) is statistically indistinguishable (\(p>0.05\)).

*Figures 4–6 in the reference demonstrate that for increased output lengths, the memory gap between batch and trie approaches grows almost linearly for batch decoding, but sublinearly for the trie-based approach* [2502.00085].

## 6. Implementation and Practical Considerations

Integration with commonly used frameworks such as HuggingFace Transformers or Fairseq requires modification of the standard beam search loop to maintain the trie, per-node KV caches, and trie-aware attention masks. Caches can be stored in Python dictionaries keyed by trie node identifiers; batched concatenation and attention masking can leverage low-level tensor operations (e.g., `torch.cat`, `index_select`).

Special considerations include:

- **Attention Masking:** For deeply branching tries, attention masks may become prohibitively large (\([N_{\mathrm{prefix}}\times (t+i)]^2\)). Sharding or sparse masked attention (FlashAttention-style) may mitigate overhead.
- **Dynamic Batching:** In workloads with heterogeneous prompts or beam sizes, multiple tries may be maintained and interleaved for optimal GPU utilization.
- **Garbage Collection:** The interval parameter \(g\) balances memory fragmentation against compute overhead; practical values are in the range \(4\leq g\leq8\).
- **Positional Embeddings:** Correctly preserving positional encodings requires carrying original beam position IDs along the trie path, as detailed in Section 3.3 of the reference.

## 7. Context and Broader Implications

Trie-based parallel decoding demonstrates that the dominant source of memory inefficiency in Transformer beam search lies in the redundant storage of model state for beams with shared prefixes. By explicitly sharing state along trie branches, the approach delivers scalable decoding suitable for large beam sizes and long generations. This suggests potential for broader application in any autoregressive sequence model where prefix sharing occurs, particularly in scenarios with constrained GPU memory or high-throughput batch serving requirements.

Reference: [2502.00085] "Efficient Beam Search for Large Language Models Using Trie-Based Decoding." Chan et al., IJCAI 2025.

Source: https://www.emergentmind.com/topics/trie-based-parallel-decoding