ggml-metal.metal 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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. static void dequantize_row_q4_0(device const block_q4_0 * x, device float * y, int k) {
  17. const int qk = QK4_0;
  18. assert(k % qk == 0);
  19. const int nb = k / qk;
  20. for (int i = 0; i < nb; i++) {
  21. const half d = x[i].d;
  22. for (int j = 0; j < qk/2; ++j) {
  23. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  24. const int x1 = (x[i].qs[j] >> 4) - 8;
  25. y[i*qk + j + 0 ] = x0*d;
  26. y[i*qk + j + qk/2] = x1*d;
  27. }
  28. }
  29. }
  30. static void dequantize_row_q4_1(device const block_q4_1 * x, device float * y, int k) {
  31. const int qk = QK4_1;
  32. assert(k % qk == 0);
  33. const int nb = k / qk;
  34. for (int i = 0; i < nb; i++) {
  35. const half d = x[i].d;
  36. const half m = x[i].m;
  37. for (int j = 0; j < qk/2; ++j) {
  38. const int x0 = (x[i].qs[j] & 0x0F);
  39. const int x1 = (x[i].qs[j] >> 4);
  40. y[i*qk + j + 0 ] = x0*d + m;
  41. y[i*qk + j + qk/2] = x1*d + m;
  42. }
  43. }
  44. }
  45. kernel void kernel_add(
  46. device const float * src0,
  47. device const float * src1,
  48. device float * dst,
  49. uint tpig[[thread_position_in_grid]]) {
  50. dst[tpig] = src0[tpig] + src1[tpig];
  51. }
  52. kernel void kernel_mul(
  53. device const float * src0,
  54. device const float * src1,
  55. device float * dst,
  56. uint tpig[[thread_position_in_grid]]) {
  57. dst[tpig] = src0[tpig] * src1[tpig];
  58. }
  59. // assumption: src1 is a row
  60. // broadcast src1 into src0
  61. kernel void kernel_mul_row(
  62. device const float * src0,
  63. device const float * src1,
  64. device float * dst,
  65. constant int64_t & ne00,
  66. uint tpig[[thread_position_in_grid]]) {
  67. dst[tpig] = src0[tpig] * src1[tpig % ne00];
  68. }
  69. kernel void kernel_scale(
  70. device const float * src0,
  71. device float * dst,
  72. constant float & scale,
  73. uint tpig[[thread_position_in_grid]]) {
  74. dst[tpig] = src0[tpig] * scale;
  75. }
  76. kernel void kernel_silu(
  77. device const float * src0,
  78. device float * dst,
  79. uint tpig[[thread_position_in_grid]]) {
  80. float x = src0[tpig];
  81. dst[tpig] = x / (1.0f + exp(-x));
  82. }
  83. kernel void kernel_relu(
  84. device const float * src0,
  85. device float * dst,
  86. uint tpig[[thread_position_in_grid]]) {
  87. dst[tpig] = max(0.0f, src0[tpig]);
  88. }
  89. constant float GELU_COEF_A = 0.044715f;
  90. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  91. kernel void kernel_gelu(
  92. device const float * src0,
  93. device float * dst,
  94. uint tpig[[thread_position_in_grid]]) {
  95. float x = src0[tpig];
  96. dst[tpig] = 0.5f*x*(1.0f + tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  97. }
  98. kernel void kernel_soft_max(
  99. device const float * src0,
  100. device float * dst,
  101. constant int64_t & ne00,
  102. constant int64_t & ne01,
  103. constant int64_t & ne02,
  104. threadgroup float * buf [[threadgroup(0)]],
  105. uint3 tgpig[[threadgroup_position_in_grid]],
  106. uint3 tpitg[[thread_position_in_threadgroup]],
  107. uint3 ntg[[threads_per_threadgroup]]) {
  108. const int64_t i03 = tgpig[2];
  109. const int64_t i02 = tgpig[1];
  110. const int64_t i01 = tgpig[0];
  111. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  112. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  113. // parallel max
  114. buf[tpitg[0]] = -INFINITY;
  115. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  116. buf[tpitg[0]] = MAX(buf[tpitg[0]], psrc0[i00]);
  117. }
  118. // reduce
  119. threadgroup_barrier(mem_flags::mem_threadgroup);
  120. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  121. if (tpitg[0] < i) {
  122. buf[tpitg[0]] = MAX(buf[tpitg[0]], buf[tpitg[0] + i]);
  123. }
  124. threadgroup_barrier(mem_flags::mem_threadgroup);
  125. }
  126. // broadcast
  127. if (tpitg[0] == 0) {
  128. buf[0] = buf[0];
  129. }
  130. threadgroup_barrier(mem_flags::mem_threadgroup);
  131. const float max = buf[0];
  132. // parallel sum
  133. buf[tpitg[0]] = 0.0f;
  134. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  135. buf[tpitg[0]] += exp(psrc0[i00] - max);
  136. }
  137. // reduce
  138. threadgroup_barrier(mem_flags::mem_threadgroup);
  139. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  140. if (tpitg[0] < i) {
  141. buf[tpitg[0]] += buf[tpitg[0] + i];
  142. }
  143. threadgroup_barrier(mem_flags::mem_threadgroup);
  144. }
  145. // broadcast
  146. if (tpitg[0] == 0) {
  147. buf[0] = buf[0];
  148. }
  149. threadgroup_barrier(mem_flags::mem_threadgroup);
  150. const float sum = buf[0];
  151. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  152. pdst[i00] = exp(psrc0[i00] - max) / sum;
  153. }
  154. }
  155. kernel void kernel_diag_mask_inf(
  156. device const float * src0,
  157. device float * dst,
  158. constant int64_t & ne00,
  159. constant int64_t & ne01,
  160. constant int & n_past,
  161. uint3 tpig[[thread_position_in_grid]]) {
  162. const int64_t i02 = tpig[2];
  163. const int64_t i01 = tpig[1];
  164. const int64_t i00 = tpig[0];
  165. if (i00 > n_past + i01) {
  166. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  167. } else {
  168. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  169. }
  170. }
  171. kernel void kernel_get_rows_f16(
  172. device const void * src0,
  173. device const int * src1,
  174. device float * dst,
  175. constant int64_t & ne00,
  176. constant uint64_t & nb01,
  177. constant uint64_t & nb1,
  178. uint tpig[[thread_position_in_grid]]) {
  179. const int i = tpig;
  180. const int r = ((device int32_t *) src1)[i];
  181. for (int j = 0; j < ne00; j++) {
  182. dst[i*nb1 + j] = ((device half *) ((device char *) src0 + r*nb01))[j];
  183. }
  184. }
  185. kernel void kernel_get_rows_q4_0(
  186. device const void * src0,
  187. device const int * src1,
  188. device float * dst,
  189. constant int64_t & ne00,
  190. constant uint64_t & nb01,
  191. constant uint64_t & nb1,
  192. uint tpig[[thread_position_in_grid]]) {
  193. const int i = tpig;
  194. const int r = ((device int32_t *) src1)[i];
  195. dequantize_row_q4_0(
  196. (device const block_q4_0 *) ((device char *) src0 + r*nb01),
  197. (device float *) ((device char *) dst + i*nb1), ne00);
  198. }
  199. kernel void kernel_get_rows_q4_1(
  200. device const void * src0,
  201. device const int * src1,
  202. device float * dst,
  203. constant int64_t & ne00,
  204. constant uint64_t & nb01,
  205. constant uint64_t & nb1,
  206. uint tpig[[thread_position_in_grid]]) {
  207. const int i = tpig;
  208. const int r = ((device int32_t *) src1)[i];
  209. dequantize_row_q4_1(
  210. (device const block_q4_1 *) ((device char *) src0 + r*nb01),
  211. (device float *) ((device char *) dst + i*nb1), ne00);
  212. }
  213. kernel void kernel_norm(
  214. device const void * src0,
  215. device float * dst,
  216. constant int64_t & ne00,
  217. constant uint64_t & nb01,
  218. constant float & eps,
  219. threadgroup float * sum [[threadgroup(0)]],
  220. uint tgpig[[threadgroup_position_in_grid]],
  221. uint tpitg[[thread_position_in_threadgroup]],
  222. uint ntg[[threads_per_threadgroup]]) {
  223. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  224. // MEAN
  225. // parallel sum
  226. sum[tpitg] = 0.0f;
  227. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  228. sum[tpitg] += x[i00];
  229. }
  230. // reduce
  231. threadgroup_barrier(mem_flags::mem_threadgroup);
  232. for (uint i = ntg/2; i > 0; i /= 2) {
  233. if (tpitg < i) {
  234. sum[tpitg] += sum[tpitg + i];
  235. }
  236. threadgroup_barrier(mem_flags::mem_threadgroup);
  237. }
  238. // broadcast
  239. if (tpitg == 0) {
  240. sum[0] /= ne00;
  241. }
  242. threadgroup_barrier(mem_flags::mem_threadgroup);
  243. const float mean = sum[0];
  244. // recenter
  245. device float * y = dst + tgpig*ne00;
  246. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  247. y[i00] = x[i00] - mean;
  248. }
  249. // VARIANCE
  250. // parallel sum
  251. sum[tpitg] = 0.0f;
  252. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  253. sum[tpitg] += y[i00] * y[i00];
  254. }
  255. // reduce
  256. threadgroup_barrier(mem_flags::mem_threadgroup);
  257. for (uint i = ntg/2; i > 0; i /= 2) {
  258. if (tpitg < i) {
  259. sum[tpitg] += sum[tpitg + i];
  260. }
  261. threadgroup_barrier(mem_flags::mem_threadgroup);
  262. }
  263. // broadcast
  264. if (tpitg == 0) {
  265. sum[0] /= ne00;
  266. }
  267. threadgroup_barrier(mem_flags::mem_threadgroup);
  268. const float variance = sum[0];
  269. const float scale = 1.0f/sqrt(variance + eps);
  270. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  271. y[i00] = y[i00] * scale;
  272. }
  273. }
  274. kernel void kernel_rms_norm(
  275. device const void * src0,
  276. device float * dst,
  277. constant int64_t & ne00,
  278. constant uint64_t & nb01,
  279. constant float & eps,
  280. threadgroup float * sum [[threadgroup(0)]],
  281. uint tgpig[[threadgroup_position_in_grid]],
  282. uint tpitg[[thread_position_in_threadgroup]],
  283. uint ntg[[threads_per_threadgroup]]) {
  284. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  285. // parallel sum
  286. sum[tpitg] = 0.0f;
  287. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  288. sum[tpitg] += x[i00] * x[i00];
  289. }
  290. // reduce
  291. threadgroup_barrier(mem_flags::mem_threadgroup);
  292. for (uint i = ntg/2; i > 0; i /= 2) {
  293. if (tpitg < i) {
  294. sum[tpitg] += sum[tpitg + i];
  295. }
  296. threadgroup_barrier(mem_flags::mem_threadgroup);
  297. }
  298. // broadcast
  299. if (tpitg == 0) {
  300. sum[0] /= ne00;
  301. }
  302. threadgroup_barrier(mem_flags::mem_threadgroup);
  303. const float mean = sum[0];
  304. const float scale = 1.0f/sqrt(mean + eps);
  305. device float * y = dst + tgpig*ne00;
  306. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  307. y[i00] = x[i00] * scale;
  308. }
  309. }
  310. kernel void kernel_mul_mat_q4_0_f32(
  311. device const void * src0,
  312. device const float * src1,
  313. device float * dst,
  314. constant int64_t & ne00,
  315. constant int64_t & ne10,
  316. constant int64_t & ne0,
  317. threadgroup float * sum [[threadgroup(0)]],
  318. uint2 tgpig[[threadgroup_position_in_grid]],
  319. uint2 tpitg[[thread_position_in_threadgroup]],
  320. uint2 tptg[[threads_per_threadgroup]]) {
  321. const int nb = ne00/QK4_0;
  322. const int64_t r0 = tgpig.x;
  323. const int64_t r1 = tgpig.y;
  324. device const block_q4_0 * x = (device const block_q4_0 *) src0 + r0*nb;
  325. device const float * y = (device const float *) src1 + r1*ne10;
  326. const int nth = tptg.x*tptg.y;
  327. const int ith = tptg.y*tpitg.x + tpitg.y;
  328. const int ix = tpitg.y/4; // 0 or 1
  329. const int iy = tpitg.y - 4*ix; // 0...3
  330. const int first = 4 * iy;
  331. float sumf = 0;
  332. for (int i = 2*tpitg.x + ix; i < nb; i += 2*tptg.x) {
  333. const float d = (float)x[i].d;
  334. device const uint8_t * xl = x[i].qs + first;
  335. device const float * yl = y + i * QK4_0 + first;
  336. float2 acc = {0.0f, 0.0f};
  337. for (int j = 0; j < 4; ++j) {
  338. acc[0] += yl[j] * (xl[j] & 0xF) + yl[j+16] * (xl[j] >> 4);
  339. acc[1] += yl[j] + yl[j+16];
  340. }
  341. sumf += d * (acc[0] - 8.f*acc[1]);
  342. }
  343. sum[ith] = sumf;
  344. //
  345. // Accumulate the sum from all threads in the threadgroup
  346. //
  347. threadgroup_barrier(mem_flags::mem_threadgroup);
  348. if (ith%4 == 0) {
  349. sum[ith] += sum[ith+1] + sum[ith+2] + sum[ith+3];
  350. }
  351. threadgroup_barrier(mem_flags::mem_threadgroup);
  352. if (ith%16 == 0) {
  353. sum[ith] += sum[ith+4] + sum[ith+8] + sum[ith+12];
  354. }
  355. threadgroup_barrier(mem_flags::mem_threadgroup);
  356. if (ith == 0) {
  357. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  358. dst[r1*ne0 + r0] = sum[0];
  359. }
  360. }
  361. kernel void kernel_mul_mat_q4_1_f32(
  362. device const void * src0,
  363. device const float * src1,
  364. device float * dst,
  365. constant int64_t & ne00,
  366. constant int64_t & ne10,
  367. constant int64_t & ne0,
  368. threadgroup float * sum [[threadgroup(0)]],
  369. uint2 tgpig[[threadgroup_position_in_grid]],
  370. uint2 tpitg[[thread_position_in_threadgroup]],
  371. uint2 tptg[[threads_per_threadgroup]]) {
  372. const int nb = ne00/QK4_1;
  373. const int64_t r0 = tgpig.x;
  374. const int64_t r1 = tgpig.y;
  375. device const block_q4_1 * x = (device const block_q4_1 *) src0 + r0*nb;
  376. device const float * y = (device const float *) src1 + r1*ne10;
  377. const uint nth = tptg.x*tptg.y;
  378. const uint ith = tptg.y*tpitg.x + tpitg.y;
  379. const int ix = tpitg.y/4; // 0 or 1
  380. const int iy = tpitg.y - 4*ix; // 0...3
  381. const int first = 4 * iy;
  382. float sumf = 0;
  383. for (int i = 2*tpitg.x + ix; i < nb; i += 2*tptg.x) {
  384. const float d = (float)x[i].d;
  385. const float m = (float)x[i].m;
  386. device const uint8_t * xl = x[i].qs + first;
  387. device const float * yl = y + i * QK4_1 + first;
  388. float2 acc = {0.0f, 0.0f};
  389. for (int j = 0; j < 4; ++j) {
  390. acc[0] += yl[j+ 0] * (d * (xl[j] & 0xF) + m);
  391. acc[1] += yl[j+16] * (d * (xl[j] >> 4) + m);
  392. }
  393. sumf += acc[0] + acc[1];
  394. }
  395. sum[ith] = sumf;
  396. //
  397. // Accumulate the sum from all threads in the threadgroup
  398. //
  399. threadgroup_barrier(mem_flags::mem_threadgroup);
  400. if (ith%4 == 0) {
  401. sum[ith] += sum[ith+1] + sum[ith+2] + sum[ith+3];
  402. }
  403. threadgroup_barrier(mem_flags::mem_threadgroup);
  404. if (ith%16 == 0) {
  405. sum[ith] += sum[ith+4] + sum[ith+8] + sum[ith+12];
  406. }
  407. threadgroup_barrier(mem_flags::mem_threadgroup);
  408. if (ith == 0) {
  409. for (uint i = 16; i < nth; i += 16) sum[0] += sum[i];
  410. dst[r1*ne0 + r0] = sum[0];
  411. }
  412. }
  413. kernel void kernel_mul_mat_f16_f32(
  414. device const char * src0,
  415. device const char * src1,
  416. device float * dst,
  417. constant int64_t & ne00,
  418. constant int64_t & ne01,
  419. constant uint64_t & nb00,
  420. constant uint64_t & nb01,
  421. constant uint64_t & nb02,
  422. constant int64_t & ne10,
  423. constant int64_t & ne11,
  424. constant uint64_t & nb10,
  425. constant uint64_t & nb11,
  426. constant uint64_t & nb12,
  427. constant int64_t & ne0,
  428. constant int64_t & ne1,
  429. threadgroup float * sum [[threadgroup(0)]],
  430. uint3 tgpig[[threadgroup_position_in_grid]],
  431. uint3 tpig[[thread_position_in_grid]],
  432. uint3 tpitg[[thread_position_in_threadgroup]],
  433. uint3 tptg[[threads_per_threadgroup]]) {
  434. const int64_t r0 = tgpig.x;
  435. const int64_t r1 = tgpig.y;
  436. const int64_t im = tgpig.z;
  437. device const half * x = (device const half *) (src0 + r0*nb01 + im*nb02);
  438. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  439. sum[tpitg.x] = 0.0f;
  440. for (int i = tpitg.x; i < ne00; i += tptg.x) {
  441. sum[tpitg.x] += (float) x[i] * (float) y[i];
  442. }
  443. // accumulate the sum from all threads in the threadgroup
  444. threadgroup_barrier(mem_flags::mem_threadgroup);
  445. for (uint i = tptg.x/2; i > 0; i /= 2) {
  446. if (tpitg.x < i) {
  447. sum[tpitg.x] += sum[tpitg.x + i];
  448. }
  449. threadgroup_barrier(mem_flags::mem_threadgroup);
  450. }
  451. if (tpitg.x == 0) {
  452. dst[im*ne1*ne0 + r1*ne0 + r0] = sum[0];
  453. }
  454. }
  455. kernel void kernel_alibi_f32(
  456. device const float * src0,
  457. device float * dst,
  458. constant int64_t & ne00,
  459. constant int64_t & ne01,
  460. constant int64_t & ne02,
  461. constant int64_t & ne03,
  462. constant uint64_t & nb00,
  463. constant uint64_t & nb01,
  464. constant uint64_t & nb02,
  465. constant uint64_t & nb03,
  466. constant int64_t & ne0,
  467. constant int64_t & ne1,
  468. constant int64_t & ne2,
  469. constant int64_t & ne3,
  470. constant uint64_t & nb0,
  471. constant uint64_t & nb1,
  472. constant uint64_t & nb2,
  473. constant uint64_t & nb3,
  474. constant float & m0,
  475. uint3 tgpig[[threadgroup_position_in_grid]],
  476. uint3 tpitg[[thread_position_in_threadgroup]],
  477. uint3 ntg[[threads_per_threadgroup]]) {
  478. const int64_t i03 = tgpig[2];
  479. const int64_t i02 = tgpig[1];
  480. const int64_t i01 = tgpig[0];
  481. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  482. const int64_t i3 = n / (ne2*ne1*ne0);
  483. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  484. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  485. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  486. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  487. float m_k = pow(m0, i2 + 1);
  488. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  489. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  490. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  491. }
  492. }
  493. kernel void kernel_rope(
  494. device const void * src0,
  495. device float * dst,
  496. constant int64_t & ne00,
  497. constant int64_t & ne01,
  498. constant int64_t & ne02,
  499. constant int64_t & ne03,
  500. constant uint64_t & nb00,
  501. constant uint64_t & nb01,
  502. constant uint64_t & nb02,
  503. constant uint64_t & nb03,
  504. constant int64_t & ne0,
  505. constant int64_t & ne1,
  506. constant int64_t & ne2,
  507. constant int64_t & ne3,
  508. constant uint64_t & nb0,
  509. constant uint64_t & nb1,
  510. constant uint64_t & nb2,
  511. constant uint64_t & nb3,
  512. constant int & n_past,
  513. constant int & n_dims,
  514. constant int & mode,
  515. uint3 tpig[[thread_position_in_grid]]) {
  516. const int64_t i3 = tpig[2];
  517. const int64_t i2 = tpig[1];
  518. const int64_t i1 = tpig[0];
  519. const bool is_neox = mode & 2;
  520. const float theta_scale = pow(10000.0, -2.0f/n_dims);
  521. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  522. float theta = (float)p;
  523. if (!is_neox) {
  524. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  525. const float cos_theta = cos(theta);
  526. const float sin_theta = sin(theta);
  527. theta *= theta_scale;
  528. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  529. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  530. const float x0 = src[0];
  531. const float x1 = src[1];
  532. dst_data[0] = x0*cos_theta - x1*sin_theta;
  533. dst_data[1] = x0*sin_theta + x1*cos_theta;
  534. }
  535. } else {
  536. // TODO: implement
  537. }
  538. }
  539. kernel void kernel_cpy_f16_f16(
  540. device const half * src0,
  541. device half * dst,
  542. constant int64_t & ne00,
  543. constant int64_t & ne01,
  544. constant int64_t & ne02,
  545. constant int64_t & ne03,
  546. constant uint64_t & nb00,
  547. constant uint64_t & nb01,
  548. constant uint64_t & nb02,
  549. constant uint64_t & nb03,
  550. constant int64_t & ne0,
  551. constant int64_t & ne1,
  552. constant int64_t & ne2,
  553. constant int64_t & ne3,
  554. constant uint64_t & nb0,
  555. constant uint64_t & nb1,
  556. constant uint64_t & nb2,
  557. constant uint64_t & nb3,
  558. uint3 tgpig[[threadgroup_position_in_grid]],
  559. uint3 tpitg[[thread_position_in_threadgroup]],
  560. uint3 ntg[[threads_per_threadgroup]]) {
  561. const int64_t i03 = tgpig[2];
  562. const int64_t i02 = tgpig[1];
  563. const int64_t i01 = tgpig[0];
  564. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  565. const int64_t i3 = n / (ne2*ne1*ne0);
  566. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  567. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  568. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  569. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  570. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  571. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  572. dst_data[i00] = src[0];
  573. }
  574. }
  575. kernel void kernel_cpy_f32_f16(
  576. device const float * src0,
  577. device half * dst,
  578. constant int64_t & ne00,
  579. constant int64_t & ne01,
  580. constant int64_t & ne02,
  581. constant int64_t & ne03,
  582. constant uint64_t & nb00,
  583. constant uint64_t & nb01,
  584. constant uint64_t & nb02,
  585. constant uint64_t & nb03,
  586. constant int64_t & ne0,
  587. constant int64_t & ne1,
  588. constant int64_t & ne2,
  589. constant int64_t & ne3,
  590. constant uint64_t & nb0,
  591. constant uint64_t & nb1,
  592. constant uint64_t & nb2,
  593. constant uint64_t & nb3,
  594. uint3 tgpig[[threadgroup_position_in_grid]],
  595. uint3 tpitg[[thread_position_in_threadgroup]],
  596. uint3 ntg[[threads_per_threadgroup]]) {
  597. const int64_t i03 = tgpig[2];
  598. const int64_t i02 = tgpig[1];
  599. const int64_t i01 = tgpig[0];
  600. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  601. const int64_t i3 = n / (ne2*ne1*ne0);
  602. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  603. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  604. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  605. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  606. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  607. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  608. dst_data[i00] = src[0];
  609. }
  610. }
  611. kernel void kernel_cpy_f32_f32(
  612. device const float * src0,
  613. device float * dst,
  614. constant int64_t & ne00,
  615. constant int64_t & ne01,
  616. constant int64_t & ne02,
  617. constant int64_t & ne03,
  618. constant uint64_t & nb00,
  619. constant uint64_t & nb01,
  620. constant uint64_t & nb02,
  621. constant uint64_t & nb03,
  622. constant int64_t & ne0,
  623. constant int64_t & ne1,
  624. constant int64_t & ne2,
  625. constant int64_t & ne3,
  626. constant uint64_t & nb0,
  627. constant uint64_t & nb1,
  628. constant uint64_t & nb2,
  629. constant uint64_t & nb3,
  630. uint3 tgpig[[threadgroup_position_in_grid]],
  631. uint3 tpitg[[thread_position_in_threadgroup]],
  632. uint3 ntg[[threads_per_threadgroup]]) {
  633. const int64_t i03 = tgpig[2];
  634. const int64_t i02 = tgpig[1];
  635. const int64_t i01 = tgpig[0];
  636. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  637. const int64_t i3 = n / (ne2*ne1*ne0);
  638. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  639. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  640. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  641. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  642. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  643. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  644. dst_data[i00] = src[0];
  645. }
  646. }
  647. //============================================ k-quants ======================================================
  648. #ifndef QK_K
  649. #define QK_K 256
  650. #else
  651. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  652. #endif
  653. #if QK_K == 256
  654. #define K_SCALE_SIZE 12
  655. #else
  656. #define K_SCALE_SIZE 4
  657. #endif
  658. typedef struct {
  659. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  660. uint8_t qs[QK_K/4]; // quants
  661. half d; // super-block scale for quantized scales
  662. half dmin; // super-block scale for quantized mins
  663. } block_q2_K;
  664. // 84 bytes / block
  665. typedef struct {
  666. uint8_t hmask[QK_K/8]; // quants - high bit
  667. uint8_t qs[QK_K/4]; // quants - low 2 bits
  668. #if QK_K == 64
  669. uint8_t scales[2];
  670. #else
  671. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  672. #endif
  673. half d; // super-block scale
  674. } block_q3_K;
  675. #if QK_K == 64
  676. typedef struct {
  677. half d[2]; // super-block scales/mins
  678. uint8_t scales[2];
  679. uint8_t qs[QK_K/2]; // 4-bit quants
  680. } block_q4_K;
  681. #else
  682. typedef struct {
  683. half d; // super-block scale for quantized scales
  684. half dmin; // super-block scale for quantized mins
  685. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  686. uint8_t qs[QK_K/2]; // 4--bit quants
  687. } block_q4_K;
  688. #endif
  689. #if QK_K == 64
  690. typedef struct {
  691. half d; // super-block scales/mins
  692. int8_t scales[QK_K/16]; // 8-bit block scales
  693. uint8_t qh[QK_K/8]; // quants, high bit
  694. uint8_t qs[QK_K/2]; // quants, low 4 bits
  695. } block_q5_K;
  696. #else
  697. typedef struct {
  698. half d; // super-block scale for quantized scales
  699. half dmin; // super-block scale for quantized mins
  700. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  701. uint8_t qh[QK_K/8]; // quants, high bit
  702. uint8_t qs[QK_K/2]; // quants, low 4 bits
  703. } block_q5_K;
  704. // 176 bytes / block
  705. #endif
  706. typedef struct {
  707. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  708. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  709. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  710. half d; // super-block scale
  711. } block_q6_K;
  712. // 210 bytes / block
  713. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  714. uchar4 r;
  715. if (j < 4) {
  716. r[0] = q[j+0] & 63;
  717. r[2] = q[j+1] & 63;
  718. r[1] = q[j+4] & 63;
  719. r[3] = q[j+5] & 63;
  720. } else {
  721. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  722. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  723. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  724. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  725. }
  726. return r;
  727. }
  728. //========================================== dequantization =============================
  729. static void dequantize_row_q2_K(device const block_q2_K * x, device float * y, int k) {
  730. assert(k % QK_K == 0);
  731. const int nb = k / QK_K;
  732. for (int i = 0; i < nb; i++) {
  733. const float d = x[i].d;
  734. const float min = x[i].dmin;
  735. device const uint8_t * q = x[i].qs;
  736. #if QK_K == 256
  737. int is = 0;
  738. float dl, ml;
  739. for (int n = 0; n < QK_K; n += 128) {
  740. int shift = 0;
  741. for (int j = 0; j < 4; ++j) {
  742. uint8_t sc = x[i].scales[is++];
  743. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  744. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  745. sc = x[i].scales[is++];
  746. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  747. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  748. shift += 2;
  749. }
  750. q += 32;
  751. }
  752. #else
  753. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  754. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  755. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  756. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  757. for (int l = 0; l < 16; ++l) {
  758. y[l+ 0] = dl1 * ((q[l] >> 0) & 3) - ml1;
  759. y[l+16] = dl2 * ((q[l] >> 2) & 3) - ml2;
  760. y[l+32] = dl3 * ((q[l] >> 4) & 3) - ml3;
  761. y[l+48] = dl4 * ((q[l] >> 6) & 3) - ml4;
  762. }
  763. y += QK_K;
  764. #endif
  765. }
  766. }
  767. static void dequantize_row_q3_K(device const block_q3_K * x, device float * y, int k) {
  768. assert(k % QK_K == 0);
  769. const int nb = k / QK_K;
  770. #if QK_K == 256
  771. const uint16_t kmask1 = 0x0303;
  772. const uint16_t kmask2 = 0x0f0f;
  773. uint16_t aux[8];
  774. thread const int8_t * scales = (thread const int8_t*)aux;
  775. for (int i = 0; i < nb; i++) {
  776. const float d_all = (float)(x[i].d);
  777. device const uint8_t * q = x[i].qs;
  778. device const uint8_t * h = x[i].hmask;
  779. uint8_t m = 1;
  780. device const uint16_t * a = (device const uint16_t *)x[i].scales;
  781. aux[0] = (a[0] & kmask2) | (((a[4] >> 0) & kmask1) << 4);
  782. aux[1] = (a[1] & kmask2) | (((a[5] >> 0) & kmask1) << 4);
  783. aux[2] = (a[2] & kmask2) | (((a[4] >> 2) & kmask1) << 4);
  784. aux[3] = (a[3] & kmask2) | (((a[5] >> 2) & kmask1) << 4);
  785. aux[4] = ((a[0] >> 4) & kmask2) | (((a[4] >> 4) & kmask1) << 4);
  786. aux[5] = ((a[1] >> 4) & kmask2) | (((a[5] >> 4) & kmask1) << 4);
  787. aux[6] = ((a[2] >> 4) & kmask2) | (((a[4] >> 6) & kmask1) << 4);
  788. aux[7] = ((a[3] >> 4) & kmask2) | (((a[5] >> 6) & kmask1) << 4);
  789. int is = 0;
  790. float dl;
  791. for (int n = 0; n < QK_K; n += 128) {
  792. int shift = 0;
  793. for (int j = 0; j < 4; ++j) {
  794. dl = d_all * (scales[is++] - 32);
  795. for (int l = 0; l < 16; ++l) {
  796. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((h[l+ 0] & m) ? 0 : 4));
  797. }
  798. dl = d_all * (scales[is++] - 32);
  799. for (int l = 0; l < 16; ++l) {
  800. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((h[l+16] & m) ? 0 : 4));
  801. }
  802. shift += 2;
  803. m <<= 1;
  804. }
  805. q += 32;
  806. }
  807. }
  808. #else
  809. for (int i = 0; i < nb; i++) {
  810. const float d_all = (float)(x[i].d);
  811. device const uint8_t * q = x[i].qs;
  812. device const uint8_t * hm = x[i].hmask;
  813. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  814. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  815. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  816. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  817. for (int l = 0; l < 8; ++l) {
  818. uint8_t h = hm[l];
  819. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  820. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  821. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  822. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  823. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  824. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  825. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  826. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  827. }
  828. y += QK_K;
  829. }
  830. #endif
  831. }
  832. static void dequantize_row_q4_K(device const block_q4_K * x, device float * y, int k) {
  833. assert(k % QK_K == 0);
  834. const int nb = k / QK_K;
  835. for (int i = 0; i < nb; i++) {
  836. device const uint8_t * q = x[i].qs;
  837. #if QK_K == 256
  838. const float d = x[i].d;
  839. const float min = x[i].dmin;
  840. device const uint8_t * scales = x[i].scales;
  841. int is = 0;
  842. for (int j = 0; j < QK_K; j += 64) {
  843. const uchar4 sc = get_scale_min_k4(is, scales);
  844. const float d1 = d * sc[0]; const float m1 = min * sc[1];
  845. const float d2 = d * sc[2]; const float m2 = min * sc[3];
  846. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  847. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  848. q += 32; is += 2;
  849. }
  850. #else
  851. device const uint8_t * s = x[i].scales;
  852. device const half2 * dh = (device const half2 *)x[i].d;
  853. const float2 d = (float2)dh[0];
  854. const float d1 = d[0] * (s[0] & 0xF);
  855. const float d2 = d[0] * (s[1] & 0xF);
  856. const float m1 = d[1] * (s[0] >> 4);
  857. const float m2 = d[1] * (s[1] >> 4);
  858. for (int l = 0; l < 32; ++l) {
  859. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  860. y[l+32] = d2 * (q[l] >> 4) - m2;
  861. }
  862. y += QK_K;
  863. #endif
  864. }
  865. }
  866. static void dequantize_row_q5_K(device const block_q5_K * x, device float * y, int k) {
  867. assert(k % QK_K == 0);
  868. const int nb = k / QK_K;
  869. #if QK_K == 256
  870. for (int i = 0; i < nb; i++) {
  871. const float d = (float)(x[i].d);
  872. const float min = (float)(x[i].dmin);
  873. device const uint8_t * ql = x[i].qs;
  874. device const uint8_t * qh = x[i].qh;
  875. int is = 0;
  876. uint8_t u1 = 1, u2 = 2;
  877. for (int j = 0; j < QK_K; j += 64) {
  878. const uchar4 sc = get_scale_min_k4(is, x[i].scales);
  879. const float d1 = d * sc[0]; const float m1 = min * sc[1];
  880. const float d2 = d * sc[2]; const float m2 = min * sc[3];
  881. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  882. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  883. ql += 32; is += 2;
  884. u1 <<= 2; u2 <<= 2;
  885. }
  886. }
  887. #else
  888. for (int i = 0; i < nb; i++) {
  889. const float d = (float)x[i].d;
  890. device const uint8_t * ql = x[i].qs;
  891. device const uint8_t * qh = x[i].qh;
  892. device const int8_t * sc = x[i].scales;
  893. for (int l = 0; l < 8; ++l) {
  894. y[l+ 0] = d * sc[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  895. y[l+ 8] = d * sc[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  896. y[l+16] = d * sc[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  897. y[l+24] = d * sc[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  898. y[l+32] = d * sc[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  899. y[l+40] = d * sc[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  900. y[l+48] = d * sc[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  901. y[l+56] = d * sc[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  902. }
  903. y += QK_K;
  904. }
  905. #endif
  906. }
  907. static void dequantize_row_q6_K(device const block_q6_K * x, device float * y, int k) {
  908. assert(k % QK_K == 0);
  909. const int nb = k / QK_K;
  910. for (int i = 0; i < nb; i++) {
  911. device const uint8_t * ql = x[i].ql;
  912. device const uint8_t * qh = x[i].qh;
  913. device const int8_t * sc = x[i].scales;
  914. const float d = x[i].d;
  915. #if QK_K == 256
  916. for (int n = 0; n < QK_K; n += 128) {
  917. for (int l = 0; l < 32; ++l) {
  918. int is = l/16;
  919. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  920. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  921. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  922. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  923. y[l + 0] = d * sc[is + 0] * q1;
  924. y[l + 32] = d * sc[is + 2] * q2;
  925. y[l + 64] = d * sc[is + 4] * q3;
  926. y[l + 96] = d * sc[is + 6] * q4;
  927. }
  928. y += 128;
  929. ql += 64;
  930. qh += 32;
  931. sc += 8;
  932. }
  933. #else
  934. for (int l = 0; l < 16; ++l) {
  935. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  936. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  937. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  938. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  939. y[l+ 0] = d * sc[0] * q1;
  940. y[l+16] = d * sc[1] * q2;
  941. y[l+32] = d * sc[2] * q3;
  942. y[l+48] = d * sc[3] * q4;
  943. }
  944. y += 64;
  945. #endif
  946. }
  947. }
  948. kernel void kernel_get_rows_q2_K(
  949. device const void * src0,
  950. device const int * src1,
  951. device float * dst,
  952. constant int64_t & ne00,
  953. constant uint64_t & nb01,
  954. constant uint64_t & nb1,
  955. uint tpig[[thread_position_in_grid]]) {
  956. const int i = tpig;
  957. const int r = ((device int32_t *) src1)[i];
  958. dequantize_row_q2_K(
  959. (device const block_q2_K *) ((device char *) src0 + r*nb01),
  960. (device float *) ((device char *) dst + i*nb1), ne00);
  961. }
  962. kernel void kernel_get_rows_q3_K(
  963. device const void * src0,
  964. device const int * src1,
  965. device float * dst,
  966. constant int64_t & ne00,
  967. constant uint64_t & nb01,
  968. constant uint64_t & nb1,
  969. uint tpig[[thread_position_in_grid]]) {
  970. const int i = tpig;
  971. const int r = ((device int32_t *) src1)[i];
  972. dequantize_row_q3_K(
  973. (device const block_q3_K *) ((device char *) src0 + r*nb01),
  974. (device float *) ((device char *) dst + i*nb1), ne00);
  975. }
  976. kernel void kernel_get_rows_q4_K(
  977. device const void * src0,
  978. device const int * src1,
  979. device float * dst,
  980. constant int64_t & ne00,
  981. constant uint64_t & nb01,
  982. constant uint64_t & nb1,
  983. uint tpig[[thread_position_in_grid]]) {
  984. const int i = tpig;
  985. const int r = ((device int32_t *) src1)[i];
  986. dequantize_row_q4_K(
  987. (device const block_q4_K *) ((device char *) src0 + r*nb01),
  988. (device float *) ((device char *) dst + i*nb1), ne00);
  989. }
  990. kernel void kernel_get_rows_q5_K(
  991. device const void * src0,
  992. device const int * src1,
  993. device float * dst,
  994. constant int64_t & ne00,
  995. constant uint64_t & nb01,
  996. constant uint64_t & nb1,
  997. uint tpig[[thread_position_in_grid]]) {
  998. const int i = tpig;
  999. const int r = ((device int32_t *) src1)[i];
  1000. dequantize_row_q5_K(
  1001. (device const block_q5_K *) ((device char *) src0 + r*nb01),
  1002. (device float *) ((device char *) dst + i*nb1), ne00);
  1003. }
  1004. kernel void kernel_get_rows_q6_K(
  1005. device const void * src0,
  1006. device const int * src1,
  1007. device float * dst,
  1008. constant int64_t & ne00,
  1009. constant uint64_t & nb01,
  1010. constant uint64_t & nb1,
  1011. uint tpig[[thread_position_in_grid]]) {
  1012. const int i = tpig;
  1013. const int r = ((device int32_t *) src1)[i];
  1014. dequantize_row_q6_K(
  1015. (device const block_q6_K *) ((device char *) src0 + r*nb01),
  1016. (device float *) ((device char *) dst + i*nb1), ne00);
  1017. }
  1018. //====================================== dot products =========================
  1019. kernel void kernel_mul_mat_q2_K_f32(
  1020. device const void * src0,
  1021. device const float * src1,
  1022. device float * dst,
  1023. constant int64_t & ne00,
  1024. constant int64_t & ne10,
  1025. constant int64_t & ne0,
  1026. threadgroup float * sum [[threadgroup(0)]],
  1027. uint2 tgpig[[threadgroup_position_in_grid]],
  1028. uint2 tpitg[[thread_position_in_threadgroup]],
  1029. uint2 tptg[[threads_per_threadgroup]]) {
  1030. const int nb = ne00/QK_K;
  1031. const int64_t r0 = tgpig.x;
  1032. const int64_t r1 = tgpig.y;
  1033. device const block_q2_K * x = (device const block_q2_K *) src0 + r0*nb;
  1034. device const float * yy = (device const float *) src1 + r1*ne10;
  1035. const int nth = tptg.x*tptg.y;
  1036. const int ith = tptg.y*tpitg.x + tpitg.y;
  1037. float sumf = 0;
  1038. #if QK_K == 256
  1039. const int tid = tpitg.y; // 0...16
  1040. const int il = tid/4; // 0...3
  1041. const int ir = tid%4; // 0...3
  1042. const int ip = il/2; // 0 or 1
  1043. const int shift1 = 4*(il%2);// 0 or 4
  1044. const int shift2 = shift1+2;// 2 or 6
  1045. const int n = 8;
  1046. const int is = 4*il + (n*ir)/16;
  1047. const int y_offset = 64*il + n*ir;
  1048. const int q_offset = 32*ip + n*ir;
  1049. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1050. device const uint8_t * q = x[i].qs + q_offset;
  1051. device const uint8_t * scales = x[i].scales + is;
  1052. uint8_t d1 = scales[0] & 0xF;
  1053. uint8_t d2 = scales[2] & 0xF;
  1054. uint8_t m1 = scales[0] >> 4;
  1055. uint8_t m2 = scales[2] >> 4;
  1056. device const float * y = yy + i*QK_K + y_offset;
  1057. float2 s = {0.f, 0.f};
  1058. float smin = 0;
  1059. for (int l = 0; l < n; ++l) {
  1060. s[0] += y[l+ 0] * ((q[l] >> shift1) & 3);
  1061. s[1] += y[l+32] * ((q[l] >> shift2) & 3);
  1062. smin += y[l+ 0] * m1 + y[l+32] * m2;
  1063. }
  1064. const float dall = (float)x[i].d;
  1065. const float dmin = (float)x[i].dmin;
  1066. sumf += dall * (s[0] * d1 + s[1] * d2) - dmin * smin;
  1067. }
  1068. #else
  1069. const int il = 4 * tpitg.x;
  1070. uint32_t aux[2];
  1071. thread const uint8_t * d = (thread const uint8_t *)aux;
  1072. thread const uint8_t * m = (thread const uint8_t *)aux + 4;
  1073. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1074. device const uint8_t * q = x[i].qs + il;
  1075. device const float * y = yy + i*QK_K + il;
  1076. const float dall = (float)x[i].d;
  1077. const float dmin = (float)x[i].dmin;
  1078. device const uint32_t * a = (device const uint32_t *)x[i].scales;
  1079. aux[0] = a[0] & 0x0f0f0f0f;
  1080. aux[1] = (a[0] >> 4) & 0x0f0f0f0f;
  1081. for (int l = 0; l < 4; ++l) {
  1082. sumf += y[l+ 0] * (dall * d[0] * ((q[l] >> 0) & 3) - dmin * m[0])
  1083. + y[l+16] * (dall * d[1] * ((q[l] >> 2) & 3) - dmin * m[1])
  1084. + y[l+32] * (dall * d[2] * ((q[l] >> 4) & 3) - dmin * m[2])
  1085. + y[l+48] * (dall * d[3] * ((q[l] >> 6) & 3) - dmin * m[3]);
  1086. }
  1087. }
  1088. #endif
  1089. sum[ith] = sumf;
  1090. //
  1091. // Accumulate the sum from all threads in the threadgroup
  1092. //
  1093. threadgroup_barrier(mem_flags::mem_threadgroup);
  1094. if (ith%4 == 0) {
  1095. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1096. }
  1097. threadgroup_barrier(mem_flags::mem_threadgroup);
  1098. if (ith%16 == 0) {
  1099. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1100. }
  1101. threadgroup_barrier(mem_flags::mem_threadgroup);
  1102. if (ith == 0) {
  1103. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1104. dst[r1*ne0 + r0] = sum[0];
  1105. }
  1106. }
  1107. kernel void kernel_mul_mat_q3_K_f32(
  1108. device const void * src0,
  1109. device const float * src1,
  1110. device float * dst,
  1111. constant int64_t & ne00,
  1112. constant int64_t & ne10,
  1113. constant int64_t & ne0,
  1114. constant int64_t & ne1,
  1115. threadgroup float * sum [[threadgroup(0)]],
  1116. uint2 tgpig[[threadgroup_position_in_grid]],
  1117. uint2 tpitg[[thread_position_in_threadgroup]],
  1118. uint2 tptg[[threads_per_threadgroup]]) {
  1119. const int nb = ne00/QK_K;
  1120. const int64_t r0 = tgpig.x;
  1121. const int64_t r1 = tgpig.y;
  1122. device const block_q3_K * x = (device const block_q3_K *) src0 + r0*nb;
  1123. device const float * yy = (device const float *) src1 + r1*ne10;
  1124. const int nth = tptg.x*tptg.y;
  1125. const int ith = tptg.y*tpitg.x + tpitg.y;
  1126. #if QK_K == 256
  1127. const uint8_t m3 = 3;
  1128. const int8_t m4 = 4;
  1129. const uint16_t kmask1 = 0x0303;
  1130. const uint16_t kmask2 = 0x0f0f;
  1131. const int tid = tpitg.y; // expecting 16
  1132. const int ip = tid/8; // 0 or 1
  1133. const int il = tid/2 - 4*ip; // 0...3
  1134. const int ir = tid%2;
  1135. const int n = 8;
  1136. const int l0 = n*ir;
  1137. const uint8_t m = 1 << (4*ip + il);
  1138. const int shift = 2*il;
  1139. const uint16_t s_shift1 = 4*ip;
  1140. const uint16_t s_shift2 = s_shift1 + 2*(il/2);
  1141. const int ik = 4 + (il%2);
  1142. const int q_offset = 32*ip + l0;
  1143. const int y_offset = 128*ip + 32*il + l0;
  1144. //float sumf = 0;
  1145. float sumf1 = 0, sumf2 = 0;
  1146. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1147. const float d_all = (float)(x[i].d);
  1148. device const uint8_t * q = x[i].qs + q_offset;
  1149. device const uint8_t * h = x[i].hmask + l0;
  1150. device const float * y = yy + i * QK_K + y_offset;
  1151. device const uint16_t * a = (device const uint16_t *)x[i].scales;
  1152. const char2 scales = as_type<char2>((uint16_t)(((a[il] >> s_shift1) & kmask2) | (((a[ik] >> s_shift2) & kmask1) << 4)));
  1153. float s = 0;
  1154. for (int l = 0; l < n; ++l) {
  1155. s += y[l+ 0] * ((int8_t)((q[l+ 0] >> shift) & m3) - ((h[l+ 0] & m) ? 0 : m4));
  1156. }
  1157. float d = d_all * s;
  1158. sumf1 += d * scales[0];
  1159. sumf2 += d;
  1160. //sumf += d_all * s * (scales[0] - 32);
  1161. s = 0;
  1162. for (int l = 0; l < n; ++l) {
  1163. s += y[l+16] * ((int8_t)((q[l+16] >> shift) & m3) - ((h[l+16] & m) ? 0 : m4));
  1164. }
  1165. d = d_all * s;
  1166. sumf1 += d * scales[1];
  1167. sumf2 += d;
  1168. //sumf += d_all * s * (scales[1] - 32);
  1169. }
  1170. //sum[ith] = sumf;
  1171. sum[ith] = sumf1 - 32.f*sumf2;
  1172. #else
  1173. const int il = 4 * tpitg.x; // 0, 4, 8, 12
  1174. const int im = il/8; // 0, 0, 1, 1
  1175. const int in = il%8; // 0, 4, 0, 4
  1176. float sumf = 0;
  1177. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1178. const float d_all = (float)(x[i].d);
  1179. device const uint8_t * q = x[i].qs + il;
  1180. device const uint8_t * h = x[i].hmask + in;
  1181. device const float * y = yy + i * QK_K + il;
  1182. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1183. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1184. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1185. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1186. for (int l = 0; l < 4; ++l) {
  1187. const uint8_t hm = h[l] >> im;
  1188. sumf += y[l+ 0] * d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((hm & 0x01) ? 0 : 4))
  1189. + y[l+16] * d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((hm & 0x04) ? 0 : 4))
  1190. + y[l+32] * d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((hm & 0x10) ? 0 : 4))
  1191. + y[l+48] * d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((hm & 0x40) ? 0 : 4));
  1192. }
  1193. }
  1194. sum[ith] = sumf;
  1195. #endif
  1196. //
  1197. // Accumulate the sum from all threads in the threadgroup
  1198. //
  1199. threadgroup_barrier(mem_flags::mem_threadgroup);
  1200. if (ith%4 == 0) {
  1201. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1202. }
  1203. threadgroup_barrier(mem_flags::mem_threadgroup);
  1204. if (ith%16 == 0) {
  1205. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1206. }
  1207. threadgroup_barrier(mem_flags::mem_threadgroup);
  1208. if (ith == 0) {
  1209. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1210. dst[r1*ne0 + r0] = sum[0];
  1211. }
  1212. }
  1213. kernel void kernel_mul_mat_q4_K_f32(
  1214. device const void * src0,
  1215. device const float * src1,
  1216. device float * dst,
  1217. constant int64_t & ne00,
  1218. constant int64_t & ne10,
  1219. constant int64_t & ne0,
  1220. threadgroup float * sum [[threadgroup(0)]],
  1221. uint2 tgpig[[threadgroup_position_in_grid]],
  1222. uint2 tpitg[[thread_position_in_threadgroup]],
  1223. uint2 tptg[[threads_per_threadgroup]]) {
  1224. const int nb = ne00/QK_K;
  1225. const int64_t r0 = tgpig.x;
  1226. const int64_t r1 = tgpig.y;
  1227. const int nth = tptg.x*tptg.y;
  1228. const int ith = tptg.y*tpitg.x + tpitg.y;
  1229. device const block_q4_K * x = (device const block_q4_K *) src0 + r0*nb;
  1230. device const float * yy = (device const float *) src1 + r1*ne10;
  1231. float sumf = 0;
  1232. #if QK_K == 256
  1233. const uint16_t kmask1 = 0x3f3f;
  1234. const uint16_t kmask2 = 0x0f0f;
  1235. const uint16_t kmask3 = 0xc0c0;
  1236. const int tid = tpitg.y; // 0...16
  1237. const int il = tid/4; // 0...3
  1238. const int ir = tid - 4*il;// 0...3
  1239. const int n = 4;
  1240. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1241. const int in = il%2;
  1242. const int l0 = n*(2*ir + in);
  1243. const int q_offset = 32*im + l0;
  1244. const int y_offset = 64*im + l0;
  1245. uchar2 sc1, sc2, sc3, sc4;
  1246. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1247. device const uint8_t * q1 = (x + i)->qs + q_offset;
  1248. device const uint8_t * q2 = q1 + 64;
  1249. device const float * y1 = yy + i*QK_K + y_offset;
  1250. device const float * y2 = y1 + 128;
  1251. const float dall = (float)((x + i)->d);
  1252. const float dmin = (float)((x + i)->dmin);
  1253. device const uint16_t * a = (device const uint16_t *)(x + i)->scales;
  1254. sc1 = as_type<uchar2>((uint16_t)(a[im+0] & kmask1));
  1255. sc2 = as_type<uchar2>((uint16_t)(a[im+2] & kmask1));
  1256. sc3 = as_type<uchar2>((uint16_t)(((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2)));
  1257. sc4 = as_type<uchar2>((uint16_t)(((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2)));
  1258. float4 s = {0.f, 0.f, 0.f, 0.f};
  1259. float smin = 0;
  1260. for (int l = 0; l < n; ++l) {
  1261. s[0] += y1[l] * (q1[l] & 0xF); s[1] += y1[l+32] * (q1[l] >> 4);
  1262. s[2] += y2[l] * (q2[l] & 0xF); s[3] += y2[l+32] * (q2[l] >> 4);
  1263. smin += y1[l] * sc2[0] + y1[l+32] * sc2[1] + y2[l] * sc4[0] + y2[l+32] * sc4[1];
  1264. }
  1265. sumf += dall * (s[0] * sc1[0] + s[1] * sc1[1] + s[2] * sc3[0] + s[3] * sc3[1]) - dmin * smin;
  1266. }
  1267. #else
  1268. uint16_t aux16[2];
  1269. thread const uint8_t * scales = (thread const uint8_t *)aux16;
  1270. const int il = 4*tpitg.x;
  1271. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1272. device const uint8_t * q = x[i].qs + il;
  1273. device const float * y = yy + i * QK_K + il;
  1274. const float d = (float)x[i].d[0];
  1275. const float m = (float)x[i].d[1];
  1276. device const uint16_t * a = (device const uint16_t *)x[i].scales;
  1277. aux16[0] = a[0] & 0x0f0f;
  1278. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1279. for (int l = 0; l < 4; ++l) {
  1280. sumf += d * scales[0] * (y[l+ 0] * (q[l] & 0xF) + y[l+16] * (q[l+16] & 0xF)) - m * scales[2] * (y[l+ 0] + y[l+16])
  1281. + d * scales[1] * (y[l+32] * (q[l] >> 4) + y[l+48] * (q[l+16] >> 4)) - m * scales[3] * (y[l+32] + y[l+48]);
  1282. }
  1283. }
  1284. #endif
  1285. sum[ith] = sumf;
  1286. //
  1287. // Accumulate the sum from all threads in the threadgroup
  1288. // This version is slightly faster than the commented out one below,
  1289. // which I copy-pasted from ggerganov's q4_0 dot product for metal.
  1290. //
  1291. threadgroup_barrier(mem_flags::mem_threadgroup);
  1292. if (ith%4 == 0) {
  1293. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1294. }
  1295. threadgroup_barrier(mem_flags::mem_threadgroup);
  1296. if (ith%16 == 0) {
  1297. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1298. }
  1299. threadgroup_barrier(mem_flags::mem_threadgroup);
  1300. if (ith == 0) {
  1301. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1302. dst[r1*ne0 + r0] = sum[0];
  1303. }
  1304. //// accumulate the sum from all threads in the threadgroup
  1305. //threadgroup_barrier(mem_flags::mem_threadgroup);
  1306. //for (uint i = nth/2; i > 0; i /= 2) {
  1307. // if (ith < i) {
  1308. // sum[ith] += sum[ith + i];
  1309. // }
  1310. // threadgroup_barrier(mem_flags::mem_threadgroup);
  1311. //}
  1312. //if (ith == 0) {
  1313. // dst[r1*ne0 + r0] = sum[0];
  1314. //}
  1315. }
  1316. kernel void kernel_mul_mat_q5_K_f32(
  1317. device const void * src0,
  1318. device const float * src1,
  1319. device float * dst,
  1320. constant int64_t & ne00,
  1321. constant int64_t & ne10,
  1322. constant int64_t & ne0,
  1323. threadgroup float * sum [[threadgroup(0)]],
  1324. uint2 tgpig[[threadgroup_position_in_grid]],
  1325. uint2 tpitg[[thread_position_in_threadgroup]],
  1326. uint2 tptg[[threads_per_threadgroup]]) {
  1327. const int nb = ne00/QK_K;
  1328. const int64_t r0 = tgpig.x;
  1329. const int64_t r1 = tgpig.y;
  1330. device const block_q5_K * x = (device const block_q5_K *) src0 + r0*nb;
  1331. device const float * yy = (device const float *) src1 + r1*ne10;
  1332. const int nth = tptg.x*tptg.y;
  1333. const int ith = tptg.y*tpitg.x + tpitg.y;
  1334. float sumf = 0;
  1335. #if QK_K == 256
  1336. const uint16_t kmask1 = 0x3f3f;
  1337. const uint16_t kmask2 = 0x0f0f;
  1338. const uint16_t kmask3 = 0xc0c0;
  1339. const int tid = tpitg.y; // 0...16
  1340. const int il = tid/4; // 0...3
  1341. const int ir = tid - 4*il;// 0...3
  1342. const int n = 4;
  1343. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1344. const int in = il%2;
  1345. const int l0 = n*(2*ir + in);
  1346. const int q_offset = 32*im + l0;
  1347. const int y_offset = 64*im + l0;
  1348. const uint8_t hm1 = 1u << (2*im);
  1349. const uint8_t hm2 = hm1 << 1;
  1350. const uint8_t hm3 = hm1 << 4;
  1351. const uint8_t hm4 = hm2 << 4;
  1352. uchar2 sc1, sc2, sc3, sc4;
  1353. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1354. device const uint8_t * q1 = (x + i)->qs + q_offset;
  1355. device const uint8_t * q2 = q1 + 64;
  1356. device const uint8_t * qh = (x + i)->qh + l0;
  1357. device const float * y1 = yy + i*QK_K + y_offset;
  1358. device const float * y2 = y1 + 128;
  1359. const float dall = (float)((x + i)->d);
  1360. const float dmin = (float)((x + i)->dmin);
  1361. device const uint16_t * a = (device const uint16_t *)(x + i)->scales;
  1362. sc1 = as_type<uchar2>((uint16_t)(a[im+0] & kmask1));
  1363. sc2 = as_type<uchar2>((uint16_t)(a[im+2] & kmask1));
  1364. sc3 = as_type<uchar2>((uint16_t)(((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2)));
  1365. sc4 = as_type<uchar2>((uint16_t)(((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2)));
  1366. float4 s = {0.f, 0.f, 0.f, 0.f};
  1367. float smin = 0;
  1368. for (int l = 0; l < n; ++l) {
  1369. s[0] += y1[l+ 0] * ((q1[l] & 0xF) + (qh[l] & hm1 ? 16 : 0));
  1370. s[1] += y1[l+32] * ((q1[l] >> 4) + (qh[l] & hm2 ? 16 : 0));
  1371. s[2] += y2[l+ 0] * ((q2[l] & 0xF) + (qh[l] & hm3 ? 16 : 0));
  1372. s[3] += y2[l+32] * ((q2[l] >> 4) + (qh[l] & hm4 ? 16 : 0));
  1373. smin += y1[l] * sc2[0] + y1[l+32] * sc2[1] + y2[l] * sc4[0] + y2[l+32] * sc4[1];
  1374. }
  1375. sumf += dall * (s[0] * sc1[0] + s[1] * sc1[1] + s[2] * sc3[0] + s[3] * sc3[1]) - dmin * smin;
  1376. }
  1377. #else
  1378. const int il = 4 * tpitg.x; // 0, 4, 8, 12
  1379. const int im = il/8; // 0, 0, 1, 1
  1380. const int in = il%8; // 0, 4, 0, 4
  1381. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1382. const float d = (float)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. device const float * y = yy + i*QK_K + il;
  1387. for (int l = 0; l < 4; ++l) {
  1388. const uint8_t hl = h[l] >> im;
  1389. sumf += y[l+ 0] * d * s[0] * ((q[l+ 0] & 0xF) - (hl & 0x01 ? 0 : 16))
  1390. + y[l+16] * d * s[1] * ((q[l+16] & 0xF) - (hl & 0x04 ? 0 : 16))
  1391. + y[l+32] * d * s[2] * ((q[l+ 0] >> 4) - (hl & 0x10 ? 0 : 16))
  1392. + y[l+48] * d * s[3] * ((q[l+16] >> 4) - (hl & 0x40 ? 0 : 16));
  1393. }
  1394. }
  1395. #endif
  1396. sum[ith] = sumf;
  1397. //
  1398. // Accumulate the sum from all threads in the threadgroup
  1399. //
  1400. threadgroup_barrier(mem_flags::mem_threadgroup);
  1401. if (ith%4 == 0) {
  1402. sum[ith] += sum[ith+1] + sum[ith+2] + sum[ith+3];
  1403. }
  1404. threadgroup_barrier(mem_flags::mem_threadgroup);
  1405. if (ith%16 == 0) {
  1406. sum[ith] += sum[ith+4] + sum[ith+8] + sum[ith+12];
  1407. }
  1408. threadgroup_barrier(mem_flags::mem_threadgroup);
  1409. if (ith == 0) {
  1410. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1411. dst[r1*ne0 + r0] = sum[0];
  1412. }
  1413. }
  1414. kernel void kernel_mul_mat_q6_K_f32(
  1415. device const void * src0,
  1416. device const float * src1,
  1417. device float * dst,
  1418. constant int64_t & ne00,
  1419. constant int64_t & ne10,
  1420. constant int64_t & ne0,
  1421. threadgroup float * sum [[threadgroup(0)]],
  1422. uint2 tgpig[[threadgroup_position_in_grid]],
  1423. uint2 tpitg[[thread_position_in_threadgroup]],
  1424. uint2 tptg[[threads_per_threadgroup]]) {
  1425. const uint8_t kmask1 = 0x03;
  1426. const uint8_t kmask2 = 0x0C;
  1427. const uint8_t kmask3 = 0x30;
  1428. const uint8_t kmask4 = 0xC0;
  1429. const int nb = ne00/QK_K;
  1430. const int64_t r0 = tgpig.x;
  1431. const int64_t r1 = tgpig.y;
  1432. device const block_q6_K * x = (device const block_q6_K *) src0 + r0*nb;
  1433. device const float * yy = (device const float *) src1 + r1*ne10;
  1434. const int nth = tptg.x*tptg.y;
  1435. const int ith = tptg.y*tpitg.x + tpitg.y;
  1436. float sumf = 0;
  1437. #if QK_K == 256
  1438. // Note: we absolutely assume that tptg.y = 16 and QK_K = 256!
  1439. const int iqs = 16 * tpitg.y;
  1440. const int ip = iqs / 128; // 0 or 1
  1441. const int il = (iqs - 128*ip)/16; // 0...7
  1442. const int n = 4;
  1443. const int l0 = n*il;
  1444. const int is = 8*ip + l0/16;
  1445. const int y_offset = 128*ip + l0;
  1446. const int q_offset_l = 64*ip + l0;
  1447. const int q_offset_h = 32*ip + l0;
  1448. for (int i = tpitg.x; i < nb; i += tptg.x) {
  1449. device const uint8_t * ql = x[i].ql + q_offset_l;
  1450. device const uint8_t * qh = x[i].qh + q_offset_h;
  1451. device const int8_t * sc = x[i].scales + is;
  1452. device const float * y = yy + i * QK_K + y_offset;
  1453. const float dall = x[i].d;
  1454. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1455. for (int l = 0; l < n; ++l) {
  1456. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1457. sums[1] += y[l+32] * ((int8_t)((ql[l+32] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1458. sums[2] += y[l+64] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1459. sums[3] += y[l+96] * ((int8_t)((ql[l+32] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1460. }
  1461. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1462. }
  1463. #else
  1464. const int il = 4*tpitg.x; // 0, 4, 8, 12
  1465. for (int i = tpitg.y; i < nb; i += tptg.y) {
  1466. device const float * y = yy + i * QK_K + il;
  1467. device const uint8_t * ql = x[i].ql + il;
  1468. device const uint8_t * qh = x[i].qh + il;
  1469. device const int8_t * s = x[i].scales;
  1470. const float d = x[i].d;
  1471. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1472. for (int l = 0; l < 4; ++l) {
  1473. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1474. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1475. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1476. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1477. }
  1478. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1479. }
  1480. #endif
  1481. sum[ith] = sumf;
  1482. //
  1483. // Accumulate the sum from all threads in the threadgroup
  1484. //
  1485. threadgroup_barrier(mem_flags::mem_threadgroup);
  1486. if (ith%4 == 0) {
  1487. for (int i = 1; i < 4; ++i) sum[ith] += sum[ith + i];
  1488. }
  1489. threadgroup_barrier(mem_flags::mem_threadgroup);
  1490. if (ith%16 == 0) {
  1491. for (int i = 4; i < 16; i += 4) sum[ith] += sum[ith + i];
  1492. }
  1493. threadgroup_barrier(mem_flags::mem_threadgroup);
  1494. if (ith == 0) {
  1495. for (int i = 16; i < nth; i += 16) sum[0] += sum[i];
  1496. dst[r1*ne0 + r0] = sum[0];
  1497. }
  1498. }