KatInterview

Building a GPU Memory Budget for LLM Fine-Tuning

Machine LearningModel answer
HardModel answerMachine Learningllmfine-tuninggpu-memory

Last updated

Work out how much accelerator memory a language-model fine-tuning job needs, covering weights, gradients, optimizer state, activations, buffers, and parallelism, for both full and LoRA-style tuning.

Building a GPU Memory Budget for LLM Fine-Tuning

A teammate wants to fine-tune a transformer language model and asks whether it will fit on the GPUs you have. Describe how you would estimate the per-device memory the job needs.

Your answer should:

  • Give a formula (or ledger) with explicitly named assumptions, instead of quoting one universal "bytes per parameter" figure.
  • Cover each component: parameters, gradients, optimizer states, activations, temporary buffers, numeric precision, and sequence/batch shape.
  • Show how the choice of data, tensor, or pipeline parallelism (and sharding) changes the per-device numbers.
  • Contrast full fine-tuning with parameter-efficient adaptation.
  • Reason about peak memory, not only the steady-state allocation, since peak decides whether the job runs out of memory.

Before estimating, say which clarifying questions you would ask (for example: which optimizer and numeric formats, how many parameters are trainable, and what batch size, sequence length, checkpointing and parallel layout are planned).

Hints

Hint 1

Start with the model-state ledger: bytes per parameter for weights, gradients, master weights, and optimizer moments, expressed in terms of the precision and optimizer you assume.

Hint 2

Activation memory grows roughly with layers x tokens per microbatch x hidden size, and attention scores can add a term that grows with sequence length squared unless fused kernels are used. Think about what checkpointing trades away.

Hint 3

Sharding and parallelism move memory around rather than always removing it: ask what each strategy splits (states, weights, activations) and what extra buffers or replicated pieces it introduces. Then add headroom for fragmentation and validate with a profiler.