mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #2652] [CLOSED] Metal: the routing trace of a resident window (STRATA_METAL_DUMP_ROUTING) #4191
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#4191
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/2652
Author: @alvarorsouza-arch
Created: 10/15/2026
Status: ❌ Closed
Base:
main← Head:mac-routing-trace📝 Commits (1)
dd7a07fMetal: the routing trace of a resident window (STRATA_METAL_DUMP_ROUTING)📊 Changes
2 files changed (+66 additions, -0 deletions)
View changed files
📝
metal/kernels/moe.metal(+13 -0)📝
metal/src/engine/window.cpp(+53 -0)📄 Description
What this changes
--dump-routingandtools/collect_profile_traces.py --mode routingwork on the pooled backends because the routed ids reach the host through the doorbell the expert pool reads (src/program/generate.cpp:5929prints exactly that when you pass--no-pool). The Metal engine has no host pool, it reads experts in place from the mapped file, so on a Mac the flag is accepted and the trace comes out empty:The file exists, so
tools/collect_profile_traces.pyreports success, andtools/make_profile.pyhas nothing to read. That blocks the inputtools/quant_bound.pywants from the engine's window logs, and falls back to--union(default 1.0) without them: U(P), the distinct experts a pass of P tokens reads per expert a single token reads. The window logs give it on the pooled backends because they count distinct against routed experts per layer; on Metal that line readsCPU experts 0.00 distinct / 0.00 routed per layer.Two files, 66 added lines, Metal only:
metal/kernels/moe.metal: aroute_dumpkernel that copies one layer'smoe_routeoutput into that layer's own slot. It has to be a device copy becauseids_is reused by the next layer and a resident window never hands the host a turn between layers.metal/src/engine/window.cpp:STRATA_METAL_DUMP_ROUTING=pathopens the file, allocates one slot pair per layer, dispatchesroute_dumpright after the route dispatch inWindow::layer(the main model's window builder only, not the draft one, since both would write slot 0), andfinish()writes the window's records in the formattools/make_profile.py:read_tracereads:int32 layer, int32 k, k int32 ids, k float weights.What it leaves alone
Opt-in by environment variable, default path untouched. With the variable unset no buffer is allocated, no dispatch is added to the window,
routing_write()returns on its first line, and no file is opened. The core is unchanged. It is an environment variable rather than a flag because the core exposes no routing trace path to the backend (nothing ininclude/orsrc/core/carries one); it followsSTRATA_METAL_KPROFnext to it. Tell me where you would rather hang it and I will move it.CUDA, HIP and SYCL builds are not affected:
metal/is its own CMake project and does not enter those builds, and no shared file is touched.How I checked
Apple M3 Max, 40-core GPU, 128 GB unified memory, macOS 26.6, in high power mode. Three one-shot runs of one command (
--spec 2with the MTP draft layer,--expert-cache 64,--max-context 65536, experts resident,--tokens-filea 37 token prompt,--max-new 120 --greedy):mainat296b59dwith no patch: 61.94 tok/sThe
output :line is the same 120 token ids in all three, byte for byte.The trace is 519,552 bytes: 48 layers by 123 window rows by 88 bytes.
tools/make_profile.py:read_tracereads it as is, and walking the file gives 5,904 records, all with layer in 0 to 47, k equal to 10 and ids in 0 to 511, no malformed record and no trailing bytes, with weights from 0.0084 to 0.8066.The cost is small but I cannot pin it down: this pair says 0.4% (60.97 against 61.21), an earlier pair on the same build said 2.9% (61.31 against 63.11), and pure decode on this machine moves about 7% from boot to boot. One extra dispatch per layer per window is the whole overhead, so it should stay under a few percent.
What it bought
U(P) over 48 layers of a run like that, greedy, one prompt, counted as the distinct experts in the pass divided by the distinct experts of one token in it:
Worth one caveat about the rows: they are the window's proposals, so a draft that was rejected still shows the experts it routed. For a cache profile that is the useful view; for accepted token accounting you would filter them.
The machine and the unmodified binary numbers are in the report PR #2649. Related but different: #1547 puts routing counts on
/metricsin the server, which is not what reads a per window trace, and nothing open covers the empty trace on Metal. If you would rather havecollect_profile_traces.pyfail on a 0 record trace than accept it, that is a one line change I can add here or in an issue.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.