Repository navigation
Description
Flow 0.12.0.
Porting a differentiable physics kernel meant hand-rolling a complex matrix type, a complex GEMM binding, a matrix exponential and its adjoint. All four belong in the standard library, and the reason none of them are there is a consistent one: every numerical facility in lib/stdlib/ is real-valued, and everything differentiable is scalar. Complex, matrix-shaped and differentiable is the empty cell.
What exists today
| Facility | Module | Stops at |
|---|---|---|
| Forward AD | autodiff.flow |
f32 scalars |
| Reverse AD tape | autodiff.flow, runtime/flow_tape.c |
f32 scalars, only mul and add |
| Reverse AD pairs | autodiff_reverse.flow |
f32 scalars, no tape |
| Gradient codegen | scripts/tools/grad/flow_grad_flow.py |
straight-line f32 scalar, no arrays or loops |
| Tensors | tensor.flow |
f32 real, chain rule wired by the caller |
| BLAS | blas.flow |
f64 real |
| Complex | c64/c128 in the language |
nothing in stdlib consumes it except rf.flow |
The c64/c128 types themselves are good. They map cleanly to C99 _Complex, arithmetic and cexp/cabs work, and ptr<c128> behaves. They just have no library around them.
Five additions, cheapest first
1. Wrap the LAPACK that is already declared. blas.flow has extern declarations for dgeev_, dgesvd_, dgeqrf_, dpotrf_ and dgetri_ and zero wrappers for any of them. The FFI half is done; only the ergonomic layer is missing.
2. Complex BLAS. A CMat beside Mat, plus zgemm, zgesv and zheev.
3. expm. Scaling and squaring around a Taylor or Pade polynomial. Any port of a physics or control model needs it.
4. Matrix adjoints paired with the primitives. tensor.flow already demonstrates the pattern with tensor_matmul_backward_a. Extending it to complex types and to gemm, solve, eigh and expm, so each ships beside its pullback, is what makes a differentiable numerical port assembly rather than derivation.
5. Loops and spans in the gradient generator. It currently handles straight-line scalar f32. A version that handled for i in 0 to values.len over a span would cover a large fraction of real numerical kernels.
Implementation for 1, 2, 3 and part of 4
Written and passing. Two new wrappers in blas.flow plus a new lib/stdlib/clinalg.flow:
export struct CMat { data: ptr<c128>, n: i32, owns: i32 }
export function cmat_mul(A: CMat, B: CMat, out: CMat) -> void # zgemm
export function cmat_solve(A: CMat, B: CMat) -> i32 # zgesv
export function cmat_eigh(A: CMat, w: ptr<f64>, vectors: CMat) -> i32 # zheev
export function expm(A: CMat, out: CMat) -> void
export function expm_adjoint(A: CMat, seed: CMat, out: CMat) -> void
plus eigvals, cholesky and inverse in blas.flow.
expm is scaling and squaring around a degree-18 Taylor polynomial evaluated Paterson-Stockmeyer in A^3, seven matrix products rather than eighteen.
expm_adjoint is the pullback of exp, which is the upper-right block of exp([[A^H, seed], [0, A^H]]). It never forms that block matrix. Matrices of that shape are closed under multiplication:
[ Y1 L1 ] [ Y2 L2 ] [ Y1 Y2 Y1 L2 + L1 Y2 ]
[ 0 Y1 ] [ 0 Y2 ] = [ 0 Y1 Y2 ]
so the whole evaluation runs on the pair (Y, L) at three n x n products apiece instead of the eight a dense 2n x 2n product costs. Same arithmetic, 2.7x fewer products.
CMat uses ptr<c128> with the extent in the struct rather than span<c128>, because the flowc host does not lower spans (filed separately).
Examples under examples/linalg/ check against closed forms rather than against themselves: Q diag(w) Q^H reconstructs the Hermitian input, L L^T reconstructs the symmetric one, exp(iH) is unitary, and expm_adjoint matches a central difference. Two of those caught real mistakes on the first run: a single-precision Taylor threshold used for f64, and a Cholesky wrapper that transposed the factor and then zeroed the half holding the data.
Happy to open this as a PR. Say the word and I will push the branch.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
stdlib: pure-Flow complex linear algebra (#616) (#880)