{
 "id": "prelude-9b-sv-hemma-v0.1",
 "title": "Hemma v0.1: Training a 9B Swedish model at home on an Apple M5 laptop",
 "dish": "Part 1 of a home-forged Swedish Prelude 9B checkpoint (4-bit MLX QLoRA + resumable LoRA adapters)",
 "lede": "Fine-tune OpenEuroLLM's 9.1B Prelude-SFT-Propella-Excellent-v0.2 on 10,023 curated, open-licensed Swedish examples in 50 minutes on a single 32 GB Apple M5 MacBook using 4-bit MLX QLoRA. Validation loss drops from 2.242 to 1.434 (−0.81 nats), garbled Swedish spelling and word-fusion bugs disappear on qualitative probes, and both Swedish JSON keys and non-editing controls stay intact. This is Part 1 — we publish both ready-to-run 4-bit MLX weights and unfused LoRA adapters so we can continue training in Part 2.",
 "stage": "LoRA SFT (Part 1 · Local)",
 "lab": "Renaissance Labs",
 "author": "Birger Moell",
 "date": "2026-12-02",
 "status": "Part 1 complete on Apple M5 (32 GB). Fused 4-bit MLX weights and unfused LoRA adapters published on Hugging Face for Part 2 continuation.",
 "checkpoint": "https://huggingface.co/RenaissanceLabsEU/prelude-9b-sv-hemma-v0.1",
 "repo_files": "https://huggingface.co/RenaissanceLabsEU/prelude-9b-sv-hemma-v0.1/blob/main/",
 "glance": [
  {
   "k": "Makes",
   "v": "1",
   "u": "9.1B 4-bit MLX + LoRA"
  },
  {
   "k": "Runtime",
   "v": "50 min",
   "u": "1,200 steps · 158 tok/s"
  },
  {
   "k": "Equipment",
   "v": "1",
   "u": "Apple M5 laptop (32 GB)"
  },
  {
   "k": "Ingredients",
   "v": "10,023",
   "u": "9,522 train · 501 valid"
  }
 ],
 "data": {
  "sub": "A compact, zero-contamination Swedish SFT mixture built strictly from open-licensed (CC BY 4.0 / Apache 2.0) sources and published directly as RenaissanceLabsEU/hemma-sv-sft-v0.1 on Hugging Face (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.1). Every example is filtered to ≤ 6,000 characters so sequences fit cleanly inside a 2,048-token laptop budget, and strictly disjoint from our held-out evaluation benchmarks (Tarnyd/sv-blimp and liu-nlp/swedish-facts-v1).",
  "groups": [
   {
    "title": "Part 1 Swedish SFT mix (9,522 train + 501 stratified valid)",
    "sub": "Three complementary Swedish buckets combining direct Swedish synthesis, native public-sector QA, and targeted grammar repair with unchanged-correct controls so the model learns not to over-edit already-correct Swedish.",
    "total": 10023,
    "sources": [
     {
      "name": "EuroMultiSynth-Instruct (sv, score ≥ 4)",
      "id": "openeurollm/EuroMultiSynth-Instruct-decontaminated-v0.1 · sv",
      "pairs": 4500,
      "licence": "Apache-2.0",
      "pinned": true,
      "note": "Direct Swedish synthetic instructions filtered to judge_score ≥ 4.0"
     },
     {
      "name": "Swedish grammaticality repair pairs",
      "id": "KeTuTu/swedish-grammaticality-pairs · train",
      "pairs": 3000,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Wrapped in varied Swedish proofreading prompts targeting V2 word order, agreement, and spelling"
     },
     {
      "name": "Upphandlingsmyndigheten native QA",
      "id": "tomdickson/upphandlingsmyndigheten-qa · train",
      "pairs": 2023,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Native-source Swedish public procurement questions and answers"
     },
     {
      "name": "Unchanged-correct Swedish controls",
      "id": "KeTuTu/swedish-grammaticality-pairs · correct -> correct",
      "pairs": 500,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Teaches the model to leave already-correct Swedish sentences untouched when asked to fix only real errors"
     }
    ]
   }
  ],
  "checksums_title": "Fingerprints of the training, validation, and before/after micro-eval files (SHA-256)",
  "checksums": [
   [
    "train/data/train.jsonl",
    "31205ac75658ded40ac70861b43f234f19cb4aa75f25a6446d1c27a91b120f17"
   ],
   [
    "train/data/valid.jsonl",
    "e05855ce6115394974b2e928c67b9594ad7bae82eb089750fbd6db274a23c8ad"
   ],
   [
    "eval/eval_before.json",
    "7d15432454c2be276535db13a4c792a809c88081c6117b17233f550845c78bf9"
   ],
   [
    "eval/eval_after.json",
    "43ac7d82cfa7bbfdb0925e25c48f549c48795db30a578b1e7725d3eea6e8dd31"
   ]
  ]
 },
 "pantry": [
  [
   "Base model",
   "openeurollm/Prelude-SFT-Propella-Excellent-v0.2",
   "9.1B dense Qwen3-style model on prelude-anneal300b-256k-rope20m-cont (256k EU tokenizer, 2.08 SV tok/word)"
  ],
  [
   "Published training dataset",
   "RenaissanceLabsEU/hemma-sv-sft-v0.1",
   "Exact 9,522 train + 501 valid Swedish MLX chat JSONL published under CC BY 4.0 (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.1)"
  ],
  [
   "Local 4-bit base",
   "mlx_lm.convert -q --q-bits 4",
   "4.50 bits/weight (~5.5 GB on disk) — lets a 9.1B model train comfortably inside 32 GB unified memory"
  ],
  [
   "Chat template fix",
   "chat_template.jinja line 1 -> default('no_think')",
   "Prelude's template raises TemplateError unless thinking_instruction='no_think' is set for non-reasoning targets"
  ],
  [
   "Held-out micro-eval",
   "Tarnyd/sv-blimp + liu-nlp/swedish-facts-v1 + 4 probes",
   "Runs in <40 seconds on M5 Metal; strictly excluded from training"
  ],
  [
   "Software",
   "mlx-lm (via uvx) · Python 3.12 · Hugging Face datasets",
   "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": "A full BF16 SFT of a 9.1B model needs ~85 GB (weights + AdamW states + activations), which OOMs on a laptop. Quantizing the frozen base weights to 4-bit MLX (~5.5 GB) and training rank-16 LoRA adapters on the top 16 layers (19.4M trainable params = 0.213%) with gradient checkpointing brings peak memory down to ~10.6 GB — leaving over 20 GB of headroom on a 32 GB MacBook while running at ~158 tok/s."
 },
 "settings_groups": [
  {
   "title": "Part 1: Home Apple Silicon MLX QLoRA",
   "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.399M trainable params = 0.213%)"
    ],
    [
     "Learning rate",
     "1.0e-5 cosine decay (60-step warmup from 1e-7, decaying to 1e-6) — intentionally low to preserve Prelude's 58.4B-token SFT calibration"
    ],
    [
     "Batch & steps",
     "batch_size 1 · grad_accumulation_steps 8 · 1,200 steps · 426.9k supervised tokens (~50 minutes)"
    ],
    [
     "Max sequence length",
     "2,048 tokens with grad_checkpoint: true"
    ],
    [
     "Validation loss",
     "2.242 (step 1) → 1.434 (step 800, best) → 1.565 (step 1,200) on 25 validation batches"
    ]
   ]
  }
 ],
 "sbatch_title": "Complete 'train at home' Apple Silicon MLX QLoRA script (copy & run)",
 "sbatch": "#!/usr/bin/env bash\n# Train Hemma v0.1 at home on a 32 GB Apple Silicon MacBook (~50 minutes).\nset -euo pipefail\n\n# 1. Quantize Prelude-SFT-Propella-Excellent-v0.2 to 4-bit MLX (~5.5 GB, ~35s)\nuvx --from mlx-lm mlx_lm.convert \\\n  --hf-path openeurollm/Prelude-SFT-Propella-Excellent-v0.2 \\\n  --mlx-path ./prelude-excellent-v0.2-4bit -q --q-bits 4\n\n# 2. Default Prelude's custom template to /no_think for non-reasoning SFT targets\nsed -i '' \"s/default(none)/default('no_think')/\" ./prelude-excellent-v0.2-4bit/chat_template.jinja\n\n# 3. Build the 10,023-row curated Swedish dataset (train.jsonl + valid.jsonl)\npython3 prepare_sv_sft_data.py\n\n# 4. Run the <40s baseline Swedish micro-eval before training\ncaffeinate -i uvx --from mlx-lm --with datasets python micro_sv_eval.py \\\n  --model ./prelude-excellent-v0.2-4bit \\\n  --output-json ./logs/eval_before.json\n\n# 5. Train 4-bit MLX QLoRA (1,200 steps, ~158 tok/s, ~50 min on Apple M5)\ncaffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./lora_config.yaml\n\n# 6. Run the exact same <40s Swedish micro-eval with the trained adapter\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-sv-overnight \\\n  --output-json ./logs/eval_after.json\n\n# 7. Fuse LoRA into standalone 4-bit MLX weights (~20s)\nuvx --from mlx-lm mlx_lm.fuse \\\n  --model ./prelude-excellent-v0.2-4bit \\\n  --adapter-path ./adapters/prelude-sv-overnight \\\n  --save-path ./exports/prelude-9b-sv-hemma-v0.1\n",
 "steps": [
  {
   "t": "Quantize the 9B base model to 4-bit MLX",
   "d": "Download openeurollm/Prelude-SFT-Propella-Excellent-v0.2 and convert it once to 4-bit MLX (4.50 bits/weight, ~5.5 GB). This is what makes a 9.1B model train comfortably on a 32 GB MacBook without swapping.",
   "code": "uvx --from mlx-lm mlx_lm.convert \\\n  --hf-path openeurollm/Prelude-SFT-Propella-Excellent-v0.2 \\\n  --mlx-path ./prelude-excellent-v0.2-4bit -q --q-bits 4"
  },
  {
   "t": "Set Prelude's chat template default to /no_think",
   "d": "Prelude-SFT-Propella-Excellent-v0.2 ships a custom chat_template.jinja that raises TemplateError if an assistant message has no <think> block and thinking_instruction is unset. Setting line 1 to default('no_think') lets mlx_lm.lora apply Prelude's exact native /no_think formatting cleanly.",
   "code": "sed -i '' \"s/default(none)/default('no_think')/\" ./prelude-excellent-v0.2-4bit/chat_template.jinja",
   "flag": "If you skip this line, mlx_lm.lora fails immediately on step 0 with Jinja TemplateError: A target without reasoning requires thinking_instruction=no_think."
  },
  {
   "t": "Download (or rebuild) the curated Swedish dataset",
   "d": "Pull the exact 9,522 train + 501 valid Swedish JSONL directly from RenaissanceLabsEU/hemma-sv-sft-v0.1 (CC BY 4.0), or rebuild it from the 3 upstream sources using prepare_sv_sft_data.py.",
   "code": "huggingface-cli download RenaissanceLabsEU/hemma-sv-sft-v0.1 --repo-type dataset --local-dir ./data"
  },
  {
   "t": "Run a <40-second Swedish micro-eval before training",
   "d": "Score 20 Tarnyd/sv-blimp grammar minimal pairs (across v2_violation, subordinate_word_order, gender_agreement, anaphor_agreement), 15 liu-nlp/swedish-facts-v1 questions, and 4 qualitative Swedish probes so you have an exact before/after reference.",
   "code": "uvx --from mlx-lm --with datasets python micro_sv_eval.py \\\n  --model ./prelude-excellent-v0.2-4bit --output-json ./logs/eval_before.json"
  },
  {
   "t": "Train with caffeinate -i and a conservative learning rate",
   "d": "Because Prelude-SFT-Propella-Excellent-v0.2 already completed 58.4B tokens of SFT decaying from 8e-5 to 0, use a gentle 1e-5 cosine schedule with assistant-only loss masking. Wrap in caffeinate -i so macOS never sleeps mid-run.",
   "code": "caffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./lora_config.yaml"
  },
  {
   "t": "Re-run the micro-eval and fuse the weights",
   "d": "Verify before/after improvements on the identical /no_think prompts, then fuse into standalone 4-bit MLX weights while keeping the unfused adapters/ folder (including the lowest-val-loss step-800 checkpoint) for Part 2.",
   "code": "uvx --from mlx-lm mlx_lm.fuse \\\n  --model ./prelude-excellent-v0.2-4bit \\\n  --adapter-path ./adapters/prelude-sv-overnight \\\n  --save-path ./exports/prelude-9b-sv-hemma-v0.1"
  }
 ],
 "verify": {
  "checks": [
   [
    "Validation loss convergence",
    "Drops smoothly from 2.242 (step 1) to 1.434 at step 800 (−0.81 nats), finishing at 1.565 at step 1,200.",
    0
   ],
   [
    "Garbled Swedish spelling eliminated",
    "Baseline errors like 'medduta er', 'gemensammavättstugna', 'ochförtroendet', and 'yrket ni gör' disappear completely in Hemma v0.1.",
    0
   ],
   [
    "Zero regressions on controls",
    "Both no_overedit (leaving correct Swedish untouched) and sv_json (exact Swedish keys myndighet/datum/belopp_sek) remain 100% intact.",
    0
   ],
   [
    "Home hardware throughput",
    "Sustains ~158 tok/s on Apple M5 (~10.6 GB peak unified memory, 50 minutes total wall-clock).",
    0
   ]
  ],
  "metrics": [
   [
    "Best validation loss (step 800)",
    "1.434 (↓ from 2.242)"
   ],
   [
    "Training throughput (Apple M5)",
    "~158 tok/s (50 min)"
   ],
   [
    "Swedish tokenizer fertility",
    "2.08 tok/word (−18% vs Gemma 4)"
   ],
   [
    "Trainable parameters",
    "19.4M (0.213% LoRA)"
   ]
  ],
  "note": "Part 1 proves that a 50-minute home laptop QLoRA pass (427k tokens) dramatically cleans up Prelude's surface Swedish fluency and eliminates word-fusion typos without breaking structured JSON or non-editing controls. Quantitative discrimination on Sv-BLiMP (10/20 = 50.0%) and factual recall on Swedish Facts (1/15 = 6.7%) stayed flat at this small token budget — motivating Part 2 (longer multi-epoch local continuation + full-parameter 200k-row Swedish SFT and on-policy DPO on LUMI).",
  "figures": [
   {
    "src": "/assets/img/hemma-v0.1-home-training-overview.png",
    "alt": "Two-panel chart showing how 4-bit MLX QLoRA fits a 9.1B model into 10.6 GB on a 32 GB MacBook alongside a donut chart of the 10,023-example curated Swedish SFT mix.",
    "caption": "How to train a 9B Swedish model at home: 4-bit MLX QLoRA reduces memory from ~85 GB (full BF16 AdamW) to ~10.6 GB peak on a 32 GB Apple Silicon MacBook, trained on a zero-contamination 10,023-row Swedish mix."
   },
   {
    "src": "/assets/img/hemma-v0.1-training-and-fertility.png",
    "alt": "Two-panel chart showing Hemma v0.1's training and validation loss curve dropping from 2.242 to 1.434 at step 800, next to a Swedish tokenizer fertility bar chart comparing Prelude 9B, Inkling-2-Mini, and Gemma 4 31B.",
    "caption": "Left: Train and validation cross-entropy over 1,200 steps (426.9k tokens, ~50 minutes on Apple M5, best val 1.434 at step 800). Right: Prelude's 256k EU tokenizer uses ~18% fewer tokens on general Swedish and ~27% fewer on Swedish compound words than Gemma 4 31B."
   },
   {
    "src": "/assets/img/hemma-v0.1-before-after-examples.png",
    "alt": "Side-by-side visual comparison of baseline Prelude-SFT-Propella-Excellent-v0.2 vs Hemma v0.1 outputs on a Swedish BRF email and an Offentlighetsprincipen plain-language explanation.",
    "caption": "Before vs. after qualitative Swedish outputs (/no_think). Hemma v0.1 eliminates baseline word-fusion and garbled-word errors ('medduta er', 'gemensammavättstugna', 'ochförtroendet') while preserving non-editing and Swedish JSON controls."
   }
  ],
  "table": {
   "head": [
    "Probe / Metric (/no_think, 4-bit MLX)",
    "Baseline (Propella-Excellent-v0.2)",
    "Hemma v0.1 (50-min M5 QLoRA)"
   ],
   "rows": [
    [
     "Validation loss (25 batches, ↓)",
     "2.242 (step 1)",
     "1.434 (step 800) / 1.565 (step 1,200)"
    ],
    [
     "Sv-BLiMP (20 grammar minimal pairs)",
     "10/20 (50.0%)",
     "10/20 (50.0%)"
    ],
    [
     "Swedish Facts (15 short-answer Qs)",
     "1/15 (6.7%)",
     "1/15 (6.7%)"
    ],
    [
     "plain_gov (Offentlighetsprincipen, ≤3 sent.)",
     "Clunky; fused typo 'ochförtroendet'",
     "Clean, natural 3-sentence Swedish"
    ],
    [
     "natural_email (BRF laundry dryer)",
     "Garbled: 'medduta er', 'gemensammavättstugna', 'yrket ni gör'",
     "Zero garbled words; coherent Swedish email"
    ],
    [
     "no_overedit (already-correct Swedish)",
     "Exact unchanged sentence ✓",
     "Exact unchanged sentence ✓"
    ],
    [
     "sv_json (myndighet / datum / belopp_sek)",
     "Valid Swedish JSON keys ✓",
     "Valid Swedish JSON keys ✓"
    ]
   ]
  }
 },
 "gaps": [
  "Part 1 trained on only 426.9k tokens (~12% of the 9,522-row training file) with 19.4M LoRA parameters: surface Swedish fluency jumped sharply, but grammar discrimination (Sv-BLiMP 50%) and factual recall (Swedish Facts 6.7%) did not move yet.",
  "Stylistic polish is still slightly formal in everyday emails (e.g. 'bekväm boendestatus') — Part 2 will add persona-conditioned everyday Swedish tasks (naiss-se/swedish-personas-v3) and on-policy Swedish DPO.",
  "Step 800 achieved the lowest validation loss (1.434 vs 1.565 at step 1,200); both 0000800_adapters.safetensors and final adapters.safetensors are published in adapters/ so Part 2 can resume from either.",
  "Full 15-dataset EuroEval Swedish BF16 evaluation is queued on LUMI (project_465002530) and will serve as the reference benchmark for Part 2."
 ],
 "files_sub": "Artifacts published in the public Hugging Face model repository (RenaissanceLabsEU/prelude-9b-sv-hemma-v0.1).",
 "files": [
  [
   "README.md",
   "Model card with quickstart, dataset provenance, and before/after micro-eval results"
  ],
  [
   "model.safetensors",
   "Standalone fused 4-bit MLX weights (4.8 GB, ready for mlx_lm.generate)"
  ],
  [
   "adapters/adapters.safetensors",
   "Unfused final step-1,200 LoRA adapter (74 MB) for resuming training in Part 2"
  ],
  [
   "adapters/0000800_adapters.safetensors",
   "Unfused lowest-validation-loss step-800 LoRA adapter (74 MB, val 1.434)"
  ],
  [
   "adapters/lora_config.yaml",
   "Exact mlx_lm.lora configuration file used for the 50-minute Apple M5 run"
  ],
  [
   "eval/eval_before.json",
   "Baseline /no_think Swedish micro-eval JSON before training"
  ],
  [
   "eval/eval_after.json",
   "Post-SFT /no_think Swedish micro-eval JSON after 1,200 steps"
  ]
 ],
 "tags": [
  "swedish",
  "supervised-fine-tuning",
  "lora",
  "mlx",
  "prelude",
  "local-training",
  "hemma"
 ],
 "related_note": "/recipes/",
 "related_note_title": "All Renaissance Labs training recipes",
 "effects": {
  "swedish": [
   "improved",
   "Eliminated garbled Swedish spelling & word-fusion typos on qualitative probes in 50 min"
  ],
  "structured": [
   "held",
   "Preserved exact Swedish JSON schema keys and non-editing controls"
  ],
  "training-cost": [
   "improved",
   "50 minutes on a single 32 GB Apple M5 MacBook (~10.6 GB peak memory, zero HPC)"
  ]
 }
}
