Index Ai Academy
Phase 6 · Editing models

Edits are deltas: task vectors, patches and hashes

Subtract a base from a fine-tune and the edit becomes an object: scale it, take it away, ship it as a patch, and prove by hash what was applied.

About 35 minutes · Optional lesson: deeper material that no phase or certificate requires · A free account opens the whole lesson.
Already have an account? Sign in to open this lesson

What it covers10

Why it matters

Every way of changing a model ends in the same place: some weights have new values. Fine-tuning, a hand edit and an edit written by a language model all leave a difference between the model before and the model after. That difference is a thing you can hold, measure, scale, share and check.

This lesson makes it concrete. You will pull the change out of a fine-tuned tiny GPT, turn it up and down like a dial, and package an edit as a patch with a fingerprint on each side. The rest of the course is built on these moves, and so is any honest claim of the form "we changed 0.016% of the weights."

What you'll be able to do

  • Subtract a base model from a fine-tune to get the edit as an object, and describe its size
  • Apply a delta at different strengths, and take one away, and measure what each does
  • Make a patch that rebuilds an edited model exactly, and say why it stores new values and not differences
  • Use fingerprints to prove which base a patch fits and what model it produces
Quick check
A tiny GPT with 40,195 weights is fine-tuned for 500 steps to answer one new kind of question. A guess: how many of its weights have a different value afterward?
  1. None: fine-tuning adds new weights and leaves the old ones alone
  2. A few hundred, in the last layer
  3. All 40,195, most of them by a small amount
  4. About half, chosen at random
Show the answer

All 40,195, most of them by a small amount

Ordinary fine-tuning nudges every weight on every step, so every one ends up at least a little different. The typical change here is about 0.025. You will measure it in a moment.

The idea: an edit is a difference you can hold

This lesson rests on Vectors: lists of numbers that point: subtracting two vectors, and scaling one, work entry by entry.

The edit as an object

Take a model before a change and the same model after it. Subtract one from the other, tensor by tensor, and what is left is the change itself:

Δ=θafter−θbefore\Delta = \theta_{\text{after}} - \theta_{\text{before}}

Read it as: θ\theta (theta) stands for all of a model's weights. Δ\Delta (delta) is the difference: for every weight, its new value minus its old one.

The delta has the same names and shapes as the model, so it is a state too. When the change came from fine-tuning on a task, researchers call the delta a task vector. The name is from a 2022 paper on "task arithmetic", which showed that these differences can be added, scaled and subtracted with meaningful results.

The course serves a base model, tiny-gpt, and a fine-tune of it, tiny-gpt-uses. The fine-tune learned to answer one question, "What is the River tent for?", with a use the catalog lists for that kind of product. wsp.score asks 72 such questions about product names held out of the fine-tuning and counts good answers.

Worked example

Pull the delta out of a fine-tune

Load both models, score them, subtract, and describe the difference: how many entries changed, how large the changes are, and how long the delta is next to the model.

import numpy as np
import wsp

base, info = wsp.load_state("tiny-gpt")
tuned, _ = wsp.load_state("tiny-gpt-uses")
for name, state in [("base", base), ("fine-tune", tuned)]:
    model, tok = wsp.build(state, info)
    good = wsp.score(model, tok, 'uses')
    print(f"{name}: {good} of 72 held-out questions answered well")

delta = wsp.delta(tuned, base)       # a state: same names and shapes as the model
d, b = wsp.flatten(delta), wsp.flatten(base)    # every number, as one long vector
print(f"entries changed: {int((d != 0).sum()):,} of {d.size:,}")
print(f"largest change {np.abs(d).max():.3f}, typical change {np.abs(d).mean():.4f}")
print(f"length of the delta {np.linalg.norm(d):.2f}")
print(f"length of the base {np.linalg.norm(b):.2f}")
It runs right here in your browser, with nothing to install.

The base answers none of the 72 questions and the fine-tune answers all of them. Every one of the 40,195 weights changed, but only slightly: the largest change is 0.294 and the typical one 0.0251. Measured as a vector, the delta is 7.96 long against the base's 98.79, about 8% of it. A skill the model didn't have is a small step away in weight space. It is also a step in every direction at once, which is why a fine-tune is hard to read.

Quick check
A delta is plain subtraction, tensor by tensor. What does this print?
import numpy as np

before = {"w": np.array([1.0, 2.0, 3.0]), "b": np.array([0.5])}
after = {"w": np.array([1.0, 2.5, 3.0]), "b": np.array([0.0])}
delta = {k: after[k] - before[k] for k in before}
print(delta["w"], delta["b"])
  1. [0. 0.5 0. ] [-0.5]
  2. [1. 2.5 3. ] [0.]
  3. [0.5] [-0.5]
  4. [0. -0.5 0. ] [0.5]
Show the answer

[0. 0.5 0. ] [-0.5]

After minus before: only the middle weight moved, up by 0.5, and the bias went down by 0.5. Entries that didn't change are exactly 0.

Try it now

Six tensors of the base model, before and after the fine-tune. "What changed" shows the delta itself. Its picture is busy everywhere: no row or column was left alone.

Drill

Describe a delta

Write delta_stats(before, after) for two states (dictionaries from names to NumPy arrays with the same shapes). Return a dictionary with "changed" (how many entries differ, over all tensors), "largest" (the largest absolute change) and "tensors" (a sorted list of the names of tensors that changed at all).

import numpy as np

def delta_stats(before, after):
    return {"changed": 0, "largest": 0.0, "tensors": []}

before = {"w": np.array([[1.0, 2.0], [3.0, 4.0]]), "b": np.array([0.0, 0.0]),
          "g": np.array([1.0])}
after = {"w": np.array([[1.0, 2.5], [3.0, 3.0]]), "b": np.array([0.0, 0.0]),
         "g": np.array([1.25])}
print(delta_stats(before, after))
In the lesson, your code is checked as you write it.
In your own words
In your own words: what is a task vector, and what did this one look like?

Write a sentence or two, then compare it with a good answer in the lesson.

In code: turn it up, turn it down, take it away

Once the edit is an object, you can apply it at any strength. Multiply the delta by a number, the scale, and add the result to the base. A scale of 1 gives back the fine-tune. A scale of 0 gives the base. Anything between is a model nobody trained.

wsp.add(base, delta, scale) does the arithmetic. The next cell sweeps the scale and scores each model on the same 72 questions.

Worked example

One delta at eight strengths

Add the delta to the base at each scale and count good answers. Nothing here is trained: every model is the base plus a multiple of one difference.

import wsp

base, info = wsp.load_state("tiny-gpt")
tuned, _ = wsp.load_state("tiny-gpt-uses")
delta = wsp.delta(tuned, base)

for scale in (0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0):
    model, tok = wsp.build(wsp.add(base, delta, scale), info)
    right = wsp.score(model, tok, "uses")
    print(f"scale {scale:<4}: {right:2d} of 72  " + "#" * (right // 3))
In the lesson you can change this and run it.

At a quarter strength the skill isn't there at all: 0 of 72. At half strength it answers 43, and from three quarters to full strength all 72. Past that it falls again: 71 at 1.25, 64 at 1.5 and 29 at twice the strength. So the delta works like a dial with a sweet spot. Too little and the behavior hasn't formed. Too much and you overshoot the model fine-tuning found. Anyone who applies a published edit "at 1.3 strength" is turning this dial, and should measure where it lands.

Quick check
At a scale of 0.25 the model answered 0 of 72, and at 0.5 it answered 43. What does that say about the edit?
  1. Behavior doesn't change in step with the scale: a quarter of the edit gave none of the skill
  2. Scaling only works for whole numbers
  3. The delta was computed wrongly, since a quarter of it should give 18 right
  4. The first quarter of the weights holds no information
Show the answer

Behavior doesn't change in step with the scale: a quarter of the edit gave none of the skill

The weights move in a straight line as the scale grows, but the model's answers don't. An answer is right or wrong only once the right token becomes the most likely one, and that can flip suddenly.

If adding a delta gives a skill, does subtracting it take the skill away? The task-arithmetic paper reported that it often does. Here is a fair test. The course also serves tiny-gpt-both, one model fine-tuned on two questions at once: what a product is for, and what it is made from. Subtract the "uses" delta from it and see whether it forgets only the first.

Worked example

Subtract a skill from a model that has two

Take the model that answers both questions and subtract the uses delta at three strengths. Score both skills each time.

import wsp

base, info = wsp.load_state("tiny-gpt")
uses, _ = wsp.load_state("tiny-gpt-uses")
both, _ = wsp.load_state("tiny-gpt-both")
uses_delta = wsp.delta(uses, base)

for scale in (0, 0.5, 0.75, 1.0):
    model, tok = wsp.build(wsp.add(both, uses_delta, -scale), info)
    uses = wsp.score(model, tok, 'uses')
    materials = wsp.score(model, tok, 'materials')
    label = f"minus {scale:<4} of the uses delta:"
    print(label, f"uses {uses:2d} of 72, materials {materials:2d} of 72")
In the lesson you can change this and run it.

Before any subtraction the model answers 72 and 72. Taking away half the uses delta drops the uses skill to 21 and leaves materials at 61: the targeted skill fell much further. But it isn't clean. Materials lost 11 answers too, and at three quarters strength both skills are gone, 0 and 0. The two skills share weights in a model this small, so removing one drags the other with it. Subtraction is a real tool with real side effects, and the only way to know them is to measure the things you didn't mean to change.

Quick check
From the supervised fine-tuning lesson: what does fine-tuning change in a model?
  1. Only its prompt, which is stored with the model
  2. Nothing in the model: it adds a lookup table of answers
  3. Its existing weights, by continuing training on example conversations
  4. Its vocabulary, by adding new tokens
Show the answer

Its existing weights, by continuing training on example conversations

Fine-tuning is more gradient steps on new examples, starting from the trained weights. That is why the result differs from the base in every weight, and why the difference can be written down as a delta.

Drill

Apply a delta at a chosen strength

Write apply_delta(base, delta, scale=1.0): return a new state where every tensor is base + scale × delta. Don't change base or delta.

import numpy as np

def apply_delta(base, delta, scale=1.0):
    return base

base = {"w": np.array([1.0, 2.0, 3.0]), "b": np.array([0.5])}
delta = {"w": np.array([0.0, 0.5, 0.0]), "b": np.array([-0.5])}
print(apply_delta(base, delta, 2.0))
In the lesson, your code is checked as you write it.

Plot the same sweep. This reruns the sweep cell and draws the scores.

import matplotlib.pyplot as plt
import wsp

base, info = wsp.load_state("tiny-gpt")
tuned, _ = wsp.load_state("tiny-gpt-uses")
delta = wsp.delta(tuned, base)
scales = [0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0]
scores = []
for scale in scales:
    model, tok = wsp.build(wsp.add(base, delta, scale), info)
    scores.append(wsp.score(model, tok, "uses"))
plt.plot(scales, scores, marker="o")
plt.xlabel("scale of the delta")
plt.ylabel("questions answered of 72")
plt.title("One delta at eight strengths")
plt.show()
In your own words
In your own words: subtracting the uses delta removed the uses skill and also hurt the materials skill. Why would that happen?

Write a sentence or two, then compare it with a good answer in the lesson.

In production: patches and fingerprints

You want to send someone your edit. Sending the whole edited model works, but it hides what you did. Sending the change is better: it is smaller when the edit is small, and the receiver can see exactly which weights moved.

Two details make this trustworthy.

A fingerprint. wsp.state_hash turns a whole state into a short code, computed from every tensor's name, shape and values. Change one weight anywhere and the code changes. Two people who get the same code hold the same model. The technique is called a hash, and the same idea protects downloads and passwords.

New values, not differences. It would seem natural for a patch to store the delta. The next cell shows why that isn't quite safe.

Worked example

Base plus delta is almost the edited model

Rebuild the fine-tune as base + delta and compare it with the real one, entry by entry and by fingerprint.

import numpy as np
import wsp

base, info = wsp.load_state("tiny-gpt")
tuned, _ = wsp.load_state("tiny-gpt-uses")
rebuilt = wsp.add(base, wsp.delta(tuned, base))

off = sum(int((rebuilt[k] != tuned[k]).sum()) for k in base)
gap = max(float(np.abs(rebuilt[k] - tuned[k]).max()) for k in base)
print(f"entries that differ: {off:,} of {wsp.count(base):,}; largest gap {gap:.1e}")
print("fingerprints:", wsp.state_hash(tuned), "and", wsp.state_hash(rebuilt))
In the lesson you can change this and run it.

1,764 entries came back different, by at most 0.000000015. The cause is rounding. A 32-bit float keeps about seven digits, so (after − before) + before can land one step away from after. The model behaves the same, but its fingerprint doesn't match: 44214fa59350 for the real one and 8777c0e33efb for the rebuilt one. A patch that promised "apply me and you get this exact model" would break that promise. So a patch stores, for each changed entry, its position and its new value. Copying a value involves no arithmetic and no rounding.

Worked example

Make a patch, apply it, and try it on the wrong model

A small edit: set four columns of one matrix to zero. Package it with wsp.make_patch, look at what the patch holds, apply it, and then try to apply it to a different model.

import wsp

base, info = wsp.load_state("tiny-gpt")
edited = {k: v.copy() for k, v in base.items()}
edited["blocks.1.mlp.2.weight"][:, :4] = 0   # four of this matrix's 128 columns

patch = wsp.make_patch(base, edited, note="zero four columns of the last MLP output")
print({k: v for k, v in patch.items() if k != "tensors"})
size = wsp.patch_bytes(patch)
print("tensors in the patch:", list(patch["tensors"]), "-", size, "bytes as text")

rebuilt = wsp.apply_patch(base, patch)
print("rebuilt exactly:", wsp.state_hash(rebuilt) == wsp.state_hash(edited))

other, _ = wsp.load_state("tiny-gpt-uses")
try:
    wsp.apply_patch(other, patch)
except ValueError as error:
    print("on another model:", error)
In the lesson you can change this and run it.

The patch records 128 changed entries in one tensor and takes 1,304 bytes, against 160,780 for the whole model. It carries two fingerprints: base, the model it was made for, and result, the model it makes. Applied to the right base it rebuilds the edit exactly. Applied to the fine-tune it refuses, because that model's fingerprint isn't the one the patch names. Without that check the patch would have gone on quietly and produced a model nobody tested.

Quick check
A patch names the base it fits by fingerprint. What does that protect against?
  1. Applying the edit to a different model, where the same positions hold other values and mean other things
  2. The patch being too large to download
  3. The edited model giving a wrong answer to a question the patch's author never tested
  4. Someone reading the weights
Show the answer

Applying the edit to a different model, where the same positions hold other values and mean other things

Positions in a matrix only mean something for the model the edit was worked out on. On another checkpoint the same positions hold different numbers, so the patch would produce an untested model. The check turns that silent mistake into an error.

Quick check
Two states hold the same numbers except for one weight that differs by 0.000001. What does this print?
import numpy as np
import wsp

a = {"w": np.array([0.5, 0.25, 0.125], dtype=np.float32)}
b = {"w": np.array([0.5, 0.25, 0.125001], dtype=np.float32)}
print(wsp.state_hash(a) == wsp.state_hash(a), wsp.state_hash(a) == wsp.state_hash(b))
  1. True False
  2. True True
  3. False False
  4. False True
Show the answer

True False

A state always has the same fingerprint as itself. The second state differs in one stored value, however slightly, so its fingerprint is different. A hash doesn't measure how far apart two models are, only whether they are identical.

Where it shows up in your week

When you receive an edited model or an adapter, you check the hash of the file against the one the author published, and the hash of the base it fits. A patch with no base hash is a patch you cannot trust to apply to the model you hold.

In your own words
In your own words: what do a patch's two fingerprints let the receiver check, and what don't they tell the receiver?

Write a sentence or two, then compare it with a good answer in the lesson.

Goes deeper in

Practice what you just read

  • 6 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 9 review cards for spaced repetition

Try a quiz question

What is a task vector?

  1. A fine-tuned model's weights minus the base model's weights
  2. The list of prompts a model was fine-tuned on
  3. The direction of the gradient on the last training step
  4. A vector that names which task a model is best at

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson

Go deeper, if you like

Optional lessons that build on this one. They never block a phase or a certificate.

Keep learning

Where this lesson leads.

575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.

Phase 6 · about 35 hours of study

Modern LLM engineering

Post-training, fine-tuning, serving and safety 48 lessons 2 free
  1. Base models vs assistants FreeWhat post-training adds to a pretrained model.30 min
  2. Supervised fine-tuningTeach behavior from example conversations.40 min
  3. Instruction and preference dataThe datasets that shape assistant behavior.30 min
  4. RLHFReward models and reinforcement learning from human feedback.40 min
  5. DPO and direct preference methodsPreference training without a separate reward model.40 min
  6. Constitutional AITraining with AI feedback guided by written principles.35 min
  7. Training reasoning modelsReinforcement learning on verifiable problems.40 min
  8. Prompting vs RAG vs fine-tuningPick the cheapest approach that works.30 min
  9. LoRAFine-tune a big model by training small low-rank updates.40 min
  10. Fine-tune an open modelRun a real fine-tune and evaluate the result.45 min
  11. DistillationTrain a small model to imitate a large one.35 min
  12. Tuning an embedding model for retrievalWhen search fails on your own vocabulary: train an adapter with a contrastive loss on question and passage pairs, test on held-out passages, use batch size and hard negatives, and measure what tuning costs elsewhere.45 min
  13. QuantizationStore weights in fewer bits to run models cheaply.35 min
  14. Quantization in practice: zero points, calibration and formatsScale and zero point, per-channel scales, calibration sets, error on a layer's output, quantization-aware training and the file formats you meet at work.35 min
  15. Serving modelsBatching, throughput and latency in production.35 min
  16. One request through a serving stack: latency and costFollow one request through queue, prefill, a prefix cache, decode and the stream, and price a token from a GPU's hourly cost.35 min
  17. Speculative decodingDraft with a small model, verify with a big one.35 min
  18. Running open models locallyDownload, quantize and run a model on your own machine.30 min
  19. Will it fit? Memory for serving and fine-tuningWeights, KV cache, runtime and adapter states added up for a model card and a GPU, with a calculator.30 min
  20. InterpretabilityLooking inside models to see what they compute.40 min
  21. Confident and wrongWhy a model answers with the same assurance whether it is right, wrong or asked about something that does not exist.30 min
  22. Why it can't spell or count lettersThe model sees token IDs, and an embedding learned from usage carries nothing about spelling.25 min
  23. Copying from the promptIn-context learning as a two-head copying circuit: the base tiny GPT cannot copy a repeated pattern, a model trained on repeats grows an induction head and can.30 min
  24. Why temperature changes the answerThe same prompt at 0, 0.7 and 1.5: deterministic loops, plausible variety, broken grammar, measured on a real model.25 min
  25. Why long prompts diluteAttention is a budget shared by every token, so a fact far from the question loses its share; measured, with the cliff beyond the trained window.25 min
  26. Why it never says "I don't know"A chat model answers every request because its fine-tuning data always did; preference training adds flattery; how to build abstention in.25 min
  27. Benchmarks and their limitsWhat leaderboards measure, and what they miss.38 min
  28. Alignment and safetyMaking capable models behave as intended.35 min
  29. Evaluating agentsMeasure multi-step, tool-using behavior.35 min
  30. Reading AI papersGet the idea out of a research paper in 30 minutes.30 min
  31. Capstone: a tuned, evaluated assistantFine-tune, evaluate and deploy a specialist assistant.45 min
  32. Weight-space programming: what it is, and what has been shown Free OptionalA language model reads and writes another network's weights with no training loop: the published experiments, their limits and the criticism, and one edit you make by hand.30 min
  33. Anatomy of a checkpoint OptionalA model file is a set of named tensors: count them, find the ones that write to the residual stream, and save one to bytes and back.35 min
  34. Reading weights without running the model OptionalSingular values, and an attention head's token-to-token circuit, read from the matrices alone and then checked against a measurement.35 min
  35. Writing weights by hand OptionalSet a small network's 55 weights so it catches every ball in a paddle game, the way a program would be written, and meet compiled transformers.40 min
  36. Edits are deltas: task vectors, patches and hashes OptionalSubtract a base from a fine-tune and the edit becomes an object: scale it, take it away, ship it as a patch, and prove by hash what was applied.35 min
  37. Merging models OptionalAverage two fine-tunes, add their task vectors, try SLERP, TIES and DARE, measure the interference, and compare with a model trained on both tasks.40 min
  38. Low-rank edits OptionalCut a fine-tune's change down to its largest directions with the SVD: how few a behavior needs, and how to pull an adapter out of a delta.35 min
  39. Directions: steer the run or edit the weights OptionalFind a behavior's direction in the residual stream, add it at run time, project it out of the weights, and see the effect change from input to input.40 min
  40. Editing one fact OptionalA rank-one change to one matrix rewrites what a product is made from; then reliability, generalization, locality, and what breaks after many edits.40 min
  41. The editor's harness OptionalWhat an LLM that edits weights is given and what it is denied: weights as text and their cost in tokens, a sandbox, a budget of forward passes, and rules the harness enforces.40 min
  42. An LLM reads a network's weights OptionalSix recorded sessions: an LLM is handed 1,315 weights as text and asked what the network was trained to do. Score each answer against the network, and study one it got wrong.40 min
  43. An LLM fixes a model's weights OptionalFind a tiny GPT's loop by hand, then replay recorded sessions in which an LLM hunts for it with ten forward passes, and measure every fix on held-out questions.45 min
  44. An LLM writes a network's weights OptionalFive recorded sessions: an LLM writes every weight of a paddle network from the game's source and one sentence. Score each against its specification, test its design claims, and edit one by hand.40 min
  45. Did it work? Testing an edit OptionalReview a patch for a tiny GPT that can't stop talking: held-out questions, a paired comparison, a same-size control, and what the edit cost.40 min
  46. The published edit, on a real model OptionalRead the edit published for an open model of 870 million weights, apply and verify one in the same form, see what 16-bit and 4-bit storage do to a fix, and run the real thing from a notebook.45 min
  47. Safety, licenses and provenance OptionalBefore a custom model is released: trace its lineage by hash, work out what its parents' licenses pass on, see what an edit can do to a safeguard, and write its card.40 min
  48. Capstone: ship a custom model OptionalSet release gates, then diagnose, edit, measure and document a tiny GPT with a fault no lesson has fixed, and write the record a stranger could check.45 min
Part of a free course 17 lessons

Creating Custom LLMs: Editing Open Weights Using Weight-Space Programming

Read a model's weights, change them by hand and judge an edit someone else made: deltas, merging, low-rank edits, directions and fact edits on a tiny GPT in your browser. Then replay recorded sessions of an LLM reading, fixing and writing weights, check the published edit of a real open model, and ship a model of your own with its patch, verdict and card. Every number is printed by the lesson's own code, and the method's limits are taught with it.

Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write