JuliaDiff / JuliaDiff/ReverseDiff.jl

Custom gradient for callable struct

Open
#135 0 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

This doesn't seem to work, simple example:

struct Over{T} den::T end

(o::Over)(x) = x ./ o.den

using ReverseDiff

(o::Over)(x::ReverseDiff.TrackedArray) = ReverseDiff.track(o, x)

ReverseDiff.@grad function (o::Over)(x)
    ReverseDiff.value(x) ./ o.den, Δ -> (Δ .* o.den,)
end
# ERROR: UndefVarError: o not defined

This is the error after changing this line to closure = gensym(string(f)):
https://github.com/JuliaDiff/ReverseDiff.jl/blob/a7441cef4d5d1d8361946662bbab03856c2f6449/src/macros.jl#L182
or equivalently defining

 Base.gensym(ex::Expr) = gensym(string(ex))

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

Start with the callable-struct example in the issue and inspect the @grad macro around src/macros.jl line 182, especially how the callable expression is converted for gensym. Reproduce the UndefVarError, then verify that the custom gradient definition for Over works without the reported workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.