ggml-cuda.cu 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915
  1. /**
  2. * llama.cpp - git e782c9e735f93ab4767ffc37462c523b73a17ddc
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023 Georgi Gerganov
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <limits>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <atomic>
  32. #include <assert.h>
  33. #include <cuda_runtime.h>
  34. #include <cublas_v2.h>
  35. #include <cuda_fp16.h>
  36. #include "ggml-cuda.h"
  37. #include "ggml.h"
  38. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  39. #if defined(_MSC_VER)
  40. #pragma warning(disable: 4244 4267) // possible loss of data
  41. #endif
  42. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  43. #define CUDA_CHECK(err) \
  44. do { \
  45. cudaError_t err_ = (err); \
  46. if (err_ != cudaSuccess) { \
  47. fprintf(stderr, "CUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  48. cudaGetErrorString(err_)); \
  49. exit(1); \
  50. } \
  51. } while (0)
  52. #if CUDART_VERSION >= 12000
  53. #define CUBLAS_CHECK(err) \
  54. do { \
  55. cublasStatus_t err_ = (err); \
  56. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  57. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  58. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  59. exit(1); \
  60. } \
  61. } while (0)
  62. #else
  63. #define CUBLAS_CHECK(err) \
  64. do { \
  65. cublasStatus_t err_ = (err); \
  66. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  67. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  68. exit(1); \
  69. } \
  70. } while (0)
  71. #endif // CUDART_VERSION >= 11
  72. #ifdef GGML_CUDA_DMMV_F16
  73. typedef half dfloat; // dequantize float
  74. typedef half2 dfloat2;
  75. #else
  76. typedef float dfloat; // dequantize float
  77. typedef float2 dfloat2;
  78. #endif //GGML_CUDA_DMMV_F16
  79. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  80. typedef void (*to_fp32_cuda_t)(const void * __restrict__ x, float * __restrict__ y, int k, cudaStream_t stream);
  81. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  82. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  83. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  84. typedef void (*ggml_cuda_op_t)(
  85. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i, float * src0_ddf_i,
  86. float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  87. cudaStream_t & cudaStream_main);
  88. // QK = number of values after dequantization
  89. // QR = QK / number of values before dequantization
  90. // QI = number of 32 bit integers before dequantization
  91. #define QK4_0 32
  92. #define QR4_0 2
  93. #define QI4_0 (QK4_0 / (4 * QR4_0))
  94. typedef struct {
  95. half d; // delta
  96. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  97. } block_q4_0;
  98. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  99. #define QK4_1 32
  100. #define QR4_1 2
  101. #define QI4_1 (QK4_1 / (4 * QR4_1))
  102. typedef struct {
  103. half d; // delta
  104. half m; // min
  105. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  106. } block_q4_1;
  107. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  108. #define QK5_0 32
  109. #define QR5_0 2
  110. #define QI5_0 (QK5_0 / (4 * QR5_0))
  111. typedef struct {
  112. half d; // delta
  113. uint8_t qh[4]; // 5-th bit of quants
  114. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  115. } block_q5_0;
  116. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  117. #define QK5_1 32
  118. #define QR5_1 2
  119. #define QI5_1 (QK5_1 / (4 * QR5_1))
  120. typedef struct {
  121. half d; // delta
  122. half m; // min
  123. uint8_t qh[4]; // 5-th bit of quants
  124. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  125. } block_q5_1;
  126. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  127. #define QK8_0 32
  128. #define QR8_0 1
  129. #define QI8_0 (QK8_0 / (4 * QR8_0))
  130. typedef struct {
  131. half d; // delta
  132. int8_t qs[QK8_0]; // quants
  133. } block_q8_0;
  134. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  135. #define QK8_1 32
  136. #define QR8_1 1
  137. #define QI8_1 (QK8_1 / (4 * QR8_1))
  138. typedef struct {
  139. half d; // delta
  140. half s; // unquantized sum
  141. int8_t qs[QK8_0]; // quants
  142. } block_q8_1;
  143. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  144. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs);
  145. //================================= k-quants
  146. #ifdef GGML_QKK_64
  147. #define QK_K 64
  148. #define K_SCALE_SIZE 4
  149. #else
  150. #define QK_K 256
  151. #define K_SCALE_SIZE 12
  152. #endif
  153. #define QR2_K 4
  154. #define QI2_K (QK_K / (4*QR2_K))
  155. typedef struct {
  156. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  157. uint8_t qs[QK_K/4]; // quants
  158. half d; // super-block scale for quantized scales
  159. half dmin; // super-block scale for quantized mins
  160. } block_q2_K;
  161. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  162. #define QR3_K 4
  163. #define QI3_K (QK_K / (4*QR3_K))
  164. typedef struct {
  165. uint8_t hmask[QK_K/8]; // quants - high bit
  166. uint8_t qs[QK_K/4]; // quants - low 2 bits
  167. #ifdef GGML_QKK_64
  168. uint8_t scales[2]; // scales, quantized with 8 bits
  169. #else
  170. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  171. #endif
  172. half d; // super-block scale
  173. } block_q3_K;
  174. //static_assert(sizeof(block_q3_K) == sizeof(ggml_fp16_t) + QK_K / 4 + QK_K / 8 + K_SCALE_SIZE, "wrong q3_K block size/padding");
  175. #define QR4_K 2
  176. #define QI4_K (QK_K / (4*QR4_K))
  177. #ifdef GGML_QKK_64
  178. typedef struct {
  179. half d[2]; // super-block scales/mins
  180. uint8_t scales[2]; // 4-bit block scales/mins
  181. uint8_t qs[QK_K/2]; // 4--bit quants
  182. } block_q4_K;
  183. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + QK_K/2 + 2, "wrong q4_K block size/padding");
  184. #else
  185. typedef struct {
  186. half d; // super-block scale for quantized scales
  187. half dmin; // super-block scale for quantized mins
  188. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  189. uint8_t qs[QK_K/2]; // 4--bit quants
  190. } block_q4_K;
  191. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  192. #endif
  193. #define QR5_K 2
  194. #define QI5_K (QK_K / (4*QR5_K))
  195. #ifdef GGML_QKK_64
  196. typedef struct {
  197. half d; // super-block scale
  198. int8_t scales[QK_K/16]; // block scales
  199. uint8_t qh[QK_K/8]; // quants, high bit
  200. uint8_t qs[QK_K/2]; // quants, low 4 bits
  201. } block_q5_K;
  202. static_assert(sizeof(block_q5_K) == sizeof(ggml_fp16_t) + QK_K/2 + QK_K/8 + QK_K/16, "wrong q5_K block size/padding");
  203. #else
  204. typedef struct {
  205. half d; // super-block scale for quantized scales
  206. half dmin; // super-block scale for quantized mins
  207. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  208. uint8_t qh[QK_K/8]; // quants, high bit
  209. uint8_t qs[QK_K/2]; // quants, low 4 bits
  210. } block_q5_K;
  211. static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_fp16_t) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");
  212. #endif
  213. #define QR6_K 2
  214. #define QI6_K (QK_K / (4*QR6_K))
  215. typedef struct {
  216. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  217. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  218. int8_t scales[QK_K/16]; // scales
  219. half d; // delta
  220. } block_q6_K;
  221. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  222. #define WARP_SIZE 32
  223. #define MATRIX_ROW_PADDING 256 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  224. #define CUDA_ADD_BLOCK_SIZE 256
  225. #define CUDA_MUL_BLOCK_SIZE 256
  226. #define CUDA_GELU_BLOCK_SIZE 256
  227. #define CUDA_SILU_BLOCK_SIZE 256
  228. #define CUDA_CPY_BLOCK_SIZE 32
  229. #define CUDA_SCALE_BLOCK_SIZE 256
  230. #define CUDA_ROPE_BLOCK_SIZE 256
  231. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  232. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  233. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  234. // dmmv = dequantize_mul_mat_vec
  235. #ifndef GGML_CUDA_DMMV_X
  236. #define GGML_CUDA_DMMV_X 32
  237. #endif
  238. #ifndef GGML_CUDA_MMV_Y
  239. #define GGML_CUDA_MMV_Y 1
  240. #endif
  241. #ifndef K_QUANTS_PER_ITERATION
  242. #define K_QUANTS_PER_ITERATION 2
  243. #else
  244. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  245. #endif
  246. struct ggml_tensor_extra_gpu {
  247. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  248. cudaEvent_t events[GGML_CUDA_MAX_DEVICES]; // events for synchronizing multiple GPUs
  249. };
  250. static __global__ void add_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  251. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  252. if (i >= kx) {
  253. return;
  254. }
  255. dst[i] = x[i] + y[i%ky];
  256. }
  257. static __global__ void add_f16_f32_f16(const half * x, const float * y, half * dst, const int k) {
  258. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  259. if (i >= k) {
  260. return;
  261. }
  262. dst[i] = __hadd(x[i], __float2half(y[i]));
  263. }
  264. static __global__ void mul_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  265. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  266. if (i >= kx) {
  267. return;
  268. }
  269. dst[i] = x[i] * y[i%ky];
  270. }
  271. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  272. const float GELU_COEF_A = 0.044715f;
  273. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  274. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  275. if (i >= k) {
  276. return;
  277. }
  278. float xi = x[i];
  279. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  280. }
  281. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  282. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  283. if (i >= k) {
  284. return;
  285. }
  286. dst[i] = x[i] / (1.0f + expf(-x[i]));
  287. }
  288. static __global__ void norm_f32(const float * x, float * dst, const int ncols) {
  289. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  290. const int tid = threadIdx.x;
  291. const float eps = 1e-5f;
  292. float mean = 0.0f;
  293. float var = 0.0f;
  294. for (int col = tid; col < ncols; col += WARP_SIZE) {
  295. const float xi = x[row*ncols + col];
  296. mean += xi;
  297. var += xi * xi;
  298. }
  299. // sum up partial sums
  300. #pragma unroll
  301. for (int mask = 16; mask > 0; mask >>= 1) {
  302. mean += __shfl_xor_sync(0xffffffff, mean, mask, 32);
  303. var += __shfl_xor_sync(0xffffffff, var, mask, 32);
  304. }
  305. mean /= ncols;
  306. var = var / ncols - mean * mean;
  307. const float inv_var = rsqrtf(var + eps);
  308. for (int col = tid; col < ncols; col += WARP_SIZE) {
  309. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_var;
  310. }
  311. }
  312. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols) {
  313. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  314. const int tid = threadIdx.x;
  315. const float eps = 1e-6f;
  316. float tmp = 0.0f; // partial sum for thread in warp
  317. for (int col = tid; col < ncols; col += WARP_SIZE) {
  318. const float xi = x[row*ncols + col];
  319. tmp += xi * xi;
  320. }
  321. // sum up partial sums
  322. #pragma unroll
  323. for (int mask = 16; mask > 0; mask >>= 1) {
  324. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  325. }
  326. const float mean = tmp / ncols;
  327. const float scale = rsqrtf(mean + eps);
  328. for (int col = tid; col < ncols; col += WARP_SIZE) {
  329. dst[row*ncols + col] = scale * x[row*ncols + col];
  330. }
  331. }
  332. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  333. const block_q4_0 * x = (const block_q4_0 *) vx;
  334. const dfloat d = x[ib].d;
  335. const int vui = x[ib].qs[iqs];
  336. v.x = vui & 0xF;
  337. v.y = vui >> 4;
  338. #ifdef GGML_CUDA_DMMV_F16
  339. v = __hsub2(v, {8.0f, 8.0f});
  340. v = __hmul2(v, {d, d});
  341. #else
  342. v.x = (v.x - 8.0f) * d;
  343. v.y = (v.y - 8.0f) * d;
  344. #endif // GGML_CUDA_DMMV_F16
  345. }
  346. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  347. const block_q4_1 * x = (const block_q4_1 *) vx;
  348. const dfloat d = x[ib].d;
  349. const dfloat m = x[ib].m;
  350. const int vui = x[ib].qs[iqs];
  351. v.x = vui & 0xF;
  352. v.y = vui >> 4;
  353. #ifdef GGML_CUDA_DMMV_F16
  354. v = __hmul2(v, {d, d});
  355. v = __hadd2(v, {m, m});
  356. #else
  357. v.x = (v.x * d) + m;
  358. v.y = (v.y * d) + m;
  359. #endif // GGML_CUDA_DMMV_F16
  360. }
  361. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  362. const block_q5_0 * x = (const block_q5_0 *) vx;
  363. const dfloat d = x[ib].d;
  364. uint32_t qh;
  365. memcpy(&qh, x[ib].qh, sizeof(qh));
  366. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  367. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  368. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  369. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  370. #ifdef GGML_CUDA_DMMV_F16
  371. v = __hsub2(v, {16.0f, 16.0f});
  372. v = __hmul2(v, {d, d});
  373. #else
  374. v.x = (v.x - 16.0f) * d;
  375. v.y = (v.y - 16.0f) * d;
  376. #endif // GGML_CUDA_DMMV_F16
  377. }
  378. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  379. const block_q5_1 * x = (const block_q5_1 *) vx;
  380. const dfloat d = x[ib].d;
  381. const dfloat m = x[ib].m;
  382. uint32_t qh;
  383. memcpy(&qh, x[ib].qh, sizeof(qh));
  384. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  385. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  386. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  387. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  388. #ifdef GGML_CUDA_DMMV_F16
  389. v = __hmul2(v, {d, d});
  390. v = __hadd2(v, {m, m});
  391. #else
  392. v.x = (v.x * d) + m;
  393. v.y = (v.y * d) + m;
  394. #endif // GGML_CUDA_DMMV_F16
  395. }
  396. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  397. const block_q8_0 * x = (const block_q8_0 *) vx;
  398. const dfloat d = x[ib].d;
  399. v.x = x[ib].qs[iqs + 0];
  400. v.y = x[ib].qs[iqs + 1];
  401. #ifdef GGML_CUDA_DMMV_F16
  402. v = __hmul2(v, {d, d});
  403. #else
  404. v.x *= d;
  405. v.y *= d;
  406. #endif // GGML_CUDA_DMMV_F16
  407. }
  408. //================================== k-quants
  409. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, float * __restrict__ yy) {
  410. const int i = blockIdx.x;
  411. const block_q2_K * x = (const block_q2_K *) vx;
  412. const int tid = threadIdx.x;
  413. #if QK_K == 256
  414. const int n = tid/32;
  415. const int l = tid - 32*n;
  416. const int is = 8*n + l/16;
  417. const uint8_t q = x[i].qs[32*n + l];
  418. float * y = yy + i*QK_K + 128*n;
  419. float dall = x[i].d;
  420. float dmin = x[i].dmin;
  421. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  422. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  423. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  424. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  425. #else
  426. const int is = tid/16; // 0 or 1
  427. const int il = tid%16; // 0...15
  428. const uint8_t q = x[i].qs[il] >> (2*is);
  429. float * y = yy + i*QK_K + 16*is + il;
  430. float dall = x[i].d;
  431. float dmin = x[i].dmin;
  432. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  433. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  434. #endif
  435. }
  436. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, float * __restrict__ yy) {
  437. const int i = blockIdx.x;
  438. const block_q3_K * x = (const block_q3_K *) vx;
  439. #if QK_K == 256
  440. const int r = threadIdx.x/4;
  441. const int tid = r/2;
  442. const int is0 = r%2;
  443. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  444. const int n = tid / 4;
  445. const int j = tid - 4*n;
  446. uint8_t m = 1 << (4*n + j);
  447. int is = 8*n + 2*j + is0;
  448. int shift = 2*j;
  449. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  450. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  451. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  452. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  453. float d_all = x[i].d;
  454. float dl = d_all * (us - 32);
  455. float * y = yy + i*QK_K + 128*n + 32*j;
  456. const uint8_t * q = x[i].qs + 32*n;
  457. const uint8_t * hm = x[i].hmask;
  458. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  459. #else
  460. const int tid = threadIdx.x;
  461. const int is = tid/16; // 0 or 1
  462. const int il = tid%16; // 0...15
  463. const int im = il/8; // 0...1
  464. const int in = il%8; // 0...7
  465. float * y = yy + i*QK_K + 16*is + il;
  466. const uint8_t q = x[i].qs[il] >> (2*is);
  467. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  468. const float d = (float)x[i].d;
  469. if (is == 0) {
  470. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  471. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  472. } else {
  473. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  474. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  475. }
  476. #endif
  477. }
  478. #if QK_K == 256
  479. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  480. if (j < 4) {
  481. d = q[j] & 63; m = q[j + 4] & 63;
  482. } else {
  483. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  484. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  485. }
  486. }
  487. #endif
  488. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, float * __restrict__ yy) {
  489. const block_q4_K * x = (const block_q4_K *) vx;
  490. const int i = blockIdx.x;
  491. #if QK_K == 256
  492. // assume 32 threads
  493. const int tid = threadIdx.x;
  494. const int il = tid/8;
  495. const int ir = tid%8;
  496. const int is = 2*il;
  497. const int n = 4;
  498. float * y = yy + i*QK_K + 64*il + n*ir;
  499. const float dall = x[i].d;
  500. const float dmin = x[i].dmin;
  501. const uint8_t * q = x[i].qs + 32*il + n*ir;
  502. uint8_t sc, m;
  503. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  504. const float d1 = dall * sc; const float m1 = dmin * m;
  505. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  506. const float d2 = dall * sc; const float m2 = dmin * m;
  507. for (int l = 0; l < n; ++l) {
  508. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  509. y[l +32] = d2 * (q[l] >> 4) - m2;
  510. }
  511. #else
  512. const int tid = threadIdx.x;
  513. const uint8_t * q = x[i].qs;
  514. float * y = yy + i*QK_K;
  515. const float d = (float)x[i].d[0];
  516. const float m = (float)x[i].d[1];
  517. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  518. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  519. #endif
  520. }
  521. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, float * __restrict__ yy) {
  522. const block_q5_K * x = (const block_q5_K *) vx;
  523. const int i = blockIdx.x;
  524. #if QK_K == 256
  525. // assume 64 threads - this is very slightly better than the one below
  526. const int tid = threadIdx.x;
  527. const int il = tid/16; // il is in 0...3
  528. const int ir = tid%16; // ir is in 0...15
  529. const int is = 2*il; // is is in 0...6
  530. float * y = yy + i*QK_K + 64*il + 2*ir;
  531. const float dall = x[i].d;
  532. const float dmin = x[i].dmin;
  533. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  534. const uint8_t * qh = x[i].qh + 2*ir;
  535. uint8_t sc, m;
  536. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  537. const float d1 = dall * sc; const float m1 = dmin * m;
  538. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  539. const float d2 = dall * sc; const float m2 = dmin * m;
  540. uint8_t hm = 1 << (2*il);
  541. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  542. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  543. hm <<= 1;
  544. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  545. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  546. #else
  547. const int tid = threadIdx.x;
  548. const uint8_t q = x[i].qs[tid];
  549. const int im = tid/8; // 0...3
  550. const int in = tid%8; // 0...7
  551. const int is = tid/16; // 0 or 1
  552. const uint8_t h = x[i].qh[in] >> im;
  553. const float d = x[i].d;
  554. float * y = yy + i*QK_K + tid;
  555. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  556. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  557. #endif
  558. }
  559. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, float * __restrict__ yy) {
  560. const block_q6_K * x = (const block_q6_K *) vx;
  561. const int i = blockIdx.x;
  562. #if QK_K == 256
  563. // assume 64 threads - this is very slightly better than the one below
  564. const int tid = threadIdx.x;
  565. const int ip = tid/32; // ip is 0 or 1
  566. const int il = tid - 32*ip; // 0...32
  567. const int is = 8*ip + il/16;
  568. float * y = yy + i*QK_K + 128*ip + il;
  569. const float d = x[i].d;
  570. const uint8_t * ql = x[i].ql + 64*ip + il;
  571. const uint8_t qh = x[i].qh[32*ip + il];
  572. const int8_t * sc = x[i].scales + is;
  573. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  574. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  575. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  576. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  577. #else
  578. // assume 32 threads
  579. const int tid = threadIdx.x;
  580. const int ip = tid/16; // 0 or 1
  581. const int il = tid - 16*ip; // 0...15
  582. float * y = yy + i*QK_K + 16*ip + il;
  583. const float d = x[i].d;
  584. const uint8_t ql = x[i].ql[16*ip + il];
  585. const uint8_t qh = x[i].qh[il] >> (2*ip);
  586. const int8_t * sc = x[i].scales;
  587. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  588. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  589. #endif
  590. }
  591. static __global__ void dequantize_mul_mat_vec_q2_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  592. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  593. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  594. if (row > nrows) return;
  595. const int num_blocks_per_row = ncols / QK_K;
  596. const int ib0 = row*num_blocks_per_row;
  597. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  598. float tmp = 0; // partial sum for thread in warp
  599. #if QK_K == 256
  600. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  601. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  602. const int step = 16/K_QUANTS_PER_ITERATION;
  603. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  604. const int in = tid - step*im; // 0...15 or 0...7
  605. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  606. const int q_offset = 32*im + l0;
  607. const int s_offset = 8*im;
  608. const int y_offset = 128*im + l0;
  609. uint32_t aux[4];
  610. const uint8_t * d = (const uint8_t *)aux;
  611. const uint8_t * m = (const uint8_t *)(aux + 2);
  612. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  613. const float * y = yy + i * QK_K + y_offset;
  614. const uint8_t * q = x[i].qs + q_offset;
  615. const float dall = x[i].d;
  616. const float dmin = x[i].dmin;
  617. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  618. aux[0] = a[0] & 0x0f0f0f0f;
  619. aux[1] = a[1] & 0x0f0f0f0f;
  620. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  621. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  622. float sum1 = 0, sum2 = 0;
  623. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  624. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  625. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  626. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  627. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  628. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  629. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  630. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  631. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  632. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  633. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  634. }
  635. tmp += dall * sum1 - dmin * sum2;
  636. }
  637. #else
  638. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  639. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  640. const int offset = tid * K_QUANTS_PER_ITERATION;
  641. uint32_t uaux[2];
  642. const uint8_t * d = (const uint8_t *)uaux;
  643. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  644. const float * y = yy + i * QK_K + offset;
  645. const uint8_t * q = x[i].qs + offset;
  646. const uint32_t * s = (const uint32_t *)x[i].scales;
  647. uaux[0] = s[0] & 0x0f0f0f0f;
  648. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  649. const half2 * dh = (const half2 *)&x[i].d;
  650. const float2 dall = __half22float2(dh[0]);
  651. float sum1 = 0, sum2 = 0;
  652. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  653. const uint8_t ql = q[l];
  654. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  655. + y[l+16] * d[1] * ((ql >> 2) & 3)
  656. + y[l+32] * d[2] * ((ql >> 4) & 3)
  657. + y[l+48] * d[3] * ((ql >> 6) & 3);
  658. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  659. }
  660. tmp += dall.x * sum1 - dall.y * sum2;
  661. }
  662. #endif
  663. // sum up partial sums and write back result
  664. #pragma unroll
  665. for (int mask = 16; mask > 0; mask >>= 1) {
  666. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  667. }
  668. if (threadIdx.x == 0) {
  669. dst[row] = tmp;
  670. }
  671. }
  672. static __global__ void dequantize_mul_mat_vec_q3_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  673. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  674. if (row > nrows) return;
  675. const int num_blocks_per_row = ncols / QK_K;
  676. const int ib0 = row*num_blocks_per_row;
  677. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  678. float tmp = 0; // partial sum for thread in warp
  679. #if QK_K == 256
  680. const uint16_t kmask1 = 0x0303;
  681. const uint16_t kmask2 = 0x0f0f;
  682. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  683. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  684. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  685. const int step = 16/K_QUANTS_PER_ITERATION;
  686. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  687. const int in = tid - step*im; // 0....15 or 0...7
  688. const uint8_t m = 1 << (4*im);
  689. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  690. const int q_offset = 32*im + l0;
  691. const int y_offset = 128*im + l0;
  692. uint16_t utmp[4];
  693. const int8_t * s = (const int8_t *)utmp;
  694. const uint16_t s_shift = 4*im;
  695. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  696. const float * y = yy + i * QK_K + y_offset;
  697. const uint8_t * q = x[i].qs + q_offset;
  698. const uint8_t * h = x[i].hmask + l0;
  699. const uint16_t * a = (const uint16_t *)x[i].scales;
  700. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  701. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  702. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  703. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  704. const float d = x[i].d;
  705. float sum = 0;
  706. for (int l = 0; l < n; ++l) {
  707. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  708. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  709. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  710. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  711. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  712. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  713. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  714. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  715. }
  716. tmp += d * sum;
  717. }
  718. #else
  719. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  720. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  721. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  722. const int in = offset/8; // 0 or 1
  723. const int im = offset%8; // 0...7
  724. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  725. const float * y = yy + i * QK_K + offset;
  726. const uint8_t * q = x[i].qs + offset;
  727. const uint8_t * s = x[i].scales;
  728. const float dall = (float)x[i].d;
  729. float sum = 0;
  730. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  731. const uint8_t hl = x[i].hmask[im+l] >> in;
  732. const uint8_t ql = q[l];
  733. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  734. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  735. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  736. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  737. }
  738. tmp += sum;
  739. }
  740. #endif
  741. // sum up partial sums and write back result
  742. #pragma unroll
  743. for (int mask = 16; mask > 0; mask >>= 1) {
  744. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  745. }
  746. if (threadIdx.x == 0) {
  747. dst[row] = tmp;
  748. }
  749. }
  750. static __global__ void dequantize_mul_mat_vec_q4_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  751. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  752. if (row > nrows) return;
  753. const int num_blocks_per_row = ncols / QK_K;
  754. const int ib0 = row*num_blocks_per_row;
  755. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  756. #if QK_K == 256
  757. const uint16_t kmask1 = 0x3f3f;
  758. const uint16_t kmask2 = 0x0f0f;
  759. const uint16_t kmask3 = 0xc0c0;
  760. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  761. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  762. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  763. const int il = tid/step; // 0...3
  764. const int ir = tid - step*il; // 0...7 or 0...3
  765. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  766. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  767. const int in = il%2;
  768. const int l0 = n*(2*ir + in);
  769. const int q_offset = 32*im + l0;
  770. const int y_offset = 64*im + l0;
  771. uint16_t aux[4];
  772. const uint8_t * sc = (const uint8_t *)aux;
  773. float tmp = 0; // partial sum for thread in warp
  774. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  775. const uint8_t * q1 = x[i].qs + q_offset;
  776. const uint8_t * q2 = q1 + 64;
  777. const float * y1 = yy + i*QK_K + y_offset;
  778. const float * y2 = y1 + 128;
  779. const float dall = x[i].d;
  780. const float dmin = x[i].dmin;
  781. const uint16_t * a = (const uint16_t *)x[i].scales;
  782. aux[0] = a[im+0] & kmask1;
  783. aux[1] = a[im+2] & kmask1;
  784. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  785. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  786. float4 s = {0.f, 0.f, 0.f, 0.f};
  787. float smin = 0;
  788. for (int l = 0; l < n; ++l) {
  789. s.x += y1[l] * (q1[l] & 0xF); s.y += y1[l+32] * (q1[l] >> 4);
  790. s.z += y2[l] * (q2[l] & 0xF); s.w += y2[l+32] * (q2[l] >> 4);
  791. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  792. }
  793. tmp += dall * (s.x * sc[0] + s.y * sc[1] + s.z * sc[4] + s.w * sc[5]) - dmin * smin;
  794. }
  795. #else
  796. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  797. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  798. const int step = tid * K_QUANTS_PER_ITERATION;
  799. uint16_t aux16[2];
  800. const uint8_t * s = (const uint8_t *)aux16;
  801. float tmp = 0;
  802. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  803. const uint8_t * q = x[i].qs + step;
  804. const float * y = yy + i*QK_K + step;
  805. const uint16_t * a = (const uint16_t *)x[i].scales;
  806. aux16[0] = a[0] & 0x0f0f;
  807. aux16[1] = (a[0] >> 4) & 0x0f0f;
  808. const float d = (float)x[i].d[0];
  809. const float m = (float)x[i].d[1];
  810. float sum = 0.f;
  811. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  812. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  813. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  814. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  815. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  816. }
  817. tmp += sum;
  818. }
  819. #endif
  820. // sum up partial sums and write back result
  821. #pragma unroll
  822. for (int mask = 16; mask > 0; mask >>= 1) {
  823. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  824. }
  825. if (tid == 0) {
  826. dst[row] = tmp;
  827. }
  828. }
  829. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  830. const int row = blockIdx.x;
  831. const int num_blocks_per_row = ncols / QK_K;
  832. const int ib0 = row*num_blocks_per_row;
  833. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  834. float tmp = 0; // partial sum for thread in warp
  835. #if QK_K == 256
  836. const uint16_t kmask1 = 0x3f3f;
  837. const uint16_t kmask2 = 0x0f0f;
  838. const uint16_t kmask3 = 0xc0c0;
  839. const int tid = threadIdx.x/2; // 0...15
  840. const int ix = threadIdx.x%2;
  841. const int il = tid/4; // 0...3
  842. const int ir = tid - 4*il;// 0...3
  843. const int n = 2;
  844. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  845. const int in = il%2;
  846. const int l0 = n*(2*ir + in);
  847. const int q_offset = 32*im + l0;
  848. const int y_offset = 64*im + l0;
  849. const uint8_t hm1 = 1 << (2*im);
  850. const uint8_t hm2 = hm1 << 4;
  851. uint16_t aux[4];
  852. const uint8_t * sc = (const uint8_t *)aux;
  853. for (int i = ix; i < num_blocks_per_row; i += 2) {
  854. const uint8_t * ql1 = x[i].qs + q_offset;
  855. const uint8_t * ql2 = ql1 + 64;
  856. const uint8_t * qh = x[i].qh + l0;
  857. const float * y1 = yy + i*QK_K + y_offset;
  858. const float * y2 = y1 + 128;
  859. const float dall = x[i].d;
  860. const float dmin = x[i].dmin;
  861. const uint16_t * a = (const uint16_t *)x[i].scales;
  862. aux[0] = a[im+0] & kmask1;
  863. aux[1] = a[im+2] & kmask1;
  864. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  865. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  866. float4 sum = {0.f, 0.f, 0.f, 0.f};
  867. float smin = 0;
  868. for (int l = 0; l < n; ++l) {
  869. sum.x += y1[l+ 0] * ((ql1[l+ 0] & 0xF) + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  870. + y1[l+16] * ((ql1[l+16] & 0xF) + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  871. sum.y += y1[l+32] * ((ql1[l+ 0] >> 4) + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  872. + y1[l+48] * ((ql1[l+16] >> 4) + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  873. sum.z += y2[l+ 0] * ((ql2[l+ 0] & 0xF) + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  874. + y2[l+16] * ((ql2[l+16] & 0xF) + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  875. sum.w += y2[l+32] * ((ql2[l+ 0] >> 4) + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  876. + y2[l+48] * ((ql2[l+16] >> 4) + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  877. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  878. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  879. }
  880. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  881. }
  882. #else
  883. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  884. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  885. const int step = tid * K_QUANTS_PER_ITERATION;
  886. const int im = step/8;
  887. const int in = step%8;
  888. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  889. const uint8_t * q = x[i].qs + step;
  890. const int8_t * s = x[i].scales;
  891. const float * y = yy + i*QK_K + step;
  892. const float d = x[i].d;
  893. float sum = 0.f;
  894. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  895. const uint8_t h = x[i].qh[in+j] >> im;
  896. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  897. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  898. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  899. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  900. }
  901. tmp += sum;
  902. }
  903. #endif
  904. // sum up partial sums and write back result
  905. #pragma unroll
  906. for (int mask = 16; mask > 0; mask >>= 1) {
  907. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  908. }
  909. if (threadIdx.x == 0) {
  910. dst[row] = tmp;
  911. }
  912. }
  913. static __global__ void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  914. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  915. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  916. if (row > nrows) return;
  917. const int num_blocks_per_row = ncols / QK_K;
  918. const int ib0 = row*num_blocks_per_row;
  919. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  920. #if QK_K == 256
  921. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  922. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  923. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  924. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  925. const int in = tid - step*im; // 0...15 or 0...7
  926. #if K_QUANTS_PER_ITERATION == 1
  927. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  928. const int is = 0;
  929. #else
  930. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  931. const int is = in / 4;
  932. #endif
  933. const int ql_offset = 64*im + l0;
  934. const int qh_offset = 32*im + l0;
  935. const int s_offset = 8*im + is;
  936. const int y_offset = 128*im + l0;
  937. float tmp = 0; // partial sum for thread in warp
  938. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  939. const float * y = yy + i * QK_K + y_offset;
  940. const uint8_t * ql = x[i].ql + ql_offset;
  941. const uint8_t * qh = x[i].qh + qh_offset;
  942. const int8_t * s = x[i].scales + s_offset;
  943. const float d = x[i].d;
  944. #if K_QUANTS_PER_ITERATION == 1
  945. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  946. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  947. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  948. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  949. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  950. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  951. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  952. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  953. tmp += sum;
  954. #else
  955. float sum = 0;
  956. for (int l = 0; l < 4; ++l) {
  957. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  958. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  959. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  960. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  961. }
  962. tmp += sum;
  963. #endif
  964. }
  965. #else
  966. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  967. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  968. const int step = tid * K_QUANTS_PER_ITERATION;
  969. float tmp = 0; // partial sum for thread in warp
  970. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  971. const float * y = yy + i * QK_K + step;
  972. const uint8_t * ql = x[i].ql + step;
  973. const uint8_t * qh = x[i].qh + step;
  974. const int8_t * s = x[i].scales;
  975. const float d = x[i+0].d;
  976. float sum = 0;
  977. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  978. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  979. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  980. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  981. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  982. }
  983. tmp += sum;
  984. }
  985. #endif
  986. // sum up partial sums and write back result
  987. #pragma unroll
  988. for (int mask = 16; mask > 0; mask >>= 1) {
  989. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  990. }
  991. if (tid == 0) {
  992. dst[row] = tmp;
  993. }
  994. }
  995. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  996. const half * x = (const half *) vx;
  997. // automatic half -> float type cast if dfloat == float
  998. v.x = x[ib + iqs + 0];
  999. v.y = x[ib + iqs + 1];
  1000. }
  1001. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int ndata, const int k) {
  1002. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1003. if (i >= k) {
  1004. return;
  1005. }
  1006. block_q8_1 * y = (block_q8_1 *) vy;
  1007. const int ib = i / QK8_1; // block index
  1008. const int iqs = i % QK8_1; // quant index
  1009. const float xi = i < ndata ? x[i] : 0.0f;
  1010. float amax = fabsf(xi);
  1011. float sum = xi;
  1012. #pragma unroll
  1013. for (int mask = 16; mask > 0; mask >>= 1) {
  1014. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1015. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1016. }
  1017. const float d = amax / 127;
  1018. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1019. y[ib].qs[iqs] = q;
  1020. if (iqs > 0) {
  1021. return;
  1022. }
  1023. y[ib].d = d;
  1024. y[ib].s = sum;
  1025. }
  1026. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  1027. static __global__ void dequantize_block(const void * __restrict__ vx, float * __restrict__ y, const int k) {
  1028. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1029. if (i >= k) {
  1030. return;
  1031. }
  1032. const int ib = i/qk; // block index
  1033. const int iqs = (i%qk)/qr; // quant index
  1034. const int iybs = i - i%qk; // y block start index
  1035. const int y_offset = qr == 1 ? 1 : qk/2;
  1036. // dequantize
  1037. dfloat2 v;
  1038. dequantize_kernel(vx, ib, iqs, v);
  1039. y[iybs + iqs + 0] = v.x;
  1040. y[iybs + iqs + y_offset] = v.y;
  1041. }
  1042. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  1043. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1044. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1045. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  1046. int vi;
  1047. memcpy(&vi, &bq4_0->qs[sizeof(int) * (iqs + 0)], sizeof(int));
  1048. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1049. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI4_0)]);
  1050. const float d = __half2float(bq4_0->d) * __half2float(bq8_1->d);
  1051. // subtract 8 from each quantized value
  1052. const int vi0 = __vsub4((vi >> 0) & 0x0F0F0F0F, 0x08080808);
  1053. const int vi1 = __vsub4((vi >> 4) & 0x0F0F0F0F, 0x08080808);
  1054. // SIMD dot product of quantized values
  1055. int sumi = __dp4a(vi0, ui0, 0);
  1056. sumi = __dp4a(vi1, ui1, sumi);
  1057. return sumi*d;
  1058. #else
  1059. return 0.0f; // only to satisfy the compiler
  1060. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1061. }
  1062. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  1063. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1064. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1065. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  1066. const int vi = *((int *) &bq4_1->qs[sizeof(int) * (iqs + 0)]);
  1067. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1068. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI4_1)]);
  1069. const float d = __half2float(bq4_1->d) * __half2float(bq8_1->d);
  1070. const float m = bq4_1->m;
  1071. const float s = bq8_1->s;
  1072. const int vi0 = (vi >> 0) & 0x0F0F0F0F;
  1073. const int vi1 = (vi >> 4) & 0x0F0F0F0F;
  1074. // SIMD dot product of quantized values
  1075. int sumi = __dp4a(vi0, ui0, 0);
  1076. sumi = __dp4a(vi1, ui1, sumi);
  1077. return sumi*d + m*s / QI4_1; // scale sum by QI4_1 because there are QI4_1 threads working on this block
  1078. #else
  1079. return 0.0f; // only to satisfy the compiler
  1080. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1081. }
  1082. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  1083. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1084. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1085. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  1086. int qs;
  1087. memcpy(&qs, &bq5_0->qs[sizeof(int) * (iqs + 0)], sizeof(int));
  1088. const int qh0 = bq5_0->qh[iqs/2 + 0] >> 4*(iqs%2);
  1089. const int qh1 = bq5_0->qh[iqs/2 + 2] >> 4*(iqs%2);
  1090. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1091. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI5_0)]);
  1092. const float d = __half2float(bq5_0->d) * __half2float(bq8_1->d);
  1093. int vi0 = (qs >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh0 as 5th bits
  1094. vi0 |= (qh0 << 4) & 0x00000010; // 1 -> 5
  1095. vi0 |= (qh0 << 11) & 0x00001000; // 2 -> 13
  1096. vi0 |= (qh0 << 18) & 0x00100000; // 3 -> 21
  1097. vi0 |= (qh0 << 25) & 0x10000000; // 4 -> 29
  1098. vi0 = __vsub4(vi0, 0x10101010); // subtract 16 from quantized values
  1099. int sumi = __dp4a(vi0, ui0, 0); // SIMD dot product of quantized values
  1100. int vi1 = (qs >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh1 as 5th bits
  1101. vi1 |= (qh1 << 4) & 0x00000010; // 1 -> 5
  1102. vi1 |= (qh1 << 11) & 0x00001000; // 2 -> 13
  1103. vi1 |= (qh1 << 18) & 0x00100000; // 3 -> 21
  1104. vi1 |= (qh1 << 25) & 0x10000000; // 4 -> 29
  1105. vi1 = __vsub4(vi1, 0x10101010); // subtract 16 from quantized values
  1106. sumi = __dp4a(vi1, ui1, sumi); // SIMD dot product of quantized values
  1107. return sumi*d;
  1108. #else
  1109. return 0.0f; // only to satisfy the compiler
  1110. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1111. }
  1112. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  1113. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1114. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1115. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  1116. const int qs = *((int *) &bq5_1->qs[sizeof(int) * (iqs + 0)]);
  1117. const int qh0 = bq5_1->qh[iqs/2 + 0] >> 4*(iqs%2);
  1118. const int qh1 = bq5_1->qh[iqs/2 + 2] >> 4*(iqs%2);
  1119. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1120. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI5_1)]);
  1121. const float d = __half2float(bq5_1->d) * __half2float(bq8_1->d);
  1122. const float m = bq5_1->m;
  1123. const float s = bq8_1->s;
  1124. int vi0 = (qs >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh0 as 5th bits
  1125. vi0 |= (qh0 << 4) & 0x00000010; // 1 -> 5
  1126. vi0 |= (qh0 << 11) & 0x00001000; // 2 -> 13
  1127. vi0 |= (qh0 << 18) & 0x00100000; // 3 -> 21
  1128. vi0 |= (qh0 << 25) & 0x10000000; // 4 -> 29
  1129. int sumi = __dp4a(vi0, ui0, 0); // SIMD dot product of quantized values
  1130. int vi1 = (qs >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh1 as 5th bits
  1131. vi1 |= (qh1 << 4) & 0x00000010; // 1 -> 5
  1132. vi1 |= (qh1 << 11) & 0x00001000; // 2 -> 13
  1133. vi1 |= (qh1 << 18) & 0x00100000; // 3 -> 21
  1134. vi1 |= (qh1 << 25) & 0x10000000; // 4 -> 29
  1135. sumi = __dp4a(vi1, ui1, sumi); // SIMD dot product of quantized values
  1136. return sumi*d + m*s / QI5_1; // scale sum by QI5_1 because there are QI5_1 threads working on this block
  1137. #else
  1138. return 0.0f; // only to satisfy the compiler
  1139. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1140. }
  1141. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  1142. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1143. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1144. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  1145. int vi;
  1146. memcpy(&vi, &bq8_0->qs[sizeof(int) * (iqs + 0)], sizeof(int));
  1147. const int ui = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1148. const float d = __half2float(bq8_0->d) * __half2float(bq8_1->d);
  1149. // SIMD dot product of quantized values
  1150. int sumi = __dp4a(vi, ui, 0);
  1151. return sumi*d;
  1152. #else
  1153. return 0.0f; // only to satisfy the compiler
  1154. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1155. }
  1156. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  1157. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1158. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1159. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  1160. const int bq8_offset = QR2_K * (iqs / QI8_1);
  1161. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  1162. float sumf_d = 0.0f;
  1163. float sumf_m = 0.0f;
  1164. const float d = bq2_K->d;
  1165. const float dmin = bq2_K->dmin;
  1166. const int v = *((int *) &bq2_K->qs[sizeof(int) * iqs]);
  1167. for (int i = 0; i < QR2_K; ++i) {
  1168. const int sc = bq2_K->scales[scale_offset + 2*i];
  1169. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  1170. const float d8i = bq8i->d;
  1171. const int vi = (v >> (2*i)) & 0x03030303;
  1172. const int ui = *((int*) &bq8i->qs[sizeof(int) * (iqs % QI8_1)]);
  1173. sumf_d += d8i * (__dp4a(vi, ui, 0) * (sc & 0xF)); // SIMD dot product
  1174. sumf_m += d8i * (__dp4a(0x01010101, ui, 0) * (sc >> 4)); // multiply constant q2_K part with sum of q8_1 values
  1175. }
  1176. return d*sumf_d - dmin*sumf_m;
  1177. #else
  1178. return 0.0f; // only to satisfy the compiler
  1179. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1180. }
  1181. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  1182. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1183. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1184. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  1185. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  1186. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  1187. float sumf = 0.0f;
  1188. const float d = bq3_K->d;
  1189. int vl;
  1190. memcpy(&vl, &bq3_K->qs[sizeof(int) * iqs], sizeof(int));
  1191. int vh;
  1192. memcpy(&vh, &bq3_K->hmask[sizeof(int) * (iqs % (QI3_K/2))], sizeof(int));
  1193. vh = ~vh; // invert the mask so that a 0/1 results in 4/0 being subtracted
  1194. vh >>= bq8_offset;
  1195. for (int i = 0; i < QR3_K; ++i) {
  1196. const int isc = scale_offset + 2*i;
  1197. const int isc_low = isc % (QK_K/32);
  1198. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1199. const int sc_low = (bq3_K->scales[isc_low] >> sc_shift_low) & 0xF;
  1200. const int isc_high = isc % (QK_K/64);
  1201. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1202. const int sc_high = ((bq3_K->scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1203. const int sc = (sc_low | sc_high) - 32;
  1204. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  1205. const int ui = *((int*) &bq8i->qs[sizeof(int) * (iqs % QI8_1)]);
  1206. const float d8i = bq8i->d;
  1207. const int vil = (vl >> (2*i)) & 0x03030303;
  1208. const int vih = ((vh >> i) << 2) & 0x04040404;
  1209. const int vi = __vsubss4(vil, vih);
  1210. sumf += d8i * (__dp4a(vi, ui, 0) * sc); // SIMD dot product
  1211. }
  1212. return d*sumf;
  1213. #else
  1214. return 0.0f; // only to satisfy the compiler
  1215. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1216. }
  1217. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  1218. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1219. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1220. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  1221. const int bq8_offset = QR4_K * (iqs / QI8_1);
  1222. float sumf_d = 0.0f;
  1223. float sumf_m = 0.0f;
  1224. const float d = bq4_K->d;
  1225. const float dmin = bq4_K->dmin;
  1226. const int v = *((int *) &bq4_K->qs[sizeof(int) * iqs]);
  1227. for (int i = 0; i < QR4_K; ++i) {
  1228. const int isc = bq8_offset + i;
  1229. uint8_t sc, m;
  1230. get_scale_min_k4(isc, bq4_K->scales, sc, m);
  1231. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  1232. const int ui = *((int*) &bq8i->qs[sizeof(int) * (iqs % QI8_1)]);
  1233. const float d8i = bq8i->d;
  1234. const int vi = (v >> (4*i)) & 0x0F0F0F0F;
  1235. sumf_d += d8i * (__dp4a(vi, ui, 0) * sc); // SIMD dot product
  1236. sumf_m += d8i * (__dp4a(0x01010101, ui, 0) * m); // multiply constant part of q4_K with sum of q8_1 values
  1237. }
  1238. return d*sumf_d - dmin*sumf_m;
  1239. #else
  1240. return 0.0f; // only to satisfy the compiler
  1241. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1242. }
  1243. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  1244. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1245. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1246. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  1247. const int bq8_offset = QR5_K * (iqs / QI8_1);
  1248. float sumf_d = 0.0f;
  1249. float sumf_m = 0.0f;
  1250. const float d = bq5_K->d;
  1251. const float dmin = bq5_K->dmin;
  1252. const int vl = *((int *) &bq5_K->qs[sizeof(int) * iqs]);
  1253. const int vh = (*((int *) &bq5_K->qh[sizeof(int) * (iqs % (QI5_K/4))])) >> bq8_offset;
  1254. for (int i = 0; i < QR5_K; ++i) {
  1255. const int isc = bq8_offset + i;
  1256. uint8_t sc, m;
  1257. get_scale_min_k4(isc, bq5_K->scales, sc, m);
  1258. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  1259. const int ui = *((int*) &bq8i->qs[sizeof(int) * (iqs % QI8_1)]);
  1260. const float d8i = bq8i->d;
  1261. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1262. const int vih = ((vh >> i) << 4) & 0x10101010;
  1263. const int vi = vil | vih;
  1264. sumf_d += d8i * (__dp4a(vi, ui, 0) * sc); // SIMD dot product
  1265. sumf_m += d8i * (__dp4a(0x01010101, ui, 0) * m); // multiply constant part of q5_K with sum of q8_1 values
  1266. }
  1267. return d*sumf_d - dmin*sumf_m;
  1268. #else
  1269. return 0.0f; // only to satisfy the compiler
  1270. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1271. }
  1272. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  1273. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int iqs) {
  1274. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1275. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  1276. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  1277. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  1278. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  1279. float sumf = 0.0f;
  1280. const float d = bq6_K->d;
  1281. int vl;
  1282. memcpy(&vl, &bq6_K->ql[sizeof(int) * iqs], sizeof(int));
  1283. int vh;
  1284. memcpy(&vh, &bq6_K->qh[sizeof(int) * ((QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4))], sizeof(int));
  1285. for (int i = 0; i < QR6_K; ++i) {
  1286. const int sc = bq6_K->scales[scale_offset + 4*i];
  1287. const block_q8_1 * bq8i = bq8_1 + bq8_offset + 2*i;
  1288. const int ui = *((int*) &bq8i->qs[sizeof(int) * (iqs % (QI8_1))]);
  1289. const float d8i = bq8i->d;
  1290. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1291. const int vih = ((vh >> (vh_shift + 4*i)) << 4) & 0x30303030;
  1292. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1293. sumf += d8i * (__dp4a(vi, ui, 0) * sc); // SIMD dot product
  1294. }
  1295. return d*sumf;
  1296. #else
  1297. return 0.0f; // only to satisfy the compiler
  1298. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1299. }
  1300. template <int qk, int qi, typename block_q_t, vec_dot_q_cuda_t vec_dot_q_cuda>
  1301. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  1302. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  1303. if (row >= nrows) {
  1304. return;
  1305. }
  1306. const int blocks_per_row = ncols / qk;
  1307. const int blocks_per_warp = WARP_SIZE / qi;
  1308. // partial sum for each thread
  1309. float tmp = 0.0f;
  1310. const block_q_t * x = (const block_q_t *) vx;
  1311. const block_q8_1 * y = (const block_q8_1 *) vy;
  1312. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  1313. const int ibx = row*blocks_per_row + i + threadIdx.x / qi; // x block index
  1314. const int iby = (i + threadIdx.x / qi) * qk/QK8_1; // y block index that aligns with ibx
  1315. const int iqs = threadIdx.x % qi; // x block quant index when casting the quants to int
  1316. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  1317. }
  1318. // sum up partial sums and write back result
  1319. #pragma unroll
  1320. for (int mask = 16; mask > 0; mask >>= 1) {
  1321. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1322. }
  1323. if (threadIdx.x == 0) {
  1324. dst[row] = tmp;
  1325. }
  1326. }
  1327. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  1328. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  1329. // qk = quantized weights per x block
  1330. // qr = number of quantized weights per data value in x block
  1331. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  1332. if (row >= nrows) {
  1333. return;
  1334. }
  1335. const int tid = threadIdx.x;
  1336. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  1337. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  1338. const int y_offset = qr == 1 ? 1 : qk/2;
  1339. // partial sum for each thread
  1340. #ifdef GGML_CUDA_DMMV_F16
  1341. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  1342. #else
  1343. float tmp = 0.0f;
  1344. #endif // GGML_CUDA_DMMV_F16
  1345. for (int i = 0; i < ncols; i += iter_stride) {
  1346. const int col = i + vals_per_iter*tid;
  1347. const int ib = (row*ncols + col)/qk; // x block index
  1348. const int iqs = (col%qk)/qr; // x quant index
  1349. const int iybs = col - col%qk; // y block start index
  1350. // processing >2 values per i iter is faster for fast GPUs
  1351. #pragma unroll
  1352. for (int j = 0; j < vals_per_iter; j += 2) {
  1353. // process 2 vals per j iter
  1354. // dequantize
  1355. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  1356. dfloat2 v;
  1357. dequantize_kernel(vx, ib, iqs + j/qr, v);
  1358. // matrix multiplication
  1359. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  1360. #ifdef GGML_CUDA_DMMV_F16
  1361. tmp += __hmul2(v, {
  1362. y[iybs + iqs + j/qr + 0],
  1363. y[iybs + iqs + j/qr + y_offset]
  1364. });
  1365. #else
  1366. tmp += v.x * y[iybs + iqs + j/qr + 0];
  1367. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  1368. #endif // GGML_CUDA_DMMV_F16
  1369. }
  1370. }
  1371. // sum up partial sums and write back result
  1372. #pragma unroll
  1373. for (int mask = 16; mask > 0; mask >>= 1) {
  1374. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1375. }
  1376. if (tid == 0) {
  1377. #ifdef GGML_CUDA_DMMV_F16
  1378. dst[row] = tmp.x + tmp.y;
  1379. #else
  1380. dst[row] = tmp;
  1381. #endif // GGML_CUDA_DMMV_F16
  1382. }
  1383. }
  1384. static __global__ void mul_mat_p021_f16_f32(const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x, const int nchannels_x) {
  1385. const half * x = (const half *) vx;
  1386. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  1387. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  1388. const int nrows_y = ncols_x;
  1389. const int nrows_dst = nrows_x;
  1390. const int row_dst = row_x;
  1391. float tmp = 0.0f;
  1392. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  1393. const int col_x = col_x0 + threadIdx.x;
  1394. if (col_x >= ncols_x) {
  1395. break;
  1396. }
  1397. // x is transposed and permuted
  1398. const int ix = row_x*nchannels_x*ncols_x + channel*ncols_x + col_x;
  1399. const float xi = __half2float(x[ix]);
  1400. const int row_y = col_x;
  1401. // y is not transposed but permuted
  1402. const int iy = channel*nrows_y + row_y;
  1403. tmp += xi * y[iy];
  1404. }
  1405. // dst is not transposed and not permuted
  1406. const int idst = channel*nrows_dst + row_dst;
  1407. // sum up partial sums and write back result
  1408. #pragma unroll
  1409. for (int mask = 16; mask > 0; mask >>= 1) {
  1410. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1411. }
  1412. if (threadIdx.x == 0) {
  1413. dst[idst] = tmp;
  1414. }
  1415. }
  1416. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  1417. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  1418. const int row_stride_x, const int channel_stride_x) {
  1419. const half * x = (const half *) vx;
  1420. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  1421. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  1422. const int nrows_y = ncols_x;
  1423. const int nrows_dst = nrows_x;
  1424. const int row_dst = row_x;
  1425. const int idst = channel*nrows_dst + row_dst;
  1426. float tmp = 0.0f;
  1427. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  1428. const int col_x = col_x0 + threadIdx.x;
  1429. if (col_x >= ncols_x) {
  1430. break;
  1431. }
  1432. const int ix = channel*channel_stride_x + row_x*row_stride_x + col_x;
  1433. const float xi = __half2float(x[ix]);
  1434. const int row_y = col_x;
  1435. const int iy = channel*nrows_y + row_y;
  1436. tmp += xi * y[iy];
  1437. }
  1438. // sum up partial sums and write back result
  1439. #pragma unroll
  1440. for (int mask = 16; mask > 0; mask >>= 1) {
  1441. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1442. }
  1443. if (threadIdx.x == 0) {
  1444. dst[idst] = tmp;
  1445. }
  1446. }
  1447. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  1448. const float * xi = (const float *) cxi;
  1449. float * dsti = (float *) cdsti;
  1450. *dsti = *xi;
  1451. }
  1452. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  1453. const float * xi = (const float *) cxi;
  1454. half * dsti = (half *) cdsti;
  1455. *dsti = __float2half(*xi);
  1456. }
  1457. template <cpy_kernel_t cpy_1>
  1458. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  1459. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  1460. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  1461. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1462. if (i >= ne) {
  1463. return;
  1464. }
  1465. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  1466. // then combine those indices with the corresponding byte offsets to get the total offsets
  1467. const int i02 = i / (ne00*ne01);
  1468. const int i01 = (i - i02*ne01*ne00) / ne00;
  1469. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  1470. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  1471. const int i12 = i / (ne10*ne11);
  1472. const int i11 = (i - i12*ne10*ne11) / ne10;
  1473. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  1474. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  1475. cpy_1(cx + x_offset, cdst + dst_offset);
  1476. }
  1477. // rope == RoPE == rotary positional embedding
  1478. static __global__ void rope_f32(const float * x, float * dst, const int ncols, const float p, const float theta_scale) {
  1479. const int col = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  1480. if (col >= ncols) {
  1481. return;
  1482. }
  1483. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1484. const int i = row*ncols + col;
  1485. const float theta = p*powf(theta_scale, col/2);
  1486. const float sin_theta = sinf(theta);
  1487. const float cos_theta = cosf(theta);
  1488. const float x0 = x[i + 0];
  1489. const float x1 = x[i + 1];
  1490. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  1491. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  1492. }
  1493. static __global__ void rope_glm_f32(const float * x, float * dst, const int ncols, const float p, const float block_p, const float theta_scale) {
  1494. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  1495. const int half_n_dims = ncols/4;
  1496. if (col >= half_n_dims) {
  1497. return;
  1498. }
  1499. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1500. const int i = row*ncols + col;
  1501. const float col_theta_scale = powf(theta_scale, col);
  1502. const float theta = p*col_theta_scale;
  1503. const float sin_theta = sinf(theta);
  1504. const float cos_theta = cosf(theta);
  1505. const float x0 = x[i + 0];
  1506. const float x1 = x[i + half_n_dims];
  1507. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  1508. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  1509. const float block_theta = block_p*col_theta_scale;
  1510. const float sin_block_theta = sinf(block_theta);
  1511. const float cos_block_theta = cosf(block_theta);
  1512. const float x2 = x[i + half_n_dims * 2];
  1513. const float x3 = x[i + half_n_dims * 3];
  1514. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  1515. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  1516. }
  1517. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  1518. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  1519. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1520. if (col >= ncols) {
  1521. return;
  1522. }
  1523. const int i = row*ncols + col;
  1524. // dst[i] = col > n_past + row ? -INFINITY : x[i];
  1525. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  1526. }
  1527. // the CUDA soft max implementation differs from the CPU implementation
  1528. // instead of doubles floats are used
  1529. // values are also not normalized to the maximum value by subtracting it in the exponential function
  1530. // theoretically these changes could cause problems with rounding error and arithmetic overflow but for LLaMa it seems to be fine
  1531. static __global__ void soft_max_f32(const float * x, float * dst, const int ncols) {
  1532. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1533. const int block_size = blockDim.x;
  1534. const int tid = threadIdx.x;
  1535. float tmp = 0.0;
  1536. for (int block_start = 0; block_start < ncols; block_start += block_size) {
  1537. const int col = block_start + tid;
  1538. if (col >= ncols) {
  1539. break;
  1540. }
  1541. const int i = row*ncols + col;
  1542. const float val = expf(x[i]);
  1543. tmp += val;
  1544. dst[i] = val;
  1545. }
  1546. // sum up partial sums
  1547. #pragma unroll
  1548. for (int mask = 16; mask > 0; mask >>= 1) {
  1549. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1550. }
  1551. for (int block_start = 0; block_start < ncols; block_start += block_size) {
  1552. const int col = block_start + tid;
  1553. if (col >= ncols) {
  1554. break;
  1555. }
  1556. const int i = row*ncols + col;
  1557. dst[i] /= tmp;
  1558. }
  1559. }
  1560. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  1561. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1562. if (i >= k) {
  1563. return;
  1564. }
  1565. dst[i] = scale * x[i];
  1566. }
  1567. static void add_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  1568. const int num_blocks = (kx + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  1569. add_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  1570. }
  1571. static void add_f16_f32_f16_cuda(const half * x, const float * y, half * dst, const int k, cudaStream_t stream) {
  1572. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  1573. add_f16_f32_f16<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  1574. }
  1575. static void mul_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  1576. const int num_blocks = (kx + CUDA_MUL_BLOCK_SIZE - 1) / CUDA_MUL_BLOCK_SIZE;
  1577. mul_f32<<<num_blocks, CUDA_MUL_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  1578. }
  1579. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  1580. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  1581. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  1582. }
  1583. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  1584. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  1585. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  1586. }
  1587. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1588. GGML_ASSERT(ncols % WARP_SIZE == 0);
  1589. const dim3 block_dims(WARP_SIZE, 1, 1);
  1590. norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  1591. }
  1592. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1593. GGML_ASSERT(ncols % WARP_SIZE == 0);
  1594. const dim3 block_dims(WARP_SIZE, 1, 1);
  1595. rms_norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  1596. }
  1597. static void quantize_row_q8_1_cuda(const float * x, void * vy, const int ndata, const int k, cudaStream_t stream) {
  1598. const int num_blocks = (k + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  1599. quantize_q8_1<<<num_blocks, CUDA_QUANTIZE_BLOCK_SIZE, 0, stream>>>(x, vy, ndata, k);
  1600. }
  1601. static void dequantize_row_q4_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1602. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1603. dequantize_block<QK4_0, QR4_0, dequantize_q4_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1604. }
  1605. static void dequantize_row_q4_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1606. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1607. dequantize_block<QK4_1, QR4_1, dequantize_q4_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1608. }
  1609. static void dequantize_row_q5_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1610. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1611. dequantize_block<QK5_0, QR5_0, dequantize_q5_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1612. }
  1613. static void dequantize_row_q5_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1614. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1615. dequantize_block<QK5_1, QR5_1, dequantize_q5_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1616. }
  1617. static void dequantize_row_q8_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1618. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1619. dequantize_block<QK8_0, QR8_0, dequantize_q8_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1620. }
  1621. static void dequantize_row_q2_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1622. const int nb = k / QK_K;
  1623. #if QK_K == 256
  1624. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  1625. #else
  1626. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  1627. #endif
  1628. }
  1629. static void dequantize_row_q3_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1630. const int nb = k / QK_K;
  1631. #if QK_K == 256
  1632. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  1633. #else
  1634. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  1635. #endif
  1636. }
  1637. static void dequantize_row_q4_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1638. const int nb = k / QK_K;
  1639. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  1640. }
  1641. static void dequantize_row_q5_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1642. const int nb = k / QK_K;
  1643. #if QK_K == 256
  1644. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  1645. #else
  1646. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  1647. #endif
  1648. }
  1649. static void dequantize_row_q6_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1650. const int nb = k / QK_K;
  1651. #if QK_K == 256
  1652. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  1653. #else
  1654. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  1655. #endif
  1656. }
  1657. static void dequantize_mul_mat_vec_q4_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1658. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1659. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1660. const dim3 block_nums(1, block_num_y, 1);
  1661. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1662. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  1663. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1664. }
  1665. static void dequantize_mul_mat_vec_q4_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1666. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1667. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1668. const dim3 block_nums(1, block_num_y, 1);
  1669. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1670. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  1671. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1672. }
  1673. static void dequantize_mul_mat_vec_q5_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1674. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1675. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1676. const dim3 block_nums(1, block_num_y, 1);
  1677. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1678. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  1679. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1680. }
  1681. static void dequantize_mul_mat_vec_q5_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1682. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1683. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1684. const dim3 block_nums(1, block_num_y, 1);
  1685. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1686. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  1687. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1688. }
  1689. static void dequantize_mul_mat_vec_q8_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1690. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1691. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1692. const dim3 block_nums(1, block_num_y, 1);
  1693. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1694. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  1695. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1696. }
  1697. static void dequantize_mul_mat_vec_q2_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1698. GGML_ASSERT(ncols % QK_K == 0);
  1699. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  1700. const int block_num_y = (nrows + ny - 1) / ny;
  1701. const dim3 block_nums(1, block_num_y, 1);
  1702. const dim3 block_dims(32, ny, 1);
  1703. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1704. }
  1705. static void dequantize_mul_mat_vec_q3_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1706. GGML_ASSERT(ncols % QK_K == 0);
  1707. const int ny = 2 / K_QUANTS_PER_ITERATION;
  1708. const int block_num_y = (nrows + ny - 1) / ny;
  1709. const dim3 block_nums(1, block_num_y, 1);
  1710. const dim3 block_dims(32, ny, 1);
  1711. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1712. }
  1713. static void dequantize_mul_mat_vec_q4_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1714. GGML_ASSERT(ncols % QK_K == 0);
  1715. const int ny = 2 / K_QUANTS_PER_ITERATION;
  1716. const int block_num_y = (nrows + ny - 1) / ny;
  1717. const dim3 block_nums(1, block_num_y, 1);
  1718. const dim3 block_dims(32, ny, 1);
  1719. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1720. }
  1721. static void dequantize_mul_mat_vec_q5_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1722. GGML_ASSERT(ncols % QK_K == 0);
  1723. const dim3 block_dims(32, 1, 1);
  1724. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  1725. }
  1726. static void dequantize_mul_mat_vec_q6_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1727. GGML_ASSERT(ncols % QK_K == 0);
  1728. const int ny = 2 / K_QUANTS_PER_ITERATION;
  1729. const int block_num_y = (nrows + ny - 1) / ny;
  1730. const dim3 block_nums(1, block_num_y, 1);
  1731. const dim3 block_dims(32, ny, 1);
  1732. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1733. }
  1734. static void mul_mat_vec_q4_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1735. GGML_ASSERT(ncols % QK4_0 == 0);
  1736. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1737. const dim3 block_nums(1, block_num_y, 1);
  1738. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1739. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, vec_dot_q4_0_q8_1>
  1740. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1741. }
  1742. static void mul_mat_vec_q4_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1743. GGML_ASSERT(ncols % QK4_1 == 0);
  1744. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1745. const dim3 block_nums(1, block_num_y, 1);
  1746. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1747. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, vec_dot_q4_1_q8_1>
  1748. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1749. }
  1750. static void mul_mat_vec_q5_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1751. GGML_ASSERT(ncols % QK5_0 == 0);
  1752. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1753. const dim3 block_nums(1, block_num_y, 1);
  1754. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1755. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, vec_dot_q5_0_q8_1>
  1756. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1757. }
  1758. static void mul_mat_vec_q5_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1759. GGML_ASSERT(ncols % QK5_1 == 0);
  1760. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1761. const dim3 block_nums(1, block_num_y, 1);
  1762. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1763. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, vec_dot_q5_1_q8_1>
  1764. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1765. }
  1766. static void mul_mat_vec_q8_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1767. GGML_ASSERT(ncols % QK8_0 == 0);
  1768. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1769. const dim3 block_nums(1, block_num_y, 1);
  1770. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1771. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, vec_dot_q8_0_q8_1>
  1772. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1773. }
  1774. static void mul_mat_vec_q2_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1775. GGML_ASSERT(ncols % QK_K == 0);
  1776. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1777. const dim3 block_nums(1, block_num_y, 1);
  1778. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1779. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, vec_dot_q2_K_q8_1>
  1780. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1781. }
  1782. static void mul_mat_vec_q3_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1783. GGML_ASSERT(ncols % QK_K == 0);
  1784. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1785. const dim3 block_nums(1, block_num_y, 1);
  1786. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1787. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, vec_dot_q3_K_q8_1>
  1788. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1789. }
  1790. static void mul_mat_vec_q4_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1791. GGML_ASSERT(ncols % QK_K == 0);
  1792. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1793. const dim3 block_nums(1, block_num_y, 1);
  1794. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1795. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, vec_dot_q4_K_q8_1>
  1796. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1797. }
  1798. static void mul_mat_vec_q5_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1799. GGML_ASSERT(ncols % QK_K == 0);
  1800. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1801. const dim3 block_nums(1, block_num_y, 1);
  1802. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1803. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, vec_dot_q5_K_q8_1>
  1804. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1805. }
  1806. static void mul_mat_vec_q6_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1807. GGML_ASSERT(ncols % QK_K == 0);
  1808. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1809. const dim3 block_nums(1, block_num_y, 1);
  1810. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1811. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, vec_dot_q6_K_q8_1>
  1812. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1813. }
  1814. static void convert_fp16_to_fp32_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1815. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1816. dequantize_block<1, 1, convert_f16><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1817. }
  1818. static void convert_mul_mat_vec_f16_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1819. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1820. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1821. const dim3 block_nums(1, block_num_y, 1);
  1822. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1823. dequantize_mul_mat_vec<1, 1, convert_f16>
  1824. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1825. }
  1826. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  1827. switch (type) {
  1828. case GGML_TYPE_Q4_0:
  1829. return dequantize_row_q4_0_cuda;
  1830. case GGML_TYPE_Q4_1:
  1831. return dequantize_row_q4_1_cuda;
  1832. case GGML_TYPE_Q5_0:
  1833. return dequantize_row_q5_0_cuda;
  1834. case GGML_TYPE_Q5_1:
  1835. return dequantize_row_q5_1_cuda;
  1836. case GGML_TYPE_Q8_0:
  1837. return dequantize_row_q8_0_cuda;
  1838. case GGML_TYPE_Q2_K:
  1839. return dequantize_row_q2_K_cuda;
  1840. case GGML_TYPE_Q3_K:
  1841. return dequantize_row_q3_K_cuda;
  1842. case GGML_TYPE_Q4_K:
  1843. return dequantize_row_q4_K_cuda;
  1844. case GGML_TYPE_Q5_K:
  1845. return dequantize_row_q5_K_cuda;
  1846. case GGML_TYPE_Q6_K:
  1847. return dequantize_row_q6_K_cuda;
  1848. case GGML_TYPE_F16:
  1849. return convert_fp16_to_fp32_cuda;
  1850. default:
  1851. return nullptr;
  1852. }
  1853. }
  1854. static void ggml_mul_mat_p021_f16_f32_cuda(const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nchannels_x, cudaStream_t stream) {
  1855. const dim3 block_nums(1, nrows_x, nchannels_x);
  1856. const dim3 block_dims(WARP_SIZE, 1, 1);
  1857. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x);
  1858. }
  1859. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  1860. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  1861. const int nchannels_x, const int channel_stride_x, cudaStream_t stream) {
  1862. const dim3 block_nums(1, nrows_x, nchannels_x);
  1863. const dim3 block_dims(WARP_SIZE, 1, 1);
  1864. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  1865. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x);
  1866. }
  1867. static void ggml_cpy_f32_f32_cuda(
  1868. const char * cx, char * cdst, const int ne,
  1869. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  1870. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  1871. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  1872. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  1873. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  1874. }
  1875. static void ggml_cpy_f32_f16_cuda(
  1876. const char * cx, char * cdst, const int ne,
  1877. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  1878. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  1879. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  1880. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  1881. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  1882. }
  1883. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  1884. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  1885. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  1886. }
  1887. static void rope_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p, const float theta_scale, cudaStream_t stream) {
  1888. GGML_ASSERT(nrows % 2 == 0);
  1889. const dim3 block_dims(2*CUDA_ROPE_BLOCK_SIZE, 1, 1);
  1890. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  1891. const dim3 block_nums(num_blocks_x, nrows, 1);
  1892. rope_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p, theta_scale);
  1893. }
  1894. static void rope_glm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p, const float block_p, const float theta_scale, cudaStream_t stream) {
  1895. GGML_ASSERT(nrows % 4 == 0);
  1896. const dim3 block_dims(4*CUDA_ROPE_BLOCK_SIZE, 1, 1);
  1897. const int num_blocks_x = (ncols + 4*CUDA_ROPE_BLOCK_SIZE - 1) / (4*CUDA_ROPE_BLOCK_SIZE);
  1898. const dim3 block_nums(num_blocks_x, nrows, 1);
  1899. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p, block_p, theta_scale);
  1900. }
  1901. static void diag_mask_inf_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, const int rows_per_channel, const int n_past, cudaStream_t stream) {
  1902. const dim3 block_dims(CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1, 1);
  1903. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  1904. const dim3 block_nums(block_num_x, nrows_x, 1);
  1905. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  1906. }
  1907. static void soft_max_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, cudaStream_t stream) {
  1908. const dim3 block_dims(WARP_SIZE, 1, 1);
  1909. const dim3 block_nums(1, nrows_x, 1);
  1910. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x);
  1911. }
  1912. // buffer pool for cuda
  1913. #define MAX_CUDA_BUFFERS 256
  1914. struct scoped_spin_lock {
  1915. std::atomic_flag& lock;
  1916. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  1917. while (lock.test_and_set(std::memory_order_acquire)) {
  1918. ; // spin
  1919. }
  1920. }
  1921. ~scoped_spin_lock() {
  1922. lock.clear(std::memory_order_release);
  1923. }
  1924. scoped_spin_lock(const scoped_spin_lock&) = delete;
  1925. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  1926. };
  1927. struct cuda_buffer {
  1928. void * ptr = nullptr;
  1929. size_t size = 0;
  1930. };
  1931. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  1932. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  1933. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  1934. scoped_spin_lock lock(g_cuda_pool_lock);
  1935. int id;
  1936. CUDA_CHECK(cudaGetDevice(&id));
  1937. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  1938. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  1939. if (b.size >= size && b.ptr != nullptr) {
  1940. void * ptr = b.ptr;
  1941. *actual_size = b.size;
  1942. b.ptr = nullptr;
  1943. b.size = 0;
  1944. return ptr;
  1945. }
  1946. }
  1947. void * ptr;
  1948. CUDA_CHECK(cudaMalloc((void **) &ptr, size));
  1949. *actual_size = size;
  1950. return ptr;
  1951. }
  1952. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  1953. scoped_spin_lock lock(g_cuda_pool_lock);
  1954. int id;
  1955. CUDA_CHECK(cudaGetDevice(&id));
  1956. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  1957. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  1958. if (b.ptr == nullptr) {
  1959. b.ptr = ptr;
  1960. b.size = size;
  1961. return;
  1962. }
  1963. }
  1964. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  1965. CUDA_CHECK(cudaFree(ptr));
  1966. }
  1967. static void * g_scratch_buffer = nullptr;
  1968. static size_t g_scratch_size = 1024*1024*1024; // 1 GB by default
  1969. static size_t g_scratch_offset = 0;
  1970. static int g_device_count = -1;
  1971. static int g_main_device = 0;
  1972. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  1973. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  1974. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  1975. static cudaStream_t g_cudaStreams_main[GGML_CUDA_MAX_DEVICES] = { nullptr };
  1976. void ggml_init_cublas() {
  1977. static bool initialized = false;
  1978. if (!initialized) {
  1979. CUDA_CHECK(cudaGetDeviceCount(&g_device_count));
  1980. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  1981. int64_t total_vram = 0;
  1982. fprintf(stderr, "%s: found %d CUDA devices:\n", __func__, g_device_count);
  1983. for (int id = 0; id < g_device_count; ++id) {
  1984. cudaDeviceProp prop;
  1985. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  1986. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  1987. g_tensor_split[id] = total_vram;
  1988. total_vram += prop.totalGlobalMem;
  1989. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  1990. }
  1991. for (int id = 0; id < g_device_count; ++id) {
  1992. g_tensor_split[id] /= total_vram;
  1993. }
  1994. for (int id = 0; id < g_device_count; ++id) {
  1995. CUDA_CHECK(cudaSetDevice(id));
  1996. // create main stream
  1997. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams_main[id], cudaStreamNonBlocking));
  1998. // create cublas handle
  1999. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  2000. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  2001. }
  2002. // configure logging to stdout
  2003. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  2004. initialized = true;
  2005. }
  2006. }
  2007. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  2008. bool all_zero = true;
  2009. for (int i = 0; i < g_device_count; ++i) {
  2010. if (tensor_split[i] != 0.0f) {
  2011. all_zero = false;
  2012. break;
  2013. }
  2014. }
  2015. if (all_zero) {
  2016. return;
  2017. }
  2018. float split_sum = 0.0f;
  2019. for (int i = 0; i < g_device_count; ++i) {
  2020. g_tensor_split[i] = split_sum;
  2021. split_sum += tensor_split[i];
  2022. }
  2023. for (int i = 0; i < g_device_count; ++i) {
  2024. g_tensor_split[i] /= split_sum;
  2025. }
  2026. }
  2027. void * ggml_cuda_host_malloc(size_t size) {
  2028. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  2029. return nullptr;
  2030. }
  2031. void * ptr = nullptr;
  2032. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  2033. if (err != cudaSuccess) {
  2034. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  2035. // This can fixed the OOM error in WSL.
  2036. cudaGetLastError();
  2037. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  2038. size/1024.0/1024.0, cudaGetErrorString(err));
  2039. return nullptr;
  2040. }
  2041. return ptr;
  2042. }
  2043. void ggml_cuda_host_free(void * ptr) {
  2044. CUDA_CHECK(cudaFreeHost(ptr));
  2045. }
  2046. static cudaError_t ggml_cuda_cpy_tensor_2d(
  2047. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  2048. cudaMemcpyKind kind;
  2049. char * src_ptr;
  2050. if (src->backend == GGML_BACKEND_CPU) {
  2051. kind = cudaMemcpyHostToDevice;
  2052. src_ptr = (char *) src->data;
  2053. } else if (src->backend == GGML_BACKEND_GPU) {
  2054. kind = cudaMemcpyDeviceToDevice;
  2055. struct ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  2056. int id;
  2057. CUDA_CHECK(cudaGetDevice(&id));
  2058. src_ptr = (char *) extra->data_device[id];
  2059. } else {
  2060. GGML_ASSERT(false);
  2061. }
  2062. char * dst_ptr = (char *) dst;
  2063. const int64_t ne0 = src->ne[0];
  2064. const int64_t nb0 = src->nb[0];
  2065. const int64_t nb1 = src->nb[1];
  2066. const int64_t nb2 = src->nb[2];
  2067. const int64_t nb3 = src->nb[3];
  2068. const enum ggml_type type = src->type;
  2069. const int64_t ts = ggml_type_size(type);
  2070. const int64_t bs = ggml_blck_size(type);
  2071. int64_t i1_diff = i1_high - i1_low;
  2072. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  2073. if (nb0 == ts && nb1 == ts*ne0/bs) {
  2074. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  2075. } else if (nb0 == ts) {
  2076. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  2077. } else {
  2078. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  2079. const void * rx = (const void *) ((const char *) x + i1*nb1);
  2080. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  2081. // pretend the row is a matrix with cols=1
  2082. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  2083. if (r != cudaSuccess) return r;
  2084. }
  2085. return cudaSuccess;
  2086. }
  2087. }
  2088. inline void ggml_cuda_op_add(
  2089. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2090. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2091. cudaStream_t & cudaStream_main){
  2092. GGML_ASSERT(src0_ddq_i != nullptr || src0_ddf_i != nullptr);
  2093. GGML_ASSERT(src1_ddf_i != nullptr);
  2094. GGML_ASSERT(dst_ddf_i != nullptr);
  2095. const int64_t ne00 = src0->ne[0];
  2096. const int64_t i01_diff = i01_high - i01_low;
  2097. const int64_t ne10 = src1->ne[0];
  2098. const int64_t ne11 = src1->ne[1];
  2099. // compute
  2100. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  2101. add_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne00*i01_diff, ne10*ne11, cudaStream_main);
  2102. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  2103. add_f16_f32_f16_cuda((half *) src0_ddq_i, src1_ddf_i, (half *) dst_ddf_i, ne00*i01_diff, cudaStream_main);
  2104. } else {
  2105. GGML_ASSERT(false);
  2106. }
  2107. (void) src1;
  2108. (void) dst;
  2109. (void) src0_ddq_i;
  2110. (void) i02;
  2111. (void) i1;
  2112. }
  2113. inline void ggml_cuda_op_mul(
  2114. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2115. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2116. cudaStream_t & cudaStream_main){
  2117. GGML_ASSERT(src0_ddf_i != nullptr);
  2118. GGML_ASSERT(src1_ddf_i != nullptr);
  2119. GGML_ASSERT(dst_ddf_i != nullptr);
  2120. const int64_t ne00 = src0->ne[0];
  2121. const int64_t i01_diff = i01_high - i01_low;
  2122. const int64_t ne10 = src1->ne[0];
  2123. const int64_t ne11 = src1->ne[1];
  2124. mul_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne00*i01_diff, ne10*ne11, cudaStream_main);
  2125. (void) dst;
  2126. (void) src0_ddq_i;
  2127. (void) i02;
  2128. }
  2129. inline void ggml_cuda_op_gelu(
  2130. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2131. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2132. cudaStream_t & cudaStream_main){
  2133. GGML_ASSERT(src0_ddf_i != nullptr);
  2134. GGML_ASSERT(dst_ddf_i != nullptr);
  2135. const int64_t ne00 = src0->ne[0];
  2136. const int64_t i01_diff = i01_high - i01_low;
  2137. // compute
  2138. gelu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  2139. (void) src1;
  2140. (void) dst;
  2141. (void) src0_ddq_i;
  2142. (void) src1_ddf_i;
  2143. (void) i02;
  2144. (void) i1;
  2145. }
  2146. inline void ggml_cuda_op_silu(
  2147. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2148. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2149. cudaStream_t & cudaStream_main){
  2150. GGML_ASSERT(src0_ddf_i != nullptr);
  2151. GGML_ASSERT(dst_ddf_i != nullptr);
  2152. const int64_t ne00 = src0->ne[0];
  2153. const int64_t i01_diff = i01_high - i01_low;
  2154. // compute
  2155. silu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  2156. (void) src1;
  2157. (void) dst;
  2158. (void) src0_ddq_i;
  2159. (void) src1_ddf_i;
  2160. (void) i02;
  2161. (void) i1;
  2162. }
  2163. inline void ggml_cuda_op_norm(
  2164. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2165. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2166. cudaStream_t & cudaStream_main){
  2167. GGML_ASSERT(src0_ddf_i != nullptr);
  2168. GGML_ASSERT(dst_ddf_i != nullptr);
  2169. const int64_t ne00 = src0->ne[0];
  2170. const int64_t i01_diff = i01_high - i01_low;
  2171. // compute
  2172. norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  2173. (void) src1;
  2174. (void) dst;
  2175. (void) src0_ddq_i;
  2176. (void) src1_ddf_i;
  2177. (void) i02;
  2178. (void) i1;
  2179. }
  2180. inline void ggml_cuda_op_rms_norm(
  2181. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2182. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2183. cudaStream_t & cudaStream_main){
  2184. GGML_ASSERT(src0_ddf_i != nullptr);
  2185. GGML_ASSERT(dst_ddf_i != nullptr);
  2186. const int64_t ne00 = src0->ne[0];
  2187. const int64_t i01_diff = i01_high - i01_low;
  2188. // compute
  2189. rms_norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  2190. (void) src1;
  2191. (void) dst;
  2192. (void) src0_ddq_i;
  2193. (void) src1_ddf_i;
  2194. (void) i02;
  2195. (void) i1;
  2196. }
  2197. inline void ggml_cuda_op_mul_mat_vec(
  2198. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2199. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2200. cudaStream_t & cudaStream_main){
  2201. GGML_ASSERT(src0_ddq_i != nullptr);
  2202. GGML_ASSERT(src1_ddf_i != nullptr);
  2203. GGML_ASSERT(dst_ddf_i != nullptr);
  2204. const int64_t ne00 = src0->ne[0];
  2205. const int64_t nrows = i01_high - i01_low;
  2206. #ifdef GGML_CUDA_FORCE_DMMV
  2207. const bool use_mul_mat_vec_q = false;
  2208. #else
  2209. int id;
  2210. CUDA_CHECK(cudaGetDevice(&id));
  2211. bool mul_mat_vec_q_implemented =
  2212. src0->type == GGML_TYPE_Q4_0 ||
  2213. src0->type == GGML_TYPE_Q4_1 ||
  2214. src0->type == GGML_TYPE_Q5_0 ||
  2215. src0->type == GGML_TYPE_Q5_1 ||
  2216. src0->type == GGML_TYPE_Q8_0;
  2217. #if QK_K == 256
  2218. mul_mat_vec_q_implemented = mul_mat_vec_q_implemented ||
  2219. src0->type == GGML_TYPE_Q2_K ||
  2220. src0->type == GGML_TYPE_Q3_K ||
  2221. src0->type == GGML_TYPE_Q4_K ||
  2222. src0->type == GGML_TYPE_Q5_K ||
  2223. src0->type == GGML_TYPE_Q6_K;
  2224. #endif // QK_K == 256
  2225. const bool use_mul_mat_vec_q = g_compute_capabilities[id] >= MIN_CC_DP4A && mul_mat_vec_q_implemented;
  2226. #endif
  2227. if (use_mul_mat_vec_q) {
  2228. int64_t padded_row_size = ne00 + MATRIX_ROW_PADDING - 1;
  2229. padded_row_size -= padded_row_size % MATRIX_ROW_PADDING;
  2230. size_t as;
  2231. void * src1_q8_1 = ggml_cuda_pool_malloc(padded_row_size*sizeof(block_q8_1)/QK8_1, &as);
  2232. quantize_row_q8_1_cuda(src1_ddf_i, src1_q8_1, ne00, padded_row_size, cudaStream_main);
  2233. switch (src0->type) {
  2234. case GGML_TYPE_Q4_0:
  2235. mul_mat_vec_q4_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2236. break;
  2237. case GGML_TYPE_Q4_1:
  2238. mul_mat_vec_q4_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2239. break;
  2240. case GGML_TYPE_Q5_0:
  2241. mul_mat_vec_q5_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2242. break;
  2243. case GGML_TYPE_Q5_1:
  2244. mul_mat_vec_q5_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2245. break;
  2246. case GGML_TYPE_Q8_0:
  2247. mul_mat_vec_q8_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2248. break;
  2249. case GGML_TYPE_Q2_K:
  2250. mul_mat_vec_q2_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2251. break;
  2252. case GGML_TYPE_Q3_K:
  2253. mul_mat_vec_q3_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2254. break;
  2255. case GGML_TYPE_Q4_K:
  2256. mul_mat_vec_q4_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2257. break;
  2258. case GGML_TYPE_Q5_K:
  2259. mul_mat_vec_q5_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2260. break;
  2261. case GGML_TYPE_Q6_K:
  2262. mul_mat_vec_q6_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  2263. break;
  2264. default:
  2265. GGML_ASSERT(false);
  2266. break;
  2267. }
  2268. ggml_cuda_pool_free(src1_q8_1, as);
  2269. } else {
  2270. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  2271. #ifdef GGML_CUDA_DMMV_F16
  2272. size_t ash;
  2273. dfloat * src1_dfloat = nullptr; // dfloat == half
  2274. bool src1_convert_f16 = src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  2275. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  2276. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  2277. if (src1_convert_f16) {
  2278. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  2279. ggml_cpy_f32_f16_cuda((char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  2280. ne00, 1, sizeof(float), 0, 0,
  2281. ne00, 1, sizeof(half), 0, 0, cudaStream_main);
  2282. }
  2283. #else
  2284. dfloat * src1_dfloat = src1_ddf_i; // dfloat == float, no conversion
  2285. #endif // GGML_CUDA_DMMV_F16
  2286. switch (src0->type) {
  2287. case GGML_TYPE_Q4_0:
  2288. dequantize_mul_mat_vec_q4_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  2289. break;
  2290. case GGML_TYPE_Q4_1:
  2291. dequantize_mul_mat_vec_q4_1_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  2292. break;
  2293. case GGML_TYPE_Q5_0:
  2294. dequantize_mul_mat_vec_q5_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  2295. break;
  2296. case GGML_TYPE_Q5_1:
  2297. dequantize_mul_mat_vec_q5_1_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  2298. break;
  2299. case GGML_TYPE_Q8_0:
  2300. dequantize_mul_mat_vec_q8_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  2301. break;
  2302. case GGML_TYPE_Q2_K:
  2303. dequantize_mul_mat_vec_q2_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  2304. break;
  2305. case GGML_TYPE_Q3_K:
  2306. dequantize_mul_mat_vec_q3_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  2307. break;
  2308. case GGML_TYPE_Q4_K:
  2309. dequantize_mul_mat_vec_q4_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  2310. break;
  2311. case GGML_TYPE_Q5_K:
  2312. dequantize_mul_mat_vec_q5_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  2313. break;
  2314. case GGML_TYPE_Q6_K:
  2315. dequantize_mul_mat_vec_q6_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  2316. break;
  2317. case GGML_TYPE_F16:
  2318. convert_mul_mat_vec_f16_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  2319. break;
  2320. default:
  2321. GGML_ASSERT(false);
  2322. break;
  2323. }
  2324. #ifdef GGML_CUDA_DMMV_F16
  2325. if (src1_convert_f16) {
  2326. ggml_cuda_pool_free(src1_dfloat, ash);
  2327. }
  2328. #endif // GGML_CUDA_DMMV_F16
  2329. }
  2330. (void) src1;
  2331. (void) dst;
  2332. (void) src0_ddf_i;
  2333. (void) i02;
  2334. (void) i1;
  2335. }
  2336. inline void ggml_cuda_op_mul_mat_cublas(
  2337. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2338. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2339. cudaStream_t & cudaStream_main){
  2340. GGML_ASSERT(src0_ddf_i != nullptr);
  2341. GGML_ASSERT(src1_ddf_i != nullptr);
  2342. GGML_ASSERT(dst_ddf_i != nullptr);
  2343. const float alpha = 1.0f;
  2344. const float beta = 0.0f;
  2345. const int64_t ne00 = src0->ne[0];
  2346. const int64_t ne10 = src1->ne[0];
  2347. const int64_t ne11 = src1->ne[1];
  2348. const int64_t ne0 = dst->ne[0];
  2349. const int64_t i01_diff = i01_high - i01_low;
  2350. int id;
  2351. CUDA_CHECK(cudaGetDevice(&id));
  2352. // the main device has a larger memory buffer to hold the results from all GPUs
  2353. // ldc == nrows of the matrix that cuBLAS writes into
  2354. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : i01_diff;
  2355. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], cudaStream_main));
  2356. CUBLAS_CHECK(
  2357. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  2358. i01_diff, ne11, ne10,
  2359. &alpha, src0_ddf_i, ne00,
  2360. src1_ddf_i, ne10,
  2361. &beta, dst_ddf_i, ldc));
  2362. (void) dst;
  2363. (void) src0_ddq_i;
  2364. (void) i02;
  2365. (void) i1;
  2366. }
  2367. inline void ggml_cuda_op_rope(
  2368. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2369. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2370. cudaStream_t & cudaStream_main){
  2371. GGML_ASSERT(src0_ddf_i != nullptr);
  2372. GGML_ASSERT(dst_ddf_i != nullptr);
  2373. const int64_t ne00 = src0->ne[0];
  2374. const int64_t i01_diff = i01_high - i01_low;
  2375. const int n_past = ((int32_t *) src1->data)[0];
  2376. const int n_dims = ((int32_t *) src1->data)[1];
  2377. const int mode = ((int32_t *) src1->data)[2];
  2378. const int n_ctx = ((int32_t *) src1->data)[3];
  2379. const float theta_scale = powf(10000.0, -2.0f/n_dims);
  2380. const float p = ((mode & 1) == 0 ? n_past + i02 : i02);
  2381. bool is_glm = mode & 4;
  2382. // compute
  2383. if (is_glm) {
  2384. const float id_p = min(p, n_ctx - 2.f);
  2385. const float block_p = max(p - (n_ctx - 2.f), 0.f);
  2386. rope_glm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, id_p, block_p, theta_scale, cudaStream_main);
  2387. } else {
  2388. rope_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, p, theta_scale, cudaStream_main);
  2389. }
  2390. (void) dst;
  2391. (void) src0_ddq_i;
  2392. (void) src1_ddf_i;
  2393. (void) i1;
  2394. }
  2395. inline void ggml_cuda_op_diag_mask_inf(
  2396. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2397. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2398. cudaStream_t & cudaStream_main){
  2399. GGML_ASSERT(src0_ddf_i != nullptr);
  2400. GGML_ASSERT(dst_ddf_i != nullptr);
  2401. const int64_t ne00 = src0->ne[0];
  2402. const int64_t ne01 = src0->ne[1];
  2403. const int64_t i01_diff = i01_high - i01_low;
  2404. const int n_past = ((int32_t *) src1->data)[0];
  2405. // compute
  2406. diag_mask_inf_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, ne01, n_past, cudaStream_main);
  2407. (void) dst;
  2408. (void) src0_ddq_i;
  2409. (void) src1_ddf_i;
  2410. (void) i02;
  2411. (void) i1;
  2412. }
  2413. inline void ggml_cuda_op_soft_max(
  2414. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2415. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2416. cudaStream_t & cudaStream_main){
  2417. GGML_ASSERT(src0_ddf_i != nullptr);
  2418. GGML_ASSERT(dst_ddf_i != nullptr);
  2419. const int64_t ne00 = src0->ne[0];
  2420. const int64_t i01_diff = i01_high - i01_low;
  2421. // compute
  2422. soft_max_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  2423. (void) src1;
  2424. (void) dst;
  2425. (void) src0_ddq_i;
  2426. (void) src1_ddf_i;
  2427. (void) i02;
  2428. (void) i1;
  2429. }
  2430. inline void ggml_cuda_op_scale(
  2431. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  2432. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  2433. cudaStream_t & cudaStream_main){
  2434. GGML_ASSERT(src0_ddf_i != nullptr);
  2435. GGML_ASSERT(dst_ddf_i != nullptr);
  2436. const float scale = ((float *) src1->data)[0];
  2437. const int64_t ne00 = src0->ne[0];
  2438. const int64_t i01_diff = i01_high - i01_low;
  2439. // compute
  2440. scale_f32_cuda(src0_ddf_i, dst_ddf_i, scale, ne00*i01_diff, cudaStream_main);
  2441. CUDA_CHECK(cudaGetLastError());
  2442. (void) src1;
  2443. (void) dst;
  2444. (void) src0_ddq_i;
  2445. (void) src1_ddf_i;
  2446. (void) i02;
  2447. (void) i1;
  2448. }
  2449. static void ggml_cuda_op(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  2450. ggml_cuda_op_t op, bool src0_needs_f32, bool flatten_rows) {
  2451. const int64_t ne00 = src0->ne[0];
  2452. const int64_t ne01 = src0->ne[1];
  2453. const int64_t ne02 = src0->ne[2];
  2454. const int64_t ne03 = src0->ne[3];
  2455. const int64_t nrows0 = ggml_nrows(src0);
  2456. const bool use_src1 = src1 != nullptr;
  2457. const int64_t ne10 = use_src1 ? src1->ne[0] : 1;
  2458. const int64_t ne11 = use_src1 ? src1->ne[1] : 1;
  2459. const int64_t ne12 = use_src1 ? src1->ne[2] : 1;
  2460. const int64_t ne13 = use_src1 ? src1->ne[3] : 1;
  2461. const int64_t ne0 = dst->ne[0];
  2462. const int64_t ne1 = dst->ne[1];
  2463. const int nb2 = dst->nb[2];
  2464. const int nb3 = dst->nb[3];
  2465. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  2466. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  2467. // strides for iteration over dims 3 and 2
  2468. const int64_t num_iters = flatten_rows ? 1 : ne02 * ne03;
  2469. const int64_t stride_mod = flatten_rows ? ne02 * ne03 : 1;
  2470. const int64_t src0_stride = ne00 * ne01 * stride_mod;
  2471. const int64_t src1_stride = ne10 * ne11 * stride_mod;
  2472. const int64_t dst_stride = ne0 * ne1 * stride_mod;
  2473. const size_t src0_ts = ggml_type_size(src0->type);
  2474. const size_t src0_bs = ggml_blck_size(src0->type);
  2475. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2476. struct ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  2477. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  2478. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  2479. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  2480. const bool src0_is_f32 = src0->type == GGML_TYPE_F32;
  2481. const bool src1_is_contiguous = use_src1 && ggml_is_contiguous(src1);
  2482. const bool src1_stays_on_host = use_src1 && (
  2483. dst->op == GGML_OP_SCALE || dst->op == GGML_OP_DIAG_MASK_INF || dst->op == GGML_OP_ROPE);
  2484. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  2485. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  2486. // dd = data device
  2487. char * src0_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // quantized
  2488. float * src0_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  2489. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr};
  2490. float * dst_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr};
  2491. // asq = actual size quantized, asf = actual size float
  2492. size_t src0_asq[GGML_CUDA_MAX_DEVICES] = {0};
  2493. size_t src0_asf[GGML_CUDA_MAX_DEVICES] = {0};
  2494. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  2495. size_t dst_asf[GGML_CUDA_MAX_DEVICES] = {0};
  2496. // if multiple devices are used they need to wait for the main device
  2497. // here an event is recorded that signifies that the main device has finished calculating the input data
  2498. if (split && g_device_count > 1) {
  2499. CUDA_CHECK(cudaSetDevice(g_main_device));
  2500. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device], g_cudaStreams_main[g_main_device]));
  2501. }
  2502. for (int id = 0; id < g_device_count; ++id) {
  2503. if (!split && id != g_main_device) {
  2504. continue;
  2505. }
  2506. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  2507. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  2508. int64_t row_low, row_high;
  2509. if (split) {
  2510. row_low = id == 0 ? 0 : nrows0*g_tensor_split[id];
  2511. row_high = id == g_device_count - 1 ? nrows0 : nrows0*g_tensor_split[id + 1];
  2512. } else {
  2513. row_low = 0;
  2514. row_high = nrows0;
  2515. }
  2516. if (row_low == row_high) {
  2517. continue;
  2518. }
  2519. int64_t row_diff = row_high - row_low;
  2520. cudaSetDevice(id);
  2521. cudaStream_t cudaStream_main = g_cudaStreams_main[id];
  2522. // wait for main GPU data if necessary
  2523. if (split && id != g_main_device) {
  2524. CUDA_CHECK(cudaStreamWaitEvent(cudaStream_main, src0_extra->events[g_main_device]));
  2525. }
  2526. if (src0_on_device && src0_is_contiguous) {
  2527. if (src0_is_f32) {
  2528. src0_ddf[id] = (float *) src0_extra->data_device[id];
  2529. } else {
  2530. src0_ddq[id] = (char *) src0_extra->data_device[id];
  2531. }
  2532. } else {
  2533. if (src0_is_f32) {
  2534. src0_ddf[id] = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_asf[id]);
  2535. } else {
  2536. src0_ddq[id] = (char *) ggml_cuda_pool_malloc(row_diff*ne00 * src0_ts/src0_bs, &src0_asq[id]);
  2537. }
  2538. }
  2539. if (src0_needs_f32 && !src0_is_f32) {
  2540. src0_ddf[id] = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_asf[id]);
  2541. }
  2542. if (use_src1 && !src1_stays_on_host) {
  2543. if (src1_on_device && src1_is_contiguous) {
  2544. src1_ddf[id] = (float *) src1_extra->data_device[id];
  2545. } else {
  2546. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(num_iters*src1_stride * sizeof(float), &src1_asf[id]);
  2547. }
  2548. }
  2549. if (dst_on_device) {
  2550. dst_ddf[id] = (float *) dst_extra->data_device[id];
  2551. } else {
  2552. size_t size_dst_ddf = split ? row_diff*ne1 * sizeof(float) : num_iters*dst_stride * sizeof(float);
  2553. dst_ddf[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_asf[id]);
  2554. }
  2555. const int64_t i03_max = flatten_rows ? 1 : ne03;
  2556. const int64_t i02_max = flatten_rows ? 1 : ne02;
  2557. const int64_t rows_per_iter = flatten_rows ? nrows0 : ne01;
  2558. for (int64_t i03 = 0; i03 < i03_max; i03++) {
  2559. const int64_t i13 = i03 % ne13;
  2560. for (int64_t i02 = 0; i02 < i02_max; i02++) {
  2561. const int64_t i12 = i02 % ne12;
  2562. const int64_t i0 = i03*ne02 + i02;
  2563. // i0 values that contain the lower/upper rows for a split tensor when using multiple GPUs
  2564. const int64_t i0_offset_low = row_low/rows_per_iter;
  2565. const int64_t i0_offset_high = row_high/rows_per_iter;
  2566. int64_t i01_low = 0;
  2567. int64_t i01_high = rows_per_iter;
  2568. if (split) {
  2569. if (i0 < i0_offset_low || i0 > i0_offset_high) {
  2570. continue;
  2571. }
  2572. if (i0 == i0_offset_low) {
  2573. i01_low = row_low % rows_per_iter;
  2574. }
  2575. if (i0 == i0_offset_high) {
  2576. i01_high = row_high % rows_per_iter;
  2577. }
  2578. }
  2579. // There is possibly a bug in the Windows nvcc compiler regarding instruction reordering or optimizing out local variables.
  2580. // Removing the first assert or changing the order of the arguments causes the second assert to fail.
  2581. // Removing both asserts results in i01_high becoming 0 which in turn results in garbage output.
  2582. // The root cause seems to be a problem with i0_offset_high becoming 0 when it should always be >0 (for single GPU).
  2583. GGML_ASSERT(i01_low == 0 || g_device_count > 1);
  2584. GGML_ASSERT(i01_high == rows_per_iter || g_device_count > 1);
  2585. const int64_t i01_diff = i01_high - i01_low;
  2586. if (i01_diff == 0) {
  2587. continue;
  2588. }
  2589. const int64_t i11 = i13*ne12 + i12;
  2590. // for split tensors the data begins at i0 == i0_offset_low
  2591. char * src0_ddq_i = src0_ddq[id] + (i0 - i0_offset_low)*src0_stride*src0_ts/src0_bs;
  2592. float * src0_ddf_i = src0_ddf[id] + (i0 - i0_offset_low)*src0_stride;
  2593. float * src1_ddf_i = src1_ddf[id] + i11*src1_stride;
  2594. float * dst_ddf_i = dst_ddf[id] + (i0 - i0_offset_low)*dst_stride;
  2595. // for split tensors the data pointer needs to be rounded down
  2596. // to the bin edge for i03, i02 bins beyond the first
  2597. if (i0 - i0_offset_low > 0) {
  2598. GGML_ASSERT(!flatten_rows);
  2599. src0_ddq_i -= (row_low % ne01)*ne00 * src0_ts/src0_bs;
  2600. src0_ddf_i -= (row_low % ne01)*ne00;
  2601. dst_ddf_i -= (row_low % ne0)*ne1;
  2602. }
  2603. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  2604. // in that case an offset on dst_ddf_i is needed
  2605. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  2606. dst_ddf_i += i01_low; // offset is 0 if no tensor split
  2607. }
  2608. // copy src0, src1 to device if necessary
  2609. if (use_src1 && !src1_stays_on_host) {
  2610. if (src1->backend == GGML_BACKEND_CPU) {
  2611. GGML_ASSERT(!flatten_rows || nrows0 == ggml_nrows(src1));
  2612. int64_t nrows1 = flatten_rows ? nrows0 : ne11;
  2613. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf_i, src1, i03, i02, 0, nrows1, cudaStream_main));
  2614. } else if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  2615. if (id != g_main_device) {
  2616. GGML_ASSERT(!flatten_rows);
  2617. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  2618. src1_ddf_i_source += i11*src1_stride;
  2619. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_stride*sizeof(float),
  2620. cudaMemcpyDeviceToDevice, cudaStream_main));
  2621. }
  2622. } else if (src1_on_device && !src1_is_contiguous) {
  2623. GGML_ASSERT(!split);
  2624. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf_i, src1, i03, i02, 0, ne11, cudaStream_main));
  2625. } else {
  2626. GGML_ASSERT(false);
  2627. }
  2628. }
  2629. if (!src0_on_device || !src0_is_contiguous) {
  2630. if (src0_is_f32) {
  2631. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf_i, src0, i03, i02, i01_low, i01_high, cudaStream_main));
  2632. } else {
  2633. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddq_i, src0, i03, i02, i01_low, i01_high, cudaStream_main));
  2634. }
  2635. }
  2636. // convert src0 to f32 if it is necessary for the ggml_cuda_op
  2637. if (src0_needs_f32 && !src0_is_f32) {
  2638. to_fp32_cuda(src0_ddq_i, src0_ddf_i, i01_diff*ne00, cudaStream_main);
  2639. CUDA_CHECK(cudaGetLastError());
  2640. }
  2641. // do the computation
  2642. op(src0, src1, dst, src0_ddq_i, src0_ddf_i, src1_ddf_i, dst_ddf_i, i02, i01_low, i01_high, i11, cudaStream_main);
  2643. CUDA_CHECK(cudaGetLastError());
  2644. // copy dst to host or other device if necessary
  2645. if (!dst_on_device) {
  2646. void * dst_off_device;
  2647. cudaMemcpyKind kind;
  2648. if (dst->backend == GGML_BACKEND_CPU) {
  2649. dst_off_device = dst->data;
  2650. kind = cudaMemcpyDeviceToHost;
  2651. } else if (dst->backend == GGML_BACKEND_GPU) {
  2652. dst_off_device = dst_extra->data_device[g_main_device];
  2653. kind = cudaMemcpyDeviceToDevice;
  2654. } else {
  2655. GGML_ASSERT(false);
  2656. }
  2657. if (split) {
  2658. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  2659. // dst is NOT transposed.
  2660. // The outputs of cuBLAS matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  2661. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  2662. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  2663. for (int64_t j = 0; j < ne1; ++j) {
  2664. float * dhf_dst_i = (float *) ((char *) dst_off_device + (j*ne0 + i01_low)*sizeof(float) + i02*nb2 + i03*nb3);
  2665. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_ddf_i + j*i01_diff, i01_diff*sizeof(float), kind, cudaStream_main));
  2666. }
  2667. } else {
  2668. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  2669. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_ddf_i, dst_stride*sizeof(float), kind, cudaStream_main));
  2670. }
  2671. }
  2672. // signify to main device that other device is done
  2673. if (split && g_device_count > 1 && id != g_main_device) {
  2674. CUDA_CHECK(cudaEventRecord(src0_extra->events[id], cudaStream_main));
  2675. }
  2676. }
  2677. }
  2678. }
  2679. // wait until each device is finished, then free their buffers
  2680. for (int id = 0; id < g_device_count; ++id) {
  2681. if (src0_asq[id] == 0 && src0_asf[id] == 0 && src1_asf[id] == 0 && dst_asf[id] == 0) {
  2682. continue;
  2683. }
  2684. CUDA_CHECK(cudaSetDevice(id));
  2685. if (src0_asq[id] > 0) {
  2686. ggml_cuda_pool_free(src0_ddq[id], src0_asq[id]);
  2687. }
  2688. if (src0_asf[id] > 0) {
  2689. ggml_cuda_pool_free(src0_ddf[id], src0_asf[id]);
  2690. }
  2691. if (src1_asf[id] > 0) {
  2692. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  2693. }
  2694. if (dst_asf[id] > 0) {
  2695. ggml_cuda_pool_free(dst_ddf[id], dst_asf[id]);
  2696. }
  2697. }
  2698. // main device waits for all other devices to be finished
  2699. if (split && g_device_count > 1) {
  2700. CUDA_CHECK(cudaSetDevice(g_main_device));
  2701. for (int id = 0; id < g_device_count; ++id) {
  2702. if (id != g_main_device) {
  2703. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams_main[g_main_device], src0_extra->events[id]));
  2704. }
  2705. }
  2706. }
  2707. if (dst->backend == GGML_BACKEND_CPU) {
  2708. CUDA_CHECK(cudaSetDevice(g_main_device));
  2709. CUDA_CHECK(cudaDeviceSynchronize());
  2710. }
  2711. }
  2712. void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2713. // ggml_cuda_add permits f16 dst even though this could in theory cause problems with the pointer arithmetic in ggml_cuda_op.
  2714. // Due to flatten_rows == true this does in practice not make a difference however.
  2715. // Better solution would be nice but right now that would require disproportionate changes.
  2716. GGML_ASSERT(
  2717. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) &&
  2718. src1->type == GGML_TYPE_F32 &&
  2719. (dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16));
  2720. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_add, false, true);
  2721. }
  2722. void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2723. GGML_ASSERT(src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2724. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul, true, false); // TODO ggml_cuda_op needs modification for flatten
  2725. }
  2726. void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2727. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2728. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_gelu, true, true);
  2729. }
  2730. void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2731. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2732. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_silu, true, true);
  2733. }
  2734. void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2735. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2736. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_norm, true, true);
  2737. }
  2738. void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2739. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2740. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_rms_norm, true, true);
  2741. }
  2742. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  2743. const int64_t ne10 = src1->ne[0];
  2744. const int64_t ne0 = dst->ne[0];
  2745. const int64_t ne1 = dst->ne[1];
  2746. // TODO: find the optimal values for these
  2747. if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  2748. src1->type == GGML_TYPE_F32 &&
  2749. dst->type == GGML_TYPE_F32 &&
  2750. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32)) {
  2751. return true;
  2752. }
  2753. return false;
  2754. }
  2755. void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  2756. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  2757. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  2758. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  2759. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  2760. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2761. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2762. const int64_t ne00 = src0->ne[0];
  2763. const int64_t ne01 = src0->ne[1];
  2764. const int64_t ne02 = src0->ne[2];
  2765. CUDA_CHECK(cudaSetDevice(g_main_device));
  2766. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  2767. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2768. void * src0_ddq = src0_extra->data_device[g_main_device];
  2769. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  2770. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  2771. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  2772. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  2773. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, cudaStream_main);
  2774. }
  2775. void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  2776. GGML_ASSERT(!ggml_is_contiguous(src0) && ggml_is_contiguous(src1));
  2777. GGML_ASSERT(!ggml_is_permuted(src0));
  2778. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  2779. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2780. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2781. const int64_t ne00 = src0->ne[0];
  2782. const int64_t ne01 = src0->ne[1];
  2783. const int64_t ne02 = src0->ne[2];
  2784. const int64_t nb01 = src0->nb[1];
  2785. const int64_t nb02 = src0->nb[2];
  2786. CUDA_CHECK(cudaSetDevice(g_main_device));
  2787. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  2788. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2789. void * src0_ddq = src0_extra->data_device[g_main_device];
  2790. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  2791. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  2792. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  2793. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  2794. const int row_stride_x = nb01 / sizeof(half);
  2795. const int channel_stride_x = nb02 / sizeof(half);
  2796. ggml_mul_mat_vec_nc_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, channel_stride_x, cudaStream_main);
  2797. }
  2798. void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2799. bool all_on_device = (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  2800. src1->backend == GGML_BACKEND_GPU && dst->backend == GGML_BACKEND_GPU;
  2801. if (all_on_device && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  2802. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  2803. } else if (all_on_device && !ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && src1->ne[1] == 1) {
  2804. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  2805. }else if (src0->type == GGML_TYPE_F32) {
  2806. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, true, false);
  2807. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  2808. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  2809. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_vec, false, false);
  2810. } else {
  2811. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, true, false);
  2812. }
  2813. } else {
  2814. GGML_ASSERT(false);
  2815. }
  2816. }
  2817. void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2818. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2819. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_scale, true, true);
  2820. }
  2821. void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2822. const int64_t ne = ggml_nelements(src0);
  2823. GGML_ASSERT(ne == ggml_nelements(src1));
  2824. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  2825. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  2826. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  2827. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  2828. const int64_t ne00 = src0->ne[0];
  2829. const int64_t ne01 = src0->ne[1];
  2830. GGML_ASSERT(src0->ne[3] == 1);
  2831. const int64_t nb00 = src0->nb[0];
  2832. const int64_t nb01 = src0->nb[1];
  2833. const int64_t nb02 = src0->nb[2];
  2834. const int64_t ne10 = src1->ne[0];
  2835. const int64_t ne11 = src1->ne[1];
  2836. GGML_ASSERT(src1->ne[3] == 1);
  2837. const int64_t nb10 = src1->nb[0];
  2838. const int64_t nb11 = src1->nb[1];
  2839. const int64_t nb12 = src1->nb[2];
  2840. CUDA_CHECK(cudaSetDevice(g_main_device));
  2841. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  2842. const struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2843. const struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  2844. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  2845. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  2846. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  2847. ggml_cpy_f32_f32_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  2848. ne10, ne11, nb10, nb11, nb12, cudaStream_main);
  2849. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  2850. ggml_cpy_f32_f16_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  2851. ne10, ne11, nb10, nb11, nb12, cudaStream_main);
  2852. } else {
  2853. GGML_ASSERT(false);
  2854. }
  2855. (void) dst;
  2856. }
  2857. void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2858. ggml_cuda_cpy(src0, dst, nullptr);
  2859. (void) src1;
  2860. }
  2861. void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2862. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2863. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_diag_mask_inf, true, true);
  2864. }
  2865. void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2866. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2867. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_soft_max, true, true);
  2868. }
  2869. void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2870. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  2871. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_rope, true, false); // FIXME flatten changes results
  2872. }
  2873. void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2874. (void) src0;
  2875. (void) src1;
  2876. (void) dst;
  2877. }
  2878. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  2879. int nrows = ggml_nrows(tensor);
  2880. const int64_t ne0 = tensor->ne[0];
  2881. const size_t nb1 = tensor->nb[1];
  2882. ggml_backend backend = tensor->backend;
  2883. struct ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  2884. memset(extra, 0, sizeof(*extra));
  2885. for (int id = 0; id < g_device_count; ++id) {
  2886. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  2887. continue;
  2888. }
  2889. cudaSetDevice(id);
  2890. int row_low, row_high;
  2891. if (backend == GGML_BACKEND_GPU) {
  2892. row_low = 0;
  2893. row_high = nrows;
  2894. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  2895. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  2896. row_high = id == g_device_count - 1 ? nrows : nrows*g_tensor_split[id + 1];
  2897. } else {
  2898. GGML_ASSERT(false);
  2899. }
  2900. if (row_low == row_high) {
  2901. continue;
  2902. }
  2903. int64_t nrows_split = row_high - row_low;
  2904. const size_t offset_split = row_low*nb1;
  2905. size_t size = ggml_nbytes_split(tensor, nrows_split);
  2906. const size_t original_size = size;
  2907. // pad last row to a multiple of 256 elements to avoid out-of-bounds memory accesses
  2908. if (ne0 % MATRIX_ROW_PADDING != 0) {
  2909. size += (MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING)
  2910. * ggml_type_size(tensor->type)/ggml_blck_size(tensor->type);
  2911. }
  2912. char * buf;
  2913. CUDA_CHECK(cudaMalloc(&buf, size));
  2914. char * buf_host = (char*)data + offset_split;
  2915. // set padding to 0 to avoid possible NaN values
  2916. if (size > original_size) {
  2917. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  2918. }
  2919. CUDA_CHECK(cudaMemcpy(buf, buf_host, size, cudaMemcpyHostToDevice));
  2920. extra->data_device[id] = buf;
  2921. if (backend == GGML_BACKEND_GPU_SPLIT) {
  2922. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id], cudaEventDisableTiming));
  2923. }
  2924. }
  2925. tensor->extra = extra;
  2926. }
  2927. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  2928. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  2929. return;
  2930. }
  2931. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  2932. for (int id = 0; id < g_device_count; ++id) {
  2933. if (extra->data_device[id] != nullptr) {
  2934. CUDA_CHECK(cudaSetDevice(id));
  2935. CUDA_CHECK(cudaFree(extra->data_device[id]));
  2936. }
  2937. if (extra->events[id] != nullptr) {
  2938. CUDA_CHECK(cudaSetDevice(id));
  2939. CUDA_CHECK(cudaEventDestroy(extra->events[id]));
  2940. }
  2941. }
  2942. delete extra;
  2943. }
  2944. static struct ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  2945. static size_t g_temp_tensor_extra_index = 0;
  2946. static struct ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  2947. if (g_temp_tensor_extras == nullptr) {
  2948. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_MAX_NODES];
  2949. }
  2950. size_t alloc_index = g_temp_tensor_extra_index;
  2951. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_MAX_NODES;
  2952. struct ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  2953. memset(extra, 0, sizeof(*extra));
  2954. return extra;
  2955. }
  2956. void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace) {
  2957. if (scratch && g_scratch_size == 0) {
  2958. return;
  2959. }
  2960. // recursively assign CUDA buffers until a compute tensor is found
  2961. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  2962. const ggml_op src0_op = tensor->src[0]->op;
  2963. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  2964. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace);
  2965. }
  2966. }
  2967. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  2968. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace);
  2969. }
  2970. tensor->backend = GGML_BACKEND_GPU;
  2971. struct ggml_tensor_extra_gpu * extra;
  2972. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  2973. tensor->op == GGML_OP_VIEW ||
  2974. force_inplace;
  2975. const size_t size = ggml_nbytes(tensor);
  2976. CUDA_CHECK(cudaSetDevice(g_main_device));
  2977. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  2978. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  2979. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  2980. size_t offset = 0;
  2981. if (tensor->op == GGML_OP_VIEW) {
  2982. memcpy(&offset, tensor->src[2]->data, sizeof(size_t));
  2983. }
  2984. extra = ggml_cuda_alloc_temp_tensor_extra();
  2985. extra->data_device[g_main_device] = src0_ddc + offset;
  2986. } else if (tensor->op == GGML_OP_CPY) {
  2987. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  2988. void * src1_ddv = src1_extra->data_device[g_main_device];
  2989. extra = ggml_cuda_alloc_temp_tensor_extra();
  2990. extra->data_device[g_main_device] = src1_ddv;
  2991. } else if (scratch) {
  2992. GGML_ASSERT(size <= g_scratch_size);
  2993. if (g_scratch_offset + size > g_scratch_size) {
  2994. g_scratch_offset = 0;
  2995. }
  2996. char * data = (char *) g_scratch_buffer;
  2997. if (data == nullptr) {
  2998. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  2999. g_scratch_buffer = data;
  3000. }
  3001. extra = ggml_cuda_alloc_temp_tensor_extra();
  3002. extra->data_device[g_main_device] = data + g_scratch_offset;
  3003. g_scratch_offset += size;
  3004. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  3005. } else { // allocate new buffers outside of scratch
  3006. void * data;
  3007. CUDA_CHECK(cudaMalloc(&data, size));
  3008. CUDA_CHECK(cudaMemset(data, 0, size));
  3009. extra = new ggml_tensor_extra_gpu;
  3010. memset(extra, 0, sizeof(*extra));
  3011. extra->data_device[g_main_device] = data;
  3012. }
  3013. tensor->extra = extra;
  3014. }
  3015. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  3016. ggml_cuda_assign_buffers_impl(tensor, true, false);
  3017. }
  3018. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  3019. ggml_cuda_assign_buffers_impl(tensor, false, false);
  3020. }
  3021. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  3022. ggml_cuda_assign_buffers_impl(tensor, false, true);
  3023. }
  3024. void ggml_cuda_set_main_device(int main_device) {
  3025. if (main_device >= g_device_count) {
  3026. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  3027. main_device, g_device_count, g_main_device);
  3028. return;
  3029. }
  3030. g_main_device = main_device;
  3031. if (g_device_count > 1) {
  3032. cudaDeviceProp prop;
  3033. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  3034. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  3035. }
  3036. }
  3037. void ggml_cuda_set_scratch_size(size_t scratch_size) {
  3038. g_scratch_size = scratch_size;
  3039. }
  3040. void ggml_cuda_free_scratch() {
  3041. if (g_scratch_buffer == nullptr) {
  3042. return;
  3043. }
  3044. CUDA_CHECK(cudaFree(g_scratch_buffer));
  3045. g_scratch_buffer = nullptr;
  3046. }
  3047. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor){
  3048. ggml_cuda_func_t func;
  3049. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  3050. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  3051. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  3052. switch (tensor->op) {
  3053. case GGML_OP_DUP:
  3054. if (!any_on_device) {
  3055. return false;
  3056. }
  3057. func = ggml_cuda_dup;
  3058. break;
  3059. case GGML_OP_ADD:
  3060. if (!any_on_device) {
  3061. return false;
  3062. }
  3063. func = ggml_cuda_add;
  3064. break;
  3065. case GGML_OP_MUL:
  3066. if (!any_on_device) {
  3067. return false;
  3068. }
  3069. func = ggml_cuda_mul;
  3070. break;
  3071. case GGML_OP_GELU:
  3072. if (!any_on_device) {
  3073. return false;
  3074. }
  3075. func = ggml_cuda_gelu;
  3076. break;
  3077. case GGML_OP_SILU:
  3078. if (!any_on_device) {
  3079. return false;
  3080. }
  3081. func = ggml_cuda_silu;
  3082. break;
  3083. case GGML_OP_NORM:
  3084. if (!any_on_device) {
  3085. return false;
  3086. }
  3087. func = ggml_cuda_norm;
  3088. break;
  3089. case GGML_OP_RMS_NORM:
  3090. if (!any_on_device) {
  3091. return false;
  3092. }
  3093. func = ggml_cuda_rms_norm;
  3094. break;
  3095. case GGML_OP_MUL_MAT:
  3096. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  3097. return false;
  3098. }
  3099. func = ggml_cuda_mul_mat;
  3100. break;
  3101. case GGML_OP_SCALE:
  3102. if (!any_on_device) {
  3103. return false;
  3104. }
  3105. func = ggml_cuda_scale;
  3106. break;
  3107. case GGML_OP_CPY:
  3108. if (!any_on_device) {
  3109. return false;
  3110. }
  3111. func = ggml_cuda_cpy;
  3112. break;
  3113. case GGML_OP_CONT:
  3114. if (!any_on_device) {
  3115. return false;
  3116. }
  3117. func = ggml_cuda_dup;
  3118. break;
  3119. case GGML_OP_RESHAPE:
  3120. case GGML_OP_VIEW:
  3121. case GGML_OP_PERMUTE:
  3122. case GGML_OP_TRANSPOSE:
  3123. if (!any_on_device) {
  3124. return false;
  3125. }
  3126. func = ggml_cuda_nop;
  3127. break;
  3128. case GGML_OP_DIAG_MASK_INF:
  3129. if (!any_on_device) {
  3130. return false;
  3131. }
  3132. func = ggml_cuda_diag_mask_inf;
  3133. break;
  3134. case GGML_OP_SOFT_MAX:
  3135. if (!any_on_device) {
  3136. return false;
  3137. }
  3138. func = ggml_cuda_soft_max;
  3139. break;
  3140. case GGML_OP_ROPE:
  3141. if (!any_on_device) {
  3142. return false;
  3143. }
  3144. func = ggml_cuda_rope;
  3145. break;
  3146. default:
  3147. return false;
  3148. }
  3149. if (params->ith != 0) {
  3150. return true;
  3151. }
  3152. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  3153. return true;
  3154. }
  3155. func(tensor->src[0], tensor->src[1], tensor);
  3156. return true;
  3157. }