Adaptation Description
Overview
The C++ extensions plugin maps custom operators to Ascend AI Processors, allowing PyTorch developers to seamlessly access the NPU operator library. Based on the native custom operator extension capabilities provided by PyTorch, developers can compile, install, and execute the custom operator wheel package. This chapter describes how to develop and adapt custom operators on Ascend NPUs using C++ extensions. For details about the official PyTorch C++ extensions feature, see PyTorch C++ Extensions documentation.
Directory Structure
This directory contains all samples for building custom operator libraries by using C++ extensions.
├── cpp_extension_base // Basic sample
├── cpp_extension_full // Complete sample
│ ├── module // Forward and backward bindings implemented in Python
│ └── torch_library_impl // Forward and backward bindings implemented in C++
├── cpp_extension_structured // Structured adaptation
├── cpp_extension_asc // Ascend C-based operator implementation
└── cpp_extension_pybind // Interface binding implemented using pybindAdaptation Methods
Six adaptation samples are provided. You can choose the appropriate adaptation method as needed.
| Adaptation Method | Scenario | Supported Features | More Information |
|---|---|---|---|
cpp_extension_base (basic sample) | Basic features | Operator definition, operator adaptation, and ATen IR registration and binding | For details, see Adaptation Development and Usage (Basic Sample). |
cpp_extension_full (complete module sample) | Full features | Operator definition, operator adaptation, ATen IR registration and binding, meta function registration, and forward/backward bindings | Forward and backward bindings are implemented using the Python class forward and backward methods.For details, see the Adaptation Development and Usage (Complete Module Sample). |
cpp_extension_full (complete TORCH_LIBRARY_IMPL sample) | Full features | Operator definition, operator adaptation, ATen IR registration and binding, meta function registration, and forward/backward bindings | Forward and backward bindings are implemented through TORCH_LIBRARY_IMPL.For details, see the Adaptation Development and Usage (Complete TORCH_LIBRARY_IMPL Sample). |
cpp_extension_structured (structured) | Structured adaptation | Operator definition, operator adaptation, and ATen IR registration and binding | Adaptation is completed automatically through YAML configuration. For details, see the Adaptation Development and Usage (Structured). |
cpp_extension_asc (Ascend-C) | Custom operator | Operator definition, operator adaptation, and ATen IR registration and binding | Operators are written using Ascend C. For details, see the Adaptation Development and Usage (AscendC-based). |
cpp_extension_pybind (pybind) | Flexible API | Operator definition, operator adaptation, and pybind binding | For details, see the Adaptation Development and Usage (pybind-based). |
Prerequisites for using cpp_extension_structured (structured): The ACLNN interface corresponding to opapi must be semantically consistent with the ATen IR, and the adaptation layer must contain no logic other than output tensor allocation.