Fast DistilBERT on CPUs

Haihao Shen, Ofir Zafrir, Bo Dong, Hengyu Meng, Xinyu Ye, Zhe Wang, Yi Ding, Hanwen Chang, Guy Boudoukh, Moshe WasserblatarXiv 2022 · DOI

Abstract

Transformer-based models like BERT have revolutionized Natural Language Processing (NLP), but their high computational requirements often necessitate expensive GPU resources for real-time deployment. This paper introduces a specialized pipeline designed to bring high-performance Transformer inference to standard CPUs. By integrating hardware-aware block-wise pruning, INT8 quantization, and a custom inference engine, we achieve up to a 4.1x speedup over standard runtimes while maintaining baseline accuracy.

Introduction

In industrial NLP applications, the "production wall"—the strict latency and throughput requirements for real-time user interaction—often limits the deployment of large Transformer models. While GPUs offer high performance, they are not always cost-effective or available at the edge. Existing compression techniques like pruning and quantization often fail to deliver actual speedups on CPUs because standard software runtimes are not optimized for sparse, low-precision data. We address this by co-designing the model architecture and the inference software.

Methods

Our "Fast Transformer" pipeline utilizes three primary techniques:

1. Block-wise Structured Sparsity

Unlike unstructured sparsity, which removes random weights and creates irregular memory access patterns, we prune weights in blocks of four. This structured approach is specifically designed for CPU vector processing units.

Block-wise structured sparsity aligns model weights with CPU vector processing capabilities.

2. Knowledge Distillation and Quantization

To recover accuracy lost during pruning, we employ Knowledge Distillation (KD) during fine-tuning, where a smaller sparse model learns from a larger teacher. Following fine-tuning, we apply Post-Training Quantization (PTQ) to convert the model from 32-bit floating point (FP32) to 8-bit integers (INT8), significantly increasing data throughput.

3. Custom Inference Engine

The core of our performance gain is a dedicated runtime engine featuring optimized sparse GEMM kernels that utilize Intel AVX512-VNNI instructions. The engine also employs graph optimization (fusing operations like Bias and LayerNorm) and advanced memory management to minimize data movement overhead.

Results

We evaluated our pipeline using DistilBERT fine-tuned on the SQuADv1.1 dataset.

Accuracy Preservation

The compressed model achieved an F1 score of 86.07%, slightly exceeding the original FP32 DistilBERT baseline of 85.80%. This demonstrates that high levels of compression can be achieved without sacrificing linguistic performance.

Performance Gains

On AWS c6i instances, our solution demonstrated superior performance across various sequence lengths compared to existing industry standards.

Performance ratio comparison across different sequence lengths, showing significant gains over ONNX Runtime and Neural Magic.

Key performance metrics include:

  • 4.1x Speedup over the standard ONNX Runtime baseline.
  • 1.5x (50%) Improvement over Neural Magic’s DeepSparse engine, the previous state-of-the-art for CPU inference.
  • Sub-10ms Latency: The engine consistently maintained high throughput while staying within production-critical latency thresholds.

Conclusion

This work demonstrates that through hardware-software co-design, standard CPUs can become highly efficient platforms for Transformer inference. By aligning model sparsity with hardware capabilities and optimizing the software stack, we bridge the gap between theoretical compression and real-world performance. Future work will extend this pipeline to other architectures like RoBERTa and GPT-style models.

Summary updated

BibTeX
@misc{shen2022fast,
  author = {Haihao Shen and Ofir Zafrir and Bo Dong and Hengyu Meng and Xinyu Ye and Zhe Wang and Yi Ding and Hanwen Chang and Guy Boudoukh and Moshe Wasserblat},
  title = {{Fast DistilBERT on CPUs}},
  year = {2022},
  eprint = {2211.07715},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2211.07715},
}
Figure