arcticoneai commited on
Commit
0739f0a
·
verified ·
1 Parent(s): 68c1a0f

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +368 -0
README.md ADDED
@@ -0,0 +1,368 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: pytorch
3
+ pipeline_tag: text-to-video
4
+ tags:
5
+ - text-to-video
6
+ - video-generation
7
+ - world-model
8
+ - video
9
+ - latent-video
10
+ - text-conditioned
11
+ - flow-matching
12
+ - transformer
13
+ - pytorch
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
+ ## Text conditioning
87
+
88
+ Text embeddings are supplied by the external `text_emb.py` module.
89
+
90
+ The expected embedding dimension is 768. The training dataset stores precomputed embeddings and attention masks, while the generation test uses:
91
+
92
+ ```python
93
+ from text_emb import encode
94
+
95
+ embeddings, attention_mask = encode([PROMPT])
96
+ ```
97
+
98
+ The base configuration uses classifier-free text dropout with a probability of 10%.
99
+
100
+ The large configuration uses conditional training without text dropout.
101
+
102
+ ## Video representation and VAE
103
+
104
+ WorldDiT does not directly predict RGB pixels.
105
+
106
+ Instead, it operates in the latent space of a separately trained VAE.
107
+
108
+ The VAE is responsible for:
109
+
110
+ 1. Encoding video sequences into compact spatiotemporal representations.
111
+ 2. Decoding generated latents into RGB video frames.
112
+
113
+ The training and generation scripts expect the VAE checkpoint at:
114
+
115
+ ```text
116
+ ~/vae_run/last.pt
117
+ ```
118
+
119
+ The checkpoint must contain a model state dictionary under the `model` key.
120
+
121
+ The decoder is loaded from `VAE().dec` and used to reconstruct generated videos.
122
+
123
+ The VAE used to decode generated latents must be compatible with the VAE used to encode the training data.
124
+
125
+ ## Dataset format
126
+
127
+ Training uses precomputed latent sequences and text embeddings stored as NumPy arrays.
128
+
129
+ Expected directory:
130
+
131
+ ```text
132
+ ~/wm_data/
133
+ latents.npy
134
+ txt_emb.npy
135
+ txt_mask.npy
136
+ txt_idx.npy
137
+ ```
138
+
139
+ The expected tensor layouts are:
140
+
141
+ | File | Expected shape | Description |
142
+ |---|---|---|
143
+ | `latents.npy` | `(N, 128, 13, 8, 8)` | VAE-encoded video sequences |
144
+ | `txt_emb.npy` | `(M, L, 768)` | Text embeddings |
145
+ | `txt_mask.npy` | `(M, L)` | Text attention masks |
146
+ | `txt_idx.npy` | `(N,)` | Mapping from video sequences to text entries |
147
+
148
+ `N` is the number of video sequences, `M` is the number of text entries, and `L` is the text sequence length.
149
+
150
+ Each element of `txt_idx.npy` selects the corresponding text embedding and attention mask for a video sequence.
151
+
152
+ The normalization statistics are stored in:
153
+
154
+ ```text
155
+ ~/wm_run/lat_stats.npz
156
+ ```
157
+
158
+ The file must contain:
159
+
160
+ ```python
161
+ mean
162
+ std
163
+ ```
164
+
165
+ These statistics are used to normalize training latents and restore their original scale before VAE decoding.
166
+
167
+ The preprocessing pipeline that creates these arrays is separate from the WorldDiT training scripts.
168
+
169
+ ## Repository structure
170
+
171
+ The training scripts expect the following files:
172
+
173
+ ```text
174
+ ~/wm.py
175
+ ~/vae.py
176
+ ~/text_emb.py
177
+
178
+ ~/wm_data/
179
+ latents.npy
180
+ txt_emb.npy
181
+ txt_mask.npy
182
+ txt_idx.npy
183
+
184
+ ~/vae_run/
185
+ last.pt
186
+
187
+ ~/wm_run/
188
+ lat_stats.npz
189
+ ```
190
+
191
+ `wm.py` contains the WorldDiT architecture.
192
+
193
+ `vae.py` contains the VAE architecture.
194
+
195
+ `text_emb.py` implements text embedding extraction.
196
+
197
+ The dataset must be prepared before starting WorldDiT training.
198
+
199
+ ## Training environment
200
+
201
+ The implementation uses:
202
+
203
+ - Python
204
+ - PyTorch
205
+ - PyTorch/XLA
206
+ - NumPy
207
+ - PyAV
208
+ - Google Cloud TPU runtime
209
+
210
+ The TPU environment must have mutually compatible PyTorch and PyTorch/XLA versions.
211
+
212
+ The training scripts use:
213
+
214
+ ```python
215
+ xr.use_spmd()
216
+ ```
217
+
218
+ and a GSPMD mesh for distributed execution.
219
+
220
+ The large configuration additionally applies parameter sharding across TPU devices.
221
+
222
+ ## Training
223
+
224
+ ### Base configuration — ~159M parameters
225
+
226
+ Save the base training script as `train.py`.
227
+
228
+ Default configuration:
229
+
230
+ | Parameter | Value |
231
+ |---|---:|
232
+ | Batch size | 64 |
233
+ | Learning rate | 2e-4 |
234
+ | Optimizer | AdamW |
235
+ | EMA decay | 0.999 |
236
+ | Checkpoint interval | 2,000 steps |
237
+ | Default runtime | 240 minutes |
238
+ | Sampling steps | 40 |
239
+ | Classifier-free guidance | 4.0 |
240
+
241
+ Launch:
242
+
243
+ ```bash
244
+ MAX_MIN=240 python train.py
245
+ ```
246
+
247
+ The script automatically resumes model and EMA weights if `~/wm_run/wm.pt` exists.
248
+
249
+ ### Large configuration — ~1.056B parameters
250
+
251
+ Save the large training script as `train_1b.py`.
252
+
253
+ Default configuration:
254
+
255
+ | Parameter | Value |
256
+ |---|---:|
257
+ | Hidden size | 1408 |
258
+ | Depth | 24 |
259
+ | Attention heads | 22 |
260
+ | Batch size | 32 |
261
+ | Learning rate | 1e-4 |
262
+ | Warmup | 1,000 steps |
263
+ | Optimizer | AdamW |
264
+ | EMA decay | 0.999 |
265
+ | Checkpoint interval | 1,000 steps |
266
+ | Default runtime | 300 minutes |
267
+ | Sampling steps | 40 |
268
+ | Classifier-free guidance | 1.0 |
269
+
270
+ Launch:
271
+
272
+ ```bash
273
+ MAX_MIN=300 python train_1b.py
274
+ ```
275
+
276
+ The large model uses GSPMD parameter sharding to distribute model weights across TPU devices.
277
+
278
+ The large training script automatically resumes from:
279
+
280
+ ```text
281
+ ~/wm1b_run/wm.pt
282
+ ```
283
+
284
+ Both scripts use `MAX_MIN` to control the duration of a training run.
285
+
286
+ ## Checkpoints and generated videos
287
+
288
+ Training checkpoints are saved as:
289
+
290
+ ```text
291
+ ~/wm_run/wm.pt
292
+ ~/wm1b_run/wm.pt
293
+ ```
294
+
295
+ Each checkpoint contains:
296
+
297
+ - `model`: Current model parameters.
298
+ - `ema`: Exponential moving average parameters.
299
+ - `step`: Training step.
300
+ - `cfg`: Architecture configuration (large model only).
301
+
302
+ The base checkpoint does not include a `cfg` field.
303
+
304
+ The training scripts save checkpoints periodically and generate test videos using the EMA model.
305
+
306
+ Generated videos are written to the corresponding output directory:
307
+
308
+ ```text
309
+ test_step0002000.mp4
310
+ test_step0004000.mp4
311
+ ```
312
+
313
+ 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.
314
+
315
+ Training logs are stored in:
316
+
317
+ ```text
318
+ log.txt
319
+ ```
320
+
321
+ ## Resuming training
322
+
323
+ Both scripts automatically detect an existing `wm.pt` checkpoint.
324
+
325
+ The model weights, EMA weights, and training step are restored.
326
+
327
+ **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.
328
+
329
+ ## Current scope and limitations
330
+
331
+ The current implementation is a text-conditioned latent video generation model.
332
+
333
+ 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.
334
+
335
+ The VAE, text encoder, and dataset preprocessing are separate components.
336
+
337
+ A standalone inference pipeline and a standardized benchmark evaluation are not included in these training scripts.
338
+
339
+ ## Implementation
340
+
341
+ The main architecture is defined in `wm.py`:
342
+
343
+ ```python
344
+ from wm import WorldDiT
345
+
346
+ model = WorldDiT()
347
+ ```
348
+
349
+ For the large configuration:
350
+
351
+ ```python
352
+ model = WorldDiT(
353
+ d=1408,
354
+ depth=24,
355
+ heads=22
356
+ )
357
+ ```
358
+
359
+ The implementation is built with PyTorch and uses PyTorch/XLA for TPU training.
360
+
361
+ ---
362
+
363
+ **Project:** LiberalMind
364
+ **Model family:** WorldDiT
365
+ **Task:** Text-conditioned latent video generation
366
+ **Training objective:** Flow matching
367
+ **Hardware target:** Google Cloud TPU
368
+ **Framework:** PyTorch / PyTorch-XLA