ggml_vk_generate_shaders.py 123 KB

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