pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
84.54k stars 22.77k forks source link

RuntimeError: Can't redefine method: forward on class: __torch__.torch.nn.quantized.modules.conv.Conv1d (of Python compilation unit at: 0x5571a223a770) #52363

Open jerryzh168 opened 3 years ago

jerryzh168 commented 3 years ago

🐛 Bug

scripting test failed in https://github.com/pytorch/pytorch/pull/52316 with error

Falsifying example: test_fn(<quantization.test_quantized_module.TestStaticQuantizedModule testMethod=test_conv1d_api>, batch_size=1, dilation=1, groups=1, in_channels_per_group=2, kernel=1, length=4, out_channels_per_group=2, pad=1, pad_mode='reflect', reference=True, stride=1, use_bias=False, use_channelwise=False, use_fused=False)
Traceback (most recent call last):
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/testing/_internal/common_quantized.py", line 151, in test_fn
    qfunction(*args, **kwargs)
  File "/data/users/jerryzh/pytorch/test/quantization/test_quantized_module.py", line 431, in test_conv1d_api
    Y_zero_point, use_bias, use_fused, use_channelwise, reference)
  File "/data/users/jerryzh/pytorch/test/quantization/test_quantized_module.py", line 340, in _test_conv_api_impl
    check_save_load=True)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/testing/_internal/common_quantization.py", line 459, in checkScriptable
    scripted = torch.jit.script(orig_mod)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_script.py", line 948, in script
    obj, torch.jit._recursive.infer_methods_to_compile
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 398, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 459, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 341, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError: Can't redefine method: forward on class: __torch__.torch.nn.quantized._reference.modules.conv.Conv1d (of Python compilation unit at: 0x5646ff93c1c0)

Falsifying example: test_fn(<quantization.test_quantized_module.TestStaticQuantizedModule testMethod=test_conv1d_api>, batch_size=1, dilation=1, groups=1, in_channels_per_group=2, kernel=1, length=4, out_channels_per_group=2, pad=0, pad_mode='zeros', reference=False, stride=1, use_bias=False, use_channelwise=False, use_fused=False)
Traceback (most recent call last):
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/testing/_internal/common_quantized.py", line 151, in test_fn
    qfunction(*args, **kwargs)
  File "/data/users/jerryzh/pytorch/test/quantization/test_quantized_module.py", line 431, in test_conv1d_api
    Y_zero_point, use_bias, use_fused, use_channelwise, reference)
  File "/data/users/jerryzh/pytorch/test/quantization/test_quantized_module.py", line 340, in _test_conv_api_impl
    check_save_load=True)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/testing/_internal/common_quantization.py", line 459, in checkScriptable
    scripted = torch.jit.script(orig_mod)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_script.py", line 948, in script
    obj, torch.jit._recursive.infer_methods_to_compile
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 398, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 459, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 341, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError: Can't redefine method: forward on class: __torch__.torch.nn.quantized.modules.conv.Conv1d (of Python compilation unit at: 0x5646ff93c1c0)

To Reproduce

ghstack checkout https://github.com/pytorch/pytorch/pull/52316
python setup develop
python test/test_quantization.py TestStaticQuantizedModule.test_conv1d_api

Expected behavior

Test should pass

cc @gmagogsfm

ansley commented 3 years ago

Possible reason: hypothesis testing module?

jerryzh168 commented 3 years ago

Thanks @ansley, yes, this is resolved, this error is caused by hypothesis. cc @zdevito

WUSHUANGPPP commented 3 years ago

@jerryzh168 Hi,I met similar problem and the results are:

WUSHUANGPPP commented 3 years ago
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 167, in forward
    outputs = self.parallel_apply(replicas, inputs, kwargs)
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 177, in parallel_apply
    return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 86, in parallel_apply
    output.reraise()
  File "/usr/local/lib64/python3.6/site-packages/torch/_utils.py", line 429, in reraise
    raise self.exc_type(msg)
RuntimeError: Caught RuntimeError in replica 0 on device 0.
Original Traceback (most recent call last):
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 61, in _worker
    output = module(*input, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/apdcephfs/private_colyban/3D-respool-4Branch-trypara3/models/resnet.py", line 227, in forward
    self.module3 = torch.jit.script(self.module3)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 943, in script
    obj, torch.jit._recursive.infer_methods_to_compile
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 391, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 452, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 335, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError: Can't redefine method: forward on class: __torch__.torch.nn.modules.fold.Unfold (of Python compilation unit at: 0x3dea450)

This problem have stuck me for weeks.Could you tell me how to solve this?I still puzzled about how to solve this problem.Thank you!

gmagogsfm commented 3 years ago
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 167, in forward
    outputs = self.parallel_apply(replicas, inputs, kwargs)
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 177, in parallel_apply
    return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 86, in parallel_apply
    output.reraise()
  File "/usr/local/lib64/python3.6/site-packages/torch/_utils.py", line 429, in reraise
    raise self.exc_type(msg)
RuntimeError: Caught RuntimeError in replica 0 on device 0.
Original Traceback (most recent call last):
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 61, in _worker
    output = module(*input, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/apdcephfs/private_colyban/3D-respool-4Branch-trypara3/models/resnet.py", line 227, in forward
    self.module3 = torch.jit.script(self.module3)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 943, in script
    obj, torch.jit._recursive.infer_methods_to_compile
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 391, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 452, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "/usr/local/lib64/python3.6/site-packages/torch/jit/_recursive.py", line 335, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError: Can't redefine method: forward on class: __torch__.torch.nn.modules.fold.Unfold (of Python compilation unit at: 0x3dea450)

This problem have stuck me for weeks.Could you tell me how to solve this?I still puzzled about how to solve this problem.Thank you!

Hi WUSHUANGPPP,

Could you please provide a minimal repro for the issue? Also, do you mind open a separate issue for easier tracking? Though the error message in your case is similar to this issue, they are different root causes for sure.

I can only infer very limited information from the stack trace you provided. One anti-pattern is calling torch.jit.script inside forward, this is not allowed because forward is executed many times while you only want to torch.jit.script once.

WUSHUANGPPP commented 3 years ago

Thanks for you response!@gmagogsfm, When I moved the torch.jit.script outside of forward the problem solved. But I got new error after I solve several bugs yesterday. Now I 'll open a new issue for that.

wangzhaode commented 3 years ago

@gmagogsfm Hi, I met similar problem and result as below: >>> bert_model_ts = torch.jit.script(model, input_idx) /usr/local/lib/python3.8/site-packages/torch/jit/_script.py:1084: UserWarning:optimizeis deprecated and has no effect. Usewith torch.jit.optimized_execution() instead warnings.warn( Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/site-packages/torch/jit/_script.py", line 1096, in script return torch.jit._recursive.create_script_module( File "/usr/local/lib/python3.8/site-packages/torch/jit/_recursive.py", line 412, in create_script_module return create_script_module_impl(nn_module, concrete_type, stubs_fn) File "/usr/local/lib/python3.8/site-packages/torch/jit/_recursive.py", line 474, in create_script_module_impl script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn) File "/usr/local/lib/python3.8/site-packages/torch/jit/_script.py", line 497, in _construct init_fn(script_module) File "/usr/local/lib/python3.8/site-packages/torch/jit/_recursive.py", line 452, in init_fn scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn) File "/usr/local/lib/python3.8/site-packages/torch/jit/_recursive.py", line 478, in create_script_module_impl create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs) File "/usr/local/lib/python3.8/site-packages/torch/jit/_recursive.py", line 355, in create_methods_and_properties_from_stubs concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults) RuntimeError: Can't redefine method: forward on class: torch.transformers.models.bert.modeling_bert.BertEmbeddings (of Python compilation unit at: 0x7fd85f7e1578)`

analyticsbot commented 3 years ago

Did you get an answer to the above issue? I'm also stuck there. @wangzhaode @gmagogsfm

wangzhaode commented 3 years ago

Did you get an answer to the above issue? I'm also stuck there. @wangzhaode @gmagogsfm

NO!I'm using trace mode now.

gmagogsfm commented 3 years ago

@wangzhaode Could you please provide minimal steps to repro? And also your Pytorch configuration according to bug report template? Without them, it is difficult to us to investigate. Thanks

wangzhaode commented 3 years ago

@gmagogsfm Tanks for your reply! My torch version is 1.9.0, and model is from transformer. The step is as below:

  1. install transformer use this command: pip install transformers
  2. use these code export torchscript:
    
    from transformers import BertModel, BertTokenizer, BertConfig
    import torch

enc = BertTokenizer.from_pretrained("bert-base-uncased")

text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]" tokenized_text = enc.tokenize(text)

masked_index = 8 tokenized_text[masked_index] = '[MASK]' indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]

tokens_tensor = torch.tensor([indexed_tokens]) segments_tensors = torch.tensor([segments_ids]) dummy_input = [tokens_tensor, segments_tensors]

config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, torchscript=True)

model = BertModel(config)

model.eval()

model = BertModel.from_pretrained("bert-base-uncased", torchscript=True)

script_model = torch.jit.script(model, [tokens_tensor, segments_tensors]) torch.jit.save(script_model, "script_bert.pt")

FuyuanChen commented 1 year ago

Hi, guy, Have you resolved this problem.

akasharidas commented 1 year ago

I have a similar error as @wangzhaode when trying to script a Huggingface diffusers model. Any help on this would be appreciated.

kabyanil commented 9 months ago

Thanks for you response!@gmagogsfm, When I moved the torch.jit.script outside of forward the problem solved. But I got new error after I solve several bugs yesterday. Now I 'll open a new issue for that.

How did you 'move' the torch.jit.script 'outside' of forward?

I am trying to save a model using torch.jit.script(model). It is throwing

RuntimeError: Can't redefine method: forward on class: __torch__.fairseq.modules.transformer_layer.TransformerDecoderLayerBase (of Python compilation unit at: 0x7f94cf4d60e8)

ckolluru commented 7 months ago

I guess I have a similar problem when trying to export an image segmentation model (nnUNet).


model_scripted = torch.jit.script(model)

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_script.py", line 1324, in script
    return torch.jit._recursive.create_script_module(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_recursive.py", line 559, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_recursive.py", line 632, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_script.py", line 639, in _construct
    init_fn(script_module)
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_recursive.py", line 608, in init_fn
    scripted = create_script_module_impl(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_recursive.py", line 636, in create_script_module_impl
    create_methods_and_properties_from_stubs(
  File "/home/chaitanya.kolluru/miniconda3/envs/rsip-env/lib/python3.11/site-packages/torch/jit/_recursive.py", line 469, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(
RuntimeError: Can't redefine method: forward on class: __torch__.dynamic_network_architectures.building_blocks.plain_conv_encoder.PlainConvEncoder (of Python compilation unit at: 0x676bc80)