ggml-metal.metal 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  4. #define QK4_0 32
  5. #define QR4_0 2
  6. typedef struct {
  7. half d; // delta
  8. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  9. } block_q4_0;
  10. #define QK4_1 32
  11. typedef struct {
  12. half d; // delta
  13. half m; // min
  14. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  15. } block_q4_1;
  16. static void dequantize_row_q4_0(device const block_q4_0 * x, device float * y, int k) {
  17. const int qk = QK4_0;
  18. assert(k % qk == 0);
  19. const int nb = k / qk;
  20. for (int i = 0; i < nb; i++) {
  21. const half d = x[i].d;
  22. for (int j = 0; j < qk/2; ++j) {
  23. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  24. const int x1 = (x[i].qs[j] >> 4) - 8;
  25. y[i*qk + j + 0 ] = x0*d;
  26. y[i*qk + j + qk/2] = x1*d;
  27. }
  28. }
  29. }
  30. static void dequantize_row_q4_1(device const block_q4_1 * x, device float * y, int k) {
  31. const int qk = QK4_1;
  32. assert(k % qk == 0);
  33. const int nb = k / qk;
  34. for (int i = 0; i < nb; i++) {
  35. const half d = x[i].d;
  36. const half m = x[i].m;
  37. for (int j = 0; j < qk/2; ++j) {
  38. const int x0 = (x[i].qs[j] & 0x0F);
  39. const int x1 = (x[i].qs[j] >> 4);
  40. y[i*qk + j + 0 ] = x0*d + m;
  41. y[i*qk + j + qk/2] = x1*d + m;
  42. }
  43. }
  44. }
  45. kernel void kernel_add(
  46. device const float * src0,
  47. device const float * src1,
  48. device float * dst,
  49. uint tpig[[thread_position_in_grid]]) {
  50. dst[tpig] = src0[tpig] + src1[tpig];
  51. }
  52. kernel void kernel_mul(
  53. device const float * src0,
  54. device const float * src1,
  55. device float * dst,
  56. uint tpig[[thread_position_in_grid]]) {
  57. dst[tpig] = src0[tpig] * src1[tpig];
  58. }
  59. // assumption: src1 is a row
  60. // broadcast src1 into src0
  61. kernel void kernel_mul_row(
  62. device const float * src0,
  63. device const float * src1,
  64. device float * dst,
  65. constant int64_t & ne00,
  66. uint tpig[[thread_position_in_grid]]) {
  67. dst[tpig] = src0[tpig] * src1[tpig % ne00];
  68. }
  69. kernel void kernel_scale(
  70. device const float * src0,
  71. device float * dst,
  72. constant float & scale,
  73. uint tpig[[thread_position_in_grid]]) {
  74. dst[tpig] = src0[tpig] * scale;
  75. }
  76. kernel void kernel_silu(
  77. device const float * src0,
  78. device float * dst,
  79. uint tpig[[thread_position_in_grid]]) {
  80. float x = src0[tpig];
  81. dst[tpig] = x / (1.0f + exp(-x));
  82. }
  83. kernel void kernel_relu(
  84. device const float * src0,
  85. device float * dst,
  86. uint tpig[[thread_position_in_grid]]) {
  87. dst[tpig] = max(0.0f, src0[tpig]);
  88. }
  89. constant float GELU_COEF_A = 0.044715f;
  90. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  91. kernel void kernel_gelu(
  92. device const float * src0,
  93. device float * dst,
  94. uint tpig[[thread_position_in_grid]]) {
  95. float x = src0[tpig];
  96. dst[tpig] = 0.5f*x*(1.0f + tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  97. }
  98. kernel void kernel_soft_max(
  99. device const float * src0,
  100. device float * dst,
  101. constant int64_t & ne00,
  102. constant int64_t & ne01,
  103. constant int64_t & ne02,
  104. threadgroup float * buf [[threadgroup(0)]],
  105. uint3 tgpig[[threadgroup_position_in_grid]],
  106. uint3 tpitg[[thread_position_in_threadgroup]],
  107. uint3 ntg[[threads_per_threadgroup]]) {
  108. const int64_t i03 = tgpig[2];
  109. const int64_t i02 = tgpig[1];
  110. const int64_t i01 = tgpig[0];
  111. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  112. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  113. // parallel max
  114. buf[tpitg[0]] = -INFINITY;
  115. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  116. buf[tpitg[0]] = MAX(buf[tpitg[0]], psrc0[i00]);
  117. }
  118. // reduce
  119. threadgroup_barrier(mem_flags::mem_threadgroup);
  120. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  121. if (tpitg[0] < i) {
  122. buf[tpitg[0]] = MAX(buf[tpitg[0]], buf[tpitg[0] + i]);
  123. }
  124. threadgroup_barrier(mem_flags::mem_threadgroup);
  125. }
  126. // broadcast
  127. if (tpitg[0] == 0) {
  128. buf[0] = buf[0];
  129. }
  130. threadgroup_barrier(mem_flags::mem_threadgroup);
  131. const float max = buf[0];
  132. // parallel sum
  133. buf[tpitg[0]] = 0.0f;
  134. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  135. buf[tpitg[0]] += exp(psrc0[i00] - max);
  136. }
  137. // reduce
  138. threadgroup_barrier(mem_flags::mem_threadgroup);
  139. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  140. if (tpitg[0] < i) {
  141. buf[tpitg[0]] += buf[tpitg[0] + i];
  142. }
  143. threadgroup_barrier(mem_flags::mem_threadgroup);
  144. }
  145. // broadcast
  146. if (tpitg[0] == 0) {
  147. buf[0] = buf[0];
  148. }
  149. threadgroup_barrier(mem_flags::mem_threadgroup);
  150. const float sum = buf[0];
  151. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  152. pdst[i00] = exp(psrc0[i00] - max) / sum;
  153. }
  154. }
  155. kernel void kernel_diag_mask_inf(
  156. device const float * src0,
  157. device float * dst,
  158. constant int64_t & ne00,
  159. constant int64_t & ne01,
  160. constant int & n_past,
  161. uint3 tpig[[thread_position_in_grid]]) {
  162. const int64_t i02 = tpig[2];
  163. const int64_t i01 = tpig[1];
  164. const int64_t i00 = tpig[0];
  165. if (i00 > n_past + i01) {
  166. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  167. } else {
  168. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  169. }
  170. }
  171. kernel void kernel_get_rows_f16(
  172. device const void * src0,
  173. device const int * src1,
  174. device float * dst,
  175. constant int64_t & ne00,
  176. constant uint64_t & nb01,
  177. constant uint64_t & nb1,
  178. uint tpig[[thread_position_in_grid]]) {
  179. const int i = tpig;
  180. const int r = ((device int32_t *) src1)[i];
  181. for (int j = 0; j < ne00; j++) {
  182. dst[i*nb1 + j] = ((device half *) ((device char *) src0 + r*nb01))[j];
  183. }
  184. }
  185. kernel void kernel_get_rows_q4_0(
  186. device const void * src0,
  187. device const int * src1,
  188. device float * dst,
  189. constant int64_t & ne00,
  190. constant uint64_t & nb01,
  191. constant uint64_t & nb1,
  192. uint tpig[[thread_position_in_grid]]) {
  193. const int i = tpig;
  194. const int r = ((device int32_t *) src1)[i];
  195. dequantize_row_q4_0(
  196. (device const block_q4_0 *) ((device char *) src0 + r*nb01),
  197. (device float *) ((device char *) dst + i*nb1), ne00);
  198. }
  199. kernel void kernel_get_rows_q4_1(
  200. device const void * src0,
  201. device const int * src1,
  202. device float * dst,
  203. constant int64_t & ne00,
  204. constant uint64_t & nb01,
  205. constant uint64_t & nb1,
  206. uint tpig[[thread_position_in_grid]]) {
  207. const int i = tpig;
  208. const int r = ((device int32_t *) src1)[i];
  209. dequantize_row_q4_1(
  210. (device const block_q4_1 *) ((device char *) src0 + r*nb01),
  211. (device float *) ((device char *) dst + i*nb1), ne00);
  212. }
  213. kernel void kernel_norm(
  214. device const void * src0,
  215. device float * dst,
  216. constant int64_t & ne00,
  217. constant uint64_t & nb01,
  218. constant float & eps,
  219. threadgroup float * sum [[threadgroup(0)]],
  220. uint tgpig[[threadgroup_position_in_grid]],
  221. uint tpitg[[thread_position_in_threadgroup]],
  222. uint ntg[[threads_per_threadgroup]]) {
  223. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  224. // MEAN
  225. // parallel sum
  226. sum[tpitg] = 0.0f;
  227. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  228. sum[tpitg] += x[i00];
  229. }
  230. // reduce
  231. threadgroup_barrier(mem_flags::mem_threadgroup);
  232. for (uint i = ntg/2; i > 0; i /= 2) {
  233. if (tpitg < i) {
  234. sum[tpitg] += sum[tpitg + i];
  235. }
  236. threadgroup_barrier(mem_flags::mem_threadgroup);
  237. }
  238. // broadcast
  239. if (tpitg == 0) {
  240. sum[0] /= ne00;
  241. }
  242. threadgroup_barrier(mem_flags::mem_threadgroup);
  243. const float mean = sum[0];
  244. // recenter
  245. device float * y = dst + tgpig*ne00;
  246. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  247. y[i00] = x[i00] - mean;
  248. }
  249. // VARIANCE
  250. // parallel sum
  251. sum[tpitg] = 0.0f;
  252. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  253. sum[tpitg] += y[i00] * y[i00];
  254. }
  255. // reduce
  256. threadgroup_barrier(mem_flags::mem_threadgroup);
  257. for (uint i = ntg/2; i > 0; i /= 2) {
  258. if (tpitg < i) {
  259. sum[tpitg] += sum[tpitg + i];
  260. }
  261. threadgroup_barrier(mem_flags::mem_threadgroup);
  262. }
  263. // broadcast
  264. if (tpitg == 0) {
  265. sum[0] /= ne00;
  266. }
  267. threadgroup_barrier(mem_flags::mem_threadgroup);
  268. const float variance = sum[0];
  269. const float scale = 1.0f/sqrt(variance + eps);
  270. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  271. y[i00] = y[i00] * scale;
  272. }
  273. }
  274. kernel void kernel_rms_norm(
  275. device const void * src0,
  276. device float * dst,
  277. constant int64_t & ne00,
  278. constant uint64_t & nb01,
  279. constant float & eps,
  280. threadgroup float * sum [[threadgroup(0)]],
  281. uint tgpig[[threadgroup_position_in_grid]],
  282. uint tpitg[[thread_position_in_threadgroup]],
  283. uint ntg[[threads_per_threadgroup]]) {
  284. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  285. // parallel sum
  286. sum[tpitg] = 0.0f;
  287. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  288. sum[tpitg] += x[i00] * x[i00];
  289. }
  290. // reduce
  291. threadgroup_barrier(mem_flags::mem_threadgroup);
  292. for (uint i = ntg/2; i > 0; i /= 2) {
  293. if (tpitg < i) {
  294. sum[tpitg] += sum[tpitg + i];
  295. }
  296. threadgroup_barrier(mem_flags::mem_threadgroup);
  297. }
  298. // broadcast
  299. if (tpitg == 0) {
  300. sum[0] /= ne00;
  301. }
  302. threadgroup_barrier(mem_flags::mem_threadgroup);
  303. const float mean = sum[0];
  304. const float scale = 1.0f/sqrt(mean + eps);
  305. device float * y = dst + tgpig*ne00;
  306. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  307. y[i00] = x[i00] * scale;
  308. }
  309. }
  310. // putting them in the kernel cause a significant performance penalty
  311. #define N_DST 4 // each SIMD group works on 4 rows
  312. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  313. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  314. kernel void kernel_mul_mat_q4_0_f32(
  315. device const void * src0,
  316. device const float * src1,
  317. device float * dst,
  318. constant int64_t & ne00,
  319. constant int64_t & ne10,
  320. constant int64_t & ne0,
  321. constant int64_t & ne01[[buffer(4)]],
  322. uint2 tgpig[[threadgroup_position_in_grid]],
  323. uint tiisg[[thread_index_in_simdgroup]],
  324. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  325. const int nb = ne00/QK4_0;
  326. const int r0 = tgpig.x;
  327. const int r1 = tgpig.y;
  328. device const block_q4_0 * x = (device const block_q4_0 *) src0 + (r0 * N_SIMDGROUP + sgitg) * N_DST * nb;
  329. device const float * y = (device const float *) src1 + r1*ne10;
  330. block_q4_0 qb_curr, qb_next;
  331. float4 y_curr[8]; // src1 vector cache
  332. float sumf[N_DST]={0.f}, all_sum;
  333. thread float * yl=(thread float *)y_curr;
  334. // bootstrap
  335. qb_curr = x[tiisg];
  336. // each thread in a SIMD group deals with 1 block.
  337. for (int column = 0; column < nb / N_SIMDWIDTH; column++) {
  338. float sumy = 0;
  339. for (int i = 0; i < QK4_0 / 4; i++) {
  340. y_curr[i] = *((device float4 *)(y + N_SIMDWIDTH * (tiisg + column * QK4_0) + 4 * i));
  341. sumy += y_curr[i][0] + y_curr[i][1] + y_curr[i][2] + y_curr[i][3];
  342. }
  343. sumy *= (-8.f);
  344. for (int row = 0; row < N_DST; row++) {
  345. // prefetch next x block
  346. qb_next = x[tiisg + ((row + 1) % N_DST) * nb + (column + ((row + 1) / N_DST)) * N_SIMDWIDTH];
  347. // calculate
  348. float d = qb_curr.d;
  349. float acc = sumy;
  350. for (int i = 0; i < 16; i++) {
  351. acc += yl[i] * (qb_curr.qs[i] & 0xF) + yl[i+16] * (qb_curr.qs[i] >> 4);
  352. }
  353. sumf[row] += d * acc;
  354. qb_curr = qb_next;
  355. }
  356. }
  357. if (nb % N_SIMDWIDTH == 0) {
  358. for (int row = 0; row < N_DST; ++row) {
  359. all_sum = simd_sum(sumf[row]);
  360. if (tiisg == 0 && ((r0 * N_SIMDGROUP + sgitg) * N_DST + row) < ne01) {
  361. dst[r1*ne0 + (r0 * N_SIMDGROUP + sgitg) * N_DST + row] = all_sum;
  362. }
  363. }
  364. } else {
  365. float sumy = 0;
  366. for (int i = 0; i < QK4_0 / 4; i++) {
  367. y_curr[i] = *((device float4 *)(y + N_SIMDWIDTH * (tiisg + (nb / N_SIMDWIDTH) * QK4_0) + 4 * i));
  368. sumy += y_curr[i][0] + y_curr[i][1] + y_curr[i][2] + y_curr[i][3];
  369. }
  370. sumy *= (-8.f);
  371. for (int row = 0; row < N_DST; row++) {
  372. // prefetch next x block
  373. qb_next = x[tiisg + ((row + 1) % N_DST) * nb + (nb / N_SIMDWIDTH + ((row + 1) / N_DST)) * N_SIMDWIDTH];
  374. // calculate
  375. float d = qb_curr.d;
  376. float acc = sumy;
  377. for (int i = 0; i < 16; i++) {
  378. acc += yl[i] * (qb_curr.qs[i] & 0xF) + yl[i+16] * (qb_curr.qs[i] >> 4);
  379. }
  380. if (tiisg < nb % N_SIMDWIDTH) {
  381. sumf[row] += d * acc;
  382. }
  383. qb_curr = qb_next;
  384. all_sum = simd_sum(sumf[row]);
  385. if (tiisg == 0 && ((r0 * N_SIMDGROUP + sgitg) * N_DST + row) < ne01) {
  386. dst[r1*ne0 + (r0 * N_SIMDGROUP + sgitg) * N_DST + row] = all_sum;
  387. }
  388. }
  389. }
  390. }
  391. kernel void kernel_mul_mat_q4_1_f32(
  392. device const void * src0,
  393. device const float * src1,
  394. device float * dst,
  395. constant int64_t & ne00,
  396. constant int64_t & ne10,
  397. constant int64_t & ne0,
  398. constant int64_t & ne01[[buffer(4)]],
  399. uint2 tgpig[[threadgroup_position_in_grid]],
  400. uint tiisg[[thread_index_in_simdgroup]],
  401. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  402. const int nb = ne00/QK4_0;
  403. const int r0 = tgpig.x;
  404. const int r1 = tgpig.y;
  405. device const block_q4_1 * x = (device const block_q4_1 *) src0 + (r0 * N_SIMDGROUP + sgitg) * N_DST * nb;
  406. device const float * y = (device const float *) src1 + r1*ne10;
  407. block_q4_1 qb_curr, qb_next;
  408. float4 y_curr[8]; // src1 vector cache
  409. float sumf[N_DST]={0.f}, all_sum;
  410. thread float * yl=(thread float *)y_curr;
  411. // bootstrap
  412. qb_curr = x[tiisg];
  413. // each thread in a SIMD group deals with 1 block.
  414. for (int column = 0; column < nb / N_SIMDWIDTH; column++) {
  415. float sumy = 0;
  416. for (int i = 0; i < QK4_0 / 4; i++) {
  417. y_curr[i] = *((device float4 *)(y + N_SIMDWIDTH * (tiisg + column * QK4_0) + 4 * i));
  418. sumy += y_curr[i][0] + y_curr[i][1] + y_curr[i][2] + y_curr[i][3];
  419. }
  420. for (int row = 0; row < N_DST; row++) {
  421. // prefetch next x block
  422. qb_next = x[tiisg + ((row + 1) % N_DST) * nb + (column + ((row + 1) / N_DST)) * N_SIMDWIDTH];
  423. // calculate
  424. const float d = qb_curr.d;
  425. const float m = qb_curr.m;
  426. float acc = 0.f;
  427. for (int i = 0; i < 16; i++) {
  428. acc += yl[i] * (qb_curr.qs[i] & 0xF) + yl[i+16] * (qb_curr.qs[i] >> 4);
  429. }
  430. sumf[row] += d * acc + m * sumy;
  431. qb_curr = qb_next;
  432. }
  433. }
  434. if (nb % N_SIMDWIDTH == 0) {
  435. for (int row = 0; row < N_DST; ++row) {
  436. all_sum = simd_sum(sumf[row]);
  437. if (tiisg == 0 && ((r0 * N_SIMDGROUP + sgitg) * N_DST + row) < ne01) {
  438. dst[r1*ne0 + (r0 * N_SIMDGROUP + sgitg) * N_DST + row] = all_sum;
  439. }
  440. }
  441. } else {
  442. float sumy = 0;
  443. for (int i = 0; i < QK4_0 / 4; i++) {
  444. y_curr[i] = *((device float4 *)(y + N_SIMDWIDTH * (tiisg + (nb / N_SIMDWIDTH) * QK4_0) + 4 * i));
  445. sumy += y_curr[i][0] + y_curr[i][1] + y_curr[i][2] + y_curr[i][3];
  446. }
  447. for (int row = 0; row < N_DST; row++) {
  448. // prefetch next x block
  449. qb_next = x[tiisg + ((row + 1) % N_DST) * nb + (nb / N_SIMDWIDTH + ((row + 1) / N_DST)) * N_SIMDWIDTH];
  450. // calculate
  451. const float d = qb_curr.d;
  452. const float m = qb_curr.m;
  453. float acc = 0.f;
  454. for (int i = 0; i < 16; i++) {
  455. acc += yl[i] * (qb_curr.qs[i] & 0xF) + yl[i+16] * (qb_curr.qs[i] >> 4);
  456. }
  457. if (tiisg < nb % N_SIMDWIDTH) {
  458. sumf[row] += d * acc + m * sumy;
  459. }
  460. qb_curr = qb_next;
  461. all_sum = simd_sum(sumf[row]);
  462. if (tiisg == 0 && ((r0 * N_SIMDGROUP + sgitg) * N_DST + row) < ne01) {
  463. dst[r1*ne0 + (r0 * N_SIMDGROUP + sgitg) * N_DST + row] = all_sum;
  464. }
  465. }
  466. }
  467. }
  468. kernel void kernel_mul_mat_f16_f32(
  469. device const char * src0,
  470. device const char * src1,
  471. device float * dst,
  472. constant int64_t & ne00,
  473. constant int64_t & ne01,
  474. constant uint64_t & nb00,
  475. constant uint64_t & nb01,
  476. constant uint64_t & nb02,
  477. constant int64_t & ne10,
  478. constant int64_t & ne11,
  479. constant uint64_t & nb10,
  480. constant uint64_t & nb11,
  481. constant uint64_t & nb12,
  482. constant int64_t & ne0,
  483. constant int64_t & ne1,
  484. threadgroup float * sum [[threadgroup(0)]],
  485. uint3 tgpig[[threadgroup_position_in_grid]],
  486. uint3 tpig[[thread_position_in_grid]],
  487. uint3 tpitg[[thread_position_in_threadgroup]],
  488. uint3 tptg[[threads_per_threadgroup]]) {
  489. const int64_t r0 = tgpig.x;
  490. const int64_t r1 = tgpig.y;
  491. const int64_t im = tgpig.z;
  492. device const half * x = (device const half *) (src0 + r0*nb01 + im*nb02);
  493. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  494. sum[tpitg.x] = 0.0f;
  495. for (int i = tpitg.x; i < ne00; i += tptg.x) {
  496. sum[tpitg.x] += (float) x[i] * (float) y[i];
  497. }
  498. // accumulate the sum from all threads in the threadgroup
  499. threadgroup_barrier(mem_flags::mem_threadgroup);
  500. for (uint i = tptg.x/2; i > 0; i /= 2) {
  501. if (tpitg.x < i) {
  502. sum[tpitg.x] += sum[tpitg.x + i];
  503. }
  504. threadgroup_barrier(mem_flags::mem_threadgroup);
  505. }
  506. if (tpitg.x == 0) {
  507. dst[im*ne1*ne0 + r1*ne0 + r0] = sum[0];
  508. }
  509. }
  510. kernel void kernel_alibi_f32(
  511. device const float * src0,
  512. device float * dst,
  513. constant int64_t & ne00,
  514. constant int64_t & ne01,
  515. constant int64_t & ne02,
  516. constant int64_t & ne03,
  517. constant uint64_t & nb00,
  518. constant uint64_t & nb01,
  519. constant uint64_t & nb02,
  520. constant uint64_t & nb03,
  521. constant int64_t & ne0,
  522. constant int64_t & ne1,
  523. constant int64_t & ne2,
  524. constant int64_t & ne3,
  525. constant uint64_t & nb0,
  526. constant uint64_t & nb1,
  527. constant uint64_t & nb2,
  528. constant uint64_t & nb3,
  529. constant float & m0,
  530. uint3 tgpig[[threadgroup_position_in_grid]],
  531. uint3 tpitg[[thread_position_in_threadgroup]],
  532. uint3 ntg[[threads_per_threadgroup]]) {
  533. const int64_t i03 = tgpig[2];
  534. const int64_t i02 = tgpig[1];
  535. const int64_t i01 = tgpig[0];
  536. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  537. const int64_t i3 = n / (ne2*ne1*ne0);
  538. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  539. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  540. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  541. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  542. float m_k = pow(m0, i2 + 1);
  543. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  544. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  545. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  546. }
  547. }
  548. kernel void kernel_rope(
  549. device const void * src0,
  550. device float * dst,
  551. constant int64_t & ne00,
  552. constant int64_t & ne01,
  553. constant int64_t & ne02,
  554. constant int64_t & ne03,
  555. constant uint64_t & nb00,
  556. constant uint64_t & nb01,
  557. constant uint64_t & nb02,
  558. constant uint64_t & nb03,
  559. constant int64_t & ne0,
  560. constant int64_t & ne1,
  561. constant int64_t & ne2,
  562. constant int64_t & ne3,
  563. constant uint64_t & nb0,
  564. constant uint64_t & nb1,
  565. constant uint64_t & nb2,
  566. constant uint64_t & nb3,
  567. constant int & n_past,
  568. constant int & n_dims,
  569. constant int & mode,
  570. uint3 tpig[[thread_position_in_grid]]) {
  571. const int64_t i3 = tpig[2];
  572. const int64_t i2 = tpig[1];
  573. const int64_t i1 = tpig[0];
  574. const bool is_neox = mode & 2;
  575. const float theta_scale = pow(10000.0, -2.0f/n_dims);
  576. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  577. float theta = (float)p;
  578. if (!is_neox) {
  579. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  580. const float cos_theta = cos(theta);
  581. const float sin_theta = sin(theta);
  582. theta *= theta_scale;
  583. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  584. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  585. const float x0 = src[0];
  586. const float x1 = src[1];
  587. dst_data[0] = x0*cos_theta - x1*sin_theta;
  588. dst_data[1] = x0*sin_theta + x1*cos_theta;
  589. }
  590. } else {
  591. // TODO: implement
  592. }
  593. }
  594. kernel void kernel_cpy_f16_f16(
  595. device const half * src0,
  596. device half * dst,
  597. constant int64_t & ne00,
  598. constant int64_t & ne01,
  599. constant int64_t & ne02,
  600. constant int64_t & ne03,
  601. constant uint64_t & nb00,
  602. constant uint64_t & nb01,
  603. constant uint64_t & nb02,
  604. constant uint64_t & nb03,
  605. constant int64_t & ne0,
  606. constant int64_t & ne1,
  607. constant int64_t & ne2,
  608. constant int64_t & ne3,
  609. constant uint64_t & nb0,
  610. constant uint64_t & nb1,
  611. constant uint64_t & nb2,
  612. constant uint64_t & nb3,
  613. uint3 tgpig[[threadgroup_position_in_grid]],
  614. uint3 tpitg[[thread_position_in_threadgroup]],
  615. uint3 ntg[[threads_per_threadgroup]]) {
  616. const int64_t i03 = tgpig[2];
  617. const int64_t i02 = tgpig[1];
  618. const int64_t i01 = tgpig[0];
  619. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  620. const int64_t i3 = n / (ne2*ne1*ne0);
  621. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  622. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  623. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  624. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  625. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  626. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  627. dst_data[i00] = src[0];
  628. }
  629. }
  630. kernel void kernel_cpy_f32_f16(
  631. device const float * src0,
  632. device half * dst,
  633. constant int64_t & ne00,
  634. constant int64_t & ne01,
  635. constant int64_t & ne02,
  636. constant int64_t & ne03,
  637. constant uint64_t & nb00,
  638. constant uint64_t & nb01,
  639. constant uint64_t & nb02,
  640. constant uint64_t & nb03,
  641. constant int64_t & ne0,
  642. constant int64_t & ne1,
  643. constant int64_t & ne2,
  644. constant int64_t & ne3,
  645. constant uint64_t & nb0,
  646. constant uint64_t & nb1,
  647. constant uint64_t & nb2,
  648. constant uint64_t & nb3,
  649. uint3 tgpig[[threadgroup_position_in_grid]],
  650. uint3 tpitg[[thread_position_in_threadgroup]],
  651. uint3 ntg[[threads_per_threadgroup]]) {
  652. const int64_t i03 = tgpig[2];
  653. const int64_t i02 = tgpig[1];
  654. const int64_t i01 = tgpig[0];
  655. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  656. const int64_t i3 = n / (ne2*ne1*ne0);
  657. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  658. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  659. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  660. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  661. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  662. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  663. dst_data[i00] = src[0];
  664. }
  665. }
  666. kernel void kernel_cpy_f32_f32(
  667. device const float * src0,
  668. device float * dst,
  669. constant int64_t & ne00,
  670. constant int64_t & ne01,
  671. constant int64_t & ne02,
  672. constant int64_t & ne03,
  673. constant uint64_t & nb00,
  674. constant uint64_t & nb01,
  675. constant uint64_t & nb02,
  676. constant uint64_t & nb03,
  677. constant int64_t & ne0,
  678. constant int64_t & ne1,
  679. constant int64_t & ne2,
  680. constant int64_t & ne3,
  681. constant uint64_t & nb0,
  682. constant uint64_t & nb1,
  683. constant uint64_t & nb2,
  684. constant uint64_t & nb3,
  685. uint3 tgpig[[threadgroup_position_in_grid]],
  686. uint3 tpitg[[thread_position_in_threadgroup]],
  687. uint3 ntg[[threads_per_threadgroup]]) {
  688. const int64_t i03 = tgpig[2];
  689. const int64_t i02 = tgpig[1];
  690. const int64_t i01 = tgpig[0];
  691. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  692. const int64_t i3 = n / (ne2*ne1*ne0);
  693. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  694. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  695. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  696. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  697. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  698. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  699. dst_data[i00] = src[0];
  700. }
  701. }
  702. //============================================ k-quants ======================================================
  703. #ifndef QK_K
  704. #define QK_K 256
  705. #else
  706. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  707. #endif
  708. #if QK_K == 256
  709. #define K_SCALE_SIZE 12
  710. #else
  711. #define K_SCALE_SIZE 4
  712. #endif
  713. typedef struct {
  714. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  715. uint8_t qs[QK_K/4]; // quants
  716. half d; // super-block scale for quantized scales
  717. half dmin; // super-block scale for quantized mins
  718. } block_q2_K;
  719. // 84 bytes / block
  720. typedef struct {
  721. uint8_t hmask[QK_K/8]; // quants - high bit
  722. uint8_t qs[QK_K/4]; // quants - low 2 bits
  723. #if QK_K == 64
  724. uint8_t scales[2];
  725. #else
  726. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  727. #endif
  728. half d; // super-block scale
  729. } block_q3_K;
  730. #if QK_K == 64
  731. typedef struct {
  732. half d[2]; // super-block scales/mins
  733. uint8_t scales[2];
  734. uint8_t qs[QK_K/2]; // 4-bit quants
  735. } block_q4_K;
  736. #else
  737. typedef struct {
  738. half d; // super-block scale for quantized scales
  739. half dmin; // super-block scale for quantized mins
  740. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  741. uint8_t qs[QK_K/2]; // 4--bit quants
  742. } block_q4_K;
  743. #endif
  744. #if QK_K == 64
  745. typedef struct {
  746. half d; // super-block scales/mins
  747. int8_t scales[QK_K/16]; // 8-bit block scales
  748. uint8_t qh[QK_K/8]; // quants, high bit
  749. uint8_t qs[QK_K/2]; // quants, low 4 bits
  750. } block_q5_K;
  751. #else
  752. typedef struct {
  753. half d; // super-block scale for quantized scales
  754. half dmin; // super-block scale for quantized mins
  755. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  756. uint8_t qh[QK_K/8]; // quants, high bit
  757. uint8_t qs[QK_K/2]; // quants, low 4 bits
  758. } block_q5_K;
  759. // 176 bytes / block
  760. #endif
  761. typedef struct {
  762. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  763. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  764. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  765. half d; // super-block scale
  766. } block_q6_K;
  767. // 210 bytes / block
  768. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  769. uchar4 r;
  770. if (j < 4) {
  771. r[0] = q[j+0] & 63;
  772. r[2] = q[j+1] & 63;
  773. r[1] = q[j+4] & 63;
  774. r[3] = q[j+5] & 63;
  775. } else {
  776. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  777. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  778. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  779. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  780. }
  781. return r;
  782. }
  783. //========================================== dequantization =============================
  784. static void dequantize_row_q2_K(device const block_q2_K * x, device float * y, int k) {
  785. assert(k % QK_K == 0);
  786. const int nb = k / QK_K;
  787. for (int i = 0; i < nb; i++) {
  788. const float d = x[i].d;
  789. const float min = x[i].dmin;
  790. device const uint8_t * q = x[i].qs;
  791. #if QK_K == 256
  792. int is = 0;
  793. float dl, ml;
  794. for (int n = 0; n < QK_K; n += 128) {
  795. int shift = 0;
  796. for (int j = 0; j < 4; ++j) {
  797. uint8_t sc = x[i].scales[is++];
  798. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  799. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  800. sc = x[i].scales[is++];
  801. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  802. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  803. shift += 2;
  804. }
  805. q += 32;
  806. }
  807. #else
  808. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  809. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  810. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  811. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  812. for (int l = 0; l < 16; ++l) {
  813. y[l+ 0] = dl1 * ((q[l] >> 0) & 3) - ml1;
  814. y[l+16] = dl2 * ((q[l] >> 2) & 3) - ml2;
  815. y[l+32] = dl3 * ((q[l] >> 4) & 3) - ml3;
  816. y[l+48] = dl4 * ((q[l] >> 6) & 3) - ml4;
  817. }
  818. y += QK_K;
  819. #endif
  820. }
  821. }
  822. static void dequantize_row_q3_K(device const block_q3_K * x, device float * y, int k) {
  823. assert(k % QK_K == 0);
  824. const int nb = k / QK_K;
  825. #if QK_K == 256
  826. const uint16_t kmask1 = 0x0303;
  827. const uint16_t kmask2 = 0x0f0f;
  828. uint16_t aux[8];
  829. thread const int8_t * scales = (thread const int8_t*)aux;
  830. for (int i = 0; i < nb; i++) {
  831. const float d_all = (float)(x[i].d);
  832. device const uint8_t * q = x[i].qs;
  833. device const uint8_t * h = x[i].hmask;
  834. uint8_t m = 1;
  835. device const uint16_t * a = (device const uint16_t *)x[i].scales;
  836. aux[0] = (a[0] & kmask2) | (((a[4] >> 0) & kmask1) << 4);
  837. aux[1] = (a[1] & kmask2) | (((a[5] >> 0) & kmask1) << 4);
  838. aux[2] = (a[2] & kmask2) | (((a[4] >> 2) & kmask1) << 4);
  839. aux[3] = (a[3] & kmask2) | (((a[5] >> 2) & kmask1) << 4);
  840. aux[4] = ((a[0] >> 4) & kmask2) | (((a[4] >> 4) & kmask1) << 4);
  841. aux[5] = ((a[1] >> 4) & kmask2) | (((a[5] >> 4) & kmask1) << 4);
  842. aux[6] = ((a[2] >> 4) & kmask2) | (((a[4] >> 6) & kmask1) << 4);
  843. aux[7] = ((a[3] >> 4) & kmask2) | (((a[5] >> 6) & kmask1) << 4);
  844. int is = 0;
  845. float dl;
  846. for (int n = 0; n < QK_K; n += 128) {
  847. int shift = 0;
  848. for (int j = 0; j < 4; ++j) {
  849. dl = d_all * (scales[is++] - 32);
  850. for (int l = 0; l < 16; ++l) {
  851. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((h[l+ 0] & m) ? 0 : 4));
  852. }
  853. dl = d_all * (scales[is++] - 32);
  854. for (int l = 0; l < 16; ++l) {
  855. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((h[l+16] & m) ? 0 : 4));
  856. }
  857. shift += 2;
  858. m <<= 1;
  859. }
  860. q += 32;
  861. }
  862. }
  863. #else
  864. for (int i = 0; i < nb; i++) {
  865. const float d_all = (float)(x[i].d);
  866. device const uint8_t * q = x[i].qs;
  867. device const uint8_t * hm = x[i].hmask;
  868. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  869. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  870. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  871. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  872. for (int l = 0; l < 8; ++l) {
  873. uint8_t h = hm[l];
  874. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  875. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  876. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  877. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  878. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  879. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  880. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  881. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  882. }
  883. y += QK_K;
  884. }
  885. #endif
  886. }
  887. static void dequantize_row_q4_K(device const block_q4_K * x, device float * y, int k) {
  888. assert(k % QK_K == 0);
  889. const int nb = k / QK_K;
  890. for (int i = 0; i < nb; i++) {
  891. device const uint8_t * q = x[i].qs;
  892. #if QK_K == 256
  893. const float d = x[i].d;
  894. const float min = x[i].dmin;
  895. device const uint8_t * scales = x[i].scales;
  896. int is = 0;
  897. for (int j = 0; j < QK_K; j += 64) {
  898. const uchar4 sc = get_scale_min_k4(is, scales);
  899. const float d1 = d * sc[0]; const float m1 = min * sc[1];
  900. const float d2 = d * sc[2]; const float m2 = min * sc[3];
  901. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  902. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  903. q += 32; is += 2;
  904. }
  905. #else
  906. device const uint8_t * s = x[i].scales;
  907. device const half2 * dh = (device const half2 *)x[i].d;
  908. const float2 d = (float2)dh[0];
  909. const float d1 = d[0] * (s[0] & 0xF);
  910. const float d2 = d[0] * (s[1] & 0xF);
  911. const float m1 = d[1] * (s[0] >> 4);
  912. const float m2 = d[1] * (s[1] >> 4);
  913. for (int l = 0; l < 32; ++l) {
  914. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  915. y[l+32] = d2 * (q[l] >> 4) - m2;
  916. }
  917. y += QK_K;
  918. #endif
  919. }
  920. }
  921. static void dequantize_row_q5_K(device const block_q5_K * x, device float * y, int k) {
  922. assert(k % QK_K == 0);
  923. const int nb = k / QK_K;
  924. #if QK_K == 256
  925. for (int i = 0; i < nb; i++) {
  926. const float d = (float)(x[i].d);
  927. const float min = (float)(x[i].dmin);
  928. device const uint8_t * ql = x[i].qs;
  929. device const uint8_t * qh = x[i].qh;
  930. int is = 0;
  931. uint8_t u1 = 1, u2 = 2;
  932. for (int j = 0; j < QK_K; j += 64) {
  933. const uchar4 sc = get_scale_min_k4(is, x[i].scales);
  934. const float d1 = d * sc[0]; const float m1 = min * sc[1];
  935. const float d2 = d * sc[2]; const float m2 = min * sc[3];
  936. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  937. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  938. ql += 32; is += 2;
  939. u1 <<= 2; u2 <<= 2;
  940. }
  941. }
  942. #else
  943. for (int i = 0; i < nb; i++) {
  944. const float d = (float)x[i].d;
  945. device const uint8_t * ql = x[i].qs;
  946. device const uint8_t * qh = x[i].qh;
  947. device const int8_t * sc = x[i].scales;
  948. for (int l = 0; l < 8; ++l) {
  949. y[l+ 0] = d * sc[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  950. y[l+ 8] = d * sc[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  951. y[l+16] = d * sc[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  952. y[l+24] = d * sc[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  953. y[l+32] = d * sc[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  954. y[l+40] = d * sc[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  955. y[l+48] = d * sc[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  956. y[l+56] = d * sc[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  957. }
  958. y += QK_K;
  959. }
  960. #endif
  961. }
  962. static void dequantize_row_q6_K(device const block_q6_K * x, device float * y, int k) {
  963. assert(k % QK_K == 0);
  964. const int nb = k / QK_K;
  965. for (int i = 0; i < nb; i++) {
  966. device const uint8_t * ql = x[i].ql;
  967. device const uint8_t * qh = x[i].qh;
  968. device const int8_t * sc = x[i].scales;
  969. const float d = x[i].d;
  970. #if QK_K == 256
  971. for (int n = 0; n < QK_K; n += 128) {
  972. for (int l = 0; l < 32; ++l) {
  973. int is = l/16;
  974. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  975. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  976. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  977. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  978. y[l + 0] = d * sc[is + 0] * q1;
  979. y[l + 32] = d * sc[is + 2] * q2;
  980. y[l + 64] = d * sc[is + 4] * q3;
  981. y[l + 96] = d * sc[is + 6] * q4;
  982. }
  983. y += 128;
  984. ql += 64;
  985. qh += 32;
  986. sc += 8;
  987. }
  988. #else
  989. for (int l = 0; l < 16; ++l) {
  990. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  991. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  992. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  993. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  994. y[l+ 0] = d * sc[0] * q1;
  995. y[l+16] = d * sc[1] * q2;
  996. y[l+32] = d * sc[2] * q3;
  997. y[l+48] = d * sc[3] * q4;
  998. }
  999. y += 64;
  1000. #endif
  1001. }
  1002. }
  1003. kernel void kernel_get_rows_q2_K(
  1004. device const void * src0,
  1005. device const int * src1,
  1006. device float * dst,
  1007. constant int64_t & ne00,
  1008. constant uint64_t & nb01,
  1009. constant uint64_t & nb1,
  1010. uint tpig[[thread_position_in_grid]]) {
  1011. const int i = tpig;
  1012. const int r = ((device int32_t *) src1)[i];
  1013. dequantize_row_q2_K(
  1014. (device const block_q2_K *) ((device char *) src0 + r*nb01),
  1015. (device float *) ((device char *) dst + i*nb1), ne00);
  1016. }
  1017. kernel void kernel_get_rows_q3_K(
  1018. device const void * src0,
  1019. device const int * src1,
  1020. device float * dst,
  1021. constant int64_t & ne00,
  1022. constant uint64_t & nb01,
  1023. constant uint64_t & nb1,
  1024. uint tpig[[thread_position_in_grid]]) {
  1025. const int i = tpig;
  1026. const int r = ((device int32_t *) src1)[i];
  1027. dequantize_row_q3_K(
  1028. (device const block_q3_K *) ((device char *) src0 + r*nb01),
  1029. (device float *) ((device char *) dst + i*nb1), ne00);
  1030. }
  1031. kernel void kernel_get_rows_q4_K(
  1032. device const void * src0,
  1033. device const int * src1,
  1034. device float * dst,
  1035. constant int64_t & ne00,
  1036. constant uint64_t & nb01,
  1037. constant uint64_t & nb1,
  1038. uint tpig[[thread_position_in_grid]]) {
  1039. const int i = tpig;
  1040. const int r = ((device int32_t *) src1)[i];
  1041. dequantize_row_q4_K(
  1042. (device const block_q4_K *) ((device char *) src0 + r*nb01),
  1043. (device float *) ((device char *) dst + i*nb1), ne00);
  1044. }
  1045. kernel void kernel_get_rows_q5_K(
  1046. device const void * src0,
  1047. device const int * src1,
  1048. device float * dst,
  1049. constant int64_t & ne00,
  1050. constant uint64_t & nb01,
  1051. constant uint64_t & nb1,
  1052. uint tpig[[thread_position_in_grid]]) {
  1053. const int i = tpig;
  1054. const int r = ((device int32_t *) src1)[i];
  1055. dequantize_row_q5_K(
  1056. (device const block_q5_K *) ((device char *) src0 + r*nb01),
  1057. (device float *) ((device char *) dst + i*nb1), ne00);
  1058. }
  1059. kernel void kernel_get_rows_q6_K(
  1060. device const void * src0,
  1061. device const int * src1,
  1062. device float * dst,
  1063. constant int64_t & ne00,
  1064. constant uint64_t & nb01,
  1065. constant uint64_t & nb1,
  1066. uint tpig[[thread_position_in_grid]]) {
  1067. const int i = tpig;
  1068. const int r = ((device int32_t *) src1)[i];
  1069. dequantize_row_q6_K(
  1070. (device const block_q6_K *) ((device char *) src0 + r*nb01),
  1071. (device float *) ((device char *) dst + i*nb1), ne00);
  1072. }
  1073. //====================================== dot products =========================
  1074. kernel void kernel_mul_mat_q2_K_f32(
  1075. device const void * src0,
  1076. device const float * src1,
  1077. device float * dst,
  1078. constant int64_t & ne00,
  1079. constant int64_t & ne10,
  1080. constant int64_t & ne0,
  1081. threadgroup float * sum [[threadgroup(0)]],
  1082. uint2 tgpig[[threadgroup_position_in_grid]],
  1083. uint2 tpitg[[thread_position_in_threadgroup]],
  1084. uint2 tptg[[threads_per_threadgroup]]) {
  1085. const int nb = ne00/QK_K;
  1086. const int64_t r0 = tgpig.x;
  1087. const int64_t r1 = tgpig.y;
  1088. device const block_q2_K * x = (device const block_q2_K *) src0 + r0*nb;
  1089. device const float * yy = (device const float *) src1 + r1*ne10;
  1090. const int nth = tptg.x*tptg.y;
  1091. const int ith = tptg.y*tpitg.x + tpitg.y;
  1092. float sumf = 0;
  1093. #if QK_K == 256
  1094. const int tid = tpitg.y; // 0...16
  1095. const int il = tid/4; // 0...3
  1096. const int ir = tid%4; // 0...3
  1097. const int ip = il/2; // 0 or 1
  1098. const int shift1 = 4*(il%2);// 0 or 4
  1099. const int shift2 = shift1+2;// 2 or 6
  1100. const int n = 8;
  1101. const int is = 4*il + (n*ir)/16;
  1102. const int y_offset = 64*il + n*ir;
  1103. const int q_offset = 32*ip + n*ir;
  1104. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1105. device const uint8_t * q = x[i].qs + q_offset;
  1106. device const uint8_t * scales = x[i].scales + is;
  1107. uint8_t d1 = scales[0] & 0xF;
  1108. uint8_t d2 = scales[2] & 0xF;
  1109. uint8_t m1 = scales[0] >> 4;
  1110. uint8_t m2 = scales[2] >> 4;
  1111. device const float * y = yy + i*QK_K + y_offset;
  1112. float2 s = {0.f, 0.f};
  1113. float smin = 0;
  1114. for (int l = 0; l < n; ++l) {
  1115. s[0] += y[l+ 0] * ((q[l] >> shift1) & 3);
  1116. s[1] += y[l+32] * ((q[l] >> shift2) & 3);
  1117. smin += y[l+ 0] * m1 + y[l+32] * m2;
  1118. }
  1119. const float dall = (float)x[i].d;
  1120. const float dmin = (float)x[i].dmin;
  1121. sumf += dall * (s[0] * d1 + s[1] * d2) - dmin * smin;
  1122. }
  1123. #else
  1124. const int il = 4 * tpitg.x;
  1125. uint32_t aux[2];
  1126. thread const uint8_t * d = (thread const uint8_t *)aux;
  1127. thread const uint8_t * m = (thread const uint8_t *)aux + 4;
  1128. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1129. device const uint8_t * q = x[i].qs + il;
  1130. device const float * y = yy + i*QK_K + il;
  1131. const float dall = (float)x[i].d;
  1132. const float dmin = (float)x[i].dmin;
  1133. device const uint32_t * a = (device const uint32_t *)x[i].scales;
  1134. aux[0] = a[0] & 0x0f0f0f0f;
  1135. aux[1] = (a[0] >> 4) & 0x0f0f0f0f;
  1136. for (int l = 0; l < 4; ++l) {
  1137. sumf += y[l+ 0] * (dall * d[0] * ((q[l] >> 0) & 3) - dmin * m[0])
  1138. + y[l+16] * (dall * d[1] * ((q[l] >> 2) & 3) - dmin * m[1])
  1139. + y[l+32] * (dall * d[2] * ((q[l] >> 4) & 3) - dmin * m[2])
  1140. + y[l+48] * (dall * d[3] * ((q[l] >> 6) & 3) - dmin * m[3]);
  1141. }
  1142. }
  1143. #endif
  1144. sum[ith] = sumf;
  1145. //
  1146. // Accumulate the sum from all threads in the threadgroup
  1147. //
  1148. threadgroup_barrier(mem_flags::mem_threadgroup);
  1149. if (ith%4 == 0) {
  1150. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1151. }
  1152. threadgroup_barrier(mem_flags::mem_threadgroup);
  1153. if (ith%16 == 0) {
  1154. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1155. }
  1156. threadgroup_barrier(mem_flags::mem_threadgroup);
  1157. if (ith == 0) {
  1158. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1159. dst[r1*ne0 + r0] = sum[0];
  1160. }
  1161. }
  1162. kernel void kernel_mul_mat_q3_K_f32(
  1163. device const void * src0,
  1164. device const float * src1,
  1165. device float * dst,
  1166. constant int64_t & ne00,
  1167. constant int64_t & ne10,
  1168. constant int64_t & ne0,
  1169. constant int64_t & ne1,
  1170. threadgroup float * sum [[threadgroup(0)]],
  1171. uint2 tgpig[[threadgroup_position_in_grid]],
  1172. uint2 tpitg[[thread_position_in_threadgroup]],
  1173. uint2 tptg[[threads_per_threadgroup]]) {
  1174. const int nb = ne00/QK_K;
  1175. const int64_t r0 = tgpig.x;
  1176. const int64_t r1 = tgpig.y;
  1177. device const block_q3_K * x = (device const block_q3_K *) src0 + r0*nb;
  1178. device const float * yy = (device const float *) src1 + r1*ne10;
  1179. const int nth = tptg.x*tptg.y;
  1180. const int ith = tptg.y*tpitg.x + tpitg.y;
  1181. #if QK_K == 256
  1182. const uint8_t m3 = 3;
  1183. const int8_t m4 = 4;
  1184. const uint16_t kmask1 = 0x0303;
  1185. const uint16_t kmask2 = 0x0f0f;
  1186. const int tid = tpitg.y; // expecting 16
  1187. const int ip = tid/8; // 0 or 1
  1188. const int il = tid/2 - 4*ip; // 0...3
  1189. const int ir = tid%2;
  1190. const int n = 8;
  1191. const int l0 = n*ir;
  1192. const uint8_t m = 1 << (4*ip + il);
  1193. const int shift = 2*il;
  1194. const uint16_t s_shift1 = 4*ip;
  1195. const uint16_t s_shift2 = s_shift1 + 2*(il/2);
  1196. const int ik = 4 + (il%2);
  1197. const int q_offset = 32*ip + l0;
  1198. const int y_offset = 128*ip + 32*il + l0;
  1199. //float sumf = 0;
  1200. float sumf1 = 0, sumf2 = 0;
  1201. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1202. const float d_all = (float)(x[i].d);
  1203. device const uint8_t * q = x[i].qs + q_offset;
  1204. device const uint8_t * h = x[i].hmask + l0;
  1205. device const float * y = yy + i * QK_K + y_offset;
  1206. device const uint16_t * a = (device const uint16_t *)x[i].scales;
  1207. const char2 scales = as_type<char2>((uint16_t)(((a[il] >> s_shift1) & kmask2) | (((a[ik] >> s_shift2) & kmask1) << 4)));
  1208. float s = 0;
  1209. for (int l = 0; l < n; ++l) {
  1210. s += y[l+ 0] * ((int8_t)((q[l+ 0] >> shift) & m3) - ((h[l+ 0] & m) ? 0 : m4));
  1211. }
  1212. float d = d_all * s;
  1213. sumf1 += d * scales[0];
  1214. sumf2 += d;
  1215. //sumf += d_all * s * (scales[0] - 32);
  1216. s = 0;
  1217. for (int l = 0; l < n; ++l) {
  1218. s += y[l+16] * ((int8_t)((q[l+16] >> shift) & m3) - ((h[l+16] & m) ? 0 : m4));
  1219. }
  1220. d = d_all * s;
  1221. sumf1 += d * scales[1];
  1222. sumf2 += d;
  1223. //sumf += d_all * s * (scales[1] - 32);
  1224. }
  1225. //sum[ith] = sumf;
  1226. sum[ith] = sumf1 - 32.f*sumf2;
  1227. #else
  1228. const int il = 4 * tpitg.x; // 0, 4, 8, 12
  1229. const int im = il/8; // 0, 0, 1, 1
  1230. const int in = il%8; // 0, 4, 0, 4
  1231. float sumf = 0;
  1232. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1233. const float d_all = (float)(x[i].d);
  1234. device const uint8_t * q = x[i].qs + il;
  1235. device const uint8_t * h = x[i].hmask + in;
  1236. device const float * y = yy + i * QK_K + il;
  1237. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1238. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1239. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1240. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1241. for (int l = 0; l < 4; ++l) {
  1242. const uint8_t hm = h[l] >> im;
  1243. sumf += y[l+ 0] * d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((hm & 0x01) ? 0 : 4))
  1244. + y[l+16] * d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((hm & 0x04) ? 0 : 4))
  1245. + y[l+32] * d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((hm & 0x10) ? 0 : 4))
  1246. + y[l+48] * d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((hm & 0x40) ? 0 : 4));
  1247. }
  1248. }
  1249. sum[ith] = sumf;
  1250. #endif
  1251. //
  1252. // Accumulate the sum from all threads in the threadgroup
  1253. //
  1254. threadgroup_barrier(mem_flags::mem_threadgroup);
  1255. if (ith%4 == 0) {
  1256. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1257. }
  1258. threadgroup_barrier(mem_flags::mem_threadgroup);
  1259. if (ith%16 == 0) {
  1260. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1261. }
  1262. threadgroup_barrier(mem_flags::mem_threadgroup);
  1263. if (ith == 0) {
  1264. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1265. dst[r1*ne0 + r0] = sum[0];
  1266. }
  1267. }
  1268. kernel void kernel_mul_mat_q4_K_f32(
  1269. device const void * src0,
  1270. device const float * src1,
  1271. device float * dst,
  1272. constant int64_t & ne00,
  1273. constant int64_t & ne10,
  1274. constant int64_t & ne0,
  1275. threadgroup float * sum [[threadgroup(0)]],
  1276. uint2 tgpig[[threadgroup_position_in_grid]],
  1277. uint2 tpitg[[thread_position_in_threadgroup]],
  1278. uint2 tptg[[threads_per_threadgroup]]) {
  1279. const int nb = ne00/QK_K;
  1280. const int64_t r0 = tgpig.x;
  1281. const int64_t r1 = tgpig.y;
  1282. const int nth = tptg.x*tptg.y;
  1283. const int ith = tptg.y*tpitg.x + tpitg.y;
  1284. device const block_q4_K * x = (device const block_q4_K *) src0 + r0*nb;
  1285. device const float * yy = (device const float *) src1 + r1*ne10;
  1286. float sumf = 0;
  1287. #if QK_K == 256
  1288. const uint16_t kmask1 = 0x3f3f;
  1289. const uint16_t kmask2 = 0x0f0f;
  1290. const uint16_t kmask3 = 0xc0c0;
  1291. const int tid = tpitg.y; // 0...16
  1292. const int il = tid/4; // 0...3
  1293. const int ir = tid - 4*il;// 0...3
  1294. const int n = 4;
  1295. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1296. const int in = il%2;
  1297. const int l0 = n*(2*ir + in);
  1298. const int q_offset = 32*im + l0;
  1299. const int y_offset = 64*im + l0;
  1300. uchar2 sc1, sc2, sc3, sc4;
  1301. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1302. device const uint8_t * q1 = (x + i)->qs + q_offset;
  1303. device const uint8_t * q2 = q1 + 64;
  1304. device const float * y1 = yy + i*QK_K + y_offset;
  1305. device const float * y2 = y1 + 128;
  1306. const float dall = (float)((x + i)->d);
  1307. const float dmin = (float)((x + i)->dmin);
  1308. device const uint16_t * a = (device const uint16_t *)(x + i)->scales;
  1309. sc1 = as_type<uchar2>((uint16_t)(a[im+0] & kmask1));
  1310. sc2 = as_type<uchar2>((uint16_t)(a[im+2] & kmask1));
  1311. sc3 = as_type<uchar2>((uint16_t)(((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2)));
  1312. sc4 = as_type<uchar2>((uint16_t)(((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2)));
  1313. float4 s = {0.f, 0.f, 0.f, 0.f};
  1314. float smin = 0;
  1315. for (int l = 0; l < n; ++l) {
  1316. s[0] += y1[l] * (q1[l] & 0xF); s[1] += y1[l+32] * (q1[l] >> 4);
  1317. s[2] += y2[l] * (q2[l] & 0xF); s[3] += y2[l+32] * (q2[l] >> 4);
  1318. smin += y1[l] * sc2[0] + y1[l+32] * sc2[1] + y2[l] * sc4[0] + y2[l+32] * sc4[1];
  1319. }
  1320. sumf += dall * (s[0] * sc1[0] + s[1] * sc1[1] + s[2] * sc3[0] + s[3] * sc3[1]) - dmin * smin;
  1321. }
  1322. #else
  1323. uint16_t aux16[2];
  1324. thread const uint8_t * scales = (thread const uint8_t *)aux16;
  1325. const int il = 4*tpitg.x;
  1326. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1327. device const uint8_t * q = x[i].qs + il;
  1328. device const float * y = yy + i * QK_K + il;
  1329. const float d = (float)x[i].d[0];
  1330. const float m = (float)x[i].d[1];
  1331. device const uint16_t * a = (device const uint16_t *)x[i].scales;
  1332. aux16[0] = a[0] & 0x0f0f;
  1333. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1334. for (int l = 0; l < 4; ++l) {
  1335. sumf += d * scales[0] * (y[l+ 0] * (q[l] & 0xF) + y[l+16] * (q[l+16] & 0xF)) - m * scales[2] * (y[l+ 0] + y[l+16])
  1336. + d * scales[1] * (y[l+32] * (q[l] >> 4) + y[l+48] * (q[l+16] >> 4)) - m * scales[3] * (y[l+32] + y[l+48]);
  1337. }
  1338. }
  1339. #endif
  1340. sum[ith] = sumf;
  1341. //
  1342. // Accumulate the sum from all threads in the threadgroup
  1343. // This version is slightly faster than the commented out one below,
  1344. // which I copy-pasted from ggerganov's q4_0 dot product for metal.
  1345. //
  1346. threadgroup_barrier(mem_flags::mem_threadgroup);
  1347. if (ith%4 == 0) {
  1348. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1349. }
  1350. threadgroup_barrier(mem_flags::mem_threadgroup);
  1351. if (ith%16 == 0) {
  1352. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1353. }
  1354. threadgroup_barrier(mem_flags::mem_threadgroup);
  1355. if (ith == 0) {
  1356. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1357. dst[r1*ne0 + r0] = sum[0];
  1358. }
  1359. //// accumulate the sum from all threads in the threadgroup
  1360. //threadgroup_barrier(mem_flags::mem_threadgroup);
  1361. //for (uint i = nth/2; i > 0; i /= 2) {
  1362. // if (ith < i) {
  1363. // sum[ith] += sum[ith + i];
  1364. // }
  1365. // threadgroup_barrier(mem_flags::mem_threadgroup);
  1366. //}
  1367. //if (ith == 0) {
  1368. // dst[r1*ne0 + r0] = sum[0];
  1369. //}
  1370. }
  1371. kernel void kernel_mul_mat_q5_K_f32(
  1372. device const void * src0,
  1373. device const float * src1,
  1374. device float * dst,
  1375. constant int64_t & ne00,
  1376. constant int64_t & ne10,
  1377. constant int64_t & ne0,
  1378. threadgroup float * sum [[threadgroup(0)]],
  1379. uint2 tgpig[[threadgroup_position_in_grid]],
  1380. uint2 tpitg[[thread_position_in_threadgroup]],
  1381. uint2 tptg[[threads_per_threadgroup]]) {
  1382. const int nb = ne00/QK_K;
  1383. const int64_t r0 = tgpig.x;
  1384. const int64_t r1 = tgpig.y;
  1385. device const block_q5_K * x = (device const block_q5_K *) src0 + r0*nb;
  1386. device const float * yy = (device const float *) src1 + r1*ne10;
  1387. const int nth = tptg.x*tptg.y;
  1388. const int ith = tptg.y*tpitg.x + tpitg.y;
  1389. float sumf = 0;
  1390. #if QK_K == 256
  1391. const uint16_t kmask1 = 0x3f3f;
  1392. const uint16_t kmask2 = 0x0f0f;
  1393. const uint16_t kmask3 = 0xc0c0;
  1394. const int tid = tpitg.y; // 0...16
  1395. const int il = tid/4; // 0...3
  1396. const int ir = tid - 4*il;// 0...3
  1397. const int n = 4;
  1398. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1399. const int in = il%2;
  1400. const int l0 = n*(2*ir + in);
  1401. const int q_offset = 32*im + l0;
  1402. const int y_offset = 64*im + l0;
  1403. const uint8_t hm1 = 1u << (2*im);
  1404. const uint8_t hm2 = hm1 << 1;
  1405. const uint8_t hm3 = hm1 << 4;
  1406. const uint8_t hm4 = hm2 << 4;
  1407. uchar2 sc1, sc2, sc3, sc4;
  1408. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1409. device const uint8_t * q1 = (x + i)->qs + q_offset;
  1410. device const uint8_t * q2 = q1 + 64;
  1411. device const uint8_t * qh = (x + i)->qh + l0;
  1412. device const float * y1 = yy + i*QK_K + y_offset;
  1413. device const float * y2 = y1 + 128;
  1414. const float dall = (float)((x + i)->d);
  1415. const float dmin = (float)((x + i)->dmin);
  1416. device const uint16_t * a = (device const uint16_t *)(x + i)->scales;
  1417. sc1 = as_type<uchar2>((uint16_t)(a[im+0] & kmask1));
  1418. sc2 = as_type<uchar2>((uint16_t)(a[im+2] & kmask1));
  1419. sc3 = as_type<uchar2>((uint16_t)(((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2)));
  1420. sc4 = as_type<uchar2>((uint16_t)(((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2)));
  1421. float4 s = {0.f, 0.f, 0.f, 0.f};
  1422. float smin = 0;
  1423. for (int l = 0; l < n; ++l) {
  1424. s[0] += y1[l+ 0] * ((q1[l] & 0xF) + (qh[l] & hm1 ? 16 : 0));
  1425. s[1] += y1[l+32] * ((q1[l] >> 4) + (qh[l] & hm2 ? 16 : 0));
  1426. s[2] += y2[l+ 0] * ((q2[l] & 0xF) + (qh[l] & hm3 ? 16 : 0));
  1427. s[3] += y2[l+32] * ((q2[l] >> 4) + (qh[l] & hm4 ? 16 : 0));
  1428. smin += y1[l] * sc2[0] + y1[l+32] * sc2[1] + y2[l] * sc4[0] + y2[l+32] * sc4[1];
  1429. }
  1430. sumf += dall * (s[0] * sc1[0] + s[1] * sc1[1] + s[2] * sc3[0] + s[3] * sc3[1]) - dmin * smin;
  1431. }
  1432. #else
  1433. const int il = 4 * tpitg.x; // 0, 4, 8, 12
  1434. const int im = il/8; // 0, 0, 1, 1
  1435. const int in = il%8; // 0, 4, 0, 4
  1436. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1437. const float d = (float)x[i].d;
  1438. device const uint8_t * q = x[i].qs + il;
  1439. device const uint8_t * h = x[i].qh + in;
  1440. device const int8_t * s = x[i].scales;
  1441. device const float * y = yy + i*QK_K + il;
  1442. for (int l = 0; l < 4; ++l) {
  1443. const uint8_t hl = h[l] >> im;
  1444. sumf += y[l+ 0] * d * s[0] * ((q[l+ 0] & 0xF) - (hl & 0x01 ? 0 : 16))
  1445. + y[l+16] * d * s[1] * ((q[l+16] & 0xF) - (hl & 0x04 ? 0 : 16))
  1446. + y[l+32] * d * s[2] * ((q[l+ 0] >> 4) - (hl & 0x10 ? 0 : 16))
  1447. + y[l+48] * d * s[3] * ((q[l+16] >> 4) - (hl & 0x40 ? 0 : 16));
  1448. }
  1449. }
  1450. #endif
  1451. sum[ith] = sumf;
  1452. //
  1453. // Accumulate the sum from all threads in the threadgroup
  1454. //
  1455. threadgroup_barrier(mem_flags::mem_threadgroup);
  1456. if (ith%4 == 0) {
  1457. sum[ith] += sum[ith+1] + sum[ith+2] + sum[ith+3];
  1458. }
  1459. threadgroup_barrier(mem_flags::mem_threadgroup);
  1460. if (ith%16 == 0) {
  1461. sum[ith] += sum[ith+4] + sum[ith+8] + sum[ith+12];
  1462. }
  1463. threadgroup_barrier(mem_flags::mem_threadgroup);
  1464. if (ith == 0) {
  1465. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1466. dst[r1*ne0 + r0] = sum[0];
  1467. }
  1468. }
  1469. kernel void kernel_mul_mat_q6_K_f32(
  1470. device const void * src0,
  1471. device const float * src1,
  1472. device float * dst,
  1473. constant int64_t & ne00,
  1474. constant int64_t & ne10,
  1475. constant int64_t & ne0,
  1476. threadgroup float * sum [[threadgroup(0)]],
  1477. uint2 tgpig[[threadgroup_position_in_grid]],
  1478. uint2 tpitg[[thread_position_in_threadgroup]],
  1479. uint2 tptg[[threads_per_threadgroup]]) {
  1480. const uint8_t kmask1 = 0x03;
  1481. const uint8_t kmask2 = 0x0C;
  1482. const uint8_t kmask3 = 0x30;
  1483. const uint8_t kmask4 = 0xC0;
  1484. const int nb = ne00/QK_K;
  1485. const int64_t r0 = tgpig.x;
  1486. const int64_t r1 = tgpig.y;
  1487. device const block_q6_K * x = (device const block_q6_K *) src0 + r0*nb;
  1488. device const float * yy = (device const float *) src1 + r1*ne10;
  1489. const int nth = tptg.x*tptg.y;
  1490. const int ith = tptg.y*tpitg.x + tpitg.y;
  1491. float sumf = 0;
  1492. #if QK_K == 256
  1493. // Note: we absolutely assume that tptg.y = 16 and QK_K = 256!
  1494. const int iqs = 16 * tpitg.y;
  1495. const int ip = iqs / 128; // 0 or 1
  1496. const int il = (iqs - 128*ip)/16; // 0...7
  1497. const int n = 4;
  1498. const int l0 = n*il;
  1499. const int is = 8*ip + l0/16;
  1500. const int y_offset = 128*ip + l0;
  1501. const int q_offset_l = 64*ip + l0;
  1502. const int q_offset_h = 32*ip + l0;
  1503. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1504. device const uint8_t * ql = x[i].ql + q_offset_l;
  1505. device const uint8_t * qh = x[i].qh + q_offset_h;
  1506. device const int8_t * sc = x[i].scales + is;
  1507. device const float * y = yy + i * QK_K + y_offset;
  1508. const float dall = x[i].d;
  1509. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1510. for (int l = 0; l < n; ++l) {
  1511. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1512. sums[1] += y[l+32] * ((int8_t)((ql[l+32] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1513. sums[2] += y[l+64] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1514. sums[3] += y[l+96] * ((int8_t)((ql[l+32] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1515. }
  1516. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1517. }
  1518. #else
  1519. const int il = 4*tpitg.x; // 0, 4, 8, 12
  1520. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1521. device const float * y = yy + i * QK_K + il;
  1522. device const uint8_t * ql = x[i].ql + il;
  1523. device const uint8_t * qh = x[i].qh + il;
  1524. device const int8_t * s = x[i].scales;
  1525. const float d = x[i].d;
  1526. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1527. for (int l = 0; l < 4; ++l) {
  1528. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1529. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1530. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1531. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1532. }
  1533. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1534. }
  1535. #endif
  1536. sum[ith] = sumf;
  1537. //
  1538. // Accumulate the sum from all threads in the threadgroup
  1539. //
  1540. threadgroup_barrier(mem_flags::mem_threadgroup);
  1541. if (ith%4 == 0) {
  1542. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1543. }
  1544. threadgroup_barrier(mem_flags::mem_threadgroup);
  1545. if (ith%16 == 0) {
  1546. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1547. }
  1548. threadgroup_barrier(mem_flags::mem_threadgroup);
  1549. if (ith == 0) {
  1550. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1551. dst[r1*ne0 + r0] = sum[0];
  1552. }
  1553. }