-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest_as_strided.py
165 lines (135 loc) · 6.16 KB
/
test_as_strided.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
# 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
class TestAsStrided(PytorchLayerTest):
def _prepare_input(self):
return (np.random.randn(8, 8).astype(np.float32),)
def create_model(self, size, stride, offset):
class aten_as_strided(torch.nn.Module):
def __init__(self, size, stride, offset):
super().__init__()
self.size = size
self.stride = stride
self.offset = offset
def forward(self, x):
return torch.as_strided(x, self.size, self.stride, self.offset)
ref_net = None
return aten_as_strided(size, stride, offset), ref_net, "aten::as_strided"
@pytest.mark.parametrize(
"size,stride",
[
([1], [1]),
([2, 2], [1, 1]),
([5, 4, 3], [1, 3, 7]),
([5, 5, 5], [5, 0, 5]),
([1, 2, 3, 4], [4, 3, 2, 1]),
],
)
@pytest.mark.parametrize("offset", [None, 1, 3, 7])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
def test_as_strided(self, size, stride, offset, ie_device, precision, ir_version):
self._test(*self.create_model(size, stride, offset), ie_device, precision, ir_version, trace_model=True)
class TestAsStridedCopy(PytorchLayerTest):
def _prepare_input(self):
return (np.random.randn(8, 8).astype(np.float32),)
def create_model(self, size, stride, offset):
class aten_as_strided_copy(torch.nn.Module):
def __init__(self, size, stride, offset):
super().__init__()
self.size = size
self.stride = stride
self.offset = offset
def forward(self, x):
return torch.as_strided_copy(x, self.size, self.stride, self.offset)
ref_net = None
return aten_as_strided_copy(size, stride, offset), ref_net, "aten::as_strided_copy"
@pytest.mark.parametrize(
"size,stride",
[
([1], [1]),
([2, 2], [1, 1]),
([5, 4, 3], [1, 3, 7]),
([5, 5, 5], [5, 0, 5]),
([1, 2, 3, 4], [4, 3, 2, 1]),
],
)
@pytest.mark.parametrize("offset", [None, 1, 3, 7])
@pytest.mark.precommit_fx_backend
def test_as_strided_copy(self, size, stride, offset, ie_device, precision, ir_version):
self._test(*self.create_model(size, stride, offset), ie_device, precision, ir_version, trace_model=True)
class TestAsStridedListConstruct(PytorchLayerTest):
def _prepare_input(self, size_shape_tensor=[1], stride_shape_tensor=[1]):
return (
np.random.randn(8, 8).astype(np.float32),
np.ones(size_shape_tensor),
np.ones(stride_shape_tensor),
)
def create_model(self, size, stride, offset, mode):
class aten_as_strided(torch.nn.Module):
def __init__(self, size, stride, offset, mode):
super().__init__()
self.size = size
self.stride = stride
self.size_shape_tensor = torch.empty(size)
self.stride_shape_tensor = torch.empty(stride)
self.offset = offset
modes = {
"no_const": self.forward_no_const,
"stride_const": self.forward_stride_const,
"size_const": self.forward_size_const,
}
self.forward = modes.get(mode)
def forward_no_const(self, x, size_shape_tensor, stride_shape_tensor):
sz1, sz2, sz3 = size_shape_tensor.shape
st1, st2, st3 = stride_shape_tensor.shape
return torch.as_strided(x, [sz1, sz2, sz3], [st1, st2, st3], self.offset)
def forward_stride_const(self, x, size_shape_tensor, stride_shape_tensor):
sz1, sz2, sz3 = size_shape_tensor.shape
return torch.as_strided(x, [sz1, sz2, sz3], self.stride, self.offset)
def forward_size_const(self, x, size_shape_tensor, stride_shape_tensor):
st1, st2, st3 = stride_shape_tensor.shape
return torch.as_strided(x, self.size, [st1, st2, st3], self.offset)
ref_net = None
return aten_as_strided(size, stride, offset, mode), ref_net, ["aten::as_strided", "prim::ListConstruct"]
@pytest.mark.parametrize("size,stride", [([5, 4, 3], [1, 3, 7]), ([5, 5, 5], [5, 0, 5])])
@pytest.mark.parametrize("offset", [None, 7])
@pytest.mark.parametrize("mode", ["no_const", "stride_const", "size_const"])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
def test_as_strided_list_construct(self, size, stride, offset, mode, ie_device, precision, ir_version):
inp_kwargs = {"size_shape_tensor": size, "stride_shape_tensor": stride}
self._test(
*self.create_model(size, stride, offset, mode),
ie_device,
precision,
ir_version,
kwargs_to_prepare_input=inp_kwargs,
trace_model=True
)
class TestAsStridedLongformer(PytorchLayerTest):
def _prepare_input(self):
return (np.random.randn(1, 10, 20, 40).astype(np.float32).transpose([0, 2, 3, 1]),)
def create_model(self):
class aten_as_strided_lf(torch.nn.Module):
def forward(self, x):
chunk_size = list(x.size())
chunk_size[1] = chunk_size[1] * 2 - 1
chunk_stride = list(x.stride())
chunk_stride[1] = chunk_stride[1] // 2
return x.as_strided(size=chunk_size, stride=chunk_stride)
ref_net = None
return aten_as_strided_lf(), ref_net, "aten::as_strided"
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
def test_as_strided_lf(self, ie_device, precision, ir_version):
self._test(*self.create_model(), ie_device, precision, ir_version, trace_model=True, freeze_model=False)