Hellas

RDNA 3 matrix accelerator internals

In this post we will investigate the internal arithmetic behaviour of the matrix multiplication unit present in the AMD RDNA 3/3.5 based consumer GPUs as exposed by the wmma instruction family. This is needed in order to be able to deterministically reproduce the exact outputs of AI computations run on such machines without having access to one, using an accurate simulator.

Introduction

GEMM (general matrix–matrix multiplication) is a fundamental linear algebra operation specified by the BLAS standard for high-performance computing. Using a separate matrix for the output, its general form is:

\[ D = \alpha AB + \beta C \]

where \(A\), \(B\), \(C\), and \(D\) are matrices, and \(\alpha\) and \(\beta\) are scalars.

GEMM is a major primitive in today's transformer-based architectures, so it accounts for a large share of computation in generative AI workloads. Arithmetic-intensive tasks such as LLM training, prompt prefill, batched inference, and image and video generation all benefit from efficient matrix multiplication. For this reason, modern AI hardware, including GPUs, TPUs, NPUs and some CPUs, has specialized units and corresponding instructions for accelerating matrix operations. Most ML/AI kernel libraries, frameworks, and applications make heavy use of these instructions.

In general, these matrix accelerators use small matrices (tiles) as inputs and outputs. They evaluate the constituent dot products in parallel, much faster and more efficiently than would be possible using only their general-purpose arithmetic units.

Many matrix accelerator instructions implement this simplified form of the GEMM formula:

\[ D = AB + C \]

A full GEMM is then built at the kernel or library level using these tile matrix multiply-accumulate instructions, together with scaling and accumulation across tiles.

A few examples of GPU matrix accelerator instructions are NVIDIA's Tensor Core operations (mma.sync on Volta and later, wgmma on Hopper, and tcgen05.mma on Blackwell), AMD CDNA's Matrix Core MFMA, and RDNA's WMMA. They come in many flavours, with successive generations adding support for new formats (e.g. BF16 on Ampere, FP8 on Hopper, and FP4 on Blackwell), different tile sizes, different accumulator precisions, and asynchronous and sparse variants.

A tile matrix multiplication produces independently calculated output elements. As a generalization of fused multiply-add to matrices, it is sometimes called Block FMA or Matrix FMA in the accelerator literature. To begin analyzing an accelerator's arithmetic, we can isolate a single output element. It is the dot product of a row of the first matrix and a column of the second, added to a previous accumulator value:

\[ d_{ij} = a_{i1}b_{1j} + a_{i2}b_{2j} + \cdots + a_{iK}b_{Kj} + c_{ij} \]

One highlighted row of A and column of B contribute to one output cell of D, together with the matching cell of C.

One output cell comes from one row–column dot product and the matching accumulator cell.

Adding these terms sequentially using ordinary floating-point addition rounds the result after each addition. Matrix accelerators often use fused dot-product accumulation, where several products share an accumulation step. The grouping, precision, and rounding points are implementation-specific. There are several architectural possibilities:

  • Monolithic fused dot product: align all products and the previous accumulator, sum them into a single wide accumulator, and round only once at the end.
  • Balanced reduction tree: sum terms as a binary tree, with intermediate rounding or truncation depending on the implementation.
  • Iterative accumulation: add products sequentially into a running accumulator.
  • A combination of the above.

Three ways to sum four products and C: one fused sum, a balanced tree, and a running accumulator. R marks possible rounding or truncation.

The same sum can have different intermediate results. R marks a possible rounding or truncation point.

The problem

IEEE 754 specifies operations such as addition, multiplication, division, and fused multiply-add, including their rounding behaviour. It also describes recommended reduction operations allowing implementation the liberty to pick the reduction order, internal precision, or intermediate rounding modes. Mixed-precision matrix accelerators often leave these details unspecified in their public instruction descriptions. Their arithmetic differs between vendors and even between successive architectures from the same vendor.

This means different accelerators can produce different output bits for the same inputs because of the reduction order, the precision of their internal accumulators, intermediate rounding steps, and their handling of subnormal values and NaNs.

The resulting problem is numerical portability. Code written in portable C++/CUDA/HIP may run on a new architecture while producing different results. In sensitive numerical algorithms or AI training runs, those differences can affect convergence or accuracy. Deterministic bitwise reproducibility across devices for verification purposes is also not guaranteed.

Sometimes multi-term addition hardware yields surprising outputs. For example, researchers found an instance where different GPUs computed very different outputs for the same inputs: “V100, A100, and MI250X produced 0; MI100 produced 255.875; and Hopper H100 produced 191.87”. Others discovered that multi-term addition may not even be monotonic for some inputs on the A100 GPU: increasing one term can decrease the computed sum.

Black-box testing of matrix accelerators

Since the introduction of Tensor Cores in NVIDIA's Volta architecture, researchers have been analyzing successive generations of matrix accelerators to infer their arithmetic behaviour. By observing outputs for carefully chosen inputs that exercise floating-point corner cases, and then testing candidate models against randomized inputs, they have constructed partially or fully accurate models of these operations. Progress ranges from early work estimating the high-level architecture of the NVIDIA V100 FP32 unit to newer work covering NVIDIA, AMD, or both vendors' data center architectures. The MMA-Sim repository associated with the latter paper implements the resulting arithmetic models in Python using scalar GPU primitives.

The main reverse engineering technique uses feature-targeted probes: matrix inputs that distinguish possible internal arithmetic implementations. These fall into a few categories:

Ordering and grouping

  • Individual dot products: use sparse matrices, zeros, and controlled accumulator values to study a single output element, separating its arithmetic from the surrounding matrix operation. The end result of a dot product is independent of the indices of the row and column involved.
  • Block-FMA width (fusion size): place large, mutually cancelling products and small contributions at different positions in a dot product. Observe where small contributions disappear to identify how many products share an accumulation step before intermediate rounding or truncation.
  • Summation order and reduction topology: permute large positive, large negative, and small terms. Differences caused by cancellation and swamping help distinguish sequential accumulation, pairwise trees, and fused multi-term reductions.
  • Accumulator placement: vary the initial accumulator \(C\) independently of the products to determine whether it participates directly in the fused sum, is added afterward, or feeds a sequence of smaller accumulation steps.

Precision and rounding

  • Internal accumulator precision: vary exponent differences between large and small terms to determine how many low-order mantissa bits survive alignment.
  • Rounding modes: construct values exactly at or around rounding midpoints to distinguish rounding to nearest from truncation and other rounding rules.
  • Intermediate normalization and rounding: construct cancellation sequences that would recover small terms under exact accumulation but lose them after intermediate normalization, rounding, or truncation.

Exceptional-value handling

  • Subnormal inputs and outputs: supply subnormal multiplicands, produce subnormal products, and construct sums whose outputs approach the normal/subnormal boundary. Determine whether each stage preserves subnormals or flushes them to zero (FTZ).
  • Special values: try signed zeros, infinities, NaNs, overflow, and underflow, including combinations whose results depend on operation order.

The AMD RDNA 3/RDNA 3.5 WMMA instruction

Starting from MMA-Sim, we investigate v_wmma_f32_16x16x16_bf16: BF16 inputs with FP32 accumulation and output, on a Strix Halo machine (Radeon 8060S, gfx1151). The goal is to find out precisely how WMMA combines the 16 products and the accumulator making up the dot product between a row and a column of the two input matrices.

By starting with the techniques described above, then adapting them and narrowing down the possibile outcomes, we'll build the following model of the WMMA instruction:

acc = c
for k in (0, 2, 4, 6, 8, 10, 12, 14):
    acc = v_dot2_f32_bf16(a[k:k+2], b[k:k+2], acc)
d = acc

where v_dot2_f32_bf16 is the existing RDNA 3 instruction doing dot product with f32 accumulation on bf16 2-vectors.

The following sections describe in more detail how the model was discovered. A coding agent was used to run and adapt the existing probes and interprets their results, with human suggestions to add random sweeps around NaN and subnormal cases.

Start with the checks that already work

We add a kernel that runs one WMMA instruction. An identity matrix and small integers check that it loads and stores the right elements. Existing tests then check that repeated inputs give the same answer and that unrelated matrix elements do not affect it.

The individual dot products technique also works unchanged. We zero everything except row 0 of \(A\), column 0 of \(B\), and \(C_{00}\). Write these inputs as \(a_k=A_{0k}\), \(b_k=B_{k0}\), and \(c=C_{00}\), with output \(d=D_{00}\).

Find which products share an accumulation step

One output contains 16 product terms, \(p_k=a_kb_k\). Does WMMA add them all in one step, or process smaller groups? We use the existing block-FMA width and summation order test to look for a pattern.

Set every \(a_k=b_k=2^{-9}\) and \(c=\varepsilon=2^{-18}\), then replace two products with \(-2^{14}\) and \(+2^{14}\), using \((2^7,-2^7)\) and \((2^7,2^7)\). The large products cancel in exact arithmetic, leaving \(15\varepsilon\). Moving the large products can expose grouping and rounding effects.

Each heatmap cell is one placement: its column selects the negative-product position and its row selects the positive-product position. Values are shown in units of \(\varepsilon\), so the exact answer would be 15 everywhere.

Cancellation tests on gfx1151: purple cells have normalized results from minus 256 to minus 242, orange cells from 512 to 524, and blue outlines mark possible adjacent product groups.

The negative results range from \(-256\) to \(-242\); the positive ones range from \(512\) to \(524\), both in steps of two. The clue is where the response changes: positions 0 and 1 behave like one group, positions 2 and 3 repeat that pattern, and so on. This suggests adjacent two-product groups \((p_0,p_1)\), \((p_2,p_3)\), …, \((p_{14},p_{15})\).

The pattern suggests pairs, but does not explain the large residues. We isolate one possible pair to see whether losing small terms is the whole explanation.

Change the tests to study one pair

We keep only positions 14 and 15 active, with positive zero elsewhere. To test accumulator placement, compare two ways of cancelling one:

  • \((a_{14},a_{15})=(1,-1)\), \((b_{14},b_{15})=(1,1)\), \(c=0\): output \(-2^{-24}\).
  • \((a_{14},a_{15})=(1,0)\), \((b_{14},b_{15})=(1,1)\), \(c=-1\): output \(2^{-23}\).

There are no small terms here to lose: both exact sums are zero. The pair operation itself differs from an ordinary fused sum, and treats a negative product differently from a negative accumulator.

We adapt precision, rounding-mode, and intermediate normalization tests to this pair, varying exponent gaps and values near rounding ties. New special-value tests also vary zero signs. With \(a_{14}=b_{14}=b_{15}=1\) and \(c=0\), changing \(a_{15}=+0\) to \(-0\) changes the result from \(1\) to \(1-2^{-24}\).

To separate alignment from final rounding, set the first product to \(2^{-24}\) and \(c=1\). A second product of \(2^{-26}\) gives \(1+2^{-23}\), but \(2^{-27}\) gives \(1\). Both exact sums exceed the FP32 midpoint, so the smaller product must be lost before final rounding.

These results guide a new search over alignment rules and sign corrections. The matching rule aligns the terms, drops low bits, applies a sign-dependent correction, and rounds to FP32. Choosing candidate rules requires reasoning beyond the generic probes; the appendix gives the measurements and formulas.

Check the eight-dot2 model

The tests suggest adjacent pairs, but do not yet establish how those pairs combine. We test a running accumulator that feeds each pair's FP32 result into the next. AMD's v_dot2_f32_bf16 already combines two BF16 products with an FP32 accumulator. We add a direct comparison with eight native dot2 operations:

Eight dot2 steps consume adjacent BF16 pairs and pass an FP32 accumulator from C to D.

The arrows describe an arithmetic model; they do not establish a physical circuit.

acc = c
for k in (0, 2, 4, 6, 8, 10, 12, 14):
    acc = v_dot2_f32_bf16(a[k:k+2], b[k:k+2], acc)
d = acc

The native dot2 sequence matches WMMA bit for bit on the tested inputs. Rounding to FP32 between pairs explains the intermediate rounding and position-dependent outputs.

Add tests near the underflow boundary

We reuse raw-bit comparison against hardware, adding isolated pairs, forced cancellation, and mixtures of special values. The original special-value checks give broad answers; new tests check exact NaN payloads and whether a later pair replaces an earlier NaN.

The subnormal inputs and outputs checks show flushing but do not locate its exact cutoff. Our initial simulator rounds each pair's result to FP32, then flushes subnormal outputs. Basic checks pass, so we add a finer sweep around the boundary: fix \(a_{14}=2^{-100}\), \(a_{15}=-0\), \(b_{15}=1\), and \(c=-2^{-126}\), then vary one input:

\(b_{14}\)WMMA output
\(2^{-51}\)\(+0\)
\(2^{-52}\)\(-2^{-126}\)

Rounding first and flushing afterward gets the first case wrong. The model must decide underflow before rounding.

With this correction, the simulator matches the tested gfx1151 behaviour as eight sequential dot2 steps.

WMMA arithmetic details

These probes and formulas give the detailed BF16-to-FP32 model behind the investigation. Unless stated otherwise, isolated-pair examples use positions 14 and 15, with other vector entries positive zero.

Matrix-fragment check

The matrix-fragment check uses:

\[ A=I_{16},\qquad B_{kj}=16k+j,\qquad C_{ij}=1, \qquad 0\leq i,j,k<16. \]

Every input is exactly representable in BF16, and the expected FP32 output is simply \(B+1\).

Position-sweep measurements

Selected measurements from the cancellation sweep above, whose exact answer is \(15\varepsilon\):

Negative-product position \(j\)Positive-product position \(i\)Observed \(d/\varepsilon\)
01\(-242\)
02\(524\)
23\(-244\)
04\(522\)
1415\(-256\)

Signed correction

We isolate the last pair, setting all other vector entries to positive zero:

\((a_{14},a_{15})\)\((b_{14},b_{15})\)\(c\)Observed \(d\)
\((1,-1)\)\((1,1)\)\(0\)\(-2^{-24}\)
\((1,0)\)\((1,1)\)\(-1\)\(2^{-23}\)
\((-1,0)\)\((1,1)\)\(1\)\(-2^{-24}\)
\((1,+0)\)\((1,1)\)\(0\)\(1\)
\((1,-0)\)\((1,1)\)\(0\)\(1-2^{-24}\)

The first three exact sums are zero; the last two differ only in a zero's sign. Ordinary IEEE fused arithmetic cannot explain these results. Sign sweeps identify a correction of minus one alignment unit per negative product, plus two units for a negative accumulator. Signed zeros participate too.

Alignment and rounding

Replacing the cancelling unit products with \((a_{14},a_{15})=(1.5,-1.5)\) and \((b_{14},b_{15})=(1.5,1.5)\), with \(c=0\), still gives \(-2^{-24}\). Although the product magnitudes increase to \(2.25\), the alignment unit does not double: product exponents are sums of input exponents, without renormalization.

Next, fix \(a_{14}=b_{14}=2^{-12}\) and vary the remaining inputs:

\(a_{15}\)\(b_{15}\)\(c\)Observed \(d\)
\(0\)\(0\)\(1\)\(1\)
\(0\)\(0\)\(1+2^{-23}\)\(1+2^{-22}\)
\(2^{-13}\)\(2^{-13}\)\(1\)\(1+2^{-23}\)
\(2^{-13}\)\(2^{-14}\)\(1\)\(1\)

The first two ties round to the FP32 neighbour with an even significand. In the last two cases, both exact sums exceed the same midpoint, but only the extra product of \(2^{-26}\) survives alignment; \(2^{-27}\) is lost. This separates alignment truncation from output rounding.

Testing candidate models against 32,768 isolated-pair outputs identifies the alignment rule:

\[ E=\max\!\left(e_{a_{14}}+e_{b_{14}},\ e_{a_{15}}+e_{b_{15}},\ e_c-2\right), \qquad u=2^{E-24}. \]

Zero products do not determine the maximum; zero accumulators use exponent \(-126\). With \(c=1\) above, \(E=-2\) and \(u=2^{-26}\), explaining the lost smaller product. Writing \(p_0=a_{14}b_{14}\) and \(p_1=a_{15}b_{15}\), the finite aligned sum is:

\[ \begin{aligned} S &= \operatorname{trunc}(p_0/u)+\operatorname{trunc}(p_1/u) +\operatorname{trunc}(c/u)\\ &\quad-\sigma(p_0)-\sigma(p_1)+2\sigma(c),\\ x &= Su. \end{aligned} \]

Here \(\sigma\) is the sign bit, including signed zero, and truncation is toward zero. The result undergoes FP32 rounding and the range handling below. The correction resembles a one's-complement conversion bias, without establishing a physical circuit design.

Range handling and special values

A BF16 subnormal \(a_{14}=2^{-127}\) produces zero with either \(b_{14}=1\) or \(b_{14}=2^{127}\). The latter would produce one if the input survived, identifying input flushing. Zero products with \(c=2^{-127}\) likewise reveal accumulator input flushing.

Products behave differently: \(a_{14}=2^{-126}\), \(b_{14}=1/2\), and \(c=0\) produce zero, but using those values for both active products produces \(2^{-126}\). Products are not independently flushed before summation. Nor do they overflow independently: \((a_{14},a_{15})=(2^{127},-2^{127})\), \((b_{14},b_{15})=(2,2)\), and \(c=0\) produce \(-2^{104}\), rather than NaN from two overflowing products.

Raw-bit probes distinguish NaN sources, with other inputs positive zero:

Active inputFP32 output bitsFinding
BF16 \(a_{14}\) bits 0x7fc0, \(b_{14}=1\), \(c=0\)0xffc00a3dFixed input-NaN tag
\(a_{14}=+\infty\), \(b_{14}=0\), \(c=0\)0xffc00000Distinct invalid-operation NaN
Zero products, FP32 \(c\) bits 0x7f8000010x7fc00001Accumulator payload preserved and quieted

Combining these cases establishes precedence: input NaN, then invalid arithmetic, then accumulator NaN. A later pair's invalid operation can therefore replace an earlier NaN; exceptional values must be handled at every step.

Underflow before rounding

For the finite aligned sum \(x\), the measured cutoff is:

\[ T=2^{-126}-2^{-151}. \]

If \(\lvert x\rvert<T\), return positive zero before FP32 rounding. Otherwise, round to nearest, ties to even; equality with the cutoff preserves the signed minimum normal value.

For the two boundary examples in the narrative, \(E=-128\) and \(u=2^{-152}\). Including the signed correction gives:

\[ \begin{aligned} b_{14}=2^{-51}:\quad x&=-2^{-126}+3u,\\ b_{14}=2^{-52}:\quad x&=-2^{-126}+2u=-T. \end{aligned} \]

The first magnitude is below the cutoff and flushes. The second is exactly at the cutoff and rounds to \(-2^{-126}\). Both would round to that minimum normal if ordinary FP32 rounding happened first.

The standalone HIP/CUDA app reproduces wmma with f32 accumulators.