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

Eval bug: Gemma 4 generates <unused> tokens in infinite loop

Open CSCSoftware opened this issue 7 months ago • 53 comments

Bug Description

Gemma 4 models generate an infinite stream of <unused> tokens (Token ID 14 = <unused8>) on the Vulkan backend, both with GPU offloading and CPU-only. No valid text is produced — the model runs until MaxTokens is exhausted.

This happens despite having all known Gemma 4 fixes applied:

  • Tokenizer fix (#21343)
  • Template parser fix (#21326)
  • Custom newline split (#21406)
  • Byte token handling (#21488)
  • Logit softcapping (#21390)
  • Plus the F32 MoE precision patch from PR #21506 (manually applied)

Environment

  • OS: Windows 11 Pro (10.0.26100)
  • GPU: NVIDIA (24 GB VRAM), Vulkan backend
  • llama.cpp: Built from 94ca829b6 (master, 2026-04-06) + PR #21506 patch
  • Model: gemma-4-E2B-it-Q4_K_M.gguf from unsloth/gemma-4-E2B-it-GGUF
  • Build: CMake + MSVC + GGML_VULKAN=ON

Steps to Reproduce

  1. Build llama.cpp from current master with Vulkan enabled
  2. Load gemma-4-E2B-it-Q4_K_M.gguf with -ngl 99
  3. Send any prompt (e.g., "Hello")
  4. Observe: model generates ~18000+ tokens of <unused8> (token id=14) without producing any readable text or hitting EOG

Diagnostic Data

Token sampling output (first 10 tokens):

Token[0] id=14
Token[1] id=14
Token[2] id=14
Token[3] id=14
Token[4] id=14
Token[5] id=14
Token[6] id=14
Token[7] id=14
Token[8] id=14
Token[9] id=14

Token 14 in Gemma 4 vocab = <unused8>.

Generation stats: 183 tok/s, 18432 tokens generated, 44.7 seconds — no EOG token emitted.

Additional Testing

  • CPU-only (-ngl 0): Same result — generates [multimodal] tokens (id=5) in an infinite loop. Also broken.
  • PR #21506 applied (F32 MoE FFN precision): No improvement on either CPU or Vulkan.
  • Ollama: Same model works correctly in Ollama (which uses its own llama.cpp fork), producing valid responses.

Init Logs (successful)

Model handle: OK
Vocab size: 262144
Layers: 35
Context size: 32768
Gemma tokens: start_of_turn=105, end_of_turn=106, bos=2
System tokens: 11
Initialization complete!

Model loads correctly, context/sampler/batch all initialized — the issue is purely in inference/sampling.

Related Issues

  • #21321 — Gemma 4 generates <unused24> tokens (CUDA, partially fixed by #21506)
  • #21343 — Gemma 4 tokenizer fix (merged, applied here)
  • #21506 — F32 MoE precision (open, manually applied, does not fix this)

The root cause may be Vulkan-specific numerical precision issues beyond what #21506 addresses, or a different code path in the Vulkan compute shaders.

CSCSoftware avatar Apr 06 '26 14:04 CSCSoftware

@CSCSoftware does https://github.com/ggml-org/llama.cpp/pull/21451 help in this case?

pwilkin avatar Apr 06 '26 18:04 pwilkin

@CSCSoftware does #21451 help in this case?

I am still having the same issue even with latest b8702. Tried both Vulkan and Cuda!

Image

fchamieh avatar Apr 08 '26 04:04 fchamieh

I encounter the exact same issue on a strix halo 128gb using vulkan and llama.cpp. So it's not just CSCSoftware and fchamieh's hardware. I am running fedora 43.

KYmidnight avatar Apr 08 '26 07:04 KYmidnight

I am getting the same with CUDA. I am on Windows, but run inside WSL. I am using one of the unsloth quants.

HDembinski avatar Apr 08 '26 10:04 HDembinski

I'm also encountering the same issue on a strix halo 128gb using vulkan and llama.cpp (built locally). I'm running Ubuntu 24.04 LTS. I'm using ggml-org/gemma-4-31B-it-GGUF:Q8_0 from Hugging Face.

smebberson avatar Apr 08 '26 23:04 smebberson

If you are having issues with the Vulkan backend, please post exact commands, model, logs and hardware that reproduce the problem. Something like this report for example:

https://github.com/ggml-org/llama.cpp/issues/21622#issuecomment-4207254477

ggerganov avatar Apr 09 '26 08:04 ggerganov

This is how I reproduce it:

llama-cli -hf ggml-org/gemma-4-31B-it-GGUF:Q8_0 --n-gpu-layers 60 --ctx-size 32768 --no-mmap --flash-attn on --jinja --reasoning-budget 0
Image

The screenshot shows the llama.cpp version and device information.

smebberson avatar Apr 09 '26 10:04 smebberson

cc @0cc4m - I can't reproduce on DGX Spark. Might be related to strix halo specifically.

@smebberson Btw, is there a reason why you use -ngl 60? You have plenty of memory to offload all 61 layers.

ggerganov avatar Apr 09 '26 10:04 ggerganov

@ggerganov - oh, it was just a left over from testing different flags. In one of the issues here someone said it helped to alleviate the unused24 issue by leaving one on the CPU.

smebberson avatar Apr 09 '26 11:04 smebberson

I am also experiencing this issue on my setup. Here are the details for reproduction:

Hardware: [Describe your GPU/CPU, e.g., NVIDIA RTX 4090 / Intel i9] OS: [e.g., Windows 11 / Ubuntu 24.04] Model: [e.g., gemma-4-27b-it-Q4_K_M.gguf] Command: [Insert the exact llama-cli or main command used]

On Thu, Apr 9, 2026, 7:02 AM Scott Mebberson @.***> wrote:

smebberson left a comment (ggml-org/llama.cpp#21516) https://github.com/ggml-org/llama.cpp/issues/21516#issuecomment-4213632103

@ggerganov https://github.com/ggerganov - oh, it was just a left over from testing different flags. In one of the issues here someone said it helped to alleviate the unused24 issue by leaving one on the CPU.

— Reply to this email directly, view it on GitHub https://github.com/ggml-org/llama.cpp/issues/21516#issuecomment-4213632103, or unsubscribe https://github.com/notifications/unsubscribe-auth/B7GFMJN73OVCFEZV2AWRE4D4U57OHAVCNFSM6AAAAACXOGW566VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DEMJTGYZTEMJQGM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

hayesmason335-hub avatar Apr 09 '26 13:04 hayesmason335-hub

I've tested on DGX Spark and Strix Halo and can't reproduce the problem on either. They both work fine with Vulkan. @jeffbolznv Do you see anything?

0cc4m avatar Apr 10 '26 04:04 0cc4m

Is this the same as https://github.com/ggml-org/llama.cpp/issues/21321?

jeffbolznv avatar Apr 10 '26 04:04 jeffbolznv

Maybe, but that was a CUDA fusion bug. Is it possible there's a similar problem in the Vulkan fusion code?

0cc4m avatar Apr 10 '26 05:04 0cc4m

It's possible, I've tried to catch memory overlap stuff in a general way but it's possible there's a bug.

jeffbolznv avatar Apr 10 '26 05:04 jeffbolznv

So.... this works?!

llama-cli -hf unsloth/gemma-4-31B-it-GGUF:Q8_0 --n-gpu-layers 99 --ctx-size 32768 --no-mmap --flash-attn on --jinja --reasoning-budget 0
Image

The original model I was using is ggml-org/gemma-4-31B-it-GGUF:Q8_0, but I switched to unsloth/gemma-4-31B-it-GGUF:Q8_0 and it works?

smebberson avatar Apr 10 '26 11:04 smebberson

Nothing works for me!!!! I am having the most horrible experience with Gemma 4 on llama.cpp, while I was able to run it with Ollama.

L:\ai-models
λ llama-cli --model L:\ai-models\gemma-4-31B-it-IQ4_XS.gguf  --n-gpu-layers 99 --ctx-size 32768 --no-mmap --flash-attn on --jinja --reasoning-budget 0 --tensor-split 75,25
ggml_cuda_init: found 2 CUDA devices (Total VRAM: 32767 MiB):
  Device 0: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes, VRAM: 24575 MiB
  Device 1: NVIDIA GeForce RTX 2070, compute capability 7.5, VMM: yes, VRAM: 8191 MiB
load_backend: loaded CUDA backend from C:\LLamaCpp\ggml-cuda.dll
load_backend: loaded RPC backend from C:\LLamaCpp\ggml-rpc.dll
load_backend: loaded CPU backend from C:\LLamaCpp\ggml-cpu-alderlake.dll

Loading model...


▄▄ ▄▄
██ ██
██ ██  ▀▀█▄ ███▄███▄  ▀▀█▄    ▄████ ████▄ ████▄
██ ██ ▄█▀██ ██ ██ ██ ▄█▀██    ██    ██ ██ ██ ██
██ ██ ▀█▄██ ██ ██ ██ ▀█▄██ ██ ▀████ ████▀ ████▀
                                    ██    ██
                                    ▀▀    ▀▀

build      : b8747-fb38d6f27
model      : gemma-4-31B-it-IQ4_XS.gguf
modalities : text

available commands:
  /exit or Ctrl+C     stop or exit
  /regen              regenerate the last response
  /clear              clear the chat history
  /read <file>        add a text file
  /glob <pattern>     add text files using globbing pattern


> hello there

<unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24><unused24>

[ Prompt: 23.3 t/s | Generation: 31.0 t/s ]

fchamieh avatar Apr 10 '26 21:04 fchamieh

@fchamieh Are you using CUDA 13.2 by any chance?

FWIW I've been using Bartowski's Q5_K_S quant with my 32GB VRAM setup and it's been working out very well.

pwilkin avatar Apr 10 '26 22:04 pwilkin

@fchamieh Are you using CUDA 13.2 by any chance?

FWIW I've been using Bartowski's Q5_K_S quant with my 32GB VRAM setup and it's been working out very well.

I am using the CUDA 13.1 files provided with the release.

Image

I even tested the Vulkan variant. Same shit.

fchamieh avatar Apr 11 '26 08:04 fchamieh

I don't know if it is related, but I also get unused token responses with the 26b moe. I tried multiple quants form ggml and unsloth (recent reuploads). If I use the --cpu-moe option, everything is fine. If I use only one GPU, everything is also fine. But with 2 GPUs I get the same behaviour as described by others (2x rtx pro 4000, CUDA 13.0, Ubuntu 24).

<unused46><unused15><unused38><unused31><unused50><unused32><unused23><unused30><unused44><unused34><unused42><unused19><unused18><unused53><unused1><unused16><unused26><unused14><unused69><unused7><unused67>[multimodal]<tool_response|><unused39><unused61><unused2><unused17><unused70>

fighter3005 avatar Apr 11 '26 12:04 fighter3005

Confirming on Strix Halo (gfx1151) — Radeon 8060S iGPU

System:

  • AMD Ryzen AI MAX+ 395, 128 GB unified memory
  • Radeon 8060S Graphics (RADV STRIX_HALO, gfx1151)
  • NixOS unstable, Mesa RADV Vulkan driver
  • llama.cpp build 8667 (commit c08d28d) via nixpkgs#llama-cpp-vulkan

Model: gemma-4-e4b-it-Q4_K_M.gguf from ggml-org/gemma-4-E4B-it-GGUF

Reproduction:

llama-server -m gemma-4-e4b-it-Q4_K_M.gguf -ngl 99 --port 8091 -c 4096
curl -s http://localhost:8091/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"gemma4","messages":[{"role":"user","content":"Write a Python function that reverses a string."}]}'

Result: 43/43 layers offloaded to Vulkan0 successfully. Model loads fine. Output is infinite <unused24> tokens (4096 tokens generated, all garbage). Prompt eval completes normally.

What works on the same hardware:

  • gemma4 on CPU (Ollama, think: false): correct output, 2.9 tok/s

What doesn't:

  • gemma4 via Ollama Vulkan: plans 43 layers on Vulkan0, but alloc_tensor_range fails silently → falls back to CPU (related: ollama/ollama#15261)
  • gemma4 via llama.cpp Vulkan (this issue): offloads successfully but produces <unused24> garbage

Observations:

  • The Vulkan allocation succeeds in llama.cpp 8667 where Ollama's embedded llama.cpp fails — suggests the offloading fix (PR #21566) is partially in this build
  • But the compute path itself is broken — output is garbage when running on Vulkan
  • RADV STRIX_HALO is a unified memory iGPU — no PCIe transfer overhead, pure compute issue

nextlevelshit avatar Apr 12 '26 16:04 nextlevelshit