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_channel
为16
的倍数,output_channel
为8
的倍数时效率最高不满足倍数限制时浪费补到对应倍数的计算量
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
推荐 channels
为 16
的倍数.
3.4. Activation
ReLU
支持最高效LeakyReLU
,HardSwish
,Swish
,Mish
也能高效支持(但弱于ReLU
)PReLU
支持效率较低
3.5. Transpose/Reshape
注意
实现效率较低, 尽量避免使用.
3.6. Pool
算子 |
高效建议 |
---|---|
MaxPool |
高效支持 |
AvgPool |
|
3.7. Resize
scale
仅支持二的幂, 建议 [1/16, 1/8, 1/4, 1/2, 2, 4, 8, 16] 范围内mode
仅支持nearest
,bilinear
和area
.