Publicaciones
Running language models on your own machine
Why I run language models locally, what actually fits on an ordinary machine, and where they still fall short of the hosted ones.
Publicado
Tiempo de lectura
4 minutos
Artículo
Running a language model at home used to mean a serious GPU and a whole afternoon of setup, now it’s one command and a few gigabytes of disk. The models got smaller, the tools got simpler, and for a lot of what I do, I don’t need to send anything to someone else’s server anymore.
Why bother
Three reasons, in order of how much they matter to me:
- Privacy, notes, code and documents stay on my machine, and for anything personal or under an NDA, that settles it.
- Cost, after the hardware, every call is free, batch jobs that would cost real money through an API just cost electricity.
- It works offline, on a train, on a bad connection, during an outage, you name it.
What fits
The rule of thumb is pretty simple, a model’s weights take roughly its number of parameters times the bits per parameter:
- 16 bits
- 8 bits
- 4 bits
The numbers
| Series | billions of parameters | GB |
|---|---|---|
| 16 bits | 1 | 2 |
| 16 bits | 3 | 6 |
| 16 bits | 8 | 16 |
| 16 bits | 14 | 28 |
| 16 bits | 32 | 64 |
| 16 bits | 70 | 140 |
| 8 bits | 1 | 1 |
| 8 bits | 3 | 3 |
| 8 bits | 8 | 8 |
| 8 bits | 14 | 14 |
| 8 bits | 32 | 32 |
| 8 bits | 70 | 70 |
| 4 bits | 1 | 0.5 |
| 4 bits | 3 | 1.5 |
| 4 bits | 8 | 4 |
| 4 bits | 14 | 7 |
| 4 bits | 32 | 16 |
| 4 bits | 70 | 35 |
Quantized to 4 bits (which costs surprisingly little quality), an 8-billion-parameter model needs around 5 GB of memory once you count the overhead, and a 3B model needs about half that.
Part of that overhead is the quantization’s own bookkeeping (a scale for every small block of weights), but part of it grows as you talk, the KV cache. For every token in the conversation, the model keeps a key and a value in every layer, so it doesn’t have to read everything again for each new word. For Llama 3.1 8B, with its 32 layers and 8 key-value heads of 128 numbers each, at 16 bits:
That’s about 131 KB a token, nothing for a short chat, but at the 128K tokens Llama 3.1 can take in (a K of tokens is 1,024 of them), it’s over four times the weights:
- weights, at 4 bits
- KV cache, at 16 bits
The numbers
| Series | K tokens in the context | GB |
|---|---|---|
| weights, at 4 bits | 0 | 4 |
| weights, at 4 bits | 8 | 4 |
| weights, at 4 bits | 16 | 4 |
| weights, at 4 bits | 32 | 4 |
| weights, at 4 bits | 64 | 4 |
| weights, at 4 bits | 128 | 4 |
| KV cache, at 16 bits | 0 | 0 |
| KV cache, at 16 bits | 8 | 1.07 |
| KV cache, at 16 bits | 16 | 2.15 |
| KV cache, at 16 bits | 32 | 4.29 |
| KV cache, at 16 bits | 64 | 8.59 |
| KV cache, at 16 bits | 128 | 17.18 |
So on an ordinary laptop, the sweet spot right now is somewhere between 1B and 8B parameters, Llama 3.1 8B, the new Llama 3.2 1B and 3B, Qwen 2.5, Gemma 2. Anything bigger wants a real GPU, or a Mac with a lot of unified memory.
My own machine isn’t a laptop though, it’s a desktop with an i9-9900KF, 64 GB of DDR4 and an RTX 2080 Ti. The 2080 Ti has 11 GB of VRAM, so an 8B model at 4 bits fits entirely on the GPU with room to spare, and anything that doesn’t fit gets split between the GPU and the system RAM, which works (64 GB helps a lot there), just a lot slower.
The tools
llama.cpp does the heavy lifting, it runs quantized models on pretty much any CPU or GPU, and Ollama wraps it in something friendlier, ollama run downloads a model and drops you in a chat, and it also exposes a local API that speaks the same format as OpenAI’s. Now that last part matters more than it sounds, because code written against a hosted API can switch to a local model by changing a URL, that’s it.
Where they fall short
Small local models are not just small versions of the big hosted ones, they know less, they lose the thread in long contexts, and they’re worse at reasoning over several steps. Ask an 8B model a question that needs real knowledge, and it’ll answer just as confidently as GPT-4 would, only wrong a lot more often.
Where they shine though is narrow tasks where you hand them the context, summarize this, classify that, rewrite this paragraph, pull the dates out of this email. Give a small model the right text and a clear, small job, and it does just fine.
And that’s pretty much what I use them for day to day, sorting my emails, looking things up in a pile of unstructured files, autocompletion, that kind of stuff, small jobs I do a lot, where I’d rather not send anything out and where waiting on an API would just get in the way.
And that’s the pattern I keep seeing, the less a task depends on what the model knows, and the more on what you give it, the smaller the model can be.
Siguiente
Building with language models, not just chatting with themoLas 21 publicacionesoAl azaroInicio