Loading a model which is saved using Dataparallel

saandeep_aathreya: <p>I have my model which uses <strong>DataParallel</strong> whose checkpoint is saved as below</p>
<pre><code class="lang-auto">model_single = MyModel()
model = nn.DataParallel(model_single)
model = model.to(device)
torch.save(model.state_dict(), checkpoint_path)
</code></pre>
<p>If I try to load this model using the below code, I get the error</p>
<pre><code class="lang-auto">model = MyModel()
checkpoint = torch.load(checkpoint_path, map_location="cpu")
model.load_state_dict(checkpoint) #ERROR HERE
</code></pre>
<pre><code class="lang-auto">RuntimeError: Error(s) in loading state_dict for Glow:
	Missing key(s) in state_dict: "blocks.0.flows.0.actnorm.loc"..., "blocks.3.prior.conv.weight", "blocks.3.prior.conv.bias".

	Unexpected key(s) in state_dict: "module.blocks.0.flows.0.actnorm.loc...,""
</code></pre>
<p>My model initialization parameters are the same as they were during training. I have searched for solutions in pytorch discussion which mostly suggests to save the model using <code>model.module.state_dict()</code> instead. <strong>Is there any other way I could load the model without having to train it again and save it differently?</strong></p>
<p>Please let me know if you require my complete code for better analysis. Thanks in advance.</p>

agolynski: <p>Seems like you have an extra ‘module.’ in your saved model.</p>
<p>in the example above you are saving DataParallel model and loading it into MyModel, the similar issue was discussed here</p><aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/9176" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/9176" target="_blank" rel="noopener nofollow ugc">torch.save() and nn.DataParallel()</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2018-07-05" data-time="04:30:58" data-timezone="UTC">04:30AM - 05 Jul 18 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2018-07-09" data-time="19:32:26" data-timezone="UTC">07:32PM - 09 Jul 18 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/linzhiqiu" target="_blank" rel="noopener nofollow ugc">
          <img alt="linzhiqiu" src="https://avatars.githubusercontent.com/u/10042427?v=4" class="onebox-avatar-inline" width="20" height="20">
          linzhiqiu
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">As pointed out in this post, when model is trained using DataParallel(), the saved model state_dict will have prefix "module." before...</p>
</div>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


saandeep_aathreya: <aside class="quote no-group" data-username="agolynski" data-post="2" data-topic="114126">
<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/agolynski/48/35451_2.png" class="avatar"> agolynski:</div>
<blockquote>
<p>in</p>
</blockquote>
</aside>
<p>Thanks for your response. I was able to solve the issue by passing the loaded model through DataParallel again.</p>
<pre><code class="lang-auto">model_single = MyModel()
#solution
model = nn.DataParallel(model_single)
model = model.to(device)
#end
checkpoint = torch.load(checkpoint_path, map_location="cpu")
model.load_state_dict(checkpoint) #ERROR HERE
</code></pre>