Repository navigation
fix: fence combine buffer reuse in mega moe - #389
Bruce-Lee-LY wants to merge 1 commit into
Conversation
🤖 ds-review-bot Code Reviewv6The added proxy fence correctly orders shared-memory reads before the TMA load stage is reused in both Mega MoE implementations. No breaking or correctness issues were identified. v4The MR fixes a shared-memory reuse ordering (WAR) hazard in the final combine stage of the sm100 bf16/fp8-fp4 Mega MoE kernels, which are the only two kernels using this two-stage shared ping-pong combine buffer. The buffer is filled by TMA via the async proxy, consumed through per-lane generic shared reads after a per-stage mbarrier wait, then reused by a later TMA prefetch. The per-stage mbarrier only orders the previous proxy write before the generic reads; it does NOT order those generic reads before the next proxy write into the SAME stage. The PR closes that gap with a new fence.proxy.async.shared::cta placed between the per-lane accumulate and the load-stage rotation/next prefetch. I verified that: both call sites and the helper are consistent, the fence is reached by every lane (outside all per-lane/elect branches), the CTA scope is required because the reusing TMA load is issued by a single elected lane, the compiler memory clobber/scope are correct, and there are no API/layout/scheduling/numerical changes. The change is minimal and correct; only documentation/robustness nits below. v3This PR fixes a shared-memory reuse race in the final combine stage of the two SM100 Mega MoE kernels (bf16_mega_moe and fp8_fp4_mega_moe). Both kernels use a two-stage ping-pong shared buffer where a stage is filled by TMA through the async proxy, consumed by the epilogue warp via generic shared-memory loads, and later reused by a subsequent TMA load. The mbarrier wait only orders the previous TMA write before the generic reads; it does not order those generic reads against the next async-proxy write that reuses the same addresses. The fix adds Files reviewed: 3 |
|
@LyricZhao @zheanxu hi, could you please take a look at this bug fix |
Summary
This PR fixes a shared-memory reuse race in the final combine stage of the SM100 Mega MoE kernels:
fp8_fp4_mega_moebf16_mega_moeBoth kernels use two shared-memory load stages. A stage is filled by TMA through the async proxy, consumed by the warp through generic shared-memory loads, and then reused by a later TMA load. The old code did not order the generic reads against the next async-proxy write to the same stage.
The fix adds
fence.proxy.async.shared::ctaafter every lane has consumed the current stage and before that stage can be selected for reuse.Problem
The combine loop overlaps loading and reduction with a two-stage ping-pong buffer:
sequenceDiagram participant TMA as TMA async proxy participant S0 as Shared stage 0 participant S1 as Shared stage 1 participant W as Epilogue warp TMA->>S0: Load top-k contribution 0 TMA->>S1: Prefetch contribution 1 W->>S0: Wait on mbarrier and read stage 0 Note over W,S0: Generic shared-memory reads TMA->>S0: Reuse stage 0 for contribution 2 Note over TMA,S0: Async-proxy write may race with prior generic readsThe mbarrier wait makes the previous TMA load visible before the warp reads the stage. It does not, by itself, order those generic reads before a later TMA async-proxy write that reuses the same shared-memory addresses. Under unlucky timing, the new TMA load can overwrite data while lanes are still consuming the old contribution, corrupting the final reduction.
Fix
flowchart LR A[TMA load into stage] --> B[mbarrier wait] B --> C[All lanes read and accumulate] C --> D[fence.proxy.async.shared::cta] D --> E[Rotate load stage] E --> F[Later TMA load may reuse stage]The new fence establishes the required generic-to-async proxy ordering at CTA scope:
The PTX instruction is exposed through a small helper in
deep_gemm/ptx/utils.cuhand used by both affected kernels. There are no API, layout, scheduling, or numerical changes.