ggml_vk_generate_shaders.py 89 KB

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