Repository navigation
cuda: don't skip GET_ROWS when the GDN state cache may be reused - #501
Open
yaxiaiyuting wants to merge 1 commit into
Open
yaxiaiyuting wants to merge 1 commit into
yaxiaiyuting wants to merge 1 commit into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
ggml_cuda_try_gdn_gather_skip()letsGATED_DELTA_NETread the recurrent state directly throughgather.baseinstead of copying it withGET_ROWS. Skipping thatGET_ROWSremoves the only visible reader of the state tensor, soggml-allocis 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:
--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. Asking1+1returns empty content and reasoning like.,. and,,,.--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 512the fusion happens to be rejected by the existing guards and the output is correct, with-ub 256/128it 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
cacheas well as withidscachelives 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:1+11 + 1 = 2✅1 + 1 = 2✅1 + 1 = 2✅1 + 1 = 2✅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()/ keepGET_ROWSvisible 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.