You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This workspace is currently used to convert ONNX [GPT-2 model](https://huggingface.co/Xenova/gpt2) to Tensorflow.js. On the one hand, ONNX allows converting pretrained models from PyTorch or Tensorflow to the ONNX format, therefore there currently exists many pretrained models in ONNX format. However, ONNX libraries currently only support inference. On the other hand, Tensorflow.js doesn't have a converter that can handle recent Transformers models (despite having a [converter](https://github.com/tensorflow/tfjs/tree/master/tfjs-converter)), but TF.js allows further training models.
4
+
5
+
Therefore, we want to convert pretrained models such as GPT-2 from ONNX format to Tensorflow.js to further fine-tune them. You generate a TF.js `model.json` by running `npm run convert_onnx` in this workspace.
6
+
7
+
What the script does is:
8
+
1. Read the ONNX GPT-2 model from [Xenova's repository](https://huggingface.co/Xenova/gpt2)
9
+
2. Use the ONNX protobuf definition to read the file and iterate through the model layers. The ONNX JavaScript protobuf comes from [this repository](https://github.com/microsoft/onnxruntime/blob/main/js/web/lib/onnxjs/).
10
+
3. Convert all weights to TF.js tensors
11
+
4. Init a TF.js model with the loaded weights and export the model
12
+
13
+
Running `npm run convert_onnx` creates a GPT-tfjs `model.json` file in the `./assets/` folder.
14
+
15
+
## ONNX JS protobuf
16
+
17
+
The ONNX specification has limited support in JavaScript. We found an old JS implementation in the [ONNX Runtime Web repository](https://github.com/microsoft/onnxruntime/tree/main/js/web/lib/onnxjs/ort-schema/protobuf). We had to adapt their files as follows to be compatible with our newer environment:
18
+
1. Copy `onnx.js` and `onnx.d.ts` from [the repository](https://github.com/microsoft/onnxruntime/tree/main/js/web/lib/onnxjs/ort-schema/protobuf) in `./onnx-converter/src/protobuf`
19
+
2. Rename `onnx.js` to `onnx.cjs`
20
+
3. Create `onnx-proto.js` as a wrapper around the protobuf definition:
21
+
```js
22
+
import { createRequire } from'module';
23
+
constrequire=createRequire(import.meta.url);
24
+
constonnxModule=require('./onnx.cjs');
25
+
26
+
exportconstonnx=onnxModule.onnx;
27
+
exportdefaultonnxModule;
28
+
```
29
+
4. Create `onnx-proto.d.ts` with the matching TypeScript definition:
0 commit comments