-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtest_layer_norm.py
More file actions
224 lines (194 loc) · 6.36 KB
/
test_layer_norm.py
File metadata and controls
224 lines (194 loc) · 6.36 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
# 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.
from typing import List, Union
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,
TosaPipelineFP,
TosaPipelineINT,
VgfPipeline,
)
class LayerNorm(torch.nn.Module):
def __init__(
self,
normalized_shape: Union[int, List[int]],
eps: float = 1e-5,
elementwise_affine: bool = True,
has_bias: bool = True,
):
super().__init__()
self.layer_norm = torch.nn.LayerNorm(
normalized_shape,
eps=eps,
elementwise_affine=elementwise_affine,
bias=has_bias,
)
if elementwise_affine:
self.layer_norm.weight = torch.nn.Parameter(torch.ones(normalized_shape))
if has_bias:
self.layer_norm.bias = torch.nn.Parameter(torch.rand(normalized_shape))
def forward(self, x):
return self.layer_norm(x)
input_t = tuple[torch.Tensor]
test_data_suite = {
"randn_last_dim": lambda: ((torch.randn(1, 5, 5, 5),), LayerNorm([5])),
"rand_last_two_dims": lambda: ((torch.rand(1, 5, 5, 5),), LayerNorm([5, 5])),
"rand_last_two_dims_not_elementwise_affine": lambda: (
(torch.rand(1, 5, 5, 5),),
LayerNorm([5, 5], 1e-5, False),
),
"rand_last_two_dims_not_elementwise_affine_no_bias": lambda: (
(torch.rand(1, 5, 5, 5),),
LayerNorm([5, 5], 1e-5, False, False),
),
"randn_last_three_dims": lambda: (
(torch.randn(1, 15, 10, 5),),
LayerNorm([15, 10, 5]),
),
"randn_last_three_dims_no_bias": lambda: (
(torch.randn(1, 15, 10, 5),),
LayerNorm([15, 10, 5], 1e-2, False, False),
),
}
@common.parametrize("test_data", test_data_suite)
def test_native_layer_norm_tosa_FP(test_data):
test_data, model = test_data()
pipeline = TosaPipelineFP[input_t](
model,
test_data,
"torch.ops.aten.layer_norm.default",
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
def test_native_layer_norm_tosa_INT(test_data):
test_data, model = test_data()
pipeline = TosaPipelineINT[input_t](
model,
test_data,
"torch.ops.aten.sub.Tensor", # Just check for sub op included in the layernorm decomposition
symmetric_io_quantization=True,
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@common.XfailIfNoCorstone300
def test_native_layer_norm_u55_INT(test_data):
test_data, model = test_data()
pipeline = EthosU55PipelineINT[input_t](
model,
test_data,
"torch.ops.aten.sub.Tensor", # Just check for sub op included in the layernorm decomposition
symmetric_io_quantization=True,
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@common.XfailIfNoCorstone320
def test_native_layer_norm_u85_INT(test_data):
test_data, model = test_data()
pipeline = EthosU85PipelineINT[input_t](
model,
test_data,
"torch.ops.aten.sub.Tensor", # Just check for sub op included in the layernorm decomposition
symmetric_io_quantization=True,
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@common.SkipIfNoModelConverter
def test_native_layer_norm_vgf_no_quant(test_data):
test_input, model = test_data()
pipeline = VgfPipeline[input_t](
model,
test_input,
"torch.ops.aten.layer_norm.default",
quantize=False,
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@common.SkipIfNoModelConverter
def test_native_layer_norm_vgf_quant(test_data):
test_input, model = test_data()
pipeline = VgfPipeline[input_t](
model,
test_input,
"torch.ops.aten.sub.Tensor",
quantize=True,
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@common.SkipIfNoModelConverter
def test_native_layer_norm_vgf_quant_a16w8(test_data):
test_input, model = test_data()
pipeline = VgfPipeline[input_t](
model,
test_input,
"torch.ops.aten.sub.Tensor",
symmetric_io_quantization=True,
quantize=True,
tosa_extensions=["int16"],
qtol=400,
)
pipeline.quantizer.set_global(
get_symmetric_a16w8_quantization_config(
epsilon=2**-16,
)
)
pipeline.run()
@common.parametrize("test_data", test_data_suite)
def test_native_layer_norm_tosa_INT_a16w8(test_data):
"""Test layer_norm with int16 I/O quantization for TOSA INT."""
test_input, model = test_data()
pipeline = TosaPipelineINT[input_t](
model,
test_input,
"torch.ops.aten.sub.Tensor", # check for sub op in decomposition
symmetric_io_quantization=True,
tosa_extensions=["int16"],
qtol=400,
epsilon=2**-16,
)
pipeline.run()
@common.parametrize(
"test_data",
test_data_suite,
xfails={
"randn_last_dim": "MLETORCH-1834 - 16A8W native_layer_norm output diff for certain configurations."
},
)
@common.XfailIfNoCorstone300
def test_native_layer_norm_16a8w_u55_INT(test_data):
"""Test layer_norm with int16 I/O quantization for U55."""
test_input, model = test_data()
pipeline = EthosU55PipelineINT[input_t](
model,
test_input,
"torch.ops.aten.sub.Tensor",
symmetric_io_quantization=True,
a16w8_quantization=True,
qtol=128,
epsilon=2**-16,
)
pipeline.run()
u85_xfails_16a8w = {
"randn_last_dim": "MLETORCH-1834 - 16A8W native_layer_norm output diff for certain configurations.",
}
@common.parametrize("test_data", test_data_suite, xfails=u85_xfails_16a8w)
@common.XfailIfNoCorstone320
def test_native_layer_norm_16a8w_u85_INT(test_data):
"""Test layer_norm with int16 I/O quantization for U85."""
test_input, model = test_data()
pipeline = EthosU85PipelineINT[input_t](
model,
test_input,
"torch.ops.aten.sub.Tensor",
symmetric_io_quantization=True,
a16w8_quantization=True,
qtol=128,
epsilon=2**-16,
)
pipeline.run()