-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Documentation of bucketize()
is wrong, and is contradicting itself.
#91580
Comments
Sorry I'm seeing that the reference table does have a closed left boundary (i.e. |
To quote, I'm going to use simpler notation below, and just use So as I've said before, the reference table does agree with the actual examples and behaviour. With Finally, regarding the documentation in the "Keyword Arguments" section, some of which you have quoted, I personally find it very confusing anyway, and I don't disagree with it (at least for now). I kind of wish it was omitted because I find it even more confusing that if it weren't there but that's my subjective view and let's leave that for another conversation. ( Don't bother reading the following but I mean, it just says "gets the lower bound index [...]", without a pronoun. And I'm like, what "gets" the lower bound index? Do I get it? Does the function get it? What? Anyway after reading it, it looks correct, if confusing. So let's not get into that here.) |
Also, to my other point, that Pytorch's
Then |
My friend just pointed out that my tone isn't good, after he read it LOL. I'm sorry if I come across rude, I don't mean to. I really appreciate your time and attention in addressing this @samdow . Thank you. And thank you everyone else who is looking into this. |
+1 I also thought this was confusing |
**TL;DR**: This PR is a first step in adding lowerings for torch.bucketize. It adds an initial lowering for this op - but because this implementation is not currently efficient, it registers the lowering for prims._inductor_bucketize. After we make the implementation more efficient, we'll remove prims._inductor_bucketize and add the lowering directly to torch.bucketize. **Background - torch.bucketize**: torch.bucketize(values, boundaries, right=False): for an arbitrary tensor of values and a non-decreasing 1D tensor of boundaries that define buckets, it returns the index of the bucket that each of the values will fall in. e.g. for values [0, 1, 2, 3, 4] and boundaries [1, 3], it will return [0, 0, 1, 1, 2]. **Implementation**: This PR adds a new inductor op called "bucketize". In this PR it only has a triton implementation - for CPU it is a fallback. The triton implementation uses a binary search in `triton_helpers.py`. This PR also adds a new prim `_inductor_bucketize()` for testing purposes and adds lowering for this op. ~~**"right"**: The current behavior of the "right" kwarg in the inductor op is the opposite of the behavior of the torch op. "right" controls how the op treats a value that is equal to one of the boundary values. In the torch op, "right=True" means "if a value is equal to a boundary value, then put it in the bucket to the right". In the inductor op, "right=True" means "the right boundary of a bucket is closed". These are opposite. **I'm open to switching the behavior of the inductor op** - but I chose to implement this way because I think it makes more sense, and I think the torch.bucketize behavior may have been a mistake (it's the opposite of numpy.digitize).~~ Switched the behavior of the inductor bucketize op to match the torch op * places where "right" means "if a value is equal to a boundary value, then put it in the bucket to the right" (i.e. current torch.bucketize behavior) + current torch.bucketize behavior + table in [torch.bucketize docs](https://pytorch.org/docs/stable/generated/torch.bucketize.html) * places where "right" means "the right boundary of a bucket is closed": + the text description of [torch.bucketize docs](https://pytorch.org/docs/stable/generated/torch.bucketize.html) (observed in #91580) + [numpy.digitize](https://numpy.org/doc/stable/reference/generated/numpy.digitize.html) (which is basically the same op) **Performance**: Benchmark script: "values" as a [16, 1024, 1024] float32 tensor and "boundaries" as a [1025] tensor (i.e. defining 1024 buckets). As is: ``` Eager 0.30117499828338623 ms PT2 0.9298200011253357 ms ``` But performance improves significantly if we add an additional pointwise autotuning config (WIP in #104456): ``` Eager 0.3015420138835907 ms PT2 0.23028500378131866 ms ``` Pull Request resolved: #104007 Approved by: https://github.com/jansel
The docs correctly (i.e matching actual op behavior) state that `right = False` means `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]`. However they previously stated that `If 'right' is False (default), then the left boundary is closed.` which contradicts the `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]` statement. This modifies the docs to say `... then the left boundary is OPEN.` and also clarifies that this is the opposite behavior of numpy.digitize. Fixes #91580 [ghstack-poisoned]
The docs correctly (i.e matching actual op behavior) state that `right = False` means `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]`. However they previously stated that `If 'right' is False (default), then the left boundary is closed.` which contradicts the `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]` statement. This modifies the docs to say `... then the left boundary is OPEN.` and also clarifies that this is the opposite behavior of numpy.digitize. Fixes #91580 [ghstack-poisoned]
…rch.bucketize docs for "right"" The docs correctly (i.e matching actual op behavior) state that `right = False` means `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]`. However they previously stated that `If 'right' is False (default), then the left boundary is closed.` which contradicts the `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]` statement. This modifies the docs to say `... then the left boundary is OPEN.` and also clarifies that this is the opposite behavior of numpy.digitize. Fixes #91580 [ghstack-poisoned]
…cs for "right"" The docs correctly (i.e matching actual op behavior) state that `right = False` means `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]`. However they previously stated that `If 'right' is False (default), then the left boundary is closed.` which contradicts the `boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]` statement. This modifies the docs to say `... then the left boundary is OPEN.` and also clarifies that this is the opposite behavior of numpy.digitize. Fixes #91580 [ghstack-poisoned]
📚 The doc issue
The documentation of the bucketize function, specifically the
right
argument, is wrong. I mean, is it wrong or am I going mad? It says if "right is False (default), then the left boundary is closed". While this makes sense, it's in direct contradiction with the reference table directly below that sentence, and the actual behaviour of the function (which agrees with the reference table but contradicts the aforementioned sentence).As a further complaint, while that sentence "if right is False (default), then the left boundary is closed" agrees with Numpy's equivalent digitize function, the actual behaviour is the opposite of Numpy's
digitize()
(with respect to theright
argument). Why??? Why not just make them behave the same way??????Suggest a potential alternative/fix
Ideally I'd like the behaviour
bucketize()
to change (slightly), so that it agrees with the (currently false) sentence "if right is False (default), then the left boundary is closed" in the documentation, and as such agrees with the behaviour of Numpy'sdigitize()
, and not with the reference table also in the documentation, but I know that's probably not going to happen because, you know, it's behaviour-changing. But let's at least change that sentence in the documentation so that it doesn't contradict itself. That sentence should read "ifright
isFalse
(default), then the right boundary is closed".By the way I apologise if it turns out I'm the one in the wrong. I've stared at that sentence for 2 hours now and it still seems wrong to me.
cc @svekars @carljparker
The text was updated successfully, but these errors were encountered: