Systolic Arrays Explained
← /learn · 06

Inside a PE

One processing element, bit by bit: a three-stage MAC that multiplies bfloat16 or FP16 exactly and accumulates in FP32, checked register for register against the author's RTL.

Check yourself ↓ three questions and what to remember, at the end

Loading the animation…

Concept

Every chapter so far drew a PE as a box that multiplies and adds in one cycle. Inside, it is a small pipeline, and this animation is one: the author's mixed-precision MAC unit, a SystemVerilog design with three stages, run cycle by cycle by this site's model of it.

  1. Stage 1 latches the two operands, aa (an activation) and bb (a weight), with a valid bit and a clear bit that marks the first term of a new dot product.
  2. Stage 2 multiplies them. For two bfloat16 numbers the 8-bit significands (7 stored bits plus the hidden 1) multiply into at most 16 bits, which FP32's 24-bit significand holds exactly, so the product needs no rounding. FP16's 11-bit significands give 22 bits: still exact. INT8 × INT8 fits 16 bits.
  3. Stage 3 adds the product to the accumulator. This is the only place the PE rounds: the smaller of the two numbers is shifted right to line up the binary points, the sum is rounded back to 24 significant bits (to nearest, ties to even), and anything that would be subnormal is flushed to zero. In INT8 mode the accumulator is a 32-bit integer that saturates instead of wrapping.

Each colour band in the picture is a field of the number: sign (purple), exponent (sky), mantissa (green); a lit cell is a 1 bit. The two dot products of 4 terms run back to back, and the second starts on the cycle the clear bit reaches stage 3: no bubble between them. In FP16 mode, 2 of the demonstration's adds round; the caption shows the exact sum next to the stored one whenever they differ.

The pipeline is why the arrays in chapters 2 to 5 can run at a high clock rate: each stage does a third of the work. It adds latency (a product reaches the accumulator three cycles after its operands arrive) but not cost per MAC, because a new pair enters every cycle.

Concept

Why bfloat16 in and FP32 out. bfloat16 is the top half of an FP32: the same 8-bit exponent, so the same range, but only 7 stored mantissa bits. Google's TPUs multiply in bfloat16 and accumulate in FP32 by default (Google Cloud TPU documentation), and Kalamkar et al. (2019) found that training with bfloat16 tensors reached the same results as FP32 across image, speech, language and recommendation models, in the same number of iterations and with no change of hyperparameters. The narrow mantissa makes the multiplier small (Google's bfloat16 article notes that a multiplier's size scales with the square of the mantissa width) while the wide accumulator keeps the long sums of a matrix multiply accurate. Why a long sum needs the extra bits, and what it does to the error, is chapter 3 of Numerics Explained; how formats from INT8 to FP4 map onto hardware is its chapter 10.

Checked against the RTL. The site's Python model of this PE (reference/pe.py) is checked against the RTL itself: rtl/check_rtl.py builds the vendored design in Verilator 5.020, drives 5 sequences (1,216 operations, FP16 and INT8, with idle cycles and exponents across FP16's whole normal range), and compares every pipeline register after every clock edge: 11,079 values, 0 mismatches. The bfloat16 mode is this site's addition on the same pipeline (the RTL has FP16 and INT8 modes); it is checked against NumPy's float32 arithmetic instead.

What it costs in gates. The square law is easy to test on real logic. rtl/synth_yosys.py synthesises a bare unsigned multiplier with Yosys Yosys 0.69 to generic gates: 8 × 8 bits (bfloat16's significand, with its hidden bit) takes 343 gates, 11 × 11 (FP16's) takes 680, 1.983 times as many against (11/8)2=(11/8)^2 = 1.891, and 24 × 24 (FP32's) takes 3,456, 10.08 times against 9: a little faster than the square, because the adder tree also gets deeper. The whole MAC unit tells the same story. With its mode pin tied so only one datapath survives, the INT8 PE is about 7,258 transistors in Yosys's CMOS estimate and the FP16 PE (multiplier, alignment, normalisation and rounding) about 19,652, 2.708 times as much. These are gate counts and an area proxy, not a placed design; the model page has the flow and every number.

Maths

A bfloat16 number with sign ss, biased exponent ee and mantissa mm (7 bits) is (−1)s⋅(1+m/27)⋅2e−127(-1)^s \cdot (1 + m/2^7) \cdot 2^{e - 127}. The product of two is (−1)sa⊕sb⋅(27+ma)(27+mb)⋅2ea+eb−254−14(-1)^{s_a \oplus s_b} \cdot (2^7 + m_a)(2^7 + m_b) \cdot 2^{e_a + e_b - 254 - 14}: the significand product is an integer below 2162^{16}, so it is exactly representable in FP32 (24 bits) as long as the exponent stays in range. The adder then computes

acc′=RN32(acc+p),\mathrm{acc}' = \mathrm{RN}_{32}(\mathrm{acc} + p),

the exact sum rounded once. The model computes the sum in double precision (53 bits) and rounds that to FP32; for addition this double rounding cannot change the result, because 53≥2⋅24+253 \ge 2 \cdot 24 + 2 (Figueroa, 1995). The RTL gets the same answer from three extra bits below the significand (guard, round and a sticky OR of everything shifted out), and the comparison above confirms it on every add.

Code

The rounding step of the RTL's FP32 adder, from rtl/mac_unit.sv (vendored unchanged from the author's Interview_RTL_LLM_Accelerators repository):

        // Round to nearest, ties to even: guard = norm_sig[2],
        // round|sticky = norm_sig[1:0], LSB of the kept mantissa = norm_sig[3]
        round_up    = norm_sig[2] & ((|norm_sig[1:0]) | norm_sig[3]);
        rounded_sig = norm_sig[26:3] + {23'd0, round_up};

Stage 3's choice between starting a new dot product and adding to the old one:

        if (s2_clear && s2_mode) begin
            // Clear then accumulate (or just clear to +0.0 if no valid data)
            fp32_acc_next = s2_valid ? s2_fp32_product : 32'h00000000;
        end else if (s2_valid && s2_mode) begin
            fp32_acc_next = fp32_add_result;

The same step in the site's TypeScript model, cut from src/lib/sa/pe.ts:

export function fp32Add(acc: number, p: number): number {
  const r = f32Bits(f32Value(acc) + f32Value(p));
  const v = f32Value(r);
  if (v === 0 || Math.abs(v) < FP32_MIN_NORMAL) return 0;
  return r;
}

Check yourself

Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.

Q1A PE multiplies two bfloat16 numbers and accumulates in FP32. Where does it round?
Reveal the answer

Answer: Only in the adder, once per add

Only in the adder. Two 8-bit significands multiply into at most 16 bits, which FP32's 24-bit significand holds exactly; the sum is rounded once, to nearest, ties to even.

Inside the PE →

Pick one, then reveal.

Q2Synthesised with Yosys, how many times as many gates is an 11 × 11 multiplier (FP16's significand) as an 8 × 8 one (bfloat16's)?
Reveal the answer

Answer: 1.983 (counted right within 10%)

1.983, a little more than the square law's (11/8)2=(11/8)^2 = 1.891, because the adder tree also gets deeper.

What it costs in gates →

Commit to a number, then reveal.

Q3A product reaches the accumulator three cycles after its operands arrive. What does that three-stage pipeline cost?
Reveal the answer

Answer: Nothing per MAC: a new pair enters every cycle; it adds latency only

Latency, not throughput: each stage does a third of the work, so the clock can be faster, and a new pair of operands still enters every cycle. Two dot products run back to back with no bubble.

Inside the PE →

Pick one, then reveal.