Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlockDiagonalAttention computes NaN gradients, when using bfloat16 and deterministic torch #1025

Open
nimia opened this issue Apr 11, 2024 · 0 comments

Comments

@nimia
Copy link

nimia commented Apr 11, 2024

馃悰 Bug

When using block diagonal attention, with bfloat16 as the datatype and when setting torch to deterministic mode, some of the computed gradients are NaN.

Command

Save the code below into reproduce.py, then run:

CUBLAS_WORKSPACE_CONFIG=:4096:8 python3 reproduce.py

To Reproduce

import torch
import torch.nn as nn
import xformers.ops as xops
from xformers.ops import fmha

# Make sure we only use deterministic algorithms for the smallest part of the
# code that reproduces the issue.
torch.use_deterministic_algorithms(False, warn_only=True)
 
num_heads = 12
dim = 64*num_heads
qkv_bias = False
qkv_layer = nn.Linear(dim, dim * 3, bias=qkv_bias).cuda()
lens = [64, 60]
x = torch.rand(1, sum(lens), dim).cuda()

with torch.cuda.amp.autocast(
    enabled=True, dtype=torch.bfloat16
):
    B, N, C = x.shape
    qkv = (
        qkv_layer(x)
        .reshape(B, N, 3, num_heads, C // num_heads)
        .permute(2, 0, 1, 3, 4)
    )
    q, k, v = qkv.unbind(0)

    attn_mask = fmha.BlockDiagonalMask.from_seqlens(lens)

    torch.use_deterministic_algorithms(True, warn_only=True)
    y = xops.memory_efficient_attention(q, k, v, attn_bias=attn_mask)
    ys = y.sum()
    ys.backward()
    torch.use_deterministic_algorithms(False, warn_only=True)
    print(qkv_layer.weight.grad.isnan().any())

Environment

PyTorch version: 2.2.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.26.3
Libc version: glibc-2.31

Python version: 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:26:04) [GCC 10.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-67-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A40
Nvidia driver version: 525.85.12
cuDNN version: Could not collect
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
Byte Order: Little Endian
Address sizes: 46 bits physical, 57 bits virtual
CPU(s): 96
On-line CPU(s) list: 0-95
Thread(s) per core: 2
Core(s) per socket: 24
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 106
Model name: Intel(R) Xeon(R) Gold 6342 CPU @ 2.80GHz
Stepping: 6
CPU MHz: 2408.766
BogoMIPS: 5600.00
Virtualization: VT-x
L1d cache: 2.3 MiB
L1i cache: 1.5 MiB
L2 cache: 60 MiB
L3 cache: 72 MiB
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] torch==2.2.1
[pip3] torchaudio==2.0.2
[pip3] torchdata==0.6.1
[pip3] torchmetrics==0.10.3
[pip3] torchtext==0.15.2
[pip3] torchtnt==0.1.0
[pip3] torchvision==0.15.2
[pip3] triton==2.2.0
[conda] blas 1.0 mkl conda-forge
[conda] libblas 3.9.0 16_linux64_mkl conda-forge
[conda] libcblas 3.9.0 16_linux64_mkl conda-forge
[conda] liblapack 3.9.0 16_linux64_mkl conda-forge
[conda] liblapacke 3.9.0 16_linux64_mkl conda-forge
[conda] mkl 2022.1.0 h84fe81f_915 https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] mkl-include 2023.1.0 h84fe81f_48680 conda-forge
[conda] numpy 1.26.4 pypi_0 pypi
[conda] pytorch-cuda 11.8 h7e8668a_3 https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] pytorch-mutex 1.0 cuda https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] torch 2.2.1 pypi_0 pypi
[conda] torchaudio 2.0.2 py310_cu118 https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] torchdata 0.6.1 py310 https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] torchmetrics 0.10.3 pypi_0 pypi
[conda] torchtext 0.15.2 py310 https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] torchtnt 0.1.0 pypi_0 pypi
[conda] torchvision 0.15.2 py310_cu118 https://aws-ml-conda-ec2.s3.us-west-2.amazonaws.com
[conda] triton 2.2.0 pypi_0 pypi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant