pytorch - 💡(How to fix) Fix Shared-memory UntypedStorage can be resized to zero despite documentation saying shared-memory storages cannot be resized

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

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…

Error Message

For example, untyped_storage().resize_(0) should raise a Python exception for shared-memory storage instead of accepting the resize and leaving an invalid tensor/storage invariant.

Code Example

import torch

x = torch.arange(16, dtype=torch.float32)
x.share_memory_()

s = x.untyped_storage()

print("is_shared:", s.is_shared())
print("before nbytes:", s.nbytes())
print("shape:", tuple(x.shape), "numel:", x.numel())

s.resize_(0)

print("resize_(0) accepted")
print("after nbytes:", s.nbytes())
print("shape:", tuple(x.shape), "numel:", x.numel())

---

PARAM_ACCEPTED resize0 on shared mem
RESEARCH_BREAK_1832f0be shared_mem_resize0

---

Python: 3.12.13
Platform: Linux-6.6.122+-x86_64-with-glibc2.35
torch: 2.11.0+cu128
CUDA built: 12.8
CUDA available: false
cuDNN: 91900

---

torch: 2.9.1+cu128
CUDA runtime: 12.8
GPU: NVIDIA RTX PRO 6000 Blackwell Server Edition
probe device: cpu
RAW_BUFFERClick to expand / collapse

Describe the bug

After moving a CPU tensor to shared memory with Tensor.share_memory_(), the tensor's underlying untyped_storage() can still be resized to zero with resize_(0).

This appears inconsistent with the PyTorch documentation. The Tensor.share_memory_() docs say that tensors in shared memory cannot be resized, and the storage docs say that storages in shared memory cannot be resized.

Reproducer

import torch

x = torch.arange(16, dtype=torch.float32)
x.share_memory_()

s = x.untyped_storage()

print("is_shared:", s.is_shared())
print("before nbytes:", s.nbytes())
print("shape:", tuple(x.shape), "numel:", x.numel())

s.resize_(0)

print("resize_(0) accepted")
print("after nbytes:", s.nbytes())
print("shape:", tuple(x.shape), "numel:", x.numel())

Actual behavior

resize_(0) is accepted even though the storage is shared.

Observed output from my regression run:

PARAM_ACCEPTED resize0 on shared mem
RESEARCH_BREAK_1832f0be shared_mem_resize0

This leaves the tensor with nonzero shape/numel metadata backed by zero-byte storage.

Expected behavior

PyTorch should reject resizing a storage after it has been moved to shared memory.

For example, untyped_storage().resize_(0) should raise a Python exception for shared-memory storage instead of accepting the resize and leaving an invalid tensor/storage invariant.

Environment

Python: 3.12.13
Platform: Linux-6.6.122+-x86_64-with-glibc2.35
torch: 2.11.0+cu128
CUDA built: 12.8
CUDA available: false
cuDNN: 91900

The same behavior was also observed in an earlier regression run on:

torch: 2.9.1+cu128
CUDA runtime: 12.8
GPU: NVIDIA RTX PRO 6000 Blackwell Server Edition
probe device: cpu

Related issue

This may be related to #171676, but this report is specifically about the documented shared-memory contract: shared-memory tensors/storages are documented as non-resizable, yet untyped_storage().resize_(0) is accepted after share_memory_().

cc @VitalyFedyunin @albanD @pragupta @ppwwyyxx @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @jerryzh168 @aditew01 @malfet

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…

FAQ

Expected behavior

PyTorch should reject resizing a storage after it has been moved to shared memory.

For example, untyped_storage().resize_(0) should raise a Python exception for shared-memory storage instead of accepting the resize and leaving an invalid tensor/storage invariant.

Still need to ship something?

×6

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

Back to top recommendations

TRENDING