Repository navigation
backends: measured T = 1 decode GEMV choices for the 40-core M5 Max - #95
Merged
Merged
Conversation
A row split (RSPLIT) on an op that writes a norm statistic gives every work item its own partial, so the next norm sums other partials and its scale can move by an ULP. With STAT_TG the block's RSPLIT items are the SIMD-groups of one threadgroup (grid mode tgsplit) and, after a barrier, the block's partial is chained over its rows in order: the outputs and the statistic are the unsplit op's byte for byte, and the narrow residual projections fill the GPU (M5 Max, int4 5120x17408 at T = 1: 142 -> 100 us against the fastest unsplit geometry). The emitter takes tgsplit for a single-variant shader op without a tile and keeps n_blocks partials. It tells the autotuner which ops write a statistic: every variant of such an op is timed writing it, in its own cache entry (|stat), and tgsplit is a candidate only there (not with silu_mul's paired rows). A table's lookup gives a producer its own entry where the table has one, else the shape's, so the tables written before keep their keys. The tile-versus-shader timing keeps both sides without the statistic (the tile's tuner writes none).
A tuned GEMV geometry is timed where the shader op always runs. In a prompt or verification program the variants below the tile run only for a chunk or a verification of a row or two: a per-block launch from that table (one threadgroup per block: 7760 for the 248K-row vocabulary projection) was dispatched for nothing whenever the tile took the step, and a tuned RG was timed only with its own launch. Those variants keep the default RG and the crew launch, and the choices' norm path: the geometry moves no output bit.
…ut bit The 40-core M5 Max shipped no kernel choices, so plain decode compiled every shader GEMV with the default crew geometry: the 5120-row projections (down, GDN out, attention o) leave a third of the crew idle and the wide ones stop short of the bandwidth the NVFP4 row kernel reaches. The table holds the autotuner's fastest variant per shape for the FP8 projections of the NVFP4 checkpoint and the int4 matrices of the MLX 4-bit one, limited to geometries that keep every output bit: no fused norm (it folds the statistic's partials in another order than norm_apply), and a statistic producer splits its rows only inside one threadgroup (tgsplit). The 4-bit gate|up split is left out: in the program it is slower than the default. Decode step, plain decode at short context: 4-bit 39.6 -> 32.4 ms, NVFP4 37.9 -> 35.4 ms, generated tokens unchanged.
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.
servecompiles with the measured kernel choices of the chip's configuration (#34), and the 40-core M5 Max shippednone. Plain decode therefore ran every T = 1 GEMV at the default crew geometry. This PR ships the M5 Max's choices,
limited to geometries that keep every output bit, plus the row split the residual projections need for that.
tgsplit(
STAT_TG), a block'sRSPLITwork items are the SIMD-groups of one threadgroup. After a barrier, the block'sstatistic is accumulated over its rows in order, so the outputs and the statistic equal the unsplit op's, byte for
byte. On int4 5120x17408 at T = 1, this takes 142 µs down to 100 µs, compared with the fastest unsplit geometry.
it, and offers
tgsplitonly for those ops.GEMV variants below the tile run only for a chunk's remainder or for one or two verification rows.
vocabulary projection.
RGhad been timed only together with its own launch.RGand the crew launch, plus the choices' norm path, so their output bitsdo not change.
FP8 projections of the NVFP4 checkpoint and the int4 matrices of the MLX 4-bit one.
Serve on the 40-core M5 Max with Qwen3.8-27B, greedy. Runs were paired as main, this PR, this PR, main. Each value is the mean decode rate over three prompts:
Time to first token at 1K, 4K and 9K tokens moves by −0.2 % to +0.7 %. The largest change is NVFP4 without a draft at 1K tokens: 1.885 s to 1.898 s. Every run's texts are identical to main's.
Other chips:
now take the default
RGand launch. Outputs are unchanged bit for bit, but the speed is not measured on thosechips.
core, while this profile dispatches two (Autotune: crew variants are timed at one threadgroup per core but run at the profile's threadgroups_per_core #59). The speeds above come from the dispatched programs. Once Autotune: crew variants are timed at one threadgroup per core but run at the profile's threadgroups_per_core #59 is
fixed, re-timing may choose different crews.
Testing
pytest tests/contracton the M5 Max: 1254 passed and 1 skipped. The two failures intest_streaming_moe_oracle(transformers' MoE rotary embedding no longer takesdevice) fail on main too.test_m5_max_choices_keep_every_output_bitchecks the shipped table.test_threadgroup_row_split_keeps_the_statisticcheckstgsplitemission, and that the tuner is told which opswrite a statistic.
test_shader_variants_beside_a_tile_keep_the_default_geometrychecks the emit rule.MTL_SHADER_VALIDATION=1 MTL_SHADER_VALIDATION_REPORT_TO_STDERR=1 pytest -s tests/kernels/test_gemv_fusions.pyon the M5 Max:66 passed. The log shows "Metal GPU Validation Enabled" and "Metal API Validation Enabled" and no report. The run
is uncaptured (
-s) because pytest's capture hides a passing test's stderr (CI: shader validation reports from passing tests never fail the GPU tiers #61).test_threadgroup_row_split_keeps_the_statisticchecks, for NVFP4, FP8 and int4, thatyand the statisticequal the unsplit op's byte for byte.
test_autotuner_times_a_statistic_producer_with_its_statisticchecks the tuner's candidates.Closes #94.