Metadata-Version: 2.4
Name: cleanjepa
Version: 0.2.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: huggingface_hub>=0.34; extra == "wm"
Requires-Dist: numpy>=1.24; extra == "wm"
Provides-Extra: logging
Requires-Dist: wandb>=0.16; extra == "logging"
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="#world-models-first-exam">First exam</a> ·
  <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 builds both:

- **Plain PyTorch.** Only `torch`. No trainer classes, no config system, no custom runtime.
- **One file per recipe.** The whole training loop in view, from data to `optimizer.step()`.
- **Reusable blocks.** Encoders, predictors, regularizers, planners, and evaluation, each usable on
  its own, with documented tensor shapes.
- **Fast.** SDPA flash attention, `torch.compile`, mixed precision, fused AdamW, and multi-GPU with
  `torchrun`.

## World model's first exam

- **Trains on an RTX 3060.** One consumer GPU, minutes per level instead of days.
- **Six levels, six problems.** Movement, walls, distractors, contact, randomness, hidden state.

<p align="center">
  <img src="https://raw.githubusercontent.com/HappyFox001/CleanJepa/main/assets/grid9_exam.svg?v=2" width="49%" alt="Animation: in each of the six Grid9 levels a world model imagines candidate rollouts, picks a plan, and acts in the real world until it reaches the goal" />
  <img src="https://raw.githubusercontent.com/HappyFox001/CleanJepa/main/assets/grid9_results.svg?v=2" width="49%" alt="Grid9 planning success rate per level: LeWM, LpWM, and random actions" />
</p>

| Level | Dataset | World | What it tests |
|---|---|---|---|
| L0 | [grid9-l0-empty](https://huggingface.co/datasets/CleanJepa/grid9-l0-empty) | Empty board | Plain movement |
| L1 | [grid9-l1-maze](https://huggingface.co/datasets/CleanJepa/grid9-l1-maze) | S-maze | Walls that force detours |
| L2 | [grid9-l2-distractor](https://huggingface.co/datasets/CleanJepa/grid9-l2-distractor) | Maze with flicker and a drifting square | Ignoring motion the actions do not cause |
| L3 | [grid9-l3-push](https://huggingface.co/datasets/CleanJepa/grid9-l3-push) | One pushable block | Sparse contact, irreversible corners |
| L4 | [grid9-l4-stochastic](https://huggingface.co/datasets/CleanJepa/grid9-l4-stochastic) | Moves slip half the time | Two-outcome dynamics |
| L5 | [grid9-l5-wind](https://huggingface.co/datasets/CleanJepa/grid9-l5-wind) | Hidden per-episode wind | Partial observability |

Each model trains with `train_wm.py --preset <method> --frameskip 1` (10 epochs) and plans with
`eval_grid9.py` (CEM MPC on 50 start/goal tasks per level). Raw results, training logs, and the
exact commands: [CleanJepa/results](https://huggingface.co/datasets/CleanJepa/results/tree/main/grid9/2026-12-04).

## Quick start

Train LeWM on Grid9 L1 (the maze), then plan with it:

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

python recipes/train_wm.py --preset lewm --data CleanJepa/grid9-l1-maze/grid_l1.h5 --frameskip 1 \
    --amp bf16 --save-dir runs/grid9_l1
python recipes/eval_grid9.py --ckpt runs/grid9_l1/epoch_10.pt --level l1
```

## 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.

## 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)
