ggml-metal.metal 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  4. #define QK4_0 32
  5. #define QR4_0 2
  6. typedef struct {
  7. half d; // delta
  8. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  9. } block_q4_0;
  10. #define QK4_1 32
  11. typedef struct {
  12. half d; // delta
  13. half m; // min
  14. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  15. } block_q4_1;
  16. #define QK5_0 32
  17. typedef struct {
  18. half d; // delta
  19. uint8_t qh[4]; // 5-th bit of quants
  20. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  21. } block_q5_0;
  22. #define QK5_1 32
  23. typedef struct {
  24. half d; // delta
  25. half m; // min
  26. uint8_t qh[4]; // 5-th bit of quants
  27. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  28. } block_q5_1;
  29. #define QK8_0 32
  30. typedef struct {
  31. half d; // delta
  32. int8_t qs[QK8_0]; // quants
  33. } block_q8_0;
  34. // general-purpose kernel for addition of two tensors
  35. // pros: works for non-contiguous tensors, supports broadcast across dims 1, 2 and 3
  36. // cons: not very efficient
  37. kernel void kernel_add(
  38. device const char * src0,
  39. device const char * src1,
  40. device char * dst,
  41. constant int64_t & ne00,
  42. constant int64_t & ne01,
  43. constant int64_t & ne02,
  44. constant int64_t & ne03,
  45. constant int64_t & nb00,
  46. constant int64_t & nb01,
  47. constant int64_t & nb02,
  48. constant int64_t & nb03,
  49. constant int64_t & ne10,
  50. constant int64_t & ne11,
  51. constant int64_t & ne12,
  52. constant int64_t & ne13,
  53. constant int64_t & nb10,
  54. constant int64_t & nb11,
  55. constant int64_t & nb12,
  56. constant int64_t & nb13,
  57. constant int64_t & ne0,
  58. constant int64_t & ne1,
  59. constant int64_t & ne2,
  60. constant int64_t & ne3,
  61. constant int64_t & nb0,
  62. constant int64_t & nb1,
  63. constant int64_t & nb2,
  64. constant int64_t & nb3,
  65. uint3 tgpig[[threadgroup_position_in_grid]],
  66. uint3 tpitg[[thread_position_in_threadgroup]],
  67. uint3 ntg[[threads_per_threadgroup]]) {
  68. const int64_t i03 = tgpig.z;
  69. const int64_t i02 = tgpig.y;
  70. const int64_t i01 = tgpig.x;
  71. const int64_t i13 = i03 % ne13;
  72. const int64_t i12 = i02 % ne12;
  73. const int64_t i11 = i01 % ne11;
  74. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01 + tpitg.x*nb00;
  75. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11 + tpitg.x*nb10;
  76. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + tpitg.x*nb0;
  77. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  78. ((device float *)dst_ptr)[0] = ((device float *)src0_ptr)[0] + ((device float *)src1_ptr)[0];
  79. src0_ptr += ntg.x*nb00;
  80. src1_ptr += ntg.x*nb10;
  81. dst_ptr += ntg.x*nb0;
  82. }
  83. }
  84. // assumption: src1 is a row
  85. // broadcast src1 into src0
  86. kernel void kernel_add_row(
  87. device const float4 * src0,
  88. device const float4 * src1,
  89. device float4 * dst,
  90. constant int64_t & nb [[buffer(27)]],
  91. uint tpig[[thread_position_in_grid]]) {
  92. dst[tpig] = src0[tpig] + src1[tpig % nb];
  93. }
  94. kernel void kernel_mul(
  95. device const float4 * src0,
  96. device const float4 * src1,
  97. device float4 * dst,
  98. uint tpig[[thread_position_in_grid]]) {
  99. dst[tpig] = src0[tpig] * src1[tpig];
  100. }
  101. // assumption: src1 is a row
  102. // broadcast src1 into src0
  103. kernel void kernel_mul_row(
  104. device const float4 * src0,
  105. device const float4 * src1,
  106. device float4 * dst,
  107. constant int64_t & nb,
  108. uint tpig[[thread_position_in_grid]]) {
  109. dst[tpig] = src0[tpig] * src1[tpig % nb];
  110. }
  111. kernel void kernel_scale(
  112. device const float4 * src0,
  113. device float4 * dst,
  114. constant float & scale,
  115. uint tpig[[thread_position_in_grid]]) {
  116. dst[tpig] = src0[tpig] * scale;
  117. }
  118. kernel void kernel_silu(
  119. device const float4 * src0,
  120. device float4 * dst,
  121. uint tpig[[thread_position_in_grid]]) {
  122. device const float4 & x = src0[tpig];
  123. dst[tpig] = x / (1.0f + exp(-x));
  124. }
  125. kernel void kernel_relu(
  126. device const float * src0,
  127. device float * dst,
  128. uint tpig[[thread_position_in_grid]]) {
  129. dst[tpig] = max(0.0f, src0[tpig]);
  130. }
  131. kernel void kernel_sqr(
  132. device const float * src0,
  133. device float * dst,
  134. uint tpig[[thread_position_in_grid]]) {
  135. dst[tpig] = src0[tpig] * src0[tpig];
  136. }
  137. constant float GELU_COEF_A = 0.044715f;
  138. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  139. kernel void kernel_gelu(
  140. device const float4 * src0,
  141. device float4 * dst,
  142. uint tpig[[thread_position_in_grid]]) {
  143. device const float4 & x = src0[tpig];
  144. // BEWARE !!!
  145. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  146. // This was observed with Falcon 7B and 40B models
  147. //
  148. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  149. }
  150. kernel void kernel_soft_max(
  151. device const float * src0,
  152. device float * dst,
  153. constant int64_t & ne00,
  154. constant int64_t & ne01,
  155. constant int64_t & ne02,
  156. uint3 tgpig[[threadgroup_position_in_grid]],
  157. uint3 tpitg[[thread_position_in_threadgroup]],
  158. uint3 ntg[[threads_per_threadgroup]]) {
  159. const int64_t i03 = tgpig[2];
  160. const int64_t i02 = tgpig[1];
  161. const int64_t i01 = tgpig[0];
  162. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  163. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  164. // parallel max
  165. float lmax = tpitg[0] < ne00 ? psrc0[tpitg[0]] : -INFINITY;
  166. for (int i00 = tpitg[0] + ntg[0]; i00 < ne00; i00 += ntg[0]) {
  167. lmax = MAX(lmax, psrc0[i00]);
  168. }
  169. const float max = simd_max(lmax);
  170. // parallel sum
  171. float lsum = 0.0f;
  172. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  173. const float exp_psrc0 = exp(psrc0[i00] - max);
  174. lsum += exp_psrc0;
  175. // Remember the result of exp here. exp is expensive, so we really do not
  176. // whish to compute it twice.
  177. pdst[i00] = exp_psrc0;
  178. }
  179. const float sum = simd_sum(lsum);
  180. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  181. pdst[i00] /= sum;
  182. }
  183. }
  184. kernel void kernel_soft_max_4(
  185. device const float * src0,
  186. device float * dst,
  187. constant int64_t & ne00,
  188. constant int64_t & ne01,
  189. constant int64_t & ne02,
  190. uint3 tgpig[[threadgroup_position_in_grid]],
  191. uint3 tpitg[[thread_position_in_threadgroup]],
  192. uint3 ntg[[threads_per_threadgroup]]) {
  193. const int64_t i03 = tgpig[2];
  194. const int64_t i02 = tgpig[1];
  195. const int64_t i01 = tgpig[0];
  196. device const float4 * psrc4 = (device const float4 *)(src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  197. device float4 * pdst4 = (device float4 *)(dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  198. // parallel max
  199. float4 lmax4 = tpitg[0] < ne00/4 ? psrc4[tpitg[0]] : -INFINITY;
  200. for (int i00 = tpitg[0] + ntg[0]; i00 < ne00/4; i00 += ntg[0]) {
  201. lmax4 = fmax(lmax4, psrc4[i00]);
  202. }
  203. float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  204. const float max = simd_max(lmax);
  205. // parallel sum
  206. float4 lsum4 = 0.0f;
  207. for (int i00 = tpitg[0]; i00 < ne00/4; i00 += ntg[0]) {
  208. const float4 exp_psrc4 = exp(psrc4[i00] - max);
  209. lsum4 += exp_psrc4;
  210. pdst4[i00] = exp_psrc4;
  211. }
  212. float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  213. const float sum = simd_sum(lsum);
  214. for (int i00 = tpitg[0]; i00 < ne00/4; i00 += ntg[0]) {
  215. pdst4[i00] /= sum;
  216. }
  217. }
  218. kernel void kernel_diag_mask_inf(
  219. device const float * src0,
  220. device float * dst,
  221. constant int64_t & ne00,
  222. constant int64_t & ne01,
  223. constant int & n_past,
  224. uint3 tpig[[thread_position_in_grid]]) {
  225. const int64_t i02 = tpig[2];
  226. const int64_t i01 = tpig[1];
  227. const int64_t i00 = tpig[0];
  228. if (i00 > n_past + i01) {
  229. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  230. } else {
  231. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  232. }
  233. }
  234. kernel void kernel_diag_mask_inf_8(
  235. device const float4 * src0,
  236. device float4 * dst,
  237. constant int64_t & ne00,
  238. constant int64_t & ne01,
  239. constant int & n_past,
  240. uint3 tpig[[thread_position_in_grid]]) {
  241. const int64_t i = 2*tpig[0];
  242. dst[i+0] = src0[i+0];
  243. dst[i+1] = src0[i+1];
  244. int64_t i4 = 4*i;
  245. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  246. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  247. const int64_t i00 = i4;
  248. for (int k = 3; k >= 0; --k) {
  249. if (i00 + 4 + k <= n_past + i01) {
  250. break;
  251. }
  252. dst[i+1][k] = -INFINITY;
  253. if (i00 + k > n_past + i01) {
  254. dst[i][k] = -INFINITY;
  255. }
  256. }
  257. }
  258. kernel void kernel_norm(
  259. device const void * src0,
  260. device float * dst,
  261. constant int64_t & ne00,
  262. constant uint64_t & nb01,
  263. constant float & eps,
  264. threadgroup float * sum [[threadgroup(0)]],
  265. uint tgpig[[threadgroup_position_in_grid]],
  266. uint tpitg[[thread_position_in_threadgroup]],
  267. uint ntg[[threads_per_threadgroup]]) {
  268. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  269. // MEAN
  270. // parallel sum
  271. sum[tpitg] = 0.0f;
  272. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  273. sum[tpitg] += x[i00];
  274. }
  275. // reduce
  276. threadgroup_barrier(mem_flags::mem_threadgroup);
  277. for (uint i = ntg/2; i > 0; i /= 2) {
  278. if (tpitg < i) {
  279. sum[tpitg] += sum[tpitg + i];
  280. }
  281. threadgroup_barrier(mem_flags::mem_threadgroup);
  282. }
  283. const float mean = sum[0] / ne00;
  284. // recenter and VARIANCE
  285. threadgroup_barrier(mem_flags::mem_threadgroup);
  286. device float * y = dst + tgpig*ne00;
  287. sum[tpitg] = 0.0f;
  288. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  289. y[i00] = x[i00] - mean;
  290. sum[tpitg] += y[i00] * y[i00];
  291. }
  292. // reduce
  293. threadgroup_barrier(mem_flags::mem_threadgroup);
  294. for (uint i = ntg/2; i > 0; i /= 2) {
  295. if (tpitg < i) {
  296. sum[tpitg] += sum[tpitg + i];
  297. }
  298. threadgroup_barrier(mem_flags::mem_threadgroup);
  299. }
  300. const float variance = sum[0] / ne00;
  301. const float scale = 1.0f/sqrt(variance + eps);
  302. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  303. y[i00] = y[i00] * scale;
  304. }
  305. }
  306. kernel void kernel_rms_norm(
  307. device const void * src0,
  308. device float * dst,
  309. constant int64_t & ne00,
  310. constant uint64_t & nb01,
  311. constant float & eps,
  312. threadgroup float * sum [[threadgroup(0)]],
  313. uint tgpig[[threadgroup_position_in_grid]],
  314. uint tpitg[[thread_position_in_threadgroup]],
  315. uint sgitg[[simdgroup_index_in_threadgroup]],
  316. uint tiisg[[thread_index_in_simdgroup]],
  317. uint ntg[[threads_per_threadgroup]]) {
  318. device const float4 * x = (device const float4 *) ((device const char *) src0 + tgpig*nb01);
  319. device const float * x_scalar = (device const float *) x;
  320. float4 sumf = 0;
  321. float all_sum = 0;
  322. // parallel sum
  323. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  324. sumf += x[i00] * x[i00];
  325. }
  326. all_sum = sumf[0] + sumf[1] + sumf[2] + sumf[3];
  327. all_sum = simd_sum(all_sum);
  328. if (tiisg == 0) {
  329. sum[sgitg] = all_sum;
  330. }
  331. threadgroup_barrier(mem_flags::mem_threadgroup);
  332. // broadcast, simd group number is ntg / 32
  333. for (uint i = ntg / 32 / 2; i > 0; i /= 2) {
  334. if (tpitg < i) {
  335. sum[tpitg] += sum[tpitg + i];
  336. }
  337. }
  338. if (tpitg == 0) {
  339. for (int i = 4 * (ne00 / 4); i < ne00; i++) {
  340. sum[0] += x_scalar[i];
  341. }
  342. sum[0] /= ne00;
  343. }
  344. threadgroup_barrier(mem_flags::mem_threadgroup);
  345. const float mean = sum[0];
  346. const float scale = 1.0f/sqrt(mean + eps);
  347. device float4 * y = (device float4 *) (dst + tgpig*ne00);
  348. device float * y_scalar = (device float *) y;
  349. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  350. y[i00] = x[i00] * scale;
  351. }
  352. if (tpitg == 0) {
  353. for (int i00 = 4 * (ne00 / 4); i00 < ne00; i00++) {
  354. y_scalar[i00] = x_scalar[i00] * scale;
  355. }
  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. float2 acc = 0.f;
  382. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 2 + il/2);
  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. // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i])
  392. // il indicates where the q5 quants begin (0 or QK5_0/4)
  393. // we assume that the yl's have been multiplied with the appropriate scale factor
  394. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  395. inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) {
  396. float d = qb_curr->d;
  397. float2 acc = 0.f;
  398. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2);
  399. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  400. for (int i = 0; i < 8; i+=2) {
  401. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010))
  402. + yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  403. acc[1] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100))
  404. + yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  405. }
  406. return d * (sumy * -16.f + acc[0] + acc[1]);
  407. }
  408. // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i])
  409. // il indicates where the q5 quants begin (0 or QK5_1/4)
  410. // we assume that the yl's have been multiplied with the appropriate scale factor
  411. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  412. inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) {
  413. float d = qb_curr->d;
  414. float m = qb_curr->m;
  415. float2 acc = 0.f;
  416. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2);
  417. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  418. for (int i = 0; i < 8; i+=2) {
  419. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010))
  420. + yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  421. acc[1] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100))
  422. + yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  423. }
  424. return d * (acc[0] + acc[1]) + sumy * m;
  425. }
  426. // putting them in the kernel cause a significant performance penalty
  427. #define N_DST 4 // each SIMD group works on 4 rows
  428. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  429. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  430. //Note: This is a template, but strictly speaking it only applies to
  431. // quantizations where the block size is 32. It also does not
  432. // giard against the number of rows not being divisible by
  433. // N_DST, so this is another explicit assumption of the implementation.
  434. template<typename block_q_type, int nr, int nsg, int nw>
  435. void mul_vec_q_n_f32(device const void * src0, device const float * src1, device float * dst,
  436. int64_t ne00, int64_t ne01, int64_t ne02, int64_t ne10, int64_t ne12, int64_t ne0, int64_t ne1, uint gqa,
  437. uint3 tgpig, uint tiisg, uint sgitg) {
  438. const int nb = ne00/QK4_0;
  439. const int r0 = tgpig.x;
  440. const int r1 = tgpig.y;
  441. const int im = tgpig.z;
  442. const int first_row = (r0 * nsg + sgitg) * nr;
  443. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  444. device const block_q_type * x = (device const block_q_type *) src0 + offset0;
  445. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  446. float yl[16]; // src1 vector cache
  447. float sumf[nr] = {0.f};
  448. const int ix = (tiisg/2);
  449. const int il = (tiisg%2)*8;
  450. device const float * yb = y + ix * QK4_0 + il;
  451. // each thread in a SIMD group deals with half a block.
  452. for (int ib = ix; ib < nb; ib += nw/2) {
  453. float sumy = 0;
  454. for (int i = 0; i < 8; i += 2) {
  455. sumy += yb[i] + yb[i+1];
  456. yl[i+0] = yb[i+ 0];
  457. yl[i+1] = yb[i+ 1]/256.f;
  458. sumy += yb[i+16] + yb[i+17];
  459. yl[i+8] = yb[i+16]/16.f;
  460. yl[i+9] = yb[i+17]/4096.f;
  461. }
  462. for (int row = 0; row < nr; row++) {
  463. sumf[row] += block_q_n_dot_y(x+ib+row*nb, sumy, yl, il);
  464. }
  465. yb += QK4_0 * 16;
  466. }
  467. for (int row = 0; row < nr; ++row) {
  468. const float tot = simd_sum(sumf[row]);
  469. if (tiisg == 0 && first_row + row < ne01) {
  470. dst[im*ne0*ne1 + r1*ne0 + first_row + row] = tot;
  471. }
  472. }
  473. }
  474. kernel void kernel_mul_mv_q4_0_f32(
  475. device const void * src0,
  476. device const float * src1,
  477. device float * dst,
  478. constant int64_t & ne00,
  479. constant int64_t & ne01[[buffer(4)]],
  480. constant int64_t & ne02[[buffer(5)]],
  481. constant int64_t & ne10[[buffer(9)]],
  482. constant int64_t & ne12[[buffer(11)]],
  483. constant int64_t & ne0[[buffer(15)]],
  484. constant int64_t & ne1[[buffer(16)]],
  485. constant uint & gqa[[buffer(17)]],
  486. uint3 tgpig[[threadgroup_position_in_grid]],
  487. uint tiisg[[thread_index_in_simdgroup]],
  488. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  489. mul_vec_q_n_f32<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  490. }
  491. kernel void kernel_mul_mv_q4_1_f32(
  492. device const void * src0,
  493. device const float * src1,
  494. device float * dst,
  495. constant int64_t & ne00,
  496. constant int64_t & ne01[[buffer(4)]],
  497. constant int64_t & ne02[[buffer(5)]],
  498. constant int64_t & ne10[[buffer(9)]],
  499. constant int64_t & ne12[[buffer(11)]],
  500. constant int64_t & ne0[[buffer(15)]],
  501. constant int64_t & ne1[[buffer(16)]],
  502. constant uint & gqa[[buffer(17)]],
  503. uint3 tgpig[[threadgroup_position_in_grid]],
  504. uint tiisg[[thread_index_in_simdgroup]],
  505. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  506. mul_vec_q_n_f32<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  507. }
  508. kernel void kernel_mul_mv_q5_0_f32(
  509. device const void * src0,
  510. device const float * src1,
  511. device float * dst,
  512. constant int64_t & ne00,
  513. constant int64_t & ne01[[buffer(4)]],
  514. constant int64_t & ne02[[buffer(5)]],
  515. constant int64_t & ne10[[buffer(9)]],
  516. constant int64_t & ne12[[buffer(11)]],
  517. constant int64_t & ne0[[buffer(15)]],
  518. constant int64_t & ne1[[buffer(16)]],
  519. constant uint & gqa[[buffer(17)]],
  520. uint3 tgpig[[threadgroup_position_in_grid]],
  521. uint tiisg[[thread_index_in_simdgroup]],
  522. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  523. mul_vec_q_n_f32<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  524. }
  525. kernel void kernel_mul_mv_q5_1_f32(
  526. device const void * src0,
  527. device const float * src1,
  528. device float * dst,
  529. constant int64_t & ne00,
  530. constant int64_t & ne01[[buffer(4)]],
  531. constant int64_t & ne02[[buffer(5)]],
  532. constant int64_t & ne10[[buffer(9)]],
  533. constant int64_t & ne12[[buffer(11)]],
  534. constant int64_t & ne0[[buffer(15)]],
  535. constant int64_t & ne1[[buffer(16)]],
  536. constant uint & gqa[[buffer(17)]],
  537. uint3 tgpig[[threadgroup_position_in_grid]],
  538. uint tiisg[[thread_index_in_simdgroup]],
  539. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  540. mul_vec_q_n_f32<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  541. }
  542. #define NB_Q8_0 8
  543. kernel void kernel_mul_mv_q8_0_f32(
  544. device const void * src0,
  545. device const float * src1,
  546. device float * dst,
  547. constant int64_t & ne00,
  548. constant int64_t & ne01[[buffer(4)]],
  549. constant int64_t & ne02[[buffer(5)]],
  550. constant int64_t & ne10[[buffer(9)]],
  551. constant int64_t & ne12[[buffer(11)]],
  552. constant int64_t & ne0[[buffer(15)]],
  553. constant int64_t & ne1[[buffer(16)]],
  554. constant uint & gqa[[buffer(17)]],
  555. uint3 tgpig[[threadgroup_position_in_grid]],
  556. uint tiisg[[thread_index_in_simdgroup]],
  557. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  558. const int nr = N_DST;
  559. const int nsg = N_SIMDGROUP;
  560. const int nw = N_SIMDWIDTH;
  561. const int nb = ne00/QK8_0;
  562. const int r0 = tgpig.x;
  563. const int r1 = tgpig.y;
  564. const int im = tgpig.z;
  565. const int first_row = (r0 * nsg + sgitg) * nr;
  566. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  567. device const block_q8_0 * x = (device const block_q8_0 *) src0 + offset0;
  568. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  569. float yl[NB_Q8_0];
  570. float sumf[nr]={0.f};
  571. const int ix = tiisg/4;
  572. const int il = tiisg%4;
  573. device const float * yb = y + ix * QK8_0 + NB_Q8_0*il;
  574. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  575. for (int ib = ix; ib < nb; ib += nw/4) {
  576. for (int i = 0; i < NB_Q8_0; ++i) {
  577. yl[i] = yb[i];
  578. }
  579. for (int row = 0; row < nr; row++) {
  580. device const int8_t * qs = x[ib+row*nb].qs + NB_Q8_0*il;
  581. float sumq = 0.f;
  582. for (int iq = 0; iq < NB_Q8_0; ++iq) {
  583. sumq += qs[iq] * yl[iq];
  584. }
  585. sumf[row] += sumq*x[ib+row*nb].d;
  586. }
  587. yb += NB_Q8_0 * nw;
  588. }
  589. for (int row = 0; row < nr; ++row) {
  590. const float tot = simd_sum(sumf[row]);
  591. if (tiisg == 0 && first_row + row < ne01) {
  592. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  593. }
  594. }
  595. }
  596. #define N_F32_F32 4
  597. kernel void kernel_mul_mv_f32_f32(
  598. device const char * src0,
  599. device const char * src1,
  600. device float * dst,
  601. constant int64_t & ne00,
  602. constant int64_t & ne01,
  603. constant int64_t & ne02,
  604. constant uint64_t & nb00,
  605. constant uint64_t & nb01,
  606. constant uint64_t & nb02,
  607. constant int64_t & ne10,
  608. constant int64_t & ne11,
  609. constant int64_t & ne12,
  610. constant uint64_t & nb10,
  611. constant uint64_t & nb11,
  612. constant uint64_t & nb12,
  613. constant int64_t & ne0,
  614. constant int64_t & ne1,
  615. uint3 tgpig[[threadgroup_position_in_grid]],
  616. uint tiisg[[thread_index_in_simdgroup]]) {
  617. const int64_t r0 = tgpig.x;
  618. const int64_t rb = tgpig.y*N_F32_F32;
  619. const int64_t im = tgpig.z;
  620. device const float * x = (device const float *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  621. if (ne00 < 128) {
  622. for (int row = 0; row < N_F32_F32; ++row) {
  623. int r1 = rb + row;
  624. if (r1 >= ne11) {
  625. break;
  626. }
  627. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  628. float sumf = 0;
  629. for (int i = tiisg; i < ne00; i += 32) {
  630. sumf += (float) x[i] * (float) y[i];
  631. }
  632. float all_sum = simd_sum(sumf);
  633. if (tiisg == 0) {
  634. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  635. }
  636. }
  637. } else {
  638. device const float4 * x4 = (device const float4 *)x;
  639. for (int row = 0; row < N_F32_F32; ++row) {
  640. int r1 = rb + row;
  641. if (r1 >= ne11) {
  642. break;
  643. }
  644. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  645. device const float4 * y4 = (device const float4 *) y;
  646. float sumf = 0;
  647. for (int i = tiisg; i < ne00/4; i += 32) {
  648. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  649. }
  650. float all_sum = simd_sum(sumf);
  651. if (tiisg == 0) {
  652. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  653. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  654. }
  655. }
  656. }
  657. }
  658. kernel void kernel_mul_mv_f16_f32_1row(
  659. device const char * src0,
  660. device const char * src1,
  661. device float * dst,
  662. constant int64_t & ne00,
  663. constant int64_t & ne01,
  664. constant int64_t & ne02,
  665. constant uint64_t & nb00,
  666. constant uint64_t & nb01,
  667. constant uint64_t & nb02,
  668. constant int64_t & ne10,
  669. constant int64_t & ne11,
  670. constant int64_t & ne12,
  671. constant uint64_t & nb10,
  672. constant uint64_t & nb11,
  673. constant uint64_t & nb12,
  674. constant int64_t & ne0,
  675. constant int64_t & ne1,
  676. uint3 tgpig[[threadgroup_position_in_grid]],
  677. uint tiisg[[thread_index_in_simdgroup]]) {
  678. const int64_t r0 = tgpig.x;
  679. const int64_t r1 = tgpig.y;
  680. const int64_t im = tgpig.z;
  681. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  682. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  683. float sumf = 0;
  684. if (ne00 < 128) {
  685. for (int i = tiisg; i < ne00; i += 32) {
  686. sumf += (float) x[i] * (float) y[i];
  687. }
  688. float all_sum = simd_sum(sumf);
  689. if (tiisg == 0) {
  690. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  691. }
  692. } else {
  693. device const half4 * x4 = (device const half4 *) x;
  694. device const float4 * y4 = (device const float4 *) y;
  695. for (int i = tiisg; i < ne00/4; i += 32) {
  696. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  697. }
  698. float all_sum = simd_sum(sumf);
  699. if (tiisg == 0) {
  700. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  701. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  702. }
  703. }
  704. }
  705. #define N_F16_F32 4
  706. kernel void kernel_mul_mv_f16_f32(
  707. device const char * src0,
  708. device const char * src1,
  709. device float * dst,
  710. constant int64_t & ne00,
  711. constant int64_t & ne01,
  712. constant int64_t & ne02,
  713. constant uint64_t & nb00,
  714. constant uint64_t & nb01,
  715. constant uint64_t & nb02,
  716. constant int64_t & ne10,
  717. constant int64_t & ne11,
  718. constant int64_t & ne12,
  719. constant uint64_t & nb10,
  720. constant uint64_t & nb11,
  721. constant uint64_t & nb12,
  722. constant int64_t & ne0,
  723. constant int64_t & ne1,
  724. uint3 tgpig[[threadgroup_position_in_grid]],
  725. uint tiisg[[thread_index_in_simdgroup]]) {
  726. const int64_t r0 = tgpig.x;
  727. const int64_t rb = tgpig.y*N_F16_F32;
  728. const int64_t im = tgpig.z;
  729. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  730. if (ne00 < 128) {
  731. for (int row = 0; row < N_F16_F32; ++row) {
  732. int r1 = rb + row;
  733. if (r1 >= ne11) {
  734. break;
  735. }
  736. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  737. float sumf = 0;
  738. for (int i = tiisg; i < ne00; i += 32) {
  739. sumf += (float) x[i] * (float) y[i];
  740. }
  741. float all_sum = simd_sum(sumf);
  742. if (tiisg == 0) {
  743. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  744. }
  745. }
  746. } else {
  747. device const half4 * x4 = (device const half4 *)x;
  748. for (int row = 0; row < N_F16_F32; ++row) {
  749. int r1 = rb + row;
  750. if (r1 >= ne11) {
  751. break;
  752. }
  753. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  754. device const float4 * y4 = (device const float4 *) y;
  755. float sumf = 0;
  756. for (int i = tiisg; i < ne00/4; i += 32) {
  757. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  758. }
  759. float all_sum = simd_sum(sumf);
  760. if (tiisg == 0) {
  761. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  762. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  763. }
  764. }
  765. }
  766. }
  767. // Assumes row size (ne00) is a multiple of 4
  768. kernel void kernel_mul_mv_f16_f32_l4(
  769. device const char * src0,
  770. device const char * src1,
  771. device float * dst,
  772. constant int64_t & ne00,
  773. constant int64_t & ne01,
  774. constant int64_t & ne02,
  775. constant uint64_t & nb00,
  776. constant uint64_t & nb01,
  777. constant uint64_t & nb02,
  778. constant int64_t & ne10,
  779. constant int64_t & ne11,
  780. constant int64_t & ne12,
  781. constant uint64_t & nb10,
  782. constant uint64_t & nb11,
  783. constant uint64_t & nb12,
  784. constant int64_t & ne0,
  785. constant int64_t & ne1,
  786. uint3 tgpig[[threadgroup_position_in_grid]],
  787. uint tiisg[[thread_index_in_simdgroup]]) {
  788. const int nrows = ne11;
  789. const int64_t r0 = tgpig.x;
  790. const int64_t im = tgpig.z;
  791. device const half4 * x4 = (device const half4 *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  792. for (int r1 = 0; r1 < nrows; ++r1) {
  793. device const float4 * y4 = (device const float4 *) (src1 + r1*nb11 + im*nb12);
  794. float sumf = 0;
  795. for (int i = tiisg; i < ne00/4; i += 32) {
  796. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  797. }
  798. float all_sum = simd_sum(sumf);
  799. if (tiisg == 0) {
  800. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  801. }
  802. }
  803. }
  804. kernel void kernel_alibi_f32(
  805. device const float * src0,
  806. device float * dst,
  807. constant int64_t & ne00,
  808. constant int64_t & ne01,
  809. constant int64_t & ne02,
  810. constant int64_t & ne03,
  811. constant uint64_t & nb00,
  812. constant uint64_t & nb01,
  813. constant uint64_t & nb02,
  814. constant uint64_t & nb03,
  815. constant int64_t & ne0,
  816. constant int64_t & ne1,
  817. constant int64_t & ne2,
  818. constant int64_t & ne3,
  819. constant uint64_t & nb0,
  820. constant uint64_t & nb1,
  821. constant uint64_t & nb2,
  822. constant uint64_t & nb3,
  823. constant float & m0,
  824. constant float & m1,
  825. constant int & n_heads_log2_floor,
  826. uint3 tgpig[[threadgroup_position_in_grid]],
  827. uint3 tpitg[[thread_position_in_threadgroup]],
  828. uint3 ntg[[threads_per_threadgroup]]) {
  829. const int64_t i03 = tgpig[2];
  830. const int64_t i02 = tgpig[1];
  831. const int64_t i01 = tgpig[0];
  832. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  833. const int64_t i3 = n / (ne2*ne1*ne0);
  834. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  835. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  836. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  837. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  838. float m_k;
  839. if (i2 < n_heads_log2_floor) {
  840. m_k = pow(m0, i2 + 1);
  841. } else {
  842. m_k = pow(m1, 2 * (i2 - n_heads_log2_floor) + 1);
  843. }
  844. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  845. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  846. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  847. }
  848. }
  849. typedef void (rope_t)(
  850. device const void * src0,
  851. device const int32_t * src1,
  852. device float * dst,
  853. constant int64_t & ne00,
  854. constant int64_t & ne01,
  855. constant int64_t & ne02,
  856. constant int64_t & ne03,
  857. constant uint64_t & nb00,
  858. constant uint64_t & nb01,
  859. constant uint64_t & nb02,
  860. constant uint64_t & nb03,
  861. constant int64_t & ne0,
  862. constant int64_t & ne1,
  863. constant int64_t & ne2,
  864. constant int64_t & ne3,
  865. constant uint64_t & nb0,
  866. constant uint64_t & nb1,
  867. constant uint64_t & nb2,
  868. constant uint64_t & nb3,
  869. constant int & n_past,
  870. constant int & n_dims,
  871. constant int & mode,
  872. constant float & freq_base,
  873. constant float & freq_scale,
  874. uint tiitg[[thread_index_in_threadgroup]],
  875. uint3 tptg[[threads_per_threadgroup]],
  876. uint3 tgpig[[threadgroup_position_in_grid]]);
  877. template<typename T>
  878. kernel void kernel_rope(
  879. device const void * src0,
  880. device const int32_t * src1,
  881. device float * dst,
  882. constant int64_t & ne00,
  883. constant int64_t & ne01,
  884. constant int64_t & ne02,
  885. constant int64_t & ne03,
  886. constant uint64_t & nb00,
  887. constant uint64_t & nb01,
  888. constant uint64_t & nb02,
  889. constant uint64_t & nb03,
  890. constant int64_t & ne0,
  891. constant int64_t & ne1,
  892. constant int64_t & ne2,
  893. constant int64_t & ne3,
  894. constant uint64_t & nb0,
  895. constant uint64_t & nb1,
  896. constant uint64_t & nb2,
  897. constant uint64_t & nb3,
  898. constant int & n_past,
  899. constant int & n_dims,
  900. constant int & mode,
  901. constant float & freq_base,
  902. constant float & freq_scale,
  903. uint tiitg[[thread_index_in_threadgroup]],
  904. uint3 tptg[[threads_per_threadgroup]],
  905. uint3 tgpig[[threadgroup_position_in_grid]]) {
  906. const int64_t i3 = tgpig[2];
  907. const int64_t i2 = tgpig[1];
  908. const int64_t i1 = tgpig[0];
  909. const bool is_neox = mode & 2;
  910. device const int32_t * pos = src1;
  911. const int64_t p = pos[i2];
  912. const float theta_0 = freq_scale * (float)p;
  913. const float inv_ndims = -1.f/n_dims;
  914. if (!is_neox) {
  915. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  916. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  917. const float cos_theta = cos(theta);
  918. const float sin_theta = sin(theta);
  919. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  920. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  921. const T x0 = src[0];
  922. const T x1 = src[1];
  923. dst_data[0] = x0*cos_theta - x1*sin_theta;
  924. dst_data[1] = x0*sin_theta + x1*cos_theta;
  925. }
  926. } else {
  927. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  928. for (int64_t ic = 2*tiitg; ic < n_dims; ic += 2*tptg.x) {
  929. const float theta = theta_0 * pow(freq_base, inv_ndims*ic - ib);
  930. const float cos_theta = cos(theta);
  931. const float sin_theta = sin(theta);
  932. const int64_t i0 = ib*n_dims + ic/2;
  933. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  934. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  935. const float x0 = src[0];
  936. const float x1 = src[n_dims/2];
  937. dst_data[0] = x0*cos_theta - x1*sin_theta;
  938. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  939. }
  940. }
  941. }
  942. }
  943. template [[host_name("kernel_rope_f32")]] kernel rope_t kernel_rope<float>;
  944. template [[host_name("kernel_rope_f16")]] kernel rope_t kernel_rope<half>;
  945. kernel void kernel_cpy_f16_f16(
  946. device const half * src0,
  947. device half * dst,
  948. constant int64_t & ne00,
  949. constant int64_t & ne01,
  950. constant int64_t & ne02,
  951. constant int64_t & ne03,
  952. constant uint64_t & nb00,
  953. constant uint64_t & nb01,
  954. constant uint64_t & nb02,
  955. constant uint64_t & nb03,
  956. constant int64_t & ne0,
  957. constant int64_t & ne1,
  958. constant int64_t & ne2,
  959. constant int64_t & ne3,
  960. constant uint64_t & nb0,
  961. constant uint64_t & nb1,
  962. constant uint64_t & nb2,
  963. constant uint64_t & nb3,
  964. uint3 tgpig[[threadgroup_position_in_grid]],
  965. uint3 tpitg[[thread_position_in_threadgroup]],
  966. uint3 ntg[[threads_per_threadgroup]]) {
  967. const int64_t i03 = tgpig[2];
  968. const int64_t i02 = tgpig[1];
  969. const int64_t i01 = tgpig[0];
  970. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  971. const int64_t i3 = n / (ne2*ne1*ne0);
  972. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  973. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  974. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  975. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  976. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  977. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  978. dst_data[i00] = src[0];
  979. }
  980. }
  981. kernel void kernel_cpy_f32_f16(
  982. device const float * src0,
  983. device half * dst,
  984. constant int64_t & ne00,
  985. constant int64_t & ne01,
  986. constant int64_t & ne02,
  987. constant int64_t & ne03,
  988. constant uint64_t & nb00,
  989. constant uint64_t & nb01,
  990. constant uint64_t & nb02,
  991. constant uint64_t & nb03,
  992. constant int64_t & ne0,
  993. constant int64_t & ne1,
  994. constant int64_t & ne2,
  995. constant int64_t & ne3,
  996. constant uint64_t & nb0,
  997. constant uint64_t & nb1,
  998. constant uint64_t & nb2,
  999. constant uint64_t & nb3,
  1000. uint3 tgpig[[threadgroup_position_in_grid]],
  1001. uint3 tpitg[[thread_position_in_threadgroup]],
  1002. uint3 ntg[[threads_per_threadgroup]]) {
  1003. const int64_t i03 = tgpig[2];
  1004. const int64_t i02 = tgpig[1];
  1005. const int64_t i01 = tgpig[0];
  1006. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1007. const int64_t i3 = n / (ne2*ne1*ne0);
  1008. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1009. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1010. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1011. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1012. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1013. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1014. dst_data[i00] = src[0];
  1015. }
  1016. }
  1017. kernel void kernel_cpy_f32_f32(
  1018. device const float * src0,
  1019. device float * dst,
  1020. constant int64_t & ne00,
  1021. constant int64_t & ne01,
  1022. constant int64_t & ne02,
  1023. constant int64_t & ne03,
  1024. constant uint64_t & nb00,
  1025. constant uint64_t & nb01,
  1026. constant uint64_t & nb02,
  1027. constant uint64_t & nb03,
  1028. constant int64_t & ne0,
  1029. constant int64_t & ne1,
  1030. constant int64_t & ne2,
  1031. constant int64_t & ne3,
  1032. constant uint64_t & nb0,
  1033. constant uint64_t & nb1,
  1034. constant uint64_t & nb2,
  1035. constant uint64_t & nb3,
  1036. uint3 tgpig[[threadgroup_position_in_grid]],
  1037. uint3 tpitg[[thread_position_in_threadgroup]],
  1038. uint3 ntg[[threads_per_threadgroup]]) {
  1039. const int64_t i03 = tgpig[2];
  1040. const int64_t i02 = tgpig[1];
  1041. const int64_t i01 = tgpig[0];
  1042. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1043. const int64_t i3 = n / (ne2*ne1*ne0);
  1044. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1045. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1046. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1047. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1048. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1049. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1050. dst_data[i00] = src[0];
  1051. }
  1052. }
  1053. kernel void kernel_concat(
  1054. device const char * src0,
  1055. device const char * src1,
  1056. device char * dst,
  1057. constant int64_t & ne00,
  1058. constant int64_t & ne01,
  1059. constant int64_t & ne02,
  1060. constant int64_t & ne03,
  1061. constant uint64_t & nb00,
  1062. constant uint64_t & nb01,
  1063. constant uint64_t & nb02,
  1064. constant uint64_t & nb03,
  1065. constant int64_t & ne10,
  1066. constant int64_t & ne11,
  1067. constant int64_t & ne12,
  1068. constant int64_t & ne13,
  1069. constant uint64_t & nb10,
  1070. constant uint64_t & nb11,
  1071. constant uint64_t & nb12,
  1072. constant uint64_t & nb13,
  1073. constant int64_t & ne0,
  1074. constant int64_t & ne1,
  1075. constant int64_t & ne2,
  1076. constant int64_t & ne3,
  1077. constant uint64_t & nb0,
  1078. constant uint64_t & nb1,
  1079. constant uint64_t & nb2,
  1080. constant uint64_t & nb3,
  1081. uint3 tgpig[[threadgroup_position_in_grid]],
  1082. uint3 tpitg[[thread_position_in_threadgroup]],
  1083. uint3 ntg[[threads_per_threadgroup]]) {
  1084. const int64_t i03 = tgpig.z;
  1085. const int64_t i02 = tgpig.y;
  1086. const int64_t i01 = tgpig.x;
  1087. const int64_t i13 = i03 % ne13;
  1088. const int64_t i12 = i02 % ne12;
  1089. const int64_t i11 = i01 % ne11;
  1090. device const char * src0_ptr = src0 + i03 * nb03 + i02 * nb02 + i01 * nb01 + tpitg.x*nb00;
  1091. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11 + tpitg.x*nb10;
  1092. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + tpitg.x*nb0;
  1093. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1094. if (i02 < ne02) {
  1095. ((device float *)dst_ptr)[0] = ((device float *)src0_ptr)[0];
  1096. src0_ptr += ntg.x*nb00;
  1097. } else {
  1098. ((device float *)dst_ptr)[0] = ((device float *)src1_ptr)[0];
  1099. src1_ptr += ntg.x*nb10;
  1100. }
  1101. dst_ptr += ntg.x*nb0;
  1102. }
  1103. }
  1104. //============================================ k-quants ======================================================
  1105. #ifndef QK_K
  1106. #define QK_K 256
  1107. #else
  1108. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  1109. #endif
  1110. #if QK_K == 256
  1111. #define K_SCALE_SIZE 12
  1112. #else
  1113. #define K_SCALE_SIZE 4
  1114. #endif
  1115. typedef struct {
  1116. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  1117. uint8_t qs[QK_K/4]; // quants
  1118. half d; // super-block scale for quantized scales
  1119. half dmin; // super-block scale for quantized mins
  1120. } block_q2_K;
  1121. // 84 bytes / block
  1122. typedef struct {
  1123. uint8_t hmask[QK_K/8]; // quants - high bit
  1124. uint8_t qs[QK_K/4]; // quants - low 2 bits
  1125. #if QK_K == 64
  1126. uint8_t scales[2];
  1127. #else
  1128. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  1129. #endif
  1130. half d; // super-block scale
  1131. } block_q3_K;
  1132. #if QK_K == 64
  1133. typedef struct {
  1134. half d[2]; // super-block scales/mins
  1135. uint8_t scales[2];
  1136. uint8_t qs[QK_K/2]; // 4-bit quants
  1137. } block_q4_K;
  1138. #else
  1139. typedef struct {
  1140. half d; // super-block scale for quantized scales
  1141. half dmin; // super-block scale for quantized mins
  1142. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  1143. uint8_t qs[QK_K/2]; // 4--bit quants
  1144. } block_q4_K;
  1145. #endif
  1146. #if QK_K == 64
  1147. typedef struct {
  1148. half d; // super-block scales/mins
  1149. int8_t scales[QK_K/16]; // 8-bit block scales
  1150. uint8_t qh[QK_K/8]; // quants, high bit
  1151. uint8_t qs[QK_K/2]; // quants, low 4 bits
  1152. } block_q5_K;
  1153. #else
  1154. typedef struct {
  1155. half d; // super-block scale for quantized scales
  1156. half dmin; // super-block scale for quantized mins
  1157. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  1158. uint8_t qh[QK_K/8]; // quants, high bit
  1159. uint8_t qs[QK_K/2]; // quants, low 4 bits
  1160. } block_q5_K;
  1161. // 176 bytes / block
  1162. #endif
  1163. typedef struct {
  1164. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  1165. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  1166. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  1167. half d; // super-block scale
  1168. } block_q6_K;
  1169. // 210 bytes / block
  1170. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  1171. uchar4 r;
  1172. if (j < 4) {
  1173. r[0] = q[j+0] & 63;
  1174. r[2] = q[j+1] & 63;
  1175. r[1] = q[j+4] & 63;
  1176. r[3] = q[j+5] & 63;
  1177. } else {
  1178. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1179. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  1180. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1181. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  1182. }
  1183. return r;
  1184. }
  1185. //====================================== dot products =========================
  1186. kernel void kernel_mul_mv_q2_K_f32(
  1187. device const void * src0,
  1188. device const float * src1,
  1189. device float * dst,
  1190. constant int64_t & ne00,
  1191. constant int64_t & ne01[[buffer(4)]],
  1192. constant int64_t & ne02[[buffer(5)]],
  1193. constant int64_t & ne10[[buffer(9)]],
  1194. constant int64_t & ne12[[buffer(11)]],
  1195. constant int64_t & ne0[[buffer(15)]],
  1196. constant int64_t & ne1[[buffer(16)]],
  1197. constant uint & gqa[[buffer(17)]],
  1198. uint3 tgpig[[threadgroup_position_in_grid]],
  1199. uint tiisg[[thread_index_in_simdgroup]],
  1200. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1201. const int nb = ne00/QK_K;
  1202. const int r0 = tgpig.x;
  1203. const int r1 = tgpig.y;
  1204. const int r2 = tgpig.z;
  1205. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1206. const int ib_row = first_row * nb;
  1207. const uint offset0 = r2/gqa*(nb*ne0);
  1208. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  1209. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1210. float yl[32];
  1211. float sumf[N_DST]={0.f}, all_sum;
  1212. const int step = sizeof(block_q2_K) * nb;
  1213. #if QK_K == 256
  1214. const int ix = tiisg/8; // 0...3
  1215. const int it = tiisg%8; // 0...7
  1216. const int im = it/4; // 0 or 1
  1217. const int ir = it%4; // 0...3
  1218. const int is = (8*ir)/16;// 0 or 1
  1219. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  1220. for (int ib = ix; ib < nb; ib += 4) {
  1221. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1222. for (int i = 0; i < 8; ++i) {
  1223. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1224. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  1225. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  1226. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  1227. }
  1228. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  1229. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1230. device const half * dh = &x[ib].d;
  1231. for (int row = 0; row < N_DST; row++) {
  1232. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1233. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1234. for (int i = 0; i < 8; i += 2) {
  1235. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1236. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1237. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1238. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1239. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1240. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1241. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1242. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1243. }
  1244. float dall = dh[0];
  1245. float dmin = dh[1] * 1.f/16.f;
  1246. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1247. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  1248. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  1249. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  1250. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  1251. qs += step/2;
  1252. sc += step;
  1253. dh += step/2;
  1254. }
  1255. y4 += 4 * QK_K;
  1256. }
  1257. #else
  1258. const int ix = tiisg/2; // 0...15
  1259. const int it = tiisg%2; // 0...1
  1260. device const float * y4 = y + ix * QK_K + 8 * it;
  1261. for (int ib = ix; ib < nb; ib += 16) {
  1262. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1263. for (int i = 0; i < 8; ++i) {
  1264. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1265. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  1266. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  1267. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  1268. }
  1269. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  1270. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1271. device const half * dh = &x[ib].d;
  1272. for (int row = 0; row < N_DST; row++) {
  1273. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1274. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1275. for (int i = 0; i < 8; i += 2) {
  1276. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1277. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1278. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1279. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1280. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1281. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1282. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1283. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1284. }
  1285. float dall = dh[0];
  1286. float dmin = dh[1];
  1287. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1288. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  1289. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  1290. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  1291. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  1292. qs += step/2;
  1293. sc += step;
  1294. dh += step/2;
  1295. }
  1296. y4 += 16 * QK_K;
  1297. }
  1298. #endif
  1299. for (int row = 0; row < N_DST; ++row) {
  1300. all_sum = simd_sum(sumf[row]);
  1301. if (tiisg == 0) {
  1302. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1303. }
  1304. }
  1305. }
  1306. #if QK_K == 256
  1307. kernel void kernel_mul_mv_q3_K_f32(
  1308. device const void * src0,
  1309. device const float * src1,
  1310. device float * dst,
  1311. constant int64_t & ne00,
  1312. constant int64_t & ne01[[buffer(4)]],
  1313. constant int64_t & ne02[[buffer(5)]],
  1314. constant int64_t & ne10[[buffer(9)]],
  1315. constant int64_t & ne12[[buffer(11)]],
  1316. constant int64_t & ne0[[buffer(15)]],
  1317. constant int64_t & ne1[[buffer(16)]],
  1318. constant uint & gqa[[buffer(17)]],
  1319. uint3 tgpig[[threadgroup_position_in_grid]],
  1320. uint tiisg[[thread_index_in_simdgroup]],
  1321. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1322. const int nb = ne00/QK_K;
  1323. const int64_t r0 = tgpig.x;
  1324. const int64_t r1 = tgpig.y;
  1325. const int64_t r2 = tgpig.z;
  1326. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1327. const uint offset0 = r2/gqa*(nb*ne0);
  1328. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  1329. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1330. float yl[32];
  1331. //const uint16_t kmask1 = 0x3030;
  1332. //const uint16_t kmask2 = 0x0f0f;
  1333. const int tid = tiisg/4;
  1334. const int ix = tiisg%4;
  1335. const int ip = tid/4; // 0 or 1
  1336. const int il = 2*((tid%4)/2); // 0 or 2
  1337. const int ir = tid%2;
  1338. const int n = 8;
  1339. const int l0 = n*ir;
  1340. // One would think that the Metal compiler would figure out that ip and il can only have
  1341. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  1342. // with these two tales.
  1343. //
  1344. // Possible masks for the high bit
  1345. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  1346. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  1347. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  1348. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  1349. // Possible masks for the low 2 bits
  1350. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  1351. const ushort4 hm = mm[2*ip + il/2];
  1352. const int shift = 2*il;
  1353. const float v1 = il == 0 ? 4.f : 64.f;
  1354. const float v2 = 4.f * v1;
  1355. const uint16_t s_shift1 = 4*ip;
  1356. const uint16_t s_shift2 = s_shift1 + il;
  1357. const int q_offset = 32*ip + l0;
  1358. const int y_offset = 128*ip + 32*il + l0;
  1359. const int step = sizeof(block_q3_K) * nb / 2;
  1360. device const float * y1 = yy + ix*QK_K + y_offset;
  1361. uint32_t scales32, aux32;
  1362. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  1363. thread const int8_t * scales = (thread const int8_t *)&scales32;
  1364. float sumf1[2] = {0.f};
  1365. float sumf2[2] = {0.f};
  1366. for (int i = ix; i < nb; i += 4) {
  1367. for (int l = 0; l < 8; ++l) {
  1368. yl[l+ 0] = y1[l+ 0];
  1369. yl[l+ 8] = y1[l+16];
  1370. yl[l+16] = y1[l+32];
  1371. yl[l+24] = y1[l+48];
  1372. }
  1373. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1374. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1375. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1376. device const half * dh = &x[i].d;
  1377. for (int row = 0; row < 2; ++row) {
  1378. const float d_all = (float)dh[0];
  1379. scales16[0] = a[4];
  1380. scales16[1] = a[5];
  1381. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  1382. scales16[0] = a[il+0];
  1383. scales16[1] = a[il+1];
  1384. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  1385. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  1386. for (int l = 0; l < n; l += 2) {
  1387. const int32_t qs = q[l/2];
  1388. s1 += yl[l+0] * (qs & qm[il/2][0]);
  1389. s2 += yl[l+1] * (qs & qm[il/2][1]);
  1390. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  1391. s4 += yl[l+16] * (qs & qm[il/2][2]);
  1392. s5 += yl[l+17] * (qs & qm[il/2][3]);
  1393. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  1394. }
  1395. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1396. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1397. sumf1[row] += d1 * (scales[0] - 32);
  1398. sumf2[row] += d2 * (scales[2] - 32);
  1399. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  1400. for (int l = 0; l < n; l += 2) {
  1401. const int32_t qs = q[l/2+8];
  1402. s1 += yl[l+8] * (qs & qm[il/2][0]);
  1403. s2 += yl[l+9] * (qs & qm[il/2][1]);
  1404. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  1405. s4 += yl[l+24] * (qs & qm[il/2][2]);
  1406. s5 += yl[l+25] * (qs & qm[il/2][3]);
  1407. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  1408. }
  1409. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1410. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1411. sumf1[row] += d1 * (scales[1] - 32);
  1412. sumf2[row] += d2 * (scales[3] - 32);
  1413. q += step;
  1414. h += step;
  1415. a += step;
  1416. dh += step;
  1417. }
  1418. y1 += 4 * QK_K;
  1419. }
  1420. for (int row = 0; row < 2; ++row) {
  1421. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  1422. sumf1[row] = simd_sum(sumf);
  1423. }
  1424. if (tiisg == 0) {
  1425. for (int row = 0; row < 2; ++row) {
  1426. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = sumf1[row];
  1427. }
  1428. }
  1429. }
  1430. #else
  1431. kernel void kernel_mul_mv_q3_K_f32(
  1432. device const void * src0,
  1433. device const float * src1,
  1434. device float * dst,
  1435. constant int64_t & ne00,
  1436. constant int64_t & ne01[[buffer(4)]],
  1437. constant int64_t & ne02[[buffer(5)]],
  1438. constant int64_t & ne10[[buffer(9)]],
  1439. constant int64_t & ne12[[buffer(11)]],
  1440. constant int64_t & ne0[[buffer(15)]],
  1441. constant int64_t & ne1[[buffer(16)]],
  1442. constant uint & gqa[[buffer(17)]],
  1443. uint3 tgpig[[threadgroup_position_in_grid]],
  1444. uint tiisg[[thread_index_in_simdgroup]],
  1445. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1446. const int nb = ne00/QK_K;
  1447. const int64_t r0 = tgpig.x;
  1448. const int64_t r1 = tgpig.y;
  1449. const int64_t r2 = tgpig.z;
  1450. const int row = 2 * r0 + sgitg;
  1451. const uint offset0 = r2/gqa*(nb*ne0);
  1452. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1453. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1454. const int ix = tiisg/4;
  1455. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1456. const int im = il/8; // 0, 0, 1, 1
  1457. const int in = il%8; // 0, 4, 0, 4
  1458. float2 sum = {0.f, 0.f};
  1459. for (int i = ix; i < nb; i += 8) {
  1460. const float d_all = (float)(x[i].d);
  1461. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1462. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1463. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1464. device const float * y = yy + i * QK_K + il;
  1465. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1466. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1467. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1468. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1469. for (int l = 0; l < 4; l += 2) {
  1470. const uint16_t hm = h[l/2] >> im;
  1471. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1472. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1473. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1474. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1475. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1476. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1477. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1478. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1479. }
  1480. }
  1481. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1482. const float tot = simd_sum(sumf);
  1483. if (tiisg == 0) {
  1484. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1485. }
  1486. }
  1487. #endif
  1488. #if QK_K == 256
  1489. kernel void kernel_mul_mv_q4_K_f32(
  1490. device const void * src0,
  1491. device const float * src1,
  1492. device float * dst,
  1493. constant int64_t & ne00,
  1494. constant int64_t & ne01 [[buffer(4)]],
  1495. constant int64_t & ne02 [[buffer(5)]],
  1496. constant int64_t & ne10 [[buffer(9)]],
  1497. constant int64_t & ne12 [[buffer(11)]],
  1498. constant int64_t & ne0 [[buffer(15)]],
  1499. constant int64_t & ne1 [[buffer(16)]],
  1500. constant uint & gqa [[buffer(17)]],
  1501. uint3 tgpig[[threadgroup_position_in_grid]],
  1502. uint tiisg[[thread_index_in_simdgroup]],
  1503. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1504. const uint16_t kmask1 = 0x3f3f;
  1505. const uint16_t kmask2 = 0x0f0f;
  1506. const uint16_t kmask3 = 0xc0c0;
  1507. const int ix = tiisg/8; // 0...3
  1508. const int it = tiisg%8; // 0...7
  1509. const int im = it/4; // 0 or 1
  1510. const int ir = it%4; // 0...3
  1511. const int nb = ne00/QK_K;
  1512. const int r0 = tgpig.x;
  1513. const int r1 = tgpig.y;
  1514. const int r2 = tgpig.z;
  1515. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1516. const int first_row = r0 * N_DST;
  1517. const int ib_row = first_row * nb;
  1518. const uint offset0 = r2/gqa*(nb*ne0);
  1519. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1520. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1521. float yl[16];
  1522. float yh[16];
  1523. float sumf[N_DST]={0.f}, all_sum;
  1524. const int step = sizeof(block_q4_K) * nb / 2;
  1525. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1526. uint16_t sc16[4];
  1527. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1528. for (int ib = ix; ib < nb; ib += 4) {
  1529. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1530. for (int i = 0; i < 8; ++i) {
  1531. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1532. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1533. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1534. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1535. }
  1536. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1537. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1538. device const half * dh = &x[ib].d;
  1539. for (int row = 0; row < N_DST; row++) {
  1540. sc16[0] = sc[0] & kmask1;
  1541. sc16[1] = sc[2] & kmask1;
  1542. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1543. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1544. device const uint16_t * q2 = q1 + 32;
  1545. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1546. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1547. for (int i = 0; i < 8; i += 2) {
  1548. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1549. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1550. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1551. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1552. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1553. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1554. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1555. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1556. }
  1557. float dall = dh[0];
  1558. float dmin = dh[1];
  1559. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1560. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1561. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1562. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1563. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1564. q1 += step;
  1565. sc += step;
  1566. dh += step;
  1567. }
  1568. y4 += 4 * QK_K;
  1569. }
  1570. for (int row = 0; row < N_DST; ++row) {
  1571. all_sum = simd_sum(sumf[row]);
  1572. if (tiisg == 0) {
  1573. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1574. }
  1575. }
  1576. }
  1577. #else
  1578. kernel void kernel_mul_mv_q4_K_f32(
  1579. device const void * src0,
  1580. device const float * src1,
  1581. device float * dst,
  1582. constant int64_t & ne00,
  1583. constant int64_t & ne01[[buffer(4)]],
  1584. constant int64_t & ne02[[buffer(5)]],
  1585. constant int64_t & ne10[[buffer(9)]],
  1586. constant int64_t & ne12[[buffer(11)]],
  1587. constant int64_t & ne0[[buffer(15)]],
  1588. constant int64_t & ne1[[buffer(16)]],
  1589. constant uint & gqa[[buffer(17)]],
  1590. uint3 tgpig[[threadgroup_position_in_grid]],
  1591. uint tiisg[[thread_index_in_simdgroup]],
  1592. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1593. const int ix = tiisg/4; // 0...7
  1594. const int it = tiisg%4; // 0...3
  1595. const int nb = ne00/QK_K;
  1596. const int r0 = tgpig.x;
  1597. const int r1 = tgpig.y;
  1598. const int r2 = tgpig.z;
  1599. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1600. const int ib_row = first_row * nb;
  1601. const uint offset0 = r2/gqa*(nb*ne0);
  1602. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1603. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1604. float yl[8];
  1605. float yh[8];
  1606. float sumf[N_DST]={0.f}, all_sum;
  1607. const int step = sizeof(block_q4_K) * nb / 2;
  1608. device const float * y4 = y + ix * QK_K + 8 * it;
  1609. uint16_t sc16[4];
  1610. for (int ib = ix; ib < nb; ib += 8) {
  1611. float2 sumy = {0.f, 0.f};
  1612. for (int i = 0; i < 8; ++i) {
  1613. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1614. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1615. }
  1616. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1617. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1618. device const half * dh = x[ib].d;
  1619. for (int row = 0; row < N_DST; row++) {
  1620. sc16[0] = sc[0] & 0x000f;
  1621. sc16[1] = sc[0] & 0x0f00;
  1622. sc16[2] = sc[0] & 0x00f0;
  1623. sc16[3] = sc[0] & 0xf000;
  1624. float2 acc1 = {0.f, 0.f};
  1625. float2 acc2 = {0.f, 0.f};
  1626. for (int i = 0; i < 8; i += 2) {
  1627. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1628. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1629. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1630. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1631. }
  1632. float dall = dh[0];
  1633. float dmin = dh[1];
  1634. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1635. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1636. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1637. qs += step;
  1638. sc += step;
  1639. dh += step;
  1640. }
  1641. y4 += 8 * QK_K;
  1642. }
  1643. for (int row = 0; row < N_DST; ++row) {
  1644. all_sum = simd_sum(sumf[row]);
  1645. if (tiisg == 0) {
  1646. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1647. }
  1648. }
  1649. }
  1650. #endif
  1651. kernel void kernel_mul_mv_q5_K_f32(
  1652. device const void * src0,
  1653. device const float * src1,
  1654. device float * dst,
  1655. constant int64_t & ne00,
  1656. constant int64_t & ne01[[buffer(4)]],
  1657. constant int64_t & ne02[[buffer(5)]],
  1658. constant int64_t & ne10[[buffer(9)]],
  1659. constant int64_t & ne12[[buffer(11)]],
  1660. constant int64_t & ne0[[buffer(15)]],
  1661. constant int64_t & ne1[[buffer(16)]],
  1662. constant uint & gqa[[buffer(17)]],
  1663. uint3 tgpig[[threadgroup_position_in_grid]],
  1664. uint tiisg[[thread_index_in_simdgroup]],
  1665. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1666. const int nb = ne00/QK_K;
  1667. const int64_t r0 = tgpig.x;
  1668. const int64_t r1 = tgpig.y;
  1669. const int r2 = tgpig.z;
  1670. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1671. const uint offset0 = r2/gqa*(nb*ne0);
  1672. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1673. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1674. float sumf[2]={0.f};
  1675. const int step = sizeof(block_q5_K) * nb;
  1676. #if QK_K == 256
  1677. #
  1678. float yl[16], yh[16];
  1679. const uint16_t kmask1 = 0x3f3f;
  1680. const uint16_t kmask2 = 0x0f0f;
  1681. const uint16_t kmask3 = 0xc0c0;
  1682. const int tid = tiisg/4;
  1683. const int ix = tiisg%4;
  1684. const int im = tid/4;
  1685. const int ir = tid%4;
  1686. const int n = 8;
  1687. const int l0 = n*ir;
  1688. const int q_offset = 32*im + l0;
  1689. const int y_offset = 64*im + l0;
  1690. const uint8_t hm1 = 1u << (2*im);
  1691. const uint8_t hm2 = hm1 << 1;
  1692. const uint8_t hm3 = hm1 << 4;
  1693. const uint8_t hm4 = hm2 << 4;
  1694. uint16_t sc16[4];
  1695. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1696. device const float * y1 = yy + ix*QK_K + y_offset;
  1697. for (int i = ix; i < nb; i += 4) {
  1698. device const uint8_t * q1 = x[i].qs + q_offset;
  1699. device const uint8_t * qh = x[i].qh + l0;
  1700. device const half * dh = &x[i].d;
  1701. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1702. device const float * y2 = y1 + 128;
  1703. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1704. for (int l = 0; l < 8; ++l) {
  1705. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1706. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1707. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1708. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1709. }
  1710. for (int row = 0; row < 2; ++row) {
  1711. device const uint8_t * q2 = q1 + 64;
  1712. sc16[0] = a[0] & kmask1;
  1713. sc16[1] = a[2] & kmask1;
  1714. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1715. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1716. float4 acc1 = {0.f};
  1717. float4 acc2 = {0.f};
  1718. for (int l = 0; l < n; ++l) {
  1719. uint8_t h = qh[l];
  1720. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  1721. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  1722. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  1723. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  1724. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  1725. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  1726. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  1727. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  1728. }
  1729. const float dall = dh[0];
  1730. const float dmin = dh[1];
  1731. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  1732. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  1733. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  1734. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  1735. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1736. q1 += step;
  1737. qh += step;
  1738. dh += step/2;
  1739. a += step/2;
  1740. }
  1741. y1 += 4 * QK_K;
  1742. }
  1743. #else
  1744. float yl[8], yh[8];
  1745. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1746. const int ix = tiisg%8;
  1747. const int im = il/8; // 0, 0, 1, 1
  1748. const int in = il%8; // 0, 4, 0, 4
  1749. device const float * y = yy + ix*QK_K + il;
  1750. for (int i = ix; i < nb; i += 8) {
  1751. for (int l = 0; l < 4; ++l) {
  1752. yl[l+0] = y[l+ 0];
  1753. yl[l+4] = y[l+16];
  1754. yh[l+0] = y[l+32];
  1755. yh[l+4] = y[l+48];
  1756. }
  1757. device const half * dh = &x[i].d;
  1758. device const uint8_t * q = x[i].qs + il;
  1759. device const uint8_t * h = x[i].qh + in;
  1760. device const int8_t * s = x[i].scales;
  1761. for (int row = 0; row < 2; ++row) {
  1762. const float d = dh[0];
  1763. float2 acc = {0.f, 0.f};
  1764. for (int l = 0; l < 4; ++l) {
  1765. const uint8_t hl = h[l] >> im;
  1766. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1767. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1768. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1769. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1770. }
  1771. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1772. q += step;
  1773. h += step;
  1774. s += step;
  1775. dh += step/2;
  1776. }
  1777. y += 8 * QK_K;
  1778. }
  1779. #endif
  1780. for (int row = 0; row < 2; ++row) {
  1781. const float tot = simd_sum(sumf[row]);
  1782. if (tiisg == 0) {
  1783. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1784. }
  1785. }
  1786. }
  1787. kernel void kernel_mul_mv_q6_K_f32(
  1788. device const void * src0,
  1789. device const float * src1,
  1790. device float * dst,
  1791. constant int64_t & ne00,
  1792. constant int64_t & ne01[[buffer(4)]],
  1793. constant int64_t & ne02[[buffer(5)]],
  1794. constant int64_t & ne10[[buffer(9)]],
  1795. constant int64_t & ne12[[buffer(11)]],
  1796. constant int64_t & ne0[[buffer(15)]],
  1797. constant int64_t & ne1[[buffer(16)]],
  1798. constant uint & gqa[[buffer(17)]],
  1799. uint3 tgpig[[threadgroup_position_in_grid]],
  1800. uint tiisg[[thread_index_in_simdgroup]],
  1801. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1802. const uint8_t kmask1 = 0x03;
  1803. const uint8_t kmask2 = 0x0C;
  1804. const uint8_t kmask3 = 0x30;
  1805. const uint8_t kmask4 = 0xC0;
  1806. const int nb = ne00/QK_K;
  1807. const int64_t r0 = tgpig.x;
  1808. const int64_t r1 = tgpig.y;
  1809. const int r2 = tgpig.z;
  1810. const int row = 2 * r0 + sgitg;
  1811. const uint offset0 = r2/gqa*(nb*ne0);
  1812. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1813. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1814. float sumf = 0;
  1815. #if QK_K == 256
  1816. const int tid = tiisg/2;
  1817. const int ix = tiisg%2;
  1818. const int ip = tid/8; // 0 or 1
  1819. const int il = tid%8;
  1820. const int n = 4;
  1821. const int l0 = n*il;
  1822. const int is = 8*ip + l0/16;
  1823. const int y_offset = 128*ip + l0;
  1824. const int q_offset_l = 64*ip + l0;
  1825. const int q_offset_h = 32*ip + l0;
  1826. for (int i = ix; i < nb; i += 2) {
  1827. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1828. device const uint8_t * q2 = q1 + 32;
  1829. device const uint8_t * qh = x[i].qh + q_offset_h;
  1830. device const int8_t * sc = x[i].scales + is;
  1831. device const float * y = yy + i * QK_K + y_offset;
  1832. const float dall = x[i].d;
  1833. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1834. for (int l = 0; l < n; ++l) {
  1835. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1836. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1837. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1838. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1839. }
  1840. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1841. }
  1842. #else
  1843. const int ix = tiisg/4;
  1844. const int il = 4*(tiisg%4);
  1845. for (int i = ix; i < nb; i += 8) {
  1846. device const float * y = yy + i * QK_K + il;
  1847. device const uint8_t * ql = x[i].ql + il;
  1848. device const uint8_t * qh = x[i].qh + il;
  1849. device const int8_t * s = x[i].scales;
  1850. const float d = x[i].d;
  1851. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1852. for (int l = 0; l < 4; ++l) {
  1853. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1854. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1855. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1856. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1857. }
  1858. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1859. }
  1860. #endif
  1861. const float tot = simd_sum(sumf);
  1862. if (tiisg == 0) {
  1863. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1864. }
  1865. }
  1866. //============================= templates and their specializations =============================
  1867. // NOTE: this is not dequantizing - we are simply fitting the template
  1868. template <typename type4x4>
  1869. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  1870. float4x4 temp = *(((device float4x4 *)src));
  1871. for (int i = 0; i < 16; i++){
  1872. reg[i/4][i%4] = temp[i/4][i%4];
  1873. }
  1874. }
  1875. template <typename type4x4>
  1876. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1877. half4x4 temp = *(((device half4x4 *)src));
  1878. for (int i = 0; i < 16; i++){
  1879. reg[i/4][i%4] = temp[i/4][i%4];
  1880. }
  1881. }
  1882. template <typename type4x4>
  1883. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1884. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1885. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1886. const float d2 = d1 / 256.f;
  1887. const float md = -8.h * xb->d;
  1888. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1889. const ushort mask1 = mask0 << 8;
  1890. for (int i=0;i<8;i++) {
  1891. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  1892. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  1893. }
  1894. }
  1895. template <typename type4x4>
  1896. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1897. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1898. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1899. const float d2 = d1 / 256.f;
  1900. const float m = xb->m;
  1901. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1902. const ushort mask1 = mask0 << 8;
  1903. for (int i=0;i<8;i++) {
  1904. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  1905. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  1906. }
  1907. }
  1908. template <typename type4x4>
  1909. void dequantize_q5_0(device const block_q5_0 *xb, short il, thread type4x4 & reg) {
  1910. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  1911. const float d = xb->d;
  1912. const float md = -16.h * xb->d;
  1913. const ushort mask = il ? 0x00F0 : 0x000F;
  1914. const uint32_t qh = *((device const uint32_t *)xb->qh);
  1915. const int x_mv = il ? 4 : 0;
  1916. const int gh_mv = il ? 12 : 0;
  1917. const int gh_bk = il ? 0 : 4;
  1918. for (int i = 0; i < 8; i++) {
  1919. // extract the 5-th bits for x0 and x1
  1920. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  1921. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  1922. // combine the 4-bits from qs with the 5th bit
  1923. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  1924. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  1925. reg[i/2][2*(i%2)+0] = d * x0 + md;
  1926. reg[i/2][2*(i%2)+1] = d * x1 + md;
  1927. }
  1928. }
  1929. template <typename type4x4>
  1930. void dequantize_q5_1(device const block_q5_1 *xb, short il, thread type4x4 & reg) {
  1931. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  1932. const float d = xb->d;
  1933. const float m = xb->m;
  1934. const ushort mask = il ? 0x00F0 : 0x000F;
  1935. const uint32_t qh = *((device const uint32_t *)xb->qh);
  1936. const int x_mv = il ? 4 : 0;
  1937. const int gh_mv = il ? 12 : 0;
  1938. const int gh_bk = il ? 0 : 4;
  1939. for (int i = 0; i < 8; i++) {
  1940. // extract the 5-th bits for x0 and x1
  1941. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  1942. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  1943. // combine the 4-bits from qs with the 5th bit
  1944. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  1945. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  1946. reg[i/2][2*(i%2)+0] = d * x0 + m;
  1947. reg[i/2][2*(i%2)+1] = d * x1 + m;
  1948. }
  1949. }
  1950. template <typename type4x4>
  1951. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1952. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1953. const half d = xb->d;
  1954. for (int i=0;i<16;i++) {
  1955. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1956. }
  1957. }
  1958. template <typename type4x4>
  1959. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1960. const half d = xb->d;
  1961. const half min = xb->dmin;
  1962. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1963. half dl, ml;
  1964. uint8_t sc = xb->scales[il];
  1965. #if QK_K == 256
  1966. q = q + 32*(il/8) + 16*(il&1);
  1967. il = (il/2)%4;
  1968. #endif
  1969. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1970. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1971. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1972. for (int i = 0; i < 16; ++i) {
  1973. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1974. }
  1975. }
  1976. template <typename type4x4>
  1977. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1978. const half d_all = xb->d;
  1979. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1980. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1981. device const int8_t * scales = (device const int8_t *)xb->scales;
  1982. #if QK_K == 256
  1983. q = q + 32 * (il/8) + 16 * (il&1);
  1984. h = h + 16 * (il&1);
  1985. uint8_t m = 1 << (il/2);
  1986. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1987. ((il/4)>0 ? 12 : 3);
  1988. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1989. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1990. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  1991. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1992. half dl = il<8 ? d_all * (dl_int - 32.h) : d_all * (dl_int / 16.h - 32.h);
  1993. const half ml = 4.h * dl;
  1994. il = (il/2) & 3;
  1995. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1996. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1997. dl *= coef;
  1998. for (int i = 0; i < 16; ++i) {
  1999. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  2000. }
  2001. #else
  2002. float kcoef = il&1 ? 1.f/16.f : 1.f;
  2003. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  2004. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  2005. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2006. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2007. uint8_t m = 1<<(il*2);
  2008. for (int i = 0; i < 16; ++i) {
  2009. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  2010. }
  2011. #endif
  2012. }
  2013. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  2014. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  2015. : uchar2{uchar((q[j+4+k] & 0xF) | ((q[j-4+k] & 0xc0) >> 2)), uchar((q[j+4+k] >> 4) | ((q[j-0+k] & 0xc0) >> 2))};
  2016. }
  2017. template <typename type4x4>
  2018. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  2019. device const uchar * q = xb->qs;
  2020. #if QK_K == 256
  2021. short is = (il/4) * 2;
  2022. q = q + (il/4) * 32 + 16 * (il&1);
  2023. il = il & 3;
  2024. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2025. const half d = il < 2 ? xb->d : xb->d / 16.h;
  2026. const half min = xb->dmin;
  2027. const half dl = d * sc[0];
  2028. const half ml = min * sc[1];
  2029. #else
  2030. q = q + 16 * (il&1);
  2031. device const uint8_t * s = xb->scales;
  2032. device const half2 * dh = (device const half2 *)xb->d;
  2033. const float2 d = (float2)dh[0];
  2034. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  2035. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  2036. #endif
  2037. const ushort mask = il<2 ? 0x0F : 0xF0;
  2038. for (int i = 0; i < 16; ++i) {
  2039. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2040. }
  2041. }
  2042. template <typename type4x4>
  2043. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  2044. device const uint8_t * q = xb->qs;
  2045. device const uint8_t * qh = xb->qh;
  2046. #if QK_K == 256
  2047. short is = (il/4) * 2;
  2048. q = q + 32 * (il/4) + 16 * (il&1);
  2049. qh = qh + 16 * (il&1);
  2050. uint8_t ul = 1 << (il/2);
  2051. il = il & 3;
  2052. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2053. const half d = il < 2 ? xb->d : xb->d / 16.h;
  2054. const half min = xb->dmin;
  2055. const half dl = d * sc[0];
  2056. const half ml = min * sc[1];
  2057. const ushort mask = il<2 ? 0x0F : 0xF0;
  2058. const half qh_val = il<2 ? 16.h : 256.h;
  2059. for (int i = 0; i < 16; ++i) {
  2060. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  2061. }
  2062. #else
  2063. q = q + 16 * (il&1);
  2064. device const int8_t * s = xb->scales;
  2065. const float dl = xb->d * s[il];
  2066. uint8_t m = 1<<(il*2);
  2067. const float coef = il<2 ? 1.f : 1.f/16.f;
  2068. const ushort mask = il<2 ? 0x0F : 0xF0;
  2069. for (int i = 0; i < 16; ++i) {
  2070. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  2071. }
  2072. #endif
  2073. }
  2074. template <typename type4x4>
  2075. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  2076. const half d_all = xb->d;
  2077. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  2078. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  2079. device const int8_t * scales = (device const int8_t *)xb->scales;
  2080. #if QK_K == 256
  2081. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  2082. qh = qh + 32*(il/8) + 16*(il&1);
  2083. half sc = scales[(il%2) + 2 * ((il/2))];
  2084. il = (il/2) & 3;
  2085. #else
  2086. ql = ql + 16 * (il&1);
  2087. half sc = scales[il];
  2088. #endif
  2089. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2090. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  2091. const half coef = il>1 ? 1.f/16.h : 1.h;
  2092. const half ml = d_all * sc * 32.h;
  2093. const half dl = d_all * sc * coef;
  2094. for (int i = 0; i < 16; ++i) {
  2095. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  2096. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  2097. reg[i/4][i%4] = dl * q - ml;
  2098. }
  2099. }
  2100. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  2101. kernel void kernel_get_rows(
  2102. device const void * src0,
  2103. device const int * src1,
  2104. device float * dst,
  2105. constant int64_t & ne00,
  2106. constant uint64_t & nb01,
  2107. constant uint64_t & nb1,
  2108. uint tgpig[[threadgroup_position_in_grid]],
  2109. uint tiitg[[thread_index_in_threadgroup]],
  2110. uint tptg[[threads_per_threadgroup]]) {
  2111. const int i = tgpig;
  2112. const int r = ((device int32_t *) src1)[i];
  2113. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  2114. float4x4 temp;
  2115. dequantize_func(
  2116. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  2117. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  2118. }
  2119. }
  2120. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  2121. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  2122. #define BLOCK_SIZE_K 32
  2123. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  2124. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  2125. #define THREAD_PER_BLOCK 128
  2126. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  2127. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  2128. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  2129. #define SG_MAT_ROW 8
  2130. // each block_q contains 16*nl weights
  2131. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  2132. kernel void kernel_mul_mm(device const uchar * src0,
  2133. device const uchar * src1,
  2134. device float * dst,
  2135. constant int64_t & ne00,
  2136. constant int64_t & ne02,
  2137. constant int64_t & nb01,
  2138. constant int64_t & nb02,
  2139. constant int64_t & ne12,
  2140. constant int64_t & nb10,
  2141. constant int64_t & nb11,
  2142. constant int64_t & nb12,
  2143. constant int64_t & ne0,
  2144. constant int64_t & ne1,
  2145. constant uint & gqa,
  2146. threadgroup uchar * shared_memory [[threadgroup(0)]],
  2147. uint3 tgpig[[threadgroup_position_in_grid]],
  2148. uint tiitg[[thread_index_in_threadgroup]],
  2149. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2150. threadgroup half * sa = (threadgroup half *)(shared_memory);
  2151. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  2152. const uint r0 = tgpig.y;
  2153. const uint r1 = tgpig.x;
  2154. const uint im = tgpig.z;
  2155. // if this block is of 64x32 shape or smaller
  2156. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  2157. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  2158. // a thread shouldn't load data outside of the matrix
  2159. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  2160. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  2161. simdgroup_half8x8 ma[4];
  2162. simdgroup_float8x8 mb[2];
  2163. simdgroup_float8x8 c_res[8];
  2164. for (int i = 0; i < 8; i++){
  2165. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  2166. }
  2167. short il = (tiitg % THREAD_PER_ROW);
  2168. uint offset0 = im/gqa*nb02;
  2169. ushort offset1 = il/nl;
  2170. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  2171. device const float * y = (device const float *)(src1
  2172. + nb12 * im
  2173. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  2174. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  2175. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  2176. // load data and store to threadgroup memory
  2177. half4x4 temp_a;
  2178. dequantize_func(x, il, temp_a);
  2179. threadgroup_barrier(mem_flags::mem_threadgroup);
  2180. #pragma unroll(16)
  2181. for (int i = 0; i < 16; i++) {
  2182. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  2183. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  2184. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  2185. }
  2186. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  2187. il = (il + 2 < nl) ? il + 2 : il % 2;
  2188. x = (il < 2) ? x + (2+nl-1)/nl : x;
  2189. y += BLOCK_SIZE_K;
  2190. threadgroup_barrier(mem_flags::mem_threadgroup);
  2191. // load matrices from threadgroup memory and conduct outer products
  2192. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  2193. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  2194. #pragma unroll(4)
  2195. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  2196. #pragma unroll(4)
  2197. for (int i = 0; i < 4; i++) {
  2198. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  2199. }
  2200. simdgroup_barrier(mem_flags::mem_none);
  2201. #pragma unroll(2)
  2202. for (int i = 0; i < 2; i++) {
  2203. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  2204. }
  2205. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  2206. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  2207. #pragma unroll(8)
  2208. for (int i = 0; i < 8; i++){
  2209. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  2210. }
  2211. }
  2212. }
  2213. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  2214. device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
  2215. + (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
  2216. for (int i = 0; i < 8; i++) {
  2217. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  2218. }
  2219. } else {
  2220. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  2221. threadgroup_barrier(mem_flags::mem_threadgroup);
  2222. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  2223. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  2224. for (int i = 0; i < 8; i++) {
  2225. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  2226. }
  2227. threadgroup_barrier(mem_flags::mem_threadgroup);
  2228. device float * C = dst + (BLOCK_SIZE_M * r0) + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  2229. if (sgitg == 0) {
  2230. for (int i = 0; i < n_rows; i++) {
  2231. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  2232. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  2233. }
  2234. }
  2235. }
  2236. }
  2237. }
  2238. #if QK_K == 256
  2239. #define QK_NL 16
  2240. #else
  2241. #define QK_NL 4
  2242. #endif
  2243. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  2244. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  2245. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  2246. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  2247. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  2248. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  2249. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_t kernel_get_rows<block_q5_0, 2, dequantize_q5_0>;
  2250. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_t kernel_get_rows<block_q5_1, 2, dequantize_q5_1>;
  2251. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  2252. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  2253. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  2254. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  2255. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  2256. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  2257. typedef void (mat_mm_t)(
  2258. device const uchar * src0,
  2259. device const uchar * src1,
  2260. device float * dst,
  2261. constant int64_t & ne00,
  2262. constant int64_t & ne02,
  2263. constant int64_t & nb01,
  2264. constant int64_t & nb02,
  2265. constant int64_t & ne12,
  2266. constant int64_t & nb10,
  2267. constant int64_t & nb11,
  2268. constant int64_t & nb12,
  2269. constant int64_t & ne0,
  2270. constant int64_t & ne1,
  2271. constant uint & gqa,
  2272. threadgroup uchar *, uint3, uint, uint);
  2273. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  2274. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  2275. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  2276. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  2277. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_0, 2, dequantize_q5_0>;
  2278. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_1, 2, dequantize_q5_1>;
  2279. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  2280. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  2281. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  2282. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  2283. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  2284. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;