EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
437 stars 62 forks source link

Why does gradient in forward mode return a tuple? #1368

Closed gdalle closed 3 months ago

gdalle commented 5 months ago

It is particularly surprising for array inputs:

julia> using Enzyme

julia> Enzyme.gradient(Enzyme.Forward, sum, rand(3))
(1.0, 1.0, 1.0)

julia> Enzyme.gradient(Enzyme.Forward, sum, rand(2, 3))
(1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
wsmoses commented 4 months ago

It returns an nutple of "ResultType" for whatever the result type is.

For example if you had function f(x) -> [x[1], x[2] + x[3]]) then gradient on it would return ([val1, val2], [val3, val4], [val5, val6]). (assuming the input array had 3 elements).

Thus in forward mode jacobian is only a wrapper over this.

Happy to explore different designs if you have suggestions, but that's what it does atm.