Authors: Jacek Czaja, Michal Gallus, Tomasz Patejko (Intel Corporation); Jian Tang (Baidu)
Abstract
Softmax is a popular normalization method used in machine learning. Deep learning solutions like Transformer or BERT use the softmax function intensively, so it is worthwhile to optimize its performance. This article presents our methodology of optimization and its results applied to softmax. By presenting this methodology, we hope to increase an interest in deep learning optimizations for CPUs. We believe that the optimization process presented here could be transferred to other deep learning frameworks such as TensorFlow or PyTorch.
Introduction
Softmax is a ubiquitous function in machine learning used to turn raw scores (logits) into a probability distribution. While it has traditionally been a negligible part of the computational budget in image classification models like ResNet, modern Natural Language Processing (NLP) architectures—specifically those relying on attention mechanisms like BERT or Transformers—use softmax much more intensively.
On CPUs, which are frequently used for inference in data centers, softmax can become a significant bottleneck. This research targets the Baidu PaddlePaddle framework and the Deep Attention Matching (DAM) model to demonstrate how standard implementations (often relying on general-purpose libraries like Eigen) leave significant performance on the table when running on Intel Xeon hardware.
Optimization Methodology
The researchers employed a four-stage optimization methodology to maximize throughput:
- Algorithmic Modification: The baseline implementation included a
ValueClipfunction to prevent numerical instability during training. Since the focus was on inference, this was unnecessary. Removing it provided an immediate 5% speedup. - Granular Profiling: Using the CPU’s Timestamp Counter (
__rdtsc), the team profiled the operator to find hotspots. They discovered that over 50% of the time was spent on exponential (exp) calculations and 30% on the sum-and-divide stage. - Library Optimization (Intel MKL): Eigen-based exponential and division operations were replaced with highly optimized routines from the Intel Math Kernel Library (MKL). This was the single most impactful change, yielding a 2x performance gain.
- Manual Vectorization & JIT Assembly: When the compiler failed to auto-vectorize certain parts of the code (specifically the "find maximum" operation), the authors used Xbyak, a Just-In-Time (JIT) assembler. This allowed for custom AVX/AVX2 assembly code tailored to specific batch sizes at runtime.
Experimental Results
Softmax Operator Performance
The optimized version of the softmax operator is consistently approximately 2x faster than the Eigen-based baseline across all tested batch sizes (1, 8, 32, and 128). To determine if further optimization was possible, the authors compared their softmax speed to a simple memcpy (memory copy). Since memcpy represents the physical limit of memory throughput, and the optimized softmax was still significantly slower than a copy, it indicated remaining headroom for computational improvements rather than being strictly memory-bound.
End-to-End Model Impact
The optimizations to this single operator significantly improved the total execution time of the Deep Attention Matching (DAM) model. Benchmarks conducted within the Baidu PaddlePaddle platform showed a 15% to 20% overall performance boost for the model during inference.
Key Insights and Future Work
- Inference vs. Training: Code optimized for training stability (like
ValueClip) often carries a performance "tax" that is unnecessary for inference-only deployments. - Compilers Have Limits: Despite modern compiler advancements, manual assembly or JIT-generated code remains necessary for performance-critical primitives where auto-vectorization fails.
- Future Directions: Further speed could be gained by using mathematical approximations for the exponential function, trading minor numerical accuracy for clock-cycle savings. Additionally, moving to AVX-512 instructions on newer Xeon Scalable processors would likely yield even higher throughput for these vector-heavy operations.
These optimizations have been upstreamed into the oneDNN (formerly Intel MKL-DNN) library, making these performance gains available to the wider deep learning community.
Summary updated