Error out on in-place (unary) ops on tensors that have internal overlap (#17927)
authorRichard Zou <rzou@fb.com>
Fri, 15 Mar 2019 14:41:08 +0000 (07:41 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 15 Mar 2019 14:50:19 +0000 (07:50 -0700)
commit3c977fb7cea9aeb2895166a13ed8ac13fd85fb1e
tree49a1caa7d13e8eb49ad541cb6eb1b826b1300865
parenta4123decf79468e1b4a1205d32356f0920d3ba01
Error out on in-place (unary) ops on tensors that have internal overlap (#17927)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17927
ghimport-source-id: 626d321e430b6b5c0ea3aa1eb9df8c1e2d058bf8

Stack:
* #17926 Implement at::has_internal_overlap helper function
* **#17927 Error out on in-place (unary) ops on tensors that have internal overlap**

On the way to #17935.

Works for CPU and CUDA on the following ops:
- abs_, acos_, asin_, atan_, ceil_, cos_, erf_, erfc_, exp_, expm1_
- floor_, log_, log10_, log1p_, log2_, round_, rsqrt_,
- sin_, sqrt_, tan_, tanh_, trunc_

This PR adds a check to see if the out/result tensor has internal
overlap. If it does, then we error out because the result **may** be
incorrect.

This is overly conservative; there are some cases where if the result is
the same as the input, the inplace operation is OK (such as floor_,
round_, and trunc_). However, the current code isn't organized in such a
way that this is easy to check, so enabling those will come in the future.

Reviewed By: ezyang

Differential Revision: D14438871

fbshipit-source-id: 15e12bf1fdb2ab7f74bb806e22bc74840bd6abd1
aten/src/ATen/MemoryOverlap.cpp
aten/src/ATen/MemoryOverlap.h
aten/src/ATen/native/cpu/UnaryOpsKernel.cpp
aten/src/THC/generic/THCTensorMathPointwise.cu
test/test_cuda.py
test/test_torch.py