Export DataParallel to ONNX

Kevin57: <p>Hi! I am new to Pytorch and distributed learning. I am currently training a RL model using 4 GPUs on the server for an embodied navigation task. I process the visual information using pretrained MobileNet v2 model from Torchvision.</p>
<p>I wrap the visual network in using DataParallel, and the visual network is included in the final actor-critic model. However, when I export the final model to onnx, it gave an error a NoneType error when reading.</p>
<p>I read from other posts and understand that if I wrap the final model into DataParallel, I could call the module attribute. However, I don’t know what exactly I should do when part of the model is wrapped in DataParallel and I couldn’t find similar issues online.</p>

mrshenli: <aside class="quote no-group" data-username="Kevin57" data-post="1" data-topic="110547">
<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/kevin57/48/34079_2.png" class="avatar"> Kevin57:</div>
<blockquote>
<p>I wrap the visual network in using DataParallel, and the visual network is included in the final actor-critic model. However, when I export the final model to onnx, it gave an error a NoneType error when reading.</p>
</blockquote>
</aside>
<p>Hey <a class="mention" href="/u/kevin57">@Kevin57</a>, could you please create an issue on PyTorch github with the error log? And please also add an onnx label to that issue. Thanks!</p>
<blockquote>
<p>I read from other posts and understand that if I wrap the final model into DataParallel, I could call the module attribute. However, I don’t know what exactly I should do when part of the model is wrapped in DataParallel and I couldn’t find similar issues online.</p>
</blockquote>
<p>Could you please show a code snippet? Is it sth like:</p>
<pre data-code-wrap="python"><code class="lang-python">class MyModule(nn.Module):
  def __init__(self):
    self.l1 = nn.Linear(20, 20)
    self.l2 = DataParallel(nn.Linear(20, 20))
</code></pre>
<p>If this is the case, does the following work?</p>
<pre data-code-wrap="python"><code class="lang-python">net = MyModule()
net.l2.module # this should returns the non-DataParallel linear module 
</code></pre>
<p>cc <a class="mention" href="/u/vitalyfedyunin">@VitalyFedyunin</a></p>

Kevin57: <p>Hi! Thank you for your response! I have submitted an issue on PyTorch but I am not able to add a label. The name is DataParallel to ONNX.</p>
<p>For the code snippet, I think this is a similar example to mine! I tried to use something like <code>net.ls.module</code> in your code snippet, but it does not work and it gives me the following error message:</p>
<blockquote>
<p>File “/home/qi/mlagents_gesthor/ml-agents/mlagents/trainers/torch/model_serialization.py”, line 119, in export_policy_model<br>
self.policy.actor_critic.module,<br>
File “/home/qi/gesthor/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 778, in <strong>getattr</strong><br>
raise ModuleAttributeError(“‘{}’ object has no attribute ‘{}’”.format(<br>
torch.nn.modules.module.ModuleAttributeError: ‘SeparateActorCritic’ object has no attribute ‘module’</p>
</blockquote>
<p>where SeparateActorCritic is similar to net in your code snippet.</p>