NVIDIA / tacotron2

Tacotron 2 - PyTorch implementation with faster-than-realtime inference
BSD 3-Clause "New" or "Revised" License
5.06k stars 1.38k forks source link

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

Open payymann opened 1 year ago

payymann commented 1 year ago

Hi, I'm trying to port my trained tacotron2 model to android, for this I've written the following code :

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')

But this gives the following error :

RuntimeError Traceback (most recent call last) /tmp/ipykernel_6820/2154414561.py in 10 model.cuda().eval().half() ---> 11 scripted_model = torch.jit.script(model) 12 torch.jit.save(scripted_model,'tacotron2_mobile.pt')

/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py in script(obj, optimize, _frames_up, _rcb, example_inputs) 1264 obj = call_prepare_scriptable_func(obj) 1265 return torch.jit._recursive.create_script_module( -> 1266 obj, torch.jit._recursive.infer_methods_to_compile 1267 ) 1268

/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_script_module(nn_module, stubs_fn, share_types, is_tracing) 452 if not is_tracing: 453 AttributeTypeIsSupportedChecker().check(nn_module) --> 454 return create_script_module_impl(nn_module, concrete_type, stubs_fn) 455 456 def create_script_module_impl(nn_module, concrete_type, stubs_fn):

/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_script_module_impl(nn_module, concrete_type, stubs_fn) 514 515 # Actually create the ScriptModule, initializing it with the function we just defined --> 516 script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn) 517 518 # Compile methods if necessary

/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py in _construct(cpp_module, init_fn) 592 """ 593 script_module = RecursiveScriptModule(cpp_module) --> 594 init_fn(script_module) 595 596 # Finalize the ScriptModule: replace the nn.Module state with our

/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in init_fn(script_module) 492 else: 493 # always reuse the provided stubs_fn to infer the methods to compile --> 494 scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn) 495 496 cpp_module.setattr(name, scripted)

/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_script_module_impl(nn_module, concrete_type, stubs_fn) 518 # Compile methods if necessary 519 if concrete_type not in concrete_type_store.methods_compiled: --> 520 create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs) 521 # Create hooks after methods to ensure no name collisions between hooks and methods. 522 # If done before, hooks can overshadow methods that aren't exported.

/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py in create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs) 369 property_rcbs = [p.resolution_callback for p in property_stubs] 370 --> 371 concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults) 372 373 def create_hooks_from_stubs(concrete_type, hook_stubs, pre_hook_stubs):

RuntimeError: Can't redefine method: forward on class: torch.model.Encoder (of Python compilation unit at: 0x5353200)