Skip to content

Commit ddfec5d

Browse files
authored
Add FLASHATTENTION_DISABLE_SPLIT_ALIGNMENT (#2680)
1 parent c56ba0f commit ddfec5d

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

csrc/flash_attn/src/flash_fwd_launch_template.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,20 @@ void run_mha_fwd_splitkv_dispatch(Flash_fwd_params &params, cudaStream_t stream)
166166
// TD [2023-08-28]: nvcc segfaults for headdim 96 with block size 64 x 256,
167167
// and for headdim 192 with block size 64 x 128.
168168
constexpr static int kBlockN = Headdim <= 64 ? 256 : (Headdim <= 128 ? 128 : 64);
169-
// if user specifies num_splits=1, we assume they want bitwise identical
169+
#ifndef FLASHATTENTION_DISABLE_SPLIT_ALIGNMENT
170+
// If a user specifies num_splits=1, we assume they want bitwise identical
170171
// numerics across the split KV and standard kernels so we align kBLockN to
171-
// match
172+
// match.
173+
// This compiles a second splitkv kernel tree (a different kBlockN), which
174+
// could push build time to hours+. Define FLASHATTENTION_DISABLE_SPLIT_ALIGNMENT
175+
// to skip.
172176
if (params.num_splits == 1) {
173177
constexpr static int kBlockN_standard = Headdim <= 64 ? 128 : 64;
174178
run_flash_splitkv_fwd<Flash_fwd_kernel_traits<Headdim, kBlockM, kBlockN_standard, 4, false, false, T>, Is_causal>(params, stream);
175-
} else {
176-
run_flash_splitkv_fwd<Flash_fwd_kernel_traits<Headdim, kBlockM, kBlockN, 4, false, false, T>, Is_causal>(params, stream);
179+
return;
177180
}
181+
#endif
182+
run_flash_splitkv_fwd<Flash_fwd_kernel_traits<Headdim, kBlockM, kBlockN, 4, false, false, T>, Is_causal>(params, stream);
178183
}
179184

180185
template<typename T, bool Is_causal>

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ def validate_and_update_archs(archs):
301301
nvcc_flags.extend(["-Xcompiler", "/Zc:__cplusplus"])
302302
compiler_c17_flag=["-O2", "/std:c++17", "/Zc:__cplusplus"]
303303

304+
# Opt-in: skip the num_splits==1 blocksize-alignment instantiation in the
305+
# splitkv dispatch. That alignment (PR #2448) compiles a second splitkv kernel
306+
# tree per head dim, roughly doubling ptxas time for hd32/64/96/128 (hd64 can
307+
# stall ptxas for hours). Disabling it keeps num_splits==1 correct but no
308+
# longer bitwise-identical to the standard kernel. nvcc-only (header in .cu).
309+
if os.getenv("FLASH_ATTENTION_DISABLE_SPLIT_ALIGNMENT", "FALSE") == "TRUE":
310+
nvcc_flags.append("-DFLASHATTENTION_DISABLE_SPLIT_ALIGNMENT")
311+
304312
ext_modules.append(
305313
CUDAExtension(
306314
name="flash_attn_2_cuda",

0 commit comments

Comments
 (0)