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

Unnecessary typeassert for ADAGrad #1410

Open
theogf opened this issue Nov 30, 2020 · 7 comments
Open

Unnecessary typeassert for ADAGrad #1410

theogf opened this issue Nov 30, 2020 · 7 comments

Comments

@theogf
Copy link

theogf commented Nov 30, 2020

So this is probably a copy of #832 and #816. But I don't think the arguments apply in my case.
When using apply! with ADAGrad, there is the line

acc = get!(() -> fill!(similar(x), ϵ), o.acc, x)::typeof(x)
but this is true in general for other optimisers.
The type assertion assumes that it should be of the same type as x and it's an issue.

I am working with views therefore the type of x is SubArray{...}, however zero(x) will of course return an Array{...} and the resulting output will not pass the type assertion.

@ToucheSir
Copy link
Member

The asserts were ostensibly added in the name of stability (see #1388), but it seems like they're not general enough. I'm not sure what a good solution would be here given the limitations of the current optimizer interface, @simeonschaub do you have any thoughts?

@CarloLucibello
Copy link
Member

let's just remove the type assertions, the performance impact should be negligible

@theogf
Copy link
Author

theogf commented Nov 30, 2020

I don't know how bad it would impact the inference but a possible fix would be to replace typeof(x) by supertype(typeof(x))

@simeonschaub
Copy link
Member

Ah I see the problem.

I don't know how bad it would impact the inference but a possible fix would be to replace typeof(x) by supertype(typeof(x))

Generally, inferring something to a non-concrete type is not that useful for inference, if it's not a small union, so I would be surprised if that made much of a difference over just deleting the type annotation. It might also not necessarily be correct still, since there's no guarantee that similar(x) returns something of type supertype(typeof(x)).

A possible solution, that could be beneficial over removing this annotation altogether would be to move all broadcasted calls after the call to get! into a separate function taking acc as an argument, which would introduce a function barrier and still allow specialization on the type of acc for broadcasting, without needing the annotation. There would be a slight cost of a dynamic function call, but that would most certainly be worth it, if that leads to better code for the broadcasts.

@DhairyaLGandhi
Copy link
Member

Can we check if the inference issue persists in optimisers.jl? If not, maybe we prioritize moving to it. There was a 20% hit in my testing, but it might be worth it in the long run.

@ToucheSir
Copy link
Member

ToucheSir commented Nov 30, 2020

That's what https://github.com/FluxML/Optimisers.jl is trying to achieve, hence my comment on "the current optimizer interface" :)

Edit: Dhairya beat me to the punch!

@CarloLucibello
Copy link
Member

CarloLucibello commented Dec 27, 2020

Unless someone is actively working on the function barrier approach, I suggest we fix this issue by removing the type assert for the time being, since it can be quite invalidating as testified by the fact that there are 2 similar issues (in fact, we should add some tests when closing this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants