How to save a storage file that can be loaded using `torch.Storage.from_file`

Hi, I wonder how to save a storage file that can be loaded using torch.Storage.from_file.

There is no document that can be found in torch.Storage — PyTorch 1.9.0 documentation. I have tried the following way but it seems that some other information is in the front.

Thank you.

# test.py
import torch


storage = torch.arange(40).long().storage()
print(storage)

fname = 'tensor.pt'
torch.save(storage, fname)
print(torch.LongStorage.from_file(fname, False, 40))
$ python test.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
[torch.LongStorage of size 40]
 578712552184433488
 0
 0
 8241868870375178240
 7017786232174897251
 4775623663572377972
 6510615555426877454
 6510615555426900570
 31543788160
 8171050767163028595
 749958681104507648
 8245937344305065804
 96828977005815649
 240945265224187904
 248108580039819264
 5777644451488081995
 4542319349629192011
 5764607524091199488
 2260595906970443
 0
 0
 7165896609811664384
 8386094135869073768
 6485184847570546529
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
[torch.LongStorage of size 40]

I found the code snippet here:

Also, according to the doc, torch.Storage.from_file

If shared is True the file will be created if needed.

Therefore,

To create:

# create.py
import torch

fname = 'tensor.pt'
storage = torch.LongStorage.from_file(fname, True, 40)
torch.LongTensor(storage).copy_(torch.arange(40))

To read:

# read.py
import torch

fname = 'tensor.pt'
storage = torch.LongStorage.from_file(fname, True, 40)
tensor = torch.LongTensor(storage)
print(tensor)
# Demo
$ python create.py
$ python read.py
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
        18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
        36, 37, 38, 39])