-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest_view.py
195 lines (154 loc) · 5.36 KB
/
test_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
import torch
from pytorch_layer_test_class import PytorchLayerTest
@pytest.mark.parametrize('input_shapes',
[
[
[2, 3, 2], np.array(2), np.array(6)
],
[
[4], np.array(2), np.array(2)
]
])
class TestViewListConstruct(PytorchLayerTest):
def _prepare_input(self):
return self.input_data
def create_model(self):
class aten_view_list_construct(torch.nn.Module):
def forward(self, input_tensor, dim1: int, dim2: int):
return input_tensor.view(dim1, dim2)
ref_net = None
return aten_view_list_construct(), ref_net, "aten::view"
@pytest.mark.nightly
@pytest.mark.precommit
def test_view_list_construct(self, ie_device, precision, ir_version, input_shapes):
self.input_data = []
for input_shape in input_shapes:
if type(input_shape) is list:
self.input_data.append(np.random.randn(*input_shape).astype(np.float32))
else:
self.input_data.append(input_shape)
self._test(*self.create_model(), ie_device, precision, ir_version)
@pytest.mark.parametrize('input_shapes',
[
[
[4], np.array(2)
]
])
class TestViewDtype(PytorchLayerTest):
def _prepare_input(self):
return self.input_data
def create_model(self):
class aten_view_dtype(torch.nn.Module):
def forward(self, input_tensor, dtype):
return input_tensor.view(torch.int64)
ref_net = None
return aten_view_dtype(), ref_net, "aten::view"
@pytest.mark.nightly
@pytest.mark.precommit
def test_view_dtype(self, ie_device, precision, ir_version, input_shapes):
self.input_data = []
for input_shape in input_shapes:
if type(input_shape) is list:
self.input_data.append(np.random.randn(*input_shape).astype(np.float32))
else:
self.input_data.append(input_shape)
self._test(*self.create_model(), ie_device, precision, ir_version)
@pytest.mark.parametrize('input_shapes',
[
[
[4], [2, 2]
]
])
class TestViewSize(PytorchLayerTest):
def _prepare_input(self):
return self.input_data
def create_model(self):
class aten_view_size(torch.nn.Module):
def forward(self, input_tensor, input_size):
return input_tensor.view(input_size.size()[:])
ref_net = None
return aten_view_size(), ref_net, "aten::view"
@pytest.mark.nightly
@pytest.mark.precommit
def test_view_size(self, ie_device, precision, ir_version, input_shapes):
self.input_data = []
for input_shape in input_shapes:
if type(input_shape) is list:
self.input_data.append(np.random.randn(*input_shape).astype(np.float32))
else:
self.input_data.append(input_shape)
self._test(*self.create_model(), ie_device, precision, ir_version)
@pytest.mark.parametrize('input_shapes',
[
[
[2, 3, 2], 2, 6
],
[
[4], 2, 2
],
[
[4], 2, 2.1
]
])
class TestView(PytorchLayerTest):
def _prepare_input(self):
return (self.input_data[0],)
def create_model(self):
class aten_view(torch.nn.Module):
def __init__(self, input_data) -> None:
super().__init__()
self.dim1 = input_data[1]
self.dim2 = input_data[2]
def forward(self, input_tensor):
return input_tensor.view(self.dim1, int(self.dim2))
ref_net = None
return aten_view(self.input_data), ref_net, "aten::view"
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
def test_view(self, ie_device, precision, ir_version, input_shapes):
self.input_data = []
for input_shape in input_shapes:
if type(input_shape) is list:
self.input_data.append(np.random.randn(*input_shape).astype(np.float32))
else:
self.input_data.append(input_shape)
self._test(*self.create_model(), ie_device, precision, ir_version)
@pytest.mark.parametrize('input_shapes',
[
[
[2, 3, 2], 2, 6
],
[
[4], 2, 2
],
[
[4], 2, 2.1
]
])
class TestViewCopy(PytorchLayerTest):
def _prepare_input(self):
return (self.input_data[0],)
def create_model(self):
class aten_view_copy(torch.nn.Module):
def __init__(self, input_data) -> None:
super().__init__()
self.dim1 = input_data[1]
self.dim2 = input_data[2]
def forward(self, input_tensor):
return torch.view_copy(input_tensor, [self.dim1, int(self.dim2)])
ref_net = None
return aten_view_copy(self.input_data), ref_net, "aten::view_copy"
@pytest.mark.precommit_fx_backend
def test_view_copy(self, ie_device, precision, ir_version, input_shapes):
self.input_data = []
for input_shape in input_shapes:
if type(input_shape) is list:
self.input_data.append(np.random.randn(*input_shape).astype(np.float32))
else:
self.input_data.append(input_shape)
self._test(*self.create_model(), ie_device, precision, ir_version)