This document describes:
- The current implementation in this repository (data, model, training, eval, artifacts).
- A practical, step-by-step migration plan to evolve the system into a latent-flow video generation model.
The intent is incremental modernization with measurable checkpoints, not a full rewrite in one pass.
- Task: action-conditioned video prediction in Procgen, trained offline.
- Inputs:
- Past frame stack:
(B, n_past_frames, 84, 84), float32 in[0,1]. - Past actions:
(B, n_past_actions), int64. - Future actions:
(B, n_future_frames), int64.
- Past frame stack:
- Target:
- Future frames:
(B, n_future_frames, 84, 84), float32 in[0,1].
- Future frames:
- Default config currently uses
n_past_frames=4,n_past_actions=3,n_future_frames=4.
- Dataset generation (
src/dataset/generate_dataset.py):- Random policy rollouts in Procgen.
- Stores sharded memmap
.npyfiles for frames/actions/done. - Manifest contains shard metadata and
seq_len.
- Offline dataset (
src/dataset/offline_dataset.py):- Memory-mapped random-access reads per sample.
- Converts uint8 frames to float32
[0,1]. - Slices sequences into
(past_frames, past_actions, future_actions, future_frames, done).
- Train/val split:
- Single dataset split using seeded random permutation and
data.val_ratio.
- Single dataset split using seeded random permutation and
- Environment creation (
src/envs/procgen_wrappers.py):- Uses Gymnasium env ids, with automatic Procgen registration via procgen+shimmy bridge.
- Wrapper stack:
- Extract RGB key when observation is dict
- Grayscale or RGB resize to
84x84 - Optional float normalize to
[0,1] - Frame stack along axis 0
- File:
src/models/world_model.py. - Model: encoder-decoder CNN with FiLM conditioning.
- Conditioning:
nn.Embeddingover discrete actions.- Concatenate past+future action embeddings into a flat conditioning vector.
- Inject conditioning into each encoder/decoder block via FiLM (
(1+gamma)*h + beta).
- Encoder:
- 4 conv stages with GroupNorm + ReLU.
- Adds CoordConv-like XY channels.
- Decoder:
- 4 upsampling stages + conv + GroupNorm + ReLU.
- Output are logits for Bernoulli/BCE objective.
- Output:
(B, n_future_frames, 84, 84)logits.
- File:
src/train.py. - Optimizer/schedule:
- Adam + mandatory OneCycleLR.
- Loss:
- Binary cross-entropy with logits.
- Motion-weighted per-pixel weighting:
- Compute motion mask from
|target - last_input_frame| > motion_tau. - Dilate mask with max-pool.
- Weight BCE map with
1 + motion_weight * motion_mask.
- Compute motion mask from
- Logging/artifacts:
- CSV metrics, PNG prediction strips, validation metrics, periodic rollout MP4s.
- Optional W&B logging.
- Checkpoint:
- Model + optimizer + training step + model hyperparameters.
- File:
src/eval.py. - Open-loop rollout:
- Warm-starts with real frames/actions.
- Predicts next frame, then feeds prediction back into the input stack.
- Environment steps with sampled actions to obtain GT for comparison.
- Metrics:
- MSE vs horizon.
- Artifacts:
- Side-by-side GT|prediction videos (MP4/GIF), horizon plot, CSV.
- Model file:
src/models/conv_autoencoder.py. - Class:
ConvAutoencoder. - Architecture:
- Strided-conv encoder with configurable downsample factor (
8or16). - Spatial latent via a
1x1conv head (latent_head). - Symmetric decoder with configurable upsampling path:
upsample_convorconv_transpose.
- Output uses sigmoid and is bounded in
[0,1].
- Strided-conv encoder with configurable downsample factor (
- Interface:
encode(x) -> zdecode(z) -> reconforward(x) -> recon
- Entrypoint:
src/train_autoencoder.pywith configconfig_autoencoder.yaml. - Optimization:
- AdamW.
- Gradient clipping.
- FP32-only training and validation (AMP/autocast disabled by design).
- Loss:
- Fixed reconstruction loss: Huber (
delta=1.0). - Variance regularizer on latent statistics:
var_reg_loss = var_reg_lambda * (Var(z) - var_target)^2- Default
var_target=1.0to keep latent variance calibrated rather than unbounded growth.
- No alternative reconstruction-loss modes.
- Fixed reconstruction loss: Huber (
- Validation:
- Reconstruction metrics: loss/MSE/PSNR/SSIM.
- Latent diagnostics:
val_latent_var(mean latent variance over batch)val_latent_mean_norm(mean L2 norm of latent vectors)
- Logs reconstruction grids (target vs recon).
- Logging and artifacts:
- CSV metrics + optional W&B.
- Checkpoints: latest, per-epoch, and best-val checkpoint.
- Resume supported from checkpoint.
- Clean end-to-end baseline (data generation -> train -> eval).
- Multi-step prediction support (
n_future_frames > 1). - Action conditioning integrated throughout via FiLM.
- Reproducible config-driven workflow and basic test coverage.
- Core predictive model is still pixel-space; latent autoencoder is not yet integrated into dynamics.
- Deterministic decoder (limited multimodal/stochastic futures).
- BCE on grayscale frames limits perceptual realism.
- No temporal latent dynamics model beyond short action-conditioned mapping.
- No transformer-based temporal modeling.
- Evaluation metric set is narrow (primarily MSE).
Move from:
x_{t-k:t} + a_{t:t+h-1} -> x_{t+1:t+h}in pixel space
to:
x -> zvia autoencoder (VAE/VQ/RAE style latent encoder)- conditional flow model in latent space to model
p(z_{future} | z_{past}, actions) - latent decoder
z -> xto reconstruct/generate frames
LatentEncoder/LatentDecoder:- Compression to spatial latent map (e.g. 4x-16x downsample).
- Current repo baseline already includes a deterministic ConvAutoencoder.
LatentDynamicsConditioner:- Encodes action history/future and optional past latents.
FlowBackbone:- U-Net or DiT-style network operating on latent tensors.
- Receives time/noise level embedding + action condition.
FlowObjective:- Flow matching or rectified-flow objective in latent space.
- Optional discriminator/perceptual loss in pixel space for sharper reconstructions.
- Add explicit interface contracts for batch structure and tensor shapes.
- Freeze reproducible baseline metrics on a fixed checkpoint/config:
- Train loss curve.
- Val loss.
- MSE@{1,5,10,30}.
- Add a model registry entry pattern so old/new models share train/eval plumbing.
Exit criteria:
- Baseline numbers recorded in a markdown table.
- Train/eval scripts can instantiate model by
model.type.
- Status: partially completed with deterministic ConvAutoencoder.
- Implemented:
src/models/conv_autoencoder.pywithencode/decode/forward.src/train_autoencoder.pyandconfig_autoencoder.yaml.- Validation metrics include MSE/PSNR/SSIM and latent diagnostics.
- Remaining for this phase:
- Add dedicated
eval_autoencoder.pyfor standalone reconstruction/video eval if needed. - Decide whether to keep deterministic AE only or add stochastic variants in a separate branch.
- Add dedicated
Exit criteria:
- Good reconstructions on validation videos.
- Stable latent scale/statistics tracked in logs.
- Replace pixel decoder target with latent target:
- Encode target frames with frozen AE encoder.
- Train action-conditioned predictor
z_past + actions -> z_future(L2 or Huber in latent space).
- Decode predicted latents for visualization and old eval compatibility.
- Keep same rollout API but route through latent predictor + decoder.
Exit criteria:
- Latent predictor beats pixel baseline on rollout MSE (after decode) or matches with lower compute.
- Multi-step latent rollouts remain stable.
- Introduce flow model module, e.g.
src/models/latent_flow.py. - Train with flow matching / rectified flow:
- Inputs: noisy latent target, conditioning context from past latents + actions.
- Network predicts velocity/transport field.
- Add sampling procedure for future latent trajectories.
- Support multiple samples per condition and sample selection metrics.
Exit criteria:
- Qualitative diversity appears for uncertain futures.
- Best-of-N and average sample metrics reported.
- Replace simple conditioner with a stronger temporal model:
- Option A: causal temporal transformer over latent tokens.
- Option B: DiT-style block with action conditioning cross-attention.
- Add positional encodings for time and frame index.
- Add teacher-forcing and scheduled sampling variants in latent rollout training.
Exit criteria:
- Better long-horizon consistency (MSE and visual coherence at horizon >=30).
- No regression in short-horizon quality.
- Data upgrades:
- Multi-game training option.
- Stronger exploration policy than random for richer transitions.
- Optional RGB mode (instead of grayscale-only) for richer visuals.
- Objective upgrades:
- Perceptual losses (LPIPS) on decoded frames.
- Optional adversarial fine-tuning for sharpness.
- Action-consistency auxiliary loss (predict action from latent transitions).
Exit criteria:
- Improved visual quality and temporal consistency on held-out seeds.
- Clear tradeoff documentation between compute and quality.
- Add configurable samplers (steps, guidance scale, temperature/noise).
- Export fast eval script for batch generation and benchmark dashboards.
- Add checkpoint versioning and compatibility loader for baseline checkpoints.
Exit criteria:
- One-command inference path for both deterministic and stochastic models.
- Reproducible benchmark report generated from CI or scripted run.
- New files likely needed:
src/models/latent_autoencoder.pysrc/models/latent_predictor.pysrc/models/latent_flow.pysrc/train_autoencoder.pysrc/train_latent_flow.pysrc/eval_latent.py
- Existing files to refactor gradually:
src/train.py(model factory and loss abstraction)src/eval.py(support latent decoding and multi-sample eval)src/config.py+config.yaml(new model/loss/sampler sections)src/utils/metrics.py(PSNR/SSIM/LPIPS, sample-based metrics)
- Risk: Latent collapse or poor reconstructions.
- Mitigation: start with reconstruction-only AE and monitor latent stats.
- Risk: Flow training instability.
- Mitigation: begin with deterministic latent predictor bridge (Phase 2) before stochastic flow.
- Risk: Metric mismatch with perceptual quality.
- Mitigation: add both distortion and perceptual metrics, plus fixed qualitative grids.
- Risk: Compute growth.
- Mitigation: keep small-latent baseline configs and stage complexity progressively.
- Add model registry (
model.type: pixel_world_model | latent_ae | latent_predictor | latent_flow_stub). - Train and benchmark existing
ConvAutoencoderon current dataset frames. - Add
eval_autoencoder.pythat writes recon videos + PSNR/SSIM/latent stats. - Implement deterministic
latent_predictor.pybridge and hook into existing rollout eval. - Lock comparison table in README for baseline world model vs latent bridge.
This sequence minimizes risk while steadily moving toward a modern latent-flow video generator.