Skip to content

[upstream_ut] test_cdist_backward_large_index GPU segfault: int32 overflow in XPU cdist backward buffer offset #5660

Open
pytorch/pytorch
#204593

Description

@daisyden

🐛 Describe the bug

test_cdist_backward_large_index (test/test_torch.py, added upstream alongside the CUDA fix pytorch/pytorch#128791) causes a GPU segfault that aborts the process on XPU for both parametrizations:

  • test_cdist_backward_large_index_b_2_r_1024_m_2048_xpu
  • test_cdist_backward_large_index_b_32_r_8192_m_1_xpu
import torch
b, r, m = 2, 1024, 2048  # also fails with 32, 8192, 1
dev = "xpu"
x1 = torch.randn(b, r, m, device=dev, requires_grad=True)
x2 = torch.randn(b, r, m, device=dev)
grad = torch.randint(-4, 5, (b, r, r), device=dev, dtype=torch.float)
(actual,) = torch.autograd.grad(torch.cdist(x1, x2, p=1), x1, grad)
Segmentation fault from GPU at 0xff0000020b800000, ctx_id: 1 (CCS) type: 0 (NotPresent), level: 2 (PDP), access: 1 (Write), banned: 1, aborting.
Abort was called at 288 line in file:
./shared/source/os_interface/linux/drm_neo.cpp

Root cause: in src/ATen/native/xpu/sycl/DistanceKernels.cpp, cdist_backward_kernel_impl_func:

const int l_size = r_size * m;   // int32
...
scalar_t* buff_i = buff_data + l * l_size + (r1 * j + i) * m + init;

l_size and l are declared int. For (b=2, r=1024, m=2048), r_size = r1*r2 = 2^20, so l_size = r_size*m = 2^31 overflows int32, and l * l_size wraps, producing an out-of-bounds GPU write. The (32, 8192, 1) case overflows dist.numel() == 2^31 similarly. This is the same int32 indexing bug fixed for CUDA in pytorch/pytorch#128791.

Fix: widen l_size (and l/k/y) to int64_t, mirroring the CUDA fix. (r1 * j + i) * m is already int64.

Because the failure is a process-aborting GPU segfault (not a catchable exception), the upstream test is skipped on XPU via @skipIfXpu referencing this issue.

Versions

torch 2.15.0.dev20260923+xpu
GPU: Intel(R) Data Center GPU Max 1550

Activity

  1. added
    module: opsOther ATen/native ops: autograd, optimizer, fx, export
    functionalityFunctional defect: error/assert/hang/timeout (non-numeric)
    trainingTraining workload phase (backward/optimizer step)
    test: utUnit-test failure surface
    on Nov 10, 2026
  2. added theissue type on Nov 10, 2026
  3. daisyden commented on Nov 10, 2026

    @daisyden
    ContributorAuthor
    label-issue: #5660

    Root cause: int32 l = y / r_size, l_size = r_size * m overflow in cdist_backward_kernel_impl_func
    (src/ATen/native/xpu/sycl/DistanceKernels.cpp:468,471,484), giving an OOB GPU write; owner torch-xpu-ops (CUDA fixed in pytorch#128791).

    axis value reason
    type Bug issue_type empty; GPU segfault aborts the process.
    test test: ut Upstream UT test_cdist_backward_large_index in test/test_torch.py; no e2e/oob signal.
    module module: ops Index overflow in cdist backward SYCL kernel DistanceKernels.cpp:471, a native ATen op.
    priority Medium 2 UT cases in group; GPU segfault on cdist backward, not a listed Core API.
    symptom functionality Process-aborting GPU segfault from out-of-bounds write.
    symptom training Failure is in the backward/gradient path (torch.autograd.grad of cdist).

    Test cases (2):

    • test/test_torch.py::TestTorchDeviceTypeXPU::test_cdist_backward_large_index_b_2_r_1024_m_2048_xpu
    • test/test_torch.py::TestTorchDeviceTypeXPU::test_cdist_backward_large_index_b_32_r_8192_m_1_xpu

    Applied to this issue. Generated by a local label-issue run.

  4. self-assigned this
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

functionalityFunctional defect: error/assert/hang/timeout (non-numeric)module: opsOther ATen/native ops: autograd, optimizer, fx, exporttest: utUnit-test failure surfacetrainingTraining workload phase (backward/optimizer step)

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions