TurboTransformers: an efficient GPU serving system for transformer models

Jiarui Fang, Yang Yu, Chengduo Zhao, Jie Zhou

TL;DR

TurboTransformers is a high-performance GPU serving system designed specifically for Transformer models. It overcomes the dual challenges of massive computational demand and variable-length inputs by introducing fused GPU kernels, a graph-aware memory allocator, and a dynamic programming-based batch scheduler that minimizes the "padding tax."

What Problem Does This Paper Address?

Transformer-based models (like BERT, GPT, and XLNet) have revolutionized NLP, but they are notoriously "heavy" to run in production. Engineers face two main hurdles:

  1. Computational Intensity: Transformers require billions of floating-point operations (Gflops) even for short sentences, far exceeding the requirements of older models like ResNet or AlexNet.
  2. Variable-Length Inputs: Unlike images, which can be resized to a fixed dimension, natural language comes in varying lengths. Standard serving systems often handle this by "padding" shorter sentences with zeros to match the longest one in a batch. This wastes memory and forces the GPU to perform useless calculations on zeros, significantly hurting throughput and latency.

Existing solutions either struggle with the dynamic nature of these inputs or require time-consuming "offline tuning" that isn't practical for real-time services.

Methods

TurboTransformers introduces three major innovations across the computing, memory, and serving layers:

  • Kernel Fusion & Optimized Reductions: The system reorganizes the model's computation graph by fusing multiple small operations into single, efficient GPU kernels. This reduces the overhead of launching many small tasks and minimizes slow trips to main memory. Specifically, they redesigned "Batch Reduction" operations (like Softmax and LayerNorm) to exploit three levels of hardware parallelism: streaming processors, warps, and instructions. The system fuses multiple operations, like bias addition and transposing, into single kernels to reduce overhead.
The system fuses multiple operations, like bias addition and transposing, into single kernels to reduce overhead.
  • Sequence-Length-Aware Memory Allocator: Instead of a generic memory manager, TurboTransformers uses the model's computation graph to predict the lifecycle of every intermediate tensor. It organizes memory into "chunks" and uses a specialized algorithm to reuse these chunks based on the specific length of the current request, drastically reducing the memory footprint. An example of how the allocator adjusts memory offsets and chunk usage based on input sequence length.
An example of how the allocator adjusts memory offsets and chunk usage based on input sequence length.
  • DP-Based Batch Scheduler: To maximize throughput, the system groups incoming requests into batches. Rather than a naive first-come-first-served approach, it uses a Dynamic Programming (DP) algorithm to find the mathematically optimal way to group requests of different lengths, minimizing the total amount of zero-padding required.

Datasets

The authors evaluated TurboTransformers using several popular Transformer variants:

  • BERT-base: A standard benchmark for NLP tasks.
  • Albert-large: A more parameter-efficient but computationally complex version of BERT.
  • DistilBert: A smaller, faster version of BERT.
  • Seq2Seq Decoder: Used for a Chinese-to-English translation task.

Evaluation was performed using randomly generated and sampled texts with sequence lengths ranging from 5 to 500 tokens to simulate real-world variability.

Key Insights and Ideas

  • The "Padding Tax" is Expensive: In traditional systems, if you batch a 10-word sentence with a 100-word sentence, you pay for 100 words of computation for both. TurboTransformers' DP scheduler treats batching as an optimization problem to minimize this waste.
  • Beyond GEMM: While Matrix Multiplication (GEMM) is the bulk of the work, "hot spots" like Softmax and LayerNorm can take up to 38% of the time in standard implementations like PyTorch. Optimizing these "non-GEMM" kernels is essential for reaching state-of-the-art performance.
  • Graph-Awareness is Key: By knowing the "topology" of the neural network, the system can be much smarter about memory than a general-purpose allocator. It knows exactly when a piece of memory is no longer needed and can immediately give it to another part of the model.

Key Results

The system was tested on an NVIDIA RTX 2060 GPU against popular runtimes like PyTorch and ONNX Runtime:

  • Speedups: TurboTransformers achieved up to 2.44x speedup over PyTorch for BERT inference on short sequences.
  • Memory Efficiency: The peak GPU memory usage for BERT was only 663 MB, compared to 1307 MB for PyTorch and 1653 MB for ONNX Runtime.
  • Throughput: The DP-based batching strategy improved serving throughput by 4.06x compared to unbatched PyTorch.
  • Ease of Use: The system can be integrated into existing PyTorch code with just a few lines of Python.

Limitations and Future Work

While TurboTransformers excels at handling variable-length inputs in real-time, it does not currently utilize some of the "offline" hardware-specific tuning tricks found in systems like NVIDIA's TensorRT (e.g., auto-selecting the best CUDA block sizes for a specific GPU). Consequently, for strictly fixed-length workloads where long offline tuning is acceptable, specialized runtimes might still hold a slight edge. The authors suggest that future versions could incorporate these hardware-specific optimizations while maintaining their flexible, variable-length-friendly design.

Summary updated

BibTeX
@inproceedings{fang2021turbotransformers,
  author = {Jiarui Fang and Yang Yu and Chengduo Zhao and Jie Zhou},
  title = {{TurboTransformers: an efficient GPU serving system for transformer models}},
  booktitle = {PPoPP},
  year = {2021},
  url = {https://openreview.net/forum?id=Zq0E054vMl},
}
Figure