55# LICENSE file in the root directory of this source tree.
66
77from dataclasses import replace
8+ from functools import partial
89
910import pytest
1011import torch
1112from executorch .backends .espressif .lowering import (
1213 get_esp_fused_quant_recipe ,
1314 get_operator_library_report ,
1415)
16+ from executorch .backends .espressif .passes .fold_activations import FoldActivations
17+ from executorch .backends .espressif .quantizer import get_esp_fused_quant_quantizer
1518from executorch .backends .espressif .test .tester import (
1619 EspTestCase ,
1720 EspTester ,
1821 ramp_tensor ,
1922)
2023from executorch .backends .test .suite .operators .test_linear import Model
21- from executorch .backends .transforms .permute_pass_utils import get_arg
24+ from executorch .backends .transforms .fused_quant .graph_utils import get_qparams_from_node
25+ from executorch .backends .transforms .permute_pass_utils import get_arg , set_arg
2226from executorch .backends .transforms .utils import get_constant
2327from executorch .backends .xnnpack .quantizer .xnnpack_quantizer import (
2428 get_symmetric_quantization_config ,
3337
3438_FQ_LINEAR = exir_ops .edge .fused_quant .linear .default
3539_ESP_LINEAR = exir_ops .edge .esp_nn .quantized_linear_int8 .default
40+ _ESP_LUT = exir_ops .edge .esp_nn .quantized_lut_int8 .default
41+ _ACTIVATIONS = {
42+ "relu" : (torch .nn .ReLU , (- 7 , 127 )),
43+ "relu6" : (torch .nn .ReLU6 , (- 7 , 41 )),
44+ "hardtanh" : (torch .nn .Hardtanh , (- 15 , 1 )),
45+ "hardtanh_asymmetric" : (
46+ partial (torch .nn .Hardtanh , min_val = - 2.0625 , max_val = 3.1875 ),
47+ (- 23 , 19 ),
48+ ),
49+ "hardtanh_positive" : (partial (torch .nn .Hardtanh , min_val = 1 , max_val = 4 ), (1 , 25 )),
50+ "hardtanh_negative" : (
51+ partial (torch .nn .Hardtanh , min_val = - 5 , max_val = - 2 ),
52+ (- 47 , - 23 ),
53+ ),
54+ }
3655
3756
3857class EspLinear (Model ):
@@ -58,16 +77,27 @@ def forward(self, x):
5877 return result , torch .sin (result ), torch .sin (x ), self .linear .bias
5978
6079
61- class EspLinearRelu (EspLinear ):
62- ops_before_transforms = {_FQ_LINEAR : 1 , exir_ops .edge .fused_quant .relu .default : 1 }
63- ops_after_transforms = {
64- _ESP_LINEAR : 1 ,
65- exir_ops .edge .esp_nn .quantized_lut_int8 .default : 1 ,
66- exir_ops .edge .aten .relu .default : 0 ,
67- }
80+ class EspLinearActivation (EspLinear ):
81+ def __init__ (self , activation , * , bias = True , fork = False ):
82+ super ().__init__ (bias = bias )
83+ self .activation = _ACTIVATIONS [activation ][0 ]()
84+ self .fused_activation = (
85+ exir_ops .edge .fused_quant .relu .default
86+ if activation == "relu"
87+ else exir_ops .edge .fused_quant .hard_tanh .default
88+ )
89+ self .fork = fork
90+ self .ops_before_transforms = {_FQ_LINEAR : 1 , self .fused_activation : 1 }
91+ self .ops_after_transforms = {
92+ _ESP_LINEAR : 1 ,
93+ self .fused_activation : 0 ,
94+ _ESP_LUT : int (fork ),
95+ }
6896
6997 def forward (self , x ):
70- return torch .relu (self .linear (x ))
98+ result = self .linear (x )
99+ activated = self .activation (result )
100+ return (activated , result , torch .sin (result )) if self .fork else activated
71101
72102
73103class EspSharedLinear (EspLinear ):
@@ -132,7 +162,6 @@ def forward(self, x, weight):
132162 "float_users" : EspTestCase (
133163 EspLinearWithFloatUsers (), (ramp_tensor (- 3 , 5 , (2 , 17 )),)
134164 ),
135- "relu" : EspTestCase (EspLinearRelu (), (ramp_tensor (- 3 , 5 , (2 , 17 )),)),
136165 "shared_bias" : EspTestCase (
137166 EspSharedLinear (), (ramp_tensor (- 3 , 5 , (2 , 17 )), ramp_tensor (- 24 , 40 , (2 , 17 )))
138167 ),
@@ -152,6 +181,38 @@ def forward(self, x, weight):
152181 calibration_samples = [(ramp_tensor (- 3 , 5 , (2 , 17 )),)],
153182 ),
154183}
184+ test_cases .update (
185+ {
186+ f"{ activation } { '_no_bias' if not bias else '' } " : EspTestCase (
187+ EspLinearActivation (activation , bias = bias ),
188+ (ramp_tensor (- 30 , 50 , (5 , 17 )),),
189+ )
190+ for activation in _ACTIVATIONS
191+ for bias in (True , False )
192+ }
193+ )
194+ test_cases .update (
195+ {
196+ "relu_fork" : EspTestCase (
197+ EspLinearActivation ("relu" , fork = True ), (ramp_tensor (- 3 , 5 , (2 , 17 )),)
198+ ),
199+ "hardtanh_fork" : EspTestCase (
200+ EspLinearActivation ("hardtanh" , fork = True ),
201+ (ramp_tensor (- 3 , 5 , (2 , 17 )),),
202+ ),
203+ "relu6_vector" : EspTestCase (
204+ EspLinearActivation ("relu6" ), (ramp_tensor (- 30 , 50 , (17 ,)),)
205+ ),
206+ "hardtanh_rank_3" : EspTestCase (
207+ EspLinearActivation ("hardtanh" ), (ramp_tensor (- 30 , 50 , (2 , 3 , 17 )),)
208+ ),
209+ "relu6_saturation" : EspTestCase (
210+ EspLinearActivation ("relu6" ),
211+ (ramp_tensor (- 30 , 50 , (2 , 17 )),),
212+ calibration_samples = [(ramp_tensor (- 3 , 5 , (2 , 17 )),)],
213+ ),
214+ }
215+ )
155216
156217
157218@pytest .mark .parametrize ("test_case" , test_cases .values (), ids = test_cases .keys ())
@@ -185,7 +246,7 @@ def test_implementation_linear(test_case, pytestconfig):
185246 test_case .model , test_case .example_inputs , test_case .calibration_samples
186247 )
187248 tester .test_implementation (
188- pytestconfig .rootpath , rtol = torch .finfo (torch .float32 ).eps
249+ pytestconfig .rootpath , rtol = torch .finfo (torch .float32 ).eps , num_inferences = 2
189250 )
190251
191252
@@ -204,7 +265,7 @@ def test_public_recipe_linear():
204265 assert session .get_executorch_program_manager ().buffer
205266
206267
207- @pytest .mark .parametrize ("case_name" , ["shared_bias" , "large_bias" ])
268+ @pytest .mark .parametrize ("case_name" , ["shared_bias" , "large_bias" , "relu" , "hardtanh" ])
208269def test_lowering_reuses_quantized_bias_and_preserves_source_program (case_name ):
209270 case = test_cases [case_name ]
210271 tester = EspTester (case .model , case .example_inputs ).export_fused_quant ().to_edge ()
@@ -226,6 +287,79 @@ def test_lowering_reuses_quantized_bias_and_preserves_source_program(case_name):
226287 source .validate ()
227288
228289
290+ @pytest .mark .parametrize ("activation" , ["relu" , "relu6" , "hardtanh" ])
291+ @pytest .mark .parametrize ("fork" , [False , True ])
292+ def test_linear_activation_quantization_boundary (activation , fork ):
293+ model = EspLinearActivation (activation , fork = fork )
294+ tester = EspTester (model , (ramp_tensor (- 3 , 5 , (2 , 17 )),))
295+ tester .export_fused_quant ().to_edge ()
296+ graph = tester .get_artifact ().exported_program ().graph
297+ (producer ,) = graph .find_nodes (op = "call_function" , target = _FQ_LINEAR )
298+ (activation_node ,) = graph .find_nodes (
299+ op = "call_function" , target = model .fused_activation
300+ )
301+ assert (get_qparams_from_node (producer , "out" ) is not None ) == fork
302+ assert (get_qparams_from_node (activation_node , "inp" ) is not None ) == fork
303+ assert get_qparams_from_node (activation_node , "out" ) is not None
304+
305+
306+ @pytest .mark .parametrize ("activation" , ["relu" , "hardtanh" ])
307+ def test_separately_quantized_linear_activation_keeps_rounding (activation ):
308+ model = EspLinearActivation (activation )
309+ quantizer = FusedQuantQuantizer (get_esp_fused_quant_quantizer ().child_quantizers )
310+ tester = EspTester (model , (ramp_tensor (- 3 , 5 , (2 , 17 )),))
311+ tester .export_fused_quant (quantizer ).to_edge ()
312+ assert not FoldActivations ()(tester .get_artifact ().exported_program ()).modified
313+ tester .run_passes ().check_node_count ({_ESP_LINEAR : 1 , _ESP_LUT : 1 })
314+
315+
316+ def _linear_activation_clamp_tester (activation ):
317+ model = EspLinearActivation (activation )
318+ tester = EspTester (model , (ramp_tensor (- 80 , 80 , (9 , 17 )),))
319+ tester .export_fused_quant ().to_edge ()
320+ program = tester .get_artifact ().exported_program ()
321+ (node ,) = program .graph .find_nodes (
322+ op = "call_function" , target = model .fused_activation
323+ )
324+ params = get_qparams_from_node (node , "out" )
325+ get_constant (program , params .scale ).fill_ (0.125 )
326+ get_constant (program , params .zero_point ).fill_ (- 7 )
327+ (output ,) = program .graph .find_nodes (op = "output" )
328+ dequant = output .args [0 ][0 ]
329+ set_arg (dequant , "scale" , 0.125 )
330+ set_arg (dequant , "zero_point" , - 7 )
331+ program .graph_module .recompile ()
332+ return tester
333+
334+
335+ @pytest .mark .parametrize ("activation" , _ACTIVATIONS )
336+ def test_fold_linear_uses_final_qparams_and_clamp (activation ):
337+ tester = _linear_activation_clamp_tester (activation )
338+ program = tester .get_artifact ().exported_program ()
339+ expected = program .module ()(* tester .example_inputs )
340+ assert FoldActivations ()(program ).modified
341+ torch .testing .assert_close (
342+ program .module ()(* tester .example_inputs ), expected , atol = 0 , rtol = 0
343+ )
344+ tester .run_passes ().check_node_count ({_ESP_LINEAR : 1 , _ESP_LUT : 0 })
345+ lowered = tester .get_artifact ().exported_program ()
346+ (node ,) = lowered .graph .find_nodes (op = "call_function" , target = _ESP_LINEAR )
347+ assert get_arg (node , "output_offset" ) == - 7
348+ assert (get_arg (node , "activation_min" ), get_arg (node , "activation_max" )) == (
349+ _ACTIVATIONS [activation ][1 ]
350+ )
351+ torch .testing .assert_close (
352+ lowered .module ()(* tester .example_inputs ), expected , atol = 0.125 , rtol = 0
353+ )
354+
355+
356+ @pytest .mark .parametrize ("activation" , _ACTIVATIONS )
357+ def test_implementation_linear_activation_clamp (activation , pytestconfig ):
358+ tester = _linear_activation_clamp_tester (activation )
359+ tester .run_passes ()
360+ tester .test_implementation (pytestconfig .rootpath , num_inferences = 2 )
361+
362+
229363@pytest .mark .parametrize (
230364 "model, inputs" ,
231365 [
0 commit comments