Skip to content

VariableVersion::join_with has hidden invariant / mark_shared_storage order matters (and is unchecked) #3077

Closed

Description

@ezyang

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:

  void join_with(VariableVersion &other) {
    if (this == &other) {
      return;
    }
    cleanup();
    version_block = other.version_block;
    version_block[1]++;
    version_block[2]++;
  }

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.

Related: #3006.

Activity

  1. ezyang commented on Feb 20, 2018

    @ezyang
    ContributorAuthor

    @goldsborough You refactored the variable version mechanics recently; does this ticket still apply?

  2. goldsborough commented on Feb 20, 2018

    @goldsborough
    Contributor

    join_with no longer exists as far as I can tell?

  3. ezyang commented on Feb 20, 2018

    @ezyang
    ContributorAuthor

    Closed. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions