Files
openvino/tests/layer_tests/pytorch_tests/test_convolution.py
Surya Siddharth Pemmaraju e14d1b2a07 Torchscript backend (#17132)
* Added torch script backend

* Added ts_backend to pytorch layer tests

* Added use_ts_backend fixture to the test suite to activate the
torchscript backend

* Fixed failing test_dict layer test

* Added USE_TS_BACKEND as an env variable

* Removed use_ts_backend fixture

* Added more tests for ts backend

* Added more information in the comments about usage

* Removed convolution3d test from precommit_ts_backend

* Added some torchscript backend tests to ci

* Removed tests from CI as torch.compile doesn't support 3.11 currently

* Fixed linter issues

* Addressed PR comments and linter issues
2023-05-17 14:58:38 +02:00

234 lines
15 KiB
Python

# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import pytest
from pytorch_layer_test_class import PytorchLayerTest
d2_params = [{'weights_shape': [3, 3, 2, 2], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [1, 1], 'groups': 1,
'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 3, 2, 2], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [
1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [
1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [
1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'bias_shape': [1], 'pads': [
1, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1], 'strides': [1, 1], 'pads': [
1, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'bias_shape': [1], 'pads': [
3, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1], 'strides': [1, 1], 'pads': [
3, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'bias_shape': [1], 'pads': [
1, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1], 'strides': [1, 1], 'pads': [
0, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [
1, 0], 'dilations': [1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [
0, 1], 'dilations': [1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [
1, 0], 'dilations': [2, 2], 'groups': 3, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [
0, 0], 'dilations': [2, 2], 'groups': 3, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [2, 1], 'bias_shape': [1], 'pads': [
1, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1], 'strides': [2, 1], 'pads': [
0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [2, 2], 'bias_shape': [1], 'pads': [
0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1], 'strides': [2, 2], 'pads': [
0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 3, 1, 1], 'strides': [2, 1], 'pads': [
0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1], 'strides': [2, 2], 'bias_shape': [1], 'pads': [
0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1], 'strides': [2, 2], 'bias_shape': [1], 'pads': [
1, 1], 'dilations': [2, 2], 'groups': 1, 'output_padding': [1, 1], 'transposed': True},
]
d1_params = [
{'weights_shape': [3, 3, 2], 'strides': [1], 'pads': [0], 'dilations': [1], 'groups': 1, 'output_padding': [0],
'transposed': True},
{'weights_shape': [3, 3, 2], 'strides': [1], 'pads': [0], 'dilations': [
1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [1], 'pads': [0], 'dilations': [
1], 'groups': 3, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 1, 1], 'strides': [1], 'pads': [0], 'dilations': [
1], 'groups': 3, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [1], 'bias_shape': [1], 'pads': [
1], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 3, 1], 'strides': [1], 'pads': [
1], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [1], 'bias_shape': [1], 'pads': [
3], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 3, 1], 'strides': [1], 'pads': [
3], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [1], 'bias_shape': [1], 'pads': [
1], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 3, 1], 'strides': [1], 'pads': [
0], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [1], 'pads': [
1], 'dilations': [1], 'groups': 3, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 1, 1], 'strides': [1], 'pads': [1], 'dilations': [
1], 'groups': 3, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [1], 'pads': [
1], 'dilations': [2], 'groups': 3, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 1, 1], 'strides': [1], 'pads': [
0], 'dilations': [2], 'groups': 3, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [2], 'bias_shape': [1], 'pads': [
1], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 3, 1], 'strides': [2], 'pads': [
0], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [2], 'bias_shape': [1], 'pads': [
0], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 3, 1], 'strides': [2], 'pads': [
0], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 3, 1], 'strides': [1], 'pads': [0], 'dilations': [
1], 'groups': 1, 'output_padding': [0], 'transposed': False},
{'weights_shape': [3, 1, 1], 'strides': [2], 'bias_shape': [1], 'pads': [
0], 'dilations': [1], 'groups': 1, 'output_padding': [0], 'transposed': True},
{'weights_shape': [3, 1, 1], 'strides': [2], 'bias_shape': [1], 'pads': [
1], 'dilations': [2], 'groups': 1, 'output_padding': [1], 'transposed': True},
]
d3_params = [
{'weights_shape': [3, 3, 2, 2, 1], 'strides': [1, 1, 1], 'pads': [0, 0, 0], 'dilations': [1, 1, 1], 'groups': 1,
'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 3, 2, 2, 1], 'strides': [1, 1, 1], 'pads': [0, 0, 0], 'dilations': [
1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [0, 0, 0], 'dilations': [
1, 1, 1], 'groups': 3, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [0, 0, 0], 'dilations': [
1, 1, 1], 'groups': 3, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'bias_shape': [1], 'pads': [
1, 1, 1], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
1, 1, 1], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'bias_shape': [1], 'pads': [
3, 1, 3], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
3, 1, 3], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'bias_shape': [1], 'pads': [
1, 0, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
0, 1, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
1, 0, 0], 'dilations': [1, 1, 1], 'groups': 3, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
0, 1, 1], 'dilations': [1, 1, 1], 'groups': 3, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
1, 0, 0], 'dilations': [2, 2, 1], 'groups': 3, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [1, 1, 1], 'pads': [
0, 0, 0], 'dilations': [2, 2, 2], 'groups': 3, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [2, 1, 1], 'bias_shape': [1], 'pads': [
1, 0, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1, 1], 'strides': [2, 1, 1], 'pads': [
0, 0, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [2, 2, 2], 'bias_shape': [1], 'pads': [
0, 0, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 3, 1, 1, 1], 'strides': [2, 2, 2], 'pads': [
0, 0, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 3, 1, 1, 1], 'strides': [2, 1, 1], 'pads': [
0, 0, 1], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': False},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [2, 2, 2], 'bias_shape': [1], 'pads': [
0, 0, 0], 'dilations': [1, 1, 1], 'groups': 1, 'output_padding': [0, 0, 0], 'transposed': True},
{'weights_shape': [3, 1, 1, 1, 1], 'strides': [2, 2, 2], 'bias_shape': [1], 'pads': [
1, 1, 1], 'dilations': [2, 2, 2], 'groups': 1, 'output_padding': [1, 1, 1], 'transposed': True},
]
class TestConvolution(PytorchLayerTest):
def _prepare_input(self, ndim=4):
import numpy as np
shape = (1, 3, 10, 10, 10)
return (np.random.randn(*shape[:ndim]).astype(np.float32),)
def create_model(self, weights_shape, strides, pads, dilations, groups, bias, transposed, output_padding=0,
bias_shape=None, underscore=True):
import torch
bias_dim = 0
class aten__convolution(torch.nn.Module):
def __init__(self):
super(aten__convolution, self).__init__()
self.weight = torch.randn(weights_shape)
self.bias_shape = bias_shape
if self.bias_shape is None:
self.bias_shape = weights_shape[bias_dim]
self.bias = torch.randn(self.bias_shape) if bias else None
self.strides = strides
self.pads = pads
self.dilations = dilations
self.groups = groups
self.transposed = transposed
self.output_padding = output_padding
self._op = torch._convolution
def forward(self, x):
return self._op(
x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.transposed,
self.output_padding, self.groups, False, False, False, False
)
class aten_convolution(torch.nn.Module):
def __init__(self):
super(aten_convolution, self).__init__()
self.weight = torch.randn(weights_shape)
self.bias_shape = bias_shape
if self.bias_shape is None:
self.bias_shape = weights_shape[bias_dim]
self.bias = torch.randn(self.bias_shape) if bias else None
self.strides = strides
self.pads = pads
self.dilations = dilations
self.groups = groups
self.transposed = transposed
self.output_padding = output_padding
self._op = torch.convolution
def forward(self, x):
return self._op(
x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.transposed,
self.output_padding, self.groups
)
ref_net = None
if underscore:
return aten__convolution(), ref_net, "aten::_convolution"
return aten_convolution(), ref_net, "aten::convolution"
@pytest.mark.parametrize("params", d1_params)
@pytest.mark.parametrize("bias", [True, False])
@pytest.mark.parametrize("underscore", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_ts_backend
def test_convolution1d(self, params, bias, underscore, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias, underscore=underscore),
ie_device, precision, ir_version, dynamic_shapes=params['groups'] == 1,
kwargs_to_prepare_input={'ndim': 3})
@pytest.mark.parametrize("params", d2_params)
@pytest.mark.parametrize("bias", [True, False])
@pytest.mark.parametrize("underscore", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_ts_backend
def test_convolution2d(self, params, bias, underscore, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias, underscore=underscore),
ie_device, precision, ir_version, dynamic_shapes=params['groups'] == 1)
@pytest.mark.parametrize("params", d3_params)
@pytest.mark.parametrize("bias", [True, False])
@pytest.mark.parametrize("underscore", [True, False])
@pytest.mark.nightly
def test_convolution3d(self, params, bias, underscore, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias, underscore=underscore),
ie_device, precision, ir_version, dynamic_shapes=params['groups'] == 1,
kwargs_to_prepare_input={'ndim': 5})