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
3 changes: 3 additions & 0 deletions flash_attn/cute/sm100_hd256_2cta_fmha_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from flash_attn.cute.sm100_hd256_2cta_fmha_backward_dkdvkernel import (
BlackwellFusedMultiHeadAttentionBackwardDKDVKernel,
)
from flash_attn.cute.cute_dsl_utils import assume_tensor_aligned


def _as_bshkrd_tensor(
Expand Down Expand Up @@ -251,6 +252,8 @@ def __call__(
else:
b = Q.shape[0]

Q, K, V, dQ, dK, dV, dO = [assume_tensor_aligned(t) for t in (Q, K, V, dQ, dK, dV, dO)]

Q = _as_bshkrd_tensor(Q, h_k, h_r, varlen)
K = _as_bshkrd_tensor(K, h_k, 1, varlen)
V = _as_bshkrd_tensor(V, h_k, 1, varlen)
Expand Down
48 changes: 32 additions & 16 deletions flash_attn/cute/sm100_hd256_2cta_fmha_backward_dkdvkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Sm100FmhaStaticTileSchedulerParams as FmhaStaticTileSchedulerParams,
)

import flash_attn.cute.copy_utils as fa_copy_utils

LAYOUT_RANK_CONSTANT = 3

Expand Down Expand Up @@ -2811,38 +2812,53 @@ def epilogue_clear(
dK.iterator + mdK_offset,
cute.make_layout((K, self.tile_shape_dQ_K, HB), stride=dK.stride),
)
gdK = cute.local_tile(
mdK, (self.dSQ_mma_tiler[0], self.dSQ_mma_tiler[1]), (None, None, None)
)
gdK = cute.local_tile(mdK, (self.cta_tiler[1], self.cta_tiler[2]), (None, None, None))
gdK = gdK[None, None, blk_coord_k, 0, blk_coord_batch]
cdK = cute.domain_offset(
(blk_coord_k * self.tile_shape_K, 0),
cute.make_identity_tensor((self.dSQ_mma_tiler[0], self.dSQ_mma_tiler[1])),
cute.make_identity_tensor((self.cta_tiler[1], self.cta_tiler[2])),
)

mdV_offset = cute.assume(blk_offset[1] * dV.stride[0], divby=64)
mdV = cute.make_tensor(
dV.iterator + mdV_offset,
cute.make_layout((K, self.tile_shape_dV_dO, HB), stride=dV.stride),
)
gdV = cute.local_tile(
mdV, (self.PdO_mma_tiler[0], self.PdO_mma_tiler[1]), (None, None, None)
)
gdV = cute.local_tile(mdV, (self.cta_tiler[1], self.cta_tiler[2]), (None, None, None))
gdV = gdV[None, None, blk_coord_k, 0, blk_coord_batch]
cdV = cute.domain_offset(
(blk_coord_k * self.tile_shape_K, 0),
cute.make_identity_tensor((self.PdO_mma_tiler[0], self.PdO_mma_tiler[1])),
cute.make_identity_tensor((self.cta_tiler[1], self.cta_tiler[2])),
)

for i in cutlass.range(tidx * 8, cute.size(gdK), block_dim_x * 8):
if cute.elem_less(cdK[i], cute.select(problem_shape, mode=[1, 2])):
gdK_i = cute.make_tensor(gdK.iterator + cute.assume(i, divby=8), (8))
gdK_i.fill(0)
num_zero_epi_threads = 256

tiled_copy_r2g = fa_copy_utils.tiled_copy_2d(
dK.element_type, self.cta_tiler[2], num_zero_epi_threads
)

thr_copy_r2g = tiled_copy_r2g.get_slice(tidx)

tRG_gdK = thr_copy_r2g.partition_D(gdK)
tRG_cdK = thr_copy_r2g.partition_D(cdK)
tRG_gdV = thr_copy_r2g.partition_D(gdV)
tRG_cdV = thr_copy_r2g.partition_D(cdV)

zero_frg = cute.make_rmem_tensor_like(tRG_gdK[None, 0, None])
zero_frg.fill(dK.element_type(0.0))

# check we don't need zero fragment duplication
V_frg_size = cute.size(tRG_gdV[None, 0, None])
assert cute.size(zero_frg) == V_frg_size

if tidx < num_zero_epi_threads:
for n in cutlass.range(cute.size(tRG_gdK.shape[1]), unroll_full=True):
if cute.elem_less(tRG_cdK[0, n, 0][0], problem_shape[1]):
cute.copy(tiled_copy_r2g, zero_frg, tRG_gdK[None, n, None])

for i in cutlass.range(tidx * 8, cute.size(gdV), block_dim_x * 8):
if cute.elem_less(cdV[i], cute.select(problem_shape, mode=[1, 2])):
gdV_i = cute.make_tensor(gdV.iterator + cute.assume(i, divby=8), (8))
gdV_i.fill(0)
for n in cutlass.range(cute.size(tRG_gdV.shape[1]), unroll_full=True):
if cute.elem_less(tRG_cdV[0, n, 0][0], problem_shape[1]):
cute.copy(tiled_copy_r2g, zero_frg, tRG_gdV[None, n, None])

@cute.jit
def epilogue(
Expand Down
Loading
Loading