ggml-metal.metal 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  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. #define QK8_0 32
  17. typedef struct {
  18. half d; // delta
  19. int8_t qs[QK8_0]; // quants
  20. } block_q8_0;
  21. kernel void kernel_add(
  22. device const float4 * src0,
  23. device const float4 * src1,
  24. device float4 * dst,
  25. uint tpig[[thread_position_in_grid]]) {
  26. dst[tpig] = src0[tpig] + src1[tpig];
  27. }
  28. // assumption: src1 is a row
  29. // broadcast src1 into src0
  30. kernel void kernel_add_row(
  31. device const float4 * src0,
  32. device const float4 * src1,
  33. device float4 * dst,
  34. constant int64_t & nb,
  35. uint tpig[[thread_position_in_grid]]) {
  36. dst[tpig] = src0[tpig] + src1[tpig % nb];
  37. }
  38. kernel void kernel_mul(
  39. device const float4 * src0,
  40. device const float4 * src1,
  41. device float4 * dst,
  42. uint tpig[[thread_position_in_grid]]) {
  43. dst[tpig] = src0[tpig] * src1[tpig];
  44. }
  45. // assumption: src1 is a row
  46. // broadcast src1 into src0
  47. kernel void kernel_mul_row(
  48. device const float4 * src0,
  49. device const float4 * src1,
  50. device float4 * dst,
  51. constant int64_t & nb,
  52. uint tpig[[thread_position_in_grid]]) {
  53. dst[tpig] = src0[tpig] * src1[tpig % nb];
  54. }
  55. kernel void kernel_scale(
  56. device const float4 * src0,
  57. device float4 * dst,
  58. constant float & scale,
  59. uint tpig[[thread_position_in_grid]]) {
  60. dst[tpig] = src0[tpig] * scale;
  61. }
  62. kernel void kernel_silu(
  63. device const float4 * src0,
  64. device float4 * dst,
  65. uint tpig[[thread_position_in_grid]]) {
  66. device const float4 & x = src0[tpig];
  67. dst[tpig] = x / (1.0f + exp(-x));
  68. }
  69. kernel void kernel_relu(
  70. device const float * src0,
  71. device float * dst,
  72. uint tpig[[thread_position_in_grid]]) {
  73. dst[tpig] = max(0.0f, src0[tpig]);
  74. }
  75. constant float GELU_COEF_A = 0.044715f;
  76. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  77. kernel void kernel_gelu(
  78. device const float4 * src0,
  79. device float4 * dst,
  80. uint tpig[[thread_position_in_grid]]) {
  81. device const float4 & x = src0[tpig];
  82. // BEWARE !!!
  83. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  84. // This was observed with Falcon 7B and 40B models
  85. //
  86. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  87. }
  88. kernel void kernel_soft_max(
  89. device const float * src0,
  90. device float * dst,
  91. constant int64_t & ne00,
  92. constant int64_t & ne01,
  93. constant int64_t & ne02,
  94. uint3 tgpig[[threadgroup_position_in_grid]],
  95. uint3 tpitg[[thread_position_in_threadgroup]],
  96. uint3 ntg[[threads_per_threadgroup]]) {
  97. const int64_t i03 = tgpig[2];
  98. const int64_t i02 = tgpig[1];
  99. const int64_t i01 = tgpig[0];
  100. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  101. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  102. // parallel max
  103. float lmax = psrc0[tpitg[0]];
  104. for (int i00 = tpitg[0] + ntg[0]; i00 < ne00; i00 += ntg[0]) {
  105. lmax = MAX(lmax, psrc0[i00]);
  106. }
  107. const float max = simd_max(lmax);
  108. // parallel sum
  109. float lsum = 0.0f;
  110. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  111. const float exp_psrc0 = exp(psrc0[i00] - max);
  112. lsum += exp_psrc0;
  113. // Remember the result of exp here. exp is expensive, so we really do not
  114. // whish to compute it twice.
  115. pdst[i00] = exp_psrc0;
  116. }
  117. const float sum = simd_sum(lsum);
  118. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  119. pdst[i00] /= sum;
  120. }
  121. }
  122. kernel void kernel_soft_max_4(
  123. device const float * src0,
  124. device float * dst,
  125. constant int64_t & ne00,
  126. constant int64_t & ne01,
  127. constant int64_t & ne02,
  128. uint3 tgpig[[threadgroup_position_in_grid]],
  129. uint3 tpitg[[thread_position_in_threadgroup]],
  130. uint3 ntg[[threads_per_threadgroup]]) {
  131. const int64_t i03 = tgpig[2];
  132. const int64_t i02 = tgpig[1];
  133. const int64_t i01 = tgpig[0];
  134. device const float4 * psrc4 = (device const float4 *)(src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  135. device float4 * pdst4 = (device float4 *)(dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  136. // parallel max
  137. float4 lmax4 = psrc4[tpitg[0]];
  138. for (int i00 = tpitg[0] + ntg[0]; i00 < ne00/4; i00 += ntg[0]) {
  139. lmax4 = fmax(lmax4, psrc4[i00]);
  140. }
  141. float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  142. const float max = simd_max(lmax);
  143. // parallel sum
  144. float4 lsum4 = 0.0f;
  145. for (int i00 = tpitg[0]; i00 < ne00/4; i00 += ntg[0]) {
  146. const float4 exp_psrc4 = exp(psrc4[i00] - max);
  147. lsum4 += exp_psrc4;
  148. pdst4[i00] = exp_psrc4;
  149. }
  150. float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  151. const float sum = simd_sum(lsum);
  152. for (int i00 = tpitg[0]; i00 < ne00/4; i00 += ntg[0]) {
  153. pdst4[i00] /= sum;
  154. }
  155. }
  156. kernel void kernel_diag_mask_inf(
  157. device const float * src0,
  158. device float * dst,
  159. constant int64_t & ne00,
  160. constant int64_t & ne01,
  161. constant int & n_past,
  162. uint3 tpig[[thread_position_in_grid]]) {
  163. const int64_t i02 = tpig[2];
  164. const int64_t i01 = tpig[1];
  165. const int64_t i00 = tpig[0];
  166. if (i00 > n_past + i01) {
  167. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  168. } else {
  169. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  170. }
  171. }
  172. kernel void kernel_diag_mask_inf_8(
  173. device const float4 * src0,
  174. device float4 * dst,
  175. constant int64_t & ne00,
  176. constant int64_t & ne01,
  177. constant int & n_past,
  178. uint3 tpig[[thread_position_in_grid]]) {
  179. const int64_t i = 2*tpig[0];
  180. dst[i+0] = src0[i+0];
  181. dst[i+1] = src0[i+1];
  182. int64_t i4 = 4*i;
  183. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  184. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  185. const int64_t i00 = i4;
  186. for (int k = 3; k >= 0; --k) {
  187. if (i00 + 4 + k <= n_past + i01) {
  188. break;
  189. }
  190. dst[i+1][k] = -INFINITY;
  191. if (i00 + k > n_past + i01) {
  192. dst[i][k] = -INFINITY;
  193. }
  194. }
  195. }
  196. kernel void kernel_norm(
  197. device const void * src0,
  198. device float * dst,
  199. constant int64_t & ne00,
  200. constant uint64_t & nb01,
  201. constant float & eps,
  202. threadgroup float * sum [[threadgroup(0)]],
  203. uint tgpig[[threadgroup_position_in_grid]],
  204. uint tpitg[[thread_position_in_threadgroup]],
  205. uint ntg[[threads_per_threadgroup]]) {
  206. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  207. // MEAN
  208. // parallel sum
  209. sum[tpitg] = 0.0f;
  210. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  211. sum[tpitg] += x[i00];
  212. }
  213. // reduce
  214. threadgroup_barrier(mem_flags::mem_threadgroup);
  215. for (uint i = ntg/2; i > 0; i /= 2) {
  216. if (tpitg < i) {
  217. sum[tpitg] += sum[tpitg + i];
  218. }
  219. threadgroup_barrier(mem_flags::mem_threadgroup);
  220. }
  221. const float mean = sum[0] / ne00;
  222. // recenter and VARIANCE
  223. threadgroup_barrier(mem_flags::mem_threadgroup);
  224. device float * y = dst + tgpig*ne00;
  225. sum[tpitg] = 0.0f;
  226. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  227. y[i00] = x[i00] - mean;
  228. sum[tpitg] += y[i00] * y[i00];
  229. }
  230. // reduce
  231. threadgroup_barrier(mem_flags::mem_threadgroup);
  232. for (uint i = ntg/2; i > 0; i /= 2) {
  233. if (tpitg < i) {
  234. sum[tpitg] += sum[tpitg + i];
  235. }
  236. threadgroup_barrier(mem_flags::mem_threadgroup);
  237. }
  238. const float variance = sum[0] / ne00;
  239. const float scale = 1.0f/sqrt(variance + eps);
  240. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  241. y[i00] = y[i00] * scale;
  242. }
  243. }
  244. kernel void kernel_rms_norm(
  245. device const void * src0,
  246. device float * dst,
  247. constant int64_t & ne00,
  248. constant uint64_t & nb01,
  249. constant float & eps,
  250. threadgroup float * sum [[threadgroup(0)]],
  251. uint tgpig[[threadgroup_position_in_grid]],
  252. uint tpitg[[thread_position_in_threadgroup]],
  253. uint sgitg[[simdgroup_index_in_threadgroup]],
  254. uint tiisg[[thread_index_in_simdgroup]],
  255. uint ntg[[threads_per_threadgroup]]) {
  256. device const float4 * x = (device const float4 *) ((device const char *) src0 + tgpig*nb01);
  257. device const float * x_scalar = (device const float *) x;
  258. float4 sumf=0;
  259. float all_sum=0;
  260. // parallel sum
  261. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  262. sumf += x[i00] * x[i00];
  263. }
  264. all_sum = sumf[0] + sumf[1] + sumf[2] + sumf[3];
  265. all_sum = simd_sum(all_sum);
  266. if (tiisg == 0) {
  267. sum[sgitg] = all_sum;
  268. }
  269. threadgroup_barrier(mem_flags::mem_threadgroup);
  270. // broadcast, simd group number is ntg / 32
  271. for (uint i = ntg / 32 / 2; i > 0; i /= 2) {
  272. if (tpitg < i) {
  273. sum[tpitg] += sum[tpitg + i];
  274. }
  275. }
  276. if (tpitg == 0) {
  277. for (int i = 4 * (ne00 / 4); i < ne00; i++) {sum[0] += x_scalar[i];}
  278. sum[0] /= ne00;
  279. }
  280. threadgroup_barrier(mem_flags::mem_threadgroup);
  281. const float mean = sum[0];
  282. const float scale = 1.0f/sqrt(mean + eps);
  283. device float4 * y = (device float4 *) (dst + tgpig*ne00);
  284. device float * y_scalar = (device float *) y;
  285. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  286. y[i00] = x[i00] * scale;
  287. }
  288. if (tpitg == 0) {
  289. for (int i00 = 4 * (ne00 / 4); i00 < ne00; i00++) {y_scalar[i00] = x_scalar[i00] * scale;}
  290. }
  291. }
  292. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  293. // il indicates where the q4 quants begin (0 or QK4_0/4)
  294. // we assume that the yl's have been multiplied with the appropriate scale factor
  295. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  296. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  297. float d = qb_curr->d;
  298. float2 acc = 0.f;
  299. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 1 + il/2);
  300. for (int i = 0; i < 8; i+=2) {
  301. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  302. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  303. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  304. + yl[i + 9] * (qs[i / 2] & 0xF000);
  305. }
  306. return d * (sumy * -8.f + acc[0] + acc[1]);
  307. }
  308. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  309. // il indicates where the q4 quants begin (0 or QK4_0/4)
  310. // we assume that the yl's have been multiplied with the appropriate scale factor
  311. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  312. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  313. float d = qb_curr->d;
  314. float m = qb_curr->m;
  315. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 2 + il/2);
  316. float2 acc = 0.f;
  317. for (int i = 0; i < 8; i+=2) {
  318. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  319. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  320. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  321. + yl[i + 9] * (qs[i / 2] & 0xF000);
  322. }
  323. return d * (acc[0] + acc[1]) + sumy * m;
  324. }
  325. // putting them in the kernel cause a significant performance penalty
  326. #define N_DST 4 // each SIMD group works on 4 rows
  327. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  328. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  329. //Note: This is a template, but strictly speaking it only applies to
  330. // quantizations where the block size is 32. It also does not
  331. // giard against the number of rows not being divisible by
  332. // N_DST, so this is another explicit assumption of the implementation.
  333. template<typename block_q_type, int nr, int nsg, int nw>
  334. void mul_vec_q_n_f32(device const void * src0, device const float * src1, device float * dst,
  335. int64_t ne00, int64_t ne01, int64_t ne02, int64_t ne10, int64_t ne12, int64_t ne0, int64_t ne1, uint gqa,
  336. uint3 tgpig, uint tiisg, uint sgitg) {
  337. const int nb = ne00/QK4_0;
  338. const int r0 = tgpig.x;
  339. const int r1 = tgpig.y;
  340. const int im = tgpig.z;
  341. const int first_row = (r0 * nsg + sgitg) * nr;
  342. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  343. device const block_q_type * x = (device const block_q_type *) src0 + offset0;
  344. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  345. float yl[16]; // src1 vector cache
  346. float sumf[nr]={0.f};
  347. const int ix = tiisg/2;
  348. const int il = 8*(tiisg%2);
  349. device const float * yb = y + ix * QK4_0 + il;
  350. // each thread in a SIMD group deals with half a block.
  351. for (int ib = ix; ib < nb; ib += nw/2) {
  352. float sumy = 0;
  353. for (int i = 0; i < 8; i += 2) {
  354. sumy += yb[i] + yb[i+1];
  355. yl[i+0] = yb[i+ 0];
  356. yl[i+1] = yb[i+ 1]/256.f;
  357. sumy += yb[i+16] + yb[i+17];
  358. yl[i+8] = yb[i+16]/16.f;
  359. yl[i+9] = yb[i+17]/4096.f;
  360. }
  361. for (int row = 0; row < nr; row++) {
  362. sumf[row] += block_q_n_dot_y(x+ib+row*nb, sumy, yl, il);
  363. }
  364. yb += QK4_0 * 16;
  365. }
  366. for (int row = 0; row < nr; ++row) {
  367. const float tot = simd_sum(sumf[row]);
  368. if (tiisg == 0 && first_row + row < ne01) {
  369. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  370. }
  371. }
  372. }
  373. kernel void kernel_mul_mat_q4_0_f32(
  374. device const void * src0,
  375. device const float * src1,
  376. device float * dst,
  377. constant int64_t & ne00,
  378. constant int64_t & ne01[[buffer(4)]],
  379. constant int64_t & ne02[[buffer(5)]],
  380. constant int64_t & ne10[[buffer(9)]],
  381. constant int64_t & ne12[[buffer(11)]],
  382. constant int64_t & ne0[[buffer(15)]],
  383. constant int64_t & ne1[[buffer(16)]],
  384. constant uint & gqa[[buffer(17)]],
  385. uint3 tgpig[[threadgroup_position_in_grid]],
  386. uint tiisg[[thread_index_in_simdgroup]],
  387. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  388. mul_vec_q_n_f32<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  389. }
  390. kernel void kernel_mul_mat_q4_1_f32(
  391. device const void * src0,
  392. device const float * src1,
  393. device float * dst,
  394. constant int64_t & ne00,
  395. constant int64_t & ne01[[buffer(4)]],
  396. constant int64_t & ne02[[buffer(5)]],
  397. constant int64_t & ne10[[buffer(9)]],
  398. constant int64_t & ne12[[buffer(11)]],
  399. constant int64_t & ne0[[buffer(15)]],
  400. constant int64_t & ne1[[buffer(16)]],
  401. constant uint & gqa[[buffer(17)]],
  402. uint3 tgpig[[threadgroup_position_in_grid]],
  403. uint tiisg[[thread_index_in_simdgroup]],
  404. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  405. mul_vec_q_n_f32<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  406. }
  407. #define NB_Q8_0 8
  408. kernel void kernel_mul_mat_q8_0_f32(
  409. device const void * src0,
  410. device const float * src1,
  411. device float * dst,
  412. constant int64_t & ne00,
  413. constant int64_t & ne01[[buffer(4)]],
  414. constant int64_t & ne02[[buffer(5)]],
  415. constant int64_t & ne10[[buffer(9)]],
  416. constant int64_t & ne12[[buffer(11)]],
  417. constant int64_t & ne0[[buffer(15)]],
  418. constant int64_t & ne1[[buffer(16)]],
  419. constant uint & gqa[[buffer(17)]],
  420. uint3 tgpig[[threadgroup_position_in_grid]],
  421. uint tiisg[[thread_index_in_simdgroup]],
  422. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  423. const int nr = N_DST;
  424. const int nsg = N_SIMDGROUP;
  425. const int nw = N_SIMDWIDTH;
  426. const int nb = ne00/QK8_0;
  427. const int r0 = tgpig.x;
  428. const int r1 = tgpig.y;
  429. const int im = tgpig.z;
  430. const int first_row = (r0 * nsg + sgitg) * nr;
  431. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  432. device const block_q8_0 * x = (device const block_q8_0 *) src0 + offset0;
  433. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  434. float yl[NB_Q8_0];
  435. float sumf[nr]={0.f};
  436. const int ix = tiisg/4;
  437. const int il = tiisg%4;
  438. device const float * yb = y + ix * QK8_0 + NB_Q8_0*il;
  439. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  440. for (int ib = ix; ib < nb; ib += nw/4) {
  441. for (int i = 0; i < NB_Q8_0; ++i) {
  442. yl[i] = yb[i];
  443. }
  444. for (int row = 0; row < nr; row++) {
  445. device const int8_t * qs = x[ib+row*nb].qs + NB_Q8_0*il;
  446. float sumq = 0.f;
  447. for (int iq = 0; iq < NB_Q8_0; ++iq) {
  448. sumq += qs[iq] * yl[iq];
  449. }
  450. sumf[row] += sumq*x[ib+row*nb].d;
  451. }
  452. yb += NB_Q8_0 * nw;
  453. }
  454. for (int row = 0; row < nr; ++row) {
  455. const float tot = simd_sum(sumf[row]);
  456. if (tiisg == 0 && first_row + row < ne01) {
  457. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  458. }
  459. }
  460. }
  461. kernel void kernel_mul_mat_f16_f32_1row(
  462. device const char * src0,
  463. device const char * src1,
  464. device float * dst,
  465. constant int64_t & ne00,
  466. constant int64_t & ne01,
  467. constant int64_t & ne02,
  468. constant uint64_t & nb00,
  469. constant uint64_t & nb01,
  470. constant uint64_t & nb02,
  471. constant int64_t & ne10,
  472. constant int64_t & ne11,
  473. constant int64_t & ne12,
  474. constant uint64_t & nb10,
  475. constant uint64_t & nb11,
  476. constant uint64_t & nb12,
  477. constant int64_t & ne0,
  478. constant int64_t & ne1,
  479. uint3 tgpig[[threadgroup_position_in_grid]],
  480. uint tiisg[[thread_index_in_simdgroup]]) {
  481. const int64_t r0 = tgpig.x;
  482. const int64_t r1 = tgpig.y;
  483. const int64_t im = tgpig.z;
  484. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  485. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  486. float sumf = 0;
  487. if (ne00 < 128) {
  488. for (int i = tiisg; i < ne00; i += 32) {
  489. sumf += (float) x[i] * (float) y[i];
  490. }
  491. float all_sum = simd_sum(sumf);
  492. if (tiisg == 0) {
  493. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  494. }
  495. } else {
  496. device const half4 * x4 = (device const half4 *) x;
  497. device const float4 * y4 = (device const float4 *) y;
  498. for (int i = tiisg; i < ne00/4; i += 32) {
  499. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  500. }
  501. float all_sum = simd_sum(sumf);
  502. if (tiisg == 0) {
  503. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  504. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  505. }
  506. }
  507. }
  508. #define N_F16_F32 4
  509. kernel void kernel_mul_mat_f16_f32(
  510. device const char * src0,
  511. device const char * src1,
  512. device float * dst,
  513. constant int64_t & ne00,
  514. constant int64_t & ne01,
  515. constant int64_t & ne02,
  516. constant uint64_t & nb00,
  517. constant uint64_t & nb01,
  518. constant uint64_t & nb02,
  519. constant int64_t & ne10,
  520. constant int64_t & ne11,
  521. constant int64_t & ne12,
  522. constant uint64_t & nb10,
  523. constant uint64_t & nb11,
  524. constant uint64_t & nb12,
  525. constant int64_t & ne0,
  526. constant int64_t & ne1,
  527. uint3 tgpig[[threadgroup_position_in_grid]],
  528. uint tiisg[[thread_index_in_simdgroup]]) {
  529. const int64_t r0 = tgpig.x;
  530. const int64_t rb = tgpig.y*N_F16_F32;
  531. const int64_t im = tgpig.z;
  532. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  533. if (ne00 < 128) {
  534. for (int row = 0; row < N_F16_F32; ++row) {
  535. int r1 = rb + row;
  536. if (r1 >= ne11) {
  537. break;
  538. }
  539. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  540. float sumf = 0;
  541. for (int i = tiisg; i < ne00; i += 32) {
  542. sumf += (float) x[i] * (float) y[i];
  543. }
  544. float all_sum = simd_sum(sumf);
  545. if (tiisg == 0) {
  546. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  547. }
  548. }
  549. } else {
  550. device const half4 * x4 = (device const half4 *)x;
  551. for (int row = 0; row < N_F16_F32; ++row) {
  552. int r1 = rb + row;
  553. if (r1 >= ne11) {
  554. break;
  555. }
  556. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  557. device const float4 * y4 = (device const float4 *) y;
  558. float sumf = 0;
  559. for (int i = tiisg; i < ne00/4; i += 32) {
  560. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  561. }
  562. float all_sum = simd_sum(sumf);
  563. if (tiisg == 0) {
  564. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  565. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  566. }
  567. }
  568. }
  569. }
  570. // Assumes row size (ne00) is a multiple of 4
  571. kernel void kernel_mul_mat_f16_f32_l4(
  572. device const char * src0,
  573. device const char * src1,
  574. device float * dst,
  575. constant int64_t & ne00,
  576. constant int64_t & ne01,
  577. constant int64_t & ne02,
  578. constant uint64_t & nb00,
  579. constant uint64_t & nb01,
  580. constant uint64_t & nb02,
  581. constant int64_t & ne10,
  582. constant int64_t & ne11,
  583. constant int64_t & ne12,
  584. constant uint64_t & nb10,
  585. constant uint64_t & nb11,
  586. constant uint64_t & nb12,
  587. constant int64_t & ne0,
  588. constant int64_t & ne1,
  589. uint3 tgpig[[threadgroup_position_in_grid]],
  590. uint tiisg[[thread_index_in_simdgroup]]) {
  591. const int nrows = ne11;
  592. const int64_t r0 = tgpig.x;
  593. const int64_t im = tgpig.z;
  594. device const half4 * x4 = (device const half4 *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  595. for (int r1 = 0; r1 < nrows; ++r1) {
  596. device const float4 * y4 = (device const float4 *) (src1 + r1*nb11 + im*nb12);
  597. float sumf = 0;
  598. for (int i = tiisg; i < ne00/4; i += 32) {
  599. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  600. }
  601. float all_sum = simd_sum(sumf);
  602. if (tiisg == 0) {
  603. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  604. }
  605. }
  606. }
  607. kernel void kernel_alibi_f32(
  608. device const float * src0,
  609. device float * dst,
  610. constant int64_t & ne00,
  611. constant int64_t & ne01,
  612. constant int64_t & ne02,
  613. constant int64_t & ne03,
  614. constant uint64_t & nb00,
  615. constant uint64_t & nb01,
  616. constant uint64_t & nb02,
  617. constant uint64_t & nb03,
  618. constant int64_t & ne0,
  619. constant int64_t & ne1,
  620. constant int64_t & ne2,
  621. constant int64_t & ne3,
  622. constant uint64_t & nb0,
  623. constant uint64_t & nb1,
  624. constant uint64_t & nb2,
  625. constant uint64_t & nb3,
  626. constant float & m0,
  627. uint3 tgpig[[threadgroup_position_in_grid]],
  628. uint3 tpitg[[thread_position_in_threadgroup]],
  629. uint3 ntg[[threads_per_threadgroup]]) {
  630. const int64_t i03 = tgpig[2];
  631. const int64_t i02 = tgpig[1];
  632. const int64_t i01 = tgpig[0];
  633. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  634. const int64_t i3 = n / (ne2*ne1*ne0);
  635. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  636. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  637. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  638. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  639. float m_k = pow(m0, i2 + 1);
  640. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  641. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  642. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  643. }
  644. }
  645. kernel void kernel_rope(
  646. device const void * src0,
  647. device float * dst,
  648. constant int64_t & ne00,
  649. constant int64_t & ne01,
  650. constant int64_t & ne02,
  651. constant int64_t & ne03,
  652. constant uint64_t & nb00,
  653. constant uint64_t & nb01,
  654. constant uint64_t & nb02,
  655. constant uint64_t & nb03,
  656. constant int64_t & ne0,
  657. constant int64_t & ne1,
  658. constant int64_t & ne2,
  659. constant int64_t & ne3,
  660. constant uint64_t & nb0,
  661. constant uint64_t & nb1,
  662. constant uint64_t & nb2,
  663. constant uint64_t & nb3,
  664. constant int & n_past,
  665. constant int & n_dims,
  666. constant int & mode,
  667. constant float & freq_base,
  668. constant float & freq_scale,
  669. uint tiitg[[thread_index_in_threadgroup]],
  670. uint3 tptg[[threads_per_threadgroup]],
  671. uint3 tgpig[[threadgroup_position_in_grid]]) {
  672. const int64_t i3 = tgpig[2];
  673. const int64_t i2 = tgpig[1];
  674. const int64_t i1 = tgpig[0];
  675. const bool is_neox = mode & 2;
  676. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  677. const float theta_0 = freq_scale * (float)p;
  678. const float inv_ndims = -1.f/n_dims;
  679. if (!is_neox) {
  680. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  681. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  682. const float cos_theta = cos(theta);
  683. const float sin_theta = sin(theta);
  684. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  685. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  686. const float x0 = src[0];
  687. const float x1 = src[1];
  688. dst_data[0] = x0*cos_theta - x1*sin_theta;
  689. dst_data[1] = x0*sin_theta + x1*cos_theta;
  690. }
  691. } else {
  692. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  693. for (int64_t ic = 2*tiitg; ic < n_dims; ic += 2*tptg.x) {
  694. const float theta = theta_0 * pow(freq_base, inv_ndims*ic - ib);
  695. const float cos_theta = cos(theta);
  696. const float sin_theta = sin(theta);
  697. const int64_t i0 = ib*n_dims + ic/2;
  698. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  699. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  700. const float x0 = src[0];
  701. const float x1 = src[n_dims/2];
  702. dst_data[0] = x0*cos_theta - x1*sin_theta;
  703. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  704. }
  705. }
  706. }
  707. }
  708. kernel void kernel_cpy_f16_f16(
  709. device const half * src0,
  710. device half * dst,
  711. constant int64_t & ne00,
  712. constant int64_t & ne01,
  713. constant int64_t & ne02,
  714. constant int64_t & ne03,
  715. constant uint64_t & nb00,
  716. constant uint64_t & nb01,
  717. constant uint64_t & nb02,
  718. constant uint64_t & nb03,
  719. constant int64_t & ne0,
  720. constant int64_t & ne1,
  721. constant int64_t & ne2,
  722. constant int64_t & ne3,
  723. constant uint64_t & nb0,
  724. constant uint64_t & nb1,
  725. constant uint64_t & nb2,
  726. constant uint64_t & nb3,
  727. uint3 tgpig[[threadgroup_position_in_grid]],
  728. uint3 tpitg[[thread_position_in_threadgroup]],
  729. uint3 ntg[[threads_per_threadgroup]]) {
  730. const int64_t i03 = tgpig[2];
  731. const int64_t i02 = tgpig[1];
  732. const int64_t i01 = tgpig[0];
  733. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  734. const int64_t i3 = n / (ne2*ne1*ne0);
  735. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  736. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  737. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  738. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  739. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  740. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  741. dst_data[i00] = src[0];
  742. }
  743. }
  744. kernel void kernel_cpy_f32_f16(
  745. device const float * src0,
  746. device half * dst,
  747. constant int64_t & ne00,
  748. constant int64_t & ne01,
  749. constant int64_t & ne02,
  750. constant int64_t & ne03,
  751. constant uint64_t & nb00,
  752. constant uint64_t & nb01,
  753. constant uint64_t & nb02,
  754. constant uint64_t & nb03,
  755. constant int64_t & ne0,
  756. constant int64_t & ne1,
  757. constant int64_t & ne2,
  758. constant int64_t & ne3,
  759. constant uint64_t & nb0,
  760. constant uint64_t & nb1,
  761. constant uint64_t & nb2,
  762. constant uint64_t & nb3,
  763. uint3 tgpig[[threadgroup_position_in_grid]],
  764. uint3 tpitg[[thread_position_in_threadgroup]],
  765. uint3 ntg[[threads_per_threadgroup]]) {
  766. const int64_t i03 = tgpig[2];
  767. const int64_t i02 = tgpig[1];
  768. const int64_t i01 = tgpig[0];
  769. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  770. const int64_t i3 = n / (ne2*ne1*ne0);
  771. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  772. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  773. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  774. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  775. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  776. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  777. dst_data[i00] = src[0];
  778. }
  779. }
  780. kernel void kernel_cpy_f32_f32(
  781. device const float * src0,
  782. device float * dst,
  783. constant int64_t & ne00,
  784. constant int64_t & ne01,
  785. constant int64_t & ne02,
  786. constant int64_t & ne03,
  787. constant uint64_t & nb00,
  788. constant uint64_t & nb01,
  789. constant uint64_t & nb02,
  790. constant uint64_t & nb03,
  791. constant int64_t & ne0,
  792. constant int64_t & ne1,
  793. constant int64_t & ne2,
  794. constant int64_t & ne3,
  795. constant uint64_t & nb0,
  796. constant uint64_t & nb1,
  797. constant uint64_t & nb2,
  798. constant uint64_t & nb3,
  799. uint3 tgpig[[threadgroup_position_in_grid]],
  800. uint3 tpitg[[thread_position_in_threadgroup]],
  801. uint3 ntg[[threads_per_threadgroup]]) {
  802. const int64_t i03 = tgpig[2];
  803. const int64_t i02 = tgpig[1];
  804. const int64_t i01 = tgpig[0];
  805. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  806. const int64_t i3 = n / (ne2*ne1*ne0);
  807. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  808. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  809. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  810. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  811. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  812. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  813. dst_data[i00] = src[0];
  814. }
  815. }
  816. //============================================ k-quants ======================================================
  817. #ifndef QK_K
  818. #define QK_K 256
  819. #else
  820. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  821. #endif
  822. #if QK_K == 256
  823. #define K_SCALE_SIZE 12
  824. #else
  825. #define K_SCALE_SIZE 4
  826. #endif
  827. typedef struct {
  828. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  829. uint8_t qs[QK_K/4]; // quants
  830. half d; // super-block scale for quantized scales
  831. half dmin; // super-block scale for quantized mins
  832. } block_q2_K;
  833. // 84 bytes / block
  834. typedef struct {
  835. uint8_t hmask[QK_K/8]; // quants - high bit
  836. uint8_t qs[QK_K/4]; // quants - low 2 bits
  837. #if QK_K == 64
  838. uint8_t scales[2];
  839. #else
  840. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  841. #endif
  842. half d; // super-block scale
  843. } block_q3_K;
  844. #if QK_K == 64
  845. typedef struct {
  846. half d[2]; // super-block scales/mins
  847. uint8_t scales[2];
  848. uint8_t qs[QK_K/2]; // 4-bit quants
  849. } block_q4_K;
  850. #else
  851. typedef struct {
  852. half d; // super-block scale for quantized scales
  853. half dmin; // super-block scale for quantized mins
  854. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  855. uint8_t qs[QK_K/2]; // 4--bit quants
  856. } block_q4_K;
  857. #endif
  858. #if QK_K == 64
  859. typedef struct {
  860. half d; // super-block scales/mins
  861. int8_t scales[QK_K/16]; // 8-bit block scales
  862. uint8_t qh[QK_K/8]; // quants, high bit
  863. uint8_t qs[QK_K/2]; // quants, low 4 bits
  864. } block_q5_K;
  865. #else
  866. typedef struct {
  867. half d; // super-block scale for quantized scales
  868. half dmin; // super-block scale for quantized mins
  869. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  870. uint8_t qh[QK_K/8]; // quants, high bit
  871. uint8_t qs[QK_K/2]; // quants, low 4 bits
  872. } block_q5_K;
  873. // 176 bytes / block
  874. #endif
  875. typedef struct {
  876. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  877. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  878. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  879. half d; // super-block scale
  880. } block_q6_K;
  881. // 210 bytes / block
  882. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  883. uchar4 r;
  884. if (j < 4) {
  885. r[0] = q[j+0] & 63;
  886. r[2] = q[j+1] & 63;
  887. r[1] = q[j+4] & 63;
  888. r[3] = q[j+5] & 63;
  889. } else {
  890. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  891. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  892. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  893. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  894. }
  895. return r;
  896. }
  897. //====================================== dot products =========================
  898. kernel void kernel_mul_mat_q2_K_f32(
  899. device const void * src0,
  900. device const float * src1,
  901. device float * dst,
  902. constant int64_t & ne00,
  903. constant int64_t & ne01[[buffer(4)]],
  904. constant int64_t & ne02[[buffer(5)]],
  905. constant int64_t & ne10[[buffer(9)]],
  906. constant int64_t & ne12[[buffer(11)]],
  907. constant int64_t & ne0[[buffer(15)]],
  908. constant int64_t & ne1[[buffer(16)]],
  909. constant uint & gqa[[buffer(17)]],
  910. uint3 tgpig[[threadgroup_position_in_grid]],
  911. uint tiisg[[thread_index_in_simdgroup]],
  912. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  913. const int nb = ne00/QK_K;
  914. const int r0 = tgpig.x;
  915. const int r1 = tgpig.y;
  916. const int r2 = tgpig.z;
  917. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  918. const int ib_row = first_row * nb;
  919. const uint offset0 = r2/gqa*(nb*ne0);
  920. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  921. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  922. float yl[32];
  923. float sumf[N_DST]={0.f}, all_sum;
  924. const int step = sizeof(block_q2_K) * nb;
  925. #if QK_K == 256
  926. const int ix = tiisg/8; // 0...3
  927. const int it = tiisg%8; // 0...7
  928. const int im = it/4; // 0 or 1
  929. const int ir = it%4; // 0...3
  930. const int is = (8*ir)/16;// 0 or 1
  931. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  932. for (int ib = ix; ib < nb; ib += 4) {
  933. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  934. for (int i = 0; i < 8; ++i) {
  935. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  936. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  937. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  938. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  939. }
  940. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  941. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  942. device const half * dh = &x[ib].d;
  943. for (int row = 0; row < N_DST; row++) {
  944. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  945. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  946. for (int i = 0; i < 8; i += 2) {
  947. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  948. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  949. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  950. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  951. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  952. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  953. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  954. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  955. }
  956. float dall = dh[0];
  957. float dmin = dh[1] * 1.f/16.f;
  958. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  959. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  960. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  961. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  962. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  963. qs += step/2;
  964. sc += step;
  965. dh += step/2;
  966. }
  967. y4 += 4 * QK_K;
  968. }
  969. #else
  970. const int ix = tiisg/2; // 0...15
  971. const int it = tiisg%2; // 0...1
  972. device const float * y4 = y + ix * QK_K + 8 * it;
  973. for (int ib = ix; ib < nb; ib += 16) {
  974. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  975. for (int i = 0; i < 8; ++i) {
  976. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  977. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  978. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  979. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  980. }
  981. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  982. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  983. device const half * dh = &x[ib].d;
  984. for (int row = 0; row < N_DST; row++) {
  985. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  986. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  987. for (int i = 0; i < 8; i += 2) {
  988. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  989. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  990. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  991. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  992. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  993. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  994. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  995. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  996. }
  997. float dall = dh[0];
  998. float dmin = dh[1];
  999. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1000. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  1001. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  1002. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  1003. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  1004. qs += step/2;
  1005. sc += step;
  1006. dh += step/2;
  1007. }
  1008. y4 += 16 * QK_K;
  1009. }
  1010. #endif
  1011. for (int row = 0; row < N_DST; ++row) {
  1012. all_sum = simd_sum(sumf[row]);
  1013. if (tiisg == 0) {
  1014. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1015. }
  1016. }
  1017. }
  1018. #if QK_K == 256
  1019. kernel void kernel_mul_mat_q3_K_f32(
  1020. device const void * src0,
  1021. device const float * src1,
  1022. device float * dst,
  1023. constant int64_t & ne00,
  1024. constant int64_t & ne01[[buffer(4)]],
  1025. constant int64_t & ne02[[buffer(5)]],
  1026. constant int64_t & ne10[[buffer(9)]],
  1027. constant int64_t & ne12[[buffer(11)]],
  1028. constant int64_t & ne0[[buffer(15)]],
  1029. constant int64_t & ne1[[buffer(16)]],
  1030. constant uint & gqa[[buffer(17)]],
  1031. uint3 tgpig[[threadgroup_position_in_grid]],
  1032. uint tiisg[[thread_index_in_simdgroup]],
  1033. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1034. const int nb = ne00/QK_K;
  1035. const int64_t r0 = tgpig.x;
  1036. const int64_t r1 = tgpig.y;
  1037. const int64_t r2 = tgpig.z;
  1038. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1039. const uint offset0 = r2/gqa*(nb*ne0);
  1040. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  1041. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1042. float yl[32];
  1043. const uint16_t kmask1 = 0x3030;
  1044. const uint16_t kmask2 = 0x0f0f;
  1045. const int tid = tiisg/4;
  1046. const int ix = tiisg%4;
  1047. const int ip = tid/4; // 0 or 1
  1048. const int il = 2*((tid%4)/2); // 0 or 2
  1049. const int ir = tid%2;
  1050. const int n = 8;
  1051. const int l0 = n*ir;
  1052. // One would think that the Metal compiler would figure out that ip and il can only have
  1053. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  1054. // with these two tales.
  1055. //
  1056. // Possible masks for the high bit
  1057. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  1058. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  1059. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  1060. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  1061. // Possible masks for the low 2 bits
  1062. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  1063. const ushort4 hm = mm[2*ip + il/2];
  1064. const int shift = 2*il;
  1065. const float v1 = il == 0 ? 4.f : 64.f;
  1066. const float v2 = 4.f * v1;
  1067. const uint16_t s_shift1 = 4*ip;
  1068. const uint16_t s_shift2 = s_shift1 + il;
  1069. const int q_offset = 32*ip + l0;
  1070. const int y_offset = 128*ip + 32*il + l0;
  1071. const int step = sizeof(block_q3_K) * nb / 2;
  1072. device const float * y1 = yy + ix*QK_K + y_offset;
  1073. uint32_t scales32, aux32;
  1074. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  1075. thread const int8_t * scales = (thread const int8_t *)&scales32;
  1076. float sumf1[2] = {0.f};
  1077. float sumf2[2] = {0.f};
  1078. for (int i = ix; i < nb; i += 4) {
  1079. for (int l = 0; l < 8; ++l) {
  1080. yl[l+ 0] = y1[l+ 0];
  1081. yl[l+ 8] = y1[l+16];
  1082. yl[l+16] = y1[l+32];
  1083. yl[l+24] = y1[l+48];
  1084. }
  1085. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1086. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1087. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1088. device const half * dh = &x[i].d;
  1089. for (int row = 0; row < 2; ++row) {
  1090. const float d_all = (float)dh[0];
  1091. scales16[0] = a[4];
  1092. scales16[1] = a[5];
  1093. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  1094. scales16[0] = a[il+0];
  1095. scales16[1] = a[il+1];
  1096. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  1097. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  1098. for (int l = 0; l < n; l += 2) {
  1099. const int32_t qs = q[l/2];
  1100. s1 += yl[l+0] * (qs & qm[il/2][0]);
  1101. s2 += yl[l+1] * (qs & qm[il/2][1]);
  1102. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  1103. s4 += yl[l+16] * (qs & qm[il/2][2]);
  1104. s5 += yl[l+17] * (qs & qm[il/2][3]);
  1105. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  1106. }
  1107. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1108. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1109. sumf1[row] += d1 * (scales[0] - 32);
  1110. sumf2[row] += d2 * (scales[2] - 32);
  1111. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  1112. for (int l = 0; l < n; l += 2) {
  1113. const int32_t qs = q[l/2+8];
  1114. s1 += yl[l+8] * (qs & qm[il/2][0]);
  1115. s2 += yl[l+9] * (qs & qm[il/2][1]);
  1116. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  1117. s4 += yl[l+24] * (qs & qm[il/2][2]);
  1118. s5 += yl[l+25] * (qs & qm[il/2][3]);
  1119. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  1120. }
  1121. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1122. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1123. sumf1[row] += d1 * (scales[1] - 32);
  1124. sumf2[row] += d2 * (scales[3] - 32);
  1125. q += step;
  1126. h += step;
  1127. a += step;
  1128. dh += step;
  1129. }
  1130. y1 += 4 * QK_K;
  1131. }
  1132. for (int row = 0; row < 2; ++row) {
  1133. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  1134. sumf1[row] = simd_sum(sumf);
  1135. }
  1136. if (tiisg == 0) {
  1137. for (int row = 0; row < 2; ++row) {
  1138. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = sumf1[row];
  1139. }
  1140. }
  1141. }
  1142. #else
  1143. kernel void kernel_mul_mat_q3_K_f32(
  1144. device const void * src0,
  1145. device const float * src1,
  1146. device float * dst,
  1147. constant int64_t & ne00,
  1148. constant int64_t & ne01[[buffer(4)]],
  1149. constant int64_t & ne02[[buffer(5)]],
  1150. constant int64_t & ne10[[buffer(9)]],
  1151. constant int64_t & ne12[[buffer(11)]],
  1152. constant int64_t & ne0[[buffer(15)]],
  1153. constant int64_t & ne1[[buffer(16)]],
  1154. constant uint & gqa[[buffer(17)]],
  1155. uint3 tgpig[[threadgroup_position_in_grid]],
  1156. uint tiisg[[thread_index_in_simdgroup]],
  1157. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1158. const int nb = ne00/QK_K;
  1159. const int64_t r0 = tgpig.x;
  1160. const int64_t r1 = tgpig.y;
  1161. const int64_t r2 = tgpig.z;
  1162. const int row = 2 * r0 + sgitg;
  1163. const uint offset0 = r2/gqa*(nb*ne0);
  1164. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1165. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1166. const int ix = tiisg/4;
  1167. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1168. const int im = il/8; // 0, 0, 1, 1
  1169. const int in = il%8; // 0, 4, 0, 4
  1170. float2 sum = {0.f, 0.f};
  1171. for (int i = ix; i < nb; i += 8) {
  1172. const float d_all = (float)(x[i].d);
  1173. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1174. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1175. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1176. device const float * y = yy + i * QK_K + il;
  1177. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1178. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1179. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1180. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1181. for (int l = 0; l < 4; l += 2) {
  1182. const uint16_t hm = h[l/2] >> im;
  1183. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1184. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1185. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1186. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1187. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1188. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1189. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1190. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1191. }
  1192. }
  1193. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1194. const float tot = simd_sum(sumf);
  1195. if (tiisg == 0) {
  1196. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1197. }
  1198. }
  1199. #endif
  1200. #if QK_K == 256
  1201. kernel void kernel_mul_mat_q4_K_f32(
  1202. device const void * src0,
  1203. device const float * src1,
  1204. device float * dst,
  1205. constant int64_t & ne00,
  1206. constant int64_t & ne01[[buffer(4)]],
  1207. constant int64_t & ne02[[buffer(5)]],
  1208. constant int64_t & ne10[[buffer(9)]],
  1209. constant int64_t & ne12[[buffer(11)]],
  1210. constant int64_t & ne0[[buffer(15)]],
  1211. constant int64_t & ne1[[buffer(16)]],
  1212. constant uint & gqa[[buffer(17)]],
  1213. uint3 tgpig[[threadgroup_position_in_grid]],
  1214. uint tiisg[[thread_index_in_simdgroup]],
  1215. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1216. const uint16_t kmask1 = 0x3f3f;
  1217. const uint16_t kmask2 = 0x0f0f;
  1218. const uint16_t kmask3 = 0xc0c0;
  1219. const int ix = tiisg/8; // 0...3
  1220. const int it = tiisg%8; // 0...7
  1221. const int im = it/4; // 0 or 1
  1222. const int ir = it%4; // 0...3
  1223. const int nb = ne00/QK_K;
  1224. const int r0 = tgpig.x;
  1225. const int r1 = tgpig.y;
  1226. const int r2 = tgpig.z;
  1227. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1228. const int first_row = r0 * N_DST;
  1229. const int ib_row = first_row * nb;
  1230. const uint offset0 = r2/gqa*(nb*ne0);
  1231. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1232. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1233. float yl[16];
  1234. float yh[16];
  1235. float sumf[N_DST]={0.f}, all_sum;
  1236. const int step = sizeof(block_q4_K) * nb / 2;
  1237. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1238. uint16_t sc16[4];
  1239. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1240. for (int ib = ix; ib < nb; ib += 4) {
  1241. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1242. for (int i = 0; i < 8; ++i) {
  1243. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1244. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1245. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1246. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1247. }
  1248. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1249. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1250. device const half * dh = &x[ib].d;
  1251. for (int row = 0; row < N_DST; row++) {
  1252. sc16[0] = sc[0] & kmask1;
  1253. sc16[1] = sc[2] & kmask1;
  1254. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1255. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1256. device const uint16_t * q2 = q1 + 32;
  1257. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1258. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1259. for (int i = 0; i < 8; i += 2) {
  1260. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1261. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1262. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1263. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1264. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1265. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1266. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1267. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1268. }
  1269. float dall = dh[0];
  1270. float dmin = dh[1];
  1271. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1272. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1273. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1274. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1275. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1276. q1 += step;
  1277. sc += step;
  1278. dh += step;
  1279. }
  1280. y4 += 4 * QK_K;
  1281. }
  1282. for (int row = 0; row < N_DST; ++row) {
  1283. all_sum = simd_sum(sumf[row]);
  1284. if (tiisg == 0) {
  1285. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1286. }
  1287. }
  1288. }
  1289. #else
  1290. kernel void kernel_mul_mat_q4_K_f32(
  1291. device const void * src0,
  1292. device const float * src1,
  1293. device float * dst,
  1294. constant int64_t & ne00,
  1295. constant int64_t & ne01[[buffer(4)]],
  1296. constant int64_t & ne02[[buffer(5)]],
  1297. constant int64_t & ne10[[buffer(9)]],
  1298. constant int64_t & ne12[[buffer(11)]],
  1299. constant int64_t & ne0[[buffer(15)]],
  1300. constant int64_t & ne1[[buffer(16)]],
  1301. constant uint & gqa[[buffer(17)]],
  1302. uint3 tgpig[[threadgroup_position_in_grid]],
  1303. uint tiisg[[thread_index_in_simdgroup]],
  1304. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1305. const int ix = tiisg/4; // 0...7
  1306. const int it = tiisg%4; // 0...3
  1307. const int nb = ne00/QK_K;
  1308. const int r0 = tgpig.x;
  1309. const int r1 = tgpig.y;
  1310. const int r2 = tgpig.z;
  1311. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1312. const int ib_row = first_row * nb;
  1313. const uint offset0 = r2/gqa*(nb*ne0);
  1314. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1315. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1316. float yl[8];
  1317. float yh[8];
  1318. float sumf[N_DST]={0.f}, all_sum;
  1319. const int step = sizeof(block_q4_K) * nb / 2;
  1320. device const float * y4 = y + ix * QK_K + 8 * it;
  1321. uint16_t sc16[4];
  1322. for (int ib = ix; ib < nb; ib += 8) {
  1323. float2 sumy = {0.f, 0.f};
  1324. for (int i = 0; i < 8; ++i) {
  1325. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1326. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1327. }
  1328. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1329. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1330. device const half * dh = x[ib].d;
  1331. for (int row = 0; row < N_DST; row++) {
  1332. sc16[0] = sc[0] & 0x000f;
  1333. sc16[1] = sc[0] & 0x0f00;
  1334. sc16[2] = sc[0] & 0x00f0;
  1335. sc16[3] = sc[0] & 0xf000;
  1336. float2 acc1 = {0.f, 0.f};
  1337. float2 acc2 = {0.f, 0.f};
  1338. for (int i = 0; i < 8; i += 2) {
  1339. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1340. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1341. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1342. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1343. }
  1344. float dall = dh[0];
  1345. float dmin = dh[1];
  1346. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1347. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1348. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1349. qs += step;
  1350. sc += step;
  1351. dh += step;
  1352. }
  1353. y4 += 8 * QK_K;
  1354. }
  1355. for (int row = 0; row < N_DST; ++row) {
  1356. all_sum = simd_sum(sumf[row]);
  1357. if (tiisg == 0) {
  1358. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1359. }
  1360. }
  1361. }
  1362. #endif
  1363. kernel void kernel_mul_mat_q5_K_f32(
  1364. device const void * src0,
  1365. device const float * src1,
  1366. device float * dst,
  1367. constant int64_t & ne00,
  1368. constant int64_t & ne01[[buffer(4)]],
  1369. constant int64_t & ne02[[buffer(5)]],
  1370. constant int64_t & ne10[[buffer(9)]],
  1371. constant int64_t & ne12[[buffer(11)]],
  1372. constant int64_t & ne0[[buffer(15)]],
  1373. constant int64_t & ne1[[buffer(16)]],
  1374. constant uint & gqa[[buffer(17)]],
  1375. uint3 tgpig[[threadgroup_position_in_grid]],
  1376. uint tiisg[[thread_index_in_simdgroup]],
  1377. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1378. const int nb = ne00/QK_K;
  1379. const int64_t r0 = tgpig.x;
  1380. const int64_t r1 = tgpig.y;
  1381. const int r2 = tgpig.z;
  1382. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1383. const uint offset0 = r2/gqa*(nb*ne0);
  1384. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1385. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1386. float sumf[2]={0.f};
  1387. const int step = sizeof(block_q5_K) * nb;
  1388. #if QK_K == 256
  1389. #
  1390. float yl[16], yh[16];
  1391. const uint16_t kmask1 = 0x3f3f;
  1392. const uint16_t kmask2 = 0x0f0f;
  1393. const uint16_t kmask3 = 0xc0c0;
  1394. const int tid = tiisg/4;
  1395. const int ix = tiisg%4;
  1396. const int im = tid/4;
  1397. const int ir = tid%4;
  1398. const int n = 8;
  1399. const int l0 = n*ir;
  1400. const int q_offset = 32*im + l0;
  1401. const int y_offset = 64*im + l0;
  1402. const uint8_t hm1 = 1u << (2*im);
  1403. const uint8_t hm2 = hm1 << 1;
  1404. const uint8_t hm3 = hm1 << 4;
  1405. const uint8_t hm4 = hm2 << 4;
  1406. uint16_t sc16[4];
  1407. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1408. device const float * y1 = yy + ix*QK_K + y_offset;
  1409. for (int i = ix; i < nb; i += 4) {
  1410. device const uint8_t * q1 = x[i].qs + q_offset;
  1411. device const uint8_t * qh = x[i].qh + l0;
  1412. device const half * dh = &x[i].d;
  1413. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1414. device const float * y2 = y1 + 128;
  1415. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1416. for (int l = 0; l < 8; ++l) {
  1417. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1418. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1419. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1420. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1421. }
  1422. for (int row = 0; row < 2; ++row) {
  1423. device const uint8_t * q2 = q1 + 64;
  1424. sc16[0] = a[0] & kmask1;
  1425. sc16[1] = a[2] & kmask1;
  1426. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1427. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1428. float4 acc1 = {0.f};
  1429. float4 acc2 = {0.f};
  1430. for (int l = 0; l < n; ++l) {
  1431. uint8_t h = qh[l];
  1432. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  1433. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  1434. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  1435. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  1436. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  1437. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  1438. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  1439. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  1440. }
  1441. const float dall = dh[0];
  1442. const float dmin = dh[1];
  1443. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  1444. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  1445. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  1446. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  1447. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1448. q1 += step;
  1449. qh += step;
  1450. dh += step/2;
  1451. a += step/2;
  1452. }
  1453. y1 += 4 * QK_K;
  1454. }
  1455. #else
  1456. float yl[8], yh[8];
  1457. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1458. const int ix = tiisg%8;
  1459. const int im = il/8; // 0, 0, 1, 1
  1460. const int in = il%8; // 0, 4, 0, 4
  1461. device const float * y = yy + ix*QK_K + il;
  1462. for (int i = ix; i < nb; i += 8) {
  1463. for (int l = 0; l < 4; ++l) {
  1464. yl[l+0] = y[l+ 0];
  1465. yl[l+4] = y[l+16];
  1466. yh[l+0] = y[l+32];
  1467. yh[l+4] = y[l+48];
  1468. }
  1469. device const half * dh = &x[i].d;
  1470. device const uint8_t * q = x[i].qs + il;
  1471. device const uint8_t * h = x[i].qh + in;
  1472. device const int8_t * s = x[i].scales;
  1473. for (int row = 0; row < 2; ++row) {
  1474. const float d = dh[0];
  1475. float2 acc = {0.f, 0.f};
  1476. for (int l = 0; l < 4; ++l) {
  1477. const uint8_t hl = h[l] >> im;
  1478. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1479. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1480. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1481. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1482. }
  1483. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1484. q += step;
  1485. h += step;
  1486. s += step;
  1487. dh += step/2;
  1488. }
  1489. y += 8 * QK_K;
  1490. }
  1491. #endif
  1492. for (int row = 0; row < 2; ++row) {
  1493. const float tot = simd_sum(sumf[row]);
  1494. if (tiisg == 0) {
  1495. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1496. }
  1497. }
  1498. }
  1499. kernel void kernel_mul_mat_q6_K_f32(
  1500. device const void * src0,
  1501. device const float * src1,
  1502. device float * dst,
  1503. constant int64_t & ne00,
  1504. constant int64_t & ne01[[buffer(4)]],
  1505. constant int64_t & ne02[[buffer(5)]],
  1506. constant int64_t & ne10[[buffer(9)]],
  1507. constant int64_t & ne12[[buffer(11)]],
  1508. constant int64_t & ne0[[buffer(15)]],
  1509. constant int64_t & ne1[[buffer(16)]],
  1510. constant uint & gqa[[buffer(17)]],
  1511. uint3 tgpig[[threadgroup_position_in_grid]],
  1512. uint tiisg[[thread_index_in_simdgroup]],
  1513. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1514. const uint8_t kmask1 = 0x03;
  1515. const uint8_t kmask2 = 0x0C;
  1516. const uint8_t kmask3 = 0x30;
  1517. const uint8_t kmask4 = 0xC0;
  1518. const int nb = ne00/QK_K;
  1519. const int64_t r0 = tgpig.x;
  1520. const int64_t r1 = tgpig.y;
  1521. const int r2 = tgpig.z;
  1522. const int row = 2 * r0 + sgitg;
  1523. const uint offset0 = r2/gqa*(nb*ne0);
  1524. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1525. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1526. float sumf = 0;
  1527. #if QK_K == 256
  1528. const int tid = tiisg/2;
  1529. const int ix = tiisg%2;
  1530. const int ip = tid/8; // 0 or 1
  1531. const int il = tid%8;
  1532. const int n = 4;
  1533. const int l0 = n*il;
  1534. const int is = 8*ip + l0/16;
  1535. const int y_offset = 128*ip + l0;
  1536. const int q_offset_l = 64*ip + l0;
  1537. const int q_offset_h = 32*ip + l0;
  1538. for (int i = ix; i < nb; i += 2) {
  1539. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1540. device const uint8_t * q2 = q1 + 32;
  1541. device const uint8_t * qh = x[i].qh + q_offset_h;
  1542. device const int8_t * sc = x[i].scales + is;
  1543. device const float * y = yy + i * QK_K + y_offset;
  1544. const float dall = x[i].d;
  1545. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1546. for (int l = 0; l < n; ++l) {
  1547. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1548. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1549. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1550. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1551. }
  1552. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1553. }
  1554. #else
  1555. const int ix = tiisg/4;
  1556. const int il = 4*(tiisg%4);
  1557. for (int i = ix; i < nb; i += 8) {
  1558. device const float * y = yy + i * QK_K + il;
  1559. device const uint8_t * ql = x[i].ql + il;
  1560. device const uint8_t * qh = x[i].qh + il;
  1561. device const int8_t * s = x[i].scales;
  1562. const float d = x[i].d;
  1563. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1564. for (int l = 0; l < 4; ++l) {
  1565. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1566. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1567. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1568. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1569. }
  1570. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1571. }
  1572. #endif
  1573. const float tot = simd_sum(sumf);
  1574. if (tiisg == 0) {
  1575. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1576. }
  1577. }
  1578. //============================= templates and their specializations =============================
  1579. template <typename type4x4>
  1580. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1581. half4x4 temp = *(((device half4x4 *)src));
  1582. for (int i = 0; i < 16; i++){
  1583. reg[i/4][i%4] = temp[i/4][i%4];
  1584. }
  1585. }
  1586. template <typename type4x4>
  1587. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1588. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1589. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1590. const float d2 = d1 / 256.f;
  1591. const float md = -8.h * xb->d;
  1592. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1593. const ushort mask1 = mask0 << 8;
  1594. for (int i=0;i<8;i++) {
  1595. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  1596. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  1597. }
  1598. }
  1599. template <typename type4x4>
  1600. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1601. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1602. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1603. const float d2 = d1 / 256.f;
  1604. const float m = xb->m;
  1605. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1606. const ushort mask1 = mask0 << 8;
  1607. for (int i=0;i<8;i++) {
  1608. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  1609. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  1610. }
  1611. }
  1612. template <typename type4x4>
  1613. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1614. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1615. const half d = xb->d;
  1616. for (int i=0;i<16;i++) {
  1617. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1618. }
  1619. }
  1620. template <typename type4x4>
  1621. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1622. const half d = xb->d;
  1623. const half min = xb->dmin;
  1624. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1625. half dl, ml;
  1626. uint8_t sc = xb->scales[il];
  1627. #if QK_K == 256
  1628. q = q + 32*(il/8) + 16*(il&1);
  1629. il = (il/2)%4;
  1630. #endif
  1631. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1632. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1633. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1634. for (int i = 0; i < 16; ++i) {
  1635. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1636. }
  1637. }
  1638. template <typename type4x4>
  1639. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1640. const half d_all = xb->d;
  1641. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1642. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1643. device const int8_t * scales = (device const int8_t *)xb->scales;
  1644. #if QK_K == 256
  1645. q = q + 32 * (il/8) + 16 * (il&1);
  1646. h = h + 16 * (il&1);
  1647. uint8_t m = 1 << (il/2);
  1648. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1649. ((il/4)>0 ? 12 : 3);
  1650. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1651. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1652. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  1653. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1654. half dl = il<8 ? d_all * (dl_int - 32.h) : d_all * (dl_int / 16.h - 32.h);
  1655. const half ml = 4.h * dl;
  1656. il = (il/2) & 3;
  1657. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1658. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1659. dl *= coef;
  1660. for (int i = 0; i < 16; ++i) {
  1661. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  1662. }
  1663. #else
  1664. float kcoef = il&1 ? 1.f/16.f : 1.f;
  1665. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  1666. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  1667. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1668. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1669. uint8_t m = 1<<(il*2);
  1670. for (int i = 0; i < 16; ++i) {
  1671. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  1672. }
  1673. #endif
  1674. }
  1675. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  1676. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  1677. : uchar2{uchar((q[j+4+k] & 0xF) | ((q[j-4+k] & 0xc0) >> 2)), uchar((q[j+4+k] >> 4) | ((q[j-0+k] & 0xc0) >> 2))};
  1678. }
  1679. template <typename type4x4>
  1680. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  1681. device const uchar * q = xb->qs;
  1682. #if QK_K == 256
  1683. short is = (il/4) * 2;
  1684. q = q + (il/4) * 32 + 16 * (il&1);
  1685. il = il & 3;
  1686. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  1687. const half d = il < 2 ? xb->d : xb->d / 16.h;
  1688. const half min = xb->dmin;
  1689. const half dl = d * sc[0];
  1690. const half ml = min * sc[1];
  1691. #else
  1692. q = q + 16 * (il&1);
  1693. device const uint8_t * s = xb->scales;
  1694. device const half2 * dh = (device const half2 *)xb->d;
  1695. const float2 d = (float2)dh[0];
  1696. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  1697. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  1698. #endif
  1699. const ushort mask = il<2 ? 0x0F : 0xF0;
  1700. for (int i = 0; i < 16; ++i) {
  1701. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1702. }
  1703. }
  1704. template <typename type4x4>
  1705. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  1706. device const uint8_t * q = xb->qs;
  1707. device const uint8_t * qh = xb->qh;
  1708. #if QK_K == 256
  1709. short is = (il/4) * 2;
  1710. q = q + 32 * (il/4) + 16 * (il&1);
  1711. qh = qh + 16 * (il&1);
  1712. uint8_t ul = 1 << (il/2);
  1713. il = il & 3;
  1714. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  1715. const half d = il < 2 ? xb->d : xb->d / 16.h;
  1716. const half min = xb->dmin;
  1717. const half dl = d * sc[0];
  1718. const half ml = min * sc[1];
  1719. const ushort mask = il<2 ? 0x0F : 0xF0;
  1720. const half qh_val = il<2 ? 16.h : 256.h;
  1721. for (int i = 0; i < 16; ++i) {
  1722. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  1723. }
  1724. #else
  1725. q = q + 16 * (il&1);
  1726. device const int8_t * s = xb->scales;
  1727. const float dl = xb->d * s[il];
  1728. uint8_t m = 1<<(il*2);
  1729. const float coef = il<2 ? 1.f : 1.f/16.f;
  1730. const ushort mask = il<2 ? 0x0F : 0xF0;
  1731. for (int i = 0; i < 16; ++i) {
  1732. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  1733. }
  1734. #endif
  1735. }
  1736. template <typename type4x4>
  1737. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  1738. const half d_all = xb->d;
  1739. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  1740. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  1741. device const int8_t * scales = (device const int8_t *)xb->scales;
  1742. #if QK_K == 256
  1743. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  1744. qh = qh + 32*(il/8) + 16*(il&1);
  1745. half sc = scales[(il%2) + 2 * ((il/2))];
  1746. il = (il/2) & 3;
  1747. #else
  1748. ql = ql + 16 * (il&1);
  1749. half sc = scales[il];
  1750. #endif
  1751. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1752. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  1753. const half coef = il>1 ? 1.f/16.h : 1.h;
  1754. const half ml = d_all * sc * 32.h;
  1755. const half dl = d_all * sc * coef;
  1756. for (int i = 0; i < 16; ++i) {
  1757. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  1758. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  1759. reg[i/4][i%4] = dl * q - ml;
  1760. }
  1761. }
  1762. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  1763. kernel void kernel_get_rows(
  1764. device const void * src0,
  1765. device const int * src1,
  1766. device float * dst,
  1767. constant int64_t & ne00,
  1768. constant uint64_t & nb01,
  1769. constant uint64_t & nb1,
  1770. uint tgpig[[threadgroup_position_in_grid]],
  1771. uint tiitg[[thread_index_in_threadgroup]],
  1772. uint tptg[[threads_per_threadgroup]]) {
  1773. const int i = tgpig;
  1774. const int r = ((device int32_t *) src1)[i];
  1775. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  1776. float4x4 temp;
  1777. dequantize_func(
  1778. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  1779. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  1780. }
  1781. }
  1782. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  1783. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix A
  1784. #define BLOCK_SIZE_K 32
  1785. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  1786. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  1787. #define THREAD_PER_BLOCK 128
  1788. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  1789. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  1790. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  1791. #define SG_MAT_ROW 8
  1792. // each block_q contains 16*nl weights
  1793. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  1794. kernel void kernel_mul_mm(device const uchar * src0,
  1795. device const float * src1,
  1796. device float * dst,
  1797. constant int64_t & ne00,
  1798. constant int64_t & ne02,
  1799. constant int64_t & nb01,
  1800. constant int64_t & nb02,
  1801. constant int64_t & ne12,
  1802. constant int64_t & ne0,
  1803. constant int64_t & ne1,
  1804. constant uint & gqa,
  1805. threadgroup uchar * shared_memory [[threadgroup(0)]],
  1806. uint3 tgpig[[threadgroup_position_in_grid]],
  1807. uint tiitg[[thread_index_in_threadgroup]],
  1808. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1809. threadgroup half * sa = ((threadgroup half *)shared_memory);
  1810. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  1811. const uint r0 = tgpig.y;
  1812. const uint r1 = tgpig.x;
  1813. const uint im = tgpig.z;
  1814. // if this block is of 64x32 shape or smaller
  1815. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  1816. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  1817. // a thread shouldn't load data outside of the matrix
  1818. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  1819. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  1820. simdgroup_half8x8 ma[4];
  1821. simdgroup_float8x8 mb[2];
  1822. simdgroup_float8x8 c_res[8];
  1823. for (int i = 0; i < 8; i++){
  1824. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  1825. }
  1826. short il = (tiitg % THREAD_PER_ROW);
  1827. uint offset0 = im/gqa*nb02; ushort offset1 = il/nl;
  1828. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  1829. device const float * y = src1 + (r1 * BLOCK_SIZE_N + thread_col) * ne00 \
  1830. + BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL) + im * ne00 * ne1;
  1831. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  1832. //load data and store to threadgroup memory
  1833. half4x4 temp_a;
  1834. dequantize_func(x, il, temp_a);
  1835. threadgroup_barrier(mem_flags::mem_threadgroup);
  1836. #pragma unroll(16)
  1837. for (int i = 0; i < 16; i++) {
  1838. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  1839. + 16 * (tiitg % THREAD_PER_ROW) + 8 * (i / 8)) \
  1840. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  1841. }
  1842. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) \
  1843. = *((device float2x4 *)y);
  1844. il = (il + 2 < nl) ? il + 2 : il % 2;
  1845. x = (il < 2) ? x + (2+nl-1)/nl : x;
  1846. y += BLOCK_SIZE_K;
  1847. threadgroup_barrier(mem_flags::mem_threadgroup);
  1848. //load matrices from threadgroup memory and conduct outer products
  1849. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  1850. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  1851. #pragma unroll(4)
  1852. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  1853. #pragma unroll(4)
  1854. for (int i = 0; i < 4; i++) {
  1855. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  1856. }
  1857. simdgroup_barrier(mem_flags::mem_none);
  1858. #pragma unroll(2)
  1859. for (int i = 0; i < 2; i++) {
  1860. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  1861. }
  1862. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  1863. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  1864. #pragma unroll(8)
  1865. for (int i = 0; i < 8; i++){
  1866. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  1867. }
  1868. }
  1869. }
  1870. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  1871. device float *C = dst + BLOCK_SIZE_M * r0 + 32 * (sgitg&1) \
  1872. + (BLOCK_SIZE_N * r1 + 16 * (sgitg>>1)) * ne0 + im*ne1*ne0;
  1873. for (int i = 0; i < 8; i++) {
  1874. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  1875. }
  1876. } else {
  1877. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  1878. threadgroup_barrier(mem_flags::mem_threadgroup);
  1879. threadgroup float *temp_str = ((threadgroup float *)shared_memory) \
  1880. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  1881. for (int i = 0; i < 8; i++) {
  1882. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  1883. }
  1884. threadgroup_barrier(mem_flags::mem_threadgroup);
  1885. device float *C = dst + BLOCK_SIZE_M * r0 + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  1886. if (sgitg==0) {
  1887. for (int i = 0; i < n_rows; i++) {
  1888. for (int j = tiitg; j< n_cols; j += BLOCK_SIZE_N) {
  1889. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  1890. }
  1891. }
  1892. }
  1893. }
  1894. }
  1895. #if QK_K == 256
  1896. #define QK_NL 16
  1897. #else
  1898. #define QK_NL 4
  1899. #endif
  1900. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  1901. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  1902. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  1903. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  1904. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  1905. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  1906. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  1907. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  1908. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  1909. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  1910. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  1911. typedef void (mat_mm_t)(device const uchar *, device const float *, device float *, constant int64_t &,\
  1912. constant int64_t &, constant int64_t &, constant int64_t &, constant int64_t &, \
  1913. constant int64_t &, constant int64_t &, constant uint &, threadgroup uchar *, uint3, uint, uint);
  1914. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  1915. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  1916. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  1917. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  1918. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  1919. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  1920. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  1921. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  1922. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;