[Paper Review] Optimizing Inference Performance of Transformers on CPUs
This paper optimizes Transformer inference on CPUs by proposing three matrix multiplication (matmul) optimizations: adaptive matrix transposition, reduced sequential overhead in matmul calls, and tuned matrix partitioning. Evaluated on BERT using HuggingFace's benchmark, these techniques achieve up to 2.37x speedup without altering model accuracy or requiring framework changes.
The Transformer architecture revolutionized the field of natural language processing (NLP). Transformers-based models (e.g., BERT) power many important Web services, such as search, translation, question-answering, etc. While enormous research attention is paid to the training of those models, relatively little efforts are made to improve their inference performance. This paper comes to address this gap by presenting an empirical analysis of scalability and performance of inferencing a Transformer-based model on CPUs. Focusing on the highly popular BERT model, we identify key components of the Transformer architecture where the bulk of the computation happens, and propose three optimizations to speed them up. The optimizations are evaluated using the inference benchmark from HuggingFace, and are shown to achieve the speedup of up to x2.37. The considered optimizations do not require any changes to the implementation of the models nor affect their accuracy.
Motivation & Objective
- Address the under-explored challenge of optimizing Transformer inference performance on CPU-based systems, despite their widespread use in production Web services.
- Identify matrix multiplication (matmul) as the primary performance bottleneck in BERT inference on CPUs.
- Propose three hardware-aware, non-invasive optimizations to accelerate matmul operations without modifying model architecture or affecting inference accuracy.
- Demonstrate that these optimizations significantly improve scalability and performance across varying sequence lengths and thread counts on Intel Skylake CPUs.
- Validate the generality of findings across different models (e.g., BERT-base) and math libraries (e.g., oneDNN), extending applicability beyond BERT.
Proposed method
- Implement adaptive matrix transposition: dynamically choose whether to transpose input matrices before matmul to improve data locality and cache utilization.
- Reduce sequential overhead in matmul invocation by analyzing and minimizing per-call latency in deep learning frameworks, improving thread-level scalability.
- Optimize matrix partitioning parameters (BK, BM, BN) in oneDNN’s GEMM kernels to better match CPU cache hierarchy, especially reducing last-level cache (LLC) misses.
- Apply tuned partitioning by reducing BK from 384 to 64, enabling smaller, cache-friendly sub-matrices and improving instruction-level parallelism.
- Use performance counters (via perf) to validate reduced LLC misses and higher IPC in the optimized oneDNN version.
- Evaluate all optimizations using the HuggingFace inference benchmark on PyTorch with oneDNN, measuring latency across multiple sequence lengths and thread counts.
Experimental results
Research questions
- RQ1Why is matmul the dominant performance bottleneck in BERT inference on CPUs, and how does its scalability degrade with increasing thread count?
- RQ2To what extent can adaptive matrix transposition improve matmul performance by enhancing data locality and cache efficiency?
- RQ3How does the sequential overhead in matmul invocation limit scalability, and can it be reduced without changing the underlying framework?
- RQ4Can tuning matrix partitioning parameters (e.g., BK, BM, BN) lead to measurable performance gains by better utilizing CPU cache and reducing cache misses?
- RQ5To what extent are the observed performance improvements generalizable across different Transformer models and math libraries?
Key findings
- The proposed optimizations achieve a maximum inference speedup of 2.37× on BERT-large across various sequence lengths and thread counts.
- Adaptive matrix transposition alone yields up to 1.44× speedup for short sequences (e.g., 8 tokens) and 1.19× for long sequences (e.g., 384 tokens) on 16 threads.
- Reducing matrix partitioning size (BK from 384 to 64) results in a 1.16× speedup for BERT-large at 4 threads and 1.19× at 16 threads, with measurable reductions in last-level cache (LLC) misses.
- Performance counters show that the optimized oneDNN version reduces cycles per instruction (IPC) while maintaining similar instruction counts, indicating improved hardware utilization.
- The optimizations are effective across multiple models: BERT-base also shows significant latency reduction with tuned partitioning, confirming broader applicability.
- The share of time spent in layer normalization increases from 2.9% (1 thread) to 12% (16 threads), indicating that future optimization should target such operations as well.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.