A Python code specialist cut out of granite-3.1-1b-a400m-base by keeping only the experts that code tokens are routed to. It has 16 of 32 experts per layer: 0.73B params instead of 1.33B, with the same code perplexity.
This is a proof of concept, not a production model.
down_proj in closed form (least squares) so it reproduces the original layer's output on code. Attention-sink tokens are excluded from the fit.down_proj and the router, on one 8 GB GPU.Perplexity on held-out samples (lower is better):
| model | params | code | math | prose |
|---|---|---|---|---|
| original | 1.33B | 4.58 | 1.97 | 10.15 |
| this model | 0.73B | 4.61 | 10.6 | 173 |
| same recipe, 16 random experts | 0.73B | 5.95 | 30.3 | 610 |
Choosing experts by routing beats choosing them at random under the same repair and fine-tuning. Outside code, the model is much worse than the original. That is by design.
Limitations:
from transformers import AutoModelForCausalLM, AutoTokenizer
m = AutoModelForCausalLM.from_pretrained("Pantelispanka/granite-moe-code-specialist-0.7b-v0", dtype="bfloat16")
t = AutoTokenizer.from_pretrained("Pantelispanka/granite-moe-code-specialist-0.7b-v0")
print(t.decode(m.generate(**t("def is_palindrome(s):", return_tensors="pt"), max_new_tokens=40)[0]))
kept_experts.json lists which original experts were kept in each layer.