3. 高效算子设计指南(ONNX篇)

当算子设计范围与硬件支持范围相契合时, 可以更充分地挖掘硬件性能, 提高模型推理速度. 本章节针对在 AX620 硬件平台上如何实现高效设计算子进行说明.

3.1. Convolution

备注

The convolution operator consumes an input tensor and a filter, and computes the output.

Conv 支持的 OpSet Version: 1, 11-13

支持属性

性能说明

备注

kernel_shape

kernel_shape 为 3 时, 能达到 100% 性能

其他取值情况下效率低于50%

kernel_shape 为 1 时,性能降至 89%, 此时要求 input_channel % 32 == 0

pads

kernel_shape 为 3 且 pads 为 1 时最高效

/

strides

stride_h = 1, stride_w <= 2 时, 性能为 100%

stride = [2, 2] 下效率约为: output_channel / (output_channel + 8)

其他情况下, output_channel 越大越高效

当 kernel_shape 为 3 时, 避免 strdies 为 3

auto_pad

/

仅支持配置为 NOTSET

dilations

效率计算为: kernel_shape / ((kernel_shape - 1) * dilation + 1)

会浪费补满 dilation 所需的计算量

group

channel / group 是 16 倍数时效率最高, 但 input_width 必须为 32 的倍数

例如: depthwise conv 效率 1/16

提示

input/output_channel
  • input_channel16 的倍数, output_channel8 的倍数时效率最高

  • 不满足倍数限制时浪费补到对应倍数的计算量

3.2. ConvTranspose

ConvTranspose 对以下三种情况支持最高效.

  • kernel_size 为 2 x 2, stride 为 2, pad 取 0

  • kernel_size 为 4 x 4, stride 为 2, pad 取 1

  • kernel_size 为 4 x 4, stride 为 4, pad 取 0

注意

ConvTranspose 的效率略低于实现同样上采样功能的 resize 算子.

3.3. Linear

推荐 channels16 的倍数.

3.4. Activation

  • ReLU 支持最高效

  • LeakyReLU, HardSwish, Swish, Mish 也能高效支持(但弱于 ReLU)

  • PReLU 支持效率较低

3.5. Transpose/Reshape

注意

实现效率较低, 尽量避免使用.

3.6. Pool

算子

高效建议

MaxPool

高效支持 kernel_size <= 2kernel_size == stride 的的情况, 建议尽量 kernel_size <= 3

AvgPool

kernel_size2 的幂最高效, 建议最大不超过 32

3.7. Resize

  • scale 仅支持二的幂, 建议 [1/16, 1/8, 1/4, 1/2, 2, 4, 8, 16] 范围内

  • mode 仅支持 nearest, bilineararea.