Repository: pytorch
Issue: SEGV in `torch.native_batch_norm`
Labels: module: error checking,triaged,module: sanitizers,module: edge cases

### 🐛 Describe the bug

Under specific inputs, `torch.native_batch_norm` triggered a crash.
```python
import torch

input = torch.full((8, 3, 7, 3, 7,), 0, dtype=torch.float32)
weight = None
bias = None
running_mean = None
running_var = None
training = False
momentum = 2.71828
eps = 9.87654e+09

output = torch.native_batch_norm(input, weight, bias, running_mean, running_var, training, momentum, eps)
```
ASAN report:
```
AddressSanitizer:DEADLYSIGNAL
=================================================================
==2761360==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fa9e37f6d2d bp 0x7ffc5f3c1fb0 sp 0x7ffc5f3c1fa0 T0)
==2761360==The signal is caused by a READ memory access.
==2761360==Hint: address points to the zero page.
    #0 0x7fa9e37f6d2d in at::TensorAccessor<float const, 1ul, at::DefaultPtrTraits, long>::operator[](long) /mnt/pytorch-2.5.0/aten/src/ATen/core/TensorAccessor.h:104
    #1 0x7fa9f5674604 in batch_norm_cpu_collect_linear_and_constant_terms<float, float> /mnt/pytorch-2.5.0/aten/src/ATen/native/cpu/batch_norm_kernel.cpp:62
    #2 0x7fa9f5644dba in batch_norm_cpu_contiguous_impl<float> /mnt/pytorch-2.5.0/aten/src/ATen/native/cpu/batch_norm_kernel.cpp:89
    #3 0x7fa9f56315ae in operator() /mnt/pytorch-2.5.0/aten/src/ATen/native/cpu/batch_norm_kernel.cpp:1333
    #4 0x7fa9f5632aa5 in operator() /mnt/pytorch-2.5.0/aten/src/ATen/native/cpu/batch_norm_kernel.cpp:1333
    #5 0x7fa9f56358e3 in batch_norm_cpu_kernel /mnt/pytorch-2.5.0/aten/src/ATen/native/cpu/batch_norm_kernel.cpp:1333
    #6 0x7fa9e3a936c2 in void at::native::DispatchStub<void (*)(at::Tensor&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, bool, double), at::native::batch_norm_cpu_stub_DECLARE_DISPATCH_type>::operator()<at::Tensor&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, bool&, double&>(c10::DeviceType, at::Tensor&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, bool&, double&) /mnt/pytorch-2.5.0/aten/src/ATen/native/DispatchStub.h:233
    #7 0x7fa9e3a40c76 in std::tuple<at::Tensor, at::Tensor, at::Tensor> at::native::batch_norm_cpu_transform_input_template<float, float>(at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, at::Tensor const&, bool, double, at::Tensor&) /mnt/pytorch-2.5.0/aten/src/ATen/native/Normalization.cpp:151
    #8 0x7fa9e3a1c810 in operator() /mnt/pytorch-2.5.0/aten/src/ATen/native/Normalization.cpp:775
    #9 0x7fa9e3a21376 in operator() /mnt/pytorch-2.5.0/aten/src/ATen/native/Normalization.cpp:775
    #10 0x7fa9e3a22a66 in at::native::batch_norm_cpu_out(at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double, at::Tensor&, at::Tensor&, at::Tensor&) /mnt/pytorch-2.5.0/aten/src/ATen/native/Normalization.cpp:775
    #11 0x7fa9e3a25eb0 in at::native::batch_norm_cpu(at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) /mnt/pytorch-2.5.0/aten/src/ATen/native/Normalization.cpp:850
    #12 0x7fa9e78dccad in wrapper_CPU__native_batch_norm /mnt/pytorch-2.5.0/build/aten/src/ATen/RegisterCPU.cpp:8834
    #13 0x7fa9e7d21e21 in operator() /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/WrapFunctionIntoFunctor.h:13
    #14 0x7fa9e7d21e21 in call /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:468
    #15 0x7fa9e624d370 in std::tuple<at::Tensor, at::Tensor, at::Tensor> c10::callUnboxedKernelFunction<std::tuple<at::Tensor, at::Tensor, at::Tensor>, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double>(void*, c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool&&, double&&, double&&) (/mnt/pytorch-2.5.0/torch/lib/libtorch_cpu.so+0x14b29370)
    #16 0x7fa9e5fc0724 in std::tuple<at::Tensor, at::Tensor, at::Tensor> c10::Dispatcher::redispatch<std::tuple<at::Tensor, at::Tensor, at::Tensor>, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double>(c10::TypedOperatorHandle<std::tuple<at::Tensor, at::Tensor, at::Tensor> (at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double)> const&, c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) const (/mnt/pytorch-2.5.0/torch/lib/libtorch_cpu.so+0x1489c724)
    #17 0x7fa9e5cd21dc in c10::TypedOperatorHandle<std::tuple<at::Tensor, at::Tensor, at::Tensor> (at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double)>::redispatch(c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:536
    #18 0x7fa9e5cd21dc in at::_ops::native_batch_norm::redispatch(c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) /mnt/pytorch-2.5.0/build/aten/src/ATen/Operators_1.cpp:4163
    #19 0x7fa9ef4c3c60 in at::redispatch::native_batch_norm(c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) /mnt/pytorch-2.5.0/build/aten/src/ATen/RedispatchFunctions.h:5452
    #20 0x7fa9ef2c411f in operator() /mnt/pytorch-2.5.0/torch/csrc/autograd/generated/VariableType_1.cpp:11314
    #21 0x7fa9ef2c4fab in native_batch_norm /mnt/pytorch-2.5.0/torch/csrc/autograd/generated/VariableType_1.cpp:11315
    #22 0x7fa9ef442f2c in operator() /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/WrapFunctionIntoFunctor.h:13
    #23 0x7fa9ef442f2c in call /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:485
    #24 0x7fa9e624d370 in std::tuple<at::Tensor, at::Tensor, at::Tensor> c10::callUnboxedKernelFunction<std::tuple<at::Tensor, at::Tensor, at::Tensor>, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double>(void*, c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool&&, double&&, double&&) (/mnt/pytorch-2.5.0/torch/lib/libtorch_cpu.so+0x14b29370)
    #25 0x7fa9e5cd1823 in std::tuple<at::Tensor, at::Tensor, at::Tensor> c10::KernelFunction::call<std::tuple<at::Tensor, at::Tensor, at::Tensor>, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double>(c10::OperatorHandle const&, c10::DispatchKeySet, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) const /mnt/pytorch-2.5.0/aten/src/ATen/core/boxing/KernelFunction_impl.h:105
    #26 0x7fa9e5cd1823 in std::tuple<at::Tensor, at::Tensor, at::Tensor> c10::Dispatcher::call<std::tuple<at::Tensor, at::Tensor, at::Tensor>, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double>(c10::TypedOperatorHandle<std::tuple<at::Tensor, at::Tensor, at::Tensor> (at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double)> const&, at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:698
    #27 0x7fa9e5cd1823 in c10::TypedOperatorHandle<std::tuple<at::Tensor, at::Tensor, at::Tensor> (at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double)>::call(at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) const /mnt/pytorch-2.5.0/aten/src/ATen/core/dispatch/Dispatcher.h:531
    #28 0x7fa9e5cd1823 in at::_ops::native_batch_norm::call(at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) /mnt/pytorch-2.5.0/build/aten/src/ATen/Operators_1.cpp:4156
    #29 0x7faa2a3d580c in at::native_batch_norm(at::Tensor const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, std::optional<at::Tensor> const&, bool, double, double) (/mnt/pytorch-2.5.0/torch/lib/libtorch_python.so+0x22ec80c)
    #30 0x7faa2a2d8b53 in operator() /mnt/pytorch-2.5.0/torch/csrc/autograd/generated/python_torch_functions_0.cpp:5324
    #31 0x7faa2a2d9703 in THPVariable_native_batch_norm /mnt/pytorch-2.5.0/torch/csrc/autograd/generated/python_torch_functions_0.cpp:5326
    #32 0x56abf2 in cfunction_call /usr/local/src/conda/python-3.13.0/Objects/methodobject.c:540
    #33 0x5341f3 in _PyObject_MakeTpCall /usr/local/src/conda/python-3.13.0/Objects/call.c:242
    #34 0x549ece in _PyEval_EvalFrameDefault /usr/local/src/conda/python-3.13.0/Python/generated_cases.c.h:813
    #35 0x60902d in PyEval_EvalCode /usr/local/src/conda/python-3.13.0/Python/ceval.c:596
    #36 0x62eedc in run_eval_code_obj /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:1323
    #37 0x629d9c in run_mod /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:1408
    #38 0x64888f in pyrun_file /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:1241
    #39 0x6473fa in _PyRun_SimpleFileObject /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:490
    #40 0x64711a in _PyRun_AnyFileObject /usr/local/src/conda/python-3.13.0/Python/pythonrun.c:77
    #41 0x640b66 in pymain_run_file_obj /usr/local/src/conda/python-3.13.0/Modules/main.c:409
    #42 0x640b66 in pymain_run_file /usr/local/src/conda/python-3.13.0/Modules/main.c:428
    #43 0x640b66 in pymain_run_python /usr/local/src/conda/python-3.13.0/Modules/main.c:696
    #44 0x640b66 in Py_RunMain /usr/local/src/conda/python-3.13.0/Modules/main.c:775
    #45 0x5f9508 in Py_BytesMain /usr/local/src/conda/python-3.13.0/Modules/main.c:829
    #46 0x7faa32df2d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f)
    #47 0x7faa32df2e3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f)
    #48 0x5f885c  (/mnt/anaconda3/envs/pytorch-2.3-asan/bin/python3.13+0x5f885c)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /mnt/pytorch-2.5.0/aten/src/ATen/core/TensorAccessor.h:104 in at::TensorAccessor<float const, 1ul, at::DefaultPtrTraits, long>::operator[](long)
==2761360==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