Skip to content

reset 模式 DR 事件无法参与 masked_autoreset:缺少掩码化写入门,且 write_randomization_tensor_mask 每次调用伴随 H2D 同步 #2623

Open

Description

@TATP-233

背景

下游任务 Uni-SuperMimic(boneseed+mjwarp,4096 envs)在 1.5.11 上落地 masked_autoreset(#2605)时,任务自带的 reset 模式 DR 事件(摩擦/CoM 每次-reset 重放)是最大障碍。#2605 的用户文档建议"不支持 reset-randomization 的任务禁用对应 term",但实测这条路对任务语义不成立,最终只能绕道私有接口。以下均为 1.5.11 + unisim-core 2.7.2 实测。

现状(三个缺口)

1. 自带 DR term 均不支持 ResetSelection,也没有掩码化写入门

EventManager.reset_selection_blockers 要求全部 reset 模式 term 支持 ResetSelection,但:

from unilab.base.reset_selection import supports_reset_selection
from unilab.envs.mdp.events import GeomFriction
supports_reset_selection(GeomFriction)  # False

RandomizeRigidBodyCom、randomize_encoder_bias 同样未声明。行索引写入门(write_geom_friction_to_sim / write_body_ipos_to_sim)只接受 env_ids 张量;在 fixed-shape reset 里拿不到行索引(nonzero 是 host 读),全宽写又会触发 outside_rows deferred verdict。

2. "禁用 reset DR term"的文档建议在 mjwarp 上会破坏任务语义

实测(逐行读 mjwarp model 字段验证):后端 reset 会把 reset 行的模型字段恢复默认——带重放时摩擦为 startup 采样的 xi 值(如 1.119),去掉重放事件后同 seed 下摩擦回到默认(0.530),轨迹从第一步就分歧。对以 xi 为特权上下文观测的任务,禁用重放 = 观测说一套、物理是另一套。重放是必需的。

3. 唯一的掩码化入口 write_randomization_tensor_mask 不适合 step 热路径

_materialize_tensor_randomization_default 对非 KP/KD 字段每次调用都执行 torch.as_tensor(np.array(default, ...), device=...)(页锁定 H2D,同步);set_sync_debug_mode("error") 下直接抛错。我们最终绕道 packed composer 的 write_model_field_mask(其 baseline() 有缓存,首次物化后纯设备操作),但这条路需要访问 env._reset_state._require_packed_transaction()、entity._geom_ids 等私有接口,版本耦合脆。

请求

  1. 为常用模型字段提供公开的掩码化写入门,对齐已有的 write_actuator_gains_tensor_mask(如 write_geom_friction_tensor_mask / write_body_ipos_tensor_mask),并让自带 DR term 声明 ResetSelection 支持;
  2. 缓存 _materialize_tensor_randomization_default 的设备默认表(按字段一次性物化);
  3. 文档补充:在恢复模型字段默认值的后端上,reset DR 重放不可禁用。

相关(unisim 侧,可能需要联动)

unisim/backend/mjwarp/backend.py::_apply_tensor_reset_randomization 在非 external-capture 路径每次 reset 显式 torch.cuda.current_stream(device).synchronize()。即 masked_autoreset 不开 autoreset_cuda_graph 时,DR payload 应用处每步仍有显式 stream sync——fixed-shape 与 graph 两个开关的实际收益是绑定的,建议在 unilab 文档里明确,或推动 unisim 消除该同步。

环境

unilab 1.5.11 / unisim-core 2.7.2 / torch 2.14.1+cu / RTX 6000D 与 RTX 4090 均复现。

Activity

  1. TATP-233 commented on Nov 19, 2026

    @TATP-233
    CollaboratorAuthor

    修复进展(全部三个缺口 + 跨仓库 sync)

    排查结论与修复已拆成两个 PR,覆盖本 issue 的全部请求项:

    UniLab 侧 — PR #2630(feat(reset): support ResetSelection in built-in model-field DR terms)

    • 请求 1(掩码化写入门 + ResetSelection 声明):新增公开 Entity.write_geom_friction_tensor_mask / write_joint_armature_tensor_mask / write_body_ipos_tensor_mask(镜像既有 write_actuator_gains_tensor_mask);内置 reset DR term GeomFriction、DofArmature、PdGains、RandomizeRigidBodyCom、RandomizeEncoderBias 现声明 supports_reset_selection 并接受 ResetSelection。g1_motion_tracking/mjwarp 的 reset_selection_blockers() 已实测为空,验收测试(graph 捕获 + 40 步 set_sync_debug_mode("error") 重放)通过。
    • 请求 2(默认表缓存):_materialize_tensor_randomization_default 改为按字段缓存 immutable device 默认表(重声明 packed reset device 时失效);KP/KD 此前每次调用的 H2D 也一并消除。write_randomization_tensor_mask 首次调用后纯设备操作。
    • 附带修复:mask 路径的负值/NaN 检查接入 env 既有 deferred verdict 通道(env.require),无 host 读;RandomizeEncoderBias 每步的 torch.tensor(joint_ids, device=cuda) H2D 改为冷路径缓存。二者都是本 issue 复现条件(sync-debug 下 graph 捕获)的阻断点。
    • Follow-up(changelog 已列):RandomizeRigidBodyMass、RandomizeBodyMassInertia、RandomizePhysicsSceneGravity 三个 term 结构不同,留待后续。

    UniSim 侧 — PR unilabsim/unisim#537(fix(mjwarp): replace per-reset host-blocking stream sync with cross-stream event wait)

    • 跨仓库联动项:backend.py:4781 的 torch.cuda.current_stream().synchronize() 替换为 _torch_execution_stream(device).wait_stream(producer)(先例 _upload_persistent_body_force)。deferred 模式(unilab 默认)下 reset 热路径从此全程无 host 阻塞;external-capture 分支不动。三个 focused 测试锁定"零 synchronize、一次 wait_stream"(旧代码会在首测失败)。

    请求 3(文档)

    • 8-sync_free_autoreset.md Behavior notes 新增 "Reset-mode DR replay cannot be disabled" 条目:在 reset 时恢复模型字段默认值的后端(如 MJWarp)上,禁用重放会让特权观测与物理从第一步分叉。

    验证

    • UniLab:聚焦测试 275 + 26 + 324 通过;slow CUDA 验收测试通过;make check、make test-all 全绿(最终 head)。
    • UniSim:make lint/make typecheck/make test 全过;真实 CUDA 全量 mjwarp 测试通过(20 个 superdex/viser 失败经 stash 对照确认是先于补丁的存量环境问题)。

    两个 PR 合并后,本 issue 的全部请求项即闭环(follow-up 三个 term 除外)。

  2. added a commit that references this issue on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions