Skip to content

Commit 73c992c

Browse files
authored
Parallelize splitkv alignment templated kernels, remove flag (#2683)
1 parent e79261e commit 73c992c

51 files changed

Lines changed: 444 additions & 20 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

csrc/flash_attn/src/flash_fwd_launch_template.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,34 @@ void run_flash_splitkv_fwd(Flash_fwd_params &params, cudaStream_t stream) {
160160
}
161161
}
162162

163+
// The num_splits==1 blocksize-aligned splitkv template. If a user specifies
164+
// num_splits=1, we assume they want bitwise identical numerics across the split
165+
// KV and standard kernels so we align kBlockN to match.
166+
// We technically can combine this into one dispatch under run_flash_splitkv_fwd
167+
// but that pathologically slowed down build time by doubling the number of kernels
168+
// in a single file, which made build go from minutes to hours. Thus, it is pulled
169+
// into its own function so it can be explicitly instantiated in a separate file
170+
// (flash_fwd_split_align_*.cu) and compiled in parallel instead of serializing in
171+
// one ptxas invocation.
172+
template<typename T, int Headdim, bool Is_causal>
173+
void run_mha_fwd_splitkv_align(Flash_fwd_params &params, cudaStream_t stream) {
174+
constexpr static int kBlockM = 64;
175+
constexpr static int kBlockN_standard = Headdim <= 64 ? 128 : 64;
176+
run_flash_splitkv_fwd<Flash_fwd_kernel_traits<Headdim, kBlockM, kBlockN_standard, 4, false, false, T>, Is_causal>(params, stream);
177+
}
178+
163179
template<typename T, int Headdim, bool Is_causal>
164180
void run_mha_fwd_splitkv_dispatch(Flash_fwd_params &params, cudaStream_t stream) {
165181
constexpr static int kBlockM = 64;
166182
// TD [2023-08-28]: nvcc segfaults for headdim 96 with block size 64 x 256,
167183
// and for headdim 192 with block size 64 x 128.
168184
constexpr static int kBlockN = Headdim <= 64 ? 256 : (Headdim <= 128 ? 128 : 64);
169-
#ifndef FLASHATTENTION_DISABLE_SPLIT_ALIGNMENT
170-
// If a user specifies num_splits=1, we assume they want bitwise identical
171-
// numerics across the split KV and standard kernels so we align kBLockN to
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.
176185
if (params.num_splits == 1) {
177-
constexpr static int kBlockN_standard = Headdim <= 64 ? 128 : 64;
178-
run_flash_splitkv_fwd<Flash_fwd_kernel_traits<Headdim, kBlockM, kBlockN_standard, 4, false, false, T>, Is_causal>(params, stream);
186+
// Defined in flash_fwd_split_align_*.cu; declared extern in the main
187+
// flash_fwd_split_*.cu so this call does not re-instantiate the tree here.
188+
run_mha_fwd_splitkv_align<T, Headdim, Is_causal>(params, stream);
179189
return;
180190
}
181-
#endif
182191
run_flash_splitkv_fwd<Flash_fwd_kernel_traits<Headdim, kBlockM, kBlockN, 4, false, false, T>, Is_causal>(params, stream);
183192
}
184193

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::bfloat16_t, 128, true>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::bfloat16_t, 128, false>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::half_t, 128, true>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::half_t, 128, false>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::bfloat16_t, 192, true>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::bfloat16_t, 192, false>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::half_t, 192, true>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::half_t, 192, false>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024, Tri Dao.
2+
// Splitting the different head dimensions to different files to speed up compilation.
3+
// This file is auto-generated. See "generate_kernels.py"
4+
#include "namespace_config.h"
5+
#include "flash_fwd_launch_template.h"
6+
7+
namespace FLASH_NAMESPACE {
8+
9+
template void run_mha_fwd_splitkv_align<cutlass::bfloat16_t, 256, true>(Flash_fwd_params &params, cudaStream_t stream);
10+
11+
} // namespace FLASH_NAMESPACE

0 commit comments

Comments
 (0)