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

Adds dim argument to torch.unique #10423

Closed
wants to merge 13 commits into from
Closed

Conversation

ptrblck
Copy link
Collaborator

@ptrblck ptrblck commented Aug 10, 2018

Initial version of unique supporting a dim argument.

As discussed in this issue I added the dim argument to torch.unique with the same behavior like numpy.

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.

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!

@ptrblck ptrblck changed the title fixes #9997 Adds dim argument to torch.unique Aug 13, 2018
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.

This comment was marked as off-topic.

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.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@@ -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.

@ptrblck
Copy link
Collaborator Author

ptrblck commented Aug 20, 2018

As there is no switch between return_inverse=True/False, since inverse_indices will be pre-computed in the unique implementation, the new CPU time is now approx. 0.00921 seconds.

@zou3519 zou3519 added the ready for review (this tag is deprecated) All PRs are ready for review unless they are draft, WIP, or have undismissed requested changes label Aug 21, 2018
Copy link
Member

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

lgtm, thanks @ptrblck

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.

colesbury 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.

soumith 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 Aug 30, 2018
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
PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open source ready for review (this tag is deprecated) All PRs are ready for review unless they are draft, WIP, or have undismissed requested changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants