Article Complete
AIM
Finally, A BetterTransformer
Finally, A BetterTransformer

Finally, A BetterTransformer

To use BetterTransformer, install PyTorch 1.12 and start using high-quality, high-performance Transformer models with the PyTorch API today.

Published Updated

Key takeaways

  • PyTorch 1.12 introduces BetterTransformer, enhancing Transformer Encoder Inference performance.
  • BetterTransformer offers a significant speed and throughput improvement for common execution scenarios.
  • The new release features optimised native implementations for MultiHeadAttention and TransformerEncoderLayer.
  • Integrated fast paths in PyTorch's standard Transformer APIs facilitate faster processing.
  • Two main optimisations in BetterTransformer include fused kernels for efficiency and advanced memory management.
5 min

The newly released PyTorch 1.12 has introduced BetterTransformer which implements a backwards-compatible fastpath of torch.nn.TransformerEncoder for Transformer Encoder Inference. It helps in 2x in speedup and throughput for many common execution scenarios.

Image: Transformer Encoder architecture

BetterTransformer launches with accelerated native implementations of MultiHeadAttention and TransformerEncoderLayer for CPUs and GPUs. These fast paths are integrated into the standard PyTorch Transformer APIs and help in accelerating TransformerEncoder, TransformerEncoderLayer and MultiHeadAttention nn.modules. 

Source: pytorch.org

These new modules implement two types of optimizations:

  1. Fused kernels combine multiple individual operators normally used to implement Transformers to provide a more efficient implementation.
  2. Take advantage of sparsity in the inputs to avoid performing unnecessary operations on padding tokens. Padding tokens frequently account for a large fraction of input batches in many Transformer models used for Natural Language Processing.

Backwards Compatibility

Advantageously, BetterTransformer does not need any model change. To benefit from fast path execution, inputs and operating conditions must satisfy some access conditions. While the internal implementation of Transformer APIs has changed, PyTorch 1.12 maintains strict compatibility with Transformer modules shipped in previous versions, enabling PyTorch users to use models created and trained with previous PyTorch releases while benefiting from BetterTransformer improvements.
For more details, click here

Membership

Stay ahead with AI insights.

Unlimited access to premium articles, exclusive analysis, and the stories shaping India's AI economy.

Subscribe now

AIM Americas

1190 Miraloma Way Suite P, Sunnyvale, CA 94085

AIM India

1st Floor, Sakti Statesman, Bellandur, Bengaluru – 560103

Β© 2026 AIM. All rights reserved.