添加自定义 aclnn 算子#

本文档描述如何在 vllm-ascend 中添加自定义的 aclnn 算子。

自定义 aclnn 算子在 vllm-ascend 中是如何工作的?#

自定义 aclnn 算子在 vllm-ascend 的构建过程中被编译并安装到 vllm_ascend/cann_ops_custom 目录中。随后,这些 aclnn 算子会被绑定到 torch.ops._C_ascend 模块,从而允许用户在 vllm-ascend 的 Python 代码中调用它们。

要启用自定义算子,请使用以下代码:

from vllm_ascend.utils import enable_custom_op

enable_custom_op()

如何添加自定义 aclnn 算子?#

  • csrc 目录下创建一个新的算子文件夹

  • 分别创建 op_hostop_kernel 目录,用于存放主机端和内核端的源代码

  • csrc/build_aclnn.sh 中为支持的 SOC 添加构建选项。注意,多个算子之间应用 ; 分隔,例如 CUSTOM_OPS=op1;op2;op3

  • csrc/torch_binding.cpp 中将 aclnn 算子绑定到 torch.ops._C_ascend 模块

  • csrc/torch_binding_meta.cpp 中为算子编写一个元(meta)实现,以便它能被捕获到 aclgraph 中

成功构建 vllm-ascend 后,便可在 Python 代码中调用该自定义 aclnn 算子。