ggml-cuda.cu 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371
  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 * x, float * y, int k, cudaStream_t stream);
  55. typedef void (*dot_kernel_k_t)(const void * vx, const int ib, const int iqs, const float * 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 * vbq, const block_q8_1 * 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 CUDA_ADD_BLOCK_SIZE 256
  188. #define CUDA_MUL_BLOCK_SIZE 256
  189. #define CUDA_SILU_BLOCK_SIZE 256
  190. #define CUDA_CPY_BLOCK_SIZE 32
  191. #define CUDA_SCALE_BLOCK_SIZE 256
  192. #define CUDA_ROPE_BLOCK_SIZE 256
  193. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  194. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  195. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  196. // dmmv = dequantize_mul_mat_vec
  197. #ifndef GGML_CUDA_DMMV_X
  198. #define GGML_CUDA_DMMV_X 32
  199. #endif
  200. #ifndef GGML_CUDA_MMV_Y
  201. #define GGML_CUDA_MMV_Y 1
  202. #endif
  203. #ifndef K_QUANTS_PER_ITERATION
  204. #define K_QUANTS_PER_ITERATION 2
  205. #else
  206. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  207. #endif
  208. struct ggml_tensor_extra_gpu {
  209. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  210. cudaEvent_t events[GGML_CUDA_MAX_DEVICES]; // events for synchronizing multiple GPUs
  211. };
  212. static __global__ void add_f32(const float * x, const float * y, float * dst, const int k) {
  213. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  214. if (i >= k) {
  215. return;
  216. }
  217. dst[i] = x[i] + y[i];
  218. }
  219. static __global__ void add_f16_f32_f16(const half * x, const float * y, half * dst, const int k) {
  220. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  221. if (i >= k) {
  222. return;
  223. }
  224. dst[i] = __hadd(x[i], __float2half(y[i]));
  225. }
  226. static __global__ void mul_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  227. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  228. if (i >= kx) {
  229. return;
  230. }
  231. dst[i] = x[i] * y[i%ky];
  232. }
  233. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  234. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  235. if (i >= k) {
  236. return;
  237. }
  238. dst[i] = x[i] / (1.0f + expf(-x[i]));
  239. }
  240. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols) {
  241. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  242. const int tid = threadIdx.x;
  243. const float eps = 1e-6;
  244. float tmp = 0.0f; // partial sum for thread in warp
  245. for (int i = 0; i < ncols; i += WARP_SIZE) {
  246. const int col = i + tid;
  247. const float xi = x[row*ncols + col];
  248. tmp += xi * xi;
  249. }
  250. // sum up partial sums
  251. #pragma unroll
  252. for (int mask = 16; mask > 0; mask >>= 1) {
  253. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  254. }
  255. const float mean = tmp / ncols;
  256. const float scale = 1.0f / sqrtf(mean + eps);
  257. for (int i = 0; i < ncols; i += WARP_SIZE) {
  258. const int col = i + tid;
  259. dst[row*ncols + col] = scale * x[row*ncols + col];
  260. }
  261. }
  262. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  263. const block_q4_0 * x = (const block_q4_0 *) vx;
  264. const dfloat d = x[ib].d;
  265. const int vui = x[ib].qs[iqs];
  266. v.x = vui & 0xF;
  267. v.y = vui >> 4;
  268. #ifdef GGML_CUDA_DMMV_F16
  269. v = __hsub2(v, {8.0f, 8.0f});
  270. v = __hmul2(v, {d, d});
  271. #else
  272. v.x = (v.x - 8.0f) * d;
  273. v.y = (v.y - 8.0f) * d;
  274. #endif // GGML_CUDA_DMMV_F16
  275. }
  276. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  277. const block_q4_1 * x = (const block_q4_1 *) vx;
  278. const dfloat d = x[ib].d;
  279. const dfloat m = x[ib].m;
  280. const int vui = x[ib].qs[iqs];
  281. v.x = vui & 0xF;
  282. v.y = vui >> 4;
  283. #ifdef GGML_CUDA_DMMV_F16
  284. v = __hmul2(v, {d, d});
  285. v = __hadd2(v, {m, m});
  286. #else
  287. v.x = (v.x * d) + m;
  288. v.y = (v.y * d) + m;
  289. #endif // GGML_CUDA_DMMV_F16
  290. }
  291. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  292. const block_q5_0 * x = (const block_q5_0 *) vx;
  293. const dfloat d = x[ib].d;
  294. uint32_t qh;
  295. memcpy(&qh, x[ib].qh, sizeof(qh));
  296. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  297. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  298. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  299. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  300. #ifdef GGML_CUDA_DMMV_F16
  301. v = __hsub2(v, {16.0f, 16.0f});
  302. v = __hmul2(v, {d, d});
  303. #else
  304. v.x = (v.x - 16.0f) * d;
  305. v.y = (v.y - 16.0f) * d;
  306. #endif // GGML_CUDA_DMMV_F16
  307. }
  308. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  309. const block_q5_1 * x = (const block_q5_1 *) vx;
  310. const dfloat d = x[ib].d;
  311. const dfloat m = x[ib].m;
  312. uint32_t qh;
  313. memcpy(&qh, x[ib].qh, sizeof(qh));
  314. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  315. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  316. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  317. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  318. #ifdef GGML_CUDA_DMMV_F16
  319. v = __hmul2(v, {d, d});
  320. v = __hadd2(v, {m, m});
  321. #else
  322. v.x = (v.x * d) + m;
  323. v.y = (v.y * d) + m;
  324. #endif // GGML_CUDA_DMMV_F16
  325. }
  326. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  327. const block_q8_0 * x = (const block_q8_0 *) vx;
  328. const dfloat d = x[ib].d;
  329. v.x = x[ib].qs[iqs + 0];
  330. v.y = x[ib].qs[iqs + 1];
  331. #ifdef GGML_CUDA_DMMV_F16
  332. v = __hmul2(v, {d, d});
  333. #else
  334. v.x *= d;
  335. v.y *= d;
  336. #endif // GGML_CUDA_DMMV_F16
  337. }
  338. //================================== k-quants
  339. static __global__ void dequantize_block_q2_K(const void * vx, float * yy) {
  340. const int i = blockIdx.x;
  341. const block_q2_K * x = (const block_q2_K *) vx;
  342. const int tid = threadIdx.x;
  343. #if QK_K == 256
  344. const int n = tid/32;
  345. const int l = tid - 32*n;
  346. const int is = 8*n + l/16;
  347. const uint8_t q = x[i].qs[32*n + l];
  348. float * y = yy + i*QK_K + 128*n;
  349. float dall = x[i].d;
  350. float dmin = x[i].dmin;
  351. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  352. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  353. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  354. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  355. #else
  356. const int is = tid/16; // 0 or 1
  357. const int il = tid%16; // 0...15
  358. const uint8_t q = x[i].qs[il] >> (2*is);
  359. float * y = yy + i*QK_K + 16*is + il;
  360. float dall = x[i].d;
  361. float dmin = x[i].dmin;
  362. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  363. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  364. #endif
  365. }
  366. static __global__ void dequantize_block_q3_K(const void * vx, float * yy) {
  367. const int i = blockIdx.x;
  368. const block_q3_K * x = (const block_q3_K *) vx;
  369. #if QK_K == 256
  370. const int r = threadIdx.x/4;
  371. const int tid = r/2;
  372. const int is0 = r%2;
  373. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  374. const int n = tid / 4;
  375. const int j = tid - 4*n;
  376. uint8_t m = 1 << (4*n + j);
  377. int is = 8*n + 2*j + is0;
  378. int shift = 2*j;
  379. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  380. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  381. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  382. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  383. float d_all = x[i].d;
  384. float dl = d_all * (us - 32);
  385. float * y = yy + i*QK_K + 128*n + 32*j;
  386. const uint8_t * q = x[i].qs + 32*n;
  387. const uint8_t * hm = x[i].hmask;
  388. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  389. #else
  390. const int tid = threadIdx.x;
  391. const int is = tid/16; // 0 or 1
  392. const int il = tid%16; // 0...15
  393. const int im = il/8; // 0...1
  394. const int in = il%8; // 0...7
  395. float * y = yy + i*QK_K + 16*is + il;
  396. const uint8_t q = x[i].qs[il] >> (2*is);
  397. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  398. const float d = (float)x[i].d;
  399. if (is == 0) {
  400. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  401. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  402. } else {
  403. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  404. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  405. }
  406. #endif
  407. }
  408. #if QK_K == 256
  409. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  410. if (j < 4) {
  411. d = q[j] & 63; m = q[j + 4] & 63;
  412. } else {
  413. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  414. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  415. }
  416. }
  417. #endif
  418. static __global__ void dequantize_block_q4_K(const void * vx, float * yy) {
  419. const block_q4_K * x = (const block_q4_K *) vx;
  420. const int i = blockIdx.x;
  421. #if QK_K == 256
  422. // assume 32 threads
  423. const int tid = threadIdx.x;
  424. const int il = tid/8;
  425. const int ir = tid%8;
  426. const int is = 2*il;
  427. const int n = 4;
  428. float * y = yy + i*QK_K + 64*il + n*ir;
  429. const float dall = x[i].d;
  430. const float dmin = x[i].dmin;
  431. const uint8_t * q = x[i].qs + 32*il + n*ir;
  432. uint8_t sc, m;
  433. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  434. const float d1 = dall * sc; const float m1 = dmin * m;
  435. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  436. const float d2 = dall * sc; const float m2 = dmin * m;
  437. for (int l = 0; l < n; ++l) {
  438. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  439. y[l +32] = d2 * (q[l] >> 4) - m2;
  440. }
  441. #else
  442. const int tid = threadIdx.x;
  443. const uint8_t * q = x[i].qs;
  444. float * y = yy + i*QK_K;
  445. const float d = (float)x[i].d[0];
  446. const float m = (float)x[i].d[1];
  447. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  448. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  449. #endif
  450. }
  451. static __global__ void dequantize_block_q5_K(const void * vx, float * yy) {
  452. const block_q5_K * x = (const block_q5_K *) vx;
  453. const int i = blockIdx.x;
  454. #if QK_K == 256
  455. // assume 64 threads - this is very slightly better than the one below
  456. const int tid = threadIdx.x;
  457. const int il = tid/16; // il is in 0...3
  458. const int ir = tid%16; // ir is in 0...15
  459. const int is = 2*il; // is is in 0...6
  460. float * y = yy + i*QK_K + 64*il + 2*ir;
  461. const float dall = x[i].d;
  462. const float dmin = x[i].dmin;
  463. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  464. const uint8_t * qh = x[i].qh + 2*ir;
  465. uint8_t sc, m;
  466. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  467. const float d1 = dall * sc; const float m1 = dmin * m;
  468. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  469. const float d2 = dall * sc; const float m2 = dmin * m;
  470. uint8_t hm = 1 << (2*il);
  471. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  472. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  473. hm <<= 1;
  474. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  475. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  476. #else
  477. const int tid = threadIdx.x;
  478. const uint8_t q = x[i].qs[tid];
  479. const int im = tid/8; // 0...3
  480. const int in = tid%8; // 0...7
  481. const int is = tid/16; // 0 or 1
  482. const uint8_t h = x[i].qh[in] >> im;
  483. const float d = x[i].d;
  484. float * y = yy + i*QK_K + tid;
  485. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  486. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  487. #endif
  488. }
  489. static __global__ void dequantize_block_q6_K(const void * vx, float * yy) {
  490. const block_q6_K * x = (const block_q6_K *) vx;
  491. const int i = blockIdx.x;
  492. #if QK_K == 256
  493. // assume 64 threads - this is very slightly better than the one below
  494. const int tid = threadIdx.x;
  495. const int ip = tid/32; // ip is 0 or 1
  496. const int il = tid - 32*ip; // 0...32
  497. const int is = 8*ip + il/16;
  498. float * y = yy + i*QK_K + 128*ip + il;
  499. const float d = x[i].d;
  500. const uint8_t * ql = x[i].ql + 64*ip + il;
  501. const uint8_t qh = x[i].qh[32*ip + il];
  502. const int8_t * sc = x[i].scales + is;
  503. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  504. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  505. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  506. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  507. #else
  508. // assume 32 threads
  509. const int tid = threadIdx.x;
  510. const int ip = tid/16; // 0 or 1
  511. const int il = tid - 16*ip; // 0...15
  512. float * y = yy + i*QK_K + 16*ip + il;
  513. const float d = x[i].d;
  514. const uint8_t ql = x[i].ql[16*ip + il];
  515. const uint8_t qh = x[i].qh[il] >> (2*ip);
  516. const int8_t * sc = x[i].scales;
  517. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  518. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  519. #endif
  520. }
  521. static __global__ void dequantize_mul_mat_vec_q2_k(const void * vx, const float * yy, float * dst, const int ncols, int nrows) {
  522. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  523. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  524. if (row > nrows) return;
  525. const int num_blocks_per_row = ncols / QK_K;
  526. const int ib0 = row*num_blocks_per_row;
  527. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  528. float tmp = 0; // partial sum for thread in warp
  529. #if QK_K == 256
  530. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  531. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  532. const int step = 16/K_QUANTS_PER_ITERATION;
  533. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  534. const int in = tid - step*im; // 0...15 or 0...7
  535. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  536. const int q_offset = 32*im + l0;
  537. const int s_offset = 8*im;
  538. const int y_offset = 128*im + l0;
  539. uint32_t aux[4];
  540. const uint8_t * d = (const uint8_t *)aux;
  541. const uint8_t * m = (const uint8_t *)(aux + 2);
  542. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  543. const float * y = yy + i * QK_K + y_offset;
  544. const uint8_t * q = x[i].qs + q_offset;
  545. const float dall = x[i].d;
  546. const float dmin = x[i].dmin;
  547. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  548. aux[0] = a[0] & 0x0f0f0f0f;
  549. aux[1] = a[1] & 0x0f0f0f0f;
  550. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  551. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  552. float sum1 = 0, sum2 = 0;
  553. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  554. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  555. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  556. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  557. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  558. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  559. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  560. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  561. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  562. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  563. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  564. }
  565. tmp += dall * sum1 - dmin * sum2;
  566. }
  567. #else
  568. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  569. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  570. const int offset = tid * K_QUANTS_PER_ITERATION;
  571. uint32_t uaux[2];
  572. const uint8_t * d = (const uint8_t *)uaux;
  573. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  574. const float * y = yy + i * QK_K + offset;
  575. const uint8_t * q = x[i].qs + offset;
  576. const uint32_t * s = (const uint32_t *)x[i].scales;
  577. uaux[0] = s[0] & 0x0f0f0f0f;
  578. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  579. const half2 * dh = (const half2 *)&x[i].d;
  580. const float2 dall = __half22float2(dh[0]);
  581. float sum1 = 0, sum2 = 0;
  582. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  583. const uint8_t ql = q[l];
  584. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  585. + y[l+16] * d[1] * ((ql >> 2) & 3)
  586. + y[l+32] * d[2] * ((ql >> 4) & 3)
  587. + y[l+48] * d[3] * ((ql >> 6) & 3);
  588. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  589. }
  590. tmp += dall.x * sum1 - dall.y * sum2;
  591. }
  592. #endif
  593. // sum up partial sums and write back result
  594. #pragma unroll
  595. for (int mask = 16; mask > 0; mask >>= 1) {
  596. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  597. }
  598. if (threadIdx.x == 0) {
  599. dst[row] = tmp;
  600. }
  601. }
  602. static __global__ void dequantize_mul_mat_vec_q3_k(const void * vx, const float * yy, float * dst, const int ncols, int nrows) {
  603. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  604. if (row > nrows) return;
  605. const int num_blocks_per_row = ncols / QK_K;
  606. const int ib0 = row*num_blocks_per_row;
  607. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  608. float tmp = 0; // partial sum for thread in warp
  609. #if QK_K == 256
  610. const uint16_t kmask1 = 0x0303;
  611. const uint16_t kmask2 = 0x0f0f;
  612. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  613. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  614. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  615. const int step = 16/K_QUANTS_PER_ITERATION;
  616. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  617. const int in = tid - step*im; // 0....15 or 0...7
  618. const uint8_t m = 1 << (4*im);
  619. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  620. const int q_offset = 32*im + l0;
  621. const int y_offset = 128*im + l0;
  622. uint16_t utmp[4];
  623. const int8_t * s = (const int8_t *)utmp;
  624. const uint16_t s_shift = 4*im;
  625. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  626. const float * y = yy + i * QK_K + y_offset;
  627. const uint8_t * q = x[i].qs + q_offset;
  628. const uint8_t * h = x[i].hmask + l0;
  629. const uint16_t * a = (const uint16_t *)x[i].scales;
  630. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  631. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  632. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  633. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  634. const float d = x[i].d;
  635. float sum = 0;
  636. for (int l = 0; l < n; ++l) {
  637. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  638. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  639. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  640. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  641. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  642. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  643. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  644. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  645. }
  646. tmp += d * sum;
  647. }
  648. #else
  649. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  650. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  651. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  652. const int in = offset/8; // 0 or 1
  653. const int im = offset%8; // 0...7
  654. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  655. const float * y = yy + i * QK_K + offset;
  656. const uint8_t * q = x[i].qs + offset;
  657. const uint8_t * s = x[i].scales;
  658. const float dall = (float)x[i].d;
  659. float sum = 0;
  660. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  661. const uint8_t hl = x[i].hmask[im+l] >> in;
  662. const uint8_t ql = q[l];
  663. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  664. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  665. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  666. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  667. }
  668. tmp += sum;
  669. }
  670. #endif
  671. // sum up partial sums and write back result
  672. #pragma unroll
  673. for (int mask = 16; mask > 0; mask >>= 1) {
  674. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  675. }
  676. if (threadIdx.x == 0) {
  677. dst[row] = tmp;
  678. }
  679. }
  680. static __global__ void dequantize_mul_mat_vec_q4_k(const void * vx, const float * yy, float * dst, const int ncols, int nrows) {
  681. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  682. if (row > nrows) return;
  683. const int num_blocks_per_row = ncols / QK_K;
  684. const int ib0 = row*num_blocks_per_row;
  685. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  686. #if QK_K == 256
  687. const uint16_t kmask1 = 0x3f3f;
  688. const uint16_t kmask2 = 0x0f0f;
  689. const uint16_t kmask3 = 0xc0c0;
  690. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  691. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  692. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  693. const int il = tid/step; // 0...3
  694. const int ir = tid - step*il; // 0...7 or 0...3
  695. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  696. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  697. const int in = il%2;
  698. const int l0 = n*(2*ir + in);
  699. const int q_offset = 32*im + l0;
  700. const int y_offset = 64*im + l0;
  701. uint16_t aux[4];
  702. const uint8_t * sc = (const uint8_t *)aux;
  703. float tmp = 0; // partial sum for thread in warp
  704. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  705. const uint8_t * q1 = x[i].qs + q_offset;
  706. const uint8_t * q2 = q1 + 64;
  707. const float * y1 = yy + i*QK_K + y_offset;
  708. const float * y2 = y1 + 128;
  709. const float dall = x[i].d;
  710. const float dmin = x[i].dmin;
  711. const uint16_t * a = (const uint16_t *)x[i].scales;
  712. aux[0] = a[im+0] & kmask1;
  713. aux[1] = a[im+2] & kmask1;
  714. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  715. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  716. float4 s = {0.f, 0.f, 0.f, 0.f};
  717. float smin = 0;
  718. for (int l = 0; l < n; ++l) {
  719. s.x += y1[l] * (q1[l] & 0xF); s.y += y1[l+32] * (q1[l] >> 4);
  720. s.z += y2[l] * (q2[l] & 0xF); s.w += y2[l+32] * (q2[l] >> 4);
  721. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  722. }
  723. tmp += dall * (s.x * sc[0] + s.y * sc[1] + s.z * sc[4] + s.w * sc[5]) - dmin * smin;
  724. }
  725. #else
  726. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  727. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  728. const int step = tid * K_QUANTS_PER_ITERATION;
  729. uint16_t aux16[2];
  730. const uint8_t * s = (const uint8_t *)aux16;
  731. float tmp = 0;
  732. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  733. const uint8_t * q = x[i].qs + step;
  734. const float * y = yy + i*QK_K + step;
  735. const uint16_t * a = (const uint16_t *)x[i].scales;
  736. aux16[0] = a[0] & 0x0f0f;
  737. aux16[1] = (a[0] >> 4) & 0x0f0f;
  738. const float d = (float)x[i].d[0];
  739. const float m = (float)x[i].d[1];
  740. float sum = 0.f;
  741. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  742. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  743. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  744. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  745. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  746. }
  747. tmp += sum;
  748. }
  749. #endif
  750. // sum up partial sums and write back result
  751. #pragma unroll
  752. for (int mask = 16; mask > 0; mask >>= 1) {
  753. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  754. }
  755. if (tid == 0) {
  756. dst[row] = tmp;
  757. }
  758. }
  759. static __global__ void dequantize_mul_mat_vec_q5_k(const void * vx, const float * yy, float * dst, const int ncols) {
  760. const int row = blockIdx.x;
  761. const int num_blocks_per_row = ncols / QK_K;
  762. const int ib0 = row*num_blocks_per_row;
  763. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  764. float tmp = 0; // partial sum for thread in warp
  765. #if QK_K == 256
  766. const uint16_t kmask1 = 0x3f3f;
  767. const uint16_t kmask2 = 0x0f0f;
  768. const uint16_t kmask3 = 0xc0c0;
  769. const int tid = threadIdx.x/2; // 0...15
  770. const int ix = threadIdx.x%2;
  771. const int il = tid/4; // 0...3
  772. const int ir = tid - 4*il;// 0...3
  773. const int n = 2;
  774. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  775. const int in = il%2;
  776. const int l0 = n*(2*ir + in);
  777. const int q_offset = 32*im + l0;
  778. const int y_offset = 64*im + l0;
  779. const uint8_t hm1 = 1 << (2*im);
  780. const uint8_t hm2 = hm1 << 4;
  781. uint16_t aux[4];
  782. const uint8_t * sc = (const uint8_t *)aux;
  783. for (int i = ix; i < num_blocks_per_row; i += 2) {
  784. const uint8_t * ql1 = x[i].qs + q_offset;
  785. const uint8_t * ql2 = ql1 + 64;
  786. const uint8_t * qh = x[i].qh + l0;
  787. const float * y1 = yy + i*QK_K + y_offset;
  788. const float * y2 = y1 + 128;
  789. const float dall = x[i].d;
  790. const float dmin = x[i].dmin;
  791. const uint16_t * a = (const uint16_t *)x[i].scales;
  792. aux[0] = a[im+0] & kmask1;
  793. aux[1] = a[im+2] & kmask1;
  794. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  795. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  796. float4 sum = {0.f, 0.f, 0.f, 0.f};
  797. float smin = 0;
  798. for (int l = 0; l < n; ++l) {
  799. sum.x += y1[l+ 0] * ((ql1[l+ 0] & 0xF) + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  800. + y1[l+16] * ((ql1[l+16] & 0xF) + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  801. sum.y += y1[l+32] * ((ql1[l+ 0] >> 4) + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  802. + y1[l+48] * ((ql1[l+16] >> 4) + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  803. sum.z += y2[l+ 0] * ((ql2[l+ 0] & 0xF) + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  804. + y2[l+16] * ((ql2[l+16] & 0xF) + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  805. sum.w += y2[l+32] * ((ql2[l+ 0] >> 4) + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  806. + y2[l+48] * ((ql2[l+16] >> 4) + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  807. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  808. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  809. }
  810. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  811. }
  812. #else
  813. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  814. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  815. const int step = tid * K_QUANTS_PER_ITERATION;
  816. const int im = step/8;
  817. const int in = step%8;
  818. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  819. const uint8_t * q = x[i].qs + step;
  820. const int8_t * s = x[i].scales;
  821. const float * y = yy + i*QK_K + step;
  822. const float d = x[i].d;
  823. float sum = 0.f;
  824. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  825. const uint8_t h = x[i].qh[in+j] >> im;
  826. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  827. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  828. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  829. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  830. }
  831. tmp += sum;
  832. }
  833. #endif
  834. // sum up partial sums and write back result
  835. #pragma unroll
  836. for (int mask = 16; mask > 0; mask >>= 1) {
  837. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  838. }
  839. if (threadIdx.x == 0) {
  840. dst[row] = tmp;
  841. }
  842. }
  843. static __global__ void dequantize_mul_mat_vec_q6_k(const void * vx, const float * yy, float * dst, const int ncols, int nrows) {
  844. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  845. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  846. if (row > nrows) return;
  847. const int num_blocks_per_row = ncols / QK_K;
  848. const int ib0 = row*num_blocks_per_row;
  849. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  850. #if QK_K == 256
  851. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  852. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  853. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  854. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  855. const int in = tid - step*im; // 0...15 or 0...7
  856. #if K_QUANTS_PER_ITERATION == 1
  857. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  858. const int is = 0;
  859. #else
  860. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  861. const int is = in / 4;
  862. #endif
  863. const int ql_offset = 64*im + l0;
  864. const int qh_offset = 32*im + l0;
  865. const int s_offset = 8*im + is;
  866. const int y_offset = 128*im + l0;
  867. float tmp = 0; // partial sum for thread in warp
  868. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  869. const float * y = yy + i * QK_K + y_offset;
  870. const uint8_t * ql = x[i].ql + ql_offset;
  871. const uint8_t * qh = x[i].qh + qh_offset;
  872. const int8_t * s = x[i].scales + s_offset;
  873. const float d = x[i].d;
  874. #if K_QUANTS_PER_ITERATION == 1
  875. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  876. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  877. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  878. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  879. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  880. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  881. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  882. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  883. tmp += sum;
  884. #else
  885. float sum = 0;
  886. for (int l = 0; l < 4; ++l) {
  887. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  888. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  889. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  890. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  891. }
  892. tmp += sum;
  893. #endif
  894. }
  895. #else
  896. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  897. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  898. const int step = tid * K_QUANTS_PER_ITERATION;
  899. float tmp = 0; // partial sum for thread in warp
  900. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  901. const float * y = yy + i * QK_K + step;
  902. const uint8_t * ql = x[i].ql + step;
  903. const uint8_t * qh = x[i].qh + step;
  904. const int8_t * s = x[i].scales;
  905. const float d = x[i+0].d;
  906. float sum = 0;
  907. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  908. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  909. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  910. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  911. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  912. }
  913. tmp += sum;
  914. }
  915. #endif
  916. // sum up partial sums and write back result
  917. #pragma unroll
  918. for (int mask = 16; mask > 0; mask >>= 1) {
  919. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  920. }
  921. if (tid == 0) {
  922. dst[row] = tmp;
  923. }
  924. }
  925. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  926. const half * x = (const half *) vx;
  927. // automatic half -> float type cast if dfloat == float
  928. v.x = x[ib + iqs + 0];
  929. v.y = x[ib + iqs + 1];
  930. }
  931. static __global__ void quantize_q8_1(const float * x, void * vy, const int k) {
  932. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  933. if (i >= k) {
  934. return;
  935. }
  936. block_q8_1 * y = (block_q8_1 *) vy;
  937. const int ib = i / QK8_0; // block index
  938. const int iqs = i % QK8_0; // quant index
  939. const float xi = x[i];
  940. float amax = fabsf(xi);
  941. float sum = xi;
  942. #pragma unroll
  943. for (int mask = 16; mask > 0; mask >>= 1) {
  944. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  945. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  946. }
  947. const float d = amax / 127;
  948. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  949. y[ib].qs[iqs] = q;
  950. if (iqs > 0) {
  951. return;
  952. }
  953. y[ib].d = d;
  954. y[ib].s = sum;
  955. }
  956. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  957. static __global__ void dequantize_block(const void * vx, float * y, const int k) {
  958. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  959. if (i >= k) {
  960. return;
  961. }
  962. const int ib = i/qk; // block index
  963. const int iqs = (i%qk)/qr; // quant index
  964. const int iybs = i - i%qk; // y block start index
  965. const int y_offset = qr == 1 ? 1 : qk/2;
  966. // dequantize
  967. dfloat2 v;
  968. dequantize_kernel(vx, ib, iqs, v);
  969. y[iybs + iqs + 0] = v.x;
  970. y[iybs + iqs + y_offset] = v.y;
  971. }
  972. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(const void * vbq, const block_q8_1 * bq8_1, const int iqs) {
  973. #if __CUDA_ARCH__ >= 600 // lowest compute capability for integer intrinsics
  974. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  975. int vi;
  976. memcpy(&vi, &bq4_0->qs[sizeof(int) * (iqs + 0)], sizeof(int));
  977. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  978. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI4_0)]);
  979. const float d = __half2float(bq4_0->d) * __half2float(bq8_1->d);
  980. // subtract 8 from each quantized value
  981. const int vi0 = __vsub4((vi >> 0) & 0x0F0F0F0F, 0x08080808);
  982. const int vi1 = __vsub4((vi >> 4) & 0x0F0F0F0F, 0x08080808);
  983. // SIMD dot product of quantized values
  984. int sumi = __dp4a(vi0, ui0, 0);
  985. sumi = __dp4a(vi1, ui1, sumi);
  986. return sumi*d;
  987. #else
  988. return 0.0f; // only to satisfy the compiler
  989. #endif // __CUDA_ARCH__ >= 600
  990. }
  991. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(const void * vbq, const block_q8_1 * bq8_1, const int iqs) {
  992. #if __CUDA_ARCH__ >= 600 // lowest compute capability for integer intrinsics
  993. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  994. const int vi = *((int *) &bq4_1->qs[sizeof(int) * (iqs + 0)]);
  995. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  996. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI4_1)]);
  997. const float d = __half2float(bq4_1->d) * __half2float(bq8_1->d);
  998. const float m = bq4_1->m;
  999. const float s = bq8_1->s;
  1000. const int vi0 = (vi >> 0) & 0x0F0F0F0F;
  1001. const int vi1 = (vi >> 4) & 0x0F0F0F0F;
  1002. // SIMD dot product of quantized values
  1003. int sumi = __dp4a(vi0, ui0, 0);
  1004. sumi = __dp4a(vi1, ui1, sumi);
  1005. return sumi*d + m*s / QI4_1; // scale sum by QI4_1 because there are QI4_1 threads working on this block
  1006. #else
  1007. return 0.0f; // only to satisfy the compiler
  1008. #endif // __CUDA_ARCH__ >= 600
  1009. }
  1010. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(const void * vbq, const block_q8_1 * bq8_1, const int iqs) {
  1011. #if __CUDA_ARCH__ >= 600 // lowest compute capability for integer intrinsics
  1012. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  1013. int qs;
  1014. memcpy(&qs, &bq5_0->qs[sizeof(int) * (iqs + 0)], sizeof(int));
  1015. const int qh0 = bq5_0->qh[iqs/2 + 0] >> 4*(iqs%2);
  1016. const int qh1 = bq5_0->qh[iqs/2 + 2] >> 4*(iqs%2);
  1017. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1018. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI5_0)]);
  1019. const float d = __half2float(bq5_0->d) * __half2float(bq8_1->d);
  1020. int vi0 = (qs >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh0 as 5th bits
  1021. vi0 |= (qh0 << 4) & 0x00000010; // 1 -> 5
  1022. vi0 |= (qh0 << 11) & 0x00001000; // 2 -> 13
  1023. vi0 |= (qh0 << 18) & 0x00100000; // 3 -> 21
  1024. vi0 |= (qh0 << 25) & 0x10000000; // 4 -> 29
  1025. vi0 = __vsub4(vi0, 0x10101010); // subtract 16 from quantized values
  1026. int sumi = __dp4a(vi0, ui0, 0); // SIMD dot product of quantized values
  1027. int vi1 = (qs >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh1 as 5th bits
  1028. vi1 |= (qh1 << 4) & 0x00000010; // 1 -> 5
  1029. vi1 |= (qh1 << 11) & 0x00001000; // 2 -> 13
  1030. vi1 |= (qh1 << 18) & 0x00100000; // 3 -> 21
  1031. vi1 |= (qh1 << 25) & 0x10000000; // 4 -> 29
  1032. vi1 = __vsub4(vi1, 0x10101010); // subtract 16 from quantized values
  1033. sumi = __dp4a(vi1, ui1, sumi); // SIMD dot product of quantized values
  1034. return sumi*d;
  1035. #else
  1036. return 0.0f; // only to satisfy the compiler
  1037. #endif // __CUDA_ARCH__ >= 600
  1038. }
  1039. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(const void * vbq, const block_q8_1 * bq8_1, const int iqs) {
  1040. #if __CUDA_ARCH__ >= 600 // lowest compute capability for integer intrinsics
  1041. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  1042. const int qs = *((int *) &bq5_1->qs[sizeof(int) * (iqs + 0)]);
  1043. const int qh0 = bq5_1->qh[iqs/2 + 0] >> 4*(iqs%2);
  1044. const int qh1 = bq5_1->qh[iqs/2 + 2] >> 4*(iqs%2);
  1045. const int ui0 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1046. const int ui1 = *((int *) &bq8_1->qs[sizeof(int) * (iqs + QI5_1)]);
  1047. const float d = __half2float(bq5_1->d) * __half2float(bq8_1->d);
  1048. const float m = bq5_1->m;
  1049. const float s = bq8_1->s;
  1050. int vi0 = (qs >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh0 as 5th bits
  1051. vi0 |= (qh0 << 4) & 0x00000010; // 1 -> 5
  1052. vi0 |= (qh0 << 11) & 0x00001000; // 2 -> 13
  1053. vi0 |= (qh0 << 18) & 0x00100000; // 3 -> 21
  1054. vi0 |= (qh0 << 25) & 0x10000000; // 4 -> 29
  1055. int sumi = __dp4a(vi0, ui0, 0); // SIMD dot product of quantized values
  1056. int vi1 = (qs >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh1 as 5th bits
  1057. vi1 |= (qh1 << 4) & 0x00000010; // 1 -> 5
  1058. vi1 |= (qh1 << 11) & 0x00001000; // 2 -> 13
  1059. vi1 |= (qh1 << 18) & 0x00100000; // 3 -> 21
  1060. vi1 |= (qh1 << 25) & 0x10000000; // 4 -> 29
  1061. sumi = __dp4a(vi1, ui1, sumi); // SIMD dot product of quantized values
  1062. return sumi*d + m*s / QI5_1; // scale sum by QI5_1 because there are QI5_1 threads working on this block
  1063. #else
  1064. return 0.0f; // only to satisfy the compiler
  1065. #endif // __CUDA_ARCH__ >= 600
  1066. }
  1067. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(const void * vbq, const block_q8_1 * bq8_1, const int iqs) {
  1068. #if __CUDA_ARCH__ >= 600 // lowest compute capability for integer intrinsics
  1069. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  1070. int vi;
  1071. memcpy(&vi, &bq8_0->qs[sizeof(int) * (iqs + 0)], sizeof(int));
  1072. const int ui = *((int *) &bq8_1->qs[sizeof(int) * (iqs + 0)]);
  1073. const float d = __half2float(bq8_0->d) * __half2float(bq8_1->d);
  1074. // SIMD dot product of quantized values
  1075. int sumi = __dp4a(vi, ui, 0);
  1076. return sumi*d;
  1077. #else
  1078. return 0.0f; // only to satisfy the compiler
  1079. #endif // __CUDA_ARCH__ >= 600
  1080. }
  1081. template <int qk, int qi, typename block_q_t, vec_dot_q_cuda_t vec_dot_q_cuda>
  1082. static __global__ void mul_mat_vec_q(const void * vx, const void * vy, float * dst, const int ncols, const int nrows) {
  1083. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  1084. if (row >= nrows) {
  1085. return;
  1086. }
  1087. const int blocks_per_row = ncols / qk;
  1088. const int blocks_per_warp = WARP_SIZE / qi;
  1089. // partial sum for each thread
  1090. float tmp = 0.0f;
  1091. const block_q_t * x = (const block_q_t *) vx;
  1092. const block_q8_1 * y = (const block_q8_1 *) vy;
  1093. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  1094. const int ibx = row*blocks_per_row + i + threadIdx.x / qi; // x block index
  1095. const int iby = i + threadIdx.x / qi; // y block index
  1096. const int iqs = threadIdx.x % qi; // x block quant index when casting the quants to int
  1097. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  1098. }
  1099. // sum up partial sums and write back result
  1100. #pragma unroll
  1101. for (int mask = 16; mask > 0; mask >>= 1) {
  1102. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1103. }
  1104. if (threadIdx.x == 0) {
  1105. dst[row] = tmp;
  1106. }
  1107. }
  1108. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  1109. static __global__ void dequantize_mul_mat_vec(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows) {
  1110. // qk = quantized weights per x block
  1111. // qr = number of quantized weights per data value in x block
  1112. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  1113. if (row >= nrows) {
  1114. return;
  1115. }
  1116. const int tid = threadIdx.x;
  1117. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  1118. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  1119. const int y_offset = qr == 1 ? 1 : qk/2;
  1120. // partial sum for each thread
  1121. #ifdef GGML_CUDA_DMMV_F16
  1122. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  1123. #else
  1124. float tmp = 0.0f;
  1125. #endif // GGML_CUDA_DMMV_F16
  1126. for (int i = 0; i < ncols; i += iter_stride) {
  1127. const int col = i + vals_per_iter*tid;
  1128. const int ib = (row*ncols + col)/qk; // x block index
  1129. const int iqs = (col%qk)/qr; // x quant index
  1130. const int iybs = col - col%qk; // y block start index
  1131. // processing >2 values per i iter is faster for fast GPUs
  1132. #pragma unroll
  1133. for (int j = 0; j < vals_per_iter; j += 2) {
  1134. // process 2 vals per j iter
  1135. // dequantize
  1136. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  1137. dfloat2 v;
  1138. dequantize_kernel(vx, ib, iqs + j/qr, v);
  1139. // matrix multiplication
  1140. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  1141. #ifdef GGML_CUDA_DMMV_F16
  1142. tmp += __hmul2(v, {
  1143. y[iybs + iqs + j/qr + 0],
  1144. y[iybs + iqs + j/qr + y_offset]
  1145. });
  1146. #else
  1147. tmp += v.x * y[iybs + iqs + j/qr + 0];
  1148. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  1149. #endif // GGML_CUDA_DMMV_F16
  1150. }
  1151. }
  1152. // sum up partial sums and write back result
  1153. #pragma unroll
  1154. for (int mask = 16; mask > 0; mask >>= 1) {
  1155. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1156. }
  1157. if (tid == 0) {
  1158. #ifdef GGML_CUDA_DMMV_F16
  1159. dst[row] = tmp.x + tmp.y;
  1160. #else
  1161. dst[row] = tmp;
  1162. #endif // GGML_CUDA_DMMV_F16
  1163. }
  1164. }
  1165. static __global__ void mul_mat_p021_f16_f32(const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nchannels_x) {
  1166. const half * x = (const half *) vx;
  1167. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  1168. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  1169. const int nrows_y = ncols_x;
  1170. const int nrows_dst = nrows_x;
  1171. const int row_dst = row_x;
  1172. float tmp = 0.0f;
  1173. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  1174. const int col_x = col_x0 + threadIdx.x;
  1175. if (col_x >= ncols_x) {
  1176. break;
  1177. }
  1178. // x is transposed and permuted
  1179. const int ix = row_x*nchannels_x*ncols_x + channel*ncols_x + col_x;
  1180. const float xi = __half2float(x[ix]);
  1181. const int row_y = col_x;
  1182. // y is not transposed but permuted
  1183. const int iy = channel*nrows_y + row_y;
  1184. tmp += xi * y[iy];
  1185. }
  1186. // dst is not transposed and not permuted
  1187. const int idst = channel*nrows_dst + row_dst;
  1188. // sum up partial sums and write back result
  1189. #pragma unroll
  1190. for (int mask = 16; mask > 0; mask >>= 1) {
  1191. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1192. }
  1193. if (threadIdx.x == 0) {
  1194. dst[idst] = tmp;
  1195. }
  1196. }
  1197. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  1198. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  1199. const int row_stride_x, const int channel_stride_x) {
  1200. const half * x = (const half *) vx;
  1201. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  1202. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  1203. const int nrows_y = ncols_x;
  1204. const int nrows_dst = nrows_x;
  1205. const int row_dst = row_x;
  1206. const int idst = channel*nrows_dst + row_dst;
  1207. float tmp = 0.0f;
  1208. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  1209. const int col_x = col_x0 + threadIdx.x;
  1210. if (col_x >= ncols_x) {
  1211. break;
  1212. }
  1213. const int ix = channel*channel_stride_x + row_x*row_stride_x + col_x;
  1214. const float xi = __half2float(x[ix]);
  1215. const int row_y = col_x;
  1216. const int iy = channel*nrows_y + row_y;
  1217. tmp += xi * y[iy];
  1218. }
  1219. // sum up partial sums and write back result
  1220. #pragma unroll
  1221. for (int mask = 16; mask > 0; mask >>= 1) {
  1222. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1223. }
  1224. if (threadIdx.x == 0) {
  1225. dst[idst] = tmp;
  1226. }
  1227. }
  1228. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  1229. const float * xi = (const float *) cxi;
  1230. float * dsti = (float *) cdsti;
  1231. *dsti = *xi;
  1232. }
  1233. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  1234. const float * xi = (const float *) cxi;
  1235. half * dsti = (half *) cdsti;
  1236. *dsti = __float2half(*xi);
  1237. }
  1238. template <cpy_kernel_t cpy_1>
  1239. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  1240. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  1241. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  1242. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1243. if (i >= ne) {
  1244. return;
  1245. }
  1246. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  1247. // then combine those indices with the corresponding byte offsets to get the total offsets
  1248. const int i02 = i / (ne00*ne01);
  1249. const int i01 = (i - i02*ne01*ne00) / ne00;
  1250. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  1251. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  1252. const int i12 = i / (ne10*ne11);
  1253. const int i11 = (i - i12*ne10*ne11) / ne10;
  1254. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  1255. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  1256. cpy_1(cx + x_offset, cdst + dst_offset);
  1257. }
  1258. // rope == RoPE == rotary positional embedding
  1259. static __global__ void rope_f32(const float * x, float * dst, const int ncols, const float p, const float theta_scale) {
  1260. const int col = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  1261. if (col >= ncols) {
  1262. return;
  1263. }
  1264. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1265. const int i = row*ncols + col;
  1266. const float theta = p*powf(theta_scale, col/2);
  1267. const float sin_theta = sinf(theta);
  1268. const float cos_theta = cosf(theta);
  1269. const float x0 = x[i + 0];
  1270. const float x1 = x[i + 1];
  1271. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  1272. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  1273. }
  1274. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  1275. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  1276. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1277. if (col >= ncols) {
  1278. return;
  1279. }
  1280. const int i = row*ncols + col;
  1281. // dst[i] = col > n_past + row ? -INFINITY : x[i];
  1282. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  1283. }
  1284. // the CUDA soft max implementation differs from the CPU implementation
  1285. // instead of doubles floats are used
  1286. // values are also not normalized to the maximum value by subtracting it in the exponential function
  1287. // theoretically these changes could cause problems with rounding error and arithmetic overflow but for LLaMa it seems to be fine
  1288. static __global__ void soft_max_f32(const float * x, float * dst, const int ncols) {
  1289. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1290. const int block_size = blockDim.x;
  1291. const int tid = threadIdx.x;
  1292. float tmp = 0.0;
  1293. for (int block_start = 0; block_start < ncols; block_start += block_size) {
  1294. const int col = block_start + tid;
  1295. if (col >= ncols) {
  1296. break;
  1297. }
  1298. const int i = row*ncols + col;
  1299. const float val = expf(x[i]);
  1300. tmp += val;
  1301. dst[i] = val;
  1302. }
  1303. // sum up partial sums
  1304. #pragma unroll
  1305. for (int mask = 16; mask > 0; mask >>= 1) {
  1306. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1307. }
  1308. for (int block_start = 0; block_start < ncols; block_start += block_size) {
  1309. const int col = block_start + tid;
  1310. if (col >= ncols) {
  1311. break;
  1312. }
  1313. const int i = row*ncols + col;
  1314. dst[i] /= tmp;
  1315. }
  1316. }
  1317. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  1318. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1319. if (i >= k) {
  1320. return;
  1321. }
  1322. dst[i] = scale * x[i];
  1323. }
  1324. static void add_f32_cuda(const float * x, const float * y, float * dst, const int k, cudaStream_t stream) {
  1325. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  1326. add_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  1327. }
  1328. static void add_f16_f32_f16_cuda(const half * x, const float * y, half * dst, const int k, cudaStream_t stream) {
  1329. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  1330. add_f16_f32_f16<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  1331. }
  1332. static void mul_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  1333. const int num_blocks = (kx + CUDA_MUL_BLOCK_SIZE - 1) / CUDA_MUL_BLOCK_SIZE;
  1334. mul_f32<<<num_blocks, CUDA_MUL_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  1335. }
  1336. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  1337. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  1338. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  1339. }
  1340. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  1341. GGML_ASSERT(ncols % WARP_SIZE == 0);
  1342. const dim3 block_dims(WARP_SIZE, 1, 1);
  1343. rms_norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  1344. }
  1345. static void quantize_row_q8_1_cuda(const float * x, void * vy, const int k, cudaStream_t stream) {
  1346. const int num_blocks = (k + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  1347. quantize_q8_1<<<num_blocks, CUDA_QUANTIZE_BLOCK_SIZE, 0, stream>>>(x, vy, k);
  1348. }
  1349. static void dequantize_row_q4_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1350. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1351. dequantize_block<QK4_0, QR4_0, dequantize_q4_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1352. }
  1353. static void dequantize_row_q4_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1354. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1355. dequantize_block<QK4_1, QR4_1, dequantize_q4_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1356. }
  1357. static void dequantize_row_q5_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1358. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1359. dequantize_block<QK5_0, QR5_0, dequantize_q5_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1360. }
  1361. static void dequantize_row_q5_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1362. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1363. dequantize_block<QK5_1, QR5_1, dequantize_q5_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1364. }
  1365. static void dequantize_row_q8_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1366. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1367. dequantize_block<QK8_0, QR8_0, dequantize_q8_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1368. }
  1369. static void dequantize_row_q2_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1370. const int nb = k / QK_K;
  1371. #if QK_K == 256
  1372. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  1373. #else
  1374. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  1375. #endif
  1376. }
  1377. static void dequantize_row_q3_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1378. const int nb = k / QK_K;
  1379. #if QK_K == 256
  1380. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  1381. #else
  1382. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  1383. #endif
  1384. }
  1385. static void dequantize_row_q4_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1386. const int nb = k / QK_K;
  1387. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  1388. }
  1389. static void dequantize_row_q5_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1390. const int nb = k / QK_K;
  1391. #if QK_K == 256
  1392. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  1393. #else
  1394. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  1395. #endif
  1396. }
  1397. static void dequantize_row_q6_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1398. const int nb = k / QK_K;
  1399. #if QK_K == 256
  1400. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  1401. #else
  1402. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  1403. #endif
  1404. }
  1405. 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) {
  1406. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1407. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1408. const dim3 block_nums(1, block_num_y, 1);
  1409. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1410. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  1411. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1412. }
  1413. 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) {
  1414. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1415. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1416. const dim3 block_nums(1, block_num_y, 1);
  1417. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1418. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  1419. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1420. }
  1421. 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) {
  1422. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1423. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1424. const dim3 block_nums(1, block_num_y, 1);
  1425. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1426. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  1427. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1428. }
  1429. 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) {
  1430. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1431. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1432. const dim3 block_nums(1, block_num_y, 1);
  1433. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1434. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  1435. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1436. }
  1437. 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) {
  1438. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1439. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1440. const dim3 block_nums(1, block_num_y, 1);
  1441. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1442. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  1443. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1444. }
  1445. 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) {
  1446. GGML_ASSERT(ncols % QK_K == 0);
  1447. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  1448. const int block_num_y = (nrows + ny - 1) / ny;
  1449. const dim3 block_nums(1, block_num_y, 1);
  1450. const dim3 block_dims(32, ny, 1);
  1451. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1452. }
  1453. 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) {
  1454. GGML_ASSERT(ncols % QK_K == 0);
  1455. const int ny = 2 / K_QUANTS_PER_ITERATION;
  1456. const int block_num_y = (nrows + ny - 1) / ny;
  1457. const dim3 block_nums(1, block_num_y, 1);
  1458. const dim3 block_dims(32, ny, 1);
  1459. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1460. }
  1461. 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) {
  1462. GGML_ASSERT(ncols % QK_K == 0);
  1463. const int ny = 2 / K_QUANTS_PER_ITERATION;
  1464. const int block_num_y = (nrows + ny - 1) / ny;
  1465. const dim3 block_nums(1, block_num_y, 1);
  1466. const dim3 block_dims(32, ny, 1);
  1467. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1468. }
  1469. 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) {
  1470. GGML_ASSERT(ncols % QK_K == 0);
  1471. const dim3 block_dims(32, 1, 1);
  1472. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  1473. }
  1474. 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) {
  1475. GGML_ASSERT(ncols % QK_K == 0);
  1476. const int ny = 2 / K_QUANTS_PER_ITERATION;
  1477. const int block_num_y = (nrows + ny - 1) / ny;
  1478. const dim3 block_nums(1, block_num_y, 1);
  1479. const dim3 block_dims(32, ny, 1);
  1480. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1481. }
  1482. 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) {
  1483. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1484. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1485. const dim3 block_nums(1, block_num_y, 1);
  1486. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1487. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, vec_dot_q4_0_q8_1>
  1488. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1489. }
  1490. 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) {
  1491. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1492. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1493. const dim3 block_nums(1, block_num_y, 1);
  1494. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1495. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, vec_dot_q4_1_q8_1>
  1496. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1497. }
  1498. 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) {
  1499. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1500. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1501. const dim3 block_nums(1, block_num_y, 1);
  1502. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1503. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, vec_dot_q5_0_q8_1>
  1504. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1505. }
  1506. 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) {
  1507. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1508. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1509. const dim3 block_nums(1, block_num_y, 1);
  1510. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1511. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, vec_dot_q5_1_q8_1>
  1512. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1513. }
  1514. 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) {
  1515. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1516. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1517. const dim3 block_nums(1, block_num_y, 1);
  1518. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1519. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, vec_dot_q8_0_q8_1>
  1520. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  1521. }
  1522. static void convert_fp16_to_fp32_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  1523. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  1524. dequantize_block<1, 1, convert_f16><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  1525. }
  1526. 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) {
  1527. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  1528. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  1529. const dim3 block_nums(1, block_num_y, 1);
  1530. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  1531. dequantize_mul_mat_vec<1, 1, convert_f16>
  1532. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  1533. }
  1534. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  1535. switch (type) {
  1536. case GGML_TYPE_Q4_0:
  1537. return dequantize_row_q4_0_cuda;
  1538. case GGML_TYPE_Q4_1:
  1539. return dequantize_row_q4_1_cuda;
  1540. case GGML_TYPE_Q5_0:
  1541. return dequantize_row_q5_0_cuda;
  1542. case GGML_TYPE_Q5_1:
  1543. return dequantize_row_q5_1_cuda;
  1544. case GGML_TYPE_Q8_0:
  1545. return dequantize_row_q8_0_cuda;
  1546. case GGML_TYPE_Q2_K:
  1547. return dequantize_row_q2_K_cuda;
  1548. case GGML_TYPE_Q3_K:
  1549. return dequantize_row_q3_K_cuda;
  1550. case GGML_TYPE_Q4_K:
  1551. return dequantize_row_q4_K_cuda;
  1552. case GGML_TYPE_Q5_K:
  1553. return dequantize_row_q5_K_cuda;
  1554. case GGML_TYPE_Q6_K:
  1555. return dequantize_row_q6_K_cuda;
  1556. case GGML_TYPE_F16:
  1557. return convert_fp16_to_fp32_cuda;
  1558. default:
  1559. return nullptr;
  1560. }
  1561. }
  1562. 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) {
  1563. const dim3 block_nums(1, nrows_x, nchannels_x);
  1564. const dim3 block_dims(WARP_SIZE, 1, 1);
  1565. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x);
  1566. }
  1567. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  1568. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  1569. const int nchannels_x, const int channel_stride_x, cudaStream_t stream) {
  1570. const dim3 block_nums(1, nrows_x, nchannels_x);
  1571. const dim3 block_dims(WARP_SIZE, 1, 1);
  1572. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  1573. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x);
  1574. }
  1575. static void ggml_cpy_f32_f32_cuda(
  1576. const char * cx, char * cdst, const int ne,
  1577. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  1578. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  1579. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  1580. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  1581. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  1582. }
  1583. static void ggml_cpy_f32_f16_cuda(
  1584. const char * cx, char * cdst, const int ne,
  1585. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  1586. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  1587. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  1588. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  1589. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  1590. }
  1591. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  1592. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  1593. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  1594. }
  1595. 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) {
  1596. GGML_ASSERT(nrows % 2 == 0);
  1597. const dim3 block_dims(2*CUDA_ROPE_BLOCK_SIZE, 1, 1);
  1598. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  1599. const dim3 block_nums(num_blocks_x, nrows, 1);
  1600. rope_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p, theta_scale);
  1601. }
  1602. 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) {
  1603. const dim3 block_dims(CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1, 1);
  1604. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  1605. const dim3 block_nums(block_num_x, nrows_x, 1);
  1606. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  1607. }
  1608. static void soft_max_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, cudaStream_t stream) {
  1609. const dim3 block_dims(WARP_SIZE, 1, 1);
  1610. const dim3 block_nums(1, nrows_x, 1);
  1611. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x);
  1612. }
  1613. // buffer pool for cuda
  1614. #define MAX_CUDA_BUFFERS 256
  1615. struct scoped_spin_lock {
  1616. std::atomic_flag& lock;
  1617. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  1618. while (lock.test_and_set(std::memory_order_acquire)) {
  1619. ; // spin
  1620. }
  1621. }
  1622. ~scoped_spin_lock() {
  1623. lock.clear(std::memory_order_release);
  1624. }
  1625. scoped_spin_lock(const scoped_spin_lock&) = delete;
  1626. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  1627. };
  1628. struct cuda_buffer {
  1629. void * ptr = nullptr;
  1630. size_t size = 0;
  1631. };
  1632. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  1633. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  1634. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  1635. scoped_spin_lock lock(g_cuda_pool_lock);
  1636. int id;
  1637. CUDA_CHECK(cudaGetDevice(&id));
  1638. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  1639. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  1640. if (b.size >= size && b.ptr != nullptr) {
  1641. void * ptr = b.ptr;
  1642. *actual_size = b.size;
  1643. b.ptr = nullptr;
  1644. b.size = 0;
  1645. return ptr;
  1646. }
  1647. }
  1648. void * ptr;
  1649. CUDA_CHECK(cudaMalloc((void **) &ptr, size));
  1650. *actual_size = size;
  1651. return ptr;
  1652. }
  1653. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  1654. scoped_spin_lock lock(g_cuda_pool_lock);
  1655. int id;
  1656. CUDA_CHECK(cudaGetDevice(&id));
  1657. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  1658. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  1659. if (b.ptr == nullptr) {
  1660. b.ptr = ptr;
  1661. b.size = size;
  1662. return;
  1663. }
  1664. }
  1665. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  1666. CUDA_CHECK(cudaFree(ptr));
  1667. }
  1668. static void * g_scratch_buffer = nullptr;
  1669. static size_t g_scratch_size = 1024*1024*1024; // 1 GB by default
  1670. static size_t g_scratch_offset = 0;
  1671. static int g_device_count = -1;
  1672. static int g_main_device = 0;
  1673. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  1674. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  1675. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  1676. static cudaStream_t g_cudaStreams_main[GGML_CUDA_MAX_DEVICES] = { nullptr };
  1677. void ggml_init_cublas() {
  1678. static bool initialized = false;
  1679. if (!initialized) {
  1680. CUDA_CHECK(cudaGetDeviceCount(&g_device_count));
  1681. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  1682. int64_t total_vram = 0;
  1683. fprintf(stderr, "%s: found %d CUDA devices:\n", __func__, g_device_count);
  1684. for (int id = 0; id < g_device_count; ++id) {
  1685. cudaDeviceProp prop;
  1686. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  1687. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  1688. g_tensor_split[id] = total_vram;
  1689. total_vram += prop.totalGlobalMem;
  1690. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  1691. }
  1692. for (int id = 0; id < g_device_count; ++id) {
  1693. g_tensor_split[id] /= total_vram;
  1694. }
  1695. for (int id = 0; id < g_device_count; ++id) {
  1696. CUDA_CHECK(cudaSetDevice(id));
  1697. // create main stream
  1698. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams_main[id], cudaStreamNonBlocking));
  1699. // create cublas handle
  1700. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  1701. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  1702. }
  1703. // configure logging to stdout
  1704. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  1705. initialized = true;
  1706. }
  1707. }
  1708. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  1709. bool all_zero = true;
  1710. for (int i = 0; i < g_device_count; ++i) {
  1711. if (tensor_split[i] != 0.0f) {
  1712. all_zero = false;
  1713. break;
  1714. }
  1715. }
  1716. if (all_zero) {
  1717. return;
  1718. }
  1719. float split_sum = 0.0f;
  1720. for (int i = 0; i < g_device_count; ++i) {
  1721. g_tensor_split[i] = split_sum;
  1722. split_sum += tensor_split[i];
  1723. }
  1724. for (int i = 0; i < g_device_count; ++i) {
  1725. g_tensor_split[i] /= split_sum;
  1726. }
  1727. }
  1728. void * ggml_cuda_host_malloc(size_t size) {
  1729. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  1730. return nullptr;
  1731. }
  1732. void * ptr = nullptr;
  1733. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  1734. if (err != cudaSuccess) {
  1735. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  1736. // This can fixed the OOM error in WSL.
  1737. cudaGetLastError();
  1738. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  1739. size/1024.0/1024.0, cudaGetErrorString(err));
  1740. return nullptr;
  1741. }
  1742. return ptr;
  1743. }
  1744. void ggml_cuda_host_free(void * ptr) {
  1745. CUDA_CHECK(cudaFreeHost(ptr));
  1746. }
  1747. static cudaError_t ggml_cuda_cpy_tensor_2d(
  1748. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  1749. cudaMemcpyKind kind;
  1750. char * src_ptr;
  1751. if (src->backend == GGML_BACKEND_CPU) {
  1752. kind = cudaMemcpyHostToDevice;
  1753. src_ptr = (char *) src->data;
  1754. } else if (src->backend == GGML_BACKEND_GPU) {
  1755. kind = cudaMemcpyDeviceToDevice;
  1756. struct ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  1757. int id;
  1758. CUDA_CHECK(cudaGetDevice(&id));
  1759. src_ptr = (char *) extra->data_device[id];
  1760. } else {
  1761. GGML_ASSERT(false);
  1762. }
  1763. char * dst_ptr = (char *) dst;
  1764. const int64_t ne0 = src->ne[0];
  1765. const int64_t nb0 = src->nb[0];
  1766. const int64_t nb1 = src->nb[1];
  1767. const int64_t nb2 = src->nb[2];
  1768. const int64_t nb3 = src->nb[3];
  1769. const enum ggml_type type = src->type;
  1770. const int64_t ts = ggml_type_size(type);
  1771. const int64_t bs = ggml_blck_size(type);
  1772. int64_t i1_diff = i1_high - i1_low;
  1773. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  1774. if (nb0 == ts && nb1 == ts*ne0/bs) {
  1775. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  1776. } else if (nb0 == ts) {
  1777. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  1778. } else {
  1779. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  1780. const void * rx = (const void *) ((const char *) x + i1*nb1);
  1781. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  1782. // pretend the row is a matrix with cols=1
  1783. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  1784. if (r != cudaSuccess) return r;
  1785. }
  1786. return cudaSuccess;
  1787. }
  1788. }
  1789. inline void ggml_cuda_op_add(
  1790. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  1791. 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,
  1792. cudaStream_t & cudaStream_main){
  1793. GGML_ASSERT(src0_ddq_i != nullptr || src0_ddf_i != nullptr);
  1794. GGML_ASSERT(src1_ddf_i != nullptr);
  1795. GGML_ASSERT(dst_ddf_i != nullptr);
  1796. const int64_t ne0 = src0->ne[0];
  1797. const int64_t i01_diff = i01_high - i01_low;
  1798. // compute
  1799. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  1800. add_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne0*i01_diff, cudaStream_main);
  1801. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  1802. add_f16_f32_f16_cuda((half *) src0_ddq_i, src1_ddf_i, (half *) dst_ddf_i, ne0*i01_diff, cudaStream_main);
  1803. } else {
  1804. GGML_ASSERT(false);
  1805. }
  1806. (void) src1;
  1807. (void) dst;
  1808. (void) src0_ddq_i;
  1809. (void) i02;
  1810. (void) i1;
  1811. }
  1812. inline void ggml_cuda_op_mul(
  1813. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  1814. 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,
  1815. cudaStream_t & cudaStream_main){
  1816. GGML_ASSERT(src0_ddf_i != nullptr);
  1817. GGML_ASSERT(src1_ddf_i != nullptr);
  1818. GGML_ASSERT(dst_ddf_i != nullptr);
  1819. const int64_t ne00 = src0->ne[0];
  1820. const int64_t ne10 = src1->ne[0];
  1821. const int64_t ne11 = src1->ne[1];
  1822. for (int64_t i01 = i01_low; i01 < i01_high; i01++) {
  1823. const int64_t i11 = i1*ne11 + i01%ne11; // broadcast src1 across src0
  1824. float * src0_ddf_i01 = src0_ddf_i + i01*ne00;
  1825. float * src1_ddf_i01 = src1_ddf_i + i11*ne10;
  1826. float * dst_ddf_i01 = dst_ddf_i + i01*ne00;
  1827. // compute
  1828. mul_f32_cuda(src0_ddf_i01, src1_ddf_i01, dst_ddf_i01, ne00, ne10, cudaStream_main);
  1829. }
  1830. (void) dst;
  1831. (void) src0_ddq_i;
  1832. (void) i02;
  1833. }
  1834. inline void ggml_cuda_op_silu(
  1835. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  1836. 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,
  1837. cudaStream_t & cudaStream_main){
  1838. GGML_ASSERT(src0_ddf_i != nullptr);
  1839. GGML_ASSERT(dst_ddf_i != nullptr);
  1840. const int64_t ne00 = src0->ne[0];
  1841. const int64_t i01_diff = i01_high - i01_low;
  1842. // compute
  1843. silu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  1844. (void) src1;
  1845. (void) dst;
  1846. (void) src0_ddq_i;
  1847. (void) src1_ddf_i;
  1848. (void) i02;
  1849. (void) i1;
  1850. }
  1851. inline void ggml_cuda_op_rms_norm(
  1852. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  1853. 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,
  1854. cudaStream_t & cudaStream_main){
  1855. GGML_ASSERT(src0_ddf_i != nullptr);
  1856. GGML_ASSERT(dst_ddf_i != nullptr);
  1857. const int64_t ne00 = src0->ne[0];
  1858. const int64_t i01_diff = i01_high - i01_low;
  1859. // compute
  1860. rms_norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  1861. (void) src1;
  1862. (void) dst;
  1863. (void) src0_ddq_i;
  1864. (void) src1_ddf_i;
  1865. (void) i02;
  1866. (void) i1;
  1867. }
  1868. inline void ggml_cuda_op_mul_mat_vec(
  1869. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  1870. 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,
  1871. cudaStream_t & cudaStream_main){
  1872. GGML_ASSERT(src0_ddq_i != nullptr);
  1873. GGML_ASSERT(src1_ddf_i != nullptr);
  1874. GGML_ASSERT(dst_ddf_i != nullptr);
  1875. const int64_t ne00 = src0->ne[0];
  1876. const int64_t nrows = i01_high - i01_low;
  1877. #ifdef GGML_CUDA_FORCE_DMMV
  1878. const bool use_mul_mat_vec_q = false;
  1879. #else
  1880. int id;
  1881. CUDA_CHECK(cudaGetDevice(&id));
  1882. const bool mul_mat_vec_q_implemented = src0->type == GGML_TYPE_Q4_0 ||
  1883. src0->type == GGML_TYPE_Q4_1 ||
  1884. src0->type == GGML_TYPE_Q5_0 ||
  1885. src0->type == GGML_TYPE_Q5_1 ||
  1886. src0->type == GGML_TYPE_Q8_0;
  1887. // The integer intrinsics used in mul_mat_vec_q are available with compute capability 6.
  1888. // However, they have bad performance with Pascal cards.
  1889. // Therefore, in a multi GPU setting decide at runtime which GPUs should use mul_mat_vec_q.
  1890. const bool use_mul_mat_vec_q = g_compute_capabilities[id] >= 700 && mul_mat_vec_q_implemented;
  1891. #endif
  1892. if (use_mul_mat_vec_q) {
  1893. size_t as;
  1894. void * src1_q8_1 = ggml_cuda_pool_malloc(ne00*sizeof(block_q8_1)/QK8_1, &as);
  1895. quantize_row_q8_1_cuda(src1_ddf_i, src1_q8_1, ne00, 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 size_t nb1 = tensor->nb[1];
  2510. ggml_backend backend = tensor->backend;
  2511. struct ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  2512. memset(extra, 0, sizeof(*extra));
  2513. for (int id = 0; id < g_device_count; ++id) {
  2514. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  2515. continue;
  2516. }
  2517. cudaSetDevice(id);
  2518. int row_low, row_high;
  2519. if (backend == GGML_BACKEND_GPU) {
  2520. row_low = 0;
  2521. row_high = nrows;
  2522. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  2523. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  2524. row_high = id == g_device_count - 1 ? nrows : nrows*g_tensor_split[id + 1];
  2525. } else {
  2526. GGML_ASSERT(false);
  2527. }
  2528. if (row_low == row_high) {
  2529. continue;
  2530. }
  2531. int64_t nrows_split = row_high - row_low;
  2532. const size_t offset_split = row_low*nb1;
  2533. const size_t size = ggml_nbytes_split(tensor, nrows_split);
  2534. void * buf;
  2535. CUDA_CHECK(cudaMalloc(&buf, size));
  2536. void * buf_host = (char*)data + offset_split;
  2537. cudaMemcpy(buf, buf_host, size, cudaMemcpyHostToDevice);
  2538. extra->data_device[id] = buf;
  2539. if (backend == GGML_BACKEND_GPU_SPLIT) {
  2540. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id], cudaEventDisableTiming));
  2541. }
  2542. }
  2543. tensor->extra = extra;
  2544. }
  2545. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  2546. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  2547. return;
  2548. }
  2549. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  2550. for (int id = 0; id < g_device_count; ++id) {
  2551. if (extra->data_device[id] != nullptr) {
  2552. CUDA_CHECK(cudaSetDevice(id));
  2553. CUDA_CHECK(cudaFree(extra->data_device[id]));
  2554. }
  2555. if (extra->events[id] != nullptr) {
  2556. CUDA_CHECK(cudaSetDevice(id));
  2557. CUDA_CHECK(cudaEventDestroy(extra->events[id]));
  2558. }
  2559. }
  2560. delete extra;
  2561. }
  2562. void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace) {
  2563. if (scratch && g_scratch_size == 0) {
  2564. return;
  2565. }
  2566. // recursively assign CUDA buffers until a compute tensor is found
  2567. if (tensor->src0 != nullptr && tensor->src0->backend == GGML_BACKEND_CPU) {
  2568. const ggml_op src0_op = tensor->src0->op;
  2569. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW) {
  2570. ggml_cuda_assign_buffers_impl(tensor->src0, scratch, force_inplace);
  2571. }
  2572. }
  2573. if (tensor->op == GGML_OP_CPY && tensor->src1->backend == GGML_BACKEND_CPU) {
  2574. ggml_cuda_assign_buffers_impl(tensor->src1, scratch, force_inplace);
  2575. }
  2576. tensor->backend = GGML_BACKEND_GPU;
  2577. struct ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu;
  2578. memset(extra, 0, sizeof(*extra));
  2579. const bool inplace = (tensor->src0 != nullptr && tensor->src0->data == tensor->data) ||
  2580. tensor->op == GGML_OP_VIEW ||
  2581. force_inplace;
  2582. const size_t size = ggml_nbytes(tensor);
  2583. CUDA_CHECK(cudaSetDevice(g_main_device));
  2584. if (inplace && (tensor->src0->backend == GGML_BACKEND_GPU || tensor->src0->backend == GGML_BACKEND_GPU_SPLIT)) {
  2585. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src0->extra;
  2586. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  2587. size_t offset = 0;
  2588. if (tensor->op == GGML_OP_VIEW) {
  2589. memcpy(&offset, tensor->opt[0]->data, sizeof(size_t));
  2590. }
  2591. extra->data_device[g_main_device] = src0_ddc + offset;
  2592. } else if (tensor->op == GGML_OP_CPY) {
  2593. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src1->extra;
  2594. void * src1_ddv = src1_extra->data_device[g_main_device];
  2595. extra->data_device[g_main_device] = src1_ddv;
  2596. } else if (scratch) {
  2597. GGML_ASSERT(size <= g_scratch_size);
  2598. if (g_scratch_offset + size > g_scratch_size) {
  2599. g_scratch_offset = 0;
  2600. }
  2601. char * data = (char *) g_scratch_buffer;
  2602. if (data == nullptr) {
  2603. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  2604. g_scratch_buffer = data;
  2605. }
  2606. extra->data_device[g_main_device] = data + g_scratch_offset;
  2607. g_scratch_offset += size;
  2608. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  2609. } else { // allocate new buffers outside of scratch
  2610. void * data;
  2611. CUDA_CHECK(cudaMalloc(&data, size));
  2612. CUDA_CHECK(cudaMemset(data, 0, size));
  2613. extra->data_device[g_main_device] = data;
  2614. }
  2615. tensor->extra = extra;
  2616. }
  2617. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  2618. ggml_cuda_assign_buffers_impl(tensor, true, false);
  2619. }
  2620. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  2621. ggml_cuda_assign_buffers_impl(tensor, false, false);
  2622. }
  2623. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  2624. ggml_cuda_assign_buffers_impl(tensor, false, true);
  2625. }
  2626. void ggml_cuda_set_main_device(int main_device) {
  2627. if (main_device >= g_device_count) {
  2628. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  2629. main_device, g_device_count, g_main_device);
  2630. return;
  2631. }
  2632. g_main_device = main_device;
  2633. if (g_device_count > 1) {
  2634. cudaDeviceProp prop;
  2635. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  2636. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  2637. }
  2638. }
  2639. void ggml_cuda_set_scratch_size(size_t scratch_size) {
  2640. g_scratch_size = scratch_size;
  2641. }
  2642. void ggml_cuda_free_scratch() {
  2643. if (g_scratch_buffer == nullptr) {
  2644. return;
  2645. }
  2646. CUDA_CHECK(cudaFree(g_scratch_buffer));
  2647. g_scratch_buffer = nullptr;
  2648. }
  2649. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor){
  2650. ggml_cuda_func_t func;
  2651. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  2652. || (tensor->src0 != nullptr && (tensor->src0->backend == GGML_BACKEND_GPU || tensor->src0->backend == GGML_BACKEND_GPU_SPLIT))
  2653. || (tensor->src1 != nullptr && tensor->src1->backend == GGML_BACKEND_GPU);
  2654. switch (tensor->op) {
  2655. case GGML_OP_ADD:
  2656. if (!any_on_device) {
  2657. return false;
  2658. }
  2659. func = ggml_cuda_add;
  2660. break;
  2661. case GGML_OP_MUL:
  2662. if (!any_on_device) {
  2663. return false;
  2664. }
  2665. func = ggml_cuda_mul;
  2666. break;
  2667. case GGML_OP_SILU:
  2668. if (!any_on_device) {
  2669. return false;
  2670. }
  2671. func = ggml_cuda_silu;
  2672. break;
  2673. case GGML_OP_RMS_NORM:
  2674. if (!any_on_device) {
  2675. return false;
  2676. }
  2677. func = ggml_cuda_rms_norm;
  2678. break;
  2679. case GGML_OP_MUL_MAT:
  2680. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src0, tensor->src1, tensor)) {
  2681. return false;
  2682. }
  2683. func = ggml_cuda_mul_mat;
  2684. break;
  2685. case GGML_OP_SCALE:
  2686. if (!any_on_device) {
  2687. return false;
  2688. }
  2689. func = ggml_cuda_scale;
  2690. break;
  2691. case GGML_OP_CPY:
  2692. if (!any_on_device) {
  2693. return false;
  2694. }
  2695. func = ggml_cuda_cpy;
  2696. break;
  2697. case GGML_OP_RESHAPE:
  2698. case GGML_OP_VIEW:
  2699. case GGML_OP_PERMUTE:
  2700. case GGML_OP_TRANSPOSE:
  2701. if (!any_on_device) {
  2702. return false;
  2703. }
  2704. func = ggml_cuda_nop;
  2705. break;
  2706. case GGML_OP_DIAG_MASK_INF:
  2707. if (!any_on_device) {
  2708. return false;
  2709. }
  2710. func = ggml_cuda_diag_mask_inf;
  2711. break;
  2712. case GGML_OP_SOFT_MAX:
  2713. if (!any_on_device) {
  2714. return false;
  2715. }
  2716. func = ggml_cuda_soft_max;
  2717. break;
  2718. case GGML_OP_ROPE:
  2719. if (!any_on_device) {
  2720. return false;
  2721. }
  2722. func = ggml_cuda_rope;
  2723. break;
  2724. default:
  2725. return false;
  2726. }
  2727. if (params->ith != 0) {
  2728. return true;
  2729. }
  2730. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  2731. return true;
  2732. }
  2733. func(tensor->src0, tensor->src1, tensor);
  2734. return true;
  2735. }