-
Notifications
You must be signed in to change notification settings - Fork 972
Expand file tree
/
Copy pathtest_view.py
More file actions
248 lines (212 loc) · 7.79 KB
/
test_view.py
File metadata and controls
248 lines (212 loc) · 7.79 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Copyright 2024-2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# Tests the view op which changes the size of a Tensor without changing the underlying data.
#
from typing import Tuple
import torch
from executorch.backends.arm.quantizer.arm_quantizer import (
get_symmetric_a16w8_quantization_config,
)
from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.test_pipeline import (
EthosU55PipelineINT,
EthosU85PipelineINT,
OpNotSupportedPipeline,
TosaPipelineFP,
TosaPipelineINT,
VgfPipeline,
)
aten_op = "torch.ops.aten.view.default"
input_t1 = Tuple[torch.Tensor, torch.Tensor] # Input x, Input y
class View(torch.nn.Module):
needs_transpose_tests = {
"rand_1d_neg": lambda: (torch.rand(100), (1, -1, 5, 2)),
"rand_4d_neg": lambda: (torch.rand(10, 2, 1, 5), (1, -1, 5, 2)),
"rand_4d_4d_small": lambda: (torch.rand(1, 2, 1, 9), (3, 1, 3, 2)),
"rand_4d_4d": lambda: (torch.rand(2, 1, 1, 9), (3, 2, 3, 1)),
"rand_4d_2d": lambda: (torch.rand(2, 50, 2, 1), (1, 200)),
"rand_4d_3d": lambda: (torch.rand(2, 5, 2, 3), (1, 15, 4)),
"rand_4d_1": lambda: (torch.rand(2, 1, 1, 9), (3, 1, 3, 2)),
"rand_4d_2": lambda: (torch.rand(5, 10, 1, 1), (25, 2, 1, 1)),
"rand_4d_2_4": lambda: (torch.rand(10, 2), (1, 1, 5, 4)),
"rand_4d_2_4_big": lambda: (torch.rand(10, 10), (5, 1, 5, 4)),
"rand_4d_4_4": lambda: (torch.rand(1, 1, 1, 10), (1, 1, 10, 1)),
"rand_4d_4_4_big": lambda: (torch.rand(1, 1, 5, 10), (1, 1, 50, 1)),
"rand_4d_4_3": lambda: (torch.rand(5, 10, 1, 1), (1, 25, 2)),
"rand_4d_4_2": lambda: (torch.rand(2, 50, 1, 1), (1, 100)),
"rand_4d_2_4_same": lambda: (torch.rand(2, 3, 2, 3), (2, 3, 3, 2)),
"rand_4d_5d": lambda: (torch.rand(1, 3, 4, 5), (1, 1, 4, 5, -1)),
"rand_5d_5d": lambda: (torch.rand(1, 1, 4, 5, 6), (1, 1, 4, -1, 6)),
"rand_5d_3d": lambda: (torch.rand(1, 1, 4, 5, 6), (2, 3, -1)),
"rand_3d_5d": lambda: (torch.rand(4, 5, 6), (1, 1, 2, -1, 3)),
"rank4_rank3_large": lambda: (torch.rand(1, 256, 6, 48), (6, 48, 256)),
"rank5_rank4_large": lambda: (torch.rand(1, 256, 2, 3, 48), (1, 256, 6, 48)),
}
needs_transpose_tests_fp16 = {
"rand_4d_4d_fp16": lambda: (
torch.rand(2, 1, 1, 9, dtype=torch.float16),
(3, 2, 3, 1),
),
"rand_4d_neg_fp16": lambda: (
torch.rand(10, 2, 1, 5, dtype=torch.float16),
(1, -1, 5, 2),
),
}
rank_product_too_large = {
"rand_5d_large": lambda: (torch.rand(2, 256, 512, 8, 64), (2, 512, 256, 8, 64)),
}
def __init__(self, new_shape):
super().__init__()
self.new_shape = new_shape
def forward(self, x: torch.Tensor):
view_op = x.view(self.new_shape)
# Because we treat a single view as a no compute operation and therefore do not partition it,
# we want to provide a mul op to verify that it does indeed get partitioned when bundled with another op.
return view_op * view_op
@common.parametrize(
"test_data", View.needs_transpose_tests | View.needs_transpose_tests_fp16
)
def test_view_tosa_FP(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = TosaPipelineFP[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_op=[],
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
def test_view_tosa_INT(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = TosaPipelineINT[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_op=[],
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
@common.XfailIfNoCorstone300
def test_view_u55_INT(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = EthosU55PipelineINT[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_ops=[],
)
pipeline.change_args(
"check_not.exir", ["executorch_exir_dialects_edge__ops_aten_view_copy_default"]
)
pipeline.run()
@common.parametrize(
"test_data", View.needs_transpose_tests | View.needs_transpose_tests_fp16
)
@common.SkipIfNoModelConverter
def test_view_vgf_no_quant(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = VgfPipeline[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
quantize=False,
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
@common.SkipIfNoModelConverter
def test_view_vgf_quant(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = VgfPipeline[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
quantize=True,
)
pipeline.run()
@common.parametrize("test_data", View.rank_product_too_large)
@common.XfailIfNoCorstone300
def test_view_u55_INT_not_delegated(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = OpNotSupportedPipeline[input_t1](
View(new_shape),
(test_tensor,),
{"executorch_exir_dialects_edge__ops_aten_view_copy": 1},
n_expected_delegates=1,
quantize=True,
u55_subset=True,
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
@common.XfailIfNoCorstone320
def test_view_u85_INT(test_data: Tuple):
test_tensor, new_shape = test_data()
pipeline = EthosU85PipelineINT[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_ops=[],
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
def test_view_16a8w_tosa_INT(test_data: Tuple):
"""Test view operation with 16A8W quantization (16-bit activations, 8-bit
weights)
"""
per_channel_quantization = False
test_tensor, new_shape = test_data()
pipeline = TosaPipelineINT[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_op=[],
per_channel_quantization=per_channel_quantization,
use_to_edge_transform_and_lower=True,
tosa_extensions=["int16"],
)
pipeline.quantizer.set_global(
get_symmetric_a16w8_quantization_config(is_per_channel=per_channel_quantization)
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
@common.XfailIfNoCorstone300
def test_view_16a8w_u55_INT(test_data: Tuple):
"""Test view operation with 16A8W quantization on U55 (16-bit activations,
8-bit weights)
"""
per_channel_quantization = False
test_tensor, new_shape = test_data()
pipeline = EthosU55PipelineINT[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_ops=[],
per_channel_quantization=per_channel_quantization,
use_to_edge_transform_and_lower=True,
)
pipeline.quantizer.set_global(
get_symmetric_a16w8_quantization_config(is_per_channel=per_channel_quantization)
)
pipeline.run()
@common.parametrize("test_data", View.needs_transpose_tests)
@common.XfailIfNoCorstone320
def test_view_16a8w_u85_INT(test_data: Tuple):
"""Test view operation with 16A8W quantization on U85 (16-bit activations,
8-bit weights)
"""
per_channel_quantization = False
test_tensor, new_shape = test_data()
pipeline = EthosU85PipelineINT[input_t1](
View(new_shape),
(test_tensor,),
aten_op,
exir_ops=[],
per_channel_quantization=per_channel_quantization,
use_to_edge_transform_and_lower=True,
)
pipeline.quantizer.set_global(
get_symmetric_a16w8_quantization_config(is_per_channel=per_channel_quantization)
)
pipeline.run()