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

Model Merging

Model merging combines the weights of several fine-tuned models into one, without further training. It works when the models were fine-tuned from the same pretrained base and stay in the same basin of the loss landscape. Methods include simple weight averaging ('model soups'), task arithmetic (add or subtract task vectors, the differences between fine-tuned and base weights), TIES-Merging (trim small changes, resolve sign conflicts, then average) and DARE, plus spherical interpolation (SLERP); mergekit makes these easy. Merging lets one model carry several skills, saves serving cost and can even improve robustness. This page covers task vectors, the methods, linear mode connectivity and limits. In the exercises you build task vectors, apply task arithmetic (including negation) and SLERP. You then merge two copies of a pretrained one-block Transformer, fine-tuned for two different tasks. Each single fine-tune was bad at the other task (worst-task loss about 5.1). Simple averaging served both (worst-task loss 1.57), approaching joint multitask fine-tuning (1.21). Averaging models built from different random bases produced a useless model (2.29, the uniform loss). Task arithmetic with full weight overshot (3.48), and TIES was in between (2.36).

Level 0: Foundation

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

Blending two fine-tuned models into one

Suppose one copy of a model was fine-tuned for maintenance questions and another for flight-operations questions. Surprisingly, if both started from the same base model, you can often just average their weights and get one model that does both reasonably well, with no extra training. This only works if they share a starting point; averaging two unrelated models gives nonsense. The demo shows when two training directions help or fight each other.

Transfer and efficiency lab
Task gradients (green, red) have cosine -0.50. Summed update (grey) length 1.00; they conflict, so PCGrad projects each off the other (black), length 1.50.
Why engineers care

Merging lets an organisation combine separately developed skills (procedures, coding, a language) into one deployable model, saving GPU memory and serving cost.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Model merging combines…
  2. 2. Merging works best when models share…
  3. 3. A task vector is…

Level 1: Beginner

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

By hand

  • Task vector: τ = θ_ft − θ_base. Adding λτ to the base adds the skill; subtracting it removes it (for example reducing toxicity); adding several merges skills.
  • Weight averaging (model soups): θ = mean of models fine-tuned from the same base (different tasks, seeds or hyperparameters).
  • Task arithmetic: θ = θ_base + λ Σ_i τ_i. λ = 1 adds full vectors and can overshoot; λ is tuned, often 0.3–0.6 with many tasks.
  • TIES-Merging: trim each τ to its largest entries (for example the top 20%), elect a sign per entry by total magnitude, and average only the agreeing entries. This reduces interference between sign conflicts.
  • DARE: randomly drop most entries of τ and rescale the rest before merging.
  • SLERP: interpolate along the sphere between two models, preserving norm.
  • Linear mode connectivity: models fine-tuned from one base usually have no loss barrier along the straight line between them; independently trained models do, which is why their average fails.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Subtracting a task vector…
  2. 2. TIES resolves…
  3. 3. Models from different random bases are…

Level 2: Intermediate

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

Model merging in code

Exercise 1.

  • Compute task vectors and apply task arithmetic (negation unlearns).
  • Implement spherical interpolation (SLERP).

Practice.

  • mergekit (YAML configs for linear, task arithmetic, TIES, DARE, SLERP) merges Hugging Face checkpoints on CPU in minutes.
  • Merge only models from the same base and architecture; check the tokenizer and chat template are identical.
  • Tune λ (and TIES density) on validation sets for every task in the merge.
  • Evaluate the merged model on each constituent task and on general benchmarks; merges can silently lose skills.
# mergekit config (merge.yml), then: mergekit-yaml merge.yml ./merged
merge_method: ties
base_model: meta-llama/Llama-3.1-8B
models:
  - model: org/llama-maintenance-sft
    parameters: {density: 0.3, weight: 0.5}
  - model: org/llama-flightops-sft
    parameters: {density: 0.3, weight: 0.5}
dtype: bfloat16
🐍 Exercise 1: task vectors and SLERP
Compute task vectors, apply task arithmetic and interpolate two weight vectors spherically.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A popular merging toolkit:
  2. 2. Before merging, check that models share…
  3. 3. The task-arithmetic weight λ is chosen on…

Level 3: Advanced

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

Theory

Why averaging can work. Fine-tuning moves weights only a little from a shared base, into a region where the loss is roughly convex along straight lines between the fine-tuned models (linear mode connectivity; Frankle and colleagues, 2020). Their average then stays in a low-loss region for both tasks, provided the tasks do not demand contradictory changes to the same weights.

Interference. Task vectors overlap: the same weight may need to increase for one task and decrease for another. Averaging halves both changes; adding both (λ = 1) applies each in full, but their interference can push the model away from both solutions. TIES and DARE reduce interference by keeping only the most important, agreeing changes.

Why different bases fail. Networks have many permutation symmetries: two independently trained models compute similar functions with hidden units in different orders. Averaging misaligned units mixes unrelated features, destroying both (unless units are first aligned, as in Git Re-Basin).

📝 Quick check (Level 3)
0/3 answered
  1. 1. Averaging works when models are…
  2. 2. Different-base merges fail mainly because of…
  3. 3. TIES reduces…

Level 4: Expert

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

When can two fine-tuned models be merged into one that does both jobs?

History.

  • M. Wortsman et al., “Model soups” (ICML 2022).
  • G. Ilharco et al., “Editing models with task arithmetic” (ICLR 2023).
  • P. Yadav et al., “TIES-Merging” (NeurIPS 2023); L. Yu et al., “DARE” (2024).
  • S. Ainsworth, J. Hayase and S. Srinivasa, “Git Re-Basin” (ICLR 2023).

Exercise 2.

  • Base: the one-block Transformer from F6-1, pretrained on task 0 (copy 8 back).
  • Fine-tunes: two copies fine-tuned (200 examples, 200 steps) for task 1 (copy + 1) and task 2 (copy + 2), each with its own task token.
  • Merges:
    • weight average;
    • task arithmetic (λ = 1);
    • TIES (keep 30%).
  • References:
    • joint fine-tuning on both datasets;
    • averaging the task-1 model with a task-2 model fine-tuned from a different random base.
model loss on task 1 loss on task 2
fine-tuned for task 1 0.564 5.073
fine-tuned for task 2 5.193 0.589
joint multitask fine-tuning 1.088 1.206
weight average 1.565 1.419
task arithmetic (λ = 1) 3.482 1.923
TIES 1.472 2.355
average across different bases 2.283 2.293
  • Averaging gives one model for both tasks, without any training on both, at a moderate cost relative to joint fine-tuning.
  • A shared base is essential: averaging with a model from a different base lands at the uniform loss (2.30) on both tasks.
  • Full task arithmetic overshoots: with λ = 1 the two task vectors interfere, and task 1 degrades badly. TIES helps task 1 slightly but hurts task 2. With only two tasks, plain averaging (λ = 0.5 per task) is the best of the merges here.
  • Seeds: seeds 1 and 2 gave average worst-task losses of 1.496/1.498, joint 1.150/1.144, different bases 2.306/2.302, task arithmetic 2.924/2.624.
🐍 Exercise 2 (research): when can two fine-tuned models be merged into one that does both jobs?
Merge two fine-tuned copies of a pretrained Transformer by averaging, task arithmetic and TIES; compare with single models, joint fine-tuning and a different-base merge.

Model merging in AI, robotics and aerospace

  • Consolidating assistants: combining separately fine-tuned skills into one deployable model.
  • Robustness: averaging several fine-tunes (soups) often generalises better than any single one.
  • Design lesson: merge only relatives (same base), tune the weights on validation data, and test every skill afterwards.

References

  1. M. Wortsman et al. (2022). “Model soups: averaging weights of multiple fine-tuned models improves accuracy without increasing inference time.” Proc. ICML.
  2. G. Ilharco et al. (2023). “Editing models with task arithmetic.” Proc. ICLR.
  3. P. Yadav, D. Tam, L. Choshen, C. Raffel and M. Bansal (2023). “TIES-Merging: resolving interference when merging models.” NeurIPS.
  4. S. Ainsworth, J. Hayase and S. Srinivasa (2023). “Git Re-Basin: merging models modulo permutation symmetries.” Proc. ICLR.