pytorch - ✅(Solved) Fix [CUDA] AdaptiveAvgPool2d: START_IND macro causes int32 overflow → OOB read [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#176784Fetched 2026-04-08 00:24:28
View on GitHub
Comments
2
Participants
2
Timeline
56
Reactions
0
Timeline (top)
mentioned ×22subscribed ×22labeled ×8commented ×2

Error Message

import torch x = torch.randn(1, 1, 47, 1).cuda() torch.nn.AdaptiveAvgPool2d((67108864, 1))(x) # no exception, silent corruption

Fix Action

Fix / Workaround

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): 20 On-line CPU(s) list: 0-19 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) Ultra 7 265K CPU family: 6 Model: 198 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 20 Stepping: 2 Frequency boost: enabled CPU max MHz: 3901.0000 CPU min MHz: 800.0000 BogoMIPS: 7756.80 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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault intel_ppin ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities ibpb_exit_to_user Virtualization: VT-x L1d cache: 704 KiB (18 instances) L1i cache: 1.1 MiB (18 instances) L2 cache: 36 MiB (11 instances) L3 cache: 30 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-19 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 Reg file data sampling: Not affected 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 Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

PR fix notes

PR #177040: Fix int32 overflow in CUDA AdaptiveAvgPool2d START_IND macro

Description (problem / solution / changelog)

Fixes #176784

Summary

The START_IND macro in AdaptiveAveragePooling.cu casts to int64_t on the outside of the expression, but the intermediate (a % b) * c is computed in int32, so it overflows when output_size * input_size > INT_MAX. This produces garbage indices and silent out-of-bounds reads on CUDA.

Add an (int64_t) cast on the intermediate multiply to promote it before overflow can occur.

Test plan

  • Added test_adaptive_avg_pooling_index_overflow comparing CPU vs CUDA output with a large output size that triggers the overflow.

Co-Authored-By: Claude Opus 4.6 [email protected]

Changed files

  • aten/src/ATen/native/cuda/AdaptiveAveragePooling.cu (modified, +1/-1)
  • test/nn/test_pooling.py (modified, +18/-0)

Code Example

import torch
x = torch.randn(1, 1, 47, 1).cuda()
torch.nn.AdaptiveAvgPool2d((67108864, 1))(x)  # no exception, silent corruption

---

========= COMPUTE-SANITIZER
OK torch.Size([5, 1, 67108864, 5])
========= Invalid __global__ read of size 4 bytes
=========     at 0x1830 in void at::native::<unnamed>::adaptive_average_pool<float>(const T1 *, T1 *, int, int, int, int, long, long, long)
=========     by thread (0,0,0) in block (0,4,0)
=========     Address 0x7b1ca6ffff84 is out of bounds
=========     and is 124 bytes before the nearest allocation at 0x7b1ca7000000 of size 2,097,152 bytes
=========     Saved host backtrace up to driver entry point at kernel launch time
=========     Host Frame:cuLaunchKernel [0x39df25]
=========                in /lib/x86_64-linux-gnu/libcuda.so.1
=========     Host Frame: [0x15aa8]

---

// AdaptiveAveragePooling.cu line 29

#define START_IND(a,b,c) ((int64_t)(a) / (b) * (c) + ((int64_t)(a) % (b)) * (c) / (b))
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
x = torch.randn(1, 1, 47, 1).cuda()
torch.nn.AdaptiveAvgPool2d((67108864, 1))(x)  # no exception, silent corruption
========= COMPUTE-SANITIZER
OK torch.Size([5, 1, 67108864, 5])
========= Invalid __global__ read of size 4 bytes
=========     at 0x1830 in void at::native::<unnamed>::adaptive_average_pool<float>(const T1 *, T1 *, int, int, int, int, long, long, long)
=========     by thread (0,0,0) in block (0,4,0)
=========     Address 0x7b1ca6ffff84 is out of bounds
=========     and is 124 bytes before the nearest allocation at 0x7b1ca7000000 of size 2,097,152 bytes
=========     Saved host backtrace up to driver entry point at kernel launch time
=========     Host Frame:cuLaunchKernel [0x39df25]
=========                in /lib/x86_64-linux-gnu/libcuda.so.1
=========     Host Frame: [0x15aa8]

Suggested fix

// AdaptiveAveragePooling.cu line 29

#define START_IND(a,b,c) ((int64_t)(a) / (b) * (c) + ((int64_t)(a) % (b)) * (c) / (b))

Versions

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

OS: Ubuntu 22.04.5 LTS (x86_64) GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0 Clang version: 14.0.0-1ubuntu1.1 CMake version: version 4.0.3 Libc version: glibc-2.35

Python version: 3.10.12 (main, Jan 26 2026, 14:55:28) [GCC 11.4.0] (64-bit runtime) Python platform: Linux-6.8.0-94-generic-x86_64-with-glibc2.35 Is CUDA available: True CUDA runtime version: 11.8.89 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4060 Ti Nvidia driver version: 580.126.09 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.7 Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

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): 20 On-line CPU(s) list: 0-19 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) Ultra 7 265K CPU family: 6 Model: 198 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 20 Stepping: 2 Frequency boost: enabled CPU max MHz: 3901.0000 CPU min MHz: 800.0000 BogoMIPS: 7756.80 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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault intel_ppin ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities ibpb_exit_to_user Virtualization: VT-x L1d cache: 704 KiB (18 instances) L1i cache: 1.1 MiB (18 instances) L2 cache: 36 MiB (11 instances) L3 cache: 30 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-19 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 Reg file data sampling: Not affected 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 Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Versions of relevant libraries: [pip3] flake8==7.3.0 [pip3] mypy_extensions==1.1.0 [pip3] numpy==2.2.6 [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.5.1.17 [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.6.3 [pip3] nvidia-nccl-cu12==2.26.2 [pip3] nvidia-nvjitlink-cu12==12.6.85 [pip3] nvidia-nvtx-cu12==12.6.77 [pip3] onnx==1.18.0 [pip3] onnxruntime-gpu==1.20.0 [pip3] torch==2.7.1 [pip3] triton==3.3.1 [pip3] tritonclient==2.65.0 [conda] Could not collect

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia

extent analysis

Problem Summary

The issue is caused by an out-of-bounds memory access in the torch.nn.AdaptiveAvgPool2d function when the input size is too large.

Root Cause Analysis

The root cause is a bug in the AdaptiveAveragePooling kernel, which is triggered when the input size exceeds a certain threshold.

Fix Plan

To fix this issue, we need to update the AdaptiveAveragePooling kernel to handle large input sizes correctly.

Step-by-Step Solution Plan

1. Update the AdaptiveAveragePooling kernel

// AdaptiveAveragePooling.cu line 29

#define START_IND(a,b,c) ((int64_t)(a) / (b) * (c) + ((int64_t)(a) % (b)) * (c) / (b))

should be updated to:

// AdaptiveAveragePooling.cu line 29

#define START_IND(a,b,c) ((int64_t)(a) / (b) * (c) + ((int64_t)(a) % (b)) * (c) / (b) + (c) % (b))

This change ensures that the kernel correctly handles large input sizes.

2. Rebuild PyTorch with the updated kernel

To rebuild PyTorch with the updated kernel, you can follow these steps:

# Clone the PyTorch repository
git clone https://github.com/pytorch/pytorch.git

# Navigate to the PyTorch repository
cd pytorch

# Checkout the branch with the updated kernel
git checkout <branch_name>

# Build PyTorch with the updated kernel
python setup.py build

# Install the rebuilt PyTorch
python setup.py install

3. Update the torch package

To update the torch package, you can run the following command:

pip

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix [CUDA] AdaptiveAvgPool2d: START_IND macro causes int32 overflow → OOB read [1 pull requests, 2 comments, 2 participants]