Skip to content

Flex Attention Silently Incorrect with bfloat16 and Masks #163588

@zaptrem

Description

@zaptrem

PyTorch flex_attention Numerical Instability with bfloat16 and Masks

🐛 Describe the bug

flex_attention produces significantly different results than F.scaled_dot_product_attention when using bfloat16 dtype with cross-attention masks with significant masked padding. The error grows to >3% of the output magnitude, which I believe is beyond acceptable numerical precision differences.

Key findings:

  • Bug triggers: When attending to ≤58% of positions with bfloat16
  • Bug disappears: When attending to ≥59% of positions
  • Error magnitude: Up to 3.4% relative error (vs 0.3% expected tolerance)
  • Does NOT occur with float32: Same code with float32 has <0.001% error
  • Comparison baseline: Physical array slicing with F.scaled_dot_product_attention provides ground truth

This affects production models using bfloat16 for memory efficiency, particularly in autoregressive generation scenarios where cross-attention masks are commonly used to handle variable-length conditioning.

Minimal Reproduction

import torch
from torch.nn.attention.flex_attention import create_block_mask, flex_attention
import torch.nn.functional as F

B, H, Q, D, KV = 1, 1, 1, 64, 1000

# Test showing error threshold: masking <=58% causes bug, >=59% is OK
print("PyTorch flex_attention bfloat16 bug demonstration:")
print("-" * 50)

for mask_amount in [580, 590]:
    mask = create_block_mask(
        lambda b, h, q, kv: kv < mask_amount,
        B, H, Q, KV, "cuda"
    )

    torch.manual_seed(0)
    q = torch.randn(B, H, Q, D, dtype=torch.bfloat16, device="cuda") * 5
    k = torch.randn(B, H, KV, D, dtype=torch.bfloat16, device="cuda") * 5
    v = torch.randn(B, H, KV, D, dtype=torch.bfloat16, device="cuda") * 5

    flex_out = flex_attention(q, k, v, block_mask=mask)
    ref_out = F.scaled_dot_product_attention(q, k[:, :, :mask_amount], v[:, :, :mask_amount])

    error = (flex_out - ref_out).abs().max() / ref_out.abs().max()
    status = "BUG!" if error > 0.01 else "OK"
    print(f"Mask {mask_amount}/{KV} ({mask_amount/KV:.0%}): Error {error:.1%} ({status})")

print("\nBug triggers when masking ≤58% of positions with bfloat16")

Output:

PyTorch flex_attention bfloat16 bug demonstration:
--------------------------------------------------
Mask 580/1000 (58%): Error 3.4% (BUG!)
Mask 590/1000 (59%): Error 0.3% (OK)

Bug triggers when masking ≤58% of positions with bfloat16

Expected behavior

flex_attention with a mask should produce nearly identical results to F.scaled_dot_product_attention with physical array slicing (within normal bfloat16 precision tolerance of <1%).

Actual behavior

Large numerical divergence (>3% error) when using bfloat16 with masks that exclude more than ~42% of positions.

Versions

Collecting environment information...
PyTorch version: 2.8.0+cu126
Is debug build: False
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.11.13 (main, Jun  4 2025, 08:57:29) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-1023-aws-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-SXM4-80GB
Nvidia driver version: 535.183.01
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      46 bits physical, 57 bits virtual
Byte Order:                         Little Endian
CPU(s):                             24
On-line CPU(s) list:                0-23
Vendor ID:                          GenuineIntel
Model name:                         Intel Xeon Processor (SapphireRapids)
CPU family:                         6
Model:                              143
Thread(s) per core:                 1
Core(s) per socket:                 24
Socket(s):                          1
Stepping:                           4
BogoMIPS:                           4200.00
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx_vnni avx512_bf16 wbnoinvd arat vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b fsrm md_clear serialize tsxldtrk avx512_fp16 arch_capabilities
Virtualization:                     VT-x
Hypervisor vendor:                  KVM
Virtualization type:                full
L1d cache:                          768 KiB (24 instances)
L1i cache:                          768 KiB (24 instances)
L2 cache:                           96 MiB (24 instances)
L3 cache:                           16 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-23
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Unknown: No mitigations
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI Syscall hardening, KVM SW loop
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.8.0+cu126
[pip3] torchaudio==2.8.0+cu126
[pip3] torchvision==0.23.0+cu126
[pip3] triton==3.4.0
[pip3] nvidia-cublas-cu12==12.6.4.1
[pip3] nvidia-cuda-cupti-cu12==12.6.80
[pip3] nvidia-cuda-nvrtc-cu12==12.6.77
[pip3] nvidia-cuda-runtime-cu12==12.6.77
[pip3] nvidia-cudnn-cu12==9.10.2.21
[pip3] nvidia-cufft-cu12==11.3.0.4
[pip3] nvidia-curand-cu12==10.3.7.77
[pip3] nvidia-cusolver-cu12==11.7.1.2
[pip3] nvidia-cusparse-cu12==12.5.4.2
[pip3] nvidia-cusparselt-cu12==0.7.1
[pip3] nvidia-nccl-cu12==2.27.3
[pip3] nvidia-nvjitlink-cu12==12.6.85
[pip3] nvidia-nvtx-cu12==12.6.77
[conda] Could not collect

cc @chauhang @penguinwu @zou3519 @ydwu4 @bdhirsh @Chillee @drisspg @yanboliang @BoyuanFeng

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: flex attentionmodule: higher order operatorstorch.cond and similarmodule: pt2-dispatcherPT2 dispatcher-related issues (e.g., aotdispatch, functionalization, faketensor, custom-op,oncall: pt2triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions