# 0hq/webgpt architecture

> Architecture diagram of the GitHub repository 0hq/webgpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0hq/webgpt
- Repository: https://github.com/0hq/webgpt

## Overview

##### Project type and purpose
- Project type: A client-side, single-page web application (vanilla HTML + JavaScript) that implements a transformer model inference engine using WebGPU.
- Purpose: Educational/proof-of-concept demo (“WebGPT”) showing how to load model weights, tokenize input, run inference on the GPU, and render results entirely in the browser. It also includes offline conversion scripts to prepare PyTorch checkpoints for browser consumption.

##### File structure
- Top-level:
- index.html – application entry point
- globals.js – constants, WebGPU adapter/device initialization
- tokenizer.js – text ↔ token encoding/decoding
- model.js & condensed/condensed.js – core transformer inference logic (attention, MLP blocks, layer norms)
- visuals.js & instructions.js – UI for input, controls, result rendering
- weights/ – binary weight files and JSON parameter descriptors for GPT-2 and “better_shakespeare” toy model
- other/conversion_scripts/ – Python scripts to convert PyTorch checkpoints into browser-loadable weights
- other/validation/ – JSON test sets and JS to verify correctness
- Patterns:
- Modular JS: separate concerns (tokenization, model math, UI)
- WebGPU pipelines: kernels fused for matmuls & transformer ops
- Offline vs. runtime: conversion scripts run offline; browser loads static binaries via Git LFS

##### README for additional insights
- Confirms WebGPU usage, performance stats, supported model sizes
- Highlights conversion scripts for custom models
- Roadmap shows where compute kernels and buffers are managed

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Browser UI Layer
    subgraph "Browser UI Layer"
        direction TB
        UI_HTML["index.html"]:::frontend
        UI_Inst["instructions.js"]:::frontend
        UI_Vis["visuals.js"]:::frontend
    end

    %% Tokenization Module
    subgraph "Tokenization Module"
        direction TB
        Tokenizer["tokenizer.js"]:::frontend
        GPT_TOK["gpt_tokens.json"]:::weight
        SIMPLE_TOK["simple_tokens.json"]:::weight
        VOCAB_BPE["vocab.bpe"]:::weight
    end

    %% Model Inference Engine
    subgraph "Model Inference Engine"
        direction TB
        Globals["globals.js"]:::frontend
        ModelCore["model.js"]:::frontend
        Condensed["condensed.js"]:::frontend
    end

    %% WebGPU Compute Kernels
    subgraph "WebGPU Compute Kernels"
        direction TB
        Kernels["Fused MatMul & Transformer Ops"]:::gpu
        GPU_Buffers["GPU Buffers & Pipelines"]:::gpu
    end

    %% Weight Loader
    subgraph "Weight Loader"
        direction TB
        BS_Params["params_gpt.json (better_shakespeare)"]:::weight
        GPT2_Params["params_gpt.json (gpt2)"]:::weight
        BS_Bins["*.bin (better_shakespeare)"]:::weight
        GPT2_Bins["*.bin (gpt2)"]:::weight
    end

    %% Offline Conversion Service
    subgraph "Offline Conversion Service"
        direction TB
        Conv1["convert_checkpoint_pytorch.py"]:::offline
        Conv2["convert_pretrained_pytorch.py"]:::offline
        CKPT1["ckpt.pt"]:::offline
        CKPT2["sample_shakespeare_ckpt.pt"]:::offline
        Conv_README["README.md"]:::offline
    end

    %% Validation Suite
    subgraph "Validation Suite"
        direction TB
        ValJS["validation.js"]:::validation
        VTest1["gpt2medium_validation.json"]:::validation
        VTest2["shakepeare_validation.json"]:::validation
        Val_README["README.md"]:::validation
    end

    %% Data Flows
    UI_HTML --> UI_Inst
    UI_Inst --> UI_Vis
    UI_Vis -->|User input text| Tokenizer
    Tokenizer -->|Token IDs| ModelCore
    BS_Params --> ModelCore
    GPT2_Params --> ModelCore
    BS_Bins --> ModelCore
    GPT2_Bins --> ModelCore
    ModelCore --> Globals
    ModelCore --> Condensed
    Globals --> Kernels
    Condensed --> Kernels
    Kernels --> GPU_Buffers
    GPU_Buffers --> Condensed
    Condensed --> ModelCore
    ModelCore -->|Logits| Tokenizer
    Tokenizer -->|Text output| UI_Vis

    %% Offline flow
    Conv1 --> BS_Bins
    Conv1 --> BS_Params
    Conv2 --> GPT2_Bins
    Conv2 --> GPT2_Params

    %% Validation
    ValJS --> VTest1
    ValJS --> VTest2

    %% Click Events
    click UI_HTML "https://github.com/0hq/webgpt/blob/main/index.html"
    click UI_Inst "https://github.com/0hq/webgpt/blob/main/instructions.js"
    click UI_Vis "https://github.com/0hq/webgpt/blob/main/visuals.js"
    click Tokenizer "https://github.com/0hq/webgpt/blob/main/tokenizer.js"
    click GPT_TOK "https://github.com/0hq/webgpt/blob/main/weights/tokenization/gpt_tokens.json"
    click SIMPLE_TOK "https://github.com/0hq/webgpt/blob/main/weights/tokenization/simple_tokens.json"
    click VOCAB_BPE "https://github.com/0hq/webgpt/blob/main/weights/tokenization/vocab.bpe"
    click Globals "https://github.com/0hq/webgpt/blob/main/globals.js"
    click ModelCore "https://github.com/0hq/webgpt/blob/main/model.js"
    click Condensed "https://github.com/0hq/webgpt/blob/main/condensed/condensed.js"
    click BS_Params "https://github.com/0hq/webgpt/blob/main/weights/better_shakespeare/params_gpt.json"
    click GPT2_Params "https://github.com/0hq/webgpt/blob/main/weights/gpt2/params_gpt.json"
    click BS_Bins "https://github.com/0hq/webgpt/blob/main/weights/better_shakespeare/*.bin"
    click GPT2_Bins "https://github.com/0hq/webgpt/blob/main/weights/gpt2/*.bin"
    click Conv1 "https://github.com/0hq/webgpt/blob/main/other/conversion_scripts/convert_checkpoint_pytorch.py"
    click Conv2 "https://github.com/0hq/webgpt/blob/main/other/conversion_scripts/convert_pretrained_pytorch.py"
    click CKPT1 "https://github.com/0hq/webgpt/blob/main/other/conversion_scripts/ckpt.pt"
    click CKPT2 "https://github.com/0hq/webgpt/blob/main/other/conversion_scripts/sample_shakespeare_ckpt.pt"
    click Conv_README "https://github.com/0hq/webgpt/blob/main/other/conversion_scripts/README.md"
    click ValJS "https://github.com/0hq/webgpt/blob/main/other/validation/validation.js"
    click VTest1 "https://github.com/0hq/webgpt/blob/main/other/validation/test/gpt2medium_validation.json"
    click VTest2 "https://github.com/0hq/webgpt/blob/main/other/validation/test/shakepeare_validation.json"
    click Val_README "https://github.com/0hq/webgpt/blob/main/other/validation/README.md"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6
    classDef gpu fill:#D0FFD8,stroke:#028A0F
    classDef offline fill:#F0F0F0,stroke:#999999
    classDef weight fill:#FFF4C1,stroke:#D4A017
    classDef validation fill:#FFE4E1,stroke:#A52A2A
```
