Edit model weights with Python
Access named tensors, evaluate your changes, and save a standalone checkpoint.
Use Python 3.12 or later in a virtual environment. You need a local Hugging Face causal-language-model snapshot with its tokenizer files. WSM does not download model files or execute model repository Python.
uv venv --python 3.12 .venv
. .venv/bin/activate
uv pip install 'wsm[editing]==0.1.0'Replace the paths below with your snapshot and an unused checkpoint destination whose parent directory exists. The example requires a tensor named model.norm.weight. Inspect model.weightsfor your model's names and shapes before choosing an edit.
from wsm import WeightModel
model = WeightModel.load("/path/to/snapshot")
cases = [{"prompt": "Say hello.", "target": "Hello!"}]
before = model.evaluate(cases)
model.weights["model.norm.weight"][:16].mul_(0.95)
after = model.evaluate(cases)
print({"baseline": before, "candidate": after})
model.save("/path/to/checkpoint")Evaluation reports target-token negative log likelihood, not generated responses. This edit demonstrates the API without claiming to improve the model.
CLI coming soon
Use the Python SDK for now. Python editing files run with your process permissions and are not sandboxed.
Read the Python editing guide for API details and execution permissions.