66
77import re
88from dataclasses import replace
9+ from functools import partial
910
1011import pytest
1112import torch
1213from executorch .backends .espressif .lowering import (
1314 get_esp_fused_quant_recipe ,
1415 get_operator_library_report ,
1516)
17+ from executorch .backends .espressif .passes .fold_activations import FoldActivations
18+ from executorch .backends .espressif .quantizer import get_esp_fused_quant_quantizer
1619from executorch .backends .espressif .test .tester import (
1720 EspTestCase ,
1821 EspTester ,
1922 ramp_tensor ,
2023)
2124from executorch .backends .test .suite .operators .test_linear import Model
22- from executorch .backends .transforms .permute_pass_utils import get_arg
25+ from executorch .backends .transforms .fused_quant .graph_utils import get_qparams_from_node
26+ from executorch .backends .transforms .permute_pass_utils import get_arg , set_arg
2327from executorch .backends .transforms .utils import get_constant
2428from executorch .backends .xnnpack .quantizer .xnnpack_quantizer import (
2529 get_symmetric_quantization_config ,
3438
3539_FQ_LINEAR = exir_ops .edge .fused_quant .linear .default
3640_ESP_LINEAR = exir_ops .edge .esp_nn .quantized_linear_int8 .default
41+ _ESP_LUT = exir_ops .edge .esp_nn .quantized_lut_int8 .default
42+ _ACTIVATIONS = {
43+ "relu" : (torch .nn .ReLU , (- 7 , 127 )),
44+ "relu6" : (torch .nn .ReLU6 , (- 7 , 41 )),
45+ "hardtanh" : (torch .nn .Hardtanh , (- 15 , 1 )),
46+ "hardtanh_asymmetric" : (
47+ partial (torch .nn .Hardtanh , min_val = - 2.0625 , max_val = 3.1875 ),
48+ (- 23 , 19 ),
49+ ),
50+ "hardtanh_positive" : (partial (torch .nn .Hardtanh , min_val = 1 , max_val = 4 ), (1 , 25 )),
51+ "hardtanh_negative" : (
52+ partial (torch .nn .Hardtanh , min_val = - 5 , max_val = - 2 ),
53+ (- 47 , - 23 ),
54+ ),
55+ }
3756
3857
3958class EspLinear (Model ):
@@ -59,16 +78,27 @@ def forward(self, x):
5978 return result , torch .sin (result ), torch .sin (x ), self .linear .bias
6079
6180
62- class EspLinearRelu (EspLinear ):
63- ops_before_transforms = {_FQ_LINEAR : 1 , exir_ops .edge .fused_quant .relu .default : 1 }
64- ops_after_transforms = {
65- _ESP_LINEAR : 1 ,
66- exir_ops .edge .esp_nn .quantized_lut_int8 .default : 1 ,
67- exir_ops .edge .aten .relu .default : 0 ,
68- }
81+ class EspLinearActivation (EspLinear ):
82+ def __init__ (self , activation , * , bias = True , fork = False ):
83+ super ().__init__ (bias = bias )
84+ self .activation = _ACTIVATIONS [activation ][0 ]()
85+ self .fused_activation = (
86+ exir_ops .edge .fused_quant .relu .default
87+ if activation == "relu"
88+ else exir_ops .edge .fused_quant .hard_tanh .default
89+ )
90+ self .fork = fork
91+ self .ops_before_transforms = {_FQ_LINEAR : 1 , self .fused_activation : 1 }
92+ self .ops_after_transforms = {
93+ _ESP_LINEAR : 1 ,
94+ self .fused_activation : 0 ,
95+ _ESP_LUT : int (fork ),
96+ }
6997
7098 def forward (self , x ):
71- return torch .relu (self .linear (x ))
99+ result = self .linear (x )
100+ activated = self .activation (result )
101+ return (activated , result , torch .sin (result )) if self .fork else activated
72102
73103
74104class EspSharedLinear (EspLinear ):
@@ -115,7 +145,6 @@ def __init__(self):
115145 "float_users" : EspTestCase (
116146 EspLinearWithFloatUsers (), (ramp_tensor (- 3 , 5 , (2 , 17 )),)
117147 ),
118- "relu" : EspTestCase (EspLinearRelu (), (ramp_tensor (- 3 , 5 , (2 , 17 )),)),
119148 "shared_bias" : EspTestCase (
120149 EspSharedLinear (), (ramp_tensor (- 3 , 5 , (2 , 17 )), ramp_tensor (- 24 , 40 , (2 , 17 )))
121150 ),
@@ -127,6 +156,38 @@ def __init__(self):
127156 calibration_samples = [(ramp_tensor (- 3 , 5 , (2 , 17 )),)],
128157 ),
129158}
159+ test_cases .update (
160+ {
161+ f"{ activation } { '_no_bias' if not bias else '' } " : EspTestCase (
162+ EspLinearActivation (activation , bias = bias ),
163+ (ramp_tensor (- 30 , 50 , (5 , 17 )),),
164+ )
165+ for activation in _ACTIVATIONS
166+ for bias in (True , False )
167+ }
168+ )
169+ test_cases .update (
170+ {
171+ "relu_fork" : EspTestCase (
172+ EspLinearActivation ("relu" , fork = True ), (ramp_tensor (- 3 , 5 , (2 , 17 )),)
173+ ),
174+ "hardtanh_fork" : EspTestCase (
175+ EspLinearActivation ("hardtanh" , fork = True ),
176+ (ramp_tensor (- 3 , 5 , (2 , 17 )),),
177+ ),
178+ "relu6_vector" : EspTestCase (
179+ EspLinearActivation ("relu6" ), (ramp_tensor (- 30 , 50 , (17 ,)),)
180+ ),
181+ "hardtanh_rank_3" : EspTestCase (
182+ EspLinearActivation ("hardtanh" ), (ramp_tensor (- 30 , 50 , (2 , 3 , 17 )),)
183+ ),
184+ "relu6_saturation" : EspTestCase (
185+ EspLinearActivation ("relu6" ),
186+ (ramp_tensor (- 30 , 50 , (2 , 17 )),),
187+ calibration_samples = [(ramp_tensor (- 3 , 5 , (2 , 17 )),)],
188+ ),
189+ }
190+ )
130191
131192
132193@pytest .mark .parametrize ("test_case" , test_cases .values (), ids = test_cases .keys ())
@@ -158,7 +219,7 @@ def test_implementation_linear(test_case, pytestconfig):
158219 test_case .model , test_case .example_inputs , test_case .calibration_samples
159220 )
160221 tester .test_implementation (
161- pytestconfig .rootpath , rtol = torch .finfo (torch .float32 ).eps
222+ pytestconfig .rootpath , rtol = torch .finfo (torch .float32 ).eps , num_inferences = 2
162223 )
163224
164225
@@ -177,7 +238,7 @@ def test_public_recipe_linear():
177238 assert session .get_executorch_program_manager ().buffer
178239
179240
180- @pytest .mark .parametrize ("case_name" , ["shared_bias" , "large_bias" ])
241+ @pytest .mark .parametrize ("case_name" , ["shared_bias" , "large_bias" , "relu" , "hardtanh" ])
181242def test_lowering_reuses_quantized_bias_and_preserves_source_program (case_name ):
182243 case = test_cases [case_name ]
183244 tester = EspTester (case .model , case .example_inputs ).export_fused_quant ().to_edge ()
@@ -199,6 +260,79 @@ def test_lowering_reuses_quantized_bias_and_preserves_source_program(case_name):
199260 source .validate ()
200261
201262
263+ @pytest .mark .parametrize ("activation" , ["relu" , "relu6" , "hardtanh" ])
264+ @pytest .mark .parametrize ("fork" , [False , True ])
265+ def test_linear_activation_quantization_boundary (activation , fork ):
266+ model = EspLinearActivation (activation , fork = fork )
267+ tester = EspTester (model , (ramp_tensor (- 3 , 5 , (2 , 17 )),))
268+ tester .export_fused_quant ().to_edge ()
269+ graph = tester .get_artifact ().exported_program ().graph
270+ (producer ,) = graph .find_nodes (op = "call_function" , target = _FQ_LINEAR )
271+ (activation_node ,) = graph .find_nodes (
272+ op = "call_function" , target = model .fused_activation
273+ )
274+ assert (get_qparams_from_node (producer , "out" ) is not None ) == fork
275+ assert (get_qparams_from_node (activation_node , "inp" ) is not None ) == fork
276+ assert get_qparams_from_node (activation_node , "out" ) is not None
277+
278+
279+ @pytest .mark .parametrize ("activation" , ["relu" , "hardtanh" ])
280+ def test_separately_quantized_linear_activation_keeps_rounding (activation ):
281+ model = EspLinearActivation (activation )
282+ quantizer = FusedQuantQuantizer (get_esp_fused_quant_quantizer ().child_quantizers )
283+ tester = EspTester (model , (ramp_tensor (- 3 , 5 , (2 , 17 )),))
284+ tester .export_fused_quant (quantizer ).to_edge ()
285+ assert not FoldActivations ()(tester .get_artifact ().exported_program ()).modified
286+ tester .run_passes ().check_node_count ({_ESP_LINEAR : 1 , _ESP_LUT : 1 })
287+
288+
289+ def _linear_activation_clamp_tester (activation ):
290+ model = EspLinearActivation (activation )
291+ tester = EspTester (model , (ramp_tensor (- 80 , 80 , (9 , 17 )),))
292+ tester .export_fused_quant ().to_edge ()
293+ program = tester .get_artifact ().exported_program ()
294+ (node ,) = program .graph .find_nodes (
295+ op = "call_function" , target = model .fused_activation
296+ )
297+ params = get_qparams_from_node (node , "out" )
298+ get_constant (program , params .scale ).fill_ (0.125 )
299+ get_constant (program , params .zero_point ).fill_ (- 7 )
300+ (output ,) = program .graph .find_nodes (op = "output" )
301+ dequant = output .args [0 ][0 ]
302+ set_arg (dequant , "scale" , 0.125 )
303+ set_arg (dequant , "zero_point" , - 7 )
304+ program .graph_module .recompile ()
305+ return tester
306+
307+
308+ @pytest .mark .parametrize ("activation" , _ACTIVATIONS )
309+ def test_fold_linear_uses_final_qparams_and_clamp (activation ):
310+ tester = _linear_activation_clamp_tester (activation )
311+ program = tester .get_artifact ().exported_program ()
312+ expected = program .module ()(* tester .example_inputs )
313+ assert FoldActivations ()(program ).modified
314+ torch .testing .assert_close (
315+ program .module ()(* tester .example_inputs ), expected , atol = 0 , rtol = 0
316+ )
317+ tester .run_passes ().check_node_count ({_ESP_LINEAR : 1 , _ESP_LUT : 0 })
318+ lowered = tester .get_artifact ().exported_program ()
319+ (node ,) = lowered .graph .find_nodes (op = "call_function" , target = _ESP_LINEAR )
320+ assert get_arg (node , "output_offset" ) == - 7
321+ assert (get_arg (node , "activation_min" ), get_arg (node , "activation_max" )) == (
322+ _ACTIVATIONS [activation ][1 ]
323+ )
324+ torch .testing .assert_close (
325+ lowered .module ()(* tester .example_inputs ), expected , atol = 0.125 , rtol = 0
326+ )
327+
328+
329+ @pytest .mark .parametrize ("activation" , _ACTIVATIONS )
330+ def test_implementation_linear_activation_clamp (activation , pytestconfig ):
331+ tester = _linear_activation_clamp_tester (activation )
332+ tester .run_passes ()
333+ tester .test_implementation (pytestconfig .rootpath , num_inferences = 2 )
334+
335+
202336class RuntimeBias (EspLinear ):
203337 def forward (self , x , bias ):
204338 return torch .nn .functional .linear (x , self .linear .weight , bias )
0 commit comments