mlx-lm · Lesson 9

Model Selection & Memory Math

Stop guessing whether a model will run. With one formula you can tell — before downloading 8 GB — what fits in your 24 GB M4 Pro, how fast it'll decode, and which size/quant to pick for a given job. Turn "will this work?" into arithmetic.

🎯 Mission skill: choose a model on purpose for your machine + task
The one idea

Weight RAM ≈ parameters × bits ÷ 8. That, plus the KV cache for your context, has to fit in unified memory alongside the OS. And bigger models decode slower (more bytes read per token). So the game is: pick the biggest model whose memory leaves headroom — and no bigger than the task needs.

01 The formula

A model is mostly its weights, and each weight takes bits bits. So the memory just to hold it is parameter-count times bytes-per-weight:

# weight RAM (GB) ≈ params(billions) × bits ÷ 8
1B × 4-bit  = 1 × 4 / 8  = 0.5 GB
3B × 4-bit  = 3 × 4 / 8  = 1.5 GB
7B × 4-bit  = 7 × 4 / 8  = 3.5 GB
7B × 8-bit  = 7 × 8 / 8  = 7.0 GB
7B × 16-bit = 7 × 16 / 8 = 14  GB

Then add ~10–25% for activations + framework overhead, plus the KV cache (§4). Quantization (Lesson 5) is the dial that moves a model between these rows.

💡 Sanity-check against reality Measured peaks on your machine: 1B-4bit → 0.8 GB (formula 0.5 + overhead) and 3B-4bit → 1.87 GB (formula 1.5 + overhead). The arithmetic predicts the real number within the overhead margin — that's all the precision you need to plan.

02 What fits on your 24 GB M4 Pro

macOS + your apps need RAM too — budget roughly 6–8 GB for the system, leaving ~16–18 GB for a model + its context. Weight RAM by size and quant (before context):

Params4-bit8-bitfp16
1B0.5 GB ✓1 GB ✓2 GB ✓
3B1.5 GB ✓3 GB ✓6 GB ✓
7–8B~4 GB ✓~7 GB ✓~15 GB ⚠️ tight
14B~7 GB ✓~14 GB ⚠️~28 GB ✗
32B~16 GB ⚠️ tight~32 GB ✗✗
70B~35 GB ✗✗✗

✓ comfortable · ⚠️ fits but leaves little headroom for context/apps · ✗ won't fit in 24 GB. Sweet spot for daily use: 7–14B at 4-bit.

03 The speed tax — bigger is slower

Decode is memory-bandwidth-bound (Lesson 1): each token reads the whole weight set. Double the weights, roughly halve the tokens/sec. Measured on your M4 Pro, same 4-bit quant:

291 tok/s1B-4bit · 0.8 GB
116 tok/s3B-4bit · 1.87 GB
≈ 1/sizethroughput scales inversely with params
fast slow model size → 1B · 291 t/s 3B · 116 t/s 7B · slower 14B · slowest
Bigger circles = more memory; lower = slower. You're always trading capability for speed and RAM. A 1B model answers instantly; a 14B model is smarter but you feel each token — pick the smallest model that's actually good enough for the job.

04 The variable part — KV cache grows with context

Weights are fixed, but the KV cache (Lesson 6) grows with how many tokens are in play — roughly proportional to context length. For small models it's modest; for long documents or long chats it can add multiple GB on top of the weights. So the real budget is:

# must fit in unified memory:
weights  +  KV cache(context)  +  activations/overhead  +  OS/apps  ≤  24 GB
💡 Practical consequence A 7B-4bit model (~4 GB) leaves lots of room for context on 24 GB. A 32B-4bit (~16 GB) fits the weights but leaves little for a long context — you'd cap it with --max-kv-size (Lesson 6) or keep prompts short. When a big model OOMs mid-long-prompt, the KV cache is usually why.

05 A decision recipe

1. Start from the task

Routing/tagging/extraction → 1–3B is plenty (and use Lesson 8 to make it reliable). Reasoning/coding/writing → reach for 7–14B.

2. Pick quant by headroom

Default 4-bit. Step to 8-bit only if you measured a quality problem (or you're preserving a fine-tune — Lesson 5) and have the RAM.

3. Check the math

params × bits ÷ 8 + overhead + context. Must leave ~6–8 GB for macOS. If it's tight, drop a quant level or a size.

4. Verify on-device

Run it once, read Peak memory + tokens/sec. The dashboard from Lesson 1 is your ground truth — trust it over the estimate.

🎯 Your tangible win Next time you browse mlx-community, read the model id (Lesson 1) and do the math in your head: "14B × 4-bit ≈ 7 GB — fits my 24 GB with room for context. Worth a download." You're now choosing models deliberately instead of trial-and-error.

06 Check yourself

Think first, then expand. (Eyeing a specific model? Tell me the name and we'll size it for your 24 GB together.)

Q: Will Qwen2.5-14B-Instruct-4bit run on your 24 GB Mac?

A: Weights ≈ 14 × 4 / 8 = 7 GB, plus overhead + context ≈ 8–10 GB used. With ~16 GB available after the OS, yes — comfortably, with room for a decent context. It'll be noticeably slower per token than a 3B, though.

Q: A 32B-4bit model loads fine but crashes when you paste a long document. Why?

A: The weights (~16 GB) fit, but the long prompt's KV cache pushed total memory past what's available. Cap it with --max-kv-size, shorten the prompt, or use a smaller model.

Q: You need maximum speed for high-volume tagging. Size and quant?

A: Smallest model that's accurate enough — 1–3B — at 4-bit, made reliable with constrained decoding (Lesson 8). Speed scales inversely with size, so small wins for throughput.

Q: Estimate the RAM for a 7B model at fp16. Does it fit?

A: 7 × 16 / 8 = 14 GB of weights, plus overhead + context → ~16–18 GB. On 24 GB it's tight but possible with a short context; 4-bit (~4 GB) is the saner choice unless you specifically need full precision.

07 Going deeper

Keep handy: Glossary & quick reference · ← Lesson 8. You can now run, serve, tune, quantize, cache, structure output, and size models on your Mac. The open invitation stands → bring a real tool you want to build (e.g. a local tagger wired into the learn pipeline) and we'll make it a capstone.