Abstract
Fine-tuning Large Language Models (LLMs) often involves a frustrating trial-and-error process to avoid Out-of-Memory (OOM) errors. Existing memory estimation tools fail to account for the complexities of modern LLM training, such as mixed-precision states, chunk-based memory management, and distributed overheads. This paper introduces LLMem, a novel framework that accurately predicts peak GPU memory usage with a remarkably low error rate of ~3%. By modeling transformer blocks and the language modeling head separately and accounting for distributed strategies like ZeRO-3 and Tensor Parallelism, LLMem enables engineers to select optimal training configurations and avoid costly mid-iteration crashes.
Introduction
Fine-tuning an LLM is frequently a game of chance. Engineers typically guess a batch size and a distributed strategy (e.g., Data Parallelism vs. Tensor Parallelism), wait for the model to load, and hope it doesn't crash with a CUDA Out of Memory error. Existing memory estimators, such as DNNMem, were designed for older, smaller models and fail for modern LLMs because they do not account for mixed-precision training, chunk-based memory management used by frameworks like PyTorch or Colossal-AI, and the temporary "all-gather" buffers required during distributed training.
Methodology
LLMem models memory consumption by breaking the LLM down into two distinct functional parts: the Transformer blocks, which utilize chunk-based management, and the Language Modeling (LM) Head, which consumes massive memory due to large vocabulary sizes but is not chunked.
Key Technical Contributions
- Mixed-Precision Modeling: LLMem tracks the memory footprint of
fp16parameters and gradients alongside thefp32momentum and variance buffers used by optimizers like ADAM. - Distributed Strategy Equations: The framework provides mathematical formulations for Advanced Data Parallelism (ADP/ZeRO-3) and Tensor Parallelism (TP), specifically accounting for the temporary "all-gather" buffers ($m_{back}^{tp}$) created during the backward pass.
- Decision Engine: A heuristic algorithm iterates through possible configurations to find the "sweet spot"—the method that allows for the largest batch size and highest throughput without exceeding GPU capacity.
Experimental Setup
The researchers validated LLMem using a wide array of popular pre-trained models, including OPT (125M to 2.7B), BLOOM (560M to 7B), CodeGen, BioGPT-Large, GPT-BigCode, GPT-Neo, and LLaMA-7B. Fine-tuning was performed on the Alpaca dataset (52K instruction-following samples) using NVIDIA Tesla V100 GPUs (16GB).
Key Results
LLMem demonstrated significantly higher accuracy than previous state-of-the-art tools. On a single GPU, LLMem achieved an error rate of only 1.6%, compared to 42.6% for DNNMem. When scaling to multiple GPUs using complex strategies like ZeRO-3 or hybrid DP+TP, the average error rate remained low at 3.0%.
Crucially, LLMem successfully predicted OOM scenarios for models like CodeGen-2B and LLaMA-7B on 16GB V100s, where other tools incorrectly suggested they would fit. Furthermore, LLMem correctly identified the fastest training method for various model sizes, often choosing Data Parallelism for smaller models and automatically switching to Tensor Parallelism as model size increased.
Discussion and Limitations
LLMem's primary insight is that the LM Head is a "special citizen" that doesn't follow standard chunking rules and can be a primary driver of OOM errors. Additionally, it is the first tool to explicitly model the temporary memory spikes during the TP all-gather process.
While highly effective for transformer-based decoder models, future work may be needed to adapt the logic for encoder-decoder architectures (like T5). Additionally, while the logic is general, newer hardware architectures (A100/H100) with different memory allocator behaviors and interconnect speeds might require slight adjustments to the optimal method selection logic.
Summary updated