Skip to content

Conversation

@yf225
Copy link
Contributor

@yf225 yf225 commented May 11, 2019

In https://github.com/pytorch/pytorch/pull/18223/files#diff-77a6f3462f2233b921d3042412fed6d3R178, we used auto saved_version_ = data_.unsafeGetTensorImpl()->version_counter().current_version() and then new_data_impl_copy->set_version_counter(saved_version_), which actually doesn't preserve the original semantics that var.set_data(tensor) should keep var's version counter object intact. This PR fixes the bug and adds test to make sure it doesn't happen again.

This PR is BC-breaking for v1.1.0, because v1.1.0 contains #18223 which introduces the bug. As an example, the following test passes on v1.0.1 and will pass on master after this PR is merged, but doesn't pass on v1.1.0:

x = torch.randn(2, 3)
xz = x[:]
assert x._version == xz._version
x.add_(1)
assert x._version == xz._version  # We can confirm that `x` and `xz` have the same version counter

x.data = torch.randn(3, 4)
x.add_(1)
assert x._version == xz._version  # We can confirm that `x` and `xz` still have the same version counter

@yf225 yf225 requested a review from gchanan May 11, 2019 03:39
@pytorchbot pytorchbot added the module: autograd Related to torch.autograd, and the autograd engine in general label May 11, 2019
@yf225 yf225 mentioned this pull request May 11, 2019
22 tasks
@yf225
Copy link
Contributor Author

yf225 commented May 11, 2019

@pytorchbot retest this please

# In-place op on `x` also updates version of `xz`,
# because they share the version counter
x.add_(1)
self.assertTrue(x._version == xz._version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some improvements to this test:

  1. change the variable through xz.
  2. check that changes post-set data actually increase the version counter.

@yf225 yf225 force-pushed the set_data_version_counter branch from 2e8cfb3 to f45dcaf Compare May 13, 2019 19:05
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yf225 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@yf225 merged this pull request in 1364104.

@yf225 yf225 added the module: bc-breaking Related to a BC-breaking change label May 14, 2019
@yf225 yf225 changed the title Fix version counter sharing in set_data() [BC-breaking] Fix version counter sharing in set_data() May 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: autograd Related to torch.autograd, and the autograd engine in general module: bc-breaking Related to a BC-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants