Skip to content
AI.info

Training and optimization

Memory, Throughput, and Compute-Efficient Training

Profile activation memory, optimizer state, input pipelines, recomputation, kernel efficiency, sequence packing, and checkpointing tradeoffs.

By the end you can

Visual

Training memory has several owners

Training memory has several owners, and which one dominates changes with model, sequence length, batch, and optimizer. For mixed-precision Adam the split has been written down byte by byte. It comes to 16 bytes per parameter: 2Ψ for the fp16 weights, 2Ψ for the fp16 gradients, and 12Ψ for the fp32 parameter copy, momentum and variance. Twelve of those sixteen bytes are optimizer state.

The ZeRO paper says what that costs on a real model: “For a model such as GPT-2 with 1.5 Billion parameters, this leads to a memory requirement of at least 24 GB, which is significantly higher than the meager 3 GB of memory required to hold the fp16 parameters alone.”

And that is only the state. The same paper puts the activations for that same 1.5B-parameter GPT-2, at sequence length 1K and batch size 32, at about 60 GB more. A fifth owner, larger than the four before it.

Hugging Face's Transformers documentation reaches the same anatomy independently, and per parameter: 6 bytes of mixed-precision weights (fp16 plus the fp32 master copy), 8 bytes of Adam momentum and variance, 4 bytes of fp32 gradients, plus activations that “vary in size with batch size, sequence length, model depth, and hidden size”. Two independent accounts, one conclusion. The weights are rarely the thing that fills the card.

FigureLayers · 5 layers
  1. 01

    Parameters

    Model weights and possibly higher-precision master copies.

  2. 02

    Gradients

    One tensor per trainable parameter before or during reduction.

  3. 03

    Optimizer state

    Momentum and adaptive moments can exceed parameter memory.

  4. 04

    Activations

    Intermediate values retained for backward, often batch-dependent.

  5. 05

    Temporary workspaces

    Kernel buffers, communication buckets, and allocator fragmentation.

Comparison

Memory-saving techniques trade something else

Each of these four saves one resource by spending another. Choose the trade that fits the limiting resource, and where someone has measured the trade, use their measurement rather than an adjective.

Take the fourth column. Padding is not a rounding error. Graphcore's packing paper opens with the size of the waste: “We show in this paper that the variation in sequence lengths in common NLP datasets is such that up to 50% of all tokens can be padding.” For GLUE-CoLA at sequence length 128 the figure reaches 89%. Removing that waste, without letting packed examples see one another, gave a 2x speedup for BERT phase-2 pre-training on Wikipedia at sequence length 512.

The “Verify: attention boundaries” line in the same column is not a precaution invented for a course. Earlier packing implementations “did not consider example boundaries when using Flash Attention 2, resulting in undesired cross-example attention that reduce quality and convergence”. That was reported on both the Hugging Face and IBM Research blogs. Boundary-aware packing — passing position_ids and calling flash_attn_varlen_func — bought up to 2x throughput and a 20% peak-memory reduction on FLAN, and 1.4x on OrcaMath, measured in tokens per second across llama2-7B, mistral-7B and granite-8B-code on 8 A100-80 GPUs.

The saving and the failure mode are the same size. The same 2x sits on either side of a correct mask.

FigureComparison · 4 columns

Mixed precision

Store and compute selected values in lower precision.

  • Saves: memory and bandwidth
  • Costs: numerical risk
  • Best when: hardware supports it
  • Verify: quality and overflow

Activation checkpointing

Discard selected activations and recompute them during backward.

  • Saves: activation memory
  • Costs: extra compute
  • Best when: depth dominates memory
  • Verify: deterministic recompute

State sharding

Partition gradients, parameters, or optimizer state across workers.

  • Saves: replicated state
  • Costs: communication complexity
  • Best when: model state dominates
  • Verify: fault and resume semantics

Sequence packing

Reduce padding by combining variable-length examples efficiently.

  • Saves: wasted token compute
  • Costs: masking complexity
  • Best when: lengths vary widely
  • Verify: attention boundaries

Case

Buying memory back with an extra forward pass, and with sharding

Two memory-saving trades come with published numbers behind them.

The first buys memory with arithmetic. A 2016 paper on sublinear-memory training describes an algorithm that “costs O(sqrt(n)) memory to train a n layer network”. The bill for that saving is “an extra forward pass per mini-batch”. On a 1,000-layer residual network on ImageNet, peak memory went “from 48G to 7G”. The measured cost was “30 percent additional running time” on that workload.

The second answers a different limit inside the same memory budget. ZeRO, presented in 2020, shards the state instead of recomputing it. It trained “models of up to 13B parameters” without any model parallelism. Above 100B parameters it reached a throughput of “15 Petaflops” on 400 GPUs.

Both free the same card. One pays in compute, the other in communication.

Figure

The activations are not stored more cleverly. Most of them are thrown away and computed a second time, and the bill for that arrives as running time.

Analogy

A factory where every station must stay balanced

Fast machines, slow delivery trucks, crowded storage, and mandatory quality inspections all sit inside the same factory. Accelerating one station can create a larger queue elsewhere.

The plant runs at the speed of its slowest committed step. Speeding up a fast one moves the queue instead of shortening it. Training behaves the same way. That is why the number worth reporting is time to a quality threshold, not the throughput of whichever stage happened to be measured.

Efficiency gains are system gains only when they shorten reliable learning.

Example

Optimizations that can move cost instead of removing it

A local metric may improve while the total system becomes slower or riskier. MLPerf, the field's training benchmark, says so in the paper that defines it: “Although many optimizations immediately improve traditional performance metrics such as throughput, some can decrease the final model quality, an effect that is only observable by running an entire training session.” The abstract states the reversal plainly: “optimizations that improve training throughput can increase the time to solution”.

The cost did not vanish. It moved somewhere the throughput counter does not look.

  • Checkpointing: Memory falls — 48G to 7G on the residual network above — but recomputation cost 30 percent additional running time on that same workload, and may change stochastic operations unless handled carefully.
  • Large batches: Utilization improves, yet MLPerf v0.5 ResNet-50 needs about 64 epochs to reach the 74.9% top-1 target at a minibatch of 4K, and more than 80 epochs at 16K — 30% more computation for an identical result. Google Brain mapped the same curve across 35 workloads: six model families, three training algorithms, seven datasets, and a public release of 71,638,836 loss measurements over 168,160 trained models. They found “there is an initial period of perfect scaling … followed by a region of diminishing returns that eventually leads to a regime of maximal data parallelism where additional parallelism provides no benefit whatsoever”.
  • Packing: Token efficiency rises — up to 50% of the tokens in common NLP datasets are padding — but an incorrect mask lets information cross example boundaries, the cross-example attention that reduced quality and convergence under Flash Attention 2.
  • Compilation: Steady-state kernels accelerate after expensive warmup and shape-specialization overhead.
  • Frequent checkpoints: Recovery improves while storage, synchronization, and wall-clock cost increase.
  • Aggressive prefetching: Input stalls disappear — the 30% of fleet training compute spent in the input pipeline is exactly what prefetching aims at — while host memory pressure or nondeterministic ordering grows.

The fastest kernel does not guarantee the fastest training run

A GPU can wait for data decoding, host transfers, distributed synchronization, checkpoint writes, or Python control flow. That wait has been measured at fleet scale rather than guessed at. Google analysed millions of tf.data jobs running in its own datacenters and concluded: “On average, ML training jobs spend 30% of their total compute time in the input pipeline.” The tail is worse than the mean. The same analysis reports that “20% of jobs spend more than a third of their compute time in the input pipeline”, and that 13% of jobs read at least 1 TB of input data.

Microsoft Research and UT Austin found the same thing independently, across nine models, three tasks and four datasets on production clusters: “We find that in many cases, DNN training time is dominated by data stall time: time spent waiting for data to be fetched and preprocessed”. Their CoorDL loader cut training time “by as much as 5x on a single server” against DALI. Two fleets, two teams, one bottleneck — and it is not the kernel.

The mirror case is real too, and it is the one profiling misses. The arithmetic units can sit near peak while end-to-end time stays poor, because the step is waiting somewhere the FLOP counter does not look.

So profile representative steady-state and transition phases. Include evaluation, accumulation, compilation, and data-loader warmup rather than timing one ideal batch.

Optimization begins with locating the real bottleneck, not applying every efficiency trick.

Compute-efficient training may choose a smaller model or better data

Architecture and dataset choices can dominate kernel-level optimizations. A smaller model trained on cleaner, better targeted evidence may reach the product threshold sooner.

That budget question was tested directly in 2022. Hoffmann and twenty-one colleagues trained “over 400 language models ranging from 70 million to over 16 billion parameters”. The rule they extracted from those runs is a short one: for compute-optimal training “the model size and the number of training tokens should be scaled equally”.

Chinchilla holds 70B parameters. It outperformed Gopher, which holds 280B. The smaller model was the better use of an identical compute budget.

Steps

Profile before and after an optimization

Use representative shapes and full training state. Each step below is a measurement, not an impression. The last one is the only step whose number a benchmark would accept.

FigureProcess · 5 steps
  1. 1. Establish the end-to-end baseline

    Measure input, forward, backward, optimizer, communication, evaluation, and checkpoint time.

  2. 2. Identify the limiting resource

    Separate compute, memory, bandwidth, communication, and input stalls.

  3. 3. Apply one targeted change

    Choose precision, recomputation, packing, sharding, or kernel optimization.

  4. 4. Revalidate correctness

    Compare losses, gradients, masks, checkpoints, and final metrics.

  5. 5. Measure time to quality

    Include warmup, failures, tuning, and resource cost in the result.

Tokens per second can reward wasted tokens

A system may report high throughput while processing padding, duplicated examples, masked positions, or low-value easy data. Raw throughput does not measure useful evidence, and the industry's own training benchmark does not score it: “MLPerf’s performance metric is the time to train to a defined quality target.” The choice is deliberate. That metric “captures the auxiliary operations necessary for training such models, including data-pipeline and accuracy calculations” — the very work a kernel timing excludes.

The idea came from DAWNBench, announced by the Stanford DAWN project on 29 November 2017, with a first competition deadline of 20 April 2018 and an explicit rejection of proxy metrics such as time per minibatch. The MLPerf paper credits it as “the first multi-entrant benchmark competition to use 'time to train' (originally called time to accuracy)”. MLCommons still runs it that way — “The MLPerf Training benchmark suite measures how fast systems can train models to a target quality metric” — and publishes each benchmark as a dataset plus a target. In MLPerf Training v6.0: Llama 3.1 8B to 3.3 log perplexity on C4, Llama 3.1 405B to 5.6, the Criteo 4TB recommendation task to 0.8032 AUC, lightweight object detection (RetinaNet on Open Images) to 34.0% mAP. Every entry is a finish line, not a speed.

Report valid tokens, unique examples, optimizer updates, energy, and time to a quality threshold. Energy is not a rhetorical item on that list. Two groups have published training budgets in those units.

Start with GPT-3. Patterson and colleagues, at Google and UC Berkeley, put its training at 1,287 MWh and 552 tCO2e — “Its estimated carbon emissions due to training are 552 tCO2e and its energy consumption is 1287 MWh” — and argued that energy usage and CO2e should be a key metric in evaluating models.

BLOOM-176B was measured rather than estimated. Luccioni and colleagues logged 118 days 5h 41m, 1,082,990 A100-80GB GPU hours, and 433,196 kWh drawn on a 57 gCO2eq/kWh grid: “We estimate that BLOOM’s final training emitted approximately 24.7 tonnes of CO2eq if we consider only the dynamic power consumption, and 50.5 tonnes if we account for all processes ranging from equipment manufacturing to energy-based operational consumption.” Their Table 4 reproduces the other group's figure independently, listing GPT-3 at 1,287 MWh and 502 tonnes, or 552 tonnes once a datacenter PUE of 1.1 is applied.

Two models of comparable size, roughly a twentieth of the emissions. No throughput counter would have shown that.

Count useful training evidence, not only tensor operations.

Useful throughput is a modeling property too

Optimization engineering should compare model scale, data mixture, training duration, and hardware techniques under one outcome budget. Efficiency is a design property of the whole experiment. The choice of model, datacenter and processor moves a training run's carbon footprint by ~100-1000X. No kernel rewrite reaches that range.

The cheapest useful model is not necessarily the model with the cheapest step.

Key takeaways