Expand description
§chainrules
chainrules provides a shared scalar rule basis for Rust automatic
differentiation crates.
It is designed for reusable scalar calculus, not for tapes, traced values, or tensor-specific execution engines. The crate focuses on stateless helpers that can be called from downstream AD runtimes and tensor libraries.
§What It Provides
- stateless scalar primal helpers
- stateless scalar
foo_frulehelpers - stateless scalar
foo_rrulehelpers - real/complex projection helpers for common scalar formulas
Supported scalar domains:
f32f64Complex32Complex64
§Supported Functions
Current shipped scalar families:
- arithmetic:
add,sub,mul,div - powers and roots:
powf,powi,sqrt - exponentials and logs:
exp,expm1,log,log1p - trigonometric:
sin,cos,asin,acos,atan - hyperbolic:
sinh,cosh,tanh,asinh,acosh,atanh - Julia-compatible trigonometric helpers:
sec,csc,cot,sinpi,cospi,sincospi,sind,cosd,tand - Julia-compatible hyperbolic helpers:
sech,csch,coth - non-smooth real helpers:
round,floor,ceil,sign,min,max - smooth helpers:
cbrt,inv,exp2,exp10,log2,log10,hypot,pow,sincos,tan - complex and projection helpers:
conj,abs,abs2,angle,real,imag,complex - real-valued binary helpers:
atan2
This crate is a landing zone for scalar rules ported or adapted from Julia’s
ChainRules.jl where they fit this crate boundary, but it is not a full port
of ChainRules.jl.
§Validation
Rules in this crate are not accepted on provenance alone. They are checked with repository-local tests:
tests/scalarops_tests.rscovers direct formulas, edge cases, and smooth real/complex behaviortests/julia_compat_trig_tests.rscovers Julia migration helpers, including landmark real inputs and representative Complex64 behaviortests/nonsmooth_scalar_tests.rscovers the documented zero-gradient and tie-routing policies for non-smooth helperstests/complex_helper_tests.rscovers the projection helpers and complex constructor surfacetests/oracle_scalar_rules.rsreplays vendored published oracle cases from../../third_party/tensor-ad-oracles, with direct float64 replay and selected direct Complex64 replay fortan,exp2, andlog2- complex forward-mode checks use the standard JVP convention on
C ~= R^2in repository-local formula tests such astests/smooth_basis_tests.rs - complex reverse-mode checks remain conjugate-Wirtinger for real-valued losses
§Examples
use chainrules::{powf, powf_frule, powf_rrule};
let y = powf(2.0_f64, 3.0_f64);
assert_eq!(y, 8.0_f64);
let (y, dy) = powf_frule(2.0_f64, 3.0_f64, 1.0_f64);
assert_eq!(y, 8.0_f64);
assert_eq!(dy, 12.0_f64);
let dx = powf_rrule(2.0_f64, 3.0_f64, 1.0_f64);
assert_eq!(dx, 12.0_f64);§Notes
This crate is the landing zone for shared scalar rule logic, including Julia-style convenience functions when they help migration.
It does not define tensor, array, broadcast, reduction, or engine-specific rules.
Structs§
- NodeId
- Stable identifier of an AD graph node.
Enums§
- Autodiff
Error - AD-specific error type.
- Save
Policy - Saved-tensor retention policy for reverse-mode rules.
Traits§
- Differentiable
- Trait defining the tangent space for a differentiable type.
- Forward
Rule - Forward-mode AD rule interface (frule).
- Reverse
Rule - Reverse-mode AD rule interface (rrule).
- Scalar
Ad - Scalar trait used by elementary AD rule helpers.
Functions§
- abs
- Primal
abs. - abs2
- Primal
abs2. - abs2_
frule - Forward rule for
abs2. - abs2_
rrule - Reverse rule for
abs2. - acos
- Primal
acos. - acos_
frule - Forward rule for
acos. - acos_
rrule - Reverse rule for
acos. - acosh
- Primal
acosh. - acosh_
frule - Forward rule for
acosh. - acosh_
rrule - Reverse rule for
acosh. - add
- Primal
add. - add_
frule - Forward rule for
add. - add_
rrule - Reverse rule for
add. - angle
- Primal
angle. - angle_
rrule - Reverse rule for
angle. - asin
- Primal
asin. - asin_
frule - Forward rule for
asin. - asin_
rrule - Reverse rule for
asin. - asinh
- Primal
asinh. - asinh_
frule - Forward rule for
asinh. - asinh_
rrule - Reverse rule for
asinh. - atan
- Primal
atan. - atan2
- Primal
atan2(y, x)for ordered real scalars. - atan2_
frule - Forward rule for
atan2(y, x). - atan2_
rrule - Reverse rule for
atan2(y, x). - atan_
frule - Forward rule for
atan. - atan_
rrule - Reverse rule for
atan. - atanh
- Primal
atanh. - atanh_
frule - Forward rule for
atanh. - atanh_
rrule - Reverse rule for
atanh. - cbrt
- Primal
cbrt. - cbrt_
frule - Forward rule for
cbrt. - cbrt_
rrule - Reverse rule for
cbrt. - ceil
- Primal
ceil. - ceil_
frule - Forward rule for
ceil. - ceil_
rrule - Reverse rule for
ceil. - complex
- Construct a complex number from real and imaginary parts.
- conj
- Primal
conj. - conj_
frule - Forward rule for
conj. - conj_
rrule - Reverse rule for
conj. - cos
- Primal
cos. - cos_
frule - Forward rule for
cos. - cos_
rrule - Reverse rule for
cos. - cosd
- Primal
cosd. - cosd_
frule - Forward rule for
cosd. - cosd_
rrule - Reverse rule for
cosd. - cosh
- Primal
cosh. - cosh_
frule - Forward rule for
cosh. - cosh_
rrule - Reverse rule for
cosh. - cospi
- Primal
cospi. - cospi_
frule - Forward rule for
cospi. - cospi_
rrule - Reverse rule for
cospi. - cot
- Primal
cot. - cot_
frule - Forward rule for
cot. - cot_
rrule - Reverse rule for
cot. - coth
- Primal
coth. - coth_
frule - Forward rule for
coth. - coth_
rrule - Reverse rule for
coth. - csc
- Primal
csc. - csc_
frule - Forward rule for
csc. - csc_
rrule - Reverse rule for
csc. - csch
- Primal
csch. - csch_
frule - Forward rule for
csch. - csch_
rrule - Reverse rule for
csch. - div
- Primal
div. - div_
frule - Forward rule for
div. - div_
rrule - Reverse rule for
div. - exp
- Primal
exp. - exp2
- Primal
2^x. - exp2_
frule - Forward rule for
2^x. - exp2_
rrule - Reverse rule for
2^x. - exp10
- Primal
10^x. - exp10_
frule - Forward rule for
10^x. - exp10_
rrule - Reverse rule for
10^x. - exp_
frule - Forward rule for
exp. - exp_
rrule - Reverse rule for
exp. - expm1
- Primal
exp(x) - 1. - expm1_
frule - Forward rule for
exp(x) - 1. - expm1_
rrule - Reverse rule for
exp(x) - 1. - floor
- Primal
floor. - floor_
frule - Forward rule for
floor. - floor_
rrule - Reverse rule for
floor. - hypot
- Primal
hypot. - hypot_
frule - Forward rule for
hypot. - hypot_
rrule - Reverse rule for
hypot. - imag
- Primal
imag. - imag_
rrule - Reverse rule for
imag. - inv
- Primal
inv. - inv_
frule - Forward rule for
inv. - inv_
rrule - Reverse rule for
inv. - log
- Primal
log. - log2
- Primal
log2. - log1p
- Primal
log(1 + x). - log1p_
frule - Forward rule for
log(1 + x). - log1p_
rrule - Reverse rule for
log(1 + x). - log2_
frule - Forward rule for
log2. - log2_
rrule - Reverse rule for
log2. - log10
- Primal
log10. - log10_
frule - Forward rule for
log10. - log10_
rrule - Reverse rule for
log10. - log_
frule - Forward rule for
log. - log_
rrule - Reverse rule for
log. - max
- Primal
max. - max_
frule - Forward rule for
max. - max_
rrule - Reverse rule for
max. - min
- Primal
min. - min_
frule - Forward rule for
min. - min_
rrule - Reverse rule for
min. - mul
- Primal
mul. - mul_
frule - Forward rule for
mul. - mul_
rrule - Reverse rule for
mul. - pow
- Primal
pow(x, exponent). - pow_
frule - Forward rule for
pow(x, exponent). - pow_
rrule - Reverse rule for
pow(x, exponent). - powf
- Primal
powf. - powf_
frule - Forward rule for
powfwith fixed exponent. Returns(primal, tangent). - powf_
rrule - Reverse rule for
powfwith fixed exponent. - powi
- Primal
powi. - powi_
frule - Forward rule for
powiwith fixed integer exponent. Returns(primal, tangent). - powi_
rrule - Reverse rule for
powiwith fixed integer exponent. - real
- Primal
real. - real_
rrule - Reverse rule for
real. - round
- Primal
round. - round_
frule - Forward rule for
round. - round_
rrule - Reverse rule for
round. - sec
- Primal
sec. - sec_
frule - Forward rule for
sec. - sec_
rrule - Reverse rule for
sec. - sech
- Primal
sech. - sech_
frule - Forward rule for
sech. - sech_
rrule - Reverse rule for
sech. - sign
- Primal
sign. - sign_
frule - Forward rule for
sign. - sign_
rrule - Reverse rule for
sign. - sin
- Primal
sin. - sin_
frule - Forward rule for
sin. - sin_
rrule - Reverse rule for
sin. - sincos
- Primal
sincos. - sincos_
frule - Forward rule for
sincos. - sincos_
rrule - Reverse rule for
sincos. - sincospi
- Primal
sincospi. - sincospi_
frule - Forward rule for
sincospi. - sincospi_
rrule - Reverse rule for
sincospi. - sind
- Primal
sind. - sind_
frule - Forward rule for
sind. - sind_
rrule - Reverse rule for
sind. - sinh
- Primal
sinh. - sinh_
frule - Forward rule for
sinh. - sinh_
rrule - Reverse rule for
sinh. - sinpi
- Primal
sinpi. - sinpi_
frule - Forward rule for
sinpi. - sinpi_
rrule - Reverse rule for
sinpi. - sqrt
- Primal
sqrt. - sqrt_
frule - Forward rule for
sqrt. - sqrt_
rrule - Reverse rule for
sqrt. - sub
- Primal
sub. - sub_
frule - Forward rule for
sub. - sub_
rrule - Reverse rule for
sub. - tan
- Primal
tan. - tan_
frule - Forward rule for
tan. - tan_
rrule - Reverse rule for
tan. - tand
- Primal
tand. - tand_
frule - Forward rule for
tand. - tand_
rrule - Reverse rule for
tand. - tanh
- Primal
tanh. - tanh_
frule - Forward rule for
tanh. - tanh_
rrule - Reverse rule for
tanh.
Type Aliases§
- AdResult
- Result alias for AD APIs.
- Pullback
Entry - Reverse-rule pullback output entry
(input_node, input_cotangent). - Pullback
With Tangents Entry - Reverse-rule pullback-with-tangents output entry.