Full Stack Optimization of Transformer Inference

Sehoon Kim, Coleman Hooper, Thanakul Wattanawong, Minwoo Kang, Ruohan Yan, Hasan Genc, Grace Dinh, Qijing Huang, Kurt Keutzer, Michael W. Mahoney, Sophia Shao, Amir GholamiASSYST @ ISCA 2023

TL;DR

This paper tackles the growing challenge of deploying large Transformer models for inference by proposing a "full-stack" optimization approach. By co-designing hardware, software, and neural architectures, they achieve significant speedups of up to 88.7x with minimal performance loss, making these powerful models more practical for real-world applications.

What Problem Does This Paper Address?

Transformer models, the backbone of many recent breakthroughs in NLP and computer vision, are becoming increasingly large and computationally demanding. This makes deploying them for inference, especially in latency-sensitive applications or on resource-constrained edge devices, a significant challenge. The authors highlight a gap in understanding the specific workload characteristics of Transformers compared to traditional CNNs, which hinders efficient hardware and software co-design for inference.

Methods

The paper employs a multi-pronged, "full-stack" approach:

  • Hardware Architecture Optimization: They adapt a CNN-focused accelerator (Gemmini) for Transformer workloads. This involves:
  • Integer-only BERT variant (I-BERT): Replacing floating-point nonlinear operations with integer approximations for faster and more efficient hardware implementation.
  • Memory Configuration Re-adjustment: Tuning the sizes of scratchpads and accumulators to better suit the matrix multiplication patterns in Transformers, leading to reduced matmul latency.
  • Dedicated Normalization Unit: Adding specialized hardware to efficiently compute operations like LayerNorm and Softmax, avoiding costly offloading to the CPU.
  • Scheduling Optimization: They investigate "fusion-optimized scheduling" to overlap the execution of matrix multiplications with subsequent nonlinear operations (like Softmax and LayerNorm). This aims to hide the latency of these often-bottlenecking operations.
  • Neural Architecture Search (NAS): They use hardware-aware NAS to search for Transformer architectures that are Pareto-optimal in terms of Energy-Delay-Product (EDP) and perplexity for their target hardware. This involves training a supernet and then using evolutionary search to find efficient subnets.

Datasets

The paper mentions using WikiText-2 for training language models during the NAS experiments. The primary workload discussed for evaluation is BERT-Base.

Key Insights and Ideas

  • Nonlinear operations are the Achilles' heel: For Transformer inference on existing CNN hardware, the time spent on nonlinear operations (LayerNorm, Softmax, GELU) and the associated dequantization/requantization overheads dominate execution time, far exceeding the time spent on matrix multiplications.
  • Fusion is not always beneficial: While fusing operations is common in CNNs, for Transformers, the benefits of overlapping matmuls with nonlinear operations are highly dependent on the specific operation and hardware. For instance, fusing FFN W2 projection with LayerNorm can actually increase total latency.
  • Hardware-software co-design is crucial: Simply adapting existing hardware isn't enough. Integrating specialized hardware for nonlinear operations (like in I-BERT) and carefully tuning memory configurations are essential for significant performance gains.
  • NAS can find hardware-tailored architectures: Hardware-aware NAS can discover Transformer architectures that offer a better trade-off between performance (perplexity) and hardware cost (EDP, latency, energy) than standard architectures.

Key Results

  • Overall Speedup: A full-stack co-design approach achieved up to 88.7x end-to-end speedup with minimal performance degradation.
  • Hardware/Software Co-Design (I-BERT): Adapting Gemmini with I-BERT features and memory tuning resulted in a 39.6x latency reduction for BERT inference, with only a 14% increase in area and 9.3% increase in power.
  • Scheduling Optimization: Fusion-optimized scheduling for MHA's query x key matmul and Softmax improved latency by 78%.
  • Neural Architecture Search: NAS found architectures that offered a 1.4x reduction in latency with a 0.1 point perplexity degradation, and up to 2.4x latency reduction with 1 point perplexity degradation. EDP improvements were also significant, with up to 10.6x reduction for 1 point perplexity degradation.

Limitations and Future Work

  • Generalizability of Scheduling: The paper notes that the findings on operator fusion and scheduling for Transformers are specific to the Gemmini accelerator and may differ on other custom hardware. Future work could explore more generalizable scheduling strategies.
  • Hardware-Specific NAS: The NAS was performed for a specific hardware configuration (Gemmini with I-BERT optimizations). Extending NAS to explore a wider range of hardware targets and constraints would be valuable.
  • End-to-End Pipeline: The authors emphasize that their work represents a single run of their co-design methodology and that results may vary depending on the target hardware and optimization goals. They suggest that further improvements can be made by considering the entire end-to-end optimization pipeline.

Summary updated

BibTeX
@inproceedings{kim2023full,
  author = {Sehoon Kim and Coleman Hooper and Thanakul Wattanawong and Minwoo Kang and Ruohan Yan and Hasan Genc and Grace Dinh and Qijing Huang and Kurt Keutzer and Michael W. Mahoney and Sophia Shao and Amir Gholami},
  title = {{Full Stack Optimization of Transformer Inference}},
  booktitle = {ASSYST @ ISCA},
  year = {2023},
  url = {https://openreview.net/forum?id=GtyQbLUUagE},
}
Figure