ggml-cuda.cu 131 KB

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