[Error: PyTorch to tf]convert_pytorch_checkpoint_to_tf2: AttributeError: bert.pooler.dense.weight not found in PyTorch model
Author: singhsidhukuldeep
Pull request: no
Labels: name: wontfix

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 3.5.1
- Platform: Linux-5.4.0-1029-gcp-x86_64-with-glibc2.10
- Python version: 3.8.5
- PyTorch version (GPU?): 1.7.0 (False)
- Tensorflow version (GPU?): 2.3.1 (False)
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

### Who can help

I think:
 @patrickvonplaten @LysandreJik @VictorSanh

Anyone is welcome!

<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @
 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.
 
 albert, bert, GPT2, XLM: @LysandreJik 
 tokenizers: @mfuntowicz
 Trainer: @sgugger
 Speed and Memory Benchmarks: @patrickvonplaten
 Model Cards: @julien-c
 TextGeneration: @TevenLeScao 
 examples/distillation: @VictorSanh
 nlp datasets: [different repo](https://github.com/huggingface/nlp)
 rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)
 Text Generation: @patrickvonplaten @TevenLeScao
 Blenderbot: @patrickvonplaten
 Bart: @patrickvonplaten
 Marian: @patrickvonplaten
 Pegasus: @patrickvonplaten
 mBART: @patrickvonplaten
 T5: @patrickvonplaten
 Longformer/Reformer: @patrickvonplaten
 TransfoXL/XLNet: @TevenLeScao
 RAG: @patrickvonplaten, @lhoestq
 FSMT: @stas00
 examples/seq2seq: @patil-suraj
 examples/bert-loses-patience: @JetRunner
 tensorflow: @jplu
 examples/token-classification: @stefan-it
 documentation: @sgugger
 -->

## Information

Trying to convert my pytorch checkpoint to tf using the below code:

```python
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
```

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->


Comments:
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!

Hi @patrickvonplaten ,

I tried this
```Python
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!

If you want to convert from <del>TF to PT</del> PT to TF this is exactly how you should to it...

@patrickvonplaten I am looking to convert PyTorch to TF!

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

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?

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
> 
> ```python
> 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!



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.