Ejemplo n.º 1
0
 def init(self, N, IC, OC, H, W, G, kernel, stride, pad):
     super(QConv2dBenchmark, self).init(N, IC, OC, (H, W), G, (kernel, kernel), stride, pad)
     self.qconv2d = nnq.Conv2d(IC, OC, kernel, stride=stride, padding=pad, groups=G)
     self.qconv2d.set_weight_bias(self.qW, None)
     self.qconv2d.scale = torch.tensor([self.scale], dtype=torch.double)
     self.qconv2d.zero_point = torch.tensor([self.zero_point], dtype=torch.int)
     self.set_module_name("QConv2d")
Ejemplo n.º 2
0
    def init(self, N, IC, OC, H, W, G, kernel, stride, pad):
        scale = 1.0 / 255
        zero_point = 0
        X = torch.randn(N, IC, H, W, dtype=torch.float32)
        qX = torch.quantize_per_tensor(X,
                                       scale=scale,
                                       zero_point=zero_point,
                                       dtype=torch.quint8)
        # Convert the tensor to NHWC format
        qX = qX.contiguous(memory_format=torch.channels_last)
        W = torch.randn(OC, IC // G, kernel, kernel, dtype=torch.float32)
        qW = torch.quantize_per_tensor(W,
                                       scale=scale,
                                       zero_point=0,
                                       dtype=torch.qint8)

        self.input = qX
        self.qconv2d = nnq.Conv2d(IC,
                                  OC,
                                  kernel,
                                  stride=stride,
                                  padding=pad,
                                  groups=G)
        self.qconv2d.set_weight_bias(qW, None)
        self.qconv2d.scale = torch.tensor([scale], dtype=torch.double)
        self.qconv2d.zero_point = torch.tensor([zero_point], dtype=torch.int)
        self.set_module_name("QConv2d")
Ejemplo n.º 3
0
    def init(self, N, IC, OC, H, W, G, kernel, stride, pad):
        scale = 1.0 / 255
        zero_point = 0
        X = torch.randn(N, IC, H, W, dtype=torch.float32)
        qX = torch.quantize_linear(X,
                                   scale=scale,
                                   zero_point=zero_point,
                                   dtype=torch.quint8)
        W = torch.randn(OC, IC // G, kernel, kernel, dtype=torch.float32)
        qW = torch.quantize_linear(W,
                                   scale=scale,
                                   zero_point=0,
                                   dtype=torch.qint8)

        self.input = qX
        self.qconv2d = nnq.Conv2d(IC,
                                  OC,
                                  kernel,
                                  stride=stride,
                                  padding=pad,
                                  groups=G)
        self.qconv2d.weight = qW
        self.qconv2d.scale = torch.tensor([scale], dtype=torch.double)
        self.qconv2d.zero_point = torch.tensor([zero_point], dtype=torch.int)
        self.set_module_name("QConv2d")
Ejemplo n.º 4
0
    def init(self, IC, OC, kernel, stride, N, H, W, G, pad, device):
        # super(QConv2dBenchmark, self).init(N, IC, OC, (H, W), G, (kernel, kernel), stride, pad)

        self.scale = 1.0 / 255
        self.zero_point = 0
        X = torch.randn(N, IC, H, W, dtype=torch.float32)
        qX = torch.quantize_per_tensor(X,
                                       scale=self.scale,
                                       zero_point=self.zero_point,
                                       dtype=torch.quint8)
        # Convert the tensor to NHWC format
        W = torch.randn(OC, IC // G, kernel, kernel, dtype=torch.float32)
        self.qW = torch.quantize_per_tensor(W,
                                            scale=self.scale,
                                            zero_point=0,
                                            dtype=torch.qint8)

        self.input = qX

        self.qconv2d = nnq.Conv2d(IC,
                                  OC,
                                  kernel,
                                  stride=stride,
                                  padding=pad,
                                  groups=G)
        self.qconv2d.set_weight_bias(self.qW, None)
        self.qconv2d.scale = torch.tensor([self.scale], dtype=torch.double)
        self.qconv2d.zero_point = torch.tensor([self.zero_point],
                                               dtype=torch.int)
        self.set_module_name("QConv2d")
Ejemplo n.º 5
0
    def test_conv2d_api(self, batch_size, in_channels_per_group, H, W,
                        out_channels_per_group, groups, kernel_h, kernel_w,
                        stride_h, stride_w, pad_h, pad_w, dilation, X_scale,
                        X_zero_point, W_scale, W_zero_point, Y_scale,
                        Y_zero_point, use_bias, use_fused, use_channelwise):
        # Tests the correctness of the conv2d module.
        in_channels = in_channels_per_group * groups
        out_channels = out_channels_per_group * groups
        input_feature_map_size = (H, W)
        kernel_size = (kernel_h, kernel_w)
        stride = (stride_h, stride_w)
        padding = (pad_h, pad_w)
        dilation = (dilation, dilation)
        if torch.backends.quantized.engine == 'qnnpack':
            use_channelwise = False
        if use_fused:
            module_name = "QuantizedConvReLU2d"
            qconv_module = nnq_fused.ConvReLU2d(in_channels,
                                                out_channels,
                                                kernel_size,
                                                stride,
                                                padding,
                                                dilation,
                                                groups,
                                                use_bias,
                                                padding_mode="zeros")
        else:
            module_name = "QuantizedConv2d"
            qconv_module = nnq.Conv2d(in_channels,
                                      out_channels,
                                      kernel_size,
                                      stride,
                                      padding,
                                      dilation,
                                      groups,
                                      use_bias,
                                      padding_mode="zeros")

        conv_module = nn.Conv2d(in_channels,
                                out_channels,
                                kernel_size,
                                stride,
                                padding,
                                dilation,
                                groups,
                                use_bias,
                                padding_mode="zeros")
        if use_fused:
            relu_module = nn.ReLU()
            conv_module = nni.ConvReLU2d(conv_module, relu_module)
        conv_module = conv_module.float()

        self._test_conv_api_impl(
            module_name, qconv_module, conv_module, batch_size,
            in_channels_per_group, input_feature_map_size,
            out_channels_per_group, groups, kernel_size, stride, padding,
            dilation, X_scale, X_zero_point, W_scale, W_zero_point, Y_scale,
            Y_zero_point, use_bias, use_fused, use_channelwise)
 def test_conv2d(self):
     module = nnq.Conv2d(3,
                         3,
                         kernel_size=3,
                         stride=1,
                         padding=0,
                         dilation=1,
                         groups=1,
                         bias=True,
                         padding_mode="zeros")
     self._test_op(module, input_size=[1, 3, 6, 6], generate=False)
Ejemplo n.º 7
0
    def init(self, N, IC, OC, H, W, G, kernel, stride, pad):
        scale = 1.0 / 255
        zero_point = 0
        X = torch.randn(N, IC, H, W, dtype=torch.float32)
        X = X.permute([0, 2, 3, 1]).contiguous()
        qX = torch.quantize_linear(X, scale=scale, zero_point=zero_point, dtype=torch.quint8)
        W = torch.randn(OC, IC // G, kernel, kernel, dtype=torch.float32)
        W = W.permute([0, 2, 3, 1]).contiguous()
        qW = torch.quantize_linear(W, scale=scale, zero_point=0, dtype=torch.qint8)

        self.input = qX
        self.qconv2d = nnq.Conv2d(IC, OC, kernel, stride=stride, padding=pad, groups=G)
        self.qconv2d.weight = qW
        self.qconv2d.scale = scale
        self.qconv2d.zero_point = zero_point
        self.set_module_name("QConv2d")
Ejemplo n.º 8
0
 def test_conv2d(self):
     for i, qengine in enumerate(supported_qengines):
         with override_quantized_engine(qengine):
             module = nnq.Conv2d(3, 3, kernel_size=3, stride=1, padding=0, dilation=1,
                                 groups=1, bias=True, padding_mode="zeros")
             self._test_op(module, input_size=[1, 3, 6, 6], generate=False, iter=i)