fattn-vec-f16.cuh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "common.cuh"
  2. #include "fattn-common.cuh"
  3. template<int D, int ncols, int parallel_blocks, ggml_type type_K, ggml_type type_V> // D == head size
  4. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5. __launch_bounds__(D, 1)
  6. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  7. static __global__ void flash_attn_vec_ext_f16(
  8. const char * __restrict__ Q,
  9. const char * __restrict__ K,
  10. const char * __restrict__ V,
  11. const char * __restrict__ mask,
  12. float * __restrict__ dst,
  13. float2 * __restrict__ dst_meta,
  14. const float scale,
  15. const float max_bias,
  16. const float m0,
  17. const float m1,
  18. const uint32_t n_head_log2,
  19. const int ne00,
  20. const int ne01,
  21. const int ne02,
  22. const int ne03,
  23. const int ne10,
  24. const int ne11,
  25. const int ne12,
  26. const int ne13,
  27. const int ne31,
  28. const int nb31,
  29. const int nb01,
  30. const int nb02,
  31. const int nb03,
  32. const int nb11,
  33. const int nb12,
  34. const int nb13,
  35. const int nb21,
  36. const int nb22,
  37. const int nb23,
  38. const int ne0,
  39. const int ne1,
  40. const int ne2,
  41. const int ne3) {
  42. #ifdef FP16_AVAILABLE
  43. //In this kernel Q, K, V are matrices while i, j, k are matrix indices.
  44. constexpr vec_dot_KQ_f16_t vec_dot_KQ = get_vec_dot_KQ_f16<D>(type_K);
  45. constexpr bool Q_q8_1 = type_K != GGML_TYPE_F16;
  46. constexpr dequantize_1_f16_t dequantize_1_v = get_dequantize_1_f16(type_V);
  47. const int ic0 = (blockIdx.x / parallel_blocks) * ncols; // Index of the Q/QKV column to work on.
  48. const int ip = blockIdx.x % parallel_blocks; // Index in group of blocks running for the same column in parallel.
  49. const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
  50. Q += nb02* blockIdx.y + nb01*ic0;
  51. K += nb12*(blockIdx.y / gqa_ratio);
  52. V += nb22*(blockIdx.y / gqa_ratio);
  53. const half * maskh = (const half *) mask + ne11*ic0;
  54. const float slopef = get_alibi_slope(max_bias, blockIdx.y, n_head_log2, m0, m1);
  55. const half slopeh = __float2half(slopef);
  56. static_assert(D % (2*WARP_SIZE) == 0, "D not divisible by 2*WARP_SIZE == 64.");
  57. constexpr int nwarps = D / WARP_SIZE;
  58. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  59. __builtin_assume(tid < D);
  60. __shared__ half KQ[ncols*D];
  61. half2 * KQ2 = (half2 *) KQ;
  62. half kqmax[ncols];
  63. #pragma unroll
  64. for (int j = 0; j < ncols; ++j) {
  65. kqmax[j] = -HALF_MAX_HALF;
  66. }
  67. half kqsum[ncols] = {0.0f};
  68. __shared__ half kqmax_shared[ncols][WARP_SIZE];
  69. __shared__ half kqsum_shared[ncols][WARP_SIZE];
  70. #pragma unroll
  71. for (int j = 0; j < ncols; ++j) {
  72. if (threadIdx.y == 0) {
  73. kqmax_shared[j][threadIdx.x] = -HALF_MAX_HALF;
  74. kqsum_shared[j][threadIdx.x] = 0.0f;
  75. }
  76. }
  77. __syncthreads();
  78. // Convert Q to half2 (f16 K) or q8_1 (quantized K) and store in registers:
  79. half2 Q_h2[ncols][D/(2*WARP_SIZE)];
  80. int Q_i32[ncols][D/(sizeof(int)*QK8_1) == 0 ? 1 : D/(sizeof(int)*QK8_1)];
  81. half2 Q_ds[ncols][D/QK8_1 == 0 ? 1 : D/QK8_1];
  82. if (Q_q8_1) {
  83. #pragma unroll
  84. for (int j0 = 0; j0 < ncols; j0 += nwarps) {
  85. const int j = j0 + threadIdx.y;
  86. if (j0 + nwarps > ncols && j >= ncols) {
  87. break;
  88. }
  89. // Reuse KQ as temporary storage for converting Q to q8_1:
  90. int * tmp_q_i32 = (int *) &KQ[j*D];
  91. half2 * tmp_q_ds = (half2 *) (tmp_q_i32 + D/sizeof(int));
  92. // Set memory to zero if out of bounds:
  93. if (ncols > 2 && ic0 + j >= ne01) {
  94. #pragma unroll
  95. for (int i0 = 0; i0 < D/sizeof(int); i0 += WARP_SIZE) {
  96. const int i = i0 + threadIdx.x;
  97. tmp_q_i32[i] = 0;
  98. }
  99. if (threadIdx.x < D/QK8_1) {
  100. tmp_q_ds[threadIdx.x] = make_half2(0.0f, 0.0f);
  101. }
  102. continue;
  103. }
  104. const float * Q_f = (const float *) (Q + j*nb01);
  105. #pragma unroll
  106. for (int i0 = 0; i0 < D/sizeof(int); i0 += WARP_SIZE) {
  107. quantize_q8_1_to_shared<half2>(Q_f + 4*i0, scale, tmp_q_i32, tmp_q_ds);
  108. }
  109. }
  110. __syncthreads();
  111. #pragma unroll
  112. for (int j = 0; j < ncols; ++j) {
  113. int * tmp_q_i32 = (int *) &KQ[j*D];
  114. half2 * tmp_q_ds = (half2 *) (tmp_q_i32 + D/sizeof(int));
  115. #pragma unroll
  116. for (int i0 = 0; i0 < D/sizeof(int); i0 += WARP_SIZE) {
  117. const int i = i0 + threadIdx.x;
  118. Q_i32[j][i0/WARP_SIZE] = tmp_q_i32[i];
  119. Q_ds[j][i0/WARP_SIZE] = tmp_q_ds[i/QI8_1];
  120. }
  121. }
  122. __syncthreads();
  123. } else {
  124. #pragma unroll
  125. for (int j = 0; j < ncols; ++j) {
  126. const float2 * Q_f2_j = (const float2 *) (Q + j*nb01);
  127. #pragma unroll
  128. for (int i0 = 0; i0 < D/2; i0 += WARP_SIZE) {
  129. const int i = i0 + threadIdx.x;
  130. const float2 tmp = ncols <= 2 || ic0 + j < ne01 ? Q_f2_j[i] : make_float2(0.0f, 0.0f);
  131. Q_h2[j][i0/WARP_SIZE] = make_half2(scale, scale) * make_half2(tmp.x, tmp.y);
  132. }
  133. }
  134. }
  135. #pragma unroll
  136. for (int j = 0; j < ncols; ++j) {
  137. KQ[j*D + tid] = -HALF_MAX_HALF;
  138. }
  139. half2 VKQ[ncols] = {{0.0f, 0.0f}};
  140. const int k_start = parallel_blocks == 1 ? 0 : ip*D;
  141. for (int k_VKQ_0 = k_start; k_VKQ_0 < ne11; k_VKQ_0 += parallel_blocks*D) {
  142. // Calculate KQ tile and keep track of new maximum KQ values:
  143. // For unknown reasons using a half array of size 1 for kqmax_new causes a performance regression,
  144. // see https://github.com/ggerganov/llama.cpp/pull/7061 .
  145. // Therefore this variable is defined twice but only used once (so that the compiler can optimize out the unused variable).
  146. half kqmax_new = kqmax[0];
  147. half kqmax_new_arr[ncols];
  148. #pragma unroll
  149. for (int j = 0; j < ncols; ++j) {
  150. kqmax_new_arr[j] = kqmax[j];
  151. }
  152. #pragma unroll
  153. for (int i_KQ_0 = 0; i_KQ_0 < D; i_KQ_0 += nwarps) {
  154. const int i_KQ = i_KQ_0 + threadIdx.y;
  155. if ((i_KQ_0 + nwarps > D && i_KQ >= D) || (FATTN_KQ_STRIDE % D != 0 && k_VKQ_0 + i_KQ >= ne11)) {
  156. break;
  157. }
  158. #pragma unroll
  159. for (int j = 0; j < ncols; ++j) {
  160. half sum = vec_dot_KQ(K + (k_VKQ_0 + i_KQ)*nb11, Q_h2[j], Q_i32[j], Q_ds[j]);
  161. sum = warp_reduce_sum(sum);
  162. sum += mask ? slopeh*maskh[j*ne11 + k_VKQ_0 + i_KQ] : __float2half(0.0f);
  163. if (ncols == 1) {
  164. kqmax_new = ggml_cuda_hmax(kqmax_new, sum);
  165. } else {
  166. kqmax_new_arr[j] = ggml_cuda_hmax(kqmax_new_arr[j], sum);
  167. }
  168. if (threadIdx.x == 0) {
  169. KQ[j*D + i_KQ] = sum;
  170. }
  171. }
  172. }
  173. #pragma unroll
  174. for (int j = 0; j < ncols; ++j) {
  175. half kqmax_new_j = ncols == 1 ? kqmax_new : kqmax_new_arr[j];
  176. kqmax_new_j = warp_reduce_max(kqmax_new_j);
  177. if (threadIdx.x == 0) {
  178. kqmax_shared[j][threadIdx.y] = kqmax_new_j;
  179. }
  180. }
  181. __syncthreads();
  182. #pragma unroll
  183. for (int j = 0; j < ncols; ++j) {
  184. half kqmax_new_j = kqmax_shared[j][threadIdx.x];
  185. kqmax_new_j = warp_reduce_max(kqmax_new_j);
  186. const half KQ_max_scale = hexp(kqmax[j] - kqmax_new_j);
  187. kqmax[j] = kqmax_new_j;
  188. const half val = hexp(KQ[j*D + tid] - kqmax[j]);
  189. kqsum[j] = kqsum[j]*KQ_max_scale + val;
  190. KQ[j*D + tid] = val;
  191. VKQ[j] *= __half2half2(KQ_max_scale);
  192. }
  193. __syncthreads();
  194. #pragma unroll
  195. for (int k0 = 0; k0 < D; k0 += 2) {
  196. if (FATTN_KQ_STRIDE % D != 0 && k_VKQ_0 + k0 >= ne11) {
  197. break;
  198. }
  199. half2 V_k;
  200. reinterpret_cast<half&>(V_k.x) = dequantize_1_v(V + (k_VKQ_0 + k0 + 0)*nb21, tid);
  201. reinterpret_cast<half&>(V_k.y) = dequantize_1_v(V + (k_VKQ_0 + k0 + 1)*nb21, tid);
  202. #pragma unroll
  203. for (int j = 0; j < ncols; ++j) {
  204. VKQ[j] += V_k*KQ2[j*(D/2) + k0/2];
  205. }
  206. }
  207. __syncthreads();
  208. }
  209. #pragma unroll
  210. for (int j = 0; j < ncols; ++j) {
  211. kqsum[j] = warp_reduce_sum(kqsum[j]);
  212. if (threadIdx.x == 0) {
  213. kqsum_shared[j][threadIdx.y] = kqsum[j];
  214. }
  215. }
  216. __syncthreads();
  217. #pragma unroll
  218. for (int j_VKQ = 0; j_VKQ < ncols; ++j_VKQ) {
  219. if (ncols > 2 && ic0 + j_VKQ >= ne01) {
  220. break;
  221. }
  222. kqsum[j_VKQ] = kqsum_shared[j_VKQ][threadIdx.x];
  223. kqsum[j_VKQ] = warp_reduce_sum(kqsum[j_VKQ]);
  224. half dst_val = (__low2half(VKQ[j_VKQ]) + __high2half(VKQ[j_VKQ]));
  225. if (parallel_blocks == 1) {
  226. dst_val /= kqsum[j_VKQ];
  227. }
  228. const int j_dst = (ic0 + j_VKQ)*parallel_blocks + ip;
  229. dst[j_dst*D*gridDim.y + D*blockIdx.y + tid] = dst_val;
  230. }
  231. if (parallel_blocks != 1 && tid < ncols && (ncols <= 2 || ic0 + tid < ne01)) {
  232. dst_meta[(ic0 + tid)*gridDim.y*parallel_blocks + blockIdx.y*parallel_blocks + ip] = make_float2(kqmax[tid], kqsum[tid]);
  233. }
  234. #else
  235. NO_DEVICE_CODE;
  236. #endif // FP16_AVAILABLE
  237. }
  238. template <int D, int cols_per_block, int parallel_blocks, ggml_type type_K, ggml_type type_V>
  239. void ggml_cuda_flash_attn_ext_vec_f16_case_impl(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  240. constexpr int nwarps = D/WARP_SIZE;
  241. fattn_kernel_t fattn_kernel = flash_attn_vec_ext_f16<D, cols_per_block, parallel_blocks, type_K, type_V>;
  242. constexpr bool need_f16_K = D != 128;
  243. constexpr bool need_f16_V = D != 128 && D != 64;
  244. launch_fattn<D, parallel_blocks>(ctx, dst, fattn_kernel, nwarps, cols_per_block, need_f16_K, need_f16_V);
  245. }
  246. template <int D, ggml_type type_K, ggml_type type_V>
  247. void ggml_cuda_flash_attn_ext_vec_f16_case(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  248. ggml_tensor * KQV = dst;
  249. ggml_tensor * Q = dst->src[0];
  250. ggml_tensor * K = dst->src[1];
  251. ggml_tensor * V = dst->src[2];
  252. const int32_t precision = KQV->op_params[2];
  253. GGML_ASSERT(precision == GGML_PREC_DEFAULT);
  254. GGML_ASSERT(K->type == type_K);
  255. GGML_ASSERT(V->type == type_V);
  256. if (Q->ne[1] == 1) {
  257. constexpr int cols_per_block = 1;
  258. constexpr int parallel_blocks = 4;
  259. ggml_cuda_flash_attn_ext_vec_f16_case_impl<D, cols_per_block, parallel_blocks, type_K, type_V>(ctx, dst);
  260. return;
  261. }
  262. if (Q->ne[1] == 2) {
  263. constexpr int cols_per_block = 2;
  264. constexpr int parallel_blocks = 4;
  265. ggml_cuda_flash_attn_ext_vec_f16_case_impl<D, cols_per_block, parallel_blocks, type_K, type_V>(ctx, dst);
  266. return;
  267. }
  268. if (Q->ne[1] <= 4) {
  269. constexpr int cols_per_block = 4;
  270. constexpr int parallel_blocks = 4;
  271. ggml_cuda_flash_attn_ext_vec_f16_case_impl<D, cols_per_block, parallel_blocks, type_K, type_V>(ctx, dst);
  272. return;
  273. }
  274. if (Q->ne[1] <= 8) {
  275. constexpr int cols_per_block = 8;
  276. constexpr int parallel_blocks = 4;
  277. ggml_cuda_flash_attn_ext_vec_f16_case_impl<D, cols_per_block, parallel_blocks, type_K, type_V>(ctx, dst);
  278. return;
  279. }
  280. constexpr int cols_per_block = 8;
  281. constexpr int parallel_blocks = 1;
  282. ggml_cuda_flash_attn_ext_vec_f16_case_impl<D, cols_per_block, parallel_blocks, type_K, type_V>(ctx, dst);
  283. }
  284. #define DECL_FATTN_VEC_F16_CASE(D, type_K, type_V) \
  285. template void ggml_cuda_flash_attn_ext_vec_f16_case \
  286. <D, type_K, type_V>(ggml_backend_cuda_context & ctx, ggml_tensor * dst) \
  287. extern DECL_FATTN_VEC_F16_CASE( 64, GGML_TYPE_F16, GGML_TYPE_Q4_0);
  288. extern DECL_FATTN_VEC_F16_CASE( 64, GGML_TYPE_F16, GGML_TYPE_Q4_1);
  289. extern DECL_FATTN_VEC_F16_CASE( 64, GGML_TYPE_F16, GGML_TYPE_Q5_0);
  290. extern DECL_FATTN_VEC_F16_CASE( 64, GGML_TYPE_F16, GGML_TYPE_Q5_1);
  291. extern DECL_FATTN_VEC_F16_CASE( 64, GGML_TYPE_F16, GGML_TYPE_Q8_0);
  292. extern DECL_FATTN_VEC_F16_CASE( 64, GGML_TYPE_F16, GGML_TYPE_F16);
  293. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0);
  294. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_1, GGML_TYPE_Q4_0);
  295. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_0, GGML_TYPE_Q4_0);
  296. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_1, GGML_TYPE_Q4_0);
  297. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0);
  298. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_F16, GGML_TYPE_Q4_0);
  299. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_0, GGML_TYPE_Q4_1);
  300. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_1, GGML_TYPE_Q4_1);
  301. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_0, GGML_TYPE_Q4_1);
  302. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_1, GGML_TYPE_Q4_1);
  303. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q8_0, GGML_TYPE_Q4_1);
  304. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_F16, GGML_TYPE_Q4_1);
  305. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_0, GGML_TYPE_Q5_0);
  306. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0);
  307. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_0, GGML_TYPE_Q5_0);
  308. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_1, GGML_TYPE_Q5_0);
  309. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q8_0, GGML_TYPE_Q5_0);
  310. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_F16, GGML_TYPE_Q5_0);
  311. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_0, GGML_TYPE_Q5_1);
  312. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_1, GGML_TYPE_Q5_1);
  313. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1);
  314. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_1, GGML_TYPE_Q5_1);
  315. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q8_0, GGML_TYPE_Q5_1);
  316. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_F16, GGML_TYPE_Q5_1);
  317. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0);
  318. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_1, GGML_TYPE_Q8_0);
  319. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_0, GGML_TYPE_Q8_0);
  320. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_1, GGML_TYPE_Q8_0);
  321. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0);
  322. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_F16, GGML_TYPE_Q8_0);
  323. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_0, GGML_TYPE_F16);
  324. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q4_1, GGML_TYPE_F16);
  325. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_0, GGML_TYPE_F16);
  326. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q5_1, GGML_TYPE_F16);
  327. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_Q8_0, GGML_TYPE_F16);
  328. extern DECL_FATTN_VEC_F16_CASE(128, GGML_TYPE_F16, GGML_TYPE_F16);
  329. extern DECL_FATTN_VEC_F16_CASE(256, GGML_TYPE_F16, GGML_TYPE_F16);