ggml-metal.metal 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  4. #define QK4_0 32
  5. #define QR4_0 2
  6. typedef struct {
  7. half d; // delta
  8. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  9. } block_q4_0;
  10. #define QK4_1 32
  11. typedef struct {
  12. half d; // delta
  13. half m; // min
  14. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  15. } block_q4_1;
  16. #define QK8_0 32
  17. typedef struct {
  18. half d; // delta
  19. int8_t qs[QK8_0]; // quants
  20. } block_q8_0;
  21. kernel void kernel_add(
  22. device const float4 * src0,
  23. device const float4 * src1,
  24. device float4 * dst,
  25. uint tpig[[thread_position_in_grid]]) {
  26. dst[tpig] = src0[tpig] + src1[tpig];
  27. }
  28. // assumption: src1 is a row
  29. // broadcast src1 into src0
  30. kernel void kernel_add_row(
  31. device const float4 * src0,
  32. device const float4 * src1,
  33. device float4 * dst,
  34. constant int64_t & nb,
  35. uint tpig[[thread_position_in_grid]]) {
  36. dst[tpig] = src0[tpig] + src1[tpig % nb];
  37. }
  38. kernel void kernel_mul(
  39. device const float4 * src0,
  40. device const float4 * src1,
  41. device float4 * dst,
  42. uint tpig[[thread_position_in_grid]]) {
  43. dst[tpig] = src0[tpig] * src1[tpig];
  44. }
  45. // assumption: src1 is a row
  46. // broadcast src1 into src0
  47. kernel void kernel_mul_row(
  48. device const float4 * src0,
  49. device const float4 * src1,
  50. device float4 * dst,
  51. constant int64_t & nb,
  52. uint tpig[[thread_position_in_grid]]) {
  53. dst[tpig] = src0[tpig] * src1[tpig % nb];
  54. }
  55. kernel void kernel_scale(
  56. device const float * src0,
  57. device float * dst,
  58. constant float & scale,
  59. uint tpig[[thread_position_in_grid]]) {
  60. dst[tpig] = src0[tpig] * scale;
  61. }
  62. kernel void kernel_silu(
  63. device const float * src0,
  64. device float * dst,
  65. uint tpig[[thread_position_in_grid]]) {
  66. float x = src0[tpig];
  67. dst[tpig] = x / (1.0f + exp(-x));
  68. }
  69. kernel void kernel_relu(
  70. device const float * src0,
  71. device float * dst,
  72. uint tpig[[thread_position_in_grid]]) {
  73. dst[tpig] = max(0.0f, src0[tpig]);
  74. }
  75. constant float GELU_COEF_A = 0.044715f;
  76. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  77. kernel void kernel_gelu(
  78. device const float * src0,
  79. device float * dst,
  80. uint tpig[[thread_position_in_grid]]) {
  81. float x = src0[tpig];
  82. // BEWARE !!!
  83. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  84. // This was observed with Falcon 7B and 40B models
  85. //
  86. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  87. }
  88. kernel void kernel_soft_max(
  89. device const float * src0,
  90. device float * dst,
  91. constant int64_t & ne00,
  92. constant int64_t & ne01,
  93. constant int64_t & ne02,
  94. threadgroup float * buf [[threadgroup(0)]],
  95. uint3 tgpig[[threadgroup_position_in_grid]],
  96. uint3 tpitg[[thread_position_in_threadgroup]],
  97. uint3 ntg[[threads_per_threadgroup]]) {
  98. const int64_t i03 = tgpig[2];
  99. const int64_t i02 = tgpig[1];
  100. const int64_t i01 = tgpig[0];
  101. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  102. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  103. // parallel max
  104. buf[tpitg[0]] = -INFINITY;
  105. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  106. buf[tpitg[0]] = MAX(buf[tpitg[0]], psrc0[i00]);
  107. }
  108. // reduce
  109. threadgroup_barrier(mem_flags::mem_threadgroup);
  110. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  111. if (tpitg[0] < i) {
  112. buf[tpitg[0]] = MAX(buf[tpitg[0]], buf[tpitg[0] + i]);
  113. }
  114. threadgroup_barrier(mem_flags::mem_threadgroup);
  115. }
  116. //// broadcast - not needed. There is a threadgroup barrier above in the last iteration of
  117. // the loop, and when that is done, buf[0] has the correct (synchronized) value
  118. //if (tpitg[0] == 0) {
  119. // buf[0] = buf[0];
  120. //}
  121. //threadgroup_barrier(mem_flags::mem_threadgroup);
  122. const float max = buf[0];
  123. // parallel sum
  124. buf[tpitg[0]] = 0.0f;
  125. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  126. const float exp_psrc0 = exp(psrc0[i00] - max);
  127. buf[tpitg[0]] += exp_psrc0;
  128. // Remember the result of exp here. exp is expensive, so we really do not
  129. // whish to compute it twice.
  130. pdst[i00] = exp_psrc0;
  131. }
  132. // reduce
  133. threadgroup_barrier(mem_flags::mem_threadgroup);
  134. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  135. if (tpitg[0] < i) {
  136. buf[tpitg[0]] += buf[tpitg[0] + i];
  137. }
  138. threadgroup_barrier(mem_flags::mem_threadgroup);
  139. }
  140. // broadcast - not needed, see above
  141. //// broadcast
  142. //if (tpitg[0] == 0) {
  143. // buf[0] = buf[0];
  144. //}
  145. //threadgroup_barrier(mem_flags::mem_threadgroup);
  146. const float sum = buf[0];
  147. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  148. pdst[i00] /= sum;
  149. }
  150. }
  151. kernel void kernel_diag_mask_inf(
  152. device const float * src0,
  153. device float * dst,
  154. constant int64_t & ne00,
  155. constant int64_t & ne01,
  156. constant int & n_past,
  157. uint3 tpig[[thread_position_in_grid]]) {
  158. const int64_t i02 = tpig[2];
  159. const int64_t i01 = tpig[1];
  160. const int64_t i00 = tpig[0];
  161. if (i00 > n_past + i01) {
  162. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  163. } else {
  164. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  165. }
  166. }
  167. kernel void kernel_norm(
  168. device const void * src0,
  169. device float * dst,
  170. constant int64_t & ne00,
  171. constant uint64_t & nb01,
  172. constant float & eps,
  173. threadgroup float * sum [[threadgroup(0)]],
  174. uint tgpig[[threadgroup_position_in_grid]],
  175. uint tpitg[[thread_position_in_threadgroup]],
  176. uint ntg[[threads_per_threadgroup]]) {
  177. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  178. // MEAN
  179. // parallel sum
  180. sum[tpitg] = 0.0f;
  181. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  182. sum[tpitg] += x[i00];
  183. }
  184. // reduce
  185. threadgroup_barrier(mem_flags::mem_threadgroup);
  186. for (uint i = ntg/2; i > 0; i /= 2) {
  187. if (tpitg < i) {
  188. sum[tpitg] += sum[tpitg + i];
  189. }
  190. threadgroup_barrier(mem_flags::mem_threadgroup);
  191. }
  192. //// broadcast
  193. //if (tpitg == 0) {
  194. // sum[0] /= ne00;
  195. //}
  196. //threadgroup_barrier(mem_flags::mem_threadgroup);
  197. const float mean = sum[0];
  198. // recenter and VARIANCE
  199. device float * y = dst + tgpig*ne00;
  200. sum[tpitg] = 0.0f;
  201. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  202. y[i00] = x[i00] - mean;
  203. sum[tpitg] += y[i00] * y[i00];
  204. }
  205. //// VARIANCE
  206. //// parallel sum
  207. //sum[tpitg] = 0.0f;
  208. //for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  209. // sum[tpitg] += y[i00] * y[i00];
  210. //}
  211. // reduce
  212. threadgroup_barrier(mem_flags::mem_threadgroup);
  213. for (uint i = ntg/2; i > 0; i /= 2) {
  214. if (tpitg < i) {
  215. sum[tpitg] += sum[tpitg + i];
  216. }
  217. threadgroup_barrier(mem_flags::mem_threadgroup);
  218. }
  219. //// broadcast
  220. //if (tpitg == 0) {
  221. // sum[0] /= ne00;
  222. //}
  223. //threadgroup_barrier(mem_flags::mem_threadgroup);
  224. const float variance = sum[0];
  225. const float scale = 1.0f/sqrt(variance + eps);
  226. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  227. y[i00] = y[i00] * scale;
  228. }
  229. }
  230. kernel void kernel_rms_norm(
  231. device const void * src0,
  232. device float * dst,
  233. constant int64_t & ne00,
  234. constant uint64_t & nb01,
  235. constant float & eps,
  236. threadgroup float * sum [[threadgroup(0)]],
  237. uint tgpig[[threadgroup_position_in_grid]],
  238. uint tpitg[[thread_position_in_threadgroup]],
  239. uint sgitg[[simdgroup_index_in_threadgroup]],
  240. uint tiisg[[thread_index_in_simdgroup]],
  241. uint ntg[[threads_per_threadgroup]]) {
  242. device const float4 * x = (device const float4 *) ((device const char *) src0 + tgpig*nb01);
  243. device const float * x_scalar = (device const float *) x;
  244. float4 sumf=0;
  245. float all_sum=0;
  246. // parallel sum
  247. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  248. sumf += x[i00] * x[i00];
  249. }
  250. all_sum = sumf[0] + sumf[1] + sumf[2] + sumf[3];
  251. all_sum = simd_sum(all_sum);
  252. if (tiisg == 0) {
  253. sum[sgitg] = all_sum;
  254. }
  255. threadgroup_barrier(mem_flags::mem_threadgroup);
  256. // broadcast, simd group number is ntg / 32
  257. for (uint i = ntg / 32 / 2; i > 0; i /= 2) {
  258. if (tpitg < i) {
  259. sum[tpitg] += sum[tpitg + i];
  260. }
  261. }
  262. if (tpitg == 0) {
  263. for (int i = 4 * (ne00 / 4); i < ne00; i++) {sum[0] += x_scalar[i];}
  264. sum[0] /= ne00;
  265. }
  266. threadgroup_barrier(mem_flags::mem_threadgroup);
  267. const float mean = sum[0];
  268. const float scale = 1.0f/sqrt(mean + eps);
  269. device float4 * y = (device float4 *) (dst + tgpig*ne00);
  270. device float * y_scalar = (device float *) y;
  271. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  272. y[i00] = x[i00] * scale;
  273. }
  274. if (tpitg == 0) {
  275. for (int i00 = 4 * (ne00 / 4); i00 < ne00; i00++) {y_scalar[i00] = x_scalar[i00] * scale;}
  276. }
  277. }
  278. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  279. // il indicates where the q4 quants begin (0 or QK4_0/4)
  280. // we assume that the yl's have been multiplied with the appropriate scale factor
  281. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  282. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  283. float d = qb_curr->d;
  284. float2 acc = 0.f;
  285. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 1 + il/2);
  286. for (int i = 0; i < 8; i+=2) {
  287. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  288. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  289. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  290. + yl[i + 9] * (qs[i / 2] & 0xF000);
  291. }
  292. return d * (sumy * -8.f + acc[0] + acc[1]);
  293. }
  294. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  295. // il indicates where the q4 quants begin (0 or QK4_0/4)
  296. // we assume that the yl's have been multiplied with the appropriate scale factor
  297. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  298. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  299. float d = qb_curr->d;
  300. float m = qb_curr->m;
  301. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 2 + il/2);
  302. float2 acc = 0.f;
  303. for (int i = 0; i < 8; i+=2) {
  304. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  305. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  306. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  307. + yl[i + 9] * (qs[i / 2] & 0xF000);
  308. }
  309. return d * (acc[0] + acc[1]) + sumy * m;
  310. }
  311. // putting them in the kernel cause a significant performance penalty
  312. #define N_DST 4 // each SIMD group works on 4 rows
  313. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  314. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  315. //Note: This is a template, but strictly speaking it only applies to
  316. // quantizations where the block size is 32. It also does not
  317. // giard against the number of rows not being divisible by
  318. // N_DST, so this is another explicit assumption of the implementation.
  319. template<typename block_q_type, int nr, int nsg, int nw>
  320. void mul_vec_q_n_f32(device const void * src0, device const float * src1, device float * dst,
  321. int64_t ne00, int64_t ne01, int64_t ne02, int64_t ne10, int64_t ne12, int64_t ne0, int64_t ne1, uint gqa,
  322. uint3 tgpig, uint tiisg, uint sgitg) {
  323. const int nb = ne00/QK4_0;
  324. const int r0 = tgpig.x;
  325. const int r1 = tgpig.y;
  326. const int im = tgpig.z;
  327. const int first_row = (r0 * nsg + sgitg) * nr;
  328. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  329. device const block_q_type * x = (device const block_q_type *) src0 + offset0;
  330. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  331. float yl[16]; // src1 vector cache
  332. float sumf[nr]={0.f};
  333. const int ix = tiisg/2;
  334. const int il = 8*(tiisg%2);
  335. device const float * yb = y + ix * QK4_0 + il;
  336. // each thread in a SIMD group deals with half a block.
  337. for (int ib = ix; ib < nb; ib += nw/2) {
  338. float sumy = 0;
  339. for (int i = 0; i < 8; i += 2) {
  340. sumy += yb[i] + yb[i+1];
  341. yl[i+0] = yb[i+ 0];
  342. yl[i+1] = yb[i+ 1]/256.f;
  343. sumy += yb[i+16] + yb[i+17];
  344. yl[i+8] = yb[i+16]/16.f;
  345. yl[i+9] = yb[i+17]/4096.f;
  346. }
  347. for (int row = 0; row < nr; row++) {
  348. sumf[row] += block_q_n_dot_y(x+ib+row*nb, sumy, yl, il);
  349. }
  350. yb += QK4_0 * 16;
  351. }
  352. for (int row = 0; row < nr; ++row) {
  353. const float tot = simd_sum(sumf[row]);
  354. if (tiisg == 0 && first_row + row < ne01) {
  355. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  356. }
  357. }
  358. }
  359. kernel void kernel_mul_mat_q4_0_f32(
  360. device const void * src0,
  361. device const float * src1,
  362. device float * dst,
  363. constant int64_t & ne00,
  364. constant int64_t & ne01[[buffer(4)]],
  365. constant int64_t & ne02[[buffer(5)]],
  366. constant int64_t & ne10[[buffer(9)]],
  367. constant int64_t & ne12[[buffer(11)]],
  368. constant int64_t & ne0[[buffer(15)]],
  369. constant int64_t & ne1[[buffer(16)]],
  370. constant uint & gqa[[buffer(17)]],
  371. uint3 tgpig[[threadgroup_position_in_grid]],
  372. uint tiisg[[thread_index_in_simdgroup]],
  373. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  374. 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);
  375. }
  376. kernel void kernel_mul_mat_q4_1_f32(
  377. device const void * src0,
  378. device const float * src1,
  379. device float * dst,
  380. constant int64_t & ne00,
  381. constant int64_t & ne01[[buffer(4)]],
  382. constant int64_t & ne02[[buffer(5)]],
  383. constant int64_t & ne10[[buffer(9)]],
  384. constant int64_t & ne12[[buffer(11)]],
  385. constant int64_t & ne0[[buffer(15)]],
  386. constant int64_t & ne1[[buffer(16)]],
  387. constant uint & gqa[[buffer(17)]],
  388. uint3 tgpig[[threadgroup_position_in_grid]],
  389. uint tiisg[[thread_index_in_simdgroup]],
  390. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  391. 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);
  392. }
  393. #define NB_Q8_0 8
  394. kernel void kernel_mul_mat_q8_0_f32(
  395. device const void * src0,
  396. device const float * src1,
  397. device float * dst,
  398. constant int64_t & ne00,
  399. constant int64_t & ne01[[buffer(4)]],
  400. constant int64_t & ne02[[buffer(5)]],
  401. constant int64_t & ne10[[buffer(9)]],
  402. constant int64_t & ne12[[buffer(11)]],
  403. constant int64_t & ne0[[buffer(15)]],
  404. constant int64_t & ne1[[buffer(16)]],
  405. constant uint & gqa[[buffer(17)]],
  406. uint3 tgpig[[threadgroup_position_in_grid]],
  407. uint tiisg[[thread_index_in_simdgroup]],
  408. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  409. const int nr = N_DST;
  410. const int nsg = N_SIMDGROUP;
  411. const int nw = N_SIMDWIDTH;
  412. const int nb = ne00/QK8_0;
  413. const int r0 = tgpig.x;
  414. const int r1 = tgpig.y;
  415. const int im = tgpig.z;
  416. const int first_row = (r0 * nsg + sgitg) * nr;
  417. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  418. device const block_q8_0 * x = (device const block_q8_0 *) src0 + offset0;
  419. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  420. float yl[NB_Q8_0];
  421. float sumf[nr]={0.f};
  422. const int ix = tiisg/4;
  423. const int il = tiisg%4;
  424. device const float * yb = y + ix * QK8_0 + NB_Q8_0*il;
  425. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  426. for (int ib = ix; ib < nb; ib += nw/4) {
  427. for (int i = 0; i < NB_Q8_0; ++i) {
  428. yl[i] = yb[i];
  429. }
  430. for (int row = 0; row < nr; row++) {
  431. device const int8_t * qs = x[ib+row*nb].qs + NB_Q8_0*il;
  432. float sumq = 0.f;
  433. for (int iq = 0; iq < NB_Q8_0; ++iq) {
  434. sumq += qs[iq] * yl[iq];
  435. }
  436. sumf[row] += sumq*x[ib+row*nb].d;
  437. }
  438. yb += NB_Q8_0 * nw;
  439. }
  440. for (int row = 0; row < nr; ++row) {
  441. const float tot = simd_sum(sumf[row]);
  442. if (tiisg == 0 && first_row + row < ne01) {
  443. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  444. }
  445. }
  446. }
  447. kernel void kernel_mul_mat_f16_f32_1row(
  448. device const char * src0,
  449. device const char * src1,
  450. device float * dst,
  451. constant int64_t & ne00,
  452. constant int64_t & ne01,
  453. constant int64_t & ne02,
  454. constant uint64_t & nb00,
  455. constant uint64_t & nb01,
  456. constant uint64_t & nb02,
  457. constant int64_t & ne10,
  458. constant int64_t & ne11,
  459. constant int64_t & ne12,
  460. constant uint64_t & nb10,
  461. constant uint64_t & nb11,
  462. constant uint64_t & nb12,
  463. constant int64_t & ne0,
  464. constant int64_t & ne1,
  465. uint3 tgpig[[threadgroup_position_in_grid]],
  466. uint tiisg[[thread_index_in_simdgroup]]) {
  467. const int64_t r0 = tgpig.x;
  468. const int64_t r1 = tgpig.y;
  469. const int64_t im = tgpig.z;
  470. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  471. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  472. float sumf = 0;
  473. if (ne00 < 128) {
  474. for (int i = tiisg; i < ne00; i += 32) {
  475. sumf += (float) x[i] * (float) y[i];
  476. }
  477. float all_sum = simd_sum(sumf);
  478. if (tiisg == 0) {
  479. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  480. }
  481. } else {
  482. device const half4 * x4 = (device const half4 *) x;
  483. device const float4 * y4 = (device const float4 *) y;
  484. for (int i = tiisg; i < ne00/4; i += 32) {
  485. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  486. }
  487. float all_sum = simd_sum(sumf);
  488. if (tiisg == 0) {
  489. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  490. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  491. }
  492. }
  493. }
  494. #define N_F16_F32 4
  495. kernel void kernel_mul_mat_f16_f32(
  496. device const char * src0,
  497. device const char * src1,
  498. device float * dst,
  499. constant int64_t & ne00,
  500. constant int64_t & ne01,
  501. constant int64_t & ne02,
  502. constant uint64_t & nb00,
  503. constant uint64_t & nb01,
  504. constant uint64_t & nb02,
  505. constant int64_t & ne10,
  506. constant int64_t & ne11,
  507. constant int64_t & ne12,
  508. constant uint64_t & nb10,
  509. constant uint64_t & nb11,
  510. constant uint64_t & nb12,
  511. constant int64_t & ne0,
  512. constant int64_t & ne1,
  513. uint3 tgpig[[threadgroup_position_in_grid]],
  514. uint tiisg[[thread_index_in_simdgroup]]) {
  515. const int64_t r0 = tgpig.x;
  516. const int64_t rb = tgpig.y*N_F16_F32;
  517. const int64_t im = tgpig.z;
  518. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  519. if (ne00 < 128) {
  520. for (int row = 0; row < N_F16_F32; ++row) {
  521. int r1 = rb + row;
  522. if (r1 >= ne11) {
  523. break;
  524. }
  525. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  526. float sumf = 0;
  527. for (int i = tiisg; i < ne00; i += 32) {
  528. sumf += (float) x[i] * (float) y[i];
  529. }
  530. float all_sum = simd_sum(sumf);
  531. if (tiisg == 0) {
  532. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  533. }
  534. }
  535. } else {
  536. device const half4 * x4 = (device const half4 *)x;
  537. for (int row = 0; row < N_F16_F32; ++row) {
  538. int r1 = rb + row;
  539. if (r1 >= ne11) {
  540. break;
  541. }
  542. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  543. device const float4 * y4 = (device const float4 *) y;
  544. float sumf = 0;
  545. for (int i = tiisg; i < ne00/4; i += 32) {
  546. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  547. }
  548. float all_sum = simd_sum(sumf);
  549. if (tiisg == 0) {
  550. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  551. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  552. }
  553. }
  554. }
  555. }
  556. kernel void kernel_alibi_f32(
  557. device const float * src0,
  558. device float * dst,
  559. constant int64_t & ne00,
  560. constant int64_t & ne01,
  561. constant int64_t & ne02,
  562. constant int64_t & ne03,
  563. constant uint64_t & nb00,
  564. constant uint64_t & nb01,
  565. constant uint64_t & nb02,
  566. constant uint64_t & nb03,
  567. constant int64_t & ne0,
  568. constant int64_t & ne1,
  569. constant int64_t & ne2,
  570. constant int64_t & ne3,
  571. constant uint64_t & nb0,
  572. constant uint64_t & nb1,
  573. constant uint64_t & nb2,
  574. constant uint64_t & nb3,
  575. constant float & m0,
  576. uint3 tgpig[[threadgroup_position_in_grid]],
  577. uint3 tpitg[[thread_position_in_threadgroup]],
  578. uint3 ntg[[threads_per_threadgroup]]) {
  579. const int64_t i03 = tgpig[2];
  580. const int64_t i02 = tgpig[1];
  581. const int64_t i01 = tgpig[0];
  582. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  583. const int64_t i3 = n / (ne2*ne1*ne0);
  584. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  585. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  586. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  587. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  588. float m_k = pow(m0, i2 + 1);
  589. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  590. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  591. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  592. }
  593. }
  594. kernel void kernel_rope(
  595. device const void * src0,
  596. device float * dst,
  597. constant int64_t & ne00,
  598. constant int64_t & ne01,
  599. constant int64_t & ne02,
  600. constant int64_t & ne03,
  601. constant uint64_t & nb00,
  602. constant uint64_t & nb01,
  603. constant uint64_t & nb02,
  604. constant uint64_t & nb03,
  605. constant int64_t & ne0,
  606. constant int64_t & ne1,
  607. constant int64_t & ne2,
  608. constant int64_t & ne3,
  609. constant uint64_t & nb0,
  610. constant uint64_t & nb1,
  611. constant uint64_t & nb2,
  612. constant uint64_t & nb3,
  613. constant int & n_past,
  614. constant int & n_dims,
  615. constant int & mode,
  616. constant float & freq_base,
  617. constant float & freq_scale,
  618. uint3 tpig[[thread_position_in_grid]]) {
  619. const int64_t i3 = tpig[2];
  620. const int64_t i2 = tpig[1];
  621. const int64_t i1 = tpig[0];
  622. const bool is_neox = mode & 2;
  623. const float theta_scale = pow(freq_base, -2.0f/n_dims);
  624. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  625. float theta = freq_scale * (float)p;
  626. if (!is_neox) {
  627. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  628. const float cos_theta = cos(theta);
  629. const float sin_theta = sin(theta);
  630. theta *= theta_scale;
  631. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  632. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  633. const float x0 = src[0];
  634. const float x1 = src[1];
  635. dst_data[0] = x0*cos_theta - x1*sin_theta;
  636. dst_data[1] = x0*sin_theta + x1*cos_theta;
  637. }
  638. } else {
  639. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  640. for (int64_t ic = 0; ic < n_dims; ic += 2) {
  641. const float cos_theta = cos(theta);
  642. const float sin_theta = sin(theta);
  643. theta *= theta_scale;
  644. const int64_t i0 = ib*n_dims + ic/2;
  645. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  646. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  647. const float x0 = src[0];
  648. const float x1 = src[n_dims/2];
  649. dst_data[0] = x0*cos_theta - x1*sin_theta;
  650. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  651. }
  652. }
  653. }
  654. }
  655. kernel void kernel_cpy_f16_f16(
  656. device const half * src0,
  657. device half * dst,
  658. constant int64_t & ne00,
  659. constant int64_t & ne01,
  660. constant int64_t & ne02,
  661. constant int64_t & ne03,
  662. constant uint64_t & nb00,
  663. constant uint64_t & nb01,
  664. constant uint64_t & nb02,
  665. constant uint64_t & nb03,
  666. constant int64_t & ne0,
  667. constant int64_t & ne1,
  668. constant int64_t & ne2,
  669. constant int64_t & ne3,
  670. constant uint64_t & nb0,
  671. constant uint64_t & nb1,
  672. constant uint64_t & nb2,
  673. constant uint64_t & nb3,
  674. uint3 tgpig[[threadgroup_position_in_grid]],
  675. uint3 tpitg[[thread_position_in_threadgroup]],
  676. uint3 ntg[[threads_per_threadgroup]]) {
  677. const int64_t i03 = tgpig[2];
  678. const int64_t i02 = tgpig[1];
  679. const int64_t i01 = tgpig[0];
  680. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  681. const int64_t i3 = n / (ne2*ne1*ne0);
  682. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  683. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  684. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  685. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  686. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  687. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  688. dst_data[i00] = src[0];
  689. }
  690. }
  691. kernel void kernel_cpy_f32_f16(
  692. device const float * src0,
  693. device half * dst,
  694. constant int64_t & ne00,
  695. constant int64_t & ne01,
  696. constant int64_t & ne02,
  697. constant int64_t & ne03,
  698. constant uint64_t & nb00,
  699. constant uint64_t & nb01,
  700. constant uint64_t & nb02,
  701. constant uint64_t & nb03,
  702. constant int64_t & ne0,
  703. constant int64_t & ne1,
  704. constant int64_t & ne2,
  705. constant int64_t & ne3,
  706. constant uint64_t & nb0,
  707. constant uint64_t & nb1,
  708. constant uint64_t & nb2,
  709. constant uint64_t & nb3,
  710. uint3 tgpig[[threadgroup_position_in_grid]],
  711. uint3 tpitg[[thread_position_in_threadgroup]],
  712. uint3 ntg[[threads_per_threadgroup]]) {
  713. const int64_t i03 = tgpig[2];
  714. const int64_t i02 = tgpig[1];
  715. const int64_t i01 = tgpig[0];
  716. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  717. const int64_t i3 = n / (ne2*ne1*ne0);
  718. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  719. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  720. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  721. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  722. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  723. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  724. dst_data[i00] = src[0];
  725. }
  726. }
  727. kernel void kernel_cpy_f32_f32(
  728. device const float * src0,
  729. device float * dst,
  730. constant int64_t & ne00,
  731. constant int64_t & ne01,
  732. constant int64_t & ne02,
  733. constant int64_t & ne03,
  734. constant uint64_t & nb00,
  735. constant uint64_t & nb01,
  736. constant uint64_t & nb02,
  737. constant uint64_t & nb03,
  738. constant int64_t & ne0,
  739. constant int64_t & ne1,
  740. constant int64_t & ne2,
  741. constant int64_t & ne3,
  742. constant uint64_t & nb0,
  743. constant uint64_t & nb1,
  744. constant uint64_t & nb2,
  745. constant uint64_t & nb3,
  746. uint3 tgpig[[threadgroup_position_in_grid]],
  747. uint3 tpitg[[thread_position_in_threadgroup]],
  748. uint3 ntg[[threads_per_threadgroup]]) {
  749. const int64_t i03 = tgpig[2];
  750. const int64_t i02 = tgpig[1];
  751. const int64_t i01 = tgpig[0];
  752. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  753. const int64_t i3 = n / (ne2*ne1*ne0);
  754. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  755. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  756. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  757. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  758. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  759. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  760. dst_data[i00] = src[0];
  761. }
  762. }
  763. //============================================ k-quants ======================================================
  764. #ifndef QK_K
  765. #define QK_K 256
  766. #else
  767. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  768. #endif
  769. #if QK_K == 256
  770. #define K_SCALE_SIZE 12
  771. #else
  772. #define K_SCALE_SIZE 4
  773. #endif
  774. typedef struct {
  775. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  776. uint8_t qs[QK_K/4]; // quants
  777. half d; // super-block scale for quantized scales
  778. half dmin; // super-block scale for quantized mins
  779. } block_q2_K;
  780. // 84 bytes / block
  781. typedef struct {
  782. uint8_t hmask[QK_K/8]; // quants - high bit
  783. uint8_t qs[QK_K/4]; // quants - low 2 bits
  784. #if QK_K == 64
  785. uint8_t scales[2];
  786. #else
  787. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  788. #endif
  789. half d; // super-block scale
  790. } block_q3_K;
  791. #if QK_K == 64
  792. typedef struct {
  793. half d[2]; // super-block scales/mins
  794. uint8_t scales[2];
  795. uint8_t qs[QK_K/2]; // 4-bit quants
  796. } block_q4_K;
  797. #else
  798. typedef struct {
  799. half d; // super-block scale for quantized scales
  800. half dmin; // super-block scale for quantized mins
  801. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  802. uint8_t qs[QK_K/2]; // 4--bit quants
  803. } block_q4_K;
  804. #endif
  805. #if QK_K == 64
  806. typedef struct {
  807. half d; // super-block scales/mins
  808. int8_t scales[QK_K/16]; // 8-bit block scales
  809. uint8_t qh[QK_K/8]; // quants, high bit
  810. uint8_t qs[QK_K/2]; // quants, low 4 bits
  811. } block_q5_K;
  812. #else
  813. typedef struct {
  814. half d; // super-block scale for quantized scales
  815. half dmin; // super-block scale for quantized mins
  816. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  817. uint8_t qh[QK_K/8]; // quants, high bit
  818. uint8_t qs[QK_K/2]; // quants, low 4 bits
  819. } block_q5_K;
  820. // 176 bytes / block
  821. #endif
  822. typedef struct {
  823. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  824. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  825. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  826. half d; // super-block scale
  827. } block_q6_K;
  828. // 210 bytes / block
  829. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  830. uchar4 r;
  831. if (j < 4) {
  832. r[0] = q[j+0] & 63;
  833. r[2] = q[j+1] & 63;
  834. r[1] = q[j+4] & 63;
  835. r[3] = q[j+5] & 63;
  836. } else {
  837. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  838. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  839. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  840. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  841. }
  842. return r;
  843. }
  844. //====================================== dot products =========================
  845. kernel void kernel_mul_mat_q2_K_f32(
  846. device const void * src0,
  847. device const float * src1,
  848. device float * dst,
  849. constant int64_t & ne00,
  850. constant int64_t & ne01[[buffer(4)]],
  851. constant int64_t & ne02[[buffer(5)]],
  852. constant int64_t & ne10[[buffer(9)]],
  853. constant int64_t & ne12[[buffer(11)]],
  854. constant int64_t & ne0[[buffer(15)]],
  855. constant int64_t & ne1[[buffer(16)]],
  856. constant uint & gqa[[buffer(17)]],
  857. uint3 tgpig[[threadgroup_position_in_grid]],
  858. uint tiisg[[thread_index_in_simdgroup]],
  859. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  860. const int nb = ne00/QK_K;
  861. const int r0 = tgpig.x;
  862. const int r1 = tgpig.y;
  863. const int r2 = tgpig.z;
  864. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  865. const int ib_row = first_row * nb;
  866. const uint offset0 = r2/gqa*(nb*ne0);
  867. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  868. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  869. float yl[32];
  870. float sumf[N_DST]={0.f}, all_sum;
  871. const int step = sizeof(block_q2_K) * nb;
  872. #if QK_K == 256
  873. const int ix = tiisg/8; // 0...3
  874. const int it = tiisg%8; // 0...7
  875. const int im = it/4; // 0 or 1
  876. const int ir = it%4; // 0...3
  877. const int is = (8*ir)/16;// 0 or 1
  878. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  879. for (int ib = ix; ib < nb; ib += 4) {
  880. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  881. for (int i = 0; i < 8; ++i) {
  882. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  883. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  884. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  885. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  886. }
  887. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  888. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  889. device const half * dh = &x[ib].d;
  890. for (int row = 0; row < N_DST; row++) {
  891. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  892. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  893. for (int i = 0; i < 8; i += 2) {
  894. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  895. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  896. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  897. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  898. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  899. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  900. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  901. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  902. }
  903. float dall = dh[0];
  904. float dmin = dh[1] * 1.f/16.f;
  905. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  906. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  907. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  908. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  909. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  910. qs += step/2;
  911. sc += step;
  912. dh += step/2;
  913. }
  914. y4 += 4 * QK_K;
  915. }
  916. #else
  917. const int ix = tiisg/2; // 0...15
  918. const int it = tiisg%2; // 0...1
  919. device const float * y4 = y + ix * QK_K + 8 * it;
  920. for (int ib = ix; ib < nb; ib += 16) {
  921. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  922. for (int i = 0; i < 8; ++i) {
  923. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  924. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  925. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  926. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  927. }
  928. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  929. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  930. device const half * dh = &x[ib].d;
  931. for (int row = 0; row < N_DST; row++) {
  932. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  933. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  934. for (int i = 0; i < 8; i += 2) {
  935. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  936. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  937. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  938. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  939. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  940. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  941. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  942. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  943. }
  944. float dall = dh[0];
  945. float dmin = dh[1];
  946. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  947. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  948. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  949. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  950. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  951. qs += step/2;
  952. sc += step;
  953. dh += step/2;
  954. }
  955. y4 += 16 * QK_K;
  956. }
  957. #endif
  958. for (int row = 0; row < N_DST; ++row) {
  959. all_sum = simd_sum(sumf[row]);
  960. if (tiisg == 0) {
  961. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  962. }
  963. }
  964. }
  965. #if QK_K == 256
  966. kernel void kernel_mul_mat_q3_K_f32(
  967. device const void * src0,
  968. device const float * src1,
  969. device float * dst,
  970. constant int64_t & ne00,
  971. constant int64_t & ne01[[buffer(4)]],
  972. constant int64_t & ne02[[buffer(5)]],
  973. constant int64_t & ne10[[buffer(9)]],
  974. constant int64_t & ne12[[buffer(11)]],
  975. constant int64_t & ne0[[buffer(15)]],
  976. constant int64_t & ne1[[buffer(16)]],
  977. constant uint & gqa[[buffer(17)]],
  978. uint3 tgpig[[threadgroup_position_in_grid]],
  979. uint tiisg[[thread_index_in_simdgroup]],
  980. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  981. const int nb = ne00/QK_K;
  982. const int64_t r0 = tgpig.x;
  983. const int64_t r1 = tgpig.y;
  984. const int64_t r2 = tgpig.z;
  985. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  986. const uint offset0 = r2/gqa*(nb*ne0);
  987. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  988. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  989. float yl[16];
  990. const uint16_t kmask1 = 0x0303;
  991. const uint16_t kmask2 = 0x0f0f;
  992. const int tid = tiisg/2;
  993. const int ix = tiisg%2;
  994. const int ip = tid/8; // 0 or 1
  995. const int il = tid/2 - 4*ip; // 0...3
  996. const int ir = tid%2;
  997. const int n = 8;
  998. const int l0 = n*ir;
  999. const uint16_t m1 = 1 << (4*ip + il);
  1000. const uint16_t m2 = m1 << 8;
  1001. const int shift = 2*il;
  1002. const uint16_t qm1 = 0x0003 << shift;
  1003. const uint16_t qm2 = 0x0300 << shift;
  1004. const int32_t v1 = 4 << shift;
  1005. const int32_t v2 = 1024 << shift;
  1006. const uint16_t s_shift1 = 4*ip;
  1007. const uint16_t s_shift2 = s_shift1 + 2*(il/2);
  1008. const int ik = 4 + (il%2);
  1009. const int q_offset = 32*ip + l0;
  1010. const int y_offset = 128*ip + 32*il + l0;
  1011. const int step = sizeof(block_q3_K) * nb / 2;
  1012. device const float * y1 = yy + ix*QK_K + y_offset;
  1013. float sumf1[2] = {0.f}, sumf2[2] = {0.f};
  1014. for (int i = ix; i < nb; i += 2) {
  1015. for (int l = 0; l < 8; ++l) {
  1016. yl[l+0] = y1[l+ 0];
  1017. yl[l+8] = y1[l+16];
  1018. }
  1019. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1020. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1021. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1022. device const half * dh = &x[i].d;
  1023. for (int row = 0; row < 2; ++row) {
  1024. const float d_all = (float)dh[0];
  1025. const char2 scales = as_type<char2>((uint16_t)(((a[il] >> s_shift1) & kmask2) | (((a[ik] >> s_shift2) & kmask1) << 4)));
  1026. float s1 = 0, s2 = 0;
  1027. for (int l = 0; l < n; l += 2) {
  1028. const uint16_t qs = q[l/2];
  1029. s1 += yl[l+0] * ((int32_t)(qs & qm1) - ((h[l/2] & m1) ? 0 : v1));
  1030. s2 += yl[l+1] * ((int32_t)(qs & qm2) - ((h[l/2] & m2) ? 0 : v2));
  1031. }
  1032. float d = d_all * (s1 + 1.f/256.f * s2);
  1033. sumf1[row] += d * scales[0];
  1034. sumf2[row] += d;
  1035. s1 = s2 = 0;
  1036. for (int l = 0; l < n; l += 2) {
  1037. const uint16_t qs = q[l/2+8];
  1038. s1 += yl[l+8] * ((int32_t)(qs & qm1) - ((h[l/2+8] & m1) ? 0 : v1));
  1039. s2 += yl[l+9] * ((int32_t)(qs & qm2) - ((h[l/2+8] & m2) ? 0 : v2));
  1040. }
  1041. d = d_all * (s1 + 1.f/256.f * s2);
  1042. sumf1[row] += d * scales[1];
  1043. sumf2[row] += d;
  1044. q += step;
  1045. h += step;
  1046. a += step;
  1047. dh += step;
  1048. }
  1049. y1 += 2 * QK_K;
  1050. }
  1051. for (int row = 0; row < 2; ++row) {
  1052. const float sumf = (sumf1[row] - 32.f*sumf2[row]) / (1 << shift);
  1053. const float tot = simd_sum(sumf);
  1054. if (tiisg == 0) {
  1055. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1056. }
  1057. }
  1058. }
  1059. #else
  1060. kernel void kernel_mul_mat_q3_K_f32(
  1061. device const void * src0,
  1062. device const float * src1,
  1063. device float * dst,
  1064. constant int64_t & ne00,
  1065. constant int64_t & ne01[[buffer(4)]],
  1066. constant int64_t & ne02[[buffer(5)]],
  1067. constant int64_t & ne10[[buffer(9)]],
  1068. constant int64_t & ne12[[buffer(11)]],
  1069. constant int64_t & ne0[[buffer(15)]],
  1070. constant int64_t & ne1[[buffer(16)]],
  1071. constant uint & gqa[[buffer(17)]],
  1072. uint3 tgpig[[threadgroup_position_in_grid]],
  1073. uint tiisg[[thread_index_in_simdgroup]],
  1074. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1075. const int nb = ne00/QK_K;
  1076. const int64_t r0 = tgpig.x;
  1077. const int64_t r1 = tgpig.y;
  1078. const int64_t r2 = tgpig.z;
  1079. const int row = 2 * r0 + sgitg;
  1080. const uint offset0 = r2/gqa*(nb*ne0);
  1081. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1082. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1083. const int ix = tiisg/4;
  1084. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1085. const int im = il/8; // 0, 0, 1, 1
  1086. const int in = il%8; // 0, 4, 0, 4
  1087. float2 sum = {0.f, 0.f};
  1088. for (int i = ix; i < nb; i += 8) {
  1089. const float d_all = (float)(x[i].d);
  1090. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1091. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1092. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1093. device const float * y = yy + i * QK_K + il;
  1094. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1095. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1096. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1097. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1098. for (int l = 0; l < 4; l += 2) {
  1099. const uint16_t hm = h[l/2] >> im;
  1100. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1101. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1102. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1103. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1104. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1105. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1106. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1107. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1108. }
  1109. }
  1110. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1111. const float tot = simd_sum(sumf);
  1112. if (tiisg == 0) {
  1113. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1114. }
  1115. }
  1116. #endif
  1117. #if QK_K == 256
  1118. kernel void kernel_mul_mat_q4_K_f32(
  1119. device const void * src0,
  1120. device const float * src1,
  1121. device float * dst,
  1122. constant int64_t & ne00,
  1123. constant int64_t & ne01[[buffer(4)]],
  1124. constant int64_t & ne02[[buffer(5)]],
  1125. constant int64_t & ne10[[buffer(9)]],
  1126. constant int64_t & ne12[[buffer(11)]],
  1127. constant int64_t & ne0[[buffer(15)]],
  1128. constant int64_t & ne1[[buffer(16)]],
  1129. constant uint & gqa[[buffer(17)]],
  1130. uint3 tgpig[[threadgroup_position_in_grid]],
  1131. uint tiisg[[thread_index_in_simdgroup]],
  1132. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1133. const uint16_t kmask1 = 0x3f3f;
  1134. const uint16_t kmask2 = 0x0f0f;
  1135. const uint16_t kmask3 = 0xc0c0;
  1136. const int ix = tiisg/8; // 0...3
  1137. const int it = tiisg%8; // 0...7
  1138. const int im = it/4; // 0 or 1
  1139. const int ir = it%4; // 0...3
  1140. const int nb = ne00/QK_K;
  1141. const int r0 = tgpig.x;
  1142. const int r1 = tgpig.y;
  1143. const int r2 = tgpig.z;
  1144. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1145. const int first_row = r0 * N_DST;
  1146. const int ib_row = first_row * nb;
  1147. const uint offset0 = r2/gqa*(nb*ne0);
  1148. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1149. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1150. float yl[16];
  1151. float yh[16];
  1152. float sumf[N_DST]={0.f}, all_sum;
  1153. const int step = sizeof(block_q4_K) * nb / 2;
  1154. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1155. uint16_t sc16[4];
  1156. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1157. for (int ib = ix; ib < nb; ib += 4) {
  1158. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1159. for (int i = 0; i < 8; ++i) {
  1160. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1161. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1162. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1163. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1164. }
  1165. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1166. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1167. device const half * dh = &x[ib].d;
  1168. for (int row = 0; row < N_DST; row++) {
  1169. sc16[0] = sc[0] & kmask1;
  1170. sc16[1] = sc[2] & kmask1;
  1171. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1172. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1173. device const uint16_t * q2 = q1 + 32;
  1174. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1175. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1176. for (int i = 0; i < 8; i += 2) {
  1177. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1178. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1179. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1180. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1181. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1182. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1183. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1184. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1185. }
  1186. float dall = dh[0];
  1187. float dmin = dh[1];
  1188. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1189. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1190. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1191. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1192. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1193. q1 += step;
  1194. sc += step;
  1195. dh += step;
  1196. }
  1197. y4 += 4 * QK_K;
  1198. }
  1199. for (int row = 0; row < N_DST; ++row) {
  1200. all_sum = simd_sum(sumf[row]);
  1201. if (tiisg == 0) {
  1202. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1203. }
  1204. }
  1205. }
  1206. #else
  1207. kernel void kernel_mul_mat_q4_K_f32(
  1208. device const void * src0,
  1209. device const float * src1,
  1210. device float * dst,
  1211. constant int64_t & ne00,
  1212. constant int64_t & ne01[[buffer(4)]],
  1213. constant int64_t & ne02[[buffer(5)]],
  1214. constant int64_t & ne10[[buffer(9)]],
  1215. constant int64_t & ne12[[buffer(11)]],
  1216. constant int64_t & ne0[[buffer(15)]],
  1217. constant int64_t & ne1[[buffer(16)]],
  1218. constant uint & gqa[[buffer(17)]],
  1219. uint3 tgpig[[threadgroup_position_in_grid]],
  1220. uint tiisg[[thread_index_in_simdgroup]],
  1221. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1222. const int ix = tiisg/4; // 0...7
  1223. const int it = tiisg%4; // 0...3
  1224. const int nb = ne00/QK_K;
  1225. const int r0 = tgpig.x;
  1226. const int r1 = tgpig.y;
  1227. const int r2 = tgpig.z;
  1228. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1229. const int ib_row = first_row * nb;
  1230. const uint offset0 = r2/gqa*(nb*ne0);
  1231. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1232. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1233. float yl[8];
  1234. float yh[8];
  1235. float sumf[N_DST]={0.f}, all_sum;
  1236. const int step = sizeof(block_q4_K) * nb / 2;
  1237. device const float * y4 = y + ix * QK_K + 8 * it;
  1238. uint16_t sc16[4];
  1239. for (int ib = ix; ib < nb; ib += 8) {
  1240. float2 sumy = {0.f, 0.f};
  1241. for (int i = 0; i < 8; ++i) {
  1242. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1243. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1244. }
  1245. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1246. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1247. device const half * dh = x[ib].d;
  1248. for (int row = 0; row < N_DST; row++) {
  1249. sc16[0] = sc[0] & 0x000f;
  1250. sc16[1] = sc[0] & 0x0f00;
  1251. sc16[2] = sc[0] & 0x00f0;
  1252. sc16[3] = sc[0] & 0xf000;
  1253. float2 acc1 = {0.f, 0.f};
  1254. float2 acc2 = {0.f, 0.f};
  1255. for (int i = 0; i < 8; i += 2) {
  1256. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1257. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1258. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1259. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1260. }
  1261. float dall = dh[0];
  1262. float dmin = dh[1];
  1263. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1264. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1265. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1266. qs += step;
  1267. sc += step;
  1268. dh += step;
  1269. }
  1270. y4 += 8 * QK_K;
  1271. }
  1272. for (int row = 0; row < N_DST; ++row) {
  1273. all_sum = simd_sum(sumf[row]);
  1274. if (tiisg == 0) {
  1275. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1276. }
  1277. }
  1278. }
  1279. #endif
  1280. kernel void kernel_mul_mat_q5_K_f32(
  1281. device const void * src0,
  1282. device const float * src1,
  1283. device float * dst,
  1284. constant int64_t & ne00,
  1285. constant int64_t & ne01[[buffer(4)]],
  1286. constant int64_t & ne02[[buffer(5)]],
  1287. constant int64_t & ne10[[buffer(9)]],
  1288. constant int64_t & ne12[[buffer(11)]],
  1289. constant int64_t & ne0[[buffer(15)]],
  1290. constant int64_t & ne1[[buffer(16)]],
  1291. constant uint & gqa[[buffer(17)]],
  1292. uint3 tgpig[[threadgroup_position_in_grid]],
  1293. uint tiisg[[thread_index_in_simdgroup]],
  1294. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1295. const int nb = ne00/QK_K;
  1296. const int64_t r0 = tgpig.x;
  1297. const int64_t r1 = tgpig.y;
  1298. const int r2 = tgpig.z;
  1299. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1300. const uint offset0 = r2/gqa*(nb*ne0);
  1301. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1302. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1303. float sumf[2]={0.f};
  1304. const int step = sizeof(block_q5_K) * nb;
  1305. #if QK_K == 256
  1306. #
  1307. float yl[16], yh[16];
  1308. const uint16_t kmask1 = 0x3f3f;
  1309. const uint16_t kmask2 = 0x0f0f;
  1310. const uint16_t kmask3 = 0xc0c0;
  1311. const int tid = tiisg/4;
  1312. const int ix = tiisg%4;
  1313. const int im = tid/4;
  1314. const int ir = tid%4;
  1315. const int n = 8;
  1316. const int l0 = n*ir;
  1317. const int q_offset = 32*im + l0;
  1318. const int y_offset = 64*im + l0;
  1319. const uint8_t hm1 = 1u << (2*im);
  1320. const uint8_t hm2 = hm1 << 1;
  1321. const uint8_t hm3 = hm1 << 4;
  1322. const uint8_t hm4 = hm2 << 4;
  1323. uint16_t sc16[4];
  1324. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1325. device const float * y1 = yy + ix*QK_K + y_offset;
  1326. for (int i = ix; i < nb; i += 4) {
  1327. device const uint8_t * q1 = x[i].qs + q_offset;
  1328. device const uint8_t * qh = x[i].qh + l0;
  1329. device const half * dh = &x[i].d;
  1330. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1331. device const float * y2 = y1 + 128;
  1332. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1333. for (int l = 0; l < 8; ++l) {
  1334. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1335. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1336. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1337. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1338. }
  1339. for (int row = 0; row < 2; ++row) {
  1340. device const uint8_t * q2 = q1 + 64;
  1341. sc16[0] = a[0] & kmask1;
  1342. sc16[1] = a[2] & kmask1;
  1343. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1344. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1345. float4 acc = {0.f, 0.f, 0.f, 0.f};
  1346. for (int l = 0; l < n; ++l) {
  1347. uint8_t h = qh[l];
  1348. acc[0] += yl[l+0] * ((uint16_t)(q1[l] & 0x0F) + (h & hm1 ? 16 : 0));
  1349. acc[1] += yl[l+8] * ((uint16_t)(q1[l] & 0xF0) + (h & hm2 ? 256 : 0));
  1350. acc[2] += yh[l+0] * ((uint16_t)(q2[l] & 0x0F) + (h & hm3 ? 16 : 0));
  1351. acc[3] += yh[l+8] * ((uint16_t)(q2[l] & 0xF0) + (h & hm4 ? 256 : 0));
  1352. }
  1353. const float dall = dh[0];
  1354. const float dmin = dh[1];
  1355. sumf[row] += dall * (acc[0] * sc8[0] + acc[1] * sc8[1] * 1.f/16.f + acc[2] * sc8[4] + acc[3] * sc8[5] * 1.f/16.f) -
  1356. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1357. q1 += step;
  1358. qh += step;
  1359. dh += step/2;
  1360. a += step/2;
  1361. }
  1362. y1 += 4 * QK_K;
  1363. }
  1364. #else
  1365. float yl[8], yh[8];
  1366. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1367. const int ix = tiisg%8;
  1368. const int im = il/8; // 0, 0, 1, 1
  1369. const int in = il%8; // 0, 4, 0, 4
  1370. device const float * y = yy + ix*QK_K + il;
  1371. for (int i = ix; i < nb; i += 8) {
  1372. for (int l = 0; l < 4; ++l) {
  1373. yl[l+0] = y[l+ 0];
  1374. yl[l+4] = y[l+16];
  1375. yh[l+0] = y[l+32];
  1376. yh[l+4] = y[l+48];
  1377. }
  1378. device const half * dh = &x[i].d;
  1379. device const uint8_t * q = x[i].qs + il;
  1380. device const uint8_t * h = x[i].qh + in;
  1381. device const int8_t * s = x[i].scales;
  1382. for (int row = 0; row < 2; ++row) {
  1383. const float d = dh[0];
  1384. float2 acc = {0.f, 0.f};
  1385. for (int l = 0; l < 4; ++l) {
  1386. const uint8_t hl = h[l] >> im;
  1387. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1388. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1389. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1390. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1391. }
  1392. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1393. q += step;
  1394. h += step;
  1395. s += step;
  1396. dh += step/2;
  1397. }
  1398. y += 8 * QK_K;
  1399. }
  1400. #endif
  1401. for (int row = 0; row < 2; ++row) {
  1402. const float tot = simd_sum(sumf[row]);
  1403. if (tiisg == 0) {
  1404. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1405. }
  1406. }
  1407. }
  1408. kernel void kernel_mul_mat_q6_K_f32(
  1409. device const void * src0,
  1410. device const float * src1,
  1411. device float * dst,
  1412. constant int64_t & ne00,
  1413. constant int64_t & ne01[[buffer(4)]],
  1414. constant int64_t & ne02[[buffer(5)]],
  1415. constant int64_t & ne10[[buffer(9)]],
  1416. constant int64_t & ne12[[buffer(11)]],
  1417. constant int64_t & ne0[[buffer(15)]],
  1418. constant int64_t & ne1[[buffer(16)]],
  1419. constant uint & gqa[[buffer(17)]],
  1420. uint3 tgpig[[threadgroup_position_in_grid]],
  1421. uint tiisg[[thread_index_in_simdgroup]],
  1422. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1423. const uint8_t kmask1 = 0x03;
  1424. const uint8_t kmask2 = 0x0C;
  1425. const uint8_t kmask3 = 0x30;
  1426. const uint8_t kmask4 = 0xC0;
  1427. const int nb = ne00/QK_K;
  1428. const int64_t r0 = tgpig.x;
  1429. const int64_t r1 = tgpig.y;
  1430. const int r2 = tgpig.z;
  1431. const int row = 2 * r0 + sgitg;
  1432. const uint offset0 = r2/gqa*(nb*ne0);
  1433. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1434. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1435. float sumf = 0;
  1436. #if QK_K == 256
  1437. const int tid = tiisg/2;
  1438. const int ix = tiisg%2;
  1439. const int ip = tid/8; // 0 or 1
  1440. const int il = tid%8;
  1441. const int n = 4;
  1442. const int l0 = n*il;
  1443. const int is = 8*ip + l0/16;
  1444. const int y_offset = 128*ip + l0;
  1445. const int q_offset_l = 64*ip + l0;
  1446. const int q_offset_h = 32*ip + l0;
  1447. for (int i = ix; i < nb; i += 2) {
  1448. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1449. device const uint8_t * q2 = q1 + 32;
  1450. device const uint8_t * qh = x[i].qh + q_offset_h;
  1451. device const int8_t * sc = x[i].scales + is;
  1452. device const float * y = yy + i * QK_K + y_offset;
  1453. const float dall = x[i].d;
  1454. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1455. for (int l = 0; l < n; ++l) {
  1456. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1457. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1458. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1459. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1460. }
  1461. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1462. }
  1463. #else
  1464. const int ix = tiisg/4;
  1465. const int il = 4*(tiisg%4);
  1466. for (int i = ix; i < nb; i += 8) {
  1467. device const float * y = yy + i * QK_K + il;
  1468. device const uint8_t * ql = x[i].ql + il;
  1469. device const uint8_t * qh = x[i].qh + il;
  1470. device const int8_t * s = x[i].scales;
  1471. const float d = x[i].d;
  1472. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1473. for (int l = 0; l < 4; ++l) {
  1474. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1475. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1476. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1477. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1478. }
  1479. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1480. }
  1481. #endif
  1482. const float tot = simd_sum(sumf);
  1483. if (tiisg == 0) {
  1484. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1485. }
  1486. }
  1487. //============================= templates and their specializations =============================
  1488. template <typename type4x4>
  1489. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1490. half4x4 temp = *(((device half4x4 *)src));
  1491. for (int i = 0; i < 16; i++){
  1492. reg[i/4][i%4] = temp[i/4][i%4];
  1493. }
  1494. }
  1495. template <typename type4x4>
  1496. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1497. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1498. const half d = il ? (xb->d / 16.h) : xb->d;
  1499. const half m = il ? ( -8.h * 16.h) : -8.h;
  1500. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1501. const ushort mask1 = il ? 0xF000 : 0x0F00;
  1502. for (int i=0;i<8;i++) {
  1503. reg[i/2][2*(i%2)] = (((qs[i] & mask0) ) + m) * d;
  1504. reg[i/2][2*(i%2)+1] = (((qs[i] & mask1) >> 8) + m) * d;
  1505. }
  1506. }
  1507. template <typename type4x4>
  1508. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1509. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1510. const half d = il ? (xb->d / 16.h) : xb->d;
  1511. const half m = xb->m;
  1512. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1513. const ushort mask1 = il ? 0xF000 : 0x0F00;
  1514. for (int i=0;i<8;i++) {
  1515. reg[i/2][2*(i%2)] = (((qs[i] & mask0) ) * d) + m;
  1516. reg[i/2][2*(i%2)+1] = (((qs[i] & mask1) >> 8) * d) + m;
  1517. }
  1518. }
  1519. template <typename type4x4>
  1520. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1521. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1522. const half d = xb->d;
  1523. for (int i=0;i<16;i++) {
  1524. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1525. }
  1526. }
  1527. template <typename type4x4>
  1528. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1529. const half d = xb->d;
  1530. const half min = xb->dmin;
  1531. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1532. half dl, ml;
  1533. uint8_t sc = xb->scales[il];
  1534. #if QK_K == 256
  1535. q = q + 32*(il/8) + 16*(il&1);
  1536. il = (il/2)%4;
  1537. #endif
  1538. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1539. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1540. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1541. for (int i = 0; i < 16; ++i) {
  1542. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1543. }
  1544. }
  1545. template <typename type4x4>
  1546. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1547. const float d_all = (float)(xb->d);
  1548. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1549. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1550. device const int8_t * scales = (device const int8_t *)xb->scales;
  1551. #if QK_K == 256
  1552. q = q + 32 * (il/8) + 16 * (il&1);
  1553. h = h + 16 * (il&1);
  1554. uint8_t m = 1 << (il/2);
  1555. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1556. ((il/4)>0 ? 12 : 3);
  1557. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1558. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1559. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2) : \
  1560. (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1561. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  1562. il = (il/2)%4;
  1563. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1564. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1565. for (int i = 0; i < 16; ++i) {
  1566. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i] & m) ? 0 : 4.f/coef));
  1567. }
  1568. #else
  1569. float kcoef = il&1 ? 1.f/16.f : 1.f;
  1570. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  1571. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  1572. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1573. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1574. uint8_t m = 1<<(il*2);
  1575. for (int i = 0; i < 16; ++i) {
  1576. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  1577. }
  1578. #endif
  1579. }
  1580. template <typename type4x4>
  1581. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  1582. device const uint8_t * q = xb->qs;
  1583. #if QK_K == 256
  1584. const float d = (float)(xb->d);
  1585. const float min = (float)(xb->dmin);
  1586. short is = (il/4) * 2;
  1587. q = q + (il/4) * 32 + 16 * (il&1);
  1588. il = il%4;
  1589. const uchar4 sc = get_scale_min_k4(is, xb->scales);
  1590. const float dl = il<2 ? d * sc[0] : d * sc[2]/16.h;
  1591. const float ml = il<2 ? min * sc[1] : min * sc[3];
  1592. #else
  1593. q = q + 16 * (il&1);
  1594. device const uint8_t * s = xb->scales;
  1595. device const half2 * dh = (device const half2 *)xb->d;
  1596. const float2 d = (float2)dh[0];
  1597. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  1598. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1 ]* (s[1]>>4);
  1599. #endif
  1600. const ushort mask = il<2 ? 0x0F : 0xF0;
  1601. for (int i = 0; i < 16; ++i) {
  1602. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1603. }
  1604. }
  1605. template <typename type4x4>
  1606. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  1607. device const uint8_t * q = xb->qs;
  1608. device const uint8_t * qh = xb->qh;
  1609. #if QK_K == 256
  1610. const float d = (float)(xb->d);
  1611. const float min = (float)(xb->dmin);
  1612. short is = (il/4) * 2;
  1613. q = q + 32 * (il/4) + 16 * (il&1);
  1614. qh = qh + 16 * (il&1);
  1615. uint8_t ul = 1 << (il/2);
  1616. il = il%4;
  1617. const uchar4 sc = get_scale_min_k4(is, xb->scales);
  1618. const float dl = il<2 ? d * sc[0] : d * sc[2]/16.h;
  1619. const float ml = il<2 ? min * sc[1] : min * sc[3];
  1620. const ushort mask = il<2 ? 0x0F : 0xF0;
  1621. const float qh_val = il<2 ? 16.f : 256.f;
  1622. for (int i = 0; i < 16; ++i) {
  1623. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  1624. }
  1625. #else
  1626. q = q + 16 * (il&1);
  1627. device const int8_t * s = xb->scales;
  1628. const float dl = xb->d * s[il];
  1629. uint8_t m = 1<<(il*2);
  1630. const float coef = il<2 ? 1.f : 1.f/16.f;
  1631. const ushort mask = il<2 ? 0x0F : 0xF0;
  1632. for (int i = 0; i < 16; ++i) {
  1633. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  1634. }
  1635. #endif
  1636. }
  1637. template <typename type4x4>
  1638. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  1639. const float d_all = (float)(xb->d);
  1640. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  1641. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  1642. device const int8_t * scales = (device const int8_t *)xb->scales;
  1643. #if QK_K == 256
  1644. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  1645. qh = qh + 32*(il/8) + 16*(il&1);
  1646. float sc = scales[(il%2) + 2 * ((il/2))];
  1647. il = (il/2)%4;
  1648. #else
  1649. ql = ql + 16 * (il&1);
  1650. float sc = scales[il];
  1651. #endif
  1652. for (int i = 0; i < 16; ++i) {
  1653. uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1654. uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  1655. const float coef = il>1 ? 1.f/16.f : 1.f;
  1656. float q = il&1 ? ((ql[i]&kmask2)|((qh[i]&kmask1)<<2)) - 32.f/coef : \
  1657. ((ql[i]&kmask2)|((qh[i]&kmask1)<<4)) - 32.f/coef;
  1658. reg[i/4][i%4] = d_all * sc * q * coef;
  1659. }
  1660. }
  1661. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  1662. kernel void kernel_get_rows(
  1663. device const void * src0,
  1664. device const int * src1,
  1665. device float * dst,
  1666. constant int64_t & ne00,
  1667. constant uint64_t & nb01,
  1668. constant uint64_t & nb1,
  1669. uint tgpig[[threadgroup_position_in_grid]],
  1670. uint tiitg[[thread_index_in_threadgroup]],
  1671. uint tptg[[threads_per_threadgroup]]) {
  1672. const int i = tgpig;
  1673. const int r = ((device int32_t *) src1)[i];
  1674. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  1675. float4x4 temp;
  1676. dequantize_func(
  1677. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  1678. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  1679. }
  1680. }
  1681. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  1682. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix A
  1683. #define BLOCK_SIZE_K 32
  1684. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  1685. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  1686. #define THREAD_PER_BLOCK 128
  1687. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  1688. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  1689. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  1690. #define SG_MAT_ROW 8
  1691. // each block_q contains 16*nl weights
  1692. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  1693. kernel void kernel_mul_mm(device const uchar * src0,
  1694. device const float * src1,
  1695. device float * dst,
  1696. constant int64_t & ne00,
  1697. constant int64_t & ne02,
  1698. constant int64_t & nb01,
  1699. constant int64_t & nb02,
  1700. constant int64_t & ne12,
  1701. constant int64_t & ne0,
  1702. constant int64_t & ne1,
  1703. constant uint & gqa,
  1704. threadgroup uchar * shared_memory [[threadgroup(0)]],
  1705. uint3 tgpig[[threadgroup_position_in_grid]],
  1706. uint tiitg[[thread_index_in_threadgroup]],
  1707. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1708. threadgroup half * sa = ((threadgroup half *)shared_memory);
  1709. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  1710. const uint r0 = tgpig.y;
  1711. const uint r1 = tgpig.x;
  1712. const uint im = tgpig.z;
  1713. // if this block is of 64x32 shape or smaller
  1714. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  1715. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  1716. // a thread shouldn't load data outside of the matrix
  1717. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  1718. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  1719. simdgroup_half8x8 ma[4];
  1720. simdgroup_float8x8 mb[2];
  1721. simdgroup_float8x8 c_res[8];
  1722. for (int i = 0; i < 8; i++){
  1723. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  1724. }
  1725. short il = (tiitg % THREAD_PER_ROW);
  1726. uint offset0 = im/gqa*nb02; ushort offset1 = il/nl;
  1727. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  1728. device const float * y = src1 + (r1 * BLOCK_SIZE_N + thread_col) * ne00 \
  1729. + BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL) + im * ne00 * ne1;
  1730. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  1731. //load data and store to threadgroup memory
  1732. half4x4 temp_a;
  1733. dequantize_func(x, il, temp_a);
  1734. threadgroup_barrier(mem_flags::mem_threadgroup);
  1735. #pragma unroll(16)
  1736. for (int i = 0; i < 16; i++) {
  1737. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  1738. + 16 * (tiitg % THREAD_PER_ROW) + 8 * (i / 8)) \
  1739. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  1740. }
  1741. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) \
  1742. = *((device float2x4 *)y);
  1743. il = (il + 2 < nl) ? il + 2 : il % 2;
  1744. x = (il < 2) ? x + (2+nl-1)/nl : x;
  1745. y += BLOCK_SIZE_K;
  1746. threadgroup_barrier(mem_flags::mem_threadgroup);
  1747. //load matrices from threadgroup memory and conduct outer products
  1748. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  1749. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  1750. #pragma unroll(4)
  1751. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  1752. #pragma unroll(4)
  1753. for (int i = 0; i < 4; i++) {
  1754. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  1755. }
  1756. simdgroup_barrier(mem_flags::mem_none);
  1757. #pragma unroll(2)
  1758. for (int i = 0; i < 2; i++) {
  1759. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  1760. }
  1761. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  1762. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  1763. #pragma unroll(8)
  1764. for (int i = 0; i < 8; i++){
  1765. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  1766. }
  1767. }
  1768. }
  1769. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  1770. device float *C = dst + BLOCK_SIZE_M * r0 + 32 * (sgitg&1) \
  1771. + (BLOCK_SIZE_N * r1 + 16 * (sgitg>>1)) * ne0 + im*ne1*ne0;
  1772. for (int i = 0; i < 8; i++) {
  1773. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  1774. }
  1775. } else {
  1776. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  1777. threadgroup_barrier(mem_flags::mem_threadgroup);
  1778. threadgroup float *temp_str = ((threadgroup float *)shared_memory) \
  1779. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  1780. for (int i = 0; i < 8; i++) {
  1781. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  1782. }
  1783. threadgroup_barrier(mem_flags::mem_threadgroup);
  1784. device float *C = dst + BLOCK_SIZE_M * r0 + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  1785. if (sgitg==0) {
  1786. for (int i = 0; i < n_rows; i++) {
  1787. for (int j = tiitg; j< n_cols; j += BLOCK_SIZE_N) {
  1788. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  1789. }
  1790. }
  1791. }
  1792. }
  1793. }
  1794. #if QK_K == 256
  1795. #define QK_NL 16
  1796. #else
  1797. #define QK_NL 4
  1798. #endif
  1799. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  1800. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  1801. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  1802. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  1803. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  1804. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  1805. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  1806. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  1807. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  1808. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  1809. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  1810. typedef void (mat_mm_t)(device const uchar *, device const float *, device float *, constant int64_t &,\
  1811. constant int64_t &, constant int64_t &, constant int64_t &, constant int64_t &, \
  1812. constant int64_t &, constant int64_t &, constant uint &, threadgroup uchar *, uint3, uint, uint);
  1813. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  1814. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  1815. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  1816. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  1817. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  1818. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  1819. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  1820. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  1821. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;