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
23 changes: 20 additions & 3 deletions flash_attn/cute/flash_fwd_sm100.py
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,16 @@ def softmax_loop(

qk_descale, _ = self._load_effective_descales(descale_tensors, batch_idx, kv_head_idx)

max_offset = 8 if cutlass.const_expr(self.q_dtype.width == 8) else 0
# P is scaled by 2^max_offset before the FP8 conversion. With rescale_threshold > 0
# the row max can be stale by up to rescale_threshold (in log2 units), so P can reach
# 2^(max_offset + rescale_threshold). max_offset + rescale_threshold must stay within
# log2(fp8_max) (448 = 2^8.8 for e4m3fn, 57344 = 2^15.8 for e5m2), otherwise the
# largest probabilities saturate and accuracy degrades (#2716).
max_offset = (
4 if cutlass.const_expr(self.q_dtype is cutlass.Float8E4M3FN) else
8 if cutlass.const_expr(self.q_dtype.width == 8) else
0
)
if const_expr(self.score_mod is None):
softmax_scale_log2_eff = softmax_scale_log2 * qk_descale
softmax_scale_eff = None
Expand Down Expand Up @@ -2457,9 +2466,17 @@ def correction_loop(
else:
softmax_scale_log2_eff = softmax_scale_log2

max_offset = Float32(8.0) if cutlass.const_expr(self.q_dtype.width == 8) else Float32(0.0)
# Must match the softmax warp's max_offset (see comment there; #2716);
# max_offset_scale = 2^max_offset.
max_offset = (
Float32(4.0) if cutlass.const_expr(self.q_dtype is cutlass.Float8E4M3FN) else
Float32(8.0) if cutlass.const_expr(self.q_dtype.width == 8) else
Float32(0.0)
)
max_offset_scale = (
Float32(256.0) if cutlass.const_expr(self.q_dtype.width == 8) else Float32(1.0)
Float32(16.0) if cutlass.const_expr(self.q_dtype is cutlass.Float8E4M3FN) else
Float32(256.0) if cutlass.const_expr(self.q_dtype.width == 8) else
Float32(1.0)
)
seqlen = SeqlenInfoCls(batch_idx)
n_block_min, n_block_max = block_info.get_n_block_min_max(seqlen, m_block, split_idx, num_splits)
Expand Down
19 changes: 10 additions & 9 deletions tests/cute/test_flash_attn.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,16 @@ def test_flash_attn_output(
learnable_sink = torch.randn(nheads, dtype=torch.bfloat16, device=device)
else:
learnable_sink = None
if dtype == torch.float8_e4m3fn:
q_descale, k_descale, v_descale = [
torch.rand(batch_size, nheads_kv, device=device, dtype=torch.float32)
* 2
for _ in range(3)
]
else:
q_descale, k_descale, v_descale = None, None, None
q, k, v = [x.detach().to(dtype).requires_grad_() for x in (q_ref, k_ref, v_ref)]
# flash_attn_func exposes no descale kwargs (the kernel then uses descale=1),
# so attention_ref must not apply descales either. Descale plumbing is
# exercised via _flash_attn_fwd directly.
q_descale, k_descale, v_descale = None, None, None
# FP8 is forward-only: the interface rejects fp8 inputs with requires_grad,
# and the grad checks below are dtype-gated anyway.
q, k, v = [
x.detach().to(dtype).requires_grad_(dtype != torch.float8_e4m3fn)
for x in (q_ref, k_ref, v_ref)
]
qv = qv_ref.detach().to(dtype).requires_grad_() if has_qv else None
out_ref, attn_ref = attention_ref(
q_ref,
Expand Down