JuliaDiff / ChainRulesCore.jl

AD-backend agnostic system defining custom forward and reverse mode rules. This is the light weight core to allow you to define rules for your functions in your packages, without depending on any particular AD system.
Other
253 stars 61 forks source link

Better error message when using @scalar_rule on functors/closures #203

Open avik-pal opened 4 years ago

avik-pal commented 4 years ago
struct MyStruct
    a
end

(m::MyStruct)(t) = m.a

@scalar_rule (m::Struct)(t) one(m.a)

Results in UndefVarError: m not defined.

As per the docs, this should not work. @oxinabox pointed out on slack that this should lead to a more meaningful error message.

willtebbutt commented 4 years ago

Although there's probably something to be said for letting functors work with @scalar_rule if none of the parameters are differentiable, I suspect it's safest just to disallow them entirely.

oxinabox commented 4 years ago

To my mind @scalar_rule should stick close to what DiffRules.jl allows and not do more. Thus it should not permit functors, and indeed the code is supposed to error for them. https://github.com/JuliaDiff/ChainRulesCore.jl/blob/36b0449792da2b91fee2c6c30288174a2f3085d2/src/rule_definition_tools.jl#L89

I suspect we are not successfully capturing the f, and thus are skipping that error message