pybind.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_new",
  24. &gelu_new,
  25. "GELU implementation used in GPT-2.");
  26. ops.def(
  27. "gelu_fast",
  28. &gelu_fast,
  29. "Approximate GELU implementation.");
  30. // Layernorm
  31. ops.def(
  32. "rms_norm",
  33. &rms_norm,
  34. "Apply Root Mean Square (RMS) Normalization to the input tensor.");
  35. ops.def(
  36. "fused_add_rms_norm",
  37. &fused_add_rms_norm,
  38. "In-place fused Add and RMS Normalization");
  39. // Rotary embedding
  40. ops.def(
  41. "rotary_embedding",
  42. &rotary_embedding,
  43. "Apply GPT-NeoX or GPT-J style rotary embedding to query and key");
  44. #ifndef USE_ROCM
  45. // Quantization ops
  46. ops.def("awq_gemm", &awq_gemm, "Quantized GEMM for AWQ");
  47. ops.def("awq_dequantize", &awq_dequantize, "Dequantization for AWQ");
  48. #endif
  49. ops.def("gptq_gemm", &gptq_gemm, "Quantized GEMM for GPTQ");
  50. ops.def("gptq_shuffle", &gptq_shuffle, "Post processing for GPTQ");
  51. ops.def("squeezellm_gemm", &squeezellm_gemm, "Quantized GEMM for SqueezeLLM");
  52. // Cache ops
  53. pybind11::module cache_ops = m.def_submodule("cache_ops", "Aphrodite cache ops");
  54. cache_ops.def(
  55. "swap_blocks",
  56. &swap_blocks,
  57. "Swap in (out) the cache blocks from src to dst");
  58. cache_ops.def(
  59. "copy_blocks",
  60. &copy_blocks,
  61. "Copy the cache blocks from src to dst");
  62. cache_ops.def(
  63. "reshape_and_cache",
  64. &reshape_and_cache,
  65. "Reshape the key and value tensors and cache them");
  66. cache_ops.def(
  67. "gather_cached_kv",
  68. &gather_cached_kv,
  69. "Gather key and value from the cache into contiguous QKV tensors");
  70. cache_ops.def(
  71. "convert_fp8_e5m2",
  72. &convert_fp8_e5m2,
  73. "Convert the key and value cache to fp8_e5m2 data type");
  74. // Cuda utils
  75. pybind11::module cuda_utils = m.def_submodule("cuda_utils", "Aphrodite cuda utils");
  76. cuda_utils.def(
  77. "get_device_attribute",
  78. &get_device_attribute,
  79. "Gets the specified device attribute.");
  80. cuda_utils.def(
  81. "get_max_shared_memory_per_block_device_attribute",
  82. &get_max_shared_memory_per_block_device_attribute,
  83. "Gets the maximum shared memory per block device attribute.");
  84. #ifndef USE_ROCM
  85. // Custom all-reduce kernels
  86. pybind11::module custom_ar = m.def_submodule("custom_ar", "custom allreduce");
  87. custom_ar.def("init_custom_ar", &init_custom_ar, "init_custom_ar");
  88. custom_ar.def("should_custom_ar", &should_custom_ar, "should_custom_ar");
  89. custom_ar.def("all_reduce_reg", &all_reduce_reg, "all_reduce_reg");
  90. custom_ar.def("all_reduce_unreg", &all_reduce_unreg, "all_reduce_unreg");
  91. custom_ar.def("dispose", &dispose, "dispose");
  92. custom_ar.def("meta_size", &meta_size, "meta_size");
  93. custom_ar.def("register_buffer", &register_buffer, "register_buffer");
  94. custom_ar.def("get_graph_buffer_ipc_meta", &get_graph_buffer_ipc_meta,
  95. "get_graph_buffer_ipc_meta");
  96. custom_ar.def("register_graph_buffers", &register_graph_buffers,
  97. "register_graph_buffers");
  98. #endif
  99. }