Skip to content

Commit 99e4fbe

Browse files
authored
Revert "NXP backend: Resolve limitations of uncertain tensor formats" (#14475)
Reverts #13942 Broke a buck target test_neutoron_backend because of "ModuleNotFoundError: No module named 'executorch.backends.nxp._passes". _passes needs a buck target specified to be included as dep
1 parent 02d8710 commit 99e4fbe

17 files changed

+76
-362
lines changed

backends/nxp/_passes/remove_getitem_pass.py

Lines changed: 0 additions & 103 deletions
This file was deleted.

backends/nxp/backend/edge_program_converter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
from torch.fx import Node
1919
from torch.nn.parameter import Parameter
2020
from executorch.backends.nxp.backend.ir.converter.node_converters.ops_converters import * # noqa F403
21-
from executorch.backends.nxp.backend.node_format import NXP_NODE_FORMAT
21+
from executorch.backends.nxp.backend.node_format_inference import (
22+
NodeFormat,
23+
NodeFormatInference,
24+
)
2225
from executorch.exir.dialects._ops import ops as exir_ops
2326

2427
# noinspection PyProtectedMember
@@ -67,10 +70,12 @@ def convert_program(
6770
:param custom_delegation_options: Custom user options which affect node delegation.
6871
:return: TFLite flatbuffers as bytes.
6972
"""
73+
node_formats = NodeFormatInference(edge_program).identify_node_formats()
7074
parameters_mapping = self.map_inputs_to_parameters(edge_program)
7175

7276
cc = self.build_conversion_context(
7377
parameters_mapping,
78+
node_formats,
7479
conversion_config,
7580
custom_delegation_options,
7681
)
@@ -96,7 +101,7 @@ def convert_program(
96101
def append_placeholders_and_tensors(nodes: list[Node], context: ConversionContext):
97102
for node in nodes:
98103
if node.op == "placeholder":
99-
node_format = node.meta[NXP_NODE_FORMAT]
104+
node_format = context.node_formats[node]
100105

101106
if node.name in context.parameters_mapping:
102107
# Node is placeholder and has data -> append as static tensor with data
@@ -109,7 +114,7 @@ def append_placeholders_and_tensors(nodes: list[Node], context: ConversionContex
109114
context.tflite_builder.append_as_fake_tensor(node, node_format)
110115
elif node.op == "call_function":
111116
# Node is call function -> append only output as a tensor
112-
node_format = node.meta[NXP_NODE_FORMAT]
117+
node_format = context.node_formats[node]
113118
context.tflite_builder.append_as_fake_tensor(node, node_format)
114119
elif node.op == "output":
115120
# Nothing to do
@@ -166,6 +171,7 @@ def map_inputs_to_parameters(edge_program: ExportedProgram) -> dict[str, Paramet
166171
@staticmethod
167172
def build_conversion_context(
168173
parameters_mapping: dict,
174+
node_formats: dict[Node, NodeFormat],
169175
conversion_config: ConversionConfig = _default_conversion_config,
170176
custom_delegation_options: CustomDelegationOptions = _default_delegation_options,
171177
) -> ConversionContext:
@@ -180,6 +186,7 @@ def build_conversion_context(
180186
tflite_builder,
181187
conversion_config,
182188
parameters_mapping,
189+
node_formats,
183190
custom_delegation_options,
184191
)
185192

backends/nxp/backend/ir/conversion_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
from executorch.backends.nxp.backend.ir.converter.builder.aten_model_builder_director import (
1111
AtenModelBuilderDirector,
1212
)
13+
from executorch.backends.nxp.backend.node_format_inference import NodeFormat
14+
from torch import Node
1315
from torch.nn import Parameter
1416

1517

1618
class ConversionContext:
1719
tflite_builder: AtenModelBuilderDirector
1820
conversion_config: ConversionConfig
1921
parameters_mapping: dict[str, Parameter]
22+
node_formats: dict[Node, NodeFormat]
2023
custom_delegation_options: CustomDelegationOptions
2124

2225
def __init__(
2326
self,
2427
tflite_builder: AtenModelBuilderDirector,
2528
conversion_config: ConversionConfig,
2629
parameters_mapping: dict,
30+
node_formats: dict[Node, NodeFormat],
2731
custom_delegation_options: CustomDelegationOptions,
2832
):
2933
"""
@@ -35,4 +39,5 @@ def __init__(
3539
self.tflite_builder = tflite_builder
3640
self.conversion_config = conversion_config
3741
self.parameters_mapping = parameters_mapping
42+
self.node_formats = node_formats
3843
self.custom_delegation_options = custom_delegation_options

backends/nxp/backend/ir/converter/builder/aten_model_builder_director.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from executorch.backends.nxp.backend.ir.converter.conversion import translator
1010
from executorch.backends.nxp.backend.ir.tensor_formatting import TensorFormat
1111
from executorch.backends.nxp.backend.ir.tflite_generator import tflite_model
12-
from executorch.backends.nxp.backend.node_format import NodeFormat
12+
from executorch.backends.nxp.backend.node_format_inference import NodeFormat
1313
from torch.fx import Node
1414
from torch.nn import Parameter
1515

backends/nxp/backend/ir/converter/node_converters/ops_converters/cat_converter.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options.concatenation_options import (
1919
Concatenation,
2020
)
21-
from executorch.backends.nxp.backend.node_format import NXP_NODE_FORMAT
2221
from torch.fx import Node
2322
from torch.nn import Parameter
2423

@@ -89,27 +88,25 @@ def _is_supported_on_target(
8988
return False
9089

9190
# Neutron requires the channels to be a multiple of `8`. The channels could either be the second or the
92-
# last dimension, depending on the formats of the node.
93-
if node.meta[NXP_NODE_FORMAT].is_channels_first():
94-
# During conversion to IR, the shape will be permuted to channels last, and the dimension on index
95-
# `1` will end up being the channels (last dim in NHWC).
96-
channels_index = 1
97-
else:
98-
# The shape will not be permuted during conversion, so the channels will remain the last dimension.
99-
channels_index = -1
100-
91+
# last dimension, depending on the formats of the node. The format, however, cannot be determined
92+
# during conversion, as it depends on what other nodes are delegated.
10193
input_channels = [
102-
_get_shape(input_)[channels_index]
94+
# The second dimension is the channels in PyTorch. If the inputs/output are not channels first, it
95+
# will still be the channels in the IR.
96+
_get_shape(input_)[1]
97+
for input_ in node.all_input_nodes
98+
] + [
99+
# If the inputs/outputs are channels first, the last dimension will be the channels.
100+
_get_shape(input_)[-1]
103101
for input_ in node.all_input_nodes
104102
]
105-
output_channels = _get_shape(node)[channels_index]
106-
107103
if any((input_channel % 8) != 0 for input_channel in input_channels):
108104
# neutron-library/src/utils/NeutronLibraryInterrogation.cpp#1492
109105
return False
110106

111-
if (output_channels % 8) != 0:
112-
# neutron-library/src/utils/NeutronLibraryInterrogation.cpp#1493
107+
output_channels = [_get_shape(node)[1], _get_shape(node)[-1]]
108+
# neutron-library/src/utils/NeutronLibraryInterrogation.cpp#1493
109+
if any((out_c % 8) != 0 for out_c in output_channels):
113110
return False
114111

115112
if len(node.all_input_nodes) < 2: # Not supported on Neutron

backends/nxp/backend/ir/converter/node_converters/ops_converters/constant_pad_nd_converter.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
pad_options,
2828
pad_v2_options,
2929
)
30-
31-
from executorch.backends.nxp.backend.node_format import NXP_NODE_FORMAT
3230
from torch.fx import Node
3331
from torch.nn import Parameter
3432

@@ -43,17 +41,11 @@ def _is_supported_on_target(
4341
) -> bool:
4442
match target:
4543
case Target.RT700:
44+
# TODO: Consider different tensor formats (dim-order)
4645
paddings = node.args[1]
47-
if node.meta[NXP_NODE_FORMAT].is_channels_first():
48-
# Dim `1` will end up being the channels. It is padded by paddings[4:6].
49-
if len(paddings) > 4 and paddings[4:6] != [0, 0]:
50-
# Attempt to Pad channels dimension -> currently not supported
51-
return False
52-
else:
53-
# Dim `-1` will end up being the channels. It is padded by paddings[:2].
54-
if len(paddings) > 0 and paddings[:2] != [0, 0]:
55-
# Attempt to Pad channels dimension -> currently not supported
56-
return False
46+
if len(paddings) > 4 and paddings[4:6] != [0, 0]:
47+
# Attempt to Pad channels dimension, which is not supported on Neutron.
48+
return False
5749

5850
return True
5951

@@ -79,6 +71,10 @@ def _is_supported_in_IR(
7971
if not NodeConverter._has_shared_q_params_if_quantized(node):
8072
return False
8173

74+
if len(paddings) > 4 and paddings[4:6] != [0, 0]:
75+
# Attempt to Pad channels dimension -> currently not supported
76+
return False
77+
8278
return True
8379

8480
# noinspection PyMethodMayBeStatic

backends/nxp/backend/ir/tensor_formatting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#
88
from enum import Enum
99

10-
from executorch.backends.nxp.backend.node_format import NodeFormat
10+
from executorch.backends.nxp.backend.node_format_inference import NodeFormat
1111

1212

1313
class TensorFormat(Enum):

backends/nxp/backend/node_format.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)