quant_ops.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include <torch/library.h>
  3. #include "core/scalar_type.hpp"
  4. #ifndef USE_ROCM
  5. // AQLM
  6. torch::Tensor aqlm_gemm(const torch::Tensor& input, const torch::Tensor& codes,
  7. const torch::Tensor& codebooks,
  8. const torch::Tensor& scales,
  9. const torch::Tensor& codebook_partition_sizes,
  10. const std::optional<torch::Tensor>& bias);
  11. torch::Tensor aqlm_dequant(const torch::Tensor& codes,
  12. const torch::Tensor& codebooks,
  13. const torch::Tensor& codebook_partition_sizes);
  14. // AWQ
  15. torch::Tensor awq_gemm(torch::Tensor _in_feats, torch::Tensor _kernel,
  16. torch::Tensor _scaling_factors, torch::Tensor _zeros,
  17. int64_t split_k_iters);
  18. torch::Tensor awq_dequantize(torch::Tensor _kernel,
  19. torch::Tensor _scaling_factors,
  20. torch::Tensor _zeros, int64_t split_k_iters,
  21. int64_t thx, int64_t thy);
  22. torch::Tensor awq_group_gemm(torch::Tensor _in_feats, torch::Tensor _kernel,
  23. torch::Tensor _scaling_factors,
  24. torch::Tensor _zeros, torch::Tensor _topk_weights,
  25. torch::Tensor _sorted_token_ids_ptr,
  26. torch::Tensor _expert_ids_ptr,
  27. torch::Tensor _num_tokens_post_padded,
  28. bool mul_weights, int64_t split_k_iters);
  29. #endif
  30. // GPTQ
  31. torch::Tensor gptq_gemm(torch::Tensor a, torch::Tensor b_q_weight,
  32. torch::Tensor b_gptq_qzeros,
  33. torch::Tensor b_gptq_scales, torch::Tensor b_g_idx,
  34. bool use_exllama, int64_t bit);
  35. void gptq_shuffle(torch::Tensor q_weight, torch::Tensor q_perm, int64_t bit);
  36. torch::Tensor group_gptq_gemm(torch::Tensor a, torch::Tensor b_q_weight,
  37. torch::Tensor b_gptq_qzeros,
  38. torch::Tensor b_gptq_scales,
  39. torch::Tensor b_g_idx, torch::Tensor topk_weights,
  40. torch::Tensor sorted_token_ids_ptr,
  41. torch::Tensor expert_ids_ptr,
  42. torch::Tensor num_tokens_post_padded,
  43. bool mul_weights, bool use_exllama);
  44. torch::Tensor dequant_gptq(torch::Tensor b_q_weight,
  45. torch::Tensor b_gptq_qzeros,
  46. torch::Tensor b_gptq_scales, torch::Tensor b_g_idx,
  47. int64_t bits, bool use_exllama);
  48. #ifndef USE_ROCM
  49. // Marlin
  50. torch::Tensor marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight,
  51. torch::Tensor& b_scales, torch::Tensor& workspace,
  52. int64_t size_m, int64_t size_n, int64_t size_k);
  53. torch::Tensor gptq_marlin_24_gemm(torch::Tensor& a, torch::Tensor& b_q_weight,
  54. torch::Tensor& b_meta,
  55. torch::Tensor& b_scales,
  56. torch::Tensor& workspace,
  57. aphrodite::ScalarTypeTorchPtr const& b_q_type,
  58. int64_t size_m, int64_t size_n,
  59. int64_t size_k);
  60. torch::Tensor gptq_marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight,
  61. torch::Tensor& b_scales, torch::Tensor& b_zeros,
  62. torch::Tensor& g_idx, torch::Tensor& perm,
  63. torch::Tensor& workspace,
  64. aphrodite::ScalarTypeTorchPtr const& b_q_type,
  65. int64_t size_m, int64_t size_n, int64_t size_k,
  66. bool is_k_full, bool has_zp,
  67. bool use_fp32_reduce);
  68. torch::Tensor gptq_marlin_repack(torch::Tensor& b_q_weight, torch::Tensor& perm,
  69. int64_t size_k, int64_t size_n,
  70. int64_t num_bits);
  71. torch::Tensor awq_marlin_repack(torch::Tensor& b_q_weight, int64_t size_k,
  72. int64_t size_n, int64_t num_bits);
  73. torch::Tensor fp8_marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight,
  74. torch::Tensor& b_scales, torch::Tensor& workspace,
  75. int64_t num_bits, int64_t size_m, int64_t size_n,
  76. int64_t size_k);
  77. // GGUF
  78. torch::Tensor ggml_dequantize(torch::Tensor W, int8_t type, int64_t m,
  79. int64_t n);
  80. torch::Tensor ggml_mul_mat_vec_a8(torch::Tensor W, torch::Tensor X, int8_t type,
  81. int64_t row);
  82. torch::Tensor ggml_mul_mat_a8(torch::Tensor W, torch::Tensor X, int8_t type,
  83. int64_t row);
  84. // QuIP#
  85. at::Tensor e8p_mm_origorder(const at::Tensor& A, const at::Tensor& B,
  86. const at::Tensor& CB);
  87. void decompress_e8p_origorder(torch::Tensor YIs, torch::Tensor CB,
  88. torch::Tensor& Y);
  89. bool cutlass_scaled_mm_supports_fp8(int64_t cuda_device_capability);
  90. void cutlass_scaled_mm(torch::Tensor& out, torch::Tensor const& a,
  91. torch::Tensor const& b, torch::Tensor const& a_scales,
  92. torch::Tensor const& b_scales,
  93. c10::optional<torch::Tensor> const& bias);
  94. void cutlass_scaled_mm_azp(torch::Tensor& out, torch::Tensor const& a,
  95. torch::Tensor const& b,
  96. torch::Tensor const& a_scales,
  97. torch::Tensor const& b_scales,
  98. torch::Tensor const& azp_adj,
  99. c10::optional<torch::Tensor> const& azp,
  100. c10::optional<torch::Tensor> const& bias);
  101. torch::Tensor marlin_qqq_gemm(torch::Tensor const& a,
  102. torch::Tensor const& b_q_weight,
  103. torch::Tensor const& s_tok,
  104. torch::Tensor const& s_ch,
  105. torch::Tensor const& s_group,
  106. torch::Tensor& workspace, int64_t size_m,
  107. int64_t size_n, int64_t size_k);
  108. #endif
  109. void static_scaled_int8_quant(torch::Tensor& out, torch::Tensor const& input,
  110. torch::Tensor const& scale);
  111. void dynamic_scaled_int8_quant(torch::Tensor& out, torch::Tensor const& input,
  112. torch::Tensor& scales);
  113. // SqueezeLLM
  114. void squeezellm_gemm(torch::Tensor vec, torch::Tensor mat, torch::Tensor mul,
  115. torch::Tensor lookup_table);
  116. // FP8
  117. void static_scaled_fp8_quant(torch::Tensor& out, torch::Tensor const& input,
  118. torch::Tensor const& scale);
  119. void dynamic_scaled_fp8_quant(torch::Tensor& out, torch::Tensor const& input,
  120. torch::Tensor& scale);
  121. void dynamic_per_token_scaled_fp8_quant(
  122. torch::Tensor& out, torch::Tensor const& input, torch::Tensor& scale,
  123. c10::optional<torch::Tensor> const& scale_ub);