Skip to content

moe: dedup transposed-table expert reads across MTP verify rows (FP4-Local decode, byte-identical) - #219

Merged
AzeezIsh merged 7 commits into
mainfrom
perf/fp4l-moe-verify-dedup
Oct 28, 2026
Merged

AzeezIsh merged 7 commits into
mainfrom
perf/fp4l-moe-verify-dedup

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Summary

On FP4-Local (nvidia/Qwen3.8-Flash-Next-FP4-Local) the MTP verify rows (draft depth 3 → 4 rows) each ran forward_row_local on the transposed-table MoE arm. So every row re-streamed the experts it shared with the other rows. Measured on 2026-10-19: the 4 rows' 40 routed slots hit a mean of 26.1 unique experts (p10 19, p90 33), so ~35% of routed-expert reads were duplicates.

This adds a dedup arm to MoeLayer::forward_rows:

  • moe_expert_union_t maps the rows' rows × top_k ids onto the union of experts (no host sync).
  • moe_expert_{gate_up,silu_down}_shared_t_rows_v3_r{2..8}_b256 stage each unique expert's tile once (smem double buffer, 16-byte loads, 256 columns per CTA) and apply it to every row that selected it. Each row's accumulator is a register, computed unconditionally with the row mask applied only at the store. Each (row, slot) output uses the single-row _t kernel's per-element expression and k-order, so it is byte-identical to running each row alone.
  • forward_rows_t.rs: the per-row router GEMV + top-k and the blend are the same launches as forward_row_local; only the routed + fused shared expert GEMVs are deduplicated. It reads only the *_ptrs_t tables; the row-major originals are freed on FP4-Local.
  • Eligibility mirrors the transposed-table arm's own conditions (plain NVFP4 _t kernels, no LoRA / router hooks, rows 2..8). Kill switch: ATLAS_QWEN4EXP_BATCH_FAST_BISECT bit BISECT_MOE_T_ROWS (no new env var). The kernels are gb10-only, so the lookups are try_kernel_gated(!cfg!(atlas_hip), …). Default-on when eligible, since it's an exact speedup.

Evidence (sparky GB10)

Microbench (examples/moe_t_rows_bench.rs, cold: 48 rotating layer tables, routing with an exact union target; ratio = rows kernel ÷ rows × single-row launches): rows=4 at union 26/40 slots 0.764, rows=2 0.908, rows=3 0.791 / 0.814. Every case byte-identical. It took a few rounds (campaign log #211, 10-27): the first version was 1.5–1.76× slower because of a runtime-indexed accumulator demoted to local memory.

In situ, on this exact branch (87b28ea vs the base binary c3bf9aa = main da7985c's tree; default FP4-Local recipe profile r1short, MTP 3, ATLAS_QWEN4EXP_BATCH_FAST=1; sparky job 30b-s7-gate-v3b256):

  • 4-request T=0 control: byte-identical to the base.
  • ATLAS_QWEN4EXP_EXACT_VERIFY_CHECK=1: 0 mismatches, with the dedup arm engaged (its one-time engage log fired).
  • NVFP4 pack (nvidia/Qwen3.8-Flash-Next-NVFP4) control: byte-identical.
  • Decode speed, 3 alternating fresh-boot pairs, ab_stream.py with fixed prompts; completion lengths are identical in both arms:
prompt (tokens out) base tok/s (3 runs) this PR tok/s (3 runs) Δ
short A (382) 26.46 / 26.46 / 26.43 27.65 / 27.68 / 27.67 +4.6%
short B (178) 26.00 / 25.98 / 25.91 27.20 / 27.19 / 27.20 +4.7%
short C (253) 25.53 / 25.50 / 25.50 26.55 / 26.51 / 26.53 +4.0%
agent ~17K-token prompt, run 0 (512) 23.02 / 23.00 / 23.04 23.98 / 24.02 / 23.93 +4.2%
agent run 1 (512) 24.42 / 24.36 / 24.34 25.43 / 25.38 / 25.39 +4.2%
agent run 2 (512) 23.43 / 23.41 / 23.40 24.47 / 24.42 / 24.46 +4.5%

Medians over 9 runs each: short 25.98 → 27.20 tok/s, agent 23.41 → 24.46 tok/s, about +4.5% decode. Within-arm spread is ≤ 0.1 tok/s. The in-situ gain is smaller than the microbench ratio implies: routed gate_up + silu_down was ~13 of ~30 ms per token in the 10-19 profile, and only part of it is verify rows.

Before merging

Per the verify-before-merge policy, a GB10 gate of this PR merged onto current main (control, agentic-webserver 50, BFCL ST-995) is queued on sparky. Byte-identical outputs mean BFCL should match main exactly. Results will be posted here and in #211.

Test plan

  • fmt (pinned + stable), clippy, file-size cap, SPDX
  • Eligibility truth-table unit test (forward_rows_t_tests.rs)
  • Microbench: byte-identity + ratio gate
  • In situ: control identical, EXACT_VERIFY_CHECK 0 mismatches (arm engaged), NVFP4 control identical, 3 speed pairs
  • GB10 gate on the merge with main (agentic 50 + BFCL ST-995)

Generated with Devin

AzeezIsh and others added 2 commits October 27, 2026 14:30
moe_expert_union_t computes the unique-expert union of ROWS verify rows;
the _v3_*_b256 rows kernels stage each unique expert's tile once (smem
double-buffer) plus the per-row activation slice, accumulating
acc[COLS=1][ROWS] with the single-row _t kernel's exact expression/order.
gb10-only symbols.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Third arm in MoeLayer::forward_rows between rows_pair and per-row:
moe_expert_union_t -> gate_up/silu_down rows kernels over *_ptrs_t only.
Eligible 2..=8 rows on the published transposed layout, NVFP4 scales, no
LoRA/pre-norm/hash/bias-router, handles resolved, kill switch
BISECT_MOE_T_ROWS. Logs once on first engage. Includes the eligibility
truth-table test.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
AzeezIsh added a commit that referenced this pull request Oct 27, 2026
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Merge gate progress (sparky GB10, refs/pull/219/merge = 949fa6b = this PR merged onto main b7af7fa; default FP4-Local recipe profile r1short):

  • 4-request T=0 control: byte-identical to main (train 1/2 control).
  • agentic-webserver 50: 50/50, 50/50, 11.99 s/turn, harness decode 22.92 tok/s (run-1793162479652381763, 378 turns). Main at b7af7fa's tree (train 2): 12.43 s/turn, 22.05 tok/s (run-1793128886445013968). That's one run per arm; the decode delta (+4.0%) matches the +4.5% fixed-prompt in-situ A/B above.
  • BFCL ST-995: running (~3 h). Byte-identical outputs should reproduce 87.44 / 87.32 exactly.

AzeezIsh added a commit that referenced this pull request Oct 28, 2026
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Correction to the in-situ evidence above. The EXACT_VERIFY_CHECK run in job 30b (160 tokens, ~55 verify steps) printed no summary line, because summaries print every SUMMARY_EVERY steps. It also had zero per-row differ warnings. That's consistent with exactness, but it isn't a positive proof that steps were compared. Queued on sparky as job 37-s7-exactness: single sequence at 800 tokens with EXACT_VERIFY_CHECK; 4 and 8 concurrent sequences with BATCH_FAST_CHECK + EXACT_VERIFY_CHECK (multi-sequence batched rows also reach forward_rows); C=4/C=8 throughput vs main. Pass requires ≥1 summary line, every summary at 0 rows differ, no per-row warnings, and the arm engaged. This PR merges only after that and the BFCL leg of the merge gate pass.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Merge gate (job 35) PASS: refs/pull/219/merge 949fa6b (binary sha256 d68ef5d4…), default FP4-Local recipe profile:

leg #219 on main main (train 2)
control byte-identical —
agentic-webserver 50 50/50, 50/50, 11.99 s/turn, decode 22.92 tok/s (run-1793162479652381763) 12.43 s/turn, 22.05 tok/s
BFCL ST-995 87.44 / 87.32 (run-1793172763024546566), all 15 subsets identical 87.44 / 87.32
TTFT p50 / p90 / Σ (380 req) 637 / 3,000 ms / 425.1 s 637 / 2,996 ms / 425.1 s
serve log 0 ERROR / 0 panic; dedup arm engaged —

TTFT is unchanged (S7 only touches decode verify rows), and the agentic speedup comes from decode (+3.9% harness decode, consistent with the +4.5% fixed-prompt A/B). Last item before merging: the exactness re-check (job 37), running now.

Pure move: main.rs (harness), tables.rs (layer tables, RNG, constants,
union routing), cases.rs (memory guard + RESULT printer). Fixes three
pre-existing clippy lints now that the example is linted.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Exactness re-check (job 37) PASS, now with summary lines proving the steps were compared:

  • single sequence, r1short, EXACT_VERIFY_CHECK: 128 verify steps / 512 rows, 0 rows differ, 0 argmax flips;
  • 4 concurrent sequences, r2, BATCH_FAST_CHECK + EXACT_VERIFY_CHECK: 192 steps / 1,468 rows, 0 differ, 0 flips;
  • 8 concurrent sequences: 192 steps / 2,944 rows, 0 differ, 0 flips.
    The dedup arm engaged in every run; zero per-row differ warnings.

Pushed 3dacfd1: the bench example is split into examples/moe_t_rows_bench/{main,tables,cases}.rs for the 500-LoC cap (pure move + 3 clippy lints that surfaced once the example is linted). Only crates/spark-model/examples/ changes, so the gated serve binary is unaffected. Merging once CI's file-size cap is green.

Pure move into init_t_rows.rs, same shape as init_optional: TRowsKernels::resolve
returns the union + per-ROWS rows handle arrays; init.rs consumes the fields.
Same kernel names, same !atlas_hip gating, same values.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
AzeezIsh added a commit that referenced this pull request Oct 28, 2026
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
AzeezIsh and others added 3 commits October 28, 2026 04:03
…e cap; main red since #197)

Pure move into init_batched.rs (BatchedKernels::resolve), same shape as
init_optional/init_t_rows: batch2/batch3/batch5 fused verify, token-major
prefill trio, atomic-c4 pair. Same kernel names, gating and field values.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
The clean branch ships _v3_r{2..8}_b256 only; rows=1 crashed on a missing
symbol and MOE_ROWS_ONLY used the old v0 names. Bench now runs rows 2..=4
and MOE_ROWS_ONLY resolves the shipped names.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Two more pushes, neither touches the serve binary:

  • 62554db: merged main (b7af7fa), and moved the batched/token-major/atomic kernel lookups out of moe/init.rs into init_batched.rs (pure move; the glm5_next gating is passed through unchanged). init.rs is 510 → 491 on the merged tree, which also clears the file-size-cap red main has carried since strix-hip train: the validated gfx1151 stack (build fix + suballoc + carveout + hipBLASLt) #197. Smoke on this head (sparky job 38c): control byte-identical, dedup arm engaged, 0 errors.
  • 139efbd: the bench example ran a rows=1 case whose _r1_b256 kernel this PR doesn't ship, so it crashed on lookup. It now runs rows 2..4 with the shipped names.

Correction: the job-30b "clean-branch microbench" guard printed PASS on that crashed run (0 RESULT lines parsed), so it was vacuous. The merge evidence here doesn't depend on it: the in-situ control, the summary-line exactness checks (job 37) and BFCL (job 35) all ran on the real binary. The first real run of the slim bench is queued as sparky job 40, and every verdict now fails on 0 parsed lines. Merging once CI's nvcc check is green.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

All gates passed: merging.

gate result
merge gate (job 35, #219 on main) control identical; agentic 50/50, 50/50, 11.99 s/turn (main 12.43); BFCL ST-995 87.44 / 87.32, all 15 subsets identical to main; 0 errors
exactness (job 37) EXACT_VERIFY_CHECK single sequence 512 rows, BATCH_FAST_CHECK C=4 1,468 rows, C=8 2,944 rows: 0 rows differ, 0 argmax flips; arm engaged
smoke after the init.rs split (job 38c, 62554db) control identical, engaged, 0 errors
microbench (job 40, first real run) rows=4 union 26/40: 0.771× of 4 single-row launches, all cases identical
CI at 139efbd all green except the repo-wide PR benchmark gate; file-size cap green (also clears main's red from #197)

Follow-up #228 (stacked) fixes this PR's silu_down rows kernel: another +25% short / +10% agent decode, byte-identical, gating now.

@AzeezIsh
AzeezIsh merged commit db442e2 into main Oct 28, 2026
25 of 26 checks passed
AzeezIsh added a commit that referenced this pull request Oct 28, 2026
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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