HyperAI超神経
Back to Headlines

NVIDIA NeMo Framework Optimizes Training of Long-Context LLMs to Handle Millions of Tokens Efficiently

6日前

The evolution of large language models (LLMs) has seen significant improvements in their ability to process and generate text, particularly in handling long context lengths. Extended context lengths, or the number of tokens a model can process in a single input, enable LLMs to handle more complex tasks such as video input processing, summarizing lengthy documents, maintaining coherence in multi-turn dialogues, and chain-of-thought reasoning. These capabilities are crucial for tasks requiring detailed and temporally coherent information, such as video generation, legal document analysis, low-resource language translation, and AI assistant functionalities. Need for Extended Context Lengths and Associated Challenges Extended context lengths are essential for advanced multimodal applications. For instance, processing long-form video content involves attending to thousands of frames simultaneously while maintaining temporal coherence. Similarly, models like DeepSeek-R1, with a context length of over 128K tokens, and Llama 4, which has pushed the boundaries to over 10 million tokens, rely on extensive contexts to solve multistep problems and avoid truncating critical logical pathways, which can lead to errors. However, training LLMs with extended context lengths presents substantial technical challenges, particularly in memory management. Transformer-based LLMs scale computationally with (O(n^2)) complexity as sequence lengths increase (reduced to (O(n)) if using flash attention). This quadratic growth in computational requirements makes training ultra-long context models extremely resource-intensive and costly. Enabling Long-Context Training with NVIDIA NeMo Framework Activation Recomputation One effective technique to manage memory is activation recomputation. During training, intermediate activations are typically stored in memory to facilitate backpropagation. However, this can rapidly exceed the memory capacity of even the largest GPUs. Activation recomputation selectively checkpoints only a subset of these activations (e.g., inputs to each transformer layer). When computing gradients during the backward pass, the necessary activations are recomputed on-the-fly. This significantly reduces memory footprint, allowing ultra-long sequences and large batch sizes to fit into GPU memory. As context length increases, activation memory can surpass the memory needed for model weights and optimizer states, making recomputation critical for cost efficiency and scalability. Context Parallelism Another powerful technique is context parallelism (CP), which splits the sequence dimension across multiple GPUs. Unlike sequence parallelism (SP), which only splits sequences for a few select layers, CP splits sequences for all layers, with communication costs minimized by overlapping them with compute. CP stores the key-value (KV) pairs for its local sequence chunk on each GPU during the forward pass. These KV pairs are gathered during the backward pass, ensuring efficient memory usage. CP leverages optimized point-to-point communications within a ring topology, which can be further enhanced by using multi-head query attention (MQA) and grouped query attention (GQA) to reduce communication volumes. For example, in a configuration with four GPUs, CP can dynamically exchange KV pairs between GPUs, as shown in the transformer layer running with TP2CP2 (Tensor Parallelism 2, Context Parallelism 2) setup. CP benchmarks demonstrate its effectiveness, showing higher teraflops starting from 32K sequence length and beyond. At a sequence length of 1 million tokens, CP becomes mandatory to run models, with minimal overhead. Activation Offloading CPU offloading is another technique to manage GPU memory efficiently. By offloading intermediate activations and inactive weights to CPU memory, NeMo Framework can reduce peak GPU memory usage. This is particularly useful when training very deep models, as it allows for more layers to be processed without memory constraints. NeMo Framework supports offloading at the transformer layer level, dynamically offloading and reloading activations as needed during the forward and backward passes. Conclusion Implementing techniques such as activation recomputation, context parallelism, and activation offloading is crucial for optimizing long-context training in LLMs. However, the best approach often depends on the specific model architecture and hardware choices. NVIDIA NeMo Framework, a GPU-accelerated training framework, provides tested recipes for training long-context models efficiently. These recipes are designed for various models, including Llama 3 8B and 70B, Mixtral 8x7B, and Nemotron 4 15B and 22B, with context windows ranging from 16K to 128K tokens. NeMo also supports extending the context window from pre-trained checkpoints, making it a versatile and powerful tool for developers and researchers. Industry Insights and Company Profiles Industry insiders highlight the transformative potential of long-context LLMs, especially in fields like healthcare, finance, and media, where retaining comprehensive context is vital for accurate analysis and decision-making. NVIDIA, a leader in GPU technology, continues to push the boundaries of deep learning with frameworks like NeMo, offering cutting-edge solutions for memory management and computational efficiency. The availability of tested recipes in NeMo Framework ensures that developers can leverage these optimizations easily, accelerating the development and deployment of advanced LLMs.

Related Links