ggml-metal.metal 66 KB

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