Commit Graph

9 Commits

Author SHA1 Message Date
David Reiss
d34d6244e7 [NNAPI] Use array instead of struct for serializing ints (#54696)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54696

This was originally developed for a Python version where array was not
available.

Test Plan: Unit test

Reviewed By: axitkhurana

Differential Revision: D27536792

Pulled By: dreiss

fbshipit-source-id: 39e5507e37d4f91871113439fe752a4d5373eaba
2021-04-06 13:49:30 -07:00
David Reiss
476c597ae6 [NNAPI] Handle binary ops combining NHWC+NCHW in some cases (#48812)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48812

This came up in a squeeze-and-excitation model.  Starting with an NHWC
tensor T, we perform a mean operation across H and W, giving an NxC
tensor, which (after some fully connected layers) is reshaped to
NxCx1x1, then multiplied with T.  To handle this, we detect the specific
case of a binary op with one NHWC input and one contiguous input with
H,W == 1,1 and allow the op to be applied (after transposing the
contiguous input).

Test Plan: Unit test.

Reviewed By: axitkhurana

Differential Revision: D25317939

Pulled By: dreiss

fbshipit-source-id: b4c17ab3b874d1a7defa04664010ba82115f1c20
2021-04-06 13:49:25 -07:00
David Reiss
b057d27b0b [NNAPI] Add support for unsqueeze, cat, and mean (#48811)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48811

Test Plan: Unit tests.

Reviewed By: axitkhurana

Differential Revision: D25317936

Pulled By: dreiss

fbshipit-source-id: 9b3a0a75b8157ae35ac13d52293a67800bad0ded
2021-04-06 13:49:22 -07:00
David Reiss
8fcf9ca341 [NNAPI] Update support for Linear (#54695)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54695

Previously, torch.nn.Linear was calling aten::addmm internally.  Now
it's calling aten::linear, so add support for that.

Test Plan: Unit test

Reviewed By: axitkhurana

Differential Revision: D27536795

Pulled By: dreiss

fbshipit-source-id: 42c8d2a80b20ac12ed9bba599c5e0e874256bb13
2021-04-06 13:49:17 -07:00
David Reiss
8d960f7043 [NNAPI] Fix hardtanh (#47520)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47520

NNAPI defines "RELU1" as clamping from [-1, 1], not [0, 1] as I
previously assumed.  Fix our implementation to match.

Test Plan: Upcoming unit test.

Reviewed By: axitkhurana

Differential Revision: D25317934

Pulled By: dreiss

fbshipit-source-id: 70efd5bb6092b0628ff6b765ce6f6274ef28d741
2021-04-06 13:49:14 -07:00
David Reiss
beca1fdbec [NNAPI] Fix MUL op (#47519)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47519

This wasn't updated when _do_add_binary was refactored.

Test Plan: Upcoming unit test.

Reviewed By: axitkhurana

Differential Revision: D25317938

Pulled By: dreiss

fbshipit-source-id: 99212404c189481cfa692dd77d8f7c7865b6872b
2021-04-06 13:49:12 -07:00
David Reiss
38a3c28f17 [NNAPI] Remove solid weights support (#47518)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47518

This was left over from an old version of the code.  The idea was that
instead of indexing into separate tensors for each weight, you could
bundle them all into a single file and use different offsets into that
file.  With the current design, this is nontrivial to support, so drop
the code for now.

Test Plan: CI

Reviewed By: axitkhurana

Differential Revision: D25317935

Pulled By: dreiss

fbshipit-source-id: e26ab3a8d437cb1bbb50319209fa56d9c571ce61
2021-04-06 13:49:09 -07:00
Akshit Khurana
d0fd41dcfe Add size op in nnapi serializer (#52026)
Summary:
serializer didn't support aten::size

Pull Request resolved: https://github.com/pytorch/pytorch/pull/52026

Test Plan: Torchvision Mobilenetv2 [script](https://pytorch.org/tutorials/prototype/nnapi_mobilenetv2.html) works. [Test](ecfed07cc5) to be merged after [this PR](https://github.com/pytorch/pytorch/pull/47521/files) is merged

Reviewed By: dreiss

Differential Revision: D26363133

Pulled By: axitkhurana

fbshipit-source-id: 772a6bea62bca69f8bba19c25c582a1734a70eb1
2021-02-10 15:57:01 -08:00
David Reiss
9a9383ef2e PyTorch NNAPI integration prototype (#46780)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46780

This is in prototype status, but pretty functional.  There are two major
parts.

- Model converter.  This is a pure Python component that consumes a
  model in TorchScript format, converts the operations into NNAPI
  semantics, and serializes the model in a custom format.  It then wraps
  the result in a new TorchScript model that can invoke NNAPI under the
  hood.
- Runtime.  This is a TorchBind object that deserializes the model and
  sends the result to NNAPI.  This is fairly simple since the serialized
  format is basically just a list of NNAPI calls to make, so most of the
  code is spent on bounds checking.

A few notes on the design.
- Currently, all tensor sizes need to be fixed, and those fixed sizes
  are burned directly into the serialized model.  This will probably
  need to change.  NNAPI supports variable-sized tensors, but the
  important hardware backends do not.  However, we're seeing use cases
  crop up where the input size is not known until around the time that
  the model is loaded (for example, it might depend on the camera aspect
  ratio).  I think the proper fix here is to remove the code in the
  converter that eagerly calculates the sizes of the intermediate
  tensors and replace it with a code generator that will generate some
  TorchScript code that will perform those calculations at model load
  time.  This way, we will be able to support models that have
  variable-sized inputs while still only showing fixed-sized operands to
  NNAPI.
- The important hardware backends want operands to be in NHWC order, but
  PyTorch natively represents all tensors and NCHW.  The strategy for
  this is to keep NCHW during most of the conversion process, but track
  and additional value per operand representing the "dimension order".
  The dimension order gets propagated through convolutions and pointwise
  ops.  When we're ready to serialize the model, we reorder the
  dimensions for "channels last" operands to NHWC.

Test Plan:
Some local testing with FB prod models.  I'll need to add some examples
and automated tests.

Reviewed By: iseeyuan

Differential Revision: D24574040

Pulled By: dreiss

fbshipit-source-id: 6adc8571b234877ee3666ec0c0de24da35c38a1f
2020-11-05 21:31:01 -08:00