1
0

q_gemm_exl2.cu 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(size_k, 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->groups,
  76. b->cuda_q_group_map,
  77. b->cuda_q_perm,
  78. b->rows_8,
  79. b->rows_6,
  80. b->rows_5,
  81. b->rows_4,
  82. b->rows_3,
  83. b->rows_2,
  84. clear
  85. );
  86. }
  87. }
  88. void gemm_half_q_half_cuda
  89. (
  90. cublasHandle_t cublas_handle,
  91. const half* a,
  92. QMatrix* b,
  93. half* c,
  94. int size_m,
  95. int size_n,
  96. int size_k,
  97. bool clear,
  98. half* temp_dq
  99. )
  100. {
  101. if (size_m > MAX_Q_GEMM_ROWS)
  102. {
  103. // Reconstruct FP16 matrix, then cuBLAS
  104. b->reconstruct(temp_dq);
  105. //cublasSetMathMode(cublas_handle, CUBLAS_TENSOR_OP_MATH);
  106. const half alpha = __float2half(1.0f);
  107. const half beta = clear ? __float2half(0.0f) : __float2half(1.0f);
  108. cublasHgemm(cublas_handle,
  109. CUBLAS_OP_N,
  110. CUBLAS_OP_N,
  111. size_n, size_m, size_k,
  112. &alpha, temp_dq, size_n,
  113. a, size_k,
  114. &beta, c, size_n);
  115. }
  116. else
  117. {
  118. // Quantized matmul
  119. int block_m_size_max = EXL2_BLOCK_M_SIZE_MAX;
  120. int max_chunks = size_m / block_m_size_max;
  121. int last_chunk = max_chunks * block_m_size_max;
  122. int last_chunk_size = size_m - last_chunk;
  123. if (max_chunks)
  124. {
  125. gemm_half_q_half_cuda_part(a, b, c, last_chunk, size_n, size_k, block_m_size_max, clear);
  126. }
  127. if (last_chunk_size)
  128. {
  129. 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);
  130. }
  131. }
  132. }
  133. } // namespace exl2
  134. } // namespace aphrodite
  135. torch::Tensor exl2_gemm
  136. (
  137. torch::Tensor a,
  138. uintptr_t b
  139. )
  140. {
  141. const at::cuda::OptionalCUDAGuard device_guard(device_of(a));
  142. aphrodite::exl2::QMatrix* qm = reinterpret_cast<aphrodite::exl2::QMatrix*> (b);
  143. auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device());
  144. at::Tensor c = torch::empty({a.size(0), qm->width}, options);
  145. at::Tensor temp_dq = torch::empty({a.size(1), qm->width}, options);
  146. aphrodite::exl2::gemm_half_q_half_cuda
  147. (
  148. at::cuda::getCurrentCUDABlasHandle(),
  149. (const half*) a.data_ptr(),
  150. qm,
  151. (half*) c.data_ptr(),
  152. c.size(0), // m
  153. c.size(1), // n
  154. a.size(1), // k
  155. true,
  156. (half*) temp_dq.data_ptr()
  157. );
  158. return c;
  159. }
  160. uintptr_t make_q_matrix
  161. (
  162. torch::Tensor q_weight,
  163. torch::Tensor q_perm,
  164. torch::Tensor q_invperm,
  165. torch::Tensor q_scale,
  166. torch::Tensor q_scale_max,
  167. torch::Tensor q_groups,
  168. torch::Tensor q_group_map
  169. )
  170. {
  171. const at::cuda::OptionalCUDAGuard device_guard(device_of(q_weight));
  172. int device = q_weight.device().index();
  173. int width = q_weight.size(1);
  174. int groups = q_scale.size(0);
  175. int height = q_invperm.size(0);
  176. aphrodite::exl2::QMatrix* m = new aphrodite::exl2::QMatrix
  177. (
  178. device,
  179. height,
  180. width,
  181. groups,
  182. (uint32_t*) q_weight.data_ptr(),
  183. (uint16_t*) q_perm.data_ptr(),
  184. (uint16_t*) q_invperm.data_ptr(),
  185. (uint32_t*) q_scale.data_ptr(),
  186. (half*) q_scale_max.data_ptr(),
  187. (uint16_t*) q_groups.data_ptr(),
  188. (uint16_t*) q_group_map.data_ptr()
  189. );
  190. return reinterpret_cast<uintptr_t>(m);
  191. }