# Cosmos + VTWM 架构文档 日期:2026-07-03 状态:draft,基于本轮全套 ablation + audit ## TL;DR 我们的 world model 用 **Cosmos CV4x8x8 tokenizer** 替换了原来的 VideoVAE+, 其他架构基本不变(DiffusionForcingDynamics + MM-DiT)。此 tokenizer 换掉带来: - 因果性打破 VideoVAE+ 里 tactile→view 的时序泄漏 shortcut - 视觉短-rollout LPIPS 降 24%,PSNR +3.3 dB - Tactile 短-rollout LPIPS 降 20-45%,PSNR +1-3.5 dB - Latent 分布更接近 N(0, 1),训练时噪声调度更"正常" - Anchor-based 时间结构(1 + 4×N raw 帧)能天然支持任意长视频 + 单张图统一 encoder 我们仍然遗留若干可优化点,见「已知问题」章节。 --- ## 1. Cosmos CV4x8x8 简介 - 来源:`nvidia/Cosmos-0.1-Tokenizer-CV4x8x8`(HF) - 类别:Continuous Video tokenizer - 压缩比:4× 时间 + 8×8 空间 - Latent shape 公式:`T_lat = (T_raw − 1) / 4 + 1` - 通道数:16 (`latent_channels`) - 部署格式:TorchScript `.jit`(encoder.jit 60.6 MB + decoder.jit 86.5 MB) - 因果性:严格 causal - `CausalConv3d._replication_pad`:只在时间前端 pad(`layers3d.py:93-97`) - `CausalTemporalAttnBlock` 用 `torch.tril` mask(`layers3d.py:366-369`) - 数值验证:喂 16 帧完全相同的 raw 帧,5 帧 latent **drift = 0.0000**(bf16 精度极限) **对比 VideoVAE+**:非对称右下 zero-pad + 双向 TemporalAttention → temporal 泄漏 5×, spatial 右下角有稳定的架构级 hotspot。 ## 2. 时间语义(这是 Cosmos vs VideoVAE+ 的核心不同) Cosmos 的 5 帧 latent(T_raw=16 输入下)代表: | ti | 语义 | Raw 帧范围 | |---|---|---| | 0 | **单帧 anchor**(image encoding) | raw[0] | | 1 | 第 1 组 4 帧 group | raw[1..4] | | 2 | 第 2 组 4 帧 group | raw[5..8] | | 3 | 第 3 组 4 帧 group | raw[9..12] | | 4 | 第 4 组 4 帧 group | raw[13..16] | **Canonical 输入是 T_raw=17**(1 + 4×4)。我们喂 T_raw=16 时 Cosmos 内部 用 `_replication_pad` 补 1 帧变成 17。 ## 3. VTWM (DiffusionForcingDynamics) 侧的参数 我们保持了跟 VideoVAE+ 版本一样的 world model 架构,只是 T_lat 从 4 → 5: ```yaml model: # 空间维度 latent_channels: 16 # ← Cosmos 输出 C latent_grid: 16 # ← Cosmos 8× 空间 (128 / 8) patch_size: 2 # spatial patchify → 8×8 = 64 spatial tokens # 网络 fusion_mode: mmdit_mv # multi-view MM-DiT embed_dim: 384 depth: 12 num_heads: 6 mlp_ratio: 4.0 # 时间 / action 处理 window: 5 # ← Cosmos T_lat (from data) history_len: 3 # ← min(config.history_len=8, T_lat=5) with factor=3 action_mode: full_window num_action_tokens: 4 # 15 raw actions → 4 latent actions via avg pool # Diffusion training noise: per_frame: false per_modality: true # 视觉/tactile 分开调度 clip_noise: 6.0 visual: {beta_schedule: sigmoid, timesteps: 1000, objective: pred_v, snr_clip: 5.0} tactile: {beta_schedule: cosine, timesteps: 1000, objective: pred_v, snr_clip: 5.0} tactile_delta_to_ref: true # z_t − z_ref 空间训练 tactile_event_abs_threshold: 0.05 # 07-03 audit 校准 data_mean: 0.0 data_std: 1.0 # Streams num_views: 3 # visual_left, visual_middle, visual_right num_tactiles: 2 # tactile_left, tactile_right ``` 参数量:**100.98 M**(跟 VideoVAE+ 版本完全相同)。 ## 4. 数据流:一次 forward ``` Raw window (16 raw frames, 128×128 RGB, uint8): view_left, view_middle, view_right (3 views) tactile_left, tactile_right (2 sensors) │ ▼ ← BGR→RGB swap (仅 visual) ▼ ← / 127.5 − 1.0 (normalize to [-1, 1]) ┌────────────────────────────────────┐ │ Cosmos CV4x8x8 Encoder (frozen) │ │ causal, right-left pad on time │ │ 8× spatial + (T-1)/4+1 temporal │ └────────────────────────────────────┘ │ ▼ Latent per stream: (C=16, T_lat=5, G=16, G=16) ti=0: raw[0] (single-frame anchor) ti=1..4: raw[1..16] (4-frame groups) │ ▼ ← _preprocess_tactiles_mv: z_tactile − z_p01_ref ▼ ← _stack_mv: cat views + tactiles along channel Stacked: (T=5, B, (Nv+Nt)*C=80, G, G) │ ▼ ┌────────────────────────────────────┐ │ DiffusionForcingDynamics │ │ per-modality noise + v-target │ │ MM-DiT: cross-modal attention │ │ + action tokens conditioning │ │ 100M params, patch_size=2 │ └────────────────────────────────────┘ │ ▼ Loss: SNR-weighted MSE on v_pred vs v_target (aggregated over all 5 ti, all 5 streams, all spatial tokens) ``` **推理时**:给 history latent(前 n_context=Th_lat=3 帧)+ 完整 action 序列 → diffusion sampling 生成 future 2 帧 latent → `_restore_tactiles_mv`(加回 z_ref)→ 可选 Cosmos decoder 解码到 pixel。 ## 5. 关键实现文件 | 组件 | 位置 | |---|---| | Cosmos encoder wrapper(推理侧) | `cosmos_tokenizer.video_lib.CausalVideoTokenizer` | | Cosmos 编码脚本 | `scripts/encode_to_latents_cosmos.py` | | Cosmos decoder 集成 | `scripts/decode_rollout.py` + `scripts/inference_rollout.py`(`--tokenizer cosmos`) | | VTWM 模型 | `model/model.py` (`DiffusionForcingDynamics`) | | 数据加载 + p01 ref | `data/dataset.py` (`WindowVideoLatentDatasetMV`) | | MM-DiT 主干 | `model/udit_video.py` | | 训练循环 | `train/train_dynamics.py` | | Cosmos JIT ckpts | `/n/netscratch/ydu_lab/Lab/yxma/cosmos_ckpts/CV4x8x8/{encoder,decoder}.jit` | ## 6. 已知问题 / 潜在改进 ### 🟡 中等风险(应该修) **#1 `data_mean=0, data_std=1` 没针对 Cosmos 分布调** Audit(`scripts/_audit_meanstd_sbatch.sh` 07-03)实测: | Stream | Global mean | Global std | |---|---|---| | visual_* | +0.03 ~ +0.09 | 0.73 ~ 0.78 | | tactile_* | +0.14 ~ +0.15 | 0.76 ~ 0.78 | Diffusion noise schedule 假设 x_0 ~ N(0, 1)。我们的实际数据 mean 偏离 0 达 0.15, std 只有 0.76。**效果**:噪声在 mid-SNR 时段偏"喧宾夺主",训练效率略低。 **建议改**:config 里改成 `data_mean: 0.1, data_std: 0.77`(全局平均), 或者做 per-channel whitening(需要额外 dataset preprocessing)。 **#2 `_episode_p01_latent_ref` 均匀 factor=3 映射**(`data/dataset.py:120-123`) Cosmos ti 语义**不均匀**(ti=0 = 1 raw 帧,ti=1..4 = 4 raw 帧一组),但代码里 `factor = round(window_size / T_lat) = round(16/5) = 3` 是均匀的。 **实际影响**:wi (window index) 总是正确;ti 在 `ref_p01_idx` 恰好在 window 开头时精确 (ti=0),其它情况偶尔差 1(audit 07-03 显示 6 个 episode/side 中 2 处差 1)。 **建议改**:在 dataset 里加 Cosmos-aware 映射:`ti = 0 if delta==0 else min(T_lat-1, ((delta-1)//4)+1)`。 不严重,可以先不改。 **#3 `tactile_delta_to_ref` 对 anchor 和 group 一视同仁** 模型对所有 5 帧 latent 减同一个 `z_ref`。但 z_ref 通常是 ti=0(anchor)编码—— 单帧图片的 latent,跟 ti=1..4 (4 帧融合) 语义不同。 理论上不严重(因为 Cosmos 因果性保证 background 输入下 ti=0..4 数值几乎相同,Test C 已证), 但在有渐变的 window 上可能引入不必要的 offset。 **建议改**:考虑对 ti=0 用单独的 loss weight,或者训练时在 ti≥1 上应用 delta,ti=0 单独处理。 或者最简单:训练时**只用 ti=1..4**(4 帧 group latent,跟 VideoVAE+ shape 对齐)。 ### 🟢 低风险 / 结构性但已知 **#4 Cosmos zero-shot on tactile** 我们没 finetune Cosmos on GelSight tactile 数据。但 audit 显示 zero-shot 已经比 VideoVAE+ 的 tactile-finetune 版**重构 PSNR 高 6-8 dB**(Test 07)。 不修理由:finetune 有 forgetting risk + 提升边际不明。 **#5 T_lat=5 里 ti=0 只覆盖 1 raw 帧** 数据"密度"不对称:4/5 的 latent 帧覆盖 4 raw,1/5 只覆盖 1 raw。 - 优点:能天然做 T=1 图片编码;能任意长视频拼接 - 缺点:训练时 4/5 的 latent 承担 80% 的信息,anchor 帧只是"起点标签" **目前观察**:VTWM 训练成功收敛,val loss 稳定,没有明显发散。留着不改。 **#6 window=16 vs canonical=17** Cosmos 内部把 T=16 pad 成 17。轻微冗余但不影响正确性。 **建议改**:改用 canonical T=17 编码,去掉内部 pad。需要重新编码全部 32 episodes。 ### 🔴 高风险 —— 目前**尚未**发现,但值得留意 **#7 长-horizon AR 累积误差** 3 sample × 40s AR rollout 上,view PSNR 从 short 29 → long 21(掉 8 dB), tactile 从 42 → 38(掉 4 dB)。**AR 累积误差存在**,且 view 比 tactile 严重。 需要看 tactile 稳定性是否维持在更长时段。 **#8 Per-channel 分布不平衡** Audit 显示 tactile latent 里 `ch6` mean = +1.65,`ch13` = −0.86,`ch14` std = 0.80,`ch10` std = 0.44。 Channel 间统计差异大。Model 能学 compensate,但可能浪费容量。 **建议改**:per-channel whitening(存 mean/std vector,训练时 normalize)。 ## 7. 我们**已经**修过的 bug(供参考) - `tactile_ref_no_contact_threshold=0.01` → **0.05**(Cosmos 实测阈值应跟 VideoVAE+ 一致, audit 07-03 后修正,见 `train_mv1_v2_stride2_p01rand_cosmos.yaml`) - `tactile_event_abs_threshold=0.01` → **0.05**(同上) ## 8. 未测试但可以做 - **p01rand 真实版本**(threshold=0.05 修好后重跑)——验证 p01 vs p01rand 是否有实际差异 - **Long-horizon 60s+**——AR 累积误差是否发散 - **spatial mask 右下角 hotspot**(虽然 Cosmos 上比 VideoVAE+ 轻很多,Test A 仍显示 gray-192 argmax=(15,15) max/mean=1.6×,可能值得 mask) - **Cosmos DV(discrete)版本**——把 latent 变成离散 token 后能不能用 AR transformer 而非 diffusion ## 9. 关键对比数字(Cosmos vs VideoVAE+,同 recipe 短-horizon rollout) || Cosmos p01 | VideoVAE+ p01 | Δ | |---|---|---|---| | view LPIPS ↓ | 0.045 | 0.059 | **−24.5%** ✅ | | view PSNR ↑ | 28.89 | 25.58 | **+3.31 dB** ✅ | | tl PSNR ↑ | 41.63 | 40.63 | **+1.00 dB** ✅ | | tr PSNR ↑ | 42.66 | 39.15 | **+3.51 dB** ✅ | | tr LPIPS ↓ | 0.060 | 0.101 | **−41.2%** ✅ | | **9/9 metrics** | | | **Cosmos 全部胜出** | ## 10. 引用 - Cosmos Tokenizer: https://github.com/NVIDIA/Cosmos-Tokenizer - HF ckpts: https://huggingface.co/nvidia/Cosmos-0.1-Tokenizer-CV4x8x8 - 本轮 audit + rollout:https://huggingface.co/datasets/yxma/vtwm-tactile-latent-delta-viz - 短 rollout HF Space: https://huggingface.co/spaces/yxma/vtwm-cosmos-rollouts - 长 rollout HF Space: https://huggingface.co/spaces/yxma/vtwm-cosmos-long-40s