question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModuleAttributeError occurs during Converting TensorFlow Checkpoints (BERT)

See original GitHub issue

Environment info

  • transformers version: 4.1.1
  • Platform: Linux-4.15.0-129-generic-x86_64-with-glibc2.10
  • Python version: 3.8.3
  • PyTorch version (GPU?): 1.7.0 (True)
  • Tensorflow version (GPU?): 2.3.1 (True)
  • Using GPU in script?: <fill in>
  • Using distributed or parallel set-up in script?: <fill in>

Who can help

albert, bert, GPT2, XLM: @LysandreJik

Information

Model I am using (Bert, XLNet …): Bert

The problem arises when using:

  • the official example scripts: (give details below)
  • my own modified scripts: (give details below)

The tasks I am working on is:

  • Convert TF v1 ckpt to PyTorch

To reproduce

I tried to convert a TensorFlow checkpoint, but ModuleAttributeError occurred.

What I run:

****@**** $ transformers-cli convert --model_type bert \
>   --tf_checkpoint $MODEL_DIR/model.ckpt \
>   --config ****/bert_config.json \
>   --pytorch_dump_output $MODEL_DIR/pytorch_model.bin

(In this time, bert_config.json is in a separate folder, but it corresponds to the ckpt.)

Output is:

Traceback (most recent call last):
  File "/****/.pyenv/versions/anaconda3-2020.07/bin/transformers-cli", line 8, in <module>
    sys.exit(main())
  File "/****/.pyenv/versions/anaconda3-2020.07/lib/python3.8/site-packages/transformers/commands/transformers_cli.py", line 51, in main
    service.run()
  File "/****/.pyenv/versions/anaconda3-2020.07/lib/python3.8/site-packages/transformers/commands/convert.py", line 105, in run
    convert_tf_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output)
  File "/****/.pyenv/versions/anaconda3-2020.07/lib/python3.8/site-packages/transformers/models/bert/convert_bert_original_tf_checkpoint_to_pytorch.py", line 36, in convert_tf_checkpoint_to_pytorch
    load_tf_weights_in_bert(model, config, tf_checkpoint_path)
  File "/****/.pyenv/versions/anaconda3-2020.07/lib/python3.8/site-packages/transformers/models/bert/modeling_bert.py", line 155, in load_tf_weights_in_bert
    pointer.shape == array.shape
  File "/****/.pyenv/versions/anaconda3-2020.07/lib/python3.8/site-packages/torch/nn/modules/module.py", line 778, in __getattr__
    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'BertEmbeddings' object has no attribute 'shape'

Expected behavior

I think it is not strange that BertEmbeddings (nn.Module) doesn’t have shape.

Is it possible to get such an error depending on the original TensorFlow checkpoint?  In such a case, is there any tips to deal with it?

I really appreciate any help you can provide.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:21 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
LysandreJikcommented, Jan 25, 2021

Fantastic! Great job, thank you for sharing your progress!

1reaction
LysandreJikcommented, Jan 20, 2021

Hmmm indeed it seems that the model doesn’t fit one-to-one to our architecture. You might need to slightly tweak the architecture and conversion script to load it, but you’re probably the most expert on the matter. If you want me to take a deeper look, feel free to send me the weights/config so I can take a look locally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error converting tensorflow checkpoints · Issue #8242 - GitHub
I'm trying to convert a BERT Tensorflow checkpoint to hugging face model ... 1) or '1type' as a synonym of type is deprecated;...
Read more >
Converting From Tensorflow Checkpoints - Hugging Face
A command-line interface is provided to convert original Bert/GPT/GPT-2/Transformer-XL/XLNet/XLM checkpoints to models that can be loaded using the ...
Read more >
Error in converting tensorflow BERT checkpoints to pytorch
When relative path is specified, the model is unable to find the corresponding files. Share.
Read more >
tf.train.Checkpoint | TensorFlow v2.11.0
with named edges, and this graph is used to match variables when restoring a checkpoint. It can be more robust to changes in...
Read more >
annotated: convert bert checkpoint - Kaggle
/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found