55# License: MIT
66# See the LICENSE_MIT for more details.
77#
8+
89from copy import deepcopy
10+ from itertools import chain
911from typing import Dict , List , Optional , Union
1012
1113import executorch .backends .nxp .backend .ir .converter .conversion .translator as translator
4850 FlexTranspose ,
4951)
5052from executorch .backends .nxp .backend .ir .tflite_optimizer import optimizer
53+ from executorch .backends .nxp .backend .neutron_operator_support import (
54+ transposition_is_supported_on_neutron ,
55+ )
5156from executorch .backends .nxp .backend .neutron_target_spec import NeutronTargetSpec
5257
5358
@@ -218,7 +223,7 @@ def channels_first_version_of(self, t_tensor: tflite_model.Tensor):
218223 new_tensor .shape = translator .channels_last_shape_to_channels_first (
219224 t_tensor .shape
220225 )
221- new_tensor .tensor_format = new_tensor . tensor_format . to_node_format ()
226+ new_tensor .tensor_format = TensorFormat . CHANNELS_FIRST
222227
223228 perm = translator .create_channels_last_to_channels_first_permutation (
224229 t_tensor .rank
@@ -355,6 +360,19 @@ def _make_inputs_channels_first(self):
355360 if input_tensor .tensor_format .is_channels_last ():
356361 # Create a Transpose operator and replace the graph input
357362
363+ new_input_shape = translator .channels_last_shape_to_channels_first (
364+ input_tensor .shape
365+ )
366+ perm = translator .create_channels_first_to_channels_last_permutation (
367+ input_tensor .rank
368+ )
369+
370+ if not transposition_is_supported_on_neutron (
371+ new_input_shape .vector , list (perm ), self .neutron_target_spec
372+ ):
373+ new_inputs .append (input_tensor )
374+ continue
375+
358376 if input_tensor .rank > 6 :
359377 msg = (
360378 f"Couldn't preserve the shape of input tensor '{ input_tensor .name } ', because it has "
@@ -365,14 +383,9 @@ def _make_inputs_channels_first(self):
365383 new_input = self .duplicate_tensor (
366384 input_tensor , input_tensor .name + "_channels_first"
367385 )
368- new_input .shape = translator .channels_last_shape_to_channels_first (
369- input_tensor .shape
370- )
371- new_input .tensor_format = input_tensor .tensor_format .to_node_format ()
386+ new_input .shape = new_input_shape
387+ new_input .tensor_format = TensorFormat .CHANNELS_FIRST
372388
373- perm = translator .create_channels_first_to_channels_last_permutation (
374- input_tensor .rank
375- )
376389 transpose = self ._create_transpose_operator (
377390 new_input , input_tensor , perm
378391 )
@@ -397,6 +410,16 @@ def _make_outputs_channels_first(self):
397410 if output_tensor .tensor_format .is_channels_last ():
398411 # Add a Transpose operator, to make the output channels first
399412
413+ shape = output_tensor .shape .vector
414+ perm = translator .create_channels_last_to_channels_first_permutation (
415+ len (shape ), True
416+ )
417+ if not transposition_is_supported_on_neutron (
418+ shape , perm , self .neutron_target_spec
419+ ):
420+ new_outputs .append (output_tensor )
421+ continue
422+
400423 if output_tensor .rank > 6 :
401424 logger .e (
402425 logger .Code .IO_PRESERVATION_ERROR ,
@@ -437,26 +460,38 @@ def _keep_one_empty_buffer(self):
437460 # It's safe to replace the buffer.
438461 t .tmp_buffer = empty_buffer
439462
463+ def replace_io_tensor_format_with_node_format (self ):
464+ for t in chain (
465+ self .get_sub_graph ().inputs .tmp_inputs ,
466+ self .get_sub_graph ().outputs .tmp_outputs ,
467+ ):
468+ if isinstance (t .tensor_format , TensorFormat ):
469+ t .tensor_format = t .tensor_format .to_equal_node_format ()
470+
440471 def finish (self ) -> tflite_model .Model :
441472 """Finalize and optimize the converted TFLite model. Then return it.
442473
443474 At least one of 'optimization_whitelist' and 'optimization_blacklist' must be 'None'.
444475 :return: The final TFLite model.
445476 """
446477
447- if self .conversion_config .keep_io_format :
478+ if self .conversion_config .use_neutron_for_format_conversion :
448479 # If the input or output is channels last, add a Transpose operator, to make is channels first.
449480 self ._make_inputs_channels_first ()
450481 self ._make_outputs_channels_first ()
451482
452483 # Apply optimizations to the internal TFLite model.
453- optimizer .Optimizer (self , self .conversion_config ).optimize (
484+ optimizer .Optimizer (
485+ self , self .conversion_config , self .neutron_target_spec
486+ ).optimize (
454487 self .conversion_config .optimization_whitelist ,
455488 self .conversion_config .optimization_blacklist ,
456489 )
457490
458491 self ._keep_one_empty_buffer ()
459492
493+ self .replace_io_tensor_format_with_node_format ()
494+
460495 # Remove outputs, which are not produced by any node. Otherwise, there would be errors after inference.
461496 operator_outputs = []
462497 for op in self .get_operators ().vector :
0 commit comments