Skip to content

[BUG] Example 77 FMHA backward (sm100): the Compute warps free Tensor Memory while the Mma and Reduce warps still use it #3935

Backlog

Description

@xxw-keju

Which component has the problem?

CUTLASS C++

Bug Report

Describe the bug

examples/77_blackwell_fmha/kernel/sm100_fmha_bwd_kernel_tma_warpspecialized.hpp, operator() (line numbers of main). The eight Compute warps run compute(...), meet at a named barrier of their own, and the first of them frees all of Tensor Memory (lines 1808–1816):

cutlass::arch::NamedBarrier(
    kNumComputeWarps * NumThreadsPerWarp,
    cutlass::arch::ReservedNamedBarriers::EpilogueBarrier
).arrive_and_wait();

if (warp_idx % kNumComputeWarps == 0) {
  uint32_t free_stage_ptr = shared_storage.tmem_base_ptr;
  tmem_allocator.free(free_stage_ptr, ArchTag::kTmemCapacityColumns);
}

Two other parties still use Tensor Memory at that point:

  • the Mma warp: its last instructions are the dQ MMAs of the last tile (lines 933–944), which it issues after the commit that the Compute warps wait for;
  • the four Reduce warps: they load that dQ from Tensor Memory (reduce(), line 1465) and store it with TMA.

Nothing makes the free wait for either of them.

Today the order comes out right by a few microseconds. With %globaltimer stamps stored for all 2048 blocks of the example's default problem, the Reduce warps' last load is 2 to 4 µs before the free in every block. When the Reduce warps are later than that, they load from Tensor Memory that has been freed, and the block's last tile of dQ is wrong.

Steps/Code to reproduce bug

The patch below makes the Reduce warps wait DELAY_US microseconds (nanosleep) before their last load of dQ; the default is 1000 (1 ms). With it, 77_blackwell_fmha_bwd (fp16) run with --verify reports failed dQ. With -DDELAY_US=0, or with -DFIX=1 and the same delay, it passes. Run it on a GPU that another process is using too: the freed Tensor Memory reads as zero once the GPU has switched to the other process and back, and the blocks that fail are those where that switch falls between the free and the load.

repro.patch
--- a/examples/77_blackwell_fmha/kernel/sm100_fmha_bwd_kernel_tma_warpspecialized.hpp
+++ b/examples/77_blackwell_fmha/kernel/sm100_fmha_bwd_kernel_tma_warpspecialized.hpp
@@ -89,6 +89,15 @@
     Empty = 0x0, Load = 0x1, Mma = 0x2, Compute = 0x3, Reduce = 0x4
   };
 
+  // (experiment) DELAY_US: the Reduce warps wait so many microseconds before their last load of dQ from Tensor Memory.
+  // FIX 1: the Compute warps free Tensor Memory only after the Mma warp and the Reduce warps are done with it.
+#ifndef DELAY_US
+#define DELAY_US 1000
+#endif
+#ifndef FIX
+#define FIX 0
+#endif
+
   static constexpr unsigned long long kWarpAssignment = 0x12'3333'3333'4444ull;
   static constexpr int kNumComputeWarps = 8;
   static constexpr int kNumReduceWarps = 4;
@@ -1461,6 +1470,9 @@
 
       Tensor tTR_rDQ = make_tensor<ElementAcc>(shape(tTR_cDQ));
 
+      if (iter_count == 1) {   // (experiment) before the last load of dQ
+        for (int spin = 0; spin < DELAY_US; ++spin) { asm volatile("nanosleep.u32 1000;" ::: "memory"); }
+      }
       // load dQ from tmem to rmem
       cute::copy(tiled_t2r, tTR_tDQ, tTR_rDQ);
 
@@ -1782,6 +1794,13 @@
           pipeline_mma_compute_dkdv, pipeline_mma_compute_dkdv_producer_state
       );
 
+#if FIX
+      // (experiment, the fix) every MMA is issued: tell the warp that frees Tensor Memory
+      cutlass::arch::NamedBarrier(
+          (kNumComputeWarps + kNumReduceWarps + 1) * NumThreadsPerWarp,
+          cutlass::arch::ReservedNamedBarriers::EpilogueBarrier
+      ).arrive();
+#endif
     }
     else if (role == WarpRole::Compute) {
       warpgroup_reg_set<RegisterAllocation::kCompute>();
@@ -1805,10 +1824,18 @@
           pipeline_mma_compute_dkdv, pipeline_mma_compute_dkdv_consumer_state
       );
 
+#if FIX
+      // (experiment, the fix) wait for the Mma warp and the Reduce warps as well: they still use Tensor Memory
+      cutlass::arch::NamedBarrier(
+          (kNumComputeWarps + kNumReduceWarps + 1) * NumThreadsPerWarp,
+          cutlass::arch::ReservedNamedBarriers::EpilogueBarrier
+      ).arrive_and_wait();
+#else
       cutlass::arch::NamedBarrier(
           kNumComputeWarps * NumThreadsPerWarp,
           cutlass::arch::ReservedNamedBarriers::EpilogueBarrier
       ).arrive_and_wait();
+#endif
 
       if (warp_idx % kNumComputeWarps == 0) {
         uint32_t free_stage_ptr = shared_storage.tmem_base_ptr;
@@ -1833,6 +1860,13 @@
       );
 
       pipeline_reduce_tma_store.producer_tail(pipeline_reduce_tma_store_producer_state);
+#if FIX
+      // (experiment, the fix) the last load from Tensor Memory is done
+      cutlass::arch::NamedBarrier(
+          (kNumComputeWarps + kNumReduceWarps + 1) * NumThreadsPerWarp,
+          cutlass::arch::ReservedNamedBarriers::EpilogueBarrier
+      ).arrive();
+#endif
     }
     else {
       warpgroup_reg_set<RegisterAllocation::kEmpty>();

Results (default problem of the example, --verify against its reference kernel; the GPU was shared with one other process):

variant --verify
unchanged 105 of 105 pass
Reduce warps delayed 10 µs, 50 µs before their last load of dQ 5 of 5 pass each
same, 200 µs 33 of 35 fail (failed dQ, max diff 0.36–0.42)
same, 1 ms 35 of 35 fail (max diff 0.43–0.55)
control: the Compute warps delayed 20 ms before the free 5 of 5 pass
control: the Mma warp delayed 20 ms before its dK MMAs 5 of 5 pass
fix (below), no delay 5 of 5 pass, at the speed of the unchanged kernel
fix, Reduce warps delayed 200 µs / 1 ms / 20 ms 10 of 10 / 35 of 35 / 5 of 5 pass

Expected behavior

Tensor Memory is freed after its last use.

A fix that works (-DFIX=1 in the patch): the Mma warp and the Reduce warps arrive at EpilogueBarrier when they are done, and the Compute warps wait there for all 13 warps before the free.

Environment details (please complete the following information):

  • CUTLASS: main f8acf1a (v4.9.0dev, 2026-11-23)
  • GPU: NVIDIA B200, CUDA 13.2

Additional context

How we found it: with Flick, a static checker of synchronization in GPU kernels that we are developing. It reports that the Mma warp's tcgen05.mma and the Reduce warps' tcgen05.ld can come after the tcgen05.dealloc; we then confirmed it on the GPU as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions