Operator Project Building

Overview

After the operator deliverables are developed, build the operator project to generate a custom OPP runfile (.run). The build workflow includes:

  • Compile the AI CPU operator code implementation files (.h and .cc) into libcust_aicpu_kernels.so.
  • Compile the operator information library definition file (.ini) into a .json file.
  • Build the operator prototype definition files (.h and .cc) into libcust_op_proto.so.
  • Compile the plugin implementation files (.h and .cc) of the TensorFlow/Caffe/ONNX operator into libcust_{tf/caffe/onnx}_parsers.so.

In Command Lines

  • Do not modify the sample project or the build configuration file that is automatically generated to avoid operator execution failures.
  • The OS where the OPP is built and that where the OPP will be deployed must have consistent version and architecture.
  1. Add the definition of Caffe-based operator to the custom.proto file of the custom operator project.

    If you want to develop operators based on another framework, skip this step. The custom.proto file is defined is as follows:

    syntax = "proto2";
    package domi.caffe;
    message NetParameter {
      optional string name = 1; 
      // LayerParameter definition. Retain the default.
      repeated LayerParameter layer = 100;  // ID 100 so layers are printed last.
    
    }
    message LayerParameter {
      optional string name = 1;  // Definition for model parsing. Retain the default.
      optional string type = 2;  // Definition for model parsing. Retain the default.
    
      // Add the definition of the custom layer to LayerParameter. The ID must be unique in the built-in caffe.proto file and must be less than 5000.
      // The built-in caffe.proto file is stored in include/proto/caffe.proto in the CANN component directory.
      optional CustomTest1Parameter custom_test1_param = 1000;  
      optional CustomTest2Parameter custom_test2_param = 1001;  
    }
    
    // Add the definition of the custom layer.
    message CustomTest1Parameter {
        optional bool adj_x1 = 1 [default = false];
        optional bool adj_x2 = 2 [default = false];
    }
    // If no attribute in the custom operator needs to be parsed and mapped, leave the definition of message xxParameter empty, as shown in the following:
    message CustomTest2Parameter {
    }

    You are advised to keep the parameter classification (in bold and italic) unique and different from that defined in the built-in caffe.proto file in include/proto/caffe.proto under the CANN component directory.

  2. Modify the build.sh script to configure the environment variables required for operator building.
    • ASCEND_TENSOR_COMPILER_INCLUDE: path of the header file of the CANN software.
      Uncomment this environment variable and change it to the path of the header file of the CANN software, for example:
      export ASCEND_TENSOR_COMPILER_INCLUDE=${INSTALL_DIR}/include

      Replace ${INSTALL_DIR} with the CANN component directory. For example, if the installation is performed by the root user, the default file storage path is /usr/local/Ascend/cann.

    • TOOLCHAIN_DIR: path of the compiler used for compiling AI CPU operators. Uncomment this environment variable and change it as follows:
      • In Ascend EP mode, set this environment variable to the path of Huawei Compiler Collection (HCC), for example:
        export TOOLCHAIN_DIR=${INSTALL_DIR}/toolkit/toolchain/hcc

        Replace ${INSTALL_DIR} with the CANN component directory. For example, if the installation is performed by the root user, the default file storage path is /usr/local/Ascend/cann.

    • AICPU_KERNEL_TARGET: name of the dynamic library file generated after the AI CPU operator implementation is built.
      • You are advised to uncomment the environment variable and add a unique suffix (for example, the software version) to the name of the dynamic library file to avoid library file naming conflicts for subsequent AI CPU upgrade. Note that the name of a dynamic library file allows a maximum of 84 bytes, for example:
        export AICPU_KERNEL_TARGET=cust_aicpu_kernels_3.3.0
      • If this environment variable is not set, the default value cust_aicpu_kernels is used.
    • AICPU_SOC_VERSION: Select the Ascend AI Processor version corresponding to the actual hardware platform.
    • vendor_name: name of the vendor to which the custom operator belongs. The default value is customize. You are advised to specify the vendor name to avoid conflicts with OPPs provided by other vendors. Currently, in a custom TBE operator project, the operator implementation code file is stored in the impl directory. After the OPP is deployed, the directory name is changed to ${vendor_name}_impl to avoid Python package name conflicts between operators of multiple vendors.
  3. Build the operator project.
    • If your project is created by Using Operator Samples, build your operator as follows:
      • To build a TBE operator only, run the following commands in the operator project directory:

        chmod +x build.sh

        ./build.sh -t

      • To build an AI CPU operator only, run the following commands in the operator project directory:

        chmod +x build.sh

        ./build.sh -c

      • To build both a TBE operator and an AI CPU operator, run the following commands in the operator project directory:

        chmod +x build.sh

        ./build.sh

    • If your project is created by Using the msopgen Tool, build your operator as follows:

      Run the following commands in the operator project directory:

      chmod +x build.sh

      ./build.sh

    After the project is built successfully, an OPP runfile named custom_opp_<target os>_<target architecture>.run is generated in the build_out directory.

    To rebuild the project, run the ./build.sh clean command to clear the build outputs.