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

Implement kthvalue in ATen #17544

Closed
wants to merge 4 commits into from
Closed

Implement kthvalue in ATen #17544

wants to merge 4 commits into from

Conversation

t-vi
Copy link
Collaborator

@t-vi t-vi commented Feb 27, 2019

The CPU version is based on the TH version.
The GPU version is based on #8406 by Pararth Shah (thank you).

CPU quickselect based on that in TH's THTensorMoreMath.cpp, but with C++ (quickselectnoindex will be achieved by a different swap)
CPU kthvalue is based on the THTensor function in the same file.
The dim_apply function is a C++ replacement for TH_TENSOR_DIM_APPLYx macros.
The CUDA kernel uses functions adapted from the THCTensorSortK implementation.
In particular radixSelect is from THCTensorTopK.cuh.
The CUDA launcher code replaces a bunch of macros with C++. It will be re-used in one of the following patches.

Plan for further PRs:

  • This
  • Sort
  • TopK + Mode + Median in any order
  • Rip out THC stuff.

There may be utility functions / structs in the SortingCommon.cuh that come into
relevance only with sort.

The CPU version is based on the TH version.
The GPU version is based on pytorch#8406 by Pararth Shah (thank you).

As discussed with @izdeby, this splits out a small(ish) chunk
from pytorch#17232, the other bits will follow in spearate PRs.

Plan for PRs:
- This
- Sort
- TopK + Mode + Median in any order
- Rip out THC stuff.

There may be utility functions in the SortingCommon.cuh that come into
relevance only with sort.

I hope this is more digestible..
@t-vi t-vi requested a review from izdeby February 27, 2019 16:21
Copy link
Contributor

@izdeby izdeby left a comment

Choose a reason for hiding this comment

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

Thomas, thanks again for breaking the original PR into a smaller chunks.
Going forward, could you, please, include some info in the description about which methods you are renaming and where are you moving them? That would help a lot.

aten/src/ATen/native/cuda/SortingKthValue.cu Show resolved Hide resolved
*topK = TopKTypeConfig<scalar_t>::deconvert(desired);
}
} // namespace native
} // namespace at
Copy link
Contributor

Choose a reason for hiding this comment

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

two questions about this file:

  1. Im just curious why is gatherTopK in cuda/SortingKthValue.cu instead of being here?
  2. Did you check if some the ported code can me deleted in TH or is it still being used somewhere?

Copy link
Collaborator Author

@t-vi t-vi Feb 28, 2019

Choose a reason for hiding this comment

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

  1. I only moved those bits into the cuh that are used by more than one file eventually, but I can move it to the .cuh if you prefer. (But I hope it's gatherKthValue. :) ) That said I'm not entirely sure whether it's still as relevant as it used to be, but the THC code went to great length to reduce the sizes of compilation units - splitting the scalar type files etc., so I think keeping the .cuh minimal is a feature.
  2. I don't think much can go - the THC code needs to stay in until we move topk. Also mode needs kth value for now.

@izdeby
Copy link
Contributor

izdeby commented Feb 28, 2019

Can we kill the _th_kthvalue declaration in the Declarations.cwrap as a part of this PR or is it still needed?

@izdeby
Copy link
Contributor

izdeby commented Feb 28, 2019

LGTM. lets make sure that all tests are passing and its good to go

@t-vi
Copy link
Collaborator Author

t-vi commented Mar 1, 2019

@pytorchbot merge this, please

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.

@ezyang 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 Mar 2, 2019
Summary:
The CPU version is based on the TH version.
The GPU version is based on #8406 by Pararth Shah (thank you).

CPU quickselect based on that in TH's THTensorMoreMath.cpp, but with C++ (quickselectnoindex will be achieved by a different swap)
CPU kthvalue is based on the THTensor function in the same file.
The dim_apply function is a C++ replacement for TH_TENSOR_DIM_APPLYx macros.
The CUDA kernel uses functions adapted from the THCTensorSortK implementation.
In particular radixSelect is from THCTensorTopK.cuh.
The CUDA launcher code replaces a bunch of macros with C++. It will be re-used in one of the following patches.

Plan for further PRs:
- This
- Sort
- TopK + Mode + Median in any order
- Rip out THC stuff.

There may be utility functions / structs in the SortingCommon.cuh that come into
relevance only with sort.
Pull Request resolved: pytorch/pytorch#17544

Differential Revision: D14286934

Pulled By: ezyang

fbshipit-source-id: 35dbea050b097e88777ac5fa5c0f499d5e23c738
@t-vi t-vi mentioned this pull request Mar 2, 2019
facebook-github-bot pushed a commit that referenced this pull request Mar 21, 2019
Summary:
This moves median to ATen.

- median with dimension reduces to kthvalue
- median without dimension (aka medianall) is implemented in parallel to kthvalue because we would not want to reshape (copying for non-contiguous) and then copy again in kthvalue. We can sue the helper functions we moved from kthvalue.
- `median_cuda` was accidentally already put into ATen in #17544.
- The quickselect algorthm without indices for CPU in TH is now obsolete and removed.
Pull Request resolved: #17637

Differential Revision: D14346510

Pulled By: ezyang

fbshipit-source-id: c07ad144efbd6b4194179bb1c02635862521d8cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants