interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / fattn-mma-f16.cuh

2184 lines · 110098 bytes Raw Blame History
1#include "common.cuh"
2#include "cp-async.cuh"
3#include "mma.cuh"
4#include "fattn-common.cuh"
5
6using namespace ggml_cuda_mma;
7
8// Config options for the MMA kernel.
9// Should not affect results, only speed/register pressure/shared memory use.
10struct fattn_mma_config {
11 int nthreads; // Number of threads per CUDA block.
12 int occupancy; // Targeted occupancy for the MMA kernel.
13 int nbatch_fa; // Number of KV rows per softmax rescaling of KQ rowsums and VKQ accumulators.
14 int nbatch_K2; // Number of K half2 values in direction of DKQ to load in parallel.
15 int nbatch_V2; // Number of V half2 values in direction of DV to load in parallel.
16 int nbatch_combine; // Number of VKQ half2 values in direction of DV to combine in parallel.
17 int nstages_target; // Number of pipeline stages to use ideally, 1 == always load data synchronously, 2 == preload data if there is hardware support.
18 bool Q_in_reg; // Whether the Q values should be kept permanently in registers.
19
20 constexpr __host__ __device__ fattn_mma_config(
21 int nthreads, int occupancy, int nbatch_fa, int nbatch_K2, int nbatch_V2, int nbatch_combine, int nstages_target, bool Q_in_reg) :
22 nthreads(nthreads), occupancy(occupancy), nbatch_fa(nbatch_fa), nbatch_K2(nbatch_K2), nbatch_V2(nbatch_V2), nbatch_combine(nbatch_combine),
23 nstages_target(nstages_target), Q_in_reg(Q_in_reg) {}
24};
25
26#define GGML_CUDA_FATTN_MMA_CONFIG_CASE(DKQ_, DV_, ncols_, nthreads_, occupancy_, nbatch_fa_, nbatch_K2_, nbatch_V2_, nbatch_combine_, nstages_target_, Q_in_reg_) \
27 if (DKQ == (DKQ_) && DV == (DV_) && ncols == (ncols_)) { \
28 static_assert((nthreads_) % 32 == 0 && (nthreads_) <= 512, "bad nthreads"); \
29 static_assert( (occupancy_) <= 8, "bad occupancy"); \
30 static_assert((nbatch_fa_) % 32 == 0 && (nbatch_fa_) <= 256, "bad nbatch_fa"); \
31 static_assert((nbatch_K2_) % 4 == 0 && (nbatch_K2_) <= 512, "bad nbatch_K2"); \
32 static_assert((nbatch_V2_) % 4 == 0 && (nbatch_V2_) <= 256, "bad nbatch_V2"); \
33 static_assert((nbatch_combine_) % 4 == 0 && (nbatch_combine_) <= 128, "bad nbatch_combine"); \
34 static_assert((nstages_target_) >= 1 && (nstages_target_) <= 2, "bad nstages_target"); \
35 return fattn_mma_config{(nthreads_), (occupancy_), (nbatch_fa_), (nbatch_K2_), (nbatch_V2_), (nbatch_combine_), (nstages_target_), (Q_in_reg_)}; \
36 } \
37
38static constexpr __host__ __device__ fattn_mma_config ggml_cuda_fattn_mma_get_config_ampere(const int DKQ, const int DV, const int ncols) {
39 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 8, 128, 2, 128, 32, 32, 32, 2, true);
40 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 16, 128, 2, 64, 32, 32, 32, 2, true);
41 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 32, 128, 2, 64, 32, 32, 32, 2, true);
42 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 64, 128, 2, 64, 32, 32, 32, 2, true);
43
44 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 8, 128, 2, 128, 40, 40, 40, 2, true);
45 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 16, 128, 2, 64, 40, 40, 40, 2, true);
46 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 32, 128, 2, 64, 40, 40, 40, 2, true);
47 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 64, 128, 2, 64, 40, 40, 40, 2, true);
48
49 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 8, 128, 2, 128, 48, 48, 48, 2, true);
50 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 16, 128, 2, 64, 48, 48, 48, 2, true);
51 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 32, 128, 2, 64, 48, 48, 48, 2, true);
52 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 64, 128, 2, 64, 48, 48, 48, 2, true);
53
54 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 8, 128, 2, 128, 56, 56, 56, 2, true);
55 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 16, 128, 2, 64, 56, 56, 56, 2, true);
56 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 32, 128, 2, 64, 56, 56, 56, 2, true);
57 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 64, 128, 2, 64, 56, 56, 56, 2, true);
58
59 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 8, 128, 2, 128, 64, 64, 64, 2, true);
60 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 16, 128, 2, 64, 64, 64, 64, 2, true);
61 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 32, 128, 2, 64, 64, 64, 64, 2, true);
62 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 64, 128, 2, 64, 64, 64, 64, 2, true);
63
64 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 8, 64, 4, 64, 96, 64, 64, 2, true);
65 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 16, 64, 4, 32, 96, 64, 64, 2, true);
66 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 32, 128, 2, 32, 96, 64, 64, 2, true);
67 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 64, 128, 2, 32, 96, 64, 64, 2, true);
68
69 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 8, 128, 2, 64, 128, 128, 128, 2, true);
70 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 16, 256, 1, 64, 128, 128, 128, 2, true);
71 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 32, 128, 2, 32, 128, 128, 128, 2, true);
72 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 64, 128, 2, 32, 128, 128, 128, 2, true);
73
74 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 32, 128, 2, 32, 128, 128, 128, 1, false);
75 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 64, 256, 1, 32, 128, 128, 128, 1, false);
76
77 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 8, 128, 2, 64, 128, 128, 128, 1, false);
78 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 16, 256, 1, 64, 128, 128, 128, 1, false);
79 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 32, 256, 1, 32, 128, 128, 128, 1, false);
80 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 64, 256, 1, 32, 128, 128, 128, 1, false);
81
82 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 8, 64, 4, 32, 288, 256, 128, 1, false);
83 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 16, 64, 4, 32, 288, 256, 128, 1, false);
84 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 32, 128, 2, 32, 160, 128, 128, 1, false);
85 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 64, 256, 1, 32, 160, 128, 128, 1, false);
86
87 return fattn_mma_config(32, 1, 0, 0, 0, 0, 0, false);
88}
89
90static constexpr __host__ __device__ fattn_mma_config ggml_cuda_fattn_mma_get_config_turing(const int DKQ, const int DV, const int ncols) {
91 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 8, 128, 2, 64, 128, 128, 128, 2, true);
92 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 16, 128, 2, 64, 128, 128, 128, 2, true);
93 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 32, 128, 2, 64, 128, 128, 64, 2, true);
94 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 64, 128, 2, 64, 128, 128, 64, 2, true);
95
96 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 32, 128, 2, 32, 128, 128, 128, 1, false);
97 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 64, 256, 1, 32, 128, 128, 128, 1, false);
98
99 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 8, 64, 4, 32, 96, 64, 128, 1, false);
100 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 16, 64, 4, 32, 96, 64, 128, 1, false);
101 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 32, 128, 2, 32, 128, 128, 128, 1, false);
102 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 64, 256, 1, 32, 128, 128, 128, 1, false);
103
104 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 8, 64, 4, 32, 96, 64, 128, 1, false);
105 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 16, 64, 4, 32, 96, 64, 128, 1, false);
106 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 32, 128, 2, 32, 160, 128, 128, 1, false);
107 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 64, 256, 1, 32, 160, 128, 128, 1, false);
108
109 return ggml_cuda_fattn_mma_get_config_ampere(DKQ, DV, ncols);
110}
111
112static constexpr __host__ __device__ fattn_mma_config ggml_cuda_fattn_mma_get_config_volta(const int DKQ, const int DV, const int ncols) {
113 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 32, 128, 2, 32, 128, 128, 64, 1, false);
114 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 64, 256, 1, 32, 128, 128, 64, 1, false);
115
116 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 8, 64, 4, 32, 256, 256, 64, 1, false);
117 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 16, 64, 4, 32, 256, 256, 64, 1, false);
118 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 32, 128, 2, 32, 128, 128, 64, 1, false);
119 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 64, 256, 1, 32, 128, 128, 64, 1, false);
120
121 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 8, 64, 4, 32, 288, 256, 64, 1, false);
122 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 16, 64, 4, 32, 288, 256, 64, 1, false);
123 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 32, 128, 2, 32, 160, 128, 64, 1, false);
124 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 64, 256, 1, 32, 160, 128, 64, 1, false);
125
126 // TODO tune specifically for Volta
127 return ggml_cuda_fattn_mma_get_config_ampere(DKQ, DV, ncols);
128}
129
130static constexpr __host__ __device__ fattn_mma_config ggml_cuda_fattn_mma_get_config_rdna(const int DKQ, const int DV, const int ncols) {
131 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 8, 128, 2, 64, 32, 32, 32, 1, true);
132 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 16, 128, 2, 64, 32, 32, 32, 1, true);
133 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 32, 128, 2, 64, 32, 32, 32, 1, true);
134 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 64, 128, 2, 64, 32, 32, 32, 1, true);
135
136 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 8, 64, 2, 32, 40, 40, 40, 1, true);
137 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 16, 64, 2, 32, 40, 40, 40, 1, true);
138 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 32, 128, 2, 64, 40, 40, 40, 1, true);
139 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 64, 128, 2, 64, 40, 40, 40, 1, true);
140
141 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 8, 64, 2, 32, 48, 48, 48, 1, true);
142 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 16, 64, 2, 32, 48, 48, 48, 1, true);
143 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 32, 128, 2, 64, 48, 48, 48, 1, true);
144 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 64, 128, 2, 64, 48, 48, 48, 1, true);
145
146 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 8, 64, 2, 32, 56, 56, 56, 1, true);
147 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 16, 64, 2, 32, 56, 56, 56, 1, true);
148 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 32, 128, 2, 64, 56, 56, 56, 1, true);
149 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 64, 128, 2, 64, 56, 56, 56, 1, true);
150
151 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 8, 64, 2, 32, 64, 64, 64, 1, true);
152 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 16, 64, 2, 32, 64, 64, 64, 1, true);
153 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 32, 128, 2, 64, 64, 64, 64, 1, true);
154 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 64, 128, 2, 64, 64, 64, 64, 1, true);
155
156 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 8, 64, 2, 32, 96, 64, 64, 1, true);
157 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 16, 64, 2, 32, 96, 64, 64, 1, true);
158 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 32, 128, 2, 64, 96, 64, 64, 1, true);
159 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 64, 128, 2, 64, 96, 64, 64, 1, true);
160
161 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 8, 64, 2, 32, 128, 128, 128, 1, true);
162 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 16, 64, 2, 32, 128, 128, 128, 1, true);
163 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 32, 256, 2, 64, 128, 128, 64, 1, true);
164 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 64, 256, 2, 64, 128, 128, 64, 1, true);
165
166 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 32, 128, 2, 32, 160, 128, 128, 1, true);
167 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 64, 128, 2, 32, 160, 128, 128, 1, true);
168
169 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 8, 128, 3, 64, 96, 64, 128, 1, true);
170 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 16, 128, 3, 64, 96, 64, 128, 1, true);
171 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 32, 128, 2, 32, 128, 128, 128, 1, true);
172 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 64, 128, 2, 32, 128, 128, 128, 1, true);
173
174 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 8, 128, 3, 64, 96, 64, 128, 1, true);
175 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 16, 128, 3, 64, 96, 64, 128, 1, true);
176 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 32, 128, 2, 32, 160, 128, 128, 1, true);
177 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 64, 128, 2, 32, 160, 128, 128, 1, true);
178
179 return fattn_mma_config(32, 1, 0, 0, 0, 0, 0, false);
180}
181
182static constexpr __host__ __device__ fattn_mma_config ggml_cuda_fattn_mma_get_config_cdna(const int DKQ, const int DV, const int ncols) {
183 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 8, 128, 1, 64, 32, 32, 32, 1, true);
184 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 16, 256, 2, 64, 32, 32, 32, 1, true);
185 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 32, 256, 2, 64, 32, 32, 32, 1, true);
186 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 64, 64, 64, 256, 3, 64, 32, 32, 32, 1, true);
187
188 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 8, 256, 2, 64, 40, 40, 40, 1, true);
189 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 16, 256, 2, 64, 40, 40, 40, 1, true);
190 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 32, 256, 2, 64, 40, 40, 40, 1, true);
191 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 80, 80, 64, 256, 2, 64, 40, 40, 40, 1, true);
192
193 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 8, 256, 2, 64, 48, 48, 48, 1, true);
194 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 16, 256, 2, 64, 48, 48, 48, 1, true);
195 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 32, 256, 2, 64, 48, 48, 48, 1, true);
196 GGML_CUDA_FATTN_MMA_CONFIG_CASE( 96, 96, 64, 256, 2, 64, 48, 48, 48, 1, true);
197
198 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 8, 256, 2, 64, 56, 56, 56, 1, true);
199 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 16, 256, 2, 64, 56, 56, 56, 1, true);
200 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 32, 256, 2, 64, 56, 56, 56, 1, true);
201 GGML_CUDA_FATTN_MMA_CONFIG_CASE(112, 112, 64, 256, 2, 64, 56, 56, 56, 1, true);
202
203 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 8, 256, 2, 64, 64, 64, 64, 1, true);
204 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 16, 256, 2, 64, 64, 64, 64, 1, true);
205 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 32, 256, 2, 64, 64, 64, 64, 1, true);
206 GGML_CUDA_FATTN_MMA_CONFIG_CASE(128, 128, 64, 256, 2, 64, 64, 64, 64, 1, true);
207
208 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 8, 256, 1, 64, 64, 64, 64, 1, true);
209 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 16, 256, 1, 64, 64, 64, 64, 1, true);
210 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 32, 256, 1, 64, 64, 64, 64, 1, true);
211 GGML_CUDA_FATTN_MMA_CONFIG_CASE(192, 128, 64, 512, 1, 64, 64, 64, 64, 1, true);
212
213 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 8, 256, 1, 64, 128, 128, 128, 1, true);
214 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 16, 256, 1, 64, 128, 128, 128, 1, true);
215 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 32, 256, 1, 64, 128, 128, 128, 1, true);
216 GGML_CUDA_FATTN_MMA_CONFIG_CASE(256, 256, 64, 512, 1, 64, 128, 128, 64, 1, true);
217
218 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 32, 256, 1, 64, 160, 128, 128, 1, true);
219 GGML_CUDA_FATTN_MMA_CONFIG_CASE(320, 256, 64, 256, 1, 64, 160, 128, 128, 1, true);
220
221 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 8, 256, 1, 64, 128, 128, 128, 1, true);
222 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 16, 256, 1, 64, 128, 128, 128, 1, true);
223 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 32, 256, 1, 64, 128, 128, 128, 1, true);
224 GGML_CUDA_FATTN_MMA_CONFIG_CASE(512, 512, 64, 256, 1, 64, 128, 128, 128, 1, true);
225
226 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 8, 256, 1, 64, 128, 128, 128, 1, true);
227 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 16, 256, 1, 64, 128, 128, 128, 1, true);
228 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 32, 256, 1, 64, 160, 128, 128, 1, true);
229 GGML_CUDA_FATTN_MMA_CONFIG_CASE(576, 512, 64, 256, 1, 64, 160, 128, 128, 1, true);
230
231 return fattn_mma_config(32, 1, 0, 0, 0, 0, 0, false);
232}
233
234static __host__ fattn_mma_config ggml_cuda_fattn_mma_get_config(const int DKQ, const int DV, const int ncols, const int cc) {
235 if (ampere_mma_available(cc)) {
236 return ggml_cuda_fattn_mma_get_config_ampere(DKQ, DV, ncols);
237 }
238 if (turing_mma_available(cc)) {
239 return ggml_cuda_fattn_mma_get_config_turing(DKQ, DV, ncols);
240 }
241 if (amd_mfma_available(cc)) {
242 return ggml_cuda_fattn_mma_get_config_cdna(DKQ, DV, ncols);
243 }
244 if (amd_wmma_available(cc)) {
245 return ggml_cuda_fattn_mma_get_config_rdna(DKQ, DV, ncols);
246 }
247 GGML_ASSERT(volta_mma_available(cc));
248 return ggml_cuda_fattn_mma_get_config_volta(DKQ, DV, ncols);
249}
250
251static constexpr __device__ fattn_mma_config ggml_cuda_fattn_mma_get_config(const int DKQ, const int DV, const int ncols) {
252#if defined(AMPERE_MMA_AVAILABLE)
253 return ggml_cuda_fattn_mma_get_config_ampere(DKQ, DV, ncols);
254#elif defined(TURING_MMA_AVAILABLE)
255 return ggml_cuda_fattn_mma_get_config_turing(DKQ, DV, ncols);
256#elif defined(AMD_MFMA_AVAILABLE)
257 return ggml_cuda_fattn_mma_get_config_cdna(DKQ, DV, ncols);
258#elif defined(VOLTA_MMA_AVAILABLE)
259 return ggml_cuda_fattn_mma_get_config_volta(DKQ, DV, ncols);
260#elif defined(AMD_WMMA_AVAILABLE)
261 return ggml_cuda_fattn_mma_get_config_rdna(DKQ, DV, ncols);
262#else
263 GGML_UNUSED_VARS(DKQ, DV, ncols);
264 return fattn_mma_config(32, 1, 0, 0, 0, 0, 0, false);
265#endif // defined(AMPERE_MMA_AVAILABLE)
266}
267
268static __host__ int ggml_cuda_fattn_mma_get_nthreads(const int DKQ, const int DV, const int ncols, const int cc) {
269 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).nthreads;
270}
271
272static constexpr __device__ int ggml_cuda_fattn_mma_get_nthreads(const int DKQ, const int DV, const int ncols) {
273 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).nthreads;
274}
275
276static __host__ int ggml_cuda_fattn_mma_get_occupancy(const int DKQ, const int DV, const int ncols, const int cc) {
277 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).occupancy;
278}
279
280static constexpr __device__ int ggml_cuda_fattn_mma_get_occupancy(const int DKQ, const int DV, const int ncols) {
281 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).occupancy;
282}
283
284static __host__ int ggml_cuda_fattn_mma_get_nbatch_fa(const int DKQ, const int DV, const int ncols, const int cc) {
285 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).nbatch_fa;
286}
287
288static constexpr __device__ int ggml_cuda_fattn_mma_get_nbatch_fa(const int DKQ, const int DV, const int ncols) {
289 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).nbatch_fa;
290}
291
292static __host__ int ggml_cuda_fattn_mma_get_nbatch_K2(const int DKQ, const int DV, const int ncols, const int cc) {
293 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).nbatch_K2;
294}
295
296static constexpr __device__ int ggml_cuda_fattn_mma_get_nbatch_K2(const int DKQ, const int DV, const int ncols) {
297 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).nbatch_K2;
298}
299
300static __host__ int ggml_cuda_fattn_mma_get_nbatch_V2(const int DKQ, const int DV, const int ncols, const int cc) {
301 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).nbatch_V2;
302}
303
304static constexpr __device__ int ggml_cuda_fattn_mma_get_nbatch_V2(const int DKQ, const int DV, const int ncols) {
305 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).nbatch_V2;
306}
307
308static __host__ int ggml_cuda_fattn_mma_get_nbatch_combine(const int DKQ, const int DV, const int ncols, const int cc) {
309 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).nbatch_combine;
310}
311
312static constexpr __device__ int ggml_cuda_fattn_mma_get_nbatch_combine(const int DKQ, const int DV, const int ncols) {
313 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).nbatch_combine;
314}
315
316static __host__ int ggml_cuda_fattn_mma_get_nstages_target(const int DKQ, const int DV, const int ncols, const int cc) {
317 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).nstages_target;
318}
319
320static constexpr __device__ int ggml_cuda_fattn_mma_get_nstages_target(const int DKQ, const int DV, const int ncols) {
321 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).nstages_target;
322}
323
324static __host__ bool ggml_cuda_fattn_mma_get_Q_in_reg(const int DKQ, const int DV, const int ncols, const int cc) {
325 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols, cc).Q_in_reg;
326}
327
328static constexpr __device__ bool ggml_cuda_fattn_mma_get_Q_in_reg(const int DKQ, const int DV, const int ncols) {
329 return ggml_cuda_fattn_mma_get_config(DKQ, DV, ncols).Q_in_reg;
330}
331
332static constexpr __device__ int get_cols_per_thread() {
333#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
334 return 1; // AMD has a single column per thread.
335#else
336 return 2; // This is specifically KQ columns, Volta only has a single VKQ column.
337#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
338}
339
340static __host__ int get_cols_per_warp(const int cc) {
341 if (turing_mma_available(cc) || amd_wmma_available(cc) || amd_mfma_available(cc)) {
342 return 16;
343 } else {
344 // Volta
345 return 32;
346 }
347}
348
349static __host__ bool ggml_cuda_fattn_mma_get_swizzled(const int DKQ, const int DV, const int ncols, const int cc) {
350 return turing_mma_available(cc) &&
351 ggml_cuda_fattn_mma_get_nbatch_K2(DKQ, DV, ncols, cc) % 32 == 0 && ggml_cuda_fattn_mma_get_nbatch_V2(DKQ, DV, ncols, cc) % 32 == 0;
352}
353
354static constexpr __device__ bool ggml_cuda_fattn_mma_get_swizzled(const int DKQ, const int DV, const int ncols) {
355#ifdef TURING_MMA_AVAILABLE
356 return ggml_cuda_fattn_mma_get_nbatch_K2(DKQ, DV, ncols) % 32 == 0 && ggml_cuda_fattn_mma_get_nbatch_V2(DKQ, DV, ncols) % 32 == 0;
357#else
358 GGML_UNUSED_VARS(DKQ, DV, ncols);
359 return false;
360#endif // TURING_MMA_AVAILABLE
361}
362
363// ------------------------------------------------------------------------------------------------------------------
364
365static __host__ int ggml_cuda_fattn_mma_get_nstages(const int DKQ, const int DV, const int ncols1, const int ncols2, const int cc) {
366 return cp_async_available(cc) && ncols2 >= 2 ? ggml_cuda_fattn_mma_get_nstages_target(DKQ, DV, ncols1*ncols2, cc) : 0;
367}
368
369static constexpr __device__ int ggml_cuda_fattn_mma_get_nstages(
370 const int DKQ, const int DV, const int ncols1, const int ncols2, const bool use_sparse) {
371#ifdef CP_ASYNC_AVAILABLE
372 const int nstages_target = ncols2 >= 2 ? ggml_cuda_fattn_mma_get_nstages_target(DKQ, DV, ncols1*ncols2) : 0;
373 // sparse gather is not implemented for multi-stage loading
374 return use_sparse && nstages_target > 1 ? 1 : nstages_target;
375#else
376 GGML_UNUSED_VARS(DKQ, DV, ncols1, ncols2, use_sparse);
377 return 0;
378#endif // CP_ASYNC_AVAILABLE
379}
380
381// ------------------------------------------------------------------------------------------------------------------
382
383template<int stride_tile, int nwarps, int nbatch_fa, bool use_cp_async, bool oob_check, bool use_sparse>
384static __device__ __forceinline__ void flash_attn_ext_f16_load_tile(
385 const half2 * const __restrict__ KV, half2 * const __restrict__ tile_KV, const int D2, const int stride_KV,
386 const int k_VKQ_0, const int i_sup, const int32_t * const __restrict__ indices) {
387 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
388 // K/V data is loaded with decreasing granularity for D for better memory bandwidth.
389 // The minimum granularity is 16 bytes.
390 constexpr int chunk_size = 16;
391 constexpr int h2_per_chunk = chunk_size / sizeof(half2);
392 const int chunks_per_row = D2 / h2_per_chunk;
393 if constexpr (use_cp_async) {
394 static_assert(warp_size == 32, "bad warp_size");
395 static_assert(!oob_check || use_sparse, "OOB check not compatible with cp_async");
396 constexpr int preload = 64;
397
398 const unsigned int tile_KV_32 = ggml_cuda_cvta_generic_to_shared(tile_KV);
399
400 auto load = [&] __device__ (auto n) {
401 const int stride_k = warp_size >> n;
402 const int k0_start = stride_k == warp_size ? 0 : chunks_per_row - chunks_per_row % (2*stride_k);
403 const int k0_stop = chunks_per_row - chunks_per_row % (1*stride_k);
404 const int stride_i = warp_size / stride_k;
405
406 if (k0_start == k0_stop) {
407 return;
408 }
409
410#pragma unroll
411 for (int i0 = 0; i0 < nbatch_fa; i0 += nwarps*stride_i) {
412 const int i = i0 + threadIdx.y*stride_i + (stride_k == warp_size ? 0 : threadIdx.x / stride_k);
413
414 if (i0 + nwarps*stride_i > nbatch_fa && i >= nbatch_fa) {
415 break;
416 }
417
418 int64_t i_KV;
419 if constexpr (use_sparse) {
420 // padded slots gather row 0, the -inf mask removes their contribution
421 const int32_t index = i < i_sup ? indices[k_VKQ_0 + i] : 0;
422 i_KV = index >= 0 ? index : 0;
423 } else if (indices) {
424 // K/V rows given per mask column, negative rows are padding
425 i_KV = max(indices[k_VKQ_0 + i], 0);
426 } else {
427 i_KV = k_VKQ_0 + i;
428 }
429
430#pragma unroll
431 for (int k0 = k0_start; k0 < k0_stop; k0 += stride_k) {
432 const int k = k0 + (stride_k == warp_size ? threadIdx.x : threadIdx.x % stride_k);
433
434 cp_async_cg_16<preload>(tile_KV_32 + swizzle<stride_tile*sizeof(half2), char>(i*stride_tile*sizeof(half2) + k*chunk_size, i), KV + i_KV*stride_KV + k*h2_per_chunk);
435 }
436 }
437 };
438 // 1: max 32*16=512 bytes, 256 half
439 // 2: max 16*16=256 bytes, 128 half
440 // 3: max 8*16=128 bytes, 64 half
441 // 4: max 4*16= 64 bytes, 32 half
442 // 5: max 2*16= 32 bytes, 16 half
443 // 6: max 1*16= 16 bytes, 8 half
444 ggml_cuda_unroll<6>{}(load);
445 } else {
446 const half2 zero[4] = {{0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}};
447 auto load = [&] __device__ (const int n) {
448 const int stride_k = 32 >> n;
449 const int k0_start = stride_k == 32 ? 0 : chunks_per_row - chunks_per_row % (2*stride_k);
450 const int k0_stop = chunks_per_row - chunks_per_row % (1*stride_k);
451 const int stride_i = warp_size / stride_k;
452
453 if (k0_start == k0_stop) {
454 return;
455 }
456
457#pragma unroll
458 for (int i0 = 0; i0 < nbatch_fa; i0 += nwarps*stride_i) {
459 const int i = i0 + threadIdx.y*stride_i + (stride_k == warp_size ? 0 : threadIdx.x / stride_k);
460
461 if (i0 + nwarps*stride_i > nbatch_fa && i >= nbatch_fa) {
462 break;
463 }
464
465#pragma unroll
466 for (int k0 = k0_start; k0 < k0_stop; k0 += stride_k) {
467 const int k = k0 + (stride_k == warp_size ? threadIdx.x : threadIdx.x % stride_k);
468
469 const half2 * src;
470 if constexpr (use_sparse) {
471 const int32_t index = i < i_sup ? indices[k_VKQ_0 + i] : -1;
472 src = index >= 0 ? KV + int64_t(index)*stride_KV + k*h2_per_chunk : zero;
473 } else if (!oob_check || i < i_sup) {
474 const int64_t i_KV = indices ? max(indices[k_VKQ_0 + i], 0) : k_VKQ_0 + i;
475 src = KV + i_KV*stride_KV + k*h2_per_chunk;
476 } else {
477 src = zero;
478 }
479 ggml_cuda_memcpy_1<16>(swizzle<stride_tile>(tile_KV, i*stride_tile + k*h2_per_chunk, i), src);
480 }
481 }
482 };
483 // 1: max 32*16=512 bytes, 256 half
484 // 2: max 16*16=256 bytes, 128 half
485 // 3: max 8*16=128 bytes, 64 half
486 // 4: max 4*16= 64 bytes, 32 half
487 // 5: max 2*16= 32 bytes, 16 half
488 // 6: max 1*16= 16 bytes, 8 half
489 ggml_cuda_unroll<6>{}(load);
490 }
491}
492
493template<int ncols1, int nwarps, int nbatch_fa, bool use_cp_async, bool oob_check, bool use_sparse>
494static __device__ __forceinline__ void flash_attn_ext_f16_load_mask(
495 const half * const __restrict__ mask_h, half * const __restrict__ tile_mask,
496 const int stride_mask, const int k_VKQ_0, const int i_sup, const int j0, const uint3 ne01,
497 const int32_t * const __restrict__ indices) {
498 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
499 if constexpr (use_cp_async) {
500 static_assert(nbatch_fa <= 8*warp_size && nbatch_fa % 8 == 0, "bad nbatch_fa");
501 static_assert(!oob_check, "OOB check incompatible with cp_async");
502 static_assert(!use_sparse, "sparse gather incompatible with cp_async");
503 constexpr int preload = nbatch_fa >= 32 ? nbatch_fa * sizeof(half) : 64;
504 constexpr int cols_per_warp = 8*warp_size/nbatch_fa;
505 constexpr int stride_j = nwarps * cols_per_warp;
506
507 const unsigned int tile_mask_32 = ggml_cuda_cvta_generic_to_shared(tile_mask);
508
509#pragma unroll
510 for (int j1 = 0; j1 < ncols1; j1 += stride_j) {
511 const int j_sram = j1 + threadIdx.y*cols_per_warp + threadIdx.x / (warp_size/cols_per_warp);
512 const int j_vram = fastmodulo(j0 + j_sram, ne01);
513
514 if (j1 + stride_j > ncols1 && j_sram >= ncols1) {
515 break;
516 }
517
518 const int i = 8 * (threadIdx.x % (nbatch_fa/8));
519
520 cp_async_cg_16<preload>(tile_mask_32 + j_sram*(nbatch_fa*sizeof(half) + 16) + i*sizeof(half), mask_h + int64_t(j_vram)*stride_mask + k_VKQ_0 + i);
521 }
522 } else if constexpr (oob_check || use_sparse) {
523#pragma unroll
524 for (int j1 = 0; j1 < ncols1; j1 += nwarps) {
525 const int j_sram = j1 + threadIdx.y;
526 const int j_vram = fastmodulo(j0 + j_sram, ne01);
527
528 if (j1 + nwarps > ncols1 && j_sram >= ncols1) {
529 break;
530 }
531
532#pragma unroll
533 for (int i0 = 0; i0 < nbatch_fa; i0 += warp_size) {
534 const int i = i0 + threadIdx.x;
535
536 if constexpr (use_sparse) {
537 const int32_t index = i < i_sup ? indices[k_VKQ_0 + i] : -1;
538 tile_mask[j_sram*(nbatch_fa + 8) + i] = index >= 0 ? mask_h[int64_t(j_vram)*stride_mask + index] : half(-INFINITY);
539 } else {
540 tile_mask[j_sram*(nbatch_fa + 8) + i] = i < i_sup ? mask_h[int64_t(j_vram)*stride_mask + k_VKQ_0 + i] : half(0.0f);
541 }
542 }
543 }
544 } else if constexpr (nbatch_fa < 2*warp_size) {
545 constexpr int cols_per_warp = 2*warp_size/nbatch_fa;
546 constexpr int stride_j = nwarps * cols_per_warp;
547#pragma unroll
548 for (int j1 = 0; j1 < ncols1; j1 += stride_j) {
549 const int j_sram = j1 + threadIdx.y*cols_per_warp + threadIdx.x / (warp_size/cols_per_warp);
550 const int j_vram = fastmodulo(j0 + j_sram, ne01);
551
552 if (j1 + stride_j > ncols1 && j_sram >= ncols1) {
553 break;
554 }
555
556 const int i = threadIdx.x % (warp_size/cols_per_warp);
557
558 ggml_cuda_memcpy_1<sizeof(half2)>(tile_mask + j_sram*(nbatch_fa + 8) + 2*i, mask_h + int64_t(j_vram)*stride_mask + k_VKQ_0 + 2*i);
559 }
560 } else {
561#pragma unroll
562 for (int j1 = 0; j1 < ncols1; j1 += nwarps) {
563 const int j_sram = j1 + threadIdx.y;
564 const int j_vram = fastmodulo(j0 + j_sram, ne01);
565
566 if (j1 + nwarps > ncols1 && j_sram >= ncols1) {
567 break;
568 }
569
570#pragma unroll
571 for (int i0 = 0; i0 < nbatch_fa; i0 += 2*warp_size) {
572 const int i = i0 + 2*threadIdx.x;
573
574 ggml_cuda_memcpy_1<sizeof(half2)>(tile_mask + j_sram*(nbatch_fa + 8) + i, mask_h + int64_t(j_vram)*stride_mask + k_VKQ_0 + i);
575 }
576 }
577 }
578}
579
580template<int DKQ, int DV, int ncols1, int ncols2, int nwarps,
581 bool use_logit_softcap, bool V_is_K_view, bool use_sparse, bool needs_fixup, bool is_fixup, bool last_iter, bool oob_check,
582 typename T_A_KQ, typename T_B_KQ, typename T_C_KQ, typename T_A_VKQ, typename T_B_VKQ, typename T_C_VKQ>
583static __device__ __forceinline__ void flash_attn_ext_f16_iter(
584 const float2 * const __restrict__ Q_f2,
585 const half2 * const __restrict__ K_h2,
586 const half2 * const __restrict__ V_h2,
587 const half * const __restrict__ mask_h,
588 const int32_t * const __restrict__ indices,
589 float2 * const __restrict__ dstk,
590 float2 * const __restrict__ dstk_fixup,
591 const float scale,
592 const float slope,
593 const float logit_softcap,
594 const uint3 ne01,
595 const int ne02,
596 const int stride_K,
597 const int stride_V,
598 const int stride_mask,
599 half2 * const __restrict__ tile_Q,
600 half2 * const __restrict__ tile_K,
601 half2 * const __restrict__ tile_V,
602 half * const __restrict__ tile_mask,
603 T_B_KQ * const __restrict__ Q_B,
604 T_C_VKQ * const __restrict__ VKQ_C,
605 float * const __restrict__ KQ_max,
606 float * const __restrict__ KQ_rowsum,
607 const int jt,
608 const int kb0,
609 const int k_VKQ_sup) {
610#if defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
611 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
612 constexpr int ncols = ncols1 * ncols2;
613 constexpr int cols_per_warp = T_B_KQ::I;
614 constexpr int cols_per_thread = get_cols_per_thread();
615 constexpr int np = cols_per_warp > ncols ? nwarps : nwarps * cols_per_warp/ncols; // Number of parallel CUDA warps per Q column.
616 constexpr int nbatch_fa = ggml_cuda_fattn_mma_get_nbatch_fa(DKQ, DV, ncols);
617 constexpr int nbatch_K2 = ggml_cuda_fattn_mma_get_nbatch_K2(DKQ, DV, ncols);
618 constexpr int nbatch_V2 = ggml_cuda_fattn_mma_get_nbatch_V2(DKQ, DV, ncols);
619 constexpr bool Q_in_reg = ggml_cuda_fattn_mma_get_Q_in_reg (DKQ, DV, ncols);
620 constexpr int nstages = ggml_cuda_fattn_mma_get_nstages (DKQ, DV, ncols1, ncols2, use_sparse);
621
622 constexpr bool swz = ggml_cuda_fattn_mma_get_swizzled(DKQ, DV, ncols);
623 constexpr int stride_tile_K = swz ? nbatch_K2 : nbatch_K2 + 4;
624 constexpr int stride_tile_V = V_is_K_view ? stride_tile_K : (swz ? nbatch_V2 : nbatch_V2 + 4);
625
626 const int k_VKQ_0 = kb0 * nbatch_fa;
627#if defined(TURING_MMA_AVAILABLE)
628 T_C_KQ KQ_C[nbatch_fa/(np*(cols_per_warp == 8 ? T_C_KQ::I : T_C_KQ::J))];
629#elif defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
630 T_C_KQ KQ_C[nbatch_fa/(np*T_C_KQ::J)];
631#else // Volta
632 T_C_KQ KQ_C[nbatch_fa/(np*T_C_KQ::J)];
633#endif // defined(TURING_MMA_AVAILABLE)
634
635 if constexpr (nstages > 1) {
636 static_assert(!oob_check, "OOB check incompatible with multi-stage pipeline");
637 static_assert(!V_is_K_view, "K data reuse not implemented multi-stage loading");
638 static_assert(nbatch_K2 == DKQ/2, "batching not implemented for multi stage loading");
639 constexpr bool use_cp_async = true;
640 cp_async_wait_all();
641 __syncthreads();
642 flash_attn_ext_f16_load_tile<stride_tile_V, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
643 (V_h2, tile_V, nbatch_V2, stride_V, k_VKQ_0, k_VKQ_sup, indices);
644 } else {
645 // the sparse mask values are gathered per element, always load them synchronously
646 constexpr bool use_cp_async = nstages == 1 && !use_sparse;
647 if (ncols2 > 1 || mask_h) {
648 flash_attn_ext_f16_load_mask<ncols1, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
649 (mask_h, tile_mask, stride_mask, k_VKQ_0, k_VKQ_sup, jt*ncols1, ne01, indices);
650 }
651 }
652
653 // For MLA K and V have the same data.
654 // Therefore, iterate over K in reverse and later re-use the data if possible.
655#pragma unroll
656 for (int k0_start = (DKQ/2-1) - (DKQ/2-1) % nbatch_K2; k0_start >= 0; k0_start -= nbatch_K2) {
657 const int k0_stop = k0_start + nbatch_K2 < DKQ/2 ? k0_start + nbatch_K2 : DKQ/2;
658
659 if constexpr (nstages <= 1) {
660 const int k0_diff = k0_stop - k0_start;
661 constexpr bool use_cp_async = nstages == 1;
662 flash_attn_ext_f16_load_tile<stride_tile_K, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
663 (K_h2 + k0_start, tile_K, k0_diff, stride_K, k_VKQ_0, k_VKQ_sup, indices);
664 if (use_cp_async) {
665 cp_async_wait_all();
666 }
667 __syncthreads();
668 }
669
670 // Calculate tile of KQ:
671 if constexpr (Q_in_reg) {
672#pragma unroll
673 for (int i_KQ_00 = 0; i_KQ_00 < nbatch_fa; i_KQ_00 += np*T_A_KQ::I) {
674 const int i_KQ_0 = i_KQ_00 + (threadIdx.y % np)*T_A_KQ::I;
675#pragma unroll
676 for (int k_KQ_0 = k0_start; k_KQ_0 < k0_stop; k_KQ_0 += T_A_KQ::J) {
677 T_A_KQ K_A;
678 load_ldmatrix_swizzled<stride_tile_K>(K_A, tile_K, i_KQ_0*stride_tile_K + k_KQ_0-k0_start);
679 if constexpr (cols_per_warp == 8) {
680 mma(KQ_C[i_KQ_00/(np*T_A_KQ::I)], K_A, Q_B[k_KQ_0/T_A_KQ::J]);
681 } else {
682 // Wide version of KQ_C is column-major
683#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
684 // AMD matrix C is column-major.
685 mma(KQ_C[i_KQ_00/(np*T_A_KQ::I)], K_A, Q_B[k_KQ_0/T_A_KQ::J]);
686#else
687 // swap A and B for CUDA.
688 mma(KQ_C[i_KQ_00/(np*T_A_KQ::I)], Q_B[k_KQ_0/T_A_KQ::J], K_A);
689#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
690 }
691 }
692 }
693 } else {
694 constexpr int stride_tile_Q = DKQ/2 + 4;
695#pragma unroll
696 for (int k_KQ_0 = k0_start; k_KQ_0 < k0_stop; k_KQ_0 += T_A_KQ::J) {
697 load_ldmatrix(Q_B[0], tile_Q + (threadIdx.y / np)*(T_B_KQ::I*stride_tile_Q) + k_KQ_0, stride_tile_Q);
698
699#pragma unroll
700 for (int i_KQ_00 = 0; i_KQ_00 < nbatch_fa; i_KQ_00 += np*T_A_KQ::I) {
701 const int i_KQ_0 = i_KQ_00 + (threadIdx.y % np)*T_A_KQ::I;
702
703 T_A_KQ K_A;
704 load_ldmatrix_swizzled<stride_tile_K>(K_A, tile_K, i_KQ_0*stride_tile_K + k_KQ_0-k0_start);
705
706 if constexpr (cols_per_warp == 8) {
707 mma(KQ_C[i_KQ_00/(np*T_A_KQ::I)], K_A, Q_B[0]);
708 } else {
709 // Wide version of KQ_C is column-major
710#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
711 // AMD matrix C is column-major.
712 mma(KQ_C[i_KQ_00/(np*T_A_KQ::I)], K_A, Q_B[0]);
713#else
714 // swap A and B for CUDA.
715 mma(KQ_C[i_KQ_00/(np*T_A_KQ::I)], Q_B[0], K_A);
716#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
717 }
718 }
719 }
720 }
721
722 if constexpr (nstages <= 1) {
723 __syncthreads(); // Only needed if tile_K == tile_V.
724 }
725 }
726
727 if (use_logit_softcap) {
728 constexpr int stride = cols_per_warp == 8 ? np*T_C_KQ::I : np*T_C_KQ::J;
729 static_assert(nbatch_fa % stride == 0, "bad loop size");
730#pragma unroll
731 for (int i = 0; i < nbatch_fa/stride; ++i) {
732#pragma unroll
733 for (int l = 0; l < T_C_KQ::ne; ++l) {
734 KQ_C[i].x[l] = logit_softcap*tanhf(KQ_C[i].x[l]);
735 }
736 }
737 }
738
739 float KQ_max_new[cols_per_thread];
740#pragma unroll
741 for (int col = 0; col < cols_per_thread; ++col) {
742 KQ_max_new[col] = KQ_max[col];
743 }
744 float KQ_rowsum_add[cols_per_thread] = {0.0f};
745
746 if constexpr (cols_per_warp == 8) {
747 if (ncols2 > 1 || mask_h) {
748#pragma unroll
749 for (int i00 = 0; i00 < nbatch_fa; i00 += np*T_C_KQ::I) {
750 const int i0 = i00 + (threadIdx.y % np)*T_C_KQ::I;
751#pragma unroll
752 for (int l = 0; l < T_C_KQ::ne; ++l) {
753 const int i = i0 + T_C_KQ::get_i(l);
754 const int j = ((threadIdx.y / np)*T_C_KQ::J + T_C_KQ::get_j(l)) / ncols2;
755
756 KQ_C[i00/(np*T_C_KQ::I)].x[l] += slope * __half2float(tile_mask[j*(nbatch_fa + 8) + i]);
757 }
758 }
759 }
760
761 // Calculate softmax for each KQ column using the current max. value.
762 // The divisor is stored in KQ_rowsum and will be applied at the end.
763 static_assert(nbatch_fa % (np*T_C_KQ::I) == 0, "bad loop size");
764#pragma unroll
765 for (int k0 = 0; k0 < nbatch_fa; k0 += np*T_C_KQ::I) {
766#pragma unroll
767 for (int l = 0; l < T_C_KQ::ne; ++l) {
768 if (!oob_check || k0 + (threadIdx.y % np)*T_C_KQ::I + T_C_KQ::get_i(l) < k_VKQ_sup) {
769#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
770 constexpr int KQ_idx = 0;
771#else
772 // Turing + Volta:
773 const int KQ_idx = l % 2;
774#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
775 KQ_max_new[KQ_idx] = fmaxf(KQ_max_new[KQ_idx], KQ_C[k0/(np*T_C_KQ::I)].x[l] + FATTN_KQ_MAX_OFFSET);
776 }
777 }
778 }
779
780 // Values per KQ column are spread across 8 threads:
781#pragma unroll
782 for (int col = 0; col < cols_per_thread; ++col) {
783#pragma unroll
784 for (int offset = 16; offset >= 4; offset >>= 1) {
785 KQ_max_new[col] = fmaxf(KQ_max_new[col], __shfl_xor_sync(0xFFFFFFFF, KQ_max_new[col], offset, warp_size));
786 }
787 }
788
789 static_assert(nbatch_fa % (np*T_C_KQ::I) == 0, "bad loop size");
790#pragma unroll
791 for (int k0 = 0; k0 < nbatch_fa; k0 += np*T_C_KQ::I) {
792#pragma unroll
793 for (int l = 0; l < T_C_KQ::ne; ++l) {
794 if (!oob_check || k0 + (threadIdx.y % np)*T_C_KQ::I + T_C_KQ::get_i(l) < k_VKQ_sup) {
795#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
796 constexpr int KQ_idx = 0;
797#else
798 // Turing + Volta:
799 const int KQ_idx = l % 2;
800#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
801 KQ_C[k0/(np*T_C_KQ::I)].x[l] = expf(KQ_C[k0/(np*T_C_KQ::I)].x[l] - KQ_max_new[KQ_idx]);
802 KQ_rowsum_add[KQ_idx] += KQ_C[k0/(np*T_C_KQ::I)].x[l];
803 } else {
804 KQ_C[k0/(np*T_C_KQ::I)].x[l] = 0.0f;
805 }
806 }
807 }
808 } else { // not Turing mma or T_B_KQ::I > 8
809 if (ncols2 > 1 || mask_h) {
810#pragma unroll
811 for (int i00 = 0; i00 < nbatch_fa; i00 += np*T_C_KQ::J) {
812 const int i0 = i00 + (threadIdx.y % np)*T_C_KQ::J;
813
814 // The mask is stored as 16 bit half values, loading them as 32 bit half2 values is preferred in terms of speed.
815 // However, this is not possible for RDNA3 where 2 consecutive l indices are not consecutive in the mask memory layout.
816#ifdef RDNA3
817#pragma unroll
818 for (int l = 0; l < T_C_KQ::ne; ++l) {
819 const int i = i0 + T_C_KQ::get_j(l);
820 const int j = ((threadIdx.y / np)*cols_per_warp + T_C_KQ::get_i(l)) / ncols2;
821
822 KQ_C[i00/(np*T_C_KQ::J)].x[l] += __half2float(tile_mask[j*(nbatch_fa + 8) + i]);
823 }
824#else
825#pragma unroll
826 for (int l0 = 0; l0 < T_C_KQ::ne; l0 += 2) {
827 const int i = (i0 + T_C_KQ::get_j(l0)) / 2;
828 const int j = ((threadIdx.y / np)*cols_per_warp + T_C_KQ::get_i(l0)) / ncols2;
829
830 const float2 tmp = __half22float2(((const half2 *)tile_mask)[j*(nbatch_fa/2 + 4) + i]);
831 KQ_C[i00/(np*T_C_KQ::J)].x[l0 + 0] += slope*tmp.x;
832 KQ_C[i00/(np*T_C_KQ::J)].x[l0 + 1] += slope*tmp.y;
833 }
834#endif // RDNA3
835 }
836 }
837
838 // Calculate softmax for each KQ column using the current max. value.
839 // The divisor is stored in KQ_rowsum and will be applied at the end.
840 static_assert(nbatch_fa % (np*T_C_KQ::J) == 0, "bad loop size");
841#pragma unroll
842 for (int k0 = 0; k0 < nbatch_fa; k0 += np*T_C_KQ::J) {
843#pragma unroll
844 for (int l = 0; l < T_C_KQ::ne; ++l) {
845 if (!oob_check || k0 + (threadIdx.y % np)*T_C_KQ::J + T_C_KQ::get_j(l) < k_VKQ_sup) {
846#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
847 constexpr int KQ_idx = 0;
848#else
849 // Turing + Volta:
850 const int KQ_idx = (l/2) % 2;
851#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
852 KQ_max_new[KQ_idx] = fmaxf(KQ_max_new[KQ_idx], KQ_C[(k0/(np*T_C_KQ::J))].x[l] + FATTN_KQ_MAX_OFFSET);
853 }
854 }
855 }
856
857#pragma unroll
858 for (int col = 0; col < cols_per_thread; ++col) {
859#if defined(TURING_MMA_AVAILABLE)
860 // Values per KQ column are spread across 4 threads:
861 constexpr int offset_first = 2;
862 constexpr int offset_last = 1;
863#elif defined(AMD_MFMA_AVAILABLE)
864 // MFMA: 4 threads per Q column (threadIdx.x % 16 == col, spaced by 16).
865 constexpr int offset_first = 32;
866 constexpr int offset_last = 16;
867#elif defined(AMD_WMMA_AVAILABLE)
868 // Values per KQ column are spread across 2 threads:
869 constexpr int offset_first = 16;
870 constexpr int offset_last = 16;
871#else // Volta
872 // Values per KQ column are spread across 2 threads:
873 constexpr int offset_first = 2;
874 constexpr int offset_last = 2;
875#endif // defined(TURING_MMA_AVAILABLE)
876#pragma unroll
877 for (int offset = offset_first; offset >= offset_last; offset >>= 1) {
878 KQ_max_new[col] = fmaxf(KQ_max_new[col], __shfl_xor_sync(0xFFFFFFFF, KQ_max_new[col], offset, warp_size));
879 }
880 }
881
882 static_assert(nbatch_fa % (np*T_C_KQ::J) == 0, "bad loop size");
883#pragma unroll
884 for (int k0 = 0; k0 < nbatch_fa; k0 += np*T_C_KQ::J) {
885#pragma unroll
886 for (int l = 0; l < T_C_KQ::ne; ++l) {
887 if (!oob_check || k0 + (threadIdx.y % np)*T_C_KQ::J + T_C_KQ::get_j(l) < k_VKQ_sup) {
888#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
889 constexpr int KQ_idx = 0;
890#else
891 // Turing + Volta:
892 const int KQ_idx = (l/2) % 2;
893#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
894 KQ_C[(k0/(np*T_C_KQ::J))].x[l] = expf(KQ_C[(k0/(np*T_C_KQ::J))].x[l] - KQ_max_new[KQ_idx]);
895 KQ_rowsum_add[KQ_idx] += KQ_C[(k0/(np*T_C_KQ::J))].x[l];
896 } else {
897 KQ_C[(k0/(np*T_C_KQ::J))].x[l] = 0.0f;
898 }
899 }
900 }
901 }
902
903 {
904 float KQ_max_scale[cols_per_thread];
905#pragma unroll
906 for (int col = 0; col < cols_per_thread; ++col) {
907 const float KQ_max_diff = KQ_max[col] - KQ_max_new[col];
908 KQ_max_scale[col] = expf(KQ_max_diff);
909 KQ_max[col] = KQ_max_new[col];
910
911 *((uint32_t *) &KQ_max_scale[col]) *= KQ_max_diff >= SOFTMAX_FTZ_THRESHOLD;
912
913 // Scale previous KQ_rowsum to account for a potential increase in KQ_max:
914 KQ_rowsum[col] = KQ_max_scale[col]*KQ_rowsum[col] + KQ_rowsum_add[col];
915 }
916
917#if defined(TURING_MMA_AVAILABLE)
918 if constexpr (cols_per_warp == 8) {
919 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[0], KQ_max_scale[cols_per_thread - 1]);
920#pragma unroll
921 for (int i = 0; i < DV/T_C_VKQ::I; ++i) {
922#pragma unroll
923 for (int l = 0; l < T_C_VKQ::ne; ++l) {
924 VKQ_C[i].x[l] *= KQ_max_scale_h2;
925 }
926 }
927 } else {
928#pragma unroll
929 for (int col = 0; col < cols_per_thread; ++col) {
930 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[col], KQ_max_scale[col]);
931#pragma unroll
932 for (int i = 0; i < (DV/2)/T_C_VKQ::J; ++i) {
933#pragma unroll
934 for (int l0 = 0; l0 < T_C_VKQ::ne; l0 += 2) {
935 VKQ_C[i].x[l0 + col] *= KQ_max_scale_h2;
936 }
937 }
938 }
939 }
940#elif defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
941 if constexpr (std::is_same_v<decltype(T_C_VKQ::x), half2[T_C_VKQ::ne]>) {
942 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[0], KQ_max_scale[0]);
943#pragma unroll
944 for (int i = 0; i < (DV/2)/T_C_VKQ::J; ++i) {
945#pragma unroll
946 for (int l = 0; l < T_C_VKQ::ne; ++l) {
947 VKQ_C[i].x[l] *= KQ_max_scale_h2;
948 }
949 }
950 } else {
951 static_assert(std::is_same_v<decltype(T_C_VKQ::x), float[T_C_VKQ::ne]>, "bad VKQ type");
952#pragma unroll
953 for (int i = 0; i < DV/T_C_VKQ::J; ++i) {
954#pragma unroll
955 for (int l = 0; l < T_C_VKQ::ne; ++l) {
956 VKQ_C[i].x[l] *= KQ_max_scale[0];
957 }
958 }
959 }
960#else // Volta
961 const half2 KQ_max_scale_h2 = make_half2(
962 KQ_max_scale[(threadIdx.x / 2) % 2], KQ_max_scale[(threadIdx.x / 2) % 2]);
963#pragma unroll
964 for (int i = 0; i < (DV/2)/T_C_VKQ::J; ++i) {
965#pragma unroll
966 for (int l = 0; l < T_C_VKQ::ne; ++l) {
967 VKQ_C[i].x[l] *= KQ_max_scale_h2;
968 }
969 }
970#endif // defined(TURING_MMA_AVAILABLE)
971 }
972
973 // Convert KQ C tiles into B tiles for VKQ calculation:
974 T_B_VKQ B[nbatch_fa/(np*2*T_B_VKQ::J)];
975 static_assert(nbatch_fa % (np*2*T_B_VKQ::J) == 0, "bad loop size");
976 if constexpr (cols_per_warp == 8) {
977#pragma unroll
978 for (int k = 0; k < nbatch_fa/(np*2*T_B_VKQ::J); ++k) {
979 B[k] = get_transposed(get_half2(KQ_C[k]));
980 }
981 } else {
982 for (int k = 0; k < nbatch_fa/(np*2*T_B_VKQ::J); ++k) {
983 B[k] = get_half2(KQ_C[k]);
984 }
985 }
986
987 if constexpr (nstages > 1) {
988 static_assert(!use_sparse, "sparse gather not implemented for multi-stage loading");
989 static_assert(!V_is_K_view, "K data reuse not implemented multi-stage loading");
990 // Preload K tile for next iteration:
991 constexpr bool use_cp_async = true;
992 cp_async_wait_all();
993 __syncthreads();
994 if (!last_iter) {
995 if (ncols2 > 1 || mask_h) {
996 flash_attn_ext_f16_load_mask<ncols1, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
997 (mask_h, tile_mask, stride_mask, k_VKQ_0 + nbatch_fa, k_VKQ_sup, jt*ncols1, ne01, nullptr);
998 }
999 flash_attn_ext_f16_load_tile<stride_tile_K, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
1000 (K_h2, tile_K, nbatch_K2, stride_K, k_VKQ_0 + nbatch_fa, k_VKQ_sup, indices);
1001 }
1002 }
1003
1004
1005 // Calculate VKQ tile, need to use logical rather than physical elements for i0 due to transposition of V:
1006#pragma unroll
1007 for (int i0_start = 0; i0_start < DV; i0_start += 2*nbatch_V2) {
1008 static_assert(DV % (2*nbatch_V2) == 0, "bad loop size");
1009 const int i0_stop = i0_start + 2*nbatch_V2;
1010
1011 if constexpr (nstages <= 1) {
1012 const int i0_diff = i0_stop - i0_start;
1013 if (!V_is_K_view || i0_stop > 2*nbatch_K2) {
1014 constexpr bool use_cp_async = nstages == 1;
1015 flash_attn_ext_f16_load_tile<stride_tile_V, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
1016 (V_h2 + i0_start/2, tile_V, i0_diff/2, stride_V, k_VKQ_0, k_VKQ_sup, indices);
1017 if (use_cp_async) {
1018 cp_async_wait_all();
1019 }
1020 __syncthreads();
1021 }
1022 }
1023 const int tile_V_offset_i = !V_is_K_view || i0_stop > 2*nbatch_K2 ? 0 : i0_start/2;
1024
1025#if defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1026#pragma unroll
1027 for (int i_VKQ_0 = i0_start; i_VKQ_0 < i0_stop; i_VKQ_0 += T_A_VKQ::I) {
1028 static_assert((nbatch_fa/2) % (np*T_A_VKQ::J) == 0, "bad loop size");
1029#pragma unroll
1030 for (int k00 = 0; k00 < nbatch_fa/2; k00 += np*T_A_VKQ::J) {
1031 const int k0 = k00 + (threadIdx.y % np)*T_A_VKQ::J;
1032
1033 T_A_VKQ A; // Transposed in SRAM but not in registers, gets transposed on load.
1034 load_ldmatrix_trans_swizzled<stride_tile_V>(A, tile_V, tile_V_offset_i + 2*k0*stride_tile_V + (i_VKQ_0 - i0_start)/2);
1035 if constexpr (T_B_KQ::I == 8) {
1036 mma(VKQ_C[i_VKQ_0/T_A_VKQ::I], A, B[k00/(np*T_A_VKQ::J)]);
1037 } else {
1038 // Wide version of VKQ_C is column-major.
1039#if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1040 // AMD matrix C is column-major.
1041 mma(VKQ_C[i_VKQ_0/T_A_VKQ::I], A, B[k00/(np*T_A_VKQ::J)]);
1042#else
1043 // swap A and B for CUDA.
1044 mma(VKQ_C[i_VKQ_0/T_A_VKQ::I], B[k00/(np*T_A_VKQ::J)], A);
1045#endif // defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1046 }
1047 }
1048 }
1049#else // Volta
1050 constexpr int i0_stride = 2*T_C_VKQ::J;
1051#pragma unroll
1052 for (int i_VKQ_0 = i0_start; i_VKQ_0 < i0_stop; i_VKQ_0 += i0_stride) {
1053 static_assert(nbatch_fa % (np*T_A_VKQ::I) == 0, "bad loop size");
1054 static_assert(2*T_B_VKQ::J == T_A_VKQ::I, "bad tile sizes");
1055#pragma unroll
1056 for (int k00 = 0; k00 < nbatch_fa; k00 += np*T_A_VKQ::I) {
1057 const int k0 = k00 + (threadIdx.y % np)*T_A_VKQ::I;
1058
1059 T_A_VKQ A; // Transposed in both SRAM and registers, load normally.
1060 load_ldmatrix_swizzled<stride_tile_V>(A, tile_V, tile_V_offset_i + k0*stride_tile_V + (i_VKQ_0 - i0_start)/2);
1061 mma(VKQ_C[i_VKQ_0/i0_stride], B[k00/(np*T_A_VKQ::I)], A);
1062 }
1063 }
1064#endif // defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1065
1066 if constexpr (nstages <= 1) {
1067 __syncthreads(); // Only needed if tile_K == tile_V.
1068 }
1069 }
1070#else
1071 GGML_UNUSED_VARS(Q_f2, K_h2, V_h2, mask_h, indices, dstk, dstk_fixup,
1072 scale, slope, logit_softcap, ne01, ne02,
1073 stride_K, stride_V, stride_mask,
1074 tile_Q, tile_K, tile_V, tile_mask,
1075 Q_B, VKQ_C, KQ_max, KQ_rowsum, kb0);
1076 NO_DEVICE_CODE;
1077#endif // defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1078}
1079
1080#if defined(TURING_MMA_AVAILABLE)
1081template<int DKQ, int ncols> struct mma_tile_sizes {
1082 using T_A_KQ = tile<16, 8, half2>; // row-major
1083 using T_B_KQ = tile<16, 8, half2>; // column-major
1084 using T_C_KQ = tile<16, 16, float>; // column-major
1085 using T_A_VKQ = tile<16, 8, half2>; // row-major
1086 using T_B_VKQ = tile<16, 8, half2>; // column-major
1087 using T_C_VKQ = tile<16, 8, half2>; // column-major
1088};
1089// If there are only 8 columns, use thinner B tiles to avoid wasting compute:
1090template<int DKQ> struct mma_tile_sizes<DKQ, 8> {
1091 using T_A_KQ = tile<16, 8, half2>; // row-major
1092 using T_B_KQ = tile< 8, 8, half2>; // column-major
1093 using T_C_KQ = tile<16, 8, float>; // row-major
1094 using T_A_VKQ = tile<16, 8, half2>; // row-major
1095 using T_B_VKQ = tile< 8, 8, half2>; // column-major
1096 using T_C_VKQ = tile<16, 4, half2>; // row-major
1097};
1098// For very large head sizes, use thinner B tiles to reduce register pressure:
1099template<> struct mma_tile_sizes<256, 16> {
1100 using T_A_KQ = tile<16, 8, half2>; // row-major
1101 using T_B_KQ = tile< 8, 8, half2>; // column-major
1102 using T_C_KQ = tile<16, 8, float>; // row-major
1103 using T_A_VKQ = tile<16, 8, half2>; // row-major
1104 using T_B_VKQ = tile< 8, 8, half2>; // column-major
1105 using T_C_VKQ = tile<16, 4, half2>; // row-major
1106};
1107template<> struct mma_tile_sizes<512, 16> {
1108 using T_A_KQ = tile<16, 8, half2>; // row-major
1109 using T_B_KQ = tile< 8, 8, half2>; // column-major
1110 using T_C_KQ = tile<16, 8, float>; // row-major
1111 using T_A_VKQ = tile<16, 8, half2>; // row-major
1112 using T_B_VKQ = tile< 8, 8, half2>; // column-major
1113 using T_C_VKQ = tile<16, 4, half2>; // row-major
1114};
1115template<> struct mma_tile_sizes<512, 32> {
1116 using T_A_KQ = tile<16, 8, half2>; // row-major
1117 using T_B_KQ = tile< 8, 8, half2>; // column-major
1118 using T_C_KQ = tile<16, 8, float>; // row-major
1119 using T_A_VKQ = tile<16, 8, half2>; // row-major
1120 using T_B_VKQ = tile< 8, 8, half2>; // column-major
1121 using T_C_VKQ = tile<16, 4, half2>; // row-major
1122};
1123#elif defined(AMD_WMMA_AVAILABLE)
1124#ifdef RDNA3
1125template<int DKQ, int ncols> struct mma_tile_sizes {
1126 using T_A_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1127 using T_B_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // column-major
1128 using T_C_KQ = tile<16, 16, float, DATA_LAYOUT_I_MAJOR>; // column-major
1129 using T_A_VKQ = tile<32, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1130 using T_B_VKQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // column-major
1131 using T_C_VKQ = tile<16, 16, half2, DATA_LAYOUT_I_MAJOR>; // column-major
1132};
1133template<int ncols> struct mma_tile_sizes<80, ncols> {
1134 using T_A_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1135 using T_B_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // column-major
1136 using T_C_KQ = tile<16, 16, float, DATA_LAYOUT_I_MAJOR>; // column-major
1137 using T_A_VKQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1138 using T_B_VKQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // column-major
1139 using T_C_VKQ = tile<16, 16, float, DATA_LAYOUT_I_MAJOR>; // column-major
1140};
1141template<int ncols> struct mma_tile_sizes<112, ncols> {
1142 using T_A_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1143 using T_B_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // column-major
1144 using T_C_KQ = tile<16, 16, float, DATA_LAYOUT_I_MAJOR>; // column-major
1145 using T_A_VKQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1146 using T_B_VKQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // column-major
1147 using T_C_VKQ = tile<16, 16, float, DATA_LAYOUT_I_MAJOR>; // column-major
1148};
1149#else
1150template<int DKQ, int ncols> struct mma_tile_sizes {
1151 using T_A_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR>; // row-major
1152 using T_B_KQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR>; // column-major
1153 using T_C_KQ = tile<16, 16, float, DATA_LAYOUT_I_MAJOR>; // column-major
1154 using T_A_VKQ = tile<32, 8, half2, DATA_LAYOUT_I_MAJOR>; // row-major
1155 using T_B_VKQ = tile<16, 8, half2, DATA_LAYOUT_I_MAJOR>; // column-major
1156 using T_C_VKQ = tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED>; // column-major
1157};
1158template<int ncols> struct mma_tile_sizes<80, ncols> {
1159 using T_A_KQ = tile<16, 8, half2>; // row-major
1160 using T_B_KQ = tile<16, 8, half2>; // column-major
1161 using T_C_KQ = tile<16, 16, float>; // column-major
1162 using T_A_VKQ = tile<16, 8, half2>; // row-major
1163 using T_B_VKQ = tile<16, 8, half2>; // column-major
1164 using T_C_VKQ = tile<16, 8, half2>; // column-major
1165};
1166template<int ncols> struct mma_tile_sizes<112, ncols> {
1167 using T_A_KQ = tile<16, 8, half2>; // row-major
1168 using T_B_KQ = tile<16, 8, half2>; // column-major
1169 using T_C_KQ = tile<16, 16, float>; // column-major
1170 using T_A_VKQ = tile<16, 8, half2>; // row-major
1171 using T_B_VKQ = tile<16, 8, half2>; // column-major
1172 using T_C_VKQ = tile<16, 8, half2>; // column-major
1173};
1174#endif // RDNA3
1175#elif defined(AMD_MFMA_AVAILABLE)
1176template<int DKQ, int ncols> struct mma_tile_sizes {
1177 using T_A_KQ = tile<16, 8, half2>; // row-major
1178 using T_B_KQ = tile<16, 8, half2>; // column-major
1179 using T_C_KQ = tile<16, 16, float>; // column-major
1180 using T_A_VKQ = tile<16, 8, half2>; // row-major
1181 using T_B_VKQ = tile<16, 8, half2>; // column-major
1182 using T_C_VKQ = tile<16, 16, float>; // column-major
1183};
1184#else // Volta
1185template<int DKQ, int ncols> struct mma_tile_sizes {
1186 using T_A_KQ = tile< 8, 4, half2, DATA_LAYOUT_I_MAJOR_MIRRORED>; // row-major
1187 using T_B_KQ = tile<32, 4, half2, DATA_LAYOUT_I_MAJOR>; // column-major
1188 using T_C_KQ = tile<32, 8, float, DATA_LAYOUT_I_MAJOR>; // column-major
1189 using T_A_VKQ = tile< 8, 4, half2, DATA_LAYOUT_J_MAJOR_MIRRORED>; // column-major
1190 using T_B_VKQ = tile<32, 4, half2, DATA_LAYOUT_I_MAJOR>; // column-major
1191 using T_C_VKQ = tile<32, 4, half2, DATA_LAYOUT_I_MAJOR>; // column-major
1192};
1193#endif // defined(TURING_MMA_AVAILABLE)
1194
1195template<int DKQ, int DV, int ncols1, int ncols2, int nwarps, bool use_logit_softcap, bool V_is_K_view, bool use_sparse, bool needs_fixup, bool is_fixup>
1196static __device__ __forceinline__ void flash_attn_ext_f16_process_tile(
1197 const float2 * const __restrict__ Q_f2,
1198 const half2 * const __restrict__ K_h2,
1199 const half2 * const __restrict__ V_h2,
1200 const half * const __restrict__ mask_h,
1201 const int32_t * const __restrict__ indices,
1202 const float * const __restrict__ sinks_f,
1203 float2 * const __restrict__ dstk,
1204 float2 * const __restrict__ dstk_fixup,
1205 const float scale,
1206 const float slope,
1207 const float logit_softcap,
1208 const uint3 ne01,
1209 const int ne02,
1210 const int gqa_ratio,
1211 const int ne11,
1212 const int stride_Q1,
1213 const int stride_Q2,
1214 const int stride_K,
1215 const int stride_V,
1216 const int stride_mask,
1217 const int jt,
1218 const int zt_gqa,
1219 const int kb0_start,
1220 const int kb0_stop) {
1221#if defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1222 //In this kernel Q, K, V are matrices while i, j, k are matrix indices.
1223
1224 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
1225 constexpr int ncols = ncols1 * ncols2;
1226 using T_A_KQ = typename mma_tile_sizes<DKQ, ncols>::T_A_KQ;
1227 using T_B_KQ = typename mma_tile_sizes<DKQ, ncols>::T_B_KQ;
1228 using T_C_KQ = typename mma_tile_sizes<DKQ, ncols>::T_C_KQ;
1229 using T_A_VKQ = typename mma_tile_sizes<DKQ, ncols>::T_A_VKQ;
1230 using T_B_VKQ = typename mma_tile_sizes<DKQ, ncols>::T_B_VKQ;
1231 using T_C_VKQ = typename mma_tile_sizes<DKQ, ncols>::T_C_VKQ;
1232
1233 constexpr int cols_per_warp = T_B_KQ::I;
1234 constexpr int cols_per_thread = get_cols_per_thread();
1235 constexpr int np = cols_per_warp > ncols ? nwarps : nwarps * cols_per_warp/ncols; // Number of parallel CUDA warps per Q column.
1236 constexpr int nbatch_fa = ggml_cuda_fattn_mma_get_nbatch_fa (DKQ, DV, ncols);
1237 constexpr int nbatch_K2 = ggml_cuda_fattn_mma_get_nbatch_K2 (DKQ, DV, ncols);
1238 constexpr int nbatch_V2 = ggml_cuda_fattn_mma_get_nbatch_V2 (DKQ, DV, ncols);
1239 constexpr int nbatch_combine = ggml_cuda_fattn_mma_get_nbatch_combine(DKQ, DV, ncols);
1240 constexpr bool Q_in_reg = ggml_cuda_fattn_mma_get_Q_in_reg (DKQ, DV, ncols);
1241 constexpr int nstages = ggml_cuda_fattn_mma_get_nstages (DKQ, DV, ncols1, ncols2, use_sparse);
1242
1243 if (cols_per_warp > ncols) {
1244 NO_DEVICE_CODE;
1245 return;
1246 }
1247
1248 static_assert(nwarps * (cols_per_warp/ncols2) % ncols1 == 0, "bad nwarps");
1249
1250 constexpr bool swz = ggml_cuda_fattn_mma_get_swizzled(DKQ, DV, ncols);
1251 constexpr int stride_tile_Q = DKQ/2 + 4;
1252 constexpr int stride_tile_K = swz ? nbatch_K2 : nbatch_K2 + 4;
1253 constexpr int stride_tile_V = V_is_K_view ? stride_tile_K : (swz ? nbatch_V2 : nbatch_V2 + 4);
1254 constexpr int stride_tile_KV_max = stride_tile_K > stride_tile_V ? stride_tile_K : stride_tile_V;
1255
1256 extern __shared__ half2 tile_Q[];
1257 half2 * tile_K = Q_in_reg ? tile_Q : tile_Q + ncols * stride_tile_Q;
1258 half2 * tile_V = nstages > 1 ? tile_K + nbatch_fa * stride_tile_K : tile_K;
1259 half * tile_mask = (half *) (nstages > 1 ? tile_V + nbatch_fa * stride_tile_V : tile_V + nbatch_fa * stride_tile_KV_max);
1260
1261 T_B_KQ Q_B[(Q_in_reg ? DKQ/(2*T_B_KQ::J) : 1)];
1262#if defined(TURING_MMA_AVAILABLE)
1263 T_C_VKQ VKQ_C[cols_per_warp == 8 ? DV/T_C_VKQ::I : DV/(2*T_C_VKQ::J)];
1264#elif defined(AMD_WMMA_256B_AVAILABLE)
1265 T_C_VKQ VKQ_C[DV % 32 != 0 ? DV/T_C_VKQ::J : DV/(2*T_C_VKQ::J)];
1266#elif defined(AMD_MFMA_AVAILABLE)
1267 T_C_VKQ VKQ_C[ DV/T_C_VKQ::J];
1268#elif defined(AMD_WMMA_AVAILABLE)
1269 T_C_VKQ VKQ_C[ DV/(2*T_C_VKQ::J)];
1270#else // Volta
1271 T_C_VKQ VKQ_C[ DV/(2*T_C_VKQ::J)];
1272#endif // defined(TURING_MMA_AVAILABLE)
1273
1274 float KQ_rowsum[cols_per_thread] = {0.0f};
1275 float KQ_max[cols_per_thread];
1276#pragma unroll
1277 for (int col = 0; col < cols_per_thread; ++col) {
1278 KQ_max[col] = -FLT_MAX/2.0f;
1279 }
1280
1281 // Load Q data into tile_Q, either temporarily or permanently.
1282 // Q in registers is faster, but register pressure is the biggest bottleneck.
1283 // The loading is done with decreasing granularity for D for better memory bandwidth.
1284 const half2 scale_h2 = make_half2(scale, scale);
1285#pragma unroll
1286 for (int stride_k : {warp_size, warp_size/2, warp_size/4, warp_size/8}) {
1287 const int k0_start = stride_k == warp_size ? 0 : DKQ/2 - (DKQ/2) % (2*stride_k);
1288 const int k0_stop = DKQ/2 - (DKQ/2) % (1*stride_k);
1289 const int stride_jc = warp_size / stride_k;
1290
1291 if (k0_start == k0_stop) {
1292 continue;
1293 }
1294
1295#pragma unroll
1296 for (int jc0 = 0; jc0 < ncols; jc0 += nwarps*stride_jc) {
1297 const int jc = jc0 + threadIdx.y*stride_jc + (stride_k == warp_size ? 0 : threadIdx.x / stride_k);
1298
1299 if (jc0 + nwarps*stride_jc > ncols && jc >= ncols) {
1300 break;
1301 }
1302
1303 const int j = jc / ncols2;
1304 const int c = jc % ncols2;
1305
1306 if ((ncols1 == 1 || jt*ncols1 + j < int(ne01.z)) && (ncols2 == 1 || zt_gqa*ncols2 + c < gqa_ratio)) {
1307#pragma unroll
1308 for (int k0 = k0_start; k0 < k0_stop; k0 += stride_k) {
1309 const int k = k0 + (stride_k == warp_size ? threadIdx.x : threadIdx.x % stride_k);
1310
1311 const float2 tmp = Q_f2[(jt*ncols1 + j)*stride_Q1 + c*stride_Q2 + k];
1312 tile_Q[jc*stride_tile_Q + k] = scale_h2 * make_half2(tmp.x, tmp.y);
1313 }
1314 } else {
1315#pragma unroll
1316 for (int k0 = k0_start; k0 < k0_stop; k0 += stride_k) {
1317 const int k = k0 + (stride_k == warp_size ? threadIdx.x : threadIdx.x % stride_k);
1318
1319 tile_Q[jc*stride_tile_Q + k] = make_half2(0.0f, 0.0f);
1320 }
1321 }
1322 }
1323 }
1324
1325 __syncthreads();
1326
1327 if (Q_in_reg) {
1328 const int j0 = (threadIdx.y / np) * cols_per_warp;
1329
1330#pragma unroll
1331 for (int k0 = 0; k0 < DKQ/2; k0 += T_B_KQ::J) {
1332 load_ldmatrix(Q_B[k0/T_B_KQ::J], tile_Q + j0*stride_tile_Q + k0, stride_tile_Q);
1333 }
1334 }
1335
1336 __syncthreads();
1337
1338 int kb0 = kb0_start;
1339
1340 // Preload mask and K data for first iteration when using cp_async with multiple stages:
1341 if constexpr (nstages > 1) {
1342 static_assert(!use_sparse, "sparse gather not implemented for multi-stage loading");
1343 static_assert(nbatch_K2 == DKQ/2, "batching not implemented for multi-stage pipeline");
1344 constexpr bool use_cp_async = true;
1345 constexpr bool oob_check = false;
1346 constexpr int k_VKQ_sup = nbatch_fa;
1347 if (ncols2 > 1 || mask_h) {
1348 flash_attn_ext_f16_load_mask<ncols1, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
1349 (mask_h, tile_mask, stride_mask, kb0*nbatch_fa, k_VKQ_sup, jt*ncols1, ne01, nullptr);
1350 }
1351 flash_attn_ext_f16_load_tile<stride_tile_K, nwarps, nbatch_fa, use_cp_async, oob_check, use_sparse>
1352 (K_h2, tile_K, nbatch_K2, stride_K, kb0*nbatch_fa, k_VKQ_sup, indices);
1353 }
1354
1355 // kb0_start is always < kb0_stop so the last iter can be executed unconditionally.
1356 if constexpr (ncols2 == 1 || use_sparse) {
1357 constexpr bool oob_check = true;
1358 for (; kb0 < kb0_stop-1; ++kb0) {
1359 constexpr bool last_iter = false;
1360 constexpr int k_VKQ_sup = nbatch_fa;
1361 flash_attn_ext_f16_iter
1362 <DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup, last_iter, oob_check,
1363 T_A_KQ, T_B_KQ, T_C_KQ, T_A_VKQ, T_B_VKQ, T_C_VKQ>
1364 (Q_f2, K_h2, V_h2, mask_h, indices, dstk, dstk_fixup, scale, slope, logit_softcap,
1365 ne01, ne02, stride_K, stride_V, stride_mask, tile_Q, tile_K, tile_V, tile_mask, Q_B, VKQ_C,
1366 KQ_max, KQ_rowsum, jt, kb0, k_VKQ_sup);
1367 }
1368 constexpr bool last_iter = true;
1369 const int k_VKQ_sup = ne11 - kb0*nbatch_fa;
1370 flash_attn_ext_f16_iter
1371 <DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup, last_iter, oob_check,
1372 T_A_KQ, T_B_KQ, T_C_KQ, T_A_VKQ, T_B_VKQ, T_C_VKQ>
1373 (Q_f2, K_h2, V_h2, mask_h, indices, dstk, dstk_fixup, scale, slope, logit_softcap,
1374 ne01, ne02, stride_K, stride_V, stride_mask, tile_Q, tile_K, tile_V, tile_mask, Q_B, VKQ_C,
1375 KQ_max, KQ_rowsum, jt, kb0, k_VKQ_sup);
1376 } else {
1377 constexpr bool oob_check = false;
1378 for (; kb0 < kb0_stop-1; ++kb0) {
1379 constexpr bool last_iter = false;
1380 constexpr int k_VKQ_sup = nbatch_fa;
1381 flash_attn_ext_f16_iter
1382 <DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup, last_iter, oob_check,
1383 T_A_KQ, T_B_KQ, T_C_KQ, T_A_VKQ, T_B_VKQ, T_C_VKQ>
1384 (Q_f2, K_h2, V_h2, mask_h, indices, dstk, dstk_fixup, scale, slope, logit_softcap,
1385 ne01, ne02, stride_K, stride_V, stride_mask, tile_Q, tile_K, tile_V, tile_mask, Q_B, VKQ_C,
1386 KQ_max, KQ_rowsum, jt, kb0, k_VKQ_sup);
1387 }
1388 constexpr bool last_iter = true;
1389 constexpr int k_VKQ_sup = nbatch_fa;
1390 flash_attn_ext_f16_iter
1391 <DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup, last_iter, oob_check,
1392 T_A_KQ, T_B_KQ, T_C_KQ, T_A_VKQ, T_B_VKQ, T_C_VKQ>
1393 (Q_f2, K_h2, V_h2, mask_h, indices, dstk, dstk_fixup, scale, slope, logit_softcap,
1394 ne01, ne02, stride_K, stride_V, stride_mask, tile_Q, tile_K, tile_V, tile_mask, Q_B, VKQ_C,
1395 KQ_max, KQ_rowsum, jt, kb0, k_VKQ_sup);
1396 }
1397
1398 // With multi-stage loading there is no __syncthreads at the end of the iter,
1399 // there can be a race condition on shared memory access for combining/writing back results.
1400 if constexpr (nstages > 1 && nwarps*cols_per_warp > nbatch_fa) {
1401 __syncthreads();
1402 }
1403
1404 // Finally, sum up partial KQ rowsums.
1405 {
1406#if defined(TURING_MMA_AVAILABLE)
1407 // The partial sums are spread across 8/4 threads.
1408 constexpr int offset_first = cols_per_warp == 8 ? 16 : 2;
1409 constexpr int offset_last = cols_per_warp == 8 ? 4 : 1;
1410#elif defined(AMD_MFMA_AVAILABLE)
1411 // The partial sums are spread across 4 threads (wavefront64, 16 cols).
1412 constexpr int offset_first = 32;
1413 constexpr int offset_last = 16;
1414#elif defined(AMD_WMMA_AVAILABLE)
1415 // The partial sums are spread across 2 threads.
1416 constexpr int offset_first = 16;
1417 constexpr int offset_last = 16;
1418#else // Volta
1419 // The partial sums are spread across 2 threads.
1420 constexpr int offset_first = 2;
1421 constexpr int offset_last = 2;
1422#endif // defined(TURING_MMA_AVAILABLE)
1423#pragma unroll
1424 for (int col = 0; col < cols_per_thread; ++col) {
1425#pragma unroll
1426 for (int offset = offset_first; offset >= offset_last; offset >>= 1) {
1427 KQ_rowsum[col] += __shfl_xor_sync(0xFFFFFFFF, KQ_rowsum[col], offset, warp_size);
1428 }
1429 }
1430 }
1431
1432 // If attention sinks are used, potentially re-scale if KQ_max is small.
1433 // Also add the sink as a value to KQ_rowsum, this is done after synchronization of KQ_rowsum
1434 // so it's being done unconditionally for every thread.
1435 if (!is_fixup && (np == 1 || threadIdx.y % np == 0) && sinks_f) {
1436 float KQ_max_scale[cols_per_thread];
1437#pragma unroll
1438 for (int col = 0; col < cols_per_thread; ++col) {
1439 const int jc = (threadIdx.y/np)*cols_per_warp + (cols_per_warp == 8 ? T_C_KQ::get_j(col) : T_C_KQ::get_i(2*col));
1440 const float sink = sinks_f[jc % ncols2];
1441
1442 const float KQ_max_new = fmaxf(KQ_max[col], sink);
1443 const float KQ_max_diff = KQ_max[col] - KQ_max_new;
1444 KQ_max_scale[col] = expf(KQ_max_diff);
1445 KQ_max[col] = KQ_max_new;
1446
1447 *((uint32_t *) &KQ_max_scale[col]) *= KQ_max_diff >= SOFTMAX_FTZ_THRESHOLD;
1448
1449 const float KQ_max_add = expf(sink - KQ_max_new);
1450 KQ_rowsum[col] = KQ_max_scale[col]*KQ_rowsum[col] + KQ_max_add;
1451 }
1452
1453#if defined(TURING_MMA_AVAILABLE)
1454 if constexpr (cols_per_warp == 8) {
1455 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[0], KQ_max_scale[cols_per_thread - 1]);
1456#pragma unroll
1457 for (int i = 0; i < DV/T_C_VKQ::I; ++i) {
1458#pragma unroll
1459 for (int l = 0; l < T_C_VKQ::ne; ++l) {
1460 VKQ_C[i].x[l] *= KQ_max_scale_h2;
1461 }
1462 }
1463 } else {
1464#pragma unroll
1465 for (int col = 0; col < cols_per_thread; ++col) {
1466 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[col], KQ_max_scale[col]);
1467#pragma unroll
1468 for (int i = 0; i < (DV/2)/T_C_VKQ::J; ++i) {
1469#pragma unroll
1470 for (int l0 = 0; l0 < T_C_VKQ::ne; l0 += 2) {
1471 VKQ_C[i].x[l0 + col] *= KQ_max_scale_h2;
1472 }
1473 }
1474 }
1475 }
1476#elif defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1477 if constexpr (std::is_same_v<decltype(T_C_VKQ::x), half2[T_C_VKQ::ne]>) {
1478 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[0], KQ_max_scale[0]);
1479#pragma unroll
1480 for (int i = 0; i < (DV/2)/T_C_VKQ::J; ++i) {
1481#pragma unroll
1482 for (int l = 0; l < T_C_VKQ::ne; ++l) {
1483 VKQ_C[i].x[l] *= KQ_max_scale_h2;
1484 }
1485 }
1486 } else {
1487 static_assert(std::is_same_v<decltype(T_C_VKQ::x), float[T_C_VKQ::ne]>, "bad VKQ type");
1488#pragma unroll
1489 for (int i = 0; i < DV/T_C_VKQ::J; ++i) {
1490#pragma unroll
1491 for (int l = 0; l < T_C_VKQ::ne; ++l) {
1492 VKQ_C[i].x[l] *= KQ_max_scale[0];
1493 }
1494 }
1495 }
1496#else // Volta
1497 const int col = (threadIdx.x / 2) % 2;
1498 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale[col], KQ_max_scale[col]);
1499#pragma unroll
1500 for (int i = 0; i < (DV/2)/T_C_VKQ::J; ++i) {
1501#pragma unroll
1502 for (int l = 0; l < T_C_VKQ::ne; ++l) {
1503 VKQ_C[i].x[l] *= KQ_max_scale_h2;
1504 }
1505 }
1506#endif // defined(TURING_MMA_AVAILABLE)
1507 }
1508
1509 // Combine VKQ accumulator values if np > 1.
1510 // It's also faster to do small writes to shared memory, then large write to VRAM than to do small writes to VRAM.
1511 // So also write VKQ accumulators to shared memory in column-major format if np == 1.
1512
1513 constexpr int tile_stride = nbatch_combine + 4;
1514 static_assert((DV/2) % nbatch_combine == 0, "bad nbatch_combine");
1515
1516 if constexpr (cols_per_warp == 8) {
1517 const int jc_cwmo = (threadIdx.x % (2*T_C_VKQ::J)) / T_C_VKQ::J; // jc combine write meta offset
1518 const int jc_cwm = threadIdx.y*(2*T_C_VKQ::J) + 2*T_C_VKQ::get_j(-1) + jc_cwmo; // jc combine write meta
1519 const float2 KQ_cmr = make_float2(KQ_max[jc_cwmo], KQ_rowsum[jc_cwmo]); // KQ combine max rowsum
1520
1521 if constexpr (swz) {
1522 __syncthreads();
1523 }
1524
1525 if (((!needs_fixup && !is_fixup) || np > 1) && threadIdx.x < 2*T_C_VKQ::J) {
1526 // Use the 16 bytes of padding in each row to store the meta data: KQ max, KQ rowsum, KQ max scale.
1527 ((float2 *) tile_Q)[jc_cwm*(tile_stride/2) + nbatch_combine/2] = KQ_cmr;
1528 }
1529
1530 __syncthreads();
1531
1532 if (np == 1) {
1533 // No combination is needed, the meta data can be directly written from registers to VRAM.
1534 if (needs_fixup && threadIdx.x < T_B_KQ::I) {
1535 float2 * dstk_fixup_meta = dstk_fixup + blockIdx.x*ncols;
1536 dstk_fixup_meta[jc_cwm] = KQ_cmr;
1537 }
1538 if (is_fixup && threadIdx.x < T_B_KQ::I) {
1539 float2 * dstk_fixup_meta = dstk_fixup + (gridDim.x + blockIdx.x)*ncols;
1540 dstk_fixup_meta[jc_cwm] = KQ_cmr;
1541 }
1542 }
1543 } else {
1544 // jc_cwm = jc combine write meta
1545 // KQ_cmr = KQ combine max rowsum
1546 // Use the 16 bytes of padding in each Q column to store the meta data: KQ max, KQ rowsum, KQ max scale.
1547#if defined(TURING_MMA_AVAILABLE)
1548 const int jc_cwm = threadIdx.y*cols_per_warp + T_C_VKQ::get_i(threadIdx.x % 4);
1549 const float2 KQ_cmr = make_float2(KQ_max[threadIdx.x % cols_per_thread], KQ_rowsum[threadIdx.x % cols_per_thread]);
1550 const bool thread_should_write = threadIdx.x % 4 < cols_per_thread;
1551#elif defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1552 const int jc_cwm = threadIdx.y*cols_per_warp + T_C_VKQ::get_i(0);
1553 const float2 KQ_cmr = make_float2(KQ_max[0], KQ_rowsum[0]);
1554 const bool thread_should_write = threadIdx.x / 16 < cols_per_thread;
1555#else // Volta
1556 const int jc_cwm = threadIdx.y*cols_per_warp + T_C_KQ::get_i(threadIdx.x & 2);
1557 const float2 KQ_cmr = make_float2(KQ_max[(threadIdx.x & 2) / 2], KQ_rowsum[(threadIdx.x & 2) / 2]);
1558 const bool thread_should_write = T_C_KQ::J == 8 || T_C_KQ::get_j(threadIdx.x & 2) < 8;
1559#endif // defined(TURING_MMA_AVAILABLE)
1560
1561 if constexpr (swz) {
1562 __syncthreads();
1563 }
1564
1565 if (((!needs_fixup && !is_fixup) || np > 1) && thread_should_write) {
1566 ((float2 *) tile_Q)[jc_cwm*(tile_stride/2) + nbatch_combine/2] = KQ_cmr;
1567 }
1568
1569 __syncthreads();
1570
1571 if (np == 1) {
1572 // No combination is needed, the meta data can be directly written from registers to VRAM.
1573 if (needs_fixup && thread_should_write) {
1574 float2 * dstk_fixup_meta = dstk_fixup + blockIdx.x*ncols;
1575 dstk_fixup_meta[jc_cwm] = KQ_cmr;
1576 }
1577 if (is_fixup && thread_should_write) {
1578 float2 * dstk_fixup_meta = dstk_fixup + (gridDim.x + blockIdx.x)*ncols;
1579 dstk_fixup_meta[jc_cwm] = KQ_cmr;
1580 }
1581 }
1582 }
1583
1584 if (np > 1) {
1585 constexpr int nmeta = np*cols_per_warp >= warp_size ? np*cols_per_warp/warp_size : 1;
1586
1587 float KQ_cmn;
1588 float KQ_cms[nmeta];
1589 float KQ_crs;
1590
1591 const int jc_meta = threadIdx.y*cols_per_warp + (np*cols_per_warp < warp_size ? threadIdx.x % (np*cols_per_warp) : threadIdx.x);
1592 float2 * const meta_ptr = ((float2 *) tile_Q) + jc_meta*(tile_stride/2) + nbatch_combine/2;
1593
1594 if (threadIdx.y % np == 0) {
1595 // Combine the meta data for parallel warps via shared memory.
1596 float2 meta[nmeta];
1597#pragma unroll
1598 for (int imeta = 0; imeta < nmeta; ++imeta) {
1599 meta[imeta] = meta_ptr[imeta * warp_size * tile_stride/2];
1600 }
1601
1602 KQ_cmn = meta[0].x; // KQ combine max new, max between all parallel warps.
1603#pragma unroll
1604 for (int imeta = 1; imeta < nmeta; ++imeta) {
1605 KQ_cmn = fmaxf(KQ_cmn, meta[imeta].x);
1606 }
1607#pragma unroll
1608 for (int offset = np*cols_per_warp/2; offset >= cols_per_warp; offset >>= 1) {
1609 if (offset < warp_size) {
1610 KQ_cmn = fmaxf(KQ_cmn, __shfl_xor_sync(0xFFFFFFFF, KQ_cmn, offset, warp_size));
1611 }
1612 }
1613
1614#pragma unroll
1615 for (int imeta = 0; imeta < nmeta; ++imeta) {
1616 KQ_cms[imeta] = expf(meta[imeta].x - KQ_cmn);
1617 }
1618
1619 KQ_crs = KQ_cms[0]*meta[0].y; // KQ combine rowsum, scaled sum of all parallel warps.
1620#pragma unroll
1621 for (int imeta = 1; imeta < nmeta; ++imeta) {
1622 KQ_crs += KQ_cms[imeta]*meta[imeta].y;
1623 }
1624#pragma unroll
1625 for (int offset = np*cols_per_warp/2; offset >= cols_per_warp; offset >>= 1) {
1626 if (offset < warp_size) {
1627 KQ_crs += __shfl_xor_sync(0xFFFFFFFF, KQ_crs, offset, warp_size);
1628 }
1629 }
1630 }
1631
1632 __syncthreads();
1633
1634 if (threadIdx.y % np == 0) {
1635 // Write back combined meta data:
1636#pragma unroll
1637 for (int imeta = 0; imeta < nmeta; ++imeta) {
1638 if (np*cols_per_warp >= warp_size || threadIdx.x < np*cols_per_warp) {
1639 // Combined KQ max scale + rowsum.
1640 meta_ptr[imeta * warp_size * tile_stride/2] = make_float2(KQ_cms[imeta], KQ_crs);
1641 }
1642 }
1643
1644 // Combined KQ max + rowsum.
1645 static_assert(cols_per_warp <= warp_size);
1646 if (needs_fixup && (cols_per_warp == warp_size || threadIdx.x < cols_per_warp)) {
1647 float2 * dstk_fixup_meta = dstk_fixup + blockIdx.x*ncols;
1648 dstk_fixup_meta[(threadIdx.y/np)*cols_per_warp + threadIdx.x] = make_float2(KQ_cmn, KQ_crs);
1649 }
1650 if (is_fixup && (cols_per_warp == warp_size || threadIdx.x < cols_per_warp)) {
1651 float2 * dstk_fixup_meta = dstk_fixup + (gridDim.x + blockIdx.x)*ncols;
1652 dstk_fixup_meta[(threadIdx.y/np)*cols_per_warp + threadIdx.x] = make_float2(KQ_cmn, KQ_crs);
1653 }
1654 }
1655 }
1656
1657#pragma unroll
1658 for (int k00 = 0; k00 < DV/2; k00 += nbatch_combine) {
1659 if constexpr (cols_per_warp == 8) {
1660 static_assert(std::is_same_v<decltype(T_C_VKQ::x), half2[T_C_VKQ::ne]>, "bad VKQ type");
1661 const int jc_cwd = threadIdx.y*T_B_KQ::I + T_B_KQ::get_i(-1); // jc combine write data
1662#pragma unroll
1663 for (int k1 = 0; k1 < nbatch_combine; k1 += T_B_KQ::J) {
1664 const T_B_KQ B = get_transposed(VKQ_C[(k00 + k1)/T_B_KQ::J]); // Conversion of C to B matrix puts it in column-major format.
1665
1666#pragma unroll
1667 for (int l = 0; l < T_B_KQ::ne; ++l) {
1668 const int k = k1 + T_B_KQ::get_j(l);
1669
1670 tile_Q[jc_cwd*tile_stride + k] = B.x[l];
1671 }
1672 }
1673 } else {
1674 const int j0 = threadIdx.y*cols_per_warp;
1675 if constexpr (std::is_same_v<decltype(T_C_VKQ::x), half2[T_C_VKQ::ne]>) {
1676 if constexpr (T_C_VKQ::dl == DATA_LAYOUT_I_MAJOR) {
1677#pragma unroll
1678 for (int k1 = 0; k1 < nbatch_combine; k1 += T_C_VKQ::J) {
1679#pragma unroll
1680 for (int l = 0; l < T_C_VKQ::ne; ++l) {
1681 const int j = j0 + T_C_VKQ::get_i(l);
1682 const int k = k1 + T_C_VKQ::get_j(l);
1683
1684 tile_Q[j*tile_stride + k] = VKQ_C[(k00 + k1)/T_C_VKQ::J].x[l];
1685 }
1686 }
1687 } else {
1688 static_assert(T_C_VKQ::dl == DATA_LAYOUT_I_MAJOR_SCRAMBLED, "bad T_C_VKQ data layout");
1689 using T_C_VKQ_us = tile<T_C_VKQ::I, T_C_VKQ::J, half2, DATA_LAYOUT_I_MAJOR>; // us == unscrambled
1690#pragma unroll
1691 for (int k1 = 0; k1 < nbatch_combine; k1 += T_C_VKQ::J) {
1692 const T_C_VKQ_us VKQ_C_us = unscramble(VKQ_C[(k00 + k1)/T_C_VKQ::J]);
1693#pragma unroll
1694 for (int l = 0; l < T_C_VKQ_us::ne; ++l) {
1695 const int j = j0 + T_C_VKQ_us::get_i(l);
1696 const int k = k1 + T_C_VKQ_us::get_j(l);
1697
1698 tile_Q[j*tile_stride + k] = VKQ_C_us.x[l];
1699 }
1700 }
1701 }
1702 } else {
1703 static_assert(std::is_same_v<decltype(T_C_VKQ::x), float[T_C_VKQ::ne]>, "bad VKQ type");
1704 half * tile_Q_h = (half *) tile_Q;
1705#pragma unroll
1706 for (int k1 = 0; k1 < nbatch_combine; k1 += T_C_VKQ::J/2) {
1707#pragma unroll
1708 for (int l = 0; l < T_C_VKQ::ne; ++l) {
1709 const int j = j0 + T_C_VKQ::get_i(l);
1710 const int k = 2*k1 + T_C_VKQ::get_j(l);
1711
1712 tile_Q_h[j*(2*tile_stride) + k] = VKQ_C[(k00 + k1)/(T_C_VKQ::J/2)].x[l];
1713 }
1714 }
1715 }
1716 }
1717
1718 __syncthreads();
1719
1720 if (np == 1 || threadIdx.y % np == 0) {
1721 // The first 2*2*gridDim.x*ncols floats in dstk_fixup are for storing max. values and row sums.
1722 // The values after that are for the partial results of the individual blocks.
1723 float2 * dstk_fixup_data = dstk_fixup + gridDim.x*(2*ncols) + blockIdx.x*(ncols*(DV/2));
1724
1725#pragma unroll
1726 for (int stride_k : {warp_size, warp_size/2, warp_size/4, warp_size/8}) {
1727 const int k0_start = stride_k == warp_size ? 0 : nbatch_combine - nbatch_combine % (2*stride_k);
1728 const int k0_stop = nbatch_combine - nbatch_combine % (1*stride_k);
1729 const int stride_jc = warp_size / stride_k;
1730
1731 if (k0_start == k0_stop) {
1732 continue;
1733 }
1734
1735#pragma unroll
1736 for (int jc0_dst = 0; jc0_dst < ncols; jc0_dst += (nwarps/np)*stride_jc) {
1737 const int jc_dst = jc0_dst + (threadIdx.y/np)*stride_jc + (stride_k == warp_size ? 0 : threadIdx.x / stride_k);
1738
1739 if (jc0_dst + (nwarps/np)*stride_jc > ncols && jc_dst >= ncols) {
1740 break;
1741 }
1742
1743 const int jc_tile_K = (jc_dst/cols_per_warp)*(np*cols_per_warp) + jc_dst % cols_per_warp;
1744
1745 const int j_dst = jc_dst / ncols2;
1746 const int c_dst = jc_dst % ncols2;
1747
1748 if (!is_fixup && ((ncols1 > 1 && jt*ncols1 + j_dst >= int(ne01.z)) || (ncols2 > 1 && zt_gqa*ncols2 + c_dst >= gqa_ratio))) {
1749 continue;
1750 }
1751
1752 const float * meta_j = (const float *) tile_Q + jc_tile_K*tile_stride + nbatch_combine;
1753#pragma unroll
1754 for (int k0 = k0_start; k0 < k0_stop; k0 += stride_k) {
1755 const int k = k0 + (stride_k == warp_size ? threadIdx.x : threadIdx.x % stride_k);
1756
1757 float2 dstk_val = make_float2(0.0f, 0.0f);
1758#pragma unroll
1759 for (int ip = 0; ip < np; ++ip) {
1760 const float KQ_crs = np == 1 ? 1.0f : meta_j[ip*cols_per_warp * tile_stride + 0];
1761 const float2 dstk_val_add = __half22float2(tile_Q[(jc_tile_K + ip*cols_per_warp) * tile_stride + k]);
1762 dstk_val.x += dstk_val_add.x*KQ_crs;
1763 dstk_val.y += dstk_val_add.y*KQ_crs;
1764 }
1765
1766 if (!needs_fixup && !is_fixup) {
1767 const float KQ_rowsum_j = meta_j[1];
1768 dstk_val.x /= KQ_rowsum_j;
1769 dstk_val.y /= KQ_rowsum_j;
1770 }
1771
1772 if (is_fixup) {
1773 dstk_fixup_data[jc_dst*(DV/2) + k00 + k] = dstk_val;
1774 } else {
1775 dstk[((jt*ncols1 + j_dst)*ne02 + c_dst)*(DV/2) + k00 + k] = dstk_val;
1776 }
1777 }
1778 }
1779 }
1780 }
1781 if (np > 1 || nbatch_combine != DKQ/2) {
1782 __syncthreads();
1783 }
1784 }
1785#else
1786 GGML_UNUSED_VARS(Q_f2, K_h2, V_h2, mask_h, indices, sinks_f, dstk, dstk_fixup,
1787 scale, slope, logit_softcap, ne01, ne02, gqa_ratio,
1788 stride_Q1, stride_Q2, stride_K, stride_V, stride_mask,
1789 jt, kb0_start, kb0_stop);
1790 NO_DEVICE_CODE;
1791#endif // defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)
1792}
1793
1794static constexpr __host__ __device__ bool ggml_cuda_flash_attn_ext_mma_f16_may_use_sparse(
1795 const int DKQ, const int DV, const int ncols1, const int ncols2) {
1796 return (DKQ == 512 && DV == 512 && ncols1 == 1 && ncols2 == 8) ||
1797 (DKQ == 576 && DV == 512 && ncols1 == 1 && ncols2 == 16) ||
1798 (DKQ == 256 && DV == 256 && ncols1 == 1 && ncols2 == 8) ||
1799 (DKQ == 256 && DV == 256 && ncols1 == 8 && ncols2 == 8);
1800}
1801
1802template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool V_is_K_view, bool use_sparse>
1803__launch_bounds__(ggml_cuda_fattn_mma_get_nthreads(DKQ, DV, ncols1*ncols2), ggml_cuda_fattn_mma_get_occupancy(DKQ, DV, ncols1*ncols2))
1804static __global__ void flash_attn_ext_f16(
1805 const char * Q_ptr,
1806 const char * K_ptr,
1807 const char * V_ptr,
1808 const char * mask_ptr,
1809 const char * sinks_ptr,
1810 const int * KV_max_ptr,
1811 const int * KV_rows_ptr,
1812 float * dst_ptr,
1813 float2 * dst_meta_ptr,
1814 const float scale,
1815 const float max_bias,
1816 const float m0,
1817 const float m1,
1818 const uint32_t n_head_log2,
1819 const float logit_softcap,
1820 const int32_t ne00, const uint3 ne01, const int32_t ne02, const int32_t ne03,
1821 const int32_t nb01, const int32_t nb02, const int32_t nb03,
1822 const int32_t ne10, const int32_t ne11, const int32_t ne12, const int32_t ne13,
1823 const int32_t nb11, const int32_t nb12, const int64_t nb13,
1824 const int32_t nb21, const int32_t nb22, const int64_t nb23,
1825 const int32_t ne31, const int32_t ne32, const int32_t ne33,
1826 const int32_t nb31, const int32_t nb32, const int64_t nb33) {
1827 ggml_cuda_pdl_sync(); // TODO optimize placement
1828#if defined(FLASH_ATTN_AVAILABLE) && (defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE))
1829 const char * GGML_CUDA_RESTRICT Q = Q_ptr;
1830 const char * GGML_CUDA_RESTRICT K = K_ptr;
1831 const char * GGML_CUDA_RESTRICT V = V_ptr;
1832 const char * GGML_CUDA_RESTRICT mask = mask_ptr;
1833 const char * GGML_CUDA_RESTRICT sinks = sinks_ptr;
1834 // sparse: one index list per (sequence, query tile), the live count of each list follows the lists
1835 const int * GGML_CUDA_RESTRICT sparse_indices = use_sparse ? KV_max_ptr : nullptr;
1836 const int * GGML_CUDA_RESTRICT KV_max = KV_max_ptr;
1837 float * GGML_CUDA_RESTRICT dst = dst_ptr;
1838 float2 * GGML_CUDA_RESTRICT dst_meta = dst_meta_ptr;
1839
1840 // Skip unused kernel variants for faster compilation:
1841 if (use_logit_softcap && !(DKQ == 128 || DKQ == 256 || DKQ == 512)) {
1842 NO_DEVICE_CODE;
1843 return;
1844 }
1845 if (DKQ == 192 && ncols2 != 8 && ncols2 != 16) {
1846 NO_DEVICE_CODE;
1847 return;
1848 }
1849
1850 if (!ggml_cuda_flash_attn_ext_mma_f16_may_use_sparse(DKQ, DV, ncols1, ncols2) && use_sparse) {
1851 NO_DEVICE_CODE;
1852 return;
1853 }
1854#ifdef VOLTA_MMA_AVAILABLE
1855 if (ncols1*ncols2 < 32) {
1856 NO_DEVICE_CODE;
1857 return;
1858 }
1859#endif // VOLTA_MMA_AVAILABLE
1860
1861#if __CUDA_ARCH__ == GGML_CUDA_CC_TURING
1862 if (ncols1*ncols2 > 32) {
1863 NO_DEVICE_CODE;
1864 return;
1865 }
1866#endif // __CUDA_ARCH__ == GGML_CUDA_CC_TURING
1867
1868#if defined(AMD_WMMA_AVAILABLE)
1869 if (ncols1*ncols2 < 16 || ncols2 == 1 || DKQ > 256) {
1870 NO_DEVICE_CODE;
1871 return;
1872 }
1873#endif // defined(AMD_WMMA_AVAILABLE)
1874
1875#if defined(AMD_MFMA_AVAILABLE)
1876 if (ncols1*ncols2 < 16 || (DKQ > 256 && ncols1*ncols2 < 32 && !use_sparse)) {
1877 NO_DEVICE_CODE;
1878 return;
1879 }
1880#endif // defined(AMD_MFMA_AVAILABLE)
1881
1882 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
1883 constexpr int ncols = ncols1 * ncols2;
1884 constexpr int nbatch_fa = ggml_cuda_fattn_mma_get_nbatch_fa(DKQ, DV, ncols);
1885 constexpr int nthreads = ggml_cuda_fattn_mma_get_nthreads(DKQ, DV, ncols);
1886 constexpr int nwarps = nthreads / warp_size;
1887
1888 const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
1889
1890 const int stride_Q1 = nb01 / sizeof(float2);
1891 const int stride_Q2 = nb02 / sizeof(float2);
1892 const int stride_K = nb11 / sizeof(half2);
1893 const int stride_mask = nb31 / sizeof(half);
1894
1895 const int stride_V = V_is_K_view ? stride_K : nb21 / sizeof(half2);
1896
1897 const int iter_k = (ne11 + (nbatch_fa - 1)) / nbatch_fa;
1898 const int iter_j = (ne01.z + (ncols1 - 1)) / ncols1;
1899 const int iter_z_gqa = (gqa_ratio + (ncols2 - 1)) / ncols2;
1900
1901 if (use_sparse) {
1902 KV_max = KV_max_ptr + int64_t(iter_j)*ne33*ne11;
1903 }
1904
1905 // kbc == k block continuous, current index in continuous ijk space.
1906 int kbc = int64_t(blockIdx.x + 0)*(iter_k*iter_j*iter_z_gqa*ne12*ne03) / gridDim.x;
1907 const int kbc_stop = int64_t(blockIdx.x + 1)*(iter_k*iter_j*iter_z_gqa*ne12*ne03) / gridDim.x;
1908
1909 // If the seams of 2 CUDA blocks fall within an output tile their results need to be combined.
1910 // For this we need to track both the block that starts the tile (needs_fixup) and the block that finishes the tile (is_fixup).
1911 // In the most general case >2 seams can fall into the same tile.
1912
1913 // kb0 == k start index when in the output tile.
1914 int kb0_start = kbc % iter_k;
1915 int kb0_stop = min(iter_k, kb0_start + kbc_stop - kbc);
1916
1917 while (kbc < kbc_stop && kb0_stop == iter_k) {
1918 // z_KV == K/V head index, zt_gqa = Q head start index per K/V head, jt = token position start index
1919 const int sequence = kbc /(iter_k*iter_j*iter_z_gqa*ne12);
1920 const int z_KV = (kbc - iter_k*iter_j*iter_z_gqa*ne12 * sequence)/(iter_k*iter_j*iter_z_gqa);
1921 const int zt_gqa = (kbc - iter_k*iter_j*iter_z_gqa*ne12 * sequence - iter_k*iter_j*iter_z_gqa * z_KV)/(iter_k*iter_j);
1922 const int jt = (kbc - iter_k*iter_j*iter_z_gqa*ne12 * sequence - iter_k*iter_j*iter_z_gqa * z_KV - iter_k*iter_j * zt_gqa) / iter_k;
1923
1924 const int zt_Q = z_KV*gqa_ratio + zt_gqa*ncols2; // Global Q head start index.
1925
1926 const float2 * Q_f2 = (const float2 *) (Q + nb03*sequence + nb02*zt_Q);
1927 const half2 * K_h2 = (const half2 *) (K + nb13*sequence + nb12*z_KV);
1928 const half * mask_h = ncols2 == 1 && !mask ? nullptr :
1929 (const half *) (mask + nb33*(sequence % ne33));
1930 float2 * dstk = ((float2 *) dst) + (sequence*ne01.z*ne02 + zt_Q) * (DV/2);
1931
1932 const half2 * V_h2 = V_is_K_view ? K_h2 : (const half2 *) (V + nb23*sequence + nb22*z_KV);
1933 const float * sinks_f = sinks ? (const float *) sinks + zt_Q : nullptr;
1934 const int32_t * indices = use_sparse ? sparse_indices + (int64_t(sequence % ne33)*iter_j + jt)*ne11 :
1935 KV_rows_ptr ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr;
1936
1937 const float slope = ncols2 == 1 ? get_alibi_slope(max_bias, zt_Q, n_head_log2, m0, m1) : 1.0f;
1938
1939 if (use_sparse) {
1940 kb0_stop = min(kb0_stop, (KV_max[(sequence % ne33)*iter_j + jt] + nbatch_fa - 1) / nbatch_fa);
1941 } else if (KV_max) {
1942 kb0_stop = min(kb0_stop, KV_max[sequence*iter_j + jt] / nbatch_fa);
1943 }
1944 constexpr bool is_fixup = false; // All but (potentially) the last iterations write their data to dst rather than the fixup buffer.
1945 if (kb0_start == 0) {
1946 constexpr bool needs_fixup = false; // CUDA block is working on an entire tile.
1947 flash_attn_ext_f16_process_tile<DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup>
1948 (Q_f2, K_h2, V_h2, mask_h, indices, sinks_f, dstk, dst_meta, scale, slope, logit_softcap,
1949 ne01, ne02, gqa_ratio, ne11, stride_Q1, stride_Q2, stride_K, stride_V, stride_mask, jt, zt_gqa, kb0_start, kb0_stop);
1950 } else {
1951 constexpr bool needs_fixup = true; // CUDA block is missing the beginning of a tile.
1952 flash_attn_ext_f16_process_tile<DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup>
1953 (Q_f2, K_h2, V_h2, mask_h, indices, sinks_f, dstk, dst_meta, scale, slope, logit_softcap,
1954 ne01, ne02, gqa_ratio, ne11, stride_Q1, stride_Q2, stride_K, stride_V, stride_mask, jt, zt_gqa, kb0_start, kb0_stop);
1955 }
1956
1957 kbc += iter_k;
1958 kbc -= kbc % iter_k;
1959
1960 kb0_start = 0;
1961 kb0_stop = min(iter_k, kbc_stop - kbc);
1962 }
1963
1964 if (kbc >= kbc_stop) {
1965 return;
1966 }
1967
1968 // z_KV == K/V head index, zt_gqa = Q head start index per K/V head, jt = token position start index.
1969 const int sequence = kbc /(iter_k*iter_j*iter_z_gqa*ne12);
1970 const int z_KV = (kbc - iter_k*iter_j*iter_z_gqa*ne12 * sequence)/(iter_k*iter_j*iter_z_gqa);
1971 const int zt_gqa = (kbc - iter_k*iter_j*iter_z_gqa*ne12 * sequence - iter_k*iter_j*iter_z_gqa * z_KV)/(iter_k*iter_j);
1972 const int jt = (kbc - iter_k*iter_j*iter_z_gqa*ne12 * sequence - iter_k*iter_j*iter_z_gqa * z_KV - iter_k*iter_j * zt_gqa) / iter_k;
1973
1974 const int zt_Q = z_KV*gqa_ratio + zt_gqa*ncols2; // Global Q head start index.
1975
1976 const float2 * Q_f2 = (const float2 *) (Q + nb03*sequence + nb02*zt_Q);
1977 const half2 * K_h2 = (const half2 *) (K + nb13*sequence + nb12*z_KV);
1978 const half * mask_h = ncols2 == 1 && !mask ? nullptr :
1979 (const half *) (mask + nb33*(sequence % ne33));
1980 float2 * dstk = ((float2 *) dst) + (sequence*ne01.z*ne02 + zt_Q) * (DV/2);
1981
1982 const half2 * V_h2 = V_is_K_view ? K_h2 : (const half2 *) (V + nb23*sequence + nb22*z_KV);
1983 const float * sinks_f = sinks ? (const float *) sinks + zt_Q : nullptr;
1984 const int32_t * indices = use_sparse ? sparse_indices + (int64_t(sequence % ne33)*iter_j + jt)*ne11 :
1985 KV_rows_ptr ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr;
1986
1987 const float slope = ncols2 == 1 ? get_alibi_slope(max_bias, zt_Q, n_head_log2, m0, m1) : 1.0f;
1988
1989 if (use_sparse) {
1990 kb0_stop = min(kb0_stop, (KV_max[(sequence % ne33)*iter_j + jt] + nbatch_fa - 1) / nbatch_fa);
1991 } else if (KV_max) {
1992 kb0_stop = min(kb0_stop, KV_max[sequence*iter_j + jt] / nbatch_fa);
1993 }
1994
1995 constexpr bool is_fixup = true; // Last index writes its data to fixup buffer to avoid data races with other blocks.
1996 constexpr bool needs_fixup = false;
1997 flash_attn_ext_f16_process_tile<DKQ, DV, ncols1, ncols2, nwarps, use_logit_softcap, V_is_K_view, use_sparse, needs_fixup, is_fixup>
1998 (Q_f2, K_h2, V_h2, mask_h, indices, sinks_f, dstk, dst_meta, scale, slope, logit_softcap,
1999 ne01, ne02, gqa_ratio, ne11, stride_Q1, stride_Q2, stride_K, stride_V, stride_mask, jt, zt_gqa, kb0_start, kb0_stop);
2000#else
2001 GGML_UNUSED_VARS(Q_ptr, K_ptr, V_ptr, mask_ptr, sinks_ptr, KV_max_ptr, KV_rows_ptr, dst_ptr, dst_meta_ptr, scale,
2002 max_bias, m0, m1, n_head_log2, logit_softcap,
2003 ne00, ne01, ne02, ne03,
2004 nb01, nb02, nb03,
2005 ne10, ne11, ne12, ne13,
2006 nb11, nb12, nb13,
2007 nb21, nb22, nb23,
2008 ne31, ne32, ne33,
2009 nb31, nb32, nb33);
2010 NO_DEVICE_CODE;
2011#endif // defined(FLASH_ATTN_AVAILABLE) && (defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE))
2012}
2013
2014bool ggml_cuda_flash_attn_ext_mma_f16_shall_use_sparse(const int cc, const ggml_tensor * dst, const int ncols1, const int ncols2);
2015
2016template <int DKQ, int DV, int ncols1, int ncols2>
2017void ggml_cuda_flash_attn_ext_mma_f16_case(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
2018 const ggml_tensor * KQV = dst;
2019 const int id = ggml_cuda_get_device();
2020 const int cc = ggml_cuda_info().devices[id].cc;
2021
2022 constexpr int ncols = ncols1 * ncols2;
2023
2024 const int nthreads = ggml_cuda_fattn_mma_get_nthreads (DKQ, DV, ncols, cc);
2025 const int nbatch_fa = ggml_cuda_fattn_mma_get_nbatch_fa (DKQ, DV, ncols, cc);
2026 const int nbatch_K2 = ggml_cuda_fattn_mma_get_nbatch_K2 (DKQ, DV, ncols, cc);
2027 const int nbatch_V2 = ggml_cuda_fattn_mma_get_nbatch_V2 (DKQ, DV, ncols, cc);
2028 const int nbatch_combine = ggml_cuda_fattn_mma_get_nbatch_combine(DKQ, DV, ncols, cc);
2029 const bool Q_in_reg = ggml_cuda_fattn_mma_get_Q_in_reg (DKQ, DV, ncols, cc);
2030 const int nstages = ggml_cuda_fattn_mma_get_nstages (DKQ, DV, ncols1, ncols2, cc);
2031
2032 const int cols_per_warp = std::min(ncols, get_cols_per_warp(cc));
2033 const int warp_size_host = ggml_cuda_info().devices[ctx.device].warp_size;
2034 const int nwarps = nthreads / warp_size_host;
2035
2036 constexpr bool V_is_K_view = DKQ == 576; // Guaranteed by the kernel selection logic in fattn.cu
2037
2038 // KV tile strides must match flash_attn_ext_f16_iter / _process_tile.
2039 const bool swz = ggml_cuda_fattn_mma_get_swizzled(DKQ, DV, ncols, cc);
2040 const int stride_tile_K = swz ? nbatch_K2 : nbatch_K2 + 4;
2041 const int stride_tile_V = V_is_K_view ? stride_tile_K : (swz ? nbatch_V2 : nbatch_V2 + 4);
2042 const size_t nbytes_shared_KV_1stage = nbatch_fa * std::max(stride_tile_K, stride_tile_V) * sizeof(half2);
2043 const size_t nbytes_shared_KV_2stage = nbatch_fa * (stride_tile_K + stride_tile_V) * sizeof(half2);
2044 const size_t nbytes_shared_Q = ncols * (DKQ/2 + 4) * sizeof(half2);
2045 const size_t nbytes_shared_mask = ncols1 * (nbatch_fa/2 + 4) * sizeof(half2);
2046 const size_t nbytes_shared_combine = nwarps*cols_per_warp * (nbatch_combine + 4) * sizeof(half2);
2047
2048 const size_t nbytes_shared_KV = nstages <= 1 ? nbytes_shared_KV_1stage : nbytes_shared_KV_2stage;
2049
2050 const size_t nbytes_shared_total = std::max(nbytes_shared_combine, Q_in_reg ?
2051 std::max(nbytes_shared_Q, nbytes_shared_KV + nbytes_shared_mask) :
2052 nbytes_shared_Q + nbytes_shared_KV + nbytes_shared_mask);
2053
2054 float logit_softcap;
2055 memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
2056
2057#if defined(GGML_USE_HIP)
2058 using fattn_kernel_ptr_t = const void*;
2059#else
2060 using fattn_kernel_ptr_t = fattn_kernel_t;
2061#endif // defined(GGML_USE_HIP)
2062 fattn_kernel_t fattn_kernel;
2063 bool use_sparse = false;
2064 if (logit_softcap == 0.0f) {
2065 constexpr bool use_logit_softcap = false;
2066#if !defined(GGML_USE_MUSA)
2067 if constexpr (ggml_cuda_flash_attn_ext_mma_f16_may_use_sparse(DKQ, DV, ncols1, ncols2)) {
2068 if (ggml_cuda_flash_attn_ext_mma_f16_shall_use_sparse(cc, dst, ncols1, ncols2)) {
2069 constexpr bool use_sparse_kernel = true;
2070 fattn_kernel = flash_attn_ext_f16<DKQ, DV, ncols1, ncols2, use_logit_softcap, V_is_K_view, use_sparse_kernel>;
2071 use_sparse = true;
2072
2073 static bool shared_memory_limit_raised[GGML_CUDA_MAX_DEVICES] = {false};
2074 if (!shared_memory_limit_raised[id]) {
2075 CUDA_CHECK(cudaFuncSetAttribute(reinterpret_cast<fattn_kernel_ptr_t>(fattn_kernel), cudaFuncAttributeMaxDynamicSharedMemorySize, nbytes_shared_total));
2076 shared_memory_limit_raised[id] = true;
2077 }
2078 } else {
2079 constexpr bool use_sparse_kernel = false;
2080 fattn_kernel = flash_attn_ext_f16<DKQ, DV, ncols1, ncols2, use_logit_softcap, V_is_K_view, use_sparse_kernel>;
2081
2082 static bool shared_memory_limit_raised[GGML_CUDA_MAX_DEVICES] = {false};
2083 if (!shared_memory_limit_raised[id]) {
2084 CUDA_CHECK(cudaFuncSetAttribute(reinterpret_cast<fattn_kernel_ptr_t>(fattn_kernel), cudaFuncAttributeMaxDynamicSharedMemorySize, nbytes_shared_total));
2085 shared_memory_limit_raised[id] = true;
2086 }
2087 }
2088 } else
2089#endif // !defined(GGML_USE_MUSA)
2090 {
2091 constexpr bool use_sparse_kernel = false;
2092 fattn_kernel = flash_attn_ext_f16<DKQ, DV, ncols1, ncols2, use_logit_softcap, V_is_K_view, use_sparse_kernel>;
2093
2094 static bool shared_memory_limit_raised[GGML_CUDA_MAX_DEVICES] = {false};
2095 if (!shared_memory_limit_raised[id]) {
2096 CUDA_CHECK(cudaFuncSetAttribute(reinterpret_cast<fattn_kernel_ptr_t>(fattn_kernel), cudaFuncAttributeMaxDynamicSharedMemorySize, nbytes_shared_total));
2097 shared_memory_limit_raised[id] = true;
2098 }
2099 }
2100 } else {
2101 constexpr bool use_logit_softcap = true;
2102 constexpr bool use_sparse_kernel = false;
2103 fattn_kernel = flash_attn_ext_f16<DKQ, DV, ncols1, ncols2, use_logit_softcap, V_is_K_view, use_sparse_kernel>;
2104
2105 static bool shared_memory_limit_raised[GGML_CUDA_MAX_DEVICES] = {false};
2106 if (!shared_memory_limit_raised[id]) {
2107 CUDA_CHECK(cudaFuncSetAttribute(reinterpret_cast<fattn_kernel_ptr_t>(fattn_kernel), cudaFuncAttributeMaxDynamicSharedMemorySize, nbytes_shared_total));
2108 shared_memory_limit_raised[id] = true;
2109 }
2110 }
2111
2112 const bool async_kv_preload = nstages == 2 && !use_sparse;
2113 launch_fattn<DV, ncols1, ncols2>
2114 (ctx, dst, fattn_kernel, nwarps, nbytes_shared_total, nbatch_fa, true, true, true, use_sparse, warp_size_host, async_kv_preload);
2115}
2116
2117
2118#define DECL_FATTN_MMA_F16_CASE(DKQ, DV, ncols1, ncols2) \
2119 template void ggml_cuda_flash_attn_ext_mma_f16_case \
2120 <DKQ, DV, ncols1, ncols2>(ggml_backend_cuda_context & ctx, ggml_tensor * dst) \
2121
2122#define DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(DKQ, DV, ncols) \
2123 extern DECL_FATTN_MMA_F16_CASE(DKQ, DV, (ncols)/ 1, 1); \
2124 extern DECL_FATTN_MMA_F16_CASE(DKQ, DV, (ncols)/ 2, 2); \
2125 extern DECL_FATTN_MMA_F16_CASE(DKQ, DV, (ncols)/ 4, 4); \
2126 extern DECL_FATTN_MMA_F16_CASE(DKQ, DV, (ncols)/ 8, 8); \
2127 extern DECL_FATTN_MMA_F16_CASE(DKQ, DV, (ncols)/16, 16); \
2128
2129DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 64, 64, 8)
2130DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 80, 80, 8)
2131DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 96, 96, 8)
2132DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(112, 112, 8)
2133DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(128, 128, 8)
2134DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(256, 256, 8)
2135
2136DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 64, 64, 16)
2137DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 80, 80, 16)
2138DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 96, 96, 16)
2139DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(112, 112, 16)
2140DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(128, 128, 16)
2141DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(256, 256, 16)
2142
2143DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 64, 64, 32)
2144DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 80, 80, 32)
2145DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 96, 96, 32)
2146DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(112, 112, 32)
2147DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(128, 128, 32)
2148DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(256, 256, 32)
2149
2150DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 64, 64, 64)
2151DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 80, 80, 64)
2152DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2( 96, 96, 64)
2153DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(112, 112, 64)
2154DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(128, 128, 64)
2155DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(256, 256, 64)
2156
2157extern DECL_FATTN_MMA_F16_CASE(512, 512, 4, 2);
2158extern DECL_FATTN_MMA_F16_CASE(512, 512, 8, 2);
2159extern DECL_FATTN_MMA_F16_CASE(512, 512, 16, 2);
2160extern DECL_FATTN_MMA_F16_CASE(512, 512, 32, 2);
2161extern DECL_FATTN_MMA_F16_CASE(512, 512, 2, 4);
2162extern DECL_FATTN_MMA_F16_CASE(512, 512, 4, 4);
2163extern DECL_FATTN_MMA_F16_CASE(512, 512, 8, 4);
2164extern DECL_FATTN_MMA_F16_CASE(512, 512, 16, 4);
2165extern DECL_FATTN_MMA_F16_CASE(512, 512, 1, 8);
2166extern DECL_FATTN_MMA_F16_CASE(512, 512, 2, 8);
2167extern DECL_FATTN_MMA_F16_CASE(512, 512, 4, 8);
2168extern DECL_FATTN_MMA_F16_CASE(512, 512, 8, 8);
2169
2170// The number of viable configurations for Deepseek is very limited:
2171extern DECL_FATTN_MMA_F16_CASE(576, 512, 1, 16);
2172extern DECL_FATTN_MMA_F16_CASE(576, 512, 2, 16);
2173extern DECL_FATTN_MMA_F16_CASE(576, 512, 4, 16);
2174
2175// Mistral Small 4 (DKQ=320, DV=256), GQA=32-only build:
2176extern DECL_FATTN_MMA_F16_CASE(320, 256, 1, 32);
2177extern DECL_FATTN_MMA_F16_CASE(320, 256, 2, 32);
2178
2179// For GLM 4.7 Flash
2180extern DECL_FATTN_MMA_F16_CASE(576, 512, 4, 4);
2181extern DECL_FATTN_MMA_F16_CASE(576, 512, 8, 4);
2182extern DECL_FATTN_MMA_F16_CASE(576, 512, 16, 4);
2183extern DECL_FATTN_MMA_F16_CASE(576, 512, 1, 32);
2184extern DECL_FATTN_MMA_F16_CASE(576, 512, 2, 32);