ggml-cuda.cu 129 KB

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