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

siahuat0727: <p>Hi, I wonder how to save a storage file that can be loaded using <code>torch.Storage.from_file</code>.</p>
<p>There is no document that can be found in <a href="https://pytorch.org/docs/stable/storage.html?highlight=from_file#torch.DoubleStorage.from_file" class="inline-onebox" rel="noopener nofollow ugc">torch.Storage — PyTorch 1.9.0 documentation</a>. I have tried the following way but it seems that some other information is in the front.</p>
<p>Thank you.</p>
<pre><code class="lang-python"># 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))
</code></pre>
<pre><code class="lang-auto">$ 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]
</code></pre>

siahuat0727: <p>I found the code snippet here:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/d7836172169ff6db0042e2589df658fa56c1812e/test/test_torch.py#L1844-L1853">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/d7836172169ff6db0042e2589df658fa56c1812e/test/test_torch.py#L1844-L1853" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/d7836172169ff6db0042e2589df658fa56c1812e/test/test_torch.py#L1844-L1853" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/d7836172169ff6db0042e2589df658fa56c1812e/test/test_torch.py#L1844-L1853</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="1844" style="counter-reset: li-counter 1843 ;">
          <li>def test_from_file(self):</li>
          <li>    def assert_with_filename(filename):</li>
          <li>        size = 10000</li>
          <li>        s1 = torch.FloatStorage.from_file(filename, True, size)</li>
          <li>        t1 = torch.FloatTensor(s1).copy_(torch.randn(size))</li>
          <li></li>
          <li>        # check mapping</li>
          <li>        s2 = torch.FloatStorage.from_file(filename, True, size)</li>
          <li>        t2 = torch.FloatTensor(s2)</li>
          <li>        self.assertEqual(t1, t2, atol=0, rtol=0)</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Also, according to the doc, <a href="https://pytorch.org/docs/stable/storage.html?highlight=from_file#torch.DoubleStorage.from_file" rel="noopener nofollow ugc">torch.Storage.from_file</a></p>
<blockquote>
<p>If shared is True the file will be created if needed.</p>
</blockquote>
<p>Therefore,</p>
<p>To create:</p>
<pre data-code-wrap="python"><code class="lang-python"># create.py
import torch

fname = 'tensor.pt'
storage = torch.LongStorage.from_file(fname, True, 40)
torch.LongTensor(storage).copy_(torch.arange(40))
</code></pre>
<p>To read:</p>
<pre data-code-wrap="python"><code class="lang-python"># read.py
import torch

fname = 'tensor.pt'
storage = torch.LongStorage.from_file(fname, True, 40)
tensor = torch.LongTensor(storage)
print(tensor)
</code></pre>
<pre data-code-wrap="bash"><code class="lang-bash"># 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])</code></pre>