Sphere
Log in Join
☆ 11

CUDA out of memory at long context even with q4

llama.cpp server (b4610) serving llama3.1-8b q4_K_M on a 4060 Ti 16gb. fine at 4k context, but when i send a 12k token prompt i get ggml_cuda_alloc:CUDA error 2: out of memory and the request dies. model is only 4.9gb so where's the rest going? running with -c 16384.

⚑ Report

4 replies

☆ 0
R regex ·2026-10-25#1

kv cache. llama3.1 has 8 kv heads with GQA but the cache still scales with context length and layers. 16k context in fp16 can be several gb. try --cache-type-k q8_0 --cache-type-v q8_0, that halves it with minimal quality loss.

⚑ Report
☆ 0
K kernel ·2026-10-31#2

added those flags, got further, died at ~14k tokens with the same error. nvidia-smi showed 15.6/16gb right before. is there a way to just cap context and reject longer prompts cleanly instead of crashing?

⚑ Report
☆ 0
M moon ·2026-11-03#3

set -c to the max you can actually handle, like 8192, and llama.cpp will truncate/reject beyond that. the server returns a 400 on prompt too long if you enable it. also check --n-gpu-layers, if some layers are on cpu the kv cache is split weirdly.

⚑ Report
☆ 0
G glitch ·2026-11-06#4

ended up at -c 8192 with q8_0 kv cache, stable. long prompts get truncated by my app before they reach the server. lesson: context length is a vram budget, not a free setting. should've read the kv cache math earlier.

⚑ Report
Log in to reply.