Operator Project Build Extension

When msOpGen is used to create an operator project, the related build scripts are fixed locally. If you need to use new features provided by the operator project (for example, support for MC2 operators), you must run msOpGen again to regenerate the project. To avoid frequent project reconstruction due to feature updates, the CMake script of the operator project is packed into the CANN software package. You can use find_package to search for the corresponding CMake modules and use the CMake function APIs exposed by the operator project.

In this development mode, you can organize your operator project by referring to the project directory below. The directory structure is similar to that generated by msOpGen. However, you do not need to create the CMake and scripts directories, as they are packed into the CANN software package. Take the AddCustom operator as an example. The directory structure is as follows:

AddCustom
├── CMakeLists.txt              // Top-level build script of the operator project
├── CMakePresets.json           // Build configuration file, which is a built-in function of CMake. If this function is enabled, the CMake version must be 3.19 or later.
├── framework                   // Directory for storing the operator plugin implementation files during AI framework adaptation
├── op_host                     // Implementation file on the host
│   ├── add_custom.cpp         // File for operator prototype registration, shape inference, tiling implementation, and more
│   ├── add_custom_tiling.h    // Tiling definition file of the operator
│   └── CMakeLists.txt         // CMakeLists file on the host
├── op_kernel                   // Implementation file on the kernel
│   ├── Add
│   │   ├── add_custom.cpp    // Operator code implementation file
└   └── CMakeLists.txt         // CMakeLists file on the kernel

Compiling CMakeLists.txt

Build Environment Options

The following table lists the build environment options that can be configured and used.

  • Currently, options related to the Release and Debug versions are not supported.
  • Currently, cross-compilation options are not supported.
Table 1 Build environment options

Type

Environment Option

Default Value

Description

STRING

vendor_name

customize

Name of the vendor to which the custom operator belongs. You are advised to specify the vendor name to avoid conflicts with operator packages provided by other vendors.

STRING

ASCEND_COMPUTE_UNIT

-

AI processor model. The package of the corresponding model is built.

PATH

ASCEND_AUTOGEN_PATH

<CMAKE_BINARY_DIR>/autogen

Path for storing source files generated through npu_op_code_gen, such as the operator prototype library required for single-operator call (aclnn file) and operator integration into a graph.

BOOL

ENABLE_SOURCE_PACKAGE

TRUE

Whether to enable source code compilation.

If the source code and binary compilation configurations are set using npu_op_package, the configurations specified in npu_op_package have a higher priority.

BOOL

ENABLE_BINARY_PACKAGE

TRUE

Whether to enable binary compilation.

If the package type is set to SHARED or STATIC, the value must be TRUE.

If the source code and binary compilation configurations are set using npu_op_package, the configurations specified in npu_op_package have a higher priority.

PATH

ASCEND_CANN_PACKAGE_PATH

-

CANN software package path. By default, you do not need to set this option and can directly use it when compiling the CMake file.

The following is an example path: /usr/local/Ascend/cann.

The preceding build environment options can be configured in either of the following ways:

Build Command

If the package type is set to RUN, you can run the following build command and use CMake's packaging capability to generate the run package.

# Run the following command in the root directory of the project.
mkdir -p build_out
rm -rf build_out/*
cmake -S . -B build_out --preset=default
cmake --build build_out --target binary -j${nproc}
cmake --build build_out --target package -j${nproc}

If the package type is set to SHARED or STATIC, you can run the following build command:

# Run the following command in the root directory of the project.
mkdir -p build_out
rm -rf build_out/*
cmake -S . -B build_out --preset=default
cmake --build build_out --target binary -j${nproc}
cmake --build build_out --target install -j${nproc}

CMake Functions

The following table lists the available CMake functions.

Type

API

Description

package

npu_op_package

Creates a package.

npu_op_package_add

Adds a target or file to a package.

library

npu_op_library

Creates a host library.

npu_op_kernel_library

Creates a kernel library.

npu_op_kernel_options

Adds compilation options for the kernel target.

npu_op_kernel_sources

Describes the source code information of the kernel target.

npu_op_device_tiling_library

Creates a tiling library on the device.

Other

npu_op_code_gen

Executes the code generation process to generate the single-operator call code for aclnn and the prototype definition code required for operator integration into a graph.

  1. npu_op_package

    Creates a package.

    npu_op_package(<package_name> TYPE <type> [CONFIG] [ENABLE_SOURCE_PKG <value>] [ENABLE_BINARY_PACKAGE <value>] [INSTALL_PATH <path>])
    The parameters are described as follows:
    • <package_name>: (required) name of the package.
    • TYPE <type>: (required) package type. The options are RUN, SHARED, and STATIC, which correspond to the .run package, dynamic library, and static library of the operator, respectively.
    • [CONFIG]: (optional) Configures the content and installation location of the package.
      • [ENABLE_SOURCE_PKG <value>]: (optional) whether to package the source code into a package. The default value is True.
      • [ENABLE_BINARY_PACKAGE <value>]: (optional) whether to package binary files into a package. The default value is True.
      • [INSTALL_PATH <path>]: (optional) Specifies the installation path of the package. The default value is CMAKE_BINARY_DIR.
      • [ENABLE_CPACK <value>]: (optional) Indicates whether to package. The default value is True. In scenarios where you need to customize the packaging, you can set this parameter to False. If this parameter is set to False, the compilation product is generated in the <path> directory and is not packaged into a .run package.
  2. npu_op_package_add

    Adds a target or file to a package.

    # Add a target.
    npu_op_package_add(<package_name> LIBRARY <target_name1> [<target_name2>...] ) 
    # Add a file. It is supported only for .run packages.
    npu_op_package_add(<package_name> FILES <file_name1> [<file_name2>...] [TYPE <target_type>] [PACKAGE_PATH <pkg_path>])

    The parameters are described as follows:

    • <package_name>: (required) name of the package.
    • LIBRARY: (required) Specifies the name of the target to be added to the package.
      • <target_name1> [<target_name2>...]: (required) target name list.
    • FILES: (required) Specifies the name of the file to be added to the package.
      • <file_name1> [<file_name2>...]: (required) file name list.
    • [TYPE <target_type>]: (optional) Specifies the type of the file to be installed in the corresponding directory. The value can be ACLNN or GRAPH. If this parameter is set to ACLNN, the file is packaged to the directory containing the aclnn single-operator call header file under the .run package directory. If this parameter is set to GRAPH, the file is packaged to the directory containing the prototype definition header file for operator integration into a graph under the .run package directory.
    • [PACKAGE_PATH <pkg_path>]: (optional) Specifies the relative path of the file in the package. The TYPE and PACKAGE_PATH parameters are mutually exclusive. That is, you can only select one of them for configuration.
  3. npu_op_library

    Creates a host library.

    npu_op_library(<library_name> TYPE <library_type> <files>) 

    The parameters are described as follows:

    • <library_name>: (required) name of the host library.
    • TYPE <library_type>: (required) type of the host library. The value can be TILING, ACLNN, GRAPH, or TF_PLUGIN.
      • TILING: library related to tiling.
      • ACLNN: aclnn single-operator call library.
      • GRAPH: operator prototype library required for operator integration into a graph.
      • TF_PLUGIN: library related to TensorFlow adaptation.
    • <files>: (required) source files to be compiled.
  4. npu_op_kernel_library

    Creates a kernel library.

    npu_op_kernel_library(<target_name> SRC_BASE <path> TILING_LIBRARY <tiling_target>) 

    The parameters are described as follows:

    • <target_name>: (required) name of the target.
    • SRC_BASE <path>: (required) Specifies the base directory of the kernel source code. The value must be an absolute path, such as the absolute path of the op_kernel directory in the sample code.
    • TILING_LIBRARY <tiling_target>: (required) Specifies the dependent tiling target.
  5. npu_op_kernel_options
    Adds compilation options for the kernel target.
    npu_op_kernel_options(<target_name> <op_type> [COMPUTE_UNIT <soc_version>] OPTIONS ...) 

    The parameters are described as follows:

    • <target_name>: (required) name of the target.
    • <op_type>: (required) Defines the scope where the configuration takes effect. The value can be ALL or OP_TYPE. ALL indicates that the configuration takes effect for all operators, and OP_TYPE indicates that the configuration takes effect for specific operators.
    • [COMPUTE_UNIT <soc_version>]: (optional) Sets the compilation options of the operator on a specific AI processor model. If this parameter is not set, the configuration takes effect for all models by default.

      The configuration of <soc_version> is as follows:

      • For the following products: Run the npu-smi info command on the server where AI processor is installed to obtain the Name information. The actual value is AscendName. For example, if Name is xxxyy, the actual value is Ascendxxxyy.

        Atlas A2 training product/Atlas A2 inference product

        Atlas 200I/500 A2 inference product

        Atlas inference product

        Atlas training product

      • For the Atlas A3 training product/Atlas A3 inference product: Run the npu-smi info -t board -i id -c chip_id command on the server where AI processor is installed to obtain the Chip Name and NPU Name information. The actual value is Chip Name_NPU Name. For example, if the value of Chip Name is Ascendxxx and the value of NPU Name is 1234, the actual value is Ascendxxx_1234. Note that:
        • id: device ID, which is the NPU ID obtained by running the npu-smi info -l command.
        • chip_id: chip ID, which is obtained by running the npu-smi info -m command.
      • For the Atlas 350 Accelerator Card: Run the npu-smi info -t board -i id command on the server where AI processor is installed to obtain the Chip Name and NPU Name information. The actual value is Chip Name_NPU Name. For example, if the value of Chip Name is Ascendxxx and the value of NPU Name is 1234, the actual value is Ascendxxx_1234.

        id indicates the device ID, which is the NPU ID obtained by running the npu-smi info -l command.

    • OPTION...: (required) compilation options passed to the compiler.
  6. npu_op_kernel_sources

    Describes the source code information of the kernel target, including the kernel implementation file and source code path of the operator.

    npu_op_kernel_sources(<target_name> [OP_TYPE <op_type>] [KERNEL_DIR <path>] [COMPUTE_UNIT <soc_version>] [KERNEL_FILE <file>]) 

    The parameters are described as follows:

    • <target_name>: (required) name of the target.
    • [OP_TYPE <op_type>]: (optional) operator type. This parameter must be used together with KERNEL_FILE.
    • [KERNEL_DIR <path>]: (optional) Specifies the relative path of the kernel source code to SRC_BASE.

      If the source code files of the operator are not directly stored in the SRC_BASE directory (specified through npu_op_kernel_library), you can use KERNEL_DIR to specify a particular directory.

    • [COMPUTE_UNIT <soc_version>]: (optional) Specifies the model on which the kernel file (defined through KERNEL_FILE) takes effect. By default, the kernel file takes effect on all models.
    • [KERNEL_FILE <file>]: (optional) Specifies the name of the kernel implementation file that serves as the operator entrypoint.

      If the name of the .cpp file for operator implementation on the kernel needs to be customized, you need to specify both OP_TYPE (operator type) and KERNEL_FILE (name of the .cpp file for kernel implementation) to configure the mapping between them. If they are not specified, the .cpp file name and the operator type must comply with the conversion rules.

  7. npu_op_device_tiling_library

    Creates a tiling library on the device. When it is used, the package type can only be set to RUN.

    npu_op_device_tiling_library(<target_name> <type> <files>) 

    The parameters are described as follows:

    • <target_name>: (required) name of the target.
    • <type>: (required) type of the tiling artifact. The value can be SHARED or STATIC.
    • <files>: (required) tiling source file.
  8. npu_op_code_gen

    Executes the code generation process to generate the single-operator call code for aclnn and the prototype definition code required for operator integration into a graph.

    npu_op_code_gen(SRC <src_files> OUT_DIR <output_dir> PACKAGE <pkg_name> [COMPILE_OPTIONS ...])

    The parameters are described as follows:

    • SRC <src_files>: (required) range of source files involved in code generation.
    • OUT_DIR <output_dir>: (required) Specifies the output path of the generated code.
    • PACKAGE <pkg_name>: (required) Specifies the name of the package for the generated code.
    • [COMPILE_OPTIONS ...]: (optional) Specifies the compilation options during the compilation.