ModuleNotFoundError: No module named 'modeling'

username: MLHafizur
cooked: <p>I am trying to load a PyTorch model:</p>
<pre><code class="lang-auto">import torch
import io

blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)

container_client = blob_service_client_instance.get_container_client(container=CONTAINERNAME)

blob_url = f"{account_url}/{CONTAINERNAME}/{BLOBNAME}/model.pt"

blob_client = BlobClient.from_blob_url(blob_url=blob_url, credential=STORAGEACCOUNTKEY)
with io.BytesIO() as model_file:
    print("Writing Model")
    model_file.write(blob_client.download_blob().readall())
    print(model_file)
    model_file.seek(0)
    print(model_file)
    print("loading pytorch model")
    adv_model = torch.load(model_file, map_location=torch.device('cpu'))
</code></pre>
<p>The error is as follows:</p>
<pre><code class="lang-auto">&lt;_io.BytesIO object at 0x0000020132C29D60&gt;
&lt;_io.BytesIO object at 0x0000020132C29D60&gt;
loading pytorch model
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In [12], line 22
     20 print(model_file)
     21 print("loading pytorch model")
---&gt; 22 adv_model = torch.load(model_file, map_location=torch.device('cpu'))

File ~\Anaconda3\envs\modelmesh\lib\site-packages\torch\serialization.py:607, in load(f, map_location, pickle_module, **pickle_load_args)
    605             opened_file.seek(orig_position)
    606             return torch.jit.load(opened_file)
--&gt; 607         return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
    608 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)

File ~\Anaconda3\envs\modelmesh\lib\site-packages\torch\serialization.py:882, in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
    880 unpickler = UnpicklerWrapper(data_file, **pickle_load_args)
    881 unpickler.persistent_load = persistent_load
--&gt; 882 result = unpickler.load()
    884 torch._utils._validate_loaded_sparse_tensors()
    886 return result

File ~\Anaconda3\envs\modelmesh\lib\site-packages\torch\serialization.py:875, in _load.&lt;locals&gt;.UnpicklerWrapper.find_class(self, mod_name, name)
    873 def find_class(self, mod_name, name):
    874     mod_name = load_module_mapping.get(mod_name, mod_name)
--&gt; 875     return super().find_class(mod_name, name)

ModuleNotFoundError: No module named 'modeling'
</code></pre>
<p>Why am I getting this ModuleNotFoundError? and What is the solution?</p>

username: ptrblck
cooked: <p>It seems you are trying to directly load a model instead of the recommended way to create a model instance and load its <code>state_dict</code> only.<br>
In this case, you would have to make sure the source file structure is equal in your working environment compared to the environment the model was saved from.</p>

username: pauljurczak
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="162150">
<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>It seems you are trying to directly load a model instead of the recommended way to create a model instance and load its <code>state_dict</code> only.</p>
</blockquote>
</aside>
<p>I just started with PyTorch and see that this problem plagued many other users. There are a lot of questions like this one with answers assuming the knowledge beginner users don’t have.</p>
<p>Documentation at <a href="https://pytorch.org/docs/stable/generated/torch.load.html#torch.load" class="inline-onebox" rel="noopener nofollow ugc">torch.load — PyTorch 2.1 documentation</a> doesn’t mention creating model instance. Neither does the tutorial at: <a href="https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_models_for_inference.html#save-and-load-entire-model" class="inline-onebox" rel="noopener nofollow ugc">Saving and loading models for inference in PyTorch — PyTorch Tutorials 2.1.1+cu121 documentation</a>.</p>
<p>Book “Machine Learning With PyTorch and Scikit-Learn” implies that <code>.pt</code> file can hold both “the model architecture and the weights”.</p>
<p>Is there a more comprehensive discussion of this subject somewhere?</p>

username: ptrblck
cooked: <p>“Creating a model instance” refers to the initialization of any model e.g. via:</p>
<pre><code class="lang-python">model = MyModel()
</code></pre>
<p>or</p>
<pre><code class="lang-python">net = Net()
</code></pre>
<p>as done in the linked tutorial.</p>
<p>For more information about the disadvantage of saving the model directly, you could check <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html">this tutorial</a>.</p>