A browser-based, WebGL2 implementation of GPT-2.
- Full GPT-2 small (117M) forward pass in the GPU via WebGL2 shaders
- BPE tokenization using
js-tiktoken
in the browser (no WASM fetch) - Simple Python script to download the pretrained weights
- Node.js ≥ 16.x and npm
- Python ≥ 3.8
- A modern browser with WebGL2 support (Chrome, Firefox, Safari, Edge)
We rely on HuggingFace’s transformers
to pull down the official GPT-2 weights and emit raw Float32Array
blobs:
- Install Python dependencies:
pip install torch numpy transformers
- Run the downloader:
This will fetch:
python download_weights.py
wte.bin
(token embeddings)wpe.bin
(positional embeddings)c_attn_q_w_0.bin
…c_attn_q_w_11.bin
c_attn_k_w_0.bin
… etc.lm_head_w.bin
,lm_head_b.bin
- And a generated
manifest.json
mapping names → URLs
We use Vite to bundle TS, serve ESM modules & handle js-tiktoken
:
- Install JS dependencies:
- Start the local dev server:
- Open your browser at
Any changes under src/
will trigger HMR and live-reload.
.
├── public/ # static assets served at `/`
│ └── weights/ # GPT-2 weight binaries + manifest.json
├── src/
│ ├── gpt2_webgl.ts # WebGL2 inference + shaders + tokenizer
│ └── main.ts # bootstrap: loads manifest, sets up UI
├── download_weights.py # Python script to fetch & dump weights
├── index.html # (copied by Vite) entrypoint HTML
├── vite.config.ts # Vite config
├── package.json
└── tsconfig.json
MIT