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
Adds dim
argument to torch.unique
#10423
Conversation
aten/src/ATen/native/cuda/Unique.cu
Outdated
Tensor output = input_sorted.index_select(0, input_sorted_indices); | ||
|
||
// // reshape back | ||
std::vector<int64_t> new_sizes(orig_sizes.begin(), orig_sizes.end()); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/ATen/native/Unique.cpp
Outdated
int mask_idx = 1; | ||
|
||
std::vector<Tensor> input_unbind = at::unbind(input_sorted, 0); | ||
auto last = std::unique(input_unbind.begin(), input_unbind.end(), [&](Tensor a, Tensor b) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/ATen/native/cuda/Unique.cu
Outdated
@@ -7,6 +7,7 @@ | |||
#include <tuple> | |||
#include <thrust/unique.h> | |||
#include <thrust/sort.h> | |||
#include <thrust/device_vector.h> |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
As there is no switch between |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thanks @ptrblck
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
colesbury has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
soumith is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Summary: Initial version of `unique` supporting a `dim` argument. As discussed in [this issue](pytorch/pytorch#9997) I added the `dim` argument to `torch.unique` with the same behavior like [numpy](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.unique.html). Since the implementation is based on `std/thrust::unique`, the `tensor` always needs to be sorted. The `sorted` argument in `torch.unique` does not have any function, as in the CUDA version of the plain `torch.unique`. To check the performance and equal behavior between `torch.unique` and `np.unique`, I've used [this gist](https://gist.github.com/ptrblck/ac0dc862f4e1766f0e1036c252cdb105). Currently we achieve the following timings for an input of `x = torch.randint(2, (1000, 1000))`: (The values are calculated by taking the average of the times for both dimension) | Device | PyTorch (return_inverse=False) | Numpy (return_inverse=False) | PyTorch (return_inverse=True) | Numpy (return_inverse=True) | | --- | --- | --- | --- | --- | | CPU | ~0.007331s | ~0.022452s | ~0.011139s | ~0.044800s | | GPU | ~0.006154s | - | ~0.105373s | - | Many thanks to colesbury for the awesome mentoring and the valuable advices on the general implementation and performance issues! Pull Request resolved: pytorch/pytorch#10423 Differential Revision: D9517289 Pulled By: soumith fbshipit-source-id: a4754f805223589c2847c98b8e4e39d8c3ddb7b5
Summary: Initial version of `unique` supporting a `dim` argument. As discussed in [this issue](pytorch#9997) I added the `dim` argument to `torch.unique` with the same behavior like [numpy](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.unique.html). Since the implementation is based on `std/thrust::unique`, the `tensor` always needs to be sorted. The `sorted` argument in `torch.unique` does not have any function, as in the CUDA version of the plain `torch.unique`. To check the performance and equal behavior between `torch.unique` and `np.unique`, I've used [this gist](https://gist.github.com/ptrblck/ac0dc862f4e1766f0e1036c252cdb105). Currently we achieve the following timings for an input of `x = torch.randint(2, (1000, 1000))`: (The values are calculated by taking the average of the times for both dimension) | Device | PyTorch (return_inverse=False) | Numpy (return_inverse=False) | PyTorch (return_inverse=True) | Numpy (return_inverse=True) | | --- | --- | --- | --- | --- | | CPU | ~0.007331s | ~0.022452s | ~0.011139s | ~0.044800s | | GPU | ~0.006154s | - | ~0.105373s | - | Many thanks to colesbury for the awesome mentoring and the valuable advices on the general implementation and performance issues! Pull Request resolved: pytorch#10423 Differential Revision: D9517289 Pulled By: soumith fbshipit-source-id: a4754f805223589c2847c98b8e4e39d8c3ddb7b5
Initial version of
unique
supporting adim
argument.As discussed in this issue I added the
dim
argument totorch.unique
with the same behavior like numpy.Since the implementation is based on
std/thrust::unique
, thetensor
always needs to be sorted. Thesorted
argument intorch.unique
does not have any function, as in the CUDA version of the plaintorch.unique
.To check the performance and equal behavior between
torch.unique
andnp.unique
, I've used this gist.Currently we achieve the following timings for an input of
x = torch.randint(2, (1000, 1000))
:(The values are calculated by taking the average of the times for both dimension)
Many thanks to @colesbury for the awesome mentoring and the valuable advices on the general implementation and performance issues!