Skip to content

backends: measured T = 1 decode GEMV choices for the 40-core M5 Max - #95

Merged
yzhou442 merged 3 commits into
lithos-ai:mainfrom
yzhou442:pr/m5max-t1-choices
Oct 21, 2026
Merged

yzhou442 merged 3 commits into
lithos-ai:mainfrom
yzhou442:pr/m5max-t1-choices

Conversation

@yzhou442

@yzhou442 yzhou442 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

serve compiles with the measured kernel choices of the chip's configuration (#34), and the 40-core M5 Max shipped
none. 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.

  • gemv_T: split a statistic producer's rows inside one threadgroup. In the new grid mode tgsplit
    (STAT_TG), a block's RSPLIT work items are the SIMD-groups of one threadgroup. After a barrier, the block's
    statistic 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.
    • The emitter tells the autotuner which ops write a statistic. The tuner times every variant of such an op writing
      it, and offers tgsplit only for those ops.
  • emit: a shader variant beside the tile keeps the default geometry. In prompt and verification programs, the
    GEMV variants below the tile run only for a chunk's remainder or for one or two verification rows.
    • A tuned per-block launch was dispatched for nothing whenever the tile took the step: 7760 threadgroups for the
      vocabulary projection.
    • A tuned RG had been timed only together with its own launch.
    • These variants now keep the default RG and the crew launch, plus the choices' norm path, so their output bits
      do not change.
  • m5_max_40c: measured T = 1 decode GEMV geometry that keeps every output bit. The table lists 12 entries: the
    FP8 projections of the NVFP4 checkpoint and the int4 matrices of the MLX 4-bit one.
    • Excluded: fused norms, and cross-threadgroup row splits on ops that write a statistic.
    • Also left out: the 4-bit gate|up split, which is slower than the default inside the program.

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:

main this PR
plain decode, MLX 4-bit 22.91 tok/s 28.07 (+22.5 %)
plain decode, NVFP4 26.24 tok/s 28.03 (+6.8 %)
DSpark decode, MLX 4-bit 56.41 tok/s 56.38 (−0.1 %)
DSpark decode, NVFP4 76.45 tok/s 76.56 (+0.1 %)

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:

Testing

  • pytest tests/contract on the M5 Max: 1254 passed and 1 skipped. The two failures in test_streaming_moe_oracle (transformers' MoE rotary embedding no longer takes device) fail on main too.
    • test_m5_max_choices_keep_every_output_bit checks the shipped table.
    • test_threadgroup_row_split_keeps_the_statistic checks tgsplit emission, and that the tuner is told which ops
      write a statistic.
    • test_shader_variants_beside_a_tile_keep_the_default_geometry checks the emit rule.
  • MTL_SHADER_VALIDATION=1 MTL_SHADER_VALIDATION_REPORT_TO_STDERR=1 pytest -s tests/kernels/test_gemv_fusions.py on 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_statistic checks, for NVFP4, FP8 and int4, that y and the statistic
      equal the unsplit op's byte for byte.
    • test_autotuner_times_a_statistic_producer_with_its_statistic checks the tuner's candidates.

Closes #94.

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.
@yzhou442
yzhou442 merged commit 6653b7d into lithos-ai:main Oct 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backends: the 40-core M5 Max ships no kernel choices, so plain decode runs the default GEMV geometry

1 participant