ggml-cuda.cu 128 KB

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