mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #2903] [CLOSED] [Engine] metal: the gate/up grid follows the tile, so a K-quant layer covers its whole ffn width #4323
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4323
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/2903
Author: @ctr-137
Created: 10/17/2026
Status: ❌ Closed
Base:
main← Head:metal-gu2-tile-grid📝 Commits (1)
fa8a309metal: the gate/up grid follows the tile, so a K-quant layer covers its whole ffn width📊 Changes
8 files changed (+232 additions, -53 deletions)
View changed files
📝
metal/CMakeLists.txt(+6 -0)📝
metal/kernels/moe_mm.metal(+10 -3)📝
metal/src/engine/prompt_chain.inc(+10 -6)📝
metal/src/engine/window.cpp(+20 -31)➕
metal/src/moe_tiles.h(+42 -0)📝
metal/tests/moe_mm_parity.cpp(+25 -5)➕
metal/tests/moe_tile_grid_test.cpp(+108 -0)📝
metal/tests/prompt_chain.cpp(+11 -8)📄 Description
The prompt chain's gate/up GEMM is dispatched over the wrong grid on a Mac without the M5 right-input layout, for
any pack whose routed gate/up is a K-quant. It computes 160 of each expert's 640 gate/up rows and leaves the rest as
the previous layer left them, and the down GEMM reads that: wrong tokens, no error, no warning.
What happens
The prompt GEMMs are dispatched indirectly, and
mm_scanpublishes the grids one MoE layer at a time. Every one ofthose fields is per layer —
meta[3]is this layer's tile count — except the gate/up grid x, which the host wroteonce per chunk as the constant
NFF / 64(window.cpp:2132,prompt_chain.inc:96).The tile is not a constant. It is chosen from the layer's quant type, and
gu2_tile()hands Q4_K, Q5_K and Q6_K aTN-16 tile (
_16_16_128_2).mm_gu2writes TN output columns ofHSper threadgroup (moe_mm.metal:302,f0 = tg.x * TN), so the launch has to coverNFF / TNthreadgroups. WithNFF640 the constant gave 10 and theK-quant tile needs 40: rows 0..159 written, 160..639 stale.
The kernel's own header said so —
moe_mm.metal:47, "gx_gu in meta[2] ... written by the host once" — and thedispatch picks the tile the grid has to match:
Reachability
No flag needed.
docs/MAC.md:61offers--model ...|UD-Q6_K_XLon any Apple Silicon Mac, anddocs/UNSLOTH_Q6.md:35-38records that pack'sffn_gate_exps/ffn_up_expsas Q6_K in 47 of 48 layers, withffn_down_expsQ8_0 (whosemm_down2instance exists, so the whole prompt path runs). On an M1–M4 every promptchunk of 64 rows or more then multiplies garbage into the hidden states. The non-M5 configurations the docs
measured use TN-64 tiles, which is why it had not been seen.
What I changed
metal/src/moe_tiles.h(new) — the v2 tiles and the TN each one writes, returned together so they cannotdrift apart, plus the v3 tiles' TN/SG and the grid rule.
metal/kernels/moe_mm.metal—MmPlanArgscarriesgu_gx/dn_gxandmm_scanwritesmeta[2]/meta[5]from it. It already writes the sibling fields per layer; one host value for the whole chunk cannot serve a tile
that varies by layer.
window.cpp/prompt_chain.inc— the plan carries the grid of this layer's tile, the v3 arm derives itfrom the tile it names, and the host writes are gone.
(
window.cpp:834,NFF / (gs * (32 / gl) * gr)).Why no test caught it
Both harnesses picked their v2 tile from a rule of their own instead of the engine's, so the engine's TN-16 dispatch
was never in front of a parity run:
Both now use
gu2_tile/dn2_tile, andmoe_mm_parity's v2 default parses the tile's own name back and refuses torun if the name and its TN disagree.
Tests
metal/tests/moe_tile_grid_test.cpp(new) — a host test: no Metal device, no model, links nothing, registeredwith ctest, so it runs on a configured tree whether or not there is a card. It checks the tile table, that every
tile's name parses back to the TN it declares, that the TN divides the output width (an extra threadgroup writes
past
HS, a missing one leaves the tail unwritten), and that a K-quant layer's grid is 40 where the constant gave10.
Compiled and run with MSVC 19.51
/std:c++20: 39 checks, all pass. With the header's K-quant TN put back to64 as a counter-proof, four fail and it exits 1.
moe_mm_parity.cpp/prompt_chain.cpp— updated for the plan struct and to run the engine's own tiles.What I could not check
Nothing here was built or run on macOS. This review was done on Windows with no GPU and no Metal toolchain, so
the two harness changes are not run: they are the runs that would confirm the fix end to end, and they are the kind
of thing
docs/TEST_REQUESTS.mdasks for. What I have is the host test above and the reading in this description.What I left alone
dn2_tilereturns TN 64 for every type, so the down grid was never wrong. It is derived the same way now forsymmetry, not because it was broken.
layers whose dispatch changes are the v2 K-quant ones, which were wrong.
There is no issue for this — it came out of a static audit of the tree rather than a report. Happy to file one if
you would rather triage it there first.
🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.