-
Notifications
You must be signed in to change notification settings - Fork 811
NXP backend: added Squeeze support #16540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
novak-vaclav
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
nxp-upstream:feature/EIEX-673-add-support-for-squeeze-operator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,281 @@ | ||
| # Copyright 2026 NXP | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import torch | ||
| from executorch.backends.nxp.aten_passes.convert_nodes_to_view import ( | ||
| ConvertNodesToViewPass, | ||
| ) | ||
| from executorch.backends.nxp.aten_passes.neutron_aten_pass_manager import ( | ||
| NeutronAtenPassManager, | ||
| ) | ||
| from executorch.backends.nxp.backend.edge_program_converter import ( | ||
| EdgeProgramToIRConverter, | ||
| ) | ||
| from executorch.backends.nxp.tests.executorch_pipeline import ( | ||
| neutron_target_spec, | ||
| to_quantized_edge_program, | ||
| ) | ||
| from executorch.backends.nxp.tests.executors import ( | ||
| convert_run_compare, | ||
| graph_contains_any_of_ops, | ||
| ) | ||
|
|
||
| from executorch.backends.nxp.tests.models import SqueezeAddModel, UnsqueezeAddModel | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from torch.export import ExportedProgram | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reseed_model_per_test_run(): | ||
| torch.manual_seed(42) | ||
| np.random.seed(23) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape, dim", | ||
| [ | ||
| pytest.param((8, 1, 1), None, id="3D, dim = None."), | ||
| pytest.param((8, 4, 1), 2, id="3D, dim hit."), | ||
| pytest.param((8, 4, 1), 1, id="3D, dim miss."), | ||
| pytest.param((8, 4, 1), -1, id="3D, negative dim hit."), | ||
| pytest.param((8, 1, 1, 8), [1, 2], id="4D, full dims overlap."), | ||
| pytest.param((8, 1, 4, 8), [1, 2], id="4D, partial dims overlap."), | ||
| pytest.param((1, 8, 4, 8), [1, 2], id="4D, no dims overlap."), | ||
| pytest.param((8, 1, 1, 8), [-2, -3], id="4D, negative full dims overlap."), | ||
| pytest.param((8, 1, 4, 8), [-2, -3], id="4D, negative partial dims overlap."), | ||
| pytest.param((1, 8, 4, 8), [-2, -3], id="4D, negative no dims overlap."), | ||
| pytest.param( | ||
| (8, 1, 1, 8), (1, 2), id="4D, tuple instead of list, full dims overlap." | ||
| ), | ||
| pytest.param( | ||
| (8, 1, 4, 8), (1, 2), id="4D, tuple instead of list, partial dims overlap." | ||
| ), | ||
| pytest.param( | ||
| (1, 8, 4, 8), (1, 2), id="4D, tuple instead of list, no dims overlap." | ||
| ), | ||
| ], | ||
| ) | ||
| def test_convert_squeeze_to_view_simple(mocker, input_shape, dim): | ||
| model = SqueezeAddModel(dim=dim) | ||
|
|
||
| example_input_1 = torch.rand(input_shape) | ||
| example_input_2 = torch.rand(input_shape) | ||
|
|
||
| exir_program_aten = torch.export.export( | ||
| model, | ||
| (example_input_1, example_input_2), | ||
| ).module() | ||
|
|
||
| # Check that `Squeeze` is present in the model. | ||
| assert graph_contains_any_of_ops( | ||
| exir_program_aten.graph, | ||
| [ | ||
| torch.ops.aten.squeeze.dim, | ||
| torch.ops.aten.squeeze.dims, | ||
| torch.ops.aten.squeeze.default, | ||
| ], | ||
| ) | ||
|
|
||
| example_input = (example_input_1, example_input_2) | ||
| outputs_before = [o.detach().numpy() for o in exir_program_aten(*example_input)] | ||
|
|
||
| # Apply the optimization. | ||
| NeutronAtenPassManager(neutron_target_spec, [ConvertNodesToViewPass()])( | ||
| exir_program_aten | ||
| ) | ||
|
|
||
| # Make sure no `Squeeze` is in the model. | ||
| assert not graph_contains_any_of_ops( | ||
| exir_program_aten.graph, | ||
| [ | ||
| torch.ops.aten.squeeze.dim, | ||
| torch.ops.aten.squeeze.dims, | ||
| torch.ops.aten.squeeze.default, | ||
| ], | ||
| ) | ||
|
|
||
| # Make sure there is `aten.view.default` in the model. | ||
| assert graph_contains_any_of_ops( | ||
| exir_program_aten.graph, | ||
| [torch.ops.aten.view.default], | ||
| ) | ||
|
|
||
| outputs_after = [o.detach().numpy() for o in exir_program_aten(*example_input)] | ||
|
|
||
| # Make sure the model still produces the exact same output. | ||
| assert len(outputs_before) == len(outputs_after) | ||
|
|
||
| for i in range(len(outputs_before)): | ||
| assert np.allclose(outputs_before[i], outputs_after[i]) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape, dim", | ||
| [ | ||
| pytest.param((8, 1, 1), None, id="3D, dim = None."), | ||
| pytest.param((8, 4, 1), 2, id="3D, dim hit."), | ||
| pytest.param((8, 4, 1), 1, id="3D, dim miss."), | ||
| pytest.param((8, 4, 1), -1, id="3D, negative dim hit."), | ||
| pytest.param((8, 1, 4, 8), [1, 2], id="4D, partial dims overlap."), | ||
| pytest.param((8, 1, 4, 8), [-2, -3], id="4D, negative partial dims overlap."), | ||
novak-vaclav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| ) | ||
| def test_convert_squeeze_to_view_full_pipeline(mocker, input_shape, dim): | ||
| model = SqueezeAddModel(dim) | ||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
|
|
||
| # Run conversion | ||
| edge_program = to_quantized_edge_program( | ||
| model, | ||
| [input_shape, input_shape], | ||
| ).exported_program() | ||
|
|
||
| # Check that `Squeeze` is no longer present in the model | ||
| assert not graph_contains_any_of_ops( | ||
| edge_program.graph, | ||
| [ | ||
| torch.ops.aten.squeeze.dim, | ||
| torch.ops.aten.squeeze.dims, | ||
| torch.ops.aten.squeeze.default, | ||
| ], | ||
| ) | ||
|
|
||
| # Capture generated model | ||
| neutron_ir_model = converter_spy.spy_return[0] | ||
| exported_program: ExportedProgram = converter_spy.call_args.args[1] | ||
|
|
||
| # Make sure `edge.aten.view_copy.default` is in the model. | ||
| assert graph_contains_any_of_ops( | ||
| exported_program.graph, | ||
| [ | ||
| exir_ops.edge.aten.view_copy.default, | ||
| ], | ||
| ) | ||
|
|
||
| example_input_1 = (np.random.random(input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| example_input_2 = (np.random.random(input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| example_input = {0: example_input_1, 1: example_input_2} | ||
|
|
||
| convert_run_compare( | ||
| exported_program, | ||
| input_data=example_input, | ||
| tfl_model=neutron_ir_model, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape, dim", | ||
| [ | ||
| pytest.param((2,), 0, id="1D."), | ||
| pytest.param((8, 4, 6), 2, id="3D."), | ||
| pytest.param((8, 4, 6, 8), -2, id="4D, negative dim."), | ||
| pytest.param((8, 4, 6), 3, id="3D, dim arg is clipped."), | ||
| pytest.param((8, 4, 6), -4, id="3D, dim arg is clipped."), | ||
| ], | ||
| ) | ||
| def test_convert_unsqueeze_to_view_simple(mocker, input_shape, dim): | ||
| model = UnsqueezeAddModel(dim) | ||
|
|
||
| example_input_1 = torch.rand(input_shape) | ||
| example_input_2 = torch.rand(input_shape) | ||
|
|
||
| exir_program_aten = torch.export.export( | ||
| model, | ||
| (example_input_1, example_input_2), | ||
| ).module() | ||
|
|
||
| # Check "aten.unsqueeze.default" is present | ||
| assert graph_contains_any_of_ops( | ||
| exir_program_aten.graph, [torch.ops.aten.unsqueeze.default] | ||
| ) | ||
|
|
||
| example_input = (example_input_1, example_input_2) | ||
| outputs_before = [o.detach().numpy() for o in exir_program_aten(*example_input)] | ||
|
|
||
| # Apply the optimization. | ||
| NeutronAtenPassManager(neutron_target_spec, [ConvertNodesToViewPass()])( | ||
| exir_program_aten | ||
| ) | ||
|
|
||
| # Make sure no "aten.unsqueeze.default" is in the model. | ||
| assert not graph_contains_any_of_ops( | ||
| exir_program_aten.graph, | ||
| [torch.ops.aten.unsqueeze.default], | ||
| ) | ||
|
|
||
| # Make sure there is "aten.view.default" in the model. | ||
| assert graph_contains_any_of_ops( | ||
| exir_program_aten.graph, | ||
| [torch.ops.aten.view.default], | ||
| ) | ||
|
|
||
| outputs_after = [o.detach().numpy() for o in exir_program_aten(*example_input)] | ||
|
|
||
| # Make sure the model still produces the exact same output. | ||
| assert len(outputs_before) == len(outputs_after) | ||
|
|
||
| for i in range(len(outputs_before)): | ||
| assert np.allclose(outputs_before[i], outputs_after[i]) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape, dim", | ||
| [ | ||
| pytest.param((2,), 0, id="1D."), | ||
| pytest.param((8, 4, 6), 2, id="3D."), | ||
| pytest.param((8, 4, 6, 8), -2, id="4D, negative dim."), | ||
| pytest.param((8, 4, 6), 3, id="3D, dim arg is clipped."), | ||
| pytest.param((8, 4, 6), -4, id="3D, dim arg is clipped."), | ||
| ], | ||
| ) | ||
| def test_convert_unsqueeze_to_view_full_pipeline(mocker, input_shape, dim): | ||
| model = UnsqueezeAddModel(dim) | ||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
|
|
||
| # Run conversion | ||
| edge_program = to_quantized_edge_program( | ||
| model, | ||
| [input_shape, input_shape], | ||
| ).exported_program() | ||
|
|
||
| # Make sure no "aten.unsqueeze.default" is in the model. | ||
| assert not graph_contains_any_of_ops( | ||
| edge_program.graph, | ||
| [ | ||
| torch.ops.aten.unsqueeze.default, | ||
| ], | ||
| ) | ||
|
|
||
| # Capture generated model | ||
| neutron_ir_model = converter_spy.spy_return[0] | ||
| exported_program: ExportedProgram = converter_spy.call_args.args[1] | ||
|
|
||
| # Make sure "edge.aten.view_copy.default" is in the model. | ||
| assert graph_contains_any_of_ops( | ||
| exported_program.graph, | ||
| [ | ||
| exir_ops.edge.aten.view_copy.default, | ||
| ], | ||
| ) | ||
|
|
||
| example_input_1 = (np.random.random(input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| example_input_2 = (np.random.random(input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| example_input = {0: example_input_1, 1: example_input_2} | ||
|
|
||
| convert_run_compare( | ||
| exported_program, | ||
| input_data=example_input, | ||
| tfl_model=neutron_ir_model, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.