Part 09›
✎

09. LLM Post-Training and On-Device Training#

Level: Practitioner · Reading time: 15 min · Exercise: 45 min Prerequisites: Quantization; for the fine-tuning vocabulary, from base model to assistant

In one sentence: After this chapter you can pick a parameter-efficient fine-tuning method for a model you want to adapt, size its memory, and decide whether the training can happen on the device instead of in the cloud.

Why this matters#

You have an open 7-billion-parameter model and a few thousand examples from your domain. Fine-tuning all of it means storing a gradient and optimizer state for every weight. That is well over a hundred gigabytes before the first activation. One laptop cannot do it, and a phone never will.

Two questions follow. Which parameters do you actually need to train? And can the training stay on the device where the data was collected?

The core idea#

Post-training is everything that happens after pre-training: training on a smaller dataset so the model does the job you want (Han, Lecture 14, 0:00). Its first stage is supervised fine-tuning (SFT): the model learns to reproduce good responses, with the loss computed only on the response tokens, not on the prompt (2:00). Preference tuning (RLHF, DPO) follows; it belongs to post-training.

Parameter-efficient fine-tuning (PEFT) freezes the pretrained weights and trains a small set of parameters instead.

The analogy is a patch file on top of a package you never modify. The analogy breaks on memory: a patch is all you need at run time, but here the base model still has to be loaded. PEFT saves training memory and storage of adapters. It does not shrink the base.

How it works: the PEFT menu#

Han's slides list the options (Lecture 14, slides 13 to 42):

Method What is trained Added cost at inference
BitFit the bias terms only none
TinyTL biases plus a light residual layer a small layer
Adapter small layers inserted into each transformer block a small layer per block
Prompt and prefix tuning soft prompts prepended to the input a few extra tokens per request
LoRA low-rank branches beside the frozen weights none once merged
QLoRA LoRA on a 4-bit quantized base model none once merged
BitDelta the difference between base and fine-tuned model, in one bit none

LoRA, step by step#

LoRA, short for low-rank adaptation, leaves the weight matrix W alone and learns an update as a product of two small matrices.

  1. Pick a rank r (the width of the two small matrices), for example 8.
  2. Add A with shape r × d_in and B with shape d_out × r, initialized so that B × A starts at zero.
  3. Train only A and B.
  4. At deployment, add B × A into W. The model has the same shape as before.

Figure 1. LoRA: a small trained branch beside the frozen weights, merged into them at deployment.

flowchart LR
    X["Input"] --> W["Frozen weight matrix W"]
    X --> A["Small matrix A, trained"]
    A --> B["Small matrix B, trained, starts at zero"]
    W --> S["Sum of both paths"]
    B --> S
    S --> O["Output"]
    S --> M["At deployment B times A is added into W"]

Only A and B are trained; after the merge the model has exactly the shape and the cost of the original.

Worked numbers for one 4,096 × 4,096 weight matrix:

QLoRA goes further: the frozen base is stored in 4 bits while the adapters train in 16-bit.

Training on the device#

On-device training adapts a model where the data is created. Han gives the reason in his Fall 2024 lecture on the subject: a gradient is not safe to share. His deep leakage from gradients slides show input images and text reconstructed from gradients alone, for vision models and for BERT, a language model (Lecture 21, 10:00). His slide concludes that, if training data can be recovered this way, sharing the gradient is not safe. Adding noise as a defense also disrupts the model, he says: only very strong noise stops the leak (12:00). So even federated learning, where devices send updates instead of data (2:00), has a privacy hole. His repair is the compression from the previous lecture: prune the gradients before sending. In his test, 99% of the gradients could be pruned without hurting accuracy, and beyond about 70% there was nothing left to match, so the leak stopped (same block).

The obstacle is memory. Training keeps every intermediate activation for the backward pass. Four of Han's remedies:

  1. Update less. TinyTL trains the biases and a small residual layer, with up to 6.5 times less memory (Lecture 21, slides 49 to 52).
  2. Backpropagate less. Sparse back-propagation updates only some layers and, inside them, only some channels, so only a subset of activations has to be stored (28:00). Updating only the last layer is cheap but inaccurate; a contribution analysis picks which intermediate layers to train. He reports the same accuracy as LoRA and accuracy comparable to full fine-tuning when adapting Llama-2 7B (40:00).
  3. Quantize the training. Fake quantization, the simulation used in quantization-aware training, saves no training memory. Real low-precision training does, but converges worse; his quantization-aware scaling repairs that and, he says, recovers a 12-point accuracy loss (54:00). His slides report training under 256 KB of memory.
  4. Compile. PockEngine turns the training graph into static code and removes operators that need no gradient, reorders them, and works in place. As he states, it is about 23 times faster and smaller than TensorFlow Lite, and runs on Jetson boards, Raspberry Pi, phones and an STM32 microcontroller (60:00; 64:00).

A procedure for adapting a model on a laptop:

  1. Quantize the base model to 4 bits.
  2. Attach LoRA adapters, rank 8 to 16.
  3. Keep sequences short and batch size 1.
  4. Evaluate before and after on held-out examples.

Worked example: sizing three training runs#

Rules of thumb from engineering practice, not course content, for a 7-billion-parameter model with 16-bit weights and the AdamW optimizer: 2 bytes of gradient and about 12 bytes of optimizer state per trainable parameter. Assume 20 million adapter parameters.

Full fine-tuning LoRA QLoRA
Weights 14 GB 14 GB 3.5 GB
Trainable parameters 7 billion 20 million 20 million
Gradients 14 GB 0.04 GB 0.04 GB
Optimizer state 84 GB 0.24 GB 0.24 GB
Total before activations 112 GB 14.3 GB 3.8 GB

The optimizer state dominates full fine-tuning, and it almost vanishes with adapters. QLoRA then cuts the weights by four.

What the practitioners say#

Pitfalls#

  1. Forgetting activation memory. Fix: short sequences, batch size 1, and a method that stores fewer activations.
  2. Expecting fake quantization to save memory. Fix: use real low-precision training with scaling.
  3. Sending gradients to a server and calling it private. Fix: keep training local.
  4. Assuming an adapter shrinks the deployed model. Fix: quantize or distill the base for that.
  5. Skipping evaluation after fine-tuning. Fix: run your eval harness before and after; adapters can damage general ability.
  6. Choosing LoRA rank by habit. Fix: try 8, 16 and 32 and compare on held-out data.

Try it#

Adapt a small open model to a narrow task on your own machine.

  1. Prepare 200 instruction and answer pairs.
  2. Fine-tune with LoRA, rank 16, using the parameter-efficient fine-tuning tools of your framework. Check its current documentation for the exact interface.
  3. Record: adapter size, share of trainable parameters, peak memory, quality on 20 held-out examples before and after.

You are done when you have those four numbers and a one-sentence judgment of whether the adaptation helped.

Stretch: repeat with the base model quantized to 4 bits and compare quality and peak memory.

Key takeaways#

Self-check#

  1. A weight matrix is 1,024 × 1,024. With LoRA rank 4, how many trainable parameters does it get?
  2. Why does LoRA add no latency at inference?
  3. What is the main memory consumer during training, beyond the weights themselves?
  4. Why does fake quantization not reduce training memory?
  5. What does deep leakage from gradients show?
  6. Which saves more memory for a 7B model: LoRA on a 16-bit base, or LoRA on a 4-bit base, and why?
Show answers 1. 1,024 × 4 + 4 × 1,024 = 8,192. 2. Because the learned update is merged into the original weight matrix. 3. The activations kept for the backward pass, plus optimizer state for every trainable parameter. 4. It rounds values only to simulate low precision; the weights are still stored and updated in high precision. 5. That the original inputs, images and text, can be reconstructed from shared gradients. 6. LoRA on a 4-bit base: the base weights take a quarter of the memory.

Sources#

Next#

→ Efficient Vision, Generative Models and Distributed Training, a tour of where the same ideas reappear in images, video and multi-machine training.