attribute [BackwardDerivative]
Description
[BackwardDerivative(bwdFn)] attribute can be used to provide a forward-mode derivative implementation. Invoking bwd_diff(decoratedFn) will place a call to bwdFn instead of synthesizing a derivative implementation. The same behavior holds if decoratedFn is used in a differentiable context.
Signature
[BackwardDerivative(function)]
Parameters
function
Remarks
The signature of bwdFn must match the expected signature of bwd_diff(decoratedFn). See the reference for bwd_diff for a full list of signature rules.
See the user guide’s section on custom derivatives for an introduction to custom derivatives.
This attribute can be used on generic functions, member functions and accessors. For generic functions, the generic signatures (parameters + constraints) of both functions must match exactly. Overloaded functions are also supported. The compiler will attempt to resolve the overload from the expected derivative signature. If it is unable to do so, it will issue a diagnostic error.
The decorated function will be considered differentiable. There is no need for a [Differentiable] tag.
Example:
[BackwardDerivative(foo_bwd)]
T foo<T : IFloat, P : IArray<T>>(T x, P xarr) { /* ... */ }
void foo_bwd<T : IFloat, P : IArray<T>>( // Use the same generic signature for a match.
inout DifferentialPair<T> x, P dp_xarr, T.Differential dresult) { /* ... */ }
For member functions, or functions nested inside namespaces, bwdFn may need to be a fully qualified name.