ggml-metal.metal 66 KB

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