[XPU] Fix int32 overflow in adaptive_avg_pool2d backward for large tensors#4337
Open
sgurwinderr wants to merge 1 commit into
Open
[XPU] Fix int32 overflow in adaptive_avg_pool2d backward for large tensors#4337sgurwinderr wants to merge 1 commit into
sgurwinderr wants to merge 1 commit into
Conversation
…nsors AdaptiveAvgPool2dBwdKernelFunctor and AdaptiveAvgPool2dBwdSLMKernelFunctor stored the input dims (ib_/ic_/ih_/iw_/oh_/ow_) and global_range_ as 32-bit int, so numel_ = ib_ * ic_ * ih_ * iw_ was computed in 32-bit and overflowed for tensors with numel > 2^31. When the wrapped product is negative, the grad-input loop 'for (int64_t i = gi; i < numel_; ...)' runs zero iterations and grad_input is left entirely unwritten. Widen the dimension and range members to int64_t so the product is computed in 64-bit. The accessors are already PackedTensorAccessor64, so no addressing changes are needed. Mirrors the fix pattern used for max_pool3d (intel#3632). Verified on Arc Pro B60 (from-source XPU build): with input (1,4,32768,16385) (numel 2,147,614,720 > 2^31), adaptive_avg_pool2d backward wrote 0/numel elements before the fix (numel_ wrapped to -2,147,352,576) and all elements correctly after. Fixes intel#4327. Signed-off-by: sgurwinderr <sgurwinderr@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
adaptive_avg_pool2dbackward silently produces an all-zero (unwritten) grad_input for tensors withnumel > 2^31on XPU, because the kernel computes the element count in 32-bit and overflows.Root cause
In
AdaptiveAvgPool2dBwdKernelFunctor(and the SLM variant), the input dimsib_/ic_/ih_/iw_/oh_/ow_andglobal_range_were declaredint.numel_ = ib_ * ic_ * ih_ * iw_is therefore evaluated in 32-bit and stored into theint64_t numel_after it has already overflowed. For a tensor whose true numel exceeds2^31, the wrapped product can be negative, so the grad loopruns zero iterations and
grad_inputis never written. (The accessors are alreadyPackedTensorAccessor64, so there is no out-of-bounds — the output is simply left uninitialized/zero.)Fix
Widen the dimension and range members to
int64_tin both backward functors so thenumel_product is computed in 64-bit. Mirrors the fix pattern already used formax_pool3d(#3632). No addressing changes needed (accessors are already 64-bit).Test plan
Verified on Arc Pro B60 (from-source XPU build) with input
(1, 4, 32768, 16385)— numel = 2,147,614,720 (> 2^31):numel_wraps to-2,147,352,576; backward writes 0 / numel elements (grad_input all zeros).The failing test
test_adaptive_avg_pool2d_backward_large_index_offsets_xpu(issue #4327) exercises exactly this large-index path.Risk
XPU-only; touches only the two adaptive_avg_pool2d backward functors. Small tensors are unaffected (identical arithmetic, now in 64-bit). No API/behavior change beyond correctly writing the full output.
Fixes #4327.