-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathtest_sigmoid_32bit.py
178 lines (149 loc) · 5.43 KB
/
test_sigmoid_32bit.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
# Copyright 2025 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.
import pytest
import torch
from executorch.backends.arm.quantizer.arm_quantizer import TOSAQuantizer
from executorch.backends.arm.quantizer.quantization_config import QuantizationConfig
from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.test_pipeline import (
EthosU85PipelineBI,
OpNotSupportedPipeline,
TosaPipelineBI,
)
from executorch.backends.xnnpack.test.tester import Quantize
from torchao.quantization.pt2e.observer import HistogramObserver
from torchao.quantization.pt2e.quantizer import QuantizationSpec
def _get_16_bit_quant_config():
int16_spec = QuantizationSpec(
dtype=torch.int16,
observer_or_fake_quant_ctr=HistogramObserver,
qscheme=torch.per_tensor_symmetric,
)
int32_spec = QuantizationSpec(
dtype=torch.int32,
observer_or_fake_quant_ctr=HistogramObserver,
qscheme=torch.per_tensor_symmetric,
)
qconfig = QuantizationConfig(
input_activation=int16_spec,
output_activation=int32_spec,
weight=None,
bias=None,
)
return qconfig
def _get_32_bit_quant_config():
int32_spec = QuantizationSpec(
dtype=torch.int32,
observer_or_fake_quant_ctr=HistogramObserver,
qscheme=torch.per_tensor_symmetric,
)
qconfig = QuantizationConfig(
input_activation=int32_spec,
output_activation=int32_spec,
weight=None,
bias=None,
)
return qconfig
def get_32bit_sigmoid_quantizer(tosa_str: str):
tosa_spec = common.TosaSpecification.create_from_string(tosa_str)
quantizer = TOSAQuantizer(tosa_spec)
quantizer.set_global(_get_32_bit_quant_config())
quantizer.set_module_type(
torch.nn.modules.activation.Sigmoid, _get_16_bit_quant_config()
)
return Quantize(quantizer, _get_32_bit_quant_config())
input_t = tuple[torch.Tensor]
test_data_suite = {
"ones": lambda: torch.ones(10, 10, 10),
"rand": lambda: torch.rand(10, 10) - 0.5,
"rand_4d": lambda: torch.rand(1, 10, 10, 10),
"randn_pos": lambda: torch.randn(10) + 10,
"randn_neg": lambda: torch.randn(10) - 10,
"ramp": lambda: torch.arange(-16, 16, 0.2),
}
class Sigmoid(torch.nn.Module):
aten_op = "torch.ops.aten.sigmoid.default"
exir_op = "executorch_exir_dialects_edge__ops_aten_sigmoid_default"
def __init__(self):
super().__init__()
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
return self.sigmoid(x)
class SigmoidAddSigmoid(torch.nn.Module):
def __init__(self):
super().__init__()
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
return self.sigmoid((self.sigmoid(x) + self.sigmoid(x)))
@common.parametrize("test_data", test_data_suite)
@pytest.mark.flaky(reruns=32) # Flaky due to Vela bug: MLBEDSW-10642
def test_sigmoid_tosa_BI(test_data):
pipeline = TosaPipelineBI(
Sigmoid(),
(test_data(),),
Sigmoid.aten_op,
Sigmoid.exir_op,
)
pipeline.change_args("quantize", get_32bit_sigmoid_quantizer("TOSA-0.80+BI"))
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@pytest.mark.flaky(reruns=32) # Flaky due to Vela bug: MLBEDSW-10642
def test_sigmoid_add_sigmoid_tosa_BI(test_data):
pipeline = TosaPipelineBI(
SigmoidAddSigmoid(),
(test_data(),),
Sigmoid.aten_op,
Sigmoid.exir_op,
)
pipeline.change_args("quantize", get_32bit_sigmoid_quantizer("TOSA-0.80+BI"))
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@pytest.mark.flaky(reruns=32) # Flaky due to Vela bug: MLBEDSW-10642
def test_sigmoid_tosa_u55(test_data):
pipeline = OpNotSupportedPipeline(
Sigmoid(), (test_data(),), "TOSA-0.80+BI+u55", {Sigmoid.exir_op: 1}
)
pipeline.change_args("quantize", get_32bit_sigmoid_quantizer("TOSA-0.80+BI+u55"))
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@pytest.mark.flaky(reruns=32) # Flaky due to Vela bug: MLBEDSW-10642
def test_sigmoid_add_sigmoid_tosa_u55(test_data):
pipeline = OpNotSupportedPipeline(
SigmoidAddSigmoid(),
(test_data(),),
"TOSA-0.80+BI+u55",
{Sigmoid.exir_op: 3},
n_expected_delegates=1,
)
pipeline.change_args("quantize", get_32bit_sigmoid_quantizer("TOSA-0.80+BI+u55"))
pipeline.run()
@common.parametrize("test_data", test_data_suite)
@pytest.mark.flaky(reruns=32) # Flaky due to Vela bug: MLBEDSW-10642
@common.XfailIfNoCorstone320
def test_sigmoid_tosa_u85(test_data):
pipeline = EthosU85PipelineBI(
Sigmoid(), (test_data(),), Sigmoid.aten_op, Sigmoid.exir_op, run_on_fvp=True
)
pipeline.change_args("quantize", get_32bit_sigmoid_quantizer("TOSA-0.80+BI"))
pipeline.run()
@common.parametrize(
"test_data",
test_data_suite,
xfails={
"ramp": "AssertionError: Output 0 does not match reference output.",
},
)
@pytest.mark.flaky(reruns=32) # Flaky due to Vela bug: MLBEDSW-10642
@common.XfailIfNoCorstone320
def test_sigmoid_add_sigmoid_tosa_u85(test_data):
pipeline = EthosU85PipelineBI(
SigmoidAddSigmoid(),
(test_data(),),
Sigmoid.aten_op,
Sigmoid.exir_op,
run_on_fvp=True,
)
pipeline.change_args("quantize", get_32bit_sigmoid_quantizer("TOSA-0.80+BI"))
pipeline.run()