尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

CANN/ge单算子转换配置示例

CANN/ge单算子转换配置示例 配置文件样例【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge单算子描述文件配置不同输入或者不同Format场景单算子描述文件配置不同本章节给出各场景的配置示例。本章节中的单算子是基于Ascend IR定义的描述文件为JSON格式。关于JSON描述文件中各参数的解释请参见表1关于单算子的Ascend IR定义请参见《算子库》 “Ascend IR算子规格说明” 。Format为ND该示例中的单算子转换后的离线模型为add.om[ { 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[ { 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.om[ { 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中列表个数为2const_value中取值类型由type决定假设type取值为float16则单算子编译时会自动将const_value中的取值转换为float16格式的取值。该示例中的单算子转换后的离线模型为resizeBilinearV2.om[ { 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 } ], 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.om[ { 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算子生效其他动态输入算子的约束以具体算子为准。[ { 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算子生效其他动态输入算子的约束以具体算子为准。[ { 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文件配置一组配置包括算子类型、算子输入和输出信息、视算子情况决定是否包括属性信息。如果JSON文件配置了多组算子则模型转换完成后会生成多组算子对应的om离线模型文件。如下配置文件只是样例请根据实际情况进行修改。[ { op: MatMul, name: matMul01, input_desc: [ { format: ND, shape: [ 16, 16 ], type: float16 }, ... ], output_desc: [ { format: ND, shape: [ 16, 16 ], type: float16 } ], attr: [ { name: alpha, type: float, value: 1.0 }, ... ] }, { op: MatMul, name: matMul02, input_desc: [ { format: ND, shape: [ 256, 256 ], type: float16 }, ... ], output_desc: [ { format: ND, shape: [ 256, 256 ], type: float16 } ], attr: [ { name: alpha, type: float, value: 1.0 }, ... ] } ]动态Shape单算子描述文件配置动态Shape场景单算子描述文件根据场景不同内容也有差异本章节就给出不同场景下的配置样例。模型编译时不指定Shape模型执行时根据输入静态Shape能推导出具体输出Shape[ { op: Add, name: add, input_desc: [ { format: ND, shape: [-1,16], shape_range: [[0, 32]], type: int64 }, { format: ND, shape: [-1,16], shape_range: [[0, 32]], type: int64 } ], output_desc: [ { format: ND, shape: [-1,16], shape_range: [[0,32]], type: int64 } ] } ]模型编译时不指定Shape模型执行时根据输入静态Shape和常量能推导出具体输出Shape[ { op: TopK, name: topK, input_desc: [ { format: ND, shape: [-1], shape_range: [[1,-1]], type: int32 }, { format: ND, shape: [], # 推理时会传入常量 type: int32 } ], output_desc: [ { format: ND, shape: [-1], shape_range: [[1,-1]], type: int32 }, { format: ND, shape: [-1], shape_range: [[1,-1]], type: int32 }], attr: [ { name: sorted, type: bool, value: true } ] } ]模型编译时不指定Shape模型执行时根据输入静态Shape无法得到算子的准确输出Shape但可以得到输出Shape的范围。该场景下在输出参数output_desc中将算子输出TensorDesc中Shape为动态维度的维度值记为“-1”并对其“-1”的维度给出shape_range取值范围[ { op: Where, name: where, input_desc: [ { format: ND, shape: [-1], shape_range: [[1,-1]], type: int32 } ], output_desc: [ { format: ND, shape: [-1, 1], shape_range: [[1,-1]], type: int64 } ] } ]【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表