attribute [ForwardDerivative]¶
Description¶
[ForwardDerivative(fwdFn)] attribute can be used to provide a forward-mode derivative implementation. Invoking fwd_diff(decoratedFn) will place a call to fwdFn instead of synthesizing a derivative implementation. The same behavior holds if decoratedFn is used in a differentiable context.
Signature¶
[ForwardDerivative(function)]
Parameters¶
function¶
Remarks¶
The signature of fwdFn must match the expected signature of fwd_diff(decoratedFn).
See the reference for fwd_diff for a full list of signature rules.
See the user guide’s section on custom derivatives for an introduction to this approach.
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.
The decorated function will be considered forward-differentiable. There is no need for a [Differentiable] tag. If the [Differentiable] tag is present, and no custom backward derivative is specified with [BackwardDerivative], then the Slang will use auto-diff to generate the backward=mode derivative, but will use the provided derivative for forward-mode.
Example:
[ForwardDerivative(foo_fwd)]
T foo<T : IFloat, P : IArray<T>>(T x, P xarr) { /* ... */ }
DifferentialPair<T> foo_fwd<T : IFloat, P : IArray<T>>( // Use the same generic signature for a match.
DifferentialPair<T> x, P dp_xarr) { /* ... */ }
For member functions, or functions nested inside namespaces, fwdFn may need a fully qualified name.
Example:
namespace A
{
DifferentialPair<float> foo(DifferentialPair<float> x) { /* ... */ }
}
[ForwardDerivative(A.foo)] // Use namespace and/or parent struct names
float bar(float x) { /* ... */ }