-
Notifications
You must be signed in to change notification settings - Fork 758
NXP backend: Add documentation for channels last dim order support in Neutron backend + example in aot_neutron_compile.py.
#16223
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
MartinPavella
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
nxp-upstream:nxg01483/feature/EIEX-643-create-an-example-of-channels-last-dim-order-use-with-neutronbackend
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.
+119
−0
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # NXP eIQ Dim Order Support | ||
|
|
||
| The NXP ExecuTorch backend supports two different dim orders (memory formats): | ||
|
|
||
| * **contiguous** (default) | ||
| * **channels last** | ||
|
|
||
| Any other dim order will cause an exception during runtime. | ||
|
|
||
| ## Using the channels last dim order | ||
|
|
||
| If you want to use the NXP backend to accelerate your model, it may be beneficial to use the *channels last* dim order. | ||
|
|
||
| The Neutron NPU computes on channels last data, whereas ExecuTorch uses the channels first format. This mismatch | ||
| requires the execution of some extra transpositions during runtime, which can noticeably impact the performance. By | ||
| forcing the ExecuTorch operators to use the channels last dim order, some of these transpositions can be eliminated. | ||
|
|
||
| There are **two** options for using the **channels last** dim order: | ||
|
|
||
| 1. Maintaining the same model inputs | ||
| 2. Changing the inputs to **channels last** | ||
|
|
||
| ### Maintaining the same model inputs | ||
|
|
||
| The **first** option requires a change to the *python* definition of your model. The model inputs remain unchanged | ||
| (contiguous/channels first), and an extra operator is inserted to change their dim order to **channels last**. This | ||
| approach does not change how the model is used later on at all. End-to-end, it behaves as if we did nothing to the dim | ||
| order. | ||
|
|
||
| Example use-case: | ||
|
|
||
| ```python | ||
| class MyModel(nn.Module): | ||
|
|
||
| def forward(self, x): | ||
| # Transform the inputs to the channels last dim order. | ||
| x = x.to(memory_format=torch.channels_last) | ||
|
|
||
| ... # Rest of your model definition | ||
|
|
||
|
|
||
| ... | ||
|
|
||
| # Turn the weights to the channels last dim order. | ||
| model = MyModel().to(memory_format=torch.channels_last) | ||
|
|
||
| ... # Export and lower the model using the NXP backend. | ||
| ``` | ||
|
|
||
| Depending on the model, the NXP backend may be able to utilize the channels last dim order to achieve faster inference. | ||
| In many cases, there will be no improvement, and it is even possible (though rare) for the performance to get worse. So | ||
| you should compare the inference speed of the contiguous and channels last variants. | ||
|
|
||
| ### Changing the inputs to channels last | ||
|
|
||
| The **second** option does not require the model definition to be altered, but it also changes the model inputs. | ||
|
|
||
| **Note:** Instead of the original **contiguous/channels_first** (NCHW) format, the **input data must be provided in the | ||
| channels last** (NHWC) format during runtime. | ||
|
|
||
| This approach may provide **significant speed improvements** by greatly reducing the number | ||
| of added transpositions (even to 0). | ||
|
|
||
| Example use-case: | ||
|
|
||
| ```python | ||
| # Turn the weights to the channels last dim order. | ||
| model = YourModel().to(memory_format=torch.channels_last) | ||
|
|
||
| # Turn the example inputs to the channels last dim order. | ||
| # This will define the dim order of the inputs and the internal data at runtime. | ||
| example_inputs = tuple( | ||
| i.to(memory_format=torch.channels_last) for i in your_example_inputs | ||
| ) | ||
|
|
||
| # Use the channels last example inputs to export the model. | ||
| exported_program = torch.export.export(model, example_inputs) | ||
|
|
||
| ... # Lower the model using the NXP backend. | ||
| ``` | ||
|
|
||
| A full example of this use case can be found in the | ||
| [aot_neutron_compile.py](https://github.com/pytorch/executorch/blob/main/examples/nxp/aot_neutron_compile.py). The | ||
| following command will create the `cifar10_nxp_delegate.pte` model, which takes channels first inputs, contains no | ||
| transpositions, and can be run on the `i.MX RT700` board using the __MCUXpresso SDK 25.06__. For details on the | ||
| installation see {doc}`nxp-overview`. | ||
|
|
||
| ``` | ||
| python -m examples.nxp.aot_neutron_compile --quantize \ | ||
| --delegate --neutron_converter_flavor SDK_25_09 -m cifar10 \ | ||
| --use_channels_last_dim_order | ||
| ``` | ||
|
|
||
| ```{toctree} | ||
| :hidden: | ||
| :maxdepth: 2 | ||
|
|
||
| nxp-overview | ||
| ``` |
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
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.