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

why is my withgradient type unstable ? #2456

Closed
filchristou opened this issue Jun 13, 2024 · 1 comment
Closed

why is my withgradient type unstable ? #2456

filchristou opened this issue Jun 13, 2024 · 1 comment

Comments

@filchristou
Copy link

I noticed the withgradient function introduces some type instability in a hot loop of mine.
I got it to the following MWE.

using Flux

function unstablewithgradients(model, obs, labels)
    loss, grads = Flux.withgradient(
        model
    ) do m
            internfunc(m, obs, labels)
        end
    return loss, grads
end

# obviously type stable function
function internfunc(m, x, y)
    modelvals = m.(x)
    meanvals = reduce(vcat, modelvals)
    Flux.mse(meanvals, y)
end

then run

fc = Chain(Dense(5=>3, relu), Dense(3=>1))
obs = [fill(5f0, 5) for _ in 1:10]
labels = fill(2f0, 10)

@code_warntype unstablewithgradients(fc, obs, labels)

snap

I am on Flux v0.14.15, and on Julia 1.10.4

@filchristou
Copy link
Author

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

1 participant