Skip to content

Commit 0567b0a

Browse files
NXP backend: Fix bug when a partition output is also used by nodes inside the partition. (pytorch#20423)
### Summary [De]quantize nodes that form QDQ clusters are handled by turning float tensors to int8 and assigning to them the quantization parameters in the Neutron IR. But these operators cannot just disappear because the graph would be disconnected (there would be "holes" in it). Usually we can redirect the outputs of these operators to their inputs, and if this is not possible, we can convert the operators to identity operators, which are optimized out later. Up until a recent PR, `dequantize` nodes were always being turned into identity ops (not skipped), which caused issues in optimization passes. This is because the QDQ nodes are converted before the compute nodes. So for example a `dequantize` that's near the end of the model would be converted to a noop Transpose and inserted into the `operators` list before the compute ops which topologically come before it. So the topological ordering of the graph was broken. The recent PR made it so that only the `dequantize` nodes that consume model inputs are turned to identity, and the rest is skipped (fixing the topological order issue). That PR however failed to identify an edge case where if a node is both the output of a partition and it is also used as input to other nodes inside that partition, the `dequantize` node that comes after it cannot be skipped, and it must be turned into identity. Therefore, we cannot avoid breaking the topological order. For this reason, after all edge operators are converted to Neutron IR (and before any IR optimizations), the operators of the graph need to be topologically sorted. This PR fixes the bug with the intermediate output, and it introduces the topological operator sorting. ### Test plan Unit test provided. cc @robert-kalmar @JakeStevens @digantdesai @rascani
1 parent 2ec218c commit 0567b0a

5 files changed

Lines changed: 141 additions & 6 deletions

File tree

‎backends/nxp/backend/edge_program_converter.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def convert_program(
113113
self._convert_qdq_cluster_q_dq_nodes(edge_program.graph.nodes, cc)
114114
self._process_nodes(edge_program.graph.nodes, cc)
115115

116+
# Make sure the operators are correctly ordered.
117+
cc.tflite_builder.sort_operators_topologically()
118+
116119
# Assign the model its inputs and outputs.
117120
cc.tflite_builder.assign_model_io_to_subgraph(edge_program.graph_signature)
118121

‎backends/nxp/backend/ir/converter/builder/model_builder.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# See the LICENSE_MIT for more details.
77
#
88

9+
import logging
10+
from collections import defaultdict, deque
911
from copy import deepcopy
1012
from typing import List, Optional, Union
1113

@@ -111,6 +113,67 @@ def __init__(
111113
self._zeros_tensor_map = {}
112114
self.edge_to_tflite_map = {}
113115

116+
def sort_operators_topologically(self):
117+
"""Sort the operators in the model graph in topological order using Kahn's algorithm.
118+
119+
Ensures that each operator appears after all operators that produce its input tensors.
120+
This is required for correct behavior of optimization passes and by the Neutron IR format.
121+
122+
The algorithm works in three steps:
123+
1. Build a mapping from each tensor to the operator that produces it.
124+
2. Compute the number of unresolved input dependencies for each operator.
125+
3. Iteratively add operators with no remaining dependencies to the sorted list,
126+
decrementing the number of dependencies of their consumers until all operators are processed.
127+
128+
If the sort succeeds, the operator list in the model's subgraph is replaced with the topologically sorted list.
129+
If it fails (e.g. due to an invalid graph), a warning is logged and the original operator order is preserved.
130+
"""
131+
132+
operators = self.get_operators()
133+
134+
# Map tensors to operators that produce them.
135+
tensor_to_producer_op = {}
136+
for producer_op in operators:
137+
for output in producer_op.tmp_outputs:
138+
tensor_to_producer_op[output] = producer_op
139+
140+
# Map operators to operators that consume their outputs.
141+
op_to_child_op = defaultdict(list)
142+
num_unresolved_input_dependencies = {op: 0 for op in operators}
143+
for child_op in operators:
144+
for input_ in child_op.tmp_inputs:
145+
parent_op = tensor_to_producer_op.get(input_)
146+
if parent_op is not None:
147+
op_to_child_op[parent_op].append(child_op)
148+
num_unresolved_input_dependencies[child_op] += 1
149+
150+
# Gradually build the graph.
151+
# Use `deque` for fast appends and pops.
152+
queue = deque(
153+
op
154+
for op, degree in num_unresolved_input_dependencies.items()
155+
if degree == 0
156+
)
157+
sorted_ops = []
158+
while len(queue) != 0:
159+
op = queue.popleft()
160+
sorted_ops.append(op)
161+
for child_op in op_to_child_op[op]:
162+
num_unresolved_input_dependencies[child_op] -= 1
163+
if num_unresolved_input_dependencies[child_op] == 0:
164+
# All input of the `child_op` are produced by operators that are already in `sorted_ops`. So we can
165+
# insert it into the graph.
166+
queue.append(child_op)
167+
168+
if len(sorted_ops) != operators.len():
169+
logging.warning(
170+
"NXP backend: ModelBuilder.sort_operators_topologically() failed. Please report this."
171+
)
172+
173+
else:
174+
# The topological sort was successful.
175+
operators.vector = sorted_ops
176+
114177
def create_zeros_tensor(
115178
self, dims: List[int], name: str, dtype: np.dtype, can_reuse: bool = False
116179
) -> tflite_model.Tensor:

‎backends/nxp/backend/ir/converter/node_converters/ops_converters/qdq_dequantize_converter.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ def convert(self, node: Node):
6060
if isinstance(self, QDQPerChannelDequantizeConverter):
6161
quantized_dimension = self.get_quantization_dimension(input_tensor, node)
6262

63-
consumes_model_input = (
63+
consumes_model_io = (
6464
node.args[0].name in self.context.edge_program_signature.user_inputs
65+
or node.args[0].name in self.context.edge_program_signature.user_outputs
6566
)
66-
if consumes_model_input:
67-
# We cannot just skip the operator. Skipping would require changing the input's name, and as the input is
68-
# also a model input, the name cannot be changed.
67+
if consumes_model_io:
68+
# We cannot just skip the operator. Skipping would require changing the input's/output's name, and as the
69+
# input/output is also a model input/output, the name cannot be changed.
6970
# Instead, we convert it into an identity (Transpose that will be removed), and we make the output tensor
7071
# quantized just like the input.
7172
t_op = self._create_tflite_op_with_io_tensors(node)
@@ -80,6 +81,7 @@ def convert(self, node: Node):
8081

8182
self.builder.turn_operator_to_identity(t_op)
8283
self.builder.append_operators([t_op])
84+
8385
else:
8486
# Dequantize consumes an internal tensor, so we can just make it so that any operators which used the float
8587
# output of the dequantize will now use its quantized input. We do this by redirecting the output to the

‎backends/nxp/tests/ir/edge_passes/test_remove_io_quant_ops_pass.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import executorch.extension.pybindings.portable_lib
99
import executorch.kernels.quantized # noqa F401
1010

11-
import pytest
1211
import torch
1312
from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program
1413
from executorch.backends.nxp.tests.models import Conv2dReLUModule
@@ -95,7 +94,6 @@ def forward(self, x, y):
9594
return x + y, z
9695

9796

98-
@pytest.mark.xfail(strict=True, reason="Known bug (EIEX-946).")
9997
def test_multiple_inputs__multiple_outputs():
10098
model = MultiInputOutputModule()
10199
model.eval()
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2026 NXP
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from copy import deepcopy
7+
8+
from executorch.backends.nxp.backend.ir.converter.builder.model_builder import (
9+
ModelBuilder,
10+
)
11+
from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options.transpose_options import (
12+
Transpose,
13+
)
14+
from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program
15+
from executorch.backends.nxp.tests.ir.edge_passes.test_remove_io_quant_ops_pass import (
16+
MultiInputOutputModule,
17+
)
18+
19+
20+
def test_topological_sorting(mocker):
21+
# This model requires the insertion of a `Transpose` operator in place of a `dequantize` node, which breaks the
22+
# topological order.
23+
model = MultiInputOutputModule()
24+
25+
# Capture the operators before and after topological sorting.
26+
original_sort_fn = ModelBuilder.sort_operators_topologically
27+
captured_ops = {}
28+
29+
def mock_sort_fn(self: ModelBuilder):
30+
captured_ops["pre_sort"] = deepcopy(self.get_operators().vector)
31+
original_sort_fn(self) # type: ignore
32+
captured_ops["post_sort"] = deepcopy(self.get_operators().vector)
33+
34+
mocker.patch.object(
35+
ModelBuilder,
36+
"sort_operators_topologically",
37+
autospec=True,
38+
side_effect=mock_sort_fn,
39+
)
40+
41+
input_shapes = [(1, 4, 32, 32), (1, 1, 1, 31)]
42+
to_quantized_edge_program(model, input_shapes)
43+
44+
ops_pre_sort = captured_ops["pre_sort"]
45+
ops_post_sort = captured_ops["post_sort"]
46+
assert len(ops_pre_sort) == len(ops_post_sort)
47+
48+
# Before sorting, the operator on index `2` should be an identity `Transpose`, which uses the output of the
49+
# operator on index `3` (breaking the topological order).
50+
assert isinstance(ops_pre_sort[2].builtin_options, Transpose)
51+
assert all(ops_pre_sort[2].tmp_inputs[1].tmp_buffer.data == [0, 1, 2, 3])
52+
assert ops_pre_sort[2].tmp_inputs[0] == ops_pre_sort[3].tmp_outputs[0]
53+
54+
# After the sort, the operators on indices `2` and `3` are swapped.
55+
assert (
56+
ops_post_sort[2].builtin_options is None
57+
) # A Relu, which doesn't have a `BuiltinOptions` in Neutron IR.
58+
assert isinstance(ops_post_sort[3].builtin_options, Transpose)
59+
assert all(ops_post_sort[3].tmp_inputs[1].tmp_buffer.data == [0, 1, 2, 3])
60+
assert ops_post_sort[2].tmp_outputs[0] == ops_post_sort[3].tmp_inputs[0]
61+
62+
# Make sure all nodes follow topological order.
63+
tensor_to_producer_op = {
64+
output_tensor: op for op in ops_post_sort for output_tensor in op.tmp_outputs
65+
}
66+
for op_idx, op in enumerate(ops_post_sort):
67+
for input_tensor in op.tmp_inputs:
68+
if input_tensor in tensor_to_producer_op:
69+
assert ops_post_sort.index(tensor_to_producer_op[input_tensor]) < op_idx

0 commit comments

Comments
 (0)