

TITLE
How to convert the bert model in tf1 ckpt format which converted by the pytorch version to tf1/tf2 SavedModel?

COMMENTS
22

REACTIONS
+1: 0
-1: 0
laugh: 0
hooray: 0
heart: 0
rocket: 0
eyes: 0

BODY
### System Info

```shell
- `transformers` version: 4.2.1
- Platform: Linux-3.10.0-957.el7.x86_64-x86_64-with-debian-buster-sid
- Python version: 3.6.13
- PyTorch version (GPU?): 1.10.0+cu102 (True)
- Tensorflow version (GPU?): 2.3.0 (True)
- Using GPU in script?: <yes>
- Using distributed or parallel set-up in script?: <no>
```


### Who can help?

@LysandreJik

### Information

- [X] The official example scripts
- [X] My own modified scripts

### Tasks

- [x] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

1.follow the official demo convert_bert_pytorch_checkpoint_to_original_tf.py to **convert pytorch bert model to tensorflow1 ckpt model**
2.then try to **convert bert.ckpt to SavedModel** format,:
`
def convert_tf1_ckpt_to_saved_model(ckpt_dir):
    # ckpt to SavedModel
    saver = tf.compat.v1.train.import_meta_graph(ckpt_dir + "/bert.ckpt.meta")
    input_ids = tf.compat.v1.placeholder(tf.int32, [None, 64], name='input_ids')
    attention_mask = tf.compat.v1.placeholder(tf.int32, [None, 64], name='attention_mask')
    inputs = {"input_ids": input_ids, "attention_mask": attention_mask}
    session = tf.compat.v1.Session()
    saver.restore(session, ckpt_dir + "/bert.ckpt")
    output = tf.compat.v1.placeholder(tf.float32, [None, 64], name='output')
    # output = session.run(output, feed_dict=inputs)
    tf.compat.v1.saved_model.simple_save(session, ckpt_dir + "/savedmodel",
                                         inputs=inputs,
                                         outputs={"output": output})
`
The code in step 2 is probably wrong because **the inputs and outputs are not specified correctly**. I followed this script [https://stackoverflow.com/questions/44251328/tensorflow-print-all-placeholder-variable-names-from-meta-graph](url) to **get inputs and outputs of the meta graph**. But, it did not work.

3. load the savedmodel format bert, i got an error:
`tf_model = tf.saved_model.load("./savedmodel")`
error info:
tensorflow.python.ops.op_selector.UnliftableError: A SavedModel signature needs an input for each placeholder the signature's outputs use. An output for signature 'serving_default' depends on a placeholder which is not an input (i.e. the placeholder is not fed a value).

Unable to lift tensor <tf.Tensor 'output:0' shape=(None, 64) dtype=float32> because it depends transitively on placeholder <tf.Operation 'output' type=Placeholder> via at least one path, e.g.: output (Placeholder)

### Expected behavior

```shell
The bert model in tf1 ckpt format converted by the pytorch version can be correctly converted to tf1/tf2 SavedModel format, and the SavedModel version model can be loaded and deployed correctly.
```
