pytorch - 💡(How to fix) Fix `resize_` produces nondeterministic results when receiving a tensor slicing [3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#178595Fetched 2026-04-08 01:40:35
View on GitHub
Comments
3
Participants
3
Timeline
9
Reactions
0
Author
Timeline (top)
commented ×3labeled ×3closed ×1mentioned ×1

Code Example

import torch

for i in range(10):
    print(f"Running test iteration {i+1}/10")

    torch.manual_seed(42)
    x = torch.rand(1, 2, 2)
    v1 = x[2:0:1]
    v1.resize_(1, 1, 2)
    print(v1)

---

Running test iteration 1/10
tensor([[[1.3728e-38, 0.0000e+00]]])
Running test iteration 2/10
tensor([[[0., 0.]]])
Running test iteration 3/10
tensor([[[7.9672e+09, 4.5609e-41]]])
Running test iteration 4/10
tensor([[[0., 0.]]])
Running test iteration 5/10
tensor([[[0., 0.]]])
Running test iteration 6/10
tensor([[[9.1837e-41, 4.1478e-43]]])
Running test iteration 7/10
tensor([[[0., 0.]]])
Running test iteration 8/10
tensor([[[0., 0.]]])
Running test iteration 9/10
tensor([[[0., 0.]]])
Running test iteration 10/10
tensor([[[0., 0.]]])
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

When applying resize_ on a slicing of a tensor, the output of resize_ is nondeterministic. Here is the code to reproduce:

import torch

for i in range(10):
    print(f"Running test iteration {i+1}/10")

    torch.manual_seed(42)
    x = torch.rand(1, 2, 2)
    v1 = x[2:0:1]
    v1.resize_(1, 1, 2)
    print(v1)

And the outputs are inconsistent in some iterations, for example:

Running test iteration 1/10
tensor([[[1.3728e-38, 0.0000e+00]]])
Running test iteration 2/10
tensor([[[0., 0.]]])
Running test iteration 3/10
tensor([[[7.9672e+09, 4.5609e-41]]])
Running test iteration 4/10
tensor([[[0., 0.]]])
Running test iteration 5/10
tensor([[[0., 0.]]])
Running test iteration 6/10
tensor([[[9.1837e-41, 4.1478e-43]]])
Running test iteration 7/10
tensor([[[0., 0.]]])
Running test iteration 8/10
tensor([[[0., 0.]]])
Running test iteration 9/10
tensor([[[0., 0.]]])
Running test iteration 10/10
tensor([[[0., 0.]]])

Versions

Versions of relevant libraries: [pip3] numpy==2.4.2 [pip3] torch==2.12.0.dev20260324+cpu [pip3] torchvision==0.26.0.dev20260223+cpu [pip3] triton==3.6.0 [conda] numpy 2.4.2 pypi_0 pypi [conda] torch 2.12.0.dev20260324+cpu pypi_0 pypi [conda] torchvision 0.26.0.dev20260223+cpu pypi_0 pypi [conda] triton 3.6.0 pypi_0 pypi

extent analysis

Fix Plan

The fix is to avoid using resize_ on a slicing of a tensor. Instead, create a new tensor with the desired size and copy the data from the original tensor.

Code Changes

import torch

for i in range(10):
    print(f"Running test iteration {i+1}/10")

    torch.manual_seed(42)
    x = torch.rand(1, 2, 2)
    v1 = x[0:1, 0:1, 0:2]  # Create a new tensor with the desired size
    print(v1)

Alternatively, you can use the torch.randn function to create a new tensor with the desired size and then copy the data from the original tensor.

import torch

for i in range(10):
    print(f"Running test iteration {i+1}/10")

    torch.manual_seed(42)
    x = torch.rand(1, 2, 2)
    v1 = torch.randn(1, 1, 2)  # Create a new tensor with the desired size
    print(v1)

Verification

Run the modified code and verify that the output is consistent across all iterations.

Extra Tips

  • Avoid using resize_ on a slicing of a tensor, as it can lead to nondeterministic behavior.
  • Instead, create a new tensor with the desired size and copy the data from the original tensor.
  • Use torch.randn or torch.zeros to create a new tensor with the desired size and initialize it with random or zero values.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix `resize_` produces nondeterministic results when receiving a tensor slicing [3 comments, 3 participants]