| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Circuit.Poly.DiffP
Description
Bridging parameterised reverse-mode AD (DiffP) and polynomial monomial
lenses.
A 'DiffP p a b' is a parametric lens: for every parameter value p it
gives an ordinary lens Mono a a -> Mono b b, and it additionally produces
a parameter gradient dp. In Poly this is naturally expressed via the
copower Depend: a parameter-indexed family of lenses.
This gives the "shared parameter" reading of DiffP a home in Poly: the
parameter is the index of a lens family, not an extra layer. The
paired-parameter reading (splitP, joinP) remains
available for independent-layer composition.
Synopsis
- diffPAt :: DiffP p a b -> p -> Morphism (Mono a a) (Mono b b)
- diffPAsFamily :: DiffP p a b -> Morphism ('Prod ('Const p) (Mono a a)) (Mono b b)
- diffPParamGrad :: DiffP p a b -> p -> a -> b -> p
- diffPFromFamily :: (p -> Morphism (Mono a a) (Mono b b)) -> (p -> a -> b -> p) -> DiffP p a b
- traceDiffPFrom :: (StarSemiring j, MergeZero (->) o) => j -> Int -> DiffP p (j, i) (j, o) -> DiffP p i o
- traceDiffPD :: MergeZero (->) o => Double -> Double -> Int -> DiffP p (Double, i) (Double, o) -> DiffP p i o
- traceDiffPMatrix :: MergeZero (->) o => [Double] -> Double -> Int -> DiffP p ([Double], i) ([Double], o) -> DiffP p i o
DiffP as a parameter-indexed lens family
diffPAt :: DiffP p a b -> p -> Morphism (Mono a a) (Mono b b) Source #
The lens at a fixed parameter value.
Forward: a -> b. Backward: a -> db -> da.
diffPParamGrad :: DiffP p a b -> p -> a -> b -> p Source #
The parameter gradient extracted from a DiffP.
For a parameter p, input a and output cotangent db, return dp.
Recover a DiffP from its Poly decomposition
diffPFromFamily :: (p -> Morphism (Mono a a) (Mono b b)) -> (p -> a -> b -> p) -> DiffP p a b Source #
Recover a DiffP from its fixed-parameter lens family and parameter
gradient. This is the inverse of the diffPAt/diffPParamGrad split.
Star-based feedback trace
Arguments
| :: (StarSemiring j, MergeZero (->) o) | |
| => j | forward seed for the state channel |
| -> Int | number of forward iterations |
| -> DiffP p (j, i) (j, o) | |
| -> DiffP p i o |
Star-based trace for DiffP.
The forward pass iterates the state channel to a fixed point; the backward
pass solves the feedback adjoint using the Kleene star of the channel
self-coupling. This is the Schur-complement view of backpropagation
through feedback: for a body s' = f(s, i), o = g(s, i) linearised at
the fixed point, the closed gradient is
do/di = D + C · star(A) · B
where A = ∂s'/∂s, B = ∂s'/∂i, C = ∂o/∂s, D = ∂o/∂i.
The same star appears in the parameter gradient.
Arguments
| :: MergeZero (->) o | |
| => Double | forward seed for the state channel |
| -> Double | residual tolerance for the primal fixed point |
| -> Int | maximum number of forward iterations |
| -> DiffP p (Double, i) (Double, o) | |
| -> DiffP p i o |
traceDiffPFrom specialised to a scalar Double state channel.
The primal is iterated until |s' - s| <= tol or maxIter is reached.
The feedback Jacobian A is then probed and guarded: |A| >= 1 is
rejected with an error, because outside the contractive regime the star
1/(1-A) either diverges or inverts the sign. This closes the §7
silent-failure gap for the scalar case.
Arguments
| :: MergeZero (->) o | |
| => [Double] | forward seed for the state channel (its length is the channel dimension) |
| -> Double | residual tolerance for the primal fixed point |
| -> Int | maximum number of forward iterations |
| -> DiffP p ([Double], i) ([Double], o) | |
| -> DiffP p i o |
Star-based trace for a vector-channel DiffP.
The state channel is a list [[Double]] of fixed dimension. The forward pass
iterates to a fixed point; the backward pass probes the feedback Jacobian
column by column, builds a Matrix, and solves the adjoint with
starMatrix. Each column is wrapped in FieldStar so the matrix star is
honest (I − A)⁻¹.
This is the multi-agent extension of traceDiffPD: instead of a scalar
self-coupling a, the feedback Jacobian is a matrix A, and the star is
the Neumann series (I − A)⁻¹.