Algorithm Search⌘K

Proofs: Winograd Fourier transform algorithm

These are the human proofs of Winograd Fourier transform algorithm, at full length.

WFTA computes the DFT of length q_1 ⋯ q_d with ∏ m_i − ∏ u_i multiplications and Σ (a_i + c_i) ∏_{j<i} m_j ∏_{j>i} q_j additions full

This proof shows: correctness, complexity.

Setting. RR is C\mathbb{C} or Z/pZ\mathbb{Z}/p\mathbb{Z}, n=q1⋯qdn = q_1 \cdots q_d with pairwise coprime qi∈{2,3,4,5,7,8}q_i \in \{2, 3, 4, 5, 7, 8\}, and ω∈R\omega \in R has order exactly nn. Put ωq=ωn/q\omega_q = \omega^{n/q}, of order exactly qq, and Fq=(ωqjk)0≤k,j<qF_q = (\omega_q^{jk})_{0 \le k, j < q}. In Z/pZ\mathbb{Z}/p\mathbb{Z} an element of order qq exists only if q∣p−1q \mid p - 1; so 22 is invertible when some qi∈{3,5,7,8}q_i \in \{3, 5, 7, 8\} (then p≥7p \ge 7), and 33 is invertible when 7∣n7 \mid n (then p≥29p \ge 29). These are the only divisions in the constants. For a root ww of order qq write C(e)=(we+w−e)/2C(e) = (w^e + w^{-e})/2 and S(e)=(we−w−e)/2S(e) = (w^e - w^{-e})/2.

Statement. WFTA returns Xk=∑j<nxjωjkX_k = \sum_{j<n} x_j \omega^{jk} for all 0≤k<n0 \le k < n. It does ∏imqi−∏iuqi\prod_i m_{q_i} - \prod_i u_{q_i} multiplications and ∑i=1d(aqi+cqi)∏j<imqj∏j>iqj\sum_{i=1}^d (a_{q_i} + c_{q_i}) \prod_{j<i} m_{q_j} \prod_{j>i} q_j additions and subtractions, with (m,a,c,u)(m, a, c, u) equal to (2,2,0,2)(2, 2, 0, 2), (3,3,3,1)(3, 3, 3, 1), (4,6,2,3)(4, 6, 2, 3), (6,8,9,1)(6, 8, 9, 1), (9,17,19,1)(9, 17, 19, 1) and (8,16,10,4)(8, 16, 10, 4) for q=2,3,4,5,7,8q = 2, 3, 4, 5, 7, 8.

Lemma 1 (index maps). The map (j1,…,jd)↦j=∑ijin/qi mod n(j_1, \ldots, j_d) \mapsto j = \sum_i j_i n/q_i \bmod n is a bijection from ∏i[0,qi)\prod_i [0, q_i) to [0,n)[0, n), and k↦(k mod q1,…,k mod qd)k \mapsto (k \bmod q_1, \ldots, k \bmod q_d) is a bijection from [0,n)[0, n) to ∏i[0,qi)\prod_i [0, q_i). With a[j1,…,jd]=xja[j_1, \ldots, j_d] = x_j and ki=k mod qik_i = k \bmod q_i,

Xk=∑j1,…,jda[j1,…,jd]∏i=1dωqijiki.X_k = \sum_{j_1, \ldots, j_d} a[j_1, \ldots, j_d] \prod_{i=1}^d \omega_{q_i}^{j_i k_i}.

Proof. For l≠il \ne i, qiq_i divides n/qln/q_l, so j≡ji n/qi(modqi)j \equiv j_i\, n/q_i \pmod{q_i}; since n/qin/q_i is coprime to qiq_i, jj determines each jij_i. The map is injective between sets of size nn, hence bijective. The second map is bijective by the Chinese remainder theorem. Finally ωjk=∏iω(n/qi)jik=∏iωqijik\omega^{jk} = \prod_i \omega^{(n/q_i) j_i k} = \prod_i \omega_{q_i}^{j_i k}, because ωn=1\omega^n = 1, and ωqijik=ωqijiki\omega_{q_i}^{j_i k} = \omega_{q_i}^{j_i k_i} because ωqiqi=1\omega_{q_i}^{q_i} = 1. Summing over jj, that is over all tuples, gives the formula. □\square

Lemma 2 (pairing). Let qq be an odd prime, ww of order qq, tj±=xj±xq−jt^\pm_j = x_j \pm x_{q-j} for 1≤j≤(q−1)/21 \le j \le (q - 1)/2. Then for 1≤k<q1 \le k < q, Xk=x0+∑j(C(jk) tj++S(jk) tj−)X_k = x_0 + \sum_j \bigl(C(jk)\, t^+_j + S(jk)\, t^-_j\bigr), and X0=x0+∑jtj+X_0 = x_0 + \sum_j t^+_j. Moreover CC is even and SS odd in ee, and both depend on ee modulo qq.

Proof. xjwjk+xq−jw−jk=C(jk)(xj+xq−j)+S(jk)(xj−xq−j)x_j w^{jk} + x_{q-j} w^{-jk} = C(jk)(x_j + x_{q-j}) + S(jk)(x_j - x_{q-j}), since C+S=wjkC + S = w^{jk} and C−S=w−jkC - S = w^{-jk}. Summing over the pairs {j,q−j}\{j, q - j\}, which partition [1,q)[1, q), gives the claim. □\square

Lemma 3 (cyclic convolution of length 3). For h,v∈R3h, v \in R^3, let yb=∑a<3h(b−a) mod 3vay_b = \sum_{a<3} h_{(b - a) \bmod 3} v_a, H=h0+h1+h2H = h_0 + h_1 + h_2, ge=he−H/3g_e = h_e - H/3, s=v0+v1+v2s = v_0 + v_1 + v_2, d0=v0−v1d_0 = v_0 - v_1, d1=v1−v2d_1 = v_1 - v_2, e=v0−v2e = v_0 - v_2, P1=g0d0P_1 = g_0 d_0, P2=g1d1P_2 = g_1 d_1, P3=(g0+g1)eP_3 = (g_0 + g_1) e. Then y=H3s (1,1,1)+(P1−P2, P3−P1, P2−P3)y = \frac H3 s\,(1, 1, 1) + (P_1 - P_2,\ P_3 - P_1,\ P_2 - P_3).

Proof. Convolution is linear in the kernel, and the constant kernel H3(1,1,1)\frac H3 (1, 1, 1) gives H3s\frac H3 s in every position. For gg, g2=−g0−g1g_2 = -g_0 - g_1. Then g0v0+g2v1+g1v2=g0(v0−v1)−g1(v1−v2)=P1−P2g_0 v_0 + g_2 v_1 + g_1 v_2 = g_0 (v_0 - v_1) - g_1 (v_1 - v_2) = P_1 - P_2; g1v0+g0v1+g2v2=g1(v0−v2)+g0(v1−v2)=(g0+g1)e−g0d0=P3−P1g_1 v_0 + g_0 v_1 + g_2 v_2 = g_1 (v_0 - v_2) + g_0 (v_1 - v_2) = (g_0 + g_1) e - g_0 d_0 = P_3 - P_1, using e=d0+d1e = d_0 + d_1; and the third entry is minus the sum of the first two, because ∑b∑agb−ava=(∑g)s=0\sum_b \sum_a g_{b-a} v_a = (\sum g) s = 0, which is P2−P3P_2 - P_3. □\square

Lemma 4 (modules). For each qq in the list and each ww of order qq, the module of the page (the code of _pre, _post and _mu) computes FqxF_q x with root ww.

Proof. q=2q = 2: w=−1w = -1, and the module returns (x0+x1,x0−x1)(x_0 + x_1, x_0 - x_1).

q=4q = 4: w2=−1w^2 = -1 (an element of order 22 in a field), w3=−ww^3 = -w. Then X0=(x0+x2)+(x1+x3)X_0 = (x_0 + x_2) + (x_1 + x_3), X2=(x0+x2)−(x1+x3)X_2 = (x_0 + x_2) - (x_1 + x_3), X1=(x0−x2)+w(x1−x3)X_1 = (x_0 - x_2) + w (x_1 - x_3) and X3=(x0−x2)−w(x1−x3)X_3 = (x_0 - x_2) - w(x_1 - x_3), which are the four outputs.

q=3q = 3: by Lemma 2 with t±=x1±x2t^\pm = x_1 \pm x_2, X1=x0+C(1)t++S(1)t−X_1 = x_0 + C(1) t^+ + S(1) t^- and X2=x0+C(1)t+−S(1)t−X_2 = x_0 + C(1) t^+ - S(1) t^-, using C(2)=C(−1)=C(1)C(2) = C(-1) = C(1) and S(2)=−S(1)S(2) = -S(1). The module has m0=x0+t+=X0m_0 = x_0 + t^+ = X_0, m1=(C(1)−1)t+m_1 = (C(1) - 1) t^+, m2=S(1)t−m_2 = S(1) t^-, and m0+m1=x0+C(1)t+m_0 + m_1 = x_0 + C(1) t^+, so its outputs m0m_0, m0+m1±m2m_0 + m_1 \pm m_2 are right.

q=5q = 5: with t0,t1=x1+x4,x2+x3t_0, t_1 = x_1 + x_4, x_2 + x_3, u0,u1=x1−x4,x2−x3u_0, u_1 = x_1 - x_4, x_2 - x_3, and Ce=C(e)C_e = C(e), Se=S(e)S_e = S(e), Lemma 2 and C4=C1C_4 = C_1, S4=−S1S_4 = -S_1 give X1=x0+C1t0+C2t1+S1u0+S2u1X_1 = x_0 + C_1 t_0 + C_2 t_1 + S_1 u_0 + S_2 u_1 and X2=x0+C2t0+C1t1+S2u0−S1u1X_2 = x_0 + C_2 t_0 + C_1 t_1 + S_2 u_0 - S_1 u_1, and X4X_4, X3X_3 are the same with the signs of the SS terms reversed. The module computes s=t0+t1s = t_0 + t_1, m0=x0+s=X0m_0 = x_0 + s = X_0, m1=(C1+C22−1)sm_1 = (\frac{C_1 + C_2}2 - 1) s, m2=C1−C22(t0−t1)m_2 = \frac{C_1 - C_2}2 (t_0 - t_1), m3=S2(u0+u1)m_3 = S_2 (u_0 + u_1), m4=(S1−S2)u0m_4 = (S_1 - S_2) u_0, m5=−(S1+S2)u1m_5 = -(S_1 + S_2) u_1. Then m0+m1±m2m_0 + m_1 \pm m_2 is x0+C1t0+C2t1x_0 + C_1 t_0 + C_2 t_1 or x0+C2t0+C1t1x_0 + C_2 t_0 + C_1 t_1; m3+m4=S1u0+S2u1m_3 + m_4 = S_1 u_0 + S_2 u_1 and m3+m5=S2u0−S1u1m_3 + m_5 = S_2 u_0 - S_1 u_1. The outputs m0m_0, r0+i0r_0 + i_0, r1+i1r_1 + i_1, r1−i1r_1 - i_1, r0−i0r_0 - i_0 are X0,…,X4X_0, \ldots, X_4.

q=7q = 7: let pj,uj=xj±x7−jp_j, u_j = x_j \pm x_{7-j} for j=1,2,3j = 1, 2, 3. By Lemma 2, with CeC_e and SeS_e taken modulo 77, the real parts Rk=x0+Ckp1+C2kp2+C3kp3R_k = x_0 + C_k p_1 + C_{2k} p_2 + C_{3k} p_3 are

R1=x0+C1p1+C2p2+C3p3,R3=x0+C3p1+C1p2+C2p3,R2=x0+C2p1+C3p2+C1p3,R_1 = x_0 + C_1 p_1 + C_2 p_2 + C_3 p_3, \quad R_3 = x_0 + C_3 p_1 + C_1 p_2 + C_2 p_3, \quad R_2 = x_0 + C_2 p_1 + C_3 p_2 + C_1 p_3,

and the imaginary parts Ik=Sku1+S2ku2+S3ku3I_k = S_k u_1 + S_{2k} u_2 + S_{3k} u_3 are I1=S1u1+S2u2+S3u3I_1 = S_1 u_1 + S_2 u_2 + S_3 u_3, I3=S3u1−S1u2+S2u3I_3 = S_3 u_1 - S_1 u_2 + S_2 u_3 and I2=S2u1−S3u2−S1u3I_2 = S_2 u_1 - S_3 u_2 - S_1 u_3, with Xk=Rk+IkX_k = R_k + I_k and X7−k=Rk−IkX_{7-k} = R_k - I_k. The convolution yy of Lemma 3 with kernel (C1,C3,C2)(C_1, C_3, C_2) and input (p1,p2,p3)(p_1, p_2, p_3) is (R1,R3,R2)−x0(R_1, R_3, R_2) - x_0, by expanding yb=∑ahb−avay_b = \sum_a h_{b-a} v_a. The convolution with kernel (S1,−S3,S2)(S_1, -S_3, S_2) and input (u1,u2,−u3)(u_1, u_2, -u_3) is (I1,−I3,I2)(I_1, -I_3, I_2), by the same expansion. The module's inputs are those of Lemma 3 for both: s,d0,d1,es, d_0, d_1, e from (p1,p2,p3)(p_1, p_2, p_3), and u1+u2−u3u_1 + u_2 - u_3, u1−u2u_1 - u_2, u2+u3u_2 + u_3, u1+u3u_1 + u_3 from (u1,u2,−u3)(u_1, u_2, -u_3). Its constants are 11 for m0=x0+s=X0m_0 = x_0 + s = X_0, H/3−1H/3 - 1 for ss, so that m0+m1=x0+H3sm_0 + m_1 = x_0 + \frac H3 s, then g0,g1,g0+g1g_0, g_1, g_0 + g_1, then T/3T/3 and the three values of the second kernel. So rb=m0+m1+wb=x0+ybr_b = m_0 + m_1 + w_b = x_0 + y_b gives R1,R3,R2R_1, R_3, R_2, and ubu_b gives I1,−I3,I2I_1, -I_3, I_2. The outputs m0m_0, r0+u0r_0 + u_0, r2+u2r_2 + u_2, r1−u1r_1 - u_1, r1+u1r_1 + u_1, r2−u2r_2 - u_2, r0−u0r_0 - u_0 are X0,X1,X2,X3,X4,X5,X6X_0, X_1, X_2, X_3, X_4, X_5, X_6.

q=8q = 8: let J=w2J = w^2, of order 44, so J2=−1J^2 = -1 and 1+J≠01 + J \ne 0; put c=w/(1+J)c = w/(1 + J). Write sj=xj+xj+4s_j = x_j + x_{j+4} and dj=xj−xj+4d_j = x_j - x_{j+4} for j<4j < 4. Since w4=−1w^4 = -1, X2k′=∑j<4sjJjk′X_{2k'} = \sum_{j<4} s_j J^{jk'}, the DFT of length 44 of ss with root JJ, which the first four products and the q=4q = 4 formulas compute. And X2k′+1=∑j<4zjJjk′X_{2k'+1} = \sum_{j<4} z_j J^{jk'} with zj=wjdjz_j = w^j d_j. Now w=c(1+J)w = c(1 + J) and w3=wJ=c(J−1)w^3 = wJ = c(J - 1), so

z1+z3=c(d1−d3)+cJ(d1+d3),J(z1−z3)=cJ(d1+d3)−c(d1−d3),z_1 + z_3 = c(d_1 - d_3) + cJ(d_1 + d_3), \qquad J(z_1 - z_3) = cJ(d_1 + d_3) - c(d_1 - d_3),

using J2=−1J^2 = -1. With a,b=d0±Jd2a, b = d_0 \pm J d_2, P=m6+m7P = m_6 + m_7 and Q=m7−m6Q = m_7 - m_6, where m6=c(d1−d3)m_6 = c(d_1 - d_3) and m7=cJ(d1+d3)m_7 = cJ(d_1 + d_3), the DFT of length 44 of zz is (a+P,b+Q,a−P,b−Q)(a + P, b + Q, a - P, b - Q), which are X1,X3,X5,X7X_1, X_3, X_5, X_7. □\square

Lemma 5 (axes and Kronecker products). Store an array of shape (s1,…,sd)(s_1, \ldots, s_d) in row-major order. Applying a linear map L:Rsi→RrL: R^{s_i} \to R^{r} to every line along axis ii is multiplication by Is1⋯si−1⊗L⊗Isi+1⋯sdI_{s_1 \cdots s_{i-1}} \otimes L \otimes I_{s_{i+1} \cdots s_d}, and multiplying entry (t1,…,td)(t_1, \ldots, t_d) by ∏iμi[ti]\prod_i \mu_i[t_i] is multiplication by diag(μ1)⊗⋯⊗diag(μd)\mathrm{diag}(\mu_1) \otimes \cdots \otimes \mathrm{diag}(\mu_d).

Proof. In row-major order the entry (t1,…,td)(t_1, \ldots, t_d) has position ∑iti∏l>isl\sum_i t_i \prod_{l > i} s_l, which is the indexing of the Kronecker product: the entry of P⊗QP \otimes Q in row (r,r′)(r, r') and column (c,c′)(c, c') is PrcQr′c′P_{rc} Q_{r'c'}. Applying LL along axis ii changes tit_i by LL and keeps the other coordinates, which is the stated product, and the diagonal case is the same statement for the entries. □\square

Correctness. By Lemma 5, line L3 multiplies vec(a)\mathrm{vec}(a) by ∏i=d1(Im1⋯mi−1⊗Ai⊗Iqi+1⋯qd)\prod_{i=d}^{1} (I_{m_1 \cdots m_{i-1}} \otimes A_i \otimes I_{q_{i+1} \cdots q_d}), which by the mixed-product rule (P⊗Q)(P′⊗Q′)=PP′⊗QQ′(P \otimes Q)(P' \otimes Q') = PP' \otimes QQ' equals A1⊗⋯⊗AdA_1 \otimes \cdots \otimes A_d. Line L4 multiplies by ⨂idiag(μi)\bigotimes_i \mathrm{diag}(\mu_i); skipping the products whose factors are all 11 does not change any value. Line L5 multiplies by C1⊗⋯⊗CdC_1 \otimes \cdots \otimes C_d in the same way, in the reverse order. By the mixed-product rule again, the whole is ⨂iCidiag(μi)Ai=⨂iFqi\bigotimes_i C_i \mathrm{diag}(\mu_i) A_i = \bigotimes_i F_{q_i} by Lemma 4 with w=ωqiw = \omega_{q_i}. The entry of (⨂iFqi)vec(a)(\bigotimes_i F_{q_i}) \mathrm{vec}(a) at (k1,…,kd)(k_1, \ldots, k_d) is ∑a[j1,…,jd]∏iωqijiki\sum a[j_1, \ldots, j_d] \prod_i \omega_{q_i}^{j_i k_i}, which is XkX_k by Lemma 1, and line L6 reads it at ki=k mod qik_i = k \bmod q_i. The inverse transform is the same computation with ω−1\omega^{-1}, which has order nn, followed by division by nn.

Operation count. A module application costs aqa_q additions in AqA_q and cqc_q in CqC_q, read off from the straight-line code; the table of the page lists them. Applying AqiA_{q_i} along axis ii costs aqia_{q_i} times the number of lines, the product of the other axis lengths. When line L3 treats axis ii, the axes j<ij < i already have length mqjm_{q_j} and the axes j>ij > i still have length qjq_j. When line L5 treats axis ii (in decreasing order), the axes j>ij > i have been shrunk back to qjq_j and the axes j<ij < i still have length mqjm_{q_j}. So both passes over axis ii cost (aqi+cqi)∏j<imqj∏j>iqj(a_{q_i} + c_{q_i}) \prod_{j<i} m_{q_j} \prod_{j>i} q_j, which sums to the stated count. Line L4 does one multiplication for each of the ∏imqi\prod_i m_{q_i} tuples except the ∏iuqi\prod_i u_{q_i} whose constants are all 11. Lines L1, L2 and L6 do no arithmetic on the data; the constants and their products depend only on nn. The index maps and the passes over the array cost O(d∏imqi)O(d \prod_i m_{q_i}) steps, and the array has at most ∏imqi\prod_i m_{q_i} entries.

Real and imaginary constants. Over C\mathbb{C} with w=e−2πi/qw = e^{-2\pi i/q}, w−e=we‾w^{-e} = \overline{w^e}, so every C(e)C(e) is real and every S(e)S(e) purely imaginary; so are HH (a sum of CC values), TT (a sum of SS values), the gg and hh values, and the constants 11. For q=8q = 8, J=−iJ = -i, 1+J=1−i1 + J = 1 - i and w=(1−i)/2w = (1 - i)/\sqrt 2, so c=1/2c = 1/\sqrt 2 is real and cJcJ imaginary. Hence every product ∏iμi[ti]\prod_i \mu_i[t_i] is real or purely imaginary, and multiplying a complex number by it costs 22 real multiplications. ■\blacksquare

Back to Winograd Fourier transform algorithm