david rosocha
← All projects

DIGITAL DESIGN & VERIFICATION · 2026

FPGA Transformer Accelerator (TPU)

SystemVerilog design and UVM verification on a Basys 3 FPGA.

SystemVerilogUVMVivadoQuestaPyTorch

Designed and verified a TPU-style transformer inference accelerator on a Basys 3 (Artix-7) FPGA. The system offloads attention layers of a trained neural network to hardware over a UART host interface.

Project overview
Architecture of the 8 by 8 systolic array
8 × 8output-stationary array
64 × 64maximum matrix size
<0.9%mean softmax error

PROJECT VIDEO

Building the accelerator

I documented the design and explained how the accelerator works in a YouTube video.

100k+ viewsin its first week

Watch on YouTube ↗

ARCHITECTURE WALKTHROUGH

From input to attention output

The FPGA handles the attention computation. The host sends data over UART, the same compute array performs the matrix products, and a lookup-based softmax turns scores into attention weights.

  1. HOST → FPGAInput & trained weightsTransfer over UART
  2. ON THE FPGAProject → score → normalize → mixMatrix products share one 8 × 8 array
  3. FPGA → HOSTAttention outputReturn the result over UART
The attention operation
Softmax of Q times K transpose divided by the square root of d sub k, multiplied by V.

The matrix operations

For one attention layer, X contains 16 tokens with 64 features each. It combines the input embeddings with positional encoding. The learned projection matrices are 64 × 64, and dk = 64.

Attention operations and matrix dimensions, in computation order
StepOperationInput shapes → output shape
QueriesQ equals X times W sub Q(16 × 64) · (64 × 64) → 16 × 64
KeysK equals X times W sub K(16 × 64) · (64 × 64) → 16 × 64
ValuesV equals X times W sub V(16 × 64) · (64 × 64) → 16 × 64
ScoresS equals Q times K transpose(16 × 64) · (64 × 16) → 16 × 16
Attention weightsA equals row-wise softmax of S divided by the square root of d sub k16 × 16 → 16 × 16 · row-wise
Value mixingC equals A times V(16 × 16) · (16 × 64) → 16 × 64
Output projectionO equals C times W sub out(16 × 64) · (64 × 64) → 16 × 64

Softmax normalizes each score row into attention weights. Those weights mix the value vectors before the final output projection. Matrix products are processed as 8 × 8 tiles on the same array.

Scroll to follow the data. Each diagram advances with the explanation.

Skip to verification ↓

01 / COMPUTE ARRAY

Make the matrix products

Feed the operands
3 × 3 teaching example. The implemented array is 8 × 8 (64 processing elements). Operands enter from the left and top; blue numbers show the running totals.
01.1

Feed the operands

The input is projected into queries (Q), keys (K), and values (V). Multiplying Q by Kᵀ then produces the attention scores. These operations use the same matrix-multiply engine.

Watch: operands enter from the left and top.

01.2

Multiply, add, pass along

Each processing element multiplies a pair of operands and adds the product to its running total. Inputs move to neighboring cells; the partial sum stays in place.

Output-stationary: the sum stays in its processing element.

01.3

Read the completed product

As the final operands pass through, each cell finishes one output value. My implementation has 64 multiply–accumulate processing elements arranged in an 8 × 8 array.

The small example shows the same dataflow as the hardware.

02 / TILING CONTROLLER

Reuse the array for larger matrices

Split the work into 8 × 8 tiles
The highlighted tiles are the pair currently being processed. This animation shows tile scheduling; it does not show the double-buffer control timing.
02.1

Split the work into 8 × 8 tiles

A matrix can be larger than the physical array. The controller divides the operands into tiles, then walks through row, column, and depth positions.

The hardware supports matrix multiplication up to 64 × 64.

02.2

Send each tile pair through the same array

Each output tile accumulates contributions across the depth dimension. The tile schedule reuses the compute array instead of requiring a larger grid of processing elements. The design also uses double buffering for tile data.

Shown: a 16 × 64 by 64 × 64 example, with 128 tile-pair operations.

03 / LOOKUP-BASED SOFTMAX

Turn scores into attention weights

Look up the exponential
Illustrative table entries and coordinates, not the implementation’s bit widths. The implemented Q8.8 softmax measured less than 0.9% mean error against a floating-point reference.
03.1

Look up the exponential

After the score scaling step, softmax needs an exponential for each score. Precomputed values replace calculating the exponential from scratch in hardware.

Shown: an illustrative input maps to a stored exponential value.

03.2

Accumulate the row

The exponentials in one score row are added together. That sum is the denominator used to normalize every value in the row.

Follow the values into the adder: 1 + 2 + 3 + 4 = 10.

03.3

Normalize with a second lookup

The exponential and the row sum select a value from a two-dimensional table. This approximates division and produces the normalized attention weight.

Illustrative example: 4 ÷ 10 ≈ 0.397 after quantization.

04 / FINISH THE ATTENTION OPERATION

Multiply by V, then return the result

The normalized attention weights determine how the value vectors are mixed. The matrix engine computes attention × V and the output projection, then the result travels back to the host over UART.

Attention weights × VOutput projectionUART → host

VERIFICATION

Checking the hardware against a reference

A fixed-point datapath can produce plausible-looking values and still be wrong. I built a UVM environment to compare the accelerator’s outputs with a golden reference model, alongside a separate evaluation of the softmax approximation.

Functional verification

The testbench includes a UVM agent, sequencer, driver, monitor, scoreboard, constrained-random stimulus, and functional coverage.

  1. Generate and drive transactions

    Constrained-random sequences supply stimulus. The sequencer and driver apply transactions to the RTL.

  2. Observe and compare outputs

    The monitor captures the hardware response. The scoreboard checks it against the golden reference to expose datapath and fixed-point errors.

  3. Track what was exercised

    Functional coverage records the cases exercised by the tests, alongside the scoreboard’s correctness checks.

NEXT PROJECTSpeakEasy ↗