Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/flashinfer/attention/sparse_mla_sm120/arch/barrier.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ __device__ __forceinline__ void bar_sync_t() {
asm volatile("barrier.cta.sync %0, %1;\n" ::"n"(ID), "n"(CNT) : "memory");
}

// Producer/consumer handshakes where the arriving side does not block must alternate
// between two barrier ids: the non-blocking side may be one iteration ahead, and
// arriving twice in one phase is invalid and desynchronizes the barrier (#3700).
template <int ID_EVEN, int ID_ODD, int CNT>
__device__ __forceinline__ void bar_arrive_alt(int parity) {
if (parity)
bar_arrive_t<ID_ODD, CNT>();
else
bar_arrive_t<ID_EVEN, CNT>();
}

template <int ID_EVEN, int ID_ODD, int CNT>
__device__ __forceinline__ void bar_sync_alt(int parity) {
if (parity)
bar_sync_t<ID_ODD, CNT>();
else
bar_sync_t<ID_EVEN, CNT>();
}

// mbarrier (SM90+) for async copy tracking
__device__ __forceinline__ void mbarrier_init(uint64_t* mbar, uint32_t count) {
uint32_t addr = static_cast<uint32_t>(__cvta_generic_to_shared(mbar));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ __global__ void __launch_bounds__(BLOCK_THREADS, 1)
sm.kv_bufs[(ti + 1) & 1], idx_base + (ti + 1) * BI, KV_cache,
sm.mbar_kv + ((ti + 1) & 1), io_tid, stride_kv_block, kv_l2_policy);
}
bar_sync_t<1, BLOCK_THREADS>();
bar_sync_alt<1, 5, BLOCK_THREADS>(ti & 1);
}

// ── Math warps ──────────────────────────────────────────────────
Expand Down Expand Up @@ -519,7 +519,7 @@ __global__ void __launch_bounds__(BLOCK_THREADS, 1)
stride_kv_block, reinterpret_cast<bf16*>(sm.w_fp8));
}

bar_arrive_t<1, BLOCK_THREADS>();
bar_arrive_alt<1, 5, BLOCK_THREADS>(ti & 1);
if (ti + 1 < actual_ni) {
const int next_phase = ((ti + 1) >> 1) & 1;
mbarrier_wait_parity(sm.mbar_kv + ((ti + 1) & 1), next_phase);
Expand Down Expand Up @@ -782,7 +782,7 @@ __device__ __forceinline__ void prefill_mg_impl(
io_tid, stride_kv_block, kv_l2_policy);
}
}
bar_sync_t<1, BLOCK_THREADS>();
bar_sync_alt<1, 5, BLOCK_THREADS>(ti & 1);
}
} else {
auto issue_tile = [&](int logical_ti, int buf) {
Expand Down Expand Up @@ -823,7 +823,7 @@ __device__ __forceinline__ void prefill_mg_impl(
if (ti + 1 < loop_bound) {
issue_tile(ti + 1, (ti + 1) & 1);
}
bar_sync_t<1, BLOCK_THREADS>();
bar_sync_alt<1, 5, BLOCK_THREADS>(ti & 1);
}
}

Expand Down Expand Up @@ -1514,7 +1514,7 @@ __device__ __forceinline__ void prefill_mg_impl(
reinterpret_cast<bf16*>(sm.w_fp8()));
}
}
bar_arrive_t<1, BLOCK_THREADS>();
bar_arrive_alt<1, 5, BLOCK_THREADS>(ti & 1);
if (ti + 1 < loop_bound) {
const int next_phase = ((ti + 1) >> 1) & 1;
mbarrier_wait_parity(sm.mbar_kv((ti + 1) & 1), next_phase);
Expand Down
Loading