Repository: pytorch
Issue: mobile deployment: optimized_for_mobile---wrap_cpp_module error
Labels: oncall: mobile,mobile_perf

pytorch: 1.6.0

    def convert_to_mobile(self):
        logging.info("mode: convert_to_mobile")

        self.net.eval()

        logging.info("step1: quantize model")
        quantized_model = self.quantize_model()
        # quantized_model = self.net

        logging.info("step2: script/trace model")
        traced_script_model = torch.jit.script(quantized_model)

        logging.info("step3: model optimization")
        traced_script_model_optimized = optimize_for_mobile(traced_script_model)


**__init__(): incompatible constructor arguments. The following argument types are supported:
    1. torch._C.ModuleDict(arg0: torch._C.ScriptModule)**

Invoked with: False

Traceback (most recent call last):
  File "../main.py", line 38, in start_engine
    keypoint_detector.convert_to_mobile()
  File "/data/fanchunbo/project/keypoint_det/src/core/keypoint_detector.py", line 784, in convert_to_mobile
    traced_script_model_optimized = optimize_for_mobile(traced_script_model)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/mobile_optimizer.py", line 34, in optimize_for_mobile
    return torch.jit._recursive.wrap_cpp_module(optimized_cpp_module)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/_recursive.py", line 594, in wrap_cpp_module
    return torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/__init__.py", line 1900, in _construct
    init_fn(script_module)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/_recursive.py", line 592, in init_fn
    setattr(script_module, name, wrap_cpp_module(cpp_module))
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/_recursive.py", line 594, in wrap_cpp_module
    return torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/__init__.py", line 1900, in _construct
    init_fn(script_module)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/_recursive.py", line 592, in init_fn
    setattr(script_module, name, wrap_cpp_module(cpp_module))
  File "/root/miniconda3/lib/python3.8/site-packages/torch/jit/_recursive.py", line 594, in wrap_cpp_module
    return torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. torch._C.ModuleDict(arg0: torch._C.ScriptModule)

Invoked with: False

**when I print some info like this:**

    def wrap_cpp_module(cpp_module):
            def init_fn(script_module):
               print(torch._C.ModuleDict(script_module._c).items())
                for name, cpp_module in torch._C.ModuleDict(script_module._c).items():
                    setattr(script_module, name, wrap_cpp_module(cpp_module))
                script_module._concrete_type = torch._C.ConcreteModuleType.from_jit_type(script_module._c._type())
            return torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)

**the print info like this:**

dict: [('module0', <torch._C.ScriptModule object at 0x7f663d2e9b70>)]
dict: [('branches', <torch._C.ScriptModule object at 0x7f663d2e9bf0>), ('fuse_layers', <torch._C.ScriptModule object at 0x7f663d2e9370>)]
dict: [('branch0', <torch._C.ScriptModule object at 0x7f663d2e90b0>), ('branch1', <torch._C.ScriptModule object at 0x7f663d2e9d30>)]
dict: [('blk0', <torch._C.ScriptModule object at 0x7f671b336c70>)]
dict: [('dw_conv1', <torch._C.ScriptModule object at 0x7f671b291630>), ('dw_conv2', <torch._C.ScriptModule object at 0x7f671b291570>)]
dict: [('dw_conv', <torch._C.ScriptModule object at 0x7f663d33eab0>), ('pw_conv', <torch._C.ScriptModule object at 0x7f663d33e530>)]
dict: []
dict: []
dict: [('dw_conv', <torch._C.ScriptModule object at 0x7f671b325d70>), ('pw_conv', <torch._C.ScriptModule object at 0x7f663d2c97f0>)]
dict: []
dict: []
dict: [('blk0', <torch._C.ScriptModule object at 0x7f663d2cb170>)]
**dict: [('dw_conv1', False), ('dw_conv2', 'relu')]**



but there is no such problem for pytorch 1.9.1, why?