roofline: capture per-launch MoE active-expert count (experts_used)#50
Open
roberteg16 wants to merge 1 commit into
Open
roofline: capture per-launch MoE active-expert count (experts_used)#50roberteg16 wants to merge 1 commit into
roberteg16 wants to merge 1 commit into
Conversation
mgehre-amd
approved these changes
Jul 15, 2026
MUL_MAT_ID reads only the experts a launch actually routes, but the byte model had no way to know that count and the consumer fell back to the shape bound min(M*top_k, E) -- which saturates to all experts and over-counts HBM traffic when routing leaves experts idle, pushing memory-bound MoE kernels past 100% of peak (the working set dwarfs L2, so it is not cache reuse). Read the routing ids tensor back per invocation and emit the distinct active-expert count as experts_used. begin_op takes the op's stream and hipStreamSynchronize's it so the ids are final before the copy; the read-back is wrapped in a sentinel correlation id (0) so any copy kernel it lowers to is attributed to no op and skipped in the total. Device-timestamp durations are unaffected, so the report's figures are unchanged apart from the new field.
1847907 to
5b06a86
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MoE (
MUL_MAT_ID) reads only the experts a launch actually routes, but the roofline had no measured count of them, so the consumer fell back to the shape boundmin(M·top_k, E). That bound saturates to all experts and over-counts HBM traffic whenever routing leaves experts idle, making memory-bound MoE kernels read >100% of peak — physically impossible, and not cache reuse (the expert weights dwarf L2/L3). It was simply a byte over-count.This PR emits the missing fact: the true number of distinct experts each launch routes.
idstensor back per invocation and emit it asexperts_usedin the JSON. It is data-dependent — it varies launch to launch — so it is captured per launch, not per shape.begin_opnow takes the op's stream (asvoid *, to keep the header free of the HIP runtime) andhipStreamSynchronizes it so the ids are final before the copy.0) so any copy kernel it might lower to is attributed to no op and excluded fromtotal_gpu_time_us. In practice the D2H copy goes through SDMA (aMEMORY_COPYrecord we don't subscribe to) and emits no dispatch at all — verified below.Notes
rogarcia.roofline-fused-raw-facts(raw-facts producer), which is the PR base.experts_usedto scale MoE weight bytes, plus mean+range aggregation and a cache-aware warning): FaaSApps/rocm-scripts#715.Test plan
-DGGML_HIP_ROOFLINE=ON(gfx1151): both the CXXggml-cuda-roofline.cppand the HIPggml-cuda.cucompile and link.pp128): everyMUL_MAT_IDrow carriesexperts_usedin[1, n_experts]; measured routing was mean 114, range 77–179 of 256 vs. the bound's flat 256. Decode (M=1) correctly givesexperts_used = 8 = min(8, 256)— the bound was already tight there, so only prefill/batched launches change.copyBuffer/fillBuffer/rocclrkernels in the trace andtotal_gpu_time_usunaffected — the sentinel-id guard was belt-and-suspenders; the SDMA copy left no dispatch.Example output (rendered by the consumer, rocm-scripts#715)
Effect of
experts_usedon themul_mat_q<q4_K,16>MoE expert matmul. Both columns are the same trace (identical421.17 µs); "before" is the shape bound (noexperts_used), "after" is the measured count.Before (no
experts_used→ all 256 experts charged):After (measured
experts_used):The impossible 348.6 GiB/s (peak 230 GiB/s) collapses to a real 66.9% once only the ~91 experts actually routed are charged.