-
-
Notifications
You must be signed in to change notification settings - Fork 609
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
Use fallback for reshape/cat OneHotArray #1459
Conversation
Sorry, if this is a dumb question, but I didn't quite understand the reasoning in #1448 why the default behavior for AbstractArrays in Base was bad.
Isn't this what |
That actually wasn't the reason for throwing the error. I guess the actual reason might have been that when you reshape the first dimension, the array is no longer one-hot, so we erred on the side of alerting the user instead of silently converting (the same was done for With respect to the part you quoted, that is referring to the non-error branch where the first dimension is the same and the other dimensions are reshaped. In this case, the array is still a But I do think JuliaLang/julia#38965 might allow us to fix this. I can test it out on this branch and commit it if that's the desired behavior. Side note: do you have similar requests about |
Ah, I see, so the goal here is just to preserve one-hotness as much as we can, to take advantage of fast-paths. Since
Yes, I think by the same reasoning that should be allowed as well. In that case, I don't think the type instabilities would be that big of a deal, since |
That makes sense. Let me look into what that would entail for the paths we want to hit. @DhairyaLGandhi are you okay with allowing |
|
That's true. I am pretty sure for these use cases, Also, we can should merge this PR with the simple fallback fixes so that they make it in for v0.12. I can submit an additional PR extending |
|
I think the argument is the same:
But I feel now that these fallback cases are never on a critical path, so it isn't so bad to silently do what's expected instead of forcing a manual |
I guess the alternative would not to pretend to be an array, and then it would only ever work on the intended straight-and-narrow. But if it is an array, then it seems like it ought to work in weird things people think up. Although where it's better to collect vs. producing some multiply wrapped thing I don't know. |
So FYI, supporting these fallback paths is not hard to add, and in both cases we just convert to a Most operations on a |
For the |
Maybe this was said, but the one downside of simple fallbacks is type-stability. I guess |
Yeah, |
This part in the manual just describes a minimal number of functions new subtypes of AbstractArray should implement so generic functions for AbstractArrays work like they are supposed to. Interfaces in Julia currently aren't very formalized, but I definitely believe it's bad style to break (although maybe only implicitly assumed by everyone) promises of generic function. I think most users would assume
In my case, I added a singleton dimension in front, so I could broadcast with another array. This is something that's definitely not rare in generic code and it is quite annoying to work around if it doesn't work in all cases. |
@simeonschaub we lose type stability for reshape though, is allowing to reshape the first dimension worth it? |
Not necessarily, see my first comment, where I proposed not overloading |
I think that's the better approach |
Are you suggesting to always wrap in a ReshapedArray when reshaping? Array wrappers are always a bit annoying to work with, and they sometimes interact badly with the cuda infrastructure AFAIK. I'm not super familiar with the topic, but there has to be some reason why reshaping an Array returns an Array, while ReshapedArray is used as a generic fallback for AbstractArrays |
What fast paths are there, BTW? Just I wondered whether |
bumping @darsnack to widen the types and get rid of the |
I am assuming you are referring to the |
Sorry for the delay. I swapped overloading |
Bump |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some small suggestions, sorry for holding this up. Otherwise looks good!
Co-authored-by: Simeon Schaub <[email protected]>
Thanks, think I need an approval from @CarloLucibello or @DhairyaLGandhi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall I find the implementation to have more "helper" methods than I'd care for, seems like we ought to make oharray code more generic, but I guess that's going to have to wait for a different day
# use this type so reshaped arrays hit fast paths | ||
# e.g. argmax | ||
const OneHotLike{T, L, N, var"N+1", I} = | ||
Union{OneHotArray{T, L, N, var"N+1", I}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Man this N+1 is tripping me up, I would say we need to remove this soon. Where is it used exactly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we could calculate var"N+1" during runtime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like it either! It can't be done at runtime since N
and var"N+1"
are used in the type specification. N
is used to specify the type of the index array, and var"N+1"
is used to inherit from AbstractArray{Bool, var"N+1"}
. Neither is evaluated at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could change it to another variable. I don't have strong feelings, but a part of me says that at least this naming signals the intent of the type parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair, I did mean we would have to switch it out during construction, because I don't think it's any better for dispatch to have to do checks on ints than types. To me it suggests that it is a preknown quantity so adding it to the type doesn't win us much.
Ah apparently I don't have merge rights. So you will still need to merge this. |
bors r+ |
1459: Use fallback for reshape/cat OneHotArray r=DhairyaLGandhi a=darsnack This falls back to reshaping a `Bool` array whenever reshaping the first dimension of a `OneHotArray`. @DhairyaLGandhi @CarloLucibello @simeonschaub ### PR Checklist - [x] Tests are added - [ ] ~~Entry in NEWS.md~~ - [x] Documentation, if applicable Co-authored-by: Kyle Daruwalla <[email protected]> Co-authored-by: Kyle Daruwalla <[email protected]>
This PR was included in a batch that successfully built, but then failed to merge into master (it was a non-fast-forward update). It will be automatically retried. |
Hmm.. Let try again bors r+ |
Already running a review |
Build succeeded: |
This falls back to reshaping a
Bool
array whenever reshaping the first dimension of aOneHotArray
.@DhairyaLGandhi @CarloLucibello @simeonschaub
PR Checklist
Entry in NEWS.md