{
 "id": "swedish-decision-qwen3-1.7b-mlx",
 "title": "Single-token MLX LoRA and temperature scaling for calibrated Swedish agency decisions",
 "dish": "A 1.7B 4-bit model for calibrated Swedish public-administration decisions on a laptop",
 "lede": "Fine-tune a 4-bit Qwen3-1.7B model on 2,731 Swedish agency decision tasks in six minutes on an Apple M5 laptop, then fit a single scalar temperature on a separate calibration split. Frozen holdout accuracy rises from 34.0% to 53.8% (+19.7pp), uncertainty handling jumps +65.7pp to 78.1%, and calibration error (ECE-10) is cut more than in half from 0.184 to 0.080.",
 "stage": "LoRA SFT + temperature calibration",
 "lab": "Renaissance Labs",
 "author": "Birger Moell",
 "date": "2026-11-29",
 "status": "Run once on Apple M5 (32 GB). Evaluated once on two untouched frozen holdouts. Companion open datasets published on Hugging Face.",
 "checkpoint": "https://huggingface.co/RenaissanceLabsEU/swedish-decision-qwen3-1.7b-mlx",
 "repo_files": "https://huggingface.co/RenaissanceLabsEU/swedish-decision-qwen3-1.7b-mlx/blob/main/",
 "glance": [
  {
   "k": "Makes",
   "v": "1",
   "u": "1.7B 4-bit MLX model"
  },
  {
   "k": "Stages",
   "v": "2",
   "u": "LoRA SFT, then temperature T"
  },
  {
   "k": "Equipment",
   "v": "1",
   "u": "Apple M5 laptop (32 GB)"
  },
  {
   "k": "Ingredients",
   "v": "2,731",
   "u": "train rows · 216 valid · 261 cal"
  }
 ],
 "data": {
  "sub": "One supervised fine-tuning mix and one strictly separate calibration split. All frozen holdout tasks (532 in swedish-decision-dataset-v1 and 88 in swedish-workflow-training-v1) are excluded from training and calibration. The publicly licensed Swedish and 53-language global companion corpora are published on Hugging Face.",
  "groups": [
   {
    "title": "Stage 1: LoRA SFT mix (single-letter choice supervision)",
    "sub": "2,731 training examples and 216 stratified validation examples, formatted with full JSON case state, lettered options A–E, and an empty Qwen3 think block so loss is computed strictly on the decision letter.",
    "total": 2731,
    "sources": [
     {
      "name": "Swedish agency workflow & typed decisions",
      "id": "swedish-workflow-training-v1/train/combined-with-v1.jsonl",
      "pairs": 1931,
      "licence": "provisional local training split",
      "pinned": true,
      "note": "90% stratified split across 5 decision types and 15+ Swedish agencies (216 held out for valid.jsonl)"
     },
     {
      "name": "Förvaltningslagen procedural next steps",
      "id": "RenaissanceLabsEU/swedish-public-decisions-v1 · forvaltning_nasta_steg",
      "pairs": 400,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Train split only from birgermoell/myndighetsbeslut"
     },
     {
      "name": "CSN student-aid statutory decisions",
      "id": "RenaissanceLabsEU/swedish-public-decisions-v1 · csn_studiemedel",
      "pairs": 200,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "Train split only, prompts ≤ 4,500 chars"
     },
     {
      "name": "ÖKS & ÖNH real Swedish appeal boards",
      "id": "RenaissanceLabsEU/swedish-public-decisions-v1 · oks + onh",
      "pairs": 200,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "100 ÖKS + 100 ÖNH real appeal decisions (train split only)"
     }
    ]
   },
   {
    "title": "Companion open datasets published alongside this run",
    "sub": "Curated and published on Hugging Face under RenaissanceLabsEU so others can train Swedish and multilingual decision models without scraping.",
    "total": 55013,
    "sources": [
     {
      "name": "Swedish Public Decisions v1",
      "id": "RenaissanceLabsEU/swedish-public-decisions-v1",
      "pairs": 14654,
      "licence": "CC-BY-4.0",
      "pinned": true,
      "note": "6 Swedish agencies (CSN, Förvaltningslagen, Skolinspektionen, JO, ÖKS, ÖNH, Upphandlingsmyndigheten)"
     },
     {
      "name": "Global Decisions v1 (53 languages)",
      "id": "RenaissanceLabsEU/global-decisions-v1",
      "pairs": 40359,
      "licence": "CC-BY-SA-4.0 (77.2% CC-BY-4.0 / Apache-2.0 / CC0)",
      "pinned": true,
      "note": "All 23 official EU languages + Basque + 29 global languages across 6 macro-regions and 7 decision domains"
     }
    ]
   }
  ],
  "checksums_title": "Fingerprints of the training, validation, and paired evaluation files (SHA-256)",
  "checksums": [
   [
    "swedish-decision-qwen3/data/train.jsonl",
    "70400c66a1d460d9d3f96e6594be92f2dd5940a0b470489e7dd9372c45822600"
   ],
   [
    "swedish-decision-qwen3/data/valid.jsonl",
    "23d83f53dea857d08cf3e1d7aef0f8040e00c4948d7ebc658c4ef6446d5a4e86"
   ],
   [
    "eval_v1_paired.json",
    "cbfa0639538ba8923bb0dd6f6fc3588776609ab8253dda444a1e3f52788a9e24"
   ],
   [
    "eval_wft1_paired.json",
    "9245eaf758cc4127b0f98a1b6e43f254b30a902b6b2cdae51456fa72813f7af6"
   ]
  ]
 },
 "pantry": [
  [
   "Base model",
   "mlx-community/Qwen3-1.7B-4bit",
   "Apache-2.0, 1.72B parameters, 4-bit MLX quantization"
  ],
  [
   "Swedish companion dataset",
   "RenaissanceLabsEU/swedish-public-decisions-v1",
   "14,654 CC-BY-4.0 rows across 6 Swedish agencies"
  ],
  [
   "Global companion dataset",
   "RenaissanceLabsEU/global-decisions-v1",
   "40,359 rows across 53 languages (all 23 EU official + global)"
  ],
  [
   "Embedding baseline",
   "RenaissanceLabsEU/swedish-decision-saga-v1",
   "SagaEmbed/saga-embed_v1 frozen embeddings + 3-seed residual bilinear head"
  ],
  [
   "Software",
   "mlx-lm (via uv) · Python 3.9 · NumPy · Matplotlib",
   "Single-forward-pass logit extraction over choice token IDs A–G"
  ]
 ],
 "equipment": {
  "nodes": 1,
  "gpus_per_node": 1,
  "gpu": "Apple M5 (32 GB unified memory)",
  "sub": "The entire LoRA training run took 6 minutes on a single Apple M5 laptop using ~3.5 GB of unified memory. Fusing the adapter took 11 seconds. Scoring all 939 calibration and holdout prompts took ~22 minutes via single-forward-pass logit extraction."
 },
 "settings_groups": [
  {
   "title": "Stage 1: MLX LoRA SFT",
   "rows": [
    [
     "Objective",
     "Causal LM cross-entropy, prompt masked (--mask-prompt), supervised strictly on the single choice letter"
    ],
    [
     "Adapter",
     "LoRA rank 8 on top 16 layers (4.981M trainable params = 0.289%)"
    ],
    [
     "Learning rate",
     "5e-5 (Adam; an earlier 2e-4 run reached val 0.429 at iter 100 then oscillated)"
    ],
    [
     "Batch & steps",
     "batch_size 1 × grad_accumulation 8 · 350 optimizer steps · checkpoint selected at iter 250"
    ],
    [
     "Max sequence length",
     "2,048 tokens with --grad-checkpoint (zero truncation on holdout tasks)"
    ],
    [
     "Validation loss",
     "14.841 → 0.134 at iter 250 (216 stratified validation tasks)"
    ]
   ]
  },
  {
   "title": "Stage 2: Post-hoc temperature scaling",
   "rows": [
    [
     "Method",
     "160-point log-grid search for scalar temperature T minimizing NLL on calibration/"
    ],
    [
     "Zero-shot T",
     "T = 8.00 (raw cal NLL 5.366 → 1.336; severe zero-shot overconfidence)"
    ],
    [
     "Fine-tuned T (v1)",
     "T = 1.166 (raw cal NLL 1.049 → 1.042; already naturally calibrated)"
    ],
    [
     "Fine-tuned T (wft1)",
     "T = 2.942 on the 58-task unseen-agency calibration split"
    ]
   ]
  }
 ],
 "sbatch_title": "End-to-end Apple Silicon MLX training, calibration, and frozen evaluation script",
 "sbatch": "#!/usr/bin/env bash\n# Runs end-to-end on a single Apple Silicon Mac (32 GB unified memory, ~3.5 GB peak).\nset -euo pipefail\n\n# 1. Prepare single-letter prompt/completion data (2,731 train / 216 valid)\npython3 swedish-decision-qwen3/prepare_data.py\n\n# 2. Score zero-shot baseline before training\nuv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py --tag before\n\n# 3. Train 4-bit MLX LoRA (lr=5e-5, grad-accum=8, ~6 minutes on M5)\nuv run --with mlx-lm python -m mlx_lm lora \\\n  --model mlx-community/Qwen3-1.7B-4bit --train \\\n  --data swedish-decision-qwen3/data \\\n  --adapter-path swedish-decision-qwen3/adapters_lr5e5 \\\n  --fine-tune-type lora --mask-prompt --grad-checkpoint \\\n  --batch-size 1 --grad-accumulation-steps 8 --iters 350 \\\n  --learning-rate 5e-5 --num-layers 16 --max-seq-length 2048 \\\n  --val-batches 25 --steps-per-eval 50 --save-every 50\n\n# 4. Stage lowest validation-loss checkpoint (iter 250, val=0.134) and score once on frozen holdouts\nmkdir -p swedish-decision-qwen3/best_adapter\ncp swedish-decision-qwen3/adapters_lr5e5/0000250_adapters.safetensors swedish-decision-qwen3/best_adapter/adapters.safetensors\ncp swedish-decision-qwen3/adapters_lr5e5/adapter_config.json swedish-decision-qwen3/best_adapter/adapter_config.json\nuv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py --tag after --adapter-path swedish-decision-qwen3/best_adapter\n\n# 5. Official paired evaluation on both untouched frozen holdouts\npython3 swedish-decision-dataset-v1/evaluate.py --split test \\\n  --predictions swedish-decision-qwen3/preds/before_v1_holdout_cal.jsonl \\\n  --compare swedish-decision-qwen3/preds/after_v1_holdout_cal.jsonl \\\n  --output swedish-decision-qwen3/eval_v1_paired.json\npython3 swedish-workflow-training-v1/evaluate.py --split test \\\n  --predictions swedish-decision-qwen3/preds/before_wft1_holdout_cal.jsonl \\\n  --compare swedish-decision-qwen3/preds/after_wft1_holdout_cal.jsonl \\\n  --output swedish-decision-qwen3/eval_wft1_paired.json\n\n# 6. Fuse into standalone 4-bit MLX weights\nuv run --with mlx-lm python -m mlx_lm fuse \\\n  --model mlx-community/Qwen3-1.7B-4bit \\\n  --adapter-path swedish-decision-qwen3/best_adapter \\\n  --save-path swedish-decision-qwen3/fused\n",
 "steps": [
  {
   "t": "Format prompts with full JSON state and empty think block",
   "d": "Render every field in state as compact JSON (so unseen agency schemas are never dropped), list sorted criteria as A–E, and end the prompt with Qwen3's empty <think> block so the next token is the decision letter.",
   "code": "python3 swedish-decision-qwen3/prepare_data.py"
  },
  {
   "t": "Score the zero-shot baseline before training",
   "d": "Run one forward pass per task on calibration/ and holdout/, extract the logits for token IDs A–E, fit scalar T on calibration/, and save predictions.",
   "code": "uv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py --tag before"
  },
  {
   "t": "Train the 4-bit MLX LoRA adapter",
   "d": "Keep learning rate conservative (5e-5) with gradient accumulation 8; single-token supervision oscillates at 2e-4. Select the lowest validation-loss checkpoint (iter 250, val 0.134) without touching holdouts.",
   "code": "uv run --with mlx-lm python -m mlx_lm lora \\\n  --model mlx-community/Qwen3-1.7B-4bit --train \\\n  --data swedish-decision-qwen3/data \\\n  --adapter-path swedish-decision-qwen3/adapters_lr5e5 \\\n  --fine-tune-type lora --mask-prompt --grad-checkpoint \\\n  --batch-size 1 --grad-accumulation-steps 8 --iters 350 \\\n  --learning-rate 5e-5 --num-layers 16 --max-seq-length 2048 \\\n  --val-batches 25 --steps-per-eval 50 --save-every 50"
  },
  {
   "t": "Calibrate and evaluate once on both frozen holdouts",
   "d": "Score calibration/ and holdout/ with the iter-250 adapter, then run the untouched official evaluate.py --split test paired against zero-shot.",
   "code": "uv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py \\\n  --tag after --adapter-path swedish-decision-qwen3/best_adapter\npython3 swedish-decision-dataset-v1/evaluate.py --split test \\\n  --predictions preds/before_v1_holdout_cal.jsonl \\\n  --compare preds/after_v1_holdout_cal.jsonl --output eval_v1_paired.json"
  },
  {
   "t": "Fuse the LoRA weights into a standalone MLX checkpoint",
   "d": "Merge the iter-250 LoRA weights into standalone 4-bit safetensors and write the model card afterwards (mlx_lm fuse writes a stub README).",
   "code": "uv run --with mlx-lm python -m mlx_lm fuse \\\n  --model mlx-community/Qwen3-1.7B-4bit \\\n  --adapter-path swedish-decision-qwen3/best_adapter \\\n  --save-path swedish-decision-qwen3/fused"
  }
 ],
 "verify": {
  "checks": [
   [
    "Validation convergence",
    "Cross-entropy on 216 stratified tasks drops 14.841 → 0.134 at iter 250.",
    0
   ],
   [
    "Primary frozen holdout (n=532)",
    "Accuracy 34.0% → 53.8% (+19.7pp, 95% agency-cluster CI [+9.1%, +24.1%]).",
    0
   ],
   [
    "Calibration quality",
    "ECE-10 drops 0.184 → 0.080; NLL 1.356 → 1.116; selective accuracy 78.8% at conf ≥ 0.70.",
    0
   ],
   [
    "Unseen agency holdout (n=88)",
    "Elsäkerhetsverket accuracy rises 33.0% → 38.6% (+5.7pp).",
    0
   ]
  ],
  "metrics": [
   [
    "Frozen holdout accuracy (v1, n=532)",
    "53.8% (+19.7pp)"
   ],
   [
    "Uncertainty handling accuracy",
    "78.1% (+65.7pp)"
   ],
   [
    "Calibration error (ECE-10)",
    "0.080 (↓ from 0.184)"
   ],
   [
    "Selective accuracy (conf ≥ 0.70)",
    "78.8%"
   ]
  ],
  "note": "Evaluated strictly once on FROZEN.json-verified holdouts. Comparing against our Swedish embedding model (swedish-decision-saga-v1, 56.0% overall) reveals a sharp architectural split: Qwen3-1.7B LoRA wins uncertainty (+28.6pp), evidence sufficiency (+7.6pp), and calibration (ECE 0.080 vs 0.168), while SagaEmbed dominates document relevance (81.2% vs 33.9%).",
  "figure": {
   "src": "/assets/img/swedish-decision-qwen3-comparison.png",
   "alt": "Grouped bar chart comparing frozen holdout accuracy across five decision types for Qwen3-1.7B zero-shot, swedish-decision-saga-v1, and swedish-decision-qwen3-1.7b-mlx.",
   "caption": "Frozen holdout accuracy by decision type on swedish-decision-dataset-v1 (n=532, 22 agency clusters). Qwen3-1.7B LoRA excels at uncertainty and evidence sufficiency with half the calibration error; SagaEmbed dominates semantic document relevance."
  },
  "table": {
   "head": [
    "swedish-decision-dataset-v1 (n=532, frozen)",
    "Qwen3-1.7B Zero-Shot",
    "SagaEmbed (saga-v1)",
    "Qwen3-1.7B LoRA (this recipe)"
   ],
   "rows": [
    [
     "Overall accuracy",
     "34.0%",
     "56.0%",
     "53.8% (+19.7pp)"
    ],
    [
     "Evidence sufficiency",
     "78.1%",
     "70.5%",
     "78.1%"
    ],
    [
     "Uncertainty handling",
     "12.4%",
     "49.5%",
     "78.1% (+65.7pp)"
    ],
    [
     "Next step",
     "4.8%",
     "40.0%",
     "40.0% (+35.2pp)"
    ],
    [
     "Rule application",
     "40.0%",
     "37.1%",
     "40.0%"
    ],
    [
     "Document relevance",
     "34.8%",
     "81.2%",
     "33.9%"
    ],
    [
     "Negative log-likelihood (NLL ↓)",
     "1.356",
     "1.168",
     "1.116"
    ],
    [
     "Multiclass Brier score (↓)",
     "0.712",
     "0.596",
     "0.587"
    ],
    [
     "Calibration error ECE-10 (↓)",
     "0.184",
     "0.168",
     "0.080"
    ],
    [
     "Selective accuracy at conf ≥ 0.70",
     "76.4%",
     "67.8%",
     "78.8%"
    ]
   ]
  }
 },
 "gaps": [
  "Single-letter non-thinking supervision plateaus at 40.0% on rule_application: confusion matrices show 40 no → yes and 40 record_no → record_yes collapses on numeric/date comparisons. Oracle-verified <think> reasoning traces are the clear next step.",
  "Document relevance (33.9%) trails our Swedish embedding model swedish-decision-saga-v1 (81.2%) by 47.3pp; production pipelines should pair a Swedish dense encoder for relevance with a calibrated generative model for uncertainty and evidence checks.",
  "Frozen holdout labels are source-grounded programmatic labels pending independent legal review.",
  "General Swedish chat, coding, and math regressions were not measured on this checkpoint; it is specialized for typed choice logit scoring.",
  "The combined-with-v1.jsonl training file contains local Swedish agency web snapshots not yet cleared for blanket redistribution; the companion datasets swedish-public-decisions-v1 (14.7K, CC-BY-4.0) and global-decisions-v1 (40.4K, 53 languages) publish the cleanly open-licensed corpora."
 ],
 "files_sub": "Artifacts published in the Hugging Face model repository.",
 "files": [
  [
   "README.md",
   "Full Run-Card model card with paired holdout tables and MLX logit-scoring snippet"
  ],
  [
   "eval_v1_paired.json",
   "Official paired evaluate.py output on swedish-decision-dataset-v1 (n=532)"
  ],
  [
   "eval_wft1_paired.json",
   "Official paired evaluate.py output on swedish-workflow-training-v1 (n=88)"
  ],
  [
   "assets/decision_type_comparison.png",
   "Per-decision-type accuracy comparison across zero-shot, SagaEmbed, and Qwen3-1.7B LoRA"
  ],
  [
   "assets/calibration_and_training.png",
   "NLL / Brier / ECE-10 comparison and LoRA validation loss trajectory"
  ]
 ],
 "tags": [
  "decisions",
  "calibration",
  "supervised-fine-tuning",
  "lora",
  "mlx",
  "swedish",
  "multilingual"
 ],
 "related_note": "/capabilities/decisions/",
 "related_note_title": "Decisions capability page",
 "effects": {
  "decisions": [
   "improved",
   "Frozen holdout accuracy 34.0% → 53.8% (+19.7pp); uncertainty 12.4% → 78.1%"
  ],
  "abstention": [
   "improved",
   "Calibration error ECE-10 halved (0.184 → 0.080); 78.8% selective accuracy at conf ≥ 0.70"
  ],
  "structured": [
   "improved",
   "Single-forward-pass calibrated probability distributions over A–E criteria"
  ],
  "reasoning": [
   "mixed",
   "Non-thinking mode plateaus at 40.0% on numeric/date rule_application; <think> traces needed next"
  ],
  "training-cost": [
   "improved",
   "6 minutes on a single Apple M5 laptop (32 GB unified memory, 4-bit MLX LoRA)"
  ]
 }
}
