Skip to content

metal : stop overflow from happening in large matrix output offsets - #30072

Merged
ggerganov merged 2 commits into
ggml-org:masterfrom
feni6:metal-mul-mm-int64-offsets
Oct 26, 2026
Merged

ggerganov merged 2 commits into
ggml-org:masterfrom
feni6:metal-mul-mm-int64-offsets

Conversation

@feni6

@feni6 feni6 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Overview

Use 64-bit arithmetic for the destination offsets in both store paths of kernel_mul_mm. This stops mistakes from happening when those offsets are above the signed 32-bit range.

Additional information

Supplied is a regression test that covers full and edge tiles both. It’s disabled by default because it needs about 45 GB of memory. On a M4 Pro Mac mini it failed before the fix and passed afterwards. When it was enabled, 1,860 Metal MUL_MAT tests all passed.

tests/test-backend-ops.cpp:

-#if 0
+#if 1
     // > 2^31-element dst (8.9 GB), about 45 GB peak memory. Too large to be enabled by default.
cmake -B build -DGGML_METAL=ON -DLLAMA_BUILD_TESTS=ON -DLLAMA_CURL=OFF -DCMAKE_BUILD_TYPE=Release
cmake --build build --target test-backend-ops -j 8
./build/bin/test-backend-ops test -b MTL0 -o MUL_MAT -p 'm=1048576'
./build/bin/test-backend-ops test -b MTL0 -o MUL_MAT

Base: c479922ac520a08969b4c1dc154d7bbb3c386d85; Apple M4 Pro, 64 GB, macOS 26.6.2.

Before: test only (abridged; ANSI colors removed)
ggml_metal_device_init: has tensor            = false
Testing 3 devices

Backend 1/3: MTL0
  Device description: Apple M4 Pro
  Device memory: 53084 MB (53083 MB free)

[MUL_MAT] ERR = 0.020336554 > 0.000500000   MUL_MAT(type_a=f32,type_b=f32,m=1048576,n=40,k=64,bs=[1,1],nr=[53,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0,m_v=0,pad=0): FAIL
ggml_metal_free: deallocating
  0/1 tests passed

Failing tests:
  MUL_MAT(type_a=f32,type_b=f32,m=1048576,n=40,k=64,bs=[1,1],nr=[53,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0,m_v=0,pad=0)
  Backend MTL0: FAIL
Backend 2/3: BLAS
  Skipping
Backend 3/3: CPU
  Skipping
2/3 backends passed
FAIL
After: fix and test (abridged; ANSI colors removed)
ggml_metal_device_init: has tensor            = false
Testing 3 devices

Backend 1/3: MTL0
  Device description: Apple M4 Pro
  Device memory: 53084 MB (53083 MB free)

  MUL_MAT(type_a=f32,type_b=f32,m=1048576,n=40,k=64,bs=[1,1],nr=[53,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0,m_v=0,pad=0): OK
ggml_metal_free: deallocating
  1/1 tests passed
  Backend MTL0: OK
Backend 2/3: BLAS
  Skipping
Backend 3/3: CPU
  Skipping
3/3 backends passed
OK
Full Metal MUL_MAT suite (abridged; ANSI colors removed)
ggml_metal_device_init: has tensor            = false
  1860/1860 tests passed
  Backend MTL0: OK
Backend 2/3: BLAS
  Skipping
Backend 3/3: CPU
  Skipping
3/3 backends passed
OK

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude did the investigating of the bug and wrote the fix and regression test. Codex prepared the branch and reran verification.

@feni6
feni6 requested review from a team and ggerganov as code owners October 6, 2026 23:07
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Oct 6, 2026
@nikwen

nikwen commented Oct 7, 2026

Copy link
Copy Markdown
Member

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Automated code review

The diff is small and targeted: two 32-bit -> 64-bit offset fixes in the legacy kernel_mul_mm store paths, plus a disabled-by-default regression test. The approach matches the existing convention already used throughout mul_mv.metal ((uint64_t)im*args.ne0*args.ne1) and mul_mv_mma.metal, and the test follows the existing #if 0 large-test convention right below it. I verified the casts bind correctly to the parenthesized expressions and that pointer arithmetic then proceeds in 64-bit. The fix is correct for the paths it touches, but the same overflow survives in sibling code paths in the same file.

Blocking - incomplete fix for the stated goal

(point 1) The tensor-API variant of the same kernel is still broken. ggml/src/ggml-metal/kernels/mul_mm.metal:138 (inside #ifdef GGML_METAL_HAS_TENSOR):

device float * dstBatch = (device float *)dst + im * N * M;

im, N, M are all int, so this is the exact same overflow. This is not dead code: on Metal 4 GPUs (props.has_tensor, ggml-metal-device.m:1146) the runtime compiles mul_mm.metal with -DGGML_METAL_HAS_TENSOR into ggml-tensor.metallib and dispatches this variant instead of the legacy one (ggml-metal-device.cpp:936). So on an M5-class device the regression test added in this PR would still fail after the fix. The PR description's machine log shows has tensor = false, which explains why the author could not observe this. Apply the same cast here, e.g. (uint64_t) im*N*M.

Will slow the review

(point 2) kernel_mul_mm_id in the same file has the identical unfixed pattern. ggml/src/ggml-metal/kernels/mul_mm.metal:827:

device float * D = (device float *) dst + r0 + ide*args.ne0 + idt*args.ne1*args.ne0;

For MUL_MAT_ID the dst is ne00 x ne21 x ne20, and idt*args.ne1*args.ne0 overflows with plausible MoE shapes (e.g. hidden 4096, 8192 tokens, 8 used experts exceeds 2^31 elements). Since this is the same file, same op family, and same bug class, reviewers will likely ask for it in this PR or an explicitly linked follow-up. Note this path is shared by both the tensor and legacy variants of kernel_mul_mm_id.

Nits

(point 3) In the full-tile path you fixed, a small sub-expression is still 32-bit: mul_mm.metal:326, C + 8*(i%4) + 8*args.ne0*(i/4). 8*args.ne0*(i/4) only overflows when ne0 >= 2^28 (~268M columns), which is unlikely, but if the goal is 64-bit-safe dst offsets, casting that term too costs nothing and removes the exception.

(point 4) Out of scope but same sweep: mul_mv.metal:293 (mul_vec_q_n_f32_impl) is the lone remaining im*args.ne0*args.ne1 + r1*args.ne0 in int32; every other mul_mv kernel already got the uint64 treatment. Worth a follow-up issue if not included here.

Test

The regression test is well formed (dst = 2.22e9 elements, batch 52 offset lands just past INT32_MAX, mixes full and edge tiles so both store paths are covered), kept behind #if 0 consistent with the neighboring >4GB test, and the comment is concise ASCII. No CI impact since it is disabled.

Everything else checks out: no API/scope concerns, comments follow the style rules, AI disclosure is present in the PR template, and no unrelated changes are bundled. Fixing point 1 (and ideally point 2) before merge would make this a complete fix for "stop overflow in kernel_mul_mm output offsets".

This review was generated automatically by pi coding agent using zai-org/GLM-5.3. It may contain mistakes. Maintainers make the final call.

@nikwen nikwen changed the title Metal : stop overflow from happening in large matrix output offsets metal : stop overflow from happening in large matrix output offsets Oct 7, 2026
nikwen
nikwen previously requested changes Oct 7, 2026

@nikwen nikwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feni6 Thanks for the PR! The review bot found a couple of additional places that we'll also need to update. Would you be able to update the PR with those?

@ggerganov ggerganov self-assigned this Oct 7, 2026
@feni6

feni6 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — I’ve addressed points 1–3. The additional destination offsets now use 64-bit arithmetic. MUL_MAT_ID also needed its two token indices widened from short to int; casts alone wouldn’t fix the 32,768-token limit.

I added two disabled regression cases. The small case fails before the update and passes afterwards on both Metal paths. All 1,859 MUL_MAT and 867 MUL_MAT_ID default tests pass on each path. The large output shapes also pass element-by-element checks using the Metal-only checker; the approximately 45 GB in-tree cases weren’t rerun.

Testing used an M4 Pro, with the tensor API explicitly enabled for those runs; no M5 hardware was tested.

#28748 overlaps the tensor batch-offset change and fixes a separate tensor-slice offset that this update doesn’t address. The mul_mv finding remains a separate follow-up.

AI assistance: Codex applied the supplied patches and reran verification.

@ggerganov

Copy link
Copy Markdown
Member

@feni6 Thanks for the patch. Could you rebase on the latest master?

@nikwen

nikwen commented Oct 19, 2026

Copy link
Copy Markdown
Member

@feni6 Friendly ping about the merge conflict. :)

@feni6

feni6 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

I'm on it. Thanks for the reminder.

@feni6
feni6 force-pushed the metal-mul-mm-int64-offsets branch from 2e87e36 to 60d9f30 Compare October 21, 2026 18:15
@feni6

feni6 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Done. Let me know if there's any problems with it please, or if you'd like any more information about it, including how it was tested.

@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 25, 2026
@ggerganov
ggerganov merged commit 9b8446b into ggml-org:master Oct 26, 2026
29 checks passed
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
…gml-org#30072)

* Metal : stop overflow from happening in large matrix output offsets

Assisted-by: Codex

* metal : widen batch offsets and MUL_MAT_ID token indices

Assisted-by: Codex

---------

Co-authored-by: feni6 <270037094+feni6@users.noreply.github.com>
@BrewTestBot BrewTestBot mentioned this pull request Oct 28, 2026
1 task done
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 30, 2026
…gml-org#30072)

* Metal : stop overflow from happening in large matrix output offsets

Assisted-by: Codex

* metal : widen batch offsets and MUL_MAT_ID token indices

Assisted-by: Codex

---------

Co-authored-by: feni6 <270037094+feni6@users.noreply.github.com>
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Nov 1, 2026
…gml-org#30072)

* Metal : stop overflow from happening in large matrix output offsets

Assisted-by: Codex

* metal : widen batch offsets and MUL_MAT_ID token indices

Assisted-by: Codex

---------

Co-authored-by: feni6 <270037094+feni6@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants