mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
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#4919
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/4141
Author: @macintog
Created: 10/25/2026
Status: 🔄 Open
Base:
main← Head:codex/metal-gu-grid-follows-tile📝 Commits (4)
424b5eemetal: the gate/up grid follows the tile, so a K-quant layer covers its whole ffn widthe0fe19cmetal: the prompt plan's grid follows b.v3e, the condition the dispatch usesce0aa40metal tests: moe_mm_parity --v2 keeps an explicit --dtile11baaaedocs/MAC.md: the window prompt route's speed on an M4 Max📊 Changes
9 files changed (+234 additions, -54 deletions)
View changed files
📝
docs/MAC.md(+1 -1)📝
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(+109 -0)📝
metal/tests/prompt_chain.cpp(+11 -8)📄 Description
Opened as a draft: validated on an M4 Max only. The M5 path needs a run I can't do (see Not run).
Problem and result
This is a critical fix for output quality and parity. On an M4 Max with a K-quant pack, Strata's default prompt path
gives wrong results, with no error or warning. With the fix, on the positions measured below, it agrees with llama.cpp
as closely as Strata's own exact path does.
KL and top-1 are measured against llama.cpp (f16 KV) on the same token ids, over 511 positions after a 32K-token
context. Prompt throughput is measured separately, on 7K-42K-token prompts (the exact fallback on 7K and 15K only).
main(the default)STRATA_METAL_PROMPT=windowThe exact fallback reads the prompt through the decode windows and doesn't use the code this PR changes; I measured
it on this branch's build. Without this fix, the accurate path the docs offer on such a pack is that fallback, which
docs/MAC.mdlists for testing at "about 190 tok/s". On this Mac it reads 78. This PR makes the default chain asaccurate as the fallback at about 7× its speed (6.7× at 7K prompt tokens, 7.0× at 15K). The chain's prompt
throughput is about 23% lower than before, because it now does the work it had been skipping.
What goes wrong. When the pack's routed gate/up experts are K-quants (Q4_K, Q5_K, Q6_K), the prompt chain
computes only a quarter of each expert's gate/up output. The other three quarters are never written, and the down
projection reads them anyway. The prompt's hidden states come out wrong, and every token generated afterwards reads
the attention keys, values and recurrent state they produced.
Why. On a Mac without the M5 matrix-unit layout,
gu2_tile()gives K-quants the tile_16_16_128_2, whichwrites 16 output columns per threadgroup (
f0 = tg.x * TNinmm_gu2). The host sets the gate/up grid once perchunk to the constant
NFF / 64, which is right for the 64-column tiles every other type uses. WithNFF640 thatlaunches 10 threadgroups where 40 are needed, so columns 0-159 are written and 160-639 are not.
This PR carries #2903 by @ctr-137, which fixed exactly this. That PR was closed in the 0.2.0-beta tracker reset. Its
author reported validating it on Windows only, with the host test. I rebased it onto
main(4b9e05a2) and ran it onan M4 Max with Unsloth's Qwen3.8-Flash-Next UD-Q4_K_XL.
Related discussion
#2903: the same defect and fix, closed in the reset ("Closed here does not mean rejected"). I'm opening a new PR
rather than continuing that one because only its author or a maintainer can
/reopenit, and its branch is theauthor's, so the rebase can't go there. Its commit is kept with its original author, and a comment on #2903 links
here. If you'd rather reopen #2903 and take the rebase there, I'll close this one.
The 10-21 M4 Max comment on #2903 reports parity on the same tile from a separately written patch, not from #2903's
code.
Approach
From #2903, unchanged apart from the rebase:
metal/src/moe_tiles.h(new) returns each v2 tile's name together with the TN it writes, so the two can't driftapart, and holds the grid rule (
moe_grid_x).mm_scanwrites the gate/up and down grids (meta[2],meta[5]) per MoE layer from the plan arguments, as italready writes that layer's tile count. A tile chosen per layer needs a grid computed per layer.
window.cpp(streaming) andprompt_chain.inc(resident) pass the grid of the tile each layer dispatches. Theper-chunk host writes are gone.
moe_mm_parityandprompt_chain(the test harnesses) take their v2 tiles from the engine'sgu2_tileanddn2_tile. Before, each had its own v2 tile rule (TN 64 for every type), so their default runs never used theK-quant tile the engine dispatches.
Small commits of mine on top:
b.v3e. Since672fd99cthe resident chain dispatches the v3 tiles only whenb.v3eis set(the trellis types keep v2 on an M5). #2903's plan still chose the grid on
b.k4, so on an M5 with a trellis packit would size the grid for a v3 tile while the v2 tile ran. Both lines now test
b.v3e, the condition the dispatchbelow them uses. The other
b.k4tests inpc_chunkselect attention kernels and stay as they are.moe_mm_parity --v2keeps an explicit--dtile. The new default overwrote it whenever--tilewas absent.docs/MAC.mdgives the exact fallback's speed on this Mac (78 tok/s) beside the existing "about 190", whosemachine isn't recorded.
The rebase had one conflict: the host's grid writes in
pc_setup, which this PR deletes.Default path: the grids for the TN-64 tiles (10 gate/up, 40 down at this pack's sizes) and for the v3 arm (5 and 20)
come out as before. Only v2 K-quant gate/up layers change, from 10 to 40.
Validation
Apple M4 Max (Mac16,5, 128 GB), macOS 27.2 (26B118), Apple clang,
-DCMAKE_BUILD_TYPE=Release. Two builds:mainat4b9e05a2, and this branch at39ca9d99. The engine runs below used430a8440; the two later commits change onlymoe_mm_parity.cppanddocs/MAC.md, so the engine binary is the same.Model: Unsloth's Qwen3.8-Flash-Next UD-Q4_K_XL. Its routed gate/up experts are Q4_K in 47 layers and Q5_K in one, so
every layer takes the 16-column tile.
Passed:
ctest(host testmoe_tile_grid): 39 checks pass. As a negative control I compiled the same test against a copyof
moe_tiles.hwith the K-quant TN set back to 64: 6 checks fail and it exits 1, as expected. (#2903 reported 4failing checks with MSVC.)
moe_mm_parity --v2 --gu <type> --dn Q8_0for Q4_K, Q5_K and Q6_K. The harness runs the engine's tile (TM 16,TN 16, TK 128, SG 2) and all three pass:
This checks the kernel's arithmetic on that tile. The harness sets its own grid, so it doesn't exercise the
engine's indirect grid; item 4 does.
prompt_chain <the pack's Metal GGUF> --prompt 2048(a timing harness on the real weights, random tokens): runs,722.0 and 719.7 tok/s GPU time in two logged runs.
Accuracy of
strataitself against llama.cpp on identical token ids, using upstream'sSTRATA_LOGPOSrows andthe
GENline'spin=(both are inmain). The model's arguments and the run's settings:With
pin, the prefix[0, pin)is read as one batched prompt (the prompt chain, the path this PR changes), andthe last 511 positions go through the decode windows and write one row each. Any damage the prompt chain did to
K/V and recurrent state shows up in those rows. The exact-fallback column adds
STRATA_METAL_PROMPT=window, sothe prefix goes through the decode windows too. There are two requests, which score the same 511 text positions:
pin32,256;pin512. Its rows are numbered from its own start, so add 31,744 tomatch them to the long request's positions.
The reference is llama.cpp (Homebrew libllama 0.7.0, ggml 0.27.0, Metal, f16 KV). It evaluates the same ids
as one context per request (32,767 tokens for long, 1,023 for control) and keeps the full-vocabulary
log-probabilities at those 511 positions. I used a small libllama program that keeps logits only at the chosen
positions;
llama-perplexity --kl-divergence-basegives the same rows at more memory. Both sides are cut to thereference's top 64 tokens and scored with
tools/quant_quality.py'scompare. I can attach that program andthe scoring scripts (about 750 lines together) if they'd help.
KL / top-1 / PPL:
mainThe exact fallback doesn't use the code this PR changes, so I ran it on this branch only. The text is a chat
transcript (a code-review request) that I can't share; any long public text works with the commands above. For
scale only: two fresh llama.cpp f16 runs of this text differ by KL 0.0034, but that was full-vocabulary KL over
2,048 positions between 16K and 32K, so it isn't directly comparable to the top-64 figures.
Prompt speed: the OpenAI server (
serve/server.py --engine strata, the same front end for both builds), startingstratawith the model arguments andSTRATA_METAL_STREAM=0from item 4, without theSTRATA_LOGPOSand runsettings, plus
--max-context 262144. Each prompt is one chat request withmax_tokens1, after one warm-uprequest, timed by the server's
timings.prompt_ms:Arms ran in the order main, branch, main, branch, then the exact fallback once on this branch:
main(s)mainreads 688-704 tok/s, leaving out its first measured request (588 tok/s, 7K). This branch reads 524-546tok/s, about 23% lower throughput. The exact fallback reads 77.6-77.8 tok/s. Token generation doesn't use this
code.
Failed:
mainonly, the baseline:moe_mm_parity --v2for Q4_K, Q5_K and Q6_K gate/up exits 3 before running. ForQ4_K it reports
mtl: no function mm_gu2_12_16_64_64_2(Q5_K and Q6_K name types 13 and 14): its own TN-64 ruleasks for a tile that doesn't exist.
prompt_chainon this pack exits 3 the same way (no function mm_gu2_12_16_64_16_2).moe_mm_parity --v2 --gu Q4_K --dn Q6_Kcrashes (exit 139).N_FF640 isn't a multiple of the256-wide K-quant block, and
ref::random_blockswrites past the buffer thatrow_bytessizes. No real pack usesa K-quant down projection at this width. This PR doesn't change it.
Not run:
b.v3echange on a trellis pack arechecked only by reading the code. The
b.v3echange needs item 4 run with a trellis pack on an M5. That pathtakes the v2 tiles, so the harnesses'
--v3runs don't cover it. Item 4 with a non-trellis pack on an M5 wouldcover the v3 arm.
tile and code path on these Macs, but I haven't run them.
STRATA_METAL_STREAM=1, thewindow.cpphalf of the change): built, not run. Items 4 and5 use the resident chain.
Limits
_16_16_128_2is the one the engine already used for these types. I haven't re-tuned it now thatthe full grid runs; that would be its own change with its own measurement.
🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.