The 2M Multiplication Algorithm for Complex Matrices
Abstract
Complex matrix multiplication is typically computed using 4 real matrix multiplications (GEMMs) of the same size. The well-known 3M multiplication algorithm reduces this cost to 3 real GEMMs, together with quadratic time pre- and post-processing steps.
In this paper, we reduce 3M to 2M for matrices with integer real and imaginary parts, performing complex GEMM with only 2 real GEMMs of the same size, along with quadratic time pre- and post-processing. For floating-point matrices, 2M multiplication combines naturally with the Ozaki-II scheme, yielding a practical, high-performance algorithm for computing a complex floating-point GEMM in roughly twice the time of a real GEMM of the same size. As corollaries, we derive new algorithms for symmetric rank- updates (SYRK/HERK) that internally use full rectangular GEMMs.
1 Introduction
The classical approach to multiply complex matrices and is to expand the product into 4 real matrix multiplications:
Let us call this the 4M algorithm, following [BLISComplex], since it involves 4 real matrix multiplications. 4M multiplication typically forms the backbone of complex floating-point GEMMs in high-performance libraries.
The well-known 3M algorithm reduces this cost to 3 real matrix multiplications, by first computing and :
| (1) |
Note that while (1) is mathematically exact, in floating-point arithmetic the additions , can reduce numerical stability [Higham3M]. Hence, numerical libraries typically offer 3M GEMMs as an opt-in feature.
For scalar complex products, it can be proven that 3 real multiplications is indeed minimal [Winograd2x2]. In this paper, we will show that we can do better for matrix products.
2 2M Complex Multiplication
To start, we consider input matrices , with integral real and imaginary parts, and write . Suppose is an integer chosen large enough that for each entry of . Then, if we know , we can evidently recover .
Now suppose that is odd and there is an integer such that , in effect a “real surrogate” for . It can be shown that such an exists iff is a product of primes congruent to 1 mod 4. We call such an a 2M modulus.
Perform the 3M-like preprocessing step
| (2a) | ||||||
| Next, perform the 2 real multiplications | ||||||
| (2b) | ||||||
| Then it is not hard to see that | ||||||
| (2c) | ||||||
Here is the inverse of 2 modulo , explaining why must be odd. Together, equations (2a–2c) form the 2M multiplication algorithm. As with 3M, the new algorithm requires scalar operations in addition to the matrix multiplications.
2.1 Algebraic Underpinnings
To understand where 2M multiplication comes from, we borrow some basic ideas from algebraic number theory. Algebraically, , , are matrices over the ring of Gaussian integers . Gaussian integers have a rich algebraic structure. In particular, they form a Euclidean domain, meaning that we have well-defined notion of division with remainder; modular arithmetic and the Chinese remainder theorem (CRT) also extend to .
Suppose now that with , and let . Factoring , the CRT allows us to reconstruct from , . We can write
The crucial step is to convert these complex multiplications to real equivalents. Algebraically speaking, the mapping induces an isomorphism of quotient rings .111Indeed, we have a trivial homomorphism , well-defined since , and an inverse map induced from by linearity, also well-defined since . In other words, working in modulo is equivalent to working in modulo .
3 2M Multiplication In Floating Point
3.1 The Ozaki-II Scheme
To apply 2M to non-integer GEMMs, we employ the Ozaki-II scheme [Ozaki2], a family of algorithms for computing floating-point matrix multiplication (GEMM) to arbitrary accuracy. Ozaki-II works by quantizing its inputs to integers, then performing fast integer matrix multiplication with modular arithmetic. By leveraging the dedicated narrow-precision matrix multipliation units available on modern GPUs, Ozaki-II can often significantly outperform standard floating-point GEMM while maintaining the same level of accuracy. Moreover, the level of accuracy may be tuned: either reduced, for additional performance; or increased, to provide additional accuracy beyond that available with a standard floating-point implementation.
We briefly restate the method here. Given real matrices , , we choose per-row and per-column scaling factors , for and , respectively, and quantize to integer matrices
Here, represents rounding to a nearest element of a set . Next, we choose a collection of coprime moduli whose product is large enough that each element of the integer product has absolute value at most . Then can be uniquely recovered from its residue modulo , and we do so by computing the products
| (3) |
and applying the Chinese remainder theorem (CRT) elementwise:
Then . The accuracy of the approximation is determined primarily by , with relative elementwise error roughly . Achieving full accuracy also requires a proper choice of scaling factors , ; for more detail, see [O2Error, ESC].
Ozaki-II derives its speed from computing the products (3) using dedicated narrow-precision matrix multiplication hardware, which guides the choice of the moduli (e.g. for 8-bit integer GEMMs).
3.2 Complex Ozaki-II With 2M
Ozaki-II extends readily to complex-valued inputs , . In Uchino et al.’s algorithm [Uchino], they first choose (real) scaling factors , and quantize , , , to integer matrices as above. The resulting integer matrices are reduced modulo each , multiplied, and then the real and imaginary parts of are reconstructed separately using CRT. Scaling factors may be chosen using straightforward extensions of any of the existing real algorithms [Ozaki2, ESC].
For better performance, Uchino et al. apply 3M to each small integer multiplication, after modular reduction; since 3M is an algebraic identity, it is valid modulo any .222It is also possible to apply 3M prior to modular reduction, but this has the disadvantage of requiring high-precision integer arithmetic in 3M’s pre- and post-proessing steps. By substituting 2M multiplication for 3M, we immediately derive a 2M Ozaki-II algorithm for complex floating-point GEMMs (Algorithm 1).
Note also that we can readily hybridize the 2M and 3M Ozaki-II methods, using 2M multiplication for moduli that satisfy the 2M conditions (odd, with a square root of ), and 3M multiplication for the remaining moduli. For a practical implementation, such a hybrid approach allows reaching higher precision in cases where insufficient 2M moduli are available.
3.3 Other 2M Ozaki-II Variants
Algebraically, we may interpret Uchino et al.’s algorithm as the usual Ozaki-II scheme operating over the Gaussian integers, with real integer moduli . 2M Ozaki-II works similarly, but with conjugate pairs of complex integer moduli.
We note in passing that 2M multiplication and Algorithm 1 have analogues where we replace by another imaginary quadratic number ring. A natural choice is the Eisenstein integers , which form a hexagonal grid in the complex plane. By varying the number ring, we vary the set of valid 2M moduli. Asymptotically speaking, half of all primes are 2M moduli w.r.t. any given imaginary quadratic number ring by quadratic reciprocity and Dirichlet’s theorem. In practice, however, since moduli are generally bounded by the input range of hardware matrix multiplication units, specific rings may perform slightly better than others, as we will see in the next section.
4 ZGEMM with 8-bit Integer GEMMs
As a concrete example of Algorithm 1, let us consider complex double-precision GEMM (ZGEMM) with 8-bit integer GEMMs underneath. We enumerate a set of coprime 2M moduli in the range , chosen using a greedy algorithm:
Recall that the accuracy of an Ozaki-II method is determined primarily by , the product of the chosen moduli. The first moduli of the above sequence, with product , is sufficient in many cases to achieve accuracy comparable to a standard ZGEMM implementation in double-precision arithmetic.
Note that by comparison Ozaki-II real GEMMs, which are not restricted to 2M moduli, have access to a larger set of moduli
Because these moduli decrease less rapidly than 2M moduli, we require only 15 moduli to achieve the same level of accuracy .
The relative sparsity of w.r.t. reflects the fact that asymptotically only half of the prime numbers are valid 2M moduli, as observed in Section 3.3. Furthermore, the product of all moduli in is , much lower than for real Ozaki-II (). If additional precision is required, e.g. due to a large exponent span in the input matrices [ESC], we may potentially exhaust available 2M moduli. In this case, additional non-2M moduli may be added to the set of moduli using the hybrid 2M/3M algorithm described in Section 3.2.
As mentioned previously, the set of 2M moduli may be varied by discretizing to different imaginary quadratic number rings. Indeed, for 8-bit integer moduli, the Eisenstein integers have a slight advantage over Gaussian integers: we can achieve with 2M moduli. However, this advantage is offset by slightly more complicated quantization/dequantization steps; in particular, quantization involves rounding each point in the complex plane to the nearest point on a hexagonal lattice.
5 2M Rank- Updates333The author thanks Robert Parrish for pointing out this method of reducing SYRK to 2M GEMM.
The 2M method also yields computationally efficient methods for the real and complex BLAS-3 rank- and rank- update routines (SYRK, HERK, SYR2K, HER2K). We recall these routines’ definitions, omitting alpha and beta scaling for simplicity:
-
•
SYRK: , .
-
•
HERK: , .
-
•
SYR2K: , .
-
•
HER2K: , .
In each case is (Hermitian) symmetric, so only the lower or upper triangle of is computed and stored. In theory, the symmetry of reduces the operation count in half (e.g. flops for SYRK vs. for a same-sized GEMM), but library implementations generally cannot obtain the full speedup. The reason is that SYRK-like routines are typically implemented with a modified GEMM kernel that masks its output to the lower or upper triangle of the output matrix [GotoBLAS3]. Each kernel operates on a fixed-size tile of , leading to wasted computation near the diagonal, where tiles lie partly outside the desired triangle.
With 2M, it is possible to implement symmetric products using full, unmasked GEMMs, while still maintaining the expected 2x speedup. The key idea is that Hermitian symmetry in the problem induces a symmetry relation in the real integer products , allowing us to compute only half as many integer GEMMs. We outline the methods below, which may be applied in either the integer domain (modulo ) or in floating point, in conjuction with Ozaki-II.
HERK:
Writing and applying 2M, we have
In particular , so we only need to compute a single real multiplication for each , providing the expected 2x speedup while still computing full GEMMs.
HER2K:
Apply 2M to each multiplication. If we use the same scaling factors for and (i.e. ), we can bring the matrix addition into the residue domains:
Again , reducing four real multiplications (per modulus) to two.
SYRK, SYR2K:
We convert (real) SYRK to an HERK. Assuming for simplicity is even, split evenly in the k dimension: and form . Then
Hence , which we compute using 2M HERK. The same method converts SYR2K to a HER2K problem of half the dimension.
6 Computational Results
To demonstrate the efficacy of the 2M method, we present in Figure 1 performance and accuracy results for int8-emulated 2M ZGEMM on a single NVIDIA B200 GPU. All results are with , , and use the Gaussian integer version of 2M.
Figure 1(a) confirms that 2M ZGEMM reaches the expected accuracy results for Ozaki-II. We measure accuracy as the maximum relative error over each entry of , on a test case with , while varying the number of moduli . Input matrices are chosen using Uchino et al.’s data distribution [Ozaki2] with . Overall, convergence matches the overall trend of real Ozaki-II DGEMM. Note that the choice of complex multiplication method (2M, 3M, or 4M) does not affect accuracy as multiplications are always performed in exact integer arithmetic, and for this reason we do not undertake a detailed accuracy study here, instead referring the reader to [O2Error, ESC, Uchino, Ozaki2].
Figure 1(b) shows 2M ZGEMM performance across a range of square problem sizes, using moduli. Performance is measured in effective teraflops per second, calculated using the floating point operation count from the baseline algorithm: operations for ZGEMM, for DGEMM. The modulus count is chosen to achieve accuracy similar to native-floating point ZGEMM; in fact, Ozaki-II 2M ZGEMM is approximately 10 more accurate than native floating-point ZGEMM for this input data (cf. Figure 1(a)).
With , 2M ZGEMM is up to faster than reference 4M fp64 ZGEMM (cuBLAS 13.1), and 1.4–1.5 faster than Uchino et al.’s 3M-based GEMMul8 library.555Tested version: https://github.com/RIKEN-RCCS/GEMMul8/commit/3115e709 2M also achieves an average speedup in effective TF/s compared to Ozaki-II DGEMM at the same accuracy. That is, in absolute time a single ZGEMM costs approximately DGEMMs, close to theoretical expectations. The cost is not exactly 2 DGEMMs primarily because 2M requires slightly more moduli than the other Ozaki-II methods (16 vs. 15) to achieve a similar accuracy level. Again, this is due to the restricted set of moduli available for 2M.
In Figure 2(a) we fix the problem size to and vary to show the tradeoff between performance and accuracy. 2M shows similar speedups over GEMMul8’s 3M ZGEMM and Ozaki-II DGEMM across the range of , with performance up to 570 TF/s (15 reference ZGEMM) at . Figure 2(b) provides a more detailed heatmap of speedups over reference ZGEMM at a range of sizes and accuracy settings.
7 Conclusion
With each successive generation of CPU and GPU hardware, narrow-precision GEMM throughput continues to outpace traditional single and double precision arithmetic. The Ozaki-II scheme and other emulation methods take advantage of this trend, using integer quantization and small integer GEMMs to accelerate floating-point GEMMs.
In this paper, we have shown that this integer-quantized approach opens up new algorithms for complex GEMMs that leverage the algebraic structure of quadratic integer rings. In particular, 2M multiplication reduces a complex GEMM to two real GEMMs of the same size, instead of the three or four real GEMMs required by previous algorithms. For rank- updates, 2M reduces a complex rank- update () to a real GEMM of the same, and a real rank- update () to a real GEMM of half the size. The new 2M algorithm is easy to implement, and achieves practical speedups that closely match these theoretical predictions.
Acknowledgements
The author is grateful for helpful reviews from numerous colleagues, which greatly improved this paper.