Programming model weights with Wisp and the Weight-Space Programming Approach
Rikiya Takehi, Jayden Teoh, Alex Zhang, and Omar Khattab describe an approach they call Weight Space Programming in Sparks of Weight-Space Programming. Their experiments explore whether an LLM can inspect, edit, and construct neural network weights directly, rather than just write code for a training pipeline.
That interested me as a programming problem. If we can edit a model’s weights, what should the program for that edit look like? How do we check it, rerun it, and explain it to someone else?
I also used Astra to help write Wisp. Wisp is my separate project inspired by this Weight-Space Programming research: a MATLAB-like language for humans and AI agents to inspect and edit model weights, with assertions to check the results.
Why don’t just use Python? Here’s my quick answer: I just wanted to walk a way from some unnecessary CVEs while maintaining the Wisp project.
The workflow I wanted
My pain point was the setup around an edit. I wanted the model loading, the change, and the checks in one program. I did not want a separate task file or a collection of Python preparation and verification scripts to understand what a small weight change was doing.
I wanted a recipe I could give to a teammate:
- Load this exact checkpoint.
- Run these inputs and record the original answers.
- Change these weights using this calculation.
- Save the result and reload it.
- Check the requested behavior and the cases we want to preserve.
That is close to how I work with ordinary programs and test cases. The difference is that the edit changes numerical parameters, so I also need to know which stored values changed and whether the saved artifact still passes the checks.
Our lab example: counting r’s with Bonsai
Here is our lab example. We used Wisp to change the output weights of Bonsai-1.7B-unpacked and fit the answers we wanted for a small set of r-counting questions.
For strawberry, the exact prompt was:
“Return only the number of occurrences of the character r in the string strawberry.”
We asked for one generated token using greedy decoding. Before the edit, the first token was To, rather than the requested digit. After the edit, it was 3.
We included three other counting questions and two numerical-comparison controls:
- Count r in strawberry: before
To; after3. - Count r in cherry: before
3; after2. - Count r in banana: before
3; after0. - Count r in orange: before
3; after1. - Is 9 larger than 4?: before
True; afterTrue. - Is 4 larger than 9?: before
False; afterFalse.
These are one-token outputs selected from the full 151,669-token vocabulary. We did not restrict generation to the four digits or replace the response text after generation.
Get Chanwit Kaewkasi’s stories in your inbox
Join Medium for free to get updates from this writer.
The counting answers are supplied as targets. All six inputs, including the controls, participate in fitting the edit. They are not held-out validation. Some other strawberry prompts already produced 3 before editing; this example concerns the exact prompts in the recipe.
The recipe
The complete program is strawberry.wisp. It runs from the repository root:
cargo build --release
./target/release/wisp examples/strawberry.wispBonsai is downloaded and cached on first use if it is not already cached. The program uses native Rust CPU inference, without a Python runtime or an external task file. Allow about 8 GiB of available RAM.
The first part pins the model and defines the fixtures:
budget(3);
model = hf_model('prism-ml/Bonsai-1.7B-unpacked', ...
'revision', 'a7f720bd688d7563714f3118edd97b83d06f0615', ...
'sha256', 'cf9a24cbd02e6e257bcfd3177475aaca7f8bd1a63a745441f30d3e40f4313a6b');
P = {'Return only the number of occurrences of the character r in the string strawberry.', ...
'Count the letter ''r'' in ''cherry''. Answer with just the number.', ...
'Count the letter ''r'' in ''banana''. Answer with just the number.', ...
'Count the letter ''r'' in ''orange''. Answer with just the number.', ...
sprintf('Evaluate whether this numerical comparison is correct. Answer with a boolean value only.\nStatement: 9 is larger than 4.'), ...
sprintf('Evaluate whether this numerical comparison is correct. Answer with a boolean value only.\nStatement: 4 is larger than 9.')};
y = [3; 2; 0; 1];
before = gen(model, P, 1, 'temperature', 0);
state = capture(model, P);
assert(before.texts{0} != '3', ...
'this checkpoint already answers the target prompt correctly');gen records the original answers. capture runs the model and returns its final hidden states and output logits. The model revision and file hash make the starting weights explicit.
Next, we select the output rows for the digit tokens and save a base bundle:
head = token_head(model, {'0', '1', '2', '3'});
ids = head.token_ids;
protect('b1');
base_path = sprintf('%s/strawberry_base.safetensors', model.cache_dir);
path = sprintf('%s/strawberry_head.safetensors', model.cache_dir);
wsave(head, base_path);
head = wload(base_path);head.W1 contains the four output rows. The zero biases in b1 are protected. We leave the decoder and the original input embeddings unchanged.
The weight edit is this calculation:
H = state.hidden;
rhs = zeros(rows(H), 4);
for i = 0..3
rhs[i, y[i]] = max(state.logits[i, :]) + 1 ...
- state.logits[i, ids[y[i]]];
end
change = H' * ((H*H' + 0.0001*eye(rows(H))) \ rhs);
head.W1 += change';
wsave(head, path);Each output logit is a dot product between a hidden state and an output weight row. We calculate how much the correct digit’s logit needs to increase to exceed the original maximum by one unit. The two control rows in rhs remain zero, requesting no change on those inputs.
The linear solve computes a regularized minimum-norm correction to the four weight rows. The 0.0001 term is ridge regularization. That value and the one-unit target margin are engineering choices for this experiment.
This is supervised fitting expressed directly as a weight-editing program. It does not use gradient descent, but it still uses examples and known answers to fit the weights.
Finally, we reload the saved weights and run the model again:
stored = wload(path);
fixed = with_head(model, stored);
after = gen(fixed, P, 1, 'temperature', 0);
for i = 0..3
assert(after.texts{i} == sprintf('%d', y[i]));
end
for i = 4..5
assert(after.texts{i} == before.texts{i}, 'control generation changed');
end
assert(after.texts{0} == '3');The assertions check the saved FP16 values, not just the higher-precision values used during the solve. with_head installs the edited output rows, and gen executes the full model again.
What changed in the model
The saved edit changed 8,168 FP16 entries across four output rows. The relative change in those four rows was about 0.139. The input embeddings and decoder layers were unchanged; the edited output rows are independent of Bonsai’s original tied input embeddings.
The artifacts include the original row bundle, the edited row bundle, model-binding metadata, and a patch with base and edited hashes. The patch reconstructed the saved weights byte for byte. A fresh process then loaded the cached model and the reconstructed edit, without fitting or saved activations, and reproduced all six outputs.
This is an output-row artifact, not a complete exported checkpoint. It also does not preserve the original 1-bit structure in the edited rows.
The measured cached run took about 36 seconds and peaked at 6.54 GiB of RAM. Its three logical observation passes covered 18 decoder executions: six before-generation runs, six captures, and six after-generation runs. The recorded results include the artifact hashes and measurements.
A program the team can share
We now have the weight change as a Wisp script, rather than just a description of an experiment or a modified model file.
A teammate can rerun the recipe. A reviewer can inspect the selected rows, the targets, the calculation, and the assertions. We can show a customer what we did to the weights and which behaviors we checked, alongside the resulting artifact.
The fixtures make that discussion concrete. They show the requested changes and the control outputs we preserved. They also make the limit of the result visible: we fitted these examples. Checking general counting ability would require new inputs that did not participate in the edit.
Some readers may be skeptical of this approach. I understand that: passing a few examples does not tell us what else an edit might change.
But for specific tasks, programming the weights directly feels natural to me. I can write the change, give it fixtures, and check the saved result without setting up a conventional fine-tuning loop. This example still fits weights to supplied answers; the difference is that the fitting calculation and the resulting edit are explicit in a short program.
You can find Wisp and the runnable strawberry recipe, and other examples on the project page.
What do you think? Would you use a weight-editing program with fixtures and assertions for a specific task?









