# saurav-34/lepong architecture

> Architecture diagram of the GitHub repository saurav-34/lepong, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/saurav-34/lepong
- Repository: https://github.com/saurav-34/lepong

## Overview

#### Purpose and Entry Points
lepong is a pixel-only JEPA world-model project centered on Pong: it predicts latent future state from screenshots and actions, decodes game state, and drives a paddle controller. Primary entry points are offline training in `model/jepa_pool.py`, state-head fitting in `scripts/train_statehead.py`, and browser-serving inference in `server/infer.py`.

#### World Simulation and Dataset Production
The native Pong environment produces labeled screenshots, actions, and state trajectories for supervised state decoding and self-supervised predictive training. `model/pong_world.py` is the canonical generator; `scripts/collect_pong.py` and the broader collectors in `scripts/collect_data.py` extend collection workflows to other game sources. Stored datasets form the boundary between simulation/data capture and model training.

#### JEPA Representation and Dynamics Model
`model/jepa_pool.py` defines and trains the core 13M-parameter architecture: CNN pixel encoder, action encoder, causal AdaLN-conditioned transformer predictor, projection head, and SIGReg objective. The principal flow is 128×128 PNG frame → 192-dimensional embedding, combined with a 2-D action conditioning signal → predicted future embedding. Checkpoints from this stage initialize later state-head training.

#### State Decoding and Control
`scripts/train_statehead.py` freezes the learned encoder/predictor and trains the lightweight linear state head to recover ten physical state quantities, including ball and paddle positions/velocities. Controller evaluation and rollout behavior are implemented in `scripts/eval_controller.py`; robustness and representation quality boundaries are tested through `scripts/eval_ood.py` and `scripts/eval_occlusion.py`.

#### Serving and Real-Time Game Loops
`server/infer.py` loads a checkpoint and exposes real-time model inference for browser clients. Game-specific runtime loops bridge inference outputs to playable environments: `server/pong_core.py` with `server/play_pong.py`, plus parallel boxing and tennis implementations in `server/boxing_core.py` and `server/tennis_core.py`. This is the operational boundary between neural prediction/control and game mechanics.

#### Browser Client and Transport
Static browser experiences live under `client/`, with the Pong play surface at `client/pong_play.html` and training/demo client logic in `client/train.js` and `client/train_worker.js`. Clients capture canvas pixels, optionally apply visual occlusion, send frames to the inference service, and use returned paddle targets to update play. The runtime stack uses Python, PyTorch, FastAPI/Uvicorn, and WebSockets; model checkpoints are externally distributed through HuggingFace.

#### Multi-Game and Federation Extensions
The repository contains early generalization paths beyond Pong: game collection configurations in `scripts/configs/`, combat/arcade data capture scripts such as `scripts/collect_kaz.py`, and server-side federation support in `server/federation_client.py` and `server/data_tap.py`. These appear to support collecting or routing observations from multiple interactive game environments while retaining the same model-serving boundary.

Main entry points: [`server/infer.py`](https://github.com/saurav-34/lepong/blob/main/server/infer.py) (Inference service), [`model/jepa_pool.py`](https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py) (JEPA trainer), [`scripts/train_statehead.py`](https://github.com/saurav-34/lepong/blob/main/scripts/train_statehead.py) (State-head fitting), [`client/pong_play.html`](https://github.com/saurav-34/lepong/blob/main/client/pong_play.html) (Pong browser client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data Production"]
  node_pong_world{{"Pong world generator<br/>simulation<br/>[pong_world.py]"}}
  node_collect_pong["Pong collector<br/>collection script<br/>[collect_pong.py]"]
  node_collect_data["General collectors<br/>collection script<br/>[collect_data.py]"]
  node_datasets[("Stored datasets<br/>training data")]
end

subgraph group_model["JEPA Training"]
  node_jepa_train["JEPA trainer<br/>training entry point<br/>[jepa_pool.py]"]
  node_pixel_encoder["CNN pixel encoder<br/>model layer<br/>[jepa_pool.py]"]
  node_action_predictor["Action-conditioned predictor<br/>model layer<br/>[jepa_pool.py]"]
  node_sigreg["Projection and SIGReg<br/>training objective<br/>[jepa_pool.py]"]
  node_checkpoints[("Model checkpoints<br/>model artifact")]
end

subgraph group_control["State and Control"]
  node_statehead_train["State-head fitting<br/>training entry point<br/>[train_statehead.py]"]
  node_controller_eval["Controller rollout<br/>evaluation and control<br/>[eval_controller.py]"]
  node_robustness_eval["Representation robustness<br/>evaluation<br/>[eval_ood.py]"]
end

subgraph group_serving["Serving and Game Loops"]
  node_infer_service["Inference service<br/>FastAPI/WebSocket server<br/>[infer.py]"]
  node_pong_runtime["Pong game runtime<br/>game loop<br/>[play_pong.py]"]
  node_pong_core["Pong mechanics<br/>game core<br/>[pong_core.py]"]
end

subgraph group_client["Browser Clients"]
  node_browser_client["Pong browser client<br/>static client<br/>[pong_play.html]"]
  node_training_client["Training demo client<br/>browser worker client<br/>[train.js]"]
end

subgraph group_extension["Multi-Game Extensions"]
  node_game_configs["Multi-game collection configs<br/>collection configuration"]
  node_federation["Federation data routing<br/>server extension"]
end

node_pong_world -->|"produces episodes"| node_collect_pong
node_collect_pong -->|"stores frames and labels"| node_datasets
node_collect_data -->|"stores captured data"| node_datasets
node_game_configs -.->|"configures games"| node_collect_data
node_datasets -->|"predictive training data"| node_jepa_train
node_jepa_train -->|"trains"| node_pixel_encoder
node_pixel_encoder -->|"frame embeddings"| node_action_predictor
node_action_predictor -->|"future embeddings"| node_sigreg
node_sigreg -->|"optimization signal"| node_jepa_train
node_jepa_train -->|"writes"| node_checkpoints
node_datasets -->|"state labels"| node_statehead_train
node_checkpoints -->|"initializes frozen JEPA"| node_statehead_train
node_statehead_train -->|"state decoder"| node_controller_eval
node_statehead_train -->|"decoded representations"| node_robustness_eval
node_checkpoints -->|"loads model"| node_infer_service
node_browser_client -->|"frames via WebSocket"| node_infer_service
node_infer_service -->|"paddle targets"| node_browser_client
node_browser_client -->|"drives play"| node_pong_runtime
node_pong_runtime -->|"uses mechanics"| node_pong_core
node_federation -.->|"routes observations"| node_infer_service
node_training_client -->|"demo inference"| node_infer_service

click node_pong_world "https://github.com/saurav-34/lepong/blob/main/model/pong_world.py"
click node_collect_pong "https://github.com/saurav-34/lepong/blob/main/scripts/collect_pong.py"
click node_collect_data "https://github.com/saurav-34/lepong/blob/main/scripts/collect_data.py"
click node_jepa_train "https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py"
click node_pixel_encoder "https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py"
click node_action_predictor "https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py"
click node_sigreg "https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py"
click node_statehead_train "https://github.com/saurav-34/lepong/blob/main/scripts/train_statehead.py"
click node_controller_eval "https://github.com/saurav-34/lepong/blob/main/scripts/eval_controller.py"
click node_robustness_eval "https://github.com/saurav-34/lepong/blob/main/scripts/eval_ood.py"
click node_infer_service "https://github.com/saurav-34/lepong/blob/main/server/infer.py"
click node_pong_runtime "https://github.com/saurav-34/lepong/blob/main/server/play_pong.py"
click node_pong_core "https://github.com/saurav-34/lepong/blob/main/server/pong_core.py"
click node_browser_client "https://github.com/saurav-34/lepong/blob/main/client/pong_play.html"
click node_training_client "https://github.com/saurav-34/lepong/blob/main/client/train.js"
click node_federation "https://github.com/saurav-34/lepong/blob/main/server/federation_client.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pong_world,node_collect_pong,node_collect_data,node_datasets toneBlue
class node_jepa_train,node_pixel_encoder,node_action_predictor,node_sigreg,node_checkpoints toneAmber
class node_statehead_train,node_controller_eval,node_robustness_eval toneMint
class node_infer_service,node_pong_runtime,node_pong_core toneRose
class node_browser_client,node_training_client toneIndigo
class node_game_configs,node_federation toneTeal
```

## Components

### Data Production

- [Pong world generator](https://github.com/saurav-34/lepong/blob/main/model/pong_world.py): simulation · Generates labeled frames, actions, and state trajectories. · `model/pong_world.py`
- [Pong collector](https://github.com/saurav-34/lepong/blob/main/scripts/collect_pong.py): collection script · `scripts/collect_pong.py`
- [General collectors](https://github.com/saurav-34/lepong/blob/main/scripts/collect_data.py): collection script · `scripts/collect_data.py`
- **Stored datasets**: training data · Boundary between capture and model fitting.

### JEPA Training

- [JEPA trainer](https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py): training entry point · Trains the 13M pixel world model. · `model/jepa_pool.py`
- [CNN pixel encoder](https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py): model layer · Maps 128×128 frames to 192-D embeddings. · `model/jepa_pool.py`
- [Action-conditioned predictor](https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py): model layer · Causal AdaLN transformer predicts future embeddings from actions. · `model/jepa_pool.py`
- [Projection and SIGReg](https://github.com/saurav-34/lepong/blob/main/model/jepa_pool.py): training objective · `model/jepa_pool.py`
- **Model checkpoints**: model artifact · Distributed externally through HuggingFace.

### State and Control

- [State-head fitting](https://github.com/saurav-34/lepong/blob/main/scripts/train_statehead.py): training entry point · Freezes JEPA features and fits a linear 10-value decoder. · `scripts/train_statehead.py`
- [Controller rollout](https://github.com/saurav-34/lepong/blob/main/scripts/eval_controller.py): evaluation and control · `scripts/eval_controller.py`
- [Representation robustness](https://github.com/saurav-34/lepong/blob/main/scripts/eval_ood.py): evaluation · Evaluates OOD and occlusion behavior. · `scripts/eval_ood.py`

### Serving and Game Loops

- [Inference service](https://github.com/saurav-34/lepong/blob/main/server/infer.py): FastAPI/WebSocket server · Loads checkpoints and returns real-time predictions. · `server/infer.py`
- [Pong game runtime](https://github.com/saurav-34/lepong/blob/main/server/play_pong.py): game loop · Game mechanics and playable Pong loop. · `server/play_pong.py`
- [Pong mechanics](https://github.com/saurav-34/lepong/blob/main/server/pong_core.py): game core · `server/pong_core.py`

### Browser Clients

- [Pong browser client](https://github.com/saurav-34/lepong/blob/main/client/pong_play.html): static client · Captures canvas pixels and applies returned paddle targets. · `client/pong_play.html`
- [Training demo client](https://github.com/saurav-34/lepong/blob/main/client/train.js): browser worker client · `client/train.js`

### Multi-Game Extensions

- **Multi-game collection configs**: collection configuration
- [Federation data routing](https://github.com/saurav-34/lepong/blob/main/server/federation_client.py): server extension · Routes observations from interactive game environments. · `server/federation_client.py`

## Connections

- Pong world generator → Pong collector: produces episodes
- Pong collector → Stored datasets: stores frames and labels
- General collectors → Stored datasets: stores captured data
- Multi-game collection configs → General collectors: configures games
- Stored datasets → JEPA trainer: predictive training data
- JEPA trainer → CNN pixel encoder: trains
- CNN pixel encoder → Action-conditioned predictor: frame embeddings
- Action-conditioned predictor → Projection and SIGReg: future embeddings
- Projection and SIGReg → JEPA trainer: optimization signal
- JEPA trainer → Model checkpoints: writes
- Stored datasets → State-head fitting: state labels
- Model checkpoints → State-head fitting: initializes frozen JEPA
- State-head fitting → Controller rollout: state decoder
- State-head fitting → Representation robustness: decoded representations
- Model checkpoints → Inference service: loads model
- Pong browser client → Inference service: frames via WebSocket
- Inference service → Pong browser client: paddle targets
- Pong browser client → Pong game runtime: drives play
- Pong game runtime → Pong mechanics: uses mechanics
- Federation data routing → Inference service: routes observations
- Training demo client → Inference service: demo inference
