Repository: pytorch
Issue: FlopsProfiler double counts sdpa flops
Labels: high priority,triaged,module: regression,module: flop counter,module: sdpa

### 🐛 Describe the bug

We found that the flops counter is reporting incorrect flops number for sdpa operations. 

This issue is not in torch 2.4+cu121 release. 

Repro code:
```
from torch.utils.flop_counter import sdpa_flop, mm_flop
import torch.nn.functional as F
from torch.utils.flop_counter import FlopCounterMode
import torch

batch = 2
headdim = 4
nheads = 5
seqlen = 9
causal = False

### Expected Behavior
q = torch.randn(seqlen, headdim).cuda().bfloat16()
k = torch.randn(headdim, seqlen).cuda().bfloat16()
target_flops = mm_flop(q.shape, k.shape)
flops_counter = FlopCounterMode(display=False)
flops_counter.__enter__()
q @ k
flops_counter.__exit__(None, None, None)
assert(flops_counter.get_total_flops() == target_flops), f"{flops_counter.get_total_flops()} != {target_flops}"

### SDPA double counts
q = torch.randn(batch, nheads, seqlen, headdim).cuda().bfloat16()
k = torch.randn(batch, nheads, seqlen, headdim).cuda().bfloat16()
v = torch.randn(batch, nheads, seqlen, headdim).cuda().bfloat16()
target_flops = sdpa_flop(q.shape, k.shape, v.shape)
flops_counter = FlopCounterMode(display=False)
flops_counter.__enter__()
F.scaled_dot_product_attention(q, k, v)
flops_counter.__exit__(None, None, None)
assert(flops_counter.get_total_flops() == target_flops),f"{flops_counter.get_total_flops()} != {target_flops}"
```

### Versions

PyTorch version: 2.5.0.dev20240808+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
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: version 3.22.1
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.146+-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.131
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA H100 80GB HBM3
GPU 1: NVIDIA H100 80GB HBM3
GPU 2: NVIDIA H100 80GB HBM3
GPU 3: NVIDIA H100 80GB HBM3
GPU 4: NVIDIA H100 80GB HBM3
GPU 5: NVIDIA H100 80GB HBM3
GPU 6: NVIDIA H100 80GB HBM3
GPU 7: NVIDIA H100 80GB HBM3

Nvidia driver version: 535.161.07
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.3
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:                      52 bits physical, 57 bits virtual
Byte Order:                         Little Endian
CPU(s):                             208
On-line CPU(s) list:                0-207
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Xeon(R) Platinum 8481C CPU @ 2.70GHz
CPU family:                         6
Model:                              143
Thread(s) per core:                 2
Core(s) per socket:                 52
Socket(s):                          2
Stepping:                           8
BogoMIPS:                           5399.99
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 nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rtm avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx_vnni avx512_bf16 arat avx512vbmi umip avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid cldemote movdiri movdir64b fsrm md_clear serialize avx512_fp16 arch_capabilities
Hypervisor vendor:                  KVM
Virtualization type:                full
L1d cache:                          4.9 MiB (104 instances)
L1i cache:                          3.3 MiB (104 instances)
L2 cache:                           208 MiB (104 instances)
L3 cache:                           210 MiB (2 instances)
NUMA node(s):                       2
NUMA node0 CPU(s):                  0-51,104-155
NUMA node1 CPU(s):                  52-103,156-207
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:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: 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

Versions of relevant libraries:
[pip3] facenet-pytorch==2.5.1
[pip3] guided-filter-pytorch==3.7.5
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.0
[pip3] onnx==1.14.0
[pip3] onnxruntime==1.15.1
[pip3] open-clip-torch==2.22.0
[pip3] pytorch-lightning==2.2.4
[pip3] pytorch-triton==3.0.0+dedb7bdf33
[pip3] pytorch3d==0.7.5
[pip3] torch==2.5.0.dev20240808+cu124
[pip3] torch-fidelity==0.3.0
[pip3] torch-tb-profiler==0.4.1
[pip3] torchao-nightly==2024.8.12+cu124
[pip3] torchaudio==2.4.0.dev20240808+cu124
[pip3] torchlibrosa==0.0.7
[pip3] torchmetrics==1.3.1
[pip3] torchray==1.0.0.2
[pip3] torchvision==0.20.0.dev20240808+cu124
[conda] Could not collect
bash: Comment: command not found

cc @ezyang @gchanan @zou3519 @kadeng @msaroufim @jbschlosser @bhosmer @cpuhrsch @erichan1 @drisspg @mikaylagawarecki