Post · 8 min read· pp. 893–896

What Is Fast Matrix Multiplication?

The definition of matrix multiplication says that for n×nn\times n matrices AA and BB, the product C=ABC = AB is given by cij=∑k=1naikbkjc_{ij} = \sum_{k=1}^n a_{ik}b_{kj}. Each element of CC is an inner product of a row of AA and a column of BB, so if this formula is used then the cost of forming CC is n2(2n−1)n^2(2n-1) additions and multiplications, that is, O(n3)O(n^3) operations. For over a century after the development of matrix algebra in the 1850s by Cayley, Sylvester and others, all methods for matrix multiplication were based on this formula and required O(n3)O(n^3) operations.

In 1969 Volker Strassen showed that when n=2n=2 the product can be computed from the formulas

p1=(a11+a22)(b11+b22),p2=(a21+a22)b11,p3=a11(b12−b22),p4=a22(b21−b11),p5=(a11+a12)b22,p6=(a21−a11)(b11+b12),p7=(a12−a22)(b21+b22),C=[p1+p4−p5+p7p3+p5p2+p4p1+p3−p2+p6].\notag \begin{gathered} p_1 =(a_{11}+a_{22})(b_{11}+b_{22}), \\ p_2=(a_{21}+a_{22})b_{11}, \qquad p_3=a_{11}(b_{12}-b_{22}), \\ p_4=a_{22}(b_{21}-b_{11}), \qquad p_5=(a_{11}+a_{12})b_{22}, \\ p_6=(a_{21}-a_{11})(b_{11}+b_{12}), \qquad p_7=(a_{12}-a_{22})(b_{21}+b_{22}), \\[3pt] C = \begin{bmatrix} p_1+p_4-p_5+p_7 & p_3+p_5 \\ p_2+p_4 & p_1+p_3-p_2+p_6 \end{bmatrix}. \end{gathered}

The evaluation requires 77 multiplications and 1818 additions instead of 88 multiplications and 44 additions for the usual formulas.

At first sight, Strassen’s formulas may appear simply to be a curiosity. However, the formulas do not rely on commutativity so are valid when the aija_{ij} and bijb_{ij} are matrices, in which case for large dimensions the saving of one multiplication greatly outweighs the extra 1414 additions. Assuming nn is a power of 22, we can partition AA and BB into four blocks of size n/2n/2, apply Strassen’s formulas for the multiplication, and then apply the same formulas recursively on the half-sized matrix products.

Let us examine the number of multiplications for the recursive Strassen algorithm. Denote by M(k)M(k) the number of scalar multiplications required to multiply two 2k×2k2^k \times 2^k matrices. We have M(k)=7M(k−1)M(k) = 7M(k-1), so

M(k)=7M(k−1)=72M(k−2)=⋯=7kM(0)=7k.M(k) = 7M(k-1) = 7^2M(k-2) = \cdots = 7^k M(0) = 7^k.

But 7k=2log⁡27k=(2k)log⁡27=nlog⁡27=n2.807…7^k = 2^{\log_2{7^k}} = (2^k)^{\log_2 7} = n^{\log_2 7} = n^{2.807\dots}. The number of additions can be shown to be of the same order of magnitude, so the algorithm requires O(nlog⁡27)=O(n2.807…)O(n^{\log_27})=O(n^{2.807\dots}) operations.

Strassen’s work sparked interest in finding matrix multiplication algorithms of even lower complexity. Since there are O(n2)O(n^2) elements of data, which must each participate in at least one operation, the exponent of nn in the operation count must be at least 22.

The current record upper bound on the exponent is 2.372862.37286, proved by Alman and Vassilevska Williams (2021) which improved on the previous record of 2.372872.37287, proved by Le Gall (2014) The following figure plots the best upper bound for the exponent for matrix multiplication over time.

mmhist.jpg

In the methods that achieve exponents lower than 2.775, various intricate techniques are used, based on representing matrix multiplication in terms of bilinear or trilinear forms and their representation as tensors having low rank. Laderman, Pan, and Sha (1993) explain that for these methods “very large overhead constants are hidden in the `OO‘ notation”, and that the methods “improve on Strassen’s (and even the classical) algorithm only for immense numbers NN.”

Strassen’s method, when carefully implemented, can be faster than conventional matrix multiplication for reasonable dimensions. In practice, one does not recur down to 1×11\times 1 matrices, but rather uses conventional multiplication once n0×n0n_0\times n_0 matrices are reached, where the parameter n0n_0 is tuned for the best performance.

Strassen’s method has the drawback that it satisfies a weaker form of rounding error bound that conventional multiplication. For conventional multiplication C=ABC = AB of A∈Rm×nA\in\mathbb{R}^{m\times n} and B∈Rn×pB\in\mathbb{R}^{n\times p} we have the componentwise bound

∣C−C^∣≤γn∣A∣ ∣B∣,(1)\notag |C - \widehat{C}| \le \gamma^{}_n |A|\, |B|, \qquad(1)

where γn=nu/(1−nu)\gamma^{}_n = nu/(1-nu) and uu is the unit roundoff. For Strassen’s method we have only a normwise error bound. The following result uses the norm ∥A∥=max⁡i,j∣aij∣\|A\| = \max_{i,j} |a_{ij}|, which is not a consistent norm.

Theorem 1 (Brent). Let A,B∈Rn×nA,B \in \mathbb{R}^{n\times n}, where n=2kn=2^k. Suppose that C=ABC=AB is computed by Strassen’s method and that n0=2rn_0=2^r is the threshold at which conventional multiplication is used. The computed product C^\widehat{C} satisfies

∥C−C^∥≤[(nn0)log⁡212(n02+5n0)−5n]u∥A∥ ∥B∥+O(u2).(2)\notag \|C - \widehat{C}\| \le \left[ \Bigl( \displaystyle\frac{n}{n_0} \Bigr)^{\log_2{12}} (n_0^2+5n_0) - 5n \right] u \|A\|\, \|B\| + O(u^2). \qquad(2)

With full recursion (n0=1n_0=1) the constant in (2) is 6nlog⁡212−5n≈6n3.585−5n6 n^{\log_2 12}-5n \approx 6 n^{3.585}-5n, whereas with just one level of recursion (n0=n/2n_0 = n/2) it is 3n2+25n3n^2+25n. These compare with n2u+O(u2)n^2u + O(u^2) for conventional multiplication (obtained by taking norms in (1)). So the constant for Strassen’s method grows at a faster rate than that for conventional multiplication no matter what the choice of n0n_0.

The fact that Strassen’s method does not satisfy a componentwise error bound is a significant weakness of the method. Indeed Strassen’s method cannot even accurately multiply by the identity matrix. The product

C=[1001][1ϵϵϵ2],0<ϵ≪1\notag C = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & \epsilon \\ \epsilon & \epsilon^2 \end{bmatrix}, \quad 0 < \epsilon \ll 1

is evaluated exactly in floating-point arithmetic by conventional multiplication, but Strassen’s method computes

c22=2(1+ϵ2)+(ϵ−ϵ2)−1−(1+ϵ).c_{22} = 2(1+\epsilon^2) + (\epsilon-\epsilon^2) - 1 - (1+\epsilon).

Because c22c_{22} involves subterms of order unity, the error c22−c^22c_{22} - \widehat c_{22} will be of order uu. Thus the relative error ∣c22−c^22∣/∣c22∣=O(u/ϵ2)≫O(u)|c_{22} - \widehat c_{22}| / |c_{22}| = O(u/\epsilon^2) \gg O(u),

Another weakness of Strassen’s method is that while the scaling AB→(AD)(D−1B)AB \to (AD) (D^{-1}B), where DD is diagonal, leaves (1) unchanged, it can alter (2) by an arbitrary amount. Dumitrescu (1998) suggests computing D1(D1−1A⋅BD2−1)D2D_1(D_1^{-1}A\cdot B D_2^{-1})D_2, where the diagonal matrices D1D_1 and D2D_2 are chosen to equilibrate the rows of AA and the columns of BB in the ∞\infty-norm; he shows that this scaling can improve the accuracy of the result. Further investigations along these lines are made by Ballard et al. (2016).

Should one use Strassen’s method in practice, assuming that an implementation is available that is faster than conventional multiplication? Not if one needs a componentwise error bound, which ensures accurate products of matrices with nonnegative entries and ensures that the column scaling of AA and row scaling of BB has no effect on the error. But if a normwise error bound with a faster growing constant than for conventional multiplication is acceptable then the method is worth considering.

Notes

For recent work on high-performance implementation of Strassen’s method see Huang et al. (2016, 2020).

Theorem 1 is from an unpublished technical report of Brent (1970). A proof can be found in Higham (2002, §23.2.2).

For more on fast matrix multiplication see Bini (2014) and Higham (2002, Chapter 23).

References

This is a minimal set of references, which contain further useful references within.

This article is part of the “What Is” series, available from https://nhigham.com/category/what-is and in PDF form from the GitHub repository https://github.com/higham/what-is.