ggml-metal.metal 77 KB

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