ggml-metal.metal 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  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 = tpitg[0] < ne00 ? psrc0[tpitg[0]] : -INFINITY;
  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 = tpitg[0] < ne00/4 ? psrc4[tpitg[0]] : -INFINITY;
  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. #define N_F32_F32 4
  462. kernel void kernel_mul_mat_f32_f32(
  463. device const char * src0,
  464. device const char * src1,
  465. device float * dst,
  466. constant int64_t & ne00,
  467. constant int64_t & ne01,
  468. constant int64_t & ne02,
  469. constant uint64_t & nb00,
  470. constant uint64_t & nb01,
  471. constant uint64_t & nb02,
  472. constant int64_t & ne10,
  473. constant int64_t & ne11,
  474. constant int64_t & ne12,
  475. constant uint64_t & nb10,
  476. constant uint64_t & nb11,
  477. constant uint64_t & nb12,
  478. constant int64_t & ne0,
  479. constant int64_t & ne1,
  480. uint3 tgpig[[threadgroup_position_in_grid]],
  481. uint tiisg[[thread_index_in_simdgroup]]) {
  482. const int64_t r0 = tgpig.x;
  483. const int64_t rb = tgpig.y*N_F32_F32;
  484. const int64_t im = tgpig.z;
  485. device const float * x = (device const float *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  486. if (ne00 < 128) {
  487. for (int row = 0; row < N_F32_F32; ++row) {
  488. int r1 = rb + row;
  489. if (r1 >= ne11) {
  490. break;
  491. }
  492. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  493. float sumf = 0;
  494. for (int i = tiisg; i < ne00; i += 32) {
  495. sumf += (float) x[i] * (float) y[i];
  496. }
  497. float all_sum = simd_sum(sumf);
  498. if (tiisg == 0) {
  499. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  500. }
  501. }
  502. } else {
  503. device const float4 * x4 = (device const float4 *)x;
  504. for (int row = 0; row < N_F32_F32; ++row) {
  505. int r1 = rb + row;
  506. if (r1 >= ne11) {
  507. break;
  508. }
  509. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  510. device const float4 * y4 = (device const float4 *) y;
  511. float sumf = 0;
  512. for (int i = tiisg; i < ne00/4; i += 32) {
  513. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  514. }
  515. float all_sum = simd_sum(sumf);
  516. if (tiisg == 0) {
  517. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  518. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  519. }
  520. }
  521. }
  522. }
  523. kernel void kernel_mul_mat_f16_f32_1row(
  524. device const char * src0,
  525. device const char * src1,
  526. device float * dst,
  527. constant int64_t & ne00,
  528. constant int64_t & ne01,
  529. constant int64_t & ne02,
  530. constant uint64_t & nb00,
  531. constant uint64_t & nb01,
  532. constant uint64_t & nb02,
  533. constant int64_t & ne10,
  534. constant int64_t & ne11,
  535. constant int64_t & ne12,
  536. constant uint64_t & nb10,
  537. constant uint64_t & nb11,
  538. constant uint64_t & nb12,
  539. constant int64_t & ne0,
  540. constant int64_t & ne1,
  541. uint3 tgpig[[threadgroup_position_in_grid]],
  542. uint tiisg[[thread_index_in_simdgroup]]) {
  543. const int64_t r0 = tgpig.x;
  544. const int64_t r1 = tgpig.y;
  545. const int64_t im = tgpig.z;
  546. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  547. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  548. float sumf = 0;
  549. if (ne00 < 128) {
  550. for (int i = tiisg; i < ne00; i += 32) {
  551. sumf += (float) x[i] * (float) y[i];
  552. }
  553. float all_sum = simd_sum(sumf);
  554. if (tiisg == 0) {
  555. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  556. }
  557. } else {
  558. device const half4 * x4 = (device const half4 *) x;
  559. device const float4 * y4 = (device const float4 *) y;
  560. for (int i = tiisg; i < ne00/4; i += 32) {
  561. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  562. }
  563. float all_sum = simd_sum(sumf);
  564. if (tiisg == 0) {
  565. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  566. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  567. }
  568. }
  569. }
  570. #define N_F16_F32 4
  571. kernel void kernel_mul_mat_f16_f32(
  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 int64_t r0 = tgpig.x;
  592. const int64_t rb = tgpig.y*N_F16_F32;
  593. const int64_t im = tgpig.z;
  594. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  595. if (ne00 < 128) {
  596. for (int row = 0; row < N_F16_F32; ++row) {
  597. int r1 = rb + row;
  598. if (r1 >= ne11) {
  599. break;
  600. }
  601. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  602. float sumf = 0;
  603. for (int i = tiisg; i < ne00; i += 32) {
  604. sumf += (float) x[i] * (float) y[i];
  605. }
  606. float all_sum = simd_sum(sumf);
  607. if (tiisg == 0) {
  608. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  609. }
  610. }
  611. } else {
  612. device const half4 * x4 = (device const half4 *)x;
  613. for (int row = 0; row < N_F16_F32; ++row) {
  614. int r1 = rb + row;
  615. if (r1 >= ne11) {
  616. break;
  617. }
  618. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  619. device const float4 * y4 = (device const float4 *) y;
  620. float sumf = 0;
  621. for (int i = tiisg; i < ne00/4; i += 32) {
  622. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  623. }
  624. float all_sum = simd_sum(sumf);
  625. if (tiisg == 0) {
  626. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  627. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  628. }
  629. }
  630. }
  631. }
  632. // Assumes row size (ne00) is a multiple of 4
  633. kernel void kernel_mul_mat_f16_f32_l4(
  634. device const char * src0,
  635. device const char * src1,
  636. device float * dst,
  637. constant int64_t & ne00,
  638. constant int64_t & ne01,
  639. constant int64_t & ne02,
  640. constant uint64_t & nb00,
  641. constant uint64_t & nb01,
  642. constant uint64_t & nb02,
  643. constant int64_t & ne10,
  644. constant int64_t & ne11,
  645. constant int64_t & ne12,
  646. constant uint64_t & nb10,
  647. constant uint64_t & nb11,
  648. constant uint64_t & nb12,
  649. constant int64_t & ne0,
  650. constant int64_t & ne1,
  651. uint3 tgpig[[threadgroup_position_in_grid]],
  652. uint tiisg[[thread_index_in_simdgroup]]) {
  653. const int nrows = ne11;
  654. const int64_t r0 = tgpig.x;
  655. const int64_t im = tgpig.z;
  656. device const half4 * x4 = (device const half4 *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  657. for (int r1 = 0; r1 < nrows; ++r1) {
  658. device const float4 * y4 = (device const float4 *) (src1 + r1*nb11 + im*nb12);
  659. float sumf = 0;
  660. for (int i = tiisg; i < ne00/4; i += 32) {
  661. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  662. }
  663. float all_sum = simd_sum(sumf);
  664. if (tiisg == 0) {
  665. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  666. }
  667. }
  668. }
  669. kernel void kernel_alibi_f32(
  670. device const float * src0,
  671. device float * dst,
  672. constant int64_t & ne00,
  673. constant int64_t & ne01,
  674. constant int64_t & ne02,
  675. constant int64_t & ne03,
  676. constant uint64_t & nb00,
  677. constant uint64_t & nb01,
  678. constant uint64_t & nb02,
  679. constant uint64_t & nb03,
  680. constant int64_t & ne0,
  681. constant int64_t & ne1,
  682. constant int64_t & ne2,
  683. constant int64_t & ne3,
  684. constant uint64_t & nb0,
  685. constant uint64_t & nb1,
  686. constant uint64_t & nb2,
  687. constant uint64_t & nb3,
  688. constant float & m0,
  689. uint3 tgpig[[threadgroup_position_in_grid]],
  690. uint3 tpitg[[thread_position_in_threadgroup]],
  691. uint3 ntg[[threads_per_threadgroup]]) {
  692. const int64_t i03 = tgpig[2];
  693. const int64_t i02 = tgpig[1];
  694. const int64_t i01 = tgpig[0];
  695. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  696. const int64_t i3 = n / (ne2*ne1*ne0);
  697. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  698. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  699. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  700. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  701. float m_k = pow(m0, i2 + 1);
  702. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  703. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  704. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  705. }
  706. }
  707. kernel void kernel_rope(
  708. device const void * src0,
  709. device float * dst,
  710. constant int64_t & ne00,
  711. constant int64_t & ne01,
  712. constant int64_t & ne02,
  713. constant int64_t & ne03,
  714. constant uint64_t & nb00,
  715. constant uint64_t & nb01,
  716. constant uint64_t & nb02,
  717. constant uint64_t & nb03,
  718. constant int64_t & ne0,
  719. constant int64_t & ne1,
  720. constant int64_t & ne2,
  721. constant int64_t & ne3,
  722. constant uint64_t & nb0,
  723. constant uint64_t & nb1,
  724. constant uint64_t & nb2,
  725. constant uint64_t & nb3,
  726. constant int & n_past,
  727. constant int & n_dims,
  728. constant int & mode,
  729. constant float & freq_base,
  730. constant float & freq_scale,
  731. uint tiitg[[thread_index_in_threadgroup]],
  732. uint3 tptg[[threads_per_threadgroup]],
  733. uint3 tgpig[[threadgroup_position_in_grid]]) {
  734. const int64_t i3 = tgpig[2];
  735. const int64_t i2 = tgpig[1];
  736. const int64_t i1 = tgpig[0];
  737. const bool is_neox = mode & 2;
  738. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  739. const float theta_0 = freq_scale * (float)p;
  740. const float inv_ndims = -1.f/n_dims;
  741. if (!is_neox) {
  742. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  743. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  744. const float cos_theta = cos(theta);
  745. const float sin_theta = sin(theta);
  746. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  747. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  748. const float x0 = src[0];
  749. const float x1 = src[1];
  750. dst_data[0] = x0*cos_theta - x1*sin_theta;
  751. dst_data[1] = x0*sin_theta + x1*cos_theta;
  752. }
  753. } else {
  754. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  755. for (int64_t ic = 2*tiitg; ic < n_dims; ic += 2*tptg.x) {
  756. const float theta = theta_0 * pow(freq_base, inv_ndims*ic - ib);
  757. const float cos_theta = cos(theta);
  758. const float sin_theta = sin(theta);
  759. const int64_t i0 = ib*n_dims + ic/2;
  760. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  761. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  762. const float x0 = src[0];
  763. const float x1 = src[n_dims/2];
  764. dst_data[0] = x0*cos_theta - x1*sin_theta;
  765. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  766. }
  767. }
  768. }
  769. }
  770. kernel void kernel_cpy_f16_f16(
  771. device const half * src0,
  772. device half * dst,
  773. constant int64_t & ne00,
  774. constant int64_t & ne01,
  775. constant int64_t & ne02,
  776. constant int64_t & ne03,
  777. constant uint64_t & nb00,
  778. constant uint64_t & nb01,
  779. constant uint64_t & nb02,
  780. constant uint64_t & nb03,
  781. constant int64_t & ne0,
  782. constant int64_t & ne1,
  783. constant int64_t & ne2,
  784. constant int64_t & ne3,
  785. constant uint64_t & nb0,
  786. constant uint64_t & nb1,
  787. constant uint64_t & nb2,
  788. constant uint64_t & nb3,
  789. uint3 tgpig[[threadgroup_position_in_grid]],
  790. uint3 tpitg[[thread_position_in_threadgroup]],
  791. uint3 ntg[[threads_per_threadgroup]]) {
  792. const int64_t i03 = tgpig[2];
  793. const int64_t i02 = tgpig[1];
  794. const int64_t i01 = tgpig[0];
  795. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  796. const int64_t i3 = n / (ne2*ne1*ne0);
  797. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  798. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  799. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  800. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  801. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  802. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  803. dst_data[i00] = src[0];
  804. }
  805. }
  806. kernel void kernel_cpy_f32_f16(
  807. device const float * src0,
  808. device half * dst,
  809. constant int64_t & ne00,
  810. constant int64_t & ne01,
  811. constant int64_t & ne02,
  812. constant int64_t & ne03,
  813. constant uint64_t & nb00,
  814. constant uint64_t & nb01,
  815. constant uint64_t & nb02,
  816. constant uint64_t & nb03,
  817. constant int64_t & ne0,
  818. constant int64_t & ne1,
  819. constant int64_t & ne2,
  820. constant int64_t & ne3,
  821. constant uint64_t & nb0,
  822. constant uint64_t & nb1,
  823. constant uint64_t & nb2,
  824. constant uint64_t & nb3,
  825. uint3 tgpig[[threadgroup_position_in_grid]],
  826. uint3 tpitg[[thread_position_in_threadgroup]],
  827. uint3 ntg[[threads_per_threadgroup]]) {
  828. const int64_t i03 = tgpig[2];
  829. const int64_t i02 = tgpig[1];
  830. const int64_t i01 = tgpig[0];
  831. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  832. const int64_t i3 = n / (ne2*ne1*ne0);
  833. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  834. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  835. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  836. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  837. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  838. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  839. dst_data[i00] = src[0];
  840. }
  841. }
  842. kernel void kernel_cpy_f32_f32(
  843. device const float * src0,
  844. device float * dst,
  845. constant int64_t & ne00,
  846. constant int64_t & ne01,
  847. constant int64_t & ne02,
  848. constant int64_t & ne03,
  849. constant uint64_t & nb00,
  850. constant uint64_t & nb01,
  851. constant uint64_t & nb02,
  852. constant uint64_t & nb03,
  853. constant int64_t & ne0,
  854. constant int64_t & ne1,
  855. constant int64_t & ne2,
  856. constant int64_t & ne3,
  857. constant uint64_t & nb0,
  858. constant uint64_t & nb1,
  859. constant uint64_t & nb2,
  860. constant uint64_t & nb3,
  861. uint3 tgpig[[threadgroup_position_in_grid]],
  862. uint3 tpitg[[thread_position_in_threadgroup]],
  863. uint3 ntg[[threads_per_threadgroup]]) {
  864. const int64_t i03 = tgpig[2];
  865. const int64_t i02 = tgpig[1];
  866. const int64_t i01 = tgpig[0];
  867. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  868. const int64_t i3 = n / (ne2*ne1*ne0);
  869. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  870. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  871. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  872. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  873. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  874. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  875. dst_data[i00] = src[0];
  876. }
  877. }
  878. //============================================ k-quants ======================================================
  879. #ifndef QK_K
  880. #define QK_K 256
  881. #else
  882. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  883. #endif
  884. #if QK_K == 256
  885. #define K_SCALE_SIZE 12
  886. #else
  887. #define K_SCALE_SIZE 4
  888. #endif
  889. typedef struct {
  890. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  891. uint8_t qs[QK_K/4]; // quants
  892. half d; // super-block scale for quantized scales
  893. half dmin; // super-block scale for quantized mins
  894. } block_q2_K;
  895. // 84 bytes / block
  896. typedef struct {
  897. uint8_t hmask[QK_K/8]; // quants - high bit
  898. uint8_t qs[QK_K/4]; // quants - low 2 bits
  899. #if QK_K == 64
  900. uint8_t scales[2];
  901. #else
  902. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  903. #endif
  904. half d; // super-block scale
  905. } block_q3_K;
  906. #if QK_K == 64
  907. typedef struct {
  908. half d[2]; // super-block scales/mins
  909. uint8_t scales[2];
  910. uint8_t qs[QK_K/2]; // 4-bit quants
  911. } block_q4_K;
  912. #else
  913. typedef struct {
  914. half d; // super-block scale for quantized scales
  915. half dmin; // super-block scale for quantized mins
  916. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  917. uint8_t qs[QK_K/2]; // 4--bit quants
  918. } block_q4_K;
  919. #endif
  920. #if QK_K == 64
  921. typedef struct {
  922. half d; // super-block scales/mins
  923. int8_t scales[QK_K/16]; // 8-bit block scales
  924. uint8_t qh[QK_K/8]; // quants, high bit
  925. uint8_t qs[QK_K/2]; // quants, low 4 bits
  926. } block_q5_K;
  927. #else
  928. typedef struct {
  929. half d; // super-block scale for quantized scales
  930. half dmin; // super-block scale for quantized mins
  931. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  932. uint8_t qh[QK_K/8]; // quants, high bit
  933. uint8_t qs[QK_K/2]; // quants, low 4 bits
  934. } block_q5_K;
  935. // 176 bytes / block
  936. #endif
  937. typedef struct {
  938. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  939. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  940. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  941. half d; // super-block scale
  942. } block_q6_K;
  943. // 210 bytes / block
  944. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  945. uchar4 r;
  946. if (j < 4) {
  947. r[0] = q[j+0] & 63;
  948. r[2] = q[j+1] & 63;
  949. r[1] = q[j+4] & 63;
  950. r[3] = q[j+5] & 63;
  951. } else {
  952. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  953. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  954. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  955. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  956. }
  957. return r;
  958. }
  959. //====================================== dot products =========================
  960. kernel void kernel_mul_mat_q2_K_f32(
  961. device const void * src0,
  962. device const float * src1,
  963. device float * dst,
  964. constant int64_t & ne00,
  965. constant int64_t & ne01[[buffer(4)]],
  966. constant int64_t & ne02[[buffer(5)]],
  967. constant int64_t & ne10[[buffer(9)]],
  968. constant int64_t & ne12[[buffer(11)]],
  969. constant int64_t & ne0[[buffer(15)]],
  970. constant int64_t & ne1[[buffer(16)]],
  971. constant uint & gqa[[buffer(17)]],
  972. uint3 tgpig[[threadgroup_position_in_grid]],
  973. uint tiisg[[thread_index_in_simdgroup]],
  974. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  975. const int nb = ne00/QK_K;
  976. const int r0 = tgpig.x;
  977. const int r1 = tgpig.y;
  978. const int r2 = tgpig.z;
  979. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  980. const int ib_row = first_row * nb;
  981. const uint offset0 = r2/gqa*(nb*ne0);
  982. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  983. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  984. float yl[32];
  985. float sumf[N_DST]={0.f}, all_sum;
  986. const int step = sizeof(block_q2_K) * nb;
  987. #if QK_K == 256
  988. const int ix = tiisg/8; // 0...3
  989. const int it = tiisg%8; // 0...7
  990. const int im = it/4; // 0 or 1
  991. const int ir = it%4; // 0...3
  992. const int is = (8*ir)/16;// 0 or 1
  993. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  994. for (int ib = ix; ib < nb; ib += 4) {
  995. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  996. for (int i = 0; i < 8; ++i) {
  997. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  998. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  999. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  1000. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  1001. }
  1002. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  1003. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1004. device const half * dh = &x[ib].d;
  1005. for (int row = 0; row < N_DST; row++) {
  1006. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1007. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1008. for (int i = 0; i < 8; i += 2) {
  1009. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1010. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1011. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1012. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1013. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1014. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1015. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1016. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1017. }
  1018. float dall = dh[0];
  1019. float dmin = dh[1] * 1.f/16.f;
  1020. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1021. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  1022. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  1023. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  1024. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  1025. qs += step/2;
  1026. sc += step;
  1027. dh += step/2;
  1028. }
  1029. y4 += 4 * QK_K;
  1030. }
  1031. #else
  1032. const int ix = tiisg/2; // 0...15
  1033. const int it = tiisg%2; // 0...1
  1034. device const float * y4 = y + ix * QK_K + 8 * it;
  1035. for (int ib = ix; ib < nb; ib += 16) {
  1036. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1037. for (int i = 0; i < 8; ++i) {
  1038. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1039. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  1040. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  1041. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  1042. }
  1043. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  1044. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1045. device const half * dh = &x[ib].d;
  1046. for (int row = 0; row < N_DST; row++) {
  1047. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1048. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1049. for (int i = 0; i < 8; i += 2) {
  1050. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1051. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1052. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1053. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1054. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1055. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1056. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1057. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1058. }
  1059. float dall = dh[0];
  1060. float dmin = dh[1];
  1061. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1062. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  1063. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  1064. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  1065. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  1066. qs += step/2;
  1067. sc += step;
  1068. dh += step/2;
  1069. }
  1070. y4 += 16 * QK_K;
  1071. }
  1072. #endif
  1073. for (int row = 0; row < N_DST; ++row) {
  1074. all_sum = simd_sum(sumf[row]);
  1075. if (tiisg == 0) {
  1076. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1077. }
  1078. }
  1079. }
  1080. #if QK_K == 256
  1081. kernel void kernel_mul_mat_q3_K_f32(
  1082. device const void * src0,
  1083. device const float * src1,
  1084. device float * dst,
  1085. constant int64_t & ne00,
  1086. constant int64_t & ne01[[buffer(4)]],
  1087. constant int64_t & ne02[[buffer(5)]],
  1088. constant int64_t & ne10[[buffer(9)]],
  1089. constant int64_t & ne12[[buffer(11)]],
  1090. constant int64_t & ne0[[buffer(15)]],
  1091. constant int64_t & ne1[[buffer(16)]],
  1092. constant uint & gqa[[buffer(17)]],
  1093. uint3 tgpig[[threadgroup_position_in_grid]],
  1094. uint tiisg[[thread_index_in_simdgroup]],
  1095. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1096. const int nb = ne00/QK_K;
  1097. const int64_t r0 = tgpig.x;
  1098. const int64_t r1 = tgpig.y;
  1099. const int64_t r2 = tgpig.z;
  1100. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1101. const uint offset0 = r2/gqa*(nb*ne0);
  1102. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  1103. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1104. float yl[32];
  1105. const uint16_t kmask1 = 0x3030;
  1106. const uint16_t kmask2 = 0x0f0f;
  1107. const int tid = tiisg/4;
  1108. const int ix = tiisg%4;
  1109. const int ip = tid/4; // 0 or 1
  1110. const int il = 2*((tid%4)/2); // 0 or 2
  1111. const int ir = tid%2;
  1112. const int n = 8;
  1113. const int l0 = n*ir;
  1114. // One would think that the Metal compiler would figure out that ip and il can only have
  1115. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  1116. // with these two tales.
  1117. //
  1118. // Possible masks for the high bit
  1119. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  1120. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  1121. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  1122. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  1123. // Possible masks for the low 2 bits
  1124. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  1125. const ushort4 hm = mm[2*ip + il/2];
  1126. const int shift = 2*il;
  1127. const float v1 = il == 0 ? 4.f : 64.f;
  1128. const float v2 = 4.f * v1;
  1129. const uint16_t s_shift1 = 4*ip;
  1130. const uint16_t s_shift2 = s_shift1 + il;
  1131. const int q_offset = 32*ip + l0;
  1132. const int y_offset = 128*ip + 32*il + l0;
  1133. const int step = sizeof(block_q3_K) * nb / 2;
  1134. device const float * y1 = yy + ix*QK_K + y_offset;
  1135. uint32_t scales32, aux32;
  1136. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  1137. thread const int8_t * scales = (thread const int8_t *)&scales32;
  1138. float sumf1[2] = {0.f};
  1139. float sumf2[2] = {0.f};
  1140. for (int i = ix; i < nb; i += 4) {
  1141. for (int l = 0; l < 8; ++l) {
  1142. yl[l+ 0] = y1[l+ 0];
  1143. yl[l+ 8] = y1[l+16];
  1144. yl[l+16] = y1[l+32];
  1145. yl[l+24] = y1[l+48];
  1146. }
  1147. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1148. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1149. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1150. device const half * dh = &x[i].d;
  1151. for (int row = 0; row < 2; ++row) {
  1152. const float d_all = (float)dh[0];
  1153. scales16[0] = a[4];
  1154. scales16[1] = a[5];
  1155. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  1156. scales16[0] = a[il+0];
  1157. scales16[1] = a[il+1];
  1158. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  1159. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  1160. for (int l = 0; l < n; l += 2) {
  1161. const int32_t qs = q[l/2];
  1162. s1 += yl[l+0] * (qs & qm[il/2][0]);
  1163. s2 += yl[l+1] * (qs & qm[il/2][1]);
  1164. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  1165. s4 += yl[l+16] * (qs & qm[il/2][2]);
  1166. s5 += yl[l+17] * (qs & qm[il/2][3]);
  1167. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  1168. }
  1169. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1170. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1171. sumf1[row] += d1 * (scales[0] - 32);
  1172. sumf2[row] += d2 * (scales[2] - 32);
  1173. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  1174. for (int l = 0; l < n; l += 2) {
  1175. const int32_t qs = q[l/2+8];
  1176. s1 += yl[l+8] * (qs & qm[il/2][0]);
  1177. s2 += yl[l+9] * (qs & qm[il/2][1]);
  1178. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  1179. s4 += yl[l+24] * (qs & qm[il/2][2]);
  1180. s5 += yl[l+25] * (qs & qm[il/2][3]);
  1181. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  1182. }
  1183. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1184. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1185. sumf1[row] += d1 * (scales[1] - 32);
  1186. sumf2[row] += d2 * (scales[3] - 32);
  1187. q += step;
  1188. h += step;
  1189. a += step;
  1190. dh += step;
  1191. }
  1192. y1 += 4 * QK_K;
  1193. }
  1194. for (int row = 0; row < 2; ++row) {
  1195. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  1196. sumf1[row] = simd_sum(sumf);
  1197. }
  1198. if (tiisg == 0) {
  1199. for (int row = 0; row < 2; ++row) {
  1200. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = sumf1[row];
  1201. }
  1202. }
  1203. }
  1204. #else
  1205. kernel void kernel_mul_mat_q3_K_f32(
  1206. device const void * src0,
  1207. device const float * src1,
  1208. device float * dst,
  1209. constant int64_t & ne00,
  1210. constant int64_t & ne01[[buffer(4)]],
  1211. constant int64_t & ne02[[buffer(5)]],
  1212. constant int64_t & ne10[[buffer(9)]],
  1213. constant int64_t & ne12[[buffer(11)]],
  1214. constant int64_t & ne0[[buffer(15)]],
  1215. constant int64_t & ne1[[buffer(16)]],
  1216. constant uint & gqa[[buffer(17)]],
  1217. uint3 tgpig[[threadgroup_position_in_grid]],
  1218. uint tiisg[[thread_index_in_simdgroup]],
  1219. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1220. const int nb = ne00/QK_K;
  1221. const int64_t r0 = tgpig.x;
  1222. const int64_t r1 = tgpig.y;
  1223. const int64_t r2 = tgpig.z;
  1224. const int row = 2 * r0 + sgitg;
  1225. const uint offset0 = r2/gqa*(nb*ne0);
  1226. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1227. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1228. const int ix = tiisg/4;
  1229. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1230. const int im = il/8; // 0, 0, 1, 1
  1231. const int in = il%8; // 0, 4, 0, 4
  1232. float2 sum = {0.f, 0.f};
  1233. for (int i = ix; i < nb; i += 8) {
  1234. const float d_all = (float)(x[i].d);
  1235. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1236. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1237. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1238. device const float * y = yy + i * QK_K + il;
  1239. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1240. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1241. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1242. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1243. for (int l = 0; l < 4; l += 2) {
  1244. const uint16_t hm = h[l/2] >> im;
  1245. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1246. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1247. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1248. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1249. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1250. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1251. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1252. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1253. }
  1254. }
  1255. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1256. const float tot = simd_sum(sumf);
  1257. if (tiisg == 0) {
  1258. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1259. }
  1260. }
  1261. #endif
  1262. #if QK_K == 256
  1263. kernel void kernel_mul_mat_q4_K_f32(
  1264. device const void * src0,
  1265. device const float * src1,
  1266. device float * dst,
  1267. constant int64_t & ne00,
  1268. constant int64_t & ne01 [[buffer(4)]],
  1269. constant int64_t & ne02 [[buffer(5)]],
  1270. constant int64_t & ne10 [[buffer(9)]],
  1271. constant int64_t & ne12 [[buffer(11)]],
  1272. constant int64_t & ne0 [[buffer(15)]],
  1273. constant int64_t & ne1 [[buffer(16)]],
  1274. constant uint & gqa [[buffer(17)]],
  1275. uint3 tgpig[[threadgroup_position_in_grid]],
  1276. uint tiisg[[thread_index_in_simdgroup]],
  1277. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1278. const uint16_t kmask1 = 0x3f3f;
  1279. const uint16_t kmask2 = 0x0f0f;
  1280. const uint16_t kmask3 = 0xc0c0;
  1281. const int ix = tiisg/8; // 0...3
  1282. const int it = tiisg%8; // 0...7
  1283. const int im = it/4; // 0 or 1
  1284. const int ir = it%4; // 0...3
  1285. const int nb = ne00/QK_K;
  1286. const int r0 = tgpig.x;
  1287. const int r1 = tgpig.y;
  1288. const int r2 = tgpig.z;
  1289. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1290. const int first_row = r0 * N_DST;
  1291. const int ib_row = first_row * nb;
  1292. const uint offset0 = r2/gqa*(nb*ne0);
  1293. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1294. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1295. float yl[16];
  1296. float yh[16];
  1297. float sumf[N_DST]={0.f}, all_sum;
  1298. const int step = sizeof(block_q4_K) * nb / 2;
  1299. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1300. uint16_t sc16[4];
  1301. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1302. for (int ib = ix; ib < nb; ib += 4) {
  1303. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1304. for (int i = 0; i < 8; ++i) {
  1305. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1306. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1307. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1308. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1309. }
  1310. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1311. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1312. device const half * dh = &x[ib].d;
  1313. for (int row = 0; row < N_DST; row++) {
  1314. sc16[0] = sc[0] & kmask1;
  1315. sc16[1] = sc[2] & kmask1;
  1316. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1317. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1318. device const uint16_t * q2 = q1 + 32;
  1319. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1320. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1321. for (int i = 0; i < 8; i += 2) {
  1322. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1323. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1324. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1325. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1326. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1327. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1328. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1329. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1330. }
  1331. float dall = dh[0];
  1332. float dmin = dh[1];
  1333. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1334. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1335. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1336. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1337. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1338. q1 += step;
  1339. sc += step;
  1340. dh += step;
  1341. }
  1342. y4 += 4 * QK_K;
  1343. }
  1344. for (int row = 0; row < N_DST; ++row) {
  1345. all_sum = simd_sum(sumf[row]);
  1346. if (tiisg == 0) {
  1347. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1348. }
  1349. }
  1350. }
  1351. #else
  1352. kernel void kernel_mul_mat_q4_K_f32(
  1353. device const void * src0,
  1354. device const float * src1,
  1355. device float * dst,
  1356. constant int64_t & ne00,
  1357. constant int64_t & ne01[[buffer(4)]],
  1358. constant int64_t & ne02[[buffer(5)]],
  1359. constant int64_t & ne10[[buffer(9)]],
  1360. constant int64_t & ne12[[buffer(11)]],
  1361. constant int64_t & ne0[[buffer(15)]],
  1362. constant int64_t & ne1[[buffer(16)]],
  1363. constant uint & gqa[[buffer(17)]],
  1364. uint3 tgpig[[threadgroup_position_in_grid]],
  1365. uint tiisg[[thread_index_in_simdgroup]],
  1366. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1367. const int ix = tiisg/4; // 0...7
  1368. const int it = tiisg%4; // 0...3
  1369. const int nb = ne00/QK_K;
  1370. const int r0 = tgpig.x;
  1371. const int r1 = tgpig.y;
  1372. const int r2 = tgpig.z;
  1373. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1374. const int ib_row = first_row * nb;
  1375. const uint offset0 = r2/gqa*(nb*ne0);
  1376. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1377. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1378. float yl[8];
  1379. float yh[8];
  1380. float sumf[N_DST]={0.f}, all_sum;
  1381. const int step = sizeof(block_q4_K) * nb / 2;
  1382. device const float * y4 = y + ix * QK_K + 8 * it;
  1383. uint16_t sc16[4];
  1384. for (int ib = ix; ib < nb; ib += 8) {
  1385. float2 sumy = {0.f, 0.f};
  1386. for (int i = 0; i < 8; ++i) {
  1387. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1388. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1389. }
  1390. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1391. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1392. device const half * dh = x[ib].d;
  1393. for (int row = 0; row < N_DST; row++) {
  1394. sc16[0] = sc[0] & 0x000f;
  1395. sc16[1] = sc[0] & 0x0f00;
  1396. sc16[2] = sc[0] & 0x00f0;
  1397. sc16[3] = sc[0] & 0xf000;
  1398. float2 acc1 = {0.f, 0.f};
  1399. float2 acc2 = {0.f, 0.f};
  1400. for (int i = 0; i < 8; i += 2) {
  1401. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1402. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1403. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1404. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1405. }
  1406. float dall = dh[0];
  1407. float dmin = dh[1];
  1408. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1409. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1410. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1411. qs += step;
  1412. sc += step;
  1413. dh += step;
  1414. }
  1415. y4 += 8 * QK_K;
  1416. }
  1417. for (int row = 0; row < N_DST; ++row) {
  1418. all_sum = simd_sum(sumf[row]);
  1419. if (tiisg == 0) {
  1420. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1421. }
  1422. }
  1423. }
  1424. #endif
  1425. kernel void kernel_mul_mat_q5_K_f32(
  1426. device const void * src0,
  1427. device const float * src1,
  1428. device float * dst,
  1429. constant int64_t & ne00,
  1430. constant int64_t & ne01[[buffer(4)]],
  1431. constant int64_t & ne02[[buffer(5)]],
  1432. constant int64_t & ne10[[buffer(9)]],
  1433. constant int64_t & ne12[[buffer(11)]],
  1434. constant int64_t & ne0[[buffer(15)]],
  1435. constant int64_t & ne1[[buffer(16)]],
  1436. constant uint & gqa[[buffer(17)]],
  1437. uint3 tgpig[[threadgroup_position_in_grid]],
  1438. uint tiisg[[thread_index_in_simdgroup]],
  1439. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1440. const int nb = ne00/QK_K;
  1441. const int64_t r0 = tgpig.x;
  1442. const int64_t r1 = tgpig.y;
  1443. const int r2 = tgpig.z;
  1444. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1445. const uint offset0 = r2/gqa*(nb*ne0);
  1446. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1447. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1448. float sumf[2]={0.f};
  1449. const int step = sizeof(block_q5_K) * nb;
  1450. #if QK_K == 256
  1451. #
  1452. float yl[16], yh[16];
  1453. const uint16_t kmask1 = 0x3f3f;
  1454. const uint16_t kmask2 = 0x0f0f;
  1455. const uint16_t kmask3 = 0xc0c0;
  1456. const int tid = tiisg/4;
  1457. const int ix = tiisg%4;
  1458. const int im = tid/4;
  1459. const int ir = tid%4;
  1460. const int n = 8;
  1461. const int l0 = n*ir;
  1462. const int q_offset = 32*im + l0;
  1463. const int y_offset = 64*im + l0;
  1464. const uint8_t hm1 = 1u << (2*im);
  1465. const uint8_t hm2 = hm1 << 1;
  1466. const uint8_t hm3 = hm1 << 4;
  1467. const uint8_t hm4 = hm2 << 4;
  1468. uint16_t sc16[4];
  1469. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1470. device const float * y1 = yy + ix*QK_K + y_offset;
  1471. for (int i = ix; i < nb; i += 4) {
  1472. device const uint8_t * q1 = x[i].qs + q_offset;
  1473. device const uint8_t * qh = x[i].qh + l0;
  1474. device const half * dh = &x[i].d;
  1475. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1476. device const float * y2 = y1 + 128;
  1477. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1478. for (int l = 0; l < 8; ++l) {
  1479. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1480. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1481. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1482. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1483. }
  1484. for (int row = 0; row < 2; ++row) {
  1485. device const uint8_t * q2 = q1 + 64;
  1486. sc16[0] = a[0] & kmask1;
  1487. sc16[1] = a[2] & kmask1;
  1488. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1489. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1490. float4 acc1 = {0.f};
  1491. float4 acc2 = {0.f};
  1492. for (int l = 0; l < n; ++l) {
  1493. uint8_t h = qh[l];
  1494. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  1495. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  1496. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  1497. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  1498. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  1499. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  1500. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  1501. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  1502. }
  1503. const float dall = dh[0];
  1504. const float dmin = dh[1];
  1505. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  1506. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  1507. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  1508. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  1509. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1510. q1 += step;
  1511. qh += step;
  1512. dh += step/2;
  1513. a += step/2;
  1514. }
  1515. y1 += 4 * QK_K;
  1516. }
  1517. #else
  1518. float yl[8], yh[8];
  1519. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1520. const int ix = tiisg%8;
  1521. const int im = il/8; // 0, 0, 1, 1
  1522. const int in = il%8; // 0, 4, 0, 4
  1523. device const float * y = yy + ix*QK_K + il;
  1524. for (int i = ix; i < nb; i += 8) {
  1525. for (int l = 0; l < 4; ++l) {
  1526. yl[l+0] = y[l+ 0];
  1527. yl[l+4] = y[l+16];
  1528. yh[l+0] = y[l+32];
  1529. yh[l+4] = y[l+48];
  1530. }
  1531. device const half * dh = &x[i].d;
  1532. device const uint8_t * q = x[i].qs + il;
  1533. device const uint8_t * h = x[i].qh + in;
  1534. device const int8_t * s = x[i].scales;
  1535. for (int row = 0; row < 2; ++row) {
  1536. const float d = dh[0];
  1537. float2 acc = {0.f, 0.f};
  1538. for (int l = 0; l < 4; ++l) {
  1539. const uint8_t hl = h[l] >> im;
  1540. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1541. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1542. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1543. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1544. }
  1545. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1546. q += step;
  1547. h += step;
  1548. s += step;
  1549. dh += step/2;
  1550. }
  1551. y += 8 * QK_K;
  1552. }
  1553. #endif
  1554. for (int row = 0; row < 2; ++row) {
  1555. const float tot = simd_sum(sumf[row]);
  1556. if (tiisg == 0) {
  1557. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1558. }
  1559. }
  1560. }
  1561. kernel void kernel_mul_mat_q6_K_f32(
  1562. device const void * src0,
  1563. device const float * src1,
  1564. device float * dst,
  1565. constant int64_t & ne00,
  1566. constant int64_t & ne01[[buffer(4)]],
  1567. constant int64_t & ne02[[buffer(5)]],
  1568. constant int64_t & ne10[[buffer(9)]],
  1569. constant int64_t & ne12[[buffer(11)]],
  1570. constant int64_t & ne0[[buffer(15)]],
  1571. constant int64_t & ne1[[buffer(16)]],
  1572. constant uint & gqa[[buffer(17)]],
  1573. uint3 tgpig[[threadgroup_position_in_grid]],
  1574. uint tiisg[[thread_index_in_simdgroup]],
  1575. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1576. const uint8_t kmask1 = 0x03;
  1577. const uint8_t kmask2 = 0x0C;
  1578. const uint8_t kmask3 = 0x30;
  1579. const uint8_t kmask4 = 0xC0;
  1580. const int nb = ne00/QK_K;
  1581. const int64_t r0 = tgpig.x;
  1582. const int64_t r1 = tgpig.y;
  1583. const int r2 = tgpig.z;
  1584. const int row = 2 * r0 + sgitg;
  1585. const uint offset0 = r2/gqa*(nb*ne0);
  1586. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1587. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1588. float sumf = 0;
  1589. #if QK_K == 256
  1590. const int tid = tiisg/2;
  1591. const int ix = tiisg%2;
  1592. const int ip = tid/8; // 0 or 1
  1593. const int il = tid%8;
  1594. const int n = 4;
  1595. const int l0 = n*il;
  1596. const int is = 8*ip + l0/16;
  1597. const int y_offset = 128*ip + l0;
  1598. const int q_offset_l = 64*ip + l0;
  1599. const int q_offset_h = 32*ip + l0;
  1600. for (int i = ix; i < nb; i += 2) {
  1601. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1602. device const uint8_t * q2 = q1 + 32;
  1603. device const uint8_t * qh = x[i].qh + q_offset_h;
  1604. device const int8_t * sc = x[i].scales + is;
  1605. device const float * y = yy + i * QK_K + y_offset;
  1606. const float dall = x[i].d;
  1607. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1608. for (int l = 0; l < n; ++l) {
  1609. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1610. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1611. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1612. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1613. }
  1614. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1615. }
  1616. #else
  1617. const int ix = tiisg/4;
  1618. const int il = 4*(tiisg%4);
  1619. for (int i = ix; i < nb; i += 8) {
  1620. device const float * y = yy + i * QK_K + il;
  1621. device const uint8_t * ql = x[i].ql + il;
  1622. device const uint8_t * qh = x[i].qh + il;
  1623. device const int8_t * s = x[i].scales;
  1624. const float d = x[i].d;
  1625. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1626. for (int l = 0; l < 4; ++l) {
  1627. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1628. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1629. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1630. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1631. }
  1632. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1633. }
  1634. #endif
  1635. const float tot = simd_sum(sumf);
  1636. if (tiisg == 0) {
  1637. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1638. }
  1639. }
  1640. //============================= templates and their specializations =============================
  1641. // NOTE: this is not dequantizing - we are simply fitting the template
  1642. template <typename type4x4>
  1643. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  1644. float4x4 temp = *(((device float4x4 *)src));
  1645. for (int i = 0; i < 16; i++){
  1646. reg[i/4][i%4] = temp[i/4][i%4];
  1647. }
  1648. }
  1649. template <typename type4x4>
  1650. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1651. half4x4 temp = *(((device half4x4 *)src));
  1652. for (int i = 0; i < 16; i++){
  1653. reg[i/4][i%4] = temp[i/4][i%4];
  1654. }
  1655. }
  1656. template <typename type4x4>
  1657. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1658. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1659. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1660. const float d2 = d1 / 256.f;
  1661. const float md = -8.h * xb->d;
  1662. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1663. const ushort mask1 = mask0 << 8;
  1664. for (int i=0;i<8;i++) {
  1665. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  1666. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  1667. }
  1668. }
  1669. template <typename type4x4>
  1670. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1671. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1672. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1673. const float d2 = d1 / 256.f;
  1674. const float m = xb->m;
  1675. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1676. const ushort mask1 = mask0 << 8;
  1677. for (int i=0;i<8;i++) {
  1678. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  1679. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  1680. }
  1681. }
  1682. template <typename type4x4>
  1683. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1684. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1685. const half d = xb->d;
  1686. for (int i=0;i<16;i++) {
  1687. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1688. }
  1689. }
  1690. template <typename type4x4>
  1691. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1692. const half d = xb->d;
  1693. const half min = xb->dmin;
  1694. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1695. half dl, ml;
  1696. uint8_t sc = xb->scales[il];
  1697. #if QK_K == 256
  1698. q = q + 32*(il/8) + 16*(il&1);
  1699. il = (il/2)%4;
  1700. #endif
  1701. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1702. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1703. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1704. for (int i = 0; i < 16; ++i) {
  1705. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1706. }
  1707. }
  1708. template <typename type4x4>
  1709. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1710. const half d_all = xb->d;
  1711. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1712. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1713. device const int8_t * scales = (device const int8_t *)xb->scales;
  1714. #if QK_K == 256
  1715. q = q + 32 * (il/8) + 16 * (il&1);
  1716. h = h + 16 * (il&1);
  1717. uint8_t m = 1 << (il/2);
  1718. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1719. ((il/4)>0 ? 12 : 3);
  1720. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1721. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1722. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  1723. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1724. half dl = il<8 ? d_all * (dl_int - 32.h) : d_all * (dl_int / 16.h - 32.h);
  1725. const half ml = 4.h * dl;
  1726. il = (il/2) & 3;
  1727. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1728. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1729. dl *= coef;
  1730. for (int i = 0; i < 16; ++i) {
  1731. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  1732. }
  1733. #else
  1734. float kcoef = il&1 ? 1.f/16.f : 1.f;
  1735. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  1736. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  1737. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1738. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1739. uint8_t m = 1<<(il*2);
  1740. for (int i = 0; i < 16; ++i) {
  1741. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  1742. }
  1743. #endif
  1744. }
  1745. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  1746. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  1747. : 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))};
  1748. }
  1749. template <typename type4x4>
  1750. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  1751. device const uchar * q = xb->qs;
  1752. #if QK_K == 256
  1753. short is = (il/4) * 2;
  1754. q = q + (il/4) * 32 + 16 * (il&1);
  1755. il = il & 3;
  1756. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  1757. const half d = il < 2 ? xb->d : xb->d / 16.h;
  1758. const half min = xb->dmin;
  1759. const half dl = d * sc[0];
  1760. const half ml = min * sc[1];
  1761. #else
  1762. q = q + 16 * (il&1);
  1763. device const uint8_t * s = xb->scales;
  1764. device const half2 * dh = (device const half2 *)xb->d;
  1765. const float2 d = (float2)dh[0];
  1766. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  1767. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  1768. #endif
  1769. const ushort mask = il<2 ? 0x0F : 0xF0;
  1770. for (int i = 0; i < 16; ++i) {
  1771. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1772. }
  1773. }
  1774. template <typename type4x4>
  1775. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  1776. device const uint8_t * q = xb->qs;
  1777. device const uint8_t * qh = xb->qh;
  1778. #if QK_K == 256
  1779. short is = (il/4) * 2;
  1780. q = q + 32 * (il/4) + 16 * (il&1);
  1781. qh = qh + 16 * (il&1);
  1782. uint8_t ul = 1 << (il/2);
  1783. il = il & 3;
  1784. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  1785. const half d = il < 2 ? xb->d : xb->d / 16.h;
  1786. const half min = xb->dmin;
  1787. const half dl = d * sc[0];
  1788. const half ml = min * sc[1];
  1789. const ushort mask = il<2 ? 0x0F : 0xF0;
  1790. const half qh_val = il<2 ? 16.h : 256.h;
  1791. for (int i = 0; i < 16; ++i) {
  1792. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  1793. }
  1794. #else
  1795. q = q + 16 * (il&1);
  1796. device const int8_t * s = xb->scales;
  1797. const float dl = xb->d * s[il];
  1798. uint8_t m = 1<<(il*2);
  1799. const float coef = il<2 ? 1.f : 1.f/16.f;
  1800. const ushort mask = il<2 ? 0x0F : 0xF0;
  1801. for (int i = 0; i < 16; ++i) {
  1802. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  1803. }
  1804. #endif
  1805. }
  1806. template <typename type4x4>
  1807. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  1808. const half d_all = xb->d;
  1809. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  1810. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  1811. device const int8_t * scales = (device const int8_t *)xb->scales;
  1812. #if QK_K == 256
  1813. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  1814. qh = qh + 32*(il/8) + 16*(il&1);
  1815. half sc = scales[(il%2) + 2 * ((il/2))];
  1816. il = (il/2) & 3;
  1817. #else
  1818. ql = ql + 16 * (il&1);
  1819. half sc = scales[il];
  1820. #endif
  1821. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1822. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  1823. const half coef = il>1 ? 1.f/16.h : 1.h;
  1824. const half ml = d_all * sc * 32.h;
  1825. const half dl = d_all * sc * coef;
  1826. for (int i = 0; i < 16; ++i) {
  1827. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  1828. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  1829. reg[i/4][i%4] = dl * q - ml;
  1830. }
  1831. }
  1832. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  1833. kernel void kernel_get_rows(
  1834. device const void * src0,
  1835. device const int * src1,
  1836. device float * dst,
  1837. constant int64_t & ne00,
  1838. constant uint64_t & nb01,
  1839. constant uint64_t & nb1,
  1840. uint tgpig[[threadgroup_position_in_grid]],
  1841. uint tiitg[[thread_index_in_threadgroup]],
  1842. uint tptg[[threads_per_threadgroup]]) {
  1843. const int i = tgpig;
  1844. const int r = ((device int32_t *) src1)[i];
  1845. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  1846. float4x4 temp;
  1847. dequantize_func(
  1848. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  1849. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  1850. }
  1851. }
  1852. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  1853. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix A
  1854. #define BLOCK_SIZE_K 32
  1855. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  1856. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  1857. #define THREAD_PER_BLOCK 128
  1858. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  1859. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  1860. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  1861. #define SG_MAT_ROW 8
  1862. // each block_q contains 16*nl weights
  1863. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  1864. kernel void kernel_mul_mm(device const uchar * src0,
  1865. device const uchar * src1,
  1866. device float * dst,
  1867. constant int64_t & ne00,
  1868. constant int64_t & ne02,
  1869. constant int64_t & nb01,
  1870. constant int64_t & nb02,
  1871. constant int64_t & ne12,
  1872. constant int64_t & nb10,
  1873. constant int64_t & nb11,
  1874. constant int64_t & nb12,
  1875. constant int64_t & ne0,
  1876. constant int64_t & ne1,
  1877. constant uint & gqa,
  1878. threadgroup uchar * shared_memory [[threadgroup(0)]],
  1879. uint3 tgpig[[threadgroup_position_in_grid]],
  1880. uint tiitg[[thread_index_in_threadgroup]],
  1881. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1882. threadgroup half * sa = (threadgroup half *)(shared_memory);
  1883. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  1884. const uint r0 = tgpig.y;
  1885. const uint r1 = tgpig.x;
  1886. const uint im = tgpig.z;
  1887. // if this block is of 64x32 shape or smaller
  1888. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  1889. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  1890. // a thread shouldn't load data outside of the matrix
  1891. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  1892. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  1893. simdgroup_half8x8 ma[4];
  1894. simdgroup_float8x8 mb[2];
  1895. simdgroup_float8x8 c_res[8];
  1896. for (int i = 0; i < 8; i++){
  1897. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  1898. }
  1899. short il = (tiitg % THREAD_PER_ROW);
  1900. uint offset0 = im/gqa*nb02;
  1901. ushort offset1 = il/nl;
  1902. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  1903. device const float * y = (device const float *)(src1
  1904. + nb12 * im
  1905. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  1906. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  1907. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  1908. //load data and store to threadgroup memory
  1909. half4x4 temp_a;
  1910. dequantize_func(x, il, temp_a);
  1911. threadgroup_barrier(mem_flags::mem_threadgroup);
  1912. #pragma unroll(16)
  1913. for (int i = 0; i < 16; i++) {
  1914. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  1915. + 16 * (tiitg % THREAD_PER_ROW) + 8 * (i / 8)) \
  1916. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  1917. }
  1918. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) \
  1919. = *((device float2x4 *)y);
  1920. il = (il + 2 < nl) ? il + 2 : il % 2;
  1921. x = (il < 2) ? x + (2+nl-1)/nl : x;
  1922. y += BLOCK_SIZE_K;
  1923. threadgroup_barrier(mem_flags::mem_threadgroup);
  1924. //load matrices from threadgroup memory and conduct outer products
  1925. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  1926. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  1927. #pragma unroll(4)
  1928. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  1929. #pragma unroll(4)
  1930. for (int i = 0; i < 4; i++) {
  1931. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  1932. }
  1933. simdgroup_barrier(mem_flags::mem_none);
  1934. #pragma unroll(2)
  1935. for (int i = 0; i < 2; i++) {
  1936. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  1937. }
  1938. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  1939. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  1940. #pragma unroll(8)
  1941. for (int i = 0; i < 8; i++){
  1942. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  1943. }
  1944. }
  1945. }
  1946. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  1947. device float *C = dst + BLOCK_SIZE_M * r0 + 32 * (sgitg&1) \
  1948. + (BLOCK_SIZE_N * r1 + 16 * (sgitg>>1)) * ne0 + im*ne1*ne0;
  1949. for (int i = 0; i < 8; i++) {
  1950. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  1951. }
  1952. } else {
  1953. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  1954. threadgroup_barrier(mem_flags::mem_threadgroup);
  1955. threadgroup float *temp_str = ((threadgroup float *)shared_memory) \
  1956. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  1957. for (int i = 0; i < 8; i++) {
  1958. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  1959. }
  1960. threadgroup_barrier(mem_flags::mem_threadgroup);
  1961. device float *C = dst + BLOCK_SIZE_M * r0 + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  1962. if (sgitg==0) {
  1963. for (int i = 0; i < n_rows; i++) {
  1964. for (int j = tiitg; j< n_cols; j += BLOCK_SIZE_N) {
  1965. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  1966. }
  1967. }
  1968. }
  1969. }
  1970. }
  1971. #if QK_K == 256
  1972. #define QK_NL 16
  1973. #else
  1974. #define QK_NL 4
  1975. #endif
  1976. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  1977. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  1978. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  1979. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  1980. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  1981. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  1982. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  1983. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  1984. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  1985. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  1986. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  1987. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  1988. typedef void (mat_mm_t)(
  1989. device const uchar * src0,
  1990. device const uchar * src1,
  1991. device float * dst,
  1992. constant int64_t & ne00,
  1993. constant int64_t & ne02,
  1994. constant int64_t & nb01,
  1995. constant int64_t & nb02,
  1996. constant int64_t & ne12,
  1997. constant int64_t & nb10,
  1998. constant int64_t & nb11,
  1999. constant int64_t & nb12,
  2000. constant int64_t & ne0,
  2001. constant int64_t & ne1,
  2002. constant uint & gqa,
  2003. threadgroup uchar *, uint3, uint, uint);
  2004. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  2005. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  2006. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  2007. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  2008. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  2009. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  2010. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  2011. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  2012. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  2013. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;