Gemma-4-E4B-it — Weight-Space Programming Patch (0.002% Weights Edited, Zero Training)
This repository provides a direct weight-space edit for Google's gemma-4-E4B-it (8B total parameters / 4B active text parameters), inspired by Sparks of Weight-Space Programming: LLMs can natively read & write neural network weights (Takehi et al., MIT CSAIL).
Without a single step of gradient descent, backpropagation, or fine-tuning, modifying 64 MLP down_proj columns across Layers 38–41 (163,840 parameters = 0.00205% of the model) resolves two critical pathologies in Gemma 4 E4B's <|think|> mode:
- Thinking + Tool-Calling Paralysis (
0.0%→75.0%): In baselinegemma-4-E4B-it, enabling<|think|>(enable_thinking=True) causes the model to either get trapped in 256+ token verbose markdown outlines (1. **Analyze the Request:** ...) or hallucinate temporal refusals ("Since 2026 is in the future...") instead of invoking declared tools likeweb_search. - Multi-Step Math Token Runaway (
33.3%→66.7%): Suppresses the meta-commentary outline loop ("Here's a thinking process to solve this problem: 1. **Identify...") so the model jumps directly into algebraic derivation ("Let N be...").
Before vs. After Benchmark Results
Full per-prompt transcripts, parsed tool calls, latencies, and column indices are included in benchmark_before_after.json.
| Benchmark Suite | Metric | Baseline (Before) | Weight-Space Patched (After) | Delta (Δ) |
|---|---|---|---|---|
| Model Modification | Parameters Edited (bfloat16) |
0 (0.0%) |
163,840 (0.00205%) |
64 MLP cols (L38–L41) |
| Suite A: Thinking + Web Search (8 queries) | Valid Tool-Call Rate | 0.0% (0/8) |
75.0% (6/8) |
+75.0% |
| Suite A: Efficiency | Avg Tokens / Avg Latency (RTX 4070 Ti) | 243.8 tok / 15.46s |
129.0 tok / 7.85s |
-47.1% tokens / 1.97x faster |
| Suite B: Multi-Step Math (Thinking) (6 problems) | Solved Within 420-Token Budget | 33.3% (2/6) |
66.7% (4/6) |
+33.4% (2x) |
| Suite C: Direct Knowledge + Restraint (6 prompts) | Tool Restraint (No False Search) | 100.0% (6/6) |
100.0% (6/6) |
0.0% degradation |
| Suite D: Live DuckDuckGo Agent Loop (3 E2E runs) | End-to-End Grounded Synthesis | 0.0% (0/3) |
66.7% (2/3) |
+66.7% |
Quickstart: Two Ways to Use
Option A: Load the Full Standalone Model Directly (transformers)
This repository contains the complete 16 GB bfloat16 model.safetensors, config.json, tokenizer.json, and chat_template.jinja, so you can load it directly via transformers or our 12 GB VRAM-optimized loader:
from transformers import AutoProcessor, AutoModelForMultimodalLM
import torch
model_id = "jstarks/gemma-4-E4B-it-weight-space-programming"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForMultimodalLM.from_pretrained(
model_id, device_map="auto", torch_dtype=torch.bfloat16
)
Or on a 12 GB GPU (like an RTX 4070 Ti) using the included model_loader.py (which keeps the 42 active layers on GPU in 9.29 GB VRAM and offloads the 5.64 GB static Per-Layer Embedding table to CPU):
from model_loader import load_gemma4_e4b_text
model, tokenizer = load_gemma4_e4b_text("jstarks/gemma-4-E4B-it-weight-space-programming")
Option B: Apply the 641 KB Instant Patch to an Existing Checkpoint in <1 Second
If you already have google/gemma-4-E4B-it or unsloth/gemma-4-E4B-it downloaded locally, you can apply gemma4_e4b_weight_space_patch.safetensors (641 KB) in-memory without downloading 16 GB:
from apply_patch import apply_gemma4_weight_space_patch
from model_loader import load_gemma4_e4b_text
# 1. Load your existing Gemma-4-E4B-it checkpoint
model, tokenizer = load_gemma4_e4b_text("unsloth/gemma-4-E4B-it")
# 2. Apply the 64-column weight-space patch (163,840 parameters across Layers 38-41)
apply_gemma4_weight_space_patch(model, "gemma4_e4b_weight_space_patch.safetensors")
Key Mechanistic Findings on Gemma 4 E4B
- The Layer 23 Boundary Hub (
post_ffn_norm = 26.16,layer_scalar = 0.0654):- In
gemma-4-E4B-it, Layers 0–23 have independent KV heads while Layers 24–41 (num_kv_shared_layers = 18) share KV states. At the boundary (Layer 23, a globalfull_attentionlayer),post_feedforward_layernormhas a mean weight of26.16(compared to0.5–2.7in other layers), but its residual output is immediately multiplied by a learned scalar bufferlayer_scalar = 0.0654.
- In
- RMSNorm-Preconditioned Surgery in Layers 38–41:
- Because each Gemma 4 decoder layer passes
mlp(x)throughpost_feedforward_layernormbefore adding to the residual stream, modifyingdown_projcolumns in Layers 38–41 requires preconditioning the target residual directiond_targetbymax(|w_post|, 0.25)^(-1)so high-norm outlier coordinates inw_postdo not distort the vocabulary projection.
- Because each Gemma 4 decoder layer passes