ggml_vk_generate_shaders.py 108 KB

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