Skip to content

Conversation

@yf225
Copy link
Contributor

@yf225 yf225 commented Feb 12, 2019

Currently, when the input tensor self is not contiguous, tril_ and triu_ calls self = self.contiguous(), which allocates a new contiguous tensor and assign it to self. This effectively changes the input tensor self's pointer and will break downstream code after Variable/Tensor merge.

This PR fixes it so that tril_ and triu_ always update the input tensor in-place and preserve the input tensor's TensorImpl.

@yf225 yf225 requested a review from gchanan February 12, 2019 22:37
@ezyang
Copy link
Contributor

ezyang commented Feb 12, 2019

I don't think this fix is still right. If I do an inplace operation on a view of some big tensor, I expect the inplace to be reflected in the appropriate memory locations on the big tensor. That's not what this patch does.

What you should do is compute it out of place, and then do a copy into the old tensor to write it back.

@gchanan
Copy link
Contributor

gchanan commented Feb 12, 2019

agree with @ezyang.

@ssnl
Copy link
Collaborator

ssnl commented Feb 12, 2019

Alternatively, it shouldn't be that hard to fix the kernels to work with arbitrarily strided tensors.

"x_nc should remain non-contiguous")
elif s < -3:
self.assertTrue(x_nc.is_contiguous(),
"x_nc should become contiguous")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since making a non-contiguous tensor contiguous is always guaranteed to be an out-of-place operation, which breaks the in-place property of triu_ and tril_, we should never expect x_nc to be made contiguous in triu_ and tril_.

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.

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.

apply_triu_tril<scalar_t, true, false>(self, self, k);
});
if (checkTrilTriuBatchContiguous(self)) {
AT_DISPATCH_ALL_TYPES(self.type(), "tril", [&]{
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think you can avoid the calling the dispatch twice (because remember, it will expand into some large thing), by just settings result and e.g. self_contiguous using a ternary based on checkTrilTriuBatchContiguous and then copying at the end if necessary.

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.

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.

}
if (!checkTrilTriuBatchContiguous(self)) self = self.contiguous();
bool inplace = checkTrilTriuBatchContiguous(self);
Tensor self_c = checkTrilTriuBatchContiguous(self) ? self : self.contiguous();
Copy link
Contributor

Choose a reason for hiding this comment

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

use inplace instead of checking again?

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.

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.

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 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

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 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

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 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Feb 19, 2019
Summary:
Currently, when the input tensor `self` is not contiguous, `tril_` and `triu_` calls `self = self.contiguous()`, which allocates a new contiguous tensor and assign it to `self`. This effectively changes the input tensor `self`'s pointer and will break downstream code after Variable/Tensor merge.

This PR fixes it so that `tril_` and `triu_` always update the input tensor in-place and preserve the input tensor's TensorImpl.
Pull Request resolved: pytorch/pytorch#17031

Differential Revision: D14069592

Pulled By: yf225

fbshipit-source-id: d188218f426446a44ccc1d33fc28ac3f828c6a05
@ezyang ezyang added the merged label Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants