arcticoneai commited on
Commit
5f7c424
·
verified ·
1 Parent(s): cc00876

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +579 -255
README.md CHANGED
@@ -5,7 +5,6 @@ tags:
5
  - text-to-video
6
  - video-generation
7
  - world-model
8
- - video
9
  - latent-video
10
  - text-conditioned
11
  - flow-matching
@@ -14,452 +13,777 @@ tags:
14
  - torch-xla
15
  - google-tpu
16
  - spatiotemporal
 
17
  - research
18
  ---
19
 
20
- # WorldDiT — Text-Conditioned Latent Video World Model
21
 
22
- **WorldDiT** is a Transformer-based model for generating short video sequences in a learned latent space, conditioned on text embeddings.
23
 
24
- The model operates on spatiotemporal latents produced by a separate Variational Autoencoder (VAE). It learns a continuous velocity field using flow matching, transforming Gaussian noise into structured video representations.
25
 
26
- The implementation supports distributed training on Google Cloud TPUs using PyTorch/XLA and GSPMD.
27
 
28
- ## Architecture
 
 
29
 
30
- WorldDiT combines several components:
31
 
32
- - **Spatiotemporal latent tokens:** Each sequence contains 13 temporal positions, with an 8 × 8 spatial latent grid and 128 latent channels.
33
- - **Temporal self-attention:** Block-causal attention allows each frame to attend to previous frames and its own spatial tokens.
34
- - **Text cross-attention:** Video tokens attend to contextual text embeddings.
35
- - **Adaptive normalization:** Time-conditioned modulation is applied to attention and MLP blocks.
36
- - **Flow matching:** The network predicts the velocity field connecting real video latents and Gaussian noise.
37
- - **EMA:** An exponential moving average of model parameters is maintained for video generation.
38
 
39
- ### Model configurations
40
 
41
- | Configuration | Hidden size | Depth | Attention heads | Parameters |
42
- |---|---:|---:|---:|---:|
43
- | Base | 768 | 12 | 12 | ~159M |
44
- | Large | 1408 | 24 | 22 | ~1.056B |
45
 
46
- Parameter counts refer to WorldDiT itself and exclude the VAE and text encoder.
47
 
48
- Both configurations use the same latent representation:
49
 
50
- | Property | Value |
51
  |---|---:|
 
 
 
 
 
52
  | Latent channels | 128 |
53
- | Temporal positions | 13 |
54
- | Latent spatial resolution | 8 × 8 |
55
  | Tokens per sequence | 832 |
56
  | Text embedding dimension | 768 |
 
57
 
58
- Each attention head has a dimension of 64.
59
 
60
- ## Training objective
 
 
 
 
 
 
61
 
62
- The model is trained using a flow-matching objective.
63
 
64
- Given clean latents \(x_0\), Gaussian noise \(\epsilon\), and a time variable \(t\), the training process constructs:
65
 
66
- \[
67
- x_t=(1-t)x_0+t\epsilon
68
- \]
69
 
70
- The target velocity is:
71
 
72
- \[
73
- v=\epsilon-x_0
74
- \]
75
 
76
- WorldDiT learns to predict this velocity:
77
 
78
- \[
79
- \mathcal{L}=\mathbb{E}\left[\|\hat v_\theta(x_t,t,c)-(\epsilon-x_0)\|_2^2\right]
80
- \]
 
 
81
 
82
- where \(c\) represents the text conditioning.
83
 
84
- During generation, the model starts from Gaussian noise and integrates the predicted velocity field in the reverse direction.
 
 
85
 
86
- ## Quick Start
87
 
88
- ### 1. Prepare the environment
89
 
90
- Run the project on a Google Cloud TPU VM with a compatible PyTorch/XLA installation.
91
 
92
- Install the required Python dependencies:
93
 
94
- ```bash
95
- pip install numpy pillow av lpips huggingface_hub
 
 
 
 
 
96
  ```
97
 
98
- PyTorch and PyTorch/XLA must be installed using versions compatible with the TPU runtime. Do not install arbitrary versions independently.
99
 
100
- Verify the environment:
101
 
102
- ```bash
103
- python -c "import torch, torch_xla, numpy, av, lpips; print('Environment OK')"
104
- ```
105
 
106
- ### 2. Prepare the files
 
 
 
 
 
 
107
 
108
- The project directory should contain:
109
 
110
  ```text
111
- WorldDiT/
112
- wm.py
113
- train.py
114
- train_1b.py
115
- vae.py
116
- text_emb.py
117
  ```
118
 
119
- The required data and VAE checkpoint should be available at:
120
 
121
- ```text
122
- ~/wm_data/
123
- latents.npy
124
- txt_emb.npy
125
- txt_mask.npy
126
- txt_idx.npy
127
 
128
- ~/vae_run/
129
- last.pt
130
 
131
- ~/wm_run/
132
- lat_stats.npz
133
- ```
134
 
135
- The dataset must be prepared before training. See the Dataset Format section for the expected array shapes.
136
 
137
- ### 3. Start Base training
 
 
 
 
 
 
 
138
 
139
- From the project directory:
140
 
141
- ```bash
142
- cd ~/WorldDiT
143
- MAX_MIN=240 python train.py
144
- ```
145
 
146
- This starts the approximately 159M-parameter configuration.
 
 
147
 
148
- The model checkpoint is saved to:
149
 
150
  ```text
151
- ~/wm_run/wm.pt
152
  ```
153
 
154
- ### 4. Start Large training
155
 
156
- To train the approximately 1.056B-parameter configuration:
157
 
158
- ```bash
159
- cd ~/WorldDiT
160
- MAX_MIN=300 python train_1b.py
161
- ```
 
 
 
 
 
 
162
 
163
- The checkpoint is saved to:
164
 
165
  ```text
166
- ~/wm1b_run/wm.pt
167
  ```
168
 
169
- ### 5. Monitor training
170
 
171
- Training logs are written to:
172
 
173
- ```text
174
- log.txt
 
 
 
 
 
 
 
 
 
 
175
  ```
176
 
177
- The scripts periodically save checkpoints and generate test videos using the EMA model. Generated MP4 files are saved in the corresponding run directory.
178
 
179
- ### Important
 
 
 
 
 
 
 
 
 
 
 
180
 
181
- The current implementation provides TPU training and periodic test generation. It does not yet include a standalone inference script for loading a pretrained checkpoint and generating a video from an arbitrary prompt without starting the training script.
182
 
183
- ## Text conditioning
184
 
185
- Text embeddings are supplied by the external `text_emb.py` module.
 
 
 
 
186
 
187
- The expected embedding dimension is 768. The training dataset stores precomputed embeddings and attention masks, while the generation test uses:
188
 
189
- ```python
190
- from text_emb import encode
191
 
192
- embeddings, attention_mask = encode([PROMPT])
193
- ```
194
 
195
- The base configuration uses classifier-free text dropout with a probability of 10%.
 
 
196
 
197
- The large configuration uses conditional training without text dropout.
198
 
199
- ## Video representation and VAE
 
 
200
 
201
- WorldDiT does not directly predict RGB pixels.
202
 
203
- Instead, it operates in the latent space of a separately trained VAE.
 
 
204
 
205
- The VAE is responsible for:
206
 
207
- 1. Encoding video sequences into compact spatiotemporal representations.
208
- 2. Decoding generated latents into RGB video frames.
209
 
210
- The training and generation scripts expect the VAE checkpoint at:
 
 
 
 
 
 
 
211
 
212
- ```text
213
- ~/vae_run/last.pt
214
- ```
215
 
216
- The checkpoint must contain a model state dictionary under the `model` key.
217
 
218
- The decoder is loaded from `VAE().dec` and used to reconstruct generated videos.
219
 
220
- The VAE used to decode generated latents must be compatible with the VAE used to encode the training data.
 
 
 
 
221
 
222
- ## Dataset format
223
 
224
- Training uses precomputed latent sequences and text embeddings stored as NumPy arrays.
225
 
226
- Expected directory:
227
 
228
  ```text
229
- ~/wm_data/
230
- latents.npy
231
- txt_emb.npy
232
- txt_mask.npy
233
- txt_idx.npy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  ```
235
 
236
- The expected tensor layouts are:
 
 
 
 
 
 
237
 
238
  | File | Expected shape | Description |
239
  |---|---|---|
240
- | `latents.npy` | `(N, 128, 13, 8, 8)` | VAE-encoded video sequences |
241
- | `txt_emb.npy` | `(M, L, 768)` | Text embeddings |
242
  | `txt_mask.npy` | `(M, L)` | Text attention masks |
243
- | `txt_idx.npy` | `(N,)` | Mapping from video sequences to text entries |
244
-
245
- `N` is the number of video sequences, `M` is the number of text entries, and `L` is the text sequence length.
 
246
 
247
- Each element of `txt_idx.npy` selects the corresponding text embedding and attention mask for a video sequence.
248
-
249
- The normalization statistics are stored in:
250
 
251
  ```text
252
  ~/wm_run/lat_stats.npz
253
  ```
254
 
255
- The file must contain:
256
 
257
  ```python
258
  mean
259
  std
260
  ```
261
 
262
- These statistics are used to normalize training latents and restore their original scale before VAE decoding.
263
 
264
- The preprocessing pipeline that creates these arrays is separate from the WorldDiT training scripts.
 
 
265
 
266
- ## Repository structure
267
 
268
- The training scripts expect the following files:
 
 
269
 
270
- ```text
271
- ~/wm.py
272
- ~/vae.py
273
- ~/text_emb.py
274
 
275
- ~/wm_data/
276
- latents.npy
277
- txt_emb.npy
278
- txt_mask.npy
279
- txt_idx.npy
280
 
281
- ~/vae_run/
282
- last.pt
 
 
 
 
 
283
 
284
- ~/wm_run/
285
- lat_stats.npz
 
 
 
 
 
 
286
  ```
287
 
288
- `wm.py` contains the WorldDiT architecture.
289
 
290
- `vae.py` contains the VAE architecture.
291
 
292
- `text_emb.py` implements text embedding extraction.
293
 
294
- The dataset must be prepared before starting WorldDiT training.
295
 
296
- ## Training environment
297
 
298
- The implementation uses:
 
 
299
 
300
- - Python
301
- - PyTorch
302
- - PyTorch/XLA
303
- - NumPy
304
- - PyAV
305
- - Google Cloud TPU runtime
306
 
307
- The TPU environment must have mutually compatible PyTorch and PyTorch/XLA versions.
 
 
308
 
309
- The training scripts use:
310
 
311
- ```python
312
- xr.use_spmd()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
  ```
314
 
315
- and a GSPMD mesh for distributed execution.
316
 
317
- The large configuration additionally applies parameter sharding across TPU devices.
 
 
 
 
 
 
318
 
319
- ## Training
320
 
321
- ### Base configuration — ~159M parameters
322
 
323
- Save the base training script as `train.py`.
 
 
324
 
325
- Default configuration:
326
 
327
- | Parameter | Value |
328
- |---|---:|
329
- | Batch size | 64 |
330
- | Learning rate | 2e-4 |
331
- | Optimizer | AdamW |
332
- | EMA decay | 0.999 |
333
- | Checkpoint interval | 2,000 steps |
334
- | Default runtime | 240 minutes |
335
- | Sampling steps | 40 |
336
- | Classifier-free guidance | 4.0 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
- Launch:
339
 
340
  ```bash
341
- MAX_MIN=240 python train.py
342
  ```
343
 
344
- The script automatically resumes model and EMA weights if `~/wm_run/wm.pt` exists.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
- ### Large configuration — ~1.056B parameters
347
 
348
- Save the large training script as `train_1b.py`.
349
 
350
- Default configuration:
351
 
352
  | Parameter | Value |
353
  |---|---:|
354
- | Hidden size | 1408 |
355
- | Depth | 24 |
356
- | Attention heads | 22 |
357
- | Batch size | 32 |
358
- | Learning rate | 1e-4 |
359
- | Warmup | 1,000 steps |
360
  | Optimizer | AdamW |
361
- | EMA decay | 0.999 |
362
- | Checkpoint interval | 1,000 steps |
363
- | Default runtime | 300 minutes |
364
- | Sampling steps | 40 |
365
- | Classifier-free guidance | 1.0 |
 
 
 
 
 
 
366
 
367
- Launch:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
 
369
  ```bash
370
- MAX_MIN=300 python train_1b.py
 
 
371
  ```
372
 
373
- The large model uses GSPMD parameter sharding to distribute model weights across TPU devices.
374
 
375
- The large training script automatically resumes from:
376
 
377
  ```text
378
- ~/wm1b_run/wm.pt
 
 
 
379
  ```
380
 
381
- Both scripts use `MAX_MIN` to control the duration of a training run.
382
 
383
- ## Checkpoints and generated videos
 
 
 
384
 
385
- Training checkpoints are saved as:
386
 
387
- ```text
388
- ~/wm_run/wm.pt
389
- ~/wm1b_run/wm.pt
 
 
 
390
  ```
391
 
392
- Each checkpoint contains:
 
 
 
 
393
 
394
- - `model`: Current model parameters.
395
- - `ema`: Exponential moving average parameters.
396
- - `step`: Training step.
397
- - `cfg`: Architecture configuration (large model only).
398
 
399
- The base checkpoint does not include a `cfg` field.
400
 
401
- The training scripts save checkpoints periodically and generate test videos using the EMA model.
 
 
 
 
 
402
 
403
- Generated videos are written to the corresponding output directory:
 
 
 
 
 
 
 
 
 
 
404
 
405
  ```text
406
- test_step0002000.mp4
407
- test_step0004000.mp4
408
  ```
409
 
410
- The test generation uses the configured text prompt, 40 sampling steps, and a fixed initial noise seed. Generated videos are encoded with H.264 at 24 FPS and 256 × 256 resolution.
 
 
 
 
 
 
 
 
 
 
 
411
 
412
- Training logs are stored in:
 
 
 
413
 
414
  ```text
415
- log.txt
 
 
 
416
  ```
417
 
418
- ## Resuming training
419
 
420
- Both scripts automatically detect an existing `wm.pt` checkpoint.
421
 
422
- The model weights, EMA weights, and training step are restored.
423
 
424
- **Important:** The current checkpoints do not save AdamW optimizer state. Resuming therefore restores the model but initializes a new optimizer. The large configuration also reconstructs its learning-rate scheduler.
425
 
426
- ## Current scope and limitations
427
 
428
- The current implementation is a text-conditioned latent video generation model.
 
 
 
 
 
429
 
430
- It includes temporal attention and video-latent generation, but the provided code does not implement explicit action conditioning, interactive environment control, or a physics-based simulator.
431
 
432
- The VAE, text encoder, and dataset preprocessing are separate components.
 
 
 
 
 
433
 
434
- A standalone inference pipeline and a standardized benchmark evaluation are not included in these training scripts.
435
 
436
- ## Implementation
 
 
437
 
438
- The main architecture is defined in `wm.py`:
439
 
440
- ```python
441
- from wm import WorldDiT
442
 
443
- model = WorldDiT()
 
444
  ```
445
 
446
- For the large configuration:
447
 
448
- ```python
449
- model = WorldDiT(
450
- d=1408,
451
- depth=24,
452
- heads=22
453
- )
454
  ```
455
 
456
- The implementation is built with PyTorch and uses PyTorch/XLA for TPU training.
457
 
458
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459
 
460
- **Project:** LiberalMind
461
- **Model family:** WorldDiT
462
- **Task:** Text-conditioned latent video generation
463
- **Training objective:** Flow matching
464
- **Hardware target:** Google Cloud TPU
465
- **Framework:** PyTorch / PyTorch-XLA
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  - text-to-video
6
  - video-generation
7
  - world-model
 
8
  - latent-video
9
  - text-conditioned
10
  - flow-matching
 
13
  - torch-xla
14
  - google-tpu
15
  - spatiotemporal
16
+ - egocentric-video
17
  - research
18
  ---
19
 
20
+ # WorldDiT — Clustered Latent Video World Models
21
 
22
+ WorldDiT is a collection of compact Transformer-based video world models trained on clustered egocentric video data.
23
 
24
+ Instead of training one large model on the entire dataset, the training pipeline divides the dataset into four semantic groups. Each group receives its own WorldDiT model, trained independently on a dedicated TPU chip.
25
 
26
+ The models operate in a learned spatiotemporal latent space produced by a separate Variational Autoencoder (VAE). Text embeddings condition video generation, while temporal context and hand-related features are incorporated into the automatically generated training prompts.
27
 
28
+ The implementation uses PyTorch, PyTorch/XLA, and Google Cloud TPUs.
29
+
30
+ ## Overview
31
 
32
+ The training pipeline consists of four main stages:
33
 
34
+ 1. **Semantic clustering:** Video segments are grouped into four clusters using cosine similarity between text embeddings.
35
+ 2. **Group preparation:** A larger and more diverse set of clips is selected for each cluster. New prompts are generated using task descriptions, temporal context, environment information, and hand features.
36
+ 3. **Independent model training:** Four separate WorldDiT models are trained, with one training process assigned to each TPU chip.
37
+ 4. **Video reconstruction:** The trained EMA models generate latent video sequences, which are decoded into RGB videos using the pretrained VAE.
 
 
38
 
39
+ Each group is trained as an independent model rather than as a shard of one distributed model.
40
 
41
+ ## Architecture
 
 
 
42
 
43
+ ### Model configuration
44
 
45
+ Each group uses the same WorldDiT architecture:
46
 
47
+ | Parameter | Value |
48
  |---|---:|
49
+ | Hidden size | 512 |
50
+ | Transformer depth | 10 |
51
+ | Attention heads | 8 |
52
+ | Head dimension | 64 |
53
+ | Approximate parameters | 50M |
54
  | Latent channels | 128 |
55
+ | Temporal latent positions | 13 |
56
+ | Spatial latent resolution | 8 × 8 |
57
  | Tokens per sequence | 832 |
58
  | Text embedding dimension | 768 |
59
+ | Text sequence length in v2 | 64 |
60
 
61
+ The model is instantiated as:
62
 
63
+ ```python
64
+ model = WorldDiT(
65
+ d=512,
66
+ depth=10,
67
+ heads=8
68
+ )
69
+ ```
70
 
71
+ The parameter count is also printed by the training script when each group starts.
72
 
73
+ ### Video latent representation
74
 
75
+ WorldDiT does not directly predict RGB pixels.
 
 
76
 
77
+ Each training example is a VAE-encoded video sequence with the shape:
78
 
79
+ ```text
80
+ (128, 13, 8, 8)
81
+ ```
82
 
83
+ The dimensions represent:
84
 
85
+ - 128 latent channels.
86
+ - 13 temporal positions.
87
+ - An 8 × 8 spatial latent grid.
88
+
89
+ The resulting sequence contains 832 spatiotemporal tokens.
90
 
91
+ The pretrained VAE decoder reconstructs the generated latent representation into RGB video. In the current implementation, the decoded output has the shape:
92
 
93
+ ```text
94
+ (3, 97, 256, 256)
95
+ ```
96
 
97
+ This corresponds to 97 RGB frames at 256 × 256 resolution.
98
 
99
+ ## Semantic clustering
100
 
101
+ The initial clustering stage divides the dataset into four groups.
102
 
103
+ The clustering script uses precomputed text embeddings and attention masks:
104
 
105
+ ```text
106
+ ~/wm_data/
107
+ txt_emb.npy
108
+ txt_mask.npy
109
+ txt_idx.npy
110
+ texts.json
111
+ meta.json
112
  ```
113
 
114
+ For each text entry, token embeddings are averaged using the attention mask and normalized to unit length.
115
 
116
+ The embedding of each video segment is selected through `txt_idx.npy`.
117
 
118
+ Clustering uses cosine similarity and k-means++ initialization:
 
 
119
 
120
+ | Parameter | Value |
121
+ |---|---:|
122
+ | Number of groups | 4 |
123
+ | Initialization | k-means++ |
124
+ | Restarts | 5 |
125
+ | Iterations per restart | 50 |
126
+ | Distance | Cosine distance |
127
 
128
+ The initial clustering stage selects 10 representative clips per group and writes:
129
 
130
  ```text
131
+ ~/wm_groups/groups.json
 
 
 
 
 
132
  ```
133
 
134
+ The v2 preparation stage reuses the same clustering procedure but increases the selection to 38 clips per group.
135
 
136
+ This produces four groups with up to 38 selected video segments each.
 
 
 
 
 
137
 
138
+ ## V2 dataset preparation
 
139
 
140
+ The v2 pipeline creates more detailed and diverse text conditions for each selected video segment.
 
 
141
 
142
+ It uses several sources of information:
143
 
144
+ - Original task descriptions.
145
+ - Environment descriptions.
146
+ - Current action or keystep.
147
+ - Previous and next actions from the same video.
148
+ - Hand visibility and movement features.
149
+ - Whether a hand is gripping an object or has an open palm.
150
+ - Approximate hand movement speed.
151
+ - The position of the segment within the full video.
152
 
153
+ ### Hand features
154
 
155
+ The preparation script loads:
 
 
 
156
 
157
+ ```text
158
+ ~/wm_data/hand_feats.npy
159
+ ```
160
 
161
+ The array is reshaped into:
162
 
163
  ```text
164
+ (-1, 4, 2, 10)
165
  ```
166
 
167
+ The hand features are used to construct textual descriptions of the observed hands.
168
 
169
+ The generated descriptions distinguish between:
170
 
171
+ - Both hands visible.
172
+ - One hand visible.
173
+ - Hands mostly outside the frame.
174
+ - Gripping an object.
175
+ - Open palm.
176
+ - Quick hand movement.
177
+ - Slow deliberate motion.
178
+ - Hands nearly still.
179
+
180
+ ### Temporal context
181
 
182
+ The preparation stage reads:
183
 
184
  ```text
185
+ ~/ego-raw/spans.csv
186
  ```
187
 
188
+ It uses the `clip_id`, `start_s`, `end_s`, and `keystep` fields to recover the previous and next actions surrounding each selected segment.
189
 
190
+ This adds temporal context to the generated prompt.
191
 
192
+ For example, a prompt can describe the current action while also mentioning what happened immediately before it and what action follows.
193
+
194
+ ### Prompt generation
195
+
196
+ The v2 pipeline uses multiple prompt templates combining task, environment, current action, previous and next actions, hand state, and movement speed.
197
+
198
+ The resulting prompts are encoded with the external `text_emb.py` module.
199
+
200
+ The encoder is configured with:
201
+
202
+ ```python
203
+ text_emb.L = 64
204
  ```
205
 
206
+ For each group, the preparation stage writes:
207
 
208
+ ```text
209
+ ~/wm_groups/v2/
210
+ groups_v2.json
211
+ emb_g0.npy
212
+ emb_g1.npy
213
+ emb_g2.npy
214
+ emb_g3.npy
215
+ msk_g0.npy
216
+ msk_g1.npy
217
+ msk_g2.npy
218
+ msk_g3.npy
219
+ ```
220
 
221
+ Each group's embeddings and masks correspond to that group's selected prompts.
222
 
223
+ The generated group metadata contains:
224
 
225
+ - Group identifier.
226
+ - Original cluster size.
227
+ - Most common tasks.
228
+ - Selected clip indices.
229
+ - Generated text prompts.
230
 
231
+ ## Flow-matching objective
232
 
233
+ WorldDiT is trained using a flow-matching objective.
 
234
 
235
+ Given a clean latent sequence \(x_0\), Gaussian noise \(\epsilon\), and a time variable \(t\), the training script constructs:
 
236
 
237
+ \[
238
+ x_t=(1-t)x_0+t\epsilon
239
+ \]
240
 
241
+ The target velocity is:
242
 
243
+ \[
244
+ v=\epsilon-x_0
245
+ \]
246
 
247
+ The model predicts:
248
 
249
+ \[
250
+ \hat v_\theta(x_t,t,c)
251
+ \]
252
 
253
+ where \(c\) represents the text embeddings and attention mask.
254
 
255
+ The training loss is:
 
256
 
257
+ \[
258
+ \mathcal{L}=
259
+ \mathbb{E}\left[
260
+ \left\|
261
+ \hat v_\theta(x_t,t,c)-(\epsilon-x_0)
262
+ \right\|_2^2
263
+ \right]
264
+ \]
265
 
266
+ The time variable is sampled independently for each temporal position in a video sequence. With a probability of 50%, all temporal positions in a training example share the same time value.
 
 
267
 
268
+ ### Optimization
269
 
270
+ The training process uses:
271
 
272
+ - AdamW optimizer.
273
+ - BF16 autocasting on XLA.
274
+ - Gradient clipping.
275
+ - Learning-rate warmup.
276
+ - Exponential moving average of model parameters.
277
 
278
+ The EMA parameters are updated with a decay of 0.99.
279
 
280
+ ## Repository and file layout
281
 
282
+ The scripts expect the following files under the user's home directory:
283
 
284
  ```text
285
+ ~/
286
+ wm.py
287
+ vae.py
288
+ text_emb.py
289
+
290
+ wm_data/
291
+ latents.npy
292
+ txt_emb.npy
293
+ txt_mask.npy
294
+ txt_idx.npy
295
+ texts.json
296
+ meta.json
297
+ hand_feats.npy
298
+
299
+ wm_run/
300
+ lat_stats.npz
301
+
302
+ vae_run/
303
+ last.pt
304
+
305
+ ego-raw/
306
+ spans.csv
307
+
308
+ wm_groups/
309
+ cluster.py
310
+ train_group.py
311
+ groups.json
312
+
313
+ v2/
314
+ groups_v2.json
315
+ emb_g0.npy
316
+ emb_g1.npy
317
+ emb_g2.npy
318
+ emb_g3.npy
319
+ msk_g0.npy
320
+ msk_g1.npy
321
+ msk_g2.npy
322
+ msk_g3.npy
323
+ run_v2.sh
324
+ stdout_g0.txt
325
+ stdout_g1.txt
326
+ stdout_g2.txt
327
+ stdout_g3.txt
328
+ done.txt
329
+
330
+ g0/
331
+ wm.pt
332
+ log.txt
333
+ gen10.mp4
334
+
335
+ g1/
336
+ wm.pt
337
+ log.txt
338
+ gen10.mp4
339
+
340
+ g2/
341
+ wm.pt
342
+ log.txt
343
+ gen10.mp4
344
+
345
+ g3/
346
+ wm.pt
347
+ log.txt
348
+ gen10.mp4
349
+
350
+ train_group2.py
351
  ```
352
 
353
+ The v2 preparation script generates `train_group2.py` and `run_v2.sh`.
354
+
355
+ The exact contents of the generated training script depend on the source `train_group.py` and the transformations performed by the preparation script.
356
+
357
+ ## Dataset format
358
+
359
+ The training pipeline expects precomputed NumPy arrays.
360
 
361
  | File | Expected shape | Description |
362
  |---|---|---|
363
+ | `latents.npy` | `(N, 128, 13, 8, 8)` | VAE-encoded video segments |
364
+ | `txt_emb.npy` | `(M, L, 768)` | Original text embeddings |
365
  | `txt_mask.npy` | `(M, L)` | Text attention masks |
366
+ | `txt_idx.npy` | `(N,)` | Mapping from video segments to text entries |
367
+ | `hand_feats.npy` | Compatible with `(-1, 4, 2, 10)` | Hand-related features |
368
+ | `texts.json` | List of strings | Text descriptions |
369
+ | `meta.json` | Segment metadata | Clip ID and segment start/end times |
370
 
371
+ The latent normalization statistics are stored in:
 
 
372
 
373
  ```text
374
  ~/wm_run/lat_stats.npz
375
  ```
376
 
377
+ The archive must contain:
378
 
379
  ```python
380
  mean
381
  std
382
  ```
383
 
384
+ The trainer normalizes latents using:
385
 
386
+ ```python
387
+ normalized_latents = (latents - mean) / std
388
+ ```
389
 
390
+ Before VAE decoding, the original scale is restored:
391
 
392
+ ```python
393
+ decoded_latents = generated_latents * std + mean
394
+ ```
395
 
396
+ The selected clip indices in the group metadata must be valid indices into `latents.npy`, `txt_idx.npy`, `meta.json`, and the corresponding hand feature data.
 
 
 
397
 
398
+ ## Training environment
399
+
400
+ The implementation uses:
 
 
401
 
402
+ - Python.
403
+ - PyTorch.
404
+ - PyTorch/XLA.
405
+ - NumPy.
406
+ - PyAV.
407
+ - The project's `wm.py`, `vae.py`, and `text_emb.py` modules.
408
+ - Google Cloud TPU runtime.
409
 
410
+ The PyTorch and PyTorch/XLA versions must be compatible with the installed TPU runtime.
411
+
412
+ The group training script uses:
413
+
414
+ ```python
415
+ import torch_xla.core.xla_model as xm
416
+
417
+ dev = xm.xla_device()
418
  ```
419
 
420
+ It uses one TPU device per process.
421
 
422
+ The v2 launcher starts four separate processes, assigning one visible TPU chip to each group. This is not a single GSPMD-sharded model.
423
 
424
+ ## Training
425
 
426
+ ### Step 1. Prepare the initial groups
427
 
428
+ Run the initial clustering script:
429
 
430
+ ```bash
431
+ python ~/wm_groups/cluster.py
432
+ ```
433
 
434
+ This creates:
 
 
 
 
 
435
 
436
+ ```text
437
+ ~/wm_groups/groups.json
438
+ ```
439
 
440
+ The v2 preparation script reads the original clustering implementation and reuses its clustering calculations.
441
 
442
+ ### Step 2. Prepare the v2 groups
443
+
444
+ Run the v2 preparation code.
445
+
446
+ This stage:
447
+
448
+ 1. Loads the original clustering implementation.
449
+ 2. Repeats clustering with four groups and up to 38 selected clips per group.
450
+ 3. Loads hand features and temporal action spans.
451
+ 4. Creates richer prompts for selected clips.
452
+ 5. Encodes prompts using `text_emb.py` with a sequence length of 64.
453
+ 6. Saves group metadata, embeddings, and masks.
454
+ 7. Generates `train_group2.py`.
455
+ 8. Generates `run_v2.sh`.
456
+
457
+ The preparation script should finish by printing:
458
+
459
+ ```text
460
+ BUILD_OK
461
  ```
462
 
463
+ After this step, verify that the generated files exist:
464
 
465
+ ```bash
466
+ ls -lh ~/wm_groups/v2/groups_v2.json
467
+ ls -lh ~/wm_groups/v2/emb_g*.npy
468
+ ls -lh ~/wm_groups/v2/msk_g*.npy
469
+ ls -lh ~/wm_groups/train_group2.py
470
+ ls -lh ~/wm_groups/v2/run_v2.sh
471
+ ```
472
 
473
+ ### Step 3. Start all four TPU training processes
474
 
475
+ Run:
476
 
477
+ ```bash
478
+ bash ~/wm_groups/v2/run_v2.sh
479
+ ```
480
 
481
+ The generated launcher executes:
482
 
483
+ ```bash
484
+ for g in 0 1 2 3; do
485
+ TPU_CHIPS_PER_PROCESS_BOUNDS=1,1,1 \
486
+ TPU_PROCESS_BOUNDS=1,1,1 \
487
+ TPU_VISIBLE_CHIPS=$g \
488
+ TPU_PROCESS_PORT=$((8476+g)) \
489
+ CLOUD_TPU_TASK_ID=0 \
490
+ PJRT_DEVICE=TPU \
491
+ GROUP=$g \
492
+ MAX_MIN=12 \
493
+ RESUME=1 \
494
+ nohup ~/xla_env/bin/python \
495
+ ~/wm_groups/train_group2.py \
496
+ > ~/wm_groups/v2/stdout_g$g.txt 2>&1 &
497
+ done
498
+
499
+ wait
500
+ echo DONE > ~/wm_groups/v2/done.txt
501
+ ```
502
+
503
+ Each process receives its own `GROUP` value:
504
+
505
+ | Process | Group | TPU chip |
506
+ |---|---:|---:|
507
+ | Process 0 | 0 | 0 |
508
+ | Process 1 | 1 | 1 |
509
+ | Process 2 | 2 | 2 |
510
+ | Process 3 | 3 | 3 |
511
+
512
+ All four groups train concurrently.
513
+
514
+ The launcher uses the `~/xla_env/bin/python` environment.
515
+
516
+ ### Important: checkpoint initialization
517
 
518
+ The launcher sets:
519
 
520
  ```bash
521
+ RESUME=1
522
  ```
523
 
524
+ This means the training script must find the checkpoint specified in its `RESUME` branch.
525
+
526
+ There are two checkpoint-path variants in the current scripts:
527
+
528
+ - The original group trainer loads `OUT/wm.pt`.
529
+ - The v2 preparation code rewrites the generated trainer to initialize from `~/wm_groups/g{G}/wm.pt`.
530
+ - Another trainer variant may use `~/wm_groups/v2/g{G}/wm_prev.pt`.
531
+
532
+ These are different paths and must not be treated as interchangeable.
533
+
534
+ Before launching, inspect the actual generated trainer:
535
+
536
+ ```bash
537
+ grep -n -E 'RESUME|torch.load|OUT =' ~/wm_groups/train_group2.py
538
+ ```
539
+
540
+ Make sure the checkpoint path in the `RESUME` branch exists for all four groups.
541
+
542
+ The intended v2 initialization in the preparation script uses the previous group checkpoints under:
543
+
544
+ ```text
545
+ ~/wm_groups/g0/wm.pt
546
+ ~/wm_groups/g1/wm.pt
547
+ ~/wm_groups/g2/wm.pt
548
+ ~/wm_groups/g3/wm.pt
549
+ ```
550
 
551
+ If training should start from randomly initialized weights, remove `RESUME=1` from the generated launcher or unset the resume branch in the training script.
552
 
553
+ ## Training configuration
554
 
555
+ Each group uses the following defaults:
556
 
557
  | Parameter | Value |
558
  |---|---:|
559
+ | Model | WorldDiT |
560
+ | Hidden size | 512 |
561
+ | Depth | 10 |
562
+ | Attention heads | 8 |
563
+ | Batch size | 20 |
564
+ | Learning rate | 4e-4 |
565
  | Optimizer | AdamW |
566
+ | Betas | (0.9, 0.95) |
567
+ | Weight decay | 0 |
568
+ | Gradient clipping | 1.0 |
569
+ | LR warmup | 150 steps |
570
+ | EMA decay | 0.99 |
571
+ | Default maximum runtime | 8 minutes |
572
+ | Runtime in v2 launcher | 12 minutes |
573
+ | Default early-stop target | 0.05 |
574
+ | Loss logging interval | 50 steps |
575
+
576
+ The learning-rate scheduler uses linear warmup:
577
 
578
+ \[
579
+ lr_s=lr_{\mathrm{base}}\cdot
580
+ \min\left(1,\frac{s+1}{150}\right)
581
+ \]
582
+
583
+ The training loop stops when the runtime limit is reached or when the configured early-stop condition is met.
584
+
585
+ The early-stop condition is checked after at least 600 steps:
586
+
587
+ \[
588
+ \text{average recent loss}<\text{TARGET}
589
+ \]
590
+
591
+ The trainer calculates the average using the most recent six logged loss measurements.
592
+
593
+ The runtime and learning-rate settings can be changed using environment variables:
594
 
595
  ```bash
596
+ MAX_MIN=12
597
+ LR=4e-4
598
+ TARGET=0.05
599
  ```
600
 
601
+ ## Checkpoints
602
 
603
+ Each group writes its checkpoint to:
604
 
605
  ```text
606
+ ~/wm_groups/v2/g0/wm.pt
607
+ ~/wm_groups/v2/g1/wm.pt
608
+ ~/wm_groups/v2/g2/wm.pt
609
+ ~/wm_groups/v2/g3/wm.pt
610
  ```
611
 
612
+ The checkpoint contains:
613
 
614
+ - `ema`: EMA model state dictionary.
615
+ - `clips`: Selected clip indices.
616
+ - `step`: Final training step.
617
+ - `cfg`: Model configuration.
618
 
619
+ The model configuration is:
620
 
621
+ ```python
622
+ {
623
+ "d": 512,
624
+ "depth": 10,
625
+ "heads": 8
626
+ }
627
  ```
628
 
629
+ The checkpoint stores EMA weights rather than the complete optimizer and scheduler state.
630
+
631
+ AdamW optimizer state is not saved, and resuming does not restore the original optimizer or learning-rate scheduler state.
632
+
633
+ ## Video generation and validation
634
 
635
+ After the training loop, each group runs a generation test using its EMA model.
 
 
 
636
 
637
+ The generation process:
638
 
639
+ 1. Creates a Gaussian noise latent sequence.
640
+ 2. Uses the corresponding text embeddings and masks.
641
+ 3. Runs 40 flow-matching sampling steps.
642
+ 4. Compares generated latents with the selected original latents.
643
+ 5. Decodes generated latents through the pretrained VAE.
644
+ 6. Saves a video montage.
645
 
646
+ The sampling update is:
647
+
648
+ \[
649
+ x \leftarrow x-\frac{\hat v_\theta(x,t,c)}{40}
650
+ \]
651
+
652
+ The initial noise uses a fixed seed of 0.
653
+
654
+ The trainer logs a latent-space MSE for each selected prompt.
655
+
656
+ The VAE checkpoint is loaded from:
657
 
658
  ```text
659
+ ~/vae_run/last.pt
 
660
  ```
661
 
662
+ The checkpoint must contain the VAE state dictionary under the `model` key.
663
+
664
+ The decoder is obtained through:
665
+
666
+ ```python
667
+ vae = VAE()
668
+ vae.load_state_dict(
669
+ torch.load(
670
+ "~/vae_run/last.pt",
671
+ map_location="cpu"
672
+ )["model"]
673
+ )
674
 
675
+ decoder = vae.dec
676
+ ```
677
+
678
+ The final video montage is written to:
679
 
680
  ```text
681
+ ~/wm_groups/v2/g0/gen10.mp4
682
+ ~/wm_groups/v2/g1/gen10.mp4
683
+ ~/wm_groups/v2/g2/gen10.mp4
684
+ ~/wm_groups/v2/g3/gen10.mp4
685
  ```
686
 
687
+ Each montage contains 10 generated video sequences arranged in a 5-column, 2-row grid.
688
 
689
+ The montage resolution is 1280 × 512, encoded using H.264 at 24 FPS.
690
 
691
+ These are group-level preview montages, not individual MP4 files for every prompt.
692
 
693
+ ## Monitoring
694
 
695
+ Each group has its own training log:
696
 
697
+ ```text
698
+ ~/wm_groups/v2/g0/log.txt
699
+ ~/wm_groups/v2/g1/log.txt
700
+ ~/wm_groups/v2/g2/log.txt
701
+ ~/wm_groups/v2/g3/log.txt
702
+ ```
703
 
704
+ Standard output and errors from the four processes are stored in:
705
 
706
+ ```text
707
+ ~/wm_groups/v2/stdout_g0.txt
708
+ ~/wm_groups/v2/stdout_g1.txt
709
+ ~/wm_groups/v2/stdout_g2.txt
710
+ ~/wm_groups/v2/stdout_g3.txt
711
+ ```
712
 
713
+ The launcher creates:
714
 
715
+ ```text
716
+ ~/wm_groups/v2/done.txt
717
+ ```
718
 
719
+ when all four processes finish.
720
 
721
+ To inspect the output:
 
722
 
723
+ ```bash
724
+ tail -f ~/wm_groups/v2/stdout_g0.txt
725
  ```
726
 
727
+ To inspect the training log:
728
 
729
+ ```bash
730
+ tail -f ~/wm_groups/v2/g0/log.txt
 
 
 
 
731
  ```
732
 
733
+ To check all groups:
734
 
735
+ ```bash
736
+ for g in 0 1 2 3; do
737
+ echo "===== GROUP $g ====="
738
+ tail -n 10 ~/wm_groups/v2/g$g/log.txt
739
+ done
740
+ ```
741
+
742
+ ## Inference status
743
+
744
+ The current implementation includes a generation and validation stage at the end of each training run.
745
+
746
+ It does not currently provide a separate standalone inference script for loading a Hugging Face checkpoint and generating an arbitrary video from a new prompt.
747
+
748
+ The generated videos are produced using the prompts and embeddings prepared for the corresponding group.
749
+
750
+ A separate inference pipeline would need to load the selected group's EMA checkpoint, recreate the text embeddings, initialize latent noise, run flow-matching sampling, and decode the result with the compatible VAE.
751
+
752
+ ## Current scope and limitations
753
 
754
+ WorldDiT is a text-conditioned latent video generation system trained using clustered egocentric video segments.
755
+
756
+ The v2 preparation stage uses hand-related features and temporal action context to build richer text prompts.
757
+
758
+ However, the current implementation does not provide:
759
+
760
+ - Explicit robot action conditioning.
761
+ - Direct robot control.
762
+ - A physics-based simulator.
763
+ - An interactive environment that reacts to agent actions.
764
+ - A standalone arbitrary-prompt inference interface.
765
+ - A standardized benchmark evaluation.
766
+
767
+ The models are trained independently for their respective data groups.
768
+
769
+ ## Project summary
770
+
771
+ | Property | Description |
772
+ |---|---|
773
+ | Project | LiberalMind |
774
+ | Model family | WorldDiT |
775
+ | Training approach | Four independent group-specific models |
776
+ | Model size | Approximately 50M parameters per group |
777
+ | Number of groups | 4 |
778
+ | Selected clips | Up to 38 per group in v2 |
779
+ | Training hardware | Google Cloud TPU |
780
+ | TPU allocation | One TPU chip per group process |
781
+ | Framework | PyTorch / PyTorch-XLA |
782
+ | Video representation | VAE latent space |
783
+ | Latent shape | 128 × 13 × 8 × 8 |
784
+ | Text conditioning | 768-dimensional embeddings |
785
+ | Training objective | Flow matching |
786
+ | Optimization | AdamW with EMA |
787
+ | Video decoding | Separate pretrained VAE |
788
+
789
+ **WorldDiT is a research implementation of clustered, text-conditioned latent video world models.**