Commit Graph

438 Commits

Author SHA1 Message Date
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
BowenBao
50b6959c0f [ONNX] Support torch.amax and torch.amin
Fixes #75167

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75268
Approved by: https://github.com/garymm
2022-04-07 00:16:26 +00:00
Gary Miguel
ca374773b4 [ONNX] update default opset_version to 13 (#73898)
Summary:
And add a new tool to update it in the future, which follows the policy
of using "latest as of 18 months ago". This policy is meant to balance:
* recent enough to increase the odds of being able to successfully
  export
* old enough to increase the odds of exported model being runnable by
  different ONNX implementations

Related changes:

* test_models.py: explicitly fix opset_version to 9 rather than relying on default. Caffe2 doesn't support newer versions.
* symbolic_helper.py:
  * Remove a misleading comment
  * Remove unnecessary check in `_set_opset_version`
  * Use a range to define `_onnx_stable_opsets`
* test_pytorch_common.py:
  * Rename a variable from min -> max. I think it was a copy-paste error.
  * Make skip test messages more informative.
  * Remove unused `skipIfONNXShapeInference`. More on that below.
* test_pytorch_onnx_onnxruntime.py:
  * Make all the `TestCase` classes explicitly specify opset version.
  * Make `test_unsupported_pad` respect `opset_version` by using `run_test`
  * Unrelated simplification: make it obvious that all tests run with `onnx_shape_inference=True`. AFAICT this was already the case.
  * There was one test that was entirely disabled (test_tolist) because it was asking to be skipped whenever `onnx_shape_inference=True`, but it was always True. I changed the model being tested so as to preserve the intended test coverage but still have the test actually pass.

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

Reviewed By: msaroufim

Differential Revision: D35264615

Pulled By: malfet

fbshipit-source-id: cda8fbdffe4cc8210d8d96e659e3a9adf1b5f1d2
(cherry picked from commit b5e639e88828d34442282d0b50c977e610a2ba3a)
2022-04-07 00:02:31 +00:00
shubhambhokare1
ef41201d4a [ONNX] Add bucketize symbolic
Add support for torch.bucketize
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74856
Approved by: https://github.com/garymm
2022-04-06 20:13:45 +00:00
BowenBao
2ba496bc23 [ONNX] Fix 1d case flatten export
Fixes #74142

Previous check `dim is not None and end_dim == dim - 2` didn't consider `end_dim` being negative. However the error only occurs when input tensor has rank 1, and the rank is known to symbolic function. So a better fix is to return `input` directly when rank is 1.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/74595
Approved by: https://github.com/garymm
2022-03-23 23:50:51 +00:00
shubhambhokare1
d8c31a819d [ONNX] Modify int[] listConstruct to support tensor arg
Avoid creating unsqueeze nodes for ListConstruct Int[] ouput case with tensor inputs
For example, if the listConstruct is (tensor[2], 1, 5), avoid adding unsqueeze nodes for tensor[2]

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73927
Approved by: https://github.com/garymm
2022-03-23 17:43:43 +00:00
Vasilis Vryniotis
ea56b9d92d Passing explicit pretrained_backbone (#74372)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74372

In preparation to the multi-weight support porting, we pass explicitly the pretrained_blackbone value. We use the default value `True` for most cases, except for when the use-case is clearly a test and thus should avoid downloading the weights of the backbone.

Test Plan: running project unit-tests

Reviewed By: jdsgomes

Differential Revision: D34961147

fbshipit-source-id: cf29e42545302716a7cd3f3eb0d69e44d5fb6c73
(cherry picked from commit c4613b7abacd106d097de1b73b13af92132e1739)
2022-03-22 18:36:47 +00:00
Michael Suo
7ccfc114b5 skip flaky onnx quantized test
see: https://github.com/pytorch/pytorch/issues/74501

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

Approved by: https://github.com/BowenBao, https://github.com/malfet
2022-03-21 22:33:28 +00:00
BowenBao
57c7bf7fee [ONNX] Remove redundant warning for reshape
Fixes #73129.

The warning is not actionable and seems to be potentially false alarming.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73265
Approved by: https://github.com/shubhambhokare1, https://github.com/garymm
2022-03-14 20:58:26 +00:00
BowenBao
b553e73c01 [ONNX] Enable topk export with non-int64 k
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73761
Approved by: https://github.com/garymm
2022-03-10 19:32:22 +00:00
BowenBao
97ae431e3e [ONNX] Add symbolic support for torch.nn.cosinesimilarity (#72128) (#73283)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73283

* Add support for torch.nn.cosine_similarity

* Remove fallback logic

* Fix onnx test failures

* Fix opset version

* Modify rtol

* Add aten fallback mode

* fix mypy

* gate with caffe2 fallback

Test Plan: Imported from OSS

Reviewed By: jbschlosser

Differential Revision: D34625650

Pulled By: malfet

fbshipit-source-id: bf15d32b1d7055d0ca166d9941ba90b5c8e81cc2
(cherry picked from commit 7086031c52e1bea9bead6966d44e2635060194db)
2022-03-09 14:26:18 +00:00
BowenBao
95b1232752 [ONNX] use onnxruntime 1.10 in CI (#69271) (#73282)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73282

Enable tests that are fixed by ORT 1.10

Test Plan: Imported from OSS

Reviewed By: jbschlosser

Differential Revision: D34625649

Pulled By: malfet

fbshipit-source-id: 103ae5275f3a7eb891ad4dd2b606ebf198a800fe
(cherry picked from commit e0ddeb4205678e993d744efa06939b0977463a76)
2022-03-09 14:26:18 +00:00
BowenBao
6c26bf0e72 [ONNX] Fix repeat interleave when repeats and dim is 1
Fixes #62936

Root cause is `Split` in `_repeat_interleave_split_helper` returns single element when `outputs=1`, instead of list if otherwise.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73760
Approved by: https://github.com/garymm
2022-03-07 20:44:35 +00:00
BowenBao
4a74285e97 [ONNX] Rewrite linspace symbolic
Original symbolic relies on ONNX Range with float inputs. The results are unstable due to precision issues.

Fixes #73559

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73610
Approved by: https://github.com/fatcat-z, https://github.com/garymm
2022-03-04 21:45:52 +00:00
BowenBao
8280919fe6 [ONNX] Export bias requantize steps to ONNX
Original `bias` is float in PyTorch. Quantization is applied in kernel.
To mimic behavior in ONNX, export the `bias` quantization step,
then append the dequantization step to ready `bias` for unquantized operators.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73336
2022-03-01 01:01:23 +00:00
BowenBao
bd4902d81f [ONNX] Add Squeeze/Unsqueeze dynamic dimensions support when opset >= 13 (#71158)
* Add Squeeze/Unsqueeze dynamic axes support when opset >= 13

Co-authored-by: hwangdeyu <dejack953outlook.com>
Co-authored-by: Gary Miguel <garymmgarymm.org>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73104
2022-02-23 06:41:15 +00:00
BowenBao
80291dff43 [ONNX] Add torch.nan_to_num and torch.maximum/minimum symbolic (#72090)
* Add nan_to_num symbolic

* Restructure if statements

* Add torch.maximum and torch.minimum support

* Squash tests

* Add dependency on input dtype

* Add documentation

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73103
2022-02-23 06:38:11 +00:00
BowenBao
40de6b80ee [ONNX] Add infra for quantized model export and support quantized mobilenet v3 (#72215)
* Add infrastructure and helper functions to enable future work for other quantized operators and models.
* Add export for quantized operators needed by torchvision mobilenet v3 large.
    * ATen namespace: hardsigmoid, flatten, adaptive_avg_pool, quantize_per_tensor, dequantize.
    * Quantized namespace: conv2d, conv2d_relu, hardswish, add, mul.
* Numerous bug fixes, in unpack_quantized_weight.cpp, symbolic functions, and unit test.

Co-authored-by: BowenBao <bowbaomicrosoft.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73102
2022-02-23 06:22:58 +00:00
BowenBao
cc2aad2ef2 [ONNX] Add symbolic for torch.addcmul (#72126)
* Add addcmul op

* Remove required_grad

Pull Request resolved: https://github.com/pytorch/pytorch/pull/73101
2022-02-22 22:48:18 +00:00
shubhambhokare1
671c8a459a [ONNX] Add pixel_unshuffle support in opset 9
Current we are unable to utilize ONNX's SpaceToDepth operator due to the lack of the mode_s attribute, hence we add an alternative symbolic in opset 9 to support pixel_unshuffle

- Adds support for pixel_unshuffle in opset9
- Adds support for dynamic input shapes for pixel_shuffle and pixel_unshuffle
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72449
2022-02-19 00:15:16 +00:00
BowenBao
5843fea94d [ONNX] Add export support for linalg norm (#66575)
* Add matrix_norm

* Add vector norm

* Fixe flake

* Fixe flake

* nit fixes

* Nit fixes

* Restructure and add comments

Pull Request resolved: https://github.com/pytorch/pytorch/pull/72987
2022-02-18 18:30:16 +00:00
BowenBao
32f6a1e2a2 [ONNX] First version of quantized model export: Support quantized.Linear (#69232)
Co-authored-by: David Fan <jiafamicrosoft.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/72986
2022-02-18 18:27:26 +00:00
ganler
3d8b6d3361 fix: onnx PReLU unidirectional broadcasting
Fixes https://github.com/pytorch/pytorch/issues/70570

Pull Request resolved: https://github.com/pytorch/pytorch/pull/70571
2022-02-16 22:28:08 +00:00
Ryan Spring
4f8b986e28 Implement Tanh Gelu Approximation (#61439)
Summary:
1. Implements https://github.com/pytorch/pytorch/issues/39853
2. Adds approximate boolean flag to Gelu
3. Enables Tanh Gelu approximation
4. Adds double backward support for Gelu
5. Enable Tanh Gelu in NvFuser

```
def gelu(x, approximate : str = 'none'):
    if approximate == 'tanh':
        # sqrt(2/pi) = 0.7978845608028654
        return 0.5 * x * (1.0 + torch.tanh(0.7978845608028654 * (x + 0.044715 * torch.pow(x, 3.0))))
    else:
        return x * normcdf(x)
```

Linking XLA PR - https://github.com/pytorch/xla/pull/3039

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

Reviewed By: VitalyFedyunin

Differential Revision: D33894937

Pulled By: jbschlosser

fbshipit-source-id: b65e8fb6ea66168af8f34f45ed50e92737a33851
(cherry picked from commit 6e986f91a9)
2022-02-14 03:40:32 +00:00
BowenBao
7884c2bbe2 [ONNX] Add Concat to Scalar type analysis JIT pass (#69227) (#69548)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR https://github.com/pytorch/pytorch/pull/24378/

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf)
2022-02-11 22:05:15 +00:00
BowenBao
308de30abc [ONNX] Support embedding_renorm ONNX export
Composite using ONNX operators for same logic from here 0a07488ed2/aten/src/ATen/native/Embedding.cpp (L153)

Replaced #72560
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72738
2022-02-11 22:02:22 +00:00
BowenBao
03afd86295 [ONNX] Fix lstm reshape shape inference regression
Fixes #72399
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72532
2022-02-11 19:40:22 +00:00
BowenBao
04c5d978b9 [ONNX] Refactor _run_symbolic_function (#67573) (#68491)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/68491

* Allows implementing symbolic functions for domains other than `aten`, for example `prim`, in symbolic_opset#.py.
* Allows symbolic function to access extra context if needed, through `SymbolicFunctionState`.
  * Particularly, the `prim::PythonOp` special case can access node without the need of passing node through inputs. Updates will be made downstreams, and in a follow-up PR we will remove the previous workaround in exporter.
* `prim::Loop`, `prim::If`, etc are now moved outside of `_run_symbolic_function` from utils.py, and to symbolic_opset9.py.

Motivation for this change:
- Better maintainability and reducing complexity. Easier to add symbolic for operators, both simple and complex ones (that need additional context), without the former needing to know the existence of the latter.
- The design idea was long outdated. prim ops are no longer rare special cases, and they shouldn't all be handled inside `_run_symbolic_function`. As a result this function becomes too clumsy. There were also prim ops symbolic added in symbolic_opset#.py with signature `prim_[opname]`, creating separation and confusion.

Test Plan: Imported from OSS

Reviewed By: jansel

Differential Revision: D32483782

Pulled By: malfet

fbshipit-source-id: f9affc31b1570af30ffa6668da9375da111fd54a

Co-authored-by: BowenBao <bowbao@microsoft.com>
(cherry picked from commit 1e04ffd2fd)
2022-02-11 18:35:35 +00:00
Steven Troxler
730fef25c7 Convert type comments to annotations in caffe2/test/onnx/ (#72632)
Summary:
Convert type comments in caffe2/test/onnx/

Produced by running:
```
python -m  libcst.tool codemod convert_type_comments.ConvertTypeComment caffe2/test/onnx/
```
from the parent directory.

One question is whether we actually want to scrap type comment here. There are some jit tests where we're explicitly aiming to validate py2-style type comments; I don't think this test is one of those cases but if I'm misreading it I can close the PR.

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

Reviewed By: msaroufim

Differential Revision: D34112196

Pulled By: stroxler

fbshipit-source-id: a3d18cb36e7eeb4af9be781e98776bf24b96b854
(cherry picked from commit 9301019e51)
2022-02-11 00:37:29 +00:00
Nikita Shulga
74c44ba9d6 Revert D33850228: [pytorch][PR] Implement Tanh Gelu Approximation
Test Plan: revert-hammer

Differential Revision:
D33850228 (23d03025dc)

Original commit changeset: 3cc33fb298e4

Original Phabricator Diff: D33850228 (23d03025dc)

fbshipit-source-id: 9436e7df73c2b2e2011f321674f24973316d3692
(cherry picked from commit c9efb58223)
2022-01-31 17:44:19 +00:00
Ryan Spring
23d03025dc Implement Tanh Gelu Approximation (#61439)
Summary:
1. Implements https://github.com/pytorch/pytorch/issues/39853
2. Adds approximate boolean flag to Gelu
3. Enables Tanh Gelu approximation
4. Adds double backward support for Gelu
5. Enable Tanh Gelu in NvFuser

```
def gelu(x, approximate : str = 'none'):
    if approximate == 'tanh':
        # sqrt(2/pi) = 0.7978845608028654
        return 0.5 * x * (1.0 + torch.tanh(0.7978845608028654 * (x + 0.044715 * torch.pow(x, 3.0))))
    else:
        return x * normcdf(x)
```

Linking XLA PR - https://github.com/pytorch/xla/pull/3039

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

Reviewed By: cpuhrsch

Differential Revision: D33850228

Pulled By: jbschlosser

fbshipit-source-id: 3cc33fb298e480d7ecc5c67716da019d60c6ab33
(cherry picked from commit 3a53b3e94f)
2022-01-31 17:07:45 +00:00
Joel Schlosser
cb823d9f07 Revert D33744717: [pytorch][PR] Implement Tanh Gelu Approximation
Test Plan: revert-hammer

Differential Revision:
D33744717 (f499ab9cef)

Original commit changeset: d64532a562ed

Original Phabricator Diff: D33744717 (f499ab9cef)

fbshipit-source-id: 396c3f63de5865f894dbc353d0790a01a624be93
(cherry picked from commit e9fb2d1db1)
2022-01-28 18:35:01 +00:00