ggml-metal.metal 111 KB

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