{
 "id": "gemma-4-12b-sv-hemma-v0.1",
 "title": "Hemma on Gemma 4 12B: Pushing Swedish grammar to 95.3% Sv-BLiMP at home",
 "dish": "Home-forged Swedish Gemma 4 12B checkpoint (4-bit MLX QLoRA + on-device GGUF for Ollama / llama.cpp)",
 "lede": "Apply the Hemma Swedish SFT brand to Google's Gemma 4 12B Instruct on 27,119 curated, open-licensed Swedish and replay examples (RenaissanceLabsEU/hemma-sv-sft-v0.2) in ~1h 40m on a single 32 GB Apple M5 MacBook. Held-out validation loss drops from 4.319 to 1.059 (−3.26 nats), Swedish grammaticality on all 16 Tarnyd/sv-blimp phenomena jumps from 87.5% (56/64) to 95.3% (61/64), and we export both 4-bit MLX weights and on-device Q4_K_M / Q6_K GGUF files using llama.cpp 1.0.0.",
 "stage": "LoRA SFT + GGUF (Local)",
 "lab": "Renaissance Labs × NAISS",
 "author": "Birger Moell",
 "date": "2026-12-03",
 "status": "Complete on Apple M5 (32 GB). Published on RenaissanceLabsEU and naiss-se Hugging Face orgs in both 4-bit MLX and on-device GGUF formats.",
 "checkpoint": "https://huggingface.co/naiss-se/gemma-4-12b-sv-hemma-v0.1",
 "repo_files": "https://huggingface.co/naiss-se/gemma-4-12b-sv-hemma-v0.1/blob/main/",
 "glance": [
  {
   "k": "Makes",
   "v": "1",
   "u": "12B MLX + GGUF + LoRA"
  },
  {
   "k": "Runtime",
   "v": "1h 40m",
   "u": "2,000 steps · 679.9k tok"
  },
  {
   "k": "Equipment",
   "v": "1",
   "u": "Apple M5 laptop (32 GB)"
  },
  {
   "k": "Sv-BLiMP",
   "v": "95.3%",
   "u": "61/64 (+7.8 pp vs base)"
  }
 ],
 "data": {
  "sub": "A 27,119-row, zero-contamination Swedish + replay SFT mixture published as RenaissanceLabsEU/hemma-sv-sft-v0.2 (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.2, CC BY 4.0 / Apache 2.0 / CC0). Every source is audited for licence and provenance (excluding COURTS, JO, and smol-smoltalk-swedish), and strictly disjoint from our held-out benchmarks (Tarnyd/sv-blimp and liu-nlp/swedish-facts-v1).",
  "groups": [
   {
    "title": "hemma-sv-sft-v0.2 mixture (25,764 train + 1,355 stratified valid)",
    "sub": "Nine complementary buckets combining direct Swedish instruction synthesis, Swedish grammar repair and A/B discrimination, native Swedish government klarspråk, persona-grounded everyday tasks, and 15% English reasoning/code replay to prevent catastrophic forgetting.",
    "total": 27119,
    "sources": [
     {
      "name": "EuroMultiSynth-Instruct (sv, score ≥ 4)",
      "id": "openeurollm/EuroMultiSynth-Instruct-decontaminated-v0.1 · sv",
      "pairs": 8000,
      "licence": "Apache-2.0",
      "pinned": true,
      "note": "High-quality direct Swedish instruction synthesis"
     },
     {
      "name": "Propella English replay (code & reasoning)",
      "id": "openeurollm/Propella-Excellent · en",
      "pairs": 4000,
      "licence": "Apache-2.0",
      "pinned": true,
      "note": "~15% English replay to protect general reasoning and coding capability"
     },
     {
      "name": "Swedish grammaticality repair pairs",
      "id": "KeTuTu/swedish-grammaticality-pairs · repair",
      "pairs": 3000,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Proofreading prompts targeting V2 word order, gender agreement, and spelling"
     },
     {
      "name": "Öppna statsliggaren (government klarspråk)",
      "id": "naiss-se/oppna-statsliggaren",
      "pairs": 3000,
      "licence": "CC0-1.0",
      "pinned": true,
      "note": "Native Swedish public-sector budget & agency governance prose"
     },
     {
      "name": "Swedish personas v3 (everyday tasks)",
      "id": "naiss-se/swedish-personas-v3",
      "pairs": 3000,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Natural, everyday Swedish persona-conditioned writing and dialogue"
     },
     {
      "name": "Upphandlingsmyndigheten native QA",
      "id": "tomdickson/upphandlingsmyndigheten-qa",
      "pairs": 2023,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Native Swedish public procurement questions and answers"
     },
     {
      "name": "Regeringsuppdrag (government assignments)",
      "id": "naiss-se/regeringsuppdrag",
      "pairs": 2000,
      "licence": "CC0-1.0",
      "pinned": true,
      "note": "Swedish government directives and plain-language summaries"
     },
     {
      "name": "Swedish grammar A/B discrimination",
      "id": "KeTuTu/swedish-grammaticality-pairs · A/B",
      "pairs": 1096,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Explicit minimal-pair discrimination between grammatical and ungrammatical Swedish"
     },
     {
      "name": "Unchanged-correct Swedish controls",
      "id": "KeTuTu/swedish-grammaticality-pairs · unchanged",
      "pairs": 1000,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Teaches the model not to over-edit already-correct Swedish sentences"
     }
    ]
   }
  ],
  "checksums_title": "SHA-256 fingerprints of the hemma-sv-sft-v0.2 dataset splits",
  "checksums": [
   [
    "train/data_v02/train.jsonl",
    "2a15565d8f0ebee1b554655b39b6a85bf63433633a2b5167ff5ac62b6f2ecfee"
   ],
   [
    "train/data_v02/valid.jsonl",
    "b6d4cc396cfdf8019521faa88f833488d5e48b5ce3df8663d2a7f5b5fe3846d6"
   ]
  ]
 },
 "pantry": [
  [
   "Base model",
   "mlx-community/gemma-4-12B-it-4bit (google/gemma-4-12B-it)",
   "12B Gemma 4 Unified model quantized to 4-bit MLX (~7.2 GB on disk)"
  ],
  [
   "Published training dataset",
   "RenaissanceLabsEU/hemma-sv-sft-v0.2",
   "25,764 train + 1,355 valid Swedish + replay chat JSONL (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.2)"
  ],
  [
   "Held-out micro-eval",
   "Tarnyd/sv-blimp (64 pairs, all 16 configs) + liu-nlp/swedish-facts-v1 (30 Qs) + 4 probes",
   "Runs in ~92 seconds on M5 Metal; strictly excluded from training"
  ],
  [
   "On-device GGUF export",
   "llama.cpp 1.0.0 (convert_hf_to_gguf.py + llama-quantize)",
   "Native Gemma4UnifiedForConditionalGeneration support (Q4_K_M & Q6_K) for Ollama / llama.cpp / LM Studio"
  ],
  [
   "Software",
   "mlx-lm (via uvx) · Python 3.12 · llama.cpp 1.0.0",
   "Zero CUDA required; caffeinate -i keeps macOS awake during unattended runs"
  ]
 ],
 "equipment": {
  "nodes": 1,
  "gpus_per_node": 1,
  "gpu": "Apple M5 (32 GB unified memory)",
  "sub": "Training rank-16 LoRA adapters on the top 16 layers of 4-bit Gemma 4 12B (21.86M trainable parameters = 0.184%) with gradient checkpointing fits inside ~13 GB peak unified memory on a 32 GB MacBook and sustains ~115 tok/s (~1h 40m for 2,000 steps / 679.9k tokens)."
 },
 "settings_groups": [
  {
   "title": "Home Apple Silicon MLX QLoRA (Matched 1:1 with Prelude v0.2)",
   "rows": [
    [
     "Objective",
     "Causal LM cross-entropy, assistant-only loss (mask_prompt: true)"
    ],
    [
     "Adapter",
     "LoRA rank 16, scale 32, dropout 0.05 on top 16 layers (21.86M trainable params = 0.184%)"
    ],
    [
     "Learning rate",
     "1.0e-5 cosine decay (100-step warmup from 1e-7, decaying to 1e-6)"
    ],
    [
     "Batch & steps",
     "batch_size 1 · grad_accumulation_steps 8 · 2,000 steps · 679.9k supervised tokens (~1h 40m)"
    ],
    [
     "Max sequence length",
     "2,048 tokens with grad_checkpoint: true"
    ],
    [
     "Validation loss",
     "4.319 (step 1) → 1.059 (step 2,000) on 30 validation batches (−3.260 nats)"
    ]
   ]
  }
 ],
 "sbatch_title": "Complete Gemma 4 12B Hemma v0.1 training + GGUF export script (Apple Silicon)",
 "sbatch": "#!/usr/bin/env bash\n# Train Hemma on Gemma 4 12B at home on a 32 GB Apple Silicon MacBook (~1h 40m)\n# and export both 4-bit MLX and on-device GGUF (Q4_K_M + Q6_K).\nset -euo pipefail\n\n# 1. Download hemma-sv-sft-v0.2 (25,764 train + 1,355 valid)\nhuggingface-cli download RenaissanceLabsEU/hemma-sv-sft-v0.2 \\\n  --repo-type dataset --local-dir ./data_v02\n\n# 2. Baseline micro-eval (64 Sv-BLiMP + 30 Swedish Facts + 4 probes)\ncaffeinate -i uvx --from mlx-lm --with datasets python micro_sv_eval.py \\\n  --model mlx-community/gemma-4-12B-it-4bit \\\n  --output-json ./logs/v02_eval_gemma4_12b_before.json\n\n# 3. Train 4-bit MLX QLoRA (2,000 steps, ~115 tok/s, ~1h 40m on Apple M5)\ncaffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./lora_v02_gemma4_12b.yaml\n\n# 4. Post-training micro-eval\ncaffeinate -i uvx --from mlx-lm --with datasets python micro_sv_eval.py \\\n  --model mlx-community/gemma-4-12B-it-4bit \\\n  --adapter-path ./adapters/gemma-4-12b-sv-hemma-v0.1 \\\n  --output-json ./logs/v02_eval_gemma4_12b_after.json\n\n# 5. Fuse 4-bit MLX release weights\nuvx --from mlx-lm mlx_lm.fuse \\\n  --model mlx-community/gemma-4-12B-it-4bit \\\n  --adapter-path ./adapters/gemma-4-12b-sv-hemma-v0.1 \\\n  --save-path ./exports/gemma-4-12b-sv-hemma-v0.1\n\n# 6. Export on-device GGUF (requires llama.cpp >= 1.0.0)\nuvx --from mlx-lm mlx_lm.fuse \\\n  --model mlx-community/gemma-4-12B-it-4bit \\\n  --adapter-path ./adapters/gemma-4-12b-sv-hemma-v0.1 \\\n  --save-path ./tmp_gguf/gemma4-hf --dequantize\npython convert_hf_to_gguf.py ./tmp_gguf/gemma4-hf --outfile ./tmp_gguf/gemma4-bf16.gguf --outtype bf16\nllama-quantize ./tmp_gguf/gemma4-bf16.gguf ./exports_gguf/gemma-4-12b-sv-hemma-v0.1-Q4_K_M.gguf Q4_K_M\nllama-quantize ./tmp_gguf/gemma4-bf16.gguf ./exports_gguf/gemma-4-12b-sv-hemma-v0.1-Q6_K.gguf Q6_K\n",
 "steps": [
  {
   "t": "Download hemma-sv-sft-v0.2",
   "d": "Pull the 25,764 train + 1,355 valid Swedish + English replay JSONL directly from RenaissanceLabsEU/hemma-sv-sft-v0.2.",
   "code": "huggingface-cli download RenaissanceLabsEU/hemma-sv-sft-v0.2 --repo-type dataset --local-dir ./data_v02"
  },
  {
   "t": "Run the 90-second baseline Swedish micro-eval",
   "d": "Score all 16 Tarnyd/sv-blimp grammar phenomena (64 minimal pairs), 30 liu-nlp/swedish-facts-v1 short-answer questions, and 4 qualitative Swedish probes before training.",
   "code": "uvx --from mlx-lm --with datasets python micro_sv_eval.py \\\n  --model mlx-community/gemma-4-12B-it-4bit --output-json ./logs/v02_eval_gemma4_12b_before.json"
  },
  {
   "t": "Train 2,000 steps of 4-bit MLX QLoRA",
   "d": "Train rank-16 LoRA on the top 16 layers with lr=1e-5 cosine decay, assistant-only loss masking, and gradient checkpointing (~679.9k tokens, ~1h 40m on Apple M5).",
   "code": "caffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./lora_v02_gemma4_12b.yaml"
  },
  {
   "t": "Verify Swedish grammar gain and fuse MLX weights",
   "d": "Re-run the identical micro-eval (Sv-BLiMP jumps 87.5% → 95.3%), then fuse into standalone 4-bit MLX weights while preserving the unfused adapters/ folder.",
   "code": "uvx --from mlx-lm mlx_lm.fuse \\\n  --model mlx-community/gemma-4-12B-it-4bit \\\n  --adapter-path ./adapters/gemma-4-12b-sv-hemma-v0.1 \\\n  --save-path ./exports/gemma-4-12b-sv-hemma-v0.1"
  },
  {
   "t": "Export on-device GGUF (Q4_K_M & Q6_K) with llama.cpp 1.0.0",
   "d": "Dequantize-fuse to BF16 safetensors, convert with llama.cpp 1.0.0 (which adds native Gemma4UnifiedForConditionalGeneration support), and quantize to Q4_K_M and Q6_K for Ollama / llama.cpp / LM Studio.",
   "code": "ollama run hf.co/naiss-se/gemma-4-12b-sv-hemma-v0.1-GGUF:Q4_K_M",
   "flag": "Older llama.cpp builds (pre-1.0.0) fail with 'Model Gemma4UnifiedForConditionalGeneration is not supported'. Upgrade to llama.cpp >= 1.0.0."
  }
 ],
 "verify": {
  "checks": [
   [
    "Validation loss convergence",
    "Drops sharply from 4.319 (step 1) to 1.059 (step 2,000) across 30 held-out Swedish validation batches (−3.260 nats).",
    0
   ],
   [
    "Sv-BLiMP grammar jump (+7.8 pp)",
    "Improves from 56/64 (87.5%) to 61/64 (95.3%) across all 16 Swedish grammar phenomena on Tarnyd/sv-blimp.",
    0
   ],
   [
    "Zero regressions on controls & code",
    "Preserves exact non-editing control, valid Swedish JSON schema keys, native-grade Swedish prose, and clean Swedish Python code with zero greedy doom loops.",
    0
   ],
   [
    "On-device GGUF compatibility",
    "Cleanly converts all 667 tensors via llama.cpp 1.0.0 (Gemma4UnifiedModel) into Q4_K_M and Q6_K GGUF files.",
    0
   ]
  ],
  "metrics": [
   [
    "Sv-BLiMP (64 pairs, 16 phenomena)",
    "95.3% (↑ from 87.5%)"
   ],
   [
    "Validation loss (30 batches, ↓)",
    "1.059 (↓ from 4.319)"
   ],
   [
    "Supervised training tokens",
    "679.9k tokens (2,000 steps)"
   ],
   [
    "Apple M5 throughput",
    "~115 tok/s (1h 40m)"
   ]
  ],
  "note": "Applying the Hemma recipe to Gemma 4 12B demonstrates that Hemma works as a base-model-agnostic Swedish quality layer: in just 679.9k tokens on a laptop, Gemma 4 12B climbs from 87.5% to 95.3% on Sv-BLiMP while adopting natural Swedish klarspråk and everyday register without losing code or reasoning ability.",
  "figures": [
   {
    "src": "/assets/img/hemma-v0.2-dataset-and-benchmarks.png",
    "alt": "Two-panel chart showing the 27,119-row hemma-sv-sft-v0.2 dataset composition alongside head-to-head Sv-BLiMP and Swedish Facts accuracy for Prelude 9B and Gemma 4 12B.",
    "caption": "Left: The 27,119-row open-licensed hemma-sv-sft-v0.2 SFT mixture (9 buckets, including 15% English replay). Right: Matched 4-bit MLX micro-eval showing Gemma 4 12B jumping from 87.5% to 95.3% on Sv-BLiMP while Prelude 9B leads Swedish Facts (13.3%)."
   },
   {
    "src": "/assets/img/hemma-v0.2-val-loss-comparison.png",
    "alt": "Two-panel comparison of validation loss and Apple M5 training throughput/parameters between Gemma 4 12B Hemma v0.1 and Prelude 9B Hemma v0.2.",
    "caption": "Matched 2,000-step Apple M5 QLoRA runs (~680k tokens each). Gemma 4 12B drops val loss 4.319 → 1.059 and gains +7.8 pp on Sv-BLiMP; Prelude 9B reaches 0.869 best val loss at step 1,200."
   }
  ],
  "table": {
   "head": [
    "Metric (4-bit MLX, matched 2,000 steps)",
    "Gemma 4 12B Base",
    "Gemma 4 12B Hemma v0.1",
    "Prelude 9B Hemma v0.2"
   ],
   "rows": [
    [
     "Validation loss (30 batches, ↓)",
     "4.319 (step 1)",
     "1.059 (step 2,000)",
     "0.869 (step 1,200) / 1.041 (step 2,000)"
    ],
    [
     "Sv-BLiMP (64 pairs, 16 phenomena)",
     "56/64 (87.5%)",
     "61/64 (95.3%)",
     "32/64 (50.0%)"
    ],
    [
     "Swedish Facts (30 short-answer Qs)",
     "2/30 (6.7%)",
     "2/30 (6.7%)",
     "4/30 (13.3%)"
    ],
    [
     "Supervised tokens trained",
     "—",
     "679.9k tokens",
     "664.0k tokens"
    ],
    [
     "Apple M5 wall-clock & throughput",
     "—",
     "1h 40m (~115 tok/s)",
     "1h 35m (~125 tok/s)"
    ],
    [
     "no_overedit & sv_json controls",
     "Exact ✓",
     "Exact ✓",
     "Exact ✓"
    ]
   ]
  }
 },
 "gaps": [
  "Only 679.9k tokens (~6.5% of one epoch over hemma-sv-sft-v0.2) were seen in this 2,000-step laptop run — a full-epoch H200 / Berzelius run will cover all 25,764 training examples.",
  "Swedish factual recall (liu-nlp/swedish-facts-v1) stayed at 2/30 (6.7%) at 680k tokens; factual knowledge requires larger Swedish CPT/SFT volume rather than short LoRA SFT.",
  "Gemma 4's tokenizer uses 2.54 SV tok/word (vs 2.08 for Prelude), making it ~18% more token-expensive on Swedish despite stronger zero-shot grammar."
 ],
 "files_sub": "Published on both RenaissanceLabsEU/gemma-4-12b-sv-hemma-v0.1 and naiss-se/gemma-4-12b-sv-hemma-v0.1, plus on-device GGUFs at naiss-se/gemma-4-12b-sv-hemma-v0.1-GGUF.",
 "files": [
  [
   "README.md",
   "Model card with quickstart, dataset provenance, and before/after micro-eval"
  ],
  [
   "model*.safetensors",
   "Standalone fused 4-bit MLX weights ready for mlx_lm.generate"
  ],
  [
   "adapters/adapters.safetensors",
   "Unfused step-2,000 LoRA adapter for continued training"
  ],
  [
   "adapters/lora_v02_gemma4_12b.yaml",
   "Exact mlx_lm.lora configuration file"
  ],
  [
   "gemma-4-12b-sv-hemma-v0.1-Q4_K_M.gguf",
   "On-device Q4_K_M GGUF for Ollama / llama.cpp 1.0.0 (in -GGUF repo)"
  ],
  [
   "gemma-4-12b-sv-hemma-v0.1-Q6_K.gguf",
   "High-precision Q6_K GGUF for Ollama / llama.cpp 1.0.0 (in -GGUF repo)"
  ]
 ],
 "tags": [
  "swedish",
  "supervised-fine-tuning",
  "lora",
  "mlx",
  "gemma4",
  "gguf",
  "local-training",
  "hemma"
 ],
 "related_note": "/recipes/prelude-9b-sv-hemma-v0.2/",
 "related_note_title": "Part 2: Matched Prelude 9B Hemma v0.2 recipe",
 "effects": {
  "swedish": [
   "improved",
   "Sv-BLiMP jumps 87.5% → 95.3% (+7.8 pp) in 1h 40m on an Apple M5 laptop"
  ],
  "structured": [
   "held",
   "Preserves Swedish JSON schema keys, non-editing controls, and clean Python code"
  ],
  "training-cost": [
   "improved",
   "1h 40m on a single 32 GB Apple M5 MacBook (~13 GB peak memory, zero HPC)"
  ]
 }
}
