ReverseDiff.jl
ReverseDiff.jl copied to clipboard
Custom gradient not getting called, incorrect zero gradients
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
Yeah, at a quick glance it looks to me like you did it correctly, so I'll let @mohamed82008 handle this.
You missed
pysin(x::ReverseDiff.TrackedArray) = ReverseDiff.track(pysin, x)
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.tracketc.