ggml-cuda.cu 155 KB

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