ggml-metal.metal 96 KB

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