Documentation

CslibFrontier.Computability.Circuit.Arithmetic.Gradient

Reverse-mode differentiation of arithmetic circuits (Baur–Strassen) #

Baur and Strassen proved that a function and all its first partial derivatives can be computed together at a constant factor of the cost of the function alone [BS83]; Morgenstern gave the reverse-mode formulation used here [Mor85]. See also Bürgisser, Clausen and Shokrollahi, §7.2.

Cslib.Circuits.Circuit.withGradient c j seed zero runs the gates of c, emits the constant gates seed and zero, and then walks the gates of c from the last to the first, keeping for every wire an adjoint wire. The adjoint of output j starts at seed, every other adjoint at zero. An addition gate adds its adjoint into the adjoints of both arguments. A multiplication gate u * v with adjoint a adds a * v into the adjoint of u and a * u into the adjoint of v. A wire read several times receives one contribution per read. The circuit keeps the outputs of c and appends the adjoints of the n inputs.

The model is the library's: circuits over Cslib.Circuits.arithSignature K, with fan-in two addition and multiplication and a constant gate for each element of K, computing polynomials formally. Size counts every gate, constant gates included. There is no subtraction, so the construction works over any commutative semiring.

For the power sum ∑ i, X i ^ (d + 1) with d + 1 invertible in K, the gradient scaled by (d + 1)⁻¹ is the power map (X 0 ^ d, …, X (n - 1) ^ d) (Cslib.Circuits.Circuit.exists_pow_of_psum). Strassen's degree bound [Str73a] (see Bürgisser, Clausen and Shokrollahi, Chapter 8) shows that the power map needs about n log₂ d nonscalar multiplications, so the single power sum needs a third of that. The degree bound is not formalized here; the reduction is stated as a lower-bound transfer (Cslib.Circuits.Circuit.le_three_mul_mulCost_of_psum).

Main definitions #

Main results #

References #

def Cslib.Circuits.Circuit.withGradient {C : Type u} {n m : ℕ} (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) :

Reverse-mode differentiation [BS83]: the circuit c followed by n further outputs, the adjoints of the inputs for output j, seeded by the constant seed. The constant zero initializes the other adjoints. Outputs 0, …, m - 1 are those of c; with seed = 1 and zero = 0, output m + i computes the partial derivative of output j along input i.

An arithmetic program has at most as many additions and multiplications together as gates.

theorem Cslib.Circuits.Circuit.eval_withGradient_castAdd {C : Type u} {n m : ℕ} {R : Type u_1} [CommSemiring R] (constant : C → R) (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) (x : Fin n → R) (k : Fin m) :
(c.withGradient j seed zero).eval (arithInterpretationWith C constant) x (Fin.castAdd n k) = c.eval (arithInterpretationWith C constant) x k

Reverse-mode differentiation keeps the outputs of the original circuit.

theorem Cslib.Circuits.Circuit.sum_eval_withGradient_natAdd {C : Type u} {n m : ℕ} {R : Type u_1} [CommSemiring R] (constant : C → R) {M : Type u_2} [AddCommMonoid M] [Module R M] (D : R → M) (hadd : ∀ (a b : R), D (a + b) = D a + D b) (hmul : ∀ (a b : R), D (a * b) = a • D b + b • D a) (hconst : ∀ (c : C), D (constant c) = 0) (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) (hzero : constant zero = 0) (x : Fin n → R) :
∑ i : Fin n, (c.withGradient j seed zero).eval (arithInterpretationWith C constant) x (Fin.natAdd m i) • D (x i) = constant seed • D (c.eval (arithInterpretationWith C constant) x j)

Adjoints. For every additive map D obeying the Leibniz rule and vanishing on the constants, the new outputs of reverse-mode differentiation, weighted by D of the inputs, sum to seed times D of output j. For D a partial derivative this identifies the new outputs with the partial derivatives.

theorem Cslib.Circuits.Circuit.cost_program_withGradient {C : Type u} {n m : ℕ} (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) (cost : ArithOp C → ℕ) :

The exact weighted cost of reverse-mode differentiation: the forward circuit, the two constant gates, two additions per addition, and two multiplications and two additions per multiplication.

theorem Cslib.Circuits.Circuit.size_withGradient {C : Type u} {n m : ℕ} (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) :

The exact size of reverse-mode differentiation.

theorem Cslib.Circuits.Circuit.size_withGradient_le {C : Type u} {n m : ℕ} (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) :
(c.withGradient j seed zero).size ≤ 5 * c.size + 2

Reverse-mode differentiation multiplies the size by at most five, plus two constant gates.

theorem Cslib.Circuits.Circuit.mulCost_withGradient {C : Type u} {n m : ℕ} (c : Circuit (arithSignature C) n m) (j : Fin m) (seed zero : C) :

Reverse-mode differentiation exactly triples the number of multiplications.

The number of additions after reverse-mode differentiation.

theorem Cslib.Circuits.Circuit.polynomial_withGradient_castAdd {n m : ℕ} {K : Type u} [CommSemiring K] (c : Circuit (arithSignature K) n m) (j : Fin m) (seed zero : K) (k : Fin m) :
(c.withGradient j seed zero).polynomial (Fin.castAdd n k) = c.polynomial k

Reverse-mode differentiation keeps the polynomials of the original circuit.

theorem Cslib.Circuits.Circuit.polynomial_withGradient_natAdd {n m : ℕ} {K : Type u} [CommSemiring K] (c : Circuit (arithSignature K) n m) (j : Fin m) (seed : K) (i : Fin n) :

Correctness of reverse-mode differentiation. Output m + i computes seed times the partial derivative of output j along X i.

The Baur–Strassen theorem for arithmetic circuits [BS83]: if a circuit with s gates computes f (as output j), then a circuit with at most 5 s + 2 gates and three times the multiplications computes f and all its first partial derivatives.

theorem Cslib.Circuits.Circuit.le_five_mul_size_add_two_of_gradient {n m : ℕ} {K : Type u} [CommSemiring K] {f : MvPolynomial (Fin n) K} {S : ℕ} (hS : ∀ (d : Circuit (arithSignature K) n n), (∀ (i : Fin n), d.polynomial i = (MvPolynomial.pderiv i) f) → S ≤ d.size) (c : Circuit (arithSignature K) n m) (j : Fin m) (hc : c.polynomial j = f) :
S ≤ 5 * c.size + 2

Lower-bound transfer for size. If every circuit computing the gradient of f has at least S gates, then every circuit computing f has at least (S - 2) / 5 gates.

theorem Cslib.Circuits.Circuit.le_three_mul_mulCost_of_gradient {n m : ℕ} {K : Type u} [CommSemiring K] {f : MvPolynomial (Fin n) K} {S : ℕ} (hS : ∀ (d : Circuit (arithSignature K) n n), (∀ (i : Fin n), d.polynomial i = (MvPolynomial.pderiv i) f) → S ≤ d.program.cost ArithOp.mulCost) (c : Circuit (arithSignature K) n m) (j : Fin m) (hc : c.polynomial j = f) :

Lower-bound transfer for multiplications. If every circuit computing the gradient of f has at least S multiplications, then every circuit computing f has at least S / 3.

theorem Cslib.Circuits.Circuit.exists_pow_of_psum {n m : ℕ} {K : Type u} [CommSemiring K] {d : ℕ} {u : K} (hu : u * (↑d + 1) = 1) (c : Circuit (arithSignature K) n m) (j : Fin m) (hc : c.polynomial j = MvPolynomial.psum (Fin n) K (d + 1)) :

Power sums to power maps. If u * (d + 1) = 1 in K, a circuit with s gates computing the power sum ∑ i, X i ^ (d + 1) yields a circuit with at most 5 s + 2 gates and three times the multiplications computing the power map (X 0 ^ d, …, X (n - 1) ^ d).

theorem Cslib.Circuits.Circuit.le_three_mul_mulCost_of_psum {n m : ℕ} {K : Type u} [CommSemiring K] {d : ℕ} {u : K} (hu : u * (↑d + 1) = 1) {S : ℕ} (hS : ∀ (e : Circuit (arithSignature K) n n), (∀ (i : Fin n), e.polynomial i = MvPolynomial.X i ^ d) → S ≤ e.program.cost ArithOp.mulCost) (c : Circuit (arithSignature K) n m) (j : Fin m) (hc : c.polynomial j = MvPolynomial.psum (Fin n) K (d + 1)) :

Lower-bound transfer for power sums. If d + 1 is invertible in K and every circuit computing the power map (X 0 ^ d, …, X (n - 1) ^ d) has at least S multiplications, then every circuit computing the power sum ∑ i, X i ^ (d + 1) has at least S / 3. Strassen's degree bound [Str73a] provides such an S of order n log₂ d; it is not formalized here.