JuliaDiff / ReverseDiff.jl

Reverse Mode Automatic Differentiation for Julia
Other
348 stars 57 forks source link

`@grad_from_chainrules` hygiene: cannot use custom types in method signature #224

Closed manuelbb-upb closed 1 year ago

manuelbb-upb commented 1 year ago

As noticed somewhere in https://github.com/FluxML/NNlib.jl/issues/484, it appears that @grad_from_chainrule is not able to see custom types in method signatures. I looked through the issues here and hope I did not overlook it. My guess is that method argument expressions should be escaped, here or in the parsing later on.

MWE

using ReverseDiff
struct myNumber val end

# define chain rules here ...
ReverseDiff.@grad_from_chainrules sin(x::myNumber)

This gives ERROR: UndefVarError: myNumber not defined. If we look into @macroexpand ... we see stuff like sin(var"#54#x"::ReverseDiff.myNumber).

avik-pal commented 1 year ago

+1 for this. I had to solve this by manually defining the functions in https://github.com/LuxDL/LuxLib.jl/blob/65337c4b5cb8c49cca5472e9aad9e3d69d51e553/ext/LuxLibReverseDiffExt.jl#L50-L108

mohamed82008 commented 1 year ago

https://github.com/JuliaDiff/ReverseDiff.jl/pull/232

mohamed82008 commented 1 year ago

fixed by #232