ggml_vk_generate_shaders.py 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094
  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_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_b[];};
  1931. layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
  1932. layout (push_constant) uniform parameter {
  1933. uint ncols;
  1934. float freq_scale;
  1935. uint p_delta_rows;
  1936. float freq_base;
  1937. float ext_factor;
  1938. float attn_factor;
  1939. float corr_dims[4];
  1940. } p;
  1941. float rope_yarn_ramp(const float low, const float high, const uint i0) {
  1942. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  1943. return 1.0f - min(1.0f, max(0.0f, y));
  1944. }
  1945. void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out float sin_theta) {
  1946. float mscale = p.attn_factor;
  1947. // Get n-d rotational scaling corrected for extrapolation
  1948. float theta_interp = p.freq_scale * theta_extrap;
  1949. float theta = theta_interp;
  1950. if (p.ext_factor != 0.0f) {
  1951. float ramp_mix = rope_yarn_ramp(p.corr_dims[0], p.corr_dims[1], i0) * p.ext_factor;
  1952. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  1953. // Get n-d magnitude scaling corrected for interpolation
  1954. mscale *= 1.0f + 0.1f * log(1.0f / p.freq_scale);
  1955. }
  1956. cos_theta = cos(theta) * mscale;
  1957. sin_theta = sin(theta) * mscale;
  1958. }
  1959. void main() {
  1960. const uint col = gl_GlobalInvocationID.y * 2;
  1961. const uint row = gl_GlobalInvocationID.x;
  1962. if (col >= p.ncols) {
  1963. return;
  1964. }
  1965. const uint i = row*p.ncols + col;
  1966. const uint i2 = row/p.p_delta_rows;
  1967. const int pos = data_b[i2];
  1968. const float theta_base = pos * pow(p.freq_base, -float(col)/p.ncols);
  1969. float cos_theta, sin_theta;
  1970. rope_yarn(theta_base, col, cos_theta, sin_theta);
  1971. const float x0 = float(data_a[i + 0]);
  1972. const float x1 = float(data_a[i + 1]);
  1973. data_d[i + 0] = D_TYPE(x0*cos_theta - x1*sin_theta);
  1974. data_d[i + 1] = D_TYPE(x0*sin_theta + x1*cos_theta);
  1975. }
  1976. """
  1977. rope_neox_src = """
  1978. #version 450
  1979. #extension GL_EXT_shader_16bit_storage : require
  1980. layout(local_size_x = 1, local_size_y = 256, local_size_z = 1) in;
  1981. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  1982. layout (binding = 1) readonly buffer Y {int data_b[];};
  1983. layout (binding = 2) readonly buffer Z {float data_freq_factors[];};
  1984. layout (binding = 3) writeonly buffer D {D_TYPE data_d[];};
  1985. layout (push_constant) uniform parameter {
  1986. uint ncols;
  1987. uint ndims;
  1988. float freq_scale;
  1989. uint p_delta_rows;
  1990. float freq_base;
  1991. float ext_factor;
  1992. float attn_factor;
  1993. float corr_dims[4];
  1994. float theta_scale;
  1995. float inv_ndims;
  1996. uint has_freq_facs;
  1997. } p;
  1998. float rope_yarn_ramp(const float low, const float high, const uint i0) {
  1999. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  2000. return 1.0f - min(1.0f, max(0.0f, y));
  2001. }
  2002. void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out float sin_theta) {
  2003. float mscale = p.attn_factor;
  2004. // Get n-d rotational scaling corrected for extrapolation
  2005. float theta_interp = p.freq_scale * theta_extrap;
  2006. float theta = theta_interp;
  2007. if (p.ext_factor != 0.0f) {
  2008. float ramp_mix = rope_yarn_ramp(p.corr_dims[0], p.corr_dims[1], i0) * p.ext_factor;
  2009. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  2010. // Get n-d magnitude scaling corrected for interpolation
  2011. mscale *= 1.0f + 0.1f * log(1.0f / p.freq_scale);
  2012. }
  2013. cos_theta = cos(theta) * mscale;
  2014. sin_theta = sin(theta) * mscale;
  2015. }
  2016. void main() {
  2017. const uint col = gl_GlobalInvocationID.y * 2;
  2018. const uint row = gl_GlobalInvocationID.x;
  2019. if (col >= p.ncols) {
  2020. return;
  2021. }
  2022. const uint ib = col / p.ndims;
  2023. const uint ic = col % p.ndims;
  2024. if (ib > 0) {
  2025. const uint i = row*p.ncols + ib*p.ndims + ic;
  2026. data_d[i + 0] = data_a[i + 0];
  2027. data_d[i + 1] = data_a[i + 1];
  2028. return;
  2029. }
  2030. const uint i = row*p.ncols + ib*p.ndims + ic/2;
  2031. const uint i2 = row/p.p_delta_rows;
  2032. const int pos = data_b[i2];
  2033. const float freq_factor = p.has_freq_facs != 0 ? data_freq_factors[ic/2] : 1.0f;
  2034. const float theta_base = pos*p.freq_scale*pow(p.theta_scale, col/2.0f) / freq_factor;
  2035. float cos_theta, sin_theta;
  2036. rope_yarn(theta_base, ic, cos_theta, sin_theta);
  2037. const float x0 = float(data_a[i + 0]);
  2038. const float x1 = float(data_a[i + p.ndims/2]);
  2039. data_d[i + 0] = D_TYPE(x0*cos_theta - x1*sin_theta);
  2040. data_d[i + p.ndims/2] = D_TYPE(x0*sin_theta + x1*cos_theta);
  2041. }
  2042. """
  2043. argsort_src = """
  2044. #version 450
  2045. #extension GL_EXT_shader_16bit_storage : require
  2046. #define BLOCK_SIZE 1024
  2047. #define ASC 0
  2048. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  2049. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  2050. layout (binding = 1) buffer D {int data_d[];};
  2051. layout (push_constant) uniform parameter {
  2052. uint ncols;
  2053. uint ncols_pad;
  2054. uint order;
  2055. } p;
  2056. shared int dst_row[BLOCK_SIZE];
  2057. void swap(uint idx0, uint idx1) {
  2058. int tmp = dst_row[idx0];
  2059. dst_row[idx0] = dst_row[idx1];
  2060. dst_row[idx1] = tmp;
  2061. }
  2062. void main() {
  2063. // bitonic sort
  2064. const int col = int(gl_LocalInvocationID.x);
  2065. const uint row = gl_WorkGroupID.y;
  2066. if (col >= p.ncols_pad) {
  2067. return;
  2068. }
  2069. const uint row_offset = row * p.ncols;
  2070. // initialize indices
  2071. dst_row[col] = col;
  2072. barrier();
  2073. for (uint k = 2; k <= p.ncols_pad; k *= 2) {
  2074. for (uint j = k / 2; j > 0; j /= 2) {
  2075. const uint ixj = col ^ j;
  2076. if (ixj > col) {
  2077. if ((col & k) == 0) {
  2078. if (dst_row[col] >= p.ncols ||
  2079. (dst_row[ixj] < p.ncols && (p.order == ASC ?
  2080. data_a[row_offset + dst_row[col]] > data_a[row_offset + dst_row[ixj]] :
  2081. data_a[row_offset + dst_row[col]] < data_a[row_offset + dst_row[ixj]]))
  2082. ) {
  2083. swap(col, ixj);
  2084. }
  2085. } else {
  2086. if (dst_row[ixj] >= p.ncols ||
  2087. (dst_row[col] < p.ncols && (p.order == ASC ?
  2088. data_a[row_offset + dst_row[col]] < data_a[row_offset + dst_row[ixj]] :
  2089. data_a[row_offset + dst_row[col]] > data_a[row_offset + dst_row[ixj]]))
  2090. ) {
  2091. swap(col, ixj);
  2092. }
  2093. }
  2094. }
  2095. barrier();
  2096. }
  2097. }
  2098. if (col < p.ncols) {
  2099. data_d[row_offset + col] = dst_row[col];
  2100. }
  2101. }
  2102. """
  2103. sum_rows_src = """
  2104. #extension GL_EXT_control_flow_attributes : enable
  2105. layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
  2106. layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
  2107. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  2108. layout (constant_id = 0) const uint BLOCK_SIZE = 32;
  2109. shared FLOAT_TYPE tmp[BLOCK_SIZE];
  2110. void main() {
  2111. const uint row = gl_WorkGroupID.x;
  2112. const uint col = gl_LocalInvocationID.x;
  2113. tmp[col] = FLOAT_TYPE(0.0f);
  2114. for (uint i = col; i < p.KX; i += BLOCK_SIZE) {
  2115. tmp[col] += FLOAT_TYPE(data_a[row*p.KX + i]);
  2116. }
  2117. barrier();
  2118. [[unroll]] for (int s = int(BLOCK_SIZE) / 2; s > 0; s >>= 1) {
  2119. if (col < s) {
  2120. tmp[col] += tmp[col + s];
  2121. }
  2122. barrier();
  2123. }
  2124. if (col == 0) {
  2125. data_d[row] = D_TYPE(tmp[0]);
  2126. }
  2127. }
  2128. """
  2129. GLSLC = "glslc"
  2130. VK_NUM_TYPES = 16
  2131. GGML_TYPE_F32 = 0
  2132. GGML_TYPE_F16 = 1
  2133. GGML_TYPE_Q4_0 = 2
  2134. GGML_TYPE_Q4_1 = 3
  2135. GGML_TYPE_Q5_0 = 6
  2136. GGML_TYPE_Q5_1 = 7
  2137. GGML_TYPE_Q8_0 = 8
  2138. GGML_TYPE_Q8_1 = 9
  2139. GGML_TYPE_Q2_K = 10
  2140. GGML_TYPE_Q3_K = 11
  2141. GGML_TYPE_Q4_K = 12
  2142. GGML_TYPE_Q5_K = 13
  2143. GGML_TYPE_Q6_K = 14
  2144. GGML_TYPE_Q8_K = 15
  2145. type_names = {
  2146. GGML_TYPE_F32: "f32",
  2147. GGML_TYPE_F16: "f16",
  2148. GGML_TYPE_Q4_0: "q4_0",
  2149. GGML_TYPE_Q4_1: "q4_1",
  2150. GGML_TYPE_Q5_0: "q5_0",
  2151. GGML_TYPE_Q5_1: "q5_1",
  2152. GGML_TYPE_Q8_0: "q8_0",
  2153. GGML_TYPE_Q8_1: "q8_1",
  2154. GGML_TYPE_Q2_K: "q2_K",
  2155. GGML_TYPE_Q3_K: "q3_K",
  2156. GGML_TYPE_Q4_K: "q4_K",
  2157. GGML_TYPE_Q5_K: "q5_K",
  2158. GGML_TYPE_Q6_K: "q6_K",
  2159. GGML_TYPE_Q8_K: "q8_K",
  2160. }
  2161. K_QUANTS_PER_ITERATION = 2
  2162. ASYNCIO_CONCURRENCY = 64
  2163. output_dir = gettempdir()
  2164. lock = asyncio.Lock()
  2165. shader_fnames = []
  2166. async def string_to_spv(name, code, defines, fp16=True):
  2167. f = NamedTemporaryFile(mode="w", delete=False)
  2168. f.write(code)
  2169. f.flush()
  2170. name = f"{name}{'_fp32' if not fp16 else ''}"
  2171. fname = os.path.join(output_dir, f"{name}.comp")
  2172. cmd = [GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", f.name, "-o", fname]
  2173. cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
  2174. proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
  2175. stdout, stderr = await proc.communicate()
  2176. stdout = stdout.decode()
  2177. error = stderr.decode()
  2178. if proc.returncode:
  2179. # Generate preprocessed code
  2180. cmd = [GLSLC, "-E", f.name]
  2181. cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
  2182. proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
  2183. stdout, stderr = await proc.communicate()
  2184. logger.info(" ".join(cmd))
  2185. if proc.returncode:
  2186. raise RuntimeError(f"{name=} {f.name=} {stdout=} {stderr=}")
  2187. preprocessed_code = stdout.decode()
  2188. cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
  2189. code_with_lines = "\n".join([f"{i + 1}: {line}" for i, line in enumerate(preprocessed_code.splitlines())])
  2190. logger.error(f"cannot compile {name}\n\n{code_with_lines}\n\n{error}")
  2191. f.close()
  2192. os.remove(f.name)
  2193. sys.exit(proc.returncode)
  2194. f.close()
  2195. os.remove(f.name)
  2196. async with lock:
  2197. shader_fnames.append((name, fname))
  2198. async def main():
  2199. logger.info("ggml_vulkan: Generating and compiling shaders to SPIR-V")
  2200. tasks = []
  2201. stream = []
  2202. for fp16 in (False, True):
  2203. # mulmat
  2204. if fp16:
  2205. shader_float_type = shader_f16
  2206. load_vec = "8"
  2207. vec_type_f16 = "f16mat2x4"
  2208. vec_type = "mat2x4"
  2209. else:
  2210. shader_float_type = shader_f32
  2211. load_vec = "4"
  2212. vec_type_f16 = "f16vec4"
  2213. vec_type = "vec4"
  2214. stream.clear()
  2215. stream.extend((mulmat_head, shader_float_type, mulmat_body1, mulmat_load_scalar, mulmat_body2))
  2216. tasks.append(string_to_spv("matmul_f32", "".join(stream), {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2217. 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))
  2218. tasks.append(string_to_spv("matmul_f32_f16", "".join(stream), {"A_TYPE": "float", "B_TYPE": "float16_t", "D_TYPE": "float"}, fp16))
  2219. 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))
  2220. tasks.append(string_to_spv("matmul_f16", "".join(stream), {"A_TYPE": "float16_t", "B_TYPE": "float16_t", "D_TYPE": "float"}, fp16))
  2221. 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))
  2222. tasks.append(string_to_spv("matmul_f16_f32", "".join(stream), {"A_TYPE": "float16_t", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2223. 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))
  2224. stream.clear()
  2225. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_0_defines, mulmat_body1, mulmat_load_q4_0, mulmat_body2))
  2226. 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))
  2227. 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))
  2228. stream.clear()
  2229. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_1_defines, mulmat_body1, mulmat_load_q4_1, mulmat_body2))
  2230. 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))
  2231. 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))
  2232. stream.clear()
  2233. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_0_defines, mulmat_body1, mulmat_load_q5_0, mulmat_body2))
  2234. 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))
  2235. 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))
  2236. stream.clear()
  2237. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_1_defines, mulmat_body1, mulmat_load_q5_1, mulmat_body2))
  2238. 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))
  2239. 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))
  2240. stream.clear()
  2241. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q8_0_defines, mulmat_body1, mulmat_load_q8_0, mulmat_body2))
  2242. 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))
  2243. 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))
  2244. stream.clear()
  2245. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q2_K_defines, mulmat_body1, mulmat_load_q2_K, mulmat_body2))
  2246. 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))
  2247. 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))
  2248. stream.clear()
  2249. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q3_K_defines, mulmat_body1, mulmat_load_q3_K, mulmat_body2))
  2250. 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))
  2251. 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))
  2252. stream.clear()
  2253. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_K_defines, mulmat_body1, mulmat_load_q4_K, mulmat_body2))
  2254. 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))
  2255. 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))
  2256. stream.clear()
  2257. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_K_defines, mulmat_body1, mulmat_load_q5_K, mulmat_body2))
  2258. 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))
  2259. 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))
  2260. stream.clear()
  2261. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q6_K_defines, mulmat_body1, mulmat_load_q6_K, mulmat_body2))
  2262. 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))
  2263. 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))
  2264. # MUL_MAT_ID
  2265. stream.clear()
  2266. stream.extend((mulmat_head, shader_float_type, mulmat_body1, mulmat_load_scalar, mulmat_body2))
  2267. tasks.append(string_to_spv("matmul_id_f32", "".join(stream), {"MUL_MAT_ID": "1", "A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
  2268. 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))
  2269. 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))
  2270. 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))
  2271. 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))
  2272. 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))
  2273. stream.clear()
  2274. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_0_defines, mulmat_body1, mulmat_load_q4_0, mulmat_body2))
  2275. 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))
  2276. 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))
  2277. stream.clear()
  2278. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_1_defines, mulmat_body1, mulmat_load_q4_1, mulmat_body2))
  2279. 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))
  2280. 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))
  2281. stream.clear()
  2282. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_0_defines, mulmat_body1, mulmat_load_q5_0, mulmat_body2))
  2283. 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))
  2284. 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))
  2285. stream.clear()
  2286. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_1_defines, mulmat_body1, mulmat_load_q5_1, mulmat_body2))
  2287. 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))
  2288. 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))
  2289. stream.clear()
  2290. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q8_0_defines, mulmat_body1, mulmat_load_q8_0, mulmat_body2))
  2291. 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))
  2292. 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))
  2293. stream.clear()
  2294. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q2_K_defines, mulmat_body1, mulmat_load_q2_K, mulmat_body2))
  2295. 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))
  2296. 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))
  2297. stream.clear()
  2298. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q3_K_defines, mulmat_body1, mulmat_load_q3_K, mulmat_body2))
  2299. 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))
  2300. 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))
  2301. stream.clear()
  2302. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q4_K_defines, mulmat_body1, mulmat_load_q4_K, mulmat_body2))
  2303. 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))
  2304. 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))
  2305. stream.clear()
  2306. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q5_K_defines, mulmat_body1, mulmat_load_q5_K, mulmat_body2))
  2307. 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))
  2308. 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))
  2309. stream.clear()
  2310. stream.extend((mulmat_head, shader_int8_ext, shader_float_type, shader_q6_K_defines, mulmat_body1, mulmat_load_q6_K, mulmat_body2))
  2311. 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))
  2312. 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))
  2313. # Shaders where precision is needed, so no fp16 version
  2314. # mul mat vec
  2315. for i in range(0, VK_NUM_TYPES):
  2316. stream.clear()
  2317. stream.extend((mul_mat_vec_head, shader_int8_ext, shader_f32))
  2318. if i == GGML_TYPE_F32:
  2319. stream.extend((shader_f32_defines, mul_mat_vec_layout, shader_float_dequant_func, mul_mat_vec_body))
  2320. elif i == GGML_TYPE_F16:
  2321. stream.extend((shader_f16_defines, mul_mat_vec_layout, shader_float_dequant_func, mul_mat_vec_body))
  2322. elif i == GGML_TYPE_Q4_0:
  2323. stream.extend((shader_q4_0_defines, mul_mat_vec_layout, shader_q4_0_dequant_func, mul_mat_vec_body))
  2324. elif i == GGML_TYPE_Q4_1:
  2325. stream.extend((shader_q4_1_defines, mul_mat_vec_layout, shader_q4_1_dequant_func, mul_mat_vec_body))
  2326. elif i == GGML_TYPE_Q5_0:
  2327. stream.extend((shader_q5_0_defines, mul_mat_vec_layout, shader_q5_0_dequant_func, mul_mat_vec_body))
  2328. elif i == GGML_TYPE_Q5_1:
  2329. stream.extend((shader_q5_1_defines, mul_mat_vec_layout, shader_q5_1_dequant_func, mul_mat_vec_body))
  2330. elif i == GGML_TYPE_Q8_0:
  2331. stream.extend((shader_q8_0_defines, mul_mat_vec_layout, shader_q8_0_dequant_func, mul_mat_vec_body))
  2332. elif i == GGML_TYPE_Q2_K:
  2333. stream.extend((shader_q2_K_defines, mul_mat_vec_layout, mul_mat_vec_q2_K_body))
  2334. elif i == GGML_TYPE_Q3_K:
  2335. stream.extend((shader_q3_K_defines, mul_mat_vec_layout, mul_mat_vec_q3_K_body))
  2336. elif i == GGML_TYPE_Q4_K:
  2337. stream.extend((shader_q4_K_defines, mul_mat_vec_layout, mul_mat_vec_q4_K_body))
  2338. elif i == GGML_TYPE_Q5_K:
  2339. stream.extend((shader_q5_K_defines, mul_mat_vec_layout, mul_mat_vec_q5_K_body))
  2340. elif i == GGML_TYPE_Q6_K:
  2341. stream.extend((shader_q6_K_defines, mul_mat_vec_layout, mul_mat_vec_q6_K_body))
  2342. else:
  2343. continue
  2344. 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}))
  2345. 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}))
  2346. 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}))
  2347. # Dequant shaders
  2348. for i in range(0, VK_NUM_TYPES):
  2349. stream.clear()
  2350. stream.extend((dequant_head, shader_int8_ext, shader_f32))
  2351. if i == GGML_TYPE_F32:
  2352. stream.append(dequant_f32_body)
  2353. elif i == GGML_TYPE_Q4_0:
  2354. stream.extend((shader_q4_0_defines, dequant_q4_0_body))
  2355. elif i == GGML_TYPE_Q4_1:
  2356. stream.extend((shader_q4_1_defines, dequant_q4_1_body))
  2357. elif i == GGML_TYPE_Q5_0:
  2358. stream.extend((shader_q5_0_defines, dequant_q5_0_body))
  2359. elif i == GGML_TYPE_Q5_1:
  2360. stream.extend((shader_q5_1_defines, dequant_q5_1_body))
  2361. elif i == GGML_TYPE_Q8_0:
  2362. stream.extend((shader_q8_0_defines, dequant_q8_0_body))
  2363. elif i == GGML_TYPE_Q2_K:
  2364. stream.extend((shader_q2_K_defines, dequant_q2_K_body))
  2365. elif i == GGML_TYPE_Q3_K:
  2366. stream.extend((shader_q3_K_defines, dequant_q3_K_body))
  2367. elif i == GGML_TYPE_Q4_K:
  2368. stream.extend((shader_q4_K_defines, dequant_q4_K_body))
  2369. elif i == GGML_TYPE_Q5_K:
  2370. stream.extend((shader_q5_K_defines, dequant_q5_K_body))
  2371. elif i == GGML_TYPE_Q6_K:
  2372. stream.extend((shader_q6_K_defines, dequant_q6_K_body))
  2373. else:
  2374. continue
  2375. tasks.append(string_to_spv(f"dequant_{type_names[i]}", "".join(stream), {"D_TYPE": "float16_t"}))
  2376. # get_rows
  2377. for i in range(0, VK_NUM_TYPES):
  2378. stream.clear()
  2379. stream.extend((generic_binary_op_head, shader_int8_ext, shader_f32))
  2380. optimization_workaround = False
  2381. if i == GGML_TYPE_F32:
  2382. stream.extend((shader_f32_defines, generic_binary_op_layout, generic_binary_op_funcs, get_rows_float_body))
  2383. elif i == GGML_TYPE_F16:
  2384. stream.extend((shader_f16_defines, generic_binary_op_layout, generic_binary_op_funcs, get_rows_float_body))
  2385. optimization_workaround = True
  2386. elif i == GGML_TYPE_Q4_0:
  2387. stream.extend((shader_q4_0_defines, generic_binary_op_layout, shader_q4_0_dequant_func, generic_binary_op_funcs, get_rows_body))
  2388. elif i == GGML_TYPE_Q4_1:
  2389. stream.extend((shader_q4_1_defines, generic_binary_op_layout, shader_q4_1_dequant_func, generic_binary_op_funcs, get_rows_body))
  2390. elif i == GGML_TYPE_Q5_0:
  2391. stream.extend((shader_q5_0_defines, generic_binary_op_layout, shader_q5_0_dequant_func, generic_binary_op_funcs, get_rows_body))
  2392. elif i == GGML_TYPE_Q5_1:
  2393. stream.extend((shader_q5_1_defines, generic_binary_op_layout, shader_q5_1_dequant_func, generic_binary_op_funcs, get_rows_body))
  2394. elif i == GGML_TYPE_Q8_0:
  2395. stream.extend((shader_q8_0_defines, generic_binary_op_layout, shader_q8_0_dequant_func, generic_binary_op_funcs, get_rows_body))
  2396. else:
  2397. continue
  2398. if optimization_workaround:
  2399. tasks.append(string_to_spv(f"get_rows_{type_names[i]}", "".join(stream), {"B_TYPE": "int", "D_TYPE": "float16_t", "OPTIMIZATION_ERROR_WORKAROUND": "1"}))
  2400. else:
  2401. tasks.append(string_to_spv(f"get_rows_{type_names[i]}", "".join(stream), {"B_TYPE": "int", "D_TYPE": "float16_t"}))
  2402. tasks.append(string_to_spv(f"get_rows_{type_names[i]}_f32", "".join(stream), {"B_TYPE": "int", "D_TYPE": "float"}))
  2403. 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"}))
  2404. 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"}))
  2405. # Norms
  2406. tasks.append(string_to_spv("norm_f32", f"{generic_head}\n{shader_f32}\n{norm_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2407. tasks.append(string_to_spv("rms_norm_f32", f"{generic_head}\n{shader_f32}\n{rms_norm_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2408. tasks.append(string_to_spv("cpy_f32_f32", f"{generic_unary_op_combined}\n{cpy_end}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2409. tasks.append(string_to_spv("cpy_f32_f16", f"{generic_unary_op_combined}\n{cpy_end}", {"A_TYPE": "float", "D_TYPE": "float16_t"}))
  2410. 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"}))
  2411. 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"}))
  2412. tasks.append(string_to_spv("split_k_reduce", mulmat_split_k_reduce_src, {}))
  2413. 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"}))
  2414. 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"}))
  2415. tasks.append(string_to_spv("scale_f32", f"{generic_unary_op_combined}\n{scale_body}", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2416. tasks.append(string_to_spv("sqr_f32", f"{generic_unary_op_combined}\n{sqr_body}", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2417. tasks.append(string_to_spv("clamp_f32", f"{generic_unary_op_combined}\n{clamp_body}", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
  2418. tasks.append(string_to_spv("gelu_f32", f"{generic_head}\n{shader_f32}\n{gelu_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2419. tasks.append(string_to_spv("silu_f32", f"{generic_head}\n{shader_f32}\n{silu_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2420. tasks.append(string_to_spv("relu_f32", f"{generic_head}\n{shader_f32}\n{relu_body}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2421. 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"}))
  2422. 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"}))
  2423. 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"}))
  2424. tasks.append(string_to_spv("rope_f32", rope_src, {"A_TYPE": "float", "D_TYPE": "float"}))
  2425. tasks.append(string_to_spv("rope_f16", rope_src, {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
  2426. tasks.append(string_to_spv("rope_neox_f32", rope_neox_src, {"A_TYPE": "float", "D_TYPE": "float"}))
  2427. tasks.append(string_to_spv("rope_neox_f16", rope_neox_src, {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
  2428. tasks.append(string_to_spv("argsort_f32", argsort_src, {"A_TYPE": "float"}))
  2429. tasks.append(string_to_spv("sum_rows_f32", f"{generic_head}\n{shader_f32}\n{sum_rows_src}", {"A_TYPE": "float", "D_TYPE": "float"}))
  2430. # Helper to decorate tasks with semaphore acquisition.
  2431. async def withSemaphore(sem, task):
  2432. async with sem:
  2433. return await task
  2434. # Run tasks concurrently guarded by a concurrency limit.
  2435. sem = asyncio.Semaphore(ASYNCIO_CONCURRENCY)
  2436. await asyncio.gather(*(withSemaphore(sem, task) for task in tasks))
  2437. with open("ggml-vulkan-shaders.hpp", "w") as f:
  2438. f.write("#include <cstdint>\n\n")
  2439. for name, path in sorted(shader_fnames):
  2440. with open(path, "rb") as spv:
  2441. counter = 0
  2442. newline_counter = 0
  2443. f.write(f"unsigned char {name}_data[] = {{\n")
  2444. for val in spv.read():
  2445. f.write(f"0x{val:02x},")
  2446. newline_counter += 1
  2447. counter += 1
  2448. if newline_counter >= 12:
  2449. newline_counter = 0
  2450. f.write("\n")
  2451. f.write("\n};\n")
  2452. f.write(f"const uint64_t {name}_len = {counter};\n\n")
  2453. os.remove(path)
  2454. if __name__ == "__main__":
  2455. parser = argparse.ArgumentParser(description="GGML Vulkan Shader Generator")
  2456. parser.add_argument("--glslc", help="Path to glslc")
  2457. parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
  2458. args = parser.parse_args()
  2459. logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
  2460. if args.glslc:
  2461. GLSLC = args.glslc
  2462. asyncio.run(main())