# Converting GPT2 to JavaScript?

**URL:** <https://discuss.huggingface.co/t/converting-gpt2-to-javascript/5576>\
**Category:** Intermediate\
**Created:** [April 17, 2021, 3:02pm UTC](https://discuss.huggingface.co/t/converting-gpt2-to-javascript/5576 "2021-04-17T15:02:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TristanBehrens](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@TristanBehrens](https://discuss.huggingface.co/u/TristanBehrens)\
**Post date:** [April 17, 2021, 3:02pm UTC](https://discuss.huggingface.co/t/converting-gpt2-to-javascript/5576/1 "2021-04-17T15:02:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](https://discuss.huggingface.co/u/lewtun)\
**Post date:** [April 17, 2021, 4:08pm UTC](https://discuss.huggingface.co/t/converting-gpt2-to-javascript/5576/2 "2021-04-17T16:08:59Z")

</div>

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.

```auto
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](https://huggingface.co/transformers/serialization.html#onnx-onnxruntime) for the export), but your approach seems simpler 🙂
