|
| 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