Renaissance Labs · Skills

Skills / train-at-home

Agent Skill

train-at-home

Fine-tune open-weight LLMs (1B–32B) locally on Apple Silicon Macs (16–128 GB unified memory) using 4-bit MLX QLoRA, assistant-only loss masking, fast before/after micro-evals, caffeinate-protected overnight runs, and clean LoRA + fused weight export to Hugging Face. Uses Hemma v0.1 (Prelude 9B Swedish on a 32 GB M5 MacBook) as its worked example.

npx skills add RenaissanceLabs-EU/skills --skill train-at-home

A practical workflow and helper CLI (scripts/train_at_home.py) for fine-tuning open-weight language models locally on an Apple Silicon Mac — no CUDA, no SLURM queue, no cloud bill. Tested on a 32 GB Apple M5 MacBook training [RenaissanceLabsEU/prelude-9b-sv-hemma-v0.1](https://huggingface.co/RenaissanceLabsEU/prelude-9b-sv-hemma-v0.1) (9.1B Prelude Swedish SFT, 50 minutes, ~10.6 GB peak memory, ~158 tok/s).

Quickstart

# 1. Inspect your Mac's unified memory and get safe MLX QLoRA defaults
python3 scripts/train_at_home.py plan --model openeurollm/Prelude-SFT-Propella-Excellent-v0.2 --params-b 9

# 2. Convert a Hugging Face model to 4-bit MLX (fits 9B in ~5.5 GB)
python3 scripts/train_at_home.py convert \
  --hf-model openeurollm/Prelude-SFT-Propella-Excellent-v0.2 \
  --out ./models/prelude-4bit --q-bits 4

# 3. Validate your train.jsonl / valid.jsonl chat dataset (schema, lengths, prompt/assistant ratio)
python3 scripts/train_at_home.py check-data --data-dir ./data

# 4. Generate a ready-to-run lora_config.yaml + caffeinate overnight launcher
python3 scripts/train_at_home.py init-run \
  --model ./models/prelude-4bit \
  --data-dir ./data \
  --out-dir ./run_hemma \
  --iters 1200 --lr 1e-5

Memory Sizing Guide (Apple Silicon Unified Memory)

Full BF16 SFT of a 9B model needs ~85 GB (weights + AdamW optimizer + activations). 4-bit MLX QLoRA with grad_checkpoint: true and batch_size: 1 cuts that by ~8×:

Mac Unified MemoryComfortable Model Size (4-bit MLX QLoRA)Suggested num_layers / rankmax_seq_lengthTypical Peak RAM
16 GB1B – 4B8–16 layers · rank 8–161,024–2,048~6–10 GB
24 GB7B – 9B12–16 layers · rank 8–161,024–2,048~10–14 GB
32 GB7B – 14B (e.g. Prelude 9B)16 layers · rank 162,048~10–16 GB
64 GB14B – 32B (e.g. Gemma 4 31B)16–24 layers · rank 16–322,048–4,096~26–40 GB
128 GB+32B – 70B24–32 layers · rank 16–324,096~45–80 GB

Step-by-Step Recipe (Worked Example: Hemma v0.1)

1. Choose a tokenizer-efficient base model

For non-English fine-tuning, measure tokenizer fertility (tokens/word) first. On Swedish text, openeurollm/Prelude-SFT-Propella-Excellent-v0.2 (256k EU vocab) averages 2.08 tok/word vs 2.54 tok/word for Gemma 4 31B — ~18% fewer tokens per example and ~27% fewer on compound words, directly speeding up laptop training.

2. Quantize the frozen base to 4-bit MLX

uvx --from mlx-lm mlx_lm.convert \
  --hf-path openeurollm/Prelude-SFT-Propella-Excellent-v0.2 \
  --mlx-path ./models/prelude-4bit -q --q-bits 4

3. Watch out for strict /think / /no_think chat templates

Modern reasoning-conditioned checkpoints (like Prelude Excellent v0.2) ship a chat_template.jinja that raises TemplateError if an assistant target has no <think> block and thinking_instruction is unset. For direct-answer SFT, default line 1 of your local converted chat_template.jinja to 'no_think':

sed -i '' "s/default(none)/default('no_think')/" ./models/prelude-4bit/chat_template.jinja

4. Curate a tight, zero-contamination chat dataset (train.jsonl & valid.jsonl)

  • Format: one JSON object per line: {"messages": [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]}.
  • Keep sequences compact (≤ 6,000 chars for max_seq_length: 2048) so laptop runs don't waste compute on truncated tails.
  • Include negative / non-over-editing controls (e.g. 500 already-correct sentences where the target is the unchanged sentence) so editing models don't hallucinate unnecessary rewrites.
  • Hold out 5% for valid.jsonl and keep evaluation benchmarks (sv-blimp, swedish-facts, EuroEval) strictly disjoint.

5. Always run a <60s before/after micro-eval

Never train blind. Run a small, repeatable <60s eval (20–35 objective items + 4 qualitative probes saved to JSON) before step 0 and after training using the exact same chat template. On Hemma v0.1, a 39-second micro-eval immediately exposed baseline word-fusion typos (*"medduta er"*, *"gemensammavättstugna"*, *"ochförtroendet"*) and confirmed they vanished after 50 minutes of QLoRA without breaking JSON or non-editing controls.

6. Train with conservative LR + caffeinate -i

  • If starting from an already-SFT checkpoint (like Prelude-SFT-Propella-Excellent-v0.2), use a **low learning rate (5e-6 to 1e-5, cosine decay)** and mask_prompt: true (assistant-only loss) so you don't overwrite prior reasoning/tool-calling calibration.
  • Always wrap overnight runs in caffeinate -i so macOS never sleeps mid-epoch:
caffeinate -i uvx --from mlx-lm mlx_lm.lora --config ./run_hemma/lora_config.yaml

7. Publish BOTH fused weights and unfused LoRA adapters

Fuse for zero-friction local inference (mlx_lm.fuse), and include the adapters/ directory (final + lowest-validation-loss checkpoint, e.g. 0000800_adapters.safetensors) so you or collaborators can resume training in Part 2.

uvx --from mlx-lm mlx_lm.fuse \
  --model ./models/prelude-4bit \
  --adapter-path ./run_hemma/adapters \
  --save-path ./exports/my-home-model

Reference Recipe

Full worked example, figures, SHA-256 checksums, and before/after Swedish outputs:

  • Recipe page: https://learn.renaissancelabs.eu/recipes/prelude-9b-sv-hemma-v0.1/
  • Checkpoint: https://huggingface.co/RenaissanceLabsEU/prelude-9b-sv-hemma-v0.1