ggml-metal.metal 90 KB

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