GPT-2 WebGL Inference Demo A browser-based, WebGL2 implementation of GPT-2. π Features Full GPT-2 small (117M) forward pass in the GPU via WebGL2 shaders BPE tokenization using js-tiktoken in the browser (no WASM fetch) in the browser (no WASM fetch) Simple Python script to download the pretrained weights π Prerequisites Node.js β₯ 16.x and npm β₯ 16.x and Python β₯ 3.8 β₯ 3.8 A modern browser with WebGL2 support (Chrome, Firefox, Safari, Edge) π Download the GPT-2 Weights 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: python download_weights.py wte.bin (token embeddings) (token embeddings) wpe.bin (positional embeddings) (positional embeddings) c_attn_q_w_0.bin β¦ c_attn_q_w_11.bin β¦ c_attn_k_w_0.bin β¦ etc. β¦ etc. lm_head_w.bin , lm_head_b.bin , And a generated manifest.json mapping names β URLs βοΈ Front-end Setup with Vite We use Vite to bundle TS, serve ESM modules & handle js-tiktoken : Install JS dependencies: npm install Start the local dev server: npm run dev Open your browser at http://localhost:5173 Any changes under src/ will trigger HMR and live-reload. π Project Structure . βββ 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 π License MIT
First seen: 2025-05-02 16:41
Last seen: 2025-05-03 12:44