What is matrix multiplication?
Matrix multiplication is the linear algebra operation that multiplies an m by n matrix with an n by p matrix to give an m by p matrix; each element of the result is the sum of products of a row of the first matrix and a column of the second.
Unlike the element-wise product, which multiplies elements in matching positions, matrix multiplication combines rows with columns. Fully connected layers and attention in neural networks are largely matrix multiplication.
- Topic
- Infrastructure and runtime
- Also known as
- GEMM, general matrix multiplication
How matrix multiplication works
- Check that the first matrix's column count equals the second's row count.
- For each result element, multiply and sum the matching row and column elements.
- GPUs run these independent multiply and add steps in parallel across thousands of units.
- Very large matrices can be split into blocks across several GPUs.
In more detail
Compute-bound prefill versus memory-bound decode
In LLM inference, matrix multiplications behave very differently across phases. Prompt prefill forms large matrices with high arithmetic intensity and saturates GPU compute (compute-bound), whereas single-request token generation shrinks to matrix-vector multiplications (GEMV) with low arithmetic intensity, leaving compute units idle while waiting on HBM memory bandwidth.
Tiling, shared memory and tensor cores
Modern GPUs do not execute matrix multiplication element by element in global memory; kernels such as cuBLAS, CUTLASS and Triton partition matrices into small tiles loaded into fast on-chip SRAM and registers. Tensor Cores then execute fused matrix-multiply-accumulate (MMA) instructions on entire tiles per clock cycle, drastically cutting off-chip HBM traffic.
Low-precision GEMM and high-precision accumulation
Because matrix multiplication dominates transformer compute, inference and training routinely use reduced-precision formats such as BF16, FP8 and FP4 (on newer architectures) to double or quadruple throughput and halve memory bandwidth needs. To prevent rounding drift across thousands of summed products, Tensor Cores multiply low-precision inputs while accumulating partial sums in higher precision (typically FP32).
Commonly confused terms
| Term | How it differs |
|---|---|
| Matrix-vector multiplication (GEMV) | GEMV (matrix-vector multiplication) multiplies a matrix by a single vector (as in batch-size-1 autoregressive decoding) and is memory-bandwidth-bound, whereas GEMM (matrix-matrix multiplication) reuses loaded weights across many columns and can become compute-bound. |
| Dot product and outer product | An outer product multiplies an m×1 column vector by a 1×n row vector to expand into an m×n matrix, whereas an inner (dot) product collapses two equal-length vectors into a single scalar. |
Why matrix multiplication matters for on-premises AI
The linear algebra chapter of a deep learning textbook defines matrix multiplication, noting it is not simply multiplying individual elements, which is the element-wise or Hadamard product. The roofline model shows performance depends on operations per byte moved from memory; large matrix multiplications are typical high-ratio operations. A paper proposing intra-layer model parallelism for language models splits transformer matrix multiplications across GPUs to train models with billions of parameters. Example calculation: multiplying m×n by n×p matrices takes about 2mnp floating point operations.
Evaluating a new GPU, an AI platform team measures matrix multiplication speed at its language model's layer sizes, judging from how close it gets to theoretical peak whether the model will run efficiently there.
Common questions
Why are GPUs fast at matrix multiplication?
It consists of many independent multiply and add steps; GPUs are built to run them across thousands of compute units at once.
Does precision affect matrix multiplication?
Yes. Lower precision formats boost speed and memory efficiency; accumulating at higher precision limits numerical error.
Related terms
- Tensor
- Parallel computing
- Compute-bound workload
- Floating-Point Operations Per Second
- Graphics Processing Unit
- Tensor parallelism
Sources
- Goodfellow, Bengio and Courville, Deep Learning, Chapter 2: Linear Algebra, 2016
- Williams, Waterman and Patterson, Roofline: An Insightful Visual Performance Model for Multicore Architectures, Communications of the ACM, 2009
- Shoeybi et al., Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism, 2019
In the glossary: Glossary, M to Q