-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to @opt_out rules with RuleConfig #1342
Comments
Oh weird. This is definitely a bug. I am on vacation for next few weeks. And don't know when exactly I will have time to look into it. |
Thanks for confirming that's a bug. I will share this info on the Julia slack and see if anyone can help. |
Update: if I instead add using ChainRulesCore: @opt_out, RuleConfig, rrule
using Zygote: ZygoteRuleConfig
import Base: ^
struct WeirdNumber <: Number
a::Float64
end
^(x::WeirdNumber, p::Int) = WeirdNumber(x.a ^ p)
@opt_out rrule(::typeof(^), x::WeirdNumber, p::Number)
@opt_out rrule(::typeof(Base.literal_pow), ::typeof(^), x::WeirdNumber, ::Val{p}) where {p}
@opt_out rrule(::RuleConfig, ::typeof(Base.literal_pow), ::typeof(^), x::WeirdNumber,
::Val{p}) where {p}
@opt_out rrule(::ZygoteRuleConfig, ::typeof(Base.literal_pow), ::typeof(^), x::WeirdNumber,
::Val{p}) where {p}
using Zygote
fun(x::WeirdNumber) = (x^4).a
gradient(fun, WeirdNumber(2.)) |
I'm attaching here some discussions on Slack so it may help to diagnose the issue Brian Chen Frames Catherine White Brian Chen Songchen Tan Songchen Tan Songchen Tan |
Hmm. sol0Just give informative ambiguity error if If sol1If This solved this case, since goal is to ignore the rule. sol2There might be an alternative solution where we only do this if there is an I am not sure if this is logically sound. |
Thanks for the purposed solutions. I'm not an expert of Zygote so couldn't compare on those, probably invite other Zygote maintainers here. To me this ambiguity sounds like a design problem to ChainRulesCore.jl, since for any AD system, the coexistence of "RuleConfig + Specific Type" method by user and "XXXRuleConfig + Generic Type" by AD author will cause an ambiguity. |
We could add a param to the context which controls whether the fallback is allowed or an error is thrown. I agree that this feels suboptimal though. Sol2 could be narrowed to if there is a |
I mean, it's a real ambiguity. There is no definite right way to handle it. |
I am inclined to open a PR with Sol 1, because it seems easy enough to implement, |
Thanks for the followup, solution 1 definitely works for my problem. (Although it won't work if someone else is trying to use "RuleConfig + Specific Type" pattern to implement custom rules instead of opting out.) I will be happy to try out when you start working on the solution 😄 |
@oxinabox Thank you for this quick fix. I will add Zygote as a dependency and write |
I think that is fine. If you update to Julia 1.9 you can make it a weak dependency. |
Let's say I have a type
WeirdNumber <: Number
is so weird that I don't want its derivative of power function (^
) be calculated byrrule
of^
andliteral_pow
, and should instead go through and differentiate its definition. Since Zygote defines AD-specific rules forliteral_pow
withRuleConfig
in its code base, I had to also@opt_out
this rule. However, the following MWE didn't work:Error:
The text was updated successfully, but these errors were encountered: