Skip to content

Allow torch.arange() to return empty tensor when bounds are inconsistent with step sign#182133

Open
senekis316 wants to merge 1 commit intopytorch:mainfrom
senekis316:fix/arange-empty-on-inconsistent-step
Open

Allow torch.arange() to return empty tensor when bounds are inconsistent with step sign#182133
senekis316 wants to merge 1 commit intopytorch:mainfrom
senekis316:fix/arange-empty-on-inconsistent-step

Conversation

@senekis316
Copy link
Copy Markdown
Contributor

Fixes #70915

Per the Array API Standard, torch.arange(start, stop, step) should return
an empty tensor when stop-start and step have opposite signs, rather than
raising a RuntimeError. This aligns with NumPy's behavior.

Before:

torch.arange(1, 0)      # RuntimeError: upper bound and lower bound inconsistent with step sign
torch.arange(0, 1, -1)  # RuntimeError

**After:**
torch.arange(1, 0)      # tensor([], dtype=torch.int64)
torch.arange(0, 1, -1)  # tensor([], dtype=torch.int64)

**Changes:**
aten/src/ATen/native/RangeUtils.h: remove inconsistent-bounds check from arange_check_bounds; clamp size_d to 0 in compute_arange_size
torch/_refs/__init__.py: remove corresponding Python-layer check; clamp length to 0
torch/testing/_internal/common_methods_invocations.py: remove now-valid inputs from error_inputs_arange

…ent with step sign

Per the Array API Standard, torch.arange(start, stop, step) should return
an empty tensor when stop-start and step have opposite signs, rather than
raising a RuntimeError. This aligns with NumPy behavior.

- Remove the inconsistent-bounds TORCH_CHECK from arange_check_bounds (RangeUtils.h)
- Clamp computed size to 0 in compute_arange_size (RangeUtils.h)
- Remove the corresponding check in torch/_refs/__init__.py
- Update error_inputs_arange to remove now-valid inputs

Fixes pytorch#70915
@senekis316 senekis316 requested a review from mruberry as a code owner May 1, 2026 16:29
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 1, 2026

🔗 Helpful Links

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

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

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

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 1, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arange should return empty array if bounds are inconsistent with step sign

3 participants