TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

How to Design and Implement an FPGA Accelerator for an ML Model

Murugavel Ganesan
by
0
Share Facebook X LinkedIn WhatsApp Email
FPGA SoC block diagram

To design and implement an FPGA accelerator for a specific ML model, you define the model and its targets, profile it to find the compute and memory bottlenecks, design a parallel datapath and dataflow that fits the FPGA's DSP, memory and I/O resources, implement it in HLS or an HDL, verify it in simulation and on hardware, and then benchmark and tune it. The key to a good result is matching the hardware to the model: quantized arithmetic, data reuse in on-chip memory and a pipeline that keeps every multiplier busy. This post walks through the six steps in plain terms first, then adds a worked sizing example, code and metrics. I assume you have some basic knowledge of FPGA programming and ML concepts.

Relationship between AI, machine learning and deep learning

Why use an FPGA for ML

CPUs are flexible but run ML layers mostly in sequence. GPUs have huge throughput but work best on large batches. An FPGA lets you build a datapath shaped exactly for one model: custom bit widths, deep pipelines and direct connections to sensors or networks. That makes FPGAs attractive for low-latency, batch-size-one inference at the edge, in networking, and in scientific triggers, and for models whose operators change faster than an ASIC could be built.

The six steps in plain words

Step 1: Define the ML model or task

Define the ML model or task that you want to accelerate. This could be a classification, regression, clustering or any other type of ML problem. Specify the input and output data formats, the performance metrics and the target platform (for example cloud, edge or embedded). Write the targets down as numbers, because every later decision is checked against them.

Step 2: Analyze the model and find the bottlenecks

Analyze the ML model or task and identify the computational bottlenecks. This can be done by profiling the execution time and resource usage of the model on a CPU or GPU. Also consider the data movement and memory access patterns, as they can affect the performance and efficiency of the FPGA accelerator as much as the arithmetic does. Most ML inference time goes into multiply-accumulate (MAC) operations, so count them.

Step 3: Design the architecture and dataflow

Choose the FPGA resources, such as logic elements, memory blocks, DSP slices and hard IP cores, and connect them in a way that maximizes the parallelism and throughput of the accelerator. Also design the dataflow between the FPGA and the host system, for example using DMA engines over PCIe or an on-chip AXI interconnect in an FPGA SoC. Keep weights and activations on chip and reuse them as much as possible.

Step 4: Implement with HLS or an HDL

High-level synthesis (HLS) tools let you write the accelerator in C or C++ and generate the hardware description automatically. Hardware description languages (HDLs) such as Verilog or VHDL let you describe the hardware directly, at a lower level. HLS shortens development; HDL gives the finest control. Vendor and open-source frameworks can generate much of the design from a trained model.

Step 5: Test and debug

Verify the accelerator in simulation first, then on real hardware such as a development board or a cloud FPGA instance. Compare results against a software model of the same quantized network.

Step 6: Evaluate and optimize

Measure latency, throughput, power and resource utilization, then tune parameters such as clock frequency, pipeline depth, loop unrolling and memory partitioning until the targets from step 1 are met.

Common mistakes

  • Sizing the design by peak MACs and ignoring memory bandwidth.
  • Quantizing without checking accuracy on a validation set.
  • Leaving the host interface until last; PCIe or DMA overhead can dominate small models.
  • Filling the device to 95% or more and then failing timing closure.

Deep dive: the numbers and details

This part holds the sizing calculations, architecture options, Verilog, tool choices and metrics. Skip it if the overview is all you need.

Example target line

An illustrative step 1 target: "ResNet-style image classifier, 224 by 224 RGB input, INT8 weights, under 5 ms latency at batch size one, under 10 W board power, on a mid-range FPGA SoC."

Worked example: one convolution layer

Take a 3 by 3 convolution with 64 input channels, 64 output channels and a 56 by 56 output (a typical early layer in a ResNet-style network). The numbers are illustrative but the method is general.

QuantityCalculationResult
MAC operations56 × 56 × 64 × 64 × 9about 115.6 million
Weights (INT8)64 × 64 × 9 bytes36,864 bytes (36 KB)
Output activations (INT8)56 × 56 × 64 bytes200,704 bytes (about 196 KB)
Time with 1,024 MACs at 300 MHz115.6 M / 1,024 / 300 MHzabout 0.38 ms

The weights fit easily in on-chip block RAM, so they can be loaded once and reused for every output pixel. Each weight is used 3,136 times (56 × 56), which is the data reuse that makes this layer compute-bound rather than memory-bound. A fully connected layer, by contrast, uses each weight only once per input and is usually limited by memory bandwidth. Repeating this analysis for every layer shows where the time and the bandwidth go.

A quick roofline-style check makes the point. For the convolution, 115.6 million MACs use 36,864 bytes of weights, about 3,136 MACs per weight byte. A fully connected layer with 4,096 inputs and 1,000 outputs needs 4,096,000 MACs and 4,096,000 weight bytes at batch one, so exactly one MAC per byte. If weights stream from external memory at, say, 10 GB/s, that layer can never exceed 10 billion MACs per second however many DSP slices you add.

Common architecture choices

ArchitectureHow it worksGood for
Systolic arrayA grid of MAC units passes data to neighbors each cycleConvolutions and matrix multiplies with high reuse
Layer pipeline (dataflow)Each layer gets its own hardware stage; data streams throughSmall models, lowest latency
Overlay or soft processorA fixed programmable engine runs many models from instructionsChanging models without rebuilding the bitstream

Precision is a major design lever. Moving from FP32 to INT8 cuts memory traffic by four times, and FPGA vendors document techniques for getting more INT8 work out of each DSP block; see AMD's white paper WP486, Deep Learning with INT8 Optimization on Xilinx Devices (2017), which claims 1.75 times solution-level INT8 performance against other FPGA DSP architectures.

The basic building block: a MAC unit

Whatever architecture you choose, it is built from multiply-accumulate units. The Verilog below is a two-stage INT8 MAC with a 32-bit accumulator; the multiply maps naturally onto a DSP slice.

// INT8 multiply-accumulate unit with a 32-bit accumulator
module mac_int8 (
  input  wire               clk,
  input  wire               clr,      // start a new dot product
  input  wire               valid,
  input  wire signed [7:0]  a,        // activation
  input  wire signed [7:0]  w,        // weight
  output reg  signed [31:0] acc
);
  reg  signed [15:0] prod;
  reg                prod_v;
  always @(posedge clk) begin
    prod   <= a * w;                  // stage 1: multiply (maps to a DSP block)
    prod_v <= valid;
    if (clr)         acc <= 32'sd0;   // stage 2: accumulate
    else if (prod_v) acc <= acc + prod;
  end
endmodule

A testbench feeding the vectors (12, 3), (−7, 5), (127, −2) and (−128, −128) produces 16,131, which matches 36 − 35 − 254 + 16,384. The 32-bit accumulator leaves plenty of headroom: even the worst-case product of 16,384 can be added tens of thousands of times without overflow (231 / 16,384 = 131,072 additions).

HLS, HDL and frameworks

ApproachExamplesStrengthsTrade-offs
HDLVerilog, SystemVerilog, VHDLFull control of timing and resourcesSlowest to write and change
HLSAMD Vitis HLS, Intel HLS and oneAPI flowsFaster iteration, C++ testbenchesNeeds pragmas and care to reach good results
ML-to-FPGA frameworksAMD Vitis AI, Intel FPGA AI Suite and OpenVINO, hls4ml, FINNGenerate much of the design from a trained modelLess freedom; supported layers vary

In HLS, the important directives are pipelining loops so a new iteration starts every cycle, unrolling inner loops to create parallel MACs, and partitioning arrays so many values can be read in the same cycle. AMD lists them in the HLS Pragmas section of the Vitis HLS user guide (UG1399). Two open-source frameworks are worth knowing: hls4ml, a Python package that translates Keras, PyTorch and ONNX models into HLS for several vendor back ends, and FINN, from AMD's research labs, which builds dataflow architectures for quantized neural networks.

Testing in detail

  • Compare every layer's output bit for bit against a quantized software reference model, not the FP32 model.
  • Use the vendor's on-chip logic analyzer to see internal signals on the board.
  • Test with real input data as well as random vectors; saturation and rounding bugs often hide in real data ranges.

Metrics and what to do when one falls short

MetricWhat to measureTypical fix if it falls short
LatencyTime for one input end to endDeeper layer pipelining, fewer host round trips
ThroughputInferences per secondMore parallel MACs, higher clock, batching
PowerBoard watts, inferences per jouleLower precision, clock gating, less off-chip traffic
UtilizationDSP, BRAM, LUT and routing useRebalance parallelism between layers

FAQ

Is an FPGA faster than a GPU for ML? It depends on the workload. FPGAs often win on latency and energy for small batches and custom precision; GPUs usually win on raw throughput for large batches and on ease of programming.

Do I need to know Verilog to build an FPGA ML accelerator? Not always. HLS and ML-to-FPGA frameworks can produce working designs from C++ or a trained model, but HDL knowledge helps a lot when debugging and optimizing.

What precision should I use? INT8 is a common starting point for inference. Lower precisions such as 4-bit or binary are possible for some models, but always confirm accuracy after quantization.

I can go into more detail on any of these steps based on interest. Leave a comment with the step you would like covered next.

Murugavel Ganesan
About the Author
Murugavel Ganesan
Product Director | SerDes · IVN · Edge AI · Compute | Ex Intel · Infineon

A semiconductor product leader working across product architecture, definition, strategy, and silicon and software execution, from concept through volume production. He has worked with teams across Asia, Europe and the USA, including at Intel, Infineon and ArrayComm.

His work covers ASICs, SoCs, FPGAs and chiplets for wireless, wireline, automotive, IoT and AI, including PCIe, CXL and DDR interfaces alongside RF and mixed-signal design.

Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!