ggml-metal.metal 59 KB

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