This model is an experiment in QAT LoRA on a ternary model: can you fine-tune Ternary-Bonsai-2-9B while it only exists as 2-bit ternary weights?
Standard quantization-aware pipelines keep a higher-precision master copy and quantize on the way into each forward pass. Here there was no stored master copy. The base stayed as frozen ternary codes for the whole run, and the LoRA delta was trained directly against that grid. At the end the adapter was discarded and its effect was committed into the GGUF by rewriting the 2-bit codes in place. Installing Shakespeare cost 55,603,249 trit flips out of 6.9 billion weights (0.80%).
Shakespearean code review was chosen as the task because style transfer gives a clean signal to evaluate. Paste a git diff and the model answers with a short analytical think block, then a lament in the voice of a tragic hero, with the actual remedy spelled out at the end. Either the review opens with "Alas" or it doesn't, which makes the persona an unambiguous instrument for measuring whether ternary training worked.
The result is a plain GGUF with the same size, format, and inference cost as the original, with no adapter or sidecar weights to merge at serve time.
Input: a raw git diff. The persona is baked into the weights, so no system prompt is required.
Output:
<think>
The diff changes `fetch_page` to return rows[:limit + 1] and removes the
empty-result guard, while `fetch_all` switches to while True with a blanket
except. The off-by-one returns one extra row per page, so the boundary row
appears on two consecutive pages...
</think>
Alas, poor pagination! This scroll doth turn but not for naught, for thou
dost slice `rows[:limit + 1]`, and so the last event is caught twice across
pages. One fate, yet doomed duplication: the same row shall live in both
scrolls, a ghost haunting two tombs.
Better cure this wound: fetch only `limit` rows, and let each page's tail
hold its own final row...
The whole pipeline runs from the original GGUF file alone. No reference fp16 checkpoint is needed.
generate_artifacts.py).extract_gguf.py).M = ternary + B@A is formed on the fly inside the forward pass.FWHT(x·s)/√4096 at each linear layer, and the weights live permanently in rotated space.| setting | value |
|---|---|
| LoRA rank / alpha | 64 / 128 |
| learning rate | 1e-4, cosine decay, 20 warmup steps |
| effective batch | 32 (2 × 16 accumulation) |
| epochs / steps | 3 / 870 |
| hysteresis threshold | 0.60 |
| trainable params | ~160M |
| data | ~9,300 diff + review pairs |
| wall clock | 21.4 h on a single 40GB GPU |
The data comes from diffs harvested from ronantakizawa/github-codereview, quality-filtered, deduplicated, and capped at 2,048 diff tokens. Review comments were restyled into Shakespearean tragedy by a teacher model, and a separate teacher pass generated 80–120 word analytical think blocks that reason about the code change (never about the review). Loss is computed on the think and review tokens only.
Loss fell from 2.31 to 1.40 over the run. Gradient norm peaked at 24 early in warmup, then settled around 3.8 for the rest of training. The flip curve is a gentle S: no flips for the first 20 steps while the LoRA grows from zero and the forward pass stays bit-identical to the base, steady commitment through the middle, then a hard flatten as the learning rate anneals. Total: 55.6M flips, 0.80% of rotated weights.
Held-out diffs never seen in training, a plain "You are a code reviewer…" system prompt, and thinking enabled:
This file needs PrismML's llama.cpp fork with support for type-42 ternary tensors. Stock llama.cpp cannot read it.
./llama-cli -m BonsaiShakespeareV3.gguf \
--chat-template chatml \
-p "<|im_start|>user
File: src/pagination.py (python)
<paste diff here><|im_end|>
<|im_start|>assistant
"
Tips:
| path | contents |
|---|---|
BonsaiShakespeareV3.gguf |
The model. Everything else is for inspection and reproduction. |
training_scripts/ |
Self-contained scripts that operate from the GGUF alone. |
sft_data/train_v3.jsonl |
9,253 tokenized training examples. |
sft_data/eval_v3.jsonl |
500 held-out examples. |
sft_data/full_data.jsonl |
All 9,753 examples with raw diff, think, and review text. |
Inside training_scripts/:
generate_artifacts.py — Hadamard signs, tokenizer, and config from GGUF metadataextract_gguf.py — GGUF to ternary codes, scales, and dense substratedataset.py — tokenization and batching for training and evalmodel.py — ternary loader and the rotated Hadamard linear layerlora.py — LoRA-STE training with hysteresis; writes lora_final.pt and qstate_final.pteval.py — generation over the held-out splitmerge.py — trit-flip merge back into a GGUFFrom the original base GGUF, run everything from the repo root:
python training_scripts/generate_artifacts.py --gguf Ternary-Bonsai-2-9B-Q2_0.gguf
python training_scripts/extract_gguf.py
python training_scripts/lora.py
python training_scripts/eval.py --ckpt lora_out_v3/lora_final.pt --out eval_final.jsonl
python training_scripts/merge.py --qstate lora_out_v3/qstate_final.pt \
--gguf Ternary-Bonsai-2-9B-Q2_0.gguf \
--out BonsaiShakespeareV3.gguf
This uses the included sft_data/ split. Paths and hyperparameters are defined at the top of each script.
prism-ml/Ternary-Bonsai-2-9B-gguf