add substract of max and testcase (#63132)
authorleslie-fang-intel <leslie.fang@intel.com>
Sat, 14 Aug 2021 03:49:27 +0000 (20:49 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Sat, 14 Aug 2021 03:50:49 +0000 (20:50 -0700)
commit385b082854b47cfc5c6cdadf9d07aa5f338c3c1b
treee0aff866aa9767528bc79edd489b475b1b2dd824
parentbaedb559e352e7766adb49e501764f2b569bf442
add substract of max and testcase (#63132)

Summary:
As discussed here https://github.com/pytorch/pytorch/pull/62897, in the path of BF16/non-last-dim Softmax, we miss the subtractions of max value which will cause the overflow in the `exp()` calculation when the value of input tensor is large, such as `1000.0`.
To avoid this issue, we add the subtractions of max value and the corresponding test cases in this PR.

Note w/o subtractions of max value(accidental reverts or changes), we will get the underlying error message of the test case
```
AssertionError: False is not true : Tensors failed to compare as equal!With rtol=0.05 and atol=0.05, found 103984 element(s) (out of 126720) whose difference(s) exceeded the margin of error (including 103984 nan comparisons). The greatest difference was nan (0.0 vs. nan), which occurred at index (0, 0, 0, 1).
```

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

Reviewed By: VitalyFedyunin

Differential Revision: D30280792

Pulled By: cpuhrsch

fbshipit-source-id: 722821debf983bbb4fec878975fa8a4da0d1d866
aten/src/ATen/native/cpu/SoftMaxKernel.cpp
test/test_nn.py