Skip to content

Fix negative dimension handling in narrow_copy (#22969) - #22969

Merged
meta-codesync[bot] merged 1 commit into
pytorch:mainfrom
JakeStevens:export-D121015244
Sep 22, 2026
Merged

meta-codesync[bot] merged 1 commit into
pytorch:mainfrom
JakeStevens:export-D121015244

Conversation

@JakeStevens

@JakeStevens JakeStevens commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Summary:

Resolves: #13556

Normalize negative dim values before validating narrow_copy arguments. This prevents valid negative dimensions from reaching tensor size checks unnormalized and causing a runtime assertion.

Adds regression coverage for copying with dim=-1.

Reviewed By: mergennachin

Differential Revision: D121015244

@pytorch-bot

pytorch-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 123 Cancelled Jobs, 2 Unrelated Failures

As of commit 46f544a with merge base bb2683b (image):

CANCELLED JOBS - The following jobs were cancelled. Please retry:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 21, 2026
@meta-codesync

meta-codesync Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

@JakeStevens has exported this pull request. If you are a Meta employee, you can view the originating Diff in D121015244.

@JakeStevens JakeStevens linked an issue Sep 21, 2026 that may be closed by this pull request
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

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

@mergennachin

Copy link
Copy Markdown
Contributor

The valid-negative-dimension fix works, but moving normalization before validation introduces a process-abort regression for out-of-range negative dimensions.

For input shape {1, 1, 1}, dim=-4, start=0, and length=1, the baseline returns Error::InvalidArgument. This change first converts dim to -1. check_narrow_copy_args() then accepts it because tensor_has_dim() accepts Python-style negative indices, and the subsequent in.size(-1) aborts:

tensor_impl.h:140: In function size(), assert failed (dim < dim_ && dim >= 0):
Dimension out of range (expected to be in range of [0, 2], but got -1

I reproduced this with the portable C++ kernel on macOS arm64, comparing head d3d29fdb50ef07e63d6e261b170700cd323ee032 against base bb2683b0049d57aaa27bca4117f2157ecce032fe, using the unchanged PR test source:

Test Base PR
New NegativeDimSupported SIGABRT Pass
Existing DimOutOfBoundDies Pass SIGABRT at dim=-4

The other nine tests pass on the PR. Despite its name, DimOutOfBoundDies expects a kernel error on the portable backend, so this abort is a test failure.

Please reject dimensions that remain negative after normalization, or validate the original dimension range before normalization. As a local diagnostic, changing the validation condition to the following made all 10 tests pass:

dim >= 0 && check_narrow_copy_args(in, dim, start, length, out)

@meta-codesync meta-codesync Bot changed the title Fix negative dimension handling in narrow_copy Fix negative dimension handling in narrow_copy (#22969) Sep 21, 2026
JakeStevens added a commit to JakeStevens/executorch that referenced this pull request Sep 21, 2026
Summary:

Resolves: pytorch#13556

Normalize negative `dim` values before validating `narrow_copy` arguments. This prevents valid negative dimensions from reaching tensor size checks unnormalized and causing a runtime assertion.

Adds regression coverage for copying with `dim=-1`.

Differential Revision: D121015244
JakeStevens added a commit to JakeStevens/executorch that referenced this pull request Sep 21, 2026
Summary:

Resolves: pytorch#13556

Normalize negative `dim` values before validating `narrow_copy` arguments. This prevents valid negative dimensions from reaching tensor size checks unnormalized and causing a runtime assertion.

Adds regression coverage for copying with `dim=-1`.

Reviewed By: mergennachin

Differential Revision: D121015244
JakeStevens added a commit to JakeStevens/executorch that referenced this pull request Sep 21, 2026
Summary:

Resolves: pytorch#13556

Normalize negative `dim` values before validating `narrow_copy` arguments. This prevents valid negative dimensions from reaching tensor size checks unnormalized and causing a runtime assertion.

Adds regression coverage for copying with `dim=-1`.

Reviewed By: mergennachin

Differential Revision: D121015244
JakeStevens added a commit to JakeStevens/executorch that referenced this pull request Sep 21, 2026
Summary:
Pull Request resolved: pytorch#22969

Resolves: pytorch#13556

Normalize negative `dim` values before validating `narrow_copy` arguments. This prevents valid negative dimensions from reaching tensor size checks unnormalized and causing a runtime assertion.

Adds regression coverage for copying with `dim=-1`.

Reviewed By: mergennachin

Differential Revision: D121015244
Summary:

Resolves: pytorch#13556

Normalize negative `dim` values before validating `narrow_copy` arguments. This prevents valid negative dimensions from reaching tensor size checks unnormalized and causing a runtime assertion.

Adds regression coverage for copying with `dim=-1`.

Reviewed By: mergennachin

Differential Revision: D121015244
@meta-codesync
meta-codesync Bot merged commit 7dd3d9e into pytorch:main Sep 22, 2026
211 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ExecuTorch execution crashed for narrow_copy.default

2 participants