• 教程 >
  • 自定义 C++ 和 CUDA 运算符
快捷方式

自定义 C++ 和 CUDA 运算符

创建于:2024 年 6 月 18 日 | 最后更新:2025 年 1 月 28 日 | 最后验证:2024 年 11 月 05 日

作者: Richard Zou

您将学到什么
  • 如何将用 C++/CUDA 编写的自定义运算符与 PyTorch 集成

  • 如何使用 torch.library.opcheck 测试自定义运算符

先决条件
  • PyTorch 2.4 或更高版本

  • C++ 和 CUDA 编程的基础知识

注意

本教程也适用于 AMD ROCm,无需额外修改。

PyTorch 提供了大量的运算符库,这些运算符库适用于张量(例如 torch.add、torch.sum 等)。但是,您可能希望将新的自定义运算符引入 PyTorch。本教程演示了编写用 C++/CUDA 编写的自定义运算符的推荐方法。

在本教程中,我们将演示如何编写一个融合乘法-加法 C++ 和 CUDA 运算符,该运算符与 PyTorch 子系统组合使用。该操作的语义如下

def mymuladd(a: Tensor, b: Tensor, c: float):
    return a * b + c

您可以在 此处 找到本教程的端到端工作示例。

设置构建系统

如果您正在开发自定义 C++/CUDA 代码,则必须对其进行编译。请注意,如果您正在与已经绑定到预编译 C++/CUDA 代码的 Python 库进行交互,则可以考虑编写自定义 Python 运算符(自定义 Python 运算符)。

使用 torch.utils.cpp_extension 编译自定义 C++/CUDA 代码以与 PyTorch C++ 扩展一起使用,可以通过 setuptools “提前” 构建,也可以通过 load_inline “即时” 构建;我们将重点关注“提前”构建方式。

使用 cpp_extension 非常简单,只需编写以下 setup.py

from setuptools import setup, Extension
from torch.utils import cpp_extension

setup(name="extension_cpp",
      ext_modules=[
          cpp_extension.CppExtension(
            "extension_cpp",
            ["muladd.cpp"],
            # define Py_LIMITED_API with min version 3.9 to expose only the stable
            # limited API subset from Python.h
            extra_compile_args={"cxx": ["-DPy_LIMITED_API=0x03090000"]},
            py_limited_api=True)],  # Build 1 wheel across multiple Python versions
      cmdclass={'build_ext': cpp_extension.BuildExtension},
      options={"bdist_wheel": {"py_limited_api": "cp39"}}  # 3.9 is minimum supported Python version
)

如果您需要编译 CUDA 代码(例如,.cu 文件),则应使用 torch.utils.cpp_extension.CUDAExtension。有关如何设置此功能的示例,请参见 extension-cpp

上面的示例代表了我们所说的 CPython 不可知论 wheel,这意味着我们正在构建一个可以在多个 CPython 版本上运行的单个 wheel(类似于纯 Python 包)。CPython 不可知论在最大限度地减少自定义库需要支持和发布的 wheel 数量方面是理想的。我们希望支持的最低版本是 3.9,因为它是当前支持的最旧版本,因此我们在整个设置代码中使用相应的十六进制代码和说明符。我们建议在与您要支持的最低 CPython 版本相同的环境中构建扩展,以最大限度地减少未知行为,因此,在这里,我们在 CPython 3.9 环境中构建扩展。构建完成后,这个单一的 wheel 将可以在任何 CPython 3.9+ 环境中运行。为了实现这一点,需要注意三个关键行。

第一个是在 extra_compile_args 中将 Py_LIMITED_API 指定为您要支持的最低 CPython 版本

extra_compile_args={"cxx": ["-DPy_LIMITED_API=0x03090000"]},

定义 Py_LIMITED_API 标志有助于验证扩展实际上仅使用 CPython 稳定有限 API,这是构建 CPython 不可知论 wheel 的要求。如果未满足此要求,则可能会构建一个看起来是 CPython 不可知论的 wheel,但在另一个 CPython 环境中会崩溃,或者更糟糕的是,会静默地不正确。请注意避免使用不稳定的 CPython API,例如来自 libtorch_python 的 API(特别是 pytorch/python 绑定),并且仅使用来自 libtorch 的 API(ATen 对象、运算符和分派器)。我们强烈建议定义 Py_LIMITED_API 标志,以帮助确定扩展是否符合规范并作为 CPython 不可知论 wheel 是安全的。请注意,定义此标志并不能完全保证构建的 wheel 是 CPython 不可知论的,但它比狂野的西部要好。 Python 文档 中提到了几个注意事项,您应该自己测试和验证 wheel 对于相关的 CPython 版本是否真正不可知。

第二个和第三个指定 py_limited_api 的行通知 setuptools 您打算构建 CPython 不可知论 wheel,并将相应地影响 wheel 的命名

setup(name="extension_cpp",
      ext_modules=[
          cpp_extension.CppExtension(
            ...,
            py_limited_api=True)],  # Build 1 wheel across multiple Python versions
      ...,
      options={"bdist_wheel": {"py_limited_api": "cp39"}}  # 3.9 is minimum supported Python version
)

必须将 py_limited_api=True 指定为 CppExtension/CUDAExtension 的参数,以及作为 "bdist_wheel" 命令的选项,并指定最小支持的 CPython 版本(在本例中为 3.9)。因此,我们教程中的 setup 将构建一个正确命名的 wheel,该 wheel 可以跨多个 CPython 版本 >=3.9 安装。

如果您的扩展使用稳定有限集之外的 CPython API,则您无法构建 CPython 不可知论 wheel!您应该为每个 CPython 版本构建一个 wheel,如下所示

from setuptools import setup, Extension
from torch.utils import cpp_extension

setup(name="extension_cpp",
      ext_modules=[
          cpp_extension.CppExtension(
            "extension_cpp",
            ["muladd.cpp"])],
      cmdclass={'build_ext': cpp_extension.BuildExtension},
)

定义自定义运算符并添加后端实现

首先,让我们编写一个 C++ 函数来计算 mymuladd

at::Tensor mymuladd_cpu(at::Tensor a, const at::Tensor& b, double c) {
  TORCH_CHECK(a.sizes() == b.sizes());
  TORCH_CHECK(a.dtype() == at::kFloat);
  TORCH_CHECK(b.dtype() == at::kFloat);
  TORCH_INTERNAL_ASSERT(a.device().type() == at::DeviceType::CPU);
  TORCH_INTERNAL_ASSERT(b.device().type() == at::DeviceType::CPU);
  at::Tensor a_contig = a.contiguous();
  at::Tensor b_contig = b.contiguous();
  at::Tensor result = torch::empty(a_contig.sizes(), a_contig.options());
  const float* a_ptr = a_contig.data_ptr<float>();
  const float* b_ptr = b_contig.data_ptr<float>();
  float* result_ptr = result.data_ptr<float>();
  for (int64_t i = 0; i < result.numel(); i++) {
    result_ptr[i] = a_ptr[i] * b_ptr[i] + c;
  }
  return result;
}

为了从 PyTorch 的 Python 前端使用它,我们需要使用 TORCH_LIBRARY API 将其注册为 PyTorch 运算符。这将自动将运算符绑定到 Python。

运算符注册是一个两步过程

  • 定义运算符 - 此步骤确保 PyTorch 知道新的运算符。

  • 注册后端实现 - 在此步骤中,各种后端(例如 CPU 和 CUDA)的实现与运算符相关联。

定义运算符

要定义运算符,请按照以下步骤操作

  1. 为运算符选择一个命名空间。我们建议命名空间是您的顶级项目的名称;在本教程中,我们将使用“extension_cpp”。

  2. 提供一个模式字符串,该字符串指定运算符的输入/输出类型,以及是否会改变输入张量。除了张量和浮点数之外,我们还支持更多类型;有关更多详细信息,请参见 自定义运算符手册

    • 如果您要编写一个可以改变其输入张量的运算符,请参见此处(创建可变运算符)了解如何指定。

TORCH_LIBRARY(extension_cpp, m) {
   // Note that "float" in the schema corresponds to the C++ double type
   // and the Python float type.
   m.def("mymuladd(Tensor a, Tensor b, float c) -> Tensor");
 }

这使得可以通过 Python 中的 torch.ops.extension_cpp.mymuladd 访问该运算符。

为运算符注册后端实现

使用 TORCH_LIBRARY_IMPL 为运算符注册后端实现。

TORCH_LIBRARY_IMPL(extension_cpp, CPU, m) {
  m.impl("mymuladd", &mymuladd_cpu);
}

如果您还有 myaddmul 的 CUDA 实现,则可以在单独的 TORCH_LIBRARY_IMPL 代码块中注册它

__global__ void muladd_kernel(int numel, const float* a, const float* b, float c, float* result) {
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < numel) result[idx] = a[idx] * b[idx] + c;
}

at::Tensor mymuladd_cuda(const at::Tensor& a, const at::Tensor& b, double c) {
  TORCH_CHECK(a.sizes() == b.sizes());
  TORCH_CHECK(a.dtype() == at::kFloat);
  TORCH_CHECK(b.dtype() == at::kFloat);
  TORCH_INTERNAL_ASSERT(a.device().type() == at::DeviceType::CUDA);
  TORCH_INTERNAL_ASSERT(b.device().type() == at::DeviceType::CUDA);
  at::Tensor a_contig = a.contiguous();
  at::Tensor b_contig = b.contiguous();
  at::Tensor result = torch::empty(a_contig.sizes(), a_contig.options());
  const float* a_ptr = a_contig.data_ptr<float>();
  const float* b_ptr = b_contig.data_ptr<float>();
  float* result_ptr = result.data_ptr<float>();

  int numel = a_contig.numel();
  muladd_kernel<<<(numel+255)/256, 256>>>(numel, a_ptr, b_ptr, c, result_ptr);
  return result;
}

TORCH_LIBRARY_IMPL(extension_cpp, CUDA, m) {
  m.impl("mymuladd", &mymuladd_cuda);
}

为运算符添加 torch.compile 支持

要为运算符添加 torch.compile 支持,我们必须添加 FakeTensor 内核(也称为“元内核”或“抽象实现”)。FakeTensor 是具有元数据(例如形状、dtype、设备)但没有数据的张量:运算符的 FakeTensor 内核指定如何根据输入张量的元数据计算输出张量的元数据。FakeTensor 内核应返回您选择的具有正确张量元数据(形状/步幅/dtype/设备)的虚拟张量。

我们建议通过 Python 中的 torch.library.register_fake API 完成此操作,尽管也可以从 C++ 完成此操作(有关更多详细信息,请参见 自定义运算符手册)。

# Important: the C++ custom operator definitions should be loaded first
# before calling ``torch.library`` APIs that add registrations for the
# C++ custom operator(s). The following import loads our
# C++ custom operator definitions.
# Note that if you are striving for Python agnosticism, you should use
# the ``load_library(...)`` API call instead. See the next section for
# more details.
from . import _C

@torch.library.register_fake("extension_cpp::mymuladd")
def _(a, b, c):
    torch._check(a.shape == b.shape)
    torch._check(a.dtype == torch.float)
    torch._check(b.dtype == torch.float)
    torch._check(a.device == b.device)
    return torch.empty_like(a)

设置混合 Python/C++ 注册

在本教程中,我们在 C++ 中定义了一个自定义运算符,在 C++ 中添加了 CPU/CUDA 实现,并在 Python 中添加了 FakeTensor 内核和反向公式。这些注册的加载(或导入)顺序很重要(以错误的顺序导入将导致错误)。

要将自定义运算符与混合 Python/C++ 注册一起使用,我们必须首先加载包含自定义运算符定义的 C++ 库,然后调用 torch.library 注册 API。可以通过三种方式实现此目的

  1. 加载包含自定义运算符定义的 C++ 库的第一种方法是为 _C 定义一个虚拟 Python 模块。然后,在 Python 中,当您使用 import _C 导入模块时,将加载与扩展对应的 .so 文件,并且将运行 TORCH_LIBRARYTORCH_LIBRARY_IMPL 静态初始化程序。可以使用 PYBIND11_MODULE 创建虚拟 Python 模块,如下所示,但您会注意到这无法使用 Py_LIMITED_API 编译,因为 pybind11 不保证仅使用稳定的有限 CPython API!使用以下代码,您很遗憾无法为您的扩展构建 CPython 不可知论 wheel!(预示:我想知道第二种方法是什么 ;) )。

// in, say, not_agnostic/csrc/extension_BAD.cpp
#include <pybind11/pybind11.h>

PYBIND11_MODULE("_C", m) {}
# in, say, extension/__init__.py
from . import _C
  1. 在本教程中,由于我们重视能够在多个 CPython 版本之间构建单个 wheel,因此我们将用稳定的 API 调用替换不稳定的 PYBIND11 调用。以下代码使用 -DPy_LIMITED_API=0x03090000 进行编译,并成功为我们的 _C 扩展创建了一个虚拟 Python 模块,以便可以从 Python 导入它。有关更多详细信息,请参见 extension_cpp/__init__.pyextension_cpp/csrc/muladd.cpp

#include <Python.h>

extern "C" {
  /* Creates a dummy empty _C module that can be imported from Python.
    The import from Python will load the .so consisting of this file
    in this extension, so that the TORCH_LIBRARY static initializers
    below are run. */
  PyObject* PyInit__C(void)
  {
      static struct PyModuleDef module_def = {
          PyModuleDef_HEAD_INIT,
          "_C",   /* name of module */
          NULL,   /* module documentation, may be NULL */
          -1,     /* size of per-interpreter state of the module,
                    or -1 if the module keeps state in global variables. */
          NULL,   /* methods */
      };
      return PyModule_Create(&module_def);
  }
}
# in, say, extension/__init__.py
from . import _C
  1. 如果您想完全避免在 C++ 自定义运算符中使用 Python.h,则可以在 Python 中使用 torch.ops.load_library("/path/to/library.so") 加载从扩展编译的 .so 文件。请注意,使用此方法,不会为扩展创建 _C Python 模块,因此您无法从 Python 调用 import _Ctorch.ops.load_library("/path/to/library.so") 将完成这项工作,而不是依赖 import 语句来触发自定义运算符的注册。然后,挑战转移到理解 .so 文件位于何处,以便您可以加载它们,这并非总是那么容易

import torch
from pathlib import Path

so_files = list(Path(__file__).parent.glob("_C*.so"))
assert (
    len(so_files) == 1
), f"Expected one _C*.so file, found {len(so_files)}"
torch.ops.load_library(so_files[0])

from . import ops

为运算符添加训练(autograd)支持

使用 torch.library.register_autograd 为运算符添加训练支持。最好使用此方法,而不是直接使用 Python torch.autograd.Function 或 C++ torch::autograd::Function;您必须以非常特定的方式使用它们才能避免静默的不正确性(有关更多详细信息,请参见 自定义运算符手册)。

def _backward(ctx, grad):
    a, b = ctx.saved_tensors
    grad_a, grad_b = None, None
    if ctx.needs_input_grad[0]:
        grad_a = grad * b
    if ctx.needs_input_grad[1]:
        grad_b = grad * a
    return grad_a, grad_b, None

def _setup_context(ctx, inputs, output):
    a, b, c = inputs
    saved_a, saved_b = None, None
    if ctx.needs_input_grad[0]:
        saved_b = b
    if ctx.needs_input_grad[1]:
        saved_a = a
    ctx.save_for_backward(saved_a, saved_b)

# This code adds training support for the operator. You must provide us
# the backward formula for the operator and a `setup_context` function
# to save values to be used in the backward.
torch.library.register_autograd(
    "extension_cpp::mymuladd", _backward, setup_context=_setup_context)

请注意,反向传播必须是 PyTorch 可理解的运算符的组合。如果您希望在反向传播中使用另一个自定义 C++ 或 CUDA 内核,则必须将其包装到自定义运算符中。

如果我们有自己的自定义 mymul 内核,我们将需要将其包装到自定义运算符中,然后从反向传播中调用它

// New! a mymul_cpu kernel
at::Tensor mymul_cpu(const at::Tensor& a, const at::Tensor& b) {
  TORCH_CHECK(a.sizes() == b.sizes());
  TORCH_CHECK(a.dtype() == at::kFloat);
  TORCH_CHECK(b.dtype() == at::kFloat);
  TORCH_CHECK(a.device().type() == at::DeviceType::CPU);
  TORCH_CHECK(b.device().type() == at::DeviceType::CPU);
  at::Tensor a_contig = a.contiguous();
  at::Tensor b_contig = b.contiguous();
  at::Tensor result = torch::empty(a_contig.sizes(), a_contig.options());
  const float* a_ptr = a_contig.data_ptr<float>();
  const float* b_ptr = b_contig.data_ptr<float>();
  float* result_ptr = result.data_ptr<float>();
  for (int64_t i = 0; i < result.numel(); i++) {
    result_ptr[i] = a_ptr[i] * b_ptr[i];
  }
  return result;
}

TORCH_LIBRARY(extension_cpp, m) {
  m.def("mymuladd(Tensor a, Tensor b, float c) -> Tensor");
  // New! defining the mymul operator
  m.def("mymul(Tensor a, Tensor b) -> Tensor");
}


TORCH_LIBRARY_IMPL(extension_cpp, CPU, m) {
  m.impl("mymuladd", &mymuladd_cpu);
  // New! registering the cpu kernel for the mymul operator
  m.impl("mymul", &mymul_cpu);
}
def _backward(ctx, grad):
    a, b = ctx.saved_tensors
    grad_a, grad_b = None, None
    if ctx.needs_input_grad[0]:
        grad_a = torch.ops.extension_cpp.mymul.default(grad, b)
    if ctx.needs_input_grad[1]:
        grad_b = torch.ops.extension_cpp.mymul.default(grad, a)
    return grad_a, grad_b, None


def _setup_context(ctx, inputs, output):
    a, b, c = inputs
    saved_a, saved_b = None, None
    if ctx.needs_input_grad[0]:
        saved_b = b
    if ctx.needs_input_grad[1]:
        saved_a = a
    ctx.save_for_backward(saved_a, saved_b)


# This code adds training support for the operator. You must provide us
# the backward formula for the operator and a `setup_context` function
# to save values to be used in the backward.
torch.library.register_autograd(
    "extension_cpp::mymuladd", _backward, setup_context=_setup_context)

测试运算符

使用 torch.library.opcheck 测试自定义运算符是否已正确注册。请注意,此函数不测试梯度在数学上是否正确 - 计划编写单独的测试来测试梯度,无论是手动测试还是使用 torch.autograd.gradcheck

def sample_inputs(device, *, requires_grad=False):
    def make_tensor(*size):
        return torch.randn(size, device=device, requires_grad=requires_grad)

    def make_nondiff_tensor(*size):
        return torch.randn(size, device=device, requires_grad=False)

    return [
        [make_tensor(3), make_tensor(3), 1],
        [make_tensor(20), make_tensor(20), 3.14],
        [make_tensor(20), make_nondiff_tensor(20), -123],
        [make_nondiff_tensor(2, 3), make_tensor(2, 3), -0.3],
    ]

def reference_muladd(a, b, c):
    return a * b + c

samples = sample_inputs(device, requires_grad=True)
samples.extend(sample_inputs(device, requires_grad=False))
for args in samples:
    # Correctness test
    result = torch.ops.extension_cpp.mymuladd(*args)
    expected = reference_muladd(*args)
    torch.testing.assert_close(result, expected)

    # Use opcheck to check for incorrect usage of operator registration APIs
    torch.library.opcheck(torch.ops.extension_cpp.mymuladd.default, args)

创建可变运算符

您可能希望编写一个会改变其输入的可变运算符。使用 Tensor(a!) 在模式中指定每个可变张量;否则,将出现未定义的行为。如果有多个可变张量,请为每个可变张量使用不同的名称(例如,Tensor(a!)Tensor(b!)Tensor(c!))。

让我们编写一个 myadd_out(a, b, out) 运算符,它将 a+b 的内容写入 out

// An example of an operator that mutates one of its inputs.
void myadd_out_cpu(const at::Tensor& a, const at::Tensor& b, at::Tensor& out) {
  TORCH_CHECK(a.sizes() == b.sizes());
  TORCH_CHECK(b.sizes() == out.sizes());
  TORCH_CHECK(a.dtype() == at::kFloat);
  TORCH_CHECK(b.dtype() == at::kFloat);
  TORCH_CHECK(out.dtype() == at::kFloat);
  TORCH_CHECK(out.is_contiguous());
  TORCH_INTERNAL_ASSERT(a.device().type() == at::DeviceType::CPU);
  TORCH_INTERNAL_ASSERT(b.device().type() == at::DeviceType::CPU);
  TORCH_INTERNAL_ASSERT(out.device().type() == at::DeviceType::CPU);
  at::Tensor a_contig = a.contiguous();
  at::Tensor b_contig = b.contiguous();
  const float* a_ptr = a_contig.data_ptr<float>();
  const float* b_ptr = b_contig.data_ptr<float>();
  float* result_ptr = out.data_ptr<float>();
  for (int64_t i = 0; i < out.numel(); i++) {
    result_ptr[i] = a_ptr[i] + b_ptr[i];
  }
}

定义运算符时,我们必须在模式中指定它会改变 out 张量

TORCH_LIBRARY(extension_cpp, m) {
  m.def("mymuladd(Tensor a, Tensor b, float c) -> Tensor");
  m.def("mymul(Tensor a, Tensor b) -> Tensor");
  // New!
  m.def("myadd_out(Tensor a, Tensor b, Tensor(a!) out) -> ()");
}

TORCH_LIBRARY_IMPL(extension_cpp, CPU, m) {
  m.impl("mymuladd", &mymuladd_cpu);
  m.impl("mymul", &mymul_cpu);
  // New!
  m.impl("myadd_out", &myadd_out_cpu);
}

注意

不要将任何可变张量作为运算符的输出返回,因为这会导致与 PyTorch 子系统(如 torch.compile)不兼容。

结论

在本教程中,我们介绍了将自定义 C++ 和 CUDA 运算符与 PyTorch 集成的推荐方法。 TORCH_LIBRARY/torch.library API 相当底层。有关如何使用 API 的更多信息,请参见 自定义运算符手册

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源