Creating an Operator Project

  1. Compile the prototype definition JSON file of the operator to generate the operator development project. For details about the parameters in the JSON file, see Table 1.
    For example, the JSON file of the AddCustom operator is named add_custom.json. The file content is as follows:
     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
    51
    [
        {
            "op": "AddCustom",
            "input_desc": [
                {
                    "name": "x",
                    "param_type": "required",
                    "format": [
                        "ND",
                        "ND",
                        "ND"
                    ],
                    "type": [
                        "fp16",
                        "float",
                        "int32"
                    ]
                },
                {
                    "name": "y",
                    "param_type": "required",
                    "format": [
                        "ND",
                        "ND",
                        "ND"
                    ],
                    "type": [
                        "fp16",
                        "float",
                        "int32"
                    ]
                }
            ],
            "output_desc": [
                {
                    "name": "z",
                    "param_type": "required",
                    "format": [
                        "ND",
                        "ND",
                        "ND"
                    ],
                    "type": [
                        "fp16",
                        "float",
                        "int32"
                    ]
                }
            ]
        }
    ]
    
    For example, the JSON file of the ReduceMaxCustom operator (including attributes) is named reduce_max_custom.json. The file content is as follows:
    [
        {
            "op": "ReduceMaxCustom",
            "input_desc": [
                {
                    "name": "x",
                    "param_type": "required",
                    "format": ["ND"],
                    "type": ["float16"]
                }
            ],
            "output_desc": [
                {
                    "name": "y",
                    "param_type": "required",
                    "format": ["ND"],
                    "type": ["float16"]
                },
                {
                    "name": "idx",
                    "param_type": "required",
                    "format": ["ND"],
                    "type": ["int32"]
                }
            ],
            "attr": [                                                                   
                {
                    "name": "reduceDim",
                    "param_type": "required",
                    "type": "int"
                },
                {
                    "name": "isKeepDim",
                    "param_type": "optional",
                    "type": "int",
                    "default_value": 1
                }
            ]
        }
    ]
    Table 1 JSON file field description

    Parameter

    Type

    Meaning

    Required

    op

    -

    String

    Operator type

    Yes

    input_desc

    -

    List

    Input description.

    No

    name

    String

    Input name.

    param_type

    String

    Parameter classification.

    • required
    • optional
    • dynamic

    Defaults to required.

    format

    List

    Supported formats for parameters of type tensor.

    Values:

    ND, NHWC, NCHW, HWCN, NC1HWC0, FRACTAL_Z, and more

    NOTE:

    format and type must be in one-to-one mapping. If only the unique value of one item is filled, the msOpGen tool automatically adds the unique value of the item that is not filled to the length of the filled item. For example, if format:["ND"] /type:["fp16","float","int32"] is set, the msOpGen tool automatically adds the unique input value ("ND") of format to the length of the type parameter. The automatically added value is format:["ND","ND","ND"]/type:["fp16","float","int32"].

    type

    List

    Parameter type.

    Value range: float, half, float16 (fp16), float32 (fp32), int8, int16, int32, int64, uint8, uint16, uint32, uint64, qint8, qint16, qint32, quint8, quint16, quint32, bool, double, string, resource, complex64, complex128, bf16, numbertype, realnumbertype, quantizedtype, all, BasicType, IndexNumberType, and so on.

    NOTE:
    • The supported data types vary with the operation. For details, see Ascend C Operator Development APIs.
    • format and type must be in one-to-one mapping. If only the unique value of one item is filled, the msOpGen tool automatically adds the unique value of the item that is not filled to the length of the filled item. For example, if format:["ND"] /type:["fp16","float","int32"] is set, the msOpGen tool automatically adds the unique input value ("ND") of format to the length of the type parameter. The automatically added value is format:["ND","ND","ND"]/type:["fp16","float","int32"].

    output_desc

    -

    List

    Output description.

    Yes

    name

    String

    Output name

    param_type

    String

    Parameter classification.

    • required
    • optional
    • dynamic

    Defaults to required.

    format

    List

    Supported formats for parameters of type tensor.

    Values:

    ND, NHWC, NCHW, HWCN, NC1HWC0, FRACTAL_Z, and more

    NOTE:

    format and type must be in one-to-one mapping. If only the unique value of one item is filled, the msOpGen tool automatically adds the unique value of the item that is not filled to the length of the filled item. For example, if format:["ND"] /type:["fp16","float","int32"] is set, the msOpGen tool automatically adds the unique input value ("ND") of format to the length of the type parameter. The automatically added value is format:["ND","ND","ND"]/type:["fp16","float","int32"].

    type

    List

    Parameter types.

    Value range: float, half, float16 (fp16), float32 (fp32), int8, int16, int32, int64, uint8, uint16, uint32, uint64, qint8, qint16, qint32, quint8, quint16, quint32, bool, double, string, resource, complex64, complex128, bf16, numbertype, realnumbertype, quantizedtype, all, BasicType, IndexNumberType, and so on.

    NOTE:
    • The supported data types vary with the operation. For details, see Ascend C Operator Development APIs.
    • format and type must be in one-to-one mapping. If only the unique value of one item is filled, the msOpGen tool automatically adds the unique value of the item that is not filled to the length of the filled item. For example, if format:["ND"] /type:["fp16","float","int32"] is set, the msOpGen tool automatically adds the unique input value ("ND") of format to the length of the type parameter. The automatically added value is format:["ND","ND","ND"]/type:["fp16","float","int32"].

    attr

    -

    List

    Attribute description.

    No

    name

    String

    Attribute name.

    param_type

    String

    Parameter classification.

    • required
    • optional

    Defaults to required.

    type

    String

    Parameter types.

    Value range:

    int, bool, float, string, list_int, list_float, and more

    default_value

    -

    Default value.

    • Multiple operators can be configured in a JSON file, which contains a list, with each element representing an operator.
    • If the input_desc or output_desc parameter has the same name, the latter parameter overwrites the previous one.
    • The type and format fields in input_desc and output_desc must match each other.

      For example, the type of the first input x is set to ["int8","int32"], the type of the second input y is set to ["fp16","fp32"], and the type of the output z is set to ["int32","int64"]. The operator supports the inputs ("int8","fp16") to generate int32 or the inputs ("int8","fp16") to generate int64. That is, the type fields of inputs are vertically mapped to the type field of the output, and cannot overlap.

    • The type and format fields in input_desc and output_desc must match each other, and must have the same quantity. If the type value is "numbertype", "realnumbertype", "quantizedtype", "BasicType", "IndexNumberType", or "all", check whether the quantities of type and format items are the same. If not, an error message is displayed during project creation. In addition, format items are supplemented based on the quantity of type items to generate an operator project. If the value of type is int32 and the type and format items cannot match, an error message is displayed during project generation, which interrupts project running.
    • The JSON file can be used to configure operator attributes. For details, see compiling the prototype definition file.
    • Name Operator Type in upper camel case and separate words with a single capitalized letter.
  2. Generate an operator development project.

    For example, to generate the AddCustom operator project, run the following command. For details about the parameters, see Table 2.

    msopgen gen -i {*.json} -f {framework type} -c {Compute Resource} -lan cpp -out {Output Path}
  3. After the command is executed, the operator project directory is generated in the specified directory. The project contains the operator implementation template file and compilation script.
    The operator project directory is generated in the ./output_data directory specified by -out. The directory is organized as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    output_data
    ├── build.sh         // Compilation entry script
    ├── cmake 
       ├── config.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
       ├── add_custom_tiling.h    // Operator tiling definition file
       ├── add_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   
       ├── add_custom.cpp        // Operator implementation file
    ├── scripts                     // Directory of scripts used for custom operator project packing
    
  4. Optional: Add an operator to an existing operator project. To add more custom operators to an existing operator project, include the -m 1 option on the command line.
    msopgen gen -i json_path/*.json -f tf -c ai_core-{Soc Version} -out ./output_data -m 1
    • -i: specifies the path of the operator prototype definition file add_custom.json.
    • -c: {Soc Version} indicates the Ascend AI Processor model.

    The operator is added to the **.json file in the operator project directory. Only operators based on the MindSpore framework can be added to the MindSpore operator project.

  5. After the operator project is generated, continue with Developing Operators.