Source-linked AI summary

LongSpec: Long-Context Lossless Speculative Decoding with Efficient Drafting and Verification

Penghui Yang, Cunxiao Du, Fengzhuo Zhang, Haonan Wang, Tianyu Pang, Chao Du, Bo An

arXiv:2502.17421v4cs.CLcs.AIcs.LG

TL;DR

Long-context inference is important, but speculative decoding methods are often trained on short sequences and face memory, training, and tree-attention challenges. LONGSPEC introduces a constant-memory draft model, Anchor-Offset Indices, and Hybrid Tree Attention. Across five evaluated datasets, it outperforms other approaches on summarization and code completion, with up to 3.26× speedup on code tasks.

  • Problem

    Many state-of-the-art speculative decoding methods are trained on sequences shorter than 4k tokens, while long-context inference is increasingly important for applications such as LLM agents.

    Source: §1 · p. 1
  • Method

    LONGSPEC combines a constant-memory draft model, Anchor-Offset Indices for training, and Hybrid Tree Attention for efficient tree verification.

    Source: §3.2 · p. 4, §5 · p. 9
  • Results

    Across five evaluated datasets, LONGSPEC outperforms other approaches on summarization and code completion, with up to 3.26× speedup on code tasks.

    Source: §4.2 · p. 6
  • Takeaways & Limitations

    The results support LONGSPEC’s robustness and generalizability for long-text generation tasks.

    Source: §4.2 · p. 6

    MagicDec is not suitable for long-output reasoning scenarios with relatively short prefixes, where its draft can degrade into the target model and fail to accelerate decoding.

    Source: §4.4 · p. 9

Abstract

from arXiv · show

As Large Language Models (LLMs) can now process extremely long contexts, efficient inference over these extended inputs has become increasingly important, especially for emerging applications like LLM agents that highly depend on this capability. Speculative decoding (SD) offers a promising lossless acceleration technique compared to lossy alternatives such as quantization and model cascades. However, most state-of-the-art SD methods are trained on short texts (typically fewer than 4k tokens), making them unsuitable for long-context scenarios. Specifically, adapting these methods to long contexts presents three key challenges: (1) the excessive memory demands posed by draft models due to large Key-Value (KV) cache; (2) performance degradation resulting from the mismatch between short-context training and long-context inference; and (3) inefficiencies in tree attention mechanisms when managing long token sequences. This work introduces LongSpec, a framework that addresses these challenges through three core innovations: a memory-efficient draft model with a constant-sized KV cache; novel position indices that mitigate the training-inference mismatch; and an attention aggregation strategy that combines fast prefix computation with standard tree attention to enable efficient decoding. Experimental results confirm the effectiveness of LongSpec, achieving up to a 3.26x speedup over strong Flash Attention baselines across five long-context understanding datasets, as well as a 2.25x reduction in wall-clock time on the AIME24 long reasoning task with the QwQ model, demonstrating significant latency improvements for long-context applications. The code is available at https://github.com/sail-sg/LongSpec.

1 Introduction

Long-context inference makes decoding latency a bottleneck, while short-context speculative decoding methods face memory, training, and verification challenges when adapted to longer sequences. LONGSPEC addresses these challenges with a constant-memory draft model, Anchor-Offset Indices, and Hybrid Tree Attention.

1 Introduction

  • Short-context speculative decoding methods typically train on sequences under 4k tokens, while long-context inference is increasingly important for agents and reasoning tasks.The paper contrasts lossless speculative decoding with alternatives such as quantization and model cascades, which may compromise output quality.Source: §1 · p. 1
  • Some long-context methods draft with the full target model and compressed KV cache, but its computational cost can limit speed and underperform short-context methods.The draft model’s weight in low-batch settings can outweigh speculative benefits.Source: §1 · p. 2

Why cannot SoTA SD methods for short contexts

  • Long-context speculative decoding faces three challenges: draft-model KV-cache growth, scarce long-sequence training data with fixed target RoPE bases, and inefficient tree attention.Conventional RoPE-base extension is not directly applicable because the draft model must match the target model’s fixed base.Source: Why cannot SoTA SD methods for short… · p. 2
  • LONGSPEC pairs a constant-memory draft architecture with Anchor-Offset Indices, enabling short-sequence-trained drafts to generalize to longer inference contexts.Hybrid Tree Attention is the third innovation, addressing inefficient tree-attention computation.Source: Why cannot SoTA SD methods for short… · p. 2
  • Across five long-context understanding datasets, LONGSPEC achieves up to 3.26× speedup over strong baselines using Flash Attention2.The paper also reports up to 7× speedup over common baselines using HuggingFace implementation.Source: Why cannot SoTA SD methods for short… · p. 2

2 Related Work

Related work spans smaller-model, specialized-draft, target-model, and retrieval-based speculative decoding. Long-context methods remain few, and several rely on computationally intensive target-model drafts.

2 Related Work

3 Methodology

LONGSPEC combines a lightweight draft model, training strategies for long-context positions and inference visibility, and a hybrid attention computation for tree verification.

3.1 Memory-Efficient Architecture

  • The draft model uses sliding-window self-attention for local context and target-model KV-cache cross-attention for long-range information, keeping additional memory constant with context length.The self-attention window is set to 512; the design also shares the embedding layer and LM head with the target model.Source: §3.1 · p. 3, Figure 2 · p. 4

3.2 Effective Training Regimes

  • Anchor-Offset Indices reserve positions 0–3 and assign later tokens consecutive indices from a random offset, so short-context training covers larger positions while limiting target-model loss increase to about 0.001.Flash Noisy Training simulates inference-time KV visibility constraints and increases acceptance length by 14.7% versus training without it.Source: §3.2 · pp. 4–5

3.3 Fast Tree Attention

  • Tree attention’s prefix-tree masks have limited compatibility with optimized long-context kernels, so the hybrid design applies masking only to the small set of speculative tokens.Earlier approaches that apply masks to the full attention matrix rely on PyTorch eager execution and slow as sequence length increases.Source: §3.3 · p. 5
  • Hybrid Tree Attention separates cached prefix KV pairs, computed with Flash Attention, from speculative-token KV pairs, computed with a custom masked Triton kernel.The two attention outputs are aggregated using a log-sum-exp trick, retaining parallel branch verification without compromising correctness.Source: §3.3 · pp. 5–6

4 Experiments

LongSpec is evaluated on long-context understanding and math-reasoning tasks, with ablations examining its position indices and hybrid attention. It reports speedups across task settings, including long reasoning and batched throughput.

4.1 Settings

  • Evaluation spans five long-context understanding datasets and four math-reasoning datasets; drafts are pretrained, long-context trained, then fine-tuned on a long-context SFT dataset.The study compares against the target model, PLD, and MagicDec, using walltime speedup relative to vanilla autoregressive decoding and average acceptance length τ.Source: §4.1 · p. 6

4.2 Main Results

  • At T = 0, LongSpec reaches up to 2.67× speedup on summarization and 3.26× on code completion; at T = 1, it achieves around 2.5× speedup.It outperforms the other approaches on both task types at T = 0 and maintains a substantial lead over MagicDec at T = 1.Source: §4.2 · p. 6, Table 1 · p. 7, Figure 3 · p. 7

4.3 Ablation Studies

  • 3.93× faster to reach the same loss level, models with Anchor-Offset Indices also achieve higher output speed and larger acceptance length on Multi-News and RepoBench-P.The loss comparison is on real long-context data; the performance comparison is with and without the indices.Source: Figure 4 · p. 8, §4.3 · p. 8, Table 2 · p. 8
  • 49.92 ms to 12.54 ms, Hybrid Tree Attention reduces target-model attention latency by approximately 75% while draft-forward and target-FFN times remain comparable.The verification-step time difference is minimal.Source: Figure 5 · p. 8, §4.3 · p. 8

4.4 Long Reasoning Acceleration

  • On QwQ-32B across four math-reasoning datasets, LongSpec achieves about 2.34× higher generation speed than the baseline and averages 3.81 accepted tokens.Evaluation uses a maximum output length of 32k tokens; MagicDec's draft degrades to the target model with a relatively short prefix in long-reasoning tasks, failing to accelerate.Source: Table 3 · p. 8, §4.4 · p. 9

4.5 Throughput

  • At batch size 8, LONGSPEC reaches 561.32 tokens/s, versus MagicDec's 310.58 tokens/s and Vanilla's 286.96 tokens/s on RepoBench-P.LONGSPEC maintains higher throughput than both baselines across all tested batch sizes.Source: Figure 6 · p. 9, §4.5 · p. 9

5 Conclusion

LongSpec targets lossless speculative decoding for long contexts with a constant-memory draft model, Anchor-Offset Indices, and Hybrid Tree Attention. Experiments demonstrate effectiveness on long-context understanding and long reasoning tasks.

5 Conclusion

  • LongSpec combines a constant-memory draft model, Anchor-Offset Indices, and Hybrid Tree Attention to address long-context decoding challenges.The draft model combines sliding-window self-attention with cache-free cross-attention; the position indices train large positional indices on short sequences, and Hybrid Tree Attention integrates tree decoding with Flash Attention.Source: §5 · p. 9
  • LongSpec is designed to improve lossless speculative decoding in long-context scenarios.The framework addresses memory overhead, inadequate training for large position indices, and inefficient tree attention computation.Source: §5 · p. 9
  • Experiments demonstrate LongSpec’s effectiveness on long-context understanding and real-world long reasoning tasks.The authors identify long-context-specific speculative decoding as important for efficient large-scale language-model inference.Source: §5 · p. 9

A Related Work about Lossy Speculative Decoding

The related work describes lossy speculative decoding methods that trade some degree of exactness for speed or cost-quality improvements. Approaches include cascade-style deferral, auxiliary models, retrieval, and partial-cache strategies.

A Related Work about Lossy Speculative Decoding

  • BiLD and speculative cascading use refinement or deferral strategies to seek speedups or improved cost-quality trade-offs, while accepting quality degradation or combining cascade rules with speculation.BiLD uses a small autoregressive model and occasionally invokes a larger model nonautoregressively; speculative cascading integrates cascade-style deferral rules with speculative execution.Source: A Related Work about Lossy Speculative… · p. 13
  • MTAD approximates a larger model’s multi-token joint distribution with a smaller auxiliary model, accepting bounded approximation error.A trained judge module can recognize valid continuations without perfect target-model alignment, boosting acceptance rates and speed.Source: A Related Work about Lossy Speculative… · p. 13
  • RSD uses a process reward model to guide target-model invocation, introducing controlled bias toward high-reward outputs to optimize cost-quality trade-offs.RAPID instead uses a retrieval-augmented-generation approach on shortened contexts as its drafter.Source: A Related Work about Lossy Speculative… · p. 13
  • TokenSwift combines LLMs with partial KV cache and N-gram tables for generation up to 100k tokens, reducing computation time from hours to minutes.The passage describes it as accelerating ultra-long sequence generation.Source: A Related Work about Lossy Speculative… · p. 13

B The Intuition of Why KV Cache Can Help

A model’s KV cache stores contextual information accumulated from previous tokens and contributes to next-token prediction alongside embeddings and parameters. LongSpec’s draft model shares the target’s input embeddings and KV cache, leaving model parameters as the remaining prediction difference.

B The Intuition of Why KV Cache Can Help

  • The KV cache stores context accumulated from earlier tokens and contributes to next-token prediction alongside input embeddings and model parameters.The target model uses all three components when predicting the next token.Source: B The Intuition of Why KV Cache Can Help · p. 13
  • LongSpec’s draft model shares the target model’s input embeddings and KV cache, leaving model parameters as the remaining difference between their predictions.The shared cache removes discrepancies caused by differing contextual representations and aligns draft predictions more closely with the target’s.Source: B The Intuition of Why KV Cache Can Help · pp. 13–14

C Correctness for Attention Aggregation

The correctness argument treats each query row independently, then applies the result to the full query matrix. It expresses attention over the merged cached and speculative key-value pairs by splitting their logits and tracking log-sum-exp terms for aggregation.

C Correctness for Attention Aggregation

  • The proof reduces the matrix-level correctness claim to showing that each individual query’s output meets the requirements.Because each row of Q represents a separate query, the proof can treat its key-value inputs as already subject to the tree mask.Source: C Correctness for Attention Aggregation · p. 14
  • Merged attention attends over stacked speculative and cached key-value pairs, whose logits are split into corresponding sub-logit matrices.Rows of Zspecs and Zcache give dot products for the same query against speculative and cached keys, respectively.Source: C Correctness for Attention Aggregation · p. 14
  • The aggregation tracks log-sum-exp values for the separate sub-logit sets to combine partial attention outputs.The passage introduces the log-sum-exp of merged attention as part of the aggregation argument.Source: C Correctness for Attention Aggregation · p. 14

D Experiments Details

The experiments use distinct training setups for short- and long-context targets and a dynamic beam-search strategy designed to avoid unnecessary KV-cache movement. Inference runs in float16 on one A100 GPU.

  • Training uses eight A100 80GB GPUs, with ZeRO-1 for short-context 7B, 8B, and 13B targets and ZeRO-3 for long-context targets and all 33B settings.Source: D Experiments Details · p. 15
  • Draft models are optimized with cross-entropy while target-model parameters remain frozen, using fused linear-and-cross-entropy computation to alleviate peak VRAM use.The fused loss computes the LM head and softmax together.Source: D Experiments Details · p. 15
  • Short-context SlimPajama-6B training uses batch size 2048 and maximum learning rate 5e-4; long-context training uses batch size 256 and maximum learning rate 5e-6.Both use a cosine schedule and AdamW, and each dataset receives one training epoch.Source: D Experiments Details · p. 15
  • The main computational cost is forwarding the target model to obtain its KV cache; pre-stored context-caching KV data can serve as training data and accelerate training.Source: D Experiments Details · p. 15
  • Dynamic beam search halts descendant computation on low-likelihood branches instead of discarding nodes, avoiding KV-cache movement while allowing draft sequences of unequal lengths.Beam width is [4, 16, 16, 16, 16] across speculation steps.Source: D Experiments Details · p. 15

E Experimental Results of EAGLE and Token Recycling on Long-Context Speculative Decoding

LongSpec with hybrid tree attention reaches about 100 tokens/s across models and datasets, outperforming the compared speculative-decoding methods. Flash Attention raises vanilla decoding speed, while EAGLE and Token Recycling offer different acceptance-length and speed trade-offs.

  • Vanilla HF decoding runs at 14–30 tokens/s across five datasets, while switching to Flash Attention raises speed to about 50 tokens/s, more than 2.5× faster.Source: E Experimental Results of EAGLE and… · p. 15
  • Token Recycling reaches an average acceptance length around 3 and provides moderate acceleration on many tasks, but consistently underperforms LongSpec.Source: E Experimental Results of EAGLE and… · p. 15
  • EAGLE reaches an acceptance length around 2 and 26–40 tokens/s, a 30–50% speedup over Vanilla HF, but remains slower than Vanilla FA in every setting.Source: E Experimental Results of EAGLE and… · p. 15
  • About 100 tokens/s: LongSpec with hybrid tree attention achieves this decoding speed across all models and datasets, exceeding the compared methods.Vanilla Flash Attention reaches about 50 tokens/s, while EAGLE remains below it in every setting and Token Recycling consistently underperforms LongSpec.Source: E Experimental Results of EAGLE and… · p. 15

F Performance Analysis with Varying Prefill Lengths

The prefill-length analysis reports stable generation behavior and modest latency increases as context grows. The accompanying pseudocode covers Anchor-Offset Indexing and Flash Noisy Training.

F Performance Analysis with Varying Prefill Lengths

  • Generation speed and average acceptance length remain stable across prefill ranges, with only a small speed drop in the 25k–32k range.The authors report that draft quality is unaffected by prefill length.Source: F Performance Analysis with Varying… · p. 16
  • Draft time rises from 8.91 ms to 9.25 ms and target time from 25.63 ms to 30.89 ms across the shortest-to-longest contexts, while verify time increases from 6.18 ms to 6.28 ms.Source: F Performance Analysis with Varying… · p. 16

G Pseudo Code

  • Flash Noisy Training pseudocode randomly selects 1–4 tokens to drop, slices the inputs and cache, then pads the output to the original query length.The pseudocode identifies the query, key-cache, and value-cache inputs and returns the output projection.Source: G Pseudo Code · pp. 16–17
  • Anchor-Offset Indexing applies an offset after the first four anchor positions; for N = 128, the example offset is 16257.The pseudocode takes sequence length, maximum length, and query states as inputs and outputs query states with RoPE applied using modified indices.Source: G Pseudo Code · p. 16

H Case Study

The case-study passages present illustrative GovReport examples for LongChat-7B, with accepted draft tokens marked in blue. They include example report summaries on opioids, railroad benefits, the Congressional Gold Medal, and DHS appropriations.

  • The examples are from GovReport on LongChat-7B, where blue marks draft tokens accepted by the target model; the complete answer is omitted.Source: H Case Study · p. 17
  • The opioid report summary covers medical uses, risks including dependence, addiction, overdose, and diversion, and the need for proper monitoring and continued treatment research.Source: H Case Study · p. 17
  • The railroad report summary describes the Railroad Retirement Board’s RRA and RUIA benefit programs and trends in railroad employment.Source: H Case Study · p. 18
  • The Congressional Gold Medal summary describes the award’s purpose, legislative process, design, and use to honor individuals and groups.Source: H Case Study · p. 18
  • The DHS report summary reviews FY2019 appropriations, the budget process, and debates including border security, cybersecurity, and disaster response.Source: H Case Study · p. 19
Loading 2502.17421v4…