q_gemm_exl2.cu 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Adapted from https://github.com/turboderp/exllamav2
  3. * Copyright (c) 2024 turboderp
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include <torch/extension.h>
  24. #include <c10/cuda/CUDAGuard.h>
  25. #include <ATen/cuda/CUDAContext.h>
  26. #include <cuda_runtime.h>
  27. #include "q_matrix.cuh"
  28. #include "matrix_view.cuh"
  29. #include "quant/qdq_2.cuh"
  30. #include "quant/qdq_3.cuh"
  31. #include "quant/qdq_4.cuh"
  32. #include "quant/qdq_5.cuh"
  33. #include "quant/qdq_6.cuh"
  34. #include "quant/qdq_8.cuh"
  35. #include "q_gemm_kernel.cuh"
  36. namespace aphrodite {
  37. namespace exl2 {
  38. #define MAX_Q_GEMM_ROWS 32
  39. #define EXL2_BLOCK_KN_SIZE 64
  40. #define EXL2_BLOCK_M_SIZE_MAX 8
  41. #define EXL2_MAX_GROUPS_IN_BLOCK (EXL2_BLOCK_KN_SIZE / 32)
  42. #define DIVIDE(x, size) (((x) + (size) - 1) / (size))
  43. void gemm_half_q_half_cuda_part
  44. (
  45. const half* a,
  46. QMatrix* b,
  47. half* c,
  48. int size_m,
  49. int size_n,
  50. int size_k,
  51. int m_count,
  52. bool clear
  53. )
  54. {
  55. {
  56. dim3 blockDim, gridDim;
  57. blockDim.x = EXL2_BLOCK_KN_SIZE;
  58. blockDim.y = 1;
  59. blockDim.z = 1;
  60. gridDim.x = DIVIDE(size_n, EXL2_BLOCK_KN_SIZE * 4);
  61. gridDim.y = DIVIDE(size_m, m_count);
  62. gridDim.z = DIVIDE(b->height, EXL2_BLOCK_KN_SIZE);
  63. fp_gemm_half_q_half_kernel kernel = pick_gemm_half_q_half_kernel(m_count);
  64. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  65. kernel<<<gridDim, blockDim, 0, stream>>>
  66. (
  67. a,
  68. b->cuda_q_weight,
  69. b->cuda_q_scale,
  70. b->cuda_q_scale_max,
  71. c,
  72. size_m,
  73. size_n,
  74. size_k,
  75. b->height,
  76. b->groups,
  77. b->cuda_q_group_map,
  78. b->cuda_q_perm,
  79. b->rows_8,
  80. b->rows_6,
  81. b->rows_5,
  82. b->rows_4,
  83. b->rows_3,
  84. b->rows_2,
  85. clear
  86. );
  87. }
  88. }
  89. void gemm_half_q_half_cuda
  90. (
  91. cublasHandle_t cublas_handle,
  92. const half* a,
  93. QMatrix* b,
  94. half* c,
  95. int size_m,
  96. int size_n,
  97. int size_k,
  98. bool clear,
  99. half* temp_dq
  100. )
  101. {
  102. if (size_m > MAX_Q_GEMM_ROWS)
  103. {
  104. // Reconstruct FP16 matrix, then cuBLAS
  105. b->reconstruct(temp_dq);
  106. //cublasSetMathMode(cublas_handle, CUBLAS_TENSOR_OP_MATH);
  107. const half alpha = __float2half(1.0f);
  108. const half beta = clear ? __float2half(0.0f) : __float2half(1.0f);
  109. cublasHgemm(cublas_handle,
  110. CUBLAS_OP_N,
  111. CUBLAS_OP_N,
  112. size_n, size_m, size_k,
  113. &alpha, temp_dq, size_n,
  114. a, size_k,
  115. &beta, c, size_n);
  116. }
  117. else
  118. {
  119. // Quantized matmul
  120. int block_m_size_max = EXL2_BLOCK_M_SIZE_MAX;
  121. int max_chunks = size_m / block_m_size_max;
  122. int last_chunk = max_chunks * block_m_size_max;
  123. int last_chunk_size = size_m - last_chunk;
  124. if (max_chunks)
  125. {
  126. gemm_half_q_half_cuda_part(a, b, c, last_chunk, size_n, size_k, block_m_size_max, clear);
  127. }
  128. if (last_chunk_size)
  129. {
  130. gemm_half_q_half_cuda_part(a + last_chunk * size_k, b, c + last_chunk * size_n, last_chunk_size, size_n, size_k, last_chunk_size, clear);
  131. }
  132. }
  133. }
  134. } // namespace exl2
  135. } // namespace aphrodite
  136. torch::Tensor exl2_gemm
  137. (
  138. torch::Tensor a,
  139. uintptr_t b
  140. )
  141. {
  142. const at::cuda::OptionalCUDAGuard device_guard(device_of(a));
  143. aphrodite::exl2::QMatrix* qm = reinterpret_cast<aphrodite::exl2::QMatrix*> (b);
  144. auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device());
  145. at::Tensor c = torch::empty({a.size(0), qm->width}, options);
  146. at::Tensor temp_dq;
  147. if (c.size(0) > MAX_Q_GEMM_ROWS) {
  148. temp_dq = torch::zeros({a.size(1), qm->width}, options);
  149. }
  150. aphrodite::exl2::gemm_half_q_half_cuda
  151. (
  152. at::cuda::getCurrentCUDABlasHandle(),
  153. (const half*) a.data_ptr(),
  154. qm,
  155. (half*) c.data_ptr(),
  156. c.size(0), // m
  157. c.size(1), // n
  158. a.size(1), // k
  159. true,
  160. c.size(0) > MAX_Q_GEMM_ROWS? (half*)temp_dq.data_ptr() : NULL
  161. );
  162. return c;
  163. }
  164. uintptr_t make_q_matrix
  165. (
  166. torch::Tensor q_weight,
  167. torch::Tensor q_perm,
  168. torch::Tensor q_invperm,
  169. torch::Tensor q_scale,
  170. torch::Tensor q_scale_max,
  171. torch::Tensor q_groups,
  172. torch::Tensor q_group_map
  173. )
  174. {
  175. const at::cuda::OptionalCUDAGuard device_guard(device_of(q_weight));
  176. int device = q_weight.device().index();
  177. int width = q_weight.size(1);
  178. int groups = q_scale.size(0);
  179. int height = q_perm.size(0);
  180. aphrodite::exl2::QMatrix* m = new aphrodite::exl2::QMatrix
  181. (
  182. device,
  183. height,
  184. width,
  185. groups,
  186. (uint32_t*) q_weight.data_ptr(),
  187. (uint16_t*) q_perm.data_ptr(),
  188. (uint16_t*) q_invperm.data_ptr(),
  189. (uint32_t*) q_scale.data_ptr(),
  190. (half*) q_scale_max.data_ptr(),
  191. (uint16_t*) q_groups.data_ptr(),
  192. (uint16_t*) q_group_map.data_ptr()
  193. );
  194. return reinterpret_cast<uintptr_t>(m);
  195. }