Commit Graph

469 Commits

Author SHA1 Message Date
Justin Chu
773d80747c [ONNX] Clean up unit tests, rename files and improve import style (#81141)
- Rename `test_pytorch_common` -> `pytorch_test_common`, `test_onnx_common` -> `onnx_test_common`, removing the test_ prefix to show that the files are not test cases
- Remove import * in `test_pytorch_common` and adjust to import from `testing._internal.common_utils` (where functions are actually defined) instead
- Import modules only in `test_pytorch_onnx_onnxruntime` (too many to handle in a single PR in other tests) (The skips are exceptions)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/81141
Approved by: https://github.com/BowenBao
2022-07-12 00:00:49 +00:00
Nikita Shulga
80bf2ea3d9 [CI] Install vision without pep517 (#81074)
If installed with pep517 support, `torchvision` will be build againstreleased version of PyTorch rather than against the one currently installed on the system

Also update `torchvision` hash to 8a45147f9d and:
 - Added `maskrcnn_resnet50_fpn_v2`, `maskrcnn_resnet50_fpn_v2`, `retinanet_resnet50_fpn_v2`, `ssd300_vgg16`, `fcos_resnet50_fpn` and `ssdlite320_mobilenet_v3_large` to the list of untraceable models
 - Set default input size to (1, 3, 16, 224, 224) for `mvit_v1_b` model
 - Skipped `test_roi_aligned`,`test_batched_nms`, `test_roi_pooled` and `test_roi_align_aligned`  ONNX test (tracked in https://github.com/pytorch/pytorch/issues/81121 )
 - Skipped TorchVision integration tests in `test_package` (tracked in https://github.com/pytorch/pytorch/issues/81115 )

Pull Request resolved: https://github.com/pytorch/pytorch/pull/81074
Approved by: https://github.com/kit1980
2022-07-08 22:53:44 +00:00
shubhambhokare1
712b3e76ef [onnx] Add argsort support (#80234)
Fixes #79859 where exporting the operator '::argsort' to ONNX opset version 16 is not supported.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/80234
Approved by: https://github.com/BowenBao
2022-07-07 22:06:29 +00:00
PyTorch MergeBot
f2c8557521 Revert "Make kl_div a composite function. (#80334)"
This reverts commit 828c787ea9.

Reverted https://github.com/pytorch/pytorch/pull/80334 on behalf of https://github.com/ezyang due to doesn't work with xla
2022-07-06 17:51:06 +00:00
titaiwang
f608db5ff0 [ONNX] Moved uninitialized_optional test cases into runtime test and parameterized left test cases (#80145)
- moved test_uninitialized_optional into runtime test, because **OptionalHasElement** node now is supported on onnxruntime. test_optional_input will be moved later when tracing/None output paradox is fixed.
- changed _TestONNXRuntime to inherited by common_utils.Testcase as it's more rich on error message
- modified the left itertools implemented test cases to using parametrized decorator
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80145
Approved by: https://github.com/justinchuby, https://github.com/BowenBao
2022-07-06 03:48:54 +00:00
lezcano
828c787ea9 Make kl_div a composite function. (#80334)
Benchmarks: https://github.com/pytorch/pytorch/pull/80334#issuecomment-1167229285

Fixes https://github.com/pytorch/pytorch/issues/80158
Fixes https://github.com/pytorch/pytorch/issues/78867
Fixes https://github.com/pytorch/pytorch/issues/69230

Supersedes https://github.com/pytorch/pytorch/pull/79007
Supersedes https://github.com/pytorch/pytorch/pull/69212
Supersedes https://github.com/pytorch/pytorch/pull/19659
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80334
Approved by: https://github.com/ezyang
2022-07-04 19:33:43 +00:00
Simon Boehm
b00e4e8017 [ONNX] Convert aten numpy_T to ONNX transpose (#79269)
This PR addes support for exporting `aten::numpy_T` to ONNX

Fixes #51183

Pull Request resolved: https://github.com/pytorch/pytorch/pull/79269
Approved by: https://github.com/justinchuby, https://github.com/garymm
2022-07-02 07:01:48 +00:00
qqaatw
3dec9fd09f [ONNX] Fix hardshrink and softshrink output's shape (#79695)
Part of #79263

Before: When the shape of the two functions is `[]`, the reduced output has `[1]` shape.
After: The shape of the two functions is now `[]` as PyTorch's behavior.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/79695
Approved by: https://github.com/justinchuby, https://github.com/BowenBao
2022-06-28 20:00:10 +00:00
Ti-Tai Wang
33761c80d2 [ONNX] Move no runtime tests out of runtime test cases (#80078)
- Moved no_runtime test cases from runtime script to no_runtime
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80078
Approved by: https://github.com/BowenBao
2022-06-26 18:46:25 +00:00
Jinkun Lin
86682caf93 [ONNX Export] Use half_pixel instead of pytorch_half_pixel. (#80003)
Fixes #79361

Pull Request resolved: https://github.com/pytorch/pytorch/pull/80003
Approved by: https://github.com/BowenBao
2022-06-24 18:25:29 +00:00
BowenBao
e1caf3465b [ONNX] Refactor TestONNXRuntime with parameterized class
* Refactor `TestONNXRuntime` with parameterized class, making it obvious whether
  the unittest is testing on TorchScript or trace based export.
* Refactor rnn_tests with parametrized test cases.

Fixes #75630

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

Approved by: https://github.com/justinchuby, https://github.com/garymm
2022-06-22 17:08:44 +00:00
Jane Xu
e675c33c51 [onnx] have all classes that call setUp inherit from common_utils.setUp (#79543)
The remaining one is `class _TestONNXRuntime:`, which intentionally doesn't want to inherit from anything, so I left it alone.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/79543
Approved by: https://github.com/malfet
2022-06-20 22:29:25 +00:00
qqaatw
24a0467149 Add opset16 onnx support for torch.scatter_add (#79103)
Fixes #32960

Pull Request resolved: https://github.com/pytorch/pytorch/pull/79103
Approved by: https://github.com/BowenBao
2022-06-19 06:37:00 +00:00
BowenBao
73f6601cfc [ONNX] Refactor heavy memory usage tests
* Move memory heavy tests from `test_pytorch_onnx_onnxruntime.py` to
  `test_models_onnxruntime.py`. The former is run in parallel in CI,
  while the latter is not. A change is that the moved tests are now
  only covered in default opset export.
* Refactor and create base class for tests that export model to ONNX
  and verify with ONNX Runtime. The new base class are parameterized
  with `opset_version` and `is_script`. Further work can be done to
  refactor existing test classes in `test_pytorch_onnx_onnxruntime.py`.
  See #75630
* Reduce unnecessarily large tensor size in
  `test_pytorch_onnx_onnxruntime.py` to further reduce memory usage
  and test time.

After this PR, the running time for `test_pytorch_onnx_onnxruntime.py`
is reduced from `1338.82s (0:22:18)` to `225.07s (0:03:45)`,
benchmarked on 10900x with `-n 10`.

Fixes #79179

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

Approved by: https://github.com/justinchuby, https://github.com/garymm
2022-06-18 02:28:00 +00:00
Justin Chu
c8b9b6266b [ONNX] Fix arg type in _set_training_mode (#78583)
When `TrainingMode.PRESERVE` is set for export, the exporter used to change the model's training mode based on some logic. Now we respect the option and not touch the model's training state.

- Previously `_set_training_mode`'s behavior doesn't match what the global variable expects. This PR removes the deprecated `_set_training_mode` and makes the type correct.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/78583
Approved by: https://github.com/BowenBao
2022-06-15 23:47:12 +00:00
Jane Xu
d2fbfe7fce [ONNX] subscribe onnx to our custom test infra (#79546)
Remove as many references as can be easily done of unittest in favor of our custom infra.

Left a todo where I could not easily replace unittest.main with run_tests()
Pull Request resolved: https://github.com/pytorch/pytorch/pull/79546
Approved by: https://github.com/seemethere
2022-06-15 15:00:04 +00:00
Justin Chu
d3ef5c3fa3 [ONNX] Clean up __init__ in torch.onnx (#78446)
- Move definitions in `__init__` to internal classes and expose them by importing to init (prevent circular dependencies): https://github.com/pytorch/pytorch/wiki/torch.onnx-Namespacing
  - Context classes and enums are moved to `_exporter_states.py`
  - Exceptions are moved to `errors.py`
- Define `__all__` for torch.onnx. https://github.com/pytorch/pytorch/wiki/Public-API-definition-and-documentation
- Moved `utils.__IN_ONNX_EXPORT` to `GLOBALS.in_onnx_export`
- Deprecated `torch.onnx._export`

Precedes #78231

Using this as an aid for finding public functions:

```python
list(filter(lambda x: not x.startswith("_"), torch.onnx.utils.__dict__.keys()))
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/78446
Approved by: https://github.com/BowenBao
2022-06-14 04:35:06 +00:00
qqaatw
1a845579b6 [ONNX] Fix inconsistent rand dtype (#79193)
ONNX export of torch.rand produced different data type. This PR makes the type correct

Fixes #77845

Pull Request resolved: https://github.com/pytorch/pytorch/pull/79193
Approved by: https://github.com/justinchuby, https://github.com/garymm
2022-06-14 03:06:39 +00:00
qqaatw
2bafb42a0a Add onnx support for movedim and moveaxis (#78931)
Fixes #68918

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78931
Approved by: https://github.com/BowenBao
2022-06-09 19:41:09 +00:00
shubhambhokare1
d26c575ff5 [ONNX] Enable script tests based on recent exporter changes (#77254)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77254
Approved by: https://github.com/justinchuby, https://github.com/BowenBao
2022-06-08 17:18:49 +00:00
Li-Huai (Allan) Lin
b45d303dac Add onnx support for torch.lerp (#78891)
Fixes #68384

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78891
Approved by: https://github.com/BowenBao
2022-06-08 17:16:45 +00:00
qqaatw
e4c3e98a48 Add onnx support for torch.tensor_split (#77437)
Fixes #73454

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77437
Approved by: https://github.com/garymm
2022-06-08 06:06:34 +00:00
Justin Chu
def778527e [ONNX] Quantization support for five ops (#78103)
- Add quantization support for `interpolate`, `avgpool`, `sigmoid` and `add_relu`
- Return the inputs to ListUnpack if the previous node is ListConstruct so that `ListConstruct` and `ListUnpack` are canceled and removed in the jit passes. ONNX doesn't support them.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/78103
Approved by: https://github.com/garymm
2022-06-03 20:22:07 +00:00
Jinkun Lin
0f05e39870 [ONNX] Fix shape inconsistency when exporting scalar log2 (#78701)
This is a simple fix addressing the exportation when the input to `torch.log2` is scalar. `log2(x)` will be exported as `log(x) / log(2)`, which creates a `log` node followed by a `div` node that divides it by a constant. The constant is constructed not as a scalar but as a tensor of shape `[1]`, so a scalar input here will get broadcasted creating the output tensor with shape `[1]`, while originally the torch model's output is a scalar.

```python
import torch
import onnx
import numpy as np

class Model(torch.nn.Module):
    def forward(self, x):
        return torch.log2(x)

x = torch.tensor(1.)  # scalar
model = Model()
torch.onnx.export(model, (x, ), "output.onnx", opset_version=14,
                  output_names=['o0'], input_names=['i0'])
y_trh = model(x).numpy()

model = onnx.load("output.onnx")
print(model.graph.output[0])

import onnxruntime as ort
sess = ort.InferenceSession(
    "output.onnx", providers=['CPUExecutionProvider'])
y_ort = sess.run(['o0'], {'i0': x.numpy()})[0]
assert y_ort.shape == y_trh.shape, 'shape mismatch, ORT is `{}` but PyTorch is `{}`'.format(
    y_ort.shape, y_trh.shape)

```
The resulting ONNX model has an output of shape `[1]` and causes shape mismatch between ORT and PyTorch. The output:
```
name: "o0"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {
        dim_value: 1
      }
    }
  }
}

Traceback (most recent call last):
  File "test.py", line 501, in <module>
    y_ort.shape, y_trh.shape)
AssertionError: shape mismatch, ORT is `(1,)` but PyTorch is `()`
```
After the fix, the output becomes:
```
name: "o0"
type {
  tensor_type {
    elem_type: 1
    shape {
    }
  }
}
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78701
Approved by: https://github.com/justinchuby, https://github.com/BowenBao
2022-06-03 18:41:17 +00:00
Gary Miguel
fb7a761ffd [ONNX] reduce log spam when exporting dropout in training mode (#78309)
The default for `torch.onnx.export` is `TrainingMode.EVAL`:
0d76299ff7/torch/onnx/__init__.py (L63)

That means that this warning is only printed when the caller overrides
that and explicitly specifies that they want training ops like Dropout.
We should assume the user knows what they're doing and not warn.

Also set `do_constant_folding=False` in the dropout related training tests. Without this, warnings are printed like:
```
UserWarning: It is recommended that constant folding be turned off ('do_constant_folding=False') when exporting the model in training-amenable mode
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/78309
Approved by: https://github.com/justinchuby, https://github.com/BowenBao
2022-06-03 18:41:07 +00:00
BowenBao
cfc968956c [ONNX] Update CI test script to run parallel by default (#78200)
Also update default process count to auto, matching the CI machine
cpu core count.

Fixes #77678

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78200
Approved by: https://github.com/garymm
2022-06-02 00:25:17 +00:00
BowenBao
6883b0ce9f [ONNX][WIP] Refactor verification.py
* Rename arguments, code clean up.
* Refactor functions to smaller reusable functions.

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

Approved by: https://github.com/justinchuby, https://github.com/garymm
2022-05-31 18:49:39 +00:00
Mike Ruberry
089203f8bc Updates floor_divide to perform floor division (#78411)
Fixes https://github.com/pytorch/pytorch/issues/43874

This PR changes floor_divide to perform floor division instead of truncation division.

This is a BC-breaking change, but it's a "bug fix," and we've already warned users for several releases this behavior would change.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/78411
Approved by: https://github.com/ngimel
2022-05-29 21:28:45 +00:00
BowenBao
483bb4f0cb [ONNX] Extract export verification as standalone api from unittest
The verification logic is refactored and extracted from
`test_pytorch_onnx_onnxruntime.py`.

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

Approved by: https://github.com/justinchuby, https://github.com/garymm
2022-05-26 19:49:19 +00:00
Justin Chu
161e931156 [ONNX] Modernize python syntax (#77935)
Use pyupgrade(https://github.com/asottile/pyupgrade) and flynt to modernize python syntax

```sh
pyupgrade --py36-plus --keep-runtime-typing torch/onnx/**/*.py
pyupgrade --py36-plus --keep-runtime-typing test/onnx/**/*.py
flynt torch/onnx/ --line-length 120
```

- Use f-strings for string formatting
- Use the new `super()` syntax for class initialization
- Use dictionary / set comprehension
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77935
Approved by: https://github.com/BowenBao
2022-05-24 22:52:37 +00:00
Xiang Gao
f274558018 Bitwise ops improvements (#77621)
- Bitwise shift remove floating point support
- Bitwise and, or, xor add (scalar, tensor) overload
- Use `test_ops.py` to test these ops, including error cases
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77621
Approved by: https://github.com/ngimel
2022-05-17 21:16:42 +00:00
Gary Miguel
bdacc0856c [ONNX] handle equality checks on devices (#77203)
Previously the newly added `test_device_eq` would fail since the inputs
to `Equal` were invalid. Handle this by replacing its inputs with a
fixed tensor `Constant`. This is OK since ONNX doesn't have the concept
of different devices.

Discovered during investigation of
https://github.com/microsoft/onnx-converters-private/issues/9
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77203
Approved by: https://github.com/BowenBao
2022-05-13 21:41:31 +00:00
Justin Chu
1dd7336441 [ONNX] Add quantization support for maxpool (#77393)
Support quantization for maxpool exporting to ONNX.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77393
Approved by: https://github.com/BowenBao
2022-05-13 00:32:25 +00:00
Xiang Gao
cc9d0f309e lshift and rshift stop support floating types (#77146)
Fixes #74358

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77146
Approved by: https://github.com/ngimel
2022-05-11 22:29:30 +00:00
Justin Chu
5dd1c67776 [ONNX] Format ONNX python with black
Format all onnx python code with black and isort with

```sh
isort torch/onnx/ test/onnx
black torch/onnx/ test/onnx
```

Updated lintrunner config to include these paths.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/76754
Approved by: https://github.com/suo, https://github.com/BowenBao
2022-05-05 00:19:22 +00:00
BowenBao
679fc90cdb [ONNX] Support optional type (#68793) (#73284)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73284

Some important ops won't support optional type until opset 16,
so we can't fully test things end-to-end, but I believe this should
be all that's needed. Once ONNX Runtime supports opset 16,
we can do more testing and fix any remaining bugs.

Test Plan: Imported from OSS

Reviewed By: albanD

Differential Revision: D34625646

Pulled By: malfet

fbshipit-source-id: 537fcbc1e9d87686cc61f5bd66a997e99cec287b

Co-authored-by: BowenBao <bowbao@microsoft.com>
Co-authored-by: neginraoof <neginmr@utexas.edu>
Co-authored-by: Nikita Shulga <nshulga@fb.com>
(cherry picked from commit 822e79f31ae54d73407f34f166b654f4ba115ea5)
2022-05-04 20:24:30 +00:00
Masaki Kozuki
0ae3aa648e [torch.onnx] support torch.nn.functional.grid_sample
summary

- Adds `F.grid_sample` support
- Adds a test case

Fixes #27212
Pull Request resolved: https://github.com/pytorch/pytorch/pull/76159
Approved by: https://github.com/justinchuby, https://github.com/BowenBao
2022-05-02 22:07:58 +00:00
BowenBao
8d31706b9e [ONNX] Support restricted quantized range for activation.
PyTorch restricts activations to be in the range (0, 127).
In ONNX, the supported ranges are (0, 255) and (-128, 127),
respectfully, uint8 and int8. This PR extends support for range
(0, 127), by adding additional clipping when detected.

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

Approved by: https://github.com/garymm
2022-04-25 01:17:21 +00:00
BowenBao
cada2cd3ae [ONNX] Support per channel quantization
Extending the support for quantization with per channel quantization.
An extra attribute `axis` can be found for per channel quantized tensors,
most commonly in quantized weight of Convolution or Linear module.
The PR adds support to correctly parse the `axis` attribute, and map to
ONNX representation in `QuantizeLinear` and `DequantizeLinear`.

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

Approved by: https://github.com/garymm
2022-04-25 01:14:57 +00:00
BowenBao
2c748b7573 [ONNX] Trace model if quantization is detected
Previously pre-tracing model is required for exporting quantized model.
e.g. calling `traced_m = torch.jit.trace(model, inputs)` and export `traced_m`.
The reason was quantized weights are stored in a unique `PackedParam` structure,
and they need to be handled by tracing to be exportable.
This PR enables export api to call tracing underneath if it detects quantization
in the model.

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

Approved by: https://github.com/garymm
2022-04-22 17:27:32 +00:00
shubhambhokare1
e0c1786587 [onnx] Add support for torch.cross and torch.cdist
Add support for following operators:
- torch.cross
- torch.linalg.cross
- torch.cdist
- torch.nn.pairwisedistance
Pull Request resolved: https://github.com/pytorch/pytorch/pull/75278
Approved by: https://github.com/BowenBao
2022-04-21 20:35:25 +00:00
BowenBao
45a7ae1929 [ONNX] Add verbose option to onnx unittests
Makes it more convenient to view exported graphs when debugging
unittest cases.

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

Approved by: https://github.com/garymm
2022-04-19 21:26:19 +00:00
Jay Zhang
166568d49f Enhance exporting torch.minimum() function to ONNX so it can handle parameters with different dtype.
Handle the case that the parameters of torch.minimum() have different dtypes. Add tests as well.

Fixes #76022
Pull Request resolved: https://github.com/pytorch/pytorch/pull/75861
Approved by: https://github.com/thiagocrepaldi, https://github.com/garymm
2022-04-19 16:36:10 +00:00
BowenBao
7545e2a4d6 [ONNX] Add constant fold for onnx::ReduceProd
One of the origins for `onnx::ReduceProd` is `aten::numel`.
Adding constant fold support for `onnx::ReduceProd` closes the gap
and enables symbolic shape inference for `aten::numel` nodes that
has static shape input.

One example is `torch.nn.EmbeddingBag` when input is 2d. An `Offset`
tensor will be created by `tensor.numel()`. This `Offset` can be
properly exported as constant now, if the input has static shape.

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

Approved by: https://github.com/garymm
2022-04-13 22:36:02 +00:00
BowenBao
aa51ee2345 Enable numel tracing
clang-format

resolve onnx test failure

update expect file

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

Approved by: https://github.com/garymm, https://github.com/eellison, https://github.com/malfet
2022-04-13 22:23:41 +00:00
BowenBao
f78e0fc956 [ONNX] Support aminmax
Support exporting `torch.aminmax`.
One of the use case is exporting fake quantized models. The observer calls 1601a4dc9f/torch/ao/quantization/observer.py (L447).

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75714
Approved by: https://github.com/garymm
2022-04-13 19:18:46 +00:00
BowenBao
6305e572ed [ONNX] Support dynamic scale & zero_point for fake_quantize_per_tensor_affine
Dynamic scale & zero_point requires opset 13 `ONNX::QuantizeLinear`
and `ONNX::DequantizeLinear`.
Improved error message when scale is not constant for opset 10 symbolic function.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/75697
Approved by: https://github.com/garymm
2022-04-13 19:17:58 +00:00
ganler
c1f0e6e763 [ONNX] Make Non-Float Op Exportation Compatible to Avoid Invalid ONNX Models
There are a few ONNX operators do not support non-float (e.g., integer) inputs at early versions. For example, Clip supports non-float types until [opset 12](https://github.com/onnx/onnx/blob/main/docs/Changelog.md#type-constraints-280), that said older versions like [opset 6](https://github.com/onnx/onnx/blob/main/docs/Changelog.md#type-constraints-107) cannot deal with integer types.

I initially find such a bug in Clip (https://github.com/pytorch/pytorch/pull/70584), but later found more:
1. Clip < 12;
2. Min/Max < 12;
3. ReLU < 14;
4. Pad < 11;

In PyTorch, if we export Max-11 with integer inputs, actually the exportation will succeed; however, fail when imported by other frameworks like ONNXRuntime.

```python
import torch

class Net(torch.nn.Module):
    def __init__(self) -> None:
        super().__init__()

    def forward(self, x: torch.Tensor):
        return torch.max(x, x + 1)

net = Net()
onnx_model = 'test.onnx'

torch.onnx.export(net, (torch.zeros((3, 3), dtype=torch.int32),),
                  onnx_model, verbose=True, opset_version=11)
```

This is an unexpected behavior as we want to ensure that every model exported by PyTorch is valid (https://github.com/pytorch/pytorch/pull/70584#issuecomment-1020636579). Theoretically, we can simply forbid such cases (e.g., `Clip<int>` < 12, `ReLU<int>` < 14). But actually we can enhance the compatibility and flexibility of PyTorch by simply casting inputs of those operators into float tensors, which allows the float operator functions, and then casting it back to original types.

This PR implements the second approach to achieve better compatibility in PyTorch.

@garymm  @thiagocrepaldi

Pull Request resolved: https://github.com/pytorch/pytorch/pull/72401
Approved by: https://github.com/garymm, https://github.com/thiagocrepaldi
2022-04-11 23:26:44 +00:00
Eugene Lyapustin
d88a116015 Fix exporting models to ONNX without allow_tf32 in _convolution call
Fixes #75098
Pull Request resolved: https://github.com/pytorch/pytorch/pull/75099
Approved by: https://github.com/BowenBao
2022-04-08 17:23:36 +00:00
BowenBao
e60c403b2f [ONNX] Use fixed test input for flaky test
Fixes #74501

Random weight and input sometimes result in slight output mismatch
between PyTorch and ONNX model. This is expected as the ONNX
model is executed in QDQ format.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75285
Approved by: https://github.com/garymm
2022-04-07 18:53:25 +00:00