Skip to content

fix(entity): resolve authored site tracking - #2678

Merged
TATP-233 merged 2 commits into
mainfrom
fix/issue-2675-static-site-variants
Dec 3, 2026
Merged

TATP-233 merged 2 commits into
mainfrom
fix/issue-2675-static-site-variants

Conversation

@TATP-233

@TATP-233 TATP-233 commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Cold-compile authored MJCF/MJB sources to resolve dynamic site-owning bodies before backend construction. MuJoCo and MJWarp now track only root, explicitly declared, and dynamic site-parent bodies instead of widening every site_names declaration to all named bodies; unresolved sources retain the documented full-tracking fallback.
  • Fail closed when scene or physical-entity fixed variants disagree on any static or dynamic site's site_bodyid, site_pos, or site_quat. The error names the variant, site, and changed fields instead of publishing one canonical pose to every environment.
  • Cover the public create_backend repro path, dynamic site mismatches, physical-entity variants, selective tracking scope, and MJWarp acceptance; update the bilingual env contract and changelog.

Linked Work

Validation

  • make test-all passed on the final local head before this PR was created or updated
  • Additional task-specific validation listed below

Commands actually run:

make test-all
# 3041 passed, 42 skipped, 384 deselected, 1 xfailed
# mypy: no issues in 212 source files; pyright: 0 errors, 1 pre-existing warning
# benchmark smoke: module-mode 35/35, script-mode 36/36

uv run --no-sync pytest tests/base/test_entity_site_state.py \
  tests/base/test_entity_scene_consumer.py -q -p no:cacheprovider
# 29 passed

PYTHONPATH=src:third-party/unisim/src uv run --no-sync pytest \
  tests/base/test_entity_site_state.py -q -p no:cacheprovider \
  -k 'fixed_variant or entity_variant'
# 3 passed against UniSim fc81ac08, including dynamic and physical-entity variant repros

uv run --no-sync pytest tests/base/test_mjwarp_site_state.py -q -p no:cacheprovider -m slow
# 4 passed on RTX 4090, including fixed-variant validation and CUDA-graph site-state replay

uv run --no-sync pytest tests/scripts/test_check_docs.py -q -p no:cacheprovider
# 20 passed

(cd docs/sphinx && UNILAB_DOCS_SKIP_AUTODOC=1 \
  uv run --no-project --with-requirements requirements.txt \
  sphinx-build -b html -n source build/issue-2675-review-html)
# build succeeded; temporary output removed

Remote CI route:

  • Base main: CI run 47033459458 and Docs run 47033459627 are running on 95878833. Sphinx, ruff, mypy, pyright, benchmark-smoke, and RL checks have passed; application/core/environment checks remain pending.

Impact

  • Backend impact: mujoco and mjwarp; other backends are unchanged
  • Platform impact: Linux (CPU MuJoCo and CUDA MJWarp validated); no macOS-specific behavior changed
  • Training effect expected: no in-tree task declares site_names; downstream tasks that do avoid unrelated body tracking. Scene and physical-entity fixed variants with incompatible site frames now fail explicitly instead of returning incorrect poses
  • Scope boundary: composed or unavailable authored sources retain full body tracking. Per-world site-frame publication is not added; this PR implements the issue's fail-closed alternative

Artifacts

  • W&B: n/a
  • benchmark result: make test-all benchmark smoke only; no performance claim
  • video / screenshot: n/a
  • ONNX / checkpoint: n/a

Checklist

  • Added or updated tests where needed
  • Updated docs if behavior or workflow changed
  • Linked the driving issue
  • Noted any follow-up work explicitly

@TATP-233 TATP-233 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

结论

一处需要在合并前补上;其余部分我在 PR 代码(48725430)上独立验证通过。

合并前要补:报错只覆盖挂在 world body 上的静态 site

resolve_authored_site_frames 只在 site 属于 world 时比较各 variant 的框架,其它 site 直接跳过。但 EntityData 发布所有 site 用的是同一个冷路径读到的 canonical 本地框架(_publish_site_states 的 frames.pos / frames.quat),动态 site 也是如此;而 unilabsim/unisim#592 放开的是所有 site 的 site_pos / site_quat。所以动态 site 的本地框架在 variant 间不同时,#2675 的问题原样保留。

实测(本 PR 加 unisim#592 fc81ac08,两个只在动态 site tip 的 pos 上不同的 variant,4 个 env 各一半,tests/fixtures/site_frames.xml 改两行):

tip 的世界坐标
env 0/1(variant 0),期望 (0.52, -0.03, 1.3)
env 2/3(variant 1),按独立编译的模型应为 (0.46, 0.05, 1.2)
env 2/3,实际发布 (0.52, -0.03, 1.3)

差 10 cm,构建不报错。

建议

  • 去掉 scene.py:172 的 world 限定,对所有 site 比较 body_name、pos、quat(含 site_bodyid 在 variant 间变化的情况),报错信息里的 "world-owned static site frames" 改成 "site frames"。理由和现在写的一样:get_ray_site_frames 只暴露一个 canonical 本地框架。
  • validate_fixed_variant_site_frames 只在 fixed_variant_plan 存在时运行,scene.entity_variant 路径没有校验。这条我没有单独复现,请确认该路径是否会走到 _publish_site_states。
  • 测试补一条:动态 site 的本地框架在 variant 间不同时应当报错。

FlashDexRetarget 不受影响:它的机器人在各 variant 间相同,站点框架一致。

验证通过的部分

  • tests/base/test_entity_site_state.py、test_mjwarp_site_state.py、test_fixed_model_variants.py 共 30 个通过,分别搭配 unisim-core 2.9.0 和 unisim#592 fc81ac08(RTX 4090,mujoco 4.0.1,mujoco-warp 4.0.1)。
  • #2675 的静态 site 复现现在报 ValueError: Fixed variants must keep world-owned static site frames identical ... variant 1 (...) changes static site 'marker' (site_pos),variant、源文件、site 和字段都在信息里。
  • 冷编译一个真实 H2R 场景(TACO seq_121,91 个网格)需要 0.07 s,提前解析父 body 的启动开销可以忽略。
复现脚本:动态 site 的本地框架在 variant 间不同

在仓库根目录运行,PYTHONPATH=src:<unisim#592>/src:

import pathlib, tempfile, numpy as np, mujoco
from unilab.base.backend_factory import create_backend
from unilab.base.entity import EntityCfg, EntityScene, SceneTensorReadSpec
from unilab.base.scene import SceneCfg
from unisim.dr.types import FixedVariantLayout, FixedVariantPlan, ModelSourceDescriptor

source = pathlib.Path("tests/fixtures/site_frames.xml").read_text()
sites = ("palm", "elbow", "tip")
with tempfile.TemporaryDirectory() as tmp:
    tmp = pathlib.Path(tmp)
    (tmp / "v0.xml").write_text(source)
    (tmp / "v1.xml").write_text(source.replace('<site name="tip" pos="0.02 -0.03 0.2"', '<site name="tip" pos="-0.04 0.05 0.1"'))
    truth = {}
    for k in ("v0", "v1"):
        m = mujoco.MjModel.from_xml_path(str(tmp / f"{k}.xml")); d = mujoco.MjData(m); mujoco.mj_kinematics(m, d)
        truth[k] = np.round(d.site_xpos[m.site("tip").id], 3).tolist()
    print("raw MuJoCo tip world pose at qpos0:", truth)
    cfg = SceneCfg(model_file=str(tmp / "v0.xml"), fixed_variant_plan=FixedVariantPlan(assignment=np.array([0, 0, 1, 1], dtype=np.int32),
        variants=(ModelSourceDescriptor(str(tmp / "v0.xml")), ModelSourceDescriptor(str(tmp / "v1.xml"))), layout=FixedVariantLayout.SAME_LAYOUT),
        entities={"robot": EntityCfg(root_body_name="base", body_names=("base",), site_names=sites)})
    try:
        backend = create_backend("mjwarp", cfg, 4, 0.01, body_state_required=True, tracked_body_names=None, mjwarp_nconmax=32, mjwarp_njmax=64)
    except Exception as e:
        print(f"FAIL CLOSED: {type(e).__name__}: {str(e)[:260]}"); raise SystemExit(0)
    scene = EntityScene.from_scene_cfg(cfg, backend, device="cuda")
    scene.compile_tensor_reads("cuda", (SceneTensorReadSpec(entity="robot", site_names=sites),)).refresh()
    tip = scene["robot"].data.site_pos_w[:, 2].cpu().numpy().astype(float)
    print("published tip pos per env:", np.round(tip, 3).tolist(), "| envs 2/3 should be", truth["v1"])

输出:

raw MuJoCo tip world pose at qpos0: {'v0': [0.52, -0.03, 1.3], 'v1': [0.46, 0.05, 1.2]}
published tip pos per env: [[0.52, -0.03, 1.3], [0.52, -0.03, 1.3], [0.52, -0.03, 1.3], [0.52, -0.03, 1.3]] | envs 2/3 should be [0.46, 0.05, 1.2]

Comment thread src/unilab/base/scene.py Outdated
canonical = resolved[0]
for variant_index, frames in enumerate(resolved[1:], start=1):
for reference, candidate in zip(canonical, frames, strict=True):
if reference.body_name != "world" and candidate.body_name != "world":

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个限定让动态 site 的框架差异被跳过,但 _publish_site_states 对动态 site 同样只用 get_ray_site_frames 返回的 canonical 本地框架,而 unisim#592 放开的是所有 site 的 site_pos / site_quat。实测动态 site tip 的 pos 在 variant 间不同时,variant 1 的 env 发布的是 variant 0 的框架((0.52, -0.03, 1.3),应为 (0.46, 0.05, 1.2)),构建不报错。建议去掉 world 限定,对所有 site 比较 body_name / pos / quat,报错信息同步改成 "site frames"。详见审查正文。

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 95878833 修复:

  • 去掉 world 限定,对所有 site 比较 body_name / pos / quat,动态 site 的本地框架差异也会 fail closed。
  • 确认 entity_variant 同样进入 _publish_site_states,已为该路径加入 authored-source 预校验。
  • 新增动态 tip 与 physical-entity variant 回归;搭配 UniSim fc81ac08 运行时 3 项均通过。

最终本地 make test-all:3041 passed, 42 skipped, 1 xfailed;MJWarp CUDA 4 项通过,Sphinx 构建通过。

@TATP-233
TATP-233 merged commit 98d0764 into main Dec 3, 2026
12 checks passed
@TATP-233
TATP-233 deleted the fix/issue-2675-static-site-variants branch December 3, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Work: [unilab.entity] fixed variants 下静态 site 返回 canonical 位姿;声明 site_names 时避免跟踪全部 body

1 participant