fattn-common.cuh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /**
  2. * llama.cpp - commit 8962422b1c6f9b8b15f5aeaea42600bcc2d44177 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "common.cuh"
  28. #include "convert.cuh"
  29. #include "vecdotq.cuh"
  30. #include <cstdint>
  31. #define FATTN_KQ_STRIDE 256
  32. #define HALF_MAX_HALF __float2half(65504.0f/2) // Use neg. of this instead of -INFINITY to initialize KQ max vals to avoid NaN upon subtraction.
  33. #define SOFTMAX_FTZ_THRESHOLD -20.0f // Softmax exp. of values smaller than this are flushed to zero to avoid NaNs.
  34. typedef void (* fattn_kernel_t)(
  35. const char * __restrict__ Q,
  36. const char * __restrict__ K,
  37. const char * __restrict__ V,
  38. const char * __restrict__ mask,
  39. float * __restrict__ dst,
  40. float2 * __restrict__ dst_meta,
  41. const float scale,
  42. const float max_bias,
  43. const float m0,
  44. const float m1,
  45. const uint32_t n_head_log2,
  46. const float logit_softcap,
  47. const int ne00,
  48. const int ne01,
  49. const int ne02,
  50. const int ne03,
  51. const int ne10,
  52. const int ne11,
  53. const int ne12,
  54. const int ne13,
  55. const int ne31,
  56. const int nb31,
  57. const int nb01,
  58. const int nb02,
  59. const int nb03,
  60. const int nb11,
  61. const int nb12,
  62. const int nb13,
  63. const int nb21,
  64. const int nb22,
  65. const int nb23,
  66. const int ne0,
  67. const int ne1,
  68. const int ne2,
  69. const int ne3);
  70. typedef half (*vec_dot_KQ_f16_t)(
  71. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds);
  72. typedef float (*vec_dot_KQ_f32_t)(
  73. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds);
  74. template<typename T, int D>
  75. static __device__ __forceinline__ T vec_dot_fattn_vec_KQ_q4_0(
  76. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
  77. const block_q4_0 * K_q4_0 = (const block_q4_0 *) K_c;
  78. GGML_UNUSED(Q_v);
  79. T sum = 0.0f;
  80. #pragma unroll
  81. for (int k_KQ_0 = 0; k_KQ_0 < D/sizeof(int); k_KQ_0 += WARP_SIZE) {
  82. const int k_KQ = k_KQ_0 + threadIdx.x;
  83. const int ib = k_KQ / QI8_1;
  84. const int iqs4 = k_KQ % QI4_0;
  85. const int shift = k_KQ & (QI8_1/2);
  86. const int v = (get_int_b2(K_q4_0[ib].qs, iqs4) >> shift) & 0x0F0F0F0F;
  87. const int u = Q_q8[k_KQ_0/WARP_SIZE];
  88. const int sumi = ggml_cuda_dp4a(v, u, 0);
  89. #ifdef FP16_AVAILABLE
  90. if (std::is_same<T, half>::value) {
  91. const half2 * Q_ds = (const half2 *) Q_ds_v;
  92. const half2 sum2 = __half2half2(K_q4_0[ib].d) * Q_ds[k_KQ_0/WARP_SIZE];
  93. sum += (T) (((half) sumi)*__low2half(sum2) - __high2half(sum2) /* *8/QI8_1 == 1 */);
  94. } else
  95. #endif // FP16_AVAILABLE
  96. {
  97. const float2 * Q_ds = (const float2 *) Q_ds_v;
  98. sum += (T) (__half2float(K_q4_0[ib].d) * (sumi*Q_ds[k_KQ_0/WARP_SIZE].x - (8/QI8_1)*Q_ds[k_KQ_0/WARP_SIZE].y));
  99. }
  100. }
  101. return sum;
  102. }
  103. template<typename T, int D>
  104. static __device__ __forceinline__ T vec_dot_fattn_vec_KQ_q4_1(
  105. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
  106. const block_q4_1 * K_q4_1 = (const block_q4_1 *) K_c;
  107. GGML_UNUSED(Q_v);
  108. T sum = 0.0f;
  109. #pragma unroll
  110. for (int k_KQ_0 = 0; k_KQ_0 < D/sizeof(int); k_KQ_0 += WARP_SIZE) {
  111. const int k_KQ = k_KQ_0 + threadIdx.x;
  112. const int ib = k_KQ / QI8_1;
  113. const int iqs4 = k_KQ % QI4_1;
  114. const int shift = k_KQ & (QI8_1/2);
  115. const int v = (get_int_b4(K_q4_1[ib].qs, iqs4) >> shift) & 0x0F0F0F0F;
  116. const int u = Q_q8[k_KQ_0/WARP_SIZE];
  117. const int sumi = ggml_cuda_dp4a(v, u, 0);
  118. #ifdef FP16_AVAILABLE
  119. if (std::is_same<T, half>::value) {
  120. const half2 * Q_ds = (const half2 *) Q_ds_v;
  121. const half2 d4d8_m4s8 = K_q4_1[ib].dm * Q_ds[k_KQ_0/WARP_SIZE];
  122. const half2 sumid4d8_m4s8scaled = d4d8_m4s8 * make_half2(sumi, 1.0f/QI8_1);
  123. sum += (T) (__low2half(sumid4d8_m4s8scaled) + __high2half(sumid4d8_m4s8scaled));
  124. } else
  125. #endif // FP16_AVAILABLE
  126. {
  127. const float2 * Q_ds = (const float2 *) Q_ds_v;
  128. const float sumid4d8 = __low2float(K_q4_1[ib].dm)*Q_ds[k_KQ_0/WARP_SIZE].x * sumi;
  129. const float m4s8scaled = __high2float(K_q4_1[ib].dm)*Q_ds[k_KQ_0/WARP_SIZE].y / QI8_1;
  130. sum += (T) (sumid4d8 + m4s8scaled);
  131. }
  132. }
  133. return sum;
  134. }
  135. template<typename T, int D>
  136. static __device__ __forceinline__ T vec_dot_fattn_vec_KQ_q5_0(
  137. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
  138. const block_q5_0 * K_q5_0 = (const block_q5_0 *) K_c;
  139. GGML_UNUSED(Q_v);
  140. T sum = 0.0f;
  141. #pragma unroll
  142. for (int k_KQ_0 = 0; k_KQ_0 < D/sizeof(int); k_KQ_0 += WARP_SIZE) {
  143. const int k_KQ = k_KQ_0 + threadIdx.x;
  144. const int ib = k_KQ / QI8_1;
  145. const int iqs4 = k_KQ % QI5_0;
  146. const int iqs8 = k_KQ % QI8_1;
  147. const int shift = k_KQ & (QI8_1/2);
  148. int v = (get_int_b2(K_q5_0[ib].qs, iqs4) >> shift) & 0x0F0F0F0F;
  149. const int vh = get_int_b2(K_q5_0[ib].qh, 0) >> (iqs8 * QI5_0);
  150. v |= (vh << 4) & 0x00000010; // 0 -> 4
  151. v |= (vh << 11) & 0x00001000; // 1 -> 12
  152. v |= (vh << 18) & 0x00100000; // 2 -> 20
  153. v |= (vh << 25) & 0x10000000; // 3 -> 28
  154. const int u = Q_q8[k_KQ_0/WARP_SIZE];
  155. const int sumi = ggml_cuda_dp4a(v, u, 0);
  156. #ifdef FP16_AVAILABLE
  157. if (std::is_same<T, half>::value) {
  158. const half2 * Q_ds = (const half2 *) Q_ds_v;
  159. const half2 sum2 = __half2half2(K_q5_0[ib].d) * Q_ds[k_KQ_0/WARP_SIZE];
  160. sum += (T) (((half) sumi)*__low2half(sum2) - __high2half(sum2)*__float2half(2.0f)) /* *16/QI8_1 == 2 */;
  161. } else
  162. #endif // FP16_AVAILABLE
  163. {
  164. const float2 * Q_ds = (const float2 *) Q_ds_v;
  165. sum += (T) (__half2float(K_q5_0[ib].d) * (sumi*Q_ds[k_KQ_0/WARP_SIZE].x - (16/QI8_1)*Q_ds[k_KQ_0/WARP_SIZE].y));
  166. }
  167. }
  168. return sum;
  169. }
  170. template<typename T, int D>
  171. static __device__ __forceinline__ T vec_dot_fattn_vec_KQ_q5_1(
  172. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
  173. const block_q5_1 * K_q5_1 = (const block_q5_1 *) K_c;
  174. GGML_UNUSED(Q_v);
  175. T sum = 0.0f;
  176. #pragma unroll
  177. for (int k_KQ_0 = 0; k_KQ_0 < D/sizeof(int); k_KQ_0 += WARP_SIZE) {
  178. const int k_KQ = k_KQ_0 + threadIdx.x;
  179. const int ib = k_KQ / QI8_1;
  180. const int iqs4 = k_KQ % QI5_1;
  181. const int iqs8 = k_KQ % QI8_1;
  182. const int shift = k_KQ & (QI8_1/2);
  183. int v = (get_int_b2(K_q5_1[ib].qs, iqs4) >> shift) & 0x0F0F0F0F;
  184. const int vh = get_int_b2(K_q5_1[ib].qh, 0) >> (iqs8 * QI5_1);
  185. v |= (vh << 4) & 0x00000010; // 0 -> 4
  186. v |= (vh << 11) & 0x00001000; // 1 -> 12
  187. v |= (vh << 18) & 0x00100000; // 2 -> 20
  188. v |= (vh << 25) & 0x10000000; // 3 -> 28
  189. const int u = Q_q8[k_KQ_0/WARP_SIZE];
  190. const int sumi = ggml_cuda_dp4a(v, u, 0);
  191. #ifdef FP16_AVAILABLE
  192. if (std::is_same<T, half>::value) {
  193. const half2 * Q_ds = (const half2 *) Q_ds_v;
  194. const half2 d5d8_m5s8 = K_q5_1[ib].dm * Q_ds[k_KQ_0/WARP_SIZE];
  195. const half2 sumid5d8_m5s8scaled = d5d8_m5s8 * make_half2(sumi, 1.0f/QI8_1);
  196. sum += (T) (__low2half(sumid5d8_m5s8scaled) + __high2half(sumid5d8_m5s8scaled));
  197. } else
  198. #endif // FP16_AVAILABLE
  199. {
  200. const float2 * Q_ds = (const float2 *) Q_ds_v;
  201. const float sumid5d8 = __low2float(K_q5_1[ib].dm)*Q_ds[k_KQ_0/WARP_SIZE].x * sumi;
  202. const float m5s8scaled = __high2float(K_q5_1[ib].dm)*Q_ds[k_KQ_0/WARP_SIZE].y / QI8_1;
  203. sum += (T) (sumid5d8 + m5s8scaled);
  204. }
  205. }
  206. return sum;
  207. }
  208. template <typename T, int D>
  209. static __device__ __forceinline__ T vec_dot_fattn_vec_KQ_q8_0(
  210. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
  211. const block_q8_0 * K_q8_0 = (const block_q8_0 *) K_c;
  212. GGML_UNUSED(Q_v);
  213. T sum = 0.0f;
  214. #pragma unroll
  215. for (int k_KQ_0 = 0; k_KQ_0 < D/sizeof(int); k_KQ_0 += WARP_SIZE) {
  216. const int k_KQ = k_KQ_0 + threadIdx.x;
  217. const int ib = k_KQ / QI8_0;
  218. const int iqs = k_KQ % QI8_0;
  219. const int v = get_int_b2(K_q8_0[ib].qs, iqs);
  220. T Q_d;
  221. if (std::is_same<T, half>::value) {
  222. const half2 * Q_ds = (const half2 *) Q_ds_v;
  223. Q_d = __low2half(Q_ds[k_KQ_0/WARP_SIZE]);
  224. } else {
  225. const float2 * Q_ds = (const float2 *) Q_ds_v;
  226. Q_d = Q_ds[k_KQ_0/WARP_SIZE].x;
  227. }
  228. sum += vec_dot_q8_0_q8_1_impl<T, 1>(&v, &Q_q8[k_KQ_0/WARP_SIZE], K_q8_0[ib].d, Q_d);
  229. }
  230. return sum;
  231. }
  232. template <typename T, int D>
  233. static __device__ __forceinline__ T vec_dot_fattn_vec_KQ_f16(
  234. const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds_v) {
  235. const half2 * K_h2 = (const half2 *) K_c;
  236. GGML_UNUSED(Q_q8);
  237. GGML_UNUSED(Q_ds_v);
  238. #ifdef FP16_AVAILABLE
  239. if (std::is_same<T, half>::value) {
  240. const half2 * Q_h2 = (const half2 *) Q_v;
  241. half2 sum2 = make_half2(0.0f, 0.0f);
  242. #pragma unroll
  243. for (int k_KQ_0 = 0; k_KQ_0 < D/2; k_KQ_0 += WARP_SIZE) {
  244. const int k_KQ = k_KQ_0 + threadIdx.x;
  245. const half2 K_ik = K_h2[k_KQ];
  246. sum2 += K_ik * Q_h2[k_KQ_0/WARP_SIZE];
  247. }
  248. return __low2half(sum2) + __high2half(sum2);
  249. }
  250. #endif // FP16_AVAILABLE
  251. const float2 * Q_f2 = (const float2 *) Q_v;
  252. float sum = 0.0f;
  253. #pragma unroll
  254. for (int k_KQ_0 = 0; k_KQ_0 < D/2; k_KQ_0 += WARP_SIZE) {
  255. const int k_KQ = k_KQ_0 + threadIdx.x;
  256. const half2 K_ik = K_h2[k_KQ];
  257. sum += __low2float(K_ik) * Q_f2[k_KQ_0/WARP_SIZE].x;
  258. sum += __high2float(K_ik) * Q_f2[k_KQ_0/WARP_SIZE].y;
  259. }
  260. return sum;
  261. }
  262. template <typename Tds>
  263. static __device__ __forceinline__ void quantize_q8_1_to_shared(
  264. const float * __restrict__ x, const float scale, int * __restrict__ yq32, void * __restrict__ yds) {
  265. float vals[sizeof(int)] = {0.0f};
  266. #pragma unroll
  267. for (int l = 0; l < sizeof(int); ++l) {
  268. vals[l] = scale * x[4*threadIdx.x + l];
  269. }
  270. float amax = fabsf(vals[0]);
  271. float sum = vals[0];
  272. #pragma unroll
  273. for (int l = 1; l < sizeof(int); ++l) {
  274. amax = fmaxf(amax, fabsf(vals[l]));
  275. sum += vals[l];
  276. }
  277. #pragma unroll
  278. for (int mask = QI8_1/2; mask > 0; mask >>= 1) {
  279. amax = fmaxf(amax, __shfl_xor_sync(0xFFFFFFFF, amax, mask, 32));
  280. sum += __shfl_xor_sync(0xFFFFFFFF, sum, mask, 32);
  281. }
  282. const float d = amax / 127;
  283. int q32 = 0;
  284. int8_t * q8 = (int8_t *) &q32;
  285. if (d != 0.0f) {
  286. #pragma unroll
  287. for (int l = 0; l < sizeof(int); ++l) {
  288. q8[l] = roundf(vals[l] / d);
  289. }
  290. }
  291. yq32[threadIdx.x] = q32;
  292. if (threadIdx.x % QI8_1 == 0) {
  293. if (std::is_same<Tds, half2>::value) {
  294. ((half2 *) yds)[threadIdx.x/QI8_1] = make_half2(d, sum);
  295. } else {
  296. ((float2 *) yds)[threadIdx.x/QI8_1] = make_float2(d, sum);
  297. }
  298. }
  299. }
  300. typedef half (*dequantize_1_f16_t)(const void *, const int64_t);
  301. typedef float (*dequantize_1_f32_t)(const void *, const int64_t);
  302. template <typename T>
  303. static __device__ __forceinline__ T dequantize_1_q4_0(const void * __restrict__ vx, const int64_t i) {
  304. const block_q4_0 * x = (const block_q4_0 *) vx;
  305. const int64_t ib = i / QK4_0;
  306. const int iqs = i % (QK4_0/2);
  307. const int shift = (i % QK4_0) / (QK4_0/2);
  308. const T d = x[ib].d;
  309. const int q0 = x[ib].qs[iqs];
  310. const int q = ((q0 >> (4*shift)) & 0x0F) - 8;
  311. #ifdef FP16_AVAILABLE
  312. if (std::is_same<T, half>::value) {
  313. return ((half) d)*((half) q);
  314. }
  315. #endif // FP16_AVAILABLE
  316. return ((float) d)*((float) q);
  317. }
  318. template <typename T>
  319. static __device__ __forceinline__ T dequantize_1_q4_1(const void * __restrict__ vx, const int64_t i) {
  320. const block_q4_1 * x = (const block_q4_1 *) vx;
  321. const int64_t ib = i / QK4_1;
  322. const int iqs = i % (QK4_1/2);
  323. const int shift = (i % QK4_1) / (QK4_1/2);
  324. const half2 dm = x[ib].dm;
  325. const int q0 = x[ib].qs[iqs];
  326. const int q = ((q0 >> (4*shift)) & 0x0F);
  327. #ifdef FP16_AVAILABLE
  328. if (std::is_same<T, half>::value) {
  329. return __low2half(dm)*((half) q) + __high2half(dm);
  330. }
  331. #endif // FP16_AVAILABLE
  332. return __low2float(dm)*((float) q) + __high2float(dm);
  333. }
  334. template <typename T>
  335. static __device__ __forceinline__ T dequantize_1_q5_0(const void * __restrict__ vx, const int64_t i) {
  336. const block_q5_0 * x = (const block_q5_0 *) vx;
  337. const int64_t ib = i / QK5_0;
  338. const int idq = i % QK5_0;
  339. const int iqs = i % (QK5_0/2);
  340. const int shift = (i % QK5_0) / (QK5_0/2);
  341. const T d = x[ib].d;
  342. const int ql0 = x[ib].qs[iqs];
  343. const int qh0 = get_int_b2(x[ib].qh, 0);
  344. const int ql = ((ql0 >> (4*shift)) & 0x0F);
  345. const int qh = ((qh0 >> idq) << 4) & 0x10;
  346. const int q = (ql | qh) - 16;
  347. #ifdef FP16_AVAILABLE
  348. if (std::is_same<T, half>::value) {
  349. return ((half) d)*((half) q);
  350. }
  351. #endif // FP16_AVAILABLE
  352. return ((float) d)*((float) q);
  353. }
  354. template <typename T>
  355. static __device__ __forceinline__ T dequantize_1_q5_1(const void * __restrict__ vx, const int64_t i) {
  356. const block_q5_1 * x = (const block_q5_1 *) vx;
  357. const int64_t ib = i / QK5_1;
  358. const int idq = i % QK5_1;
  359. const int iqs = i % (QK5_1/2);
  360. const int shift = (i % QK5_1) / (QK5_1/2);
  361. const half2 dm = x[ib].dm;
  362. const int ql0 = x[ib].qs[iqs];
  363. const int qh0 = get_int_b4(x[ib].qh, 0);
  364. const int ql = ((ql0 >> (4*shift)) & 0x0F);
  365. const int qh = ((qh0 >> idq) << 4) & 0x10;
  366. const int q = (ql | qh);
  367. #ifdef FP16_AVAILABLE
  368. if (std::is_same<T, half>::value) {
  369. return __low2half(dm)*((half) q) + __high2half(dm);
  370. }
  371. #endif // FP16_AVAILABLE
  372. return __low2float(dm)*((float) q) + __high2float(dm);
  373. }
  374. template <typename T>
  375. static __device__ __forceinline__ T dequantize_1_q8_0(const void * __restrict__ vx, const int64_t i) {
  376. const block_q8_0 * x = (const block_q8_0 *) vx;
  377. const int64_t ib = i / QK8_0;
  378. const int iqs = i % QK8_0;
  379. const T d = x[ib].d;
  380. const int q = x[ib].qs[iqs];
  381. #ifdef FP16_AVAILABLE
  382. if (std::is_same<T, half>::value) {
  383. return ((half) d)*((half) q);
  384. }
  385. #endif // FP16_AVAILABLE
  386. return ((float) d)*((float) q);
  387. }
  388. template <typename T>
  389. static __device__ __forceinline__ T dequantize_1_f16(const void * __restrict__ vx, const int64_t i) {
  390. const half * x = (const half *) vx;
  391. return x[i];
  392. }
  393. template <int D>
  394. constexpr __device__ vec_dot_KQ_f16_t get_vec_dot_KQ_f16(ggml_type type_K) {
  395. return type_K == GGML_TYPE_Q4_0 ? vec_dot_fattn_vec_KQ_q4_0<half, D> :
  396. type_K == GGML_TYPE_Q4_1 ? vec_dot_fattn_vec_KQ_q4_1<half, D> :
  397. type_K == GGML_TYPE_Q5_0 ? vec_dot_fattn_vec_KQ_q5_0<half, D> :
  398. type_K == GGML_TYPE_Q5_1 ? vec_dot_fattn_vec_KQ_q5_1<half, D> :
  399. type_K == GGML_TYPE_Q8_0 ? vec_dot_fattn_vec_KQ_q8_0<half, D> :
  400. type_K == GGML_TYPE_F16 ? vec_dot_fattn_vec_KQ_f16<half, D> :
  401. nullptr;
  402. }
  403. template <int D>
  404. constexpr __device__ vec_dot_KQ_f32_t get_vec_dot_KQ_f32(ggml_type type_K) {
  405. return type_K == GGML_TYPE_Q4_0 ? vec_dot_fattn_vec_KQ_q4_0<float, D> :
  406. type_K == GGML_TYPE_Q4_1 ? vec_dot_fattn_vec_KQ_q4_1<float, D> :
  407. type_K == GGML_TYPE_Q5_0 ? vec_dot_fattn_vec_KQ_q5_0<float, D> :
  408. type_K == GGML_TYPE_Q5_1 ? vec_dot_fattn_vec_KQ_q5_1<float, D> :
  409. type_K == GGML_TYPE_Q8_0 ? vec_dot_fattn_vec_KQ_q8_0<float, D> :
  410. type_K == GGML_TYPE_F16 ? vec_dot_fattn_vec_KQ_f16<float, D> :
  411. nullptr;
  412. }
  413. constexpr __device__ dequantize_1_f16_t get_dequantize_1_f16(ggml_type type_V) {
  414. return type_V == GGML_TYPE_Q4_0 ? dequantize_1_q4_0<half> :
  415. type_V == GGML_TYPE_Q4_1 ? dequantize_1_q4_1<half> :
  416. type_V == GGML_TYPE_Q5_0 ? dequantize_1_q5_0<half> :
  417. type_V == GGML_TYPE_Q5_1 ? dequantize_1_q5_1<half> :
  418. type_V == GGML_TYPE_Q8_0 ? dequantize_1_q8_0<half> :
  419. type_V == GGML_TYPE_F16 ? dequantize_1_f16<half> :
  420. nullptr;
  421. }
  422. constexpr __device__ dequantize_1_f32_t get_dequantize_1_f32(ggml_type type_V) {
  423. return type_V == GGML_TYPE_Q4_0 ? dequantize_1_q4_0<float> :
  424. type_V == GGML_TYPE_Q4_1 ? dequantize_1_q4_1<float> :
  425. type_V == GGML_TYPE_Q5_0 ? dequantize_1_q5_0<float> :
  426. type_V == GGML_TYPE_Q5_1 ? dequantize_1_q5_1<float> :
  427. type_V == GGML_TYPE_Q8_0 ? dequantize_1_q8_0<float> :
  428. type_V == GGML_TYPE_F16 ? dequantize_1_f16<float> :
  429. nullptr;
  430. }
  431. template<int D, int parallel_blocks> // D == head size
  432. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  433. __launch_bounds__(D, 1)
  434. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  435. static __global__ void flash_attn_combine_results(
  436. const float * __restrict__ VKQ_parts,
  437. const float2 * __restrict__ VKQ_meta,
  438. float * __restrict__ dst) {
  439. VKQ_parts += parallel_blocks*D * gridDim.y*blockIdx.x;
  440. VKQ_meta += parallel_blocks * gridDim.y*blockIdx.x;
  441. dst += D * gridDim.y*blockIdx.x;
  442. const int tid = threadIdx.x;
  443. __builtin_assume(tid < D);
  444. __shared__ float2 meta[parallel_blocks];
  445. if (tid < 2*parallel_blocks) {
  446. ((float *) meta)[threadIdx.x] = ((const float *)VKQ_meta) [blockIdx.y*(2*parallel_blocks) + tid];
  447. }
  448. __syncthreads();
  449. float kqmax = meta[0].x;
  450. #pragma unroll
  451. for (int l = 1; l < parallel_blocks; ++l) {
  452. kqmax = max(kqmax, meta[l].x);
  453. }
  454. float VKQ_numerator = 0.0f;
  455. float VKQ_denominator = 0.0f;
  456. #pragma unroll
  457. for (int l = 0; l < parallel_blocks; ++l) {
  458. const float diff = meta[l].x - kqmax;
  459. const float KQ_max_scale = expf(diff);
  460. const uint32_t ftz_mask = 0xFFFFFFFF * (diff > SOFTMAX_FTZ_THRESHOLD);
  461. *((uint32_t *) &KQ_max_scale) &= ftz_mask;
  462. VKQ_numerator += KQ_max_scale * VKQ_parts[l*gridDim.y*D + blockIdx.y*D + tid];
  463. VKQ_denominator += KQ_max_scale * meta[l].y;
  464. }
  465. dst[blockIdx.y*D + tid] = VKQ_numerator / VKQ_denominator;
  466. }
  467. static void on_no_fattn_vec_case(const int D) {
  468. if (D == 64) {
  469. fprintf(stderr, "Unsupported KV type combination for head_size 64.\n");
  470. fprintf(stderr, "By default only f16 KV cache is supported.\n");
  471. fprintf(stderr, "Compile with GGML_CUDA_FA_ALL_QUANTS for V cache quantization support.\n");
  472. GGML_ABORT("fatal error");
  473. } else if (D == 128) {
  474. fprintf(stderr, "Unsupported KV type combination for head_size 128.\n");
  475. fprintf(stderr, "Supported combinations:\n");
  476. fprintf(stderr, " - K == q4_0, V == q4_0, 4.50 BPV\n");
  477. fprintf(stderr, " - K == q8_0, V == q8_0, 8.50 BPV\n");
  478. fprintf(stderr, " - K == f16, V == f16, 16.00 BPV\n");
  479. fprintf(stderr, "Compile with GGML_CUDA_FA_ALL_QUANTS for all combinations of q4_0, q4_1, q5_0, q5_1, q8_0, and f16.\n");
  480. GGML_ABORT("fatal error");
  481. } else {
  482. fprintf(stderr, "Unsupported KV type combination for head_size 256.\n");
  483. fprintf(stderr, "Only f16 is supported.\n");
  484. GGML_ABORT("fatal error");
  485. }
  486. }
  487. template <int D, int parallel_blocks>
  488. void launch_fattn(
  489. ggml_backend_cuda_context & ctx, ggml_tensor * dst, fattn_kernel_t fattn_kernel,
  490. const int nwarps, const int cols_per_block, const bool need_f16_K, const bool need_f16_V
  491. ) {
  492. const ggml_tensor * Q = dst->src[0];
  493. const ggml_tensor * K = dst->src[1];
  494. const ggml_tensor * V = dst->src[2];
  495. const ggml_tensor * mask = dst->src[3];
  496. ggml_tensor * KQV = dst;
  497. GGML_ASSERT(Q->type == GGML_TYPE_F32);
  498. GGML_ASSERT(KQV->type == GGML_TYPE_F32);
  499. GGML_ASSERT(!mask || mask->type == GGML_TYPE_F16);
  500. GGML_ASSERT(!mask || mask->ne[1] >= GGML_PAD(Q->ne[1], 16) &&
  501. "the Flash-Attention CUDA kernel requires the mask to be padded to 16 and at least n_queries big");
  502. GGML_ASSERT(K->ne[1] % FATTN_KQ_STRIDE == 0 && "Incorrect KV cache padding.");
  503. ggml_cuda_pool & pool = ctx.pool();
  504. cudaStream_t main_stream = ctx.stream();
  505. ggml_cuda_pool_alloc<half> K_f16(pool);
  506. ggml_cuda_pool_alloc<half> V_f16(pool);
  507. ggml_cuda_pool_alloc<float> dst_tmp(pool);
  508. ggml_cuda_pool_alloc<float2> dst_tmp_meta(pool);
  509. char * K_data = (char *) K->data;
  510. size_t nb11 = K->nb[1];
  511. size_t nb12 = K->nb[2];
  512. size_t nb13 = K->nb[3];
  513. char * V_data = (char *) V->data;
  514. size_t nb21 = V->nb[1];
  515. size_t nb22 = V->nb[2];
  516. size_t nb23 = V->nb[3];
  517. if (need_f16_K && K->type != GGML_TYPE_F16) {
  518. K_f16.alloc(ggml_nelements(K));
  519. to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(K->type);
  520. to_fp16(K_data, K_f16.ptr, ggml_nelements(K), main_stream);
  521. K_data = (char *) K_f16.ptr;
  522. const size_t bs = ggml_blck_size(K->type);
  523. const size_t ts = ggml_type_size(K->type);
  524. nb11 = nb11*bs*sizeof(half)/ts;
  525. nb12 = nb12*bs*sizeof(half)/ts;
  526. nb13 = nb13*bs*sizeof(half)/ts;
  527. }
  528. if (need_f16_V && V->type != GGML_TYPE_F16) {
  529. V_f16.alloc(ggml_nelements(V));
  530. to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(V->type);
  531. to_fp16(V_data, V_f16.ptr, ggml_nelements(V), main_stream);
  532. V_data = (char *) V_f16.ptr;
  533. const size_t bs = ggml_blck_size(V->type);
  534. const size_t ts = ggml_type_size(V->type);
  535. nb21 = nb21*bs*sizeof(half)/ts;
  536. nb22 = nb22*bs*sizeof(half)/ts;
  537. nb23 = nb23*bs*sizeof(half)/ts;
  538. }
  539. if (parallel_blocks > 1) {
  540. dst_tmp.alloc(parallel_blocks*ggml_nelements(KQV));
  541. dst_tmp_meta.alloc(parallel_blocks*ggml_nrows(KQV));
  542. }
  543. const dim3 block_dim(WARP_SIZE, nwarps, 1);
  544. const dim3 blocks_num(parallel_blocks*((Q->ne[1] + cols_per_block - 1) / cols_per_block), Q->ne[2], Q->ne[3]);
  545. const int shmem = 0;
  546. float scale = 1.0f;
  547. float max_bias = 0.0f;
  548. float logit_softcap = 0.0f;
  549. memcpy(&scale, (float *) KQV->op_params + 0, sizeof(float));
  550. memcpy(&max_bias, (float *) KQV->op_params + 1, sizeof(float));
  551. memcpy(&logit_softcap, (float *) KQV->op_params + 2, sizeof(float));
  552. if (logit_softcap != 0.0f) {
  553. scale /= logit_softcap;
  554. }
  555. const uint32_t n_head = Q->ne[2];
  556. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head));
  557. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  558. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  559. fattn_kernel<<<blocks_num, block_dim, shmem, main_stream>>>(
  560. (const char *) Q->data,
  561. K_data,
  562. V_data,
  563. mask ? ((const char *) mask->data) : nullptr,
  564. (parallel_blocks) == 1 ? (float *) KQV->data : dst_tmp.ptr, dst_tmp_meta.ptr,
  565. scale, max_bias, m0, m1, n_head_log2, logit_softcap,
  566. Q->ne[0], Q->ne[1], Q->ne[2], Q->ne[3],
  567. K->ne[0], K->ne[1], K->ne[2], K->ne[3],
  568. mask ? mask->ne[1] : 0, mask ? mask->nb[1] : 0,
  569. Q->nb[1], Q->nb[2], Q->nb[3],
  570. nb11, nb12, nb13,
  571. nb21, nb22, nb23,
  572. KQV->ne[0], KQV->ne[1], KQV->ne[2], KQV->ne[3]
  573. );
  574. CUDA_CHECK(cudaGetLastError());
  575. if ((parallel_blocks) == 1) {
  576. return;
  577. }
  578. const dim3 block_dim_combine(D, 1, 1);
  579. const dim3 blocks_num_combine(Q->ne[1], blocks_num.y, blocks_num.z);
  580. const int shmem_combine = 0;
  581. flash_attn_combine_results<D, parallel_blocks>
  582. <<<blocks_num_combine, block_dim_combine, shmem_combine, main_stream>>>
  583. (dst_tmp.ptr, dst_tmp_meta.ptr, (float *) KQV->data);
  584. CUDA_CHECK(cudaGetLastError());
  585. }