gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
857 stars 143 forks source link

Trouble importing Roberta #101

Open mortonjt opened 4 years ago

mortonjt commented 4 years ago

Feature request I'm having trouble exporting the Roberta model from the fairseq library to Tensorflow. Below is some example code of how I am currently loading Roberta and my initial attempt to convert it to keras.

from fairseq.models.roberta import RobertaModel as FairseqRobertaModel
 roberta = FairseqRobertaModel.from_pretrained(roberta_checkpoint_path, 'checkpoint_best.pt', data_dir)

 from torch.autograd import Variable
 from pytorch2keras import pytorch_to_keras

 input_np = np.random.uniform(0, 29, (1, 1024))
 input_np.astype(np.int64)
 input_var = Variable(torch.LongTensor(input_np.astype(np.int64)))
 k_model = pytorch_to_keras(roberta.model, input_var, [(1, 1024,)], verbose=True)

I had to hack Roberta to only return tensors (the forward function also returned dictionaries). Once that was done, some of the nodes couldn't convert, namely those with the Equal operator.
Specifically, I get the following error

Traceback (most recent call last):
  File "convert_roberta_original_pytorch_checkpoint_to_pytorch.py", line 62, in <module>
    k_model = pytorch_to_keras(roberta.model, input_var, [(1, 1024,)], verbose=True)
  File "/mnt/home/jmorton/miniconda3/envs/pytorch2keras/lib/python3.7/site-packages/pytorch2keras/converter.py", line 73, in pytorch_to_keras
    verbose=verbose, change_ordering=change_ordering)
  File "/mnt/home/jmorton/miniconda3/envs/pytorch2keras/lib/python3.7/site-packages/onnx2keras/converter.py", line 173, in onnx_to_keras
    AVAILABLE_CONVERTERS[node_type](
KeyError: 'Equal'

Additional context Just so that this message is clear, this is not a problem with pytorch2kera, rather the above KeyError is thrown due to a missing feature in onnx2keras. Specifically, the Equal converter has not yet been implemented here: https://github.com/nerox8664/onnx2keras/blob/master/onnx2keras/layers.py#L18

LamyaMohaned commented 2 years ago

did you find a solution?