ReverseDiff.jl icon indicating copy to clipboard operation
ReverseDiff.jl copied to clipboard

Custom gradient not getting called, incorrect zero gradients

Open samuela opened this issue 5 years ago • 3 comments

So I'm trying to specify a custom gradient for an FFI function:

import PyCall
import ReverseDiff

math = PyCall.pyimport("math")
pysin(x) = math.sin(x[1])
ReverseDiff.@grad function pysin(x)
    @error "fwd"
    function pullback(δ)
        @error "bwd"
        (δ * math.cos(x[1]), )
    end
    math.sin(x[1]), pullback
end
ReverseDiff.gradient(pysin, [1.5])

but unfortunately this outputs:

julia> include("difftaichi/zygote_zero_nothing_bug.jl")
1-element Array{Float64,1}:
 0.0

instead of the errors that we would expect. My conclusion from this is that my ReverseDiff.@grad custom gradient is not getting called at all. In addition, the gradient returned is zero, instead of producing an error. So I see two issues I guess:

  • the custom gradient is not working
  • ReverseDiff gives me incorrect gradients when it doesn't know what to do instead of producing an error (unsound-and-incomplete as opposed to sound-but-incomplete).

cc @ChrisRackauckas

samuela avatar Sep 15 '20 06:09 samuela

Yeah, at a quick glance it looks to me like you did it correctly, so I'll let @mohamed82008 handle this.

ChrisRackauckas avatar Sep 15 '20 06:09 ChrisRackauckas

You missed

pysin(x::ReverseDiff.TrackedArray) = ReverseDiff.track(pysin, x)

mohdibntarek avatar Sep 15 '20 11:09 mohdibntarek

Thanks @mohamed82008 ! That does indeed work. So I guess

  • ~~the custom gradient is not working~~
  • ReverseDiff gives me incorrect gradients when it doesn't know what to do instead of producing an error (unsound-and-incomplete as opposed to sound-but-incomplete).
  • Documentation/example for using ReverseDiff.track etc.

samuela avatar Sep 16 '20 23:09 samuela