Import torch file with trained model in CUDA, in a CPU machine

username: datascguy
cooked: <p>Hello,</p>
<p>I have trained my Agent and saved it in a torch file, using Colab and its CUDA resources.</p>
<p>I want to import the torch file locally in a machine with a CPU and use it to test the trained agent in a set of test environments. However, I get the following Error:</p>
<p>RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device(‘cpu’) to map your storages to the CPU.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/2/428c1e78afa88cc05dd2d11d4a58e2312fc62beb.png" data-download-href="https://discuss.pytorch.org/uploads/default/428c1e78afa88cc05dd2d11d4a58e2312fc62beb" title="Screenshot 2020-09-27 at 19.21.47"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/2/428c1e78afa88cc05dd2d11d4a58e2312fc62beb_2_525x500.png" alt="Screenshot 2020-09-27 at 19.21.47" data-base62-sha1="9uHIqW3ohnFNdXjSDibNv8pHPf5" width="525" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/2/428c1e78afa88cc05dd2d11d4a58e2312fc62beb_2_525x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/2/428c1e78afa88cc05dd2d11d4a58e2312fc62beb_2_787x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/2/428c1e78afa88cc05dd2d11d4a58e2312fc62beb.png 2x" data-dominant-color="F2F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2020-09-27 at 19.21.47</span><span class="informations">816×777 130 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: tom
cooked: <p>You don’t set that as a global variable, but pass it as a keyword argument (after the file) to torch.load, i.e. <code>torch.load('fdsgfg.pt', map_location=torch.device('cpu'))</code>.</p>
<p>Best regards</p>
<p>Thomas</p>

username: datascguy
cooked: <aside class="quote no-group" data-username="tom" data-post="2" data-topic="97591">
<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/tom/48/3162_2.png" class="avatar"> tom:</div>
<blockquote>
<p>pass it as a keyword argument (after the file) to to</p>
</blockquote>
</aside>
<p>Thank you Tom. That worked!</p>

username: sakh251
cooked: <p>I have same problem and tried this solution.<br>
My PyTorch version is 1.12.</p>
<p>I also tried to use pickle as follow:</p>
<pre><code class="lang-auto">class CPU_Unpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if module == 'torch.storage' and name == '_load_from_bytes':
            return lambda b: torch.load(io.BytesIO(b), map_location='cpu')
        else:
            return super().find_class(module, name)
model = CPU_Unpickler(f).load()

</code></pre>
<p>I get another error:</p>
<pre><code class="lang-auto">    model = CPU_Unpickler(f).load()
_pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.
</code></pre>
<p>I trained the model on google colab and using on cpu computer. both version are same.</p>