Training Long-Context LLMs Efficiently via Chunk-wise Optimization

Wenhao Li, Yuxin Zhang, Gen Luo, Daohai Yu, Rongrong Ji

Abstract

Training Large Language Models (LLMs) with long contexts is constrained by the linear growth of activation memory relative to sequence length. We present Sequential Chunk-wise Optimization (SeCO) and Sparse Chunk-wise Optimization (SpaCO), two training paradigms that partition long sequences into chunks to achieve constant activation memory. SeCO utilizes sequence-dimensional gradient checkpointing to reconstruct the computational graph chunk-by-chunk. SpaCO extends this with stochastic chunk selection and a mathematical compensation factor to maintain unbiased gradients while significantly reducing training time. Our methods enable fine-tuning an 8B-parameter model with 16K context on a single 24GB GPU, achieving a 16x context extension and 10x speedup over standard offloading techniques.

Introduction

Training LLMs with long contexts is notoriously resource-intensive. Even with optimizations like FlashAttention, two major bottlenecks remain: memory constraints and time overhead. The memory required to store forward activations—the intermediate data needed for backpropagation—grows linearly with sequence length. This usually limits fine-tuning of 8B models to roughly 1K tokens on standard 24GB consumer GPUs. Furthermore, the quadratic complexity of attention mechanisms makes long-sequence training prohibitively slow. Existing solutions often compromise gradient accuracy or create communication bottlenecks. This paper introduces SeCO and SpaCO to overcome these hurdles without sacrificing learning integrity.

Methodology

Sequential Chunk-wise Optimization (SeCO)

SeCO introduces the concept of sequence-dimensional gradient checkpointing. Unlike traditional methods that partition the model by layers, SeCO splits the input sequence into $k$ manageable chunks.

Overview of SeCO showing the full computational graph and the sequential reconstruction process across four steps.

In the first pass, the model computes Key-Value (KV) caches for all chunks in inference mode, which requires minimal memory. Backpropagation is then performed in reverse order, reconstructing the computational graph for only one chunk at a time. This approach ensures that activation memory remains constant relative to the number of chunks, effectively breaking the linear memory wall.

Sparse Chunk-wise Optimization (SpaCO)

While SeCO optimizes memory, it still processes every chunk, leading to significant time overhead. SpaCO is a stochastic variant that randomly selects a subset of $t$ chunks for backpropagation in each iteration. To prevent gradient bias, the authors discovered that the gradient chain in Transformers is bounded by model depth rather than sequence length. They introduced a compensation factor $(k/t)^p$ to scale the remaining gradients, ensuring the total gradient estimate remains unbiased while skipping up to 7/8ths of the backpropagation work.

Experimental Setup and Results

The methods were evaluated using the PG19 dataset and the LLaMA3-8B model. Key findings include:

  • Massive Memory Savings: On a single RTX 3090 (24GB VRAM), SeCO expanded the maximum sequence length for Llama-3-8B from 1K to 16K tokens—a 16x improvement.
  • Training Speed: SpaCO achieved up to 3x speedup over SeCO. Compared to DeepSpeed's ZeRO3-offload, SeCO was approximately 10x faster by avoiding the CPU-GPU communication bottleneck.
  • Convergence: SpaCO maintains high accuracy; even at a 1/8 sparsity ratio, the increase in language modeling error remained below 0.1, demonstrating that the stochastic approach does not compromise the learning process.

Discussion and Future Work

While SeCO and SpaCO significantly improve efficiency, the underlying attention mechanism remains quadratic ($O(L^2)$). Additionally, SpaCO introduces gradient variance, which may necessitate larger batch sizes or more training epochs to reach the same level of smoothness as exact gradient training. Future research could explore combining these chunk-wise strategies with 4-bit quantization or advanced sparse attention patterns to further push the boundaries of long-context training on consumer hardware.

Summary updated

BibTeX
@inproceedings{li2025training,
  author = {Wenhao Li and Yuxin Zhang and Gen Luo and Daohai Yu and Rongrong Ji},
  title = {{Training Long-Context LLMs Efficiently via Chunk-wise Optimization}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure