Attempted to read a PyTorch file with version 4, but the maximum supported version for reading is 3. Your PyTorch installation may be too old

DableUTeeF: <p>I trained a model with Colab TPU using torch XLA, every time I load the saved weight I got this error.</p>
<p>My pytorch version is 1.5.0.</p>
<p>I can’t even install the XLA in my machine. And can’t find any way to install the 1.6.</p>
<p>How to solve this and what’s the reason behind this <strong>feature</strong>?</p>

DableUTeeF: <p>Interestingly, I can just open the file with Xarchiver and change the version to 3 and everything works.</p>
<p>Makes me really curious about the reason behind this.</p>

albanD: <p>Hi,</p>
<p>It happens because when we make breaking changes in the serialization format, we increase the archive version to make sure that the version of pytorch loading the file knows how to handle all the content of the file.</p>
<p>The “right” fix is to upgrade your version of pytorch to at least the version that was used to create the saved file.</p>

DableUTeeF: <p>Which means we need a backward compatible <code>save</code>. Or do we already have one? I can’t find any after several hours of searching (<code>backward</code> is a method name after all -_-).</p>
<p>Because most of the time the <code>save</code> function only write weight, and almost all of them are in either <code>float16</code> or <code>float32</code> which aren’t suppose to be any problem no matter what version.</p>

albanD: <p>We do have a backward compatible save.<br>
What you try to do here is forward compatible: an old version of pytorch is able to load a file created by a newer version of pytorch. This is significantly harder to do than backward compatibility and so we don’t guarantee it.</p>

DableUTeeF: <aside class="quote no-group" data-username="albanD" data-post="5" data-topic="85412">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>We do have a backward compatible save</p>
</blockquote>
</aside>
<p>How to do it?</p>
<p>Honestly all I can find is about <code>nn.Module.backward()</code>.</p>

albanD: <p>The default save is backward compatible.<br>
Meaning that a model saved with an old version can always be loaded with a newer version.</p>

DableUTeeF: <p>No, I won’t be here otherwise. At least not with the 1.6</p>

albanD: <p>The error you mention is about forward compatibility: " Attempted to read a PyTorch file with version 4, but the maximum supported version for reading is 3."<br>
The file’s version comes from a newer version of pytorch than the version you try to load the file with.</p>

DableUTeeF: <p>What’s with it?<br>
Yes, and I’m asking how to use <code>save</code> it with backward compatible, save a single float from <strong>1.6</strong> which is not without bugs to have a 1.5 load it.</p>
<p>Which point you don’t understand.</p>
<p>It make no sense if an error can be fixed by having user edit one single character in weight file and <strong>absolutely everything works</strong>.</p>

DableUTeeF: <p>Or you want me to start a new thread?</p>

albanD: <blockquote>
<p>save a single float from <strong>1.6</strong> which is not without bugs to have a 1.5 load it.</p>
</blockquote>
<p>This is forward compatibility. Not backward compatibility !</p>
<blockquote>
<p>It make no sense if an error can be fixed by having user edit one single character in weight file and <strong>absolutely everything works</strong> .</p>
</blockquote>
<p>This happens for most library that use version for their serialization format. The library can only load the a given set of formats. It won’t be able to load the future ones.</p>
<p>Is there a specific reason why you want to be able to do this?</p>

DableUTeeF: <aside class="quote no-group" data-username="albanD" data-post="12" data-topic="85412">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>This is forward compatibility. Not backward compatibility !</p>
</blockquote>
</aside>
<p>The first line of the saved files from torch 1.6.0a0+541814f ends with this</p>
<pre><code class="lang-auto">versionFB\x12\x00ZZZZZZZZZZZZZZZZZZ4
</code></pre>
<p>Which’s not gonna be opened in the latest torch in pip or conda even though it just ResNet weights.</p>
<p>But!!!<br>
If you change it to</p>
<pre><code class="lang-auto">versionFB\x12\x00ZZZZZZZZZZZZZZZZZZ3
</code></pre>
<p>Now everything works.</p>
<p>My question all along is how to make 1.6 save weight with <code>x00ZZZZZZZZZZZZZZZZZZ3</code>?<br>
How could this possibly be <strong>forward</strong>?</p>
<p>After all, all you need to do to make the saved weight backward compatible is just change a letter.</p>

DableUTeeF: <p>For reasons, it just I accidentally use torch-xla nightly in a TPU machine and that nightly come along with torch 1.6.</p>
<p>Training ResNet50 with 20 times faster than my GPU, which is great. But then I can’t load the saved weights to make use of it even though I’m using the 1.5.0.</p>
<p>The latest pip and conda I found is also 1.5, I’m not gonna try upgrade my PC lib to compile 1.6 it in risk of breaking something else, happens a lot as you might expect from Ubuntu, thus this situation.</p>
<p>If you want to say “just update it and everything will work”, even though I only need to change one character and it’ll work too.</p>
<p>Question is, what cause more trouble between</p>
<ol>
<li>Pytorch loading the file <strong>has a chance of doesn’t</strong> know how to handle some content of the file, even though most of the time nothing’s gonna happen</li>
<li>Raise the error if version mismatch regardless of what’s in the file, even though most Pytorch users never use <code>save()</code> and <code>load()</code> for anything other than some float32</li>
</ol>

albanD: <blockquote>
<p>How could this possibly be <strong>forward</strong> ?</p>
</blockquote>
<p>“save with 1.5” → “load with 1.6” is backward compat: files created with older version can still be loaded with newer versions.<br>
“save with 1.6” → “load with 1.5” is forward compat: the older version needs to know how to load newer files.</p>
<blockquote>
<p>After all, all you need to do to make the saved weight backward compatible is just change a letter.</p>
</blockquote>
<p>Yes for your particular case it just works. But depending on what you save, it might fail unpredictably.<br>
Since 99% of people will never need forward compatibility, we did not added the complexity in the code to make this work, maintain it and test it.</p>
<blockquote>
<p>The latest pip and conda I found is also 1.5,</p>
</blockquote>
<p>You can select the nightly build on the get started page: <a href="https://pytorch.org/get-started/locally/" class="inline-onebox">Start Locally | PyTorch</a> to get a more recent version without having to compile yourself. These are the same ones that xla uses IIRC.</p>

sharkdeng: <p>how to use xarchiver on Mac?</p>

DableUTeeF: <p>I think other zip readers should work too though.</p>
<p>On my case, when I opened the saved file with text editor (actually Pycharm) the first line looks like this</p>
<pre><code class="lang-auto">PK                    e a checkpoint-032epoch/versionFB ZZZ4
</code></pre>
<p>I then just change the last letter from 4 to 3, like</p>
<pre><code class="lang-auto">a = open('w1.pth', 'rb').readlines()
a[0] = a[0][:-2] + bytes(b'3\n')
with open('w2.pth', 'wb') as wr:
    for aa in a:
        wr.write(aa)
</code></pre>
<p>Don’t forget to backup your file though.</p>