ggml_vk_generate_shaders.py 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777
  1. #!/usr/bin/env python
  2. import argparse
  3. import asyncio
  4. import os
  5. import sys
  6. from tempfile import gettempdir, NamedTemporaryFile
  7. shader_f32 = """
  8. #define FLOAT_TYPE float
  9. """
  10. shader_f16 = """
  11. #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
  12. #define FLOAT_TYPE float16_t
  13. """
  14. shader_int8_ext = """
  15. #extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
  16. """
  17. # Type-specific defines
  18. shader_f32_defines = """
  19. #define QUANT_K 1
  20. #define QUANT_R 1
  21. #define A_TYPE float
  22. """
  23. shader_f16_defines = """
  24. #define QUANT_K 1
  25. #define QUANT_R 1
  26. #define A_TYPE float16_t
  27. """
  28. shader_q4_0_defines = """
  29. #define QUANT_K 32
  30. #define QUANT_R 2
  31. struct block_q4_0
  32. {
  33. float16_t d;
  34. uint8_t qs[16];
  35. };
  36. #define A_TYPE block_q4_0
  37. """
  38. shader_q4_1_defines = """
  39. #define QUANT_K 32
  40. #define QUANT_R 2
  41. struct block_q4_1
  42. {
  43. float16_t d;
  44. float16_t m;
  45. uint8_t qs[16];
  46. };
  47. #define A_TYPE block_q4_1
  48. """
  49. shader_q5_0_defines = """
  50. #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
  51. #define QUANT_K 32
  52. #define QUANT_R 2
  53. struct block_q5_0
  54. {
  55. float16_t d;
  56. uint16_t qh[2];
  57. uint8_t qs[16];
  58. };
  59. #define A_TYPE block_q5_0
  60. """
  61. shader_q5_1_defines = """
  62. #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
  63. #define QUANT_K 32
  64. #define QUANT_R 2
  65. struct block_q5_1
  66. {
  67. float16_t d;
  68. float16_t m;
  69. uint qh;
  70. uint8_t qs[16];
  71. };
  72. #define A_TYPE block_q5_1
  73. """
  74. shader_q8_0_defines = """
  75. #define QUANT_K 32
  76. #define QUANT_R 1
  77. struct block_q8_0
  78. {
  79. float16_t d;
  80. int8_t qs[32];
  81. };
  82. #define A_TYPE block_q8_0
  83. """
  84. # K-quants
  85. shader_q2_K_defines = """
  86. #define QUANT_K 256
  87. struct block_q2_K
  88. {
  89. uint8_t scales[QUANT_K/16];
  90. uint8_t qs[QUANT_K/4];
  91. f16vec2 d;
  92. };
  93. #define A_TYPE block_q2_K
  94. """
  95. shader_q3_K_defines = """
  96. #define QUANT_K 256
  97. struct block_q3_K
  98. {
  99. uint8_t hmask[QUANT_K/8];
  100. uint8_t qs[QUANT_K/4];
  101. uint8_t scales[12];
  102. float16_t d;
  103. };
  104. #define A_TYPE block_q3_K
  105. """
  106. shader_q4_K_defines = """
  107. #define QUANT_K 256
  108. struct block_q4_K
  109. {
  110. f16vec2 d;
  111. uint8_t scales[3*QUANT_K/64];
  112. uint8_t qs[QUANT_K/2];
  113. };
  114. #define A_TYPE block_q4_K
  115. """
  116. shader_q5_K_defines = """
  117. #define QUANT_K 256
  118. struct block_q5_K
  119. {
  120. f16vec2 d;
  121. uint8_t scales[12];
  122. uint8_t qh[QUANT_K/8];
  123. uint8_t qs[QUANT_K/2];
  124. };
  125. #define A_TYPE block_q5_K
  126. """
  127. shader_q6_K_defines = """
  128. #define QUANT_K 256
  129. struct block_q6_K
  130. {
  131. uint8_t ql[QUANT_K/2];
  132. uint8_t qh[QUANT_K/4];
  133. int8_t scales[QUANT_K/16];
  134. float16_t d;
  135. };
  136. #define A_TYPE block_q6_K
  137. """
  138. # Dequant functions
  139. shader_float_dequant_func = """
  140. #define DEQUANT_FUNC vec2 v = vec2(ib, ib); // data_a[ib], data_a[ib + 1]);
  141. """
  142. shader_q4_0_dequant_func = """
  143. #define DEQUANT_FUNC const float d = float(data_a[ib].d); \
  144. const uint vui = uint(data_a[ib].qs[iqs]); \
  145. vec2 v = vec2(vui & 0xF, vui >> 4); \
  146. v = (v - 8.0f)*d;
  147. """
  148. shader_q4_1_dequant_func = """
  149. #define DEQUANT_FUNC const float d = float(data_a[ib].d); \
  150. const float m = float(data_a[ib].m); \
  151. const uint vui = uint(data_a[ib].qs[iqs]); \
  152. vec2 v = vec2(vui & 0xF, vui >> 4); \
  153. v = v*d + m;
  154. """
  155. shader_q5_0_dequant_func = """
  156. #define DEQUANT_FUNC const float d = float(data_a[ib].d); \
  157. const uint uint_qh = uint(data_a[ib].qh[1]) << 16 | data_a[ib].qh[0]; \
  158. const ivec2 qh = ivec2(((uint_qh >> iqs) << 4) & 0x10, (uint_qh >> (iqs + 12)) & 0x10); \
  159. const uint vui = uint(data_a[ib].qs[iqs]); \
  160. vec2 v = vec2((vui & 0xF) | qh.x, (vui >> 4) | qh.y); \
  161. v = (v - 16.0f) * d;
  162. """
  163. shader_q5_1_dequant_func = """
  164. #define DEQUANT_FUNC const float d = float(data_a[ib].d); \
  165. const float m = float(data_a[ib].m); \
  166. const uint uint_qh = data_a[ib].qh; \
  167. const ivec2 qh = ivec2(((uint_qh >> iqs) << 4) & 0x10, (uint_qh >> (iqs + 12)) & 0x10); \
  168. const uint vui = uint(data_a[ib].qs[iqs]); \
  169. vec2 v = vec2((vui & 0xF) | qh.x, (vui >> 4) | qh.y); \
  170. v = v*d + m;
  171. """
  172. shader_q8_0_dequant_func = """
  173. #define DEQUANT_FUNC const float d = float(data_a[ib].d); \
  174. vec2 v = vec2(int(data_a[ib].qs[iqs]), int(data_a[ib].qs[iqs + 1])); \
  175. v = v * d;
  176. """
  177. # MULMAT
  178. mulmat_head = """#version 450
  179. #extension GL_EXT_control_flow_attributes : enable
  180. #extension GL_EXT_shader_16bit_storage : require
  181. #ifndef LOAD_VEC_A
  182. #define LOAD_VEC_A 1
  183. #endif
  184. #ifndef LOAD_VEC_B
  185. #define LOAD_VEC_B 1
  186. #endif
  187. """
  188. mulmat_body1 = """
  189. layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
  190. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  191. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  192. layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
  193. layout (push_constant) uniform parameter
  194. {
  195. uint M;
  196. uint N;
  197. uint K;
  198. uint stride_a;
  199. uint stride_b;
  200. uint stride_d;
  201. uint k_split;
  202. uint ne02;
  203. uint ne12;
  204. uint broadcast2;
  205. uint broadcast3;
  206. uint batch_stride_a;
  207. uint batch_stride_b;
  208. uint batch_stride_d;
  209. } p;
  210. layout (constant_id = 1) const uint BM = 64;
  211. layout (constant_id = 2) const uint BN = 64;
  212. layout (constant_id = 3) const uint BK = 16; // Assumed to be 32 if working with a quant
  213. layout (constant_id = 4) const uint WM = 32;
  214. layout (constant_id = 5) const uint WN = 32;
  215. layout (constant_id = 6) const uint WMITER = 2;
  216. layout (constant_id = 7) const uint TM = 4;
  217. layout (constant_id = 8) const uint TN = 2;
  218. layout (constant_id = 9) const uint WARP = 32;
  219. shared FLOAT_TYPE buf_a[BM * (BK+1)];
  220. shared FLOAT_TYPE buf_b[BN * (BK+1)];
  221. void main() {
  222. const uint i13 = gl_GlobalInvocationID.z / p.ne12;
  223. const uint i12 = gl_GlobalInvocationID.z % p.ne12;
  224. const uint i03 = i13 / p.broadcast3;
  225. const uint i02 = i12 / p.broadcast2;
  226. const uint batch_idx_a = i03 * p.ne02 + i02;
  227. const uint blocks_m = (p.M + BM - 1) / BM;
  228. const uint ir = gl_WorkGroupID.x % blocks_m;
  229. const uint ik = gl_WorkGroupID.x / blocks_m;
  230. const uint ic = gl_WorkGroupID.y;
  231. const uint warp_i = gl_LocalInvocationID.x / WARP;
  232. const uint warp_r = warp_i % (BM / WM);
  233. const uint warp_c = warp_i / (BM / WM);
  234. const uint WNITER = (WM * WN) / (WARP * TM * TN * WMITER);
  235. const uint WSUBM = WM / WMITER;
  236. const uint WSUBN = WN / WNITER;
  237. const uint tiw = gl_LocalInvocationID.x % WARP;
  238. const uint tiwr = tiw % (WSUBM / TM);
  239. const uint tiwc = tiw / (WSUBM / TM);
  240. const uint loadr_a = gl_LocalInvocationID.x % (BK / LOAD_VEC_A);
  241. const uint loadc_a = gl_LocalInvocationID.x / (BK / LOAD_VEC_A);
  242. const uint loadr_b = gl_LocalInvocationID.x % (BK / LOAD_VEC_B);
  243. const uint loadc_b = gl_LocalInvocationID.x / (BK / LOAD_VEC_B);
  244. const uint loadstride_a = gl_WorkGroupSize.x * LOAD_VEC_A / BK;
  245. const uint loadstride_b = gl_WorkGroupSize.x * LOAD_VEC_B / BK;
  246. const uint start_k = ik * p.k_split;
  247. const uint end_k = min(p.K, (ik + 1) * p.k_split);
  248. uint pos_a = (batch_idx_a * p.batch_stride_a + ir * BM * p.stride_a + start_k) / LOAD_VEC_A;
  249. uint pos_b = (gl_GlobalInvocationID.z * p.batch_stride_b + ic * BN * p.stride_b + start_k) / LOAD_VEC_B;
  250. float sums[WMITER * TM * WNITER * TN];
  251. FLOAT_TYPE cache_a[WMITER * TM];
  252. FLOAT_TYPE cache_b[WNITER * TN];
  253. [[unroll]] for (uint i = 0; i < WMITER*TM*WNITER*TN; i++) {
  254. sums[i] = 0.0f;
  255. }
  256. [[unroll]] for (uint block = start_k; block < end_k; block += BK) {
  257. [[unroll]] for (uint l = 0; l < BM; l += loadstride_a) {"""
  258. mulmat_load_scalar = """
  259. #if LOAD_VEC_A == 8
  260. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  261. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  262. buf_a[buf_idx ] = FLOAT_TYPE(data_a[idx][0].x);
  263. buf_a[buf_idx + 1] = FLOAT_TYPE(data_a[idx][0].y);
  264. buf_a[buf_idx + 2] = FLOAT_TYPE(data_a[idx][0].z);
  265. buf_a[buf_idx + 3] = FLOAT_TYPE(data_a[idx][0].w);
  266. buf_a[buf_idx + 4] = FLOAT_TYPE(data_a[idx][1].x);
  267. buf_a[buf_idx + 5] = FLOAT_TYPE(data_a[idx][1].y);
  268. buf_a[buf_idx + 6] = FLOAT_TYPE(data_a[idx][1].z);
  269. buf_a[buf_idx + 7] = FLOAT_TYPE(data_a[idx][1].w);
  270. #elif LOAD_VEC_A == 4
  271. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  272. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  273. buf_a[buf_idx ] = FLOAT_TYPE(data_a[idx].x);
  274. buf_a[buf_idx + 1] = FLOAT_TYPE(data_a[idx].y);
  275. buf_a[buf_idx + 2] = FLOAT_TYPE(data_a[idx].z);
  276. buf_a[buf_idx + 3] = FLOAT_TYPE(data_a[idx].w);
  277. #else
  278. if (ir * BM + loadc_a + l < p.M && block + loadr_a < end_k) {
  279. buf_a[(loadc_a + l) * (BK+1) + loadr_a] = FLOAT_TYPE(data_a[pos_a + (loadc_a + l) * p.stride_a + loadr_a]);
  280. } else {
  281. buf_a[(loadc_a + l) * (BK+1) + loadr_a] = FLOAT_TYPE(0.0f);
  282. }
  283. #endif
  284. """
  285. mulmat_load_q4_0 = """
  286. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  287. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a;
  288. const uint ib = idx / 16;
  289. const uint iqs = idx & 0xF;
  290. const float d = float(data_a[ib].d);
  291. const uint vui = uint(data_a[ib].qs[iqs]);
  292. const vec2 v = (vec2(vui & 0xF, vui >> 4) - 8.0f) * d;
  293. buf_a[buf_idx ] = FLOAT_TYPE(v.x);
  294. buf_a[buf_idx + 16] = FLOAT_TYPE(v.y);"""
  295. mulmat_load_q4_1 = """
  296. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  297. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a;
  298. const uint ib = idx / 16;
  299. const uint iqs = idx & 0xF;
  300. const float d = float(data_a[ib].d);
  301. const float m = float(data_a[ib].m);
  302. const uint vui = uint(data_a[ib].qs[iqs]);
  303. const vec2 v = vec2(vui & 0xF, vui >> 4) * d + m;
  304. buf_a[buf_idx ] = FLOAT_TYPE(v.x);
  305. buf_a[buf_idx + 16] = FLOAT_TYPE(v.y);"""
  306. mulmat_load_q5_0 = """
  307. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  308. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a;
  309. const uint ib = idx / 16;
  310. const uint iqs = idx & 0xF;
  311. const float d = float(data_a[ib].d);
  312. const uint uint_qh = uint(data_a[ib].qh[1]) << 16 | data_a[ib].qh[0];
  313. const ivec2 qh = ivec2(((uint_qh >> iqs) << 4) & 0x10, (uint_qh >> (iqs + 12)) & 0x10);
  314. const uint vui = uint(data_a[ib].qs[iqs]);
  315. const vec2 v = (vec2((vui & 0xF) | qh.x, (vui >> 4) | qh.y) - 16.0f) * d;
  316. buf_a[buf_idx ] = FLOAT_TYPE(v.x);
  317. buf_a[buf_idx + 16] = FLOAT_TYPE(v.y);"""
  318. mulmat_load_q5_1 = """
  319. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  320. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a;
  321. const uint ib = idx / 16;
  322. const uint iqs = idx & 0xF;
  323. const float d = float(data_a[ib].d);
  324. const float m = float(data_a[ib].m);
  325. const uint uint_qh = data_a[ib].qh;
  326. const ivec2 qh = ivec2(((uint_qh >> iqs) << 4) & 0x10, (uint_qh >> (iqs + 12)) & 0x10);
  327. const uint vui = uint(data_a[ib].qs[iqs]);
  328. const vec2 v = vec2((vui & 0xF) | qh.x, (vui >> 4) | qh.y) * d + m;
  329. buf_a[buf_idx ] = FLOAT_TYPE(v.x);
  330. buf_a[buf_idx + 16] = FLOAT_TYPE(v.y);"""
  331. mulmat_load_q8_0 = """
  332. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  333. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  334. const uint ib = idx / 16;
  335. const uint iqs = (idx & 0xF) * 2;
  336. const float d = float(data_a[ib].d);
  337. const vec2 v = vec2(int(data_a[ib].qs[iqs]), int(data_a[ib].qs[iqs + 1])) * d;
  338. buf_a[buf_idx ] = FLOAT_TYPE(v.x);
  339. buf_a[buf_idx + 1] = FLOAT_TYPE(v.y);"""
  340. mulmat_load_q2_K = """
  341. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  342. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  343. const uint ib = idx / 128; // 2 values per idx
  344. const uint iqs = idx % 128; // 0..127
  345. const uint qsi = (iqs / 64) * 32 + (iqs % 16) * 2; // 0,2,4..30
  346. const uint scalesi = iqs / 8; // 0..15
  347. const uint qsshift = ((iqs % 64) / 16) * 2; // 0,2,4,6
  348. const uvec2 qs = uvec2(data_a[ib].qs[qsi], data_a[ib].qs[qsi + 1]);
  349. const uint scales = data_a[ib].scales[scalesi];
  350. const vec2 d = vec2(data_a[ib].d);
  351. const vec2 v = d.x * float(scales & 0xF) * vec2((qs >> qsshift) & 3) - d.y * float(scales >> 4);
  352. buf_a[buf_idx ] = FLOAT_TYPE(v.x);
  353. buf_a[buf_idx + 1] = FLOAT_TYPE(v.y);"""
  354. mulmat_load_q3_K = """
  355. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  356. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  357. const uint ib = idx / 128; // 2 values per idx
  358. const uint iqs = idx % 128; // 0..127
  359. const uint n = iqs / 64; // 0,1
  360. const uint qsi = n * 32 + (iqs % 16) * 2; // 0,2,4..62
  361. const uint hmi = (iqs % 16) * 2; // 0,2,4..30
  362. const uint j = (iqs % 64) / 4; // 0..3
  363. const uint is = iqs / 8; // 0..15
  364. const uint halfsplit = ((iqs % 64) / 16); // 0,1,2,3
  365. const uint qsshift = halfsplit * 2; // 0,2,4,6
  366. const uint m = 1 << (4 * n + halfsplit); // 1,2,4,8,16,32,64,128
  367. const int8_t us = int8_t(is < 4 ? (data_a[ib].scales[is-0] & 0xF) | (((data_a[ib].scales[is+8] >> 0) & 3) << 4) :
  368. is < 8 ? (data_a[ib].scales[is-0] & 0xF) | (((data_a[ib].scales[is+4] >> 2) & 3) << 4) :
  369. is < 12 ? (data_a[ib].scales[is-8] >> 4) | (((data_a[ib].scales[is+0] >> 4) & 3) << 4) :
  370. (data_a[ib].scales[is-8] >> 4) | (((data_a[ib].scales[is-4] >> 6) & 3) << 4));
  371. const float dl = float(data_a[ib].d) * float(us - 32);
  372. buf_a[buf_idx ] = FLOAT_TYPE(dl * float(int8_t((data_a[ib].qs[qsi ] >> qsshift) & 3) - (((data_a[ib].hmask[hmi ] & m) != 0) ? 0 : 4)));
  373. buf_a[buf_idx + 1] = FLOAT_TYPE(dl * float(int8_t((data_a[ib].qs[qsi + 1] >> qsshift) & 3) - (((data_a[ib].hmask[hmi + 1] & m) != 0) ? 0 : 4)));"""
  374. mulmat_load_q4_K = """
  375. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  376. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  377. const uint ib = idx / 128; // 2 values per idx
  378. const uint iqs = idx % 128; // 0..127
  379. const uint n = iqs / 32; // 0,1,2,3
  380. const uint b = (iqs % 32) / 16; // 0,1
  381. const uint is = 2 * n + b; // 0..7
  382. const uint qsi = n * 32 + (iqs % 16) * 2; // 0,2,4..126
  383. const vec2 loadd = vec2(data_a[ib].d);
  384. uint8_t sc;
  385. uint8_t mbyte;
  386. if (is < 4) {
  387. sc = uint8_t(data_a[ib].scales[is ] & 63);
  388. mbyte = uint8_t(data_a[ib].scales[is + 4] & 63);
  389. } else {
  390. sc = uint8_t((data_a[ib].scales[is + 4] & 0xF) | ((data_a[ib].scales[is - 4] >> 6) << 4));
  391. mbyte = uint8_t((data_a[ib].scales[is + 4] >> 4) | ((data_a[ib].scales[is ] >> 6) << 4));
  392. }
  393. const float d = loadd.x * sc;
  394. const float m = loadd.y * mbyte;
  395. buf_a[buf_idx ] = FLOAT_TYPE(d * float((data_a[ib].qs[qsi ] >> (b * 4)) & 0xF) - m);
  396. buf_a[buf_idx + 1] = FLOAT_TYPE(d * float((data_a[ib].qs[qsi + 1] >> (b * 4)) & 0xF) - m);"""
  397. mulmat_load_q5_K = """
  398. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  399. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  400. const uint ib = idx / 128; // 2 values per idx
  401. const uint iqs = idx % 128; // 0..127
  402. const uint n = iqs / 32; // 0,1,2,3
  403. const uint b = (iqs % 32) / 16; // 0,1
  404. const uint is = 2 * n + b; // 0..7
  405. const uint qsi = n * 32 + (iqs % 16) * 2; // 0,2,4..126
  406. const uint qhi = (iqs % 16) * 2; // 0,2,4..30
  407. const uint8_t hm = uint8_t(1 << (iqs / 16));
  408. const vec2 loadd = vec2(data_a[ib].d);
  409. uint8_t sc;
  410. uint8_t mbyte;
  411. if (is < 4) {
  412. sc = uint8_t(data_a[ib].scales[is ] & 63);
  413. mbyte = uint8_t(data_a[ib].scales[is + 4] & 63);
  414. } else {
  415. sc = uint8_t((data_a[ib].scales[is + 4] & 0xF) | ((data_a[ib].scales[is - 4] >> 6) << 4));
  416. mbyte = uint8_t((data_a[ib].scales[is + 4] >> 4) | ((data_a[ib].scales[is ] >> 6) << 4));
  417. }
  418. const float d = loadd.x * sc;
  419. const float m = loadd.y * mbyte;
  420. buf_a[buf_idx ] = FLOAT_TYPE(d * (float((data_a[ib].qs[qsi ] >> (b * 4)) & 0xF) + float((data_a[ib].qh[qhi ] & hm) != 0 ? 16 : 0)) - m);
  421. buf_a[buf_idx + 1] = FLOAT_TYPE(d * (float((data_a[ib].qs[qsi + 1] >> (b * 4)) & 0xF) + float((data_a[ib].qh[qhi + 1] & hm) != 0 ? 16 : 0)) - m);"""
  422. mulmat_load_q6_K = """
  423. const uint idx = pos_a + (loadc_a + l) * p.stride_a / LOAD_VEC_A + loadr_a;
  424. const uint buf_idx = (loadc_a + l) * (BK+1) + loadr_a * LOAD_VEC_A;
  425. const uint ib = idx / 128; // 2 values per idx
  426. const uint iqs = idx % 128; // 0..127
  427. const uint n = iqs / 64; // 0,1
  428. const uint b = (iqs % 64) / 32; // 0,1
  429. const uint is_b = (iqs % 16) / 8; // 0,1
  430. const uint qhshift = ((iqs % 64) / 16) * 2; // 0,2,4,6
  431. const uint is = 8 * n + qhshift + is_b; // 0..15
  432. const uint qsi = n * 64 + (iqs % 32) * 2; // 0,2,4..126
  433. const uint qhi = n * 32 + (iqs % 16) * 2; // 0,2,4..62
  434. const float dscale = float(data_a[ib].d) * float(data_a[ib].scales[is]);
  435. buf_a[buf_idx ] = FLOAT_TYPE(dscale * float(int8_t(((data_a[ib].ql[qsi ] >> (b * 4)) & 0xF) | (((data_a[ib].qh[qhi ] >> qhshift) & 3) << 4)) - 32));
  436. buf_a[buf_idx + 1] = FLOAT_TYPE(dscale * float(int8_t(((data_a[ib].ql[qsi + 1] >> (b * 4)) & 0xF) | (((data_a[ib].qh[qhi + 1] >> qhshift) & 3) << 4)) - 32));"""
  437. mulmat_body2 = """
  438. }
  439. [[unroll]] for (uint l = 0; l < BN; l += loadstride_b) {
  440. #if LOAD_VEC_B == 8
  441. const uint idx = pos_b + (loadc_b + l) * p.stride_b / LOAD_VEC_B + loadr_b;
  442. const uint buf_idx = (loadc_b + l) * (BK+1) + loadr_b * LOAD_VEC_B;
  443. buf_b[buf_idx + 0] = FLOAT_TYPE(data_b[idx][0].x);
  444. buf_b[buf_idx + 1] = FLOAT_TYPE(data_b[idx][0].y);
  445. buf_b[buf_idx + 2] = FLOAT_TYPE(data_b[idx][0].z);
  446. buf_b[buf_idx + 3] = FLOAT_TYPE(data_b[idx][0].w);
  447. buf_b[buf_idx + 4] = FLOAT_TYPE(data_b[idx][1].x);
  448. buf_b[buf_idx + 5] = FLOAT_TYPE(data_b[idx][1].y);
  449. buf_b[buf_idx + 6] = FLOAT_TYPE(data_b[idx][1].z);
  450. buf_b[buf_idx + 7] = FLOAT_TYPE(data_b[idx][1].w);
  451. #elif LOAD_VEC_B == 4
  452. const uint idx = pos_b + (loadc_b + l) * p.stride_b / LOAD_VEC_B + loadr_b;
  453. const uint buf_idx = (loadc_b + l) * (BK+1) + loadr_b * LOAD_VEC_B;
  454. buf_b[buf_idx + 0] = FLOAT_TYPE(data_b[idx].x);
  455. buf_b[buf_idx + 1] = FLOAT_TYPE(data_b[idx].y);
  456. buf_b[buf_idx + 2] = FLOAT_TYPE(data_b[idx].z);
  457. buf_b[buf_idx + 3] = FLOAT_TYPE(data_b[idx].w);
  458. #else
  459. if (ic * BN + loadc_b + l < p.N && block + loadr_b < end_k) {
  460. buf_b[(loadc_b + l) * (BK+1) + loadr_b] = FLOAT_TYPE(data_b[pos_b + (loadc_b + l) * p.stride_b + loadr_b]);
  461. } else {
  462. buf_b[(loadc_b + l) * (BK+1) + loadr_b] = FLOAT_TYPE(0.0f);
  463. }
  464. #endif
  465. }
  466. barrier();
  467. pos_a += BK / LOAD_VEC_A;
  468. pos_b += BK / LOAD_VEC_B;
  469. for (uint i = 0; i < BK; i++) {
  470. // Load from shared into cache
  471. [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) {
  472. [[unroll]] for (uint j = 0; j < TM; j++) {
  473. cache_a[wsir * TM + j] = buf_a[(warp_r * WM + wsir * WSUBM + tiwr * TM + j) * (BK+1) + i];
  474. }
  475. }
  476. [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) {
  477. [[unroll]] for (uint j = 0; j < TN; j++) {
  478. cache_b[wsic * TN + j] = buf_b[(warp_c * WN + wsic * WSUBN + tiwc * TN + j) * (BK+1) + i];
  479. }
  480. }
  481. [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) {
  482. [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) {
  483. [[unroll]] for (uint cc = 0; cc < TN; cc++) {
  484. [[unroll]] for (uint cr = 0; cr < TM; cr++) {
  485. sums[(wsic * TN + cc) * (WMITER * TM) + wsir * TM + cr] += float(cache_a[wsir * TM + cr]) * float(cache_b[wsic * TN + cc]);
  486. }
  487. }
  488. }
  489. }
  490. }
  491. barrier();
  492. }
  493. const uint dr = ir * BM + warp_r * WM;
  494. const uint dc = ic * BN + warp_c * WN;
  495. const uint offsets = gl_GlobalInvocationID.z * p.batch_stride_d + ik * p.batch_stride_d * gl_NumWorkGroups.z;
  496. [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) {
  497. [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) {
  498. const uint dr_warp = dr + wsir * WSUBM + tiwr * TM;
  499. const uint dc_warp = dc + wsic * WSUBN + tiwc * TN;
  500. [[unroll]] for (uint cc = 0; cc < TN; cc++) {
  501. [[unroll]] for (uint cr = 0; cr < TM; cr++) {
  502. if (dr_warp + cr < p.M && dc_warp + cc < p.N) {
  503. data_d[offsets + (dc_warp + cc) * p.stride_d + dr_warp + cr] = D_TYPE(sums[(wsic * TN + cc) * (WMITER * TM) + wsir * TM + cr]);
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. """
  511. mulmat_split_k_reduce_src = """#version 450
  512. #extension GL_EXT_control_flow_attributes : enable
  513. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  514. layout (binding = 0) readonly buffer A {float data_a[];};
  515. layout (binding = 1) writeonly buffer D {float data_d[];};
  516. layout (push_constant) uniform parameter {
  517. uint ne;
  518. uint k_num;
  519. } p;
  520. void main() {
  521. const uint idx = gl_GlobalInvocationID.x;
  522. if (idx >= p.ne) {
  523. return;
  524. }
  525. float result = 0.0f;
  526. [[unroll]] for (uint i = 0; i < p.k_num; i++) {
  527. result += data_a[i * p.ne + idx];
  528. }
  529. data_d[idx] = result;
  530. }
  531. """
  532. # DEQUANT SHADER
  533. dequant_head = """#version 450
  534. #extension GL_EXT_control_flow_attributes : require
  535. #extension GL_EXT_shader_16bit_storage : require
  536. layout (push_constant) uniform parameter
  537. {
  538. uint M;
  539. uint K;
  540. uint stride_a;
  541. uint stride_b;
  542. uint nel;
  543. } p;
  544. """
  545. dequant_f32_body = """
  546. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  547. layout (binding = 0) readonly buffer A {float data_a[];};
  548. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  549. void main() {
  550. const uint i = gl_GlobalInvocationID.x * 16;
  551. if (i >= p.nel) {
  552. return;
  553. }
  554. [[unroll]] for (uint l = 0; l < 16; l++) {
  555. data_b[i + l] = D_TYPE(data_a[i + l]);
  556. }
  557. }
  558. """
  559. dequant_q4_0_body = """
  560. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  561. layout (binding = 0) readonly buffer A {block_q4_0 data_a[];};
  562. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  563. void main() {
  564. const uint i = gl_WorkGroupID.x * 4 + gl_LocalInvocationID.x / 64;
  565. const uint tid = gl_LocalInvocationID.x % 64;
  566. const uint il = tid/32;
  567. const uint ir = tid%32;
  568. const uint ib = 32*i + ir;
  569. if (ib >= p.nel / 32) {
  570. return;
  571. }
  572. const uint b_idx = 1024*i + 32*ir + 8*il;
  573. const float d = float(data_a[ib].d);
  574. const float dm = -8.0f * d;
  575. const uint q_idx = 8*il;
  576. [[unroll]] for (uint l = 0; l < 8; ++l) {
  577. data_b[b_idx + l + 0] = D_TYPE(d * (data_a[ib].qs[q_idx + l] & 0xF) + dm);
  578. data_b[b_idx + l + 16] = D_TYPE(d * (data_a[ib].qs[q_idx + l] >> 4) + dm);
  579. }
  580. }
  581. """
  582. dequant_q4_1_body = """
  583. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  584. layout (binding = 0) readonly buffer A {block_q4_1 data_a[];};
  585. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  586. void main() {
  587. const uint i = gl_WorkGroupID.x * 4 + gl_LocalInvocationID.x / 64;
  588. const uint tid = gl_LocalInvocationID.x % 64;
  589. const uint il = tid/32;
  590. const uint ir = tid%32;
  591. const uint ib = 32*i + ir;
  592. if (ib >= p.nel / 32) {
  593. return;
  594. }
  595. const uint b_idx = 1024*i + 32*ir + 8*il;
  596. const float d = float(data_a[ib].d);
  597. const float m = float(data_a[ib].m);
  598. const uint q_idx = 8*il;
  599. [[unroll]] for (uint l = 0; l < 8; ++l) {
  600. data_b[b_idx + l + 0] = D_TYPE(d * (data_a[ib].qs[q_idx + l] & 0xF) + m);
  601. data_b[b_idx + l + 16] = D_TYPE(d * (data_a[ib].qs[q_idx + l] >> 4) + m);
  602. }
  603. }
  604. """
  605. dequant_q5_0_body = """
  606. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  607. layout (binding = 0) readonly buffer A {block_q5_0 data_a[];};
  608. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  609. void main() {
  610. const uint i = gl_WorkGroupID.x * 4 + gl_LocalInvocationID.x / 64;
  611. const uint tid = gl_LocalInvocationID.x % 64;
  612. const uint il = tid/32;
  613. const uint ir = tid%32;
  614. const uint ib = 32*i + ir;
  615. if (ib >= p.nel / 32) {
  616. return;
  617. }
  618. const uint b_idx = 1024*i + 32*ir + 8*il;
  619. const float d = float(data_a[ib].d);
  620. const uint qh = uint(data_a[ib].qh[1]) << 16 | data_a[ib].qh[0];
  621. const uint q_idx = 8*il;
  622. [[unroll]] for (uint l = 0; l < 8; ++l) {
  623. const uint iqs = q_idx + l;
  624. const uint vui = uint(data_a[ib].qs[iqs]);
  625. data_b[b_idx + l + 0] = D_TYPE(d * (((vui & 0xF) | (((qh >> iqs) << 4) & 0x10)) - 16.0f));
  626. data_b[b_idx + l + 16] = D_TYPE(d * (((vui >> 4) | ((qh >> (iqs + 12)) & 0x10)) - 16.0f));
  627. }
  628. }
  629. """
  630. dequant_q5_1_body = """
  631. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  632. layout (binding = 0) readonly buffer A {block_q5_1 data_a[];};
  633. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  634. void main() {
  635. const uint i = gl_WorkGroupID.x * 4 + gl_LocalInvocationID.x / 64;
  636. const uint tid = gl_LocalInvocationID.x % 64;
  637. const uint il = tid/32;
  638. const uint ir = tid%32;
  639. const uint ib = 32*i + ir;
  640. if (ib >= p.nel / 32) {
  641. return;
  642. }
  643. const uint b_idx = 1024*i + 32*ir + 8*il;
  644. const float d = float(data_a[ib].d);
  645. const float m = float(data_a[ib].m);
  646. const uint qh = data_a[ib].qh;
  647. const uint q_idx = 8*il;
  648. [[unroll]] for (uint l = 0; l < 8; ++l) {
  649. const uint iqs = q_idx + l;
  650. const uint vui = uint(data_a[ib].qs[iqs]);
  651. data_b[b_idx + l + 0] = D_TYPE(d * (((vui & 0xF) | (((qh >> iqs) << 4) & 0x10))) + m);
  652. data_b[b_idx + l + 16] = D_TYPE(d * (((vui >> 4) | ((qh >> (iqs + 12)) & 0x10))) + m);
  653. }
  654. }
  655. """
  656. dequant_q8_0_body = """
  657. layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  658. layout (binding = 0) readonly buffer A {block_q8_0 data_a[];};
  659. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  660. void main() {
  661. const uint i = gl_WorkGroupID.x * 4 + gl_LocalInvocationID.x / 64;
  662. const uint tid = gl_LocalInvocationID.x % 64;
  663. const uint il = tid/32;
  664. const uint ir = tid%32;
  665. const uint ib = 32*i + ir;
  666. if (ib >= p.nel / 32) {
  667. return;
  668. }
  669. const uint b_idx = 1024*i + 32*ir + 16*il;
  670. const float d = float(data_a[ib].d);
  671. const uint q_idx = 16*il;
  672. [[unroll]] for (uint l = 0; l < 16; l += 2) {
  673. data_b[b_idx + l ] = D_TYPE(d * data_a[ib].qs[q_idx + l ]);
  674. data_b[b_idx + l + 1] = D_TYPE(d * data_a[ib].qs[q_idx + l + 1]);
  675. }
  676. }
  677. """
  678. # K-quants
  679. dequant_q2_K_body = """
  680. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  681. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  682. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  683. void main() {
  684. [[unroll]] for (uint wgy = 0; wgy < 256; wgy++) {
  685. const uint i = gl_WorkGroupID.x * 256 + wgy;
  686. if (i >= p.M * p.K / QUANT_K) {
  687. return;
  688. }
  689. const uint tid = gl_LocalInvocationID.x;
  690. const uint ip = tid / 32;
  691. const uint il = tid - 32 * ip;
  692. const uint is = 8 * ip + il / 16;
  693. const uint y_idx = i * QUANT_K + 128 * ip + il;
  694. const uint ql_idx = 32 * ip + il;
  695. const uint8_t qs = data_a[i].qs[32 * ip + il];
  696. FLOAT_TYPE dall = FLOAT_TYPE(data_a[i].d.x);
  697. FLOAT_TYPE dmin = FLOAT_TYPE(data_a[i].d.y);
  698. data_b[y_idx + 0] = D_TYPE(dall * FLOAT_TYPE((data_a[i].scales[is+0] & 0xF) * ((qs >> 0) & 3)) - dmin * FLOAT_TYPE(data_a[i].scales[is+0] >> 4));
  699. data_b[y_idx + 32] = D_TYPE(dall * FLOAT_TYPE((data_a[i].scales[is+2] & 0xF) * ((qs >> 2) & 3)) - dmin * FLOAT_TYPE(data_a[i].scales[is+2] >> 4));
  700. data_b[y_idx + 64] = D_TYPE(dall * FLOAT_TYPE((data_a[i].scales[is+4] & 0xF) * ((qs >> 4) & 3)) - dmin * FLOAT_TYPE(data_a[i].scales[is+4] >> 4));
  701. data_b[y_idx + 96] = D_TYPE(dall * FLOAT_TYPE((data_a[i].scales[is+6] & 0xF) * ((qs >> 6) & 3)) - dmin * FLOAT_TYPE(data_a[i].scales[is+6] >> 4));
  702. }
  703. }
  704. """
  705. dequant_q3_K_body = """
  706. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  707. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  708. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  709. void main() {
  710. [[unroll]] for (uint wgy = 0; wgy < 256; wgy++) {
  711. const uint i = uint(gl_WorkGroupID.x * 256 + wgy);
  712. if (i >= p.M * p.K / QUANT_K) {
  713. return;
  714. }
  715. const uint r = gl_LocalInvocationID.x / 4;
  716. const uint tid = r / 2;
  717. const uint is0 = r % 2;
  718. const uint l0 = 16 * is0 + 4 * (gl_LocalInvocationID.x % 4);
  719. const uint n = tid / 4;
  720. const uint j = tid - 4*n;
  721. const uint8_t m = uint8_t(1 << (4*n + j));
  722. const uint is = 8*n + 2*j + is0;
  723. const uint shift = 2*j;
  724. const int8_t us = int8_t(is < 4 ? (data_a[i].scales[is-0] & 0xF) | (((data_a[i].scales[is+8] >> 0) & 3) << 4) :
  725. is < 8 ? (data_a[i].scales[is-0] & 0xF) | (((data_a[i].scales[is+4] >> 2) & 3) << 4) :
  726. is < 12 ? (data_a[i].scales[is-8] >> 4) | (((data_a[i].scales[is+0] >> 4) & 3) << 4) :
  727. (data_a[i].scales[is-8] >> 4) | (((data_a[i].scales[is-4] >> 6) & 3) << 4));
  728. const FLOAT_TYPE d_all = FLOAT_TYPE(data_a[i].d);
  729. const FLOAT_TYPE dl = d_all * FLOAT_TYPE(us - 32);
  730. const uint y_idx = i * QUANT_K + 128 * n + 32 * j;
  731. const uint qs_idx = 32*n;
  732. for (uint l = l0; l < l0 + 4; ++l) {
  733. data_b[y_idx + l] = D_TYPE(dl * FLOAT_TYPE(int8_t((data_a[i].qs[qs_idx + l] >> shift) & 3) - (((data_a[i].hmask[l] & m) != 0) ? 0 : 4)));
  734. }
  735. }
  736. }
  737. """
  738. dequant_q4_K_body = """
  739. layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
  740. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  741. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  742. void main() {
  743. [[unroll]] for (uint wgy = 0; wgy < 256; wgy++) {
  744. const uint i = gl_WorkGroupID.x * 256 + wgy;
  745. if (i >= p.M * p.K / QUANT_K) {
  746. return;
  747. }
  748. const uint tid = gl_LocalInvocationID.x;
  749. const uint il = tid / 8;
  750. const uint ir = tid % 8;
  751. const uint is = 2 * il;
  752. const uint n = 4;
  753. const FLOAT_TYPE dall = FLOAT_TYPE(data_a[i].d.x);
  754. const FLOAT_TYPE dmin = FLOAT_TYPE(data_a[i].d.y);
  755. const uint y_idx = i * QUANT_K + 64 * il + n * ir;
  756. const uint qs_idx = 32*il + n * ir;
  757. uint8_t sc;
  758. uint8_t m;
  759. if (is < 4) {
  760. sc = uint8_t(data_a[i].scales[is] & 63);
  761. m = uint8_t(data_a[i].scales[is + 4] & 63);
  762. } else {
  763. sc = uint8_t((data_a[i].scales[is + 4] & 0xF) | ((data_a[i].scales[is - 4] >> 6) << 4));
  764. m = uint8_t((data_a[i].scales[is + 4] >> 4) | ((data_a[i].scales[is ] >> 6) << 4));
  765. }
  766. const FLOAT_TYPE d1 = dall * sc;
  767. const FLOAT_TYPE m1 = dmin * m;
  768. if (is < 4) {
  769. sc = uint8_t(data_a[i].scales[is + 1] & 63);
  770. m = uint8_t(data_a[i].scales[is + 5] & 63);
  771. } else {
  772. sc = uint8_t((data_a[i].scales[is + 5] & 0xF) | ((data_a[i].scales[is - 3] >> 6) << 4));
  773. m = uint8_t((data_a[i].scales[is + 5] >> 4) | ((data_a[i].scales[is + 1] >> 6) << 4));
  774. }
  775. const FLOAT_TYPE d2 = dall * sc;
  776. const FLOAT_TYPE m2 = dmin * m;
  777. [[unroll]] for (uint l = 0; l < n; ++l) {
  778. data_b[y_idx + l ] = D_TYPE(d1 * FLOAT_TYPE(data_a[i].qs[qs_idx + l] & 0xF) - m1);
  779. data_b[y_idx + l + 32] = D_TYPE(d2 * FLOAT_TYPE(data_a[i].qs[qs_idx + l] >> 4) - m2);
  780. }
  781. }
  782. }
  783. """
  784. dequant_q5_K_body = """
  785. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  786. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  787. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  788. void main() {
  789. [[unroll]] for (uint wgy = 0; wgy < 256; wgy++) {
  790. const uint i = gl_WorkGroupID.x * 256 + wgy;
  791. if (i >= p.M * p.K / QUANT_K) {
  792. return;
  793. }
  794. const uint tid = gl_LocalInvocationID.x;
  795. const uint il = tid / 16;
  796. const uint ir = tid % 16;
  797. const uint is = 2 * il;
  798. const FLOAT_TYPE dall = FLOAT_TYPE(data_a[i].d.x);
  799. const FLOAT_TYPE dmin = FLOAT_TYPE(data_a[i].d.y);
  800. const uint y_idx = i * QUANT_K + 64 * il + 2 * ir;
  801. const uint qs_idx = 32*il + 2 * ir;
  802. const uint qh_idx = 2 * ir;
  803. uint8_t sc;
  804. uint8_t m;
  805. if (is < 4) {
  806. sc = uint8_t(data_a[i].scales[is] & 63);
  807. m = uint8_t(data_a[i].scales[is + 4] & 63);
  808. } else {
  809. sc = uint8_t((data_a[i].scales[is + 4] & 0xF) | ((data_a[i].scales[is - 4] >> 6) << 4));
  810. m = uint8_t((data_a[i].scales[is + 4] >> 4) | ((data_a[i].scales[is ] >> 6) << 4));
  811. }
  812. const FLOAT_TYPE d1 = dall * sc;
  813. const FLOAT_TYPE m1 = dmin * m;
  814. if (is < 4) {
  815. sc = uint8_t(data_a[i].scales[is + 1] & 63);
  816. m = uint8_t(data_a[i].scales[is + 5] & 63);
  817. } else {
  818. sc = uint8_t((data_a[i].scales[is + 5] & 0xF) | ((data_a[i].scales[is - 3] >> 6) << 4));
  819. m = uint8_t((data_a[i].scales[is + 5] >> 4) | ((data_a[i].scales[is + 1] >> 6) << 4));
  820. }
  821. const FLOAT_TYPE d2 = dall * sc;
  822. const FLOAT_TYPE m2 = dmin * m;
  823. const uint8_t hm1 = uint8_t(1 << (2 * il ));
  824. const uint8_t hm2 = uint8_t(1 << (2 * il + 1));
  825. data_b[y_idx ] = D_TYPE(d1 * FLOAT_TYPE((data_a[i].qs[qs_idx ] & 0xF) + (((data_a[i].qh[qh_idx ] & hm1) != 0) ? 16 : 0)) - m1);
  826. data_b[y_idx + 1] = D_TYPE(d1 * FLOAT_TYPE((data_a[i].qs[qs_idx + 1] & 0xF) + (((data_a[i].qh[qh_idx + 1] & hm1) != 0) ? 16 : 0)) - m1);
  827. data_b[y_idx + 32] = D_TYPE(d2 * FLOAT_TYPE((data_a[i].qs[qs_idx ] >> 4) + (((data_a[i].qh[qh_idx ] & hm2) != 0) ? 16 : 0)) - m2);
  828. data_b[y_idx + 33] = D_TYPE(d2 * FLOAT_TYPE((data_a[i].qs[qs_idx + 1] >> 4) + (((data_a[i].qh[qh_idx + 1] & hm2) != 0) ? 16 : 0)) - m2);
  829. }
  830. }
  831. """
  832. dequant_q6_K_body = """
  833. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  834. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  835. layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
  836. void main() {
  837. [[unroll]] for (uint wgy = 0; wgy < 256; wgy++) {
  838. const uint i = gl_WorkGroupID.x * 256 + wgy;
  839. if (i >= p.M * p.K / QUANT_K) {
  840. return;
  841. }
  842. const uint tid = gl_LocalInvocationID.x;
  843. const uint ip = tid / 32;
  844. const uint il = tid - 32 * ip;
  845. const uint is = 8 * ip + il / 16;
  846. const uint y_idx = i * QUANT_K + 128 * ip + il;
  847. const uint ql_idx = 64 * ip + il;
  848. const uint8_t qh = data_a[i].qh[32 * ip + il];
  849. const FLOAT_TYPE d = FLOAT_TYPE(data_a[i].d);
  850. data_b[y_idx + 0] = D_TYPE(d * FLOAT_TYPE(data_a[i].scales[is + 0] * (int8_t((data_a[i].ql[ql_idx + 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32)));
  851. data_b[y_idx + 32] = D_TYPE(d * FLOAT_TYPE(data_a[i].scales[is + 2] * (int8_t((data_a[i].ql[ql_idx + 32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32)));
  852. data_b[y_idx + 64] = D_TYPE(d * FLOAT_TYPE(data_a[i].scales[is + 4] * (int8_t((data_a[i].ql[ql_idx + 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32)));
  853. data_b[y_idx + 96] = D_TYPE(d * FLOAT_TYPE(data_a[i].scales[is + 6] * (int8_t((data_a[i].ql[ql_idx + 32] >> 4) | (((qh >> 6) & 3) << 4)) - 32)));
  854. }
  855. }
  856. """
  857. # Mul Mat Vec
  858. mul_mat_vec_head = """#version 450
  859. #extension GL_EXT_control_flow_attributes : enable
  860. #extension GL_EXT_shader_16bit_storage : require
  861. #extension GL_EXT_shader_8bit_storage : require
  862. layout (push_constant) uniform parameter
  863. {
  864. uint ncols;
  865. uint b_offset;
  866. uint d_offset;
  867. } p;
  868. """
  869. mul_mat_vec_body = """
  870. layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
  871. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  872. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  873. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  874. layout (constant_id = 0) const uint BLOCK_SIZE = 32;
  875. shared FLOAT_TYPE tmp[BLOCK_SIZE];
  876. void main() {
  877. const uint row = gl_WorkGroupID.x;
  878. const uint tid = gl_LocalInvocationID.x;
  879. const uint y_offset = QUANT_R == 1 ? 1 : QUANT_K/2;
  880. tmp[tid] = FLOAT_TYPE(0.0f);
  881. [[unroll]] for (uint i = 0; i < p.ncols/BLOCK_SIZE; i += 2) {
  882. const uint col = i*BLOCK_SIZE + 2*tid;
  883. const uint ib = (row*p.ncols + col)/QUANT_K; // block index
  884. const uint iqs = (col%QUANT_K)/QUANT_R; // quant index
  885. const uint iybs = col - col%QUANT_K; // y block start index
  886. DEQUANT_FUNC
  887. // matrix multiplication
  888. tmp[tid] += FLOAT_TYPE(v.x) * FLOAT_TYPE(data_b[p.b_offset + iybs + iqs + 0]) +
  889. FLOAT_TYPE(v.y) * FLOAT_TYPE(data_b[p.b_offset + iybs + iqs + y_offset]);
  890. }
  891. // sum up partial sums and write back result
  892. barrier();
  893. [[unroll]] for (uint s = BLOCK_SIZE/2; s > 0; s >>= 1) {
  894. if (tid < s) {
  895. tmp[tid] += tmp[tid + s];
  896. }
  897. barrier();
  898. }
  899. if (tid == 0) {
  900. dst[p.d_offset + row] = D_TYPE(tmp[0]);
  901. }
  902. }
  903. """
  904. # K-quants
  905. mul_mat_vec_q2_K_body = """
  906. layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
  907. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  908. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  909. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  910. shared FLOAT_TYPE tmp[32];
  911. void main() {
  912. const uint row = gl_WorkGroupID.x;
  913. const uint num_blocks_per_row = p.ncols / QUANT_K;
  914. const uint ib0 = row*num_blocks_per_row;
  915. const uint tid = gl_LocalInvocationID.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  916. const uint ix = gl_LocalInvocationID.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  917. const uint step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  918. const uint v_im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  919. const uint v_in = tid - step*v_im; // 0...15 or 0...7
  920. const uint l0 = K_QUANTS_PER_ITERATION*v_in; // 0...15
  921. const uint q_offset = 32*v_im + l0;
  922. const uint s_offset = 8*v_im;
  923. const uint y_offset = 128*v_im + l0;
  924. tmp[16 * ix + tid] = FLOAT_TYPE(0.0); // partial sum for thread in warp
  925. [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  926. const uint y_idx = i * QUANT_K + y_offset;
  927. const FLOAT_TYPE dall = FLOAT_TYPE(data_a[ib0 + i].d.x);
  928. const FLOAT_TYPE dmin = FLOAT_TYPE(data_a[ib0 + i].d.y);
  929. FLOAT_TYPE sum1 = FLOAT_TYPE(0.0);
  930. FLOAT_TYPE sum2 = FLOAT_TYPE(0.0);
  931. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  932. sum1 += FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 0]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 0] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l + 0] >> 0) & 3)
  933. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 16]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 1] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l +16] >> 0) & 3)
  934. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 32]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 2] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l + 0] >> 2) & 3)
  935. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 48]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 3] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l +16] >> 2) & 3)
  936. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 64]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 4] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l + 0] >> 4) & 3)
  937. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 80]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 5] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l +16] >> 4) & 3)
  938. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 96]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 6] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l + 0] >> 6) & 3)
  939. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l +112]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 7] & 0xF) * FLOAT_TYPE((data_a[ib0 + i].qs[q_offset + l +16] >> 6) & 3);
  940. sum2 += FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 0]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 0] >> 4) & 0xF)
  941. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 16]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 1] >> 4) & 0xF)
  942. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 32]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 2] >> 4) & 0xF)
  943. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 48]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 3] >> 4) & 0xF)
  944. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 64]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 4] >> 4) & 0xF)
  945. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 80]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 5] >> 4) & 0xF)
  946. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 96]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 6] >> 4) & 0xF)
  947. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l +112]) * FLOAT_TYPE((data_a[ib0 + i].scales[s_offset + 7] >> 4) & 0xF);
  948. }
  949. tmp[16 * ix + tid] += dall * sum1 - dmin * sum2;
  950. }
  951. // sum up partial sums and write back result
  952. barrier();
  953. [[unroll]] for (uint s = 16; s > 0; s >>= 1) {
  954. if (tid < s) {
  955. tmp[tid] += tmp[tid + s];
  956. }
  957. barrier();
  958. }
  959. if (tid == 0) {
  960. dst[p.d_offset + row] = D_TYPE(tmp[0]);
  961. }
  962. }
  963. """
  964. mul_mat_vec_q3_K_body = """
  965. layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
  966. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  967. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  968. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  969. shared FLOAT_TYPE tmp[32];
  970. void main() {
  971. const uint row = gl_WorkGroupID.x;
  972. const uint num_blocks_per_row = p.ncols / QUANT_K;
  973. const uint ib0 = row*num_blocks_per_row;
  974. const uint tid = gl_LocalInvocationID.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  975. const uint ix = gl_LocalInvocationID.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  976. const uint step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  977. const uint v_im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  978. const uint v_in = tid - step*v_im; // 0...15 or 0...7
  979. const uint8_t m = uint8_t(1 << (4 * v_im));
  980. const uint l0 = K_QUANTS_PER_ITERATION*v_in; // 0...15
  981. const uint q_offset = 32*v_im + l0;
  982. const uint y_offset = 128*v_im + l0;
  983. tmp[16 * ix + tid] = FLOAT_TYPE(0.0); // partial sum for thread in warp
  984. const uint s_shift = 4 * v_im;
  985. [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  986. const uint y_idx = i * QUANT_K + y_offset;
  987. const FLOAT_TYPE d = FLOAT_TYPE(data_a[ib0 + i].d);
  988. FLOAT_TYPE sum = FLOAT_TYPE(0.0);
  989. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  990. sum += FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 0]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[0] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[ 8] >> (s_shift + 0) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l ] ) & 3) - (((data_a[ib0 + i].hmask[l0 + l ] & (m << 0)) != 0) ? 0 : 4))
  991. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 32]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[2] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[10] >> (s_shift + 0) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l ] >> 2) & 3) - (((data_a[ib0 + i].hmask[l0 + l ] & (m << 1)) != 0) ? 0 : 4))
  992. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 64]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[4] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[ 8] >> (s_shift + 2) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l ] >> 4) & 3) - (((data_a[ib0 + i].hmask[l0 + l ] & (m << 2)) != 0) ? 0 : 4))
  993. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 96]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[6] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[10] >> (s_shift + 2) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l ] >> 6) & 3) - (((data_a[ib0 + i].hmask[l0 + l ] & (m << 3)) != 0) ? 0 : 4))
  994. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 16]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[1] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[ 9] >> (s_shift + 0) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l+16] ) & 3) - (((data_a[ib0 + i].hmask[l0 + l+16] & (m << 0)) != 0) ? 0 : 4))
  995. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 48]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[3] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[11] >> (s_shift + 0) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l+16] >> 2) & 3) - (((data_a[ib0 + i].hmask[l0 + l+16] & (m << 1)) != 0) ? 0 : 4))
  996. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l + 80]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[5] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[ 9] >> (s_shift + 2) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l+16] >> 4) & 3) - (((data_a[ib0 + i].hmask[l0 + l+16] & (m << 2)) != 0) ? 0 : 4))
  997. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l +112]) * FLOAT_TYPE(int8_t(((data_a[ib0 + i].scales[7] >> s_shift) & 0xF) | ((data_a[ib0 + i].scales[11] >> (s_shift + 2) & 0x3) << 4)) - 32) * FLOAT_TYPE(((data_a[ib0 + i].qs[q_offset + l+16] >> 6) & 3) - (((data_a[ib0 + i].hmask[l0 + l+16] & (m << 3)) != 0) ? 0 : 4));
  998. }
  999. tmp[16 * ix + tid] += d * sum;
  1000. }
  1001. // sum up partial sums and write back result
  1002. barrier();
  1003. [[unroll]] for (uint s = 16; s > 0; s >>= 1) {
  1004. if (tid < s) {
  1005. tmp[tid] += tmp[tid + s];
  1006. }
  1007. barrier();
  1008. }
  1009. if (tid == 0) {
  1010. dst[p.d_offset + row] = D_TYPE(tmp[0]);
  1011. }
  1012. }
  1013. """
  1014. mul_mat_vec_q4_K_body = """
  1015. layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
  1016. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1017. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  1018. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  1019. shared FLOAT_TYPE tmp[32];
  1020. void main() {
  1021. const uint row = gl_WorkGroupID.x;
  1022. const uint num_blocks_per_row = p.ncols / QUANT_K;
  1023. const uint ib0 = row*num_blocks_per_row;
  1024. const uint tid = gl_LocalInvocationID.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1025. const uint ix = gl_LocalInvocationID.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1026. const uint step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1027. const uint il = tid/step; // 0...3
  1028. const uint ir = tid - step*il; // 0...7 or 0...3
  1029. const uint n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1030. const uint v_im = il / 2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1031. const uint v_in = il % 2;
  1032. const uint l0 = n * (2 * ir + v_in); // 0...15
  1033. const uint q_offset = 32*v_im + l0;
  1034. const uint y_offset = 64*v_im + l0;
  1035. tmp[16 * ix + tid] = FLOAT_TYPE(0.0); // partial sum for thread in warp
  1036. [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1037. const uint y1_idx = i * QUANT_K + y_offset;
  1038. const uint y2_idx = y1_idx + 128;
  1039. const FLOAT_TYPE dall = FLOAT_TYPE(data_a[ib0 + i].d.x);
  1040. const FLOAT_TYPE dmin = FLOAT_TYPE(data_a[ib0 + i].d.y);
  1041. const uint8_t sc0 = uint8_t( data_a[ib0 + i].scales[v_im * 2 ] & 0x3f);
  1042. const uint8_t sc1 = uint8_t( data_a[ib0 + i].scales[v_im * 2 + 1] & 0x3f);
  1043. const uint8_t sc2 = uint8_t( data_a[ib0 + i].scales[v_im * 2 + 4] & 0x3f);
  1044. const uint8_t sc3 = uint8_t( data_a[ib0 + i].scales[v_im * 2 + 5] & 0x3f);
  1045. const uint8_t sc4 = uint8_t(( data_a[ib0 + i].scales[v_im * 2 + 8] & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 ] & 0xc0) >> 2));
  1046. const uint8_t sc5 = uint8_t(( data_a[ib0 + i].scales[v_im * 2 + 9] & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 + 1] & 0xc0) >> 2));
  1047. const uint8_t sc6 = uint8_t(((data_a[ib0 + i].scales[v_im * 2 + 8] >> 4) & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 + 4] & 0xc0) >> 2));
  1048. const uint8_t sc7 = uint8_t(((data_a[ib0 + i].scales[v_im * 2 + 9] >> 4) & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 + 5] & 0xc0) >> 2));
  1049. #if K_QUANTS_PER_ITERATION == 2
  1050. const uint8_t q4_0 = uint8_t(data_a[ib0 + i].qs[q_offset ] & 0xf);
  1051. const uint8_t q4_1 = uint8_t(data_a[ib0 + i].qs[q_offset + 1] & 0xf);
  1052. const uint8_t q4_2 = uint8_t(data_a[ib0 + i].qs[q_offset + 2] & 0xf);
  1053. const uint8_t q4_3 = uint8_t(data_a[ib0 + i].qs[q_offset + 3] & 0xf);
  1054. const uint8_t q4_4 = uint8_t(data_a[ib0 + i].qs[q_offset ] >> 4);
  1055. const uint8_t q4_5 = uint8_t(data_a[ib0 + i].qs[q_offset + 1] >> 4);
  1056. const uint8_t q4_6 = uint8_t(data_a[ib0 + i].qs[q_offset + 2] >> 4);
  1057. const uint8_t q4_7 = uint8_t(data_a[ib0 + i].qs[q_offset + 3] >> 4);
  1058. const uint8_t q4_8 = uint8_t(data_a[ib0 + i].qs[q_offset + 64] & 0xf);
  1059. const uint8_t q4_9 = uint8_t(data_a[ib0 + i].qs[q_offset + 65] & 0xf);
  1060. const uint8_t q4_10 = uint8_t(data_a[ib0 + i].qs[q_offset + 66] & 0xf);
  1061. const uint8_t q4_11 = uint8_t(data_a[ib0 + i].qs[q_offset + 67] & 0xf);
  1062. const uint8_t q4_12 = uint8_t(data_a[ib0 + i].qs[q_offset + 64] >> 4);
  1063. const uint8_t q4_13 = uint8_t(data_a[ib0 + i].qs[q_offset + 65] >> 4);
  1064. const uint8_t q4_14 = uint8_t(data_a[ib0 + i].qs[q_offset + 66] >> 4);
  1065. const uint8_t q4_15 = uint8_t(data_a[ib0 + i].qs[q_offset + 67] >> 4);
  1066. const FLOAT_TYPE sx = FLOAT_TYPE(data_b[p.b_offset + y1_idx] * q4_0 + data_b[p.b_offset + y1_idx + 1] * q4_1 + data_b[p.b_offset + y1_idx + 2] * q4_2 + data_b[p.b_offset + y1_idx + 3] * q4_3);
  1067. const FLOAT_TYPE sy = FLOAT_TYPE(data_b[p.b_offset + y1_idx + 32] * q4_4 + data_b[p.b_offset + y1_idx + 33] * q4_5 + data_b[p.b_offset + y1_idx + 34] * q4_6 + data_b[p.b_offset + y1_idx + 35] * q4_7);
  1068. const FLOAT_TYPE sz = FLOAT_TYPE(data_b[p.b_offset + y2_idx] * q4_8 + data_b[p.b_offset + y2_idx + 1] * q4_9 + data_b[p.b_offset + y2_idx + 2] * q4_10 + data_b[p.b_offset + y2_idx + 3] * q4_11);
  1069. const FLOAT_TYPE sw = FLOAT_TYPE(data_b[p.b_offset + y2_idx + 32] * q4_12 + data_b[p.b_offset + y2_idx + 33] * q4_13 + data_b[p.b_offset + y2_idx + 34] * q4_14 + data_b[p.b_offset + y2_idx + 35] * q4_15);
  1070. const FLOAT_TYPE smin = FLOAT_TYPE(
  1071. data_b[p.b_offset + y1_idx ] * sc2 + data_b[p.b_offset + y1_idx + 32] * sc3 + data_b[p.b_offset + y2_idx ] * sc6 + data_b[p.b_offset + y2_idx + 32] * sc7
  1072. + data_b[p.b_offset + y1_idx + 1] * sc2 + data_b[p.b_offset + y1_idx + 33] * sc3 + data_b[p.b_offset + y2_idx + 1] * sc6 + data_b[p.b_offset + y2_idx + 33] * sc7
  1073. + data_b[p.b_offset + y1_idx + 2] * sc2 + data_b[p.b_offset + y1_idx + 34] * sc3 + data_b[p.b_offset + y2_idx + 2] * sc6 + data_b[p.b_offset + y2_idx + 34] * sc7
  1074. + data_b[p.b_offset + y1_idx + 3] * sc2 + data_b[p.b_offset + y1_idx + 35] * sc3 + data_b[p.b_offset + y2_idx + 3] * sc6 + data_b[p.b_offset + y2_idx + 35] * sc7
  1075. );
  1076. tmp[16 * ix + tid] += FLOAT_TYPE(dall * (sx * sc0 + sy * sc1 + sz * sc4 + sw * sc5) - dmin * smin);
  1077. #else
  1078. const uint8_t q4_0 = uint8_t(data_a[ib0 + i].qs[q_offset ] & 0xf);
  1079. const uint8_t q4_1 = uint8_t(data_a[ib0 + i].qs[q_offset + 1] & 0xf);
  1080. const uint8_t q4_2 = uint8_t(data_a[ib0 + i].qs[q_offset ] >> 4);
  1081. const uint8_t q4_3 = uint8_t(data_a[ib0 + i].qs[q_offset + 1] >> 4);
  1082. const uint8_t q4_4 = uint8_t(data_a[ib0 + i].qs[q_offset + 64] & 0xf);
  1083. const uint8_t q4_5 = uint8_t(data_a[ib0 + i].qs[q_offset + 65] & 0xf);
  1084. const uint8_t q4_6 = uint8_t(data_a[ib0 + i].qs[q_offset + 64] >> 4);
  1085. const uint8_t q4_7 = uint8_t(data_a[ib0 + i].qs[q_offset + 65] >> 4);
  1086. const FLOAT_TYPE sx = FLOAT_TYPE(data_b[p.b_offset + y1_idx ] * q4_0 + data_b[p.b_offset + y1_idx + 1] * q4_1);
  1087. const FLOAT_TYPE sy = FLOAT_TYPE(data_b[p.b_offset + y1_idx + 32] * q4_2 + data_b[p.b_offset + y1_idx + 33] * q4_3);
  1088. const FLOAT_TYPE sz = FLOAT_TYPE(data_b[p.b_offset + y2_idx ] * q4_4 + data_b[p.b_offset + y2_idx + 1] * q4_5);
  1089. const FLOAT_TYPE sw = FLOAT_TYPE(data_b[p.b_offset + y2_idx + 32] * q4_6 + data_b[p.b_offset + y2_idx + 33] * q4_7);
  1090. const FLOAT_TYPE smin = FLOAT_TYPE(
  1091. data_b[p.b_offset + y1_idx] * sc2 + data_b[p.b_offset + y1_idx + 32] * sc3 + data_b[p.b_offset + y2_idx] * sc6 + data_b[p.b_offset + y2_idx + 32] * sc7
  1092. + data_b[p.b_offset + y1_idx + 1] * sc2 + data_b[p.b_offset + y1_idx + 33] * sc3 + data_b[p.b_offset + y2_idx + 1] * sc6 + data_b[p.b_offset + y2_idx + 33] * sc7
  1093. );
  1094. tmp[16 * ix + tid] += FLOAT_TYPE(dall * (sx * FLOAT_TYPE(data_a[ib0 + i].scales[v_im] & 0x3f) + sy * FLOAT_TYPE(data_a[ib0 + i].scales[v_im + 1] & 0x3f) + sz * FLOAT_TYPE((data_a[ib0 + i].scales[v_im + 4] & 0x0f) | ((data_a[ib0 + i].scales[v_im] & 0xc0) >> 2)) + sw * FLOAT_TYPE((data_a[ib0 + i].scales[v_im + 5] & 0x0f) | ((data_a[ib0 + i].scales[v_im + 1] & 0xc0) >> 2))) - dmin * smin);
  1095. #endif
  1096. }
  1097. // sum up partial sums and write back result
  1098. barrier();
  1099. [[unroll]] for (uint s = 16; s > 0; s >>= 1) {
  1100. if (tid < s) {
  1101. tmp[tid] += tmp[tid + s];
  1102. }
  1103. barrier();
  1104. }
  1105. if (tid == 0) {
  1106. dst[p.d_offset + row] = D_TYPE(tmp[0]);
  1107. }
  1108. }
  1109. """
  1110. mul_mat_vec_q5_K_body = """
  1111. layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
  1112. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1113. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  1114. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  1115. shared FLOAT_TYPE tmp[32];
  1116. void main() {
  1117. const uint row = gl_WorkGroupID.x;
  1118. const uint num_blocks_per_row = p.ncols / QUANT_K;
  1119. const uint ib0 = row*num_blocks_per_row;
  1120. const uint tid = gl_LocalInvocationID.x/2; // 0...31 or 0...16
  1121. const uint ix = gl_LocalInvocationID.x%2; // 0 or 0, 1
  1122. const uint il = tid/4; // 0...3
  1123. const uint ir = tid - 4*il; // 0...7 or 0...3
  1124. const uint v_im = il / 2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1125. const uint v_in = il % 2;
  1126. const uint l0 = 4*ir + 2*v_in; // 0...15
  1127. const uint q_offset = 32*v_im + l0;
  1128. const uint y_offset = 64*v_im + l0;
  1129. const uint8_t hm1 = uint8_t(1 << (2*v_im));
  1130. const uint8_t hm2 = uint8_t(hm1 << 4);
  1131. tmp[16 * ix + tid] = FLOAT_TYPE(0.0); // partial sum for thread in warp
  1132. [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += 2) {
  1133. const uint y1_idx = i * QUANT_K + y_offset;
  1134. const uint y2_idx = y1_idx + 128;
  1135. const FLOAT_TYPE dall = FLOAT_TYPE(data_a[ib0 + i].d.x);
  1136. const FLOAT_TYPE dmin = FLOAT_TYPE(data_a[ib0 + i].d.y);
  1137. const uint8_t sc0 = uint8_t( data_a[ib0 + i].scales[v_im * 2 ] & 0x3f);
  1138. const uint8_t sc1 = uint8_t( data_a[ib0 + i].scales[v_im * 2 + 1] & 0x3f);
  1139. const uint8_t sc2 = uint8_t( data_a[ib0 + i].scales[v_im * 2 + 4] & 0x3f);
  1140. const uint8_t sc3 = uint8_t( data_a[ib0 + i].scales[v_im * 2 + 5] & 0x3f);
  1141. const uint8_t sc4 = uint8_t(( data_a[ib0 + i].scales[v_im * 2 + 8] & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 ] & 0xc0) >> 2));
  1142. const uint8_t sc5 = uint8_t(( data_a[ib0 + i].scales[v_im * 2 + 9] & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 + 1] & 0xc0) >> 2));
  1143. const uint8_t sc6 = uint8_t(((data_a[ib0 + i].scales[v_im * 2 + 8] >> 4) & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 + 4] & 0xc0) >> 2));
  1144. const uint8_t sc7 = uint8_t(((data_a[ib0 + i].scales[v_im * 2 + 9] >> 4) & 0x0f) | ((data_a[ib0 + i].scales[v_im * 2 + 5] & 0xc0) >> 2));
  1145. const uint8_t q4_0 = uint8_t(data_a[ib0 + i].qs[q_offset ] & 0xf);
  1146. const uint8_t q4_1 = uint8_t(data_a[ib0 + i].qs[q_offset + 1] & 0xf);
  1147. const uint8_t q4_2 = uint8_t(data_a[ib0 + i].qs[q_offset + 16] & 0xf);
  1148. const uint8_t q4_3 = uint8_t(data_a[ib0 + i].qs[q_offset + 17] & 0xf);
  1149. const uint8_t q4_4 = uint8_t(data_a[ib0 + i].qs[q_offset ] >> 4);
  1150. const uint8_t q4_5 = uint8_t(data_a[ib0 + i].qs[q_offset + 1] >> 4);
  1151. const uint8_t q4_6 = uint8_t(data_a[ib0 + i].qs[q_offset + 16] >> 4);
  1152. const uint8_t q4_7 = uint8_t(data_a[ib0 + i].qs[q_offset + 17] >> 4);
  1153. const uint8_t q4_8 = uint8_t(data_a[ib0 + i].qs[q_offset + 64] & 0xf);
  1154. const uint8_t q4_9 = uint8_t(data_a[ib0 + i].qs[q_offset + 65] & 0xf);
  1155. const uint8_t q4_10 = uint8_t(data_a[ib0 + i].qs[q_offset + 80] & 0xf);
  1156. const uint8_t q4_11 = uint8_t(data_a[ib0 + i].qs[q_offset + 81] & 0xf);
  1157. const uint8_t q4_12 = uint8_t(data_a[ib0 + i].qs[q_offset + 64] >> 4);
  1158. const uint8_t q4_13 = uint8_t(data_a[ib0 + i].qs[q_offset + 65] >> 4);
  1159. const uint8_t q4_14 = uint8_t(data_a[ib0 + i].qs[q_offset + 80] >> 4);
  1160. const uint8_t q4_15 = uint8_t(data_a[ib0 + i].qs[q_offset + 81] >> 4);
  1161. const FLOAT_TYPE sx = FLOAT_TYPE(
  1162. data_b[p.b_offset + y1_idx ] * (q4_0 + (((data_a[ib0 + i].qh[l0 ] & hm1) != 0) ? 16 : 0))
  1163. + data_b[p.b_offset + y1_idx + 1] * (q4_1 + (((data_a[ib0 + i].qh[l0 + 1] & hm1) != 0) ? 16 : 0))
  1164. + data_b[p.b_offset + y1_idx + 16] * (q4_2 + (((data_a[ib0 + i].qh[l0 + 16] & hm1) != 0) ? 16 : 0))
  1165. + data_b[p.b_offset + y1_idx + 17] * (q4_3 + (((data_a[ib0 + i].qh[l0 + 17] & hm1) != 0) ? 16 : 0))
  1166. );
  1167. const FLOAT_TYPE sy = FLOAT_TYPE(
  1168. data_b[p.b_offset + y1_idx + 32] * (q4_4 + (((data_a[ib0 + i].qh[l0 ] & (hm1 << 1)) != 0) ? 16 : 0))
  1169. + data_b[p.b_offset + y1_idx + 33] * (q4_5 + (((data_a[ib0 + i].qh[l0 + 1] & (hm1 << 1)) != 0) ? 16 : 0))
  1170. + data_b[p.b_offset + y1_idx + 48] * (q4_6 + (((data_a[ib0 + i].qh[l0 + 16] & (hm1 << 1)) != 0) ? 16 : 0))
  1171. + data_b[p.b_offset + y1_idx + 49] * (q4_7 + (((data_a[ib0 + i].qh[l0 + 17] & (hm1 << 1)) != 0) ? 16 : 0))
  1172. );
  1173. const FLOAT_TYPE sz = FLOAT_TYPE(
  1174. data_b[p.b_offset + y2_idx ] * (q4_8 + (((data_a[ib0 + i].qh[l0 ] & hm2) != 0) ? 16 : 0))
  1175. + data_b[p.b_offset + y2_idx + 1] * (q4_9 + (((data_a[ib0 + i].qh[l0 + 1] & hm2) != 0) ? 16 : 0))
  1176. + data_b[p.b_offset + y2_idx + 16] * (q4_10 + (((data_a[ib0 + i].qh[l0 + 16] & hm2) != 0) ? 16 : 0))
  1177. + data_b[p.b_offset + y2_idx + 17] * (q4_11 + (((data_a[ib0 + i].qh[l0 + 17] & hm2) != 0) ? 16 : 0))
  1178. );
  1179. const FLOAT_TYPE sw = FLOAT_TYPE(
  1180. data_b[p.b_offset + y2_idx + 32] * (q4_12 + (((data_a[ib0 + i].qh[l0 ] & (hm2 << 1)) != 0) ? 16 : 0))
  1181. + data_b[p.b_offset + y2_idx + 33] * (q4_13 + (((data_a[ib0 + i].qh[l0 + 1] & (hm2 << 1)) != 0) ? 16 : 0))
  1182. + data_b[p.b_offset + y2_idx + 48] * (q4_14 + (((data_a[ib0 + i].qh[l0 + 16] & (hm2 << 1)) != 0) ? 16 : 0))
  1183. + data_b[p.b_offset + y2_idx + 49] * (q4_15 + (((data_a[ib0 + i].qh[l0 + 17] & (hm2 << 1)) != 0) ? 16 : 0))
  1184. );
  1185. const FLOAT_TYPE smin = FLOAT_TYPE(
  1186. (data_b[p.b_offset + y1_idx] + data_b[p.b_offset + y1_idx + 1] + data_b[p.b_offset + y1_idx + 16] + data_b[p.b_offset + y1_idx + 17]) * sc2 + (data_b[p.b_offset + y1_idx + 32] + data_b[p.b_offset + y1_idx + 33] + data_b[p.b_offset + y1_idx + 48] + data_b[p.b_offset + y1_idx + 49]) * sc3
  1187. + (data_b[p.b_offset + y2_idx] + data_b[p.b_offset + y2_idx + 1] + data_b[p.b_offset + y2_idx + 16] + data_b[p.b_offset + y2_idx + 17]) * sc6 + (data_b[p.b_offset + y2_idx + 32] + data_b[p.b_offset + y2_idx + 33] + data_b[p.b_offset + y2_idx + 48] + data_b[p.b_offset + y2_idx + 49]) * sc7
  1188. );
  1189. tmp[16 * ix + tid] += FLOAT_TYPE(dall * (sx * sc0 + sy * sc1 + sz * sc4 + sw * sc5) - dmin * smin);
  1190. }
  1191. // sum up partial sums and write back result
  1192. barrier();
  1193. [[unroll]] for (uint s = 16; s > 0; s >>= 1) {
  1194. if (tid < s) {
  1195. tmp[tid] += tmp[tid + s];
  1196. }
  1197. barrier();
  1198. }
  1199. if (tid == 0) {
  1200. dst[p.d_offset + row] = D_TYPE(tmp[0]);
  1201. }
  1202. }
  1203. """
  1204. mul_mat_vec_q6_K_body = """
  1205. layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
  1206. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1207. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  1208. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  1209. shared FLOAT_TYPE tmp[32];
  1210. void main() {
  1211. const uint block_size = gl_WorkGroupSize.x;
  1212. const uint row = gl_WorkGroupID.x;
  1213. const uint num_blocks_per_row = p.ncols / QUANT_K;
  1214. const uint ib0 = row*num_blocks_per_row;
  1215. const uint tid = gl_LocalInvocationID.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1216. const uint ix = gl_LocalInvocationID.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1217. const uint step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1218. const uint v_im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1219. const uint v_in = tid - step*v_im; // 0...15 or 0...7
  1220. #if K_QUANTS_PER_ITERATION == 1
  1221. const uint l0 = v_in; // 0...15
  1222. const uint is = 0;
  1223. #else
  1224. const uint l0 = 4 * v_in; // 0, 4, 8, ..., 28
  1225. const uint is = v_in / 4;
  1226. #endif
  1227. const uint ql_offset = 64*v_im + l0;
  1228. const uint qh_offset = 32*v_im + l0;
  1229. const uint s_offset = 8*v_im + is;
  1230. const uint y_offset = 128*v_im + l0;
  1231. tmp[16 * ix + tid] = FLOAT_TYPE(0.0); // partial sum for thread in warp
  1232. [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1233. const uint y_idx = i * QUANT_K + y_offset;
  1234. const FLOAT_TYPE d = FLOAT_TYPE(data_a[ib0 + i].d);
  1235. #if K_QUANTS_PER_ITERATION == 1
  1236. FLOAT_TYPE sum = FLOAT_TYPE(data_b[p.b_offset + y_idx + 0]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 0]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 0] & 0xF) | ((data_a[ib0 + i].qh[qh_offset + 0] & 0x03) << 4)) - 32)
  1237. + FLOAT_TYPE(data_b[p.b_offset + y_idx + 16]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 1]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 16] & 0xF) | ((data_a[ib0 + i].qh[qh_offset + 16] & 0x03) << 4)) - 32)
  1238. + FLOAT_TYPE(data_b[p.b_offset + y_idx + 32]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 2]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 32] & 0xF) | ((data_a[ib0 + i].qh[qh_offset + 0] & 0x0c) << 2)) - 32)
  1239. + FLOAT_TYPE(data_b[p.b_offset + y_idx + 48]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 3]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 48] & 0xF) | ((data_a[ib0 + i].qh[qh_offset + 16] & 0x0c) << 2)) - 32)
  1240. + FLOAT_TYPE(data_b[p.b_offset + y_idx + 64]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 4]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 0] >> 4) | ((data_a[ib0 + i].qh[qh_offset + 0] & 0x30) >> 0)) - 32)
  1241. + FLOAT_TYPE(data_b[p.b_offset + y_idx + 80]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 5]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 16] >> 4) | ((data_a[ib0 + i].qh[qh_offset + 16] & 0x30) >> 0)) - 32)
  1242. + FLOAT_TYPE(data_b[p.b_offset + y_idx + 96]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 6]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 32] >> 4) | ((data_a[ib0 + i].qh[qh_offset + 0] & 0xc0) >> 2)) - 32)
  1243. + FLOAT_TYPE(data_b[p.b_offset + y_idx +112]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 7]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + 48] >> 4) | ((data_a[ib0 + i].qh[qh_offset + 16] & 0xc0) >> 2)) - 32);
  1244. tmp[16 * ix + tid] += sum;
  1245. #else
  1246. FLOAT_TYPE sum = FLOAT_TYPE(0.0);
  1247. [[unroll]] for (int l = 0; l < 4; ++l) {
  1248. sum += FLOAT_TYPE(data_b[p.b_offset + y_idx + l+ 0]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 0]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + l+ 0] & 0xF) | (((data_a[ib0 + i].qh[qh_offset + l] >> 0) & 3) << 4)) - 32)
  1249. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l+32]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 2]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + l+32] & 0xF) | (((data_a[ib0 + i].qh[qh_offset + l] >> 2) & 3) << 4)) - 32)
  1250. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l+64]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 4]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + l+ 0] >> 4) | (((data_a[ib0 + i].qh[qh_offset + l] >> 4) & 3) << 4)) - 32)
  1251. + FLOAT_TYPE(data_b[p.b_offset + y_idx + l+96]) * FLOAT_TYPE(data_a[ib0 + i].scales[s_offset + 6]) * d * FLOAT_TYPE(int8_t((data_a[ib0 + i].ql[ql_offset + l+32] >> 4) | (((data_a[ib0 + i].qh[qh_offset + l] >> 6) & 3) << 4)) - 32);
  1252. }
  1253. tmp[16 * ix + tid] += sum;
  1254. #endif
  1255. }
  1256. // sum up partial sums and write back result
  1257. barrier();
  1258. [[unroll]] for (uint s = 16; s > 0; s >>= 1) {
  1259. if (tid < s) {
  1260. tmp[tid] += tmp[tid + s];
  1261. }
  1262. barrier();
  1263. }
  1264. if (tid == 0) {
  1265. dst[p.d_offset + row] = D_TYPE(tmp[0]);
  1266. }
  1267. }
  1268. """
  1269. mul_mat_p021_src = """#version 450
  1270. #extension GL_EXT_control_flow_attributes : enable
  1271. #extension GL_EXT_shader_16bit_storage : require
  1272. #define BLOCK_SIZE 32
  1273. #define FLOAT_TYPE float
  1274. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  1275. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1276. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  1277. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  1278. layout (push_constant) uniform parameter
  1279. {
  1280. uint ncols_x;
  1281. uint nrows_x;
  1282. uint nchannels_x;
  1283. uint nchannels_y;
  1284. uint b_offset;
  1285. uint d_offset;
  1286. } p;
  1287. shared FLOAT_TYPE tmp[BLOCK_SIZE];
  1288. void main() {
  1289. const uint tid = gl_LocalInvocationID.x;
  1290. const uint row_x = gl_GlobalInvocationID.y;
  1291. const uint channel = gl_GlobalInvocationID.z;
  1292. const uint channel_x = channel / (p.nchannels_y / p.nchannels_x);
  1293. const uint nrows_y = p.ncols_x;
  1294. const uint nrows_dst = p.nrows_x;
  1295. const uint row_dst = row_x;
  1296. tmp[tid] = FLOAT_TYPE(0.0f);
  1297. for (uint col_x0 = 0; col_x0 < p.ncols_x; col_x0 += BLOCK_SIZE) {
  1298. const uint col_x = col_x0 + tid;
  1299. if (col_x >= p.ncols_x) {
  1300. break;
  1301. }
  1302. // x is transposed and permuted
  1303. const uint ix = row_x*p.nchannels_x*p.ncols_x + channel_x*p.ncols_x + col_x;
  1304. const FLOAT_TYPE xi = FLOAT_TYPE(data_a[ix]);
  1305. const uint row_y = col_x;
  1306. // y is not transposed but permuted
  1307. const uint iy = channel*nrows_y + row_y;
  1308. tmp[tid] += xi * FLOAT_TYPE(data_b[iy]);
  1309. }
  1310. // dst is not transposed and not permuted
  1311. const uint idst = channel*nrows_dst + row_dst;
  1312. // sum up partial sums and write back result
  1313. barrier();
  1314. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  1315. if (tid < s) {
  1316. tmp[tid] += tmp[tid + s];
  1317. }
  1318. barrier();
  1319. }
  1320. if (tid == 0) {
  1321. dst[idst] = tmp[0];
  1322. }
  1323. }
  1324. """
  1325. mul_mat_nc_src = """#version 450
  1326. #extension GL_EXT_control_flow_attributes : enable
  1327. #extension GL_EXT_shader_16bit_storage : require
  1328. #define BLOCK_SIZE 32
  1329. #define FLOAT_TYPE float
  1330. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  1331. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1332. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  1333. layout (binding = 2) writeonly buffer D {D_TYPE dst[];};
  1334. layout (push_constant) uniform parameter
  1335. {
  1336. uint ncols_x;
  1337. uint nrows_x;
  1338. uint row_stride_x;
  1339. uint channel_stride_x;
  1340. uint channel_x_divisor;
  1341. uint b_offset;
  1342. uint d_offset;
  1343. } p;
  1344. shared FLOAT_TYPE tmp[BLOCK_SIZE];
  1345. void main() {
  1346. const uint tid = gl_LocalInvocationID.x;
  1347. const uint row_x = gl_GlobalInvocationID.y;
  1348. const uint channel = gl_GlobalInvocationID.z;
  1349. const uint channel_x = channel / p.channel_x_divisor;
  1350. const uint nrows_y = p.ncols_x;
  1351. const uint nrows_dst = p.nrows_x;
  1352. const uint row_dst = row_x;
  1353. const uint idst = channel*nrows_dst + row_dst;
  1354. tmp[tid] = 0.0f;
  1355. for (uint col_x0 = 0; col_x0 < p.ncols_x; col_x0 += BLOCK_SIZE) {
  1356. const uint col_x = col_x0 + tid;
  1357. if (col_x >= p.ncols_x) {
  1358. break;
  1359. }
  1360. const uint row_y = col_x;
  1361. const uint ix = channel_x*p.channel_stride_x + row_x*p.row_stride_x + col_x;
  1362. const uint iy = channel*nrows_y + row_y;
  1363. const FLOAT_TYPE xi = FLOAT_TYPE(data_a[ix]);
  1364. tmp[tid] += xi * FLOAT_TYPE(data_b[iy]);
  1365. }
  1366. // sum up partial sums and write back result
  1367. barrier();
  1368. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  1369. if (tid < s) {
  1370. tmp[tid] += tmp[tid + s];
  1371. }
  1372. barrier();
  1373. }
  1374. if (tid == 0) {
  1375. dst[idst] = tmp[0];
  1376. }
  1377. }
  1378. """
  1379. generic_head = """
  1380. #version 450
  1381. #extension GL_EXT_shader_16bit_storage : require
  1382. layout (push_constant) uniform parameter
  1383. {
  1384. uint KX;
  1385. uint KY;
  1386. float param1;
  1387. float param2;
  1388. } p;
  1389. """
  1390. generic_unary_op_head = """#version 450
  1391. #extension GL_EXT_shader_16bit_storage : require
  1392. layout (push_constant) uniform parameter
  1393. {
  1394. uint ne;
  1395. uint ne00; uint ne01; uint ne02; uint ne03; uint nb00; uint nb01; uint nb02; uint nb03;
  1396. uint ne10; uint ne11; uint ne12; uint ne13; uint nb10; uint nb11; uint nb12; uint nb13;
  1397. uint d_offset;
  1398. float param1; float param2;
  1399. } p;"""
  1400. generic_unary_op_funcs = """
  1401. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  1402. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1403. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1404. uint src0_idx(uint idx) {
  1405. const uint i03 = idx / (p.ne02*p.ne01*p.ne00);
  1406. const uint i03_offset = i03 * p.ne02*p.ne01*p.ne00;
  1407. const uint i02 = (idx - i03_offset) / (p.ne01*p.ne00);
  1408. const uint i02_offset = i02*p.ne01*p.ne00;
  1409. const uint i01 = (idx - i03_offset - i02_offset) / p.ne00;
  1410. const uint i00 = idx - i03_offset - i02_offset - i01*p.ne00;
  1411. return i03*p.nb03 + i02*p.nb02 + i01*p.nb01 + i00*p.nb00;
  1412. }
  1413. uint dst_idx(uint idx) {
  1414. const uint i13 = idx / (p.ne12*p.ne11*p.ne10);
  1415. const uint i13_offset = i13 * p.ne12*p.ne11*p.ne10;
  1416. const uint i12 = (idx - i13_offset) / (p.ne11*p.ne10);
  1417. const uint i12_offset = i12*p.ne11*p.ne10;
  1418. const uint i11 = (idx - i13_offset - i12_offset) / p.ne10;
  1419. const uint i10 = idx - i13_offset - i12_offset - i11*p.ne10;
  1420. return i13*p.nb13 + i12*p.nb12 + i11*p.nb11 + i10*p.nb10;
  1421. }"""
  1422. generic_unary_op_main = """
  1423. void main() {
  1424. if (gl_GlobalInvocationID.x >= p.ne) {
  1425. return;
  1426. }
  1427. """
  1428. generic_unary_op_combined = f"{generic_unary_op_head}\n{generic_unary_op_funcs}\n{generic_unary_op_main}"
  1429. generic_binary_op_head = """#version 450
  1430. #extension GL_EXT_shader_16bit_storage : require
  1431. layout (push_constant) uniform parameter
  1432. {
  1433. uint ne;
  1434. uint ne00; uint ne01; uint ne02; uint ne03; uint nb00; uint nb01; uint nb02; uint nb03;
  1435. uint ne10; uint ne11; uint ne12; uint ne13; uint nb10; uint nb11; uint nb12; uint nb13;
  1436. uint ne20; uint ne21; uint ne22; uint ne23; uint nb20; uint nb21; uint nb22; uint nb23;
  1437. uint d_offset;
  1438. float param1; float param2;
  1439. } p;"""
  1440. generic_binary_op_funcs = """
  1441. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  1442. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1443. layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
  1444. layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
  1445. uint src0_idx(uint idx) {
  1446. const uint i03 = idx / (p.ne02*p.ne01*p.ne00);
  1447. const uint i03_offset = i03 * p.ne02*p.ne01*p.ne00;
  1448. const uint i02 = (idx - i03_offset) / (p.ne01*p.ne00);
  1449. const uint i02_offset = i02*p.ne01*p.ne00;
  1450. const uint i01 = (idx - i03_offset - i02_offset) / p.ne00;
  1451. const uint i00 = idx - i03_offset - i02_offset - i01*p.ne00;
  1452. return i03*p.nb03 + i02*p.nb02 + i01*p.nb01 + i00*p.nb00;
  1453. }
  1454. uint src1_idx(uint idx) {
  1455. const uint i03 = idx / (p.ne02*p.ne01*p.ne00);
  1456. const uint i03_offset = i03 * p.ne02*p.ne01*p.ne00;
  1457. const uint i02 = (idx - i03_offset) / (p.ne01*p.ne00);
  1458. const uint i02_offset = i02*p.ne01*p.ne00;
  1459. const uint i01 = (idx - i03_offset - i02_offset) / p.ne00;
  1460. const uint i00 = idx - i03_offset - i02_offset - i01*p.ne00;
  1461. return (i03 % p.ne13)*p.nb13 + (i02 % p.ne12)*p.nb12 + (i01 % p.ne11)*p.nb11 + (i00 % p.ne10)*p.nb10;
  1462. }
  1463. uint dst_idx(uint idx) {
  1464. const uint i23 = idx / (p.ne22*p.ne21*p.ne20);
  1465. const uint i23_offset = i23 * p.ne22*p.ne21*p.ne20;
  1466. const uint i22 = (idx - i23_offset) / (p.ne21*p.ne20);
  1467. const uint i22_offset = i22*p.ne21*p.ne20;
  1468. const uint i21 = (idx - i23_offset - i22_offset) / p.ne20;
  1469. const uint i20 = idx - i23_offset - i22_offset - i21*p.ne20;
  1470. return i23*p.nb23 + i22*p.nb22 + i21*p.nb21 + i20*p.nb20;
  1471. }"""
  1472. generic_binary_op_main = """
  1473. void main() {
  1474. if (gl_GlobalInvocationID.x >= p.ne) {
  1475. return;
  1476. }
  1477. """
  1478. generic_binary_op_combined = f"{generic_binary_op_head}\n{generic_binary_op_funcs}\n{generic_binary_op_main}"
  1479. # MUL F32
  1480. mul_body = """
  1481. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = D_TYPE(FLOAT_TYPE(data_a[src0_idx(gl_GlobalInvocationID.x)]) * FLOAT_TYPE(data_b[src1_idx(gl_GlobalInvocationID.x)]));
  1482. }
  1483. """
  1484. # ADD
  1485. add_body = """
  1486. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = D_TYPE(FLOAT_TYPE(data_a[src0_idx(gl_GlobalInvocationID.x)]) + FLOAT_TYPE(data_b[src1_idx(gl_GlobalInvocationID.x)]));
  1487. }
  1488. """
  1489. # SCALE
  1490. scale_body = """
  1491. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = D_TYPE(FLOAT_TYPE(data_a[src0_idx(gl_GlobalInvocationID.x)]) + FLOAT_TYPE(p.param1));
  1492. }
  1493. """
  1494. # SQR
  1495. sqr_body = """
  1496. const FLOAT_TYPE val = FLOAT_TYPE(data_a[src0_idx(gl_GlobalInvocationID.x)]);
  1497. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = D_TYPE(val * val);
  1498. }
  1499. """
  1500. # CLAMP
  1501. clamp_body = """
  1502. const FLOAT_TYPE val = FLOAT_TYPE(data_a[src0_idx(gl_GlobalInvocationID.x)]);
  1503. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = D_TYPE(val < p.param1 ? p.param1 : (val > p.param2 ? p.param2 : val));
  1504. }
  1505. """
  1506. # CPY
  1507. cpy_end = """
  1508. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = D_TYPE(data_a[src0_idx(gl_GlobalInvocationID.x)]);
  1509. }
  1510. """
  1511. # Causes an optimization error otherwise
  1512. cpy_f16_f16_end = """
  1513. data_d[p.d_offset + dst_idx(gl_GlobalInvocationID.x)] = data_a[src0_idx(gl_GlobalInvocationID.x)];
  1514. }
  1515. """
  1516. # GET_ROWS
  1517. get_rows_float_body = """
  1518. void main() {
  1519. const uint i00 = gl_GlobalInvocationID.x;
  1520. const uint i10 = gl_GlobalInvocationID.y;
  1521. const uint i11 = (gl_GlobalInvocationID.z)/p.ne12;
  1522. const uint i12 = (gl_GlobalInvocationID.z)%p.ne12;
  1523. if (i00 >= p.ne00) {
  1524. return;
  1525. }
  1526. const uint i01 = data_b[i10*p.nb10 + i11*p.nb11 + i12*p.nb12];
  1527. const uint a_offset = i01*p.nb01 + i11*p.nb02 + i12*p.nb03;
  1528. const uint d_offset = i10*p.nb21 + i11*p.nb22 + i12*p.nb23;
  1529. #ifndef OPTIMIZATION_ERROR_WORKAROUND
  1530. data_d[d_offset + i00] = D_TYPE(data_a[a_offset + i00]);
  1531. #else
  1532. data_d[d_offset + i00] = data_a[a_offset + i00];
  1533. #endif
  1534. }
  1535. """
  1536. get_rows_body = """
  1537. void main() {
  1538. const uint i00 = (gl_GlobalInvocationID.x)*2;
  1539. const uint i10 = gl_GlobalInvocationID.y;
  1540. const uint i11 = (gl_GlobalInvocationID.z)/p.ne12;
  1541. const uint i12 = (gl_GlobalInvocationID.z)%p.ne12;
  1542. if (i00 >= p.ne00) {
  1543. return;
  1544. }
  1545. const uint i01 = data_b[i10*p.nb10 + i11*p.nb11 + i12*p.nb12];
  1546. const uint a_offset = i01*p.nb01 + i11*p.nb02 + i12*p.nb03;
  1547. const uint d_offset = i10*p.nb21 + i11*p.nb22 + i12*p.nb23;
  1548. const uint ib = a_offset + i00/QUANT_K; // block index
  1549. const uint iqs = (i00%QUANT_K)/QUANT_R; // quant index
  1550. const uint iybs = i00 - i00%QUANT_K; // dst block start index
  1551. const uint y_offset = QUANT_R == 1 ? 1 : QUANT_K/2;
  1552. DEQUANT_FUNC
  1553. data_d[d_offset + iybs + iqs ] = D_TYPE(v.x);
  1554. data_d[d_offset + iybs + iqs + y_offset] = D_TYPE(v.y);
  1555. }
  1556. """
  1557. # UNARY
  1558. gelu_body = """
  1559. #extension GL_EXT_control_flow_attributes : enable
  1560. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  1561. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1562. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1563. void main() {
  1564. const float GELU_COEF_A = 0.044715f;
  1565. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1566. const uint i = gl_GlobalInvocationID.x;
  1567. if (i >= p.KX) {
  1568. return;
  1569. }
  1570. const float xi = float(data_a[i]);
  1571. const float val = SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi);
  1572. data_d[i] = D_TYPE(0.5f*xi*(2.0f - 2.0f / (exp(2 * val) + 1)));
  1573. }
  1574. """
  1575. silu_body = """
  1576. #extension GL_EXT_control_flow_attributes : enable
  1577. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  1578. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1579. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1580. void main() {
  1581. const uint i = gl_GlobalInvocationID.x;
  1582. if (i >= p.KX) {
  1583. return;
  1584. }
  1585. const float xi = float(data_a[i]);
  1586. data_d[i] = D_TYPE(xi / (1.0f + exp(-xi)));
  1587. }
  1588. """
  1589. relu_body = """
  1590. #extension GL_EXT_control_flow_attributes : enable
  1591. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  1592. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1593. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1594. void main() {
  1595. const uint i = gl_GlobalInvocationID.x;
  1596. if (i >= p.KX) {
  1597. return;
  1598. }
  1599. data_d[i] = max(float(data_a[i]), 0);
  1600. }
  1601. """
  1602. # DIAG_MASK_INF
  1603. diag_mask_inf_head = """#version 450
  1604. #extension GL_EXT_shader_16bit_storage : require
  1605. layout (push_constant) uniform parameter
  1606. {
  1607. uint ncols;
  1608. uint rows_per_channel;
  1609. uint n_past;
  1610. } p;
  1611. """
  1612. diag_mask_inf_body = """
  1613. #extension GL_EXT_control_flow_attributes : enable
  1614. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  1615. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1616. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1617. void main() {
  1618. const uint col = gl_GlobalInvocationID.y;
  1619. const uint row = gl_GlobalInvocationID.x;
  1620. if (col >= p.ncols) {
  1621. return;
  1622. }
  1623. const uint i = row*p.ncols + col;
  1624. data_d[i] = D_TYPE(data_a[i] - float(uint(col > p.n_past + row % p.rows_per_channel) * 0xFFFFFFFF));
  1625. }
  1626. """
  1627. # NORMS
  1628. norm_body = """
  1629. #extension GL_EXT_control_flow_attributes : enable
  1630. #define BLOCK_SIZE 512
  1631. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  1632. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1633. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1634. shared vec2 sum[BLOCK_SIZE];
  1635. void main() {
  1636. const uint row = gl_WorkGroupID.x;
  1637. const uint tid = gl_LocalInvocationID.x;
  1638. const float eps = 1e-5f;
  1639. sum[tid] = vec2(0.0f, 0.0f);
  1640. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1641. const float xi = float(data_a[row*p.KX + col]);
  1642. sum[tid].x += xi;
  1643. sum[tid].y += xi * xi;
  1644. }
  1645. // sum up partial sums and write back result
  1646. barrier();
  1647. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  1648. if (tid < s) {
  1649. sum[tid] += sum[tid + s];
  1650. }
  1651. barrier();
  1652. }
  1653. const float mean = sum[0].x / p.KX;
  1654. const float var = sum[0].y / p.KX - mean * mean;
  1655. const float inv_std = inversesqrt(var + 1e-5f);
  1656. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1657. data_d[row*p.KX + col] = D_TYPE((float(data_a[row*p.KX + col]) - mean) * inv_std);
  1658. }
  1659. }
  1660. """
  1661. rms_norm_body = """
  1662. #extension GL_EXT_control_flow_attributes : enable
  1663. #define BLOCK_SIZE 512
  1664. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  1665. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1666. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  1667. shared FLOAT_TYPE sum[BLOCK_SIZE];
  1668. void main() {
  1669. const uint row = gl_WorkGroupID.x;
  1670. const uint tid = gl_LocalInvocationID.x;
  1671. sum[tid] = FLOAT_TYPE(0.0f); // partial sum for thread in warp
  1672. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1673. const FLOAT_TYPE xi = FLOAT_TYPE(data_a[row*p.KX + col]);
  1674. sum[tid] += xi * xi;
  1675. }
  1676. // sum up partial sums and write back result
  1677. barrier();
  1678. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  1679. if (tid < s) {
  1680. sum[tid] += sum[tid + s];
  1681. }
  1682. barrier();
  1683. }
  1684. const FLOAT_TYPE mean = sum[0] / FLOAT_TYPE(p.KX);
  1685. const FLOAT_TYPE scale = inversesqrt(mean + FLOAT_TYPE(p.param1));
  1686. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1687. data_d[row*p.KX + col] = D_TYPE(scale * FLOAT_TYPE(data_a[row*p.KX + col]));
  1688. }
  1689. }
  1690. """
  1691. # SOFT_MAX
  1692. soft_max_head = """
  1693. #version 450
  1694. #extension GL_EXT_shader_16bit_storage : require
  1695. layout (push_constant) uniform parameter
  1696. {
  1697. uint KX;
  1698. uint KY;
  1699. uint KZ;
  1700. float scale;
  1701. float max_bias;
  1702. float m0;
  1703. float m1;
  1704. uint n_head_log2;
  1705. } p;
  1706. """
  1707. soft_max_body = """
  1708. #extension GL_EXT_control_flow_attributes : enable
  1709. #define BLOCK_SIZE 512
  1710. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  1711. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1712. layout (binding = 1) readonly buffer Y {B_TYPE data_b[];};
  1713. layout (binding = 2) readonly buffer Z {C_TYPE data_c[];};
  1714. layout (binding = 3) buffer D {D_TYPE data_d[];};
  1715. shared FLOAT_TYPE vals[BLOCK_SIZE];
  1716. void main() {
  1717. const uint tid = gl_LocalInvocationID.x;
  1718. const uint rowx = gl_WorkGroupID.x;
  1719. const uint rowy = rowx % p.KY;
  1720. float slope = 0.0f;
  1721. // ALiBi
  1722. if (p.max_bias > 0.0f) {
  1723. const uint h = rowx/p.KY; // head index
  1724. const float base = h < p.n_head_log2 ? p.m0 : p.m1;
  1725. const uint exp = h < p.n_head_log2 ? h + 1 : 2*(h - p.n_head_log2) + 1;
  1726. slope = pow(base, exp);
  1727. }
  1728. // Find max
  1729. vals[tid] = uintBitsToFloat(0xFF800000);
  1730. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1731. vals[tid] = max(vals[tid], FLOAT_TYPE(data_a[rowx * p.KX + col]) * p.scale + (p.KY > 0 ? FLOAT_TYPE(data_b[rowy * p.KX + col]) : FLOAT_TYPE(0.0f)) + (p.KZ > 0 ? slope * data_c[col] : 0.0f));
  1732. }
  1733. barrier();
  1734. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  1735. if (tid < s) {
  1736. vals[tid] = max(vals[tid], vals[tid + s]);
  1737. }
  1738. barrier();
  1739. }
  1740. const FLOAT_TYPE max_val = vals[0];
  1741. barrier();
  1742. // Sum up values
  1743. vals[tid] = FLOAT_TYPE(0.0f);
  1744. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1745. const uint i = rowx * p.KX + col;
  1746. const FLOAT_TYPE val = exp(FLOAT_TYPE(data_a[i]) * p.scale + (p.KY > 0 ? FLOAT_TYPE(data_b[rowy * p.KX + col]) : FLOAT_TYPE(0.0f)) - max_val);
  1747. vals[tid] += val;
  1748. data_d[i] = D_TYPE(val);
  1749. }
  1750. barrier();
  1751. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  1752. if (tid < s) {
  1753. vals[tid] += vals[tid + s];
  1754. }
  1755. barrier();
  1756. }
  1757. const D_TYPE divisor = D_TYPE(vals[0]);
  1758. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  1759. data_d[rowx*p.KX + col] /= divisor;
  1760. }
  1761. }
  1762. """
  1763. # ROPE
  1764. rope_src = """
  1765. #version 450
  1766. #extension GL_EXT_shader_16bit_storage : require
  1767. layout(local_size_x = 1, local_size_y = 256, local_size_z = 1) in;
  1768. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1769. layout (binding = 1) readonly buffer Y {int data_b[];};
  1770. layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
  1771. layout (push_constant) uniform parameter {
  1772. uint ncols;
  1773. float freq_scale;
  1774. uint p_delta_rows;
  1775. float freq_base;
  1776. float ext_factor;
  1777. float attn_factor;
  1778. float corr_dims[4];
  1779. } p;
  1780. float rope_yarn_ramp(const float low, const float high, const uint i0) {
  1781. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  1782. return 1.0f - min(1.0f, max(0.0f, y));
  1783. }
  1784. void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out float sin_theta) {
  1785. float mscale = p.attn_factor;
  1786. // Get n-d rotational scaling corrected for extrapolation
  1787. float theta_interp = p.freq_scale * theta_extrap;
  1788. float theta = theta_interp;
  1789. if (p.ext_factor != 0.0f) {
  1790. float ramp_mix = rope_yarn_ramp(p.corr_dims[0], p.corr_dims[1], i0) * p.ext_factor;
  1791. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  1792. // Get n-d magnitude scaling corrected for interpolation
  1793. mscale *= 1.0f + 0.1f * log(1.0f / p.freq_scale);
  1794. }
  1795. cos_theta = cos(theta) * mscale;
  1796. sin_theta = sin(theta) * mscale;
  1797. }
  1798. void main() {
  1799. const uint col = gl_GlobalInvocationID.y * 2;
  1800. const uint row = gl_GlobalInvocationID.x;
  1801. if (col >= p.ncols) {
  1802. return;
  1803. }
  1804. const uint i = row*p.ncols + col;
  1805. const uint i2 = row/p.p_delta_rows;
  1806. const int pos = data_b[i2];
  1807. const float theta_base = pos * pow(p.freq_base, -float(col)/p.ncols);
  1808. float cos_theta, sin_theta;
  1809. rope_yarn(theta_base, col, cos_theta, sin_theta);
  1810. const float x0 = float(data_a[i + 0]);
  1811. const float x1 = float(data_a[i + 1]);
  1812. data_d[i + 0] = D_TYPE(x0*cos_theta - x1*sin_theta);
  1813. data_d[i + 1] = D_TYPE(x0*sin_theta + x1*cos_theta);
  1814. }
  1815. """
  1816. rope_neox_src = """
  1817. #version 450
  1818. #extension GL_EXT_shader_16bit_storage : require
  1819. layout(local_size_x = 1, local_size_y = 256, local_size_z = 1) in;
  1820. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1821. layout (binding = 1) readonly buffer Y {int data_b[];};
  1822. layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
  1823. layout (push_constant) uniform parameter {
  1824. uint ncols;
  1825. uint ndims;
  1826. float freq_scale;
  1827. uint p_delta_rows;
  1828. float freq_base;
  1829. float ext_factor;
  1830. float attn_factor;
  1831. float corr_dims[4];
  1832. float theta_scale;
  1833. float inv_ndims;
  1834. } p;
  1835. float rope_yarn_ramp(const float low, const float high, const uint i0) {
  1836. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  1837. return 1.0f - min(1.0f, max(0.0f, y));
  1838. }
  1839. void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out float sin_theta) {
  1840. float mscale = p.attn_factor;
  1841. // Get n-d rotational scaling corrected for extrapolation
  1842. float theta_interp = p.freq_scale * theta_extrap;
  1843. float theta = theta_interp;
  1844. if (p.ext_factor != 0.0f) {
  1845. float ramp_mix = rope_yarn_ramp(p.corr_dims[0], p.corr_dims[1], i0) * p.ext_factor;
  1846. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  1847. // Get n-d magnitude scaling corrected for interpolation
  1848. mscale *= 1.0f + 0.1f * log(1.0f / p.freq_scale);
  1849. }
  1850. cos_theta = cos(theta) * mscale;
  1851. sin_theta = sin(theta) * mscale;
  1852. }
  1853. void main() {
  1854. const uint col = gl_GlobalInvocationID.y * 2;
  1855. const uint row = gl_GlobalInvocationID.x;
  1856. if (col >= p.ncols) {
  1857. return;
  1858. }
  1859. const uint ib = col / p.ndims;
  1860. const uint ic = col % p.ndims;
  1861. if (ib > 0) {
  1862. const uint i = row*p.ncols + ib*p.ndims + ic;
  1863. data_d[i + 0] = data_a[i + 0];
  1864. data_d[i + 1] = data_a[i + 1];
  1865. return;
  1866. }
  1867. const uint i = row*p.ncols + ib*p.ndims + ic/2;
  1868. const uint i2 = row/p.p_delta_rows;
  1869. const float cur_rot = p.inv_ndims * ic - ib;
  1870. const int pos = data_b[i2];
  1871. const float theta_base = pos*p.freq_scale*pow(p.theta_scale, col/2.0f);
  1872. float cos_theta, sin_theta;
  1873. rope_yarn(theta_base, uint(cur_rot), cos_theta, sin_theta);
  1874. const float x0 = float(data_a[i + 0]);
  1875. const float x1 = float(data_a[i + p.ndims/2]);
  1876. data_d[i + 0] = D_TYPE(x0*cos_theta - x1*sin_theta);
  1877. data_d[i + p.ndims/2] = D_TYPE(x0*sin_theta + x1*cos_theta);
  1878. }
  1879. """
  1880. argsort_src = """
  1881. #version 450
  1882. #extension GL_EXT_shader_16bit_storage : require
  1883. layout(local_size_x = 1024, local_size_y = 1, local_size_z = 1) in;
  1884. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  1885. layout (binding = 1) buffer D {int data_d[];};
  1886. layout (push_constant) uniform parameter {
  1887. uint ncols;
  1888. bool ascending;
  1889. } p;
  1890. void swap(uint idx0, uint idx1) {
  1891. int tmp = data_d[idx0];
  1892. data_d[idx0] = data_d[idx1];
  1893. data_d[idx1] = tmp;
  1894. }
  1895. void main() {
  1896. // bitonic sort
  1897. const int col = int(gl_LocalInvocationID.x);
  1898. const uint row = gl_WorkGroupID.y;
  1899. if (col >= p.ncols) {
  1900. return;
  1901. }
  1902. const uint a_idx = row * p.ncols;
  1903. const uint d_idx = row * p.ncols;
  1904. // initialize indices
  1905. if (col < p.ncols) {
  1906. data_d[col] = col;
  1907. }
  1908. barrier();
  1909. for (uint k = 2; k <= p.ncols; k *= 2) {
  1910. for (uint j = k / 2; j > 0; j /= 2) {
  1911. const uint ixj = col ^ j;
  1912. if (ixj > col) {
  1913. if ((col & k) == 0) {
  1914. if (p.ascending ? data_a[a_idx + data_d[d_idx + col]] > data_a[a_idx + data_d[d_idx + ixj]] : data_a[a_idx + data_d[d_idx + col]] < data_a[a_idx + data_d[d_idx + ixj]]) {
  1915. swap(d_idx + col, d_idx + ixj);
  1916. }
  1917. } else {
  1918. if (p.ascending ? data_a[a_idx + data_d[d_idx + col]] < data_a[a_idx + data_d[d_idx + ixj]] : data_a[a_idx + data_d[d_idx + col]] > data_a[a_idx + data_d[d_idx + ixj]]) {
  1919. swap(d_idx + col, d_idx + ixj);
  1920. }
  1921. }
  1922. }
  1923. barrier();
  1924. }
  1925. }
  1926. }
  1927. """
  1928. GLSLC = "glslc"
  1929. VK_NUM_TYPES = 16
  1930. GGML_TYPE_F32 = 0
  1931. GGML_TYPE_F16 = 1
  1932. GGML_TYPE_Q4_0 = 2
  1933. GGML_TYPE_Q4_1 = 3
  1934. GGML_TYPE_Q5_0 = 6
  1935. GGML_TYPE_Q5_1 = 7
  1936. GGML_TYPE_Q8_0 = 8
  1937. GGML_TYPE_Q8_1 = 9
  1938. GGML_TYPE_Q2_K = 10
  1939. GGML_TYPE_Q3_K = 11
  1940. GGML_TYPE_Q4_K = 12
  1941. GGML_TYPE_Q5_K = 13
  1942. GGML_TYPE_Q6_K = 14
  1943. GGML_TYPE_Q8_K = 15
  1944. type_names = {
  1945. GGML_TYPE_F32: "f32",
  1946. GGML_TYPE_F16: "f16",
  1947. GGML_TYPE_Q4_0: "q4_0",
  1948. GGML_TYPE_Q4_1: "q4_1",
  1949. GGML_TYPE_Q5_0: "q5_0",
  1950. GGML_TYPE_Q5_1: "q5_1",
  1951. GGML_TYPE_Q8_0: "q8_0",
  1952. GGML_TYPE_Q8_1: "q8_1",
  1953. GGML_TYPE_Q2_K: "q2_K",
  1954. GGML_TYPE_Q3_K: "q3_K",
  1955. GGML_TYPE_Q4_K: "q4_K",
  1956. GGML_TYPE_Q5_K: "q5_K",
  1957. GGML_TYPE_Q6_K: "q6_K",
  1958. GGML_TYPE_Q8_K: "q8_K",
  1959. }
  1960. K_QUANTS_PER_ITERATION = 2
  1961. ASYNCIO_CONCURRENCY = 64
  1962. output_dir = gettempdir()
  1963. lock = asyncio.Lock()
  1964. shader_fnames = []
  1965. async def string_to_spv(name, code, defines, fp16=True):
  1966. f = NamedTemporaryFile(mode="w", delete=False)
  1967. f.write(code)
  1968. f.flush()
  1969. name = f"{name}{'_fp32' if not fp16 else ''}"
  1970. fname = os.path.join(output_dir, f"{name}.comp")
  1971. cmd = [GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", f.name, "-o", fname]
  1972. cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
  1973. proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
  1974. stdout, stderr = await proc.communicate()
  1975. stdout = stdout.decode()
  1976. error = stderr.decode()
  1977. if proc.returncode:
  1978. # Generate preprocessed code
  1979. cmd = [GLSLC, "-E", f.name]
  1980. cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
  1981. proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
  1982. stdout, stderr = await proc.communicate()
  1983. print(" ".join(cmd))
  1984. if proc.returncode:
  1985. raise RuntimeError(f"{name=} {f.name=} {stdout=} {stderr=}")
  1986. preprocessed_code = stdout.decode()
  1987. cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
  1988. code_with_lines = "\n".join([f"{i + 1}: {line}" for i, line in enumerate(preprocessed_code.splitlines())])
  1989. print(f"ERROR compiling {name}\n\n{code_with_lines}\n\n{error}")
  1990. f.close()
  1991. os.remove(f.name)
  1992. sys.exit(proc.returncode)
  1993. f.close()
  1994. os.remove(f.name)
  1995. async with lock:
  1996. shader_fnames.append((name, fname))
  1997. async def main():
  1998. print("ggml_vulkan: Generating and compiling shaders to SPIR-V")
  1999. tasks = []
  2000. stream = []
  2001. for fp16 in (False, True):
  2002. # mulmat
  2003. if fp16:
  2004. shader_float_type = shader_f16
  2005. load_vec = "8"
  2006. vec_type_f16 = "f16mat2x4"
  2007. vec_type = "mat2x4"
  2008. else:
  2009. shader_float_type = shader_f32
  2010. load_vec = "4"
  2011. vec_type_f16 = "f16vec4"
  2012. vec_type = "vec4"
  2013. stream.clear()
  2014. stream.extend((mulmat_head, shader_float_type, mulmat_body1, mulmat_load_scalar, mulmat_body2))
  2015. tasks.append(string_to_spv("matmul_f32", "".join(stream), {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2016. tasks.append(string_to_spv("matmul_f32_aligned", "".join(stream), {"LOAD_VEC_A": load_vec, "LOAD_VEC_B": load_vec, "A_TYPE": vec_type, "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2017. tasks.append(string_to_spv("matmul_f16", "".join(stream), {"A_TYPE": "float16_t", "B_TYPE": "float16_t", "D_TYPE": "float"}, fp16))
  2018. tasks.append(string_to_spv("matmul_f16_aligned", "".join(stream), {"LOAD_VEC_A": load_vec, "LOAD_VEC_B": load_vec, "A_TYPE": vec_type_f16, "B_TYPE": vec_type_f16, "D_TYPE": "float"}, fp16))
  2019. tasks.append(string_to_spv("matmul_f16_f32", "".join(stream), {"A_TYPE": "float16_t", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2020. tasks.append(string_to_spv("matmul_f16_f32_aligned", "".join(stream), {"LOAD_VEC_A": load_vec, "LOAD_VEC_B": load_vec, "A_TYPE": vec_type_f16, "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2021. stream.clear()
  2022. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_0_defines, mulmat_body1, mulmat_load_q4_0, mulmat_body2))
  2023. tasks.append(string_to_spv("matmul_q4_0_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q4_0", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2024. tasks.append(string_to_spv("matmul_q4_0_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q4_0", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2025. stream.clear()
  2026. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_1_defines, mulmat_body1, mulmat_load_q4_1, mulmat_body2))
  2027. tasks.append(string_to_spv("matmul_q4_1_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q4_1", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2028. tasks.append(string_to_spv("matmul_q4_1_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q4_1", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2029. stream.clear()
  2030. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_0_defines, mulmat_body1, mulmat_load_q5_0, mulmat_body2))
  2031. tasks.append(string_to_spv("matmul_q5_0_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q5_0", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2032. tasks.append(string_to_spv("matmul_q5_0_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q5_0", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2033. stream.clear()
  2034. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_1_defines, mulmat_body1, mulmat_load_q5_1, mulmat_body2))
  2035. tasks.append(string_to_spv("matmul_q5_1_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q5_1", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2036. tasks.append(string_to_spv("matmul_q5_1_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q5_1", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2037. stream.clear()
  2038. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q8_0_defines, mulmat_body1, mulmat_load_q8_0, mulmat_body2))
  2039. tasks.append(string_to_spv("matmul_q8_0_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q8_0", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2040. tasks.append(string_to_spv("matmul_q8_0_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q8_0", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2041. stream.clear()
  2042. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q2_K_defines, mulmat_body1, mulmat_load_q2_K, mulmat_body2))
  2043. tasks.append(string_to_spv("matmul_q2_k_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q2_K", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2044. tasks.append(string_to_spv("matmul_q2_k_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q2_K", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2045. stream.clear()
  2046. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q3_K_defines, mulmat_body1, mulmat_load_q3_K, mulmat_body2))
  2047. tasks.append(string_to_spv("matmul_q3_k_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q3_K", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2048. tasks.append(string_to_spv("matmul_q3_k_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q3_K", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2049. stream.clear()
  2050. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_K_defines, mulmat_body1, mulmat_load_q4_K, mulmat_body2))
  2051. tasks.append(string_to_spv("matmul_q4_k_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q4_K", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2052. tasks.append(string_to_spv("matmul_q4_k_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q4_K", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2053. stream.clear()
  2054. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_K_defines, mulmat_body1, mulmat_load_q5_K, mulmat_body2))
  2055. tasks.append(string_to_spv("matmul_q5_k_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q5_K", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2056. tasks.append(string_to_spv("matmul_q5_k_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q5_K", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2057. stream.clear()
  2058. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q6_K_defines, mulmat_body1, mulmat_load_q6_K, mulmat_body2))
  2059. tasks.append(string_to_spv("matmul_q6_k_f32", "".join(stream), {"LOAD_VEC_A": 2, "A_TYPE": "block_q6_K", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2060. tasks.append(string_to_spv("matmul_q6_k_f32_aligned", "".join(stream), {"LOAD_VEC_A": 2, "LOAD_VEC_B": load_vec, "A_TYPE": "block_q6_K", "B_TYPE": vec_type, "D_TYPE": "float"}, fp16))
  2061. # Shaders where precision is needed, so no fp16 version
  2062. # mul mat vec
  2063. for i in range(0, VK_NUM_TYPES):
  2064. stream.clear()
  2065. stream.extend((mul_mat_vec_head, shader_int8_ext, shader_f32))
  2066. if i == GGML_TYPE_F16:
  2067. stream.extend((shader_f16_defines, shader_float_dequant_func, mul_mat_vec_body))
  2068. elif i == GGML_TYPE_Q4_0:
  2069. stream.extend((shader_q4_0_defines, shader_q4_0_dequant_func, mul_mat_vec_body))
  2070. elif i == GGML_TYPE_Q4_1:
  2071. stream.extend((shader_q4_1_defines, shader_q4_1_dequant_func, mul_mat_vec_body))
  2072. elif i == GGML_TYPE_Q5_0:
  2073. stream.extend((shader_q5_0_defines, shader_q5_0_dequant_func, mul_mat_vec_body))
  2074. elif i == GGML_TYPE_Q5_1:
  2075. stream.extend((shader_q5_1_defines, shader_q5_1_dequant_func, mul_mat_vec_body))
  2076. elif i == GGML_TYPE_Q8_0:
  2077. stream.extend((shader_q8_0_defines, shader_q8_0_dequant_func, mul_mat_vec_body))
  2078. elif i == GGML_TYPE_Q2_K:
  2079. stream.extend((shader_q2_K_defines, mul_mat_vec_q2_K_body))
  2080. elif i == GGML_TYPE_Q3_K:
  2081. stream.extend((shader_q3_K_defines, mul_mat_vec_q3_K_body))
  2082. elif i == GGML_TYPE_Q4_K:
  2083. stream.extend((shader_q4_K_defines, mul_mat_vec_q4_K_body))
  2084. elif i == GGML_TYPE_Q5_K:
  2085. stream.extend((shader_q5_K_defines, mul_mat_vec_q5_K_body))
  2086. elif i == GGML_TYPE_Q6_K:
  2087. stream.extend((shader_q6_K_defines, mul_mat_vec_q6_K_body))
  2088. else:
  2089. continue
  2090. tasks.append(string_to_spv(f"mul_mat_vec_{type_names[i]}_f32", "".join(stream), {"B_TYPE": "float", "D_TYPE": "float", "K_QUANTS_PER_ITERATION": K_QUANTS_PER_ITERATION}))
  2091. # Dequant shaders
  2092. for i in range(0, VK_NUM_TYPES):
  2093. stream.clear()
  2094. stream.extend((dequant_head, shader_int8_ext, shader_f32))
  2095. if i == GGML_TYPE_F32:
  2096. stream.append(dequant_f32_body)
  2097. elif i == GGML_TYPE_Q4_0:
  2098. stream.extend((shader_q4_0_defines, dequant_q4_0_body))
  2099. elif i == GGML_TYPE_Q4_1:
  2100. stream.extend((shader_q4_1_defines, dequant_q4_1_body))
  2101. elif i == GGML_TYPE_Q5_0:
  2102. stream.extend((shader_q5_0_defines, dequant_q5_0_body))
  2103. elif i == GGML_TYPE_Q5_1:
  2104. stream.extend((shader_q5_1_defines, dequant_q5_1_body))
  2105. elif i == GGML_TYPE_Q8_0:
  2106. stream.extend((shader_q8_0_defines, dequant_q8_0_body))
  2107. elif i == GGML_TYPE_Q2_K:
  2108. stream.extend((shader_q2_K_defines, dequant_q2_K_body))
  2109. elif i == GGML_TYPE_Q3_K:
  2110. stream.extend((shader_q3_K_defines, dequant_q3_K_body))
  2111. elif i == GGML_TYPE_Q4_K:
  2112. stream.extend((shader_q4_K_defines, dequant_q4_K_body))
  2113. elif i == GGML_TYPE_Q5_K:
  2114. stream.extend((shader_q5_K_defines, dequant_q5_K_body))
  2115. elif i == GGML_TYPE_Q6_K:
  2116. stream.extend((shader_q6_K_defines, dequant_q6_K_body))
  2117. else:
  2118. continue
  2119. tasks.append(string_to_spv(f"dequant_{type_names[i]}", "".join(stream), {"D_TYPE": "float16_t"}))
  2120. # get_rows
  2121. for i in range(0, VK_NUM_TYPES):
  2122. stream.clear()
  2123. stream.extend((generic_binary_op_head, shader_int8_ext, shader_f32))
  2124. optimization_workaround = False
  2125. if i == GGML_TYPE_F32:
  2126. stream.extend((shader_f32_defines, generic_binary_op_funcs, get_rows_float_body))
  2127. elif i == GGML_TYPE_F16:
  2128. stream.extend((shader_f16_defines, generic_binary_op_funcs, get_rows_float_body))
  2129. optimization_workaround = True
  2130. elif i == GGML_TYPE_Q4_0:
  2131. stream.extend((shader_q4_0_defines, shader_q4_0_dequant_func, generic_binary_op_funcs, get_rows_body))
  2132. elif i == GGML_TYPE_Q4_1:
  2133. stream.extend((shader_q4_1_defines, shader_q4_1_dequant_func, generic_binary_op_funcs, get_rows_body))
  2134. elif i == GGML_TYPE_Q5_0:
  2135. stream.extend((shader_q5_0_defines, shader_q5_0_dequant_func, generic_binary_op_funcs, get_rows_body))
  2136. elif i == GGML_TYPE_Q5_1:
  2137. stream.extend((shader_q5_1_defines, shader_q5_1_dequant_func, generic_binary_op_funcs, get_rows_body))
  2138. elif i == GGML_TYPE_Q8_0:
  2139. stream.extend((shader_q8_0_defines, shader_q8_0_dequant_func, generic_binary_op_funcs, get_rows_body))
  2140. else:
  2141. continue
  2142. if optimization_workaround:
  2143. tasks.append(string_to_spv(f"get_rows_{type_names[i]}", "".join(stream), {"B_TYPE": "int", "D_TYPE": "float16_t", "OPTIMIZATION_ERROR_WORKAROUND": "1"}))
  2144. else:
  2145. tasks.append(string_to_spv(f"get_rows_{type_names[i]}", "".join(stream), {"B_TYPE": "int", "D_TYPE": "float16_t"}))
  2146. tasks.append(string_to_spv(f"get_rows_{type_names[i]}_f32", "".join(stream), {"B_TYPE": "int", "D_TYPE": "float"}))
  2147. tasks.append(string_to_spv("mul_mat_vec_p021_f16_f32", mul_mat_p021_src, {"A_TYPE": "float16_t", "B_TYPE": "float", "D_TYPE": "float"}))
  2148. tasks.append(string_to_spv("mul_mat_vec_nc_f16_f32", mul_mat_nc_src, {"A_TYPE": "float16_t", "B_TYPE": "float", "D_TYPE": "float"}))
  2149. # Norms
  2150. tasks.append(string_to_spv("norm_f32", f"{generic_head}\n{shader_f32}\n{norm_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2151. tasks.append(string_to_spv("rms_norm_f32", f"{generic_head}\n{shader_f32}\n{rms_norm_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2152. tasks.append(string_to_spv("cpy_f32_f32", f"{generic_unary_op_combined}\n{cpy_end}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2153. tasks.append(string_to_spv("cpy_f32_f16", f"{generic_unary_op_combined}\n{cpy_end}", {"A_TYPE": "float", "D_TYPE": "float16_t"}))
  2154. tasks.append(string_to_spv("cpy_f16_f16", f"{generic_unary_op_combined}\n{cpy_f16_f16_end}", {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
  2155. tasks.append(string_to_spv("add_f32", f"{generic_binary_op_combined}\n{add_body}", {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2156. tasks.append(string_to_spv("split_k_reduce", mulmat_split_k_reduce_src, {}))
  2157. tasks.append(string_to_spv("mul_f32", f"{generic_binary_op_combined}\n{mul_body}", {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2158. tasks.append(string_to_spv("scale_f32", f"{generic_unary_op_combined}\n{scale_body}", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2159. tasks.append(string_to_spv("sqr_f32", f"{generic_unary_op_combined}\n{sqr_body}", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2160. tasks.append(string_to_spv("clamp_f32", f"{generic_unary_op_combined}\n{clamp_body}", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2161. tasks.append(string_to_spv("gelu_f32", f"{generic_head}\n{shader_f32}\n{gelu_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2162. tasks.append(string_to_spv("silu_f32", f"{generic_head}\n{shader_f32}\n{silu_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2163. tasks.append(string_to_spv("relu_f32", f"{generic_head}\n{shader_f32}\n{relu_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2164. tasks.append(string_to_spv("diag_mask_inf_f32", f"{diag_mask_inf_head}\n{shader_f32}\n{diag_mask_inf_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2165. tasks.append(string_to_spv("soft_max_f32", f"{soft_max_head}\n{shader_f32}\n{soft_max_body}", {"A_TYPE": "float", "B_TYPE": "float", "C_TYPE": "float", "D_TYPE": "float"}))
  2166. tasks.append(string_to_spv("rope_f32", rope_src, {"A_TYPE": "float", "D_TYPE": "float"}))
  2167. tasks.append(string_to_spv("rope_f16", rope_src, {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
  2168. tasks.append(string_to_spv("rope_neox_f32", rope_neox_src, {"A_TYPE": "float", "D_TYPE": "float"}))
  2169. tasks.append(string_to_spv("rope_neox_f16", rope_neox_src, {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
  2170. tasks.append(string_to_spv("argsort_f32", argsort_src, {"A_TYPE": "float"}))
  2171. # Helper to decorate tasks with semaphore acquisition.
  2172. async def withSemaphore(sem, task):
  2173. async with sem:
  2174. return await task
  2175. # Run tasks concurrently guarded by a concurrency limit.
  2176. sem = asyncio.Semaphore(ASYNCIO_CONCURRENCY)
  2177. await asyncio.gather(*(withSemaphore(sem, task) for task in tasks))
  2178. with open("ggml-vulkan-shaders.hpp", "w") as f:
  2179. f.write("#include <cstdint>\n\n")
  2180. for name, path in sorted(shader_fnames):
  2181. with open(path, "rb") as spv:
  2182. counter = 0
  2183. newline_counter = 0
  2184. f.write(f"unsigned char {name}_data[] = {{\n")
  2185. for val in spv.read():
  2186. f.write(f"0x{val:02x},")
  2187. newline_counter += 1
  2188. counter += 1
  2189. if newline_counter >= 12:
  2190. newline_counter = 0
  2191. f.write("\n")
  2192. f.write("\n};\n")
  2193. f.write(f"const uint64_t {name}_len = {counter};\n\n")
  2194. os.remove(path)
  2195. if __name__ == "__main__":
  2196. parser = argparse.ArgumentParser(description="GGML Vulkan Shader Generator")
  2197. parser.add_argument("--glslc", help="Path to glslc")
  2198. args = parser.parse_args()
  2199. if args.glslc:
  2200. GLSLC = args.glslc
  2201. asyncio.run(main())