ggml-metal: fix int32 overflow in kernel_mul_mm batched dst offsets (deep-context corruption) - #28210
Draft
feni6 wants to merge 1 commit into
Draft
ggml-metal: fix int32 overflow in kernel_mul_mm batched dst offsets (deep-context corruption)#28210feni6 wants to merge 1 commit into
feni6 wants to merge 1 commit into
Conversation
…dexing The per-batch output offsets im*N*M (dst4 path) and im*ne1*ne0 (tensor path) are computed in 32-bit and wrap once a batch base offset reaches 2^31 elements. For a large f32 dst such as attention KQ [n_kv, n_ubatch, n_head], head h's base wraps once h*n_ubatch*n_kv >= 2^31 - first at h = ceil(2^31/(n_ubatch*n_kv)) - so the top heads of a deep-context decode are written ~8.6 GiB below the tensor (corrupting whatever lives there) while their own region is read back stale. Observed as GLM-5.3-Flash collapsing to repeated '@' (token 31) past a joint (depth, n_ubatch, n_ctx) threshold on Metal; root-caused with a standalone kernel probe whose first-faulty-head matches the arithmetic exactly on M4 Pro, M3 Ultra and M3 Max. Promote the offset arithmetic to uint64_t at the three affected sites. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUN8B4NPpyNhziETmLiNpk
This was referenced Sep 1, 2026
|
Hi @feni6, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
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.
Summary
kernel_mul_mmcomputes its per-batch output offsets in 32-bit:dstBatch = dst + im * N * M(dst4 path)... + im*args.ne1*args.ne0(tensor path, two sites)For a large f32 destination the product overflows
int32once a batch's base offset reaches 2³¹ elements. The visible case is attention KQ = [n_kv, n_ubatch, n_head] at long context: headh's base wraps onceh·n_ubatch·n_kv ≥ 2³¹, i.e. first ath = ceil(2³¹/(n_ubatch·n_kv)). The wrapped heads are stored ~8.59 GiB below the tensor — silently corrupting whatever buffer happens to live there in GPU address space — while their own region is never written, so softmax ingests stale scratch.This PR promotes the offset arithmetic to
uint64_tat the three affected sites. Three lines, no functional change below 2³¹.Real-world impact
GLM-5.3-Flash (#27754, #27752) collapses to an infinite
@(token 31) from the first sampled token once a joint (depth,n_ubatch,n_ctx) threshold is crossed on Metal — e.g. a 108,710-token prompt at-c 131072 -ub 512. The model's dense-masked DSA attention keeps KQ at full[n_kv, 512, 64]during prefill, crossing the 13 GiB f32 mark deep into context. Whether the wrap is fatal depends on what lives below the compute buffer — on our hosts, the model's own K-cache (GPU-VA adjacent, gap 0.000 GiB), which explains the depth/n_ctxdependence of the failure boundary: full diagnosis in this comment.Any model/backend-path combination that drives
mul_mmwith ≥2³¹-element batched f32 output is exposed; deep-context MLA/DSA-style attention is simply the first to get there.Evidence
ceil(2³¹/(ub·n_kv))across five geometries and three machine classes: M4 Pro, M3 Ultra (ours), M3 Max (independent confirmation by @eauchs, including the negative control: casts removed → probe fails again).-c 524288) all pass on the patched build; byte-identical prompts on the unpatched build still collapse (battery summary).-s 1234),test_mtpdraft+reload OK on three backends, seven neighbouring archs FAIL=0.Notes
-ub 128is the workaround in the wild for affected models (keepsh·ub·n_kv < 2³¹longer); this fix removes the need for it.Fixes the Metal deep-context collapse reported in #27754 / #27752 (model PRs, not yet merged; the defect is in shared ggml code and reproduces on both).
🤖 Generated with Claude Code
https://claude.ai/code/session_01YUN8B4NPpyNhziETmLiNpk