Repository: pytorch
Issue: Floating point exception (core dumped) in `scaled_dot_product_attention`
Labels: module: crash,module: error checking,triaged,module: edge cases,module: empty tensor,topic: fuzzer,module: sdpa

### 🐛 Describe the bug

Under specific inputs, `scaled_dot_product_attention` triggered a crash.
```python
import torch

query = torch.full((0,1,7,2,8,9,0,0,3,), 0, dtype=torch.half)
key = torch.full((0,0,7,), 0, dtype=torch.half)
value = torch.full((0,0,7,0,9,9,2,8,), 0, dtype=torch.half)
attn_mask = None
dropout_p = 9.88131e-324
is_causal = False
scale = 9.0072e+15
enable_gqa = True

torch.nn.functional.scaled_dot_product_attention(query, key, value, attn_mask=attn_mask, dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa)
```
Output:
```
Floating point exception (core dumped)
```
ASAN Report
```
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3684861==ERROR: AddressSanitizer: FPE on unknown address 0x7f2e2648481a (pc 0x7f2e2648481a bp 0x7fffafe0b360 sp 0x7fffafe0b330 T0)
    #0 0x7f2e2648481a in decltype (((forward<long&>)({parm#1}))%((forward<long&>)({parm#2}))) std::modulus<void>::operator()<long&, long&>(long&, long&) const (/mnt/pytorch-2.5.0/torch/lib/libc10.so+0x11081a)
    #1 0x7f2e2646cec1 in c10::SymInt::operator%(c10::SymInt const&) const /mnt/pytorch-2.5.0/c10/core/SymInt.cpp:69
    #2 0x7f2e3a1f93a1 in pre_process_group_query_attention_input /mnt/pytorch-2.5.0/aten/src/ATen/native/transformers/attention.cpp:645
    #3 0x7f2e3a1fdc79 in at::native::_scaled_dot_product_attention_math(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool) /mnt/pytorch-2.5.0/aten/src/ATen/native/transformers/attention.cpp:861
    #4 0x7f2e3da7a8de in wrapper_CompositeImplicitAutograd___scaled_dot_product_attention_math /mnt/pytorch-2.5.0/build/aten/src/ATen/RegisterCompositeImplicitAutograd.cpp:6911
    #5 0x7f2e3ddc2f1f in operator() /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/WrapFunctionIntoFunctor.h:13
    #6 0x7f2e3ddc2f1f in call /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:468
    #7 0x7f2e3c761bd2 in std::tuple<at::Tensor, at::Tensor> c10::callUnboxedKernelFunction<std::tuple<at::Tensor, at::Tensor>, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool>(void*, c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double&&, bool&&, std::optional<at::Tensor> const&, std::optional<double>&&, bool&&) /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/KernelFunction_impl.h:53
    #8 0x7f2e3c4f0edf in std::tuple<at::Tensor, at::Tensor> c10::KernelFunction::call<std::tuple<at::Tensor, at::Tensor>, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool>(c10::OperatorHandle const&, c10::DispatchKeySet, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool) const /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/KernelFunction_impl.h:105
    #9 0x7f2e3c4f0edf in std::tuple<at::Tensor, at::Tensor> c10::Dispatcher::call<std::tuple<at::Tensor, at::Tensor>, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool>(c10::TypedOperatorHandle<std::tuple<at::Tensor, at::Tensor> (at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool)> const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:698
    #10 0x7f2e3c4f0edf in c10::TypedOperatorHandle<std::tuple<at::Tensor, at::Tensor> (at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool)>::call(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:531
    #11 0x7f2e3c4f0edf in at::_ops::_scaled_dot_product_attention_math::call(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool) /mnt/pytorch-2.5.0/build/aten/src/ATen/Operators_4.cpp:11567
    #12 0x7f2e3a2072cc in at::_scaled_dot_product_attention_math(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<at::Tensor> const&, std::optional<double>, bool) /mnt/pytorch-2.5.0/build/aten/src/ATen/ops/_scaled_dot_product_attention_math.h:27
    #13 0x7f2e3a1fc06a in at::native::scaled_dot_product_attention(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool) /mnt/pytorch-2.5.0/aten/src/ATen/native/transformers/attention.cpp:776
    #14 0x7f2e3da7a71a in wrapper_CompositeImplicitAutograd__scaled_dot_product_attention /mnt/pytorch-2.5.0/build/aten/src/ATen/RegisterCompositeImplicitAutograd.cpp:6904
    #15 0x7f2e3ddc2677 in operator() /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/WrapFunctionIntoFunctor.h:13
    #16 0x7f2e3ddc2677 in call /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:468
    #17 0x7f2e3bbaf90f in at::Tensor c10::callUnboxedKernelFunction<at::Tensor, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool>(void*, c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double&&, bool&&, std::optional<double>&&, bool&&) /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/KernelFunction_impl.h:53
    #18 0x7f2e3b7070b9 in at::Tensor c10::KernelFunction::call<at::Tensor, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool>(c10::OperatorHandle const&, c10::DispatchKeySet, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool) const /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/KernelFunction_impl.h:105
    #19 0x7f2e3b7070b9 in at::Tensor c10::Dispatcher::call<at::Tensor, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool>(c10::TypedOperatorHandle<at::Tensor (at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool)> const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:698
    #20 0x7f2e3b7070b9 in c10::TypedOperatorHandle<at::Tensor (at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool)>::call(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:531
    #21 0x7f2e3b7070b9 in at::_ops::scaled_dot_product_attention::call(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool) /mnt/pytorch-2.5.0/build/aten/src/ATen/Operators_2.cpp:14255
    #22 0x7f2e7f7bb8a3 in at::scaled_dot_product_attention(at::Tensor const&, at::Tensor const&, at::Tensor const&, std::optional<at::Tensor> const&, double, bool, std::optional<double>, bool) /mnt/pytorch-2.5.0/build/aten/src/ATen/ops/scaled_dot_product_attention.h:27
    #23 0x7f2e7f78d4f1 in operator() /mnt/pytorch-2.5.0/torch/csrc/autograd/generated/python_nn_functions.cpp:2689
    #24 0x7f2e7f78dc51 in THPVariable_scaled_dot_product_attention /mnt/pytorch-2.5.0/torch/csrc/autograd/generated/python_nn_functions.cpp:2691
    #25 0x56abf2 in cfunction_call /usr/local/src/conda/python-3.13.0/Objects/methodobject.c:540
    #26 0x5341f3 in _PyObject_MakeTpCall /usr/local/src/conda/python-3.13.0/Objects/call.c:242
    #27 0x55292a in _PyEval_EvalFrameDefault /usr/local/src/conda/python-3.13.0/Python/generated_cases.c.h:1502
    #28 0x60902d in PyEval_EvalCode /usr/local/src/conda/python-3.13.0/Python/ceval.c:596
    #29 0x62eedc in run_eval_code_obj /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:1323
    #30 0x629d9c in run_mod /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:1408
    #31 0x64888f in pyrun_file /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:1241
    #32 0x6473fa in _PyRun_SimpleFileObject /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:490
    #33 0x64711a in _PyRun_AnyFileObject /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:77
    #34 0x640b66 in pymain_run_file_obj /usr/local/src/conda/python-3.13.0/Modules/main.c:409
    #35 0x640b66 in pymain_run_file /usr/local/src/conda/python-3.13.0/Modules/main.c:428
    #36 0x640b66 in pymain_run_python /usr/local/src/conda/python-3.13.0/Modules/main.c:696
    #37 0x640b66 in Py_RunMain /usr/local/src/conda/python-3.13.0/Modules/main.c:775
    #38 0x5f9508 in Py_BytesMain /usr/local/src/conda/python-3.13.0/Modules/main.c:829
    #39 0x7f2e87e21d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f)
    #40 0x7f2e87e21e3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f)
    #41 0x5f885c  (/mnt/anaconda3/envs/pytorch-2.3-asan/bin/python3.13+0x5f885c)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: FPE (/mnt/pytorch-2.5.0/torch/lib/libc10.so+0x11081a) in decltype (((forward<long&>)({parm#1}))%((forward<long&>)({parm#2}))) std::modulus<void>::operator()<long&, long&>(long&, long&) const
==3684861==ABORTING
```

### Versions

PyTorch version: 2.5.0a0+git32f585d
Is debug build: False
CUDA used to build PyTorch: None
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.13.0 | packaged by Anaconda, Inc. | (main, Oct 7 2024, 21:29:38) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-124-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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, 48 bits virtual
Byte Order: Little Endian
CPU(s): 80
On-line CPU(s) list: 0-79
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Gold 5218R CPU @ 2.10GHz
CPU family: 6
Model: 85
Thread(s) per core: 2
Core(s) per socket: 20
Socket(s): 2
Stepping: 7
CPU max MHz: 4000.0000
CPU min MHz: 800.0000
BogoMIPS: 4200.00
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 cdp_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 mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 1.3 MiB (40 instances)
L1i cache: 1.3 MiB (40 instances)
L2 cache: 40 MiB (40 instances)
L3 cache: 55 MiB (2 instances)
NUMA node(s): 2
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
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
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; Enhanced IBRS
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 / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==2.1.3
[pip3] torch==2.5.0a0+git32f585d
[conda] numpy 2.1.3 pypi_0 pypi
[conda] torch 2.5.0a0+git32f585d pypi_0 pypi

cc @malfet @jbschlosser @bhosmer @cpuhrsch @erichan1 @drisspg @mikaylagawarecki