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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proposal] batch mode for randperm #42502

Open
jamesal1 opened this issue Aug 4, 2020 · 4 comments
Open

[proposal] batch mode for randperm #42502

jamesal1 opened this issue Aug 4, 2020 · 4 comments
Labels
enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: random Related to random number generation in PyTorch (rng generator) triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@jamesal1
Copy link

jamesal1 commented Aug 4, 2020

馃殌 Feature

I'd like to implement some features for torch.random.randperm. What I've thought of so far:
-batch parameter, allowing multiple permutations to be sampled at the same time.
-partial or k-permutations

These would be accessible using optional arguments whose default behavior match current behavior (i.e. batch=1, k=None).

I'm uncertain about what options would be given for controlling the shape of the output (i.e. column or row permutations, automatic squeezing). One possibility is an optional size parameter for the output, and a dim parameter that specifies which axis the permutation lies on. If size is none then it defaults to current behavior.

I'm also open to additional features.

Motivation

Whenever I've used randperm I've used it in a batched context, so it seems like a natural feature.

Pitch

I will code cpu and gpu replacements for the current randperm implementations to allow the new features.

Alternatives

Currently batching can be done using a for loop, which is tedious and unperformant
Partial permutations can easily be obtained by taking a subtensor of the return value but can be unperformant if n is large and k is small.

Additional context

cc @pbelevich

@agolynski agolynski added enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: random Related to random number generation in PyTorch (rng generator) triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Aug 4, 2020
@vadimkantorov
Copy link
Contributor

Stumbled on this as well while coding a batched K-means random initialization (also reported at https://discuss.pytorch.org/t/batched-shuffling-of-feature-vectors/30188/4)

@vadimkantorov
Copy link
Contributor

One way could be accepting size or shape argument and also a dim argument. Then it would create a tensor of desired shape and put the perm[:shape[dim]] into the dim dimension

@vadimkantorov
Copy link
Contributor

randperm_like can also be created like this with dim argument

@Balandat
Copy link
Contributor

Balandat commented Dec 3, 2021

This would be useful to have. The torch.argsort(torch.rand(batch, k)) solution can get too slow for large k.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: random Related to random number generation in PyTorch (rng generator) triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants