pybind.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "cache.h"
  2. #include "cuda_utils.h"
  3. #include "ops.h"
  4. #include <torch/extension.h>
  5. PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  6. // Aphrodite custom ops
  7. pybind11::module ops = m.def_submodule("ops", "Aphrodite custom operators");
  8. // Attention ops
  9. ops.def(
  10. "paged_attention_v1",
  11. &paged_attention_v1,
  12. "Compute the attention between an input query and the cached keys/values using PagedAttention.");
  13. ops.def(
  14. "paged_attention_v2",
  15. &paged_attention_v2,
  16. "PagedAttention V2.");
  17. // Activation ops
  18. ops.def(
  19. "silu_and_mul",
  20. &silu_and_mul,
  21. "Activation function used in SwiGLU.");
  22. ops.def(
  23. "gelu_and_mul",
  24. &gelu_and_mul,
  25. "Activation function used in GeGLU.");
  26. ops.def(
  27. "gelu_new",
  28. &gelu_new,
  29. "GELU implementation used in GPT-2.");
  30. ops.def(
  31. "gelu_fast",
  32. &gelu_fast,
  33. "Approximate GELU implementation.");
  34. // Layernorm
  35. ops.def(
  36. "rms_norm",
  37. &rms_norm,
  38. "Apply Root Mean Square (RMS) Normalization to the input tensor.");
  39. ops.def(
  40. "fused_add_rms_norm",
  41. &fused_add_rms_norm,
  42. "In-place fused Add and RMS Normalization");
  43. // Rotary embedding
  44. ops.def(
  45. "rotary_embedding",
  46. &rotary_embedding,
  47. "Apply GPT-NeoX or GPT-J style rotary embedding to query and key");
  48. #ifndef USE_ROCM
  49. // Quantization ops
  50. ops.def("aqlm_gemm", &aqlm_gemm, "Quantized GEMM for AQLM");
  51. ops.def("awq_gemm", &awq_gemm, "Quantized GEMM for AWQ");
  52. ops.def("awq_dequantize", &awq_dequantize, "Dequantization for AWQ");
  53. ops.def("autoquant_convert_s4_k_m8", &autoquant_convert_s4_k_m8, "convert kernel.");
  54. ops.def("autoquant_s4_f16_gemm", &autoquant_s4_f16_gemm, "weight int4 activation float16 gemm kernel.");
  55. ops.def("quip_decompress", &decompress_e8p_origorder, "decompress_packed_e8p");
  56. ops.def("quip_gemv", &e8p_mm_origorder, "e8p_mm_origorder");
  57. ops.def("marlin_gemm", &marlin_gemm, "Marlin Optimized Quantized GEMM for GPTQ");
  58. #endif
  59. ops.def("gptq_gemm", &gptq_gemm, "Quantized GEMM for GPTQ");
  60. ops.def("gptq_shuffle", &gptq_shuffle, "Post processing for GPTQ");
  61. ops.def("squeezellm_gemm", &squeezellm_gemm, "Quantized GEMM for SqueezeLLM");
  62. ops.def("ggml_dequantize", &ggml_dequantize, "ggml_dequantize");
  63. ops.def("ggml_mul_mat_vec", &ggml_mul_mat_vec, "ggml_mul_mat_vec");
  64. ops.def("ggml_mul_mat_vec_a8", &ggml_mul_mat_vec_a8, "ggml_mul_mat_vec_a8");
  65. ops.def("ggml_mul_mat_a8", &ggml_mul_mat_a8, "ggml_mul_mat_a8");
  66. ops.def("exl2_make_q_matrix",&make_q_matrix, "preprocess for exl2");
  67. ops.def("exl2_gemm", &exl2_gemm, "exl2 gemm");
  68. ops.def("moe_align_block_size",
  69. &moe_align_block_size,
  70. "Aligning the number of tokens to be processed by each expert such that it is divisible by the block size.");
  71. // Cache ops
  72. pybind11::module cache_ops = m.def_submodule("cache_ops", "Aphrodite cache ops");
  73. cache_ops.def(
  74. "swap_blocks",
  75. &swap_blocks,
  76. "Swap in (out) the cache blocks from src to dst");
  77. cache_ops.def(
  78. "copy_blocks",
  79. &copy_blocks,
  80. "Copy the cache blocks from src to dst");
  81. cache_ops.def(
  82. "reshape_and_cache",
  83. &reshape_and_cache,
  84. "Reshape the key and value tensors and cache them");
  85. cache_ops.def(
  86. "gather_cached_kv",
  87. &gather_cached_kv,
  88. "Gather key and value from the cache into contiguous QKV tensors");
  89. cache_ops.def(
  90. "convert_fp8_e5m2",
  91. &convert_fp8_e5m2,
  92. "Convert the key and value cache to fp8_e5m2 data type");
  93. // Cuda utils
  94. pybind11::module cuda_utils = m.def_submodule("cuda_utils", "Aphrodite cuda utils");
  95. cuda_utils.def(
  96. "get_device_attribute",
  97. &get_device_attribute,
  98. "Gets the specified device attribute.");
  99. cuda_utils.def(
  100. "get_max_shared_memory_per_block_device_attribute",
  101. &get_max_shared_memory_per_block_device_attribute,
  102. "Gets the maximum shared memory per block device attribute.");
  103. #ifndef USE_ROCM
  104. // Custom all-reduce kernels
  105. pybind11::module custom_ar = m.def_submodule("custom_ar", "custom allreduce");
  106. custom_ar.def("init_custom_ar", &init_custom_ar, "init_custom_ar");
  107. custom_ar.def("should_custom_ar", &should_custom_ar, "should_custom_ar");
  108. custom_ar.def("all_reduce_reg", &all_reduce_reg, "all_reduce_reg");
  109. custom_ar.def("all_reduce_unreg", &all_reduce_unreg, "all_reduce_unreg");
  110. custom_ar.def("dispose", &dispose, "dispose");
  111. custom_ar.def("meta_size", &meta_size, "meta_size");
  112. custom_ar.def("register_buffer", &register_buffer, "register_buffer");
  113. custom_ar.def("get_graph_buffer_ipc_meta", &get_graph_buffer_ipc_meta,
  114. "get_graph_buffer_ipc_meta");
  115. custom_ar.def("register_graph_buffers", &register_graph_buffers,
  116. "register_graph_buffers");
  117. #endif
  118. }