|
| 1 | +from executorch.backends.openvino.tests.ops.base_openvino_op_test import BaseOpenvinoOpTest |
| 2 | +import torch |
| 3 | + |
| 4 | +d2_params = [{'weights_shape': [3, 3, 2, 2], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [1, 1], 'groups': 1, |
| 5 | + 'output_padding': [0, 0], 'transposed': True}, |
| 6 | + {'weights_shape': [3, 3, 2, 2], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [ |
| 7 | + 1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 8 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [ |
| 9 | + 1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': True}, |
| 10 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [0, 0], 'dilations': [ |
| 11 | + 1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': False}, |
| 12 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'bias_shape': [1], 'pads': [ |
| 13 | + 1, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True}, |
| 14 | + {'weights_shape': [3, 3, 1, 1], 'strides': [1, 1], 'pads': [ |
| 15 | + 1, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 16 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'bias_shape': [1], 'pads': [ |
| 17 | + 3, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True}, |
| 18 | + {'weights_shape': [3, 3, 1, 1], 'strides': [1, 1], 'pads': [ |
| 19 | + 3, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 20 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'bias_shape': [1], 'pads': [ |
| 21 | + 1, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True}, |
| 22 | + {'weights_shape': [3, 3, 1, 1], 'strides': [1, 1], 'pads': [ |
| 23 | + 0, 1], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 24 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [ |
| 25 | + 1, 0], 'dilations': [1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': True}, |
| 26 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [ |
| 27 | + 0, 1], 'dilations': [1, 1], 'groups': 3, 'output_padding': [0, 0], 'transposed': False}, |
| 28 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [ |
| 29 | + 1, 0], 'dilations': [2, 2], 'groups': 3, 'output_padding': [0, 0], 'transposed': True}, |
| 30 | + {'weights_shape': [3, 1, 1, 1], 'strides': [1, 1], 'pads': [ |
| 31 | + 0, 0], 'dilations': [2, 2], 'groups': 3, 'output_padding': [0, 0], 'transposed': False}, |
| 32 | + {'weights_shape': [3, 1, 1, 1], 'strides': [2, 1], 'bias_shape': [1], 'pads': [ |
| 33 | + 1, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True}, |
| 34 | + {'weights_shape': [3, 3, 1, 1], 'strides': [2, 1], 'pads': [ |
| 35 | + 0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 36 | + {'weights_shape': [3, 1, 1, 1], 'strides': [2, 2], 'bias_shape': [1], 'pads': [ |
| 37 | + 0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True}, |
| 38 | + {'weights_shape': [3, 3, 1, 1], 'strides': [2, 2], 'pads': [ |
| 39 | + 0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 40 | + {'weights_shape': [3, 3, 1, 1], 'strides': [2, 1], 'pads': [ |
| 41 | + 0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': False}, |
| 42 | + {'weights_shape': [3, 1, 1, 1], 'strides': [2, 2], 'bias_shape': [1], 'pads': [ |
| 43 | + 0, 0], 'dilations': [1, 1], 'groups': 1, 'output_padding': [0, 0], 'transposed': True}, |
| 44 | + {'weights_shape': [3, 1, 1, 1], 'strides': [2, 2], 'bias_shape': [1], 'pads': [ |
| 45 | + 1, 1], 'dilations': [2, 2], 'groups': 1, 'output_padding': [1, 1], 'transposed': True}, |
| 46 | + ] |
| 47 | + |
| 48 | +class TestConvolutionOperator(BaseOpenvinoOpTest): |
| 49 | + |
| 50 | + def create_model(self, weights_shape, strides, pads, dilations, groups, bias, transposed, output_padding=0, |
| 51 | + bias_shape=None, underscore=False): |
| 52 | + |
| 53 | + bias_dim = 0 |
| 54 | + |
| 55 | + class Convolution(torch.nn.Module): |
| 56 | + def __init__(self): |
| 57 | + super().__init__() |
| 58 | + self.weight = torch.nn.Parameter(torch.randn(weights_shape)) |
| 59 | + self.bias_shape = bias_shape |
| 60 | + if self.bias_shape is None: |
| 61 | + self.bias_shape = weights_shape[bias_dim] |
| 62 | + self.bias = torch.nn.Parameter(torch.randn(self.bias_shape)) if bias else None |
| 63 | + self.strides = strides |
| 64 | + self.pads = pads |
| 65 | + self.dilations = dilations |
| 66 | + self.groups = groups |
| 67 | + self.transposed = transposed |
| 68 | + self.output_padding = output_padding |
| 69 | + if underscore: |
| 70 | + self.forward = self.forward_ |
| 71 | + |
| 72 | + def forward(self, x): |
| 73 | + return torch.convolution( |
| 74 | + x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.transposed, |
| 75 | + self.output_padding, self.groups |
| 76 | + ) |
| 77 | + |
| 78 | + def forward_(self, x): |
| 79 | + return torch._convolution( |
| 80 | + x, self.weight, self.bias, self.strides, self.pads, self.dilations, self.transposed, |
| 81 | + self.output_padding, self.groups, False, False, False, False |
| 82 | + ) |
| 83 | + |
| 84 | + return Convolution() |
| 85 | + |
| 86 | + def test_convolution(self): |
| 87 | + bias_underscore_config = [(False, False), (True, False)] |
| 88 | + for bias, underscore in bias_underscore_config: |
| 89 | + for params in d2_params: |
| 90 | + with self.subTest(params=params, bias=bias, underscore=underscore): |
| 91 | + bias_shape = None |
| 92 | + if 'bias_shape' in params: |
| 93 | + bias_shape = params['bias_shape'] |
| 94 | + module = self.create_model(weights_shape=params['weights_shape'], |
| 95 | + strides=params['strides'], |
| 96 | + pads=params['pads'], |
| 97 | + dilations=params['dilations'], |
| 98 | + groups=params['groups'], |
| 99 | + output_padding=params['output_padding'], |
| 100 | + transposed=params['transposed'], |
| 101 | + bias_shape=bias_shape, |
| 102 | + bias=bias, |
| 103 | + underscore=underscore) |
| 104 | + sample_input = (torch.randn(1, 3, 10, 10),) |
| 105 | + self.execute_layer_test(module, sample_input) |
0 commit comments