Pytorch quantizes the pit of depthwise separable convolution

Use the quantization method that comes with pytorch.

The dynamic quantization method cannot quantize the convolutional layer, the code will not report an error, but it will not perform quantization operations

quantized_model = torch.quantization.quantize_dynamic(model, {nn.Conv2d}, dtype=torch.qint8)

 Only the following layers can be quantized

        if dtype == torch.qint8:
            qconfig_spec = {
                nn.Linear : default_dynamic_qconfig,
                nn.LSTM : default_dynamic_qconfig,
                nn.GRU : default_dynamic_qconfig,
                nn.LSTMCell : default_dynamic_qconfig,
                nn.RNNCell : default_dynamic_qconfig,
                nn.GRUCell : default_dynamic_qconfig,
            }

Adopt the static quantization method to the step of transforming the model

model_int8 = torch.quantization.convert(model_fused_prepared)

Since the depthwise separable convolution model is defined as follows

        def conv_dw(inp, oup, stride):
            return nn.Sequential(
                nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=True),
                nn.ReLU(inplace=True),
                nn.Conv2d(inp, oup, 1, 1, 0, bias=True),
                nn.ReLU(inplace=True),
            )

Reporting an error, the groups of the deep convolutional layer are 3, so this will not work.

RuntimeError: Quantized cudnn conv2d is currenty limited to groups = 1; received groups =3

Guess you like

Origin blog.csdn.net/qq_62426375/article/details/130007348