dmmv.cu 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. #include "dmmv.cuh"
  2. #include "dequantize.cuh"
  3. #include "convert.cuh"
  4. #ifndef K_QUANTS_PER_ITERATION
  5. #define K_QUANTS_PER_ITERATION 2
  6. #else
  7. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  8. #endif
  9. static __global__ void dequantize_mul_mat_vec_q2_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  10. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  11. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  12. if (row > nrows) return;
  13. const int num_blocks_per_row = ncols / QK_K;
  14. const int ib0 = row*num_blocks_per_row;
  15. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  16. float tmp = 0; // partial sum for thread in warp
  17. #if QK_K == 256
  18. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  19. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  20. const int step = 16/K_QUANTS_PER_ITERATION;
  21. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  22. const int in = tid - step*im; // 0...15 or 0...7
  23. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  24. const int q_offset = 32*im + l0;
  25. const int s_offset = 8*im;
  26. const int y_offset = 128*im + l0;
  27. uint32_t aux[4];
  28. const uint8_t * d = (const uint8_t *)aux;
  29. const uint8_t * m = (const uint8_t *)(aux + 2);
  30. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  31. const float * y = yy + i * QK_K + y_offset;
  32. const uint8_t * q = x[i].qs + q_offset;
  33. const float dall = __low2half(x[i].dm);
  34. const float dmin = __high2half(x[i].dm);
  35. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  36. aux[0] = a[0] & 0x0f0f0f0f;
  37. aux[1] = a[1] & 0x0f0f0f0f;
  38. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  39. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  40. float sum1 = 0, sum2 = 0;
  41. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  42. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  43. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  44. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  45. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  46. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  47. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  48. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  49. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  50. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  51. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  52. }
  53. tmp += dall * sum1 - dmin * sum2;
  54. }
  55. #else
  56. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  57. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  58. const int offset = tid * K_QUANTS_PER_ITERATION;
  59. uint32_t uaux[2];
  60. const uint8_t * d = (const uint8_t *)uaux;
  61. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  62. const float * y = yy + i * QK_K + offset;
  63. const uint8_t * q = x[i].qs + offset;
  64. const uint32_t * s = (const uint32_t *)x[i].scales;
  65. uaux[0] = s[0] & 0x0f0f0f0f;
  66. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  67. const float2 dall = __half22float2(x[i].dm);
  68. float sum1 = 0, sum2 = 0;
  69. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  70. const uint8_t ql = q[l];
  71. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  72. + y[l+16] * d[1] * ((ql >> 2) & 3)
  73. + y[l+32] * d[2] * ((ql >> 4) & 3)
  74. + y[l+48] * d[3] * ((ql >> 6) & 3);
  75. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  76. }
  77. tmp += dall.x * sum1 - dall.y * sum2;
  78. }
  79. #endif
  80. // sum up partial sums and write back result
  81. tmp = warp_reduce_sum(tmp);
  82. if (threadIdx.x == 0) {
  83. dst[row] = tmp;
  84. }
  85. }
  86. static __global__ void dequantize_mul_mat_vec_q3_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  87. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  88. if (row > nrows) return;
  89. const int num_blocks_per_row = ncols / QK_K;
  90. const int ib0 = row*num_blocks_per_row;
  91. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  92. float tmp = 0; // partial sum for thread in warp
  93. #if QK_K == 256
  94. const uint16_t kmask1 = 0x0303;
  95. const uint16_t kmask2 = 0x0f0f;
  96. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  97. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  98. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  99. const int step = 16/K_QUANTS_PER_ITERATION;
  100. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  101. const int in = tid - step*im; // 0....15 or 0...7
  102. const uint8_t m = 1 << (4*im);
  103. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  104. const int q_offset = 32*im + l0;
  105. const int y_offset = 128*im + l0;
  106. uint16_t utmp[4];
  107. const int8_t * s = (const int8_t *)utmp;
  108. const uint16_t s_shift = 4*im;
  109. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  110. const float * y = yy + i * QK_K + y_offset;
  111. const uint8_t * q = x[i].qs + q_offset;
  112. const uint8_t * h = x[i].hmask + l0;
  113. const uint16_t * a = (const uint16_t *)x[i].scales;
  114. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  115. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  116. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  117. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  118. const float d = x[i].d;
  119. float sum = 0;
  120. for (int l = 0; l < n; ++l) {
  121. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  122. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  123. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  124. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  125. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  126. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  127. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  128. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  129. }
  130. tmp += d * sum;
  131. }
  132. #else
  133. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  134. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  135. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  136. const int in = offset/8; // 0 or 1
  137. const int im = offset%8; // 0...7
  138. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  139. const float * y = yy + i * QK_K + offset;
  140. const uint8_t * q = x[i].qs + offset;
  141. const uint8_t * s = x[i].scales;
  142. const float dall = (float)x[i].d;
  143. float sum = 0;
  144. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  145. const uint8_t hl = x[i].hmask[im+l] >> in;
  146. const uint8_t ql = q[l];
  147. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  148. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  149. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  150. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  151. }
  152. tmp += sum;
  153. }
  154. #endif
  155. // sum up partial sums and write back result
  156. tmp = warp_reduce_sum(tmp);
  157. if (threadIdx.x == 0) {
  158. dst[row] = tmp;
  159. }
  160. }
  161. static __global__ void dequantize_mul_mat_vec_q4_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  162. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  163. if (row > nrows) return;
  164. const int num_blocks_per_row = ncols / QK_K;
  165. const int ib0 = row*num_blocks_per_row;
  166. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  167. #if QK_K == 256
  168. const uint16_t kmask1 = 0x3f3f;
  169. const uint16_t kmask2 = 0x0f0f;
  170. const uint16_t kmask3 = 0xc0c0;
  171. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  172. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  173. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  174. const int il = tid/step; // 0...3
  175. const int ir = tid - step*il; // 0...7 or 0...3
  176. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  177. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  178. const int in = il%2;
  179. const int l0 = n*(2*ir + in);
  180. const int q_offset = 32*im + l0;
  181. const int y_offset = 64*im + l0;
  182. uint16_t aux[4];
  183. const uint8_t * sc = (const uint8_t *)aux;
  184. #if K_QUANTS_PER_ITERATION == 2
  185. uint32_t q32[4];
  186. const uint8_t * q4 = (const uint8_t *)q32;
  187. #else
  188. uint16_t q16[4];
  189. const uint8_t * q4 = (const uint8_t *)q16;
  190. #endif
  191. float tmp = 0; // partial sum for thread in warp
  192. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  193. const float * y1 = yy + i*QK_K + y_offset;
  194. const float * y2 = y1 + 128;
  195. const float dall = __low2half(x[i].dm);
  196. const float dmin = __high2half(x[i].dm);
  197. const uint16_t * a = (const uint16_t *)x[i].scales;
  198. aux[0] = a[im+0] & kmask1;
  199. aux[1] = a[im+2] & kmask1;
  200. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  201. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  202. #if K_QUANTS_PER_ITERATION == 2
  203. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  204. const uint32_t * q2 = q1 + 16;
  205. q32[0] = q1[0] & 0x0f0f0f0f;
  206. q32[1] = q1[0] & 0xf0f0f0f0;
  207. q32[2] = q2[0] & 0x0f0f0f0f;
  208. q32[3] = q2[0] & 0xf0f0f0f0;
  209. float4 s = {0.f, 0.f, 0.f, 0.f};
  210. float smin = 0;
  211. for (int l = 0; l < 4; ++l) {
  212. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  213. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  214. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  215. }
  216. tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
  217. #else
  218. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  219. const uint16_t * q2 = q1 + 32;
  220. q16[0] = q1[0] & 0x0f0f;
  221. q16[1] = q1[0] & 0xf0f0;
  222. q16[2] = q2[0] & 0x0f0f;
  223. q16[3] = q2[0] & 0xf0f0;
  224. float4 s = {0.f, 0.f, 0.f, 0.f};
  225. float smin = 0;
  226. for (int l = 0; l < 2; ++l) {
  227. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  228. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  229. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  230. }
  231. tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
  232. #endif
  233. }
  234. #else
  235. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  236. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  237. const int step = tid * K_QUANTS_PER_ITERATION;
  238. uint16_t aux16[2];
  239. const uint8_t * s = (const uint8_t *)aux16;
  240. float tmp = 0;
  241. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  242. const uint8_t * q = x[i].qs + step;
  243. const float * y = yy + i*QK_K + step;
  244. const uint16_t * a = (const uint16_t *)x[i].scales;
  245. aux16[0] = a[0] & 0x0f0f;
  246. aux16[1] = (a[0] >> 4) & 0x0f0f;
  247. const float d = (float)x[i].dm[0];
  248. const float m = (float)x[i].dm[1];
  249. float sum = 0.f;
  250. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  251. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  252. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  253. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  254. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  255. }
  256. tmp += sum;
  257. }
  258. #endif
  259. // sum up partial sums and write back result
  260. tmp = warp_reduce_sum(tmp);
  261. if (tid == 0) {
  262. dst[row] = tmp;
  263. }
  264. }
  265. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  266. const int row = blockIdx.x;
  267. const int num_blocks_per_row = ncols / QK_K;
  268. const int ib0 = row*num_blocks_per_row;
  269. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  270. float tmp = 0; // partial sum for thread in warp
  271. #if QK_K == 256
  272. const uint16_t kmask1 = 0x3f3f;
  273. const uint16_t kmask2 = 0x0f0f;
  274. const uint16_t kmask3 = 0xc0c0;
  275. const int tid = threadIdx.x/2; // 0...15
  276. const int ix = threadIdx.x%2;
  277. const int il = tid/4; // 0...3
  278. const int ir = tid - 4*il;// 0...3
  279. const int n = 2;
  280. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  281. const int in = il%2;
  282. const int l0 = n*(2*ir + in);
  283. const int q_offset = 32*im + l0;
  284. const int y_offset = 64*im + l0;
  285. const uint8_t hm1 = 1 << (2*im);
  286. const uint8_t hm2 = hm1 << 4;
  287. uint16_t aux[4];
  288. const uint8_t * sc = (const uint8_t *)aux;
  289. uint16_t q16[8];
  290. const uint8_t * q4 = (const uint8_t *)q16;
  291. for (int i = ix; i < num_blocks_per_row; i += 2) {
  292. const uint8_t * ql1 = x[i].qs + q_offset;
  293. const uint8_t * qh = x[i].qh + l0;
  294. const float * y1 = yy + i*QK_K + y_offset;
  295. const float * y2 = y1 + 128;
  296. const float dall = __low2half(x[i].dm);
  297. const float dmin = __high2half(x[i].dm);
  298. const uint16_t * a = (const uint16_t *)x[i].scales;
  299. aux[0] = a[im+0] & kmask1;
  300. aux[1] = a[im+2] & kmask1;
  301. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  302. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  303. float4 sum = {0.f, 0.f, 0.f, 0.f};
  304. float smin = 0;
  305. const uint16_t * q1 = (const uint16_t *)ql1;
  306. const uint16_t * q2 = q1 + 32;
  307. q16[0] = q1[0] & 0x0f0f;
  308. q16[1] = q1[8] & 0x0f0f;
  309. q16[2] = (q1[0] >> 4) & 0x0f0f;
  310. q16[3] = (q1[8] >> 4) & 0x0f0f;
  311. q16[4] = q2[0] & 0x0f0f;
  312. q16[5] = q2[8] & 0x0f0f;
  313. q16[6] = (q2[0] >> 4) & 0x0f0f;
  314. q16[7] = (q2[8] >> 4) & 0x0f0f;
  315. for (int l = 0; l < n; ++l) {
  316. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  317. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  318. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  319. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  320. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  321. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  322. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  323. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  324. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  325. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  326. }
  327. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  328. }
  329. #else
  330. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  331. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  332. const int step = tid * K_QUANTS_PER_ITERATION;
  333. const int im = step/8;
  334. const int in = step%8;
  335. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  336. const uint8_t * q = x[i].qs + step;
  337. const int8_t * s = x[i].scales;
  338. const float * y = yy + i*QK_K + step;
  339. const float d = x[i].d;
  340. float sum = 0.f;
  341. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  342. const uint8_t h = x[i].qh[in+j] >> im;
  343. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  344. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  345. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  346. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  347. }
  348. tmp += sum;
  349. }
  350. #endif
  351. // sum up partial sums and write back result
  352. tmp = warp_reduce_sum(tmp);
  353. if (threadIdx.x == 0) {
  354. dst[row] = tmp;
  355. }
  356. }
  357. static __global__ void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  358. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  359. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  360. if (row > nrows) return;
  361. const int num_blocks_per_row = ncols / QK_K;
  362. const int ib0 = row*num_blocks_per_row;
  363. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  364. #if QK_K == 256
  365. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  366. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  367. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  368. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  369. const int in = tid - step*im; // 0...15 or 0...7
  370. #if K_QUANTS_PER_ITERATION == 1
  371. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  372. const int is = 0;
  373. #else
  374. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  375. const int is = in / 4;
  376. #endif
  377. const int ql_offset = 64*im + l0;
  378. const int qh_offset = 32*im + l0;
  379. const int s_offset = 8*im + is;
  380. const int y_offset = 128*im + l0;
  381. float tmp = 0; // partial sum for thread in warp
  382. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  383. const float * y = yy + i * QK_K + y_offset;
  384. const uint8_t * ql = x[i].ql + ql_offset;
  385. const uint8_t * qh = x[i].qh + qh_offset;
  386. const int8_t * s = x[i].scales + s_offset;
  387. const float d = x[i].d;
  388. #if K_QUANTS_PER_ITERATION == 1
  389. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  390. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  391. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  392. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  393. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  394. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  395. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  396. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  397. tmp += sum;
  398. #else
  399. float sum = 0;
  400. for (int l = 0; l < 4; ++l) {
  401. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  402. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  403. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  404. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  405. }
  406. tmp += sum;
  407. #endif
  408. }
  409. #else
  410. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  411. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  412. const int step = tid * K_QUANTS_PER_ITERATION;
  413. float tmp = 0; // partial sum for thread in warp
  414. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  415. const float * y = yy + i * QK_K + step;
  416. const uint8_t * ql = x[i].ql + step;
  417. const uint8_t * qh = x[i].qh + step;
  418. const int8_t * s = x[i].scales;
  419. const float d = x[i+0].d;
  420. float sum = 0;
  421. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  422. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  423. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  424. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  425. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  426. }
  427. tmp += sum;
  428. }
  429. #endif
  430. // sum up partial sums and write back result
  431. tmp = warp_reduce_sum(tmp);
  432. if (tid == 0) {
  433. dst[row] = tmp;
  434. }
  435. }
  436. static __device__ void convert_f16(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
  437. const half * x = (const half *) vx;
  438. // automatic half -> float type cast if dfloat == float
  439. v.x = x[ib + iqs + 0];
  440. v.y = x[ib + iqs + 1];
  441. }
  442. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  443. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  444. // qk = quantized weights per x block
  445. // qr = number of quantized weights per data value in x block
  446. const int64_t row = (int64_t)blockIdx.x*blockDim.y + threadIdx.y;
  447. if (row >= nrows) {
  448. return;
  449. }
  450. const int tid = threadIdx.x;
  451. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  452. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  453. const int y_offset = qr == 1 ? 1 : qk/2;
  454. // partial sum for each thread
  455. #ifdef GGML_CUDA_F16
  456. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  457. #else
  458. float tmp = 0.0f;
  459. #endif // GGML_CUDA_F16
  460. for (int i = 0; i < ncols; i += iter_stride) {
  461. const int col = i + vals_per_iter*tid;
  462. const int64_t ib = ((int64_t)row*ncols + col)/qk; // x block index
  463. const int iqs = (col%qk)/qr; // x quant index
  464. const int iybs = col - col%qk; // y block start index
  465. // processing >2 values per i iter is faster for fast GPUs
  466. #pragma unroll
  467. for (int j = 0; j < vals_per_iter; j += 2) {
  468. // process 2 vals per j iter
  469. // dequantize
  470. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  471. dfloat2 v;
  472. dequantize_kernel(vx, ib, iqs + j/qr, v);
  473. // matrix multiplication
  474. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  475. #ifdef GGML_CUDA_F16
  476. tmp += __hmul2(v, {
  477. y[iybs + iqs + j/qr + 0],
  478. y[iybs + iqs + j/qr + y_offset]
  479. });
  480. #else
  481. tmp += v.x * y[iybs + iqs + j/qr + 0];
  482. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  483. #endif // GGML_CUDA_F16
  484. }
  485. }
  486. // sum up partial sums and write back result
  487. tmp = warp_reduce_sum(tmp);
  488. if (tid == 0) {
  489. #ifdef GGML_CUDA_F16
  490. dst[row] = tmp.x + tmp.y;
  491. #else
  492. dst[row] = tmp;
  493. #endif // GGML_CUDA_F16
  494. }
  495. }
  496. static void dequantize_mul_mat_vec_q4_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  497. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  498. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  499. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  500. const dim3 block_nums(block_num_y, 1, 1);
  501. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  502. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  503. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  504. }
  505. static void dequantize_mul_mat_vec_q4_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  506. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  507. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  508. const dim3 block_nums(block_num_y, 1, 1);
  509. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  510. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  511. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  512. }
  513. static void dequantize_mul_mat_vec_q5_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  514. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  515. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  516. const dim3 block_nums(block_num_y, 1, 1);
  517. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  518. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  519. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  520. }
  521. static void dequantize_mul_mat_vec_q5_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  522. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  523. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  524. const dim3 block_nums(block_num_y, 1, 1);
  525. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  526. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  527. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  528. }
  529. static void dequantize_mul_mat_vec_q8_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  530. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  531. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  532. const dim3 block_nums(block_num_y, 1, 1);
  533. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  534. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  535. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  536. }
  537. static void dequantize_mul_mat_vec_q2_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  538. GGML_ASSERT(ncols % QK_K == 0);
  539. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  540. const int block_num_y = (nrows + ny - 1) / ny;
  541. const dim3 block_nums(block_num_y, 1, 1);
  542. const dim3 block_dims(32, ny, 1);
  543. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  544. }
  545. static void dequantize_mul_mat_vec_q3_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  546. GGML_ASSERT(ncols % QK_K == 0);
  547. const int ny = 2 / K_QUANTS_PER_ITERATION;
  548. const int block_num_y = (nrows + ny - 1) / ny;
  549. const dim3 block_nums(block_num_y, 1, 1);
  550. const dim3 block_dims(32, ny, 1);
  551. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  552. }
  553. static void dequantize_mul_mat_vec_q4_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  554. GGML_ASSERT(ncols % QK_K == 0);
  555. const int ny = 2 / K_QUANTS_PER_ITERATION;
  556. const int block_num_y = (nrows + ny - 1) / ny;
  557. const dim3 block_nums(block_num_y, 1, 1);
  558. const dim3 block_dims(32, ny, 1);
  559. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  560. }
  561. static void dequantize_mul_mat_vec_q5_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  562. GGML_ASSERT(ncols % QK_K == 0);
  563. const dim3 block_dims(32, 1, 1);
  564. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  565. }
  566. static void dequantize_mul_mat_vec_q6_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  567. GGML_ASSERT(ncols % QK_K == 0);
  568. const int ny = 2 / K_QUANTS_PER_ITERATION;
  569. const int block_num_y = (nrows + ny - 1) / ny;
  570. const dim3 block_nums(block_num_y, 1, 1);
  571. const dim3 block_dims(32, ny, 1);
  572. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  573. }
  574. static void convert_mul_mat_vec_f16_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  575. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  576. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  577. const dim3 block_nums(block_num_y, 1, 1);
  578. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  579. dequantize_mul_mat_vec<1, 1, convert_f16>
  580. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  581. }
  582. void ggml_cuda_op_dequantize_mul_mat_vec(
  583. ggml_backend_cuda_context & ctx,
  584. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  585. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  586. const int64_t src1_padded_row_size, cudaStream_t stream) {
  587. GGML_UNUSED(ctx);
  588. const int64_t ne00 = src0->ne[0];
  589. const int64_t row_diff = row_high - row_low;
  590. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  591. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  592. #ifdef GGML_CUDA_F16
  593. ggml_cuda_pool_alloc<half> src1_dfloat_a(ctx.pool());
  594. half * src1_dfloat = nullptr; // dfloat == half
  595. bool src1_convert_f16 =
  596. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  597. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  598. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  599. if (src1_convert_f16) {
  600. src1_dfloat = src1_dfloat_a.alloc(ne00);
  601. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  602. GGML_ASSERT(to_fp16_cuda != nullptr);
  603. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  604. }
  605. #else
  606. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  607. #endif // GGML_CUDA_F16
  608. switch (src0->type) {
  609. case GGML_TYPE_Q4_0:
  610. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  611. break;
  612. case GGML_TYPE_Q4_1:
  613. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  614. break;
  615. case GGML_TYPE_Q5_0:
  616. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  617. break;
  618. case GGML_TYPE_Q5_1:
  619. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  620. break;
  621. case GGML_TYPE_Q8_0:
  622. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  623. break;
  624. case GGML_TYPE_Q2_K:
  625. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  626. break;
  627. case GGML_TYPE_Q3_K:
  628. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  629. break;
  630. case GGML_TYPE_Q4_K:
  631. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  632. break;
  633. case GGML_TYPE_Q5_K:
  634. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  635. break;
  636. case GGML_TYPE_Q6_K:
  637. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  638. break;
  639. case GGML_TYPE_F16:
  640. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  641. break;
  642. default:
  643. GGML_ASSERT(false);
  644. break;
  645. }
  646. GGML_UNUSED(src1);
  647. GGML_UNUSED(dst);
  648. GGML_UNUSED(src1_ddq_i);
  649. GGML_UNUSED(src1_ncols);
  650. GGML_UNUSED(src1_padded_row_size);
  651. }