Hardware Accelerator for Multi-Head Attention and Position-Wise Feed-Forward in the Transformer

Siyuan Lu, Meiqi Wang, Shuang Liang, Jun Lin, Zhongfeng Wang · DOI

Abstract

Designing hardware accelerators for deep neural networks (DNNs) has become essential for efficient deployment. While many accelerators exist for CNNs and RNNs, the Transformer model—which is rapidly replacing RNNs in Natural Language Processing (NLP)—presents unique challenges due to intensive matrix computations and complex data flows. This paper proposes the first hardware accelerator specifically for the Multi-Head Attention (MHA) and Position-Wise Feed-Forward Network (FFN) ResBlocks, the two most computationally demanding layers in the Transformer. By introducing efficient matrix partitioning, optimized computation flows for systolic arrays, and streamlined non-linear function units, our FPGA-based design achieves significant performance gains. Compared to a GPU implementation, the proposed accelerator demonstrates a 14.6× speedup for MHA and a 3.4× speedup for FFN ResBlocks.

What Problem Does This Paper Address?

Transformers have revolutionized NLP, but their massive computational requirements hinder deployment on mobile or embedded devices. Unlike CNNs or RNNs, Transformers rely on massive General Matrix-Matrix Multiplications (GEMMs) and dynamic "attention" mechanisms with complex data dependencies. Furthermore, non-linear functions like Softmax and Layer Normalization (LayerNorm) often become performance bottlenecks on the critical path.

The standard Transformer architecture consisting of Encoder and Decoder stacks.

Methods

The proposed accelerator focuses on the MHA and FFN ResBlocks, implemented on a Xilinx FPGA. The design is built around three primary pillars:

1. Matrix Partitioning and Resource Sharing

The authors identified that weight matrices in both MHA and FFN can be partitioned into uniform $s \times 64$ blocks (where $s$ is the sequence length). This uniformity allows a single high-performance Systolic Array (SA)—the heart of the accelerator—to be reused for both layer types, significantly reducing the hardware footprint.

2. Optimized Non-linear Units

  • Softmax: To avoid expensive division and exponentiation, a "log-sum-exp" approximation is used. This transforms these operations into simple additions and shifts, reducing hardware complexity without sacrificing numerical stability.
  • Layer Normalization: Traditionally, LayerNorm waits for an entire matrix row to be calculated. The authors reformulated the variance equation to allow LayerNorm to start "in advance," overlapping its execution with the systolic array's output to parallelize the process.

3. Hardware-Friendly Data Flow

The computation flow is meticulously designed to ensure high hardware utilization of the systolic array. The architecture supports 8-bit integer (INT8) quantization, which reduces memory and power requirements while maintaining translation quality.

Detailed hardware architecture for MHA (left) and FFN (right) ResBlocks, showing data flow and matrix dimensions.

Key Results

The design was implemented on a Xilinx xcvu13p FPGA running at 200MHz and evaluated using the IWSLT 2016 German-English translation task.

  • Performance Speedup: The accelerator achieved a 14.6× speedup in MHA ResBlocks and a 3.4× speedup in FFN ResBlocks compared to an NVIDIA V100 GPU.
  • Energy Efficiency: Total on-chip power was measured at 16.7W, offering superior energy efficiency compared to high-end server GPUs.
  • Accuracy Preservation: The INT8 quantized model achieved a BLEU score of 23.57, slightly exceeding the FP32 baseline of 23.48, proving that the hardware-specific optimizations do not degrade translation quality.

Conclusion

This work establishes a foundation for efficient Transformer hardware accelerators. By addressing the core computational bottlenecks of MHA and FFN through co-designed hardware and algorithms, the proposed architecture enables high-speed, low-power Transformer inference. Future work will focus on integrating embedding layers and the final output stages to create a complete end-to-end Transformer ASIC.

Summary updated

Abstract

Designing hardware accelerators for deep neural networks (DNNs) has been much desired. Nonetheless, most of these existing accelerators are built for either convolutional neural networks (CNNs) or recurrent neural networks (RNNs). Recently, the Transformer model is replacing the RNN in the natural language processing (NLP) area. However, because of intensive matrix computations and complicated data flow being involved, the hardware design for the Transformer model has never been reported. In this paper, we propose the first hardware accelerator for two key components, i.e., the multi-head attention (MHA) ResBlock and the position-wise feed-forward network (FFN) ResBlock, which are the two most complex layers in the Transformer. Firstly, an efficient method is introduced to partition the huge matrices in the Transformer, allowing the two ResBlocks to share most of the hardware resources. Secondly, the computation flow is well designed to ensure the high hardware utilization of the systolic array, which is the biggest module in our design. Thirdly, complicated nonlinear functions are highly optimized to further reduce the hardware complexity and also the latency of the entire system. Our design is coded using hardware description language (HDL) and evaluated on a Xilinx FPGA. Compared with the implementation on GPU with the same setting, the proposed design demonstrates a speed-up of 14.6x in the MHA ResBlock, and 3.4x in the FFN ResBlock, respectively. Therefore, this work lays a good foundation for building efficient hardware accelerators for multiple Transformer networks.
BibTeX
@misc{lu2020hardware,
  author = {Siyuan Lu and Meiqi Wang and Shuang Liang and Jun Lin and Zhongfeng Wang},
  title = {{Hardware Accelerator for Multi-Head Attention and Position-Wise Feed-Forward in the Transformer}},
  year = {2020},
  eprint = {2009.08605},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2009.08605},
}
Figure