单算子描述文件配置
不同输入或者不同format场景,单算子描述文件配置不同,本章节给出各场景的配置示例。
本章节中的单算子是基于Ascend IR定义的,描述文件为json格式。关于json描述文件中各参数的解释请参见描述文件参数说明,关于单算子的定义请参见《算子加速库接口参考》。
- format为ND:
该示例中的单算子转换后的离线模型为:add.om
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
[ { "op": "Add", "name": "add", "input_desc": [ { "format": "ND", "shape": [3,3], "type": "int32" }, { "format": "ND", "shape": [3,3], "type": "int32" } ], "output_desc": [ { "format": "ND", "shape": [3,3], "type": "int32" } ] } ]
- format为NCHW:
该示例中的单算子转换后的离线模型为:conv2d.om
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
[ { "op": "Conv2D", "name": "conv2d", "input_desc": [ { "format": "NCHW", "shape": [1, 3, 16, 16], "type": "float16" }, { "format": "NCHW", "shape": [3, 3, 3, 3], "type": "float16" } ], "output_desc": [ { "format": "NCHW", "shape": [1, 3, 16, 16], "type": "float16" } ], "attr": [ { "name": "strides", "type": "list_int", "value": [1, 1, 1, 1] }, { "name": "pads", "type": "list_int", "value": [1, 1, 1, 1] }, { "name": "dilations", "type": "list_int", "value": [1, 1, 1, 1] } ] } ]
- Tensor的实现format与原始format不同
ATC模型转换时,会将origin_format与origin_shape转成离线模型需要的format与shape。
该示例中的单算子转换后的离线模型为:add.om1 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
[ { "op": "Add", "name": "add", "input_desc": [ { "format": "NC1HWC0", "origin_format": "NCHW", "shape": [8, 1, 16, 4, 16], "origin_shape": [8, 16, 16, 4], "type": "float16" }, { "format": "NC1HWC0", "origin_format": "NCHW", "shape": [8, 1, 16, 4, 16], "origin_shape": [8, 16, 16, 4], "type": "float16" } ], "output_desc": [ { "format": "NC1HWC0", "origin_format": "NCHW", "shape": [8, 1, 16, 4, 16], "origin_shape": [8, 16, 16, 4], "type": "float16" } ] } ]
- 输入指定为常量
该场景下,支持设置为常量的输入,新增is_const和const_value两个参数,分别表示是否为常量以及常量取值,const_value当前仅支持一维list配置,具体配置个数由shape取值决定,例如,如下样例中shape为2,则const_value中列表个数为2;const_value中取值类型由type决定,假设type取值为float16,则单算子编译时会自动将const_value中的取值转换为float16格式的取值。
该示例中的单算子转换后的离线模型为:resizeBilinearV2.om
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 52 53 54 55
[ { "op": "ResizeBilinearV2", "name": "resizeBilinearV2", "input_desc": [ { "format": "NHWC", "name": "x", "shape": [ 4, 16, 16, 16 ], "type": "float16" }, { "format": "NHWC", "is_const": true, "const_value": [49, 49], "name": "size", "shape": [ 2 ], "type": "int32", "test": [7, 7.0] } ], "output_desc": [ { "format": "NHWC", "name": "y", "shape": [ 4, 48, 48, 16 ], "type": "float" } ], "attr": [ { "name": "align_corners", "type": "bool", "value": false }, { "name": "half_pixel_centers", "type": "bool", "value": false } ] } ]
- 可选输入(optional input):
当存在可选输入,且可选输入没有输入数据时,则必须将可选输入的format配置为RESERVED,同时将type配置为UNDEFINED;若可选输入有输入数据时,则按其输入数据的format、type配置即可。
该示例中的单算子转换后的离线模型为:matMulV2.om1 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
[ { "op": "MatMulV2", "name": "matMulV2", "input_desc": [ { "format": "ND", "shape": [16, 16], "type": "float" }, { "format": "ND", "shape": [16, 16], "type": "float" }, { "format": "RESERVED", "shape": [], "type": "UNDEFINED" }, { "format": "RESERVED", "shape": [], "type": "UNDEFINED" } ], "attr": [ { "name": "transpose_x1", "type": "bool", "value": false }, { "name": "transpose_x2", "type": "bool", "value": false } ], "output_desc": [ { "format": "ND", "shape": [16, 16], "type": "float" } ] } ]
- 输入个数不确定(动态输入场景):
该场景下,单算子的输入个数不确定。此处以AddN单算子为例。
该示例中的单算子转换后的离线模型为:addN.om
- 构造的单算子json文件使用动态输入dynamic_input参数,而不使用Tensor的名称name参数,构造的描述文件为:
该场景下算子的dynamic_input取值必须和算子信息库中该算子定义的输入name的取值相同。
具体设置几个输入,由AddN单算子描述文件属性参数中N的取值决定,用户可以自行修改输入的个数,但是必须和属性中N的取值匹配。(该说明仅针对AddN算子生效,其他动态输入算子的约束以具体算子为准。)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
[ { "op": "AddN", "name": "addN", "input_desc": [ { "dynamic_input": "x", "format": "NCHW", "shape": [1,3,166,166], "type": "float32" }, { "dynamic_input": "x", "format": "NCHW", "shape": [1,3,166,166], "type": "int32" }, { "dynamic_input": "x", "format": "NCHW", "shape": [1,3,166,166], "type": "float32" } ], "output_desc": [ { "format": "NCHW", "shape": [1,3,166,166], "type": "float32" } ], "attr": [ { "name": "N", "type": "int", "value": 3 } ] } ]
- 构造的单算子json文件使用Tensor的名称name参数,而不使用动态输入dynamic_input参数,构造的描述文件为:
该场景下算子的name取值必须和算子原型定义中算子的输入名称相同,根据输入的个数自动生成x0、x1、x2……。
具体设置几个Tensor名称,由AddN单算子描述文件属性参数中N的取值决定,用户可以自行修改Tensor名称的个数,但是必须和属性中N的取值匹配,例如N取值为3,则name取值分别设置为x0、x1、x2。(该说明仅针对AddN算子生效,其他动态输入算子的约束以具体算子为准。)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
[ { "op": "AddN", "name": "addN", "input_desc": [ { "name":"x0", "format": "NCHW", "shape": [1,3,166,166], "type": "float32" }, { "name":"x1", "format": "NCHW", "shape": [1,3,166,166], "type": "int32" }, { "name":"x2" "format": "NCHW", "shape": [1,3,166,166], "type": "float32", } ], "output_desc": [ { "format": "NCHW", "shape": [1,3,166,166], "type": "float32" } ], "attr": [ { "name": "N", "type": "int", "value": 3 } ] } ]
- 构造的单算子json文件使用动态输入dynamic_input参数,而不使用Tensor的名称name参数,构造的描述文件为:
父主题: 配置文件样例