JuliaDiff / JuliaDiff/ReverseDiff.jl

Custom gradient not getting called, incorrect zero gradients

Open
#150 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
393
Forks
60
Avg merge
18h 24m
Merged PRs (30d)
8

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in difftaichi/zygote_zero_nothing_bug.jl, starting with the ReverseDiff.@grad definition and the ReverseDiff.gradient call around the PyCall math.sin FFI function. Trace whether the custom gradient entry point is reached and how the unsupported operation produces a zero gradient; done means the behavior is covered by a clear regression test and the reported gradient behavior is no longer incorrect.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia, python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.