Skip to content

Initial implementation of quantile operator#39417

Closed
heitorschueroff wants to merge 1 commit intopytorch:masterfrom
heitorschueroff:quantile
Closed

Initial implementation of quantile operator#39417
heitorschueroff wants to merge 1 commit intopytorch:masterfrom
heitorschueroff:quantile

Conversation

@heitorschueroff
Copy link
Copy Markdown
Contributor

@heitorschueroff heitorschueroff commented Jun 2, 2020

Implementing the quantile operator similar to numpy.quantile.

For this implementation I'm reducing it to existing torch operators to get free CUDA implementation. It is more efficient to implement multiple quickselect algorithm instead of sorting but this can be addressed in a future PR.

@mruberry mruberry self-requested a review June 3, 2020 02:04
@mruberry mruberry added the module: numpy Related to numpy support, and also numpy compatibility of our operators label Jun 3, 2020
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Jun 3, 2020

💊 CI failures summary and remediations

As of commit 678b1dc (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 74 times.

Comment thread aten/src/ATen/native/native_functions.yaml Outdated
Comment thread torch/_torch_docs.py Outdated
Comment thread torch/_torch_docs.py Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread torch/_torch_docs.py Outdated
@heitorschueroff heitorschueroff requested a review from mruberry July 2, 2020 17:11
@mruberry
Copy link
Copy Markdown
Collaborator

mruberry commented Jul 9, 2020

Ping me when this is ready for review again, @heitorschueroff

Comment thread torch/_torch_docs.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Comment thread test/test_torch.py Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

1e-03 and 1e-06?

Was there a problem with the defaults?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've been struggling to find good numbers for atol and rtol because NumPy promotes float to double and hence has better accuracy. For double it works fine, but for floats I keep failing tests even though the numbers are pretty close to about 5 digits.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, because NumPy promotes float to double I'm struggling to find numbers that work for the float case.

Comment thread aten/src/ATen/native/Sorting.cpp Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This copy is unfortunate. Can it be eliminated? One way to eliminate it most of the time would be to have this function return the reshaped quantiles tensor and have the out variant perform the shape check and copy it into the out_ param, if given. Another option would be to not support an out variant at all. Out is little-used and getting the result shape of this operation correct is tricky, after all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll make it so only the out variant makes the copy but I agree that this is not optimal. I also don't like how I have to permute the dimensions to follow NumPy shape. I think this would really benefit from a custom cpu/cuda implementation. Maybe a follow-up PR?

Copy link
Copy Markdown
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.

Looks really good overall. Made a few test and doc suggestions and asked a question about the algorithm.

@heitorschueroff heitorschueroff requested a review from mruberry July 13, 2020 15:07
Comment thread torch/_torch_docs.py Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm a little confused by the "i < j" here. What's that intended to express? Rest of this sentence looks great.

Comment thread torch/_torch_docs.py Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same question about "i < j" here. Also you have two periods following "j."

Copy link
Copy Markdown
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.

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

Author:    Heitor Schueroff <heitorschueroff@fb.com>
Date:      Mon Jun 8 09:18:02 2020 -0700
Copy link
Copy Markdown
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.

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

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@heitorschueroff merged this pull request in c7798dd.

heitorschueroff added a commit that referenced this pull request Aug 10, 2020
Attempting to land quantile again after being landed here #39417 and reverted here #41616.

[ghstack-poisoned]
facebook-github-bot pushed a commit that referenced this pull request Aug 11, 2020
Summary:
Pull Request resolved: #42755

Attempting to land quantile again after being landed here #39417 and reverted here #41616.

Test Plan: Imported from OSS

Reviewed By: mruberry

Differential Revision: D23030338

Pulled By: heitorschueroff

fbshipit-source-id: 124a86eea3aee1fdaa0aad718b04863935be26c7
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Implementing the quantile operator similar to [numpy.quantile](https://numpy.org/devdocs/reference/generated/numpy.quantile.html).

For this implementation I'm reducing it to existing torch operators to get free CUDA implementation. It is more efficient to implement multiple quickselect algorithm instead of sorting but this can be addressed in a future PR.

Pull Request resolved: pytorch#39417

Reviewed By: mruberry

Differential Revision: D22525217

Pulled By: heitorschueroff

fbshipit-source-id: 27a8bb23feee24fab7f8c228119d19edbb6cea33
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#42755

Attempting to land quantile again after being landed here pytorch#39417 and reverted here pytorch#41616.

Test Plan: Imported from OSS

Reviewed By: mruberry

Differential Revision: D23030338

Pulled By: heitorschueroff

fbshipit-source-id: 124a86eea3aee1fdaa0aad718b04863935be26c7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: numpy Related to numpy support, and also numpy compatibility of our operators

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants