Developing Ascend C Custom Operator

Explains how to use the msOpGen tool to generate, build, and deploy an Ascend C custom operator project, and how to use the msOpST tool to test the functions of the operator.

Prerequisites

Preparations for using the msOpGen tool are ready. For details about the preparations, see Before You Start.

Procedure

  1. Prepare the operator prototype file by referring to the following JSON file. The MatMulCustom operator is used as an example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    [
        {
            "op": "MatmulCustom",
            "language": "cpp",
            "input_desc": [
                {
                    "name": "a",
                    "param_type": "required",
                    "format": [
                        "ND"
                    ],
                    "type": [
                        "float16"
                    ]
                },
                {
                    "name": "b",
                    "param_type": "required",
                    "format": [
                        "ND"
                    ],
                    "type": [
                        "float16"
                    ]
                },
                {
                    "name": "bias",
                    "param_type": "required",
                    "format": [
                        "ND"
                    ],
                    "type": [
                        "float"
                    ]
                }
            ],
            "output_desc": [
                {
                    "name": "c",
                    "param_type": "required",
                    "format": [
                        "ND"
                    ],
                    "type": [
                        "float"
                    ]
                }
            ]
        }
    ]
    
  2. Use the msOpGen tool to run the following command to generate an operator project:

    The msOpGen tool generates only an empty operator project template. You need to add operators. For details, see Operator Implementation.

    msopgen gen -i MatMulCustom.json -f tf -c ai_core-ascendxxxyy -lan cpp -out MatmulCustom
  3. After the command is executed, the following operator project directory is generated in the specified directory.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    MatmulCustom/
    ├── build.sh         // Compilation entry script
    ├── cmake 
       ├── xx.cmake
       ├── ....
       ├── util        // Directory that stores the scripts used for operator project compilation and common compilation files.
    ├── CMakeLists.txt   // CMakeLists.txt script of the operator project
    ├── CMakePresets.json // Compilation configuration item
    ├── framework        // Directory for storing the implementation file of the operator plugin. The generation of single-operator model files does not depend on the operator plugin and can be ignored.
    ├── op_host                      // Implementation file on the host
       ├── matmul_custom_tiling.h    // Operator tiling definition file.
       ├── matmul_custom.cpp         // Content file for operator prototype registration, shape derivation, information library, and tiling implementation.
       ├── CMakeLists.txt
    ├── op_kernel                   // Implementation file on the kernel
       ├── CMakeLists.txt   
       ├── matmul_custom.cpp        // Operator implementation file.
    ├── scripts                     // Directory of scripts used for custom operator project packing.
    
  4. Compile the operator project.
    ./build.sh
  5. Deploy the custom OPP.
    • Run the following command to deploy the operator on CANN:
      1
      ./build_out/custom_opp_<target_os>_<target_architecture>.run
      
    • Run the following command to deploy the operator to a custom path (xxx/MatmulCustom/installed is used as an example):
      1
      ./build_out/custom_opp_<target_os>_<target_architecture>.run --install-path="xxx/MatmulCustom/installed" 
      
  6. Run the following command to generate ST cases.
    msopst create -i "xxx/MatmulCustom/op_host/matmul_custom.cpp" -out ./st //Change xxx to the actual project path.
  7. Then, perform the ST.
    1. Configure the following environment variables according to the CANN package installation path:
      export DDK_PATH=${INSTALL_DIR}
      export NPU_HOST_LIB=${INSTALL_DIR}/{arch-os}/devlib
    2. Run the following command to perform ST and save the test result to the specified path:
      msopst run -i ./st/xxx.json -soc Ascendxxxyy -out ./st/out  //xxx.json is the test case obtained in Step 6.