ggml-metal.metal 76 KB

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