Why run it yourself?
- Privacy. Your words, files and photos never leave the device.
- Offline. It works on a plane, in a village with weak signal, or when a service is down.
- Cost. Once you own the hardware, there is no bill per token.
- Control. The model never changes under you, there are no rate limits, and every setting in this course is yours to change.
- Learning. You can watch prefill, decode, the KV cache and quantization happen on your own machine.
Will it fit? Will it be fast?
memory ≈ parameters × bits ÷ 8 + KV cache + about 1 GBThe weights, the cache for your context, and working space.
Qwen3.8-27B at Q4_K_M: 27.78 billion × 4.89 ÷ 8 ≈ 17.0 GBBefore the KV cache. Q4_K_M averages 4.89 bits per weight.
tokens per second ≤ memory speed ÷ bytes read per tokenA dense model reads every weight for each token: 17.0 GB at the RTX 4090’s 1,008 GB/s gives at most about 59 tokens per second.
| Memory | Memory speed | |
|---|---|---|
| Ordinary PC, DDR5, two channels | 16–128 GB | about 90 GB/ |
| Apple M5 | Up to 32 GB | 153 GB/ |
| Apple M5 Pro | Up to 64 GB | 307 GB/ |
| Apple M5 Max | Up to 128 GB | 460 or 614 GB/ |
| Apple M5 Ultra (Mac Studio) | Up to 512 GB | 1,200 GB/ |
| NVIDIA RTX 3060 | 12 GB | 360 GB/ |
| NVIDIA RTX 4090 | 24 GB | 1,008 GB/ |
| NVIDIA RTX 5090 | 32 GB | 1,792 GB/ |
Apple’s figures are from its spec pages. DDR5-5600 with two channels moves 2 × 8 bytes × 5,600 million transfers per second, about 90 GB/s. On a PC, the graphics card’s own memory is many times faster than system memory.
Pick “Laptop with 16 GB” and Llama 3.1 8B at Q4_K_M: it fits with room to spare. Try Qwen3.8-27B: it does not. Now pick the RTX 4090 and gpt-oss-20b at MXFP4. Only 3.6 billion of its weights work on each token, so its speed limit is several times that of the dense 27B. Then raise the context to 131,072 tokens and watch the KV cache grow.
This interactive part needs JavaScript.
Quick check
The programs
| What it is | Good for | |
|---|---|---|
| llama.cpp | The C/C++ engine that many local apps build on. Reads GGUF files; runs on CPUs and on NVIDIA, AMD, Apple and other GPUs | Full control, a local server, the newest models first |
| Ollama | A small background service with one-command downloads and an API on port 11434 | Getting started; apps that expect a local API |
| LM Studio | A desktop app with a chat window and a model browser. Runs llama.cpp, and Apple’s MLX on Macs | People who would rather not use the command line |
| MLX (mlx-lm) | Apple’s own framework for Apple silicon | Fast models, and fine-tuning, on a Mac |
| vLLM, SGLang | The serving engines from the last lesson | One strong GPU answering many requests |
| Phone apps | Google AI Edge Gallery (Android and iPhone) runs Gemma 4 and other models offline; PocketPal AI runs GGUF files | Trying AI with no internet at all |
Apps on Apple devices can also use Apple’s own on-device model, about 3 billion parameters, through its Foundation Models framework. Apple’s 2025 tech report describes how it was squeezed to fit: 2-bit quantization-aware training and a KV cache shared between layers.
Your first local model
# Ollama: download and chat in one step
ollama run gpt-oss:20b
# What is loaded, and how much of it is on the GPU?
ollama ps
# llama.cpp: a server with a chat page at http://localhost:8080
llama-server -hf ggml-org/gpt-oss-20b-GGUF:MXFP4 --port 8080
# Apple silicon: MLX
pip install mlx-lm
mlx_lm.generate --model mlx-community/Llama-3.2-3B-Instruct-4bit --prompt "Hello"
- Ollama picks a default context from your GPU memory: 4K tokens below 24 GiB, 32K up to 48 GiB, 256K above. Its guide says agents and coding tools need at least 64,000. If memory allows, start it with
OLLAMA_CONTEXT_LENGTH=64000 ollama serve. ollama psshows100% GPUwhen the model is entirely on the graphics card, or a split such as48%/52% CPU/GPUwhen it is not. A split is the first thing to check when a model feels slow.-hfdownloads from Hugging Face;:MXFP4picks the file. Newer llama.cpp installs from llama.app also give you a singlellamacommand, andllama serve -hf …starts the same server.mlx_lm.chatopens an interactive chat instead of a single answer.
When it does not fit: splitting the model
If a model is bigger than the graphics card’s memory, the rest can live in the computer’s own memory. That works, but each token then waits for the slow part. A card reads its memory at 360–1,792 GB/s; two channels of DDR5 manage about 90.
about 58 % fits on the cardAfter leaving room for the KV cache and working space.
0.58 × 17 GB ÷ 360 GB/s + 0.42 × 17 GB ÷ 90 GB/s ≈ 27 ms + 80 msThe two-fifths in system memory take three-quarters of the time.
≈ 107 ms per token ≈ 9 tokens per secondUsable for chat, but far from the card’s own speed.
Mixture-of-experts models change the picture. Most of their weights are experts, and each token uses only a few of them. So keep everything else, attention, embeddings and the shared parts, on the card, and leave the experts in system memory. Each token then reads only its few experts from the slow memory. In llama.cpp, --cpu-moe keeps all the experts in system memory, and --n-cpu-moe N keeps only the first N layers’ experts there, so the rest can use spare room on the card.
Start with Qwen3.8-27B on the RTX 3060: only 58 % fits, and you get about 9 tokens per second. Now pick gpt-oss-120b on the RTX 4090. With whole layers, about 26. Switch to “Everything but the experts”: about 53 with as many experts on the card as fit, and still about 41 with none. That is a 120-billion-parameter model on a gaming card, in a PC with 64 GB of memory.
This interactive part needs JavaScript.
# Every expert in system memory, everything else on the card
llama-server -hf ggml-org/gpt-oss-120b-GGUF:MXFP4 --cpu-moe --port 8080
# Experts of the first 24 layers in system memory, the rest on the card
llama-server -hf ggml-org/gpt-oss-120b-GGUF:MXFP4 --n-cpu-moe 24 --port 8080
- Recent builds try to do this for you:
--fitis on by default and adjusts any settings you leave out so the model fits your memory. - Lower
--n-cpu-moestep by step until the card is nearly full. Each layer of experts moved to the card makes every token a little faster.
Choosing a model
| Try | Why | |
|---|---|---|
| Phone | Gemma 4 E2B in Google AI Edge Gallery, or a 1–4B model at Q4 | Small enough for phone memory and battery |
| Laptop with 16 GB | An 8B model at Q4_K_M | About 5 GB of weights leaves room for a long context |
| Graphics card with 12–16 GB | gpt-oss-20b, or a 12–14B dense model at Q4 | Mostly or entirely on the card |
| Graphics card with 24–32 GB | Qwen3.8-27B or Gemma 4 31B at Q4_K_M | A strong dense model, entirely on the card |
| Any good card, plus 64 GB or more of system memory | gpt-oss-120b with the experts in system memory | Mixture-of-experts offloading |
| Mac with 128 GB | gpt-oss-120b entirely in memory | 65 GB of weights, read at up to 614 GB/s |
| Mac Studio with 512 GB | Models of a few hundred billion parameters at 4 bits, such as DeepSeek-V4-Flash | Unified memory holds what no single graphics card can |
- Bigger and 4-bit beats smaller and 8-bit, when both fit, as the quantization lesson showed.
- Test your own language. Small models are often much weaker outside English. Ask the same questions in your language before you trust one.
- Read the licence. Open weights come with terms: gpt-oss uses Apache 2.0, while Gemma and Llama have their own licences.
- Thinking models write a lot before they answer. At 10 tokens per second, 2,000 tokens of reasoning take more than three minutes.
Will each one run well?
-
gpt-oss-20b on a laptop with 32 GB of memory
Answer: Runs well
-
A dense 70B model at Q4 on a laptop with 16 GB
Answer: Too big or too slow
-
Qwen3.8-27B at Q4_K_M on an RTX 4090
Answer: Runs well
-
DeepSeek-V4-Flash on a phone
Answer: Too big or too slow
-
A 1B model on a phone
Answer: Runs well
-
A dense 27B model with half its layers in DDR4 system memory, for a coding agent
Answer: Too big or too slow
Use it from your own programs
Each of these tools can act as a local server with an OpenAI-style API, so any app or agent that can talk to a cloud model can talk to yours instead. Point it at http://localhost:11434/v1 for Ollama, http://localhost:1234/v1 for LM Studio, or the port you gave llama-server.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
reply = client.chat.completions.create(
model="gpt-oss:20b",
messages=[{"role": "user", "content": "Explain the KV cache in two sentences."}],
)
print(reply.choices[0].message.content)
- Ollama needs a key in this field but ignores it; any text works.
- Ollama listens only on
127.0.0.1, so only programs on the same computer can reach it.OLLAMA_HOSTchanges that, for example to use it from your phone on the same Wi-Fi. Do that only on a network you trust: anyone who can reach the port can use your model.
Quick check
Remember
- Memory ≈ parameters × bits ÷ 8, plus the KV cache. Speed ≤ memory speed ÷ bytes read per token.
- llama.cpp is the engine underneath many tools; Ollama and LM Studio make it easy; MLX is Apple’s own; phone apps run small models offline.
- When a dense model does not fit on the card, the part in system memory sets the speed.
- Mixture-of-experts models suit home computers: keep everything but the experts on the card (
--cpu-moe,--n-cpu-moe). gpt-oss-120b runs on one gaming card this way. - Choose the biggest model that fits at about 4 bits, and test it in your own language.
- Local servers speak the OpenAI-style API, so your apps and agents can use them privately.
Words to know
- Local model
- A model that runs on your own device instead of a company’s servers.
- Open weights
- A model whose weights you can download and run yourself, under its licence.
- GGUF
- The single-file model format used by llama.cpp and the tools built on it.
- VRAM
- The graphics card’s own memory. Fast, but limited in size.
- System memory (RAM)
- The computer’s main memory. Bigger than VRAM, but several times slower.
- Offloading
- Putting part of a model in system memory, or on the CPU, because it does not all fit on the GPU.
- MoE offloading
- Keeping a mixture-of-experts model’s experts in system memory and everything else on the GPU.
- MLX
- Apple’s framework for running and training models on Apple silicon.
- localhost, 127.0.0.1
- The computer’s address for itself. A server there is reachable only from the same machine.
- OpenAI-style API
- The request format most tools accept, so a program can switch between cloud and local models by changing one address.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
llama.cpp
The engine itself. The server README lists every option used in this lesson.
-
Ollama: Context length
Ollama’s default context sizes, and how to check whether a model is split between CPU and GPU.
-
MLX LM
Running, converting and fine-tuning models on Apple silicon.
-
gpt-oss-120b & gpt-oss-20b Model Card
Two open mixture-of-experts models built to run on one GPU or within 16 GB.
-
PowerInfer: Fast Large Language Model Serving with a Consumer-grade GPU
Hot neurons on the GPU, cold ones on the CPU. From Shanghai Jiao Tong University.
-
PowerInfer-2: Fast Large Language Model Inference on a Smartphone
A 47B model on a phone, using its NPU, CPU and storage together.
-
LLM in a flash: Efficient Large Language Model Inference with Limited Memory
Apple’s method for running models bigger than a device’s memory by reading weights from flash.
-
Apple Intelligence Foundation Language Models: Tech Report 2025
How Apple fits a 3B model on its devices: 2-bit quantization-aware training and a shared KV cache.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonAPIs: using a model over the internet