Skip to content

[perf](kt-kernel): AVX2 MXFP4 decodes each weight group once for the last 2-3 rows of an expert - #2253

Open
05yuki wants to merge 1 commit into
kvcache-ai:mainfrom
05yuki:perf/avx2-mxfp4-small-m
Open

05yuki wants to merge 1 commit into
kvcache-ai:mainfrom
05yuki:perf/avx2-mxfp4-small-m

Conversation

@05yuki

@05yuki 05yuki commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

The AVX2 MXFP4 group-32 fast path runs an expert's rows in 4-row blocks and the rest one at a time. Each of those remaining rows decodes every 32-value weight group again (PSHUFB, unpack), which is what bounds this kernel, so 2 rows on an expert cost 1.5x one row and 3 rows 2.1x. Small decode batches, speculative verify steps and the few rows a short prefill puts on each expert land exactly there.

  • operators/avx2/mxfp4-moe.hpp: when 2 or 3 rows remain after the 4-row blocks (m = 2, 3, 6, 7), each group is decoded once and applied to all of them. Each row keeps the single-row loop's own arithmetic (one product chain a group, the scale folded into two sums by group parity, groups in order), so every output is bit-identical to before. KT_MXFP4_REM_ROWS=0 restores the row-at-a-time loop.
  • m = 1 and multiples of 4 do not change.

Measured on one socket of a dual EPYC 7452 (Zen 2, 32 threads), DeepSeek-V4.1-Flash's TP2 rank shapes (hidden 5120, intermediate 1152, 384 experts) with synthetic weights, 4 experts a call, every row on the same 4 experts, median of 2 x 200 calls (kt-mxfp4-decode-bw.py --same):

rows on an expert before this PR
1 516 us 522 us
2 778 us 633 us (-19%)
3 1084 us 754 us (-30%)
4 903 us 906 us
6 1533 us 1372 us (-10%)
7 1839 us 1532 us (-17%)
8 1702 us 1705 us

Outputs: the hash over 64 calls is the same before and after at every row count above, and each row of the 2- and 3-row calls is byte-identical to that row computed alone.

🤖 Generated with Claude Code

https://claude.ai/code/session_01C2UBZdnrDZSBh8yQz8rc9g

…last 2-3 rows of an expert

The group-32 fast path runs an expert's rows in 4-row blocks and the rest one at a time,
decoding every 32-value weight group again for each of those rows. With 2, 3, 6 or 7 rows
on an expert (a small decode batch, a speculative verify, the few rows of a short prefill)
the remaining 2-3 rows now share one decode a group. Each row keeps the single-row loop's
arithmetic, so every output is bit-identical to before. KT_MXFP4_REM_ROWS=0 restores the
row-at-a-time loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C2UBZdnrDZSBh8yQz8rc9g

This branch has not been deployed

No deployments
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.

1 participant