Metadata-Version: 2.4
Name: cleanjepa
Version: 0.1.0
Summary: Clean, minimal, composable PyTorch implementations of JEPA & latent world models.
License: MIT License
        
        Copyright (c) 2026 CleanJepa Contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/HappyFox001/CleanJepa
Project-URL: Repository, https://github.com/HappyFox001/CleanJepa
Project-URL: Issues, https://github.com/HappyFox001/CleanJepa/issues
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.1
Provides-Extra: wm
Requires-Dist: h5py>=3.10; extra == "wm"
Requires-Dist: hdf5plugin>=4.4; extra == "wm"
Requires-Dist: numpy>=1.24; extra == "wm"
Provides-Extra: hub
Requires-Dist: huggingface_hub>=0.34; extra == "hub"
Provides-Extra: logging
Requires-Dist: wandb>=0.16; extra == "logging"
Provides-Extra: viz
Requires-Dist: matplotlib>=3.8; extra == "viz"
Requires-Dist: numpy>=1.24; extra == "viz"
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="https://raw.githubusercontent.com/HappyFox001/CleanJepa/main/assets/logo.svg" width="108" height="108" alt="CleanJepa logo" />
</p>

<h1 align="center">CleanJepa</h1>

<p align="center">
  <strong>Readable, hackable JEPAs and latent world models in plain PyTorch.</strong>
</p>

<p align="center">
  <a href="https://github.com/HappyFox001/CleanJepa/actions/workflows/ci.yml"><img src="https://github.com/HappyFox001/CleanJepa/actions/workflows/ci.yml/badge.svg" alt="CI" /></a>
  <img src="https://img.shields.io/badge/python-3.10%2B-blue" alt="Python 3.10+" />
  <img src="https://img.shields.io/badge/pytorch-2.1%2B-ee4c2c" alt="PyTorch 2.1+" />
  <a href="https://github.com/HappyFox001/CleanJepa/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="License: MIT" /></a>
</p>

<p align="center">
  <a href="#quick-start">Quick start</a> ·
  <a href="#methods">Methods</a> ·
  <a href="#use-it-as-a-library">Library</a> ·
  <a href="https://github.com/HappyFox001/CleanJepa/blob/main/ROADMAP.md">Roadmap</a> ·
  <a href="https://github.com/HappyFox001/CleanJepa/blob/main/CONTRIBUTING.md">Contributing</a>
</p>

---

A JEPA learns by predicting the representation of what it cannot see, instead of reconstructing
pixels. JEPA world models go one step further: they predict the next latent state from an action,
then plan by searching for actions whose predicted future matches a goal image.

CleanJepa implements this family in plain PyTorch, in the spirit of
[nanoGPT](https://github.com/karpathy/nanoGPT) and [CleanRL](https://github.com/vwxyzjn/cleanrl).
The library is about 2,000 lines with `torch` as its only dependency, and every training script is
a single file you can read top to bottom.

- **Plain PyTorch.** No trainer classes, config systems, or custom runtimes between you and the
  math.
- **Reusable blocks.** Encoders, predictors, anti-collapse regularizers, planners, and evaluation
  tools import on their own, with tensor shapes documented on every public function.
- **One file per recipe.** Each recipe keeps its training loop in view, from data to
  `loss.backward()` and `optimizer.step()`.
- **Fast without magic.** Flash attention through SDPA, `torch.compile`, mixed precision, fused
  AdamW, and multi-GPU training with `torchrun`.

## Quick start

```bash
git clone https://github.com/HappyFox001/CleanJepa.git && cd CleanJepa
pip install -e .

# Train a small LeWM-style world model on synthetic moving squares (no download needed)
python recipes/train_wm.py --synthetic --epochs 2
```

Or skip training: download a pretrained toy world model and watch it plan. `--device auto` uses
CUDA, then Apple-silicon MPS, then the CPU.

```bash
pip install -e ".[wm]"
hf download CleanJepa/toy-square-wm lpwm/seed0.pt --local-dir models
python recipes/eval_toy.py --ckpt models/lpwm/seed0.pt --num-eval 10 --device auto
```

See what it learns: this trains a model with and without SIGReg and plots their latent spaces side
by side.

```bash
pip install -e ".[viz]"
python recipes/demo_manifold.py --epochs 40 --out manifold.png
```

Train on real data, in the LeWM HDF5 format (for example
[`quentinll/lewm-pusht`](https://huggingface.co/datasets/quentinll/lewm-pusht)), then probe what the
latents encode:

```bash
pip install -e ".[wm]"
python recipes/train_wm.py --data pusht_expert_train.h5 --amp bf16 --save-dir runs/pusht
python recipes/probe_wm.py --ckpt runs/pusht/epoch_10.pt --data pusht_expert_train.h5 --targets proprio
```

Compare methods on the toy world in one command: train each `--preset`, probe the latents, plan with
CEM, and print a table across seeds:

```bash
python recipes/toy_benchmark.py --out runs/toy_bench --methods lewm,lpwm,vicreg --seeds 0,1,2
```

## Methods

<table>
<tr>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2603.19312">LeWM</a></b><br><code>train_wm.py --preset lewm</code><br><sub><code>ViTEncoder</code> · <code>ARPredictor</code> · <code>SIGReg</code> (<a href="https://arxiv.org/abs/2511.08544">LeJEPA</a>)</sub></td>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2608.22764">LpWM</a></b><br><code>train_wm.py --preset lpwm</code><br><sub><code>RDMReg</code> (<a href="https://arxiv.org/abs/2602.01456">Rectified LpJEPA</a>)</sub></td>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2105.04906">VICReg</a> world model</b><br><code>train_wm.py --preset vicreg</code><br><sub><code>VICReg</code></sub></td>
</tr>
<tr>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2411.04983">DINO-WM</a></b><br><code>train_wm.py --preset dino-wm</code><br><sub><code>FrozenDINOv2Encoder</code></sub></td>
<td width="33%" valign="top"><b>Token world model</b><br><code>train_wm.py --rep patches --head residual</code><br><sub><code>TokenPredictor</code> (joint or factorized)</sub></td>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2506.09985">V-JEPA 2-AC</a></b><br><code>train_vjepa2_ac.py</code><br><sub><code>ACPredictor</code> · <code>VJEPA2ACWorldModel</code></sub></td>
</tr>
<tr>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2301.08243">I-JEPA</a></b><br><code>train_ijepa.py</code><br><sub><code>ViTEncoder</code> · <code>EMAEncoder</code></sub></td>
<td width="33%" valign="top"><b><a href="https://arxiv.org/abs/2404.08471">V-JEPA</a></b><br><code>train_vjepa.py</code><br><sub><code>VideoViTEncoder</code> · <code>EMAEncoder</code></sub></td>
<td width="33%"></td>
</tr>
</table>

World models plan with `CEMSolver`, `MPPISolver`, or `GradientPlanner`. Each recipe lists its
differences from the official code in its docstring.

## Planning results

| Success rate | LpWM | DINO-WM | LeWM | VICReg | Random actions |
|---|---|---|---|---|---|
| Toy square world | 0.93 ± 0.06 | 0.92 ± 0.08 | 0.78 ± 0.17 | 0.76 ± 0.04 | 0.22 ± 0.15 |

Mean ± 95% CI over 3 seeds. Toy square world: `toy_benchmark.py` (30 epochs per method, CEM on 100 random
start/goal tasks reachable within 3 actions). Raw results, training logs, and the exact
command: [CleanJepa/results](https://huggingface.co/datasets/CleanJepa/results/tree/main/toy-square/2026-12-03).

## Use it as a library

The library is on PyPI and needs only PyTorch; the recipes stay in the repository.

```bash
pip install cleanjepa
```

```python
import torch
from cleanjepa import CEMSolver, JEPAWorldModel, SIGReg, ViTEncoder

encoder = ViTEncoder(img_size=224, patch_size=14, embed_dim=192, depth=12, heads=3)
model = JEPAWorldModel(encoder, action_dim=10, embed_dim=192, history_size=3)
sigreg = SIGReg()

pixels = torch.randn(8, 4, 3, 224, 224)  # (B, T, C, H, W)
actions = torch.randn(8, 4, 10)          # (B, T, A)

emb = model.encode_pixels(pixels)        # (B, T, D)
pred = model.predict(emb[:, :3], model.encode_actions(actions)[:, :3])
loss = (pred - emb[:, 1:]).pow(2).mean() + 0.09 * sigreg(emb)
loss.backward()

# Plan 5 action blocks that move the last frame towards a goal image.
planner = CEMSolver(model, horizon=5, num_samples=300, topk=30)
plan = planner.plan(pixels[:, :1], pixels[:, 3:], action_dim=10)  # (B, 5, A)
```

## Repository layout

```text
cleanjepa/     The library: models, regularizers, planners, data, evaluation (torch only)
recipes/       One script per task: training, probing, planning evaluation, benchmarking
tests/         Fast CPU tests, one file per area
```

## Learn more

- [ROADMAP.md](https://github.com/HappyFox001/CleanJepa/blob/main/ROADMAP.md): goals and directions
- [CONTRIBUTING.md](https://github.com/HappyFox001/CleanJepa/blob/main/CONTRIBUTING.md): adding a method and reporting results

## Citation

```bibtex
@software{cleanjepa,
  title  = {CleanJepa: Readable JEPAs and Latent World Models in PyTorch},
  author = {{CleanJepa contributors}},
  url    = {https://github.com/HappyFox001/CleanJepa},
  year   = {2026}
}
```

## Acknowledgements

CleanJepa builds on the papers and official code of LeWM, LeJEPA, DINO-WM, LpWM, Rectified LpJEPA,
I-JEPA, V-JEPA, and VICReg, and on the design of nanoGPT and CleanRL.

## License

[MIT](https://github.com/HappyFox001/CleanJepa/blob/main/LICENSE)
