{
 "id": "prelude-9b-sv-hemma-v0.2",
 "title": "Hemma v0.2 (Part 2): Prelude 9B on expanded Swedish data + on-device GGUF",
 "dish": "Part 2 of home-forged Swedish Prelude 9B (27k-row v0.2 mix, 4-bit MLX + token-patched GGUF for Ollama)",
 "lede": "Part 2 scales our home-forged Prelude 9B Swedish fine-tune to the 27,119-row hemma-sv-sft-v0.2 dataset (2,000 steps, 664.0k supervised tokens, ~1h 35m on Apple M5) with 100% matched hyperparameters against Gemma 4 12B Hemma v0.1. Held-out validation loss drops from 1.815 to 0.869 at step 1,200 (−0.946 nats), Swedish Facts improves from 10.0% to 13.3%, everyday Swedish emails become completely natural, and we document the exact SentencePiece SFT special-token patch required to export clean Prelude GGUF files for Ollama and llama.cpp.",
 "stage": "LoRA SFT + GGUF (Part 2 · Local)",
 "lab": "Renaissance Labs × NAISS",
 "author": "Birger Moell",
 "date": "2026-12-03",
 "status": "Part 2 complete on Apple M5 (32 GB). Published on RenaissanceLabsEU and naiss-se Hugging Face orgs in 4-bit MLX and verified Q4_K_M / Q6_K GGUF formats.",
 "checkpoint": "https://huggingface.co/naiss-se/prelude-9b-sv-hemma-v0.2",
 "repo_files": "https://huggingface.co/naiss-se/prelude-9b-sv-hemma-v0.2/blob/main/",
 "glance": [
  {
   "k": "Makes",
   "v": "1",
   "u": "9.1B MLX + GGUF + LoRA"
  },
  {
   "k": "Runtime",
   "v": "1h 35m",
   "u": "2,000 steps · 664.0k tok"
  },
  {
   "k": "Best Val Loss",
   "v": "0.869",
   "u": "↓ from 1.815 (step 1,200)"
  },
  {
   "k": "Swedish Facts",
   "v": "13.3%",
   "u": "4/30 (highest in suite)"
  }
 ],
 "data": {
  "sub": "Identical 27,119-row hemma-sv-sft-v0.2 dataset used for Gemma 4 12B Hemma v0.1 (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.2), enabling a controlled head-to-head comparison between OpenEuroLLM Prelude 9B and Gemma 4 12B on the same Apple M5 laptop.",
  "groups": [
   {
    "title": "hemma-sv-sft-v0.2 mixture (25,764 train + 1,355 stratified valid)",
    "sub": "2.7× larger than Hemma v0.1, adding Swedish government klarspråk (Öppna statsliggaren & Regeringsuppdrag), Swedish personas v3, explicit grammar A/B discrimination pairs, and 15% English replay.",
    "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 synthetic instructions"
     },
     {
      "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 reasoning and code"
     },
     {
      "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, 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 governance prose"
     },
     {
      "name": "Swedish personas v3 (everyday tasks)",
      "id": "naiss-se/swedish-personas-v3",
      "pairs": 3000,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Eliminates stiff v0.1 phrasing in everyday Swedish emails"
     },
     {
      "name": "Upphandlingsmyndigheten native QA",
      "id": "tomdickson/upphandlingsmyndigheten-qa",
      "pairs": 2023,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Native Swedish public procurement QA"
     },
     {
      "name": "Regeringsuppdrag (government assignments)",
      "id": "naiss-se/regeringsuppdrag",
      "pairs": 2000,
      "licence": "CC0-1.0",
      "pinned": true,
      "note": "Swedish government directives and summaries"
     },
     {
      "name": "Swedish grammar A/B discrimination",
      "id": "KeTuTu/swedish-grammaticality-pairs · A/B",
      "pairs": 1096,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Minimal-pair grammaticality discrimination"
     },
     {
      "name": "Unchanged-correct Swedish controls",
      "id": "KeTuTu/swedish-grammaticality-pairs · unchanged",
      "pairs": 1000,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Prevents over-editing already-correct Swedish"
     }
    ]
   }
  ],
  "checksums_title": "SHA-256 fingerprints of hemma-sv-sft-v0.2 splits",
  "checksums": [
   [
    "train/data_v02/train.jsonl",
    "2a15565d8f0ebee1b554655b39b6a85bf63433633a2b5167ff5ac62b6f2ecfee"
   ],
   [
    "train/data_v02/valid.jsonl",
    "b6d4cc396cfdf8019521faa88f833488d5e48b5ce3df8663d2a7f5b5fe3846d6"
   ]
  ]
 },
 "pantry": [
  [
   "Base model",
   "openeurollm/Prelude-SFT-Propella-Excellent-v0.2",
   "9.1B Qwen3-architecture European base (256k EU SentencePiece tokenizer, 2.08 SV tok/word)"
  ],
  [
   "Published training dataset",
   "RenaissanceLabsEU/hemma-sv-sft-v0.2",
   "25,764 train + 1,355 valid Swedish + replay JSONL (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.2)"
  ],
  [
   "Prelude GGUF tokenizer fix",
   "Patch token IDs 3–18 & CONTROL types in GGUF metadata",
   "Crucial gotcha: prelude-base tokenizer.model uses <start_of_turn>/<end_of_turn>/<unused_5>, while SFT chat_template uses <|im_start|>/<|im_end|>/<think>"
  ],
  [
   "On-device GGUF files",
   "naiss-se/prelude-9b-sv-hemma-v0.2-GGUF",
   "Q4_K_M (5.3 GB) & Q6_K (7.0 GB) verified in llama-completion with clean <|im_end|> stop"
  ]
 ],
 "equipment": {
  "nodes": 1,
  "gpus_per_node": 1,
  "gpu": "Apple M5 (32 GB unified memory)",
  "sub": "Identical laptop hardware and hyperparameters as Gemma 4 12B Hemma v0.1. Prelude 9B runs at ~125 tok/s (~10.6 GB peak memory, ~1h 35m for 2,000 steps / 664.0k supervised tokens)."
 },
 "settings_groups": [
  {
   "title": "Part 2: Home Apple Silicon MLX QLoRA (Matched 1:1 with Gemma 4 12B)",
   "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 (19.40M trainable params = 0.213%)"
    ],
    [
     "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 · 664.0k supervised tokens (~1h 35m)"
    ],
    [
     "Validation loss",
     "1.815 (step 1) → 0.869 (step 1,200, best) → 1.041 (step 2,000) on 30 validation batches"
    ]
   ]
  }
 ],
 "sbatch_title": "Complete Prelude 9B Hemma v0.2 training + token-patched GGUF export script",
 "sbatch": "#!/usr/bin/env bash\n# Part 2: Train Prelude 9B Hemma v0.2 on hemma-sv-sft-v0.2 (~1h 35m on Apple M5)\n# and export token-verified Q4_K_M + Q6_K GGUF files for Ollama / llama.cpp.\nset -euo pipefail\n\n# 1. Train 2,000 steps (matched 1:1 with Gemma 4 12B)\ncaffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./lora_v02_prelude.yaml\n\n# 2. Evaluate on 64 Sv-BLiMP + 30 Swedish Facts + 4 qualitative probes\ncaffeinate -i uvx --from mlx-lm --with datasets python micro_sv_eval.py \\\n  --model ./prelude-excellent-v0.2-4bit \\\n  --adapter-path ./adapters/prelude-9b-sv-hemma-v0.2 \\\n  --output-json ./logs/v02_eval_prelude_9b_after.json\n\n# 3. Fuse standalone 4-bit MLX weights\nuvx --from mlx-lm mlx_lm.fuse \\\n  --model ./prelude-excellent-v0.2-4bit \\\n  --adapter-path ./adapters/prelude-9b-sv-hemma-v0.2 \\\n  --save-path ./exports/prelude-9b-sv-hemma-v0.2\n\n# 4. Dequantize-fuse to BF16 -> GGUF -> Q4_K_M / Q6_K\nuvx --from mlx-lm mlx_lm.fuse \\\n  --model ./prelude-excellent-v0.2-4bit \\\n  --adapter-path ./adapters/prelude-9b-sv-hemma-v0.2 \\\n  --save-path ./tmp_gguf/prelude-hf --dequantize\n# Copy prelude-base tokenizer.model, convert to GGUF, quantize, then patch SFT special tokens (IDs 3..18):\npython convert_hf_to_gguf.py ./tmp_gguf/prelude-hf --outfile ./tmp_gguf/prelude-bf16.gguf --outtype bf16\nllama-quantize ./tmp_gguf/prelude-bf16.gguf ./exports_gguf/prelude-9b-sv-hemma-v0.2-Q4_K_M.gguf Q4_K_M\nllama-quantize ./tmp_gguf/prelude-bf16.gguf ./exports_gguf/prelude-9b-sv-hemma-v0.2-Q6_K.gguf Q6_K\npython patch_prelude_gguf_tokens.py \\\n  ./exports_gguf/prelude-9b-sv-hemma-v0.2-Q4_K_M.gguf \\\n  ./exports_gguf/prelude-9b-sv-hemma-v0.2-Q6_K.gguf\n",
 "steps": [
  {
   "t": "Train 2,000 steps on hemma-sv-sft-v0.2",
   "d": "Run 4-bit MLX QLoRA with identical hyperparameters to Gemma 4 12B Hemma v0.1 (lr=1e-5 cosine, rank 16, top 16 layers, mask_prompt: true). Best validation loss hits 0.869 at step 1,200.",
   "code": "caffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./lora_v02_prelude.yaml"
  },
  {
   "t": "Export BF16 safetensors and quantize to Q4_K_M & Q6_K GGUF",
   "d": "Dequantize-fuse the 4-bit MLX base + LoRA adapter, convert via convert_hf_to_gguf.py (using prelude-base's SentencePiece tokenizer.model), and quantize to Q4_K_M (5.3 GB) and Q6_K (7.0 GB).",
   "code": "llama-quantize prelude-bf16.gguf prelude-9b-sv-hemma-v0.2-Q4_K_M.gguf Q4_K_M"
  },
  {
   "t": "Patch Prelude SFT special tokens in the GGUF header",
   "d": "Because prelude-base's tokenizer.model still labels IDs 3, 4, 13–18 as <start_of_turn>, <end_of_turn>, and <unused_0..5>, while Prelude-SFT's chat_template.jinja expects <|im_start|>, <|im_end|>, <tools>, <think>, and </think>, patch those 8 token strings and set IDs 3–10 to CONTROL type.",
   "code": "python patch_prelude_gguf_tokens.py prelude-9b-sv-hemma-v0.2-Q4_K_M.gguf prelude-9b-sv-hemma-v0.2-Q6_K.gguf",
   "flag": "Without this patch, llama.cpp emits '<unused_5>' and fails to tokenize <|im_start|> / <|im_end|> as single control tokens."
  },
  {
   "t": "Run locally with Ollama or llama.cpp",
   "d": "Verified in llama-completion with clean <|im_end|> stopping and natural Swedish output at 11.7 tok/s on CPU alone (and 40+ tok/s on Apple Silicon Metal).",
   "code": "ollama run hf.co/naiss-se/prelude-9b-sv-hemma-v0.2-GGUF:Q4_K_M"
  }
 ],
 "verify": {
  "checks": [
   [
    "Validation loss drops below 0.90",
    "Drops from 1.815 (step 1) to 0.869 at step 1,200 (−0.946 nats), finishing at 1.041 at step 2,000.",
    0
   ],
   [
    "Swedish Facts improves (10.0% → 13.3%)",
    "Reaches 4/30 (13.3%) on liu-nlp/swedish-facts-v1 — the highest factual recall in the matched local suite (vs 6.7% for Gemma 4 12B).",
    0
   ],
   [
    "Natural everyday Swedish prose",
    "Eliminates both baseline typos ('medduta er', 'gemensammavättstugna') and v0.1's stiff phrasing ('bekväm boendestatus').",
    0
   ],
   [
    "Verified on-device GGUF tokenization",
    "Patching token IDs 3–18 eliminates <unused_5> artifacts and gives clean <|im_end|> stopping in llama.cpp.",
    0
   ]
  ],
  "metrics": [
   [
    "Best validation loss (step 1,200)",
    "0.869 (↓ from 1.815)"
   ],
   [
    "Swedish Facts (30 Qs)",
    "13.3% (↑ from 10.0%)"
   ],
   [
    "GGUF Q4_K_M footprint",
    "5.3 GB (11.7 tok/s CPU)"
   ],
   [
    "Swedish tokenizer fertility",
    "2.08 tok/word (−18% vs Gemma 4)"
   ]
  ],
  "note": "The matched Part 2 comparison reveals a clear architectural trade-off: Prelude 9B has an 18% more efficient Swedish tokenizer and leads Swedish Facts (13.3% vs 6.7%), while Gemma 4 12B dominates forced-choice Swedish grammar (95.3% vs 50.0% on Sv-BLiMP) and avoids greedy repetition loops. Next steps for Prelude are full-parameter Swedish SFT + DPO on LUMI (project_465002530) and FTPO (antidoom) to eliminate greedy repetition.",
  "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": "Matched head-to-head evaluation on hemma-sv-sft-v0.2: Prelude 9B leads Swedish Facts (13.3%), while Gemma 4 12B dominates Sv-BLiMP grammar (95.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": "Validation loss and Apple M5 efficiency across matched 2,000-step QLoRA runs (~664k–680k tokens)."
   }
  ],
  "table": {
   "head": [
    "Metric (4-bit MLX)",
    "Prelude Base",
    "Prelude Hemma v0.1",
    "Prelude Hemma v0.2"
   ],
   "rows": [
    [
     "Training data & tokens",
     "—",
     "v0.1 (10k rows, 426.9k tok)",
     "v0.2 (27.1k rows, 664.0k tok)"
    ],
    [
     "Best validation loss (↓)",
     "1.815 (v0.2 split)",
     "1.434 (v0.1 split)",
     "0.869 (step 1,200)"
    ],
    [
     "Sv-BLiMP (64 pairs)",
     "33/64 (51.6%)",
     "32/64 (50.0%)",
     "32/64 (50.0%)"
    ],
    [
     "Swedish Facts (30 Qs)",
     "3/30 (10.0%)",
     "3/30 (10.0%)",
     "4/30 (13.3%)"
    ],
    [
     "Everyday Swedish email",
     "Garbled typos",
     "Clean, slightly formal",
     "Natural, idiomatic Swedish ✓"
    ],
    [
     "On-device GGUF (Q4_K_M / Q6_K)",
     "—",
     "—",
     "5.3 GB / 7.0 GB (verified ✓)"
    ]
   ]
  }
 },
 "gaps": [
  "Prelude 9B remains at 50.0% on forced-choice Sv-BLiMP minimal pairs even after 664k SFT tokens — fixing discriminative grammar requires larger full-parameter SFT + on-policy DPO on LUMI.",
  "Greedy decoding on open-ended creative prompts can still trigger repetition loops; applying Liquid4All/antidoom (FTPO) is the planned lightweight fix.",
  "Step 1,200 achieved lower validation loss (0.869) than step 2,000 (1.041); both checkpoints are preserved in adapters/ for resumption."
 ],
 "files_sub": "Published on RenaissanceLabsEU/prelude-9b-sv-hemma-v0.2, naiss-se/prelude-9b-sv-hemma-v0.2, and naiss-se/prelude-9b-sv-hemma-v0.2-GGUF.",
 "files": [
  [
   "README.md",
   "Model card with head-to-head results and quickstart"
  ],
  [
   "model.safetensors",
   "Standalone fused 4-bit MLX weights (~4.8 GB)"
  ],
  [
   "adapters/0001200_adapters.safetensors",
   "Lowest-validation-loss step-1,200 LoRA adapter (val 0.869)"
  ],
  [
   "adapters/adapters.safetensors",
   "Final step-2,000 LoRA adapter"
  ],
  [
   "prelude-9b-sv-hemma-v0.2-Q4_K_M.gguf",
   "Token-patched Q4_K_M GGUF (5.3 GB, in -GGUF repo)"
  ],
  [
   "prelude-9b-sv-hemma-v0.2-Q6_K.gguf",
   "Token-patched Q6_K GGUF (7.0 GB, in -GGUF repo)"
  ]
 ],
 "tags": [
  "swedish",
  "supervised-fine-tuning",
  "lora",
  "mlx",
  "prelude",
  "gguf",
  "local-training",
  "hemma"
 ],
 "related_note": "/recipes/gemma-4-12b-sv-hemma-v0.1/",
 "related_note_title": "Head-to-head: Gemma 4 12B Hemma v0.1 recipe",
 "effects": {
  "swedish": [
   "improved",
   "Best val loss 0.869, Swedish Facts 10.0% → 13.3%, natural everyday Swedish emails"
  ],
  "structured": [
   "held",
   "Preserves Swedish JSON schema keys and non-editing controls"
  ],
  "training-cost": [
   "improved",
   "1h 35m on Apple M5 + verified 5.3 GB Q4_K_M GGUF for local Ollama / llama.cpp"
  ]
 }
}
