Skip to content

metal: threadgroup-memory alignment and expert-index width fixes - #27944

Closed
tarruda wants to merge 2 commits into
ggml-org:masterfrom
tarruda:metal-correctness-fixes
Closed

metal: threadgroup-memory alignment and expert-index width fixes#27944
tarruda wants to merge 2 commits into
ggml-org:masterfrom
tarruda:metal-correctness-fixes

Conversation

@tarruda

@tarruda tarruda commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Overview

Extracted 2 metal fixes from #27879 as requested by @ggerganov

Requirements

@tarruda
tarruda marked this pull request as ready for review August 29, 2026 09:35
@tarruda
tarruda requested review from a team and ggerganov as code owners August 29, 2026 09:35
// test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 512, 262144, 9216, {1, 1}, {1, 1}));

// test large experts*tokens
test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 512, 10, false, 64, 512, 64));

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.

@tarruda Thanks. And are you sure this test fails on master? It passes on my end without the metal changes in the PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replied here

Seems like it requires some validation to be enabled for it to be detected on master

Comment on lines 802 to 804
void ggml_metal_encoder_set_threadgroup_memory_size(ggml_metal_encoder_t encoder, size_t size, int idx) {
[encoder->obj setThreadgroupMemoryLength:size atIndex:idx];
[encoder->obj setThreadgroupMemoryLength:GGML_PAD(size, 16) atIndex:idx];
}

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.

I have extracted this change into: #27951

threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiitg[[thread_index_in_threadgroup]],
uint tiitg[[thread_index_in_threadgroup]],

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.

This change does not seem necessary, so for now not merging it. That is unless the AI can demonstrate an issue with it that I am not seeing.

@tarruda

tarruda commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

@ggerganov in the interest of full disclosure (in case it was not clear from my previous PRs that touch any of the ML things): I have no idea what I'm doing here. I'm merely a GPT prompter when it comes to any ML/Metal related code. I . Entirely possible this is all just hallucination.

With that said, these issues were surfaced by GPT 5.6 Sol as metal correctness fixes that were uncovered by my Qwen4exp exploration. I have forwarded your comments to it, and here is what is told me:


Yes. ggerganov is correct that the added test passes on master, but that does not invalidate the fixes. The failures only appear with Metal API validation enabled.

   Change                          Unpatched, normal run           Unpatched with MTL_DEBUG_LAYER=1                    Patched with validation
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━  ━━━━━━━━━━━━━━━━━━━━━━━━━
   Threadgroup memory alignment    Qwen4Exp passes                 Aborts: length(4) must be a multiple of 16 bytes    Qwen4Exp passes
  ──────────────────────────────  ──────────────────────────────  ──────────────────────────────────────────────────  ─────────────────────────
   tiitg: ushort -> uint           Added MUL_MAT_ID test passes    Aborts: 65536 must be <= 65535 for tiitg            Test passes

  I reproduced these on an Apple M1 Ultra using Metal code identical to the PR’s master base.

  My assessment:

  - The 16-byte alignment fix is clearly justified. Qwen4Exp directly exposed an existing Metal API violation. Padding centrally is also consistent with another Metal caller that already documents
    the 16-byte requirement.

  - The uint fix is also effective, but it addresses Metal validation of a Qwen-sized dispatch rather than a demonstrated numerical error in normal execution. The local threadgroup only has 128
    threads, so this looks partly like validation-layer compatibility, but using the canonical wider type is harmless.

  - The PR’s current test is not a regression test under standard CI. Apple CI enables GGML_METAL_SHADER_DEBUG, but not Metal API validation. Consequently, that test passes both before and after the
    change.

  - The fixes should therefore be described as pre-existing Metal API/validation problems uncovered by Qwen4Exp, not as ordinary Qwen numerical-correctness failures.

  The strongest improvement would be a targeted test invocation with MTL_DEBUG_LAYER=1. Otherwise, ggerganov cannot reproduce the failure from the test as currently configured.

  All temporary diagnostic changes have been removed; only the previously existing untracked comparison report remains.

@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 Aug 29, 2026
@ggerganov

Copy link
Copy Markdown
Member

Ok, got it. I can see the error with MTL_DEBUG_LAYER=1. Seems benign, but might be useful to do it like this so that we can see other potential errors in the future. I'll consider what to do from here - thanks.

@ggerganov in the interest of full disclosure (in case it was not clear from my previous PRs that touch any of the ML things): I have no idea what I'm doing here. I'm merely a GPT prompter when it comes to any ML/Metal related code. I . Entirely possible this is all just hallucination.

Yes, no problem at all. I've already accepted that the AI is going to obsolete our job soon. Just trying to understand and learn a few more things while still can.

@tarruda

tarruda commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

I'll consider what to do from here - thanks.

Ok closing this one then. LMK if I can do anything else to help.

Yes, no problem at all. I've already accepted that the AI is going to obsolete our job soon. Just trying to understand and learn a few more things while still can.

😢

This is a side discussion, but no matter how good AI gets at programming GPU kernels, it still cannot always make good judgement calls, especially when the whole architecture of a system is involved.

When I'm prompting LLMs, I try to make it as combative to my ideas as possible, but I still see a lot of sycophancy, basic mistakes and overall slop, even on SOTA models like GPT 5.6 Sol.

Obviously I cannot predict the future, but with the current technology, it is hard to see how a frontier model will be able to obsolete a domain expert like you paired with a small local model for doing grunt work/investigations. So keep up the good work!

@tarruda tarruda closed this Aug 29, 2026
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 testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants