▶Fine-tune Calculator▋

// guides

Fine-Tuning Memory Levers: Optimizer, Gradient Checkpointing, Batch Size and LoRA Rank

The four settings that actually move your fine-tuning memory — with measured-by-formula examples for an 8B model — and a recipe for making a job fit.

reviewed 2026-11-15 · by Bedino

Once you have picked a method (see the previous guide), four settings decide whether a job fits. This guide shows what each one is worth, using Llama 3.1 8B (32 layers, hidden size 4,096) and the same formulas as the calculator. Values are in GiB.

1. The optimizer

The optimizer’s state is the biggest part of a full fine-tune. Adam stores two values per parameter in fp32; add the fp32 master copy and full fine-tuning needs 12 bytes per parameter for the optimizer alone.

Optimizer State per trainable parameter 8B full fine-tune total
Adam / AdamW 8 bytes 121.1
Adafactor ~4 bytes 91.2
8-bit Adam 2 bytes 76.2

Switching from Adam to 8-bit Adam saves about 45 GiB on a full 8B run. For LoRA and QLoRA the optimizer only holds the adapters, a few hundred megabytes, so this lever is irrelevant there.

2. Gradient checkpointing

Without checkpointing, the backward pass needs every layer’s activations, so activation memory grows with the number of layers. With it, only about the square root of that is kept and the rest is recomputed.

QLoRA on the 8B model at batch 4 and sequence length 2,048:

Gradient checkpointing Total memory
On 5.4 GiB
Off 12.0 GiB

Switching checkpointing off here costs more than twice the memory and saves roughly a quarter to a third of the compute. Almost every consumer-GPU fine-tune turns checkpointing on; turn it off only when you have memory to spare and want the speed.

3. Batch size and sequence length

Activation memory is proportional to batch size times sequence length. QLoRA on the 8B model:

Batch Sequence Total memory
1 2,048 4.4 GiB
4 2,048 5.4 GiB
16 2,048 9.7 GiB
4 8,192 9.7 GiB

Batch 16 at 2,048 tokens and batch 4 at 8,192 tokens cost the same, because both process 32,768 tokens per step. If memory is tight, lower the micro-batch and use gradient accumulation to keep the effective batch size your learning-rate schedule expects. Packing several short examples into one sequence wastes fewer tokens on padding, which is a free speed-up.

4. LoRA rank (and which modules)

The rank controls how many parameters the adapters have. For the 8B model:

Rank Trainable parameters Total memory (LoRA, 16-bit base)
8 14.7 M 16.5 GiB
16 29.4 M 16.6 GiB
64 117.4 M 17.5 GiB

An eightfold increase in rank adds about 1 GiB. The frozen base dominates, so choose the rank for quality: small ranks are fine for style and format; higher ranks help when the model must learn a lot of new knowledge. Adapting more modules (all attention and MLP projections rather than only the query and value matrices) increases capacity by more than raising the rank does.

A recipe for making a job fit

  1. Choose the method by the base-weight budget. If the 16-bit base does not fit, use QLoRA.
  2. Turn gradient checkpointing on.
  3. Start with micro-batch 1 and the sequence length your data needs; raise the batch until memory is about 85 percent used, leaving room for memory spikes and fragmentation.
  4. Use gradient accumulation to reach your target effective batch.
  5. For full fine-tuning, move to an 8-bit optimizer before you reach for more GPUs, then shard across GPUs (see the multi-GPU guide).
  6. Leave a margin. Real runs fragment memory and spike on the first long batch; a job at 98 percent of the card’s memory will crash on the 3,000th step, not the first.

Check your own setup

Enter your model, method, optimizer, batch size, sequence length and rank in the fine-tuning VRAM calculator to see the four line items and the total. The numbers are first-order estimates: framework specifics, flash-attention and offloading will move them, so confirm with a short real run.

faq
What is gradient checkpointing and does it hurt quality?
Instead of storing every layer's activations for the backward pass, checkpointing stores only some of them and recomputes the rest when needed. It does not change the maths or the result, only the speed: expect roughly a third more compute per step in exchange for a large cut in activation memory.
Is gradient accumulation the same as a bigger batch?
For the optimizer update, yes. You run several small micro-batches, add up their gradients and step once, so the effective batch is the product. Memory is set by the micro-batch, so accumulation lets you reach a large effective batch on a small card at the cost of more time per update.
Which optimizer should I choose?
AdamW is the safe default. If the optimizer state does not fit, an 8-bit Adam variant cuts that part by about 4 times with little quality loss in most reports, and Adafactor stores far less state. For LoRA the choice barely matters for memory because only a tiny set of parameters is trained.
Does a longer sequence length cost as much as a bigger batch?
In this model, yes: activation memory scales with batch size times sequence length. Four sequences of 2,048 tokens and one sequence of 8,192 tokens use the same activation memory. Attention implementations such as flash-attention change the constants but not that rule.
more guides