2023-11-23 16:31:19 -08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "cuda_utils.h"
|
|
|
|
#include "ops.h"
|
|
|
|
#include <torch/extension.h>
|
|
|
|
|
|
|
|
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
|
|
|
// vLLM custom ops
|
|
|
|
pybind11::module ops = m.def_submodule("ops", "vLLM custom operators");
|
|
|
|
|
|
|
|
// Attention ops
|
|
|
|
ops.def(
|
|
|
|
"paged_attention_v1",
|
|
|
|
&paged_attention_v1,
|
|
|
|
"Compute the attention between an input query and the cached keys/values using PagedAttention.");
|
|
|
|
ops.def(
|
|
|
|
"paged_attention_v2",
|
|
|
|
&paged_attention_v2,
|
|
|
|
"PagedAttention V2.");
|
|
|
|
|
|
|
|
// Activation ops
|
|
|
|
ops.def(
|
|
|
|
"silu_and_mul",
|
|
|
|
&silu_and_mul,
|
|
|
|
"Activation function used in SwiGLU.");
|
|
|
|
ops.def(
|
2024-02-21 20:17:52 -08:00
|
|
|
"gelu_and_mul",
|
|
|
|
&gelu_and_mul,
|
2024-03-12 22:06:17 -07:00
|
|
|
"Activation function used in GeGLU with `none` approximation.");
|
|
|
|
ops.def(
|
|
|
|
"gelu_tanh_and_mul",
|
|
|
|
&gelu_tanh_and_mul,
|
|
|
|
"Activation function used in GeGLU with `tanh` approximation.");
|
2024-02-21 20:17:52 -08:00
|
|
|
ops.def(
|
2023-11-23 16:31:19 -08:00
|
|
|
"gelu_new",
|
|
|
|
&gelu_new,
|
|
|
|
"GELU implementation used in GPT-2.");
|
|
|
|
ops.def(
|
|
|
|
"gelu_fast",
|
|
|
|
&gelu_fast,
|
|
|
|
"Approximate GELU implementation.");
|
|
|
|
|
|
|
|
// Layernorm
|
|
|
|
ops.def(
|
|
|
|
"rms_norm",
|
|
|
|
&rms_norm,
|
|
|
|
"Apply Root Mean Square (RMS) Normalization to the input tensor.");
|
|
|
|
|
|
|
|
ops.def(
|
|
|
|
"fused_add_rms_norm",
|
|
|
|
&fused_add_rms_norm,
|
|
|
|
"In-place fused Add and RMS Normalization");
|
|
|
|
|
|
|
|
// Rotary embedding
|
|
|
|
ops.def(
|
|
|
|
"rotary_embedding",
|
|
|
|
&rotary_embedding,
|
|
|
|
"Apply GPT-NeoX or GPT-J style rotary embedding to query and key");
|
|
|
|
|
2024-03-13 13:45:26 -07:00
|
|
|
ops.def(
|
|
|
|
"batched_rotary_embedding",
|
|
|
|
&batched_rotary_embedding,
|
|
|
|
"Apply GPT-NeoX or GPT-J style rotary embedding to query and key (supports multiple loras)");
|
|
|
|
|
2024-03-01 14:47:51 -06:00
|
|
|
// Quantization ops
|
2024-02-05 17:38:02 -08:00
|
|
|
#ifndef USE_ROCM
|
2024-04-23 13:59:33 -04:00
|
|
|
ops.def("aqlm_gemm", &aqlm_gemm, "Quantized GEMM for AQLM");
|
|
|
|
ops.def("aqlm_dequant", &aqlm_dequant, "Decompression method for AQLM");
|
2023-11-23 16:31:19 -08:00
|
|
|
ops.def("awq_gemm", &awq_gemm, "Quantized GEMM for AWQ");
|
2024-05-16 12:56:15 -04:00
|
|
|
ops.def("marlin_gemm", &marlin_gemm, "Marlin (Dense) Optimized Quantized GEMM for GPTQ");
|
|
|
|
ops.def("gptq_marlin_24_gemm", &gptq_marlin_24_gemm, "Marlin_24 (Sparse) Optimized Quantized GEMM for GPTQ");
|
2024-04-29 12:35:34 -04:00
|
|
|
ops.def("gptq_marlin_gemm", &gptq_marlin_gemm, "gptq_marlin Optimized Quantized GEMM for GPTQ");
|
|
|
|
ops.def("gptq_marlin_repack", &gptq_marlin_repack, "gptq_marlin repack from GPTQ");
|
2024-01-27 08:53:17 +01:00
|
|
|
ops.def("awq_dequantize", &awq_dequantize, "Dequantization for AWQ");
|
2023-12-08 15:16:52 +08:00
|
|
|
#endif
|
2024-03-01 14:47:51 -06:00
|
|
|
|
2023-12-15 19:04:22 +08:00
|
|
|
ops.def("gptq_gemm", &gptq_gemm, "Quantized GEMM for GPTQ");
|
|
|
|
ops.def("gptq_shuffle", &gptq_shuffle, "Post processing for GPTQ");
|
2023-11-23 16:31:19 -08:00
|
|
|
ops.def("squeezellm_gemm", &squeezellm_gemm, "Quantized GEMM for SqueezeLLM");
|
2024-04-26 21:49:59 -07:00
|
|
|
ops.def("static_scaled_fp8_quant", &static_scaled_fp8_quant, "Compute FP8 quantized tensor for given scaling factor");
|
|
|
|
ops.def("dynamic_scaled_fp8_quant", &dynamic_scaled_fp8_quant, "Compute FP8 quantized tensor and scaling factor");
|
2024-01-30 13:19:48 +08:00
|
|
|
ops.def(
|
2024-01-29 22:43:37 -08:00
|
|
|
"moe_align_block_size",
|
|
|
|
&moe_align_block_size,
|
|
|
|
"Aligning the number of tokens to be processed by each expert such that it is divisible by the block size.");
|
2023-11-23 16:31:19 -08:00
|
|
|
|
|
|
|
// Cache ops
|
|
|
|
pybind11::module cache_ops = m.def_submodule("cache_ops", "vLLM cache ops");
|
|
|
|
cache_ops.def(
|
|
|
|
"swap_blocks",
|
|
|
|
&swap_blocks,
|
|
|
|
"Swap in (out) the cache blocks from src to dst");
|
|
|
|
cache_ops.def(
|
|
|
|
"copy_blocks",
|
|
|
|
©_blocks,
|
|
|
|
"Copy the cache blocks from src to dst");
|
|
|
|
cache_ops.def(
|
|
|
|
"reshape_and_cache",
|
|
|
|
&reshape_and_cache,
|
|
|
|
"Reshape the key and value tensors and cache them");
|
2024-05-03 15:51:27 -07:00
|
|
|
cache_ops.def(
|
|
|
|
"reshape_and_cache_flash",
|
|
|
|
&reshape_and_cache_flash,
|
|
|
|
"Reshape the key and value tensors and cache them");
|
2024-01-29 08:43:54 +08:00
|
|
|
cache_ops.def(
|
2024-04-03 16:15:55 -05:00
|
|
|
"convert_fp8",
|
|
|
|
&convert_fp8,
|
|
|
|
"Convert the key and value cache to fp8 data type");
|
2023-11-23 16:31:19 -08:00
|
|
|
|
|
|
|
// Cuda utils
|
|
|
|
pybind11::module cuda_utils = m.def_submodule("cuda_utils", "vLLM cuda utils");
|
|
|
|
cuda_utils.def(
|
|
|
|
"get_device_attribute",
|
|
|
|
&get_device_attribute,
|
|
|
|
"Gets the specified device attribute.");
|
2024-01-26 15:41:10 -05:00
|
|
|
|
|
|
|
cuda_utils.def(
|
|
|
|
"get_max_shared_memory_per_block_device_attribute",
|
|
|
|
&get_max_shared_memory_per_block_device_attribute,
|
|
|
|
"Gets the maximum shared memory per block device attribute.");
|
|
|
|
|
2024-01-28 04:46:35 +08:00
|
|
|
#ifndef USE_ROCM
|
|
|
|
// Custom all-reduce kernels
|
|
|
|
pybind11::module custom_ar = m.def_submodule("custom_ar", "custom allreduce");
|
|
|
|
custom_ar.def("init_custom_ar", &init_custom_ar, "init_custom_ar");
|
|
|
|
custom_ar.def("should_custom_ar", &should_custom_ar, "should_custom_ar");
|
|
|
|
custom_ar.def("all_reduce_reg", &all_reduce_reg, "all_reduce_reg");
|
|
|
|
custom_ar.def("all_reduce_unreg", &all_reduce_unreg, "all_reduce_unreg");
|
|
|
|
custom_ar.def("dispose", &dispose, "dispose");
|
|
|
|
custom_ar.def("meta_size", &meta_size, "meta_size");
|
|
|
|
custom_ar.def("register_buffer", ®ister_buffer, "register_buffer");
|
|
|
|
custom_ar.def("get_graph_buffer_ipc_meta", &get_graph_buffer_ipc_meta,
|
|
|
|
"get_graph_buffer_ipc_meta");
|
|
|
|
custom_ar.def("register_graph_buffers", ®ister_graph_buffers,
|
|
|
|
"register_graph_buffers");
|
|
|
|
#endif
|
|
|
|
|
2023-11-23 16:31:19 -08:00
|
|
|
}
|