

TITLE
Converting models for tensoflowjs (node)

COMMENTS
5

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

BODY
## Environment info
- `transformers` version: 4.3.3
- Platform: Linux-5.11.2-zen1-1-zen-x86_64-with-glibc2.2.5
- Python version: 3.8.8 (also 3.9.2 but tensorflowjs is not available in 3.9.2)
- PyTorch version (GPU?): 1.7.1 (True)
- Tensorflow version (GPU?): 2.4.1 (False)
- Using GPU in script?: No 
- Using distributed or parallel set-up in script?: No

### Who can help
- tensorflow: @jplu

## Information

Model I am using (Bert, XLNet ...): Any tensorflow ones

The problem arises when using:
* [X] the official example scripts: (give details below)
* [X] my own modified scripts: (give details below)

The tasks I am working on is:
* [X] my own task or dataset: (give details below)

## To reproduce

Steps to reproduce the behavior:

1. Load a tensforflow model (e.g : `model = TFAutoModel.from_pretrained("distilbert-base-uncased")`) 
2. Convert it to H5 with `model.save_pretrained(path)`
3. Try to load it in tensorflowjs and get `UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'model_config' of null`

## Expected behavior
Be able to load a model in the LayerModel format as it's the only one which allows finetuning. 

## More informations. 
I know the question was already posted in https://github.com/huggingface/transformers/issues/4073

I need to do finetuning so onnx, graphModels & cie should be avoided. 

Seems that the H5 model just need the config file which seems saved on the side with the custom HF script. 
I went to read some issues on tensorflowjs (exemple : [this](https://github.com/tensorflow/tfjs/issues/2455) or [that](https://github.com/tensorflow/tfjs/issues/931)) and the problem is that the HF model contains only the weights and not the architecture. The goal would be to adapt the `save_pretrained` function to save the architecture as well. I guess it's complex because of the `Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model.` error in described bellow. 

Seems also that only H5 model can be converted to a trainable LayerModel. 

I'm willing to work on a PR or to help as i'm working on a web stack (nodejs) and I need this. 

I made a drawing of all models (that I'm aware of) to summarize loading converting : 
![image](https://user-images.githubusercontent.com/30974685/109985131-6edebe80-7cd2-11eb-8f3c-60080e98a115.png)

## Also tried : 
Use the nodejs `tf.node.loadSavedModel` which return only a saved model which I cannot use as the base structure with something like : 
```
const bert = await tf.loadLayersModel(`file://${this._bert_model_path}/model.json`)
this._clf = tf.sequential();
this._clf.add(bert); // Raise Error
this._clf.add(tf.layers.dense({ units: 768, useBias: true }));
etc...
this._clf.compile(....)
this._clf.train(...)
```

Look for other libraries to train models (libtorch : incomplete, onnx training: only in python etc..) 

Should I also write an issue on tensorflowjs ? 

Thanks in advance for you time and have a great day. 