12

Is that possible to generate texts from OpenAI GPT-2 using TensorFlowJS?

If not what is the limitation, like model format or ...?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
jay
  • 477
  • 5
  • 13

2 Answers2

8

I don't see any reason as to why not, other than maybe some operation that is in gpt-2 that is not supported by tensorflowjs.

I don't know how to do it, but here's a nice starting point:

install.sh

python3 -m pip install -q git+https://github.com/huggingface/transformers.git
python3 -m pip install tensorflow

save.py

from transformers import TFGPT2LMHeadModel, GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# add the EOS token as PAD token to avoid warnings
model = TFGPT2LMHeadModel.from_pretrained("gpt2", pad_token_id=tokenizer.eos_token_id)
model.save("./test_gpt2")

that will give you a SavedModel file. Now you can try figure out the input and output nodes, and use tensorflowjs_converter to try and convert it. Pointer: https://www.tensorflow.org/js/tutorials/conversion/import_saved_model.

Frederik Bode
  • 2,632
  • 1
  • 10
  • 17
2

It's possible. Maybe someone finds this useful in 2023:

  • One way to achieve this is to convert a TF model with tensorflowjs-converter as Frederik described (possible problem with this approach is missing custom layers)

  • Use gpt-tfjs - implementation of GPT model in TensorFlow.js. It's possible to load weights directly from HF (example). I developed it to experiment with model training in the browser.

If you just want to generate text without training, you have more options:

  • Use transformers.js or ONNX in general. The lib is great and follows Python's transformers library API. Unfortunately - inference only.
  • Use ggml + WASM. It's a C/C++ model implementation compiled to WebAssembly (example, talk)