Skip to content

FA4 phase 2: warp-specialized pipelined tcgen05 forward (#35) - #37

Merged
feitreim merged 3 commits into
mainfrom
feat/issue-35-fa4-fwd-phase2
Jul 19, 2026
Merged

feitreim merged 3 commits into
mainfrom
feat/issue-35-fa4-fwd-phase2

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Phase 2 of #35: the warp-specialized pipelined forward — K/V TMA rings, S TMEM double-buffering, and mbarrier-only role handoffs, landed behind the same gates as phase 1. The phase-1 synchronous kernel stays in the artifact as an oracle and hang-debugging fallback; the model still runs the fp32 tiled kernels (integration is phase 3).

What's here

flash_forward_pipelined — 192 threads in three roles, connected purely by mbarrier phase-parity spins (no named barriers in cuda-oxide v0.2.1; FA4 does the same):

  • Load warp (warp 4): TMAs Q once, then streams K/V through a PIPELINE_STAGES-deep ring (kv_full[stage] expect_tx, recycling on kv_free[stage]).
  • MMA warp (warp 5): issues tcgen05 with a staggered order — S-MMA(i) into the double-buffered S TMEM (columns 0/128, guarded by s_free) before O-MMA(i-1) (guarded by p_full) — so while the softmax warpgroup works tile i, the tensor core is already producing S(i+1). This is the checklist's softmax(i) ∥ MMA(i+1) overlap, and it is also why PIPELINE_STAGES >= 2 is a const assert: with one stage the kv_full wait for S-MMA(i) would precede the O-MMA(i-1) whose completion recycles the stage.
  • Softmax warpgroup (warps 0–3): waits s_full, runs the register softmax, releases s_free/p_full, waits o_full, merges the O segment. Correction and epilogue stay fused here rather than in FA4's separate warpgroups: the output accumulator lives in these registers and TMEM lane ownership pins every drain to warps 0–3 — a standalone correction warpgroup only becomes possible with tcgen05.st (Bump cuda-oxide pin from v0.2.1 to current upstream main #34).

Two safety-driven choices worth reviewing:

  • P stays single-buffered with no extra barrier: the warpgroup's own o_full(i) wait (needed anyway before draining O) proves O-MMA(i) finished reading P before pass 2 of tile i+1 overwrites it.
  • kv_free is released by the warpgroup after that same wait, not by a second tcgen05.commit from the MMA warp — the PTX "commits all prior operations" wording is ambiguous about already-committed MMAs, and a commit that arrived immediately would let TMA overwrite a V tile mid-read.

The kernel doc comment records the phase-parity soundness argument (every barrier's completions lead their waiter by at most one phase) since that invariant is what a future reordering could silently break.

Shared per-tile math — softmax_tile / merge_output_tile / store_outputs are extracted #[inline(always)] helpers used by both kernels, so the sync kernel gates also gate the shared code, and phase 3's conditional-rescale work edits one place.

SWEEP + launch bounds — PIPELINE_STAGES is the tuning const (pub const ... usize per SPEC §10). The host side allocates a fixed SMEM ceiling sized for 4 stages (176 KiB), so sweeping stages is a one-const edit that can never desync host from device; the flash bin asserts the ceiling. Over-allocating is free here: the 512-column TMEM alloc against a 512-column SM budget already pins occupancy to one CTA/SM. The kernel carries #[launch_bounds(192, 1)] (the issue's __launch_bounds_config item — the macro lowers to exactly that intrinsic). BK and the role ratios from the issue's sweep list are structurally pinned (TMA box + MMA shapes; TMEM lane ownership) and recorded as such rather than swept.

run_sweep in modal_app.py learned to rebuild flash.ptx per config and use the flash bin as gate+bench for this crate.

Gates (B200, same container)

  • exp2 7.5e-5 rel / log2 1.9e-7 abs; swizzle + transpose_b probes unchanged, pass
  • both forwards vs staged-bf16 CPU reference (T=128/256/512) and vs both fp32 oracles (T=128/256/1024): pipelined max errors are bit-identical to sync at every shape (y ≤ 2.4e-3, LSE ≤ 8.9e-4 vs oracles) — the schedule changed, not one arithmetic result, which is the strongest cheap evidence the pipeline is race-free
  • per-row + fp32 tiled oracle chain and 200-round bit-stability: unchanged, pass
  • compute-sanitizer synccheck: attempted, but the image's sanitizer rejects the B200 ("Device not supported"). The run did flush out that run_sanitizer's build path lacked the --arch sm_100a pin (fixed here); rerun once the image's CUDA toolkit supports sm_100 sanitizing
  • bench at the profile shape (B=32, T=1024, H=24), same container: sync 1.574 ms / 73.7 TFLOP/s → pipelined 1.388 ms / 83.6 TFLOP/s (+12%)
  • PIPELINE_STAGES sweep (one container, parity green in every config): 2 → 1.383 ms, 3 → 1.385 ms, 4 → 1.386 ms — flat within noise, confirming TMA is fully hidden at every depth and the warpgroup is the critical path. Default stays 3 (headroom for phase 3's second workstream)

The modest headline delta is the expected shape of phase 2: the softmax warpgroup is the critical path, and this phase only strips TMA waits, S-MMA, and part of O-MMA out of it while building the substrate phase 3 needs — two-Q-tile ping-pong (fills the tensor-core idle window during softmax) and conditional rescaling (removes most per-tile O drains from the warpgroup).

Phase-2 checklist from #35

  • K/V TMA rings (2–3 stages) with mbarrier ring plumbing
  • S TMEM double-buffer; softmax(i) ∥ MMA(i+1) overlap
  • warp specialization (load / MMA / softmax / correction+epilogue roles — correction+epilogue fused into the softmax warpgroup, see above)
  • __launch_bounds_config register budgeting; SWEEP stages (BK × role ratios structurally pinned, recorded)

Next per the issue: phase 3 (conditional rescaling segments, full-tile/diagonal loop split, two-Q-tile ping-pong, persistent scheduler, model integration).

feitreim added 3 commits July 18, 2026 22:36
- flash_forward_pipelined: 192 threads — TMA load warp with a
  PIPELINE_STAGES-deep K/V mbarrier ring, MMA-issue warp staggered so
  S-MMA(i+1) overlaps softmax(i) via a double-buffered S TMEM, and the
  128-thread softmax warpgroup keeping correction+epilogue (register
  O accumulator + TMEM lane ownership pin them there)
- per-tile softmax/drain/epilogue extracted into helpers shared with the
  phase-1 sync kernel, which stays as the in-artifact oracle
- PIPELINE_STAGES is the SWEEP knob; the host allocates a 4-stage SMEM
  ceiling so stage sweeps are a one-const edit (free at 1 CTA/SM, which
  the 512-column TMEM alloc already forces)
- launch_bounds(192, 1); bin gates + bench and harness oracle parity now
  cover both forwards
@feitreim
feitreim merged commit eb9fdff into main Jul 19, 2026
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