Training–Inference Consistent Segmented Execution for Long-Context LLMs

Xianpeng Shang, Jiang Li, Zehua Duo, Qianyi Cai, Xiangdong Su

TL;DR

This paper introduces a novel framework for training large language models (LLMs) that ensures consistency between how the model is trained and how it performs inference on long contexts. By aligning these two phases, the authors achieve comparable performance to full-context attention while significantly improving efficiency and scalability for extremely long sequences.

What Problem Does This Paper Address?

Transformer-based LLMs struggle with long contexts due to the quadratic computational and memory costs of full-context attention. To address this, many existing methods use "segmented execution" (processing text in chunks) only during inference, while still training with full-context attention. This mismatch between training and inference leads to performance degradation and instability when the model encounters long sequences in practice. The core problem is bridging this training-inference gap to enable efficient and stable long-context LLMs.

Methods

The authors propose a Training-Inference Consistent Segmented Execution framework. The key idea is to make segmented execution a shared assumption for both training and inference.

  • Segmented Execution: The input sequence is divided into segments. A fixed-size "carried KV tail" acts as the sole differentiable interface state passed between segments.
  • Training-Inference Alignment: To enforce consistency, gradients during training are restricted to propagate through only a limited number of past KV states (controlled by a parameter K, using Truncated Backpropagation Through Time or TBPTT). This ensures that the model learns to rely only on information available during inference.
  • Forward-Only Retrieval: For accessing information beyond the immediate carried KV state, a "forward-only" retrieval mechanism is used. This mechanism accesses past KV states but does not involve gradients, thus not creating a training-inference mismatch.
  • Head- and Layer-Sparse Architecture: The model architecture is optimized by using "local heads" for within-segment attention and "long-range heads" for cross-segment information. This sparsity is motivated by observations that only a subset of heads are crucial for long-context behavior.

Datasets

  • PG19: Used for language modeling perplexity evaluation to assess stability and degradation patterns across varying context lengths.
  • LongBench-E: A benchmark for evaluating long-context LLMs on tasks requiring cross-segment information integration and long-range dependency modeling.
  • RULER (CWE and FWE tasks): Used for controlled length-scaling analyses to stress-test robustness and dependency modeling behavior.

Key Insights and Ideas

  • The Mismatch is the Bottleneck: The most significant insight is that the discrepancy between training with full-context attention and inference with segmented execution is a primary cause of performance issues in long-context LLMs.
  • TBPTT for Exact Gradients: By carefully bounding the gradient propagation with TBPTT (specifically, K=1 is shown to be effective), the authors can compute the exact gradient of an inference-consistent objective, avoiding approximations that can lead to instability.
  • Forward-Only Retrieval is Key: The "forward-only" nature of the long-range retrieval mechanism is crucial. It allows access to historical information without introducing new gradient paths that would break training-inference alignment.
  • Sparsity is Efficient and Effective: Using sparse attention heads and layers for long-range interactions significantly improves efficiency without sacrificing performance, suggesting that not all parts of the model need to handle long contexts.

Key Results

  • Comparable Performance: The proposed method achieves performance comparable to full-context attention on various long-context benchmarks.
  • Improved Efficiency: The approach offers competitive latency-memory trade-offs against strong inference-efficient baselines.
  • Substantial Scalability: At very long context lengths (e.g., 128K tokens), the method demonstrates significant improvements, with approximately 6x lower peak prefill memory compared to full-context attention with FlashAttention.
  • Smoother Perplexity Trends: The model exhibits a smoother perplexity increase across evaluated context lengths, avoiding abrupt spikes seen in other methods, especially when evaluation lengths exceed training lengths.
  • Robustness to Length Generalization: The method shows stronger robustness to context length scaling, retaining non-zero accuracy even when context lengths far exceed training lengths, unlike many baselines that collapse.

Limitations and Future Work

  • Hyperparameter Sensitivity: While K=1 is found to be optimal for TBPTT, the paper notes that increasing it to K=2 did not improve performance and could slightly degrade it, suggesting a sensitivity to truncation depth.
  • Moderate Local State Capacity: Ablation studies suggest that while a moderate local KV state capacity is beneficial, overly large capacities might not uniformly benefit all tasks and could even lead to non-monotonic gains.
  • Future Work: The paper implicitly suggests further exploration into optimal head grouping strategies for long-range modules and investigating the precise balance between local and long-range capacities for different task types. The impact statement also highlights the need for responsible deployment practices due to the potential for misuse of more capable LLMs.

Summary updated

BibTeX
@inproceedings{shang2026traininginference,
  author = {Xianpeng Shang and Jiang Li and Zehua Duo and Qianyi Cai and Xiangdong Su},
  title = {{Training–Inference Consistent Segmented Execution for Long-Context LLMs}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=PoRigyDOcC},
}
Figure