Skip to content

Commit

Permalink
Merge pull request #1068 from mcabbott/bcast_second
Browse files Browse the repository at this point in the history
Don't use dual numbers in broadcasting within 2nd derivatives
  • Loading branch information
mcabbott authored Sep 10, 2021
2 parents 7f2fed9 + 76c27d8 commit b90d752
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ _dual_safearg(x) = false
# Avoid generic broadcasting in two easy cases:
if T == Bool
return (f.(args...), _ -> nothing)
elseif T <: Real && isconcretetype(T) && _dual_purefun(F) && all(_dual_safearg, args)
elseif T <: Real && isconcretetype(T) && _dual_purefun(F) && all(_dual_safearg, args) && !isderiving()
return broadcast_forward(f, args...)
end
len = inclen(args)
Expand Down
5 changes: 5 additions & 0 deletions test/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,9 @@ end
@test gradient((x,p) -> sum(x .^ p), [1.0,2.0,4.0], -1)[1] [-1.0, -0.25, -0.0625]
@test gradient((x,p) -> sum(z -> z^p, x), [1.0,2.0,4.0], -1)[1] [-1.0, -0.25, -0.0625]
@test gradient((x,p) -> mapreduce(z -> z^p, +, x), [1.0,2.0,4.0], -1)[1] [-1.0, -0.25, -0.0625]

# second order
@test gradient(x -> sum(gradient(y -> sum(y.^2), x)[1]), [1, 2])[1] [2, 2]
@test gradient(x -> sum(gradient(y -> sum(sin.(y)), x)[1]), [1, 2])[1] [-0.8414709848078965, -0.9092974268256817]
@test gradient(x -> sum(abs, gradient(y -> sum(log.(2 .* exp.(y)) .^ 2), x)[1]), [1, 2])[1] [2,2]
end

0 comments on commit b90d752

Please sign in to comment.