Fix Flash Attention precision loss on RDNA3 (gfx11xx) #2189
+89
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Flash Attention precision loss on RDNA3 (gfx11xx)
Problem
Flash Attention on RDNA3 (gfx1100/Navi31) GPUs was failing precision tests with error ratios exceeding 2.0x compared to PyTorch reference. The issue manifested in
test_flash_attn_triton_amd.pytests.Root Cause
The WMMA instructions on RDNA3 accumulate in FP32, but the intermediate GEMM0 output buffer was being allocated as FP16 (matching the input type), and preSoftmaxBody immediately extends back to FP32 via
arith.extffor softmax. This caused an unnecessary FP32→FP16→FP32 roundtrip. This roundtrip loses precision.Solution
In
GridwiseGemmToBlockwise.cpp, detect when thepreSoftmaxBodycontains an immediatearith.extfoperation on the GEMM0 output. When detected:gemm0OutBufferin FP32 (the softmax precision type) instead of FP16linalg.genericblock argument types to match the new buffer typearith.extfops (FP32→FP32) with their inputThis keeps the WMMA FP32 result in FP32 until after softmax, eliminating the precision loss.
NOTE: THIS IS STILL JUST A DRAFT TO CONFIRM THAT IT FIXES A PROBLEM.