llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Eval bug: Checkpoints and MMProj on Gemma 4 consume abnormal amounts of RAM, leading to llama-server going OOM

Open SerialKicked opened this issue 7 months ago • 50 comments

Name and Version

version: 8724 (b54cb2e3d) built with Clang 19.1.5 for Windows x86_64

Operating systems

Windows, Linux

GGML backends

CUDA 12, CUDA 13, Vulkan

Hardware

1x RTX3090 24GB AMD Ryzen 5 3.6Ghz (6 core) + 64GB RAM

Multiple other hardware configurations.

Models

Gemma4 https://huggingface.co/llmfan46/gemma-4-31B-it-uncensored-heretic-GGUF/

Tried many others.

Problem description & steps to reproduce

REVISED ISSUE (see previous edits for history)

With Gemma 4 models (any variant), and Gemma 4 only, checkpoints (ctx-checkpoints) on llama-server are consuming enormous (and increasing) amounts of RAM, to a point that on a machine with 64GB of RAM (and 24GB of VRAM), the entirety of the RAM space can be reached in 4 to 5 full prompt processing of about 16K tokens each. Each generation is making llama-server's RAM consumption bump by several gigabytes.

Workaround: Setting "--ctx-checkpoints" to 1 or 0 with "-np 1" will prevent the server from going out of RAM.

Something notable beyond the OOM issue, is that the memory usage will be different right from the start.

Examples of memory usage with the settings below on a Q4_K_M GGUF

-fa on -kvo -ngl 255 -c 16384 --mmap -m "location/model.gguf" --nowebui
ctx-checkpoints VRAM (start) RAM (start) RAM (1st gen) RAM (2nd gen) RAM (after 3rd gen)
(defaults) 23.6 / 24 GB 0.7GB (+ file) 10GB (+ file) 18GB (+ file) OOM (>25GB + File)
set to 1 + np 1 21.6 / 24 GB 0.7GB (+ file) 1.5GB (+ file) 1.5GB (+ file) 1.5GB (+ file)
set to 0 + np 1 21.6 / 24 GB 0.4GB (+ file) 0.4GB (+ file) 0.4GB (+ file) 0.4GB (+ file)

(each generation is a different 16K tokens long chatlog sent for text or chat completion processing to the server)

Notes:

  • The "-np" setting seems to have a multiplicative value to the consumed VRAM. The higher, the more RAM is used.
  • The "-cram" flag doesn't seem to have any impact either way (set or unset, same values)
  • The server behaves normally with other architectures (test Qwen 3.5 and Mistral)

First Bad Commit

At least since b8660. Bug is consistent since the initial implementation of Gemma 4.

Relevant log output

Not super relevant anymore, but left as is, just in case.

Logs

One log (crashed during processing i think)

srv  params_from_: Grammar lazy: false
srv  params_from_: Generation prompt: ''
res  add_waiting_: add task 891 to waiting list. current waiting = 0 (before add)
que          post: new task, id = 891/1, front = 0
que    start_loop: processing new tasks
que    start_loop: processing task, id = 891
slot get_availabl: id  2 | task -1 | selected slot by LRU, t_last = -1
srv  get_availabl: updating prompt cache
srv          load:  - looking for better prompt, base f_keep = -1.000, sim = 0.000
srv        update:  - cache state: 0 prompts, 0.000 MiB (limits: 8192.000 MiB, 20480 tokens, 8589934592 est)
srv  get_availabl: prompt cache update took 0.01 ms
slot launch_slot_: id  2 | task -1 | launching slot : {"id":2,"n_ctx":20480,"speculative":false,"is_processing":false}
slot launch_slot_: id  2 | task -1 | sampler chain: logits -> ?penalties -> dry -> ?top-n-sigma -> ?top-k -> ?typical -> ?top-p -> min-p -> ?xtc -> temp-ext -> dist 
slot launch_slot_: id  2 | task 891 | processing task, is_child = 0
slot slot_save_an: id  3 | task -1 | saving idle slot to prompt cache
slot slot_save_an: id  3 | task -1 | __TEST_TAG_CLEAR_IDLE_SLOT__
srv   prompt_save:  - saving prompt with length 18674, total state size = 5059.174 MiB
slot prompt_clear: id  3 | task -1 | clearing prompt with 18674 tokens
srv        update:  - cache state: 1 prompts, 19459.390 MiB (limits: 8192.000 MiB, 20480 tokens, 20480 est)
srv        update:    - prompt 000001B16FFD4730:   18674 tokens, checkpoints:  4, 19459.390 MiB
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 892, front = 0
slot update_slots: id  2 | task 891 | new prompt, n_ctx_slot = 20480, n_keep = 0, task.n_tokens = 17702
slot update_slots: id  2 | task 891 | n_tokens = 0, memory_seq_rm [0, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 2048, batch.n_tokens = 2048, progress = 0.115693
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
ggml_backend_cuda_graph_compute: CUDA graph warmup reset
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 892
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 893, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 2048, memory_seq_rm [2048, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 4096, batch.n_tokens = 2048, progress = 0.231386
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 893
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 894, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 4096, memory_seq_rm [4096, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 6144, batch.n_tokens = 2048, progress = 0.347079
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 894
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 895, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 6144, memory_seq_rm [6144, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 8192, batch.n_tokens = 2048, progress = 0.462773
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 895
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 896, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 8192, memory_seq_rm [8192, end)
slot update_slots: id  2 | task 891 | 8192 tokens since last checkpoint at 0, creating new checkpoint during processing at position 10240
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 10240, batch.n_tokens = 2048, progress = 0.578466
slot update_slots: id  2 | task 891 | created context checkpoint 1 of 32 (pos_min = 3584, pos_max = 8191, n_tokens = 8192, size = 3600.054 MiB)
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 896
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 897, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 10240, memory_seq_rm [10240, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 12288, batch.n_tokens = 2048, progress = 0.694159
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 897
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 898, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 12288, memory_seq_rm [12288, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 14336, batch.n_tokens = 2048, progress = 0.809852
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 898
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 899, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 14336, memory_seq_rm [14336, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 16384, batch.n_tokens = 2048, progress = 0.925545
srv  update_slots: decoding batch, n_tokens = 2048
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 899
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 900, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 16384, memory_seq_rm [16384, end)
slot update_slots: id  2 | task 891 | 8192 tokens since last checkpoint at 8192, creating new checkpoint during processing at position 17186
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 17186, batch.n_tokens = 802, progress = 0.970851
slot update_slots: id  2 | task 891 | created context checkpoint 2 of 32 (pos_min = 11776, pos_max = 16383, n_tokens = 16384, size = 3600.054 MiB)
srv  update_slots: decoding batch, n_tokens = 802
set_adapters_lora: adapters = 0000000000000000
adapters_lora_are_same: adapters = 0000000000000000
set_embeddings: value = 0
srv  update_slots: run slots completed
que    start_loop: waiting for new tasks
que    start_loop: processing new tasks
que    start_loop: processing task, id = 900
que    start_loop: update slots
srv  update_slots: posting NEXT_RESPONSE
que          post: new task, id = 901, front = 0
slot update_slots: id  2 | task 891 | n_tokens = 17186, memory_seq_rm [17186, end)
slot update_slots: id  2 | task 891 | prompt processing progress, n_tokens = 17698, batch.n_tokens = 512, progress = 0.999774

This one crashes right at the start.

srv  params_from_: Grammar lazy: false
srv  params_from_: Generation prompt: ''
res  add_waiting_: add task 3475 to waiting list. current waiting = 0 (before add)
que          post: new task, id = 3475/1, front = 0
que    start_loop: processing new tasks
que    start_loop: processing task, id = 3475
slot get_availabl: id  0 | task -1 | selected slot by LRU, t_last = -1
srv  get_availabl: updating prompt cache
srv          load:  - looking for better prompt, base f_keep = -1.000, sim = 0.000
srv        update:  - cache state: 1 prompts, 12377.916 MiB (limits: 8192.000 MiB, 24576 tokens, 24576 est)
srv        update:    - prompt 000001FCBE9A81D0:   22401 tokens, checkpoints:  5, 12377.916 MiB
srv  get_availabl: prompt cache update took 0.01 ms
slot launch_slot_: id  0 | task -1 | launching slot : {"id":0,"n_ctx":24576,"speculative":false,"is_processing":false}
slot launch_slot_: id  0 | task -1 | sampler chain: logits -> ?penalties -> dry -> ?top-n-sigma -> ?top-k -> ?typical -> ?top-p -> min-p -> ?xtc -> temp-ext -> dist 
slot launch_slot_: id  0 | task 3475 | processing task, is_child = 0
slot slot_save_an: id  1 | task -1 | saving idle slot to prompt cache
slot slot_save_an: id  1 | task -1 | __TEST_TAG_CLEAR_IDLE_SLOT__
srv   prompt_save:  - saving prompt with length 22901, total state size = 2863.297 MiB

SerialKicked avatar Apr 09 '26 17:04 SerialKicked

I also observe this memory leak, RAM usage grows extremely fast, RTX 3090 (Ampere) happens at least on CUDA and Vulkan, I tried both

+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.126.20             Driver Version: 580.126.20     CUDA Version: 13.0     |
+-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 3090        On  |   00000000:06:00.0  On |                  N/A |
|  0%   41C    P8             20W /  380W |   23931MiB /  24576MiB |      4%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
/home/user/llama.cpp/build/bin/llama-server \
      --model /mnt/storage/GGUFs/gemma-4/gemma-4-31B-it-UD-Q3_K_XL.gguf \
      # --mmproj /mnt/storage/GGUFs/gemma-4/mmproj-BF16.gguf \
      --webui-mcp-proxy \
      --no-warmup \
      --ctx-size 52000 \
      --no-context-shift \
      --n-gpu-layers 61 \
      --temp 1 \
      --top-p 0.95 \
      --top-k 64 \
      --min-p 0 \
      --presence-penalty 0 \
      --repeat-penalty 1 \
      --jinja \
      --host 0.0.0.0 \
      --port ${PORT} \
      --flash-attn on \
      --mlock \
      # --backend-sampling \
      # --chat-template-kwargs '{"enable_thinking":false}'

blakkd avatar Apr 09 '26 18:04 blakkd

Try reducing the number of checkpoints.

aldehir avatar Apr 09 '26 19:04 aldehir

Try reducing the number of checkpoints.

That's the first thing I tried, I assumed it was tied to the checkpoints, but it's not, at least not directly. This has no effect. 3, 8, or 32 checkpoints don't change a thing. The -cram option is also ignored.

Also, when we say "it grows fast", RAM usage balloons by several GB per new prompt being processed.

SerialKicked avatar Apr 09 '26 19:04 SerialKicked

Does this happen with version b8660 for you?

ColbyB722 avatar Apr 09 '26 19:04 ColbyB722

Does this happen with version b8660 for you?

Yes, at least for me (seems even worse at it)

SerialKicked avatar Apr 09 '26 19:04 SerialKicked

--cache-ram 0 --ctx-checkpoints 1 fixes the RAM issue for me. For VRAM, you should use -np 1 and honestly, -ctk q4_0 -ctv q4_0 works pretty great too.

Offset0x avatar Apr 09 '26 19:04 Offset0x

--cache-ram 0 --ctx-checkpoints 1 fixes the RAM issue for me.

This seems to negate the leak, yes. So I guess we can assume that cram is either all or nothing (max value is ignored) or/and that checkpoints are leaking memory like crazy on Gemma 4.

Double checked against Qwen 3.5 and Magistral 24B, and neither have the issue.

SerialKicked avatar Apr 09 '26 20:04 SerialKicked

I have conducted some tests regarding this issue. It appears that --cache-ram can be used as desired without issue. However, I noticed that --ctx-checkpoints (defaulting to 32) leads to very high memory consumption. By setting --ctx-checkpoints to 1 (or 0), the memory usage stabilizes, and we can still benefit from the KV cache as long as --cache-ram is enabled. It seems the checkpointing mechanism in Gemma 4 requires much more memory than expected.

Atliac avatar Apr 09 '26 21:04 Atliac

By setting --ctx-checkpoints to 1 (or 0)

confirmed working, letting --cache-ram at the 8192 default

blakkd avatar Apr 10 '26 00:04 blakkd

Same problem solved By setting --ctx-checkpoints to 1 (or 0).

hql1229 avatar Apr 10 '26 01:04 hql1229

Okay, I'll edit the op to reflect the proper issue (and workaround) as soon as I'm back in front of a computer and not a phone tomorrow, for clarity sake.

SerialKicked avatar Apr 10 '26 01:04 SerialKicked

I'm curious if this is a bug, or an unfortunate fact of life related to Gemma's implementation. Hoping it's just a bug that can be fixed!

andysalerno avatar Apr 10 '26 04:04 andysalerno

Updated OP.

I'm curious if this is a bug, or an unfortunate fact of life related to Gemma's implementation. Hoping it's just a bug that can be fixed!

Same, while internal llama.cpp checkpointing is of no/limited use in most situations, I'm making the assumption that triggering manual checkpoint saves/reload via API has the same issues, which is a bit more annoying.

Given the many commits related to SWA and Gemma4 in a way or another this week, I doubt we'll know until the dust settles in a week or two.

SerialKicked avatar Apr 10 '26 14:04 SerialKicked

According to GG, sadly that's just how it is. Gemma 4 just needs much more RAM for the context checkpoints. https://github.com/ggml-org/llama.cpp/discussions/21480#discussioncomment-16474304

Dampfinchen avatar Apr 10 '26 18:04 Dampfinchen

Can confirm that I am also experiencing this issue. Each checkpoint is taking around 500 MB of RAM, and with llama.cpp making a checkpoint every 8192 tokens, by the time I hit 32768 tokens, my RAM usage has gone up by 2 GB minimum.

EntityDeleter avatar Apr 11 '26 07:04 EntityDeleter

I'm also seeing this and can repro reliably using llama-benchy on Gemma 4 31B:

/home/node/.openclaw/workspace/src/llama-benchy/.venv/bin/python3 -m llama_benchy --base-url http://192.168.50.50:13305/api/v1 --model Gemma-4-31B-it-GGUF --tokenizer google/gemma-4-31b-it --depth 0 4096 32768 --pp 2048 --tg 32 --latency-mode generation --enable-prefix-caching --format json --save-result /home/node/.openclaw/workspace/tasks/llama-benchy-benchmarks/runs/2026-04-13_0350/gemma-31b-debug.json

I'm running on a Strix Halo 128GB, and serving the model via Lemonade SDK. When the OOM killer kicks in, my checkpoint size is > 17GB (full logs here).

My logs look like this before the OOM killer:

Apr 12 20:58:27 foundry lemond[738016]: 2026-04-12 20:58:27.991 [Info] (Server) Model already loaded: Gemma-4-31B-it-GGUF Apr 12 20:58:27 foundry lemond[738016]: 2026-04-12 20:58:27.991 [Info] (Server) POST /api/v1/chat/completions - Streaming Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv params_from_: Chat format: peg-gemma4 Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) slot get_availabl: id 3 | task -1 | selected slot by LRU, t_last = 172438853933 Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv get_availabl: updating prompt cache Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv load: - looking for better prompt, base f_keep = -1.000, sim = 0.000 Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv update: - cache state: 1 prompts, 17278.135 MiB (limits: 8192.000 MiB, 262144 tokens, 262144 est) Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv update: - prompt 0x55ebc0735130: 6176 tokens, checkpoints: 4, 17278.135 MiB Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv get_availabl: prompt cache update took 0.02 ms Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.940 [Info] (Process) slot launch_slot_: id 3 | task -1 | sampler chain: logits -> ?penalties -> ?dry -> ?top-n-sigma -> top-k -> ?typical -> top-p -> min-p -> ?xtc -> temp-ext -> dist Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.940 [Info] (Process) slot launch_slot_: id 3 | task 422 | processing task, is_child = 0 Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.940 [Info] (Process) slot slot_save_an: id 0 | task -1 | saving idle slot to prompt cache Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.941 [Info] (Process) srv prompt_save: - saving prompt with length 32799, total state size = 6162.851 MiB Apr 12 20:58:36 foundry lemond[738016]: Shutdown signal received, exiting... Apr 12 20:58:36 foundry systemd[1]: lemonade-server.service: A process of this unit has been killed by the OOM killer. Apr 12 20:58:36 foundry systemd[1]: lemonade-server.service: Killing process 867721 (llama-server) with signal SIGKILL. Apr 12 20:58:37 foundry systemd[1]: lemonade-server.service: Failed with result 'oom-kill'. Apr 12 20:58:37 foundry systemd[1]: lemonade-server.service: Consumed 19min 45.613s CPU time, 89.1G memory peak, 6.8G memory swap peak.

Notice:

Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv update: - cache state: 1 prompts, 17278.135 MiB (limits: 8192.000 MiB, 262144 tokens, 262144 est) Apr 12 20:58:28 foundry lemond[738016]: 2026-04-12 20:58:28.939 [Info] (Process) srv update: - prompt 0x55ebc0735130: 6176 tokens, checkpoints: 4, 17278.135 MiB

b1skit avatar Apr 13 '26 04:04 b1skit

The mentioned workaround does not help for my usecase: lots of image processing.

approaching to - CUDA error: out of memory Image

jetnet avatar Apr 14 '26 00:04 jetnet

I feel like image processing with Gemma has a memory leak. When its just being loaded its fine, but once its used with --no-mmproj-offload, RAM usage skyrockets like really bad and beyond what you would expect. I gotta test this more.

Dampfinchen avatar Apr 14 '26 05:04 Dampfinchen

I feel like image processing with Gemma has a memory leak. When its just being loaded its fine, but once its used with --no-mmproj-offload, RAM usage skyrockets like really bad and beyond what you would expect. I gotta test this more.

For now, I am judt using the aforementioned workaround and disabling image input, but I would really like to see this fixed. Is there any response from upstream?

EntityDeleter avatar Apr 14 '26 09:04 EntityDeleter

I can confirm a separate memory issue tied to image mmproj indeed and in that case, the whole "no checkpoint" workaround doesn't work. I'm not a fan of mixing two different problems together, but yeah llama.cpp goes into the deep end memory wise with mmproj files and G4 models.

Also, even if (ignoring image recognition) yes, we can work around the checkpoint issue, having the backend default behavior leading to a crash for this particular model with default settings is still a critical bug in my books.

SerialKicked avatar Apr 17 '26 18:04 SerialKicked