ggml-metal.metal 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  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. thread float * cos_theta, thread 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. float 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 * log(1.0f / freq_scale);
  928. }
  929. *cos_theta = cos(theta) * mscale;
  930. *sin_theta = sin(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 int & n_orig_ctx,
  968. constant float & freq_base,
  969. constant float & freq_scale,
  970. constant float & ext_factor,
  971. constant float & attn_factor,
  972. constant float & beta_fast,
  973. constant float & beta_slow,
  974. uint tiitg[[thread_index_in_threadgroup]],
  975. uint3 tptg[[threads_per_threadgroup]],
  976. uint3 tgpig[[threadgroup_position_in_grid]]);
  977. template<typename T>
  978. kernel void kernel_rope(
  979. device const void * src0,
  980. device const int32_t * src1,
  981. device float * dst,
  982. constant int64_t & ne00,
  983. constant int64_t & ne01,
  984. constant int64_t & ne02,
  985. constant int64_t & ne03,
  986. constant uint64_t & nb00,
  987. constant uint64_t & nb01,
  988. constant uint64_t & nb02,
  989. constant uint64_t & nb03,
  990. constant int64_t & ne0,
  991. constant int64_t & ne1,
  992. constant int64_t & ne2,
  993. constant int64_t & ne3,
  994. constant uint64_t & nb0,
  995. constant uint64_t & nb1,
  996. constant uint64_t & nb2,
  997. constant uint64_t & nb3,
  998. constant int & n_past,
  999. constant int & n_dims,
  1000. constant int & mode,
  1001. constant int & n_orig_ctx,
  1002. constant float & freq_base,
  1003. constant float & freq_scale,
  1004. constant float & ext_factor,
  1005. constant float & attn_factor,
  1006. constant float & beta_fast,
  1007. constant float & beta_slow,
  1008. uint tiitg[[thread_index_in_threadgroup]],
  1009. uint3 tptg[[threads_per_threadgroup]],
  1010. uint3 tgpig[[threadgroup_position_in_grid]]) {
  1011. const int64_t i3 = tgpig[2];
  1012. const int64_t i2 = tgpig[1];
  1013. const int64_t i1 = tgpig[0];
  1014. const bool is_neox = mode & 2;
  1015. float corr_dims[2];
  1016. rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims);
  1017. device const int32_t * pos = src1;
  1018. const int64_t p = pos[i2];
  1019. const float theta_0 = (float)p;
  1020. const float inv_ndims = -1.f/n_dims;
  1021. if (!is_neox) {
  1022. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  1023. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  1024. float cos_theta, sin_theta;
  1025. rope_yarn(theta, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1026. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1027. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1028. const T x0 = src[0];
  1029. const T x1 = src[1];
  1030. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1031. dst_data[1] = x0*sin_theta + x1*cos_theta;
  1032. }
  1033. } else {
  1034. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  1035. for (int64_t ic = 2*tiitg; ic < n_dims; ic += 2*tptg.x) {
  1036. // simplified from `(ib * n_dims + ic) * inv_ndims`
  1037. const float cur_rot = inv_ndims*ic - ib;
  1038. const float theta = theta_0 * pow(freq_base, cur_rot);
  1039. float cos_theta, sin_theta;
  1040. rope_yarn(theta, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1041. const int64_t i0 = ib*n_dims + ic/2;
  1042. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1043. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1044. const float x0 = src[0];
  1045. const float x1 = src[n_dims/2];
  1046. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1047. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. template [[host_name("kernel_rope_f32")]] kernel rope_t kernel_rope<float>;
  1053. template [[host_name("kernel_rope_f16")]] kernel rope_t kernel_rope<half>;
  1054. kernel void kernel_cpy_f16_f16(
  1055. device const half * src0,
  1056. device half * dst,
  1057. constant int64_t & ne00,
  1058. constant int64_t & ne01,
  1059. constant int64_t & ne02,
  1060. constant int64_t & ne03,
  1061. constant uint64_t & nb00,
  1062. constant uint64_t & nb01,
  1063. constant uint64_t & nb02,
  1064. constant uint64_t & nb03,
  1065. constant int64_t & ne0,
  1066. constant int64_t & ne1,
  1067. constant int64_t & ne2,
  1068. constant int64_t & ne3,
  1069. constant uint64_t & nb0,
  1070. constant uint64_t & nb1,
  1071. constant uint64_t & nb2,
  1072. constant uint64_t & nb3,
  1073. uint3 tgpig[[threadgroup_position_in_grid]],
  1074. uint3 tpitg[[thread_position_in_threadgroup]],
  1075. uint3 ntg[[threads_per_threadgroup]]) {
  1076. const int64_t i03 = tgpig[2];
  1077. const int64_t i02 = tgpig[1];
  1078. const int64_t i01 = tgpig[0];
  1079. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1080. const int64_t i3 = n / (ne2*ne1*ne0);
  1081. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1082. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1083. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1084. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1085. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1086. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1087. dst_data[i00] = src[0];
  1088. }
  1089. }
  1090. kernel void kernel_cpy_f32_f16(
  1091. device const float * src0,
  1092. device half * dst,
  1093. constant int64_t & ne00,
  1094. constant int64_t & ne01,
  1095. constant int64_t & ne02,
  1096. constant int64_t & ne03,
  1097. constant uint64_t & nb00,
  1098. constant uint64_t & nb01,
  1099. constant uint64_t & nb02,
  1100. constant uint64_t & nb03,
  1101. constant int64_t & ne0,
  1102. constant int64_t & ne1,
  1103. constant int64_t & ne2,
  1104. constant int64_t & ne3,
  1105. constant uint64_t & nb0,
  1106. constant uint64_t & nb1,
  1107. constant uint64_t & nb2,
  1108. constant uint64_t & nb3,
  1109. uint3 tgpig[[threadgroup_position_in_grid]],
  1110. uint3 tpitg[[thread_position_in_threadgroup]],
  1111. uint3 ntg[[threads_per_threadgroup]]) {
  1112. const int64_t i03 = tgpig[2];
  1113. const int64_t i02 = tgpig[1];
  1114. const int64_t i01 = tgpig[0];
  1115. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1116. const int64_t i3 = n / (ne2*ne1*ne0);
  1117. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1118. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1119. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1120. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1121. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1122. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1123. dst_data[i00] = src[0];
  1124. }
  1125. }
  1126. kernel void kernel_cpy_f32_f32(
  1127. device const float * src0,
  1128. device float * dst,
  1129. constant int64_t & ne00,
  1130. constant int64_t & ne01,
  1131. constant int64_t & ne02,
  1132. constant int64_t & ne03,
  1133. constant uint64_t & nb00,
  1134. constant uint64_t & nb01,
  1135. constant uint64_t & nb02,
  1136. constant uint64_t & nb03,
  1137. constant int64_t & ne0,
  1138. constant int64_t & ne1,
  1139. constant int64_t & ne2,
  1140. constant int64_t & ne3,
  1141. constant uint64_t & nb0,
  1142. constant uint64_t & nb1,
  1143. constant uint64_t & nb2,
  1144. constant uint64_t & nb3,
  1145. uint3 tgpig[[threadgroup_position_in_grid]],
  1146. uint3 tpitg[[thread_position_in_threadgroup]],
  1147. uint3 ntg[[threads_per_threadgroup]]) {
  1148. const int64_t i03 = tgpig[2];
  1149. const int64_t i02 = tgpig[1];
  1150. const int64_t i01 = tgpig[0];
  1151. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1152. const int64_t i3 = n / (ne2*ne1*ne0);
  1153. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1154. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1155. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1156. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1157. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1158. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1159. dst_data[i00] = src[0];
  1160. }
  1161. }
  1162. kernel void kernel_concat(
  1163. device const char * src0,
  1164. device const char * src1,
  1165. device char * dst,
  1166. constant int64_t & ne00,
  1167. constant int64_t & ne01,
  1168. constant int64_t & ne02,
  1169. constant int64_t & ne03,
  1170. constant uint64_t & nb00,
  1171. constant uint64_t & nb01,
  1172. constant uint64_t & nb02,
  1173. constant uint64_t & nb03,
  1174. constant int64_t & ne10,
  1175. constant int64_t & ne11,
  1176. constant int64_t & ne12,
  1177. constant int64_t & ne13,
  1178. constant uint64_t & nb10,
  1179. constant uint64_t & nb11,
  1180. constant uint64_t & nb12,
  1181. constant uint64_t & nb13,
  1182. constant int64_t & ne0,
  1183. constant int64_t & ne1,
  1184. constant int64_t & ne2,
  1185. constant int64_t & ne3,
  1186. constant uint64_t & nb0,
  1187. constant uint64_t & nb1,
  1188. constant uint64_t & nb2,
  1189. constant uint64_t & nb3,
  1190. uint3 tgpig[[threadgroup_position_in_grid]],
  1191. uint3 tpitg[[thread_position_in_threadgroup]],
  1192. uint3 ntg[[threads_per_threadgroup]]) {
  1193. const int64_t i03 = tgpig.z;
  1194. const int64_t i02 = tgpig.y;
  1195. const int64_t i01 = tgpig.x;
  1196. const int64_t i13 = i03 % ne13;
  1197. const int64_t i12 = i02 % ne12;
  1198. const int64_t i11 = i01 % ne11;
  1199. device const char * src0_ptr = src0 + i03 * nb03 + i02 * nb02 + i01 * nb01 + tpitg.x*nb00;
  1200. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11 + tpitg.x*nb10;
  1201. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + tpitg.x*nb0;
  1202. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1203. if (i02 < ne02) {
  1204. ((device float *)dst_ptr)[0] = ((device float *)src0_ptr)[0];
  1205. src0_ptr += ntg.x*nb00;
  1206. } else {
  1207. ((device float *)dst_ptr)[0] = ((device float *)src1_ptr)[0];
  1208. src1_ptr += ntg.x*nb10;
  1209. }
  1210. dst_ptr += ntg.x*nb0;
  1211. }
  1212. }
  1213. //============================================ k-quants ======================================================
  1214. #ifndef QK_K
  1215. #define QK_K 256
  1216. #else
  1217. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  1218. #endif
  1219. #if QK_K == 256
  1220. #define K_SCALE_SIZE 12
  1221. #else
  1222. #define K_SCALE_SIZE 4
  1223. #endif
  1224. typedef struct {
  1225. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  1226. uint8_t qs[QK_K/4]; // quants
  1227. half d; // super-block scale for quantized scales
  1228. half dmin; // super-block scale for quantized mins
  1229. } block_q2_K;
  1230. // 84 bytes / block
  1231. typedef struct {
  1232. uint8_t hmask[QK_K/8]; // quants - high bit
  1233. uint8_t qs[QK_K/4]; // quants - low 2 bits
  1234. #if QK_K == 64
  1235. uint8_t scales[2];
  1236. #else
  1237. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  1238. #endif
  1239. half d; // super-block scale
  1240. } block_q3_K;
  1241. #if QK_K == 64
  1242. typedef struct {
  1243. half d[2]; // super-block scales/mins
  1244. uint8_t scales[2];
  1245. uint8_t qs[QK_K/2]; // 4-bit quants
  1246. } block_q4_K;
  1247. #else
  1248. typedef struct {
  1249. half d; // super-block scale for quantized scales
  1250. half dmin; // super-block scale for quantized mins
  1251. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  1252. uint8_t qs[QK_K/2]; // 4--bit quants
  1253. } block_q4_K;
  1254. #endif
  1255. #if QK_K == 64
  1256. typedef struct {
  1257. half d; // super-block scales/mins
  1258. int8_t scales[QK_K/16]; // 8-bit block scales
  1259. uint8_t qh[QK_K/8]; // quants, high bit
  1260. uint8_t qs[QK_K/2]; // quants, low 4 bits
  1261. } block_q5_K;
  1262. #else
  1263. typedef struct {
  1264. half d; // super-block scale for quantized scales
  1265. half dmin; // super-block scale for quantized mins
  1266. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  1267. uint8_t qh[QK_K/8]; // quants, high bit
  1268. uint8_t qs[QK_K/2]; // quants, low 4 bits
  1269. } block_q5_K;
  1270. // 176 bytes / block
  1271. #endif
  1272. typedef struct {
  1273. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  1274. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  1275. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  1276. half d; // super-block scale
  1277. } block_q6_K;
  1278. // 210 bytes / block
  1279. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  1280. uchar4 r;
  1281. if (j < 4) {
  1282. r[0] = q[j+0] & 63;
  1283. r[2] = q[j+1] & 63;
  1284. r[1] = q[j+4] & 63;
  1285. r[3] = q[j+5] & 63;
  1286. } else {
  1287. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1288. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  1289. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1290. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  1291. }
  1292. return r;
  1293. }
  1294. //====================================== dot products =========================
  1295. kernel void kernel_mul_mv_q2_K_f32(
  1296. device const void * src0,
  1297. device const float * src1,
  1298. device float * dst,
  1299. constant int64_t & ne00,
  1300. constant int64_t & ne01[[buffer(4)]],
  1301. constant int64_t & ne02[[buffer(5)]],
  1302. constant int64_t & ne10[[buffer(9)]],
  1303. constant int64_t & ne12[[buffer(11)]],
  1304. constant int64_t & ne0[[buffer(15)]],
  1305. constant int64_t & ne1[[buffer(16)]],
  1306. constant uint & gqa[[buffer(17)]],
  1307. uint3 tgpig[[threadgroup_position_in_grid]],
  1308. uint tiisg[[thread_index_in_simdgroup]],
  1309. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1310. const int nb = ne00/QK_K;
  1311. const int r0 = tgpig.x;
  1312. const int r1 = tgpig.y;
  1313. const int r2 = tgpig.z;
  1314. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1315. const int ib_row = first_row * nb;
  1316. const uint offset0 = r2/gqa*(nb*ne0);
  1317. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  1318. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1319. float yl[32];
  1320. float sumf[N_DST]={0.f}, all_sum;
  1321. const int step = sizeof(block_q2_K) * nb;
  1322. #if QK_K == 256
  1323. const int ix = tiisg/8; // 0...3
  1324. const int it = tiisg%8; // 0...7
  1325. const int im = it/4; // 0 or 1
  1326. const int ir = it%4; // 0...3
  1327. const int is = (8*ir)/16;// 0 or 1
  1328. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  1329. for (int ib = ix; ib < nb; ib += 4) {
  1330. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1331. for (int i = 0; i < 8; ++i) {
  1332. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1333. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  1334. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  1335. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  1336. }
  1337. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  1338. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1339. device const half * dh = &x[ib].d;
  1340. for (int row = 0; row < N_DST; row++) {
  1341. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1342. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1343. for (int i = 0; i < 8; i += 2) {
  1344. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1345. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1346. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1347. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1348. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1349. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1350. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1351. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1352. }
  1353. float dall = dh[0];
  1354. float dmin = dh[1] * 1.f/16.f;
  1355. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1356. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  1357. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  1358. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  1359. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  1360. qs += step/2;
  1361. sc += step;
  1362. dh += step/2;
  1363. }
  1364. y4 += 4 * QK_K;
  1365. }
  1366. #else
  1367. const int ix = tiisg/2; // 0...15
  1368. const int it = tiisg%2; // 0...1
  1369. device const float * y4 = y + ix * QK_K + 8 * it;
  1370. for (int ib = ix; ib < nb; ib += 16) {
  1371. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1372. for (int i = 0; i < 8; ++i) {
  1373. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1374. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  1375. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  1376. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  1377. }
  1378. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  1379. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1380. device const half * dh = &x[ib].d;
  1381. for (int row = 0; row < N_DST; row++) {
  1382. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1383. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1384. for (int i = 0; i < 8; i += 2) {
  1385. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1386. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1387. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1388. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1389. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1390. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1391. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1392. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1393. }
  1394. float dall = dh[0];
  1395. float dmin = dh[1];
  1396. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1397. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  1398. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  1399. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  1400. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  1401. qs += step/2;
  1402. sc += step;
  1403. dh += step/2;
  1404. }
  1405. y4 += 16 * QK_K;
  1406. }
  1407. #endif
  1408. for (int row = 0; row < N_DST; ++row) {
  1409. all_sum = simd_sum(sumf[row]);
  1410. if (tiisg == 0) {
  1411. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1412. }
  1413. }
  1414. }
  1415. #if QK_K == 256
  1416. kernel void kernel_mul_mv_q3_K_f32(
  1417. device const void * src0,
  1418. device const float * src1,
  1419. device float * dst,
  1420. constant int64_t & ne00,
  1421. constant int64_t & ne01[[buffer(4)]],
  1422. constant int64_t & ne02[[buffer(5)]],
  1423. constant int64_t & ne10[[buffer(9)]],
  1424. constant int64_t & ne12[[buffer(11)]],
  1425. constant int64_t & ne0[[buffer(15)]],
  1426. constant int64_t & ne1[[buffer(16)]],
  1427. constant uint & gqa[[buffer(17)]],
  1428. uint3 tgpig[[threadgroup_position_in_grid]],
  1429. uint tiisg[[thread_index_in_simdgroup]],
  1430. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1431. const int nb = ne00/QK_K;
  1432. const int64_t r0 = tgpig.x;
  1433. const int64_t r1 = tgpig.y;
  1434. const int64_t r2 = tgpig.z;
  1435. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1436. const uint offset0 = r2/gqa*(nb*ne0);
  1437. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  1438. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1439. float yl[32];
  1440. //const uint16_t kmask1 = 0x3030;
  1441. //const uint16_t kmask2 = 0x0f0f;
  1442. const int tid = tiisg/4;
  1443. const int ix = tiisg%4;
  1444. const int ip = tid/4; // 0 or 1
  1445. const int il = 2*((tid%4)/2); // 0 or 2
  1446. const int ir = tid%2;
  1447. const int n = 8;
  1448. const int l0 = n*ir;
  1449. // One would think that the Metal compiler would figure out that ip and il can only have
  1450. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  1451. // with these two tales.
  1452. //
  1453. // Possible masks for the high bit
  1454. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  1455. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  1456. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  1457. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  1458. // Possible masks for the low 2 bits
  1459. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  1460. const ushort4 hm = mm[2*ip + il/2];
  1461. const int shift = 2*il;
  1462. const float v1 = il == 0 ? 4.f : 64.f;
  1463. const float v2 = 4.f * v1;
  1464. const uint16_t s_shift1 = 4*ip;
  1465. const uint16_t s_shift2 = s_shift1 + il;
  1466. const int q_offset = 32*ip + l0;
  1467. const int y_offset = 128*ip + 32*il + l0;
  1468. const int step = sizeof(block_q3_K) * nb / 2;
  1469. device const float * y1 = yy + ix*QK_K + y_offset;
  1470. uint32_t scales32, aux32;
  1471. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  1472. thread const int8_t * scales = (thread const int8_t *)&scales32;
  1473. float sumf1[2] = {0.f};
  1474. float sumf2[2] = {0.f};
  1475. for (int i = ix; i < nb; i += 4) {
  1476. for (int l = 0; l < 8; ++l) {
  1477. yl[l+ 0] = y1[l+ 0];
  1478. yl[l+ 8] = y1[l+16];
  1479. yl[l+16] = y1[l+32];
  1480. yl[l+24] = y1[l+48];
  1481. }
  1482. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1483. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1484. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1485. device const half * dh = &x[i].d;
  1486. for (int row = 0; row < 2; ++row) {
  1487. const float d_all = (float)dh[0];
  1488. scales16[0] = a[4];
  1489. scales16[1] = a[5];
  1490. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  1491. scales16[0] = a[il+0];
  1492. scales16[1] = a[il+1];
  1493. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  1494. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  1495. for (int l = 0; l < n; l += 2) {
  1496. const int32_t qs = q[l/2];
  1497. s1 += yl[l+0] * (qs & qm[il/2][0]);
  1498. s2 += yl[l+1] * (qs & qm[il/2][1]);
  1499. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  1500. s4 += yl[l+16] * (qs & qm[il/2][2]);
  1501. s5 += yl[l+17] * (qs & qm[il/2][3]);
  1502. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  1503. }
  1504. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1505. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1506. sumf1[row] += d1 * (scales[0] - 32);
  1507. sumf2[row] += d2 * (scales[2] - 32);
  1508. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  1509. for (int l = 0; l < n; l += 2) {
  1510. const int32_t qs = q[l/2+8];
  1511. s1 += yl[l+8] * (qs & qm[il/2][0]);
  1512. s2 += yl[l+9] * (qs & qm[il/2][1]);
  1513. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  1514. s4 += yl[l+24] * (qs & qm[il/2][2]);
  1515. s5 += yl[l+25] * (qs & qm[il/2][3]);
  1516. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  1517. }
  1518. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1519. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1520. sumf1[row] += d1 * (scales[1] - 32);
  1521. sumf2[row] += d2 * (scales[3] - 32);
  1522. q += step;
  1523. h += step;
  1524. a += step;
  1525. dh += step;
  1526. }
  1527. y1 += 4 * QK_K;
  1528. }
  1529. for (int row = 0; row < 2; ++row) {
  1530. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  1531. sumf1[row] = simd_sum(sumf);
  1532. }
  1533. if (tiisg == 0) {
  1534. for (int row = 0; row < 2; ++row) {
  1535. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = sumf1[row];
  1536. }
  1537. }
  1538. }
  1539. #else
  1540. kernel void kernel_mul_mv_q3_K_f32(
  1541. device const void * src0,
  1542. device const float * src1,
  1543. device float * dst,
  1544. constant int64_t & ne00,
  1545. constant int64_t & ne01[[buffer(4)]],
  1546. constant int64_t & ne02[[buffer(5)]],
  1547. constant int64_t & ne10[[buffer(9)]],
  1548. constant int64_t & ne12[[buffer(11)]],
  1549. constant int64_t & ne0[[buffer(15)]],
  1550. constant int64_t & ne1[[buffer(16)]],
  1551. constant uint & gqa[[buffer(17)]],
  1552. uint3 tgpig[[threadgroup_position_in_grid]],
  1553. uint tiisg[[thread_index_in_simdgroup]],
  1554. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1555. const int nb = ne00/QK_K;
  1556. const int64_t r0 = tgpig.x;
  1557. const int64_t r1 = tgpig.y;
  1558. const int64_t r2 = tgpig.z;
  1559. const int row = 2 * r0 + sgitg;
  1560. const uint offset0 = r2/gqa*(nb*ne0);
  1561. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1562. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1563. const int ix = tiisg/4;
  1564. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1565. const int im = il/8; // 0, 0, 1, 1
  1566. const int in = il%8; // 0, 4, 0, 4
  1567. float2 sum = {0.f, 0.f};
  1568. for (int i = ix; i < nb; i += 8) {
  1569. const float d_all = (float)(x[i].d);
  1570. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1571. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1572. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1573. device const float * y = yy + i * QK_K + il;
  1574. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1575. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1576. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1577. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1578. for (int l = 0; l < 4; l += 2) {
  1579. const uint16_t hm = h[l/2] >> im;
  1580. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1581. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1582. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1583. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1584. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1585. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1586. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1587. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1588. }
  1589. }
  1590. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1591. const float tot = simd_sum(sumf);
  1592. if (tiisg == 0) {
  1593. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1594. }
  1595. }
  1596. #endif
  1597. #if QK_K == 256
  1598. kernel void kernel_mul_mv_q4_K_f32(
  1599. device const void * src0,
  1600. device const float * src1,
  1601. device float * dst,
  1602. constant int64_t & ne00,
  1603. constant int64_t & ne01 [[buffer(4)]],
  1604. constant int64_t & ne02 [[buffer(5)]],
  1605. constant int64_t & ne10 [[buffer(9)]],
  1606. constant int64_t & ne12 [[buffer(11)]],
  1607. constant int64_t & ne0 [[buffer(15)]],
  1608. constant int64_t & ne1 [[buffer(16)]],
  1609. constant uint & gqa [[buffer(17)]],
  1610. uint3 tgpig[[threadgroup_position_in_grid]],
  1611. uint tiisg[[thread_index_in_simdgroup]],
  1612. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1613. const uint16_t kmask1 = 0x3f3f;
  1614. const uint16_t kmask2 = 0x0f0f;
  1615. const uint16_t kmask3 = 0xc0c0;
  1616. const int ix = tiisg/8; // 0...3
  1617. const int it = tiisg%8; // 0...7
  1618. const int im = it/4; // 0 or 1
  1619. const int ir = it%4; // 0...3
  1620. const int nb = ne00/QK_K;
  1621. const int r0 = tgpig.x;
  1622. const int r1 = tgpig.y;
  1623. const int r2 = tgpig.z;
  1624. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1625. const int first_row = r0 * N_DST;
  1626. const int ib_row = first_row * nb;
  1627. const uint offset0 = r2/gqa*(nb*ne0);
  1628. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1629. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1630. float yl[16];
  1631. float yh[16];
  1632. float sumf[N_DST]={0.f}, all_sum;
  1633. const int step = sizeof(block_q4_K) * nb / 2;
  1634. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1635. uint16_t sc16[4];
  1636. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1637. for (int ib = ix; ib < nb; ib += 4) {
  1638. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1639. for (int i = 0; i < 8; ++i) {
  1640. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1641. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1642. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1643. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1644. }
  1645. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1646. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1647. device const half * dh = &x[ib].d;
  1648. for (int row = 0; row < N_DST; row++) {
  1649. sc16[0] = sc[0] & kmask1;
  1650. sc16[1] = sc[2] & kmask1;
  1651. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1652. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1653. device const uint16_t * q2 = q1 + 32;
  1654. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1655. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1656. for (int i = 0; i < 8; i += 2) {
  1657. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1658. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1659. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1660. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1661. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1662. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1663. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1664. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1665. }
  1666. float dall = dh[0];
  1667. float dmin = dh[1];
  1668. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1669. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1670. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1671. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1672. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1673. q1 += step;
  1674. sc += step;
  1675. dh += step;
  1676. }
  1677. y4 += 4 * QK_K;
  1678. }
  1679. for (int row = 0; row < N_DST; ++row) {
  1680. all_sum = simd_sum(sumf[row]);
  1681. if (tiisg == 0) {
  1682. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1683. }
  1684. }
  1685. }
  1686. #else
  1687. kernel void kernel_mul_mv_q4_K_f32(
  1688. device const void * src0,
  1689. device const float * src1,
  1690. device float * dst,
  1691. constant int64_t & ne00,
  1692. constant int64_t & ne01[[buffer(4)]],
  1693. constant int64_t & ne02[[buffer(5)]],
  1694. constant int64_t & ne10[[buffer(9)]],
  1695. constant int64_t & ne12[[buffer(11)]],
  1696. constant int64_t & ne0[[buffer(15)]],
  1697. constant int64_t & ne1[[buffer(16)]],
  1698. constant uint & gqa[[buffer(17)]],
  1699. uint3 tgpig[[threadgroup_position_in_grid]],
  1700. uint tiisg[[thread_index_in_simdgroup]],
  1701. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1702. const int ix = tiisg/4; // 0...7
  1703. const int it = tiisg%4; // 0...3
  1704. const int nb = ne00/QK_K;
  1705. const int r0 = tgpig.x;
  1706. const int r1 = tgpig.y;
  1707. const int r2 = tgpig.z;
  1708. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1709. const int ib_row = first_row * nb;
  1710. const uint offset0 = r2/gqa*(nb*ne0);
  1711. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1712. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1713. float yl[8];
  1714. float yh[8];
  1715. float sumf[N_DST]={0.f}, all_sum;
  1716. const int step = sizeof(block_q4_K) * nb / 2;
  1717. device const float * y4 = y + ix * QK_K + 8 * it;
  1718. uint16_t sc16[4];
  1719. for (int ib = ix; ib < nb; ib += 8) {
  1720. float2 sumy = {0.f, 0.f};
  1721. for (int i = 0; i < 8; ++i) {
  1722. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1723. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1724. }
  1725. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1726. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1727. device const half * dh = x[ib].d;
  1728. for (int row = 0; row < N_DST; row++) {
  1729. sc16[0] = sc[0] & 0x000f;
  1730. sc16[1] = sc[0] & 0x0f00;
  1731. sc16[2] = sc[0] & 0x00f0;
  1732. sc16[3] = sc[0] & 0xf000;
  1733. float2 acc1 = {0.f, 0.f};
  1734. float2 acc2 = {0.f, 0.f};
  1735. for (int i = 0; i < 8; i += 2) {
  1736. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1737. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1738. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1739. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1740. }
  1741. float dall = dh[0];
  1742. float dmin = dh[1];
  1743. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1744. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1745. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1746. qs += step;
  1747. sc += step;
  1748. dh += step;
  1749. }
  1750. y4 += 8 * QK_K;
  1751. }
  1752. for (int row = 0; row < N_DST; ++row) {
  1753. all_sum = simd_sum(sumf[row]);
  1754. if (tiisg == 0) {
  1755. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1756. }
  1757. }
  1758. }
  1759. #endif
  1760. kernel void kernel_mul_mv_q5_K_f32(
  1761. device const void * src0,
  1762. device const float * src1,
  1763. device float * dst,
  1764. constant int64_t & ne00,
  1765. constant int64_t & ne01[[buffer(4)]],
  1766. constant int64_t & ne02[[buffer(5)]],
  1767. constant int64_t & ne10[[buffer(9)]],
  1768. constant int64_t & ne12[[buffer(11)]],
  1769. constant int64_t & ne0[[buffer(15)]],
  1770. constant int64_t & ne1[[buffer(16)]],
  1771. constant uint & gqa[[buffer(17)]],
  1772. uint3 tgpig[[threadgroup_position_in_grid]],
  1773. uint tiisg[[thread_index_in_simdgroup]],
  1774. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1775. const int nb = ne00/QK_K;
  1776. const int64_t r0 = tgpig.x;
  1777. const int64_t r1 = tgpig.y;
  1778. const int r2 = tgpig.z;
  1779. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1780. const uint offset0 = r2/gqa*(nb*ne0);
  1781. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1782. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1783. float sumf[2]={0.f};
  1784. const int step = sizeof(block_q5_K) * nb;
  1785. #if QK_K == 256
  1786. #
  1787. float yl[16], yh[16];
  1788. const uint16_t kmask1 = 0x3f3f;
  1789. const uint16_t kmask2 = 0x0f0f;
  1790. const uint16_t kmask3 = 0xc0c0;
  1791. const int tid = tiisg/4;
  1792. const int ix = tiisg%4;
  1793. const int im = tid/4;
  1794. const int ir = tid%4;
  1795. const int n = 8;
  1796. const int l0 = n*ir;
  1797. const int q_offset = 32*im + l0;
  1798. const int y_offset = 64*im + l0;
  1799. const uint8_t hm1 = 1u << (2*im);
  1800. const uint8_t hm2 = hm1 << 1;
  1801. const uint8_t hm3 = hm1 << 4;
  1802. const uint8_t hm4 = hm2 << 4;
  1803. uint16_t sc16[4];
  1804. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1805. device const float * y1 = yy + ix*QK_K + y_offset;
  1806. for (int i = ix; i < nb; i += 4) {
  1807. device const uint8_t * q1 = x[i].qs + q_offset;
  1808. device const uint8_t * qh = x[i].qh + l0;
  1809. device const half * dh = &x[i].d;
  1810. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1811. device const float * y2 = y1 + 128;
  1812. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1813. for (int l = 0; l < 8; ++l) {
  1814. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1815. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1816. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1817. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1818. }
  1819. for (int row = 0; row < 2; ++row) {
  1820. device const uint8_t * q2 = q1 + 64;
  1821. sc16[0] = a[0] & kmask1;
  1822. sc16[1] = a[2] & kmask1;
  1823. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1824. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1825. float4 acc1 = {0.f};
  1826. float4 acc2 = {0.f};
  1827. for (int l = 0; l < n; ++l) {
  1828. uint8_t h = qh[l];
  1829. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  1830. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  1831. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  1832. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  1833. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  1834. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  1835. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  1836. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  1837. }
  1838. const float dall = dh[0];
  1839. const float dmin = dh[1];
  1840. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  1841. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  1842. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  1843. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  1844. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1845. q1 += step;
  1846. qh += step;
  1847. dh += step/2;
  1848. a += step/2;
  1849. }
  1850. y1 += 4 * QK_K;
  1851. }
  1852. #else
  1853. float yl[8], yh[8];
  1854. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1855. const int ix = tiisg%8;
  1856. const int im = il/8; // 0, 0, 1, 1
  1857. const int in = il%8; // 0, 4, 0, 4
  1858. device const float * y = yy + ix*QK_K + il;
  1859. for (int i = ix; i < nb; i += 8) {
  1860. for (int l = 0; l < 4; ++l) {
  1861. yl[l+0] = y[l+ 0];
  1862. yl[l+4] = y[l+16];
  1863. yh[l+0] = y[l+32];
  1864. yh[l+4] = y[l+48];
  1865. }
  1866. device const half * dh = &x[i].d;
  1867. device const uint8_t * q = x[i].qs + il;
  1868. device const uint8_t * h = x[i].qh + in;
  1869. device const int8_t * s = x[i].scales;
  1870. for (int row = 0; row < 2; ++row) {
  1871. const float d = dh[0];
  1872. float2 acc = {0.f, 0.f};
  1873. for (int l = 0; l < 4; ++l) {
  1874. const uint8_t hl = h[l] >> im;
  1875. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1876. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1877. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1878. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1879. }
  1880. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1881. q += step;
  1882. h += step;
  1883. s += step;
  1884. dh += step/2;
  1885. }
  1886. y += 8 * QK_K;
  1887. }
  1888. #endif
  1889. for (int row = 0; row < 2; ++row) {
  1890. const float tot = simd_sum(sumf[row]);
  1891. if (tiisg == 0) {
  1892. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1893. }
  1894. }
  1895. }
  1896. kernel void kernel_mul_mv_q6_K_f32(
  1897. device const void * src0,
  1898. device const float * src1,
  1899. device float * dst,
  1900. constant int64_t & ne00,
  1901. constant int64_t & ne01[[buffer(4)]],
  1902. constant int64_t & ne02[[buffer(5)]],
  1903. constant int64_t & ne10[[buffer(9)]],
  1904. constant int64_t & ne12[[buffer(11)]],
  1905. constant int64_t & ne0[[buffer(15)]],
  1906. constant int64_t & ne1[[buffer(16)]],
  1907. constant uint & gqa[[buffer(17)]],
  1908. uint3 tgpig[[threadgroup_position_in_grid]],
  1909. uint tiisg[[thread_index_in_simdgroup]],
  1910. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1911. const uint8_t kmask1 = 0x03;
  1912. const uint8_t kmask2 = 0x0C;
  1913. const uint8_t kmask3 = 0x30;
  1914. const uint8_t kmask4 = 0xC0;
  1915. const int nb = ne00/QK_K;
  1916. const int64_t r0 = tgpig.x;
  1917. const int64_t r1 = tgpig.y;
  1918. const int r2 = tgpig.z;
  1919. const int row = 2 * r0 + sgitg;
  1920. const uint offset0 = r2/gqa*(nb*ne0);
  1921. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1922. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1923. float sumf = 0;
  1924. #if QK_K == 256
  1925. const int tid = tiisg/2;
  1926. const int ix = tiisg%2;
  1927. const int ip = tid/8; // 0 or 1
  1928. const int il = tid%8;
  1929. const int n = 4;
  1930. const int l0 = n*il;
  1931. const int is = 8*ip + l0/16;
  1932. const int y_offset = 128*ip + l0;
  1933. const int q_offset_l = 64*ip + l0;
  1934. const int q_offset_h = 32*ip + l0;
  1935. for (int i = ix; i < nb; i += 2) {
  1936. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1937. device const uint8_t * q2 = q1 + 32;
  1938. device const uint8_t * qh = x[i].qh + q_offset_h;
  1939. device const int8_t * sc = x[i].scales + is;
  1940. device const float * y = yy + i * QK_K + y_offset;
  1941. const float dall = x[i].d;
  1942. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1943. for (int l = 0; l < n; ++l) {
  1944. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1945. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1946. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1947. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1948. }
  1949. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1950. }
  1951. #else
  1952. const int ix = tiisg/4;
  1953. const int il = 4*(tiisg%4);
  1954. for (int i = ix; i < nb; i += 8) {
  1955. device const float * y = yy + i * QK_K + il;
  1956. device const uint8_t * ql = x[i].ql + il;
  1957. device const uint8_t * qh = x[i].qh + il;
  1958. device const int8_t * s = x[i].scales;
  1959. const float d = x[i].d;
  1960. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1961. for (int l = 0; l < 4; ++l) {
  1962. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1963. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1964. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1965. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1966. }
  1967. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1968. }
  1969. #endif
  1970. const float tot = simd_sum(sumf);
  1971. if (tiisg == 0) {
  1972. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1973. }
  1974. }
  1975. //============================= templates and their specializations =============================
  1976. // NOTE: this is not dequantizing - we are simply fitting the template
  1977. template <typename type4x4>
  1978. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  1979. float4x4 temp = *(((device float4x4 *)src));
  1980. for (int i = 0; i < 16; i++){
  1981. reg[i/4][i%4] = temp[i/4][i%4];
  1982. }
  1983. }
  1984. template <typename type4x4>
  1985. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1986. half4x4 temp = *(((device half4x4 *)src));
  1987. for (int i = 0; i < 16; i++){
  1988. reg[i/4][i%4] = temp[i/4][i%4];
  1989. }
  1990. }
  1991. template <typename type4x4>
  1992. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1993. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1994. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1995. const float d2 = d1 / 256.f;
  1996. const float md = -8.h * xb->d;
  1997. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1998. const ushort mask1 = mask0 << 8;
  1999. for (int i=0;i<8;i++) {
  2000. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  2001. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  2002. }
  2003. }
  2004. template <typename type4x4>
  2005. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  2006. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  2007. const float d1 = il ? (xb->d / 16.h) : xb->d;
  2008. const float d2 = d1 / 256.f;
  2009. const float m = xb->m;
  2010. const ushort mask0 = il ? 0x00F0 : 0x000F;
  2011. const ushort mask1 = mask0 << 8;
  2012. for (int i=0;i<8;i++) {
  2013. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  2014. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  2015. }
  2016. }
  2017. template <typename type4x4>
  2018. void dequantize_q5_0(device const block_q5_0 *xb, short il, thread type4x4 & reg) {
  2019. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  2020. const float d = xb->d;
  2021. const float md = -16.h * xb->d;
  2022. const ushort mask = il ? 0x00F0 : 0x000F;
  2023. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2024. const int x_mv = il ? 4 : 0;
  2025. const int gh_mv = il ? 12 : 0;
  2026. const int gh_bk = il ? 0 : 4;
  2027. for (int i = 0; i < 8; i++) {
  2028. // extract the 5-th bits for x0 and x1
  2029. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2030. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2031. // combine the 4-bits from qs with the 5th bit
  2032. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2033. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2034. reg[i/2][2*(i%2)+0] = d * x0 + md;
  2035. reg[i/2][2*(i%2)+1] = d * x1 + md;
  2036. }
  2037. }
  2038. template <typename type4x4>
  2039. void dequantize_q5_1(device const block_q5_1 *xb, short il, thread type4x4 & reg) {
  2040. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  2041. const float d = xb->d;
  2042. const float m = xb->m;
  2043. const ushort mask = il ? 0x00F0 : 0x000F;
  2044. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2045. const int x_mv = il ? 4 : 0;
  2046. const int gh_mv = il ? 12 : 0;
  2047. const int gh_bk = il ? 0 : 4;
  2048. for (int i = 0; i < 8; i++) {
  2049. // extract the 5-th bits for x0 and x1
  2050. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2051. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2052. // combine the 4-bits from qs with the 5th bit
  2053. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2054. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2055. reg[i/2][2*(i%2)+0] = d * x0 + m;
  2056. reg[i/2][2*(i%2)+1] = d * x1 + m;
  2057. }
  2058. }
  2059. template <typename type4x4>
  2060. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  2061. device const int8_t * qs = ((device const int8_t *)xb->qs);
  2062. const half d = xb->d;
  2063. for (int i=0;i<16;i++) {
  2064. reg[i/4][i%4] = (qs[i + 16*il] * d);
  2065. }
  2066. }
  2067. template <typename type4x4>
  2068. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  2069. const half d = xb->d;
  2070. const half min = xb->dmin;
  2071. device const uint8_t * q = (device const uint8_t *)xb->qs;
  2072. half dl, ml;
  2073. uint8_t sc = xb->scales[il];
  2074. #if QK_K == 256
  2075. q = q + 32*(il/8) + 16*(il&1);
  2076. il = (il/2)%4;
  2077. #endif
  2078. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2079. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2080. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  2081. for (int i = 0; i < 16; ++i) {
  2082. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2083. }
  2084. }
  2085. template <typename type4x4>
  2086. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  2087. const half d_all = xb->d;
  2088. device const uint8_t * q = (device const uint8_t *)xb->qs;
  2089. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  2090. device const int8_t * scales = (device const int8_t *)xb->scales;
  2091. #if QK_K == 256
  2092. q = q + 32 * (il/8) + 16 * (il&1);
  2093. h = h + 16 * (il&1);
  2094. uint8_t m = 1 << (il/2);
  2095. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  2096. ((il/4)>0 ? 12 : 3);
  2097. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  2098. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  2099. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  2100. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  2101. half dl = il<8 ? d_all * (dl_int - 32.h) : d_all * (dl_int / 16.h - 32.h);
  2102. const half ml = 4.h * dl;
  2103. il = (il/2) & 3;
  2104. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2105. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2106. dl *= coef;
  2107. for (int i = 0; i < 16; ++i) {
  2108. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  2109. }
  2110. #else
  2111. float kcoef = il&1 ? 1.f/16.f : 1.f;
  2112. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  2113. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  2114. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2115. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2116. uint8_t m = 1<<(il*2);
  2117. for (int i = 0; i < 16; ++i) {
  2118. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  2119. }
  2120. #endif
  2121. }
  2122. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  2123. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  2124. : 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))};
  2125. }
  2126. template <typename type4x4>
  2127. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  2128. device const uchar * q = xb->qs;
  2129. #if QK_K == 256
  2130. short is = (il/4) * 2;
  2131. q = q + (il/4) * 32 + 16 * (il&1);
  2132. il = il & 3;
  2133. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2134. const half d = il < 2 ? xb->d : xb->d / 16.h;
  2135. const half min = xb->dmin;
  2136. const half dl = d * sc[0];
  2137. const half ml = min * sc[1];
  2138. #else
  2139. q = q + 16 * (il&1);
  2140. device const uint8_t * s = xb->scales;
  2141. device const half2 * dh = (device const half2 *)xb->d;
  2142. const float2 d = (float2)dh[0];
  2143. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  2144. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  2145. #endif
  2146. const ushort mask = il<2 ? 0x0F : 0xF0;
  2147. for (int i = 0; i < 16; ++i) {
  2148. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2149. }
  2150. }
  2151. template <typename type4x4>
  2152. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  2153. device const uint8_t * q = xb->qs;
  2154. device const uint8_t * qh = xb->qh;
  2155. #if QK_K == 256
  2156. short is = (il/4) * 2;
  2157. q = q + 32 * (il/4) + 16 * (il&1);
  2158. qh = qh + 16 * (il&1);
  2159. uint8_t ul = 1 << (il/2);
  2160. il = il & 3;
  2161. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2162. const half d = il < 2 ? xb->d : xb->d / 16.h;
  2163. const half min = xb->dmin;
  2164. const half dl = d * sc[0];
  2165. const half ml = min * sc[1];
  2166. const ushort mask = il<2 ? 0x0F : 0xF0;
  2167. const half qh_val = il<2 ? 16.h : 256.h;
  2168. for (int i = 0; i < 16; ++i) {
  2169. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  2170. }
  2171. #else
  2172. q = q + 16 * (il&1);
  2173. device const int8_t * s = xb->scales;
  2174. const float dl = xb->d * s[il];
  2175. uint8_t m = 1<<(il*2);
  2176. const float coef = il<2 ? 1.f : 1.f/16.f;
  2177. const ushort mask = il<2 ? 0x0F : 0xF0;
  2178. for (int i = 0; i < 16; ++i) {
  2179. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  2180. }
  2181. #endif
  2182. }
  2183. template <typename type4x4>
  2184. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  2185. const half d_all = xb->d;
  2186. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  2187. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  2188. device const int8_t * scales = (device const int8_t *)xb->scales;
  2189. #if QK_K == 256
  2190. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  2191. qh = qh + 32*(il/8) + 16*(il&1);
  2192. half sc = scales[(il%2) + 2 * ((il/2))];
  2193. il = (il/2) & 3;
  2194. #else
  2195. ql = ql + 16 * (il&1);
  2196. half sc = scales[il];
  2197. #endif
  2198. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2199. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  2200. const half coef = il>1 ? 1.f/16.h : 1.h;
  2201. const half ml = d_all * sc * 32.h;
  2202. const half dl = d_all * sc * coef;
  2203. for (int i = 0; i < 16; ++i) {
  2204. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  2205. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  2206. reg[i/4][i%4] = dl * q - ml;
  2207. }
  2208. }
  2209. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  2210. kernel void kernel_get_rows(
  2211. device const void * src0,
  2212. device const int * src1,
  2213. device float * dst,
  2214. constant int64_t & ne00,
  2215. constant uint64_t & nb01,
  2216. constant uint64_t & nb1,
  2217. uint tgpig[[threadgroup_position_in_grid]],
  2218. uint tiitg[[thread_index_in_threadgroup]],
  2219. uint tptg[[threads_per_threadgroup]]) {
  2220. const int i = tgpig;
  2221. const int r = ((device int32_t *) src1)[i];
  2222. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  2223. float4x4 temp;
  2224. dequantize_func(
  2225. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  2226. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  2227. }
  2228. }
  2229. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  2230. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  2231. #define BLOCK_SIZE_K 32
  2232. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  2233. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  2234. #define THREAD_PER_BLOCK 128
  2235. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  2236. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  2237. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  2238. #define SG_MAT_ROW 8
  2239. // each block_q contains 16*nl weights
  2240. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  2241. kernel void kernel_mul_mm(device const uchar * src0,
  2242. device const uchar * src1,
  2243. device float * dst,
  2244. constant int64_t & ne00,
  2245. constant int64_t & ne02,
  2246. constant int64_t & nb01,
  2247. constant int64_t & nb02,
  2248. constant int64_t & ne12,
  2249. constant int64_t & nb10,
  2250. constant int64_t & nb11,
  2251. constant int64_t & nb12,
  2252. constant int64_t & ne0,
  2253. constant int64_t & ne1,
  2254. constant uint & gqa,
  2255. threadgroup uchar * shared_memory [[threadgroup(0)]],
  2256. uint3 tgpig[[threadgroup_position_in_grid]],
  2257. uint tiitg[[thread_index_in_threadgroup]],
  2258. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2259. threadgroup half * sa = (threadgroup half *)(shared_memory);
  2260. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  2261. const uint r0 = tgpig.y;
  2262. const uint r1 = tgpig.x;
  2263. const uint im = tgpig.z;
  2264. // if this block is of 64x32 shape or smaller
  2265. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  2266. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  2267. // a thread shouldn't load data outside of the matrix
  2268. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  2269. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  2270. simdgroup_half8x8 ma[4];
  2271. simdgroup_float8x8 mb[2];
  2272. simdgroup_float8x8 c_res[8];
  2273. for (int i = 0; i < 8; i++){
  2274. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  2275. }
  2276. short il = (tiitg % THREAD_PER_ROW);
  2277. uint offset0 = im/gqa*nb02;
  2278. ushort offset1 = il/nl;
  2279. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  2280. device const float * y = (device const float *)(src1
  2281. + nb12 * im
  2282. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  2283. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  2284. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  2285. // load data and store to threadgroup memory
  2286. half4x4 temp_a;
  2287. dequantize_func(x, il, temp_a);
  2288. threadgroup_barrier(mem_flags::mem_threadgroup);
  2289. #pragma unroll(16)
  2290. for (int i = 0; i < 16; i++) {
  2291. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  2292. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  2293. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  2294. }
  2295. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  2296. il = (il + 2 < nl) ? il + 2 : il % 2;
  2297. x = (il < 2) ? x + (2+nl-1)/nl : x;
  2298. y += BLOCK_SIZE_K;
  2299. threadgroup_barrier(mem_flags::mem_threadgroup);
  2300. // load matrices from threadgroup memory and conduct outer products
  2301. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  2302. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  2303. #pragma unroll(4)
  2304. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  2305. #pragma unroll(4)
  2306. for (int i = 0; i < 4; i++) {
  2307. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  2308. }
  2309. simdgroup_barrier(mem_flags::mem_none);
  2310. #pragma unroll(2)
  2311. for (int i = 0; i < 2; i++) {
  2312. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  2313. }
  2314. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  2315. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  2316. #pragma unroll(8)
  2317. for (int i = 0; i < 8; i++){
  2318. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  2319. }
  2320. }
  2321. }
  2322. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  2323. device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
  2324. + (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
  2325. for (int i = 0; i < 8; i++) {
  2326. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  2327. }
  2328. } else {
  2329. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  2330. threadgroup_barrier(mem_flags::mem_threadgroup);
  2331. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  2332. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  2333. for (int i = 0; i < 8; i++) {
  2334. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  2335. }
  2336. threadgroup_barrier(mem_flags::mem_threadgroup);
  2337. device float * C = dst + (BLOCK_SIZE_M * r0) + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  2338. if (sgitg == 0) {
  2339. for (int i = 0; i < n_rows; i++) {
  2340. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  2341. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  2342. }
  2343. }
  2344. }
  2345. }
  2346. }
  2347. #if QK_K == 256
  2348. #define QK_NL 16
  2349. #else
  2350. #define QK_NL 4
  2351. #endif
  2352. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  2353. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  2354. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  2355. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  2356. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  2357. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  2358. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_t kernel_get_rows<block_q5_0, 2, dequantize_q5_0>;
  2359. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_t kernel_get_rows<block_q5_1, 2, dequantize_q5_1>;
  2360. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  2361. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  2362. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  2363. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  2364. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  2365. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  2366. typedef void (mat_mm_t)(
  2367. device const uchar * src0,
  2368. device const uchar * src1,
  2369. device float * dst,
  2370. constant int64_t & ne00,
  2371. constant int64_t & ne02,
  2372. constant int64_t & nb01,
  2373. constant int64_t & nb02,
  2374. constant int64_t & ne12,
  2375. constant int64_t & nb10,
  2376. constant int64_t & nb11,
  2377. constant int64_t & nb12,
  2378. constant int64_t & ne0,
  2379. constant int64_t & ne1,
  2380. constant uint & gqa,
  2381. threadgroup uchar *, uint3, uint, uint);
  2382. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  2383. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  2384. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  2385. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  2386. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_0, 2, dequantize_q5_0>;
  2387. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_1, 2, dequantize_q5_1>;
  2388. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  2389. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  2390. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  2391. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  2392. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  2393. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;