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

Mixed Activity of Float, Vector in reverse mode #1316

Open
ptiede opened this issue Feb 29, 2024 · 2 comments
Open

Mixed Activity of Float, Vector in reverse mode #1316

ptiede opened this issue Feb 29, 2024 · 2 comments

Comments

@ptiede
Copy link

ptiede commented Feb 29, 2024

On Enzyme 0.11.7 (it seems main is broken right now?), I am having some gradients drop when I try to take a derivative w.r.t. a NamedTuple.

A MWE example is

f(p) = p.b^2 + sum(abs2, p.c)
p = (b = 2.0, c = ones(2))
dp = (b = 0.0, c = zeros(2))
autodiff(Reverse, f, Duplicated(p, dp))
@show dp
# dp = (b = 0.0, c = [2.0, 2.0])
# correct gradient (b = 4.0, c=[2.0, 2.0])

My guess is that this is because the NamedTuple has Mixed activity. If I do the following

@noinline fref(p) = f(p[])
pref = Ref(p)
dpref = Ref(dp)
autodiff(Reverse, fref, Duplicated(pref, dpref))

the gradients are correct.

@wsmoses
Copy link
Member

wsmoses commented Mar 5, 2024

Oh yeah the issue here is mixedactivity precisely. Marking it duplicated won't compute the derivative of any in-place values (since we can't update the float in db in place, passing it as a duplicated cannot let us change it).

This is hopefully described in the docs now here: https://enzymead.github.io/Enzyme.jl/dev/#Mixed-Activity

Active variables are used for immutable variables (like Float64), whereas Duplicated variables are used for mutable variables (like Vector{Float64}). Speciically, since Active variables are immutable, functions with Active inputs will return the adjoint of that variable. In contrast Duplicated variables will have their derivatives +='d in place.

This error indicates that you have a type, like Tuple{Float, Vector{Float64}} that has immutable components and mutable components. Therefore neither Active nor Duplicated can be used for this type.

Please feel free to open a PR if you think that needs more (or perhaps we could issue a warning in reverse mode?)

@wsmoses wsmoses changed the title Getting the wrong derivative when differentiating w.r.t a NamedTuple Mixed Activity of Float, Vector in reverse mode Mar 5, 2024
@ptiede
Copy link
Author

ptiede commented Mar 6, 2024

Ok excellent, I was hoping I understood the docs. Is there a way that Enzyme could potentially alert the user to this during compilation? The docs suggest I should see an error Type T has mixed internal activity type which I don't see on the main branch.

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

2 participants