Converting GPT2 to JavaScript?

Hello everyone,

I am contemplating converting my trained GPT2 model to JavaScript to go wild. The model has been trained in its PyTorch variant.

I have an idea of how to map it to JavaScript and would like to ask your opinion.

Maybe this is a little to much overkill:

  1. Save PyTorch GPT2 as pretrained.
  2. Load the TensorFlow GPT2 from the file I have just saved. (Believing the binaries are framework agnostic).
  3. Convert the TensorFlow model to TensorflowJS.

Would that work?

1 Like

hey @TristanBehrens, your approach sounds sensible (although i don’t know anything about the TF → TFJS step). to load the pytorch model in tf, you’ll just need to add a from_pt=True argument, e.g.

tf_model = TFDistilBertForSequenceClassification.from_pretrained("path/to/pytorch-model", from_pt=True)

an alternative would be to use onnx as the intermediate representation (see here for the export), but your approach seems simpler :slight_smile: