q_gemm_exl2.cu 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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
  13. * all 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/all.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. #if defined(USE_ROCM)
  43. __host__ __forceinline__ hipblasStatus_t __compat_hipblasHgemm(
  44. hipblasHandle_t handle, hipblasOperation_t transA,
  45. hipblasOperation_t transB, int m, int n, int k, const half* alpha,
  46. const half* AP, int lda, const half* BP, int ldb, const half* beta,
  47. half* CP, int ldc) {
  48. return hipblasHgemm(handle, transA, transB, m, n, k,
  49. reinterpret_cast<const hipblasHalf*>(alpha),
  50. reinterpret_cast<const hipblasHalf*>(AP), lda,
  51. reinterpret_cast<const hipblasHalf*>(BP), ldb,
  52. reinterpret_cast<const hipblasHalf*>(beta),
  53. reinterpret_cast<hipblasHalf*>(CP), ldc);
  54. }
  55. #define hipblasHgemm __compat_hipblasHgemm
  56. #endif
  57. #define DIVIDE(x, size) (((x) + (size) - 1) / (size))
  58. void gemm_half_q_half_cuda_part(const half* a, QMatrix* b, half* c, int size_m,
  59. int size_n, int size_k, int m_count,
  60. bool clear) {
  61. {
  62. dim3 blockDim, gridDim;
  63. blockDim.x = EXL2_BLOCK_KN_SIZE;
  64. blockDim.y = 1;
  65. blockDim.z = 1;
  66. gridDim.x = DIVIDE(size_n, EXL2_BLOCK_KN_SIZE * 4);
  67. gridDim.y = DIVIDE(size_m, m_count);
  68. gridDim.z = DIVIDE(b->height, EXL2_BLOCK_KN_SIZE);
  69. fp_gemm_half_q_half_kernel kernel = pick_gemm_half_q_half_kernel(m_count);
  70. const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
  71. kernel<<<gridDim, blockDim, 0, stream>>>(
  72. a, b->cuda_q_weight, b->cuda_q_scale, b->cuda_q_scale_max, c, size_m,
  73. size_n, size_k, b->height, b->groups, b->cuda_q_group_map,
  74. b->cuda_q_perm, b->rows_8, b->rows_6, b->rows_5, b->rows_4, b->rows_3,
  75. b->rows_2, clear);
  76. }
  77. }
  78. void gemm_half_q_half_cuda(cublasHandle_t cublas_handle, const half* a,
  79. QMatrix* b, half* c, int size_m, int size_n,
  80. int size_k, bool clear, half* temp_dq) {
  81. if (size_m > MAX_Q_GEMM_ROWS) {
  82. // Reconstruct FP16 matrix, then cuBLAS
  83. b->reconstruct(temp_dq);
  84. // cublasSetMathMode(cublas_handle, CUBLAS_TENSOR_OP_MATH);
  85. const half alpha = __float2half(1.0f);
  86. const half beta = clear ? __float2half(0.0f) : __float2half(1.0f);
  87. cublasHgemm(cublas_handle, CUBLAS_OP_N, CUBLAS_OP_N, size_n, size_m, size_k,
  88. &alpha, temp_dq, size_n, a, size_k, &beta, c, size_n);
  89. } else {
  90. // Quantized matmul
  91. int block_m_size_max = EXL2_BLOCK_M_SIZE_MAX;
  92. int max_chunks = size_m / block_m_size_max;
  93. int last_chunk = max_chunks * block_m_size_max;
  94. int last_chunk_size = size_m - last_chunk;
  95. if (max_chunks) {
  96. gemm_half_q_half_cuda_part(a, b, c, last_chunk, size_n, size_k,
  97. block_m_size_max, clear);
  98. }
  99. if (last_chunk_size) {
  100. gemm_half_q_half_cuda_part(a + last_chunk * size_k, b,
  101. c + last_chunk * size_n, last_chunk_size,
  102. size_n, size_k, last_chunk_size, clear);
  103. }
  104. }
  105. }
  106. } // namespace exl2
  107. } // namespace aphrodite
  108. torch::Tensor exl2_gemm(torch::Tensor a, uintptr_t b) {
  109. const at::cuda::OptionalCUDAGuard device_guard(device_of(a));
  110. aphrodite::exl2::QMatrix* qm = reinterpret_cast<aphrodite::exl2::QMatrix*>(b);
  111. auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device());
  112. at::Tensor c = torch::empty({a.size(0), qm->width}, options);
  113. at::Tensor temp_dq;
  114. if (c.size(0) > MAX_Q_GEMM_ROWS) {
  115. temp_dq = torch::zeros({a.size(1), qm->width}, options);
  116. }
  117. aphrodite::exl2::gemm_half_q_half_cuda(
  118. at::cuda::getCurrentCUDABlasHandle(), (const half*)a.data_ptr(), qm,
  119. (half*)c.data_ptr(),
  120. c.size(0), // m
  121. c.size(1), // n
  122. a.size(1), // k
  123. true, c.size(0) > MAX_Q_GEMM_ROWS ? (half*)temp_dq.data_ptr() : NULL);
  124. return c;
  125. }
  126. uintptr_t make_q_matrix(torch::Tensor q_weight, torch::Tensor q_perm,
  127. torch::Tensor q_invperm, torch::Tensor q_scale,
  128. torch::Tensor q_scale_max, torch::Tensor q_groups,
  129. torch::Tensor q_group_map) {
  130. const at::cuda::OptionalCUDAGuard device_guard(device_of(q_weight));
  131. int device = q_weight.device().index();
  132. int width = q_weight.size(1);
  133. int groups = q_scale.size(0);
  134. int height = q_perm.size(0);
  135. aphrodite::exl2::QMatrix* m = new aphrodite::exl2::QMatrix(
  136. device, height, width, groups, (uint32_t*)q_weight.data_ptr(),
  137. (uint16_t*)q_perm.data_ptr(), (uint16_t*)q_invperm.data_ptr(),
  138. (uint32_t*)q_scale.data_ptr(), (half*)q_scale_max.data_ptr(),
  139. (uint16_t*)q_groups.data_ptr(), (uint16_t*)q_group_map.data_ptr());
  140. return reinterpret_cast<uintptr_t>(m);
  141. }