Skip to content

Fix CUDA split_with_sizes_copy crash on empty inputs - #199732

Closed
weifengpy wants to merge 1 commit into
gh/weifengpy/214/basefrom
gh/weifengpy/214/head
Closed

weifengpy wants to merge 1 commit into
gh/weifengpy/214/basefrom
gh/weifengpy/214/head

Conversation

@weifengpy

@weifengpy weifengpy commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

AI-assisted change prepared with Claude Code. It was split out of #199430 so it can land on main independently.

On CUDA, split_with_sizes_copy crashed the process when its input had no elements (since #117203). The fast path's launch size math computed zero chunks per block and then divided by it, so any call with an empty input that took the fast path crashed. The CUDA fast path now returns early for empty inputs, since there is nothing to copy.

Test Plan: the new empty cases in test_split_with_sizes_copy_out, including all-empty splits, crash the process without the fix (found when the fix was written for #199430) and pass with it. The test runs on CUDA when a GPU is available. The local build predates main, so the test ran on an older main (ff18741) whose build includes this kernel change; TensorShape.cu is unchanged on main since then.

python test/test_torch.py -k test_split_with_sizes_copy_out

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199732

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 3ef5f2e with merge base b8ef869 (image):
💚 Looks good so far! There are no failures yet. 💚

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Single-cause bugfix: split_with_sizes_copy_out_cuda_contiguous_no_cast returns early when self.numel() == 0
    • Old code divides by zero: get_num_chunks divides by a zero size, or div_up(iter_factor, chunks_per_block) gets chunks_per_block == 0
  • Correctness
    • Caller split_with_sizes_copy_out_cuda runs every validation and resizes out before the fast path, so the early return skips no checks
    • An empty input makes every split empty, so the skipped kernel had nothing to copy
  • Tests
    • test_split_with_sizes_copy_out adds (30, 0, 50) with [0, 0] and (0, 40) with [10, 30], each reaching one of the old divide-by-zero paths on CUDA
  • Socialization
    • Turns a process crash into the empty result the composite path already returns; no signature, default, or message change

reason: clean
Reviewed commit: 3ef5f2e

Inference job

🐛 Report a wrong verdict

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@weifengpy
weifengpy marked this pull request as ready for review October 5, 2026 06:07
@weifengpy
weifengpy requested a review from anshul-si October 5, 2026 06:08
@Skylion007

Copy link
Copy Markdown
Collaborator

@claude review this PR

@claude

claude Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Claude finished @Skylion007's task in 43s —— View job


The fix is correct and I'd approve it. My only note is a small wording issue in the new code comment.

Review details

PR Review: #199732

Summary

The CUDA fast path for split_with_sizes_copy now returns early when the input is empty, which fixes the divide-by-zero crash. The caller split_with_sizes_copy_out_cuda already does every validation and resizes out before it calls the fast path, so the early return doesn't skip any checks. If the input has any elements, every dimension is greater than zero, so num_chunks >= 1 and chunks_per_block >= 1 on the remaining path. No correctness issues found.

Code Quality

  • aten/src/ATen/native/cuda/TensorShape.cu:624-625: the comment only covers one of the two crash paths. When the split dimension has size zero, as in (30, 0, 50) with dim=1, the crash happens earlier, in detail::get_num_chunks, which divides by tensor.sizes()[d] == 0. The chunks_per_block == 0 division only happens when a dimension before dim is zero, as in (0, 40). A more accurate comment would be something like // Every output is empty, and the launch size math below divides by zero sizes/chunk counts.

Recommendation

Approve

The fix is minimal and correct, and the regression test hits both divide-by-zero paths on CUDA.

@Skylion007 Skylion007 added the release notes: cuda release notes category label Oct 5, 2026
@Skylion007

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 5, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
> AI-assisted change prepared with Claude Code. It was split out of pytorch#199430 so it can land on main independently.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since pytorch#117203). The fast path's launch size math computed zero chunks per block and then divided by it, so any call with an empty input that took the fast path crashed. The CUDA fast path now returns early for empty inputs, since there is nothing to copy.
>
> Test Plan: the new empty cases in `test_split_with_sizes_copy_out`, including all-empty splits, crash the process without the fix (found when the fix was written for pytorch#199430) and pass with it. The test runs on CUDA when a GPU is available. The local build predates main, so the test ran on an older main (ff18741) whose build includes this kernel change; `TensorShape.cu` is unchanged on main since then.
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> ```
Pull Request resolved: pytorch#199732
Approved by: https://github.com/pytorchgreenlight, https://github.com/Skylion007
torchxpubot pushed a commit to intel/torch-xpu-ops that referenced this pull request Nov 3, 2026
Fixes #5661

### Root cause
`split_with_sizes_copy_out_xpu_contiguous_no_cast` divides by values
derived from self.numel(). For an empty self, get_num_chunks computes 0
/ 0 when the split dim has size 0. When num_chunks is 0,
chunks_per_group is also 0, so ceil_div(iter_factor, chunks_per_group)
divides by zero. Both cases raise SIGFPE and kill the interpreter.

### Fix
CUDA has the same launch-size math and fixed the crash in
pytorch/pytorch#199732 with an early return when self.numel() == 0.
Every output is empty in that case, so there is nothing to copy. This PR
adds the same guard in the same place, after the caller has validated
and resized the outputs, so the XPU behavior matches CUDA.

### Testing

It also updates `test_split_with_sizes_copy_out` in
test/xpu/test_torch_xpu.py to the current upstream version. That version
is device-generic (TestTorchDeviceType) and includes the empty-input
cases. The old device-less copy in TestTorch is removed. The CUDA-graph
part lives in upstream TestTorchCUDA and was not ported.

```
python test/test_torch.py TestTorchDeviceTypeXPU.test_split_with_sizes_copy_out_xpu   # on upstream
python test/xpu/test_torch_xpu.py -k split_with_sizes_copy_out
```
### Steps after merge
Create PR to PyTorch upstream to unskip test_split_with_sizes_copy_out
in test_torch.py

---------

Co-authored-by: Damian Janusz <[email protected]>
Co-authored-by: Benedykt Bela <[email protected]>
pytorchmergebot pushed a commit to DamJanusz/pytorch that referenced this pull request Dec 1, 2026
test_split_with_sizes_copy_out was skipped on XPU because
split_with_sizes_copy.out crashed with SIGFPE on empty inputs
(intel/torch-xpu-ops#5661). The XPU kernel computed launch sizes by
dividing by values derived from self.numel(); for an empty input the
chunk count was 0, so both get_num_chunks and ceil_div(iter_factor,
chunks_per_group) divided by zero.

The kernel now returns early when self.numel() == 0 (every output is
empty, so there is nothing to copy), matching the CUDA fix in pytorch#199732.
That fix is in intel/torch-xpu-ops#5912. This change only removes the
now-unneeded @skipIfXpu so the test runs on XPU.

Only the skip decorator is removed. Fixing the kernel in the same PR
is not possible because the XPU ops live in the torch-xpu-ops repo.

Test Plan:
(fill in after running; requires a build with torch-xpu-ops#5912)
python test/test_torch.py -k test_split_with_sizes_copy_out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: cuda release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants