autodiff
This manual documents the unreleased main branch of Luna-Flow/autodiff
(version 0.3.1 in moon.mod) after its migration to MoonBit 0.10.
Overview
Luna-Flow/autodiff computes exact first derivatives of ordinary MoonBit
programs by forward-mode automatic differentiation. A program written
against the Luna Flow traits is run on dual numbers with
, and the derivative appears in the
component. The repository provides the dual-number type, scalar drivers,
checked domains for division and square roots, gradients and Jacobians over
linear-algebra vectors, and derivatives of luna-poly polynomials.
Dual[T]is generic over the scalar and implements exactly the ring-level traits ofluna-generic: noField,Inverseor order, because has no inverse.- Every elementary rule is , and the chain rule follows from composition.
- Gradients and Jacobians take one forward pass per input; there is no reverse mode.
Install
moon add Luna-Flow/autodiff@0.3.1
Then import the root package in your moon.pkg:
import {
"Luna-Flow/autodiff",
}
The bridges need their libraries too: moon add Luna-Flow/linear-algebra
for linalg and moon add Luna-Flow/luna-poly for poly. The code needs
the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20) with moon.mod /
moon.pkg manifests, and the test suite runs on the wasm-gc, wasm, js
and native targets.
Pages
The root package Luna-Flow/autodiff is documented as autodiff, because
the name core belongs to the package src/core. Every other page is named
after its directory under src/.
| Part | Tutorial | API | Design |
|---|---|---|---|
autodiff: one-stop facade with Dual, diff, value_and_diff and the re-exported traits | tutorial | API | design |
dual: the Dual[T] type, its arithmetic, derivative rules and instances | tutorial | API | design |
forward: scalar drivers diff and value_and_diff | tutorial | API | design |
core: algebraic facade, Dual and the luna-generic structure traits | tutorial | API | design |
elementary: analytic facade, Dual and the arithmetic function traits | tutorial | API | design |
checked: checked facade, DivChecked, SqrtChecked, context and errors | tutorial | API | design |
linalg: gradients and Jacobians over linear-algebra/immut | tutorial | API | design |
poly: derivatives of dense and univariate sparse luna-poly polynomials | tutorial | API | design |
| Guides | architecture | conventions |
Two more packages have no manual pages. examples holds five small
functions (basic_diff_example, square_diff_example, gradient_example,
jacobian_example, polynomial_derivative_example) that show each layer in
a few lines; read src/examples/examples.mbt.
tests is the black-box test suite of all packages, including the
linear-algebra and luna-poly integration tests.
Exported items
The number type
Dual[T]withDual::new,constant,variable,value,tangent,zero,one,from_naturalandfrom_integer- Arithmetic:
add,sub,neg,mul,divandequal, also as operators - Elementary rules:
sqrt,exp,exp2,ln,log2,log10,sin,cos,tan - Checked forms:
div_checked,sqrt_checked
Drivers
- Scalar:
diff,value_and_diff - Vector:
gradient,value_and_gradient,jacobian,value_and_jacobian - Polynomial:
eval_dual,dense_value_and_derivative_at,dense_derivative_at, thesparse_univariate_*functions and their compatibility names
Re-exported vocabulary
- From
luna-generic:Zero,One,AddMonoid,AddGroup,MulMonoid,Semiring,Ring,FromNat,FromInteger - From
arithmetic:Sqrt,SqrtChecked,Exponential,Logarithmic,Trigonometric,Constants,DivChecked,ArithmeticContext,ArithmeticError,ArithmeticErrorKind,RoundingMode - From
linear-algebraandluna-poly:Vector,Matrix,DensePolynomial,SparsePolynomialin the bridge packages
Where to read next
The autodiff tutorial differentiates a function with one import. The dual API lists every rule and instance of the number type, and the dual design derives them, with the rounding error bounds and the comparison with finite differences.
- New to automatic differentiation: read the autodiff tutorial, then the dual tutorial, which compares dual numbers with finite differences.
- Using it in a library: import
Luna-Flow/autodiffand keep the autodiff API and the dual API at hand; add the linalg tutorial or the poly tutorial for vectors and polynomials, and the checked tutorial when failures must be values. - Contributing: read the dual design for the algebra, the derivative rules and the error bounds that every new operation must respect, then the architecture guide, the repository conventions and the design page of the package you change.
Validation
Recommended release checks:
moon check --target all
./run_test.sh
moon info