Skip to content

Int64 support for UpsampleNearest3d#3737

Merged
chuanqi129 merged 10 commits into
mainfrom
fix/upsample-nearest3d-int64-indexing
Jun 10, 2026
Merged

Int64 support for UpsampleNearest3d#3737
chuanqi129 merged 10 commits into
mainfrom
fix/upsample-nearest3d-int64-indexing

Conversation

@kdrozd-dev

Copy link
Copy Markdown
Contributor

Fixes: #2510.

Align with changes from: pytorch/pytorch#144865. Improve error messages to resemble the cuda ones and change the relevant int32 variables to int64.

Comment thread src/ATen/native/xpu/sycl/UpSampleNearest3dKernels.cpp Outdated
@github-actions github-actions Bot added disable_e2e Disable all e2e test jobs for the PR disable_distributed Disable distributed UT test jobs for the PR labels May 22, 2026
Comment thread src/ATen/native/xpu/sycl/UpSampleNearest3dKernels.cpp Outdated
@chuanqi129
chuanqi129 marked this pull request as draft May 22, 2026 07:41
@chuanqi129
chuanqi129 marked this pull request as ready for review May 22, 2026 07:41
Co-authored-by: Slawomir Siwek <slawomir.siwek@intel.com>
Comment thread src/ATen/native/xpu/sycl/UpSampleNearest3dKernels.cpp Outdated
Co-authored-by: Slawomir Siwek <slawomir.siwek@intel.com>

@pbielak pbielak 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, but similar to my changes with the MaxPool3D kernel (see PR: #3558 and #3632 ), we should check the impact of the int64_t "hardcoding". Maybe a dispatch over int32/int64 would give better perf results.

@kdrozd-dev

kdrozd-dev commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

Looks good, but similar to my changes with the MaxPool3D kernel (see PR: #3558 and #3632 ), we should check the impact of the int64_t "hardcoding". Maybe a dispatch over int32/int64 would give better perf results.

Seems like a good idea will switch to dispatch approach after a quick benchmark

@kdrozd-dev

kdrozd-dev commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

3 runs with 20 execs per test case each

Case Shape dtype Output numel Speedup (med) Speedup (avg) Stable
small-f32 (2, 64, 16, 16, 16) float32 4M 0.869x 0.899x no
small-bf16 (2, 64, 16, 16, 16) bfloat16 4M 1.044x 1.062x no
small-f16 (2, 64, 16, 16, 16) float16 4M 1.482x 1.287x no
med-f32 (4, 128, 32, 32, 32) float32 134M 1.034x 0.869x no
med-bf16 (4, 128, 32, 32, 32) bfloat16 134M 0.986x 1.089x no
med-f16 (4, 128, 32, 32, 32) float16 134M 0.925x 0.848x no
large-f32 (1, 32, 64, 128, 128) float32 268M 1.053x 1.053x YES
large-bf16 (1, 32, 64, 128, 128) bfloat16 268M 1.058x 1.058x YES
xl-bf16 (1, 64, 64, 128, 256) bfloat16 1.07B 1.054x 1.054x YES
xl-f32 (1, 64, 64, 128, 256) float32 1.07B 1.055x 1.055x YES
med-exact-f32 (4, 128, 32, 32, 32) float32 134M 0.847x 0.845x no
med-scale3-f32 (2, 64, 16, 16, 16) float32 14M 1.302x 1.063x no

The benchmark shows that for stable test cases dispatch based approach is faster by around 5%. For small kernels results were mostly noise and varied greatly.

@kdrozd-dev
kdrozd-dev requested review from EikanWang and guangyey May 28, 2026 09:48
@EikanWang

Copy link
Copy Markdown
Contributor

@copilot please review the changes in depth

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

@copilot please review the changes in depth

I've reviewed the changes in depth. Here's my assessment:

Overall approach: The PR correctly implements an int32/int64 index dispatch pattern using AT_DISPATCH_INDEX_TYPES with canUse32BitIndexMath, which gives optimal performance for small tensors (int32 path) while supporting large tensors (int64 path). This aligns with the CUDA implementation from pytorch/pytorch#144865 and the benchmark results show ~5% speedup for large tensors.

Key changes reviewed:

  1. Forward kernel (UpsampleNearest3dKernelFunctor)index_t is correctly applied to dst_idx, dst_c_stride, src_c_stride, and src_idx which are the variables that can overflow with large tensors. The per-dimension variables (c, dst_z, src_z, etc.) remain int which is correct since individual dimensions won't exceed int32 limits.

  2. Backward kernel (UpsampleNearest3dBackwardFunctor) — Same pattern applied correctly. index_t used for stride computations and src_idx inside the triple-nested loop.

  3. Template function signatures — Changed from unsigned int n to int64_t n, and ceil_div calls updated to use static_cast<int64_t>(work_group_size).

  4. TORCH_CHECK removal — The old TORCH_CHECK(output.numel() <= std::numeric_limits<int32_t>::max()) is correctly removed since the dispatch now handles both cases.

One minor observation: The UpsampleNearest3dBackwardFunctor constructor (line 282-308) doesn't initialize index_bw_op_ in its member initializer list, though it's declared as a member (line 325). This is a pre-existing issue (it exists in the original code), but it means index_bw_op_ will be default-constructed rather than using the passed index_bw_op parameter. This doesn't affect correctness since NearestBwIndexOp and NearestExactBwIndexOp are stateless functors, but it's worth noting.

No blocking issues found — the implementation is correct and well-aligned with the upstream CUDA approach.

@kdrozd-dev

Copy link
Copy Markdown
Contributor Author

/merge -f "unrelated CI failure"

@chuanqi129
chuanqi129 merged commit fbddd93 into main Jun 10, 2026
20 of 21 checks passed
@chuanqi129

Copy link
Copy Markdown
Contributor

✅ PR has been successfully merged by @kdrozd-dev (force merge).

@chuanqi129
chuanqi129 deleted the fix/upsample-nearest3d-int64-indexing branch June 10, 2026 08:40
daisyden pushed a commit to daisyden/torch-xpu-ops that referenced this pull request Jun 18, 2026
Fixes: intel#2510.

Align with changes from: pytorch/pytorch#144865.
Improve error messages to resemble the cuda ones and change the relevant
int32 variables to int64.

---------

Co-authored-by: Slawomir Siwek <slawomir.siwek@intel.com>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[upstream_ut] RuntimeError: Expected output.numel() <= std::numeric_limits<int32_t>::max() to be true, but got fa

6 participants