Show HN: GPT-2 implemented using graphics shaders

https://news.ycombinator.com/rss Hits: 21
Summary

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