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 function f 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 for AutoTargetAD.
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)
As explored in discussions (e.g., #68), we can introduce a flexible "rule switch" mechanism into
DifferentiationInterface.jl(DI) by generalising the existingDifferentiateWithfunctionality.The central concept is a macro that allows for the dynamic generation of differentiation rules:
In this definition:
@rule_from_DifferentiateWithis a macro designed to generate new differentiation rules.AutoTargetADspecifies the automatic differentiation (AD) package for which the new rule is being created (e.g.,AutoChainRules).func_sigrepresents the signature of the functionfalong with its argument types (argtypes...) for which the rule is being defined.backend_to_useindicates 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