ggml-metal.metal 71 KB

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