Matrix multiplication

Matrix multiplication combines two grids of numbers by multiplying rows by columns and adding the results; it is the core arithmetic of neural networks and the operation AI chips are built to speed up.

A matrix is a grid of numbers. To multiply two matrices, each entry of the result is found by taking a row from the first, a column from the second, multiplying the pairs of numbers and adding them up. Multiplying an M by K matrix by a K by N matrix takes M times N times K multiply-and-add steps. Nvidia's documentation counts each step as two operations, so the total is 2MNK floating-point operations. Two square matrices 8,192 numbers on a side therefore need about 1.1 trillion operations.

Neural networks are built from this operation. A fully connected layer multiplies its input by a matrix of learned weights, and Nvidia describes general matrix multiplications, known in the trade as GEMMs, as a fundamental building block of fully connected, recurrent and convolutional layers. The feed-forward layers in a transformer are fully connected layers, and attention compares tokens by multiplying matrices too. Training and running a large model is, in large part, an enormous number of matrix multiplications.

That is why hardware is designed around it. Nvidia's Tensor Cores, Intel's Advanced Matrix Extensions and Arm's Scalable Matrix Extension all speed up matrix arithmetic directly. Speed depends on memory as well as maths. Nvidia's guide explains that a multiplication is math-limited when it does many operations per byte moved, and memory-limited when it does few. Multiplying a matrix by a single vector, the shape of work when a model handles one request token by token, is always memory-limited, which is one reason memory bandwidth matters so much for AI chips.

Mathematicians have long looked for shortcuts. The schoolbook method takes on the order of n cubed steps for n by n matrices. In 1969 Volker Strassen showed that 2 by 2 blocks could be multiplied with seven multiplications instead of eight, which applied recursively gives roughly n to the power 2.807. Theoretical bounds have since fallen below 2.372, but those methods carry such large overheads that they are never used in practice. In October 2022 Google DeepMind's AlphaTensor, published in Nature, treated the search for multiplication algorithms as a game, building on AlphaZero, and found new ones, including versions tuned to specific hardware that ran 10 to 20% faster on large matrices than commonly used algorithms on Nvidia V100 GPUs and Google TPU v2 chips.

In practice, when chip makers quote performance in trillions of operations per second, or when stories describe a new number format or a faster kernel, the subject is almost always how quickly hardware can multiply matrices and how fast it can feed them with data.

Common questions

Why is matrix multiplication so important for AI?

Neural network layers work by multiplying inputs by large matrices of learned weights, so most of the arithmetic in training and running a model is matrix multiplication. Making that one operation faster or cheaper speeds up almost everything a model does.

Why are GPUs good at matrix multiplication?

Matrix multiplication breaks into many independent multiply-and-add steps that can run at the same time, which suits a GPU's thousands of parallel cores. Modern GPUs also include specialised units such as Nvidia's Tensor Cores that perform small matrix multiplications in a single step.

Can matrix multiplication be done faster?

Yes, in theory and sometimes in practice. Strassen's 1969 method reduced the number of multiplications needed, and later theoretical methods go further but are too costly to use. In 2022 DeepMind's AlphaTensor found algorithms that ran 10 to 20% faster on specific hardware. Most real speed-ups come from better chips and software rather than new formulas.

What is a GEMM?

GEMM stands for general matrix multiplication, the standard routine in numerical libraries that multiplies two matrices and adds the result to a third. Deep learning software relies heavily on GEMM routines, and chip makers tune their libraries to run them as fast as possible.

Finding more

Search for "matrix multiplication deep learning", "GEMM performance GPU" or "Strassen algorithm" to go further. Reliable sources include chip makers' developer documentation, linear algebra textbooks and courses, and research papers on new algorithms. Be careful with peak performance figures in chip marketing, which assume ideal matrix sizes and number formats that real workloads rarely reach.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.