ggml-cuda.cu 127 KB

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