circuits-diagrams
Safe HaskellNone
LanguageGHC2024

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

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.

diffPAsFamily :: DiffP p a b -> Morphism ('Prod ('Const p) (Mono a a)) (Mono b b) Source #

View a DiffP as a Poly morphism Const p * Mono a a -> Mono b b.

The parameter is carried by the constant functor; choosing a position p selects one lens from the family.

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

traceDiffPFrom Source #

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.

traceDiffPD Source #

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.

traceDiffPMatrix Source #

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)⁻¹.

Orphan instances

MergeZero (->) p => Strength (,) (DiffP p :: Type -> Type -> Type) Source #

Cartesian strength for DiffP.

Threads a plain morphism through the feedback channel, copying the parameter gradient unchanged.

Instance details

Methods

strength :: DiffP p b c -> DiffP p (a, b) (a, c) #