Skip to main content

News

Optimizing PyTorch FlexAttention for AMD Instinct MI350X GPUs

AMD's tuning of PyTorch FlexAttention improves fp16 throughput by up to 3x on MI350X GPUs, with significant application benefits in AI workloads.

X LinkedIn
Optimizing PyTorch FlexAttention for AMD Instinct MI350X GPUs

AMD has announced significant performance improvements for PyTorch's FlexAttention on its Instinct MI350X GPUs. By tuning two key configuration defaults, the company achieved a 2.1x to 3.0x increase in fp16 forward throughput across measured workloads. These optimizations involve reducing the wavefront count from eight to four and increasing the software pipeline depth from one stage to two, while maintaining the same algorithm and user-facing API.

Technical Adjustments and Performance Gains

The changes streamline how FlexAttention processes attention variants, which are expressed as Python functions and compiled into fused kernels by torch.compile. On the MI350X GPUs, each compute unit's 160 KB of local data storage (LDS) is better utilized by reducing redundant matrix work and optimizing memory addressing. For instance, the wavefront count adjustment alone improved fp16 throughput by 1.86x to 2.63x, while the increased pipeline depth further enhanced performance by 1.10x to 1.18x on bf16 workloads.

These optimizations also extend to application-level benefits. Researchers at MIT's Computer Science and Artificial Intelligence Laboratory (CSAIL) leveraged FlexAttention in their Mirror: Self-Improving Video-to-Action Models. Using 16 MI350X GPUs, they fine-tuned Minecraft Diamond video planners, achieving a 1.69x reduction in attention latency (from 27.18 ms to 16.05 ms) and a 26% increase in planner throughput (from 15.8 to 19.9 video-seconds per second), excluding VAE and text processing components.

Underlying Mechanisms

FlexAttention's forward kernel processes query tiles by iterating through key/value blocks, computing QKᵀ, applying score modifications and masks, and updating online softmax values. The optimized configuration uses four wavefronts for both QKᵀ and PV matrix products, eliminating redundant memory conversions and reducing static instruction counts. For example, static ds_write sites fell from 72 to 9, while weighted matrix instruction counts dropped from 384 to 256 with the new settings.

Additionally, increasing the pipeline depth allows Triton to overlap computation with memory loads, enhancing throughput. However, resource tradeoffs, such as increased VGPR allocation and shared memory usage, slightly reduced performance in specific configurations, such as fp16 with eight wavefronts. These findings highlight the importance of evaluating pipeline depth and wavefront count as a combined tuning space.

Impact on Memory Addressing

To further optimize performance, AMD introduced pointer-range annotations for HIP tensor arguments, enabling Triton to canonicalize pointers and lower eligible accesses to buffer operations. This adjustment improved throughput by 1.31x for causal attention and 1.04x for unmasked attention in specific configurations.

Overall, these tuning efforts demonstrate AMD's commitment to maximizing the performance of AI workloads on its Instinct MI350X GPUs, providing developers with a more efficient and scalable solution for advanced machine learning applications.

Sign in to save this article Free account: build your research collection and return to read it anytime.