Algorithm Search⌘K

Proofs: Winograd's inner product algorithm

These are the human proofs of Winograd's inner product algorithm, at full length.

Winograd's inner product algorithm computes the matrix product full

This proof shows: correctness, termination.

Let RR be a commutative ring, A∈Rn×mA \in R^{n \times m}, B∈Rm×pB \in R^{m \times p} with n,m,p≥0n, m, p \ge 0, and h=⌊m/2⌋h = \lfloor m/2 \rfloor.

Claim. WINOGRAD-PRODUCT(A,B)(A, B) stops and returns C=ABC = AB.

Lemma. For all x1,x2,y1,y2∈Rx_1, x_2, y_1, y_2 \in R, (x1+y2)(x2+y1)=x1y1+x2y2+x1x2+y1y2(x_1 + y_2)(x_2 + y_1) = x_1 y_1 + x_2 y_2 + x_1 x_2 + y_1 y_2.

Proof. By distributivity, (x1+y2)(x2+y1)=x1x2+x1y1+y2x2+y2y1(x_1 + y_2)(x_2 + y_1) = x_1 x_2 + x_1 y_1 + y_2 x_2 + y_2 y_1. By commutativity, y2x2=x2y2y_2 x_2 = x_2 y_2 and y2y1=y1y2y_2 y_1 = y_1 y_2. □\square

Proof of the claim. Lines L2 and L3 set ξi=∑t<hA[i][2t]A[i][2t+1]\xi_i = \sum_{t<h} A[i][2t] A[i][2t+1] and ηj=∑t<hB[2t][j]B[2t+1][j]\eta_j = \sum_{t<h} B[2t][j] B[2t+1][j] (empty sums are 00).

Fix 0≤i<n0 \le i < n and 0≤j<p0 \le j < p. After line L5, s=−ξi−ηjs = -\xi_i - \eta_j. By induction on the loop of line L6, after the iteration with index tt,

s=−ξi−ηj+∑u≤t(A[i][2u]+B[2u+1][j])(A[i][2u+1]+B[2u][j]).s = -\xi_i - \eta_j + \sum_{u \le t} (A[i][2u] + B[2u+1][j])(A[i][2u+1] + B[2u][j]).

The base case before the first iteration is the empty sum, and each iteration adds the next term. After the loop, applying the lemma with x1=A[i][2u]x_1 = A[i][2u], x2=A[i][2u+1]x_2 = A[i][2u+1], y1=B[2u][j]y_1 = B[2u][j], y2=B[2u+1][j]y_2 = B[2u+1][j] to each term,

s=−ξi−ηj+∑u<h(A[i][2u]B[2u][j]+A[i][2u+1]B[2u+1][j])+∑u<hA[i][2u]A[i][2u+1]+∑u<hB[2u][j]B[2u+1][j].s = -\xi_i - \eta_j + \sum_{u<h} \big( A[i][2u] B[2u][j] + A[i][2u+1] B[2u+1][j] \big) + \sum_{u<h} A[i][2u] A[i][2u+1] + \sum_{u<h} B[2u][j] B[2u+1][j].

The last two sums are ξi\xi_i and ηj\eta_j, so s=∑k<2hA[i][k]B[k][j]s = \sum_{k < 2h} A[i][k] B[k][j]. If mm is even, 2h=m2h = m and this is C[i][j]C[i][j]. If mm is odd, 2h=m−12h = m - 1, and line L7 adds A[i][m−1]B[m−1][j]A[i][m-1] B[m-1][j], so again s=∑k<mA[i][k]B[k][j]=C[i][j]s = \sum_{k<m} A[i][k] B[k][j] = C[i][j]. Line L8 stores this value. The value of ss for one entry does not depend on any other entry, so every entry of CC is correct.

Termination. Every loop runs over a finite range fixed before it starts (nn, pp and hh iterations), and each iteration does a bounded number of ring operations. ■\blacksquare

Where commutativity is needed. Without it, the lemma fails: for 2×22 \times 2 matrices x2=(0100)x_2 = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix}, y2=(0010)y_2 = \begin{pmatrix} 0 & 0 \\ 1 & 0 \end{pmatrix} and x1=y1=0x_1 = y_1 = 0, the left side is y2x2=(0001)y_2 x_2 = \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix} while the right side is x2y2=(1000)x_2 y_2 = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}.

Back to Winograd's inner product algorithm