ggml-metal.metal 67 KB

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