pytorch - 💡(How to fix) Fix torch.export.export accepts a parameter whose storage is too small, then save/runtime fails with low-level setStorage error

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

import io import platform import sys import traceback

import torch import torch.nn as nn

print("python:", sys.version.split()[0]) print("platform:", platform.platform()) print("torch:", torch.version) print("cuda_available:", torch.cuda.is_available())

x = torch.ones(1, 4) m = nn.Linear(4, 4) with torch.no_grad(): m.weight.fill_(7.0) m.bias.zero_()

print("clean_eager_output:", m(x).tolist()) print("weight_shape:", tuple(m.weight.shape)) print("weight_numel:", m.weight.numel()) print("storage_nbytes_before:", m.weight.untyped_storage().nbytes())

m.weight.data.untyped_storage().resize_(0) print("storage_nbytes_after:", m.weight.untyped_storage().nbytes())

try: m(x) print("corrupt_eager_status: unexpectedly succeeded") except Exception as e: print("corrupt_eager_status: raised", type(e).name, str(e))

try: ep = torch.export.export(m, (x,)) print("export_status: succeeded") except Exception as e: print("export_status: raised", type(e).name, str(e)) raise SystemExit

try: ep.module()(x) print("exported_module_status: unexpectedly succeeded") except Exception as e: print("exported_module_status: raised", type(e).name, str(e))

try: buf = io.BytesIO() torch.export.save(ep, buf) print("save_status: unexpectedly succeeded") except Exception as e: print("save_status: raised") print("exception_type:", type(e).name) print("exception_message:", str(e)) traceback.print_exc(limit=10)

Root Cause

The bad parameter is already rejected by eager execution, but torch.export.export() accepts it and defers the failure until ep.module() or torch.export.save(). This makes the exported artifact state inconsistent and harder to diagnose.

Code Example

import io
import platform
import sys
import traceback

import torch
import torch.nn as nn

print("python:", sys.version.split()[0])
print("platform:", platform.platform())
print("torch:", torch.__version__)
print("cuda_available:", torch.cuda.is_available())

x = torch.ones(1, 4)
m = nn.Linear(4, 4)
with torch.no_grad():
    m.weight.fill_(7.0)
    m.bias.zero_()

print("clean_eager_output:", m(x).tolist())
print("weight_shape:", tuple(m.weight.shape))
print("weight_numel:", m.weight.numel())
print("storage_nbytes_before:", m.weight.untyped_storage().nbytes())

m.weight.data.untyped_storage().resize_(0)
print("storage_nbytes_after:", m.weight.untyped_storage().nbytes())

try:
    m(x)
    print("corrupt_eager_status: unexpectedly succeeded")
except Exception as e:
    print("corrupt_eager_status: raised", type(e).__name__, str(e))

try:
    ep = torch.export.export(m, (x,))
    print("export_status: succeeded")
except Exception as e:
    print("export_status: raised", type(e).__name__, str(e))
    raise SystemExit

try:
    ep.module()(x)
    print("exported_module_status: unexpectedly succeeded")
except Exception as e:
    print("exported_module_status: raised", type(e).__name__, str(e))

try:
    buf = io.BytesIO()
    torch.export.save(ep, buf)
    print("save_status: unexpectedly succeeded")
except Exception as e:
    print("save_status: raised")
    print("exception_type:", type(e).__name__)
    print("exception_message:", str(e))
    traceback.print_exc(limit=10)

---

python: 3.13.5
platform: Linux-4.4.0-x86_64-with-glibc2.41
torch: 2.10.0+cpu
cuda_available: False
clean_eager_output: [[28.0, 28.0, 28.0, 28.0]]
weight_shape: (4, 4)
weight_numel: 16
storage_nbytes_before: 64
storage_nbytes_after: 0
corrupt_eager_status: raised RuntimeError setStorage: sizes [4, 4], strides [1, 4], storage offset 0, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0
export_status: succeeded
exported_module_status: raised RuntimeError setStorage: sizes [4, 4], strides [1, 4], storage offset 0, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0
save_status: raised
exception_type: RuntimeError
exception_message: setStorage: sizes [], strides [], storage offset 15, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0

---

File ".../torch/export/pt2_archive/_package_weights.py", line 11, in _end_ptr
    stop = tensor.view(-1)[-1].data_ptr() + tensor.element_size()
RuntimeError: setStorage: sizes [], strides [], storage offset 15, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0

---

python: 3.13.5
torch: 2.10.0+cpu
platform: Linux-4.4.0-x86_64-with-glibc2.41
cuda_available: False
RAW_BUFFERClick to expand / collapse

Describe the bug

torch.export.export() succeeds on a module whose parameter still has shape (4, 4) and numel() == 16, but whose underlying storage has been resized to zero.

Eager execution rejects the same module immediately with a storage out-of-bounds error. The exported program is still created, but ep.module() and torch.export.save() fail later with a low-level setStorage error.

I am filing this as a regular validation/correctness bug, not as a security advisory.

Reproducer

import io
import platform
import sys
import traceback

import torch
import torch.nn as nn

print("python:", sys.version.split()[0])
print("platform:", platform.platform())
print("torch:", torch.__version__)
print("cuda_available:", torch.cuda.is_available())

x = torch.ones(1, 4)
m = nn.Linear(4, 4)
with torch.no_grad():
    m.weight.fill_(7.0)
    m.bias.zero_()

print("clean_eager_output:", m(x).tolist())
print("weight_shape:", tuple(m.weight.shape))
print("weight_numel:", m.weight.numel())
print("storage_nbytes_before:", m.weight.untyped_storage().nbytes())

m.weight.data.untyped_storage().resize_(0)
print("storage_nbytes_after:", m.weight.untyped_storage().nbytes())

try:
    m(x)
    print("corrupt_eager_status: unexpectedly succeeded")
except Exception as e:
    print("corrupt_eager_status: raised", type(e).__name__, str(e))

try:
    ep = torch.export.export(m, (x,))
    print("export_status: succeeded")
except Exception as e:
    print("export_status: raised", type(e).__name__, str(e))
    raise SystemExit

try:
    ep.module()(x)
    print("exported_module_status: unexpectedly succeeded")
except Exception as e:
    print("exported_module_status: raised", type(e).__name__, str(e))

try:
    buf = io.BytesIO()
    torch.export.save(ep, buf)
    print("save_status: unexpectedly succeeded")
except Exception as e:
    print("save_status: raised")
    print("exception_type:", type(e).__name__)
    print("exception_message:", str(e))
    traceback.print_exc(limit=10)

Actual behavior

python: 3.13.5
platform: Linux-4.4.0-x86_64-with-glibc2.41
torch: 2.10.0+cpu
cuda_available: False
clean_eager_output: [[28.0, 28.0, 28.0, 28.0]]
weight_shape: (4, 4)
weight_numel: 16
storage_nbytes_before: 64
storage_nbytes_after: 0
corrupt_eager_status: raised RuntimeError setStorage: sizes [4, 4], strides [1, 4], storage offset 0, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0
export_status: succeeded
exported_module_status: raised RuntimeError setStorage: sizes [4, 4], strides [1, 4], storage offset 0, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0
save_status: raised
exception_type: RuntimeError
exception_message: setStorage: sizes [], strides [], storage offset 15, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0

The relevant torch.export.save() stack ends in:

File ".../torch/export/pt2_archive/_package_weights.py", line 11, in _end_ptr
    stop = tensor.view(-1)[-1].data_ptr() + tensor.element_size()
RuntimeError: setStorage: sizes [], strides [], storage offset 15, and itemsize 4 requiring a storage size of 64 are out of bounds for storage of size 0

Expected behavior

torch.export.export() should reject parameters/buffers whose metadata requires more bytes than their underlying storage contains, or otherwise produce a clear validation error before returning an ExportedProgram.

At minimum, torch.export.export() should not return an exported program that cannot be run or saved because one parameter has impossible shape/stride/storage metadata.

Why this matters

The bad parameter is already rejected by eager execution, but torch.export.export() accepts it and defers the failure until ep.module() or torch.export.save(). This makes the exported artifact state inconsistent and harder to diagnose.

Related issue check

The closest related report I found is https://github.com/pytorch/pytorch/issues/162700, which also mentions tensors with non-zero elements but unallocated/zero storage. I did not find an existing minimal report for torch.export.export() accepting a zero-storage parameter and failing later during torch.export.save().

Environment

python: 3.13.5
torch: 2.10.0+cpu
platform: Linux-4.4.0-x86_64-with-glibc2.41
cuda_available: False

cc @chauhang @penguinwu @avikchaudhuri @zhxchen17 @tugsbayasgalan @angelayi @ydwu4

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

torch.export.export() should reject parameters/buffers whose metadata requires more bytes than their underlying storage contains, or otherwise produce a clear validation error before returning an ExportedProgram.

At minimum, torch.export.export() should not return an exported program that cannot be run or saved because one parameter has impossible shape/stride/storage metadata.

Still need to ship something?

×6

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

Back to top recommendations

TRENDING