forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generalize conv2d implementation for conv1d and conv3d
- Loading branch information
Showing
3 changed files
with
161 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# Copyright (C) 2018-2022 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
from pytorch_layer_test_class import PytorchLayerTest | ||
|
||
|
||
class TestConv2D(PytorchLayerTest): | ||
def _prepare_input(self): | ||
import numpy as np | ||
return (np.random.randn(2, 3, 25, 25).astype(np.float32),) | ||
|
||
def create_model(self, weights_shape, strides, pads, dilations, groups, bias): | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
class aten_conv2d(torch.nn.Module): | ||
def __init__(self): | ||
super(aten_conv2d, self).__init__() | ||
self.weight = torch.randn(weights_shape) | ||
self.bias = None | ||
if bias: | ||
self.bias = torch.randn(weights_shape[0]) | ||
self.strides = strides | ||
self.pads = pads | ||
self.dilations = dilations | ||
self.groups = groups | ||
|
||
def forward(self, x): | ||
return F.conv2d(x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.groups) | ||
|
||
ref_net = None | ||
|
||
return aten_conv2d(), ref_net, "aten::conv2d" | ||
|
||
@pytest.mark.parametrize("params", | ||
[{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 2, 'pads': 0, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': 1, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 2, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': [0, 1], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': [1, 0], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': 'same', 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3], 'strides': 1, 'pads': 'valid', 'dilations': 1, 'groups': 1}, | ||
# doesn't work because input shape is dynamic which makes kernel shape dynamic | ||
# {'weights_shape': [2, 3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 1, 'groups': 2}, | ||
]) | ||
@pytest.mark.parametrize("bias", [True, False]) | ||
@pytest.mark.nightly | ||
def test_conv2d(self, params, bias, ie_device, precision, ir_version): | ||
self._test(*self.create_model(**params, bias=bias), | ||
ie_device, precision, ir_version) | ||
|
||
|
||
class TestConv1D(PytorchLayerTest): | ||
def _prepare_input(self): | ||
import numpy as np | ||
return (np.random.randn(2, 3, 25).astype(np.float32),) | ||
|
||
def create_model(self, weights_shape, strides, pads, dilations, groups, bias): | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
class aten_conv1d(torch.nn.Module): | ||
def __init__(self): | ||
super(aten_conv1d, self).__init__() | ||
self.weight = torch.randn(weights_shape) | ||
self.bias = None | ||
if bias: | ||
self.bias = torch.randn(weights_shape[0]) | ||
self.strides = strides | ||
self.pads = pads | ||
self.dilations = dilations | ||
self.groups = groups | ||
|
||
def forward(self, x): | ||
return F.conv1d(x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.groups) | ||
|
||
ref_net = None | ||
|
||
return aten_conv1d(), ref_net, "aten::conv1d" | ||
|
||
@pytest.mark.parametrize("params", | ||
[{'weights_shape': [3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [3, 3, 3], 'strides': 2, 'pads': 0, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [3, 3, 3], 'strides': 1, 'pads': 1, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 2, 'groups': 1}, | ||
{'weights_shape': [3, 3, 3], 'strides': 1, 'pads': 'same', 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [3, 3, 3], 'strides': 1, 'pads': 'valid', 'dilations': 1, 'groups': 1}, | ||
# doesn't work because input shape is dynamic which makes kernel shape dynamic | ||
# {'weights_shape': [3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 1, 'groups': 2}, | ||
]) | ||
@pytest.mark.parametrize("bias", [True, False]) | ||
@pytest.mark.nightly | ||
def test_conv1d(self, params, bias, ie_device, precision, ir_version): | ||
self._test(*self.create_model(**params, bias=bias), | ||
ie_device, precision, ir_version) | ||
|
||
|
||
class TestConv3D(PytorchLayerTest): | ||
def _prepare_input(self): | ||
import numpy as np | ||
return (np.random.randn(2, 3, 25, 25, 25).astype(np.float32),) | ||
|
||
def create_model(self, weights_shape, strides, pads, dilations, groups, bias): | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
class aten_conv3d(torch.nn.Module): | ||
def __init__(self): | ||
super(aten_conv3d, self).__init__() | ||
self.weight = torch.randn(weights_shape) | ||
self.bias = None | ||
if bias: | ||
self.bias = torch.randn(weights_shape[0]) | ||
self.strides = strides | ||
self.pads = pads | ||
self.dilations = dilations | ||
self.groups = groups | ||
|
||
def forward(self, x): | ||
return F.conv3d(x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.groups) | ||
|
||
ref_net = None | ||
|
||
return aten_conv3d(), ref_net, "aten::conv3d" | ||
|
||
@pytest.mark.parametrize("params", | ||
[{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 2, 'pads': 0, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': 1, 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 2, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': [0, 1, 0], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': [1, 0, 0], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': [0, 0, 1], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': [1, 1, 0], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': [0, 1, 1], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': [1, 0, 1], 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': 'same', 'dilations': 1, 'groups': 1}, | ||
{'weights_shape': [1, 3, 3, 3, 3], 'strides': 1, 'pads': 'valid', 'dilations': 1, 'groups': 1}, | ||
# doesn't work because input shape is dynamic which makes kernel shape dynamic | ||
# {'weights_shape': [2, 3, 3, 3], 'strides': 1, 'pads': 0, 'dilations': 1, 'groups': 2}, | ||
]) | ||
@pytest.mark.parametrize("bias", [True, False]) | ||
@pytest.mark.nightly | ||
def test_conv3d(self, params, bias, ie_device, precision, ir_version): | ||
self._test(*self.create_model(**params, bias=bias), | ||
ie_device, precision, ir_version) |