Supported Fusion Features

During quantization, operator fusion is performed on some structures in the model. Fusion optimizes the graph structure to improve inference performance of the network. This section describes the supported fusion features in detail.

  • Caffe:
    • Conv+BN+Scale fusion: Before quantization, AMCT performs Conv+BN+Scale fusion on the "Convolution+BatchNorm+Scale" structure in the model. After the fusion, the BatchNorm and Scale layers are deleted.
    • DeConv+BN+Scale fusion: Before quantization, AMCT performs DeConv+BN+Scale fusion on the "Deconvolution+BatchNorm+Scale" structure in the model. After the fusion, the BatchNorm and Scale layers are deleted.
  • TensorFlow:
    • Conv+BN fusion: Before quantization, AMCT performs Conv+BN fusion on the "Conv2D/Conv3D+BatchNorm" structure in the model. After the fusion, the BatchNorm layer is deleted.
    • Depthwise_Conv+BN fusion: Before quantization, AMCT performs Depthwise_Conv+BN fusion on the "DepthwiseConv2dNative+BatchNorm" structure in the model. After the fusion, the BatchNorm layer is deleted.
    • Conv2DBackpropInput+BN fusion: Before quantization, AMCT performs Conv2DBackpropInput+BN fusion on the "Conv2DBackpropInput+BatchNorm" structure in the model. After the fusion, the BatchNorm layer is deleted.
    • Split+Conv+Concat fusion: Before quantization, AMCT performs group_conv fusion on the "Split+Conv (multiple)+Bias (optional)+Concat" structure in the model to reduce the computation workload in the calibration process and optimize the memory usage and performance of the quantized model. Split operators include Split and SplitV, convolution operators include Conv2d, and Concat operators include Concat and ConcatV2.

      Requirements: split_dim of the Split operator and concat_dim of the Concat operator must match the index of the Cout axis of the convolution. All convolution operators must have consistent quantization configurations and operator attributes.

  • ONNX:
    • Conv+BN fusion: Before quantization, AMCT performs Conv+BN fusion on the "Conv+BatchNormalization" structure in the model. After the fusion, the BatchNorm layer is deleted.
    • ConvTranspose+BN fusion: Before quantization, AMCT performs ConvTranspose+BN fusion on the "ConvTranspose+BatchNormalization" structure in the model. After the fusion, the BatchNorm layer is deleted.
  • Replacing Relu6 (which cannot be used for requant fusion) with Relu in requant fusion scenarios: Relu6 adds truncation of values above 6 on top of Relu. Since input floating-point values are also truncated during quantization, "AscendDequant+Relu6+AscendQuant" and "AscendDequant+Relu+AscendQuant" are equivalent in some scenarios and can be replaced. Accordingly, AMCT replaces "AscendDequant+Relu6+AscendQuant" with "AscendDequant+Relu+AscendQuant" in the quantized deployable model.

    Replacement is allowed only when the following condition is met: (127 − offset)/scale < 6, where scale and offset are the quantization parameters obtained from quant.

  • BatchNorm+Mul+Add fusion (applicable to TensorFlow and ONNX)

    Before quantization, AMCT performs BN+Mul fusion on the "BatchNorm+Mul" structure in the model. The Mul layer is deleted after fusion. Then, it performs BN+Add fusion on the "BatchNorm+Add" structure in the model. The Add layer is deleted after fusion.

  • Fusion of small BN operators into a large BatchNorm operator (applicable to TensorFlow and ONNX)

    AMCT matches small-operator BN structures in the model and replaces them with the large-operator BN structure. Preconditions for this fusion include:

    1. The data node in the BN structure must be a quantizable node (Conv2D, DepthwiseConv2D, MatMulV2, or Conv3D).
    2. If a node other than data, scale, offset, mean, variance, and output node inside the structure is connected to a node outside the structure, fusion is not performed.

    For BN structures without offset, an all-zero offset node is constructed during fusion. For BN structures without scale, an all-one scale node is constructed during fusion. The supported small-operator BN structures are as follows:

    • No offset is configured, is_training is set to False, data_format is NHWC, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • No offset is configured, is_training is set to False, data_format is NCHW, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • No scale or offset is configured, is_training is set to False, data_format is NHWC, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • No scale or offset is configured, is_training is set to False, data_format is NCHW, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • No scale is configured, is_training is set to False, data_format is NHWC, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • No scale is configured, is_training is set to False, data_format is NCHW, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • is_training is set to False, data_format is NHWC, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • is_training is set to False, data_format is NCHW, and the input node is of the Const type. The following figure shows the network structures before and after fusion.

    • No offset is configured, is_training is set to False, data_format is NHWC, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • No offset is configured, is_training is set to False, data_format is NCHW, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • No scale or offset is configured, is_training is set to False, data_format is NHWC, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • No scale or offset is configured, is_training is set to False, data_format is NCHW, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • No scale is configured, is_training is set to False, data_format is NHWC, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • No scale is configured, is_training is set to False, data_format is NCHW, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • is_training is set to False, data_format is NHWC, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • is_training is set to False, data_format is NCHW, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • This is a simplified BN structure where no offset is configured, data_format is NHWC, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • This is a simplified BN structure where no offset is configured, data_format is NCHW, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • This is a simplified BN structure where no scale or offset is configured, data_format is NHWC, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

    • This is a simplified BN structure where no scale or offset is configured, data_format is NCHW, and the input node is of the Variable type. The following figure shows the network structures before and after fusion.

  • Fusion of bn_branch small-operator structures into a large-operator BN structure

    AMCT matches dual-BN structures of the bn_branch small-operator pattern and replaces the matched small-operator structures with a large-operator BN structure. Fusion is skipped if any of the following three conditions is met: the two BN branches take input data from different sources, the structure outputs the training BN result, or the is_training parameter of the inference BN is set to True. During operator fusion, the large inference BN operator in the original structure is retained, redundant nodes such as Switch in the small-operator structure are deleted, and the input and output edges of the retained BN operator are reconnected, thereby achieving fusion of the small operators into a large operator. The supported small-operator BN structures are as follows.