Below is a pragmatic “recipe” that people regularly use to squeeze a ≈ 3 GB GGUF/llamafile onto a machine that only has
• 2 GB of GPU-VRAM
• 8 GB of system RAM
while still getting the best tokens-per-second (t/s) the hardware can deliver.
I’ll assume you are using the Mozilla-Ocho “llamafile”, which is nothing more than llama.cpp wrapped inside a self-contained executable.
The same flags you would pass to llama.cpp therefore work with a llamafile.
──────────────────────────────
- Make sure the model is already small enough
──────────────────────────────
• 3.2 GB normally means a 7 B model quantised to Q4_K-M / Q4_0.
That is perfect for 2 GB of VRAM.
• If your file is bigger, run a quick quantisation:
# one-time step (need Python only for this)
python3 -m llama_cpp.quantize \
original.gguf \
model-q4_k_m.gguf \
q4_k_m # or q4_0/q5_1 if you prefer
──────────────────────────────
2. Build the llamafile with GPU support
──────────────────────────────
Cuda (NVIDIA) OpenCL (AMD/iGPU)
make -j LLAMA_CUBLAS=1 make -j LLAMA_CLBLAST=1
→ The produced “llamafile” binary already contains the model plus the GPU-capable llama.cpp runtime.
──────────────────────────────
3. Understand where the memory goes
──────────────────────────────
VRAM is taken by:
• model weights you decide to off-load (n_gpu_layers)
• some scratch buffers / cuBLAS handle
System RAM is taken by:
• all remaining layers
• the KV-cache (grows with ctx-size × #tokens seen)
• the executable itself, page cache, etc.
Rule of thumb for a 7 B Q4 model
Weight per transformer layer ≈ 45–50 MB
There are 32 layers in total
2 GB VRAM ⇒ you can safely off-load ≈ 25-28 layers (leaves ~300 MB head-room).
──────────────────────────────
4. Run a quick “find-the-sweet-spot” loop
──────────────────────────────
# ─── helper function ───
test_layers () {
./my7b.llamafile \
-ngl "$1" \ # GPU layers
-t 6 \ # 6 CPU worker threads (adjust to your cores)
-c 4096 \ # context size (4096 is usually safe in 8 GB RAM)
-b 512 \ # batch size for generation
--temp 0.7 \
--repeat_penalty 1.1 \
-p "Bench me!"
}
for L in 32 28 24 20; do
echo "▶ Trying $L GPU layers"
test_layers "$L" 2>&1 | tee "bench_$L.txt"
done
• Start high (-ngl 32).
• If it crashes with “CUDA out of memory” drop by 2-4 layers and re-run.
• Pick the largest -ngl that still fits; in practice on 2 GB cards that ends up around 26 ± 2.
──────────────────────────────
5. Typical one-liner that works on 2 GB VRAM
──────────────────────────────
./my7b.llamafile \
-ngl 26 \ # everything the card can hold
-t 6 \ # physical CPU cores (hyper-threads add very little)
-c 4096 \ # long context – uses RAM, not VRAM
-b 512 \ # bigger batches = more t/s until RAM becomes the bottleneck
--mlock \ # pin pages, avoids swap stutter (Linux only)
--prompt-cache prompt.bin \
-i \ # interactive
Expected speed on a modest laptop 2 GB VRAM + 8 GB RAM:
• 19-28 t/s for 7 B Q4_K_M (GPU 26 layers)
• 13-18 t/s if you had to drop to ~16 layers (still respectable)
──────────────────────────────
6. Micro-optimisations you can toggle
──────────────────────────────
- KV-cache on GPU (
LLAMA_CUDA_KV_OFFLOAD=1)
– costs a lot of VRAM; usually not worth it on a 2 GB card.
-b (batch size). You will see throughput rise up to ≈ 512-1024; after that diminishing returns / OOM.
--threads above physical cores = more context-switching, not speed.
--mmapped vs --mlock
• --mmapped (§ default in llamafile) leaves pages pageable; OK if you trust your swap.
• --mlock prevents the model from being swapped out → steadier t/s, slightly higher RSS.
──────────────────────────────
7. If you only have an iGPU (no dedicated CUDA/ROCm)
──────────────────────────────
• Build with LLAMA_CLBLAST=1 and use the same -ngl flag.
• OpenCL driver overhead is higher; expect ~70-80 % of CUDA’s speed on the same memory budget.
──────────────────────────────
8. Quick checklist when speed is still low
──────────────────────────────
☐ Using the correct build (did you see “using CUDA” / “using CLBlast” in the first log lines?)
☐ Power settings: set GPU to “Prefer Maximum Performance” (NVIDIA) / disable automatic throttling.
☐ CPU governor: cpupower frequency-set -g performance.
☐ RAM not swapping? free -h should show “0 SwapUsed” while generating.
☐ Antivirus / Windows Defender real-time scanning disabled on the folder.
──────────────────────────────
Summary TL;DR
──────────────────────────────
- Quantise to Q4 (~3 GB).
- Compile llamafile with GPU support.
- Use
-ngl to off-load as many layers as 2 GB allows (≈ 26).
- 6 CPU threads, 4096 ctx, 512 batch are safe on 8 GB RAM.
- Expect ~20–30 tokens / sec.
That’s it—happy prompting!