Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] torch.kthvalue : handle non-contiguous CUDA tensor #45802

Closed
wants to merge 6 commits into from

Conversation

kshitij12345
Copy link
Collaborator

@kshitij12345 kshitij12345 commented Oct 3, 2020

Fixes #45721

TODO

  • Test

@mruberry mruberry self-requested a review October 5, 2020 17:35
@albanD albanD added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 6, 2020
@@ -241,7 +241,7 @@ std::tuple<Tensor&, Tensor&> kthvalue_out_cuda(
bool keepdim) {
auto result = [&]() {
NoNamesGuard guard;
return kthvalue_out_impl_cuda(values, indices, self, k, dim, keepdim);
return kthvalue_out_impl_cuda(values, indices, self.contiguous(), k, dim, keepdim);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you just add a comment here or above the kthvalue_out_impl_cuda() that the function expects (and only works properly on) contiguous tensors?

@@ -9010,6 +9010,13 @@ def test_kthvalue(self, device, dtype):
self.assertEqual(res1val, res2val, atol=0, rtol=0)
self.assertEqual(res1ind, res2ind, atol=0, rtol=0)

# non-contiguous [Reference: https://github.com/pytorch/pytorch/issues/45721]
non_contig_t = torch.tensor([0, -1, 1, -2, 2], dtype=dtype, device=device)[::2]
expected_val, expected_ind = non_contig_t.contiguous().kthvalue(2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you also compare it with the cpu?

expected_val_cpu, expected_ind_cpu = non_contig_t.kthvalue(2)
...
self.assertEqual(expected_val_cpu, out_val.cpu(), atol=0, rtol=0)
self.assertEqual(expected_ind_cpu, out_ind.cpu(), atol=0, rtol=0)

Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

Hey @kshitij12345 Thanks for fixing this high priority issue! This is a great, simple fix. Maybe in the future we should consider if the kth value kernel should be updated to handle discontiguous inputs, too.

I just made two small suggestions, and it looks like this PR needs to rebased, too (sorry about that). Just ping me when they're done!

Since this fixes a silent correctness issue I think we'll want to put it in the 1.7 release, too.

@kshitij12345
Copy link
Collaborator Author

@mruberry

Have addressed the comments. PTAL:)

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.

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

@codecov
Copy link

codecov bot commented Oct 10, 2020

Codecov Report

Merging #45802 into master will increase coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #45802   +/-   ##
=======================================
  Coverage   68.20%   68.20%           
=======================================
  Files         410      410           
  Lines       53453    53453           
=======================================
+ Hits        36457    36458    +1     
+ Misses      16996    16995    -1     
Impacted Files Coverage Δ
torch/testing/_internal/expecttest.py 78.57% <0.00%> (+1.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 689499f...2af6d07. Read the comment docs.

@facebook-github-bot
Copy link
Contributor

@mruberry merged this pull request in a814231.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kthvalue incorrect with strided GPU tensor
5 participants