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 pybind

Adaptation Methods

Six adaptation samples are provided. You can choose the appropriate adaptation method as needed.

Adaptation MethodScenarioSupported FeaturesMore Information
cpp_extension_base (basic sample)Basic featuresOperator definition, operator adaptation, and ATen IR registration and bindingFor details, see Adaptation Development and Usage (Basic Sample).
cpp_extension_full (complete module sample)Full featuresOperator definition, operator adaptation, ATen IR registration and binding, meta function registration, and forward/backward bindingsForward 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 featuresOperator definition, operator adaptation, ATen IR registration and binding, meta function registration, and forward/backward bindingsForward 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 adaptationOperator definition, operator adaptation, and ATen IR registration and bindingAdaptation is completed automatically through YAML configuration. For details, see the Adaptation Development and Usage (Structured).
cpp_extension_asc (Ascend-C)Custom operatorOperator definition, operator adaptation, and ATen IR registration and bindingOperators are written using Ascend C. For details, see the Adaptation Development and Usage (AscendC-based).
cpp_extension_pybind (pybind)Flexible APIOperator definition, operator adaptation, and pybind bindingFor 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.