Skip to content

Add NNAPI tutorial #1229

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

Merged
merged 1 commit into from
Nov 11, 2020
Merged

Add NNAPI tutorial #1229

merged 1 commit into from
Nov 11, 2020

Conversation

dreiss
Copy link
Contributor

@dreiss dreiss commented Nov 11, 2020

No description provided.

@netlify
Copy link

netlify bot commented Nov 11, 2020

Deploy preview for pytorch-tutorials-preview ready!

Built with commit f8791e5

https://deploy-preview-1229--pytorch-tutorials-preview.netlify.app

Copy link
Contributor

@jeffxtang jeffxtang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half way thru the PR and got errors when running the script. The right torch and torchvision versions and setup info would be helpful. Thanks

NNAPI provides access to powerful and efficient computational cores
on many modern Android devices.

PyTorch's NNAPI is currently in the "prototype" phase and only supports
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should a developer who wants to try this prototype feature go to the PyTorch master branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating to reference the specific version that is known to work.

First, we must prepare our model to execute with NNAPI.
This step runs on your training server or laptop.
The key conversion function to call is
``torch.backends._nnapi.prepare.convert_model_to_nnapi``,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a link to the convert_model_to_nnapi documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This still needs to be written.

but some extra steps are required to ensure that
the model is properly structured.
Most notably, quantizing the model is required
in order to run the model on certain hardware backends.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be more specific on the hardware backends that can run a quantized model? Is quantization required to run on certain hardware backends but:
a. not required on others, or
b. on some other hardware backends, even quantizing a model won't make the model run?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified that this refers to accelerators.

# - "none": Fully floating-point model.
# - "core": Quantize the core of the model, but wrap it a
# quantizer/dequantizer pair, so the interface uses floating point.
# - "full": Quantize the model, and use quantize tensors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quantize tensors => quantized tensors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

import torch
import torch.utils.bundled_inputs
import torch.utils.mobile_optimizer
import torch.backends._nnapi.prepare
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which PyTorch and torchvision versions are required to run this? I built and installed PyTorch from the master branch on Oct 22, got an error "ModuleNotFoundError: No module named 'torch.backends._nnapi'". Then I built from the latest pytorch master branch, and got a new error when running the script:

Exception: Unsupported node kind ('aten::size') in node %13 : int = aten::size(%input.88, %32) # /Users/jeffxtang/opt/anaconda3/lib/python3.8/site-packages/torchvision/models/mobilenet.py:166:0

I then installed torchvision from the latest source but the script still picks the older 0.7...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated instructions to reference the specific known-working version.


Because these models have bundled inputs, we can run the benchmark as follows:

.. code:: shell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code below doesn't show in the preview, maybe due to some format issue. code => code-block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.


Now that the models are ready, we can benchmark them on our Android devices.
See `our performance recipe <https://pytorch.org/tutorials/recipes/mobile_perf.html#android-benchmarking-setup>`_ for details.
The best-performing models are likely to be the "fully-quantized" models:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be great to show benchmark data here.. maybe in a future update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I'll add this after the blog post goes live.

@dreiss dreiss force-pushed the dreiss-nnapi-tutorial branch from 4ea1ee0 to f8791e5 Compare November 11, 2020 22:55
@brianjo brianjo merged commit 74b7663 into pytorch:master Nov 11, 2020
rodrigo-techera pushed a commit to Experience-Monks/tutorials that referenced this pull request Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants