ggml-metal.metal 90 KB

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