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
Out of place broadcasting creates unexpected results #278
Comments
Just for fun can you also print w |
I didn't save the original seed, but this repros with fixed inputs as well so I've updated the MWE to match. |
This feels like what was fixed in EnzymeAD/Enzyme#604 I’ll try it locally to see if it resolves. If so I’ll do a jll bump with it and the fix for #277 once ready |
Reducing a bit:
Incidentally the bug here is not that it computes the wrong derivative (it gets the right one) but somewhere activity analysis incorrectly believes x to be active and +='s its derivative into x. Incidentally making x duplicated should get you the desired outcome. This also requires the broadcast and works fine without it. Looking into it here (https://fwd.gymni.ch/H2S9ke). @vchuravy this relates to our earlier convo. |
using Enzyme
Enzyme.API.printall!(true)
x = Float32[3]
w = Float32[1]
dw = zero(w)
function loss(w, x, cond)
if cond
x = copy(x)
end
@inbounds w[1] * x[1]
end
@show w, dw, x
Enzyme.autodiff(loss, Active, Duplicated(w, dw), Const(x), Const(false))
@show w, dw, x |
An activity analysis fix in Enzyme proper fixes the minimal case, still investigating below: using Enzyme
Enzyme.API.printall!(true)
Enzyme.API.printactivity!(true)
x = Float32[3]
w = Float32[1]
dw = zero(w)
loss2(w, x, c) = @inbounds Base.materialize(Base.broadcasted(*,w,x))[1]
function loss(w, x, cond)
r = Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}}(*, (w, x), axes(w))
# m = Base.materialize(r)
# @inbounds m[1]
dest = Float32[0]
bcc = Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}}(r.f, (Base.Broadcast.extrude(Base.Broadcast.broadcast_unalias(dest, w)), Base.Broadcast.extrude(Base.Broadcast.broadcast_unalias(dest, x))), axes(w))
# preprocess_args(dest, r.args), r.axes)
dest[1] = bcc[1]
@inbounds dest[1]
end
@show w, dw, x
Enzyme.autodiff(loss, Active, Duplicated(w, dw), Const(x), Const(false))
@show w, dw, x |
MWE from Slack:
Output:
The text was updated successfully, but these errors were encountered: