Running a model from a loaded state_dict without model's code definition

username: satbek
cooked: <p>Hi there,</p>
<p>Say, I download a trained model (g.e. <a href="http://ml.cs.tsinghua.edu.cn/~chenxi/pytorch-models/mnist-b07bb66b.pth" rel="nofollow noopener">http://ml.cs.tsinghua.edu.cn/~chenxi/pytorch-models/mnist-b07bb66b.pth</a>, it’ll download if you click).</p>
<p>It contains the state_dict of the model. The question is, how do I run the model without having access to its code definition as in here <a href="https://github.com/aaron-xichen/pytorch-playground/blob/master/mnist/model.py" rel="nofollow noopener">https://github.com/aaron-xichen/pytorch-playground/blob/master/mnist/model.py</a>? Is it possible?</p>
<p>The generalized question might sound like “How to run a trained model if you have its state_dict only?”.</p>
<p>Thanks for help!</p>

username: ptrblck
cooked: <p>You won’t be able to run the model as the <code>state_dict</code> only contains its parameters and buffers.<br>
In PyTorch “eager” mode the model definition is the source code of it, which thus would need to be accessible.<br>
You can script the model and store the scripted model, which would be executable without the model definition.</p>

username: satbek
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="97326">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>You can script the model and store the scripted model</p>
</blockquote>
</aside>
<p>Do you mean saving and loading the entire model like here <a href="https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_models_for_inference.html#save-and-load-entire-model" rel="noopener nofollow ugc">https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_models_for_inference.html#save-and-load-entire-model</a>?</p>

username: satbek
cooked: <p>Is it practically possible to parse the state_dict to reconstruct the code for the model? … and then just load the parameters from state_dict.</p>

username: ptrblck
cooked: <p>No, I wouldn’t use this approach as you would have to make sure all source files are in the expected locations and your loading might easily break.<br>
The introduction section explains it a bit better.</p>
<p>I was referring to <a href="https://pytorch.org/docs/stable/generated/torch.jit.save.html#torch.jit.save"><code>torch.jit.save</code></a>.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="satbek" data-post="4" data-topic="97326">
<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/s/8e8cbc/48.png" class="avatar"> satbek:</div>
<blockquote>
<p>Is it practically possible to parse the state_dict to reconstruct the code for the model?</p>
</blockquote>
</aside>
<p>If you have an idea how the model would look like, e.g. were all parameters called sequentially, you might hack a model definition. Since the <code>forward</code> method is unknown, I don’t think it’s a practical approach.</p>

username: satbek
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="97326">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>PyTorch “eager” mode</p>
</blockquote>
</aside>
<p>Can you please clarify this?</p>

username: ptrblck
cooked: <p>By “eager” I mean the normal mode without using the JIT.</p>