Skip to content

Fix int32 overflow in max_pool3d backward kernel#3558

Merged
chuanqi129 merged 1 commit into
mainfrom
fix-int32-overflow-in-max-pool3d-backward
May 18, 2026
Merged

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

Conversation

@pbielak

@pbielak pbielak commented May 5, 2026

Copy link
Copy Markdown
Contributor

Widen parameters and variables in max_pool3d_with_indices_backward_template and MaxPool3dBackwardKernelFunctor from int to int64_t to prevent overflow when computing offsets for large tensors (e.g. shape [70, 32, 100, 100, 100]).

Fixes test case: test_pool3d_large_size_int64

Copilot AI review requested due to automatic review settings May 5, 2026 13:39
@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 disable_windows_ut labels May 5, 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 fixes a large-tensor indexing bug in the XPU max_pool3d backward SYCL kernel by widening stride/size arithmetic to int64_t, which aligns with the repository’s large-tensor safety requirements for backend kernels.

Changes:

  • Widened backward-kernel stride members and constructor parameters from int to int64_t.
  • Made gradOutputSize and derived stride products explicitly compute in 64-bit arithmetic.
  • Preserved the existing kernel logic while removing the specific overflow path described in the PR for large 3D pooling workloads.

Read for this review: .github/skills/xpu-ops-pr-review/SKILL.md, .github/skills/xpu-ops-pr-review/references/torch-xpu-ops-review-notes.md, .github/skills/xpu-ops-pr-review/references/review-checklist.md, .github/skills/xpu-ops-pr-review/references/bc-guidelines.md.

Comment thread src/ATen/native/xpu/sycl/DilatedMaxPool3d.cpp Outdated
Copilot AI review requested due to automatic review settings May 6, 2026 12:26
@pbielak
pbielak force-pushed the fix-int32-overflow-in-max-pool3d-backward branch from 51b3e15 to 8f93b1c Compare May 6, 2026 12:26

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 1 out of 1 changed files in this pull request and generated no new comments.

@pbielak
pbielak force-pushed the fix-int32-overflow-in-max-pool3d-backward branch from 8f93b1c to f802d5d Compare May 6, 2026 12:31

@BBBela BBBela 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.

Looks good to me, thanks for addressing review comments!

Copilot AI review requested due to automatic review settings May 7, 2026 08:28
@pbielak
pbielak force-pushed the fix-int32-overflow-in-max-pool3d-backward branch from f802d5d to 1e9981a Compare May 7, 2026 08:28

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 1 out of 1 changed files in this pull request and generated 1 comment.

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

@guangyey guangyey 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.

Thanks!

@guangyey

Copy link
Copy Markdown
Contributor

@pbielak Could you help check if the same issue persisted in the forward kernel? If yes, better to file another PR to fix it.

@pbielak
pbielak force-pushed the fix-int32-overflow-in-max-pool3d-backward branch from 1e9981a to a1390df Compare May 11, 2026 10:47
pbielak added a commit that referenced this pull request May 11, 2026
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
@pbielak

pbielak commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

@guangyey Thanks for the idea - I checked the forward kernel and it was also susceptible to int32 overflow, but there was no existing UT for it; I fixed the forward kernel and added the new unit test in PR: #3632

pbielak added a commit that referenced this pull request May 11, 2026
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
pbielak added a commit that referenced this pull request May 11, 2026
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
pbielak added a commit that referenced this pull request May 11, 2026
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
pbielak added a commit that referenced this pull request May 12, 2026
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/regressions/test_max_pool3d_fwd_int64.py with shapes
  that overflow int32 batch strides in the forward pass
pbielak added a commit that referenced this pull request May 12, 2026
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/regressions/test_max_pool3d_fwd_int64.py with shapes
  that overflow int32 batch strides in the forward pass
pbielak added a commit that referenced this pull request May 12, 2026
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/regressions/test_max_pool3d_fwd_int64.py with shapes
  that overflow int32 batch strides in the forward pass
@guangyey
guangyey requested a review from jianyizh May 13, 2026 07:05
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.

same as #3558, I prefer keep two instance of int32 and int64. We can dispatch according to shape

@guangyey

Copy link
Copy Markdown
Contributor

same as #3558, I prefer keep two instance of int32 and int64. We can dispatch according to shape

Good catch!

@jianyizh

jianyizh commented May 13, 2026

Copy link
Copy Markdown
Contributor

same as #3558, I prefer keep two instance of int32 and int64. We can dispatch according to shape

@Stonepia @liangan1 I see copilot will read some skills. Can we update the skill so that copilot can be aware of such cases that may hurt performance? Maybe just ask copilot will this PR hurt perf?

Copilot AI review requested due to automatic review settings May 13, 2026 08:36
@pbielak
pbielak force-pushed the fix-int32-overflow-in-max-pool3d-backward branch 2 times, most recently from 5dfcfca to 2f9c249 Compare May 13, 2026 08:46
@Stonepia

Copy link
Copy Markdown
Contributor

same as #3558, I prefer keep two instance of int32 and int64. We can dispatch according to shape

@Stonepia @liangan1 I see copilot will read some skills. Can we update the skill so that copilot can be aware of such cases that may hurt performance? Maybe just ask copilot will this PR hurt perf?

I think currently, the copilot does not have the skill for performance related issues. We need to manually give it skills. So when reviewing, it will aware such issues. As you could see there is a check list in .github/skills/xpu-ops-pr-review/references/review-checklist.md, we could enrich it. Currently it basically all written by AI, so only workflow exists.

@jianyizh

jianyizh commented May 13, 2026

Copy link
Copy Markdown
Contributor

same as #3558, I prefer keep two instance of int32 and int64. We can dispatch according to shape

@Stonepia @liangan1 I see copilot will read some skills. Can we update the skill so that copilot can be aware of such cases that may hurt performance? Maybe just ask copilot will this PR hurt perf?

I think currently, the copilot does not have the skill for performance related issues. We need to manually give it skills. So when reviewing, it will aware such issues. As you could see there is a check list in .github/skills/xpu-ops-pr-review/references/review-checklist.md, we could enrich it. Currently it basically all written by AI, so only workflow exists.

@Stonepia I mean just add one line to ask copilot if this pr hurt perf, no extra skill needed

Template `MaxPool3dBackwardKernelFunctor` and
`max_pool3d_with_indices_backward_template` with an `index_t` type
parameter, and dispatch between `int32_t` and `int64_t` at runtime
using `canUse32BitIndexMath`. This avoids overflow when computing
offsets for large tensors (e.g. shape `[70, 32, 100, 100, 100]`)
while preserving int32 performance for smaller tensors.

Fixes test case: `test_pool3d_large_size_int64`
@pbielak
pbielak force-pushed the fix-int32-overflow-in-max-pool3d-backward branch from 2f9c249 to 8e6d572 Compare May 13, 2026 14:04
@Stonepia

Copy link
Copy Markdown
Contributor

same as #3558, I prefer keep two instance of int32 and int64. We can dispatch according to shape

@Stonepia @liangan1 I see copilot will read some skills. Can we update the skill so that copilot can be aware of such cases that may hurt performance? Maybe just ask copilot will this PR hurt perf?

I think currently, the copilot does not have the skill for performance related issues. We need to manually give it skills. So when reviewing, it will aware such issues. As you could see there is a check list in .github/skills/xpu-ops-pr-review/references/review-checklist.md, we could enrich it. Currently it basically all written by AI, so only workflow exists.

@Stonepia I mean just add one line to ask copilot if this pr hurt perf, no extra skill needed

Oh that's possible. Will add that.

@pbielak

pbielak commented May 14, 2026

Copy link
Copy Markdown
Contributor Author

@jianyizh Added the template parameter of the index dtype - waiting for CI signal

@pbielak

pbielak commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

/merge -f "CI failure is unrelated (test_dynamic_lstm_retraceability_strict)"

@chuanqi129
chuanqi129 merged commit 56f1da4 into main May 18, 2026
40 of 42 checks passed
@chuanqi129

Copy link
Copy Markdown
Contributor

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

@chuanqi129
chuanqi129 deleted the fix-int32-overflow-in-max-pool3d-backward branch May 18, 2026 10:34
BartoszKokoszko pushed a commit to wpietka/torch-xpu-ops that referenced this pull request May 21, 2026
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
intel#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
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 disable_windows_ut

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants