Is `map_location` in `torch.load()` and `model.load_state_dict()` independent from `device` in `.to()`?

username: FlorentMeyer
cooked: <p>Hello community,</p>
<p>When I get a model on CPU then do <code>model.load_state_dict(torch.load(PATH, map_location=device))</code> as explained <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-across-devices" rel="noopener nofollow ugc">here</a>, <code>model.device</code> <em>doesn’t</em> return the <code>device</code> specified in <code>model.load_state_dict(torch.load(PATH, map_location=device))</code> but “cpu” instead. I then have to perform <code>model.to(device)</code> for it to be on the desired device.</p>
<p>Yet when I load a random Tensor stored on a HDD with <code>my_tensor = torch.load(PATH, map_location=device)</code> then <code>model.device</code> <em>does</em> returns the device specified in <code>torch.load(PATH, map_location=device)</code>.</p>
<p>Why is that? Is it <code>load_state_dict</code> that behaves in a special way? <strong>Or do I also need to do</strong> <code>my_tensor = my_tensor.to(device)</code> <strong>after</strong> <code>my_tensor = torch.load(PATH, map_location=device)</code><strong>?</strong></p>
<p>And can I do <code>my_tensor = torch.load(PATH, map_location="cpu")</code> then <code>my_tensor = my_tensor.to("cuda:0")</code>? I don’t quite get if both are related or not, if they should be consistent and performed successively or not.</p>
<p>Thanks for the help.</p>

username: albanD
cooked: <p>Hi,</p>
<p>The <code>map_location</code> changes the device of the Tensors in the state dict that is returned.<br>
But when you <code>load_state_dict()</code>, then these values are loaded (and only values) into the model. But that does not change the model’s device! you will need to move the model itself with <code>.to()</code> if you want to have it on a different device.</p>

username: FlorentMeyer
cooked: <p>Ok, thanks for the answer. So the model is kind of separate from its weights.</p>
<p>Now talking about Tensors, do I need to do <code>my_tensor = my_tensor.to(device)</code> <strong>after</strong> <code>my_tensor = torch.load(PATH, map_location=device)</code>?<br>
In other words, are <code>my_tensor = torch.load(PATH, map_location="cpu")</code> then <code>my_tensor = my_tensor.to("cuda:0")</code> VERSUS <code>my_tensor = torch.load(PATH, map_location="cuda:0")</code> exactly the same?</p>

username: albanD
cooked: <aside class="quote no-group" data-username="FlorentMeyer" data-post="3" data-topic="99983">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/977dab/48.png" class="avatar"> FlorentMeyer:</div>
<blockquote>
<p>In other words, are <code>my_tensor = torch.load(PATH, map_location="cpu")</code> then <code>my_tensor = my_tensor.to("cuda:0")</code> VERSUS <code>my_tensor = torch.load(PATH, map_location="cuda:0")</code> exactly the same?</p>
</blockquote>
</aside>
<p>Yes the end result is the same. So no need to add a <code>.to()</code> if you already used map_location properly.</p>

username: FlorentMeyer
cooked: <p>Thanks for the cristal-clear answer! <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>