Can not port checkpoint to mobile using torchscritp : Can't redefine method: forward on class: __torch__.model.Encoder

payman: <p>Hi,<br>
I’m trying to port my trained tacotron2 model to android, for this I’ve written the following code :</p>
<pre><code class="lang-auto">checkpoint_path = "outdir/checkpoint_5000"
model = load_model(hparams)
model.load_state_dict(torch.load(checkpoint_path)['state_dict'])
model.cuda().eval().half()
scripted_model = torch.jit.script(model)
torch.jit.save(scripted_model,'tacotron2_mobile.pt')
</code></pre>
<p>But this gives the following error :</p>
<blockquote>
<p>RuntimeError Traceback (most recent call last)<br>
/tmp/ipykernel_6820/2154414561.py in<br>
10 model.cuda().eval().half()<br>
—&gt; 11 scripted_model = torch.jit.script(model)<br>
12 torch.jit.save(scripted_model,‘tacotron2_mobile.pt’)</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py in script(obj, optimize, _frames_up, _rcb, example_inputs)<br>
1264 obj = call_prepare_scriptable_func(obj)<br>
1265 return torch.jit._recursive.create_script_module(<br>
 → 1266 obj, torch.jit._recursive.infer_methods_to_compile<br>
1267 )<br>
1268</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_script_module(nn_module, stubs_fn, share_types, is_tracing)<br>
452 if not is_tracing:<br>
453 AttributeTypeIsSupportedChecker().check(nn_module)<br>
 → 454 return create_script_module_impl(nn_module, concrete_type, stubs_fn)<br>
455<br>
456 def create_script_module_impl(nn_module, concrete_type, stubs_fn):</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_script_module_impl(nn_module, concrete_type, stubs_fn)<br>
514<br>
515 # Actually create the ScriptModule, initializing it with the function we just defined<br>
 → 516 script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)<br>
517<br>
518 # Compile methods if necessary</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py in _construct(cpp_module, init_fn)<br>
592 “”"<br>
593 script_module = RecursiveScriptModule(cpp_module)<br>
 → 594 init_fn(script_module)<br>
595<br>
596 # Finalize the ScriptModule: replace the nn.Module state with our</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in init_fn(script_module)<br>
492 else:<br>
493 # always reuse the provided stubs_fn to infer the methods to compile<br>
 → 494 scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)<br>
495<br>
496 cpp_module.setattr(name, scripted)</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_script_module_impl(nn_module, concrete_type, stubs_fn)<br>
518 # Compile methods if necessary<br>
519 if concrete_type not in concrete_type_store.methods_compiled:<br>
 → 520 create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)<br>
521 # Create hooks after methods to ensure no name collisions between hooks and methods.<br>
522 # If done before, hooks can overshadow methods that aren’t exported.</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)<br>
369 property_rcbs = [p.resolution_callback for p in property_stubs]<br>
370<br>
 → 371 concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)<br>
372<br>
373 def create_hooks_from_stubs(concrete_type, hook_stubs, pre_hook_stubs):</p>
<h3>RuntimeError: Can’t redefine method: forward on class: <strong>torch</strong>.model.Encoder (of Python compilation unit at: 0x5353200)</h3>
</blockquote>