Skip to content

Fix int32 overflow in max_pool3d forward kernel#3632

Merged
chuanqi129 merged 1 commit into
mainfrom
fix-int32-overflow-max-pool3d-forward
May 21, 2026
Merged

Fix int32 overflow in max_pool3d forward kernel#3632
chuanqi129 merged 1 commit into
mainfrom
fix-int32-overflow-max-pool3d-forward

Conversation

@pbielak

@pbielak pbielak commented May 11, 2026

Copy link
Copy Markdown
Contributor

Widen parameters and variables in MaxPool3dKernelFunctor and max_pool3d_with_indices_out_template from int to int64_t to prevent overflow when computing strides for large tensors. This mirrors the backward kernel fix from #3558

The forward kernel was partially protected by int64_t local variables and explicit casts at usage sites, so the existing test shape [70, 32, 100, 100, 100] did not trigger failures. However, the stride values themselves (e.g. in_batch_stride = features * T * H * W) were computed as int, which silently overflows for shapes where a single stride exceeds INT32_MAX (e.g. [2, 2200, 100, 100, 100] gives in_batch_stride = 2.2B).

Changes:

  • Fix typo: MaxPool3dKerenlFunctor -> MaxPool3dKernelFunctor
  • Widen dimension and stride params/members to int64_t (kernel, stride, padding, and dilation params remain int)
  • Remove now-redundant (int64_t) casts in operator()
  • Add test_pool3d_fwd_large_size_int64 with a shape that overflows int32 batch strides in the forward pass

Copilot AI review requested due to automatic review settings May 11, 2026 12:20
@pbielak pbielak added disable_e2e Disable all e2e test jobs for the PR disable_distributed Disable distributed UT test jobs for the PR disable_win Disable Windows CI test jobs for the PR labels May 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses potential int32 overflow in the XPU SYCL max_pool3d forward implementation by widening shape/stride-related values to int64_t, and adds a large-tensor regression test intended to exercise the overflow scenario.

Skill files read: .github/skills/xpu-ops-pr-review/SKILL.md

Changes:

  • Fixes a typo in the forward kernel functor name and widens forward kernel shape/stride parameters and members from int to int64_t to prevent overflow in stride math.
  • Updates forward indexing calculations to use widened stride/index types (removing now-redundant explicit casts).
  • Adds a new large-tensor test intended to cover forward-pass stride overflow.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp Widen forward max_pool3d functor/template parameters and stride computations to int64_t to avoid overflow.
test/xpu/nn/test_pooling_xpu.py Adds a new large-tensor forward max_pool3d test targeting int32 stride overflow scenarios.

Comment thread test/xpu/nn/test_pooling_xpu.py Outdated
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from cd613ce to 4086fba Compare May 11, 2026 14:34
Copilot AI review requested due to automatic review settings May 11, 2026 14:36
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from 4086fba to 35542e0 Compare May 11, 2026 14:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from 35542e0 to 9bb3a5c Compare May 11, 2026 15:02
Comment thread src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp
Comment thread test/xpu/nn/test_pooling_xpu.py Outdated
Comment thread test/xpu/nn/test_pooling_xpu.py Outdated
Comment thread test/xpu/nn/test_pooling_xpu.py Outdated
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from 9bb3a5c to e4dd222 Compare May 12, 2026 13:30
Copilot AI review requested due to automatic review settings May 12, 2026 13:30
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from e4dd222 to c5208d7 Compare May 12, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread test/regressions/test_max_pool3d_fwd_int64.py Outdated
Comment thread test/regressions/test_max_pool3d_fwd_int64.py
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from c5208d7 to cd20c86 Compare May 12, 2026 15:19
@guangyey
guangyey requested a review from jianyizh May 13, 2026 02:17
Comment thread src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp Outdated

@jianyizh jianyizh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check bool canUse32BitIndexMath(const TensorBase& t, int64_t max_elem), you should set index dtype as a template parameter

Copilot AI review requested due to automatic review settings May 13, 2026 08:46
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch 2 times, most recently from e7874af to 86298d4 Compare May 13, 2026 08:47
@pbielak

pbielak commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

I discussed the results with @jianyizh and we compared our benchmark scripts.

I re-ran the benchmark with parameters provided by @jianyizh i.e., kernel_size=3, stride=2, padding=1. With a larger pooling window (333 = 27 elements vs 222 = 8), the inner loop performs significantly more index arithmetic per output element, making the int32 savings meaningful.

Ratios are int64-only/int32-dispatch (>1x = dispatch is faster):

Shape Time (contiguous) Time (channels last)
(16, 32, 64, 64, 64) 1.29x 1.21x
(512, 512, 12, 12, 12) 1.32x 1.14x

The earlier regressions (0.6–0.9x) were specific to kernel_size=2, where the kernel is memory-bound and the dispatch overhead dominates. With kernel_size=3, the kernel is more compute-bound and int32 indexing provides a 14–32% speedup.

Conclusion: the int32 dispatch is worth keeping for the forward kernel.

Copilot AI review requested due to automatic review settings May 18, 2026 11:12
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from cbfab56 to 6bef852 Compare May 18, 2026 11:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp:567

  • The index width is selected only from the input tensor, but this kernel indexes and launches over the output/indices tensor as well. Max-pool output can be larger than the input when padding is used, so an input that satisfies 32-bit index math can still produce an output whose numel/strides exceed INT_MAX; in that case OutputSize and output strides are truncated to int. Please include the output/indices (or at least work_output) in this decision, consistent with other kernels that dispatch based on the tensor being indexed.
        AT_DISPATCH_INDEX_TYPES(
            at::native::canUse32BitIndexMath(input, INT_MAX) ? ScalarType::Int
                                                             : ScalarType::Long,

src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp:259

  • OutputSize should not be narrowed to index_t before computing the launch size. When 32-bit indexing is selected, a valid output size close to INT_MAX can overflow in the subsequent (OutputSize + work_group_size - 1) rounding expression, whereas the previous int64_t OutputSize avoided that. Keep the launch-size arithmetic in int64_t/size_t even when per-element offsets use 32-bit indices.
  index_t OutputSize = obatch * features * otime * oheight * owidth;

Comment thread src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp
@chuanqi129

Copy link
Copy Markdown
Contributor

@copilot New UT failures detected in op_ut tests (2 total). Please check these new failures and analyze whether they are caused by the PR changes.

Important: Do NOT update the plan in the PR description directly. Use reply comments to update the status.

Job log: https://github.com/intel/torch-xpu-ops/actions/runs/26029952738
Full new failure report: https://github.com/intel/torch-xpu-ops/actions/runs/26029952738/artifacts/7067258744

New failures:

Category Class name Test name Status Message
op_ut third_party.torch-xpu-ops.test.xpu.export.test_export_training_ir_to_run_decomp_xpu.TrainingIRToRunDecompExportNonStrictTestExport test_dynamic_lstm_training_ir_to_decomp_nonstrict failed AssertionError: Tensor-likes are not close!
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_vmap_xpu.TestVmapOperatorsOpInfoXPU test_op_has_batch_rule_count_nonzero_xpu_float32 failed Unexpected success

Instructions:

  1. Download the full new failure report artifact for complete error messages
  2. Analyze whether the failures are caused by PR changes
  3. If caused by PR changes, fix those failures on this PR branch directly
  4. Add failing test cases with pytest commands in the PR description for the reproducer job to use
  5. Use reply comments to report analysis status - do NOT modify the PR description plan directly

Copilot AI commented May 18, 2026

Copy link
Copy Markdown
Contributor

Check bool canUse32BitIndexMath(const TensorBase& t, int64_t max_elem), you should set index dtype as a template parameter

Implemented in commit 6bef852: max_pool3d forward now templates index type (index_t) and dispatches with AT_DISPATCH_INDEX_TYPES based on canUse32BitIndexMath(...), so index dtype is selected via template parameter instead of hardcoding.

@copilot New UT failures detected in op_ut tests (2 total). Please check these new failures and analyze whether they are caused by the PR changes.

**Important: Do NOT update the plan in the PR de...

I investigated run 26029952738 and downloaded the New-UT-Failures-3632-op_ut-26029952738-1 and full Inductor-XPU-UT-Data-3632-op_ut-26029952738-1 artifacts against commit 6bef852.

Conclusion: the 2 new failures are not caused by this PR’s max_pool3d changes.

  • ...test_dynamic_lstm_training_ir_to_decomp_nonstrict: failure is in export LSTM numerical parity (AssertionError: Tensor-likes are not close, max abs diff 2.66e-05) with stack in pytorch/test/export/test_export.py.
  • ...test_op_has_batch_rule_count_nonzero_xpu_float32: Unexpected success for count_nonzero in functorch OpInfo batch-rule test.

Neither traceback/execution path touches max_pool3d or src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp.

Reproducer commands:

  1. PYTORCH_TEST_WITH_SLOW=1 python test/xpu/export/test_export_training_ir_to_run_decomp_xpu.py TrainingIRToRunDecompExportNonStrictTestExport.test_dynamic_lstm_training_ir_to_decomp_nonstrict
  2. PYTORCH_TEST_WITH_SLOW=1 python test/xpu/functorch/test_vmap_xpu.py TestVmapOperatorsOpInfoXPU.test_op_has_batch_rule_count_nonzero_xpu_float32

No code change was made on this PR branch for these failures.

Copilot AI requested a review from chuanqi129 May 18, 2026 19:35
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from 6bef852 to d1022f7 Compare May 19, 2026 13:19
@chuanqi129

Copy link
Copy Markdown
Contributor

@copilot New UT failures detected in op_ut tests (8 total). Please check these new failures and analyze whether they are caused by the PR changes.

Important: Do NOT update the plan in the PR description directly. Use reply comments to update the status.

Job log: https://github.com/intel/torch-xpu-ops/actions/runs/26099835430
Full new failure report: https://github.com/intel/torch-xpu-ops/actions/runs/26099835430/artifacts/7100641040

New failures:

Category Class name Test name Status Message
op_ut third_party.torch-xpu-ops.test.xpu.dynamo.test_regional_inductor_xpu.RegionalInductorInvokeSubgraphTests test_max_autotune_no_cudagraphs_serialize_False failed AttributeError: module 'torch._inductor' has no attribute 'compile_fx'
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_control_flow_xpu.TestControlFlowTraced test_while_loop_autograd_simple failed AssertionError: 'clas[152 chars] zeros: "i64[]" = torch.ops.aten.zeros.def[3514 chars]1)\n' != 'clas[152 chars] clone: "f32[3, 3]" = torch.ops.aten.clone[3636 chars]1)\n'
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_ops_xpu error ImportError: cannot import name 'skipOps' from 'common_utils' (/__w/torch-xpu-ops/torch-xpu-ops/pytorch/third_party/torch-xpu-ops/test/xpu/../../../../test/functorch/common_utils.py)
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_vmap_xpu error ImportError: cannot import name 'skipOps' from 'common_utils' (/__w/torch-xpu-ops/torch-xpu-ops/pytorch/third_party/torch-xpu-ops/test/xpu/../../../../test/functorch/common_utils.py)
op_ut third_party.torch-xpu-ops.test.xpu.test_modules_xpu.TestModuleXPU test_non_contiguous_tensors_nn_Conv3d_xpu_float32 failed AssertionError: Tensor-likes are not close!

... and 3 more failures. See full list in artifact.

Instructions:

  1. Download the full new failure report artifact for complete error messages
  2. Analyze whether the failures are caused by PR changes
  3. If caused by PR changes, fix those failures on this PR branch directly
  4. Add failing test cases with pytest commands in the PR description for the reproducer job to use
  5. Use reply comments to report analysis status - do NOT modify the PR description plan directly

Copilot AI commented May 20, 2026

Copy link
Copy Markdown
Contributor

@copilot New UT failures detected in op_ut tests (8 total). Please check these new failures and analyze whether they are caused by the PR changes.

**Important: Do NOT update the plan in the PR de...

I analyzed run 26099835430 and downloaded both New-UT-Failures-3632-op_ut-26099835430-1 and full Inductor-XPU-UT-Data-3632-op_ut-26099835430-1 artifacts for commit d1022f7.

Conclusion: these 8 new failures are not caused by this PR.

  • PR only changes: src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp and test/regressions/test_max_pool3d_fwd_int64.py.
  • New failures are in dynamo/functorch/decomp/modules import/config/expectation paths (e.g. missing torch._inductor.compile_fx, missing skipOps/skip imports, frozen dataclass config mutation, control-flow expected text mismatch, Conv3d grad tolerance mismatch), with no max_pool3d call path in traces.

Pytest repro commands:

  1. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/dynamo/test_regional_inductor_xpu.py::RegionalInductorInvokeSubgraphTests::test_max_autotune_no_cudagraphs_serialize_False
  2. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/functorch/test_control_flow_xpu.py::TestControlFlowTraced::test_while_loop_autograd_simple
  3. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/functorch/test_ops_xpu.py
  4. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/functorch/test_vmap_xpu.py
  5. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/test_modules_xpu.py::TestModuleXPU::test_non_contiguous_tensors_nn_Conv3d_xpu_float32
  6. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/dynamo/test_aot_autograd_cache_xpu.py::AOTAutogradCachePicklerTests::test_identical_graphs_and_configs
  7. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/dynamo/test_aot_autograd_cache_xpu.py::AOTAutogradCachePicklerTests::test_different_configs
  8. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/test_decomp_xpu.py

UI screenshot: N/A (no UI changes).

Template MaxPool3dKernelFunctor and max_pool3d_with_indices_out_template
with an index_t type parameter, and dispatch between int32 and int64 at
runtime using canUse32BitIndexMath. This prevents overflow when computing
strides for large tensors while preserving int32 performance for smaller
ones.

The forward kernel computes stride values (e.g. in_batch_stride =
features * T * H * W) that can exceed INT32_MAX for shapes like
[2, 2200, 100, 100, 100] (in_batch_stride = 2.2B).

Changes:
- Fix typo: MaxPool3dKerenlFunctor -> MaxPool3dKernelFunctor
- Add index_t template parameter to functor and template function
- Dispatch index_t via AT_DISPATCH_INDEX_TYPES + canUse32BitIndexMath
- Remove now-redundant (int64_t) casts in operator()
- Add test/regressions/test_max_pool3d_fwd_int64.py with shapes
  that overflow int32 batch strides in the forward pass
Copilot AI review requested due to automatic review settings May 20, 2026 08:08
@pbielak
pbielak force-pushed the fix-int32-overflow-max-pool3d-forward branch from d1022f7 to 433cae9 Compare May 20, 2026 08:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread test/regressions/test_max_pool3d_fwd_int64.py
Comment thread src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp
@chuanqi129

Copy link
Copy Markdown
Contributor

@copilot New UT failures detected in op_ut tests (7 total). Please check these new failures and analyze whether they are caused by the PR changes.

Important: Do NOT update the plan in the PR description directly. Use reply comments to update the status.

Job log: https://github.com/intel/torch-xpu-ops/actions/runs/26149912888
Full new failure report: https://github.com/intel/torch-xpu-ops/actions/runs/26149912888/artifacts/7123194058

New failures:

Category Class name Test name Status Message
op_ut third_party.torch-xpu-ops.test.xpu.export.test_export_training_ir_to_run_decomp_xpu.TrainingIRToRunDecompExportNonStrictTestExport test_dynamic_lstm_training_ir_to_decomp_nonstrict failed AssertionError: Tensor-likes are not close!
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_control_flow_xpu.TestControlFlowTraced test_while_loop_autograd_simple failed AssertionError: 'clas[152 chars] zeros: "i64[]" = torch.ops.aten.zeros.def[3514 chars]1)\n' != 'clas[152 chars] clone: "f32[3, 3]" = torch.ops.aten.clone[3636 chars]1)\n'
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_ops_xpu error ImportError: cannot import name 'skipOps' from 'common_utils' (/__w/torch-xpu-ops/torch-xpu-ops/pytorch/third_party/torch-xpu-ops/test/xpu/../../../../test/functorch/common_utils.py)
op_ut third_party.torch-xpu-ops.test.xpu.functorch.test_vmap_xpu error ImportError: cannot import name 'skipOps' from 'common_utils' (/__w/torch-xpu-ops/torch-xpu-ops/pytorch/third_party/torch-xpu-ops/test/xpu/../../../../test/functorch/common_utils.py)
op_ut third_party.torch-xpu-ops.test.xpu.dynamo.test_aot_autograd_cache_xpu.AOTAutogradCachePicklerTests test_identical_graphs_and_configs failed dataclasses.FrozenInstanceError: cannot assign to field 'aot_id'

... and 2 more failures. See full list in artifact.

Instructions:

  1. Download the full new failure report artifact for complete error messages
  2. Analyze whether the failures are caused by PR changes
  3. If caused by PR changes, fix those failures on this PR branch directly
  4. Add failing test cases with pytest commands in the PR description for the reproducer job to use
  5. Use reply comments to report analysis status - do NOT modify the PR description plan directly

Copilot AI commented May 20, 2026

Copy link
Copy Markdown
Contributor

@copilot New UT failures detected in op_ut tests (7 total). Please check these new failures and analyze whether they are caused by the PR changes.

**Important: Do NOT update the plan in the PR de...

Investigated run 26149912888 and downloaded both New-UT-Failures-3632-op_ut-26149912888-1 and Inductor-XPU-UT-Data-3632-op_ut-26149912888-1 for commit 433cae9.

Conclusion: the 7 new failures are not caused by this PR.

  • PR scope is only:
    • src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp
    • test/regressions/test_max_pool3d_fwd_int64.py
  • New failures are in export/functorch/dynamo/decomp test/import/config paths; stack traces do not involve max_pool3d/DilatedMaxPool3d.

Pytest repro commands:

  1. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/export/test_export_training_ir_to_run_decomp_xpu.py::TrainingIRToRunDecompExportNonStrictTestExport::test_dynamic_lstm_training_ir_to_decomp_nonstrict
  2. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/functorch/test_control_flow_xpu.py::TestControlFlowTraced::test_while_loop_autograd_simple
  3. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/functorch/test_ops_xpu.py
  4. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/functorch/test_vmap_xpu.py
  5. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/dynamo/test_aot_autograd_cache_xpu.py::AOTAutogradCachePicklerTests::test_identical_graphs_and_configs
  6. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/dynamo/test_aot_autograd_cache_xpu.py::AOTAutogradCachePicklerTests::test_different_configs
  7. PYTORCH_TEST_WITH_SLOW=1 pytest test/xpu/test_decomp_xpu.py

UI screenshot: N/A (no UI changes).

@pbielak

pbielak commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

/merge -f "All failing test cases are unrelated to the PR changes"

@chuanqi129
chuanqi129 merged commit 9f4d7eb into main May 21, 2026
27 of 28 checks passed
@chuanqi129

Copy link
Copy Markdown
Contributor

✅ PR has been successfully merged by @pbielak (force merge).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disable_distributed Disable distributed UT test jobs for the PR disable_e2e Disable all e2e test jobs for the PR disable_win Disable Windows CI test jobs for the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants