Algorithm Search⌘K

Proofs: Straight-line program

These are the human proofs of Straight-line program, at full length.

Correctness of evaluation, reverse-mode gradient, degree bound and zero test for straight-line programs full

This proof shows: correctness.

Setting. RR is a commutative ring and γ1,…,γL\gamma_1, \ldots, \gamma_L is a program in nn variables, as defined on the page. Its values v1,…,vL∈R[x1,…,xn]v_1, \ldots, v_L \in R[x_1, \ldots, x_n] are defined by vj=xiv_j = x_i, vj=cv_j = c or vj=va∘vbv_j = v_a \circ v_b, according to γj\gamma_j, and f=vLf = v_L (with f=0f = 0 if L=0L = 0). For ξ∈Rn\xi \in R^n let ϕξ:R[x1,…,xn]→R\phi_\xi : R[x_1, \ldots, x_n] \to R be evaluation at ξ\xi.

Theorem. (a) EVALUATE returns f(ξ)f(\xi), and with reduction modulo pp it returns f(ξ) mod pf(\xi) \bmod p. (b) GRADIENT returns (∂f/∂x1(ξ),…,∂f/∂xn(ξ))(\partial f / \partial x_1 (\xi), \ldots, \partial f / \partial x_n (\xi)), where ∂/∂xi\partial / \partial x_i is the formal derivative. (c) The propagated bound δj\delta_j satisfies deg⁡vj≤δj\deg v_j \le \delta_j, with δj=−1\delta_j = -1 only if vj=0v_j = 0. (d) If R=Z/pZR = \mathbb{Z}/p\mathbb{Z} and f≠0f \ne 0 has total degree at most DD, then ss independent uniform points all give 00 with probability at most (D/p)s(D / p)^s; if f=0f = 0, they all give 00.

Proof of (a)

By induction on jj, the stored value is ϕξ(vj)\phi_\xi(v_j). For a load this is the definition. For vj=va∘vbv_j = v_a \circ v_b with a,b<ja, b < j, the stored value is ϕξ(va)∘ϕξ(vb)=ϕξ(va∘vb)\phi_\xi(v_a) \circ \phi_\xi(v_b) = \phi_\xi(v_a \circ v_b), since ϕξ\phi_\xi is a ring homomorphism. The map R→R/pRR \to R / pR is also a ring homomorphism, so reducing after each step gives the image of ϕξ(vj)\phi_\xi(v_j), by the same induction. □\square

Proof of (b)

Introduce new indeterminates y1,…,yLy_1, \ldots, y_L. For j=L,L−1,…,0j = L, L - 1, \ldots, 0 define Fj∈R[x1,…,xn,y1,…,yj]F_j \in R[x_1, \ldots, x_n, y_1, \ldots, y_j] by FL=yLF_L = y_L and

Fj−1=Fj∣yj←ej,ej={xiif γj=xi,cif γj=c,ya∘ybif γj=va∘vb.F_{j-1} = F_j \big|_{y_j \leftarrow e_j}, \qquad e_j = \begin{cases} x_i & \text{if } \gamma_j = x_i, \\ c & \text{if } \gamma_j = c, \\ y_a \circ y_b & \text{if } \gamma_j = v_a \circ v_b. \end{cases}

Substituting the values for the yy's shows that Fj(x,v1,…,vj)=fF_j(x, v_1, \ldots, v_j) = f for every jj, and F0=fF_0 = f. Write ⋅ˉ\bar{\cdot} for evaluation at x=ξx = \xi, yk=vk(ξ)y_k = v_k(\xi).

Claim. When the loop of G4 is about to handle index jj, we have wk=∂Fj/∂yk‾w_k = \overline{\partial F_j / \partial y_k} for all k≤jk \le j, and gi=∂Fj/∂xi‾g_i = \overline{\partial F_j / \partial x_i} for all ii.

For j=Lj = L: FL=yLF_L = y_L, so ∂FL/∂yL=1\partial F_L / \partial y_L = 1 and all other derivatives are 00, which matches G2 and G3.

Step from jj to j−1j - 1. By the chain rule for formal derivatives (which holds over any commutative ring, because both sides are RR-linear derivations that agree on monomials),

∂Fj−1∂u=(∂Fj∂u+∂Fj∂yj⋅∂ej∂u)∣yj←ej\frac{\partial F_{j-1}}{\partial u} = \Bigl(\frac{\partial F_j}{\partial u} + \frac{\partial F_j}{\partial y_j} \cdot \frac{\partial e_j}{\partial u}\Bigr)\Big|_{y_j \leftarrow e_j}

for every indeterminate uu other than yjy_j. After evaluation, the substitution yj←ejy_j \leftarrow e_j has no effect, because eˉj=vj(ξ)\bar{e}_j = v_j(\xi) by (a). So the new value for uu is the old one plus wˉj⋅∂ej/∂u‾\bar{w}_j \cdot \overline{\partial e_j / \partial u}, where wjw_j is the current value at index jj. Now ∂ej/∂u\partial e_j / \partial u is: 11 for u=xiu = x_i if γj=xi\gamma_j = x_i (step G5); 11 for u=yau = y_a and ±1\pm 1 for u=ybu = y_b if γj=va±vb\gamma_j = v_a \pm v_b (step G6); yby_b for u=yau = y_a and yay_a for u=ybu = y_b if γj=va×vb\gamma_j = v_a \times v_b, with sum 2ya2 y_a if a=ba = b, and the code adds both terms (step G7); and 00 in all other cases and for constants. These are exactly the updates of the loop body, so the claim holds for j−1j - 1.

At the end (j=0j = 0), gi=∂F0/∂xi‾=∂f/∂xi(ξ)g_i = \overline{\partial F_0 / \partial x_i} = \partial f / \partial x_i (\xi). If L=0L = 0, then f=0f = 0 and G2 returns the zero vector, which is correct. □\square

Cost. The forward pass is LL operations. In the backward pass each instruction costs at most two multiplications and two additions, so the total is at most 5L5L operations.

Proof of (c)

By induction on jj. Loads: deg⁡xi=1\deg x_i = 1, deg⁡c=0\deg c = 0 for c≠0c \ne 0, and the bound −1-1 is used only for c=0c = 0. For va±vbv_a \pm v_b: deg⁡(va±vb)≤max⁡(deg⁡va,deg⁡vb)≤max⁡(δa,δb)\deg(v_a \pm v_b) \le \max(\deg v_a, \deg v_b) \le \max(\delta_a, \delta_b), and if the maximum is −1-1 then both are 00, so the sum is 00. For vavbv_a v_b: deg⁡(vavb)≤deg⁡va+deg⁡vb\deg(v_a v_b) \le \deg v_a + \deg v_b over any commutative ring (the product of the top parts may vanish, but no larger degree can appear), and the code sets −1-1 exactly when one factor has bound −1-1, in which case that factor is 00 and so is the product. □\square

Proof of (d)

If f=0f = 0 then f(ξ)=0f(\xi) = 0 for all ξ\xi, so by (a) every point gives 00. If f≠0f \ne 0 has total degree at most DD, the Schwartz-Zippel lemma gives Pr⁡[f(ξ)=0]≤D/p\Pr[f(\xi) = 0] \le D / p for a uniform ξ∈(Z/pZ)n\xi \in (\mathbb{Z}/p\mathbb{Z})^n. The points are independent, so all ss give 00 with probability at most (D/p)s(D / p)^s. By (c), the propagated bound δL\delta_L can serve as DD. ■\blacksquare

Back to Straight-line program