ggml-metal.metal 98 KB

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