Files
openvino/tests/layer_tests/pytorch_tests/test_convolution.py
T
d42b53c070 Torchfx backend (#18244)
* Added Torchscript Backend

* First commit for backend with Torch FX Decoder

* Merging changes from Torch FX branch

* Torch FX initial fixes (Temporary)

* Fixed type/shape issues in Torch FX decoder

* Added translation for built-in getitem

* MaxPool update & Output shape fix (Torch FX)

* Torch FX graph outputs fix

* Torch FX support for sigmoid and slu_

* Torch FX graph module caching

* Torch Fx partitioner cache removed

* Torch FX initial getitem replacer added

* Index check for torch fx getitem replacer

* Debug print removed from partitioner

* Added environment variables for pytorch tracing mode and openvino device

* FX translation fix for getitem & getitem replacer removed

* Added checks for PyTorch tracing mode environment variable

* Adding compile mode for fallback

* Added more ops for resnet18

* Added a check for environment variable

* Generalized addmm to work with torchscript and torchfx

* Added the missing batch_norm.default translation

* fx_backend: include get_attr ops to the partitions

* AddeTODO note t to improvget_attr algorithm

* created function for adding get_attr nodes

* fx_backend: added aten.mul.Tensor, re-enabled aten.empty.memory_format

* fx_backend: Additional op support/improvement for Inception V3

* Added comment for fix 64-bit to 32-bit max int conversion

* fx_backend: Update for avg_poolnd to support 3 inputs

* Fixed erorr in decoder.py

* TorchFX caching fix

* Torch backend, op support for Stable Diff. & BERT

* Arranged ops in order and added torch tensor mapping

* Added support for more ops for super glue

* TorchFX: Initial permanent fallback

* TorchFX: New ops for improved TorchVision support

* TorchFX backend optimizations for partitioning and tmp fallback

* working operator updates for superglue

* Updates to operators for superglue

* Removed max.dim and stack

* Cleanup

* Cleanup

* Fixed a couple of syntax issues

* Fixed a couple of syntax issues

* Added missing method to TorchFX Decoder

* Added missing method to TorchFX Decoder

* Removed redundant code for transpose

* TorchFX: Initial StableDiffusion support

* PyTorch decoder ovtype to ctype fix for int64

* Added ops for distilbert

* Fixed few unnecessary include statements

* Seperated TorchFX and TorchScript decoders

* Modified import statements to reflect two decoders

* f64 fix for TorchFX

* Import fix for PyTorch backend modules

* TorchFX serialize graph for debugging (Temporary)

* Serialize and load back feature enabled for TorchFX

* Temporary optimization to remove Broadcast

* Temporary SoftmaxRehapeElimination pass is added

* TorchFX custom model cache directory

* PyTorch bitwise translation, conversion checks enabled

* Naming fix in make_list_construct

* TorchFX: Added comments to Softmax and Slice translations

* translate_chunk temporarily removed for TS backend

* Fixed linter issues

* Addressed clang formatting issues

* Fixed few more clang and linter issues

* Fixed tests to use ts_decoder

* Fixed naming convention issues

* Added missing import

* Added inlined_inputs to TorchScriptDecoder

* Added tests for torch fx backend

* Removed magic numbers in PyTorch decoder utils

* TorchFX decoder data type fix

* Added cast from size_t to int

* TorchFX output handling code cleanup

* TorchFX: Use detached input tensor

* Added missing cast from size_t to int

* Added static cast in group_norm

* Fixed casting issue in split

---------

Co-authored-by: ynimmaga <yamini.nimmagadda@intel.com>
Co-authored-by: Cavus Mustafa <mustafa.cavus@intel.com>
2023-07-26 16:23:42 +02:00

236 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
@pytest.mark.precommit_fx_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
@pytest.mark.precommit_fx_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})