ggml-metal.metal 101 KB

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