huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
132.51k stars 26.4k forks source link

[Error: PyTorch to tf]convert_pytorch_checkpoint_to_tf2: AttributeError: bert.pooler.dense.weight not found in PyTorch model #8766

Closed singhsidhukuldeep closed 3 years ago

singhsidhukuldeep commented 3 years ago

Environment info

Who can help

I think: @patrickvonplaten @LysandreJik @VictorSanh

Anyone is welcome!

Information

Trying to convert my pytorch checkpoint to tf using the below code:

from transformers import convert_pytorch_checkpoint_to_tf2
convert_pytorch_checkpoint_to_tf2.convert_pt_checkpoint_to_tf(
    model_type = "bert", 
    pytorch_checkpoint_path="model/pytorch_model.bin", 
    config_file="model/config.json", 
    tf_dump_path="TFmodel", 
    compare_with_pt_model=False, 
    use_cached_models=False
)

my model folder has a tiny bert trained using HuggingFace: contents of model folder are: checkpoint-500 special_tokens_map.json config.json tokenizer_config.json eval_results_mlm_wwm.txt training_args.bin pytorch_model.bin vocab.txt

To reproduce

Error:

Loading PyTorch weights from /home/3551351/bert-mlm/model/pytorch_model.bin
PyTorch checkpoint contains 8,354,548 parameters
Traceback (most recent call last):
  File "pt2tf.py", line 7, in <module>
    convert_pytorch_checkpoint_to_tf2.convert_pt_checkpoint_to_tf(
  File "/home/3551351/.conda/envs/kuldeepVenv/lib/python3.8/site-packages/transformers/convert_pytorch_checkpoint_to_tf2.py", line 283, in convert_pt_checkpoint_to_tf
    tf_model = load_pytorch_checkpoint_in_tf2_model(tf_model, pytorch_checkpoint_path)
  File "/home/3551351/.conda/envs/kuldeepVenv/lib/python3.8/site-packages/transformers/modeling_tf_pytorch_utils.py", line 96, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(
  File "/home/3551351/.conda/envs/kuldeepVenv/lib/python3.8/site-packages/transformers/modeling_tf_pytorch_utils.py", line 172, in load_pytorch_weights_in_tf2_model
    raise AttributeError("{} not found in PyTorch model".format(name))
AttributeError: bert.pooler.dense.weight not found in PyTorch model

Expected behavior

patrickvonplaten commented 3 years ago

Hey @singhsidhukuldeep,

Could you maybe upload your weights to a colab so that I can reproduce the error or upload your weights to the model hub and give me a path to it?

This way, I can reproduce the error and solve it :-)

Thanks a lot!

singhsidhukuldeep commented 3 years ago

Hi @patrickvonplaten ,

I tried this

pt_model = TFBertForPreTraining.from_pretrained(model_output_location, from_pt=True)

print("\n\n>>> Saving HuggingFace to tensorflow(pb)")
tf.saved_model.save(pt_model,TF_model_output_location)

and it worked, but I am not able to understand the limitations here!

patrickvonplaten commented 3 years ago

If you want to convert from TF to PT PT to TF this is exactly how you should to it...

singhsidhukuldeep commented 3 years ago

@patrickvonplaten I am looking to convert PyTorch to TF!

patrickvonplaten commented 3 years ago

Sorry I meant PT to TF -> your approach is correct here.

singhsidhukuldeep commented 3 years ago

Got it! Thanks for the help.

One last thing, this gives a *.h5 file (weights only) Is there a way to get *.pb file with structure and weights?

pranavpawar3 commented 3 years ago

Hi @singhsidhukuldeep, I tried this, it did save the model in assets, variables, and saved_model.pb format, but couldn't get any .h5 file that I need, am I missing something?

P.S. I am trying to convert a standard config BERT MaskedLM model

Hi @patrickvonplaten ,

I tried this

pt_model = TFBertForPreTraining.from_pretrained(model_output_location, from_pt=True)

print("\n\n>>> Saving HuggingFace to tensorflow(pb)")
tf.saved_model.save(pt_model,TF_model_output_location)

and it worked, but I am not able to understand the limitations here!

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale and been closed because it has not had recent activity. Thank you for your contributions.

If you think this still needs to be addressed please comment on this thread.