Pytorch load_state_dict not work properly

silverbulletmdc: <p>I try to load a checkpoint to a pytorch module. But after I call the <code>load_state_dict</code> function, the parameters of the module are not aligned with the checkpoint file and all the results are noises.</p>
<p>I add an assertion to ensure the loaded parameters are equal with the checkpoint, and it was triggred.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/59007a3b2179334cc1630dad140e135e52110365.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/59007a3b2179334cc1630dad140e135e52110365" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/59007a3b2179334cc1630dad140e135e52110365_2_690x90.jpeg" alt="image" data-base62-sha1="cHlw6ydQE1DqneEL9qDsRPcWqOh" width="690" height="90" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/59007a3b2179334cc1630dad140e135e52110365_2_690x90.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/59007a3b2179334cc1630dad140e135e52110365_2_1035x135.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/59007a3b2179334cc1630dad140e135e52110365_2_1380x180.jpeg 2x" data-dominant-color="2B2C2C"><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">image</span><span class="informations">1526×200 65.2 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae9ab2f44a23a662129b7540a857815482c369ac.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/ae9ab2f44a23a662129b7540a857815482c369ac" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/e/ae9ab2f44a23a662129b7540a857815482c369ac_2_690x51.jpeg" alt="image" data-base62-sha1="oUCwiOl9Ezx2fizMTK8llxg8uMQ" width="690" height="51" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/e/ae9ab2f44a23a662129b7540a857815482c369ac_2_690x51.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/e/ae9ab2f44a23a662129b7540a857815482c369ac_2_1035x76.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/e/ae9ab2f44a23a662129b7540a857815482c369ac_2_1380x102.jpeg 2x" data-dominant-color="302F2E"><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">image</span><span class="informations">1708×128 66.6 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>
<p>I use pytorch 2.4.1. Can anyone have idea?</p>

Bodkens: <p>Is your model compiled using <code>torch.compile</code> before saving <code>state_dict</code>? If so, before loading state dictionary to model you need to  compile model and then load state dictionary</p>

silverbulletmdc: <p>I have solved the problem. The parameters of my model are in different devices (some of them in cpu and some of them in cuda). After I call model.cuda() before load_state_dict, the problem was resolved. I have no idea why this was occured.</p>