@@ -516,7 +516,7 @@ constexpr __device__ dequantize_1_f32_t get_dequantize_1_f32(ggml_type type_V) {
516
516
nullptr ;
517
517
}
518
518
519
- template <int D, int ncols1, int ncols2, int KQ_stride > // D == head size
519
+ template <int D, int ncols1, int ncols2> // D == head size
520
520
__launch_bounds__ (D, 1 )
521
521
static __global__ void flash_attn_stream_k_fixup(
522
522
float * __restrict__ dst, const float2 * __restrict__ dst_fixup, const int ne01, const int ne02, const int ne11) {
@@ -665,13 +665,13 @@ static void on_no_fattn_vec_case(const int D) {
665
665
fprintf (stderr, " Compile with GGML_CUDA_FA_ALL_QUANTS for all combinations of q4_0, q4_1, q5_0, q5_1, q8_0, and f16.\n " );
666
666
GGML_ABORT (" fatal error" );
667
667
} else {
668
- fprintf (stderr, " Unsupported KV type combination for head_size 256 .\n " );
668
+ fprintf (stderr, " Unsupported KV type combination for head_size %d .\n " , D );
669
669
fprintf (stderr, " Only f16 is supported.\n " );
670
670
GGML_ABORT (" fatal error" );
671
671
}
672
672
}
673
673
674
- template <int D , int ncols1, int ncols2, int KQ_stride >
674
+ template <int DV , int ncols1, int ncols2>
675
675
void launch_fattn (
676
676
ggml_backend_cuda_context & ctx, ggml_tensor * dst, fattn_kernel_t fattn_kernel, const int nwarps, const size_t nbytes_shared,
677
677
const int KQ_row_granularity, const bool need_f16_K, const bool need_f16_V, const bool stream_k, const int warp_size = WARP_SIZE
@@ -691,7 +691,7 @@ void launch_fattn(
691
691
692
692
GGML_ASSERT (!mask || mask->type == GGML_TYPE_F16);
693
693
GGML_ASSERT (!mask || mask->ne [1 ] >= GGML_PAD (Q->ne [1 ], 16 ) &&
694
- " the Flash-Attention CUDA kernel requires the mask to be padded to 16 and at least n_queries big" );
694
+ " the Flash-Attention CUDA kernel requires the mask to be padded to 16 and at least n_queries big" );
695
695
696
696
GGML_ASSERT (K->ne [1 ] % FATTN_KQ_STRIDE == 0 && " Incorrect KV cache padding." );
697
697
@@ -752,10 +752,13 @@ void launch_fattn(
752
752
const int ntiles_total = ntiles_x * (Q->ne [2 ] / ncols2) * Q->ne [3 ];
753
753
754
754
const dim3 block_dim (warp_size, nwarps, 1 );
755
+ int max_blocks_per_sm = 1 ; // Max. number of active blocks limited by occupancy.
756
+ CUDA_CHECK (cudaOccupancyMaxActiveBlocksPerMultiprocessor (&max_blocks_per_sm, fattn_kernel, block_dim.x * block_dim.y * block_dim.z , nbytes_shared));
757
+
755
758
dim3 blocks_num;
756
759
if (stream_k) {
757
760
// For short contexts it can be faster to have the SMs work on whole tiles because this lets us skip the fixup.
758
- const int max_blocks = 2 *nsm;
761
+ const int max_blocks = max_blocks_per_sm *nsm;
759
762
const int tiles_nwaves = (ntiles_total + max_blocks - 1 ) / max_blocks;
760
763
const int tiles_efficiency_percent = 100 * ntiles_total / (max_blocks*tiles_nwaves);
761
764
@@ -767,14 +770,11 @@ void launch_fattn(
767
770
blocks_num.y = 1 ;
768
771
blocks_num.z = 1 ;
769
772
770
- dst_tmp_meta.alloc (blocks_num.x *ncols * (2 *2 + D ) * sizeof (float ));
773
+ dst_tmp_meta.alloc (blocks_num.x *ncols * (2 *2 + DV ) * sizeof (float ));
771
774
} else {
772
775
GGML_ASSERT (K->ne [1 ] % KQ_row_granularity == 0 );
773
776
const int ntiles_KQ = K->ne [1 ] / KQ_row_granularity; // Max. number of parallel blocks limited by tensor size.
774
777
775
- int max_blocks_per_sm = 1 ; // Max. number of active blocks limited by occupancy.
776
- CUDA_CHECK (cudaOccupancyMaxActiveBlocksPerMultiprocessor (&max_blocks_per_sm, fattn_kernel, block_dim.x * block_dim.y * block_dim.z , nbytes_shared));
777
-
778
778
// parallel_blocks should be at least large enough to achieve max. occupancy for a single wave:
779
779
parallel_blocks = std::max ((nsm * max_blocks_per_sm) / ntiles_total, 1 );
780
780
@@ -851,19 +851,19 @@ void launch_fattn(
851
851
852
852
if (stream_k) {
853
853
if (ntiles_total % blocks_num.x != 0 ) { // Fixup is only needed if the SMs work on fractional tiles.
854
- const dim3 block_dim_combine (D , 1 , 1 );
854
+ const dim3 block_dim_combine (DV , 1 , 1 );
855
855
const dim3 blocks_num_combine = {blocks_num.x , ncols1, ncols2};
856
856
857
- flash_attn_stream_k_fixup<D , ncols1, ncols2, KQ_stride >
857
+ flash_attn_stream_k_fixup<DV , ncols1, ncols2>
858
858
<<<blocks_num_combine, block_dim_combine, 0 , main_stream>>>
859
859
((float *) KQV->data , dst_tmp_meta.ptr , Q->ne [1 ], Q->ne [2 ], K->ne [1 ]);
860
860
}
861
861
} else if (parallel_blocks > 1 ) {
862
- const dim3 block_dim_combine (D , 1 , 1 );
862
+ const dim3 block_dim_combine (DV , 1 , 1 );
863
863
const dim3 blocks_num_combine (Q->ne [1 ], 1 , blocks_num.z );
864
864
const size_t nbytes_shared_combine = parallel_blocks*sizeof (float2 );
865
865
866
- flash_attn_combine_results<D >
866
+ flash_attn_combine_results<DV >
867
867
<<<blocks_num_combine, block_dim_combine, nbytes_shared_combine, main_stream>>>
868
868
(dst_tmp.ptr , dst_tmp_meta.ptr , (float *) KQV->data , parallel_blocks);
869
869
}
0 commit comments