Open
Description
As explored in discussions (e.g., #68), we can introduce a flexible "rule switch" mechanism into DifferentiationInterface.jl
(DI) by generalising the existing DifferentiateWith
functionality.
The central concept is a macro that allows for the dynamic generation of differentiation rules:
Targtypes = (typeof(x), typeof(y), Constant{typeof(z)}, Cache{typeof(c)})
Tfunc_sig =Tuple{typeof(f), Targtypes...}
@rule_from_DifferentiateWith(AutoTargetAD, ::Tfunc_sig, backend_to_use::AbstractADType)
In this definition:
@rule_from_DifferentiateWith
is a macro designed to generate new differentiation rules.AutoTargetAD
specifies the automatic differentiation (AD) package for which the new rule is being created (e.g.,AutoChainRules
).func_sig
represents the signature of the functionf
along with its argument types (argtypes...
) for which the rule is being defined.backend_to_use
indicates another AD package (the "backend") whose capabilities will be leveraged to define the rule forAutoTargetAD
.
This approach provides a powerful and general way to establish a "rule switch mechanism." It avoids imposing strict assumptions on the rule interface, unlike systems such as ChainRules
, offering greater flexibility.
After #806, we could also do
dw = DifferentiateWith(Tfunc_sig, backend_to_use::AbstractADType)
@rule_from_DifferentiateWith(AutoTargetAD, dw)