77import pytest
88import torch
99
10- from executorch .backends .nxp .backend .edge_program_converter import (
11- EdgeProgramToIRConverter ,
12- )
1310from executorch .backends .nxp .tests .executorch_pipeline import to_quantized_edge_program
14- from executorch .backends .nxp .tests .executors import (
15- convert_run_compare ,
16- graph_contains_any_of_ops ,
17- ToChannelFirstPreprocess ,
18- ToChannelLastPreprocess ,
19- )
11+ from executorch .backends .nxp .tests .executors import graph_contains_any_of_ops
2012
2113from executorch .backends .nxp .tests .graph_verifier import DetailedGraphVerifier
2214
@@ -55,164 +47,7 @@ def assert_softmax_not_delegated(graph):
5547 assert graph_contains_any_of_ops (graph , [Softmax ])
5648
5749
58- def random_input_data (input_shape ):
59- return (np .random .random (input_shape ).astype (np .float32 ) * 256.0 - 128.0 ).astype (
60- np .int8
61- )
62-
63-
64- @pytest .mark .parametrize (
65- "input_shape, dim" ,
66- [
67- # Dim must always be the last dimension, which must be a multiple of 8 (num_macs).
68- pytest .param ((4096 , 128 ), - 1 , id = "2D_total_size_limit" ),
69- pytest .param ((5 , 8 ), - 1 , id = "2D_dim_-1" ),
70- pytest .param ((5 , 8 ), 1 , id = "2D_dim_1" ),
71- pytest .param ((4096 , 8 ), - 1 , id = "2D_WxH_limit" ),
72- pytest .param ((2 , 2048 - 8 ), - 1 , id = "2D_channels_limit" ),
73- pytest .param ((5 , 4 , 8 ), - 1 , id = "3D_dim_-1" ),
74- pytest .param ((4096 , 1 , 8 ), - 1 , id = "3D_WxH_limit" ),
75- pytest .param ((5 , 4 , 3 , 8 ), - 1 , id = "4D_dim_-1" ),
76- pytest .param ((1 , 64 , 64 , 8 ), - 1 , id = "4D_WxH_limit" ),
77- pytest .param ((64 , 1 , 64 , 128 ), - 1 , id = "4D_total_size_limit" ),
78- pytest .param ((5 , 4 , 3 , 2 , 8 ), - 1 , id = "5D_dim_-1" ),
79- ],
80- )
81- def test_softmax_delegation (input_shape , dim : int , mocker ):
82- model = SoftmaxModule (dim )
83-
84- converter_spy = mocker .spy (EdgeProgramToIRConverter , "convert_program" )
85- delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
86-
87- assert_softmax_delegated (delegated_ep .graph )
88-
89- # Verify correct behavior of the converted NeutronIR model.
90- intermediate_ep = converter_spy .call_args .args [1 ]
91- neutron_ir_model , _ = converter_spy .spy_return
92- input_data = random_input_data (input_shape )
93-
94- # Make sure the tested program contains the `softmax`, and its input has the expected rank.
95- nodes = list (intermediate_ep .graph .nodes )
96- assert nodes [2 ].target == Softmax
97- assert len (nodes [2 ].args [0 ].meta ["val" ].shape ) == len (input_shape )
98-
99- convert_run_compare (
100- intermediate_ep ,
101- tfl_model = neutron_ir_model ,
102- input_data = input_data ,
103- )
104-
105-
106- @pytest .mark .parametrize (
107- "input_shape,dim" ,
108- [
109- # `dim` must be the second dimension, which must be a multiple of 8 (num_macs).
110- pytest .param ((1 , 8 , 2 , 3 ), 1 , id = "4D_dim_1" ),
111- pytest .param ((1 , 8 , 64 , 64 ), 1 , id = "4D_WxH_limit" ),
112- pytest .param ((64 , 128 , 1 , 64 ), - 3 , id = "4D_dim_-3_total_size_limit" ),
113- ],
114- )
115- def test_softmax_delegation__channel_first (input_shape , dim : int , mocker ):
116- model = ConvSoftmaxModule (dim , input_shape [1 ])
117-
118- converter_spy = mocker .spy (EdgeProgramToIRConverter , "convert_program" )
119- delegated_ep = to_quantized_edge_program (
120- model , input_shape , use_neutron_for_format_conversion = False
121- ).exported_program ()
122-
123- assert_softmax_delegated (delegated_ep .graph )
124-
125- # Verify correct behavior of the converted NeutronIR model.
126- intermediate_ep = converter_spy .call_args .args [1 ]
127- neutron_ir_model , _ = converter_spy .spy_return
128- input_data = random_input_data (input_shape )
129-
130- # Make sure the tested program contains the `softmax`.
131- assert graph_contains_any_of_ops (intermediate_ep .graph , [Softmax ])
132-
133- convert_run_compare (
134- intermediate_ep ,
135- tfl_model = neutron_ir_model ,
136- input_data = input_data ,
137- tflite_input_preprocess = ToChannelLastPreprocess (),
138- tflite_output_preprocess = ToChannelFirstPreprocess (),
139- )
140-
141-
142- @pytest .mark .parametrize (
143- "input_shape,dim" ,
144- [
145- # `dim` is not the last dimension.
146- pytest .param ((10 , 32 ), 0 , id = "2D_dim_0" ),
147- pytest .param ((10 , 32 , 32 ), 1 , id = "3D_dim_1" ),
148- pytest .param ((10 , 32 , 32 , 8 ), 2 , id = "4D_dim_2" ),
149- pytest .param ((10 , 32 , 32 , 8 , 8 ), 3 , id = "5D_dim_3" ),
150- pytest .param ((10 , 32 , 32 , 8 , 8 ), 2 , id = "5D_dim_2" ),
151- ],
152- )
153- def test_softmax_delegation__unsupported_dims (input_shape , dim : int ):
154- model = SoftmaxModule (dim )
155- delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
156- assert_softmax_not_delegated (delegated_ep .graph )
157-
158-
159- @pytest .mark .parametrize (
160- "input_shape,dim" ,
161- [
162- # `dim` is not the second dimension.
163- pytest .param ((10 , 32 , 32 , 8 ), 2 , id = "dim_2" ),
164- pytest .param ((10 , 32 , 32 , 8 ), - 1 , id = "dim_-1" ),
165- pytest .param ((10 , 32 , 32 , 8 ), 3 , id = "dim_3" ),
166- ],
167- )
168- def test_softmax_delegation__unsupported_dims__channels_first (input_shape , dim : int ):
169- model = SoftmaxModule (dim )
170- delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
171- assert_softmax_not_delegated (delegated_ep .graph )
172-
173-
174- @pytest .mark .parametrize (
175- "input_shape,dim" ,
176- [
177- pytest .param ((4096 + 1 , 8 ), - 1 , id = "2D_WxH_exceeded" ),
178- pytest .param ((4096 , 2 , 8 ), - 1 , id = "3D_WxH_exceeded" ),
179- pytest .param ((2 , 64 , 64 , 8 ), - 1 , id = "4D_WxH_exceeded" ),
180- pytest .param ((1 , 2048 ), - 1 , id = "2D_channels_exceeded" ),
181- pytest .param ((4096 , 128 + 8 ), - 1 , id = "2D_total_size_exceeded" ),
182- pytest .param ((64 , 1 , 64 , 128 + 8 ), - 1 , id = "4D_total_size_exceeded" ),
183- ],
184- )
185- def test_softmax_delegation__unsupported_dimension_sizes (input_shape , dim : int ):
186- model = SoftmaxModule (dim )
187- delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
188- assert_softmax_not_delegated (delegated_ep .graph )
189-
190-
191- @pytest .mark .parametrize (
192- "input_shape,dim" ,
193- [
194- pytest .param ((2 , 8 , 64 , 64 ), - 1 , id = "4D_WxH_exceeded" ),
195- pytest .param ((64 , 128 + 8 , 1 , 64 ), - 1 , id = "4D_total_size_exceeded" ),
196- ],
197- )
198- def test_softmax_delegation__unsupported_dimension_sizes__channels_first (
199- input_shape , dim : int
200- ):
201- model = ConvSoftmaxModule (dim , input_shape [1 ])
202- delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
203- assert_softmax_not_delegated (delegated_ep .graph )
204-
205-
206- def test_softmax_delegation__1d ():
207- input_shape = (8 ,)
208- dim = 0
209-
210- model = SoftmaxModule (dim )
211- delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
212- assert_softmax_not_delegated (delegated_ep .graph )
213-
214-
215- class TestSoftmaxNewNeutronFlow :
50+ class TestSoftmax :
21651 @pytest .mark .parametrize (
21752 "input_shape, dim" ,
21853 [
@@ -238,7 +73,6 @@ def test__basic_nsys_inference(self, mocker, input_shape, dim):
23873 model ,
23974 input_shape ,
24075 graph_verifier ,
241- use_new_flow_neutron_c = True ,
24276 output_comparator = output_comparator ,
24377 )
24478
@@ -254,9 +88,7 @@ def test__basic_nsys_inference(self, mocker, input_shape, dim):
25488 )
25589 def test__limits (self , input_shape , dim , mocker ):
25690 model = SoftmaxModule (dim )
257- delegated_ep = to_quantized_edge_program (
258- model , input_shape , use_new_flow_neutron_c = True
259- ).exported_program ()
91+ delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
26092
26193 # Make sure the `softmax` was delegated.
26294 assert_softmax_delegated (delegated_ep .graph )
@@ -273,9 +105,7 @@ def test__limits(self, input_shape, dim, mocker):
273105 )
274106 def test__limits_exceeded (self , input_shape , dim ):
275107 model = SoftmaxModule (dim )
276- delegated_ep = to_quantized_edge_program (
277- model , input_shape , use_new_flow_neutron_c = True
278- ).exported_program ()
108+ delegated_ep = to_quantized_edge_program (model , input_shape ).exported_program ()
279109
280110 # Make sure the `softmax` was NOT delegated.
281111
0 commit comments