Renaissance LabsLearn

Recipes · LoRA SFT + temperature calibration

Single-token MLX LoRA and temperature scaling for calibrated Swedish agency decisions

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.

From Renaissance LabsRecipe by Birger MoellRun once on Apple M5 (32 GB). Evaluated once on two untouched frozen holdouts. Companion open datasets published on Hugging Face.

CapabilityStructured outputKnowing what it doesn't knowCalibrated decisionsCost to train

DecisionsCalibrationSupervised fine-tuningLoraMlxSwedishMultilingual

Makes11.7B 4-bit MLX model
Stages2LoRA SFT, then temperature T
Equipment1Apple M5 laptop (32 GB)
Ingredients2,731train rows · 216 valid · 261 cal

1Ingredients

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.

Stage 1: LoRA SFT mix (single-letter choice supervision)

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.

  • Swedish agency workflow & typed decisionsswedish-workflow-training-v1/train/combined-with-v1.jsonl90% stratified split across 5 decision types and 15+ Swedish agencies (216 held out for valid.jsonl) · provisional local training split
    1,93170.7%
    Pinned
  • Förvaltningslagen procedural next stepsRenaissanceLabsEU/swedish-public-decisions-v1 · forvaltning_nasta_stegTrain split only from birgermoell/myndighetsbeslut · CC-BY-4.0
    40014.6%
    Pinned
  • CSN student-aid statutory decisionsRenaissanceLabsEU/swedish-public-decisions-v1 · csn_studiemedelTrain split only, prompts ≤ 4,500 chars · CC-BY-4.0
    2007.3%
    Pinned
  • ÖKS & ÖNH real Swedish appeal boardsRenaissanceLabsEU/swedish-public-decisions-v1 · oks + onh100 ÖKS + 100 ÖNH real appeal decisions (train split only) · CC-BY-4.0
    2007.3%
    Pinned

Companion open datasets published alongside this run

Curated and published on Hugging Face under RenaissanceLabsEU so others can train Swedish and multilingual decision models without scraping.

  • Swedish Public Decisions v1RenaissanceLabsEU/swedish-public-decisions-v16 Swedish agencies (CSN, Förvaltningslagen, Skolinspektionen, JO, ÖKS, ÖNH, Upphandlingsmyndigheten) · CC-BY-4.0
    14,65426.6%
    Pinned
  • Global Decisions v1 (53 languages)RenaissanceLabsEU/global-decisions-v1All 23 official EU languages + Basque + 29 global languages across 6 macro-regions and 7 decision domains · CC-BY-SA-4.0 (77.2% CC-BY-4.0 / Apache-2.0 / CC0)
    40,35973.4%
    Pinned
Fingerprints of the training, validation, and paired evaluation files (SHA-256)
swedish-decision-qwen3/data/train.jsonl70400c66a1d4…822600
swedish-decision-qwen3/data/valid.jsonl23d83f53dea8…5a4e86
eval_v1_paired.jsoncbfa0639538b…8a9e24
eval_wft1_paired.json9245eaf758cc…3f7af6

2Pinned inputs

Everything that must match for a faithful rerun.

Base model
mlx-community/Qwen3-1.7B-4bitApache-2.0, 1.72B parameters, 4-bit MLX quantization
Swedish companion dataset
RenaissanceLabsEU/swedish-public-decisions-v114,654 CC-BY-4.0 rows across 6 Swedish agencies
Global companion dataset
RenaissanceLabsEU/global-decisions-v140,359 rows across 53 languages (all 23 EU official + global)
Embedding baseline
RenaissanceLabsEU/swedish-decision-saga-v1SagaEmbed/saga-embed_v1 frozen embeddings + 3-seed residual bilinear head
Software
mlx-lm (via uv) · Python 3.9 · NumPy · MatplotlibSingle-forward-pass logit extraction over choice token IDs A–G

3Equipment

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.

Node 1

4Method

Paths in angle brackets are yours to fill in. Account IDs and personal paths have been removed.

  1. 1

    Format prompts with full JSON state and empty think block

    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.

    python3 swedish-decision-qwen3/prepare_data.py
  2. 2

    Score the zero-shot baseline before training

    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.

    uv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py --tag before
  3. 3

    Train the 4-bit MLX LoRA adapter

    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.

    uv run --with mlx-lm python -m mlx_lm lora \
      --model mlx-community/Qwen3-1.7B-4bit --train \
      --data swedish-decision-qwen3/data \
      --adapter-path swedish-decision-qwen3/adapters_lr5e5 \
      --fine-tune-type lora --mask-prompt --grad-checkpoint \
      --batch-size 1 --grad-accumulation-steps 8 --iters 350 \
      --learning-rate 5e-5 --num-layers 16 --max-seq-length 2048 \
      --val-batches 25 --steps-per-eval 50 --save-every 50
  4. 4

    Calibrate and evaluate once on both frozen holdouts

    Score calibration/ and holdout/ with the iter-250 adapter, then run the untouched official evaluate.py --split test paired against zero-shot.

    uv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py \
      --tag after --adapter-path swedish-decision-qwen3/best_adapter
    python3 swedish-decision-dataset-v1/evaluate.py --split test \
      --predictions preds/before_v1_holdout_cal.jsonl \
      --compare preds/after_v1_holdout_cal.jsonl --output eval_v1_paired.json
  5. 5

    Fuse the LoRA weights into a standalone MLX checkpoint

    Merge the iter-250 LoRA weights into standalone 4-bit safetensors and write the model card afterwards (mlx_lm fuse writes a stub README).

    uv run --with mlx-lm python -m mlx_lm fuse \
      --model mlx-community/Qwen3-1.7B-4bit \
      --adapter-path swedish-decision-qwen3/best_adapter \
      --save-path swedish-decision-qwen3/fused
End-to-end Apple Silicon MLX training, calibration, and frozen evaluation script
#!/usr/bin/env bash
# Runs end-to-end on a single Apple Silicon Mac (32 GB unified memory, ~3.5 GB peak).
set -euo pipefail

# 1. Prepare single-letter prompt/completion data (2,731 train / 216 valid)
python3 swedish-decision-qwen3/prepare_data.py

# 2. Score zero-shot baseline before training
uv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py --tag before

# 3. Train 4-bit MLX LoRA (lr=5e-5, grad-accum=8, ~6 minutes on M5)
uv run --with mlx-lm python -m mlx_lm lora \
  --model mlx-community/Qwen3-1.7B-4bit --train \
  --data swedish-decision-qwen3/data \
  --adapter-path swedish-decision-qwen3/adapters_lr5e5 \
  --fine-tune-type lora --mask-prompt --grad-checkpoint \
  --batch-size 1 --grad-accumulation-steps 8 --iters 350 \
  --learning-rate 5e-5 --num-layers 16 --max-seq-length 2048 \
  --val-batches 25 --steps-per-eval 50 --save-every 50

# 4. Stage lowest validation-loss checkpoint (iter 250, val=0.134) and score once on frozen holdouts
mkdir -p swedish-decision-qwen3/best_adapter
cp swedish-decision-qwen3/adapters_lr5e5/0000250_adapters.safetensors swedish-decision-qwen3/best_adapter/adapters.safetensors
cp swedish-decision-qwen3/adapters_lr5e5/adapter_config.json swedish-decision-qwen3/best_adapter/adapter_config.json
uv run --with mlx-lm --with numpy python swedish-decision-qwen3/score_mlx.py --tag after --adapter-path swedish-decision-qwen3/best_adapter

# 5. Official paired evaluation on both untouched frozen holdouts
python3 swedish-decision-dataset-v1/evaluate.py --split test \
  --predictions swedish-decision-qwen3/preds/before_v1_holdout_cal.jsonl \
  --compare swedish-decision-qwen3/preds/after_v1_holdout_cal.jsonl \
  --output swedish-decision-qwen3/eval_v1_paired.json
python3 swedish-workflow-training-v1/evaluate.py --split test \
  --predictions swedish-decision-qwen3/preds/before_wft1_holdout_cal.jsonl \
  --compare swedish-decision-qwen3/preds/after_wft1_holdout_cal.jsonl \
  --output swedish-decision-qwen3/eval_wft1_paired.json

# 6. Fuse into standalone 4-bit MLX weights
uv run --with mlx-lm python -m mlx_lm fuse \
  --model mlx-community/Qwen3-1.7B-4bit \
  --adapter-path swedish-decision-qwen3/best_adapter \
  --save-path swedish-decision-qwen3/fused

5Settings

Stage 1: MLX LoRA SFT

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)

Stage 2: Post-hoc temperature scaling

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

6What you should see

Numbers from the original run, so you can tell early whether yours is on track.

  • Validation convergenceCross-entropy on 216 stratified tasks drops 14.841 → 0.134 at iter 250.
  • Primary frozen holdout (n=532)Accuracy 34.0% → 53.8% (+19.7pp, 95% agency-cluster CI [+9.1%, +24.1%]).
  • Calibration qualityECE-10 drops 0.184 → 0.080; NLL 1.356 → 1.116; selective accuracy 78.8% at conf ≥ 0.70.
  • Unseen agency holdout (n=88)Elsäkerhetsverket accuracy rises 33.0% → 38.6% (+5.7pp).
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.
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.
swedish-decision-dataset-v1 (n=532, frozen)Qwen3-1.7B Zero-ShotSagaEmbed (saga-v1)Qwen3-1.7B LoRA (this recipe)
Overall accuracy34.0%56.0%53.8% (+19.7pp)
Evidence sufficiency78.1%70.5%78.1%
Uncertainty handling12.4%49.5%78.1% (+65.7pp)
Next step4.8%40.0%40.0% (+35.2pp)
Rule application40.0%37.1%40.0%
Document relevance34.8%81.2%33.9%
Negative log-likelihood (NLL ↓)1.3561.1681.116
Multiclass Brier score (↓)0.7120.5960.587
Calibration error ECE-10 (↓)0.1840.1680.080
Selective accuracy at conf ≥ 0.7076.4%67.8%78.8%
53.8% (+19.7pp)Frozen holdout accuracy (v1, n=532)
78.1% (+65.7pp)Uncertainty handling accuracy
0.080 (↓ from 0.184)Calibration error (ECE-10)
78.8%Selective accuracy (conf ≥ 0.70)

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%).

7Known gaps

What stands between this card and an exact copy of the run.

  • 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.

8Variants

No variants yet.

Reproduced it? Changed something? Fixed a problem or added a capability? Report it and it appears here, linked to this recipe.

Report a variant

9Original files

Artifacts published in the Hugging Face model repository.

Background: Decisions capability page. Machine-readable: this recipe as JSON.