Skip to content

feat(entity): publish mjlab-aligned site pose and velocity on EntityData - #2674

Merged
TATP-233 merged 1 commit into
mainfrom
feat/issue-2673-entity-site-state
Dec 2, 2026
Merged

TATP-233 merged 1 commit into
mainfrom
feat/issue-2673-entity-site-state

Conversation

@TATP-233

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

Copy link
Copy Markdown
Collaborator

Summary

  • EntityData publishes mjlab-aligned site state for entities that declare site_names: site_pos_w, site_quat_w (wxyz), site_lin_vel_w, site_ang_vel_w, each (num_envs, num_sites, 3|4) in the declared site order, world frame, linear velocity taken at the site origin. Entities without sites return (num_envs, 0, ...); sites owned by the static world body keep a constant pose and zero velocity.
  • The public SimBackend has no site-state read (only get_site_ids, get_site_jacobian_w, get_ray_site_frames), so the scene tensor read plan derives every site from its owning body's tracked link state and the local frame from SimBackend.get_ray_site_frames. Tracked body velocity is the link-origin velocity on both MuJoCo and MJWarp (mjOBJ_XBODY frame sensors), so the rigid transfer is exact and final-state fresh on both backends:
    p_site = p_body + R(q_body) @ site_pos, q_site = q_body * site_quat, v_site = v_body + w_body x (p_site - p_body), w_site = w_body.
    This is the same recipe downstream RobotView._sites() repeats per task; it now lives in the owner layer. No unisim change.
  • Plumbing: SceneTensorReadSpec.site_names requests the publish, SceneTensorReadPlan.site_tensor_view() returns the phase view, EntityData.site_frames(device) exposes the cold local frames (EntitySiteFrames), and Manager environments publish every declared site automatically. Owning bodies join the packed packet even when they are not in EntityCfg.body_names.
  • Lifecycle: the per-entity carrier is allocated once and written in place every phase, so masked-autoreset CUDA graph capture records fixed addresses (covered by an MJWarp graph-replay test). Before a read plan is compiled the carrier is zero-valued, which lets plain function observation terms read data.site_pos_w during ObservationManager's construction-time shape probe (the same pre-bind convention as the motion-tracking bind_read_phase terms); after compilation an unrefreshed phase fails closed.
  • Behavior change: declaring site_names keeps the full named-body tracking request on MuJoCo and MJWarp (owning bodies resolve only after backend materialization, so the derived selection cannot know them). An explicit SceneCfg.tracked_body_names that omits an owning body fails closed at read-plan compilation and names the body. A site request also opts that plan out of the pose-only host body blocks, because sites need velocities.
  • Backends without get_ray_site_frames keep constructing with declared site_names; reading site_*_w then raises NotImplementedError.

Linked Work

Validation

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

Commands actually run (worktree synced with uv sync --extra mujoco --extra mjwarp plus unilab-pull-assets --robot all; RTX 4090, CUDA 13.0 torch, mujoco 4.0.1, unisim-core 2.9.0):

make check
# ruff format/check clean; mypy: no issues in 212 source files; pyright: 0 errors, 1 warning
# (pre-existing reportUnsupportedDunderAll in src/unilab/base/__init__.py)

uv run --no-sync pytest tests/base/test_entity_site_state.py -q -p no:cacheprovider
# 17 passed — CPU MuJoCo parity (<=1e-5), empty-site tensors, declared order / subset
# selection, static sites, cold-probe then fail-closed, untracked owning body, env e2e
# (init/step/reset), declared-sites tracking contract, incapable backend

uv run --no-sync pytest tests/base/test_mjwarp_site_state.py -q -p no:cacheprovider -m slow
# 3 passed — MJWarp parity vs raw MuJoCo and vs the CPU backend (<=1e-5), no host transfer
# during refresh/reads (torch.Tensor.cpu/.item patched to raise), stable carrier across
# invalidate/refresh, untracked owning body fails closed, autoreset CUDA graph capture +
# >=30 replays with masked resets, site state still matching raw MuJoCo

uv run --no-sync pytest tests/base/test_entity_facade.py tests/base/test_entity_tensor_read_plan.py \
  tests/base/test_entity_scene_consumer.py tests/base/test_entity_reset_staging.py \
  tests/base/test_sim_backend_smoke.py tests/base/test_backend_conformance.py \
  tests/envs/test_manager_based_rl_env.py tests/envs/mdp/test_joint_position_action.py \
  tests/architecture tests/tasks/test_package_boundary.py tests/managers/test_device_contracts.py \
  tests/envs/test_fixed_shape_autoreset.py tests/base/test_mjwarp_selective_tracking.py -q
# 331 passed, 21 skipped

make test-all
# 3009 passed, 64 skipped, 383 deselected in 125.38s; coverage TOTAL 78%;
# benchmark smoke [module-mode] 35/35, [script-mode] 36/36; EXIT=0

uv run --no-sync pytest tests/scripts/test_check_docs.py -q
# 20 passed
uv run --no-sync interrogate src/unilab
# RESULT: PASSED (minimum: 40.0%, actual: 49.6%)
(cd docs/sphinx && UNILAB_DOCS_SKIP_AUTODOC=1 uv run --no-project --with-requirements \
  requirements.txt sphinx-build -b html -n source <tmp>)
# build succeeded, no warnings

Measured parity (max abs error, declared tolerance 1e-5):

Path pos_w quat_w lin_vel_w ang_vel_w
CPU MuJoCo backend vs mj_kinematics + mj_comPos/mj_comVel + mj_objectVelocity(SITE) 3.9e-08 3.4e-07 1.0e-07 1.0e-07
MJWarp vs the same raw reference 7.5e-08 2.9e-07 2.5e-07 3.9e-07
Sharpa Wave contact_*_tip vs raw MuJoCo (downstream scenario) 6.7e-07 1.4e-07 2.4e-07 1.3e-06
Sharpa Wave contact_*_tip vs "body pose + local offset" hand calculation 0.0 0.0 0.0 0.0

Downstream scenario (issue acceptance): a local script reproduced FlashDexRetarget's robot_spec() — assets/robot/sharpa/right.xml with the native sites from bimanual.xml injected by body (28 bodies, 35 sites including the five contact_right_*_tip sites on right_*_DP) — on the CPU MuJoCo backend with random joint state, and compared EntityData.site_*_w against raw MuJoCo and against the task-side body-pose-plus-offset recipe. The asset is private to that repo, so the check is a one-off script rather than an in-tree test; the in-tree fixture tests/fixtures/site_frames.xml covers the same shape (COM offset from the link origin, non-trivial local rotations, static sites).

Pre-existing and unrelated: tests/base/test_mjwarp_backend.py (slow, not part of make test-all) has 6 failures on main at db6cceeb from removed 1.x NumPy APIs (set_state, get_body_pos_w); this PR does not touch it.

Remote CI route:

Impact

  • Backend impact: mujoco and mjwarp (both validated); other backends are unchanged and fail closed on site reads
  • Platform impact: Linux (CPU path is platform-independent; MJWarp path is CUDA-only)
  • Training effect expected: no — no in-tree task declares site_names, existing reads are unchanged; the only cost is for tasks that opt into site_names (all named bodies stay tracked, and the plan carries the owning bodies' four velocity/pose sensors)

Artifacts

  • W&B: n/a
  • benchmark result: make test-all benchmark smoke only (35/35 modules, 36/36 scripts)
  • video / screenshot: n/a
  • ONNX / checkpoint: n/a

Checklist

EntityData gains site_pos_w, site_quat_w (wxyz), site_lin_vel_w, and
site_ang_vel_w for entities that declare site_names: (num_envs, num_sites,
3|4) in the declared site order, world frame, linear velocity at the site
origin. The public SimBackend has no site-state read, so the scene tensor
read plan transfers each owning body's tracked link state (link-origin
velocity on MuJoCo and MJWarp) through the local frame from
SimBackend.get_ray_site_frames:

  p_site = p_body + R(q_body) @ site_pos
  q_site = q_body * site_quat
  v_site = v_body + w_body x (p_site - p_body)
  w_site = w_body

The per-entity carrier is written in place every phase, so masked-autoreset
CUDA graph capture records fixed addresses. Before a read plan is compiled
the carrier is zero-valued for ObservationManager's construction-time shape
probe; afterwards an unrefreshed phase fails closed. Entities without sites
return empty tensors, static world-owned sites keep a constant frame, and
SceneTensorReadSpec.site_names / SceneTensorReadPlan.site_tensor_view expose
the phase view. Declaring site_names keeps the full named-body tracking
request on MuJoCo and MJWarp because owning bodies resolve only after backend
materialization; an explicit SceneCfg.tracked_body_names that omits one fails
closed at read-plan compilation.

Closes #2673
@TATP-233
TATP-233 merged commit 4c49d4b into main Dec 2, 2026
12 checks passed
@TATP-233
TATP-233 deleted the feat/issue-2673-entity-site-state branch December 2, 2026 13:57
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.

1 participant