code-HFD_lzk / codex /codex.txt
Tinghong-Ye22's picture
Add files using upload-large-folder tool
55e0599 verified
Raw History Blame Contribute Delete
11.1 kB
【本次目标】
你要求我直接改代码,并且做到:
1) 能加载你当前 posttrain_v1 训练出来的 ckpt 做推理。
2) 当前是 delta 模式,输出两版结果:
- delta 版 CSV
- 还原后(绝对姿态)版 CSV
3) 尽量最小改动,并且跑到有实际输出结果。
【我实际改了哪些文件】
1) 修改:`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/src/psi/models/psi0.py`
2) 新增:`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/scripts/infer/seed_text_to_motion.py`
【详细改动说明】
====================
A. `psi0.py` 改动
====================
A1) 新增 Omni 相关 import + 默认 system prompt 常量
- 位置:`psi0.py` 顶部 import 区
- 新增:
- `Qwen2_5OmniThinkerForConditionalGeneration`
- `Qwen2_5OmniProcessor`
- 新增常量:`OMNI_DEFAULT_SYSTEM_PROMPT`(用于 text-only 时避免 Omni 警告刷屏)
A2) `from_pretrained(...)` 改成兼容 Omni + 分片 ckpt
- 位置:`psi0.py` 中 `Psi0Model.from_pretrained`
- 关键改动:
1. 原来只认 `model.safetensors`,现在支持两种:
- 单文件:`model.safetensors`
- 分片:`model.safetensors.index.json` + `model-0000x-of-0000y.safetensors`
2. 根据 `launch_config.model.model_name_or_path` 自动判断是否 Omni:
- Omni 用 `Qwen2_5OmniThinkerForConditionalGeneration.from_pretrained(...)`
- 非 Omni 保留 Qwen3 路径
3. attention 实现按设备自动选择:
- CUDA: `flash_attention_2`
- CPU: `sdpa`
4. `lm_head.weight` 改成鲁棒回填:
- 如果 ckpt 已有 `lm_head.weight` 就直接用
- 没有才从 `embed_tokens.weight` 补
5. processor 按模型分支加载:
- Omni: `Qwen2_5OmniProcessor.from_pretrained(...)`
- 非 Omni: `AutoProcessor.from_pretrained(...)`
6. 把配置挂到模型实例:
- `model.text_only`
- `model.use_delta_actions`
A3) `predict_action(...)` 增加 text-only 推理分支
- 位置:`psi0.py` 中 `Psi0Model.predict_action`
- 关键改动:
1. `observations` 改为可选(`Optional`)
2. 如果 `model.text_only=True`:
- 不走 `process_vision_info`
- 仅用文本构造输入(带默认 system prompt)
- 不再依赖 `pixel_values/image_grid_thw`
3. 如果不是 text-only,保持原视觉分支
4. autocast 改成设备自适应:
- CUDA 才用 `torch.autocast("cuda")`
- CPU 用 `nullcontext`
5. 修正 dtype:`vlm_hidden_states` 对齐到 `states.dtype`,避免 `bf16 vs fp32` 报错
A4) `predict_action_with_training_rtc_flow(...)` 同步做了同样的 text-only/设备兼容
- 位置:`psi0.py` 中 `Psi0Model.predict_action_with_training_rtc_flow`
- 目标是保持 RTC 推理链路也能走 text-only(最小同步改动)
==========================================
B. 新增离线推理导出脚本 `seed_text_to_motion.py`
==========================================
文件:`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/scripts/infer/seed_text_to_motion.py`
功能:
1) 输入:`run_dir + ckpt_step + metadata`,按文本做离线推理。
2) 自动读取训练配置(`argv.txt` + `run_config.json`)。
3) 自动支持 `--ckpt-step latest`。
4) 输出每个样本两套结果:
- delta 序列
- restored(按累加还原)序列
5) 同时输出:
- `.npy`
- 三个 csv:`joint_pos / body_pos / body_quat`
脚本关键点:
- 文本构造逻辑和 dataset 保持一致(content_* 字段拼接)。
- restored 计算:
- `x[t] = x0 + sum_{i<=t}(delta[i])`
- `x0` 默认取对应样本真实首帧(可关)
- 对 restored 的四元数做了逐帧归一化。
【我已完成的实测(已真正产出文件)】
我跑通了 1 条样本离线导出(CPU smoke)。
运行命令:
```bash
cd /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth
source .venv-psi/bin/activate
python scripts/infer/seed_text_to_motion.py \
--run-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/envs/.psi-home/runs/posttrain_v1/posttrain.sg.flow1000.consta.lr1.0e-04.b64.gpus1.2604152353 \
--ckpt-step latest \
--output-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/codex/seed_infer_smoke_cpu \
--num-samples 1 \
--start-index 0 \
--num-inference-steps 1 \
--device cpu
```
成功输出目录:
`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/codex/seed_infer_smoke_cpu/sample_0000`
生成文件(两版都在):
- `motion_delta.npy`
- `motion_restored.npy`
- `joint_pos_delta.csv`
- `body_pos_delta.csv`
- `body_quat_delta.csv`
- `joint_pos_restored.csv`
- `body_pos_restored.csv`
- `body_quat_restored.csv`
- `meta.json`
- 上层还有 `manifest.json`
【为什么用 CPU smoke】
你当前机器上的 GPU 正在高负载训练(我检查到 GPU 占用 100%,显存占用很高),直接 CUDA 推理会报 `device busy/unavailable`。所以我用 CPU 完成了“端到端可出结果”的验证。
【你现在直接可用的正式命令(GPU)】
当 GPU 空出来后,直接跑:
```bash
cd /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth
source .venv-psi/bin/activate
export CUDA_VISIBLE_DEVICES=0
python scripts/infer/seed_text_to_motion.py \
--run-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/envs/.psi-home/runs/posttrain_v1/posttrain.sg.flow1000.consta.lr1.0e-04.b64.gpus1.2604152353 \
--ckpt-step latest \
--output-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/envs/.psi-home/runs/posttrain_v1/infer_export \
--num-samples 100 \
--start-index 0 \
--num-inference-steps 10 \
--device cuda:0
```
【输出语义说明(你要的两版)】
1) delta 版(`*_delta.csv`)
- 含义:每一步预测的 36 维增量。
- 与你当前训练配置 `use_delta_actions=true` 一致。
2) restored 版(`*_restored.csv`)
- 含义:把 delta 序列累计还原后的绝对姿态序列。
- 还原公式:
- `x0` 取对应样本真实首帧(默认)
- `x[t] = x0 + cumsum(delta)[t]`
- 已做四元数归一化(最后4维)。
【补充】
你之前给的 `ckpt_9100` 路径在当前 run 下已经不存在(训练在继续并清理旧 ckpt),脚本支持 `latest` 自动解析当前最大 ckpt。
====================
【追加更新(按你这条新需求)】
====================
你这次提的 4 个点我已落实:
1) CSV 列名对齐 `csv_sonic` 现有格式。
2) “正常输出”不再带 restored 后缀,直接标准文件名。
3) delta 输出改为可选,默认不输出。
4) 新增三项速度 CSV,计算逻辑与
`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/GR00T-WholeBodyControl/gear_sonic/scripts/convert_seed_to_reference_motion.py`
一致(其底层函数来自 `convert_gmr_pkl_to_reference_motion.py`)。
【本轮实际改动文件】
- 修改:`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/scripts/infer/seed_text_to_motion.py`
说明:这轮没有再扩大改动推理主干(`psi0.py` 没有新增改动),仅在导出脚本层实现你要的格式和速度计算。
【输出目录结构与命名(已改)】
现在每个样本输出目录会复用 seed 原始层级,形如:
`<output_dir>/g1/csv_sonic/<日期>/<动作名>/`
默认输出(不带 delta 后缀,与你给的示例一致):
- `joint_pos.csv`(29列,列名 `joint_0..joint_28`)
- `joint_vel.csv`(29列,列名 `joint_vel_0..joint_vel_28`)
- `body_pos.csv`(3列,`body_0_x/body_0_y/body_0_z`)
- `body_quat.csv`(4列,`body_0_w/body_0_x/body_0_y/body_0_z`)
- `body_lin_vel.csv`(3列,`body_0_vel_x/body_0_vel_y/body_0_vel_z`)
- `body_ang_vel.csv`(3列,`body_0_angvel_x/body_0_angvel_y/body_0_angvel_z`)
- `motion.npy`
- `metadata.txt`
- `info.txt`
并且如果源目录存在:
- `info_source.txt`(复制自源 `info.txt`)
- `metadata_source.txt`(复制自源 `metadata.txt`)
【delta 输出开关(默认关闭)】
- 默认不输出 delta 文件。
- 传 `--export-delta` 时才输出:
- `joint_pos_delta.csv`
- `body_pos_delta.csv`
- `body_quat_delta.csv`
- `motion_delta.npy`
【速度计算与外部脚本一致性】
已按外部脚本同逻辑实现:
- `joint_vel = finite_difference(joint_pos, dt)`
- `body_lin_vel = finite_difference(body_pos, dt)`
- `body_ang_vel = quaternion_angular_velocity(body_quat, dt)`
- `dt = 1/50`
其中:
- `finite_difference` 使用 `np.gradient(..., edge_order=2 if T>2 else 1)`
- `quaternion_angular_velocity` 使用相邻帧四元数差:
- `dq = q_{t+1} * conj(q_t)`
- `angvel_t = rotvec(dq) / dt`
- 最后一帧速度 `angvel[-1] = angvel[-2]`
这部分与 `convert_gmr_pkl_to_reference_motion.py` 对齐实现。
【我已做的 CUDA 实测结果】
已在 GPU 上实际跑通(不是 CPU),命令:
```bash
cd /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth
source .venv-psi/bin/activate
export CUDA_VISIBLE_DEVICES=0
python scripts/infer/seed_text_to_motion.py \
--run-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/envs/.psi-home/runs/posttrain_v1/posttrain.sg.flow1000.consta.lr1.0e-04.b64.gpus1.2604152353 \
--ckpt-step latest \
--output-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/codex/seed_infer_smoke_gpu \
--num-samples 1 \
--start-index 0 \
--num-inference-steps 2 \
--device cuda:0
```
成功样例目录:
`/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth/codex/seed_infer_smoke_gpu/g1/csv_sonic/230125/idle_crouch_loop_001__A147`
并验证列名与 6 个主 CSV 文件均符合 `csv_sonic` 风格。
【你现在正式批量跑的命令】
(默认不导出 delta)
```bash
cd /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0-yth
source .venv-psi/bin/activate
export CUDA_VISIBLE_DEVICES=0
python scripts/infer/seed_text_to_motion.py \
--run-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/envs/.psi-home/runs/posttrain_v1/posttrain.sg.flow1000.consta.lr1.0e-04.b64.gpus1.2604152353 \
--ckpt-step latest \
--output-dir /cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/envs/.psi-home/runs/posttrain_v1/infer_export \
--num-samples 100 \
--start-index 0 \
--num-inference-steps 10 \
--device cuda:0
```
需要 delta 时,加:
`--export-delta`