You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
VariableVersion::join_with has hidden invariant / mark_shared_storage order matters (and is unchecked) #3077
I was working on the PyTorch paper, and while trying to explain join_with, I realized that there are problems.
From its name, you'd expect VariableVersion::join_with to be a symmetric operation; in fact, there is even a comment (added by me) claiming this is the case. However, a quick look at the implementation makes it clear that it is NOT symmetric:
There are three call sites of join_with, and one of them accepts a user provided pair of variables to join counters (triggered by mark_shared_storage). From this, I was able to reverse engineer an example where we silently do the wrong thing:
import torch
x = torch.autograd.Variable(torch.Tensor([[0.1, 0.2], [0.3, 0.4]]), requires_grad=True)
class SomeFn(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
y = x.view(4,1)
ctx.mark_shared_storage((y, x))
#ctx.mark_shared_storage((x, y))
return y
@staticmethod
def backward(ctx, grad_output):
return grad_output.contiguous().view(2,2)
b = x.tanh()
a = SomeFn.apply(b)
del b
a.add_(2)
#a = a.add(2)
a.sum().backward()
print(x.grad)
This program does not fail, despite the fact that the add_ operation is clobbering a view of b, which is needed to perform tanh backward computation. You can see the correct output by commenting a.add_(2) and uncommenting a = a.add(2); you can trigger an error by switching the order of x and y in the mark_shared_storage call (see the commented out segment.) The problem is that if you swap the order of variables in mark_shared_storage, we end up discarding the "old" version counter associated with b (which, when incremented, would trigger our sanity checking on backwards) and using the version counter for a instead (which isn't hooked up to anything; it was just generated.)
It's easy enough to add the necessary check to mark_shared_storage processing, but it seems like a rename of join_with is also in order; it is more of a "set storage to" operation, and this new name makes clear the asymmetry. Arguably, we should be forced to specify the version counter when creating a Variable in the first place, but this is somewhat inconvenient to do.
Activity
ezyang commentedon Feb 20, 2018
@goldsborough You refactored the variable version mechanics recently; does this ticket still apply?
goldsborough commentedon Feb 20, 2018
join_withno longer exists as far as I can tell?ezyang commentedon Feb 20, 2018
Closed. :)
Prefer cublas when TORCH_BLAS_PREFER_CUBLASLT is false (#pytorch#174377…
Prefer cublas when TORCH_BLAS_PREFER_CUBLASLT is false (#pytorch#174377…
[Test] Classify the test classes in test_dlpack.py
[Test] Classify the test classes in test_dlpack.py
[Test] Classify the test classes in test_dlpack.py
[Test] Classify the test classes in test_dlpack.py
[Test] Classify the test classes in test_dlpack.py
[Test] Classify the test classes in test_dlpack.py
[Test] Classify the test classes in test_dlpack.py
[Test] Widen the DLPack copy tests from CUDA to accelerators
[Test] Widen the DLPack copy tests from CUDA to accelerators
[Test] Widen the DLPack copy tests from CUDA to accelerators
[Test] Widen the DLPack copy tests from CUDA to accelerators