Skip to content

cuda: don't skip GET_ROWS when the GDN state cache may be reused - #501

Open
yaxiaiyuting wants to merge 1 commit into
PrismML-Eng:prismfrom
yaxiaiyuting:fix/gdn-gather-cache-reuse
Open

yaxiaiyuting wants to merge 1 commit into
PrismML-Eng:prismfrom
yaxiaiyuting:fix/gdn-gather-cache-reuse

Conversation

@yaxiaiyuting

Copy link
Copy Markdown

What this fixes

ggml_cuda_try_gdn_gather_skip() lets GATED_DELTA_NET read the recurrent state directly through gather.base instead of copying it with GET_ROWS. Skipping that GET_ROWS removes the only visible reader of the state tensor, so ggml-alloc is free to hand the same memory to a later node of the same graph — and the GDN kernel then reads another node's data. No crash, no CUDA error, just silently wrong numbers.

The existing guard scans later nodes for overlap with ids. The state cache (cache) has exactly the same hazard and was not covered.

Reproduction

Ternary Bonsai 2.5 27B (hybrid attention, PTQ1_0), RTX 4060 Laptop 8 GB:

llama-server -m Ternary-Bonsai-2.5-27B-PTQ1_0-mtp.gguf \
  -c 16384 -ngl 99 -ctk q4_0 -ctv q4_0 --no-kv-offload -ub 256
  • --no-kv-offload → state lives in host memory, the scheduler copies it into the backend compute buffer for the split, and that copy is reused before the GDN op. Asking 1+1 returns empty content and reasoning like .,. and,,,.
  • without --no-kv-offload (state resident on device) → 1 + 1 = 2, correct.
  • -ngl 0 (pure CPU, no CUDA path) → correct.

The discriminator is the batch layout, not the KV quantization: with -ub 512 the fusion happens to be rejected by the existing guards and the output is correct, with -ub 256/128 it is accepted and the output is garbage. Disabling only this fusion (GGML_CUDA_GDN_GATHER_FUSION=0) restores correct output, which is what pointed here.

The change

  • scan later nodes for overlap with cache as well as with ids
  • also refuse the fusion when cache lives in a reusable compute buffer (GGML_BACKEND_BUFFER_USAGE_COMPUTE)

Validation

Same command, patched build (-ngl 99 -ctk q4_0 -ctv q4_0 -ub 256), 16k/32k/64k with --no-kv-offload:

ctx KV 1+1 normal question tg
16k host 1 + 1 = 2 ✅ correct ✅ 11.3 t/s
32k host 1 + 1 = 2 ✅ correct ✅ 11.4 t/s
64k host 1 + 1 = 2 ✅ correct ✅ 11.5 t/s
16k device (ref) 1 + 1 = 2 ✅ correct ✅ 27.9 t/s

Before the patch all three host-KV rows produced garbage.

No regression on the fast path: in the default configuration (state on device) every fusion that was accepted before is still accepted, so throughput is unchanged.

Note

A cheaper alternative to the second condition would be to mark the state as ggml_set_output() / keep GET_ROWS visible to the allocator, but that touches the graph builder; guarding the fusion here is local and keeps the fast path intact for the resident case.

ggml_cuda_try_gdn_gather_skip() lets gated_delta_net read the recurrent
state directly through gather.base instead of copying it with GET_ROWS.
That skip removes the only visible reader of the state tensor, so
ggml-alloc is free to hand the same memory to a later node in the same
graph, and the GDN kernel then reads another node's data. The result is
silent numerical corruption, not a crash.

The existing guard only scanned later nodes for overlap with 'ids'. The
state cache ('cache') has exactly the same hazard, and it is what happens
with --no-kv-offload: the state lives in host memory, the scheduler copies
it into the backend compute buffer for the split, and that copy is reused
before the GDN op runs.

- scan later nodes for overlap with the cache as well
- also refuse the fusion when the cache sits in a reusable compute buffer

Fixes garbage output for ternary Bonsai 2.5 27B with --no-kv-offload
(gated 16k/32k/64k now match the KV-on-device results; default path is
unaffected: every fusion that used to be accepted is still accepted).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant