Skip to content

perf: retile router fp32 kernels for D=3072 (#44) - #52

Merged
feitreim merged 2 commits into
mainfrom
perf/issue-44-router-kernels
Jul 23, 2026
Merged

feitreim merged 2 commits into
mainfrom
perf/issue-44-router-kernels

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #44.

The three fp32 router matmuls ran ~30× off bandwidth-bound at D=3072
because their launch geometry, not precision, was wrong. The router
stays fp32 end-to-end (SPEC 8b) and routing decisions are unchanged.

What changed

  • forward.router.logits — replaced the eight-threads-per-token launch
    (each striding the full D row) with a register-tiled skinny fp32 GEMM
    (router_gemm_impl). A [BM=32, BN=8] output tile loads the router weight
    once per BK step and reuses it across the token tile, so the weight
    streams from L2 instead of being re-read per token; BN=8 matches the
    routed expert width, so the skinny dimension carries no tile padding.
  • backward.router.input (dx is write-bound) — one block per token
    row, staging the 32-byte gate row once in shared and broadcasting it while
    lanes stride D with coalesced dx writes, instead of a per-(token,d)
    serial dot that re-read the gate row from HBM. (Routing the skinny GEMM
    here instead regressed it by breaking the wide coalesced write.)
  • backward.router.weight — kept its deterministic, atomic-free
    fixed-order reduction but re-sized the D=1536-era 2×8-tile/16-partition
    geometry to 8 coalesced D-rows × 8 token partitions per block for
    D=3072.

Before / after (B200, §10.1, L=12 B=12 T=2048 D=3072 E=8, same-container A/B vs main)

span main this branch speedup
forward.router.logits 18.66 ms 3.96 ms 4.71×
backward.router.weight 17.48 ms 7.96 ms 2.20×
backward.router.input 7.63 ms 6.59 ms 1.16×
three combined 43.78 ms 18.52 ms 2.36×
full step 748.04 ms 719.89 ms −3.8%

Parity (routing decisions unchanged)

./run.sh ops:

  • ✓ ops forward/backward parity checks passed

./run.sh model:

  • ✓ fp32-oracle substituted MoE model matches CPU with drops and underfull experts
  • ✓ aligned tcgen05 substituted MoE model matches CPU with drops and underfull experts
  • ✓ aligned tcgen05 MoE overfits with scheduled aux loss (2.944996 -> 0.030016)
  • ✓ full GPU Dense forward/backward and AdamW (bf16 lm-head) match CPU
  • ✓ checkpoint v4 resumes bit-identically and rejects E/K/C/L or schedule mismatches

Caveats / follow-ups

  • backward.router.input and backward.router.weight remain
    L2-weight-bandwidth bound (still ~15–20× off the pure-HBM floor). A full
    skinny-GEMM weight-reuse pass for both — without the per-block staging
    overhead that hurt the tiled input variant here — is the next lever if
    these spans stay on the profile hot list.

feitreim added 2 commits July 23, 2026 17:04
The three fp32 router matmuls ran ~30x off bandwidth-bound at D=3072
because their launch geometry, not precision, was wrong (issue #44):

- forward.router.logits launched eight threads per token, each striding
  the full D row; both occupancy and coalescing were wrecked.
- backward.router.input ran one thread per (token, d), re-reading the
  E-wide dlogits row from HBM per output element.
- backward.router.weight kept its D=1536-era 2x8-tile/16-partition
  geometry, which under-coalesces X and under-fills the SMs at D=3072.

forward.router.logits now uses a register-tiled fp32 skinny GEMM
(router_gemm_impl): a [BM=32, BN=8] output tile loads the router weight
once per BK step and reuses it across the token tile, so the weight
streams from L2 instead of being re-read per token; BN=8 matches the
routed expert width, so the skinny dimension carries no tile padding.

backward.router.input (dx-write-bound) is one block per token row: the
32-byte gate row is staged once in shared and broadcast, and the lanes
stride D with coalesced dx writes instead of re-reading the gate row per
output (routing the skinny GEMM here regressed it by breaking the wide
coalesced dx write).

backward.router.weight keeps its deterministic fixed-order reduction but
is re-sized to 8 coalesced D-rows x 8 token partitions per block.

The router stays fp32 end-to-end; routing decisions are unchanged (ops
and model parity gates green).
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.

Router fp32 kernels run ~30x off bandwidth-bound at D=3072

1 participant