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

Initial implementation of quantile operator #39417

Closed
wants to merge 1 commit into from
Closed

Initial implementation of quantile operator #39417

wants to merge 1 commit into from

Conversation

heitorschueroff
Copy link
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

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.

torch/_torch_docs.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
torch/_torch_docs.py Outdated Show resolved Hide resolved
@mruberry
Copy link
Collaborator

mruberry commented Jul 9, 2020

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

torch/_torch_docs.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
test/test_torch.py Outdated Show resolved Hide resolved
result = torch.quantile(a, q, dim=dim, keepdim=keepdim).cpu()
expected = np.quantile(a.cpu().numpy(), q.cpu().numpy(), axis=dim, keepdims=keepdim)
expected = torch.from_numpy(np.array(expected)).type(result.type())
self.assertTrue(torch.allclose(result, expected, rtol=1e03, atol=1e06))
Copy link
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
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
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.

quantiles = quantiles.permute(numpy_dim_order);
}

out.copy_(quantiles.reshape(out_shape));
Copy link
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
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
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.

quantile(input, q) -> Tensor

Returns the q-th quantiles of all elements in the :attr:`input` tensor, doing a linear
interpolation when the q-th quantile lies between two data points i < j.
Copy link
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.


Returns the q-th quantiles of each row of the :attr:`input` tensor along the dimension
:attr:`dim`, doing a linear interpolation when the q-th quantile lies between two data points
i < j.. By default, :attr:`dim` is `None` resulting in the :attr:`input` tensor
Copy link
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
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
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
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
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.

None yet

4 participants