1
0

q_gemm.cu 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. /*
  2. Adapted from https://github.com/turboderp/exllamav2 and
  3. https://github.com/qwopqwop200/GPTQ-for-LLaMa
  4. */
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <torch/all.h>
  8. #include <c10/cuda/CUDAGuard.h>
  9. #include <ATen/cuda/CUDAContext.h>
  10. #include <cuda_runtime.h>
  11. #include <cuda_fp16.h>
  12. #include "compat.cuh"
  13. #include "matrix_view.cuh"
  14. #include "qdq_2.cuh"
  15. #include "qdq_3.cuh"
  16. #include "qdq_4.cuh"
  17. #include "qdq_8.cuh"
  18. namespace aphrodite {
  19. namespace gptq {
  20. #define BLOCK_KN_SIZE 128
  21. #define BLOCK_M_SIZE_MAX 8
  22. #define MAX_GROUPS_IN_BLOCK (BLOCK_KN_SIZE / 32)
  23. #define MAX_Q_GEMM_ROWS 50
  24. #define MAX_Q_GEMM_ROWS_8BIT 24
  25. #define MAX_ALT_GEMM_ROWS 8
  26. #define THREADS_X 32
  27. #define THREADS_Y 32
  28. #define DIVIDE(x, size) (((x) + (size) - 1) / (size))
  29. #if defined(USE_ROCM)
  30. #include <hipblas/hipblas.h>
  31. __host__ __forceinline__ hipblasStatus_t __compat_hipblasHgemm(
  32. hipblasHandle_t handle, hipblasOperation_t transA,
  33. hipblasOperation_t transB, int m, int n, int k, const half* alpha,
  34. const half* AP, int lda, const half* BP, int ldb, const half* beta,
  35. half* CP, int ldc) {
  36. return hipblasHgemm(handle, transA, transB, m, n, k,
  37. reinterpret_cast<const hipblasHalf*>(alpha),
  38. reinterpret_cast<const hipblasHalf*>(AP), lda,
  39. reinterpret_cast<const hipblasHalf*>(BP), ldb,
  40. reinterpret_cast<const hipblasHalf*>(beta),
  41. reinterpret_cast<hipblasHalf*>(CP), ldc);
  42. }
  43. #define hipblasHgemm __compat_hipblasHgemm
  44. // Previous version of PyTorch were converting to rocBLAS instead of hipBLAS.
  45. #define rocblas_operation_none HIPBLAS_OP_N
  46. #define rocblas_hgemm __compat_hipblasHgemm
  47. #endif
  48. __forceinline__ __device__ half2 dot22_8(half2 (&dq)[4], const half* a_ptr,
  49. const half2 g_result) {
  50. half2 result = {};
  51. const half2* a2_ptr = (const half2*)a_ptr;
  52. #pragma unroll
  53. for (int i = 0; i < 4; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  54. return __hadd2(result, g_result);
  55. }
  56. __forceinline__ __device__ float dot22_8_f(half2 (&dq)[4], const half* a_ptr) {
  57. half2 result = {};
  58. const half2* a2_ptr = (const half2*)a_ptr;
  59. #pragma unroll
  60. for (int i = 0; i < 4; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  61. return __half2float(__low2half(result)) + __half2float(__high2half(result));
  62. }
  63. __forceinline__ __device__ half2 dot22_8(half2 (&dq)[4], const half* a_ptr,
  64. const half2 g_result,
  65. const half qs_h) {
  66. half2 result = {};
  67. const half2* a2_ptr = (const half2*)a_ptr;
  68. #pragma unroll
  69. for (int i = 0; i < 4; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  70. return __hfma2(result, __halves2half2(qs_h, qs_h), g_result);
  71. }
  72. __forceinline__ __device__ half2 dot22_16(half2 (&dq)[8], const half* a_ptr,
  73. const half2 g_result,
  74. const half qs_h) {
  75. half2 result = {};
  76. const half2* a2_ptr = (const half2*)a_ptr;
  77. #pragma unroll
  78. for (int i = 0; i < 8; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  79. return __hfma2(result, __halves2half2(qs_h, qs_h), g_result);
  80. }
  81. __forceinline__ __device__ half2 dot22_32(half2 (&dq)[16], const half* a_ptr,
  82. const half2 g_result,
  83. const half qs_h) {
  84. half2 result = {};
  85. const half2* a2_ptr = (const half2*)a_ptr;
  86. #pragma unroll
  87. for (int i = 0; i < 16; i += 1) result = __hfma2(dq[i], *a2_ptr++, result);
  88. return __hfma2(result, __halves2half2(qs_h, qs_h), g_result);
  89. }
  90. __forceinline__ __device__ float dot22_8_f(half2 (&dq)[4], const half* a_ptr,
  91. const float g_result,
  92. const float qs_f) {
  93. half2 result = {};
  94. const half2* a2_ptr = (const half2*)a_ptr;
  95. #pragma unroll
  96. for (int i = 0; i < 4; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  97. float result_f =
  98. __half2float(__low2half(result)) + __half2float(__high2half(result));
  99. return fma(result_f, qs_f, g_result);
  100. }
  101. __forceinline__ __device__ float dot22_16_f(half2 (&dq)[8], const half* a_ptr,
  102. const float g_result,
  103. const float qs_f) {
  104. half2 result = {};
  105. const half2* a2_ptr = (const half2*)a_ptr;
  106. #pragma unroll
  107. for (int i = 0; i < 8; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  108. float result_f =
  109. __half2float(__low2half(result)) + __half2float(__high2half(result));
  110. return fma(result_f, qs_f, g_result);
  111. }
  112. __forceinline__ __device__ float dot22_32_f(half2 (&dq)[16], const half* a_ptr,
  113. const float g_result,
  114. const float qs_f) {
  115. half2 result = {};
  116. const half2* a2_ptr = (const half2*)a_ptr;
  117. #pragma unroll
  118. for (int i = 0; i < 16; i += 1) result = __hfma2(dq[i], *a2_ptr++, result);
  119. float result_f =
  120. __half2float(__low2half(result)) + __half2float(__high2half(result));
  121. return fma(result_f, qs_f, g_result);
  122. }
  123. __forceinline__ __device__ half dot22_8_h(half2 (&dq)[4], const half* a_ptr,
  124. const half g_result,
  125. const half qs_h) {
  126. // Use FP32 accumulator to avoid potential overflow since unscaled weights are
  127. // in the range -128..127
  128. float result = {};
  129. #pragma unroll
  130. for (int i = 0; i < 4; i++) {
  131. half2 w01 = dq[i];
  132. float w0 = __low2float(w01);
  133. float w1 = __high2float(w01);
  134. float x0 = __half2float(*a_ptr++);
  135. float x1 = __half2float(*a_ptr++);
  136. result = fma(w0, x0, result);
  137. result = fma(w1, x1, result);
  138. }
  139. float qs = __half2float(qs_h);
  140. result *= qs;
  141. half result_h = __float2half_rn(result);
  142. return __hadd(result_h, g_result);
  143. }
  144. __forceinline__ __device__ half dot22_16_h(half2 (&dq)[8], const half* a_ptr,
  145. const half g_result,
  146. const half qs_h) {
  147. half2 result = {};
  148. const half2* a2_ptr = (const half2*)a_ptr;
  149. #pragma unroll
  150. for (int i = 0; i < 8; i++) result = __hfma2(dq[i], *a2_ptr++, result);
  151. half result_h = __hadd(__low2half(result), __high2half(result));
  152. return __hfma(result_h, qs_h, g_result);
  153. }
  154. __forceinline__ __device__ half dot22_32_h(half2 (&dq)[16], const half* a_ptr,
  155. const half g_result,
  156. const half qs_h) {
  157. half2 result = {};
  158. const half2* a2_ptr = (const half2*)a_ptr;
  159. #pragma unroll
  160. for (int i = 0; i < 16; i += 1) result = __hfma2(dq[i], *a2_ptr++, result);
  161. half result_h = __hadd(__low2half(result), __high2half(result));
  162. return __hfma(result_h, qs_h, g_result);
  163. }
  164. typedef void (*fp_gemm_half_q_half_gptq_kernel)(const half*, const uint32_t*,
  165. const uint32_t*, const half*,
  166. half*, const int, const int,
  167. const int, const int,
  168. const int*);
  169. template <bool first_block, int m_count>
  170. __global__ void gemm_half_q_half_gptq_4bit_kernel(
  171. const half* __restrict__ a, const uint32_t* __restrict__ b_q_weight,
  172. const uint32_t* __restrict__ b_gptq_qzeros,
  173. const half* __restrict__ b_gptq_scales, half* __restrict__ c,
  174. const int size_m, const int size_n, const int size_k, const int groups,
  175. const int* __restrict__ b_q_perm) {
  176. MatrixView_half a_(a, size_m, size_k);
  177. MatrixView_half_rw c_(c, size_m, size_n);
  178. MatrixView_q4_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  179. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  180. int t = threadIdx.x;
  181. // Block
  182. int offset_n = blockIdx.x * BLOCK_KN_SIZE * 4;
  183. int offset_m = blockIdx.y * m_count;
  184. int offset_k = blockIdx.z * BLOCK_KN_SIZE;
  185. int end_n = min(offset_n + BLOCK_KN_SIZE * 4, size_n);
  186. int end_m = min(offset_m + m_count, size_m);
  187. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  188. int n = offset_n + t * 4;
  189. // Preload block_a
  190. __shared__ half block_a[m_count][BLOCK_KN_SIZE];
  191. if (offset_k + t < end_k) {
  192. for (int m = 0; m < m_count; ++m) {
  193. const half* a_ptr = a_.item_ptr(offset_m + m, 0);
  194. half* block_a_ptr = block_a[m];
  195. half a0;
  196. if (b_q_perm)
  197. a0 = a_ptr[b_q_perm[offset_k + t]];
  198. else
  199. a0 = a_ptr[offset_k + t];
  200. block_a_ptr[t] = a0;
  201. }
  202. }
  203. // Zero output
  204. if (n >= size_n) return;
  205. if (blockIdx.z == 0) {
  206. for (int m = 0; m < m_count; m++)
  207. *((uint64_t*)c_.item_ptr(offset_m + m, n)) = 0;
  208. }
  209. __syncthreads();
  210. // Find initial group
  211. int groupsize = size_k / groups;
  212. int group = offset_k / groupsize;
  213. int nextgroup = offset_k + groupsize;
  214. // a, b offset
  215. int qk = offset_k / (32 / 4);
  216. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  217. const half* a_ptr = &block_a[0][0];
  218. int a_stride = BLOCK_KN_SIZE;
  219. // Initial group
  220. int zeros[4];
  221. float scales[4];
  222. half2 z1z16[4][2];
  223. half2 y1y16[4][2];
  224. b_gptq_qzeros_.item4(zeros, group, n);
  225. b_gptq_scales_.item4_f(scales, group, n);
  226. dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
  227. dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
  228. dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
  229. dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
  230. // Column result
  231. float block_c[m_count][4] = {};
  232. // Dequantize and multiply
  233. int k = offset_k;
  234. while (k < end_k) {
  235. if (k == nextgroup) {
  236. group++;
  237. nextgroup += groupsize;
  238. b_gptq_qzeros_.item4(zeros, group, n);
  239. b_gptq_scales_.item4_f(scales, group, n);
  240. dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
  241. dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
  242. dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
  243. dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
  244. }
  245. #pragma unroll
  246. for (int j = 0; j < 4; j++) {
  247. const int4* b_ptr4 = (int4*)b_ptr;
  248. int4 load_int4 = *b_ptr4;
  249. half2 dq[4][4];
  250. dequant_4bit_8_gptq(load_int4.x, dq[0], z1z16[0], y1y16[0], size_n,
  251. false);
  252. dequant_4bit_8_gptq(load_int4.y, dq[1], z1z16[1], y1y16[1], size_n,
  253. false);
  254. dequant_4bit_8_gptq(load_int4.z, dq[2], z1z16[2], y1y16[2], size_n,
  255. false);
  256. dequant_4bit_8_gptq(load_int4.w, dq[3], z1z16[3], y1y16[3], size_n,
  257. false);
  258. #pragma unroll
  259. for (int m = 0; m < m_count; m++) {
  260. block_c[m][0] = fma(dot22_8_f(dq[0], a_ptr + m * a_stride), scales[0],
  261. block_c[m][0]);
  262. block_c[m][1] = fma(dot22_8_f(dq[1], a_ptr + m * a_stride), scales[1],
  263. block_c[m][1]);
  264. block_c[m][2] = fma(dot22_8_f(dq[2], a_ptr + m * a_stride), scales[2],
  265. block_c[m][2]);
  266. block_c[m][3] = fma(dot22_8_f(dq[3], a_ptr + m * a_stride), scales[3],
  267. block_c[m][3]);
  268. }
  269. b_ptr += size_n;
  270. a_ptr += 8;
  271. }
  272. k += 32;
  273. }
  274. for (int m = 0; m < m_count; m++) {
  275. half2* out = (half2*)c_.item_ptr(offset_m + m, n);
  276. half2 result01 = __halves2half2(__float2half_rn(block_c[m][0]),
  277. __float2half_rn(block_c[m][1]));
  278. half2 result23 = __halves2half2(__float2half_rn(block_c[m][2]),
  279. __float2half_rn(block_c[m][3]));
  280. atomicAdd(out, result01);
  281. atomicAdd(out + 1, result23);
  282. }
  283. }
  284. template <bool first_block, int m_count>
  285. __global__ void gemm_half_q_half_gptq_2bit_kernel(
  286. const half* __restrict__ a, const uint32_t* __restrict__ b_q_weight,
  287. const uint32_t* __restrict__ b_gptq_qzeros,
  288. const half* __restrict__ b_gptq_scales, half* __restrict__ c,
  289. const int size_m, const int size_n, const int size_k, const int groups,
  290. const int* __restrict__ b_q_perm) {
  291. MatrixView_half a_(a, size_m, size_k);
  292. MatrixView_half_rw c_(c, size_m, size_n);
  293. MatrixView_q2_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  294. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  295. int t = threadIdx.x;
  296. // Block
  297. int offset_n = blockIdx.x * BLOCK_KN_SIZE * 4;
  298. int offset_m = blockIdx.y * m_count;
  299. int offset_k = blockIdx.z * BLOCK_KN_SIZE;
  300. int end_n = min(offset_n + BLOCK_KN_SIZE * 4, size_n);
  301. int end_m = min(offset_m + m_count, size_m);
  302. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  303. int n = offset_n + t * 4;
  304. // Preload block_a
  305. __shared__ half block_a[m_count][BLOCK_KN_SIZE];
  306. if (offset_k + t < end_k) {
  307. for (int m = 0; m < m_count; ++m) {
  308. const half* a_ptr = a_.item_ptr(offset_m + m, 0);
  309. half* block_a_ptr = block_a[m];
  310. half a0;
  311. if (b_q_perm)
  312. a0 = a_ptr[b_q_perm[offset_k + t]];
  313. else
  314. a0 = a_ptr[offset_k + t];
  315. block_a_ptr[t] = a0;
  316. }
  317. }
  318. // Zero output
  319. if (n >= size_n) return;
  320. if (blockIdx.z == 0) {
  321. for (int m = 0; m < m_count; m++)
  322. *((uint64_t*)c_.item_ptr(offset_m + m, n)) = 0;
  323. }
  324. __syncthreads();
  325. // Find initial group
  326. int groupsize = size_k / groups;
  327. int group = offset_k / groupsize;
  328. int nextgroup = offset_k + groupsize;
  329. // a, b offset
  330. int qk = offset_k / (32 / 2);
  331. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  332. const half* a_ptr = &block_a[0][0];
  333. int a_stride = BLOCK_KN_SIZE;
  334. // Initial group
  335. int zeros[4];
  336. half scales[4];
  337. b_gptq_qzeros_.item4(zeros, group, n);
  338. b_gptq_scales_.item4(scales, group, n);
  339. // Column result
  340. half block_c[m_count][4] = {};
  341. // Dequantize and multiply
  342. int k = offset_k;
  343. while (k < end_k) {
  344. if (k == nextgroup) {
  345. group++;
  346. nextgroup += groupsize;
  347. b_gptq_qzeros_.item4(zeros, group, n);
  348. b_gptq_scales_.item4(scales, group, n);
  349. }
  350. #pragma unroll
  351. for (int j = 0; j < 1; j++) {
  352. const int4* b_ptr4 = (int4*)b_ptr;
  353. int4 load_int4 = *b_ptr4;
  354. half2 dq[4][8];
  355. dequant_2bit_16(load_int4.x, dq[0], size_n, zeros[0] + 1);
  356. dequant_2bit_16(load_int4.y, dq[1], size_n, zeros[1] + 1);
  357. dequant_2bit_16(load_int4.z, dq[2], size_n, zeros[2] + 1);
  358. dequant_2bit_16(load_int4.w, dq[3], size_n, zeros[3] + 1);
  359. #pragma unroll
  360. for (int m = 0; m < m_count; m++) {
  361. block_c[m][0] =
  362. dot22_16_h(dq[0], a_ptr + m * a_stride, block_c[m][0], scales[0]);
  363. block_c[m][1] =
  364. dot22_16_h(dq[1], a_ptr + m * a_stride, block_c[m][1], scales[1]);
  365. block_c[m][2] =
  366. dot22_16_h(dq[2], a_ptr + m * a_stride, block_c[m][2], scales[2]);
  367. block_c[m][3] =
  368. dot22_16_h(dq[3], a_ptr + m * a_stride, block_c[m][3], scales[3]);
  369. }
  370. b_ptr += size_n;
  371. a_ptr += 16;
  372. }
  373. k += 16;
  374. }
  375. for (int m = 0; m < m_count; m++) {
  376. half2* out = (half2*)c_.item_ptr(offset_m + m, n);
  377. half2 result01 = __halves2half2(block_c[m][0], block_c[m][1]);
  378. half2 result23 = __halves2half2(block_c[m][2], block_c[m][3]);
  379. atomicAdd(out, result01);
  380. atomicAdd(out + 1, result23);
  381. }
  382. }
  383. template <bool first_block, int m_count>
  384. __global__ void gemm_half_q_half_gptq_3bit_kernel(
  385. const half* __restrict__ a, const uint32_t* __restrict__ b_q_weight,
  386. const uint32_t* __restrict__ b_gptq_qzeros,
  387. const half* __restrict__ b_gptq_scales, half* __restrict__ c,
  388. const int size_m, const int size_n, const int size_k, const int groups,
  389. const int* __restrict__ b_q_perm) {
  390. MatrixView_half a_(a, size_m, size_k);
  391. MatrixView_half_rw c_(c, size_m, size_n);
  392. MatrixView_q3_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  393. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  394. int t = threadIdx.x;
  395. // Block
  396. int offset_n = blockIdx.x * BLOCK_KN_SIZE * 4;
  397. int offset_m = blockIdx.y * m_count;
  398. int offset_k = blockIdx.z * BLOCK_KN_SIZE;
  399. int end_n = min(offset_n + BLOCK_KN_SIZE * 4, size_n);
  400. int end_m = min(offset_m + m_count, size_m);
  401. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  402. int n = offset_n + t * 4;
  403. // Preload block_a
  404. __shared__ half block_a[m_count][BLOCK_KN_SIZE];
  405. if (offset_k + t < end_k) {
  406. for (int m = 0; m < m_count; ++m) {
  407. const half* a_ptr = a_.item_ptr(offset_m + m, 0);
  408. half* block_a_ptr = block_a[m];
  409. half a0;
  410. if (b_q_perm)
  411. a0 = a_ptr[b_q_perm[offset_k + t]];
  412. else
  413. a0 = a_ptr[offset_k + t];
  414. block_a_ptr[t] = a0;
  415. }
  416. }
  417. // Zero output
  418. if (n >= size_n) return;
  419. if (blockIdx.z == 0) {
  420. for (int m = 0; m < m_count; m++)
  421. *((uint64_t*)c_.item_ptr(offset_m + m, n)) = 0;
  422. }
  423. __syncthreads();
  424. // Find initial group
  425. int groupsize = size_k / groups;
  426. int group = offset_k / groupsize;
  427. int nextgroup = offset_k + groupsize;
  428. // a, b offset
  429. int qk = offset_k / 32 * 3;
  430. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  431. const half* a_ptr = &block_a[0][0];
  432. int a_stride = BLOCK_KN_SIZE;
  433. // Initial group
  434. int zeros[4];
  435. half scales[4];
  436. b_gptq_qzeros_.item4(zeros, group, n);
  437. b_gptq_scales_.item4(scales, group, n);
  438. // Column result
  439. half block_c[m_count][4] = {};
  440. // Dequantize and multiply
  441. int k = offset_k;
  442. while (k < end_k) {
  443. if (k == nextgroup) {
  444. group++;
  445. nextgroup += groupsize;
  446. b_gptq_qzeros_.item4(zeros, group, n);
  447. b_gptq_scales_.item4(scales, group, n);
  448. }
  449. #pragma unroll
  450. for (int j = 0; j < 1; j++) {
  451. int4 load_int4[3];
  452. load_int4[0] = *((int4*)b_ptr);
  453. b_ptr += size_n;
  454. load_int4[1] = *((int4*)b_ptr);
  455. b_ptr += size_n;
  456. load_int4[2] = *((int4*)b_ptr);
  457. b_ptr += size_n;
  458. half2 dq[4][16];
  459. dequant_3bit_32(load_int4[0].x, load_int4[1].x, load_int4[2].x, dq[0],
  460. size_n, zeros[0] + 1);
  461. dequant_3bit_32(load_int4[0].y, load_int4[1].y, load_int4[2].y, dq[1],
  462. size_n, zeros[1] + 1);
  463. dequant_3bit_32(load_int4[0].z, load_int4[1].z, load_int4[2].z, dq[2],
  464. size_n, zeros[2] + 1);
  465. dequant_3bit_32(load_int4[0].w, load_int4[1].w, load_int4[2].w, dq[3],
  466. size_n, zeros[3] + 1);
  467. #pragma unroll
  468. for (int m = 0; m < m_count; m++) {
  469. block_c[m][0] =
  470. dot22_32_h(dq[0], a_ptr + m * a_stride, block_c[m][0], scales[0]);
  471. block_c[m][1] =
  472. dot22_32_h(dq[1], a_ptr + m * a_stride, block_c[m][1], scales[1]);
  473. block_c[m][2] =
  474. dot22_32_h(dq[2], a_ptr + m * a_stride, block_c[m][2], scales[2]);
  475. block_c[m][3] =
  476. dot22_32_h(dq[3], a_ptr + m * a_stride, block_c[m][3], scales[3]);
  477. }
  478. a_ptr += 32;
  479. }
  480. k += 32;
  481. }
  482. for (int m = 0; m < m_count; m++) {
  483. half2* out = (half2*)c_.item_ptr(offset_m + m, n);
  484. half2 result01 = __halves2half2(block_c[m][0], block_c[m][1]);
  485. half2 result23 = __halves2half2(block_c[m][2], block_c[m][3]);
  486. atomicAdd(out, result01);
  487. atomicAdd(out + 1, result23);
  488. }
  489. }
  490. template <bool first_block, int m_count>
  491. __global__ void gemm_half_q_half_gptq_8bit_kernel(
  492. const half* __restrict__ a, const uint32_t* __restrict__ b_q_weight,
  493. const uint32_t* __restrict__ b_gptq_qzeros,
  494. const half* __restrict__ b_gptq_scales, half* __restrict__ c,
  495. const int size_m, const int size_n, const int size_k, const int groups,
  496. const int* __restrict__ b_q_perm) {
  497. MatrixView_half a_(a, size_m, size_k);
  498. MatrixView_half_rw c_(c, size_m, size_n);
  499. MatrixView_q8_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  500. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  501. int t = threadIdx.x;
  502. // Block
  503. int offset_n = blockIdx.x * BLOCK_KN_SIZE * 4;
  504. int offset_m = blockIdx.y * m_count;
  505. int offset_k = blockIdx.z * BLOCK_KN_SIZE;
  506. int end_n = min(offset_n + BLOCK_KN_SIZE * 4, size_n);
  507. int end_m = min(offset_m + m_count, size_m);
  508. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  509. int n = offset_n + t * 4;
  510. // Preload block_a
  511. __shared__ half block_a[m_count][BLOCK_KN_SIZE];
  512. if (offset_k + t < end_k) {
  513. for (int m = 0; m < m_count; ++m) {
  514. const half* a_ptr = a_.item_ptr(offset_m + m, 0);
  515. half* block_a_ptr = block_a[m];
  516. half a0;
  517. if (b_q_perm)
  518. a0 = a_ptr[b_q_perm[offset_k + t]];
  519. else
  520. a0 = a_ptr[offset_k + t];
  521. block_a_ptr[t] = a0;
  522. }
  523. }
  524. // Zero output
  525. if (n >= size_n) return;
  526. if (blockIdx.z == 0) {
  527. for (int m = 0; m < m_count; m++)
  528. *((uint64_t*)c_.item_ptr(offset_m + m, n)) = 0;
  529. }
  530. __syncthreads();
  531. // Find initial group
  532. int groupsize = size_k / groups;
  533. int group = offset_k / groupsize;
  534. int nextgroup = offset_k + groupsize;
  535. // a, b offset
  536. int qk = offset_k / (32 / 8);
  537. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  538. const half* a_ptr = &block_a[0][0];
  539. int a_stride = BLOCK_KN_SIZE;
  540. // Initial group
  541. int zeros[4];
  542. half scales[4];
  543. b_gptq_qzeros_.item4(zeros, group, n);
  544. b_gptq_scales_.item4(scales, group, n);
  545. // Column result
  546. half block_c[m_count][4] = {};
  547. // Dequantize and multiply
  548. int k = offset_k;
  549. while (k < end_k) {
  550. if (k == nextgroup) {
  551. group++;
  552. nextgroup += groupsize;
  553. b_gptq_qzeros_.item4(zeros, group, n);
  554. b_gptq_scales_.item4(scales, group, n);
  555. }
  556. #pragma unroll
  557. for (int j = 0; j < 4; j++) {
  558. int4 load_int4[2];
  559. load_int4[0] = *((int4*)b_ptr);
  560. b_ptr += size_n;
  561. load_int4[1] = *((int4*)b_ptr);
  562. b_ptr += size_n;
  563. half2 dq[4][4];
  564. dequant_8bit_8(load_int4[0].x, load_int4[1].x, dq[0], size_n,
  565. zeros[0] + 1);
  566. dequant_8bit_8(load_int4[0].y, load_int4[1].y, dq[1], size_n,
  567. zeros[1] + 1);
  568. dequant_8bit_8(load_int4[0].z, load_int4[1].z, dq[2], size_n,
  569. zeros[2] + 1);
  570. dequant_8bit_8(load_int4[0].w, load_int4[1].w, dq[3], size_n,
  571. zeros[3] + 1);
  572. for (int m = 0; m < m_count; m++) {
  573. block_c[m][0] =
  574. dot22_8_h(dq[0], a_ptr + m * a_stride, block_c[m][0], scales[0]);
  575. block_c[m][1] =
  576. dot22_8_h(dq[1], a_ptr + m * a_stride, block_c[m][1], scales[1]);
  577. block_c[m][2] =
  578. dot22_8_h(dq[2], a_ptr + m * a_stride, block_c[m][2], scales[2]);
  579. block_c[m][3] =
  580. dot22_8_h(dq[3], a_ptr + m * a_stride, block_c[m][3], scales[3]);
  581. }
  582. a_ptr += 8;
  583. }
  584. k += 32;
  585. }
  586. for (int m = 0; m < m_count; m++) {
  587. half2* out = (half2*)c_.item_ptr(offset_m + m, n);
  588. half2 result01 = __halves2half2(block_c[m][0], block_c[m][1]);
  589. half2 result23 = __halves2half2(block_c[m][2], block_c[m][3]);
  590. atomicAdd(out, result01);
  591. atomicAdd(out + 1, result23);
  592. }
  593. }
  594. fp_gemm_half_q_half_gptq_kernel pick_gemm_half_q_half_gptq_kernel(
  595. bool first_block, const int m_count, const int bit) {
  596. #define SELECT_KERNEL(M_COUNT) \
  597. if (m_count == M_COUNT) { \
  598. if (bit == 2) return gemm_half_q_half_gptq_2bit_kernel<true, M_COUNT>; \
  599. if (bit == 3) return gemm_half_q_half_gptq_3bit_kernel<true, M_COUNT>; \
  600. if (bit == 4) return gemm_half_q_half_gptq_4bit_kernel<true, M_COUNT>; \
  601. if (bit == 8) return gemm_half_q_half_gptq_8bit_kernel<true, M_COUNT>; \
  602. }
  603. #if BLOCK_M_SIZE_MAX >= 1
  604. SELECT_KERNEL(1);
  605. #endif
  606. #if BLOCK_M_SIZE_MAX >= 2
  607. SELECT_KERNEL(2);
  608. #endif
  609. #if BLOCK_M_SIZE_MAX >= 3
  610. SELECT_KERNEL(3);
  611. #endif
  612. #if BLOCK_M_SIZE_MAX >= 4
  613. SELECT_KERNEL(4);
  614. #endif
  615. #if BLOCK_M_SIZE_MAX >= 5
  616. SELECT_KERNEL(5);
  617. #endif
  618. #if BLOCK_M_SIZE_MAX >= 6
  619. SELECT_KERNEL(6);
  620. #endif
  621. #if BLOCK_M_SIZE_MAX >= 7
  622. SELECT_KERNEL(7);
  623. #endif
  624. #if BLOCK_M_SIZE_MAX >= 8
  625. SELECT_KERNEL(8);
  626. #endif
  627. return NULL;
  628. }
  629. void gemm_half_q_half_cuda_part(const half* a, const uint32_t* b_q_weight,
  630. const uint32_t* b_gptq_qzeros,
  631. const half* b_gptq_scales, const int* b_q_perm,
  632. half* c, int size_m, int size_n, int size_k,
  633. int m_count, int groups, int bit) {
  634. dim3 blockDim, gridDim;
  635. blockDim.x = BLOCK_KN_SIZE;
  636. blockDim.y = 1;
  637. blockDim.z = 1;
  638. gridDim.x = DIVIDE(size_n, BLOCK_KN_SIZE * 4);
  639. gridDim.y = DIVIDE(size_m, m_count);
  640. gridDim.z = DIVIDE(size_k, BLOCK_KN_SIZE);
  641. fp_gemm_half_q_half_gptq_kernel kernel =
  642. pick_gemm_half_q_half_gptq_kernel(true, m_count, bit);
  643. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  644. kernel<<<gridDim, blockDim, 0, stream>>>(a, b_q_weight, b_gptq_qzeros,
  645. b_gptq_scales, c, size_m, size_n,
  646. size_k, groups, b_q_perm);
  647. }
  648. __global__ void reconstruct_exllama_8bit_kernel(
  649. const uint32_t* __restrict__ b_q_weight, const int* __restrict__ b_q_perm,
  650. const uint32_t* __restrict__ b_gptq_qzeros,
  651. const half* __restrict__ b_gptq_scales, const int size_k, const int size_n,
  652. const int groups, half* __restrict__ b) {
  653. MatrixView_half_rw b_(b, size_k, size_n);
  654. MatrixView_q8_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  655. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  656. int offset_k = BLOCK_KN_SIZE * blockIdx.y;
  657. int offset_n = BLOCK_KN_SIZE * blockIdx.x * 4;
  658. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  659. // Preload remapping table
  660. __shared__ int perm[BLOCK_KN_SIZE];
  661. int t = threadIdx.x;
  662. if (b_q_perm) {
  663. if (offset_k + t < size_k) perm[t] = b_q_perm[offset_k + t];
  664. }
  665. // Column
  666. int n = offset_n + t * 4;
  667. if (n >= size_n) return;
  668. // Find initial group
  669. int groupsize = size_k / groups;
  670. int group = offset_k / groupsize;
  671. int nextgroup = offset_k + groupsize;
  672. // b offset
  673. int qk = offset_k / (32 / 8);
  674. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  675. // Initial zeros/scale
  676. int zeros[4];
  677. half2 scales[4];
  678. b_gptq_qzeros_.item4(zeros, group, n);
  679. b_gptq_scales_.item4_h2(scales, group, n);
  680. __syncthreads();
  681. int k = offset_k;
  682. int lk = 0;
  683. while (k < end_k) {
  684. if (k == nextgroup) {
  685. group++;
  686. nextgroup += groupsize;
  687. b_gptq_qzeros_.item4(zeros, group, n);
  688. b_gptq_scales_.item4_h2(scales, group, n);
  689. }
  690. for (int p = 0; p < 4; p++) {
  691. int4 load_int4[2];
  692. load_int4[0] = *((int4*)b_ptr);
  693. b_ptr += size_n;
  694. load_int4[1] = *((int4*)b_ptr);
  695. b_ptr += size_n;
  696. half2 dq[4][4];
  697. dequant_8bit_8(load_int4[0].x, load_int4[1].x, dq[0], size_n,
  698. zeros[0] + 1);
  699. dequant_8bit_8(load_int4[0].y, load_int4[1].y, dq[1], size_n,
  700. zeros[1] + 1);
  701. dequant_8bit_8(load_int4[0].z, load_int4[1].z, dq[2], size_n,
  702. zeros[2] + 1);
  703. dequant_8bit_8(load_int4[0].w, load_int4[1].w, dq[3], size_n,
  704. zeros[3] + 1);
  705. // half* dqh = (half*)dq;
  706. if (b_q_perm) {
  707. for (int j = 0; j < 4; j++) {
  708. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  709. b_.set4(perm[lk++], n, __low2half(dq[0][j]), __low2half(dq[1][j]),
  710. __low2half(dq[2][j]), __low2half(dq[3][j]));
  711. b_.set4(perm[lk++], n, __high2half(dq[0][j]), __high2half(dq[1][j]),
  712. __high2half(dq[2][j]), __high2half(dq[3][j]));
  713. }
  714. } else {
  715. for (int j = 0; j < 4; j++) {
  716. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  717. b_.set4(offset_k + lk++, n, __low2half(dq[0][j]),
  718. __low2half(dq[1][j]), __low2half(dq[2][j]),
  719. __low2half(dq[3][j]));
  720. b_.set4(offset_k + lk++, n, __high2half(dq[0][j]),
  721. __high2half(dq[1][j]), __high2half(dq[2][j]),
  722. __high2half(dq[3][j]));
  723. }
  724. }
  725. }
  726. k += 32;
  727. }
  728. }
  729. __global__ void reconstruct_exllama_4bit_kernel(
  730. const uint32_t* __restrict__ b_q_weight, const int* __restrict__ b_q_perm,
  731. const uint32_t* __restrict__ b_gptq_qzeros,
  732. const half* __restrict__ b_gptq_scales, const int size_k, const int size_n,
  733. const int groups, half* __restrict__ b) {
  734. if (blockIdx.z > 0) {
  735. b_q_weight = b_q_weight + blockIdx.z * size_k * size_n / 8;
  736. b_gptq_scales = b_gptq_scales + blockIdx.z * groups * size_n;
  737. b_gptq_qzeros = b_gptq_qzeros + blockIdx.z * groups * size_n / 8;
  738. if (b_q_perm) b_q_perm = b_q_perm + blockIdx.z * size_k;
  739. b = b + blockIdx.z * size_k * size_n;
  740. }
  741. MatrixView_half_rw b_(b, size_k, size_n);
  742. MatrixView_q4_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  743. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  744. int offset_k = BLOCK_KN_SIZE * blockIdx.y;
  745. int offset_n = BLOCK_KN_SIZE * blockIdx.x * 4;
  746. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  747. // Preload remapping table
  748. __shared__ int perm[BLOCK_KN_SIZE];
  749. int t = threadIdx.x;
  750. if (b_q_perm) {
  751. if (offset_k + t < size_k) perm[t] = b_q_perm[offset_k + t];
  752. }
  753. // Column
  754. int n = offset_n + t * 4;
  755. if (n >= size_n) return;
  756. // Find initial group
  757. int groupsize = size_k / groups;
  758. int group = offset_k / groupsize;
  759. int nextgroup = offset_k + groupsize;
  760. // b offset
  761. int qk = offset_k / (32 / 4);
  762. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  763. // Initial zeros/scale
  764. int zeros[4];
  765. half2 scales[4];
  766. half2 z1z16[4][2];
  767. half2 y1y16[4][2];
  768. b_gptq_qzeros_.item4(zeros, group, n);
  769. b_gptq_scales_.item4_h2(scales, group, n);
  770. dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
  771. dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
  772. dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
  773. dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
  774. __syncthreads();
  775. int k = offset_k;
  776. int lk = 0;
  777. while (k < end_k) {
  778. if (k == nextgroup) {
  779. group++;
  780. nextgroup += groupsize;
  781. b_gptq_qzeros_.item4(zeros, group, n);
  782. b_gptq_scales_.item4_h2(scales, group, n);
  783. dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
  784. dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
  785. dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
  786. dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
  787. }
  788. for (int p = 0; p < 4; p++) {
  789. half2 dq[4][4];
  790. const int4* b_ptr4 = (int4*)b_ptr;
  791. int4 load_int4 = *b_ptr4;
  792. dequant_4bit_8_gptq(load_int4.x, dq[0], z1z16[0], y1y16[0], size_n,
  793. false);
  794. dequant_4bit_8_gptq(load_int4.y, dq[1], z1z16[1], y1y16[1], size_n,
  795. false);
  796. dequant_4bit_8_gptq(load_int4.z, dq[2], z1z16[2], y1y16[2], size_n,
  797. false);
  798. dequant_4bit_8_gptq(load_int4.w, dq[3], z1z16[3], y1y16[3], size_n,
  799. false);
  800. b_ptr += size_n;
  801. // half* dqh = (half*)dq;
  802. if (b_q_perm) {
  803. for (int j = 0; j < 4; j++) {
  804. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  805. b_.set4(perm[lk++], n, __low2half(dq[0][j]), __low2half(dq[1][j]),
  806. __low2half(dq[2][j]), __low2half(dq[3][j]));
  807. b_.set4(perm[lk++], n, __high2half(dq[0][j]), __high2half(dq[1][j]),
  808. __high2half(dq[2][j]), __high2half(dq[3][j]));
  809. }
  810. } else {
  811. for (int j = 0; j < 4; j++) {
  812. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  813. b_.set4(offset_k + lk++, n, __low2half(dq[0][j]),
  814. __low2half(dq[1][j]), __low2half(dq[2][j]),
  815. __low2half(dq[3][j]));
  816. b_.set4(offset_k + lk++, n, __high2half(dq[0][j]),
  817. __high2half(dq[1][j]), __high2half(dq[2][j]),
  818. __high2half(dq[3][j]));
  819. }
  820. }
  821. }
  822. k += 32;
  823. }
  824. }
  825. __global__ void reconstruct_exllama_3bit_kernel(
  826. const uint32_t* __restrict__ b_q_weight, const int* __restrict__ b_q_perm,
  827. const uint32_t* __restrict__ b_gptq_qzeros,
  828. const half* __restrict__ b_gptq_scales, const int size_k, const int size_n,
  829. const int groups, half* __restrict__ b) {
  830. MatrixView_half_rw b_(b, size_k, size_n);
  831. MatrixView_q3_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  832. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  833. int offset_k = BLOCK_KN_SIZE * blockIdx.y;
  834. int offset_n = BLOCK_KN_SIZE * blockIdx.x * 4;
  835. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  836. // Preload remapping table
  837. __shared__ int perm[BLOCK_KN_SIZE];
  838. int t = threadIdx.x;
  839. if (b_q_perm) {
  840. if (offset_k + t < size_k) perm[t] = b_q_perm[offset_k + t];
  841. }
  842. // Column
  843. int n = offset_n + t * 4;
  844. if (n >= size_n) return;
  845. // Find initial group
  846. int groupsize = size_k / groups;
  847. int group = offset_k / groupsize;
  848. int nextgroup = offset_k + groupsize;
  849. // b offset
  850. int qk = offset_k / 32 * 3;
  851. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  852. // Initial zeros/scale
  853. int zeros[4];
  854. half2 scales[4];
  855. b_gptq_qzeros_.item4(zeros, group, n);
  856. b_gptq_scales_.item4_h2(scales, group, n);
  857. __syncthreads();
  858. int k = offset_k;
  859. int lk = 0;
  860. while (k < end_k) {
  861. if (k == nextgroup) {
  862. group++;
  863. nextgroup += groupsize;
  864. b_gptq_qzeros_.item4(zeros, group, n);
  865. b_gptq_scales_.item4_h2(scales, group, n);
  866. }
  867. for (int p = 0; p < 1; p++) {
  868. int4 load_int4[3];
  869. load_int4[0] = *((int4*)b_ptr);
  870. b_ptr += size_n;
  871. load_int4[1] = *((int4*)b_ptr);
  872. b_ptr += size_n;
  873. load_int4[2] = *((int4*)b_ptr);
  874. b_ptr += size_n;
  875. half2 dq[4][16];
  876. dequant_3bit_32(load_int4[0].x, load_int4[1].x, load_int4[2].x, dq[0],
  877. size_n, zeros[0] + 1);
  878. dequant_3bit_32(load_int4[0].y, load_int4[1].y, load_int4[2].y, dq[1],
  879. size_n, zeros[1] + 1);
  880. dequant_3bit_32(load_int4[0].z, load_int4[1].z, load_int4[2].z, dq[2],
  881. size_n, zeros[2] + 1);
  882. dequant_3bit_32(load_int4[0].w, load_int4[1].w, load_int4[2].w, dq[3],
  883. size_n, zeros[3] + 1);
  884. if (b_q_perm) {
  885. for (int j = 0; j < 16; j++) {
  886. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  887. b_.set4(perm[lk++], n, __low2half(dq[0][j]), __low2half(dq[1][j]),
  888. __low2half(dq[2][j]), __low2half(dq[3][j]));
  889. b_.set4(perm[lk++], n, __high2half(dq[0][j]), __high2half(dq[1][j]),
  890. __high2half(dq[2][j]), __high2half(dq[3][j]));
  891. }
  892. } else {
  893. for (int j = 0; j < 16; j++) {
  894. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  895. b_.set4(offset_k + lk++, n, __low2half(dq[0][j]),
  896. __low2half(dq[1][j]), __low2half(dq[2][j]),
  897. __low2half(dq[3][j]));
  898. b_.set4(offset_k + lk++, n, __high2half(dq[0][j]),
  899. __high2half(dq[1][j]), __high2half(dq[2][j]),
  900. __high2half(dq[3][j]));
  901. }
  902. }
  903. }
  904. k += 32;
  905. }
  906. }
  907. __global__ void reconstruct_exllama_2bit_kernel(
  908. const uint32_t* __restrict__ b_q_weight, const int* __restrict__ b_q_perm,
  909. const uint32_t* __restrict__ b_gptq_qzeros,
  910. const half* __restrict__ b_gptq_scales, const int size_k, const int size_n,
  911. const int groups, half* __restrict__ b) {
  912. MatrixView_half_rw b_(b, size_k, size_n);
  913. MatrixView_q2_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  914. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  915. int offset_k = BLOCK_KN_SIZE * blockIdx.y;
  916. int offset_n = BLOCK_KN_SIZE * blockIdx.x * 4;
  917. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  918. // Preload remapping table
  919. __shared__ int perm[BLOCK_KN_SIZE];
  920. int t = threadIdx.x;
  921. if (b_q_perm) {
  922. if (offset_k + t < size_k) perm[t] = b_q_perm[offset_k + t];
  923. }
  924. // Column
  925. int n = offset_n + t * 4;
  926. if (n >= size_n) return;
  927. // Find initial group
  928. int groupsize = size_k / groups;
  929. int group = offset_k / groupsize;
  930. int nextgroup = offset_k + groupsize;
  931. // b offset
  932. int qk = offset_k / (32 / 2);
  933. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  934. // Initial zeros/scale
  935. int zeros[4];
  936. half2 scales[4];
  937. b_gptq_qzeros_.item4(zeros, group, n);
  938. b_gptq_scales_.item4_h2(scales, group, n);
  939. __syncthreads();
  940. int k = offset_k;
  941. int lk = 0;
  942. while (k < end_k) {
  943. if (k == nextgroup) {
  944. group++;
  945. nextgroup += groupsize;
  946. b_gptq_qzeros_.item4(zeros, group, n);
  947. b_gptq_scales_.item4_h2(scales, group, n);
  948. }
  949. for (int p = 0; p < 2; p++) {
  950. const int4* b_ptr4 = (int4*)b_ptr;
  951. int4 load_int4 = *b_ptr4;
  952. half2 dq[4][8];
  953. dequant_2bit_16(load_int4.x, dq[0], size_n, zeros[0] + 1);
  954. dequant_2bit_16(load_int4.y, dq[1], size_n, zeros[1] + 1);
  955. dequant_2bit_16(load_int4.z, dq[2], size_n, zeros[2] + 1);
  956. dequant_2bit_16(load_int4.w, dq[3], size_n, zeros[3] + 1);
  957. b_ptr += size_n;
  958. // half* dqh = (half*)dq;
  959. if (b_q_perm) {
  960. for (int j = 0; j < 8; j++) {
  961. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  962. b_.set4(perm[lk++], n, __low2half(dq[0][j]), __low2half(dq[1][j]),
  963. __low2half(dq[2][j]), __low2half(dq[3][j]));
  964. b_.set4(perm[lk++], n, __high2half(dq[0][j]), __high2half(dq[1][j]),
  965. __high2half(dq[2][j]), __high2half(dq[3][j]));
  966. }
  967. } else {
  968. for (int j = 0; j < 8; j++) {
  969. for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
  970. b_.set4(offset_k + lk++, n, __low2half(dq[0][j]),
  971. __low2half(dq[1][j]), __low2half(dq[2][j]),
  972. __low2half(dq[3][j]));
  973. b_.set4(offset_k + lk++, n, __high2half(dq[0][j]),
  974. __high2half(dq[1][j]), __high2half(dq[2][j]),
  975. __high2half(dq[3][j]));
  976. }
  977. }
  978. }
  979. k += 32;
  980. }
  981. }
  982. void reconstruct_exllama(const uint32_t* b_q_weight,
  983. const uint32_t* b_gptq_qzeros,
  984. const half* b_gptq_scales, const int* b_q_perm,
  985. half* out, int height, int width, int groups,
  986. int num_experts, int bit) {
  987. dim3 blockDim, gridDim;
  988. blockDim.x = BLOCK_KN_SIZE;
  989. blockDim.y = 1;
  990. gridDim.y = DIVIDE(height, BLOCK_KN_SIZE);
  991. gridDim.x = DIVIDE(width, BLOCK_KN_SIZE);
  992. gridDim.z = num_experts;
  993. auto reconstruct_exllama_kernel = reconstruct_exllama_4bit_kernel;
  994. if (bit == 2) {
  995. reconstruct_exllama_kernel = reconstruct_exllama_2bit_kernel;
  996. } else if (bit == 3) {
  997. reconstruct_exllama_kernel = reconstruct_exllama_3bit_kernel;
  998. } else if (bit == 8) {
  999. reconstruct_exllama_kernel = reconstruct_exllama_8bit_kernel;
  1000. }
  1001. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1002. reconstruct_exllama_kernel<<<gridDim, blockDim, 0, stream>>>(
  1003. b_q_weight, b_q_perm, b_gptq_qzeros, b_gptq_scales, height, width, groups,
  1004. out);
  1005. }
  1006. __global__ void gemm_half_q_half_alt_4bit_kernel(
  1007. const half2* __restrict__ vec, const uint32_t* __restrict__ mat,
  1008. half* __restrict__ mul, const half* __restrict__ scales,
  1009. const uint32_t* __restrict__ zeros, const int* __restrict__ g_idx,
  1010. int batch, int height, int width) {
  1011. int zero_width = width / 8;
  1012. int vec_height = height * 4;
  1013. const int blockwidth2 = BLOCK_KN_SIZE / 2;
  1014. int b = blockIdx.y * BLOCK_M_SIZE_MAX;
  1015. int b_end = min(BLOCK_M_SIZE_MAX, batch - b);
  1016. int h = BLOCK_KN_SIZE * blockIdx.z / 8;
  1017. int h_end = min(BLOCK_KN_SIZE / 8, height - h) * 4;
  1018. int w = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
  1019. __shared__ half2 blockvec[BLOCK_M_SIZE_MAX][blockwidth2];
  1020. if (threadIdx.x < h_end) {
  1021. for (int m = 0; m < b_end; ++m) {
  1022. blockvec[m][threadIdx.x] =
  1023. vec[(m + b) * vec_height + blockIdx.z * BLOCK_KN_SIZE / 2 +
  1024. threadIdx.x];
  1025. }
  1026. }
  1027. __shared__ half2 deq2[256][8];
  1028. int val = threadIdx.x / 8;
  1029. int off = threadIdx.x % 8;
  1030. for (; val < 256; val += BLOCK_KN_SIZE / 8) {
  1031. deq2[val][off] =
  1032. __halves2half2(__int2half_rn(val & 0xF), __int2half_rn(val >> 4));
  1033. }
  1034. if (blockIdx.z == 0) {
  1035. for (int m = 0; m < b_end; m++) mul[(b + m) * width + w] = __int2half_rn(0);
  1036. }
  1037. __syncthreads();
  1038. int i = width * h + w;
  1039. int g_h = h * 8;
  1040. int k = 0;
  1041. int z_w = w / 8;
  1042. int z_mod = (w % 8) * 4;
  1043. half2 res2;
  1044. half res[BLOCK_M_SIZE_MAX] = {};
  1045. unsigned int tmp;
  1046. while (k < h_end) {
  1047. tmp = mat[i];
  1048. half2 scales_tmp[4];
  1049. half2 zeros_tmp[4];
  1050. for (int tmp_k = 0; tmp_k < 4; tmp_k++) {
  1051. int g = g_idx[g_h + (k + tmp_k) * 2];
  1052. int g2 = g_idx[g_h + (k + tmp_k) * 2 + 1];
  1053. half scale_f = scales[g * width + w];
  1054. half scale_f2 = scales[g2 * width + w];
  1055. half2 scale = __halves2half2(scale_f, scale_f2);
  1056. half2 zero = __halves2half2(
  1057. __hmul(scale_f,
  1058. __int2half_rn(-((zeros[g * zero_width + z_w] >> z_mod) & 0xF) -
  1059. 1)),
  1060. __hmul(scale_f2,
  1061. __int2half_rn(
  1062. -((zeros[g2 * zero_width + z_w] >> z_mod) & 0xF) - 1)));
  1063. scales_tmp[tmp_k] = scale;
  1064. zeros_tmp[tmp_k] = zero;
  1065. }
  1066. for (int m = 0; m < b_end; m++) {
  1067. #ifndef USE_ROCM
  1068. res2 = {};
  1069. #else
  1070. res2.x = __half_as_ushort(__float2half(0));
  1071. res2.y = __half_as_ushort(__float2half(0));
  1072. #endif
  1073. res2 = __hfma2(
  1074. __hfma2(deq2[(tmp >> 0) & 0xff][off], scales_tmp[0], zeros_tmp[0]),
  1075. blockvec[m][k + 0], res2);
  1076. res2 = __hfma2(
  1077. __hfma2(deq2[(tmp >> 8) & 0xff][off], scales_tmp[1], zeros_tmp[1]),
  1078. blockvec[m][k + 1], res2);
  1079. res2 = __hfma2(
  1080. __hfma2(deq2[(tmp >> 16) & 0xff][off], scales_tmp[2], zeros_tmp[2]),
  1081. blockvec[m][k + 2], res2);
  1082. res2 = __hfma2(
  1083. __hfma2(deq2[(tmp >> 24) & 0xff][off], scales_tmp[3], zeros_tmp[3]),
  1084. blockvec[m][k + 3], res2);
  1085. #ifndef USE_ROCM
  1086. res[m] = __hadd(res[m], __hadd(res2.x, res2.y));
  1087. #else
  1088. res[m] = __hadd(
  1089. res[m], __hadd(__ushort_as_half(res2.x), __ushort_as_half(res2.y)));
  1090. #endif
  1091. }
  1092. i += width;
  1093. k += 4;
  1094. }
  1095. for (int m = 0; m < b_end; m++) {
  1096. atomicAdd(&mul[(b + m) * width + w], res[m]);
  1097. }
  1098. }
  1099. __global__ void gemm_half_q_half_alt_8bit_kernel(
  1100. const half2* __restrict__ vec, const uint32_t* __restrict__ mat,
  1101. half* __restrict__ mul, const half* __restrict__ scales,
  1102. const uint32_t* __restrict__ zeros, const int* __restrict__ g_idx,
  1103. int batch, int height, int width) {
  1104. int zero_width = width / 4;
  1105. int vec_height = height * 2;
  1106. const int blockwidth2 = BLOCK_KN_SIZE / 2;
  1107. int b = blockIdx.y * BLOCK_M_SIZE_MAX;
  1108. int b_end = min(BLOCK_M_SIZE_MAX, batch - b);
  1109. int h = BLOCK_KN_SIZE * blockIdx.z / 4;
  1110. int h_end = min(BLOCK_KN_SIZE / 4, height - h) * 2;
  1111. int w = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
  1112. __shared__ half2 blockvec[BLOCK_M_SIZE_MAX][blockwidth2];
  1113. if (threadIdx.x < h_end) {
  1114. for (int m = 0; m < b_end; ++m) {
  1115. blockvec[m][threadIdx.x] =
  1116. vec[(m + b) * vec_height + blockIdx.z * BLOCK_KN_SIZE / 2 +
  1117. threadIdx.x];
  1118. }
  1119. }
  1120. if (blockIdx.z == 0) {
  1121. for (int m = 0; m < b_end; m++) mul[(b + m) * width + w] = __int2half_rn(0);
  1122. }
  1123. __syncthreads();
  1124. int i = width * h + w;
  1125. int g_h = h * 4;
  1126. int k = 0;
  1127. int z_w = w / 4;
  1128. int z_mod = (w % 4) * 8;
  1129. half2 res2;
  1130. half res[BLOCK_M_SIZE_MAX] = {};
  1131. unsigned int tmp;
  1132. while (k < h_end) {
  1133. tmp = mat[i];
  1134. half2 scales_tmp[2];
  1135. half2 zeros_tmp[2];
  1136. for (int tmp_k = 0; tmp_k < 2; tmp_k++) {
  1137. int g = g_idx[g_h + (k + tmp_k) * 2];
  1138. int g2 = g_idx[g_h + (k + tmp_k) * 2 + 1];
  1139. half scale_f = scales[g * width + w];
  1140. half scale_f2 = scales[g2 * width + w];
  1141. half2 scale = __halves2half2(scale_f, scale_f2);
  1142. half2 zero = __halves2half2(
  1143. __hmul(scale_f,
  1144. __int2half_rn(
  1145. -((zeros[g * zero_width + z_w] >> z_mod) & 0xff) - 1)),
  1146. __hmul(scale_f2,
  1147. __int2half_rn(
  1148. -((zeros[g2 * zero_width + z_w] >> z_mod) & 0xff) - 1)));
  1149. scales_tmp[tmp_k] = scale;
  1150. zeros_tmp[tmp_k] = zero;
  1151. }
  1152. for (int m = 0; m < b_end; m++) {
  1153. #ifndef USE_ROCM
  1154. res2 = {};
  1155. #else
  1156. res2.x = __half_as_ushort(__float2half(0));
  1157. res2.y = __half_as_ushort(__float2half(0));
  1158. #endif
  1159. half2 v12 = __halves2half2(__int2half_rn(tmp & 0xFF),
  1160. __int2half_rn((tmp >> 8) & 0xFF));
  1161. res2 = __hfma2(__hfma2(v12, scales_tmp[0], zeros_tmp[0]),
  1162. blockvec[m][k + 0], res2);
  1163. half2 v34 = __halves2half2(__int2half_rn((tmp >> 16) & 0xFF),
  1164. __int2half_rn((tmp >> 24) & 0xFF));
  1165. res2 = __hfma2(__hfma2(v34, scales_tmp[1], zeros_tmp[1]),
  1166. blockvec[m][k + 1], res2);
  1167. #ifndef USE_ROCM
  1168. res[m] = __hadd(res[m], __hadd(res2.x, res2.y));
  1169. #else
  1170. res[m] = __hadd(
  1171. res[m], __hadd(__ushort_as_half(res2.x), __ushort_as_half(res2.y)));
  1172. #endif
  1173. }
  1174. i += width;
  1175. k += 2;
  1176. }
  1177. for (int m = 0; m < b_end; m++) {
  1178. atomicAdd(&mul[(b + m) * width + w], res[m]);
  1179. }
  1180. }
  1181. void gemm_half_q_half_alt(const half* a, const uint32_t* b_q_weight,
  1182. const uint32_t* b_gptq_qzeros,
  1183. const half* b_gptq_scales, const int* b_g_idx,
  1184. half* c, int size_m, int size_n, int size_k,
  1185. int bit) {
  1186. dim3 blockDim, gridDim;
  1187. blockDim.x = BLOCK_KN_SIZE;
  1188. blockDim.y = 1;
  1189. blockDim.z = 1;
  1190. gridDim.x = DIVIDE(size_n, BLOCK_KN_SIZE);
  1191. gridDim.y = DIVIDE(size_m, BLOCK_M_SIZE_MAX);
  1192. gridDim.z = DIVIDE(size_k, BLOCK_KN_SIZE);
  1193. auto kernel = gemm_half_q_half_alt_4bit_kernel;
  1194. if (bit == 8) {
  1195. kernel = gemm_half_q_half_alt_8bit_kernel;
  1196. }
  1197. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1198. kernel<<<gridDim, blockDim, 0, stream>>>(
  1199. (const half2*)a, b_q_weight, c, b_gptq_scales, b_gptq_qzeros, b_g_idx,
  1200. size_m, size_k / 32 * bit, size_n);
  1201. }
  1202. template <class T, int bit>
  1203. __global__ void reconstruct_gptq_kernel(const uint32_t* __restrict__ w,
  1204. const half* __restrict__ w_scales,
  1205. const uint32_t* __restrict__ w_zeros,
  1206. const int* __restrict__ g_idx,
  1207. const int height, const int width,
  1208. const int group,
  1209. half* __restrict__ out) {
  1210. if (blockIdx.z > 0) {
  1211. w = w + blockIdx.z * height * width / 8;
  1212. w_scales = w_scales + blockIdx.z * group * width;
  1213. w_zeros = w_zeros + blockIdx.z * group * width / 8;
  1214. g_idx = g_idx + blockIdx.z * height;
  1215. out = out + blockIdx.z * height * width;
  1216. }
  1217. // Start of block
  1218. int column = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
  1219. int row = blockIdx.y * 32 / bit;
  1220. if (column >= width) return;
  1221. // Views
  1222. MatrixView_half_rw out_(out, height, width);
  1223. MatrixView_half w_scales_(w_scales, group, width);
  1224. T w_zeros_(w_zeros, group, width);
  1225. uint32_t w_read = w[blockIdx.y * width + column];
  1226. half* out_ptr = out_.item_ptr(row, column);
  1227. #pragma unroll
  1228. for (int s = 0; s < 32; s += bit) {
  1229. int group = g_idx[row + s / bit];
  1230. half w_scale = w_scales_.item(group, column);
  1231. uint32_t w_zero = w_zeros_.item(group, column) + 1;
  1232. half w_item =
  1233. __hmul(__int2half_rn((int)((w_read >> s) & ((1 << bit) - 1)) - w_zero),
  1234. w_scale);
  1235. *out_ptr = w_item;
  1236. out_ptr += out_.width;
  1237. }
  1238. }
  1239. __global__ void reconstruct_gptq_3bit_kernel(
  1240. const uint32_t* __restrict__ w, const half* __restrict__ w_scales,
  1241. const uint32_t* __restrict__ w_zeros, const int* __restrict__ g_idx,
  1242. const int height, const int width, const int group,
  1243. half* __restrict__ out) {
  1244. // Start of block
  1245. int column = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
  1246. int row = blockIdx.y * 32;
  1247. if (column >= width) return;
  1248. // Views
  1249. MatrixView_half_rw out_(out, height, width);
  1250. MatrixView_half w_scales_(w_scales, group, width);
  1251. MatrixView_q3_row w_zeros_(w_zeros, group, width);
  1252. uint32_t w1 = w[(blockIdx.y * 3) * width + column];
  1253. uint32_t w2 = w[(blockIdx.y * 3 + 1) * width + column];
  1254. uint32_t w3 = w[(blockIdx.y * 3 + 2) * width + column];
  1255. half* out_ptr = out_.item_ptr(row, column);
  1256. #pragma unroll
  1257. for (int i = 0; i < 32; i += 1) {
  1258. int group = g_idx[row + i];
  1259. half w_scale = w_scales_.item(group, column);
  1260. uint32_t w_zero = w_zeros_.item(group, column) + 1;
  1261. int w_item;
  1262. if (i == 10) {
  1263. w_item = (w1 >> 30) | ((w2 << 2) & 0x4);
  1264. } else if (i == 21) {
  1265. w_item = (w2 >> 31) | ((w3 << 1) & 0x6);
  1266. } else if (i < 10) {
  1267. w_item = ((w1 >> (i * 3)) & 0x7);
  1268. } else if (i < 21) {
  1269. w_item = ((w2 >> (i * 3 - 32)) & 0x7);
  1270. } else {
  1271. w_item = ((w3 >> (i * 3 - 64)) & 0x7);
  1272. }
  1273. *out_ptr = __hmul(__int2half_rn(w_item - w_zero), w_scale);
  1274. out_ptr += out_.width;
  1275. }
  1276. }
  1277. void reconstruct_gptq(const uint32_t* b_q_weight, const uint32_t* b_gptq_qzeros,
  1278. const half* b_gptq_scales, const int* b_g_idx, half* out,
  1279. int height, int width, int groups, int num_experts,
  1280. int bit) {
  1281. dim3 blockDim, gridDim;
  1282. blockDim.x = BLOCK_KN_SIZE;
  1283. blockDim.y = 1;
  1284. gridDim.y = DIVIDE(height, 32 / bit);
  1285. gridDim.x = DIVIDE(width, BLOCK_KN_SIZE);
  1286. gridDim.z = num_experts;
  1287. auto kernel = reconstruct_gptq_kernel<MatrixView_q4_row, 4>;
  1288. if (bit == 2) {
  1289. kernel = reconstruct_gptq_kernel<MatrixView_q2_row, 2>;
  1290. } else if (bit == 8) {
  1291. kernel = reconstruct_gptq_kernel<MatrixView_q8_row, 8>;
  1292. } else if (bit == 3) {
  1293. kernel = reconstruct_gptq_3bit_kernel;
  1294. gridDim.y = DIVIDE(height, 32);
  1295. }
  1296. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1297. kernel<<<gridDim, blockDim, 0, stream>>>(b_q_weight, b_gptq_scales,
  1298. b_gptq_qzeros, b_g_idx, height,
  1299. width, groups, out);
  1300. }
  1301. void dequant_gptq_cuda(const uint32_t* b_q_weight,
  1302. const uint32_t* b_gptq_qzeros, const half* b_gptq_scales,
  1303. const int* b_g_idx, half* temp_dq, int size_k,
  1304. int size_n, int groups, int num_experts, int bits,
  1305. bool use_exllama) {
  1306. if (use_exllama) {
  1307. reconstruct_exllama(b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx,
  1308. temp_dq, size_k, size_n, groups, num_experts, bits);
  1309. } else {
  1310. reconstruct_gptq(b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx, temp_dq,
  1311. size_k, size_n, groups, num_experts, bits);
  1312. }
  1313. }
  1314. void gemm_half_q_half_cuda(cublasHandle_t cublas_handle, const half* a,
  1315. const uint32_t* b_q_weight,
  1316. const uint32_t* b_gptq_qzeros,
  1317. const half* b_gptq_scales, const int* b_g_idx,
  1318. half* c, half* temp_dq, int size_m, int size_n,
  1319. int size_k, int groups, bool use_exllama, int bit) {
  1320. bool use_reconstruct;
  1321. if (use_exllama) {
  1322. use_reconstruct = ((bit == 8 && size_m > MAX_Q_GEMM_ROWS_8BIT) ||
  1323. (bit != 8 && size_m > MAX_Q_GEMM_ROWS));
  1324. } else {
  1325. // The 2/3-bit kernels are somehow slower than dequant + gemm baseline, so
  1326. // we disabled them for now.
  1327. use_reconstruct = (bit < 4 || size_m > MAX_ALT_GEMM_ROWS);
  1328. }
  1329. if (use_reconstruct) {
  1330. // Reconstruct FP16 matrix, then cuBLAS
  1331. dequant_gptq_cuda(b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx,
  1332. temp_dq, size_k, size_n, groups, 1, bit, use_exllama);
  1333. const half alpha = __float2half(1.0f);
  1334. const half beta = __float2half(0.0f);
  1335. cublasHgemm(cublas_handle, CUBLAS_OP_N, CUBLAS_OP_N, size_n, size_m, size_k,
  1336. &alpha, temp_dq, size_n, a, size_k, &beta, c, size_n);
  1337. } else if (use_exllama) {
  1338. // Quantized matmul
  1339. int max_chunks = size_m / BLOCK_M_SIZE_MAX;
  1340. int last_chunk = max_chunks * BLOCK_M_SIZE_MAX;
  1341. int last_chunk_size = size_m - last_chunk;
  1342. if (max_chunks) {
  1343. gemm_half_q_half_cuda_part(a, b_q_weight, b_gptq_qzeros, b_gptq_scales,
  1344. b_g_idx, c, last_chunk, size_n, size_k,
  1345. BLOCK_M_SIZE_MAX, groups, bit);
  1346. }
  1347. if (last_chunk_size) {
  1348. gemm_half_q_half_cuda_part(a + last_chunk * size_k, b_q_weight,
  1349. b_gptq_qzeros, b_gptq_scales, b_g_idx,
  1350. c + last_chunk * size_n, last_chunk_size,
  1351. size_n, size_k, last_chunk_size, groups, bit);
  1352. }
  1353. } else {
  1354. gemm_half_q_half_alt(a, b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx,
  1355. c, size_m, size_n, size_k, bit);
  1356. }
  1357. }
  1358. __global__ void shuffle_4bit_kernel(uint32_t* __restrict__ b_q_weight,
  1359. const int size_k, const int size_n) {
  1360. int n = blockIdx.x * THREADS_X + threadIdx.x;
  1361. if (n >= size_n) return;
  1362. int k = 0;
  1363. uint32_t* b_ptr = b_q_weight + n;
  1364. while (k < size_k) {
  1365. shuffle_4bit_8(b_ptr, size_n);
  1366. b_ptr += 1 * size_n;
  1367. k += 8;
  1368. }
  1369. }
  1370. __global__ void shuffle_8bit_kernel(uint32_t* __restrict__ b_q_weight,
  1371. const int size_k, const int size_n) {
  1372. int n = blockIdx.x * THREADS_X + threadIdx.x;
  1373. if (n >= size_n) return;
  1374. int k = 0;
  1375. uint32_t* b_ptr = b_q_weight + n;
  1376. while (k < size_k) {
  1377. shuffle_8bit_4(b_ptr, size_n);
  1378. b_ptr += 1 * size_n;
  1379. k += 4;
  1380. }
  1381. }
  1382. __global__ void shuffle_2bit_kernel(uint32_t* __restrict__ b_q_weight,
  1383. const int size_k, const int size_n) {
  1384. int n = blockIdx.x * THREADS_X + threadIdx.x;
  1385. if (n >= size_n) return;
  1386. int k = 0;
  1387. uint32_t* b_ptr = b_q_weight + n;
  1388. while (k < size_k) {
  1389. shuffle_2bit_16(b_ptr, size_n);
  1390. b_ptr += 1 * size_n;
  1391. k += 16;
  1392. }
  1393. }
  1394. __global__ void shuffle_3bit_kernel(uint32_t* __restrict__ b_q_weight,
  1395. const int size_k, const int size_n) {
  1396. int n = blockIdx.x * THREADS_X + threadIdx.x;
  1397. if (n >= size_n) return;
  1398. int k = 0;
  1399. uint32_t* b_ptr = b_q_weight + n;
  1400. while (k < size_k) {
  1401. shuffle_3bit_32(b_ptr, size_n);
  1402. b_ptr += 3 * size_n;
  1403. k += 32;
  1404. }
  1405. }
  1406. __global__ void make_sequential_4bit_kernel(const uint32_t* __restrict__ w,
  1407. uint32_t* __restrict__ w_new,
  1408. const int* __restrict__ q_perm,
  1409. const int w_height,
  1410. const int w_width) {
  1411. if (blockIdx.z > 0) {
  1412. w = w + blockIdx.z * w_height * w_width;
  1413. w_new = w_new + blockIdx.z * w_height * w_width;
  1414. q_perm = q_perm + blockIdx.z * w_height * 8;
  1415. }
  1416. const uint64_t* w2 = (uint64_t*)w;
  1417. uint64_t* w_new2 = (uint64_t*)w_new;
  1418. int w2_stride = w_width >> 1;
  1419. int w2_column = THREADS_X * blockIdx.x + threadIdx.x;
  1420. if (w2_column >= w2_stride) return;
  1421. int w_new2_row = blockIdx.y;
  1422. int q_perm_idx = w_new2_row << 3;
  1423. uint64_t dst = 0;
  1424. #pragma unroll
  1425. for (int i = 0; i < 8; i++) {
  1426. int source_row = q_perm[q_perm_idx++];
  1427. int w2_row = source_row >> 3;
  1428. int w2_subrow = source_row & 0x07;
  1429. int w2_row_shift = w2_subrow << 2;
  1430. int wnew2_row_shift = i << 2;
  1431. uint64_t src = w2[w2_row * w2_stride + w2_column];
  1432. src >>= w2_row_shift;
  1433. src &= 0x0000000f0000000f;
  1434. src <<= wnew2_row_shift;
  1435. dst |= src;
  1436. }
  1437. w_new2[w_new2_row * w2_stride + w2_column] = dst;
  1438. }
  1439. __global__ void make_sequential_2bit_kernel(const uint32_t* __restrict__ w,
  1440. uint32_t* __restrict__ w_new,
  1441. const int* __restrict__ q_perm,
  1442. const int w_height,
  1443. const int w_width) {
  1444. if (blockIdx.z > 0) {
  1445. w = w + blockIdx.z * w_height * w_width;
  1446. w_new = w_new + blockIdx.z * w_height * w_width;
  1447. q_perm = q_perm + blockIdx.z * w_height * 16;
  1448. }
  1449. const uint64_t* w2 = (uint64_t*)w;
  1450. uint64_t* w_new2 = (uint64_t*)w_new;
  1451. int w2_stride = w_width >> 1;
  1452. int w2_column = THREADS_X * blockIdx.x + threadIdx.x;
  1453. if (w2_column >= w2_stride) return;
  1454. int w_new2_row = blockIdx.y;
  1455. int q_perm_idx = w_new2_row << 4;
  1456. uint64_t dst = 0;
  1457. #pragma unroll
  1458. for (int i = 0; i < 16; i++) {
  1459. int source_row = q_perm[q_perm_idx++];
  1460. int w2_row = source_row >> 4;
  1461. int w2_subrow = source_row & 0x0f;
  1462. int w2_row_shift = w2_subrow << 1;
  1463. int wnew2_row_shift = i << 1;
  1464. uint64_t src = w2[w2_row * w2_stride + w2_column];
  1465. src >>= w2_row_shift;
  1466. src &= 0x0000000300000003;
  1467. src <<= wnew2_row_shift;
  1468. dst |= src;
  1469. }
  1470. w_new2[w_new2_row * w2_stride + w2_column] = dst;
  1471. }
  1472. __global__ void make_sequential_3bit_kernel(const uint32_t* __restrict__ w,
  1473. uint32_t* __restrict__ w_new,
  1474. const int* __restrict__ q_perm,
  1475. const int w_height,
  1476. const int w_width) {
  1477. if (blockIdx.z > 0) {
  1478. w = w + blockIdx.z * w_height * w_width;
  1479. w_new = w_new + blockIdx.z * w_height * w_width;
  1480. q_perm = q_perm + blockIdx.z * w_height * 32 / 3;
  1481. }
  1482. int w_column = THREADS_X * blockIdx.x + threadIdx.x;
  1483. if (w_column >= w_width) return;
  1484. int w_new_row = blockIdx.y * 3;
  1485. int q_perm_idx = blockIdx.y << 5;
  1486. uint32_t dst[3] = {0, 0, 0};
  1487. #pragma unroll
  1488. for (int i = 0; i < 32; i++) {
  1489. int source_row = q_perm[q_perm_idx++];
  1490. int z_w = (source_row / 32) * 3;
  1491. int z_mod = source_row % 32;
  1492. int z_bit;
  1493. if (z_mod != 10) {
  1494. if (z_mod != 21) {
  1495. z_bit = z_mod;
  1496. if (z_bit > 21) {
  1497. z_bit *= 3;
  1498. z_bit -= 64;
  1499. z_w += 2;
  1500. } else if (z_bit > 10) {
  1501. z_bit *= 3;
  1502. z_bit -= 32;
  1503. z_w += 1;
  1504. } else {
  1505. z_bit *= 3;
  1506. }
  1507. } else {
  1508. z_w += 1;
  1509. }
  1510. }
  1511. uint64_t src;
  1512. if (z_mod == 10) {
  1513. src = (w[z_w * w_width + w_column] >> 30) |
  1514. ((w[(z_w + 1) * w_width + w_column] << 2) & 0x4);
  1515. } else if (z_mod == 21) {
  1516. src = (w[z_w * w_width + w_column] >> 31) |
  1517. ((w[(z_w + 1) * w_width + w_column] << 1) & 0x6);
  1518. } else {
  1519. src = w[z_w * w_width + w_column];
  1520. src >>= z_bit;
  1521. src &= 0x07;
  1522. }
  1523. z_w = 0;
  1524. if (i != 10) {
  1525. if (i != 21) {
  1526. z_bit = i;
  1527. if (z_bit > 21) {
  1528. z_bit *= 3;
  1529. z_bit -= 64;
  1530. z_w += 2;
  1531. } else if (z_bit > 10) {
  1532. z_bit *= 3;
  1533. z_bit -= 32;
  1534. z_w += 1;
  1535. } else {
  1536. z_bit *= 3;
  1537. }
  1538. } else {
  1539. z_w += 1;
  1540. }
  1541. }
  1542. if (i == 10) {
  1543. dst[z_w] |= (src & 0x03) << 30;
  1544. dst[z_w + 1] |= ((src & 0x4) >> 2);
  1545. } else if (i == 21) {
  1546. dst[z_w] |= (src & 0x01) << 31;
  1547. dst[z_w + 1] |= ((src & 0x6) >> 1);
  1548. } else {
  1549. dst[z_w] |= (src << z_bit);
  1550. }
  1551. }
  1552. w_new[w_new_row * w_width + w_column] = dst[0];
  1553. w_new[(w_new_row + 1) * w_width + w_column] = dst[1];
  1554. w_new[(w_new_row + 2) * w_width + w_column] = dst[2];
  1555. }
  1556. __global__ void make_sequential_8bit_kernel(const uint32_t* __restrict__ w,
  1557. uint32_t* __restrict__ w_new,
  1558. const int* __restrict__ q_perm,
  1559. const int w_height,
  1560. const int w_width) {
  1561. if (blockIdx.z > 0) {
  1562. w = w + blockIdx.z * w_height * w_width;
  1563. w_new = w_new + blockIdx.z * w_height * w_width;
  1564. q_perm = q_perm + blockIdx.z * w_height * 4;
  1565. }
  1566. const uint64_t* w2 = (uint64_t*)w;
  1567. uint64_t* w_new2 = (uint64_t*)w_new;
  1568. int w2_stride = w_width >> 1;
  1569. int w2_column = THREADS_X * blockIdx.x + threadIdx.x;
  1570. if (w2_column >= w2_stride) return;
  1571. int w_new2_row = blockIdx.y;
  1572. int q_perm_idx = w_new2_row << 2;
  1573. uint64_t dst = 0;
  1574. #pragma unroll
  1575. for (int i = 0; i < 4; i++) {
  1576. int source_row = q_perm[q_perm_idx++];
  1577. int w2_row = source_row >> 2;
  1578. int w2_subrow = source_row & 0x03;
  1579. int w2_row_shift = w2_subrow << 3;
  1580. int wnew2_row_shift = i << 3;
  1581. uint64_t src = w2[w2_row * w2_stride + w2_column];
  1582. src >>= w2_row_shift;
  1583. src &= 0x000000ff000000ff;
  1584. src <<= wnew2_row_shift;
  1585. dst |= src;
  1586. }
  1587. w_new2[w_new2_row * w2_stride + w2_column] = dst;
  1588. }
  1589. void shuffle_exllama_weight(uint32_t* q_weight, int* q_perm, int height,
  1590. int width, int num_experts, int bit) {
  1591. if (q_perm) {
  1592. uint32_t* new_qweight = NULL;
  1593. cudaMalloc(&new_qweight,
  1594. num_experts * height / 32 * bit * width * sizeof(uint32_t));
  1595. dim3 blockDim, gridDim;
  1596. blockDim.x = THREADS_X;
  1597. blockDim.y = 1;
  1598. gridDim.x = DIVIDE(width, THREADS_X);
  1599. gridDim.y = height / 32 * bit;
  1600. gridDim.z = num_experts;
  1601. auto kernel = make_sequential_4bit_kernel;
  1602. if (bit == 2) {
  1603. kernel = make_sequential_2bit_kernel;
  1604. } else if (bit == 3) {
  1605. kernel = make_sequential_3bit_kernel;
  1606. gridDim.y = height / 32;
  1607. } else if (bit == 8) {
  1608. kernel = make_sequential_8bit_kernel;
  1609. }
  1610. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1611. kernel<<<gridDim, blockDim, 0, stream>>>(q_weight, new_qweight, q_perm,
  1612. height / 32 * bit, width);
  1613. // Replace qweights
  1614. cudaMemcpyAsync(q_weight, new_qweight,
  1615. num_experts * height / 32 * bit * width * sizeof(uint32_t),
  1616. cudaMemcpyDeviceToDevice);
  1617. // Cleanup
  1618. cudaDeviceSynchronize();
  1619. cudaFree(new_qweight);
  1620. }
  1621. dim3 blockDim, gridDim;
  1622. blockDim.x = THREADS_X;
  1623. blockDim.y = 1;
  1624. gridDim.x = DIVIDE(width, THREADS_X);
  1625. gridDim.y = 1;
  1626. auto shuffle_kernel = shuffle_4bit_kernel;
  1627. if (bit == 2) {
  1628. shuffle_kernel = shuffle_2bit_kernel;
  1629. } else if (bit == 3) {
  1630. shuffle_kernel = shuffle_3bit_kernel;
  1631. } else if (bit == 8) {
  1632. shuffle_kernel = shuffle_8bit_kernel;
  1633. }
  1634. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1635. shuffle_kernel<<<gridDim, blockDim, 0, stream>>>(q_weight,
  1636. height * num_experts, width);
  1637. }
  1638. template <int m_count>
  1639. __global__ void group_gemm_half_q_half_gptq_kernel(
  1640. const half* __restrict__ a, const uint32_t* __restrict__ b_q_weight,
  1641. const uint32_t* __restrict__ b_gptq_qzeros,
  1642. const half* __restrict__ b_gptq_scales, half* __restrict__ c,
  1643. const int size_m, const int size_n, const int size_k, const int groups,
  1644. const int* __restrict__ b_q_perm, const float* __restrict__ topk_weights,
  1645. const int* __restrict__ sorted_token_ids_ptr,
  1646. const int* __restrict__ expert_ids_ptr,
  1647. const int* __restrict__ num_tokens_post_padded, const int num_valid_tokens,
  1648. const int top_k) {
  1649. int num_tokens = *num_tokens_post_padded;
  1650. int offset_m = blockIdx.y * m_count;
  1651. if (offset_m >= num_tokens) return;
  1652. int expert_id = expert_ids_ptr[blockIdx.y];
  1653. b_q_weight = b_q_weight + size_k * size_n / 8 * expert_id;
  1654. b_gptq_qzeros = b_gptq_qzeros + groups * size_n / 8 * expert_id;
  1655. b_gptq_scales = b_gptq_scales + groups * size_n * expert_id;
  1656. if (b_q_perm) b_q_perm = b_q_perm + size_k * expert_id;
  1657. MatrixView_half a_(a, size_m, size_k);
  1658. MatrixView_half_rw c_(c, size_m, size_n);
  1659. MatrixView_q4_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
  1660. MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
  1661. int t = threadIdx.x;
  1662. // Block
  1663. int offset_n = blockIdx.x * BLOCK_KN_SIZE * 4;
  1664. int offset_k = blockIdx.z * BLOCK_KN_SIZE;
  1665. int end_n = min(offset_n + BLOCK_KN_SIZE * 4, size_n);
  1666. int end_m = min(offset_m + m_count, size_m);
  1667. int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
  1668. int n = offset_n + t * 4;
  1669. // Preload block_a
  1670. __shared__ half block_a[m_count][BLOCK_KN_SIZE];
  1671. int token_a[m_count];
  1672. int valid_count = m_count;
  1673. for (int m = 0; m < m_count; ++m) {
  1674. int token_id = sorted_token_ids_ptr[offset_m + m];
  1675. if (token_id >= num_valid_tokens) {
  1676. valid_count = m;
  1677. break;
  1678. }
  1679. token_a[m] = token_id;
  1680. }
  1681. if (offset_k + t < end_k) {
  1682. for (int m = 0; m < valid_count; ++m) {
  1683. const half* a_ptr = a_.item_ptr(token_a[m] / top_k, 0);
  1684. half* block_a_ptr = block_a[m];
  1685. half a0;
  1686. if (b_q_perm)
  1687. a0 = a_ptr[b_q_perm[offset_k + t]];
  1688. else
  1689. a0 = a_ptr[offset_k + t];
  1690. block_a_ptr[t] = a0;
  1691. }
  1692. }
  1693. // Zero output
  1694. if (n >= size_n) return;
  1695. __syncthreads();
  1696. // Find initial group
  1697. int groupsize = size_k / groups;
  1698. int group = offset_k / groupsize;
  1699. int nextgroup = offset_k + groupsize;
  1700. // a, b offset
  1701. int qk = offset_k / (32 / 4);
  1702. const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
  1703. const half* a_ptr = &block_a[0][0];
  1704. int a_stride = BLOCK_KN_SIZE;
  1705. // Initial group
  1706. int zeros[4];
  1707. float scales[4];
  1708. half2 z1z16[4][2];
  1709. half2 y1y16[4][2];
  1710. b_gptq_qzeros_.item4(zeros, group, n);
  1711. b_gptq_scales_.item4_f(scales, group, n);
  1712. dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
  1713. dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
  1714. dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
  1715. dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
  1716. // Column result
  1717. float block_c[m_count][4] = {};
  1718. // Dequantize and multiply
  1719. int k = offset_k;
  1720. while (k < end_k) {
  1721. if (k == nextgroup) {
  1722. group++;
  1723. nextgroup += groupsize;
  1724. b_gptq_qzeros_.item4(zeros, group, n);
  1725. b_gptq_scales_.item4_f(scales, group, n);
  1726. dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
  1727. dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
  1728. dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
  1729. dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
  1730. }
  1731. #pragma unroll
  1732. for (int j = 0; j < 4; j++) {
  1733. const int4* b_ptr4 = (int4*)b_ptr;
  1734. int4 load_int4 = *b_ptr4;
  1735. half2 dq[4][4];
  1736. dequant_4bit_8_gptq(load_int4.x, dq[0], z1z16[0], y1y16[0], size_n,
  1737. false);
  1738. dequant_4bit_8_gptq(load_int4.y, dq[1], z1z16[1], y1y16[1], size_n,
  1739. false);
  1740. dequant_4bit_8_gptq(load_int4.z, dq[2], z1z16[2], y1y16[2], size_n,
  1741. false);
  1742. dequant_4bit_8_gptq(load_int4.w, dq[3], z1z16[3], y1y16[3], size_n,
  1743. false);
  1744. for (int m = 0; m < valid_count; m++) {
  1745. block_c[m][0] = fma(dot22_8_f(dq[0], a_ptr + m * a_stride), scales[0],
  1746. block_c[m][0]);
  1747. block_c[m][1] = fma(dot22_8_f(dq[1], a_ptr + m * a_stride), scales[1],
  1748. block_c[m][1]);
  1749. block_c[m][2] = fma(dot22_8_f(dq[2], a_ptr + m * a_stride), scales[2],
  1750. block_c[m][2]);
  1751. block_c[m][3] = fma(dot22_8_f(dq[3], a_ptr + m * a_stride), scales[3],
  1752. block_c[m][3]);
  1753. }
  1754. b_ptr += size_n;
  1755. a_ptr += 8;
  1756. }
  1757. k += 32;
  1758. }
  1759. for (int m = 0; m < valid_count; m++) {
  1760. if (topk_weights) {
  1761. #pragma unroll
  1762. for (int j = 0; j < 4; ++j) {
  1763. block_c[m][j] = block_c[m][j] * topk_weights[token_a[m]];
  1764. }
  1765. }
  1766. half2* out = (half2*)c_.item_ptr(token_a[m], n);
  1767. half2 result01 = __halves2half2(__float2half_rn(block_c[m][0]),
  1768. __float2half_rn(block_c[m][1]));
  1769. half2 result23 = __halves2half2(__float2half_rn(block_c[m][2]),
  1770. __float2half_rn(block_c[m][3]));
  1771. atomicAdd(out, result01);
  1772. atomicAdd(out + 1, result23);
  1773. }
  1774. }
  1775. void group_gemm_half_q_half(const half* a, const uint32_t* b_q_weight,
  1776. const uint32_t* b_gptq_qzeros,
  1777. const half* b_gptq_scales, const int* b_q_perm,
  1778. half* c, const float* __restrict__ topk_weights,
  1779. const int* __restrict__ sorted_token_ids_ptr,
  1780. const int* __restrict__ expert_ids_ptr,
  1781. const int* __restrict__ num_tokens_post_padded,
  1782. const int num_valid_tokens, const int top_k,
  1783. int size_m, int size_n, int size_k, int pad_size_m,
  1784. int groups) {
  1785. dim3 blockDim, gridDim;
  1786. blockDim.x = BLOCK_KN_SIZE;
  1787. blockDim.y = 1;
  1788. blockDim.z = 1;
  1789. gridDim.x = DIVIDE(size_n, BLOCK_KN_SIZE * 4);
  1790. gridDim.y = DIVIDE(pad_size_m, BLOCK_M_SIZE_MAX);
  1791. gridDim.z = DIVIDE(size_k, BLOCK_KN_SIZE);
  1792. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1793. group_gemm_half_q_half_gptq_kernel<BLOCK_M_SIZE_MAX>
  1794. <<<gridDim, blockDim, 0, stream>>>(
  1795. a, b_q_weight, b_gptq_qzeros, b_gptq_scales, c, size_m, size_n,
  1796. size_k, groups, b_q_perm, topk_weights, sorted_token_ids_ptr,
  1797. expert_ids_ptr, num_tokens_post_padded, num_valid_tokens, top_k);
  1798. }
  1799. __global__ void group_gemm_half_q_half_alt_kernel(
  1800. const half2* __restrict__ vec, const uint32_t* __restrict__ mat,
  1801. half* __restrict__ mul, const half* __restrict__ scales,
  1802. const uint32_t* __restrict__ zeros, const int* __restrict__ g_idx,
  1803. int batch, int height, int width, int groups,
  1804. const float* __restrict__ topk_weights,
  1805. const int* __restrict__ sorted_token_ids_ptr,
  1806. const int* __restrict__ expert_ids_ptr,
  1807. const int* __restrict__ num_tokens_post_padded, const int num_valid_tokens,
  1808. const int top_k) {
  1809. int num_tokens = *num_tokens_post_padded;
  1810. int b = blockIdx.y * BLOCK_M_SIZE_MAX;
  1811. if (b >= num_tokens) return;
  1812. int expert_id = expert_ids_ptr[blockIdx.y];
  1813. mat = mat + height * width * expert_id;
  1814. scales = scales + groups * width * expert_id;
  1815. zeros = zeros + groups * width / 8 * expert_id;
  1816. g_idx = g_idx + height * 8 * expert_id;
  1817. int zero_width = width / 8;
  1818. int vec_height = height * 4;
  1819. const int blockwidth2 = BLOCK_KN_SIZE / 2;
  1820. int b_end = BLOCK_M_SIZE_MAX;
  1821. int h = BLOCK_KN_SIZE * blockIdx.z / 8;
  1822. int h_end = min(BLOCK_KN_SIZE / 8, height - h) * 4;
  1823. int w = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
  1824. int token_a[BLOCK_M_SIZE_MAX];
  1825. for (int m = 0; m < b_end; ++m) {
  1826. int token_id = sorted_token_ids_ptr[b + m];
  1827. if (token_id >= num_valid_tokens) {
  1828. b_end = m;
  1829. break;
  1830. }
  1831. token_a[m] = token_id;
  1832. }
  1833. __shared__ half2 blockvec[BLOCK_M_SIZE_MAX][blockwidth2];
  1834. if (threadIdx.x < h_end) {
  1835. for (int m = 0; m < b_end; ++m) {
  1836. blockvec[m][threadIdx.x] =
  1837. vec[token_a[m] / top_k * vec_height + blockIdx.z * BLOCK_KN_SIZE / 2 +
  1838. threadIdx.x];
  1839. }
  1840. }
  1841. __shared__ half2 deq2[256][8];
  1842. int val = threadIdx.x / 8;
  1843. int off = threadIdx.x % 8;
  1844. for (; val < 256; val += BLOCK_KN_SIZE / 8) {
  1845. deq2[val][off] =
  1846. __halves2half2(__int2half_rn(val & 0xF), __int2half_rn(val >> 4));
  1847. }
  1848. __syncthreads();
  1849. int i = width * h + w;
  1850. int g_h = h * 8;
  1851. int k = 0;
  1852. int z_w = w / 8;
  1853. int z_mod = (w % 8) * 4;
  1854. half2 res2;
  1855. half res[BLOCK_M_SIZE_MAX] = {};
  1856. unsigned int tmp;
  1857. while (k < h_end) {
  1858. tmp = mat[i];
  1859. half2 scales_tmp[4];
  1860. half2 zeros_tmp[4];
  1861. for (int tmp_k = 0; tmp_k < 4; tmp_k++) {
  1862. int g = g_idx[g_h + (k + tmp_k) * 2];
  1863. int g2 = g_idx[g_h + (k + tmp_k) * 2 + 1];
  1864. half scale_f = scales[g * width + w];
  1865. half scale_f2 = scales[g2 * width + w];
  1866. half2 scale = __halves2half2(scale_f, scale_f2);
  1867. half2 zero = __halves2half2(
  1868. __hmul(scale_f,
  1869. __int2half_rn(-((zeros[g * zero_width + z_w] >> z_mod) & 0xF) -
  1870. 1)),
  1871. __hmul(scale_f2,
  1872. __int2half_rn(
  1873. -((zeros[g2 * zero_width + z_w] >> z_mod) & 0xF) - 1)));
  1874. scales_tmp[tmp_k] = scale;
  1875. zeros_tmp[tmp_k] = zero;
  1876. }
  1877. for (int m = 0; m < b_end; m++) {
  1878. #ifndef USE_ROCM
  1879. res2 = {};
  1880. #else
  1881. res2.x = __half_as_ushort(__float2half(0));
  1882. res2.y = __half_as_ushort(__float2half(0));
  1883. #endif
  1884. res2 = __hfma2(
  1885. __hfma2(deq2[(tmp >> 0) & 0xff][off], scales_tmp[0], zeros_tmp[0]),
  1886. blockvec[m][k + 0], res2);
  1887. res2 = __hfma2(
  1888. __hfma2(deq2[(tmp >> 8) & 0xff][off], scales_tmp[1], zeros_tmp[1]),
  1889. blockvec[m][k + 1], res2);
  1890. res2 = __hfma2(
  1891. __hfma2(deq2[(tmp >> 16) & 0xff][off], scales_tmp[2], zeros_tmp[2]),
  1892. blockvec[m][k + 2], res2);
  1893. res2 = __hfma2(
  1894. __hfma2(deq2[(tmp >> 24) & 0xff][off], scales_tmp[3], zeros_tmp[3]),
  1895. blockvec[m][k + 3], res2);
  1896. #ifndef USE_ROCM
  1897. res[m] = __hadd(res[m], __hadd(res2.x, res2.y));
  1898. #else
  1899. res[m] = __hadd(
  1900. res[m], __hadd(__ushort_as_half(res2.x), __ushort_as_half(res2.y)));
  1901. #endif
  1902. }
  1903. i += width;
  1904. k += 4;
  1905. }
  1906. for (int m = 0; m < b_end; m++) {
  1907. if (topk_weights) {
  1908. res[m] = __float2half(__half2float(res[m]) * topk_weights[token_a[m]]);
  1909. }
  1910. atomicAdd(&mul[token_a[m] * width + w], res[m]);
  1911. }
  1912. }
  1913. void group_gemm_half_q_half_alt(const half* a, const uint32_t* b_q_weight,
  1914. const uint32_t* b_gptq_qzeros,
  1915. const half* b_gptq_scales, const int* b_g_idx,
  1916. half* c, const float* __restrict__ topk_weights,
  1917. const int* __restrict__ sorted_token_ids_ptr,
  1918. const int* __restrict__ expert_ids_ptr,
  1919. const int* __restrict__ num_tokens_post_padded,
  1920. const int num_valid_tokens, const int top_k,
  1921. int size_m, int size_n, int size_k,
  1922. int pad_size_m, int groups) {
  1923. dim3 blockDim, gridDim;
  1924. blockDim.x = BLOCK_KN_SIZE;
  1925. blockDim.y = 1;
  1926. blockDim.z = 1;
  1927. gridDim.x = DIVIDE(size_n, BLOCK_KN_SIZE);
  1928. gridDim.y = DIVIDE(pad_size_m, BLOCK_M_SIZE_MAX);
  1929. gridDim.z = DIVIDE(size_k, BLOCK_KN_SIZE);
  1930. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  1931. group_gemm_half_q_half_alt_kernel<<<gridDim, blockDim, 0, stream>>>(
  1932. (const half2*)a, b_q_weight, c, b_gptq_scales, b_gptq_qzeros, b_g_idx,
  1933. size_m, size_k / 8, size_n, groups, topk_weights, sorted_token_ids_ptr,
  1934. expert_ids_ptr, num_tokens_post_padded, num_valid_tokens, top_k);
  1935. }
  1936. // Only support 4-bit so far
  1937. void group_gemm_half_q_half_cuda(const half* a, const uint32_t* b_q_weight,
  1938. const uint32_t* b_gptq_qzeros,
  1939. const half* b_gptq_scales, const int* b_g_idx,
  1940. half* c,
  1941. const float* __restrict__ topk_weights,
  1942. const int* __restrict__ sorted_token_ids_ptr,
  1943. const int* __restrict__ expert_ids_ptr,
  1944. const int* __restrict__ num_tokens_post_padded,
  1945. const int num_valid_tokens, const int top_k,
  1946. int size_m, int size_n, int size_k,
  1947. int pad_size_m, int groups, bool use_exllama) {
  1948. if (use_exllama) {
  1949. group_gemm_half_q_half(
  1950. a, b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx, c, topk_weights,
  1951. sorted_token_ids_ptr, expert_ids_ptr, num_tokens_post_padded,
  1952. num_valid_tokens, top_k, size_m, size_n, size_k, pad_size_m, groups);
  1953. } else {
  1954. group_gemm_half_q_half_alt(
  1955. a, b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx, c, topk_weights,
  1956. sorted_token_ids_ptr, expert_ids_ptr, num_tokens_post_padded,
  1957. num_valid_tokens, top_k, size_m, size_n, size_k, pad_size_m, groups);
  1958. }
  1959. }
  1960. } // namespace gptq
  1961. } // namespace aphrodite
  1962. torch::Tensor gptq_gemm(torch::Tensor a, torch::Tensor b_q_weight,
  1963. torch::Tensor b_gptq_qzeros,
  1964. torch::Tensor b_gptq_scales, torch::Tensor b_g_idx,
  1965. bool use_exllama, int64_t bit) {
  1966. const at::cuda::OptionalCUDAGuard device_guard(device_of(a));
  1967. auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device());
  1968. at::Tensor c = torch::empty({a.size(0), b_q_weight.size(1)}, options);
  1969. at::Tensor temp_dq = torch::empty(
  1970. {b_q_weight.size(0) * 32 / bit, b_q_weight.size(1)}, options);
  1971. aphrodite::gptq::gemm_half_q_half_cuda(
  1972. at::cuda::getCurrentCUDABlasHandle(), (const half*)a.data_ptr(),
  1973. (const uint32_t*)b_q_weight.data_ptr(),
  1974. (const uint32_t*)b_gptq_qzeros.data_ptr(),
  1975. (const half*)b_gptq_scales.data_ptr(),
  1976. b_g_idx.device().is_meta() ? NULL : (const int*)b_g_idx.data_ptr(),
  1977. (half*)c.data_ptr(), (half*)temp_dq.data_ptr(),
  1978. c.size(0), // m
  1979. c.size(1), // n
  1980. a.size(1), // k
  1981. b_gptq_qzeros.size(0), // group number
  1982. use_exllama, bit);
  1983. return c;
  1984. }
  1985. void gptq_shuffle(torch::Tensor q_weight, torch::Tensor q_perm, int64_t bit) {
  1986. const at::cuda::OptionalCUDAGuard device_guard(device_of(q_weight));
  1987. int num_experts = q_weight.dim() == 3 ? q_weight.size(0) : 1;
  1988. int size_k = q_weight.dim() == 3 ? q_weight.size(1) * 32 / bit
  1989. : q_weight.size(0) * 32 / bit;
  1990. int size_n = q_weight.dim() == 3 ? q_weight.size(2) : q_weight.size(1);
  1991. aphrodite::gptq::shuffle_exllama_weight(
  1992. (uint32_t*)q_weight.data_ptr(),
  1993. q_perm.device().is_meta() || q_perm.numel() == 0
  1994. ? NULL
  1995. : (int*)q_perm.data_ptr(),
  1996. size_k, size_n, num_experts, bit);
  1997. }
  1998. // Only support 4-bit
  1999. // todo: extend support to other bits
  2000. torch::Tensor group_gptq_gemm(torch::Tensor a, torch::Tensor b_q_weight,
  2001. torch::Tensor b_gptq_qzeros,
  2002. torch::Tensor b_gptq_scales,
  2003. torch::Tensor b_g_idx, torch::Tensor topk_weights,
  2004. torch::Tensor sorted_token_ids_ptr,
  2005. torch::Tensor expert_ids_ptr,
  2006. torch::Tensor num_tokens_post_padded,
  2007. bool mul_weights, bool use_exllama) {
  2008. const at::cuda::OptionalCUDAGuard device_guard(device_of(a));
  2009. auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device());
  2010. at::Tensor c = torch::zeros(
  2011. {a.size(0), topk_weights.size(1), b_q_weight.size(2)}, options);
  2012. aphrodite::gptq::group_gemm_half_q_half_cuda(
  2013. (const half*)a.data_ptr(), (const uint32_t*)b_q_weight.data_ptr(),
  2014. (const uint32_t*)b_gptq_qzeros.data_ptr(),
  2015. (const half*)b_gptq_scales.data_ptr(),
  2016. b_g_idx.device().is_meta() ? NULL : (const int*)b_g_idx.data_ptr(),
  2017. (half*)c.data_ptr(),
  2018. mul_weights ? (const float*)topk_weights.data_ptr() : NULL,
  2019. (const int*)sorted_token_ids_ptr.data_ptr(),
  2020. (const int*)expert_ids_ptr.data_ptr(),
  2021. (const int*)num_tokens_post_padded.data_ptr(),
  2022. topk_weights.numel(), // num tokens
  2023. topk_weights.size(1) / a.size(1), // top_k
  2024. a.size(0) * a.size(1), // m
  2025. c.size(2), // n
  2026. a.size(2), // k
  2027. sorted_token_ids_ptr.size(0),
  2028. b_gptq_qzeros.size(1), // group number
  2029. use_exllama);
  2030. return c;
  2031. }
  2032. torch::Tensor dequant_gptq(torch::Tensor b_q_weight,
  2033. torch::Tensor b_gptq_qzeros,
  2034. torch::Tensor b_gptq_scales, torch::Tensor b_g_idx,
  2035. int bits, bool use_exllama) {
  2036. const at::cuda::OptionalCUDAGuard device_guard(device_of(b_gptq_scales));
  2037. auto options = torch::TensorOptions()
  2038. .dtype(b_gptq_scales.dtype())
  2039. .device(b_gptq_scales.device());
  2040. at::Tensor temp_dq;
  2041. int num_experts;
  2042. int size_k;
  2043. int size_n;
  2044. int groups;
  2045. // moe
  2046. if (b_q_weight.dim() == 3) {
  2047. temp_dq = torch::empty({b_q_weight.size(0), b_q_weight.size(1) * 32 / bits,
  2048. b_q_weight.size(2)},
  2049. options);
  2050. num_experts = b_q_weight.size(0);
  2051. size_k = b_q_weight.size(1) * 32 / bits;
  2052. size_n = b_q_weight.size(2);
  2053. groups = b_gptq_scales.size(1);
  2054. } else {
  2055. temp_dq = torch::empty({b_q_weight.size(0) * 32 / bits, b_q_weight.size(1)},
  2056. options);
  2057. num_experts = 1;
  2058. size_k = b_q_weight.size(0) * 32 / bits;
  2059. size_n = b_q_weight.size(1);
  2060. groups = b_gptq_scales.size(0);
  2061. }
  2062. aphrodite::gptq::dequant_gptq_cuda(
  2063. (const uint32_t*)b_q_weight.data_ptr(),
  2064. (const uint32_t*)b_gptq_qzeros.data_ptr(),
  2065. (const half*)b_gptq_scales.data_ptr(),
  2066. b_g_idx.device().is_meta() ? NULL : (const int*)b_g_idx.data_ptr(),
  2067. (half*)temp_dq.data_ptr(), size_k, size_n, groups, num_experts, bits,
  2068. use_exllama);
  2069. return temp_dq;
  2070. }