Foundations to Frontier
Tests
F. NLP and Large Language Models › F6 Fine-Tuning and Adaptation

Full Fine-Tuning

Full fine-tuning continues training every weight of a pretrained model on task-specific data. It adapts the model most completely, but it needs memory for all gradients and optimizer states (about 16 bytes per parameter), stores a full copy per task, can overfit small datasets, and can make the model forget what it knew (catastrophic forgetting). The main controls are a small learning rate with warm-up, few epochs with early stopping, regularisation, layer-wise learning rates, and mixing some original data back in (replay). This page covers the procedure, hyperparameters, overfitting and forgetting, and when cheaper methods (F6-3) suffice. In the exercises you count trainable parameters and compute layer-wise learning rates, then fine-tune a one-block Transformer, pretrained on task A (copy 8 back), for task B (copy 8 back and add 1) using only 30 examples. Training from scratch on those 30 examples reached loss 3.97 on B. Fine-tuning reached 0.80 at learning rate 0.001, and tuning only the output layer 0.67; at learning rate 0.01, full fine-tuning overfitted (3.27). Every option without replay forgot task A (loss 5.4–21.9, up from 0.55). Replaying task-A data kept A at 1.16 but learned B less well (2.14).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Teaching a trained model a new job

A pretrained model already knows a lot. Fine-tuning gives it a little extra practice on your task so it gets good at exactly that. Full fine-tuning lets every part of the model change. That works well, but with very little practice data it can memorise instead of learning, and it can forget its old skills. Gentle, short training and mixing in some old material help. The demo compares training from scratch, tuning only the last layer, and full fine-tuning as data grows.

Transfer and efficiency lab
With 30 target labels (illustrative curves): scratch 0.59, linear probe 0.74, fine-tuning 0.82. Transfer matters most when labels are scarce; the probe plateaus because its features are frozen.
Why engineers care

Fine-tuning turns a general model into a maintenance-report classifier, a procedure assistant or a code helper for flight software, usually from a few hundred to a few thousand examples.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Full fine-tuning updates…
  2. 2. With very little data, full fine-tuning risks…
  3. 3. Forgetting old skills after fine-tuning is called…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Procedure: start from pretrained weights, add or reuse an output head, train on task data with a small learning rate (about 1e-5 to 1e-4 for LLMs), warm-up and decay, for a few epochs, and select the checkpoint on validation loss.
  • Memory: about 16 bytes per parameter for mixed-precision Adam (112 GB for 7B) plus activations; each task needs its own full checkpoint (14 GB in bf16 for 7B).
  • Overfitting: many epochs over a tiny set (200 steps × 32 on 30 examples is over 200 epochs) memorises it. Early stopping, weight decay, dropout and data augmentation help.
  • Forgetting: the shared weights move to suit the new task, degrading old abilities. Remedies: lower learning rate, fewer steps, layer-wise learning-rate decay (lower layers change less), replaying pretraining or general data, or parameter-efficient methods that freeze most weights (F6-3).
  • Head-only tuning (linear probe) is cheapest and forgets nothing in the frozen body, but here the output layer was shared with task A, so changing it forgot A.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Full fine-tuning states for a 7B model:
  2. 2. Layer-wise learning-rate decay makes lower layers…
  3. 3. Replay mixes into fine-tuning…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Full fine-tuning in code

Exercise 1.

  • Count trainable parameters for different freezing choices.
  • Compute layer-wise learning rates.
  • Measure forgetting as the increase in loss on the original task.

Practice.

  • Hugging Face Trainer or TRL SFTTrainer with learning_rate around 1e-5–5e-5 for LLMs, cosine schedule, warm-up ratio 0.03–0.1, 1–3 epochs.
  • Use FSDP or DeepSpeed ZeRO for models that do not fit on one GPU; gradient checkpointing for long sequences.
  • Hold out validation data and evaluate general benchmarks too, to detect forgetting.
  • Prefer LoRA (F6-3) when data is small, many tasks need separate adapters, or memory is limited.
from transformers import AutoModelForCausalLM, TrainingArguments, Trainer
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B", torch_dtype="bfloat16")
args = TrainingArguments("ft-out", learning_rate=2e-5, lr_scheduler_type="cosine", warmup_ratio=0.05,
                         num_train_epochs=2, per_device_train_batch_size=8, gradient_checkpointing=True,
                         eval_strategy="steps", eval_steps=200, load_best_model_at_end=True, bf16=True)
Trainer(model=model, args=args, train_dataset=train_ds, eval_dataset=val_ds).train()
🐍 Exercise 1: parameters, learning rates and forgetting
Count trainable parameters, compute layer-wise learning rates and measure forgetting.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A typical full fine-tuning learning rate for LLMs:
  2. 2. Forgetting is detected by evaluating…
  3. 3. When data is small or memory limited, consider…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why pretraining transfers. Task B needs the same attention skill as task A (find the token 8 back) plus a new output mapping. The pretrained model already has the skill, so 30 examples suffice to learn the mapping. From scratch, 30 examples cannot teach the attention pattern and the model memorises them.

Why a high learning rate hurts. Large updates on a tiny dataset move the weights far from the pretrained solution, overwriting the transferable skill while fitting the 30 examples. A small learning rate keeps the model near the pretrained basin, as an implicit regulariser.

Forgetting and interference. Parameters shared between tasks receive gradients only from the new task. Unless old data is replayed (or changes are constrained, as in EWC, E19-4), the old task’s solution drifts. Replay is a direct trade: with half of each batch spent on task A, task B learns more slowly in the same number of steps.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Pretraining helped task B because B reuses…
  2. 2. A small learning rate acts as…
  3. 3. Replay trades…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How should a pretrained model be fine-tuned on very little data?

History.

  • J. Howard and S. Ruder, “Universal language model fine-tuning for text classification” (ACL 2018).
  • J. Devlin et al., “BERT” (NAACL 2019), fine-tuning for every task.
  • A. Kumar et al., “Fine-tuning can distort pretrained features and underperform out-of-distribution” (ICLR 2022).

Exercise 2.

  • Model: one Transformer block (causal attention plus a ReLU MLP, d = 32) with a task token at the start of each sequence.
  • Task A (pretraining, 3000 sequences): each token copies the token 8 back with probability 0.9.
  • Task B (30 sequences): each token copies the token 8 back and adds 1 (mod 10).
  • Fine-tuning options (200 Adam steps each):
    • full fine-tuning at learning rate 0.01 or 0.001;
    • output layer only (0.01);
    • full fine-tuning (0.01) with half of each batch replayed from task A;
    • training from scratch on the 30 examples.
  • Metric: held-out loss on both tasks (floor 0.50).
model loss on A loss on B
pretrained base 0.545 4.629
full fine-tuning, lr 0.01 21.938 3.268
full fine-tuning, lr 0.001 5.722 0.802
output layer only 6.179 0.670
full fine-tuning + replay 1.158 2.135
from scratch on B 16.565 3.968
  • Pretraining makes 30 examples enough: fine-tuned models reach 0.67–0.80 on B, while training from scratch memorises the examples (3.97).
  • The learning rate matters most: at 0.01, full fine-tuning overfits and destroys the transferable skill (3.27 on B); at 0.001 it works.
  • Tuning only the output layer is best here, because B differs from A only in the output mapping.
  • Forgetting is severe without replay: every non-replay option loses task A (5.4 to 21.9).
  • Replay preserves A but slows B: 1.16 on A, 2.14 on B in the same 200 steps.
  • Seeds: seeds 1 and 2 gave B losses 0.768/0.880 (lr 0.001), 0.666/0.740 (head only) and 3.167/3.265 (scratch); replay kept A at 1.126/1.138.
  • Two designs I discarded:
    • task B = copy 3 back needed a different skill, so pretraining gave no advantage and the two tasks contradicted each other;
    • an attention-only model could not make the output mapping depend on the task token, so I added the MLP.
🐍 Exercise 2 (research): how should a pretrained model be fine-tuned on very little data?
Pretrain a one-block Transformer on one task and fine-tune it on a related task from 30 examples with different learning rates, head-only tuning and replay; measure gains and forgetting.

Full fine-tuning in AI, robotics and aerospace

  • Domain assistants: fully fine-tuning mid-sized models on curated procedures and reports when data and compute allow.
  • Safety: checking that general abilities (and safety behaviour) survive fine-tuning before deployment.
  • Design lesson: small learning rates, early stopping and some replay; and try the cheapest adaptation first.

References

  1. J. Howard and S. Ruder (2018). “Universal language model fine-tuning for text classification.” Proc. ACL.
  2. A. Kumar et al. (2022). “Fine-tuning can distort pretrained features and underperform out-of-distribution.” Proc. ICLR.
  3. Y. Luo et al. (2023). “An empirical study of catastrophic forgetting in large language models during continual fine-tuning.” arXiv:2308.08747.