-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest_chunk.py
190 lines (157 loc) · 5.89 KB
/
test_chunk.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
# 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 aten_chunk_2(torch.nn.Module):
def __init__(self, dim, unsafe=False) -> None:
torch.nn.Module.__init__(self)
self.dim = dim
self.chunk_op = torch.chunk
if unsafe:
self.chunk_op = torch._VF.unsafe_chunk
def forward(self, input_tensor):
a, b = self.chunk_op(input_tensor,
chunks=2,
dim=self.dim
)
return a, b
class aten_chunk_3(torch.nn.Module):
def __init__(self, dim, unsafe=False) -> None:
torch.nn.Module.__init__(self)
self.dim = dim
self.chunk_op = torch.chunk
if unsafe:
self.chunk_op = torch._VF.unsafe_chunk
def forward(self, input_tensor):
a, b, c = self.chunk_op(input_tensor,
chunks=3,
dim=self.dim
)
return a, b, c
class aten_chunk_4(torch.nn.Module):
def __init__(self, dim, unsafe=False) -> None:
torch.nn.Module.__init__(self)
self.dim = dim
self.chunk_op = torch.chunk
if unsafe:
self.chunk_op = torch._VF.unsafe_chunk
def forward(self, input_tensor):
a, b, c, d = self.chunk_op(input_tensor,
chunks=4,
dim=self.dim
)
return a, b, c, d
class aten_chunk_getitem(torch.nn.Module):
def __init__(self, chunks, dim, idx, unsafe=False) -> None:
torch.nn.Module.__init__(self)
self.chunks = chunks
self.dim = dim
self.idx = idx
self.chunk_op = torch.chunk
if unsafe:
self.chunk_op = torch._VF.unsafe_chunk
def forward(self, input_tensor):
return self.chunk_op(input_tensor,
chunks=self.chunks,
dim=self.dim
)[self.idx]
class TestChunk(PytorchLayerTest):
def _prepare_input(self):
return (np.random.rand(*self.input_shape),)
@pytest.mark.parametrize("input_shape", [
(4, 4),
(5, 9, 7),
(10, 13, 11),
(8, 7, 6, 5, 4),
])
@pytest.mark.parametrize("chunks", [
# Does not work for 1 - no list_unpack present in the graph
# 1,
2,
3,
4
])
@pytest.mark.parametrize("unsafe", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
def test_chunk(self, input_shape, chunks, unsafe, ie_device, precision, ir_version):
self.input_shape = input_shape
for dim, dim_shape in enumerate(input_shape):
chunk_size = dim_shape // chunks
chunk_size += 1 if dim_shape % chunks > 0 else 0
output_chunks = dim_shape // chunk_size
output_chunks += 1 if dim_shape % chunk_size > 0 else 0
if output_chunks == 2:
cls = aten_chunk_2
elif output_chunks == 3:
cls = aten_chunk_3
elif output_chunks == 4:
cls = aten_chunk_4
self._test(cls(dim, unsafe), None,
"aten::unsafe_chunk" if unsafe else "aten::chunk",
ie_device, precision, ir_version, dynamic_shapes=False,
freeze_model=True, trace_model=True)
@pytest.mark.parametrize("input_shape", [
(4, 4),
(10, 13, 11),
(8, 7, 6, 5, 4),
])
@pytest.mark.parametrize("chunks", [
2,
3,
4
])
@pytest.mark.parametrize("unsafe", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
def test_chunk_getitem(self, input_shape, chunks, unsafe, ie_device, precision, ir_version):
self.input_shape = input_shape
for dim, dim_shape in enumerate(input_shape):
chunk_size = dim_shape // chunks
chunk_size += 1 if dim_shape % chunks > 0 else 0
output_chunks = dim_shape // chunk_size
output_chunks += 1 if dim_shape % chunk_size > 0 else 0
for idx in [0, 1, output_chunks - 1]:
self._test(aten_chunk_getitem(chunks, dim, idx, unsafe), None,
"aten::unsafe_chunk" if unsafe else "aten::chunk",
ie_device, precision, ir_version)
class aten_chunk_loop_getitem(torch.nn.Module):
def __init__(self, num_chunks, unsafe=False) -> None:
torch.nn.Module.__init__(self)
self.num_chunks = num_chunks
self.chunk_op = torch.chunk
if unsafe:
self.chunk_op = torch._VF.unsafe_chunk
def forward(self, input_tensor):
x = torch.arange(input_tensor.shape[0])
chunks = self.chunk_op(x, self.num_chunks)
for inds in chunks:
input_tensor[inds] *= 10
return input_tensor
class TestChunkLoopGetitem(PytorchLayerTest):
def _prepare_input(self):
return (np.random.rand(*self.input_shape),)
@pytest.mark.parametrize("input_shape", [
(4, 4),
(5, 9, 7),
(10, 13, 11),
(8, 7, 6, 5, 4),
])
@pytest.mark.parametrize("chunks", [
2,
3,
4
])
@pytest.mark.parametrize("unsafe", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
def test_chunk_loop_getitem(self, input_shape, chunks, unsafe, ie_device, precision, ir_version):
self.input_shape = input_shape
chunk_op = "aten::unsafe_chunk" if unsafe else "aten::chunk"
self._test(aten_chunk_loop_getitem(chunks, unsafe), None,
[chunk_op, "prim::Loop", "aten::__getitem__"],
ie_device, precision, ir_version)