Search

Search papers and lessons

Tensor Core

Level 3 · 13 min read

A tensor core is a piece of GPU hardware that multiplies two small matrices and adds a third in one step, which speeds up the matrix arithmetic at the heart of deep learning.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

A tensor core is a special unit inside a GPU that multiplies two small matrices and adds a third matrix to the result, all in one hardware step. Ordinary GPU cores do one multiplication and one addition at a time, so this design does many more operations per clock cycle on the matrix arithmetic that dominates deep learning. It works best when the numbers are stored in low-precision formats such as 16-bit floats.

Builds on: Matrix Multiplication, Floating-Point Numbers, GPU Architecture · Leads to: FlashAttention-3 and Warp Specialization, which keep the tensor cores fed with data

Where it's used

Training large language models. Most of the time spent training a large transformer is spent multiplying large matrices, in the layers that compute attention and the layers that mix features. Frameworks such as PyTorch send those multiplications to cuBLAS and cuDNN, which use tensor cores when the shapes and data types allow it.

Serving image and speech models. A convolution layer in a vision model can be rewritten as a large matrix multiplication. Running that multiplication on tensor cores lets a single GPU answer many requests per second, which is why inference servers on data-centre GPUs usually run in 16-bit mode.

Scientific computing. Some simulation codes, such as dense linear solvers for physics problems, use mixed-precision methods that keep most arithmetic in 16-bit tensor-core operations and correct the result in 32-bit or 64-bit arithmetic. The speed-up comes from the same matrix hardware.

How it works

Take a tiny example. We want the product of two 2 by 2 matrices, plus a third matrix that is added at the end. A tensor core computes the whole expression D=AB+CD = AB + C in one operation.

A=(1234),B=(5678),C=(1001).A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, \quad B = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}, \quad C = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}.
  1. Load the input tiles. The hardware receives the matrix AA, the matrix BB, and the matrix CC as small blocks called tiles. The entries of AA and BB are stored as 16-bit floating-point numbers. The entries of CC are stored in 32-bit form. Check: each tile has four entries, so AA, BB and CC hold 12 numbers in total.
  2. Compute the first entry of the product. The top-left entry of ABAB is a dot product of the first row of AA and the first column of BB: 1×5+2×7=5+14=191 \times 5 + 2 \times 7 = 5 + 14 = 19. Check: 5 + 14 = 19.
  3. Add the matching entry of C. The top-left entry of CC is 1, so the top-left entry of DD is 19+1=2019 + 1 = 20. The sum is kept in a 32-bit accumulator, not rounded back to 16 bits. Check: 19 + 1 = 20.
  4. Repeat for the other three entries. The top-right entry is 1×6+2×8+0=221 \times 6 + 2 \times 8 + 0 = 22. The bottom-left entry is 3×5+4×7+0=433 \times 5 + 4 \times 7 + 0 = 43. The bottom-right entry is 3×6+4×8+1=513 \times 6 + 4 \times 8 + 1 = 51. Check: 22, 43 and 51 are the other three entries, and 20 + 22 + 43 + 51 is the full result.
  5. Write the result. The hardware writes out D=(20224351)D = \begin{pmatrix} 20 & 22 \\ 43 & 51 \end{pmatrix}. Check: four entries, as expected for a 2 by 2 result.
  6. Count the work. Those four results used 8 multiplications and 8 additions in one instruction. A real tensor core works on a 4 by 4 tile for the same operation and performs 64 multiply-add steps at once. Check: 2 × 2 × 2 = 8 multiplications; 4 × 4 × 4 = 64.

The next visual shows the data flow. The three input tiles enter the tensor core, and one output tile leaves it.

flowchart LR
  A["Tile A, 16-bit"] --> T["Tensor core: multiply A by B, add C"]
  B["Tile B, 16-bit"] --> T
  C["Tile C, 32-bit"] --> T
  T --> D["Tile D, 32-bit result"]

The math

Definition

Definition: Tensor core operation

A tensor core operation takes a matrix AA of size m×km \times k, a matrix BB of size k×nk \times n and a matrix CC of size m×nm \times n, and returns the matrix D=AB+CD = AB + C of size m×nm \times n. The inputs AA and BB are low-precision numbers, and the accumulation and output use higher precision. The sizes mm, nn and kk are fixed by the hardware, for example 4 by 4 by 4 in the first generation.

Step 1 in symbols: loading the tiles

The hardware reads three tiles. We write AikA_{ik} for the entry in row ii and column kk of AA, with the same convention for BB and CC. Here ii runs from 1 to mm, jj runs from 1 to nn, and kk runs from 1 to the inner size. In the example, m=n=k=2m = n = k = 2.

Step 2 in symbols: the dot products

Each entry of DD is a sum of products and one entry of CC:

Dij=∑k=1KAikBkj+Cij.D_{ij} = \sum_{k=1}^{K} A_{ik} B_{kj} + C_{ij}.

Here KK is the inner size, the number of terms in each dot product. For the first entry of the example, D11=(1⋅5+2⋅7)+1=20D_{11} = (1 \cdot 5 + 2 \cdot 7) + 1 = 20. This matches step 3 of "How it works".

Step 3 in symbols: the precision

The products AikBkjA_{ik} B_{kj} are formed from 16-bit inputs, and the sum over kk is held in a 32-bit accumulator. The 16-bit format stores each number with a short mantissa, as described in Floating-Point Numbers. Summing many terms in 16 bits would lose accuracy quickly, so the accumulator is kept at 32 bits. The final entries of DD are written in 32-bit form, or converted to 16-bit if the next step needs it.

Step 4 in symbols: counting the work

One tensor core operation performs m⋅n⋅Km \cdot n \cdot K multiply-add steps. Each multiply-add counts as 2 floating-point operations, so the operation performs

FLOPs=2 m n K.\text{FLOPs} = 2 \, m \, n \, K.

For the 2 by 2 example this is 2×2×2×2=162 \times 2 \times 2 \times 2 = 16 operations, which is 8 multiplications and 8 additions.

Step 5 in symbols: the time at peak speed

If a chip can perform PP operations per second on tensor cores, then a multiplication of an N×NN \times N matrix by an N×NN \times N matrix needs at least

t≥2N3Pt \geq \frac{2 N^3}{P}

seconds, ignoring memory and other overheads. This is a best case. Real code is slower because data must be moved to the chip's units before the arithmetic can start, as discussed in the GPU memory hierarchy.

Worked examples

Example
1. A single 1 by 1 operation

Let A=(2)A = (2), B=(3)B = (3) and C=(1)C = (1). Each is a 1 by 1 matrix.

  1. Compute the product AB=2×3=6AB = 2 \times 3 = 6.
  2. Add the entry of CC: 6+1=76 + 1 = 7.
  3. The result is D=(7)D = (7).

Answer: 7. This is the simplest case of D=AB+CD = AB + C. It shows the add-in step that makes the operation a multiply-accumulate.

Example
2. The 2 by 2 tile from How it works

Use AA, BB and CC from the walkthrough.

  1. Compute the top-left entry of ABAB: 1×5+2×7=191 \times 5 + 2 \times 7 = 19.
  2. Add the top-left entry of CC: 19+1=2019 + 1 = 20. This is D11D_{11}.
  3. Compute the top-right entry of ABAB: 1×6+2×8=221 \times 6 + 2 \times 8 = 22.
  4. Add the top-right entry of CC, which is 0: 22+0=2222 + 0 = 22. This is D12D_{12}.
  5. Compute the bottom-left entry of ABAB: 3×5+4×7=433 \times 5 + 4 \times 7 = 43.
  6. Add the bottom-left entry of CC, which is 0: 43+0=4343 + 0 = 43. This is D21D_{21}.
  7. Compute the bottom-right entry of ABAB: 3×6+4×8=503 \times 6 + 4 \times 8 = 50.
  8. Add the bottom-right entry of CC, which is 1: 50+1=5150 + 1 = 51. This is D22D_{22}.

Answer: D=(20224351)D = \begin{pmatrix} 20 & 22 \\ 43 & 51 \end{pmatrix}. The operation used 8 multiplications and 8 additions.

Example
3. A realistic 4096 by 4096 product

A layer in a large model multiplies two matrices of size N=4096N = 4096. Suppose the chip has a tensor-core peak of P=125P = 125 trillion operations per second, the figure quoted for NVIDIA's Volta V100 in 16-bit mode.

  1. Compute N3=40963=68,719,476,736N^3 = 4096^3 = 68{,}719{,}476{,}736.
  2. Multiply by 2 to get the operation count: 2N3=137,438,953,4722 N^3 = 137{,}438{,}953{,}472, about 1.37×10111.37 \times 10^{11} operations.
  3. Divide by the peak rate: 1.37×1011/1.25×1014≈1.1×10−31.37 \times 10^{11} / 1.25 \times 10^{14} \approx 1.1 \times 10^{-3} seconds.
  4. Convert to milliseconds: about 1.1 ms.
  5. For comparison, the same chip's ordinary 32-bit cores have a peak of about 15.7 trillion operations per second. The same product would need about 1.37×1011/1.57×1013≈8.8×10−31.37 \times 10^{11} / 1.57 \times 10^{13} \approx 8.8 \times 10^{-3} seconds, roughly 8 times longer.

Answer: about 1.1 ms on tensor cores at peak, and about 8.8 ms on ordinary cores at peak. Real runs are slower than both, because data movement takes time, but the ratio shows why the layer uses tensor cores.

Try it: a 1024 by 1024 product, N=1024N = 1024, on the same 125 trillion operations per second chip. Find the ideal time. Answer: 2×10243=2,147,483,6482 \times 1024^3 = 2{,}147{,}483{,}648 operations, and 2.147×109/1.25×1014≈1.7×10−52.147 \times 10^9 / 1.25 \times 10^{14} \approx 1.7 \times 10^{-5} seconds, about 17 microseconds.

Common mistakes and tradeoffs

It is tempting to think that a GPU with tensor cores speeds up every operation. In fact the tensor cores only perform matrix multiply-accumulate on fixed tile sizes and data types. Element-wise operations such as adding a bias or applying an activation, and reductions such as sums over a whole vector, still run on the ordinary cores. A model made mostly of such operations gains little.

It is tempting to think that 16-bit inputs mean a 16-bit result. In fact the accumulation is kept at 32 bits, because summing many 16-bit numbers in 16 bits would lose accuracy. The output can be 32-bit, or it can be converted back to 16-bit by the next step.

It is tempting to think that the tensor core is always the bottleneck. In fact many real programs are limited by how fast data reaches the chip. If the arithmetic finishes quickly but the data arrives slowly, the chip waits. That is why the design of tensor-core code is tied to the memory hierarchy, and why warp specialization and similar methods overlap data loading with arithmetic.

When to use tensor cores: when the work is a large matrix multiplication, the data types are supported, and the sizes are multiples of the tile size, which is commonly a multiple of 8 or 16. Training and inference of neural networks usually meet these conditions.

When not to use them: when the matrices are small or irregular, when you need full 64-bit accuracy, or when the arithmetic is mostly element-wise. Lower precision, as in quantization or 16-bit tensor-core inputs, can change the results enough to matter, so Mixed-Precision Training adds loss scaling and a 32-bit master copy of the weights to keep training stable.

Alternatives: other accelerators, such as Google's TPU, use different matrix units based on a systolic array. Some CPUs also have matrix-instruction extensions. The same idea, a fixed-size matrix operation in hardware, appears in all of them.

History

Before 2017, NVIDIA's GPUs ran matrix arithmetic on ordinary floating-point cores, one multiplication and one addition at a time. Deep learning research had made large matrix multiplications the main cost of training, and the cost was high enough to justify dedicated hardware.

In 2017 NVIDIA introduced the Volta architecture with first-generation tensor cores. The unit computed a 4 by 4 by 4 multiply-accumulate with 16-bit inputs and 32-bit accumulation. NVIDIA quoted much higher throughput for deep learning than the previous generation.

The Turing architecture in 2018 added support for lower-precision integer formats for inference. The Ampere architecture in 2020 added the TF32 and bfloat16 formats, and it made tensor cores easier for frameworks to use without changing model code. The Hopper architecture in 2022 added 8-bit floating-point formats.

The idea caught on because most of the work in deep learning could be written as large matrix multiplications. Each generation added formats that trade a little accuracy for more speed, and frameworks then changed their defaults to use them.

Recap

  • A tensor core computes D=AB+CD = AB + C on a small fixed-size tile in one hardware operation.
  • The inputs are 16-bit or lower precision; the accumulation is kept at 32 bits so that the sum stays accurate.
  • One operation does many multiply-add steps at once, which is why it is far faster than ordinary cores for matrix arithmetic.
  • The speed-up is real only if the data arrives fast enough and the shapes match the tile sizes.
  • Next, read Warp Specialization to see how programs keep the tensor cores busy, and FlashAttention-3 for a worked use of them.

Check yourself

QuestionIn one sentence, what does a tensor core compute, and what is the role of the matrix C?Show answer

A tensor core computes D = AB + C on a small tile in one operation. The matrix C is added to the product, so the operation is a multiply-accumulate, and it lets a program add a bias or an earlier partial sum without a separate step.

QuestionLet A = (4), B = (5) and C = (2), all 1 by 1 matrices. What is D?Show answer

AB = 4 × 5 = 20. Then D = 20 + 2 = 22. The answer is D = (22).

QuestionWhy does a tensor core keep the sum in 32 bits when its inputs are 16-bit numbers?Show answer

Adding many 16-bit numbers in 16 bits loses small contributions to rounding, so the error grows with the length of the sum. A 32-bit accumulator keeps the running total accurate, even though the inputs are stored in fewer bits.

QuestionA chip delivers 125 trillion tensor operations per second. Roughly how long does one 2048 by 2048 matrix product take at peak, ignoring data movement?Show answer

The operation count is 2 × 2048³ = 2 × 8,589,934,592 = 17,179,869,184, about 1.72 × 10^10. Dividing by 1.25 × 10^14 gives about 1.4 × 10^-4 seconds, or about 0.14 ms.

QuestionA model's time is mostly spent in element-wise operations such as activations and bias additions. Will tensor cores make it much faster? Explain.Show answer

Probably not much. Tensor cores only accelerate matrix multiply-accumulate on supported shapes and types. Element-wise operations run on the ordinary cores, so the speed-up applies only to the matrix part of the work.

Sources