Fitting Llama 3.1 8B on a 12GB RTX 3060 with Q4_K_M and llama.cpp b3618
## The setup I started with
I have got a 12GB RTX 3060 in my home box and wanted Llama 3.1 8B running locally, mostly for autocomplete on a private repo I cannot send to any API. Machine is Ubuntu 22.04, CUDA 12.4, llama.cpp at b3618 built with DLLAMA_CUBLAS=on. I pulled the Q8_0 GGUF from TheBloke's repo because bigger number means better quality, right.
It loaded. It even answered one prompt. Then on the second prompt the whole thing died:
CUDA error: out of memory
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
ggml_cuda_compute_forward: MUL_MAT failed
No idea why at first, because nvidia-smi showed the card sitting at maybe 4GB. Turns out the KV cache is the greedy part, not the weights.
## Doing the memory math
The 8B in Q8_0 is roughly 8.5GB of weights. That leaves about 3.5GB on a 12GB card for the context window and compute buffers, and the CUDA context itself eats ~300-500MB immediately. With context at 8192 tokens the cache alone was asking for more than I had. llama.cpp prints the breakdown at startup if you actually look:
llama_model_loader: loaded meta data with 26 key-value pairs and 291 tensors
llama_context: n_ctx = 8192
llama_context: kv cache size = 2.00 GiB
llama_context: computed KV cache size = 2048 MiB
So roughly 8.5GB weights plus 2GB cache plus overhead, and I was over the line. Switching to Q4_K_M dropped the model to about 4.9GB. That is the quant I would recommend for an 8B on 12GB; the perplexity hit is small compared to Q8 and you get the VRAM back for context. Q4_K_S is a bit smaller again but noticeably worse on reasoning, and Q5_K_M is about 6.2GB if you want a middle ground and can live with 6144 context.
## The command that worked
llama-server -m models/Llama-3.1-8B-Instruct-Q4_K_M.gguf \
-c 8192 -ngl 35 --host 127.0.0.1 --port 8080
A few notes on the flags. -ngl 35 offloads all 35 layers; anything less and you are doing partial CPU inference, which is fine but slower. -c 8192 is context. I tried 16384 and it still fit, but generation got sluggish and my prompts were never that long anyway. Drop --host 0.0.0.0 unless you want the whole LAN hitting it. I learned that one after my router logs showed a scan on port 8080, which was a fun five minutes.
## The dumb thing that cost me an hour
I rebuilt llama.cpp twice chasing this, convinced the CUDA build was broken. It was not. My LD_LIBRARY_PATH was still pointing at an old cuDNN 8 from a previous install, so the loader was mixing runtimes:
$ ldd ./llama-server | grep cuda
libcublas.so.12 => /usr/local/cuda-12.4/lib64/libcublas.so.12
libcudart.so.12 => /usr/local/cuda-12.4/lib64/libcudart.so.12
Purged the old cuDNN, relinked, fine. I should have checked that before the second rebuild, but I was in the mood to blame the compiler. Anyway, if you are on 12GB and wondering which quant to grab: Q4_K_M, 8192 context, all layers offloaded. I get about 45 tok/s here. If you know a cleaner way to squeeze more context without the slowdown, I am all ears.