{
 "id": "inkling-2-mini-sv-hemma-v0.1",
 "title": "Hemma on Inkling-2-Mini: Fine-tuning a 26.4B MoE for Swedish in 27 minutes at home",
 "dish": "Home-forged Swedish Inkling-2-Mini 26.4B MoE (4-bit MLX attention QLoRA on Apple M5)",
 "lede": "Apply the Hemma Swedish SFT recipe (hemma-sv-sft-v0.2) to Thinking Machines' Apache-2.0 Inkling-2-Mini (26.4B-total looped MoE) in just 27 minutes on a 32 GB Apple M5 MacBook. By keeping the NVFP4/MXFP8 routed experts frozen, training attention LoRA on the top 4 layers (1.11M trainable params = 0.004%), and filtering sequences to <=500 tokens with mlx_lm's exact ChatDataset to prevent mask_prompt NaN, validation loss drops from 1.451 to 0.731 (best, step 400) / 0.867 (step 2,000), Sv-BLiMP climbs from 89.1% to 92.2% (+3.1 pp), and creative Swedish agreement errors disappear.",
 "stage": "MoE LoRA SFT (Local)",
 "lab": "Renaissance Labs × NAISS",
 "author": "Birger Moell",
 "date": "2026-12-03",
 "status": "Complete on Apple M5 (32 GB). Fused 4-bit MLX weights and unfused step-400/step-2000 LoRA adapters ready.",
 "checkpoint": "https://huggingface.co/naiss-se/inkling-2-mini-sv-hemma-v0.1",
 "repo_files": "https://huggingface.co/naiss-se/inkling-2-mini-sv-hemma-v0.1/blob/main/",
 "glance": [
  {
   "k": "Makes",
   "v": "1",
   "u": "26.4B MoE 4-bit MLX + LoRA"
  },
  {
   "k": "Runtime",
   "v": "27 min",
   "u": "2,000 steps · 202.4k tok"
  },
  {
   "k": "Equipment",
   "v": "1",
   "u": "Apple M5 laptop (32 GB)"
  },
  {
   "k": "Sv-BLiMP",
   "v": "92.2%",
   "u": "59/64 (+3.1 pp vs base)"
  }
 ],
 "data": {
  "sub": "Built from the <=500-token, zero-truncation slice (8,435 train + 418 valid rows) of RenaissanceLabsEU/hemma-sv-sft-v0.2 (https://huggingface.co/datasets/RenaissanceLabsEU/hemma-sv-sft-v0.2), spanning all 9 open-licensed Swedish and replay buckets while guaranteeing >=8 unmasked assistant tokens per example.",
  "groups": [
   {
    "title": "hemma-sv-sft-v0.2 <=500-token zero-truncation slice (8,435 train + 418 valid)",
    "sub": "Filtered using mlx_lm's exact ChatDataset tokenizer so 100% of rows fit inside max_seq_length=512 with zero prompt truncation and zero 0/0 NaN loss under mask_prompt: true.",
    "total": 8853,
    "sources": [
     {
      "name": "hemma-sv-sft-v0.2 (<=500 tok, train split)",
      "id": "RenaissanceLabsEU/hemma-sv-sft-v0.2 · filtered train",
      "pairs": 8435,
      "licence": "CC-BY-4.0 / Apache-2.0 / CC0",
      "pinned": true,
      "note": "All 9 Swedish + replay buckets filtered with mlx_lm ChatDataset (max_len=500, >=8 assistant tokens)"
     },
     {
      "name": "hemma-sv-sft-v0.2 (<=500 tok, valid split)",
      "id": "RenaissanceLabsEU/hemma-sv-sft-v0.2 · filtered valid",
      "pairs": 418,
      "licence": "CC-BY-4.0 / Apache-2.0 / CC0",
      "pinned": true,
      "note": "Stratified held-out validation split (max_len=500, >=8 assistant tokens)"
     }
    ]
   }
  ],
  "checksums_title": "SHA-256 fingerprints of the <=500-token Inkling-2-Mini data splits",
  "checksums": [
   [
    "train/data_v02_inkling512/train.jsonl",
    "ac99af9056528e34f907dc72b3f9ef77873dbc6d91d00561b98d774aa70ab9fb"
   ],
   [
    "train/data_v02_inkling512/valid.jsonl",
    "f384f908f4748a8ac7f22319f7aa395b19fb88cd2fd5fb240ba2ce4a4f96b648"
   ]
  ]
 },
 "pantry": [
  [
   "Base model",
   "mlx-community/Inkling-2-Mini-4bit (thinkingmachines/Inkling-2-Mini)",
   "26.4B-total Apache 2.0 looped MoE (8 active experts/tok, 24 layers, 2.40 SV tok/word, 17.3 GB on disk)"
  ],
  [
   "mlx-lm branch",
   "git+https://github.com/ml-explore/mlx-lm.git@refs/pull/2086/head",
   "Adds native inkling2_loop architecture support (NVFP4/MXFP8 routed experts + 4-bit affine attention)"
  ],
  [
   "Chat template fix",
   "reasoning_effort default 0.9 → 0.0 in symlinked chat_template.jinja",
   "Inkling-2-Mini defaults to high thinking (0.9); defaulting to 0.0 keeps non-thinking SFT calibrated"
  ],
  [
   "Exact ChatDataset length filter",
   "len(tokens) <= 500 and (len(tokens) - offset) >= 8",
   "Critical: avoids mask_prompt 0/0 NaN loss when long prompts get truncated at max_seq_length=512"
  ]
 ],
 "equipment": {
  "nodes": 1,
  "gpus_per_node": 1,
  "gpu": "Apple M5 (32 GB unified memory)",
  "sub": "Because Inkling-2-Mini-4bit weights occupy 17.3 GB against macOS's ~21.3 GB default Metal wired cap, targeting attention projections (q/k/v/o) on the top 4 layers (1.114M params = 0.004%) with max_seq_length=512 and grad_checkpoint=true keeps peak memory safely under the cap while sustaining ~130–160 tok/s (27 minutes for 2,000 steps)."
 },
 "settings_groups": [
  {
   "title": "Home Apple Silicon MoE Attention QLoRA",
   "rows": [
    [
     "Objective",
     "Causal LM cross-entropy, assistant-only loss (mask_prompt: true)"
    ],
    [
     "Adapter",
     "LoRA rank 16, scale 32, dropout 0.05 on self_attn.{q,k,v,o}_proj across top 4 layers (1.114M params = 0.004%)"
    ],
    [
     "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 4 · 2,000 steps · 202.4k tokens (27 min)"
    ],
    [
     "Validation loss",
     "1.451 (step 1) → 0.731 (step 400, best) → 0.867 (step 2,000)"
    ]
   ]
  }
 ],
 "sbatch_title": "Complete Inkling-2-Mini Hemma v0.1 training script (27 min on Apple M5)",
 "sbatch": "#!/usr/bin/env bash\n# Train Hemma v0.1 on Inkling-2-Mini-4bit (26.4B MoE) in 27 minutes on Apple M5.\nset -euo pipefail\n\nMLX_SPEC=\"git+https://github.com/ml-explore/mlx-lm.git@refs/pull/2086/head\"\n\n# 1. Symlink cached mlx-community/Inkling-2-Mini-4bit with reasoning_effort default=0.0\n# 2. Filter hemma-sv-sft-v0.2 with mlx_lm ChatDataset to <=500 tokens & >=8 assistant tokens\n# 3. Train 2,000 steps (27 min, ~130-160 tok/s, zero OOM, zero NaN)\ncaffeinate -i uvx --from \"$MLX_SPEC\" mlx_lm.lora --config ./lora_v02_inkling2_mini.yaml\n\n# 4. Evaluate before vs after (pre-filling <|message_model|><|content_text|> for direct answers)\ncaffeinate -i uvx --from \"$MLX_SPEC\" --with datasets \\\n  python micro_sv_eval.py \\\n  --model ./mlx_models/inkling-2-mini-4bit \\\n  --adapter-path ./adapters/inkling-2-mini-sv-hemma-v0.1 \\\n  --output-json ./logs/v02_eval_inkling2_mini_after.json\n\n# 5. Fuse 4-bit MLX release weights\nuvx --from \"$MLX_SPEC\" mlx_lm.fuse \\\n  --model ./mlx_models/inkling-2-mini-4bit \\\n  --adapter-path ./adapters/inkling-2-mini-sv-hemma-v0.1 \\\n  --save-path ./exports/inkling-2-mini-sv-hemma-v0.1\n",
 "steps": [
  {
   "t": "Use the mlx-lm PR #2086 branch for inkling2_loop",
   "d": "PyPI mlx-lm 0.32.2 does not yet include inkling2_loop. Run directly via uvx from git+https://github.com/ml-explore/mlx-lm.git@refs/pull/2086/head.",
   "code": "uvx --from \"git+https://github.com/ml-explore/mlx-lm.git@refs/pull/2086/head\" mlx_lm.lora --help"
  },
  {
   "t": "Filter sequences with mlx_lm's exact ChatDataset",
   "d": "Filter hemma-sv-sft-v0.2 to len(tokens) <= 500 and (len(tokens) - offset) >= 8 using mlx_lm's own ChatDataset. Filtering with transformers AutoTokenizer alone lets some >512-token rows slip through, causing mask_prompt truncation NaN.",
   "code": "# Keeps 8,435 train / 418 valid rows with max_len=500 and zero truncation warnings",
   "flag": "If any prompt exceeds max_seq_length=512 under mask_prompt: true, all assistant tokens are truncated off -> 0/0 = NaN loss poisons the LoRA weights."
  },
  {
   "t": "Train attention LoRA on top 4 layers (27 min)",
   "d": "Keep NVFP4/MXFP8 routed experts frozen and adapt self_attn.{q,k,v,o}_proj on the top 4 layers (1.114M params) so peak Metal memory stays comfortably inside 32 GB.",
   "code": "caffeinate -i uvx --from \"git+https://github.com/ml-explore/mlx-lm.git@refs/pull/2086/head\" mlx_lm.lora --config ./lora_v02_inkling2_mini.yaml"
  },
  {
   "t": "Pre-fill <|message_model|><|content_text|> for non-thinking eval",
   "d": "Inkling-2-Mini's chat template does not emit <|message_model|><|content_text|> on add_generation_prompt=True. Appending it lets you evaluate direct Swedish answers without burning tokens inside <|content_thinking|>.",
   "code": "python micro_sv_eval.py --model ./mlx_models/inkling-2-mini-4bit --adapter-path ./adapters/inkling-2-mini-sv-hemma-v0.1"
  }
 ],
 "verify": {
  "checks": [
   [
    "Validation loss drops by half",
    "Drops from 1.451 (step 1) to 0.731 at step 400 (best) and 0.867 at step 2,000, with zero NaN and zero OOM.",
    0
   ],
   [
    "Sv-BLiMP climbs 89.1% → 92.2% (+3.1 pp)",
    "Improves from 57/64 to 59/64 across all 16 Swedish grammar phenomena in just 202.4k tokens.",
    0
   ],
   [
    "Creative Swedish agreement errors eliminated",
    "Base errors like 'en svag, osäker ljus' and 'I det tystnaden' disappear completely in Hemma v0.1, alongside proper constitutional verbs ('riksdagen stiftar', 'utfärdar').",
    0
   ],
   [
    "Zero regressions on JSON & code",
    "Preserves exact raw Swedish JSON, non-editing controls, and adds input validation in Swedish Python code.",
    0
   ]
  ],
  "metrics": [
   [
    "Sv-BLiMP (64 pairs)",
    "92.2% (↑ from 89.1%)"
   ],
   [
    "Best validation loss (step 400)",
    "0.731 (↓ from 1.451)"
   ],
   [
    "Wall-clock on Apple M5",
    "27 min (~130–160 tok/s)"
   ],
   [
    "Trainable parameters",
    "1.114M (0.004% of 26.4B)"
   ]
  ],
  "note": "In just 27 minutes and 0.004% trainable parameters, Hemma pushes Inkling-2-Mini (26.4B Apache 2.0 MoE) from 89.1% to 92.2% on Sv-BLiMP while eliminating subtle Swedish gender/definiteness agreement errors in creative prose and adopting natural Swedish klarspråk.",
  "figures": [
   {
    "src": "/assets/img/hemma-v0.2-three-way-comparison.png",
    "alt": "Two-panel chart showing 3-way Sv-BLiMP before/after comparison for Prelude 9B, Gemma 4 12B, and Inkling-2-Mini alongside Inkling-2-Mini's 27-minute validation loss curve.",
    "caption": "Left: 3-way matched Sv-BLiMP comparison across Prelude 9B, Gemma 4 12B (87.5% → 95.3%), and Inkling-2-Mini (89.1% → 92.2%). Right: Inkling-2-Mini Hemma v0.1 validation loss over 2,000 steps (27 minutes on Apple M5)."
   }
  ],
  "table": {
   "head": [
    "Metric (4-bit MLX)",
    "Inkling-2-Mini Base",
    "Inkling-2-Mini Hemma v0.1",
    "Gemma 4 12B Hemma v0.1"
   ],
   "rows": [
    [
     "Validation loss (↓)",
     "1.451 (step 1)",
     "0.731 (step 400) / 0.867 (step 2,000)",
     "1.059 (step 2,000)"
    ],
    [
     "Sv-BLiMP (64 pairs, 16 phenomena)",
     "57/64 (89.1%)",
     "59/64 (92.2%)",
     "61/64 (95.3%)"
    ],
    [
     "Swedish Facts (30 Qs)",
     "2/30 (6.7%)",
     "2/30 (6.7%)",
     "2/30 (6.7%)"
    ],
    [
     "Creative Swedish agreement",
     "Errors: 'en svag, osäker ljus', 'I det tystnaden'",
     "Zero agreement errors, natural prose ✓",
     "Zero agreement errors ✓"
    ],
    [
     "Wall-clock & tokens on M5",
     "—",
     "27 min (202.4k tokens)",
     "1h 40m (679.9k tokens)"
    ]
   ]
  }
 },
 "gaps": [
  "Only sequences <=500 tokens (8,435 of 25,764 train rows) were used to stay inside macOS's ~21.3 GB Metal wired limit on a 32 GB MacBook; full 2,048-token MoE training can run on H200 (Berzelius).",
  "On grammar proofreading prompts that also request an explanation, Hemma v0.1 outputs the exact corrected sentence cleanly but skips the explanation because the KeTuTu grammar-repair SFT pairs omit explanations.",
  "Step 400 achieved the lowest validation loss (0.731 vs 0.867 at step 2,000); both checkpoints are preserved in adapters/."
 ],
 "files_sub": "Published in both 4-bit MLX fused form and unfused LoRA adapters.",
 "files": [
  [
   "README.md",
   "Model card with quantitative 3-way table and real side-by-side qualitative base vs. Hemma examples"
  ],
  [
   "model-*.safetensors",
   "Standalone fused 4-bit MLX weights (~16.1 GB)"
  ],
  [
   "adapters/0000400_adapters.safetensors",
   "Lowest-validation-loss step-400 LoRA adapter (val 0.731)"
  ],
  [
   "adapters/adapters.safetensors",
   "Final step-2,000 LoRA adapter"
  ],
  [
   "eval/v02_eval_inkling2_mini_*.json",
   "Before and after Swedish micro-eval JSON logs"
  ]
 ],
 "tags": [
  "swedish",
  "supervised-fine-tuning",
  "lora",
  "mlx",
  "moe",
  "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",
   "Sv-BLiMP 89.1% → 92.2% (+3.1 pp) and eliminates creative Swedish gender/definiteness agreement errors"
  ],
  "structured": [
   "improved",
   "Adds input validation in Swedish Python code while preserving exact raw JSON and non-editing controls"
  ],
  "training-cost": [
   "improved",
   "27 minutes on a 32 GB Apple M5 MacBook for a 26.4B MoE (0.004% trainable params)"
  ]
 }
}
