Creating an Operator Project
- 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 } ] } ]
- 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.
- 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} - 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
- 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.
- After the operator project is generated, continue with Developing Operators.
Parent topic: msOpGen (Operator Project Generation)