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.
- Correctness. Output
n + iisC seed * ∂(c j)/∂X i(Cslib.Circuits.Circuit.polynomial_withGradient_natAdd). The underlying evaluation theorem (Cslib.Circuits.Circuit.sum_eval_withGradient_natAdd) holds for every additive map satisfying the Leibniz rule and vanishing on the constants. - Cost. The size is exactly
s + 2 + 2 · #additions + 4 · #multiplications, so at most5 s + 2for a circuit withsgates; the number of multiplications is exactly three times that ofc. The latter is Baur and Strassen's factor three; it counts multiplications by constants, which their nonscalar measure does not. - Lower bounds. A lower bound
Sfor computing the gradient offgives the lower bound(S - 2) / 5on the size of any circuit computingf, andS / 3on its multiplications (Cslib.Circuits.Circuit.le_five_mul_size_add_two_of_gradient,Cslib.Circuits.Circuit.le_three_mul_mulCost_of_gradient).
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 #
Cslib.Circuits.Circuit.withGradient: the circuit with the gradient of one output appended.
Main results #
Cslib.Circuits.Circuit.eval_withGradient_castAdd,Cslib.Circuits.Circuit.polynomial_withGradient_castAdd: the original outputs are kept.Cslib.Circuits.Circuit.sum_eval_withGradient_natAdd: the new outputs are the adjoints.Cslib.Circuits.Circuit.polynomial_withGradient_natAdd: the new outputs are the partial derivatives.Cslib.Circuits.Circuit.cost_program_withGradient,Cslib.Circuits.Circuit.size_withGradient,Cslib.Circuits.Circuit.size_withGradient_le,Cslib.Circuits.Circuit.mulCost_withGradient,Cslib.Circuits.Circuit.addCost_withGradient: exact costs.Cslib.Circuits.Program.cost_addCost_add_cost_mulCost_le: additions and multiplications together are at most the number of gates.Cslib.Circuits.Circuit.exists_gradient: the Baur–Strassen theorem for arithmetic circuits.Cslib.Circuits.Circuit.le_five_mul_size_add_two_of_gradient,Cslib.Circuits.Circuit.le_three_mul_mulCost_of_gradient: lower-bound transfer.Cslib.Circuits.Circuit.exists_pow_of_psum,Cslib.Circuits.Circuit.le_three_mul_mulCost_of_psum: power sums and power maps.
References #
- W. Baur and V. Strassen, The complexity of partial derivatives
- J. Morgenstern, How to compute fast a function and all its derivatives
- V. Strassen, Die Berechnungskomplexität von elementarsymmetrischen Funktionen und von Interpolationskoeffizienten
- P. Bürgisser, M. Clausen, M. A. Shokrollahi, Algebraic Complexity Theory
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.
Reverse-mode differentiation keeps the outputs of the original circuit.
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.
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.
The exact size of reverse-mode differentiation.
Reverse-mode differentiation multiplies the size by at most five, plus two constant gates.
Reverse-mode differentiation exactly triples the number of multiplications.
The number of additions after reverse-mode differentiation.
Reverse-mode differentiation keeps the polynomials of the original circuit.
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.
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.
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.
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).
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.