RuntimeError: storage has wrong size: expected 0 got 1

I’m trying to load a .pt/.pth file with torch.load. Having worked perfectly previously, after adding another item to my instance before creating the .pt file again, this error popped up. Full error is:

Traceback (most recent call last):
File “outcome.py”, line 12, in
torch.load(r"path")
File “C:\Users\Flawnson\Anaconda3\lib\site-packages\torch\serialization.py”, line 368, in load
return _load(f, map_location, pickle_module)
File “C:\Users\Flawnson\Anaconda3\lib\site-packages\torch\serialization.py”, line 549, in _load
deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
RuntimeError: storage has wrong size: expected 0 got 1

By “adding another item to my instance” do you mean you’ve added another module to your model?
Could you post some pseudo code so that we could see what’s going on?

I did a little experimenting, and the root of the problem is actually a bit simpler. Every time I use torch.load on a .pt file, I get:

RuntimeError: storage has wrong size: expected 0 got 1

Could you upload such a file so that I could try to load it on my machine?
Also, which PyTorch version are you using?

I’m using version 1.0.1
Here are two examples I’m working with. Earlier I tried loading vgg16 model, but it returned the same error.

https://drive.google.com/file/d/1LGBELLn4bmwWwNMzIz5jXf75ZdFwXXFM/view?usp=sharing
https://drive.google.com/file/d/1_w6TIVvSwHRwXS6-JxFYXIK6ENlcxRWQ/view?usp=sharing

Thanks for the files!
Both throw an error:

ModuleNotFoundError: No module named ‘torch_geometric’

I’ll try to install the missing lib and try it again.

EDIT: After installing the missing lib, I could load the files:

>>> a = torch.load('data_0.pt')
>>> a
Data(edge_attr=Unsupported attribute type., edge_index=Unsupported attribute type., x=Unsupported attribute type., y=Unsupported attribute type.)
>>> b = torch.load('data_99.pth')
>>> b
Data(edge_attr=Unsupported attribute type., edge_index=Unsupported attribute type., x=Unsupported attribute type., y=Unsupported attribute type.)

Could you re-download the files and try it again or did you just upload your local files?

1 Like

The same error with torch.load occurred to me after I mistakenly set the size of unused embedding layer to 0. This network consumed a day of computational time. Could there be a way to bypass the error and load it (maybe ignoring empty layer)?

I got similar error and found out torch.cuda.set_device(0) fixed it:

import torch
torch.cuda.set_device(0)  # adding this line fixed it
model = torch.load('mod.pth')

Also it worked on CPU every time:

m = torch.load('mod.pth', map_location=torch.device('cpu'))

The same error with torch.load occurred to me. Many methods were tried but all failed. Finally, I downloaded the weight file <xxx.pth> again and succeeded…