From 9f4b0a503088e189b2f69f37f69a708d34275696 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 12:51:34 +0800 Subject: [PATCH 01/13] feat(sim): adapt DexSim 0.6.0rc1 integration Update Newton solver, contact query, USD loading and rendering adapters for the Python 3.12 DexSim release. Align regression coverage and project context with the new contracts. --- .../topics/sensor-system/contact-history.md | 40 +++-- .../topics/sensor-system/lifecycle.md | 26 ++- .../topics/sensor-system/sensor-system.md | 2 +- .../physics-configuration.md | 23 ++- .../scene_engine/pipeline/utils/scene_usd.py | 6 +- embodichain/lab/sim/cfg/physics.py | 124 ++++++------- embodichain/lab/sim/cfg/rigid.py | 19 ++ embodichain/lab/sim/cfg/robot.py | 13 +- embodichain/lab/sim/physics/newton.py | 32 +++- .../lab/sim/sensors/contact/_warp/history.py | 48 ++++-- .../lab/sim/sensors/contact/_warp/scatter.py | 37 +++- .../lab/sim/sensors/contact/history.py | 43 ++++- embodichain/lab/sim/sensors/contact/sensor.py | 163 +++++++++++++----- embodichain/lab/sim/sim_manager.py | 21 ++- embodichain/lab/sim/spawn/descriptors.py | 2 +- embodichain/lab/sim/spawn/source.py | 17 ++ .../rl/experimental/newton/planar_reach.py | 4 +- .../embodichain_robots/brainco/revo1/cfg.py | 4 +- pyproject.toml | 2 +- tests/gen_sim/scene_engine/test_scene_usd.py | 20 +++ .../task_program/test_contact_evidence.py | 2 + .../test_simulation_scene_expansion.py | 2 +- tests/sim/atomic_actions/test_actions.py | 3 +- tests/sim/objects/test_deformable_object.py | 4 +- tests/sim/objects/test_usd.py | 8 +- tests/sim/robots/test_brainco_revo1.py | 9 +- tests/sim/sensors/test_contact_history.py | 61 +++++++ .../sim/sensors/test_contact_query_sensor.py | 144 ++++++++++++++-- .../sim/sensors/test_contact_sensor_graph.py | 144 ++++------------ tests/sim/sensors/test_support_observation.py | 2 +- .../test_support_observation_native.py | 4 +- tests/sim/spawn/test_descriptors.py | 37 +++- tests/sim/spawn/test_source.py | 56 +++++- tests/sim/test_backend_parity.py | 2 +- tests/sim/test_cfg.py | 6 +- tests/sim/test_sim_manager_cfg.py | 143 ++++++++++++--- 36 files changed, 924 insertions(+), 349 deletions(-) diff --git a/agent_context/topics/sensor-system/contact-history.md b/agent_context/topics/sensor-system/contact-history.md index 94426876ca..652d19f37d 100644 --- a/agent_context/topics/sensor-system/contact-history.md +++ b/agent_context/topics/sensor-system/contact-history.md @@ -21,27 +21,39 @@ boundary. Observation reads reuse the sampled sensor buffer. `SimulationManager.physics_step_index` and the query's current actor binding. Reading a raw sample before its history callback does not consume the history update. Query metadata is checked even on cache hits so topology rebuilds cannot -retain old contacts. Cached CUDA reads wait for the producing stream. Caller-owned -CUDA graph capture bypasses raw/history deduplication and records the query read -and reductions on the capture stream. Capture invalidates eager cache keys rather -than marking unexecuted work as a completed sample; replay does not execute Python -cache bookkeeping. Callers own replay scheduling and establish stream dependencies before capture -and replay. Capture skips eager producer waits and never records its stream as -a completed producer, including control-interval initialization and reset. +retain old contacts. Cached CUDA reads wait for the producing stream. +`update()` and `update_physics_step()` require eager calls outside caller-owned +CUDA graph capture. Native snapshot reads can wait on uncaptured producer events; +the sensor rejects capture before calling the query or sampling observers. +Callers sample eagerly before capture/replay and establish stream dependencies, +then consume the stable `get_data()` buffers inside their graphs. Replays alone +do not refresh contacts or advance history. The sensor's own reduction graph +remains supported because native acquisition runs before its capture boundary. Sensor reset clears only selected rows and invalidates this cache; it does not reset the physics solver. An explicit `update()` may therefore refresh a valid settled snapshot without advancing history, as scene preparation requires. -It cannot recompute solved contacts after physical state writes; the backend's -contact-availability rules still apply. Empty selections do not invalidate the -cache. `BaseEnv` retains its ordinary acquisition and substep scheduling. +If DexSim reports that physics reset invalidated its solved snapshot, observation +reads return invalid contact rows until a new solve. Other query failures still +propagate. Empty selections do not invalidate the cache. `BaseEnv` retains its +ordinary acquisition and substep scheduling. -CUDA queries use `fetch_async()` and device-resident counts. Sparse Warp kernels +CUDA queries use `fetch_solver_report_async()` and device-resident counts. Sparse Warp kernels process valid contact rows, using precomputed sorted environment/actor indices; no environment-by-contact-by-body tensor is allocated. Per-body force, contact flags and timing buffers persist between samples. `current_air_time` measures an unfinished flight; `last_air_time` retains its completed duration on landing. -Selective reset clears only the selected rows. +Selective reset clears only the selected rows. Force reduction uses only +components marked available by the sample's normal/friction validity flags. +Geometry-only rows can count as contact when no force threshold is requested; +they do not create measured force or satisfy a positive force threshold. +Native friction is the tangential impulse on actor 0, while the normal points +toward actor 1: actor-0 force is `(-normal * impulse + friction) / impulse_dt` +and actor 1 receives the opposite force. Newton reports the final internal +solver substep's impulse, so the sensor uses the actual World-step duration +divided by the active backend's `num_substeps` for `impulse_dt`. History clocks +still advance by the full World-step duration. Standalone `ContactHistory.update` +accepts `impulse_dt` explicitly and defaults it to the elapsed `dt`. `dropped_contacts` reports query loss plus separately counted scatter overflow across the control interval. Query counts stay on the device until this diagnostic @@ -64,8 +76,8 @@ and `tests/gym/envs/test_substep_sensors.py`. observer to the same lifecycle. It receives begin, before-sample, after-sample and selected-row reset notifications. With no observers, the update path does not enumerate subscriptions, construct token generators or enter callback loops. -Hooks run outside the sensor's internal CUDA capture; external capture with -observers fails before callbacks run. Observer tokens and after-sample callbacks +Hooks run outside the sensor's internal CUDA capture; all sampling in external +capture fails before callbacks run. Observer tokens and after-sample callbacks run once per physics step alongside history reduction. A raw sample fetched earlier in the same step is reused; repeated reads do not advance history or repeat callbacks. Removal can supply `expected=observer` to retain a replacement diff --git a/agent_context/topics/sensor-system/lifecycle.md b/agent_context/topics/sensor-system/lifecycle.md index aca05cafc8..b1029747c2 100644 --- a/agent_context/topics/sensor-system/lifecycle.md +++ b/agent_context/topics/sensor-system/lifecycle.md @@ -22,7 +22,20 @@ configuration failures; missing per-arena render topology is a runtime failure. `SimulationManager.add_sensor()` prepares the scene and passes its explicit owner to `ContactSensor`. The sensor resolves configured rigid/link targets to -Spawn handles and creates one query. Preserve these query/buffer contracts: +Spawn handles and creates one query. + +Sampling reads completed solver contacts through DexSim's +`fetch_solver_report()` or `fetch_solver_report_async()`. `detect()` and +`fetch()` operate on a separate geometry-only observation and cannot replace +solver reports when interpreting measured impulses or force histories. + +After a physics reset invalidates the native solved report, sensor observation +reads return invalid rows until the next solve. A sensor-buffer-only reset may +still reread the same valid native report. Contact sampling must run outside +caller-owned CUDA graph capture; only the stable observation buffers are consumed +inside that graph. See [history and substeps](contact-history.md) for ordering. + +Preserve these query/buffer contracts: - `filter_need_both_actor` chooses query `match="all"` or `"any"`. - Capacity is bounded per Arena as well as globally, so a busy environment @@ -35,11 +48,18 @@ Spawn handles and creates one query. Preserve these query/buffer contracts: - Only validity/counts reset each update. Data outside valid rows is undefined. `contact_capabilities` distinguishes geometry from normal/friction impulse -support. Do not infer force support from sensor construction alone. Inspect +support for the whole query. Current DexSim DexUni reports may mix geometry-only +and measured-force rows; `impulse_valid` and `friction_valid` preserve their +per-row quantity availability through sensor scatter, filtering and reset. +Unavailable values are placeholders, not measured zeros. Older buffers without +per-row flags use the query capabilities; DexUni queries require the newer +validity contract. Do not infer force support from sensor construction alone. Inspect `supports_contact_sensor` in `embodichain/lab/sim/physics/` for the current -backend/solver/device matrix: Newton AutoSolver may change the answer during +backend/solver/device matrix: DexUni's scene-based dispatch may change the answer during preparation, so the manager checks again afterwards. +Particle-shape contacts do not enter this rigid ContactQuery. + The sensor retains backend-emitted rows rather than reducing an actor pair to one contact. Force-reporting paths apply the query's positive-impulse filtering; geometry-only paths may provide valid rows with zero impulse. Solver settings diff --git a/agent_context/topics/sensor-system/sensor-system.md b/agent_context/topics/sensor-system/sensor-system.md index 3bad691b56..9c34899b29 100644 --- a/agent_context/topics/sensor-system/sensor-system.md +++ b/agent_context/topics/sensor-system/sensor-system.md @@ -27,7 +27,7 @@ Create sensors through `SimulationManager.add_sensor()`. The explicit manager owns the World, ordered Arenas, preparation and semantic parent resolution; sensors must not rediscover a singleton manager. Cameras are render features on both physics backends. Contact availability is a backend capability checked -before preparation and again after Newton AutoSolver selection. +before preparation and again after DexUni's scene-based dispatch. Robot-mounted cameras are declared in `RobotCfg.sensors` with robot-local parent links. `SimulationManager.add_robot()` initializes them only when the diff --git a/agent_context/topics/simulation-system/physics-configuration.md b/agent_context/topics/simulation-system/physics-configuration.md index 2d243afe7f..f3408e77a7 100644 --- a/agent_context/topics/simulation-system/physics-configuration.md +++ b/agent_context/topics/simulation-system/physics-configuration.md @@ -21,10 +21,11 @@ device, not a separate environment tensor device. `None` retains the native default. Gym accepts the former in `physics_config` and the latter at the top level through `config_to_cfg()`. -Newton `solver_cfg=None` preserves DexSim's scene-aware `AutoSolverCfg`. -The concrete solver is resolved from the complete Spawn scene, then exposed -through `NewtonPhysicsBackend.solver_type`. Do not guess a solver-specific robot -preset while it is unresolved. Gradient mode requires an explicit +Newton `solver_cfg=None` preserves DexSim's scene-aware `DexUniSolverCfg`. +DexUni resolves its ownership and contact paths from the complete Spawn scene; +the configured solver type is exposed through `NewtonPhysicsBackend.solver_type`. +Do not guess a solver-specific robot preset while scene ownership is unresolved. +Gradient mode requires an explicit `semi_implicit` solver. Read exact solver options/defaults in `cfg/simulation.py` and the installed DexSim config types; do not substitute a hardcoded fallback when an expected DexSim API is absent. @@ -40,8 +41,10 @@ The manual differentiable trajectory requires the external pipeline; see Capability checks remain explicit: Default owns native rigid constraints and Newton deformables are separate. Contact support is rechecked after Newton -solver resolution; DexUni and MuJoCo-Warp on CPU do not expose the supported -ContactQuery path. See `physics/` and [sensors](../sensor-system/sensor-system.md). +solver resolution. DexUni requires per-row `ContactBuffer` quantity validity; +its one-way CPU MuJoCo path can expose geometry-only contacts, while CPU +MuJoCo force reports are unavailable. See `physics/` and +[sensors](../sensor-system/sensor-system.md). ## Sparse source physics @@ -86,9 +89,11 @@ preserve mode, without replacing explicit zero, margins or materials. Inspect `spawn/descriptors.py` for these application defaults rather than altering DexSim global defaults. -Newton `condim` belongs in collision descriptors before preparation; friction -coefficients belong in material properties. Use per-link overlays for contact -intent, not finalized solver-array mutations. `has_particle_collision` is +Newton `condim`, `contype` and `conaffinity` belong in collision descriptors +before preparation; friction coefficients and raw MuJoCo `solref`, `solimp` and +`solmix` belong in material properties. Explicit `solref` retains its native +positive or direct-mode meaning. Use per-link overlays for contact intent, not +finalized solver-array mutations. `has_particle_collision` is per-shape participation; Arena isolation remains scene-owned. `DefaultRigidBodyPropertiesCfg.has_gravity` is Default-native configuration, diff --git a/embodichain/gen_sim/scene_engine/pipeline/utils/scene_usd.py b/embodichain/gen_sim/scene_engine/pipeline/utils/scene_usd.py index bdbd227e2b..634cbd8568 100644 --- a/embodichain/gen_sim/scene_engine/pipeline/utils/scene_usd.py +++ b/embodichain/gen_sim/scene_engine/pipeline/utils/scene_usd.py @@ -956,7 +956,11 @@ def _export_material_without_texture( material.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), "surface") _set_safe_material_input( source_material=source_material, - getter_name="get_base_color", + getter_name=( + "get_base_color_linear" + if callable(getattr(source_material, "get_base_color_linear", None)) + else "get_base_color" + ), shader=shader, input_name="diffuseColor", value_type=Sdf.ValueTypeNames.Color3f, diff --git a/embodichain/lab/sim/cfg/physics.py b/embodichain/lab/sim/cfg/physics.py index 314eca508a..f7f01b1885 100644 --- a/embodichain/lab/sim/cfg/physics.py +++ b/embodichain/lab/sim/cfg/physics.py @@ -18,7 +18,6 @@ from __future__ import annotations -import inspect import math from collections.abc import Mapping @@ -341,14 +340,52 @@ class NewtonPhysicsCfg(PhysicsBackendCfg): Genuine Newton/Warp warnings and errors are not suppressed. """ + async_sync: bool = False + """Whether Newton-to-render publication returns without waiting for rendering. + + ``True`` removes the render-thread wait from each published step, which + mostly matters while a window is open. ``False`` keeps the blocking + DexSim default. + """ + + rigid_transform_sync_mode: Literal["all", "dynamic"] = "all" + """Rigid transforms copied from Newton to the renderer after each step. + + ``"all"`` copies every mapped body. ``"dynamic"`` skips static and + kinematic bodies, which are already posed by their setters. + """ + + capture_acquire_timeout: float | None = None + """Seconds to wait for CUDA-graph capture rights shared with other users. + + ``None`` waits indefinitely. A finite value lets DexSim fall back to + uncaptured stepping when another component holds capture. + """ + + capture_wait_log_interval: float | None = 10.0 + """Seconds between progress logs while waiting for capture rights. + + ``None`` or a non-positive value disables the progress log. + """ + solver_cfg: Mapping[str, Any] | NewtonSolverCfg | None = None """Optional Newton solver configuration. A mapping is converted to the matching DexSim Newton solver config. Include ``solver_type`` or ``class_type`` to select the solver, then add any parameters accepted by that DexSim solver config. If omitted, EmbodiChain - preserves DexSim's scene-aware ``AutoSolverCfg`` default. A DexSim build - exporting ``AutoSolverCfg`` is required; no concrete-solver fallback is used. + preserves DexSim's ``DexUniSolverCfg`` default and its scene-based dispatch. + Unset ownership selectors retain automatic scene partitioning; + ``contact_mode='auto'`` independently selects the native contact path. + For ``solver_type='dexuni'``, the current DexSim contract keeps expert + options under ``vbd_options``, ``mujoco_options`` and + ``collision_options``. ``joint_mode`` controls joint integration. + ``coupling='two_way'`` enables contact reactions + on dynamically integrated articulations. Optional ``coupling_options`` + accepts ``mass_scale``, ``proxy_relaxation`` and ``iterations``. Two-way + coupling requires ``joint_mode='dynamic'``. Current DexSim supports + full-surface rigid-soft feedback; native validation owns the compatible + contact modes and option combinations. """ collision_cfg: NewtonCollisionPipelineCfg | Mapping[str, Any] | None = field( @@ -366,6 +403,19 @@ def __post_init__(self) -> None: """Normalize dictionary collision settings at the config boundary.""" if isinstance(self.collision_cfg, Mapping): self.collision_cfg = NewtonCollisionPipelineCfg(**self.collision_cfg) + if self.rigid_transform_sync_mode not in ("all", "dynamic"): + raise ValueError( + "NewtonPhysicsCfg.rigid_transform_sync_mode must be 'all' or " + f"'dynamic'; got {self.rigid_transform_sync_mode!r}." + ) + if self.capture_acquire_timeout is not None and not ( + math.isfinite(self.capture_acquire_timeout) + and self.capture_acquire_timeout >= 0.0 + ): + raise ValueError( + "NewtonPhysicsCfg.capture_acquire_timeout must be None or a " + "finite non-negative number of seconds." + ) def to_dexsim_cfg( self, @@ -373,7 +423,6 @@ def to_dexsim_cfg( ) -> NewtonCfg: """Convert this config to ``dexsim.engine.newton_physics.NewtonCfg``.""" from dexsim.engine.newton_physics import ( - AutoSolverCfg, DexUniSolverCfg, FeatherstoneSolverCfg, MJWarpSolverCfg, @@ -394,7 +443,6 @@ def to_dexsim_cfg( ) solver_cfg_map: dict[str, type] = { - "auto": AutoSolverCfg, "dexuni": DexUniSolverCfg, "mujoco_warp": MJWarpSolverCfg, "xpbd": XPBDSolverCfg, @@ -412,7 +460,7 @@ def to_dexsim_cfg( ): logger.log_error( "Newton gradient mode requires an explicit " - "solver_type='semi_implicit'; AutoSolver does not select a " + "solver_type='semi_implicit'; the default does not select a " "differentiable solver." ) @@ -423,14 +471,9 @@ def to_dexsim_cfg( for item in fields(self.collision_cfg) } collision_values["requires_grad"] = self.requires_grad - # Newton 1.6 renamed this field; published DexSim builds also use - # Newton 1.4, whose descriptor retains soft_contact_margin. - if "soft_contact_gap" in { - item.name for item in fields(DexsimNewtonCollisionPipelineCfg) - }: - collision_values["soft_contact_gap"] = collision_values.pop( - "soft_contact_margin" - ) + collision_values["soft_contact_gap"] = collision_values.pop( + "soft_contact_margin" + ) collision_pipeline_cfg = DexsimNewtonCollisionPipelineCfg( **collision_values ) @@ -445,6 +488,10 @@ def to_dexsim_cfg( "suppress_warp_kernel_logs": self.suppress_warp_kernel_logs, "collision_pipeline_cfg": collision_pipeline_cfg, "sync_to_dexsim": self.sync_to_renderer, + "async_sync": self.async_sync, + "rigid_transform_sync_mode": self.rigid_transform_sync_mode, + "capture_acquire_timeout": self.capture_acquire_timeout, + "capture_wait_log_interval": self.capture_wait_log_interval, } if solver_cfg is not None: newton_cfg_args["solver_cfg"] = solver_cfg @@ -460,11 +507,6 @@ def _normalize_newton_solver_type(solver_type: str) -> str: """Normalize public EmbodiChain and DexSim Newton solver aliases.""" key = solver_type.replace("-", "_").lower() aliases = { - "auto": "auto", - "autosolver": "auto", - "autosolvercfg": "auto", - "auto_solver": "auto", - "auto_solver_cfg": "auto", "mjwarp": "mujoco_warp", "mjwarpsolver": "mujoco_warp", "mjwarpsolvercfg": "mujoco_warp", @@ -496,7 +538,7 @@ def _normalize_newton_solver_type(solver_type: str) -> str: if key not in aliases: logger.log_error( f"Unsupported Newton solver type '{solver_type}'. " - "Expected one of 'auto', 'dexuni', 'mjwarp', 'xpbd', 'semi_implicit', " + "Expected one of 'dexuni', 'mjwarp', 'xpbd', 'semi_implicit', " "'featherstone', or 'vbd'." ) return aliases[key] @@ -522,50 +564,10 @@ def _newton_solver_cfg_to_dexsim( configured_solver_type = ( solver_cfg_data.pop("solver_type", None) or solver_cfg_data.pop("class_type", None) - or "auto" + or "dexuni" ) normalized_solver_type = _normalize_newton_solver_type(str(configured_solver_type)) solver_cfg_type = solver_cfg_map[normalized_solver_type] - if normalized_solver_type == "dexuni": - try: - uses_grouped_options = ( - "vbd_options" in inspect.signature(solver_cfg_type).parameters - ) - except (TypeError, ValueError): - uses_grouped_options = True - if not uses_grouped_options: - # DexSim 0.5.0 still owns the legacy flat DexUni dataclass. - return solver_cfg_type(**solver_cfg_data) - - # DexSim 0.5.0 exposed these options directly on DexUniSolverCfg. The - # 0.5.1rc1 facade groups them by owned solver path, so keep existing - # EmbodiChain mappings source-compatible while forwarding the values to - # the native option contract. - legacy_values: dict[str, Any] = {} - vbd_options = dict(solver_cfg_data.get("vbd_options", {})) - if "iterations" in solver_cfg_data: - legacy_values["iterations"] = solver_cfg_data.pop("iterations") - vbd_options.setdefault("iterations", legacy_values["iterations"]) - if "step_rigid_bodies" in solver_cfg_data: - legacy_values["step_rigid_bodies"] = solver_cfg_data.pop( - "step_rigid_bodies" - ) - # The new option describes the inverse operation: when true, VBD - # integrates rigid state supplied by an external rigid solver. - vbd_options.setdefault( - "integrate_with_external_rigid_solver", - not legacy_values["step_rigid_bodies"], - ) - if vbd_options: - solver_cfg_data["vbd_options"] = vbd_options - - native_cfg = solver_cfg_type(**solver_cfg_data) - # Preserve read-only compatibility for callers that still inspect the - # legacy attributes after conversion. Native DexSim dataclasses are - # not slotted, so these aliases do not affect its option validation. - for name, value in legacy_values.items(): - setattr(native_cfg, name, value) - return native_cfg return solver_cfg_type(**solver_cfg_data) diff --git a/embodichain/lab/sim/cfg/rigid.py b/embodichain/lab/sim/cfg/rigid.py index 2d32f5b4af..a2e10700ff 100644 --- a/embodichain/lab/sim/cfg/rigid.py +++ b/embodichain/lab/sim/cfg/rigid.py @@ -246,6 +246,12 @@ class NewtonCollisionPropertiesCfg(CollisionPropertiesCfg): can allocate the corresponding constraint storage. """ + contype: int | None = None + """MuJoCo collision-type bitmask; None preserves the source value.""" + + conaffinity: int | None = None + """MuJoCo collision-affinity bitmask; None preserves the source value.""" + has_particle_collision: bool | None = None """Whether Newton particles collide with this shape. @@ -323,6 +329,19 @@ class NewtonRigidBodyMaterialCfg(RigidBodyMaterialCfg): kd: float | None = None """Normal contact damping coefficient.""" + solref: tuple[float, float] | None = None + """Raw MuJoCo contact reference, overriding stiffness/damping conversion. + + Both positive time-constant/damping-ratio and non-positive direct modes + retain their native meaning. Unsupported solvers reject explicit values. + """ + + solimp: tuple[float, float, float, float, float] | None = None + """Raw MuJoCo contact impedance; None preserves the source value.""" + + solmix: float | None = None + """MuJoCo weight for mixing contact parameters between colliding shapes.""" + kf: float | None = None """Tangential/friction damping coefficient.""" diff --git a/embodichain/lab/sim/cfg/robot.py b/embodichain/lab/sim/cfg/robot.py index 3c3d13b364..05fd178b25 100644 --- a/embodichain/lab/sim/cfg/robot.py +++ b/embodichain/lab/sim/cfg/robot.py @@ -387,21 +387,22 @@ def resolve( if solver_type is None: solver_cfg = physics_cfg.solver_cfg if solver_cfg is None: - solver_type = "auto" + solver_type = "dexuni" elif isinstance(solver_cfg, Mapping): solver_type = str( solver_cfg.get("solver_type") or solver_cfg.get("class_type") - or "auto" + or "dexuni" ) else: solver_type = str(getattr(solver_cfg, "solver_type")) solver_type = _normalize_newton_solver_type(solver_type) solver_candidates = [] - if solver_type != "auto": - solver_candidates.append(f"newton_{solver_type}") - if solver_type == "mujoco_warp": - solver_candidates.append("newton_mjwarp") + solver_candidates.append(f"newton_{solver_type}") + if solver_type == "dexuni": + solver_candidates.extend(("newton_mujoco_warp", "newton_mjwarp")) + if solver_type == "mujoco_warp": + solver_candidates.append("newton_mjwarp") candidates = (*solver_candidates, "newton", "default") for candidate in candidates: diff --git a/embodichain/lab/sim/physics/newton.py b/embodichain/lab/sim/physics/newton.py index cbb37d11bf..ac1f8544e7 100644 --- a/embodichain/lab/sim/physics/newton.py +++ b/embodichain/lab/sim/physics/newton.py @@ -201,19 +201,39 @@ def supports_rigid_object_group(self) -> bool: @property def supports_contact_sensor(self) -> bool: # ContactSensor consumes the backend-neutral Scene ContactQuery API, - # but not every Newton solver/device pair currently publishes that - # query. Keep this runtime-sensitive so ``solver_type='auto'`` can be - # rechecked after Spawn finalization resolves the concrete solver. + # but CPU MuJoCo cannot publish its force report. Keep this runtime- + # sensitive so DexUni's scene-selected solver path is + # rechecked after Spawn finalization. solver_type = self.solver_type if solver_type is None: return True solver_type = solver_type.lower() if solver_type.endswith("dexuni"): - return False + from dexsim.scene import ContactBuffer + + # Current DexSim exposes mixed DexUni/MuJoCo contact reports with + # per-row quantity validity. Older wheels lack that contract. + if not hasattr(ContactBuffer, "impulse_valid"): + return False + world = getattr(self._manager, "_world", None) + if world is not None: + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + + backend = get_newton_backend(world) + mujoco_solver = getattr(backend, "mujoco_solver", None) + if mujoco_solver is not None and mujoco_solver.use_mujoco_cpu: + features = getattr(getattr(backend, "solver", None), "features", None) + # One-way DexUni disables MuJoCo contacts and keeps the VBD + # geometry-only source. Other CPU MuJoCo paths cannot report + # contacts, even when the Newton model itself lives on CUDA. + return bool( + solver_type.endswith("dexuni") + and getattr(features, "backend", None) == "coupled" + and not getattr(features, "two_way_coupling_enabled", False) + ) runtime_device = self._runtime_device if runtime_device is None: runtime_device = str(getattr(self._manager, "device", "")) return not ( - solver_type.endswith("mujoco_warp") - and runtime_device.lower().startswith("cpu") + solver_type == "mujoco_warp" and runtime_device.lower().startswith("cpu") ) diff --git a/embodichain/lab/sim/sensors/contact/_warp/history.py b/embodichain/lab/sim/sensors/contact/_warp/history.py index d788468d3b..2eab89abad 100644 --- a/embodichain/lab/sim/sensors/contact/_warp/history.py +++ b/embodichain/lab/sim/sensors/contact/_warp/history.py @@ -84,13 +84,17 @@ def reduce_contact_rows( actors: wp.array(dtype=wp.int32, ndim=2), env_ids: wp.array(dtype=wp.int32), counts: wp.array(dtype=wp.int32), + quantity_valid: wp.array(dtype=wp.bool, ndim=2), + has_quantity_valid: bool, + impulse_available: bool, + friction_available: bool, keys: wp.array(dtype=wp.int64), rows: wp.array(dtype=wp.int32), counterparts: wp.array(dtype=wp.int64), accept_all: bool, accept_unknown: bool, threshold: float, - dt: float, + impulse_dt: float, forces: wp.array(dtype=wp.float32, ndim=3), hits: wp.array(dtype=wp.int32, ndim=2), env_hits: wp.array(dtype=wp.int32), @@ -101,14 +105,19 @@ def reduce_contact_rows( env = env_ids[i] if env < 0 or env >= hits.shape[0] or hits.shape[1] == 0: return - force = ( - wp.vec3( - data[i, 3] * data[i, 9] + data[i, 6], - data[i, 4] * data[i, 9] + data[i, 7], - data[i, 5] * data[i, 9] + data[i, 8], - ) - / dt - ) + normal_valid = impulse_available + tangent_valid = friction_available + if has_quantity_valid: + normal_valid = quantity_valid[i, 0] + tangent_valid = quantity_valid[i, 1] + # reduce_pair negates this vector for actor 0. Native friction already + # describes the impulse on actor 0, so subtract it here. + force = wp.vec3(0.0) + if normal_valid: + force = wp.vec3(data[i, 3], data[i, 4], data[i, 5]) * data[i, 9] + if tangent_valid: + force = force - wp.vec3(data[i, 6], data[i, 7], data[i, 8]) + force = force / impulse_dt reduce_pair( env, actors[i, 0], @@ -134,13 +143,15 @@ def reduce_contact_batch( normal: wp.array(dtype=wp.float32, ndim=3), friction: wp.array(dtype=wp.float32, ndim=3), impulse: wp.array(dtype=wp.float32, ndim=2), + impulse_valid: wp.array(dtype=wp.bool, ndim=2), + friction_valid: wp.array(dtype=wp.bool, ndim=2), keys: wp.array(dtype=wp.int64), rows: wp.array(dtype=wp.int32), counterparts: wp.array(dtype=wp.int64), accept_all: bool, accept_unknown: bool, threshold: float, - dt: float, + impulse_dt: float, forces: wp.array(dtype=wp.float32, ndim=3), hits: wp.array(dtype=wp.int32, ndim=2), env_hits: wp.array(dtype=wp.int32), @@ -148,14 +159,17 @@ def reduce_contact_batch( env, i = wp.tid() if not valid[env, i] or hits.shape[1] == 0: return - force = ( - wp.vec3( - normal[env, i, 0] * impulse[env, i] + friction[env, i, 0], - normal[env, i, 1] * impulse[env, i] + friction[env, i, 1], - normal[env, i, 2] * impulse[env, i] + friction[env, i, 2], + force = wp.vec3(0.0) + if impulse_valid[env, i]: + force = ( + wp.vec3(normal[env, i, 0], normal[env, i, 1], normal[env, i, 2]) + * impulse[env, i] ) - / dt - ) + if friction_valid[env, i]: + force = force - wp.vec3( + friction[env, i, 0], friction[env, i, 1], friction[env, i, 2] + ) + force = force / impulse_dt reduce_pair( env, actors[env, i, 0], diff --git a/embodichain/lab/sim/sensors/contact/_warp/scatter.py b/embodichain/lab/sim/sensors/contact/_warp/scatter.py index 24915e5f60..63ef5d9544 100644 --- a/embodichain/lab/sim/sensors/contact/_warp/scatter.py +++ b/embodichain/lab/sim/sensors/contact/_warp/scatter.py @@ -41,7 +41,7 @@ def _scatter_contact_row( ): env_id = env_ids[i] if env_id < 0 or env_id >= num_contacts_per_env.shape[0]: - return + return wp.vec2i(-1, -1) # Atomically increment contact counter for this environment contact_idx = wp.atomic_add(num_contacts_per_env, env_id, 1) @@ -51,7 +51,7 @@ def _scatter_contact_row( wp.atomic_add(dropped, env_id, 1) # Decrement counter since we didn't write this contact wp.atomic_sub(num_contacts_per_env, env_id, 1) - return + return wp.vec2i(-1, -1) # Extract contact data columns x = contact_data[i, 0] @@ -86,6 +86,7 @@ def _scatter_contact_row( user_ids_out[env_id, contact_idx, 1] = user_ids[i, 1] is_valid[env_id, contact_idx] = True + return wp.vec2i(env_id, contact_idx) @wp.kernel(enable_backward=False) @@ -141,11 +142,17 @@ def scatter_contact_rows( is_valid: wp.array(dtype=wp.bool, ndim=2), counts: wp.array(dtype=wp.int32), dropped: wp.array(dtype=wp.int32), + quantity_valid: wp.array(dtype=wp.bool, ndim=2), + has_quantity_valid: bool, + impulse_available: bool, + friction_available: bool, + impulse_valid: wp.array(dtype=wp.bool, ndim=2), + friction_valid: wp.array(dtype=wp.bool, ndim=2), ): """Scatter valid device-counted rows and record per-environment overflow.""" i = wp.tid() if i < counts[0]: - _scatter_contact_row( + row = _scatter_contact_row( i, contact_data, user_ids, @@ -162,6 +169,14 @@ def scatter_contact_rows( True, dropped, ) + if row[0] >= 0: + normal_valid = impulse_available + tangent_valid = friction_available + if has_quantity_valid: + normal_valid = quantity_valid[i, 0] + tangent_valid = quantity_valid[i, 1] + impulse_valid[row[0], row[1]] = normal_valid + friction_valid[row[0], row[1]] = tangent_valid @wp.kernel(enable_backward=False) @@ -180,6 +195,12 @@ def scatter_contact_rows_with_loss( is_valid: wp.array(dtype=wp.bool, ndim=2), counts: wp.array(dtype=wp.int32), dropped: wp.array(dtype=wp.int32), + quantity_valid: wp.array(dtype=wp.bool, ndim=2), + has_quantity_valid: bool, + impulse_available: bool, + friction_available: bool, + impulse_valid: wp.array(dtype=wp.bool, ndim=2), + friction_valid: wp.array(dtype=wp.bool, ndim=2), ): """Scatter and retain query loss without a separate device copy. @@ -191,7 +212,7 @@ def scatter_contact_rows_with_loss( if i == 0: dropped[dropped.shape[0] - 1] = counts[1] if i < counts[0]: - _scatter_contact_row( + row = _scatter_contact_row( i, contact_data, user_ids, @@ -208,3 +229,11 @@ def scatter_contact_rows_with_loss( True, dropped, ) + if row[0] >= 0: + normal_valid = impulse_available + tangent_valid = friction_available + if has_quantity_valid: + normal_valid = quantity_valid[i, 0] + tangent_valid = quantity_valid[i, 1] + impulse_valid[row[0], row[1]] = normal_valid + friction_valid[row[0], row[1]] = tangent_valid diff --git a/embodichain/lab/sim/sensors/contact/history.py b/embodichain/lab/sim/sensors/contact/history.py index 0061f983a8..037fabd413 100644 --- a/embodichain/lab/sim/sensors/contact/history.py +++ b/embodichain/lab/sim/sensors/contact/history.py @@ -108,9 +108,12 @@ def _index_actors(ids: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: keys, rows = keys.flatten().sort() return keys, rows.to(torch.int32) - def _start_sample(self, dt: float) -> list: + def _start_sample(self, dt: float, impulse_dt: float | None) -> list: if dt <= 0: raise ValueError("Contact sampling dt must be positive.") + impulse_dt = dt if impulse_dt is None else impulse_dt + if impulse_dt <= 0: + raise ValueError("Contact impulse duration must be positive.") self.force.zero_() self._hits.zero_() self._env_hits.zero_() @@ -121,7 +124,7 @@ def _start_sample(self, dt: float) -> list: self.counterpart_ids is None, self.include_unknown_counterpart, self.force_threshold, - dt, + impulse_dt, wp.from_torch(self.force), wp.from_torch(self._hits), wp.from_torch(self._env_hits), @@ -160,9 +163,18 @@ def _finish_sample(self, dt: float) -> None: ], ) - def _update_from_query(self, buffer: ContactBuffer, dt: float) -> None: + def _update_from_query( + self, + buffer: ContactBuffer, + dt: float, + *, + impulse_dt: float | None = None, + impulse_available: bool = True, + friction_available: bool = True, + ) -> None: """Reduce compact query rows using its device-resident valid-row count.""" - args = self._start_sample(dt) + args = self._start_sample(dt, impulse_dt) + quantity_valid = getattr(buffer, "quantity_valid", None) self._launch( reduce_contact_rows, buffer.capacity, @@ -171,6 +183,12 @@ def _update_from_query(self, buffer: ContactBuffer, dt: float) -> None: wp.from_torch(buffer.actor_ids), wp.from_torch(buffer.env_ids), wp.from_torch(buffer.count_device), + wp.from_torch( + quantity_valid if quantity_valid is not None else self.contact + ), + quantity_valid is not None, + impulse_available, + friction_available, *args, ], ) @@ -183,14 +201,23 @@ def begin_control_step(self) -> None: self.peak_force.zero_() self.contact_count.zero_() - def update(self, data: Mapping[str, torch.Tensor], dt: float) -> None: + def update( + self, + data: Mapping[str, torch.Tensor], + dt: float, + *, + impulse_dt: float | None = None, + ) -> None: """Reduce a new physics sample and advance timing exactly once. Args: data: ContactSensor data in its documented actor/impulse convention. - dt: Duration of the physics sample in seconds. + dt: Elapsed World-step duration for history clocks, in seconds. + impulse_dt: Duration represented by the reported impulses, in + seconds. Defaults to dt. Newton solver reports represent the + final internal substep, so pass World dt / num_substeps. """ - args = self._start_sample(dt) + args = self._start_sample(dt, impulse_dt) self._launch( reduce_contact_batch, data["is_valid"].shape, @@ -200,6 +227,8 @@ def update(self, data: Mapping[str, torch.Tensor], dt: float) -> None: wp.from_torch(data[name]) for name in ("is_valid", "normal", "friction", "impulse") ], + wp.from_torch(data.get("impulse_valid", data["is_valid"])), + wp.from_torch(data.get("friction_valid", data["is_valid"])), *args, ], ) diff --git a/embodichain/lab/sim/sensors/contact/sensor.py b/embodichain/lab/sim/sensors/contact/sensor.py index d3723c29b0..7c8b15eda1 100644 --- a/embodichain/lab/sim/sensors/contact/sensor.py +++ b/embodichain/lab/sim/sensors/contact/sensor.py @@ -148,7 +148,10 @@ class ContactSensor(BaseSensor): The sensor preserves every backend-emitted contact row that passes the backend's positive-impulse filter. Geometry-only Newton solvers retain all - candidate contact rows. Repeated updates within one completed physics step + candidate rigid-contact rows. Mixed DexUni reports preserve measured + MuJoCo rows alongside geometry-only rows. Inspect ``impulse_valid`` and + ``friction_valid`` per row before interpreting the reported quantities. + Repeated updates within one completed physics step reuse the sample, unless the query's topology changes or the sensor resets. """ @@ -160,6 +163,8 @@ class ContactSensor(BaseSensor): "distance", "user_ids", "is_valid", + "impulse_valid", + "friction_valid", ] def __init__( @@ -189,6 +194,7 @@ def __init__( self.device = device self.cfg = config self._query: ContactQuery | None = None + self._empty_contact_buffer: ContactBuffer | None = None self._metadata_actor_table: tuple[ContactActorInfo, ...] | None = None self._metadata_selected_ids: tuple[int, ...] | None = None @@ -333,6 +339,16 @@ def _build_sensor_from_config( dtype=torch.bool, device=device, ), + "impulse_valid": torch.zeros( + (num_envs, config.max_contacts_per_env), + dtype=torch.bool, + device=device, + ), + "friction_valid": torch.zeros( + (num_envs, config.max_contacts_per_env), + dtype=torch.bool, + device=device, + ), }, batch_size=[num_envs, config.max_contacts_per_env], device=device, @@ -376,30 +392,34 @@ def update(self, **kwargs) -> None: Repeated calls reuse the scattered data and establish readiness on the caller's CUDA stream. Resetting sensor buffers permits a fresh read of the current backend snapshot without advancing contact history. This - does not recompute contacts after physical state writes: backend rules - still determine whether a solved contact snapshot is available. + does not recompute contacts after physical state writes. A report + invalidated by physics reset yields invalid rows until the next solve. + Call outside caller-owned CUDA capture; graphs may consume get_data(). Args: **kwargs: Additional keyword arguments for sensor update. """ + self._require_eager_sampling() self._wait_for_sample() - capturing = ( - self.device.type == "cuda" and torch.cuda.is_current_stream_capturing() - ) - key = None if capturing else self._contact_sample_key() - if not capturing and key == self._last_update_key: + key = self._contact_sample_key() + if key == self._last_update_key: return buffer = self._fetch_contacts() if not self.requires_substep_update: self._query_dropped_count.copy_(buffer.dropped_count_device) self._scatter_dropped_count.copy_(self._sample_scatter_dropped_count) self._last_update_key = key - self._last_contact_buffer = None if capturing else buffer - if capturing: - self._last_history_key = None - else: - self._record_sample_stream() + self._last_contact_buffer = buffer + self._record_sample_stream() + + def _require_eager_sampling(self) -> None: + if self.device.type == "cuda" and torch.cuda.is_current_stream_capturing(): + raise RuntimeError( + "Contact sampling must run outside external CUDA capture. " + "Sample eagerly before capture/replay, establish stream " + "dependencies, and consume get_data() buffers inside the graph." + ) def _contact_sample_key(self) -> tuple[int, int, int]: """Consult public query metadata so topology refreshes cannot be skipped.""" @@ -431,10 +451,25 @@ def _fetch_contacts(self) -> ContactBuffer: def _read_contacts(self) -> ContactBuffer: assert self._query is not None - if self.device.type == "cuda": - buffer = self._query.fetch_async() - else: - buffer = self._query.fetch() + try: + if self.device.type == "cuda": + buffer = self._query.fetch_solver_report_async() + else: + buffer = self._query.fetch_solver_report() + except RuntimeError as error: + # DexSim 0.6 has no public report-availability flag or dedicated + # exception. Only this reset-time state is an empty observation; + # unrelated query and device failures must still reach the caller. + if str(error) != ( + "Contact state is unavailable after reset until the next World.update" + ): + raise + if self._empty_contact_buffer is None: + from dexsim.scene import ContactBuffer + + self._empty_contact_buffer = ContactBuffer.allocate(1, self.device) + buffer = self._empty_contact_buffer + if self.device.type == "cpu": buffer.device_counts[0] = buffer.count buffer.device_counts[1] = buffer.dropped_count self._sync_filter_actor_metadata() @@ -449,8 +484,11 @@ def _scatter_contacts(self, buffer: ContactBuffer) -> None: kernel = scatter_contact_rows_with_loss assert self._num_contacts_per_env is not None self._num_contacts_per_env.zero_() - self._data_buffer["is_valid"].zero_() + for name in ("is_valid", "impulse_valid", "friction_valid"): + self._data_buffer[name].zero_() self._sample_scatter_dropped_count.zero_() + quantity_valid = getattr(buffer, "quantity_valid", None) + capabilities = self.contact_capabilities wp.launch( kernel=kernel, dim=buffer.capacity, @@ -474,6 +512,16 @@ def _scatter_contacts(self, buffer: ContactBuffer) -> None: ], wp.from_torch(buffer.device_counts), wp.from_torch(self._latest_loss_counters), + wp.from_torch( + quantity_valid + if quantity_valid is not None + else self._data_buffer["is_valid"] + ), + quantity_valid is not None, + capabilities.impulse, + capabilities.friction, + wp.from_torch(self._data_buffer["impulse_valid"]), + wp.from_torch(self._data_buffer["friction_valid"]), ], device=str(self.device), stream=current_warp_stream(self.device), @@ -577,11 +625,17 @@ def get_data(self) -> TensorDict: Returns: Batched contact data. ``position`` is in the Arena frame; - ``normal`` points from actor 0 toward actor 1; ``friction`` and - ``impulse`` are impulses; ``distance`` is signed separation; + ``normal`` points from actor 0 toward actor 1; ``friction`` is + tangential impulse on actor 0; ``impulse`` preserves the native + scalar magnitude (total for Default CPU, normal otherwise); + ``distance`` is signed separation; ``user_ids`` contains backend-neutral contact actor IDs; and ``is_valid`` marks rows populated by the latest update. Values in invalid rows are unspecified and may come from an earlier update. + ``impulse_valid`` and ``friction_valid`` mark measured quantities + per row; unavailable values are placeholders, not measured zeros. + These flags may be true even when the aggregate capability is false + for a mixed DexUni report. """ return self._data_buffer @@ -729,18 +783,11 @@ def update_physics_step(self, dt: float) -> None: """ if dt <= 0: raise ValueError("Contact sampling dt must be positive.") + self._require_eager_sampling() self._wait_for_sample() - # Capturing records future work; Python cache keys cannot track replay. - capturing = ( - self.device.type == "cuda" and torch.cuda.is_current_stream_capturing() - ) observers = self._sampling_observers - if observers and capturing: - raise RuntimeError( - "Contact sampling observers require calls outside external CUDA capture." - ) - key = None if capturing else self._contact_sample_key() - if not capturing and key == self._last_history_key: + key = self._contact_sample_key() + if key == self._last_history_key: return # Keep the default history-only path free of observer snapshots and # generator allocation. Capture a stable subscription set only when used. @@ -752,28 +799,42 @@ def update_physics_step(self, dt: float) -> None: ) buffer = ( self._last_contact_buffer - if not capturing and key == self._last_update_key + if key == self._last_update_key else self._read_contacts() ) assert buffer is not None self._sample_physics_contacts(buffer, dt) self._last_update_key = key self._last_history_key = key - self._last_contact_buffer = None if capturing else buffer - if not capturing: - self._record_sample_stream() + self._last_contact_buffer = buffer + self._record_sample_stream() if tokens is not None: for observer, token in tokens: observer.after_sample(dt, token) def _sample_physics_contacts(self, buffer: ContactBuffer, dt: float) -> None: """Run the reduction on the existing caller/capture stream boundary.""" - if self.device.type != "cuda" or torch.cuda.is_current_stream_capturing(): - self._sample_contacts(buffer, dt) + impulse_dt = dt + if ( + self._histories + and getattr(getattr(self._sim, "physics", None), "name", None) == "newton" + ): + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + + backend = get_newton_backend(self._sim._world) + # Newton reports the last internal solver substep's impulse, not + # the impulse integrated over the whole World.update interval. + impulse_dt = dt / max(int(backend.cfg.num_substeps), 1) + if self.device.type != "cuda": + self._sample_contacts(buffer, dt, impulse_dt) return key = ( id(buffer), + id(getattr(buffer, "quantity_valid", None)), + self.contact_capabilities.impulse, + self.contact_capabilities.friction, dt, + impulse_dt, self.cfg.max_contacts_per_env, tuple( ( @@ -801,22 +862,30 @@ def _sample_physics_contacts(self, buffer: ContactBuffer, dt: float) -> None: self._sample_graph_key = key # This real sample also compiles/warms every kernel. Do not # execute history updates twice while preparing capture. - self._sample_contacts(buffer, dt) + self._sample_contacts(buffer, dt, impulse_dt) else: if self._sample_graph is None: with wp.ScopedCapture(stream=self._sample_stream) as capture: - self._sample_contacts(buffer, dt) + self._sample_contacts(buffer, dt, impulse_dt) self._sample_graph = capture.graph wp.capture_launch(self._sample_graph, stream=self._sample_stream) if not same_stream: caller.wait_stream(self._sample_torch_stream) - def _sample_contacts(self, buffer: ContactBuffer, dt: float) -> None: + def _sample_contacts( + self, buffer: ContactBuffer, dt: float, impulse_dt: float + ) -> None: self._scatter_contacts(buffer) self._query_dropped_count.add_(buffer.dropped_count_device) self._scatter_dropped_count.add_(self._sample_scatter_dropped_count) for history in self._histories.values(): - history._update_from_query(buffer, dt) + history._update_from_query( + buffer, + dt, + impulse_dt=impulse_dt, + impulse_available=self.contact_capabilities.impulse, + friction_available=self.contact_capabilities.friction, + ) def reset(self, env_ids: Sequence[int] | None = None) -> None: """Clear contact samples and histories only for the selected rows. @@ -839,6 +908,8 @@ def reset(self, env_ids: Sequence[int] | None = None) -> None: self._wait_for_sample() for value in ( self._data_buffer["is_valid"], + self._data_buffer["impulse_valid"], + self._data_buffer["friction_valid"], self._num_contacts_per_env, self._scatter_dropped_count, self._sample_scatter_dropped_count, @@ -889,6 +960,12 @@ def filter_by_user_ids( "distance": self._data_buffer["distance"][env_ids_tensor].flatten(0, 1), "user_ids": self._data_buffer["user_ids"][env_ids_tensor].flatten(0, 1), "is_valid": self._data_buffer["is_valid"][env_ids_tensor].flatten(0, 1), + "impulse_valid": self._data_buffer["impulse_valid"][env_ids_tensor].flatten( + 0, 1 + ), + "friction_valid": self._data_buffer["friction_valid"][ + env_ids_tensor + ].flatten(0, 1), } # Create valid mask (only slots up to _num_contacts_per_env are valid) @@ -927,6 +1004,12 @@ def filter_by_user_ids( (0, 2), dtype=torch.int32, device=self.device ), "is_valid": torch.empty((0,), dtype=torch.bool, device=self.device), + "impulse_valid": torch.empty( + (0,), dtype=torch.bool, device=self.device + ), + "friction_valid": torch.empty( + (0,), dtype=torch.bool, device=self.device + ), }, batch_size=[0], device=self.device, diff --git a/embodichain/lab/sim/sim_manager.py b/embodichain/lab/sim/sim_manager.py index a19328f9fe..2c1f261bb5 100644 --- a/embodichain/lab/sim/sim_manager.py +++ b/embodichain/lab/sim/sim_manager.py @@ -624,9 +624,9 @@ def _initialize_simulation( self._requested_renderer = sim_config.render_cfg.renderer solver_cfg = getattr(sim_config.physics_cfg, "solver_cfg", None) self._requested_solver = str( - solver_cfg.get("solver_type", solver_cfg.get("class_type", "auto")) + solver_cfg.get("solver_type", solver_cfg.get("class_type", "dexuni")) if isinstance(solver_cfg, Mapping) - else getattr(solver_cfg, "solver_type", "auto") + else getattr(solver_cfg, "solver_type", "dexuni") ) # Cache paths self._sim_cache_dir = SIM_CACHE_DIR @@ -2892,11 +2892,11 @@ def add_deformable_object(self, cfg: DeformableObjectCfg) -> DeformableObject: "Newton deformable particle sets currently require a CUDA device." ) solver_type = self._active_newton_solver_type - supported_solvers = {"auto", "dexuni", "xpbd", "semi_implicit", "vbd"} + supported_solvers = {"dexuni", "xpbd", "semi_implicit", "vbd"} if solver_type not in supported_solvers: raise NotImplementedError( f"Newton solver {solver_type!r} does not support deformable " - "particle sets; select one of 'auto', 'dexuni', 'xpbd', " + "particle sets; select one of 'dexuni', 'xpbd', " "'semi_implicit', or 'vbd'." ) physics_cfg = self.sim_config.physics_cfg @@ -4950,8 +4950,8 @@ def reset_objects_state( solver_type = getattr(self.physics, "solver_type", None) requires_complete_world_clear = solver_type in { None, - "auto", "mujoco_warp", + "dexuni", } if ( use_coordinated_newton_clear @@ -5059,6 +5059,9 @@ def export_usd(self, fpath: str) -> bool: bool: True if export is successful, False otherwise. """ try: + # DexSim registers USD export methods when the optional kit is imported. + import dexsim.kit.usd + self._env.export_to_usd_file(fpath) logger.log_info(f"Simulation scene exported to USD file: {fpath}") return True @@ -5178,6 +5181,14 @@ def _deferred_destroy(self) -> None: for asset in getattr(self, registry_name, {}).values(): if isinstance(asset, VisualObject): asset._release_spawn_references() + # Release parsed material wrappers before the native renderer. + for material_attr in ( + "_visual_material", + "_original_visual_material", + "_original_visual_material_inst", + ): + if hasattr(asset, material_attr): + setattr(asset, material_attr, []) if hasattr(asset, "_data"): asset._data = None if hasattr(asset, "_spawn_result"): diff --git a/embodichain/lab/sim/spawn/descriptors.py b/embodichain/lab/sim/spawn/descriptors.py index a580e12397..73d399007c 100644 --- a/embodichain/lab/sim/spawn/descriptors.py +++ b/embodichain/lab/sim/spawn/descriptors.py @@ -1225,7 +1225,7 @@ def _compile_joint_properties( if newton_solver_type is None else newton_solver_type.replace("-", "_").lower() ) - if normalized_solver not in {None, "auto", "mujoco_warp", "mjwarp"} and any( + if normalized_solver not in {None, "dexuni", "mujoco_warp", "mjwarp"} and any( mode == 1 for mode in joint_target_modes.values() ): warnings.warn( diff --git a/embodichain/lab/sim/spawn/source.py b/embodichain/lab/sim/spawn/source.py index e916610c1a..0e2a5163b5 100644 --- a/embodichain/lab/sim/spawn/source.py +++ b/embodichain/lab/sim/spawn/source.py @@ -31,6 +31,7 @@ DexsimPhysicsDesc, RigidBodyPhysicsDesc, ) +from dexsim.engine import JointType from embodichain.lab.sim._inertia import _principal_inertia_matrix from embodichain.utils.math import quat_wxyz_to_xyzw @@ -91,6 +92,12 @@ def resolve_articulation_source( desc.joints = [] desc.root_link_name = None + retained_joint_limits = { + joint.name + for joint in desc.joints + if joint.lower_limit is not None or joint.upper_limit is not None + } + arena = _source_arena(builder, desc) temp_name = f"__embodichain_resolve__{desc.name.replace('/', '__')}__{id(desc)}" skeleton = arena.create_skeleton("skeleton") @@ -121,6 +128,16 @@ def resolve_articulation_source( except (KeyError, RuntimeError, TypeError, AttributeError): continue _translate_urdf_articulation(skeleton, desc) + # Native Skeleton metadata exposes (0, 0) for URDF continuous joints. + # Preserve their unbounded source intent before a MuJoCo build, while + # retaining caller-authored limits and subsequent config overlays. + for joint in desc.joints: + if ( + joint.joint_type == JointType.CONTINUOUS + and joint.name not in retained_joint_limits + ): + joint.lower_limit = -np.inf + joint.upper_limit = np.inf source_states = _read_urdf_inertial_states(desc.urdf_path) _annotate_urdf_source_physics( desc, diff --git a/embodichain/learning/rl/experimental/newton/planar_reach.py b/embodichain/learning/rl/experimental/newton/planar_reach.py index 41c3e5adf9..81d3f2f04c 100644 --- a/embodichain/learning/rl/experimental/newton/planar_reach.py +++ b/embodichain/learning/rl/experimental/newton/planar_reach.py @@ -351,7 +351,7 @@ def close(self) -> None: """No-op; Warp/Newton resources are released with the Python object.""" def _build_model(self) -> tuple[Any, wp.array]: - template = newton.ModelBuilder(gravity=0.0, up_axis=newton.Axis.Y) + template = newton.ModelBuilder(gravity=(0.0, 0.0, 0.0), up_axis=newton.Axis.Y) first_link = template.add_link(label="planar_first_link") template.add_shape_sphere(first_link, radius=0.01) second_link = template.add_link(label="planar_end_link") @@ -388,7 +388,7 @@ def _build_model(self) -> tuple[Any, wp.array]: ) template.add_articulation([first_joint, second_joint]) - builder = newton.ModelBuilder(gravity=0.0, up_axis=newton.Axis.Y) + builder = newton.ModelBuilder(gravity=(0.0, 0.0, 0.0), up_axis=newton.Axis.Y) builder.replicate( template, world_count=self.num_envs, diff --git a/extensions/embodichain_robots/brainco/revo1/cfg.py b/extensions/embodichain_robots/brainco/revo1/cfg.py index 713395b5bc..7fdd4380bb 100644 --- a/extensions/embodichain_robots/brainco/revo1/cfg.py +++ b/extensions/embodichain_robots/brainco/revo1/cfg.py @@ -22,7 +22,7 @@ import torch -from embodichain.data import get_data_path +import embodichain.data as data from embodichain.lab.sim.cfg import ( ArticulationRootPropertiesCfg, JointDrivePropertiesCfg, @@ -83,7 +83,7 @@ def _build_defaults(self, init_dict: dict[str, Any] | None = None) -> None: self.side = init_dict.get("side", self.side) spec = get_side_spec(self.side) self.uid = f"BrainCoRevo1{self.side.title()}" - self.fpath = init_dict.get("fpath") or get_data_path(spec.urdf_path) + self.fpath = init_dict.get("fpath") or data.get_data_path(spec.urdf_path) self.init_pos = [0.0, 0.0, 0.3] self.control_parts = {"eef": list(spec.joint_names)} self.init_qpos = None diff --git a/pyproject.toml b/pyproject.toml index 140fdf5a9e..65ac097e65 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ dynamic = ["version"] # Core install dependencies (kept from requirements.txt). dependencies = [ - "dexsim_engine==0.5.1rc1", + "dexsim_engine==0.6.0rc1", # DexSim and LeRobot require different OpenCV distributions sharing cv2. # Keep their bindings on the same release to avoid mixed Python/native APIs. "opencv-python==4.12.0.88; python_version < '3.12'", diff --git a/tests/gen_sim/scene_engine/test_scene_usd.py b/tests/gen_sim/scene_engine/test_scene_usd.py index 4bfc291f9a..68e28d32b6 100644 --- a/tests/gen_sim/scene_engine/test_scene_usd.py +++ b/tests/gen_sim/scene_engine/test_scene_usd.py @@ -186,3 +186,23 @@ def test_usd_source_texture_retains_native_orientation(tmp_path: Path) -> None: reader = UsdShade.Shader(texture.GetInput("st").GetConnectedSource()[0].GetPrim()) assert reader.GetIdAttr().Get() == "UsdPrimvarReader_float2" assert reader.GetInput("varname").Get() == "st" + + +def test_scalar_material_export_prefers_scene_linear_base_color(): + stage = Usd.Stage.CreateInMemory() + native = _native_material() + native.get_base_color = lambda: (0.5, 0.6, 0.7) + linear_color = (0.21404114, 0.31854678, 0.44798842) + native.get_base_color_linear = lambda: linear_color + material = _export_material_without_texture(native, stage, Sdf.Path("/World")) + shader = material.ComputeSurfaceSource()[0] + np.testing.assert_allclose(shader.GetInput("diffuseColor").Get(), linear_color) + + +def test_scalar_material_export_keeps_legacy_base_color_getter(): + stage = Usd.Stage.CreateInMemory() + native = _native_material() + native.get_base_color = lambda: (0.1, 0.2, 0.3) + material = _export_material_without_texture(native, stage, Sdf.Path("/World")) + shader = material.ComputeSurfaceSource()[0] + np.testing.assert_allclose(shader.GetInput("diffuseColor").Get(), (0.1, 0.2, 0.3)) diff --git a/tests/gym/envs/task_program/test_contact_evidence.py b/tests/gym/envs/task_program/test_contact_evidence.py index 0313fad2b4..5635a6b889 100644 --- a/tests/gym/envs/task_program/test_contact_evidence.py +++ b/tests/gym/envs/task_program/test_contact_evidence.py @@ -901,6 +901,8 @@ def test_timeline_reset_matches_sensor_indices_without_double_generation( indices, generations ): source = ContactSource(count=3) + for name in ("impulse_valid", "friction_valid"): + source.data[name] = source.data["is_valid"].clone() timeline, _ = make_timeline(source) capture_step(timeline, source) before = timeline.snapshot() diff --git a/tests/gym/envs/task_program/test_simulation_scene_expansion.py b/tests/gym/envs/task_program/test_simulation_scene_expansion.py index 43804f4404..2b117cd5e7 100644 --- a/tests/gym/envs/task_program/test_simulation_scene_expansion.py +++ b/tests/gym/envs/task_program/test_simulation_scene_expansion.py @@ -687,7 +687,7 @@ def test_initial_validator_reads_contacts_without_replaying_history() -> None: selected_actor_ids=(4,), actors=(actor,), actor_info=lambda actor_id: actor, - fetch=lambda: buffer, + fetch_solver_report=lambda: buffer, ) owner = SimpleNamespace( num_envs=1, diff --git a/tests/sim/atomic_actions/test_actions.py b/tests/sim/atomic_actions/test_actions.py index 2f607c70fb..3b89ae1dd1 100644 --- a/tests/sim/atomic_actions/test_actions.py +++ b/tests/sim/atomic_actions/test_actions.py @@ -1332,7 +1332,8 @@ def instantiate(self, request, current): torch.testing.assert_close( named_plan.joint_trajectory.positions, move_plan.joint_trajectory.positions ) - unavailable_source = Mock() + unavailable_source = Source({"transport": 4}, [1, 1, 1, 1]) + unavailable_source.instantiate = Mock() with pytest.raises(ValueError, match="requires object 'other'"): _ACTION_ENGINES[id(move)].plan( replace( diff --git a/tests/sim/objects/test_deformable_object.py b/tests/sim/objects/test_deformable_object.py index 0f0a0191e9..1d4b5bff4d 100644 --- a/tests/sim/objects/test_deformable_object.py +++ b/tests/sim/objects/test_deformable_object.py @@ -188,13 +188,11 @@ def test_manager_rejects_non_particle_newton_solver(solver_type: str) -> None: sim.add_deformable_object(VolumeDeformableObjectCfg(uid="soft")) -@pytest.mark.parametrize("solver_type", ["auto", "dexuni"]) @pytest.mark.parametrize( "config_type", [SurfaceDeformableObjectCfg, VolumeDeformableObjectCfg] ) def test_manager_declares_deformables_with_supported_solver( config_type: type[DeformableObjectCfg], - solver_type: str, monkeypatch: pytest.MonkeyPatch, ) -> None: from embodichain.lab.sim.shapes import MeshCfg @@ -202,7 +200,7 @@ def test_manager_declares_deformables_with_supported_solver( sim = object.__new__(SimulationManager) sim.physics = NewtonPhysicsBackend(SimpleNamespace()) - sim.physics._configured_solver_type = solver_type + sim.physics._configured_solver_type = "dexuni" sim.device = torch.device("cuda") sim.sim_config = SimpleNamespace(physics_cfg=NewtonPhysicsCfg()) sim._deformable_objects = {} diff --git a/tests/sim/objects/test_usd.py b/tests/sim/objects/test_usd.py index 14dee60c4d..6da0e5825d 100644 --- a/tests/sim/objects/test_usd.py +++ b/tests/sim/objects/test_usd.py @@ -16,6 +16,7 @@ from __future__ import annotations +import math from pathlib import Path import pytest @@ -243,7 +244,9 @@ def test_usd_properties(self): print(f"All joint stiffness: {stiffness}") print(f"All joint damping: {damping}") print(f"All joint max force: {max_force}") - expected_stiffness = 10000000.0 + # USD angular drives are authored per degree; DexSim reports the + # equivalent per-radian stiffness in its joint-state API. + expected_stiffness = 10000000.0 * 180.0 / math.pi assert torch.allclose( stiffness, torch.tensor(expected_stiffness, dtype=torch.float32) ) @@ -256,7 +259,8 @@ def test_usd_properties(self): joint_idx = joint_names.index(target_joint_name) # check for the first instance assert torch.isclose( - stiffness[0, joint_idx], torch.tensor(10000000.0, dtype=torch.float32) + stiffness[0, joint_idx], + torch.tensor(expected_stiffness, dtype=torch.float32), ) assert torch.isclose( damping[0, joint_idx], torch.tensor(0.0, dtype=torch.float32) diff --git a/tests/sim/robots/test_brainco_revo1.py b/tests/sim/robots/test_brainco_revo1.py index 179ddde3f7..7c29ba9c1a 100644 --- a/tests/sim/robots/test_brainco_revo1.py +++ b/tests/sim/robots/test_brainco_revo1.py @@ -90,7 +90,7 @@ def local_path(reference: str) -> str: side = "left" if "LeftHand" in reference else "right" return str(paths[side]) - monkeypatch.setattr(revo1, "get_data_path", local_path) + monkeypatch.setattr(revo1.data, "get_data_path", local_path) return paths @@ -160,7 +160,7 @@ def test_revo1_defaults_overrides_and_instances_are_isolated( def unexpected_download(reference: str) -> str: raise AssertionError(f"Local fpath must bypass downloads: {reference}") - monkeypatch.setattr(revo1, "get_data_path", unexpected_download) + monkeypatch.setattr(revo1.data, "get_data_path", unexpected_download) custom = resolve_robot_cfg( "BrainCoRevo1", { @@ -242,6 +242,11 @@ def test_standalone_and_mounted_hand_control(self, selection: str) -> None: else: cfg = resolve_robot_cfg("BrainCoRevo1", variant=selection) part = "eef" + source_joints = { + joint.get("name") + for joint in ET.parse(cfg.fpath).getroot().findall("joint") + } + assert set(cfg.control_parts[part]) <= source_joints, cfg.fpath robot = self.sim.add_robot(cfg=cfg) self.sim.prepare() initial_qpos = robot.get_qpos().clone() diff --git a/tests/sim/sensors/test_contact_history.py b/tests/sim/sensors/test_contact_history.py index 63309333ec..00a133c444 100644 --- a/tests/sim/sensors/test_contact_history.py +++ b/tests/sim/sensors/test_contact_history.py @@ -83,6 +83,36 @@ def test_landing_retains_completed_air_time_until_next_landing(): assert not history.first_contact.item() +@pytest.mark.parametrize("compact", [False, True]) +def test_impulse_duration_does_not_shorten_history_clocks(compact): + from dexsim.scene import ContactBuffer + + history = ContactHistory(torch.tensor([[10]]), force_threshold=1.0) + data = sample([[[0, 10]]]) + data["impulse"].fill_(0.00981) + buffer = ContactBuffer.allocate(1, "cpu") + buffer.data.zero_() + buffer.data[:, 5] = 1.0 + buffer.data[:, 9] = 0.00981 + buffer.actor_ids[:] = torch.tensor([[0, 10]]) + buffer.env_ids.zero_() + buffer.quantity_valid.fill_(True) + + for contact in (False, False, True): + data["is_valid"].fill_(contact) + buffer.device_counts[0] = int(contact) + if compact: + history._update_from_query(buffer, 0.01, impulse_dt=0.001) + else: + history.update(data, 0.01, impulse_dt=0.001) + if not contact: + assert not history.found.any() + assert history.found.item() + assert history.force[0, 0, 2].item() == pytest.approx(9.81) + assert history.current_air_time.item() == 0.0 + assert history.last_air_time.item() == pytest.approx(0.03) + + def test_selective_reset_preserves_every_untouched_history_buffer(): history = ContactHistory(torch.tensor([[10], [20]])) history.update(sample([[[0, 10]], [[0, 20]]], valid=False), 0.1) @@ -124,6 +154,8 @@ def test_sparse_rows_use_device_count_and_environment_qualified_actor_ids(device counterpart_ids=torch.tensor([[0], [0]], device=device), ) buffer = ContactBuffer.allocate(6, device) + if hasattr(buffer, "quantity_valid"): + buffer.quantity_valid.fill_(True) buffer.data.fill_(float("nan")) buffer.data[:4] = torch.tensor( [ @@ -146,3 +178,32 @@ def test_sparse_rows_use_device_count_and_environment_qualified_actor_ids(device ) assert history.contact.tolist() == [[True, False], [False, True]] assert history.contact_count.tolist() == [1.0, 1.0] + + +@pytest.mark.parametrize("threshold,found", [(0.0, True), (1.0, False)]) +def test_geometry_only_rows_do_not_create_force_or_force_threshold_events( + threshold, found +): + history = ContactHistory(torch.tensor([[10]]), force_threshold=threshold) + data = sample([[[0, 10]]]) + data["impulse_valid"] = torch.zeros((1, 1), dtype=torch.bool) + data["friction_valid"] = torch.zeros((1, 1), dtype=torch.bool) + data["impulse"].fill_(float("nan")) + data["friction"].fill_(float("nan")) + history.update(data, 0.1) + assert not history.force.any() + assert history.found.item() == found + + +@pytest.mark.parametrize( + "pair,expected", [((10, 0), (5.0, 0.0, -20.0)), ((0, 10), (-5.0, 0.0, 20.0))] +) +def test_force_preserves_friction_impulse_on_actor_zero( + pair: tuple[int, int], expected: tuple[float, float, float] +) -> None: + history = ContactHistory(torch.tensor([[10]])) + data = sample([[pair]]) + data["impulse"][0, 0] = 2.0 + data["friction"][0, 0] = torch.tensor([0.5, 0.0, 0.0]) + history.update(data, 0.1) + torch.testing.assert_close(history.force[0, 0], torch.tensor(expected)) diff --git a/tests/sim/sensors/test_contact_query_sensor.py b/tests/sim/sensors/test_contact_query_sensor.py index 0bdfb74a7c..66c87b8b16 100644 --- a/tests/sim/sensors/test_contact_query_sensor.py +++ b/tests/sim/sensors/test_contact_query_sensor.py @@ -42,6 +42,8 @@ def __init__(self) -> None: self.available = True self.buffer = ContactBuffer.allocate(4, "cpu") self.buffer.count = 2 + if hasattr(self.buffer, "quantity_valid"): + self.buffer.quantity_valid.fill_(True) self.buffer.data[0] = torch.tensor( [1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 0.1, 0.2, 0.3, 0.4, -0.01] ) @@ -55,7 +57,7 @@ def actor_info(self, actor_id: int) -> ContactActorInfo: self.actor_info_calls += 1 return self._actors[actor_id] - def fetch(self) -> ContactBuffer: + def fetch_solver_report(self) -> ContactBuffer: assert self.available, "SDK contact snapshot is unavailable after reset" self.fetch_calls += 1 return self.buffer @@ -163,6 +165,18 @@ def _sensor_for_query(query, capacity=2): ) +def test_contact_report_errors_other_than_reset_are_not_hidden(): + query = _FakeQuery() + sensor = _sensor_for_query(query) + + def fail(): + raise RuntimeError("contact device failure") + + query.fetch_solver_report = fail + with pytest.raises(RuntimeError, match="contact device failure"): + sensor.update() + + def test_default_history_enables_substep_sampling_and_preserves_interval_drops(): query = _FakeQuery() sensor = _sensor_for_query(query) @@ -220,6 +234,104 @@ def test_scatter_overflow_is_counted_separately_and_reset_by_selected_row(): assert sensor.dropped_contacts == 0 +@pytest.mark.parametrize( + "device", ["cpu", pytest.param("cuda:0", marks=pytest.mark.gpu)] +) +def test_mixed_contact_quantities_survive_scatter_filter_and_history(device): + query = _FakeQuery() + if not hasattr(query.buffer, "quantity_valid"): + pytest.skip("Per-row contact quantities require DexSim's current dev API.") + query.capabilities = ContactQueryCapabilities(True, False, False) + query.buffer.quantity_valid[:2] = torch.tensor([[True, False], [False, True]]) + # Poison unsupported quantities; neither the observation mask nor the + # history reducer may promote them to measured forces. + query.buffer.data[0, 6:9] = float("nan") + query.buffer.data[1, 9] = float("nan") + query.buffer.data[1, 6:9] = torch.tensor([0.0, 0.5, 0.0]) + if device != "cpu": + old = query.buffer + query.buffer = ContactBuffer.allocate(old.capacity, device) + for name in ("data", "actor_ids", "env_ids", "quantity_valid"): + getattr(query.buffer, name).copy_(getattr(old, name)) + query.buffer.count = old.count + query.buffer.device_counts[0] = old.count + query.fetch_solver_report_async = query.fetch_solver_report + handles = ( + SimpleNamespace(path="arena_0/cube"), + SimpleNamespace(path="arena_1/cube"), + ) + owner = SimpleNamespace( + num_envs=2, + physics_step_index=0, + arena_offsets=torch.zeros((2, 3), device=device), + _spawn_scene=SimpleNamespace(handles=lambda uid: handles), + spawn_result=SimpleNamespace( + create_contact_query=lambda *args, **kwargs: query + ), + ) + sensor = ContactSensor( + ContactSensorCfg( + uid="contacts", + rigid_uid_list=["cube"], + filter_need_both_actor=False, + max_contacts_per_env=2, + ), + torch.device(device), + owner=owner, + ) + history = sensor.create_history( + "forces", torch.tensor([[4], [7]], device=device), force_threshold=1.0 + ) + sensor.update_physics_step(0.1) + data = sensor.get_data() + assert data["impulse_valid"].cpu().tolist() == [[True, False], [False, False]] + assert data["friction_valid"].cpu().tolist() == [[False, False], [True, False]] + torch.testing.assert_close( + history.force.cpu(), torch.tensor([[[0.0, 0.0, -4.0]], [[0.0, -5.0, 0.0]]]) + ) + assert history.contact.all() + filtered = sensor.filter_by_user_ids(torch.tensor([4])) + assert filtered["impulse_valid"].tolist() == [True] + assert filtered["friction_valid"].tolist() == [False] + sensor.reset([0]) + assert not data["impulse_valid"].any() + assert data["friction_valid"][1, 0] + query.buffer.count = 0 + query.buffer.device_counts.zero_() + sensor.update() + assert not data["is_valid"].any() + assert not data["friction_valid"].any() + + +def test_legacy_contact_buffer_uses_query_quantity_capabilities(): + query = _FakeQuery() + old = query.buffer + query.buffer = SimpleNamespace( + data=old.data, + actor_ids=old.actor_ids, + env_ids=old.env_ids, + device_counts=old.device_counts, + count_device=old.count_device, + dropped_count_device=old.dropped_count_device, + capacity=old.capacity, + count=old.count, + dropped_count=old.dropped_count, + ) + query.capabilities = ContactQueryCapabilities(True, False, False) + query.buffer.data[:2, 6:10] = float("nan") + sensor = _sensor_for_query(query) + history = sensor.create_history( + "forces", torch.tensor([[4], [7]]), force_threshold=1.0 + ) + sensor.update_physics_step(0.1) + data = sensor.get_data() + assert data["is_valid"][:, 0].all() + assert not data["impulse_valid"].any() + assert not data["friction_valid"].any() + assert not history.force.any() + assert not history.found.any() + + @pytest.mark.parametrize("with_history", [False, True]) def test_sampling_observers_are_instance_owned_and_bracket_native_fetch(with_history): query = _FakeQuery() @@ -228,13 +340,13 @@ def test_sampling_observers_are_instance_owned_and_bracket_native_fetch(with_his sensor.create_history("existing_history", torch.tensor([[4], [7]])) other = _sensor_for_query(_FakeQuery()) events = [] - original_fetch = query.fetch + original_fetch = query.fetch_solver_report - def fetch(): - events.append("fetch") + def fetch_solver_report(): + events.append("fetch_solver_report") return original_fetch() - query.fetch = fetch + query.fetch_solver_report = fetch_solver_report observer = SimpleNamespace( begin_control_step=lambda: events.append("begin"), before_sample=lambda: (events.append("before"), "stamp")[1], @@ -254,7 +366,7 @@ def fetch(): assert events == [ "begin", "before", - "fetch", + "fetch_solver_report", ("after", 0.01, "stamp", 2), ("reset", [0]), ] @@ -269,7 +381,7 @@ def fetch(): events.clear() sensor._sim.physics_step_index += 1 sensor.update_physics_step(0.01) - assert events == ["fetch"] + assert events == ["fetch_solver_report"] def test_sampling_observers_reject_external_capture_before_callbacks(monkeypatch): @@ -299,17 +411,17 @@ def test_contact_sampler_normal_update_fetches_once_before_reading_data(): query = _FakeQuery() sensor = _sensor_for_query(query) calls = [] - original = query.fetch + original = query.fetch_solver_report - def fetch(): - calls.append("fetch") + def fetch_solver_report(): + calls.append("fetch_solver_report") return original() - query.fetch = fetch + query.fetch_solver_report = fetch_solver_report sample = ContactEvidenceSampler(sensor).sample( PhysicalEvidenceRequest(0.0, 0, torch.tensor([1, 0])) ) - assert calls == ["fetch"] + assert calls == ["fetch_solver_report"] assert sample.frame.get(CONTACT_VALID_MASK).values.sum().item() == 2 assert all(batch.valid.all() for batch in sample.frame.batches.values()) @@ -528,7 +640,7 @@ def before(): @pytest.mark.parametrize("loss", ["none", "query", "scatter"]) def test_first_standalone_sample_with_observer_preserves_loss_unavailability(loss): - """The observer's interval counter has not yet seen this standalone fetch.""" + """The observer's interval counter has not yet seen this standalone fetch_solver_report.""" from embodichain.lab.sim.atomic_actions.evidence import PhysicalEvidenceRequest from embodichain.lab.sim.sensors.contact.sampling import ( ContactEvidenceSampler, @@ -554,13 +666,13 @@ def test_first_standalone_sample_with_observer_preserves_loss_unavailability(los query.buffer.actor_ids[:3] = torch.tensor([[4, -1], [4, -1], [7, -1]]) query.buffer.data[2] = query.buffer.data[1] fetches = [] - original_fetch = query.fetch + original_fetch = query.fetch_solver_report - def fetch(): + def fetch_solver_report(): fetches.append(True) return original_fetch() - query.fetch = fetch + query.fetch_solver_report = fetch_solver_report sample = ContactEvidenceSampler(sensor).sample( PhysicalEvidenceRequest(1.0, 0, torch.tensor([1, 0])) ) diff --git a/tests/sim/sensors/test_contact_sensor_graph.py b/tests/sim/sensors/test_contact_sensor_graph.py index ff6d66a869..2241692cbb 100644 --- a/tests/sim/sensors/test_contact_sensor_graph.py +++ b/tests/sim/sensors/test_contact_sensor_graph.py @@ -37,6 +37,8 @@ def __init__(self, device: torch.device) -> None: ContactActorInfo(20, "arena_1/cube", None, "arena_1", 1), ) self.buffer = ContactBuffer.allocate(4, str(device)) + if hasattr(self.buffer, "quantity_valid"): + self.buffer.quantity_valid.fill_(True) self.buffer.data.zero_() self.buffer.data[:2, 5] = 1.0 self.buffer.data[:2, 9] = 0.2 @@ -49,7 +51,7 @@ def __init__(self, device: torch.device) -> None: def actor_info(self, actor_id: int) -> ContactActorInfo: return next(actor for actor in self.actors if actor.actor_id == actor_id) - def fetch_async(self) -> ContactBuffer: + def fetch_solver_report_async(self) -> ContactBuffer: return self.buffer @@ -164,6 +166,34 @@ def assert_air_time(value: float) -> None: assert sensor._sample_graph is recaptured assert not history.contact.any() assert_air_time(0.3) + + if hasattr(query.buffer, "quantity_valid"): + # Replay reads changed masks without recapture and never evaluates + # unavailable poisoned force components. + query.buffer.quantity_valid.zero_() + query.buffer.data[:2, 6:10] = float("nan") + sample() + assert sensor._sample_graph is recaptured + assert not history.force.any() + assert not sensor.get_data()["impulse_valid"].any() + assert not sensor.get_data()["friction_valid"].any() + + # A topology refresh can replace the native mask storage. The + # captured pointer must be refreshed before the next sample. + query.buffer.quantity_valid = torch.ones_like(query.buffer.quantity_valid) + query.buffer.data[:2, 6:9] = 0.0 + query.buffer.data[:2, 9] = 0.4 + sample() + assert sensor._sample_graph is None + assert history.contact.all() + torch.testing.assert_close( + history.force, + torch.tensor([[[0.0, 0.0, 4.0]], [[0.0, 0.0, 4.0]]], device=device), + ) + sample() + assert sensor._sample_graph is not None + assert sensor._sample_graph is not recaptured + assert history.contact.all() # The graph writes query loss even when there are no valid contacts. query.buffer.device_counts.zero_() query.buffer.device_counts[1] = 3 @@ -236,115 +266,3 @@ def sample() -> None: torch.tensor([[[0.0, 0.0, 2.0]], [[0.0, 0.0, 2.0]]], device=device), ) producer.synchronize() - - -@pytest.mark.parametrize("history_sample", [False, True]) -@pytest.mark.parametrize("capture_after_advance", [False, True]) -@pytest.mark.parametrize("capture_mode", ["same_stream", "other_stream", "lifecycle"]) -def test_outer_capture_preserves_sampling_and_eager_refresh( - history_sample: bool, capture_after_advance: bool, capture_mode: str -) -> None: - """Warmup must not suppress captured fetch/scatter/history or eager refresh.""" - wp.init() - device = torch.device("cuda:0") - query = _CudaQuery(device) - source = query.buffer.data.clone() - - def fetch_async() -> ContactBuffer: - # Model a backend whose query must copy fresh device data on each replay. - query.buffer.data.copy_(source) - return query.buffer - - query.fetch_async = fetch_async - owner = SimpleNamespace( - num_envs=2, - physics_step_index=0, - arena_offsets=torch.zeros((2, 3), device=device), - _spawn_scene=SimpleNamespace( - handles=lambda uid: tuple( - SimpleNamespace(path=actor.path) for actor in query.actors - ) - ), - spawn_result=SimpleNamespace(create_contact_query=lambda *a, **kw: query), - ) - sensor = ContactSensor( - ContactSensorCfg( - uid="contacts", rigid_uid_list=["cube"], max_contacts_per_env=2 - ), - device, - owner=owner, - ) - history = ( - sensor.create_history("feet", torch.tensor([[10], [20]], device=device)) - if history_sample - else None - ) - - def sample() -> None: - if history_sample: - sensor.update_physics_step(0.1) - else: - sensor.update() - - def assert_sample(impulse: float) -> None: - assert sensor.get_data()["is_valid"].tolist() == [[True, False], [True, False]] - torch.testing.assert_close( - sensor.get_data()["impulse"][:, 0], - torch.full_like(sensor.get_data()["impulse"][:, 0], impulse), - ) - if history is not None: - torch.testing.assert_close( - history.force, - torch.tensor( - [[[0.0, 0.0, impulse / 0.1]], [[0.0, 0.0, impulse / 0.1]]], - device=device, - ), - ) - - stream = torch.cuda.Stream(device=device) - stream.wait_stream(torch.cuda.current_stream(device)) - producer = ( - stream if capture_mode == "same_stream" else torch.cuda.Stream(device=device) - ) - producer.wait_stream(torch.cuda.current_stream(device)) - with torch.cuda.stream(producer): - sample() - # Even a completed producer must not introduce an uncaptured event into capture. - producer.synchronize() - with torch.cuda.stream(stream): - if capture_after_advance: - owner.physics_step_index += 1 - graph = torch.cuda.CUDAGraph() - with torch.cuda.graph(graph, stream=stream): - if capture_mode == "lifecycle": - sensor.reset() - sensor.begin_control_step() - sample() - # Capture has not executed a new sample. Same-step eager calls must work. - query.buffer.device_counts[0] = 2 - source[:2, 9] = 0.3 - sample() - assert_sample(0.3) - sensor.reset() - for impulse in (0.2, 0.5): - owner.physics_step_index += 1 - source[:2, 9] = impulse - graph.replay() - assert_sample(impulse) - if history is not None: - torch.testing.assert_close( - history.contact_count, - torch.full_like( - history.contact_count, 1 if capture_mode == "lifecycle" else 2 - ), - ) - # Caller-owned replay must restore latest loss after full reset too. - query.buffer.device_counts[0] = 0 - for loss in (3, 0): - sensor.reset() - assert sensor.evidence_dropped_contacts == 0 - query.buffer.device_counts[1] = loss - graph.replay() - assert sensor.latest_sample_dropped_contacts == loss - assert sensor.evidence_dropped_contacts == loss - stream.synchronize() diff --git a/tests/sim/sensors/test_support_observation.py b/tests/sim/sensors/test_support_observation.py index 710c4d64c4..433bdb472a 100644 --- a/tests/sim/sensors/test_support_observation.py +++ b/tests/sim/sensors/test_support_observation.py @@ -108,7 +108,7 @@ def __init__(self, count: int) -> None: def actor_info(self, actor_id): return next(actor for actor in self.actors if actor.actor_id == actor_id) - def fetch(self): + def fetch_solver_report(self): self.fetches += 1 self.on_fetch() return self.buffer diff --git a/tests/sim/sensors/test_support_observation_native.py b/tests/sim/sensors/test_support_observation_native.py index 6d44810d30..fb445e0097 100644 --- a/tests/sim/sensors/test_support_observation_native.py +++ b/tests/sim/sensors/test_support_observation_native.py @@ -87,13 +87,13 @@ def test_default_boxes_preserve_native_support_witness_and_clock_domains(): clock = EnvironmentStepClock(0.04) observation.bind_clock(clock) calls = [] - fetch = observation.sensor._query.fetch + fetch = observation.sensor._query.fetch_solver_report def counted_fetch(): calls.append(True) return fetch() - observation.sensor._query.fetch = counted_fetch + observation.sensor._query.fetch_solver_report = counted_fetch def window(): before = len(calls) diff --git a/tests/sim/spawn/test_descriptors.py b/tests/sim/spawn/test_descriptors.py index e441bfc913..835e05081e 100644 --- a/tests/sim/spawn/test_descriptors.py +++ b/tests/sim/spawn/test_descriptors.py @@ -648,6 +648,39 @@ def test_newton_contact_dimension_survives_config_and_descriptor(condim: int) -> assert descriptor.collisions[0].newton.condim == condim +@pytest.mark.parametrize("solref", [(0.02, 1.0), (-1000.0, -20.0)]) +def test_newton_raw_contact_parameters_survive_config_and_descriptor( + solref: tuple[float, float], +) -> None: + solimp = (0.9, 0.95, 0.001, 0.5, 2.0) + cfg = RigidObjectCfg.from_dict( + { + "uid": "cube", + "shape": {"shape_type": "Cube", "size": [0.1, 0.1, 0.1]}, + "attrs": { + "collision_props": {"contype": 2, "conaffinity": 4}, + "material_props": { + "backend": "newton", + "solref": list(solref), + "solimp": list(solimp), + "solmix": 0.5, + }, + }, + } + ) + descriptor, _ = rigid_desc_from_cfg( + RigidObjectCfg.from_dict(cfg.to_dict()), newton_solver_type="mujoco_warp" + ) + native = descriptor.collisions[0].newton + assert native.contype == 2 + assert native.conaffinity == 4 + assert native.solref == pytest.approx(solref) + assert native.solimp == pytest.approx(solimp) + assert native.solmix == pytest.approx(0.5) + assert NewtonCollisionPropertiesCfg().contype is None + assert NewtonRigidBodyMaterialCfg().solref is None + + def test_procedural_rigid_collision_defaults_compile_to_both_backends() -> None: cfg = RigidObjectCfg( uid="cube", @@ -1466,7 +1499,7 @@ def test_non_mode_aware_newton_position_fallback_is_explicit() -> None: configure_articulation_desc(descriptor, cfg, newton_solver_type="xpbd") -def test_auto_solver_defers_position_mode_compatibility_warning() -> None: +def test_dexuni_solver_consumes_position_mode_without_warning() -> None: cfg = ArticulationCfg( uid="robot", fpath="robot.urdf", @@ -1481,7 +1514,7 @@ def test_auto_solver_defers_position_mode_compatibility_warning() -> None: with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") - configure_articulation_desc(descriptor, cfg, newton_solver_type="auto") + configure_articulation_desc(descriptor, cfg, newton_solver_type="dexuni") assert not caught diff --git a/tests/sim/spawn/test_source.py b/tests/sim/spawn/test_source.py index 9e5d6bba4b..9756953ba9 100644 --- a/tests/sim/spawn/test_source.py +++ b/tests/sim/spawn/test_source.py @@ -22,13 +22,15 @@ import numpy as np import pytest -from dexsim.spawn import ArticulationDesc, LinkDesc +from dexsim.spawn import ArticulationDesc, JointDesc, LinkDesc +from dexsim.engine import JointType from embodichain.lab.sim.spawn.source import ( _apply_dexsim_source_overlay, _capture_dexsim_source_physics, _clear_invalid_source_com, _retain_dexsim_source_descriptor, + resolve_articulation_source, ) pytestmark = pytest.mark.no_sim @@ -38,6 +40,58 @@ def _link(name: str) -> LinkDesc: return LinkDesc(name, "", np.eye(4, dtype=np.float32)) +@pytest.mark.parametrize("authored_limits", [None, (-1.0, 1.0)]) +def test_urdf_continuous_joint_retains_unbounded_source_limits( + monkeypatch: pytest.MonkeyPatch, tmp_path, authored_limits +) -> None: + from embodichain.lab.sim.spawn import source + from dexsim.spawn.adapters import newton_articulation_adapter + + path = tmp_path / "continuous.urdf" + path.write_text('', encoding="utf-8") + continuous = JointDesc("spinner", "base", "wheel", JointType.CONTINUOUS) + if authored_limits is not None: + continuous.lower_limit, continuous.upper_limit = authored_limits + descriptor = ArticulationDesc( + name="source", + urdf_path=str(path), + joints=[] if authored_limits is None else [continuous], + ) + limited = JointDesc( + "locked", + "base", + "other", + JointType.REVOLUTE, + lower_limit=0.0, + upper_limit=0.0, + ) + skeleton = SimpleNamespace( + set_name=lambda _: None, + detach_parent=lambda: None, + load_urdf=lambda *_: 0, + get_link_names=lambda _: (), + ) + arena = SimpleNamespace( + create_skeleton=lambda _: skeleton, + remove_skeleton=lambda _: None, + ) + monkeypatch.setattr(source, "_source_arena", lambda *_: arena) + + def translate(_skeleton, desc): + if not desc.joints: + continuous.lower_limit = continuous.upper_limit = 0.0 + desc.joints = [continuous, limited] + + monkeypatch.setattr( + newton_articulation_adapter, "_translate_urdf_articulation", translate + ) + resolve_articulation_source(SimpleNamespace(), descriptor) + + expected = (-np.inf, np.inf) if authored_limits is None else authored_limits + assert (continuous.lower_limit, continuous.upper_limit) == expected + assert (limited.lower_limit, limited.upper_limit) == (0.0, 0.0) + + def _physical_attr( mass: float, inertia: tuple[float, float, float], diff --git a/tests/sim/test_backend_parity.py b/tests/sim/test_backend_parity.py index c5fabe1ce2..68a0a9f443 100644 --- a/tests/sim/test_backend_parity.py +++ b/tests/sim/test_backend_parity.py @@ -123,7 +123,7 @@ def test_contact_sensor_guard_uses_backend_capability() -> None: [ ("mujoco_warp", "cuda:0", True), ("mujoco_warp", "cpu", False), - ("dexuni", "cuda:0", False), + ("dexuni", "cuda:0", True), ("xpbd", "cpu", True), ], ) diff --git a/tests/sim/test_cfg.py b/tests/sim/test_cfg.py index c63dd25ad0..971838a70f 100644 --- a/tests/sim/test_cfg.py +++ b/tests/sim/test_cfg.py @@ -596,6 +596,10 @@ def names(config_type: type) -> set[str]: "is_site", # Asset-loading adapter option, not a native ShapeConfig parameter. "use_native_mesh_loader", + "convert_to_heightfield", + "heightfield_resolution", + "preserve_mesh_transform", + "max_hull_vertices", # Mesh cooking is outside rigid-body physics. } assert ( newton_fields == names(NewtonCollisionDesc) - intentionally_unowned_shape_fields @@ -792,7 +796,7 @@ def test_robot_preset_accepts_newton_solver_alias() -> None: preset = _NewtonSolverAliasRobotPresetCfg() assert preset.resolve(DefaultPhysicsCfg()).uid == "fallback" - assert preset.resolve(NewtonPhysicsCfg()).uid == "fallback" + assert preset.resolve(NewtonPhysicsCfg()).uid == "mjwarp" assert ( preset.resolve(NewtonPhysicsCfg(solver_cfg={"solver_type": "mjwarp"})).uid == "mjwarp" diff --git a/tests/sim/test_sim_manager_cfg.py b/tests/sim/test_sim_manager_cfg.py index 6adf8c2945..cb2fce6578 100644 --- a/tests/sim/test_sim_manager_cfg.py +++ b/tests/sim/test_sim_manager_cfg.py @@ -124,15 +124,15 @@ def test_newton_physics_cfg_uses_device() -> None: @pytest.mark.no_sim -def test_newton_physics_cfg_preserves_dexsim_auto_solver_default() -> None: - from dexsim.engine.newton_physics import AutoSolverCfg +def test_newton_physics_cfg_preserves_dexsim_dexuni_default() -> None: + from dexsim.engine.newton_physics import DexUniSolverCfg cfg = NewtonPhysicsCfg() dexsim_cfg = cfg.to_dexsim_cfg(gpu_id=0) - assert isinstance(dexsim_cfg.solver_cfg, AutoSolverCfg) - assert dexsim_cfg.solver_cfg.solver_type == "auto" + assert isinstance(dexsim_cfg.solver_cfg, DexUniSolverCfg) + assert dexsim_cfg.solver_cfg.solver_type == "dexuni" @pytest.mark.no_sim @@ -158,23 +158,23 @@ def test_newton_physics_cfg_forwards_render_sync_policy( @pytest.mark.no_sim -def test_newton_physics_cfg_requires_dexsim_auto_solver_api( +def test_newton_physics_cfg_requires_dexsim_dexuni_api( monkeypatch: pytest.MonkeyPatch, ) -> None: from dexsim.engine import newton_physics - monkeypatch.delattr(newton_physics, "AutoSolverCfg") + monkeypatch.delattr(newton_physics, "DexUniSolverCfg") with pytest.raises( ImportError, - match="cannot import name 'AutoSolverCfg'", + match="cannot import name 'DexUniSolverCfg'", ): NewtonPhysicsCfg().to_dexsim_cfg(gpu_id=0) @pytest.mark.no_sim -def test_newton_gradient_mode_rejects_auto_solver() -> None: - cfg = NewtonPhysicsCfg(requires_grad=True) +def test_newton_gradient_mode_rejects_default_solver() -> None: + cfg = NewtonPhysicsCfg(solver_cfg=None, requires_grad=True) with pytest.raises(RuntimeError, match="explicit.*semi_implicit"): cfg.to_dexsim_cfg(gpu_id=0) @@ -193,7 +193,9 @@ def test_newton_physics_cfg_passes_warp_log_suppression() -> None: def test_newton_physics_cfg_forwards_collision_pipeline_update_interval( update_interval: int | None, ) -> None: - cfg = NewtonPhysicsCfg(collision_cfg={"update_interval": update_interval}) + cfg = NewtonPhysicsCfg( + solver_cfg=None, collision_cfg={"update_interval": update_interval} + ) dexsim_cfg = cfg.to_dexsim_cfg(gpu_id=0) @@ -292,15 +294,15 @@ def test_newton_backend_exposes_resolved_solver_type() -> None: @pytest.mark.no_sim -def test_newton_backend_reports_scene_resolved_auto_solver( +def test_newton_backend_reports_runtime_solver( monkeypatch: pytest.MonkeyPatch, ) -> None: world = object() - native_backend = SimpleNamespace(solver_type="mujoco_warp") + native_backend = SimpleNamespace(solver_type="xpbd") manager = SimpleNamespace(_world=world) backend = NewtonPhysicsBackend(manager) world_config = SimpleNamespace(newton_cfg=None) - sim_config = SimulationManagerCfg(physics_cfg=NewtonPhysicsCfg()) + sim_config = SimulationManagerCfg(physics_cfg=NewtonPhysicsCfg(solver_cfg=None)) monkeypatch.setattr( "dexsim.engine.newton_physics.backend_registry.get_newton_backend", lambda candidate: native_backend if candidate is world else None, @@ -308,8 +310,8 @@ def test_newton_backend_reports_scene_resolved_auto_solver( backend.configure_world(world_config, sim_config) - assert world_config.newton_cfg.solver_cfg.solver_type == "auto" - assert backend.solver_type == "mujoco_warp" + assert world_config.newton_cfg.solver_cfg.solver_type == "dexuni" + assert backend.solver_type == "xpbd" def test_newton_teardown_releases_render_views_on_the_resolved_device( @@ -450,14 +452,44 @@ def test_newton_physics_cfg_converts_mapping_solver_cfg_to_dexsim_cfg() -> None: @pytest.mark.no_sim -def test_newton_physics_cfg_accepts_explicit_auto_solver_mapping() -> None: - from dexsim.engine.newton_physics import AutoSolverCfg +@pytest.mark.parametrize( + "solver_mapping", + [None, {}], +) +def test_newton_physics_cfg_preserves_automatic_dexuni_defaults(solver_mapping) -> None: + from dexsim.engine.newton_physics import DexUniSolverCfg - cfg = NewtonPhysicsCfg(solver_cfg={"class_type": "AutoSolverCfg"}) + cfg = NewtonPhysicsCfg(solver_cfg=solver_mapping) dexsim_cfg = cfg.to_dexsim_cfg(gpu_id=0) - assert isinstance(dexsim_cfg.solver_cfg, AutoSolverCfg) + assert isinstance(dexsim_cfg.solver_cfg, DexUniSolverCfg) + assert dexsim_cfg.solver_cfg.contact_mode == "auto" + assert dexsim_cfg.solver_cfg.mujoco_articulations is None + + +@pytest.mark.no_sim +@pytest.mark.parametrize( + "solver_mapping", + [ + {"solver_type": "unknown_solver"}, + {"class_type": "UnknownSolverCfg"}, + ], +) +def test_newton_physics_cfg_rejects_unknown_solver_names(solver_mapping) -> None: + with pytest.raises(RuntimeError, match="Unsupported Newton solver type"): + NewtonPhysicsCfg(solver_cfg=solver_mapping).to_dexsim_cfg(gpu_id=0) + + +@pytest.mark.no_sim +@pytest.mark.parametrize( + "field,value", [("iterations", 8), ("step_rigid_bodies", False)] +) +def test_dexuni_rejects_legacy_flat_options(field, value) -> None: + with pytest.raises(TypeError, match=field): + NewtonPhysicsCfg( + solver_cfg={"solver_type": "dexuni", field: value} + ).to_dexsim_cfg(gpu_id=0) @pytest.mark.no_sim @@ -474,8 +506,9 @@ def test_newton_physics_cfg_accepts_dexuni_solver_mapping( cfg = NewtonPhysicsCfg( solver_cfg={ config_key: solver_name, - "iterations": 12, - "step_rigid_bodies": False, + "joint_mode": "kinematic", + "contact_mode": "auto", + "vbd_options": {"iterations": 12}, } ) @@ -483,8 +516,10 @@ def test_newton_physics_cfg_accepts_dexuni_solver_mapping( assert isinstance(dexsim_cfg.solver_cfg, DexUniSolverCfg) assert dexsim_cfg.solver_cfg.solver_type == "dexuni" - assert dexsim_cfg.solver_cfg.iterations == 12 - assert dexsim_cfg.solver_cfg.step_rigid_bodies is False + assert dexsim_cfg.solver_cfg.joint_mode == "kinematic" + assert dexsim_cfg.solver_cfg.contact_mode == "auto" + assert dexsim_cfg.solver_cfg.mujoco_articulations is None + assert dexsim_cfg.solver_cfg.vbd_options["iterations"] == 12 @pytest.mark.no_sim @@ -499,3 +534,65 @@ def test_newton_physics_cfg_directly_accepts_dexsim_solver_cfg_object() -> None: assert isinstance(dexsim_cfg.solver_cfg, XPBDSolverCfg) assert dexsim_cfg.solver_cfg.iterations == 8 assert dexsim_cfg.solver_cfg.enable_restitution is True + + +@pytest.mark.no_sim +def test_newton_physics_cfg_forwards_two_way_coupling() -> None: + cfg = NewtonPhysicsCfg( + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "joint_mode": "dynamic", + "coupling": "two_way", + "coupling_options": { + "mass_scale": 4.0, + "proxy_relaxation": 0.5, + "iterations": 2, + }, + "mujoco_articulation_paths": ("arena_0/robot",), + "vbd_options": {"iterations": 10}, + }, + ) + native = cfg.to_dexsim_cfg(gpu_id=0) + assert native.collision_pipeline_cfg is None + assert native.solver_cfg.joint_mode == "dynamic" + assert native.solver_cfg.coupling == "two_way" + assert native.solver_cfg.coupling_options == cfg.solver_cfg["coupling_options"] + assert native.solver_cfg.mujoco_articulation_paths == ("arena_0/robot",) + assert native.solver_cfg.vbd_options == {"iterations": 10} + + +@pytest.mark.no_sim +@pytest.mark.parametrize( + ("options", "error"), + [ + ({"joint_mode": "kinematic"}, "requires joint_mode='dynamic'"), + ({"coupling_options": {"mass_scale": 0.0}}, "mass_scale"), + ({"coupling_options": {"proxy_relaxation": 1.1}}, "proxy_relaxation"), + ({"coupling_options": {"iterations": 0}}, "iterations"), + ({"coupling_options": {"unknown": 1.0}}, "Unsupported"), + ], +) +def test_newton_physics_cfg_preserves_native_two_way_validation(options, error) -> None: + cfg = NewtonPhysicsCfg( + solver_cfg={"solver_type": "dexuni", "coupling": "two_way", **options} + ) + with pytest.raises(ValueError, match=error): + cfg.to_dexsim_cfg(gpu_id=0) + + +@pytest.mark.no_sim +def test_two_way_full_surface_options_are_forwarded_to_native() -> None: + cfg = NewtonPhysicsCfg( + solver_cfg={ + "solver_type": "dexuni", + "coupling": "two_way", + "collision_options": {"enable_rigid_soft_full_surface_contact": True}, + } + ) + native = cfg.to_dexsim_cfg(gpu_id=0) + assert native.solver_cfg.coupling == "two_way" + assert ( + native.solver_cfg.collision_options["enable_rigid_soft_full_surface_contact"] + is True + ) From e9e249d2250b10eddc6405a5d9e72781f2b59e63 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 13:18:25 +0800 Subject: [PATCH 02/13] ci: run primary validation on Python 3.12 Switch the main workflow and shared Conda activation to py312. Keep the supported Python 3.11 compatibility lane, remove Python 3.10 installation checks, and raise the package minimum to 3.11 because DexSim 0.6.0rc1 has no cp310 wheel. --- .github/actions/activate-conda/action.yml | 10 +++++----- .github/workflows/dataset-tooling.yml | 7 +------ .github/workflows/main.yml | 12 ++++++------ pyproject.toml | 2 +- tests/ci/test_workflows.py | 2 +- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.github/actions/activate-conda/action.yml b/.github/actions/activate-conda/action.yml index 491c70a7fa..0c89191656 100644 --- a/.github/actions/activate-conda/action.yml +++ b/.github/actions/activate-conda/action.yml @@ -1,13 +1,13 @@ -name: Activate conda py311 environment -description: Activate py311 and expose its executables on PATH. +name: Activate conda py312 environment +description: Activate py312 and expose its executables on PATH. runs: using: composite steps: - - name: Activate conda py311 environment + - name: Activate conda py312 environment shell: bash -l {0} run: | - source activate py311 + source activate py312 echo "$CONDA_PREFIX/bin" >> "$GITHUB_PATH" - echo "EMBODICHAIN_ENV=py311" >> "$GITHUB_ENV" + echo "EMBODICHAIN_ENV=py312" >> "$GITHUB_ENV" python --version which python pip diff --git a/.github/workflows/dataset-tooling.yml b/.github/workflows/dataset-tooling.yml index 69595c96ed..af8cfa64a9 100644 --- a/.github/workflows/dataset-tooling.yml +++ b/.github/workflows/dataset-tooling.yml @@ -36,7 +36,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python: ['3.10', '3.11', '3.12'] + python: ['3.11', '3.12'] steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 @@ -61,11 +61,6 @@ jobs: fail-fast: false matrix: include: - - python: '3.10' - torch: '2.7.0' - torchvision: '0.22.0' - torchcodec: '0.5.0' - tensordict: '0.13.0' - python: '3.11' torch: '2.7.0' torchvision: '0.22.0' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2a1d5ede5b..bee44902d1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,7 +37,7 @@ jobs: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.11' + python-version: '3.12' - name: Code Style check run: | echo "Workspace: ${GITHUB_WORKSPACE}" @@ -93,7 +93,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.11' + python-version: '3.12' - name: Install planner dependencies shell: bash run: | @@ -378,7 +378,7 @@ jobs: - name: Install test package shell: bash -l {0} run: | - source activate py311 + source activate py312 set -euo pipefail if [[ "${NEEDS_GENSIM}" == "true" ]]; then package='.[gensim]' @@ -412,7 +412,7 @@ jobs: - name: Run selected tests shell: bash -l {0} run: | - source activate py311 + source activate py312 set -euo pipefail # Login shells may use a different Git config from actions/checkout. git config --global --add safe.directory "$GITHUB_WORKSPACE" @@ -447,7 +447,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.11' + python-version: '3.12' - name: Install docs test dependencies run: | python -m pip --isolated install --disable-pip-version-check \ @@ -533,7 +533,7 @@ jobs: - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.11' + python-version: '3.12' - name: Validate release versions shell: bash diff --git a/pyproject.toml b/pyproject.toml index 65ac097e65..389e6b12df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ name = "embodichain" description = "An end-to-end, GPU-accelerated, and modular platform for building generalized Embodied Intelligence." readme = "README.md" authors = [ { name = "EmbodiChain Developers" } ] -requires-python = ">=3.10" +requires-python = ">=3.11" dynamic = ["version"] # Core install dependencies (kept from requirements.txt). diff --git a/tests/ci/test_workflows.py b/tests/ci/test_workflows.py index 1f8e2aba96..458271dcfd 100644 --- a/tests/ci/test_workflows.py +++ b/tests/ci/test_workflows.py @@ -281,7 +281,7 @@ def test_test_job_restores_git_access_before_running_selected_tests( tests = next(step for step in steps if step.get("name") == "Run selected tests") assert tests["shell"] == "bash -l {0}" preparation = tests["run"].split("python scripts/ci/run_test_plan.py", 1)[0] - preparation = preparation.replace("source activate py311", ":").replace( + preparation = preparation.replace("source activate py312", ":").replace( "node --version", ":" ) repository = tmp_path / "checkout with spaces" From d49161da7fad322593f40a939fae3c71eeb6f6ed Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 13:50:23 +0800 Subject: [PATCH 03/13] chore: drop Python 3.10 compatibility paths Remove CI planner and example fallbacks for Python 3.10, update metadata tests, and align user-facing support documentation with the Python 3.11 minimum. --- README.md | 2 +- docker/README.md | 4 ++-- docker/VALIDATION.md | 4 ++++ embodichain/data_pipeline/README.md | 2 +- embodichain/data_pipeline/depth_video/__init__.py | 2 +- embodichain/data_pipeline/depth_video/depth_utils.py | 2 +- examples/learning/act/README.md | 2 +- examples/ods/simple_bc_demo/trainer.py | 12 ++---------- scripts/ci/select_tests.py | 12 +----------- tests/test_release_metadata.py | 7 ++----- tools/lerobot_export/README.md | 2 +- 11 files changed, 17 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 7e6a5ac4f7..a92b307523 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![License](https://img.shields.io/github/license/DexForce/EmbodiChain?style=for-the-badge)](LICENSE) [![Website](https://img.shields.io/badge/website-dexforce.com-yellow?style=for-the-badge&logo=google-chrome&logoColor=white)](https://dexforce.com/embodichain/index.html#/) [![GitHub Pages](https://img.shields.io/badge/GitHub%20Pages-docs-blue?style=for-the-badge&logo=github&logoColor=white)](https://dexforce.github.io/EmbodiChain/main/index.html) -[![Python](https://img.shields.io/badge/python-3.10%20|%203.11%20|%203.12-blue?style=for-the-badge&logo=python&logoColor=white)](https://docs.python.org/3/whatsnew/3.12.html) +[![Python](https://img.shields.io/badge/python-3.11%20|%203.12-blue?style=for-the-badge&logo=python&logoColor=white)](https://docs.python.org/3/whatsnew/3.12.html) [![Version](https://img.shields.io/github/v/release/DexForce/EmbodiChain?style=for-the-badge&label=version)](https://github.com/DexForce/EmbodiChain/releases) --- diff --git a/docker/README.md b/docker/README.md index 5546e1daa1..33e9066721 100644 --- a/docker/README.md +++ b/docker/README.md @@ -189,10 +189,10 @@ free to install their own Torch/codec profile. ## CI rollout and rollback CI defaults to the published internal `v4-py311-py312` image -and activates `py311`. The image reference and activation command are explicit +and activates `py312`. The image reference and activation command are explicit in the existing workflows/shared action; the existing job graph, triggers, test selection and installation commands are preserved. The SDK compatibility -matrix selects Python 3.10, 3.11 and 3.12 independently of this default. +matrix selects Python 3.11 and 3.12 independently of this default. Builds continue producing source-commit candidate tags. After qualification and successful CI, promote the existing manifests to the public diff --git a/docker/VALIDATION.md b/docker/VALIDATION.md index e4858d3da3..58f75a0bf4 100644 --- a/docker/VALIDATION.md +++ b/docker/VALIDATION.md @@ -1,5 +1,9 @@ # Published container images +> Historical qualification below includes Python 3.10 results from an earlier +> dependency profile. This checkout requires Python 3.11 or newer; +> the main CI uses Python 3.12. + Source commit: `c42e8a0b0857ae00aeeb21d414d30318abb2e171`. Implementation: [PR #914](https://github.com/DexForce/EmbodiChain/pull/914), which incorporates [PR #911](https://github.com/DexForce/EmbodiChain/pull/911). diff --git a/embodichain/data_pipeline/README.md b/embodichain/data_pipeline/README.md index 04305b57de..d60d3f5c5f 100644 --- a/embodichain/data_pipeline/README.md +++ b/embodichain/data_pipeline/README.md @@ -23,7 +23,7 @@ results remain example evidence rather than generic ODS defaults. ## Expert dataset tools -EmbodiChain selects LeRobot 0.4.4 on Python 3.10/3.11 and LeRobot 0.6.1 +EmbodiChain selects LeRobot 0.4.4 on Python 3.11 and LeRobot 0.6.1 on Python >=3.12. The official ACT workflow uses Python 3.12; see the [LeRobot integration guide](../../docs/source/guides/lerobot.md) for training and simulation evaluation. diff --git a/embodichain/data_pipeline/depth_video/__init__.py b/embodichain/data_pipeline/depth_video/__init__.py index 3bd91f6799..d8c2aea7dd 100644 --- a/embodichain/data_pipeline/depth_video/__init__.py +++ b/embodichain/data_pipeline/depth_video/__init__.py @@ -14,7 +14,7 @@ # limitations under the License. # ---------------------------------------------------------------------------- -"""Compressed depth sidecar storage for LeRobot datasets on Python 3.10--3.12. +"""Compressed depth sidecar storage for LeRobot datasets on Python 3.11--3.12. This package implements issue #424 *Path A*: an EmbodiChain-owned depth writer that stores camera depth as ``gray12le``/HEVC sidecar videos alongside a diff --git a/embodichain/data_pipeline/depth_video/depth_utils.py b/embodichain/data_pipeline/depth_video/depth_utils.py index 05e806a9ff..1895732804 100644 --- a/embodichain/data_pipeline/depth_video/depth_utils.py +++ b/embodichain/data_pipeline/depth_video/depth_utils.py @@ -31,7 +31,7 @@ The math is ported from lerobot 0.6.0 (itself adapted from BEHAVIOR-1K's ``obs_utils.py``) and depends only on :mod:`av`, :mod:`numpy` and -:mod:`torch` -- all of which run on Python 3.10--3.12. See issue #424. +:mod:`torch` -- all of which run on supported Python 3.11--3.12. See issue #424. """ from __future__ import annotations diff --git a/examples/learning/act/README.md b/examples/learning/act/README.md index ae7808e3e2..d34b820062 100644 --- a/examples/learning/act/README.md +++ b/examples/learning/act/README.md @@ -158,7 +158,7 @@ have been replaced. Historical `.pt` checkpoints require retraining; use Git history to reproduce those models. Keep generated datasets, checkpoints, evaluation reports and videos in a local output directory outside the checkout. -Core trainer/dataset/robot support on Python 3.10/3.11 remains independent of +Core trainer/dataset/robot support on Python 3.11 remains independent of this Python 3.12 ACT workflow. The focused production tests exercise real SDK configuration, native training/resume, portable policy reload, normalization, reset and content checks without starting a simulator. GPU collection and diff --git a/examples/ods/simple_bc_demo/trainer.py b/examples/ods/simple_bc_demo/trainer.py index 16cbaaacb9..f1f2f35d86 100644 --- a/examples/ods/simple_bc_demo/trainer.py +++ b/examples/ods/simple_bc_demo/trainer.py @@ -85,16 +85,8 @@ class _BudgetExpired(Exception): def _add_error_note(error: BaseException, note: str) -> None: - """Retain secondary errors without replacing the original on Python 3.10.""" - add_note = getattr(error, "add_note", None) - if add_note is not None: - add_note(note) - else: - notes = getattr(error, "__notes__", None) - if notes is None: - notes = [] - error.__notes__ = notes - notes.append(note) + """Retain secondary errors without replacing the original.""" + error.add_note(note) def _timeout(args: argparse.Namespace, tracker: EfficiencyTracker) -> float: diff --git a/scripts/ci/select_tests.py b/scripts/ci/select_tests.py index f58d8d06d8..0adeecd004 100644 --- a/scripts/ci/select_tests.py +++ b/scripts/ci/select_tests.py @@ -35,13 +35,7 @@ from pathlib import Path from typing import Any, Iterable, Mapping, Sequence -try: # Python 3.11 (the CI planner runtime) has this in the standard library. - import tomllib -except ModuleNotFoundError: # pragma: no cover - exercised only on Python 3.10. - try: - import tomli as tomllib # type: ignore[import-not-found] - except ModuleNotFoundError: - tomllib = None # type: ignore[assignment] +import tomllib __all__ = [ "ChangedPath", @@ -249,10 +243,6 @@ def collect_changed_paths( def load_manifest(path: str | Path) -> dict[str, Any]: """Load and validate the CI impact manifest.""" - if tomllib is None: # pragma: no cover - Python 3.10 without tomli. - raise RuntimeError( - "Python 3.11 or the tomli package is required to read the impact manifest" - ) manifest_path = Path(path) try: data = tomllib.loads(manifest_path.read_text(encoding="utf-8")) diff --git a/tests/test_release_metadata.py b/tests/test_release_metadata.py index 399fdf5cb6..911b6d1d82 100644 --- a/tests/test_release_metadata.py +++ b/tests/test_release_metadata.py @@ -22,10 +22,7 @@ import pytest from packaging.requirements import Requirement -try: - import tomllib -except ModuleNotFoundError: # Python 3.10 - import tomli as tomllib +import tomllib import setup as package_setup from scripts.validate_wheel_metadata import WheelMetadataError, validate_wheel @@ -150,7 +147,7 @@ def test_project_metadata_has_no_direct_dependencies() -> None: def test_wheel_metadata_accepts_index_dependencies(tmp_path: Path) -> None: wheel_path = _write_test_wheel( tmp_path, - ["gymnasium>=0.29.1", 'viser==1.0.21; python_version >= "3.10"'], + ["gymnasium>=0.29.1", 'viser==1.0.21; python_version >= "3.11"'], ) validate_wheel(wheel_path) diff --git a/tools/lerobot_export/README.md b/tools/lerobot_export/README.md index a73752aeff..a1e23a20a2 100644 --- a/tools/lerobot_export/README.md +++ b/tools/lerobot_export/README.md @@ -3,7 +3,7 @@ This standalone tool reads finalized EmbodiChain LeRobot v3.0 datasets from disk. It runs in Python 3.12 independently of the simulator and does not install or import the EmbodiChain package. EmbodiChain selects LeRobot 0.4.4 on Python -3.10/3.11 and 0.6.1 on Python 3.12+. Both recording routes retain the source +3.11 and 0.6.1 on Python 3.12+. Both recording routes retain the source task/subtask and depth-sidecar schema consumed by this converter. Keep the converter environment separate so its dependency stack can evolve independently. From 61d1b440cef31904576c767ea6dbc098f043f177 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 14:21:19 +0800 Subject: [PATCH 04/13] fix(ci): align DexSim locks and demo solver configs Regenerate Python 3.11/3.12 Docker locks for DexSim 0.6.0rc1, sync curated architecture evidence, and migrate shipped soft-body demos to grouped DexUni options with per-object contact materials. --- .../topics/simulation-system/deformables.md | 7 ++- docker/README.md | 3 +- docker/VALIDATION.md | 2 + docker/locks/py311.txt | 10 ++-- docker/locks/py312.txt | 10 ++-- docker/profiles/py311.in | 2 +- docker/profiles/py312.in | 2 +- docs/architecture/curated.json | 2 +- embodichain/lab/sim/cfg/deformable.py | 6 +++ embodichain/lab/sim/spawn/descriptors.py | 16 +++++- examples/sim/demo/fold_tshirt.py | 35 +++++++------ examples/sim/demo/pick_up_cloth.py | 49 ++++++++++--------- examples/sim/demo/press_softbody.py | 34 +++++++------ tests/sim/test_deformable_cfg.py | 28 +++++++++++ tests/test_release_metadata.py | 38 +++++++++++++- 15 files changed, 173 insertions(+), 71 deletions(-) diff --git a/agent_context/topics/simulation-system/deformables.md b/agent_context/topics/simulation-system/deformables.md index af1be92764..54b37d7e97 100644 --- a/agent_context/topics/simulation-system/deformables.md +++ b/agent_context/topics/simulation-system/deformables.md @@ -12,8 +12,8 @@ binding. Return to the [simulation overview](simulation-system.md). uses one registry and backend dispatch for both topologies. The supported path is Newton on CUDA, without gradient mode. Declarations -accept unresolved AutoSolver or a supported particle solver (`xpbd`, -`semi_implicit`, `vbd`, `dexuni`); they reject unsupported backends/solvers and +accept the default DexUni solver or an explicit supported particle solver +(`xpbd`, `semi_implicit`, `vbd`); they reject unsupported backends/solvers and post-finalization additions. Default soft/cloth compatibility is absent. Future backend support belongs at dispatch, not inside a monolithic config. @@ -45,6 +45,9 @@ parser. Volume-only `meshing` and common `surface_props` preserve topology ownership. Volume elasticity uses Young's modulus/Poisson's ratio, translated to Lamé coefficients at Spawn; density units differ (kg/m³ volume, kg/m² surface). Exact fields and defaults stay in the typed config source. +Optional `attrs.contact_material` sets DexUni stiffness, damping and dynamic +friction per soft or cloth object; it is separate from element elasticity and +replaces removed flat solver-level soft-contact coefficients in examples. ## Kinematic node controls diff --git a/docker/README.md b/docker/README.md index 33e9066721..ec41af076e 100644 --- a/docker/README.md +++ b/docker/README.md @@ -163,7 +163,8 @@ complete Python dependency sets and explicit conda package URLs/checksums. The CUDA base, Node base, Miniforge installer and cuRobo commit are pinned. Both DexSim wheel URLs carry SHA256 pins, which the installer verifies even though the wheel server uses HTTP. These match the published index checksums -and the payloads in the previously qualified Python 3.11/3.12 installations. +for DexSim 0.6.0rc1. Build and qualify both updated profiles before promoting +new image digests. DexSim and LeRobot currently require different OpenCV distributions sharing `cv2`. Python-selected dependencies pin both to the same release; qualification checks the imported binding as well as distribution metadata. diff --git a/docker/VALIDATION.md b/docker/VALIDATION.md index 58f75a0bf4..b62e862ca3 100644 --- a/docker/VALIDATION.md +++ b/docker/VALIDATION.md @@ -3,6 +3,8 @@ > Historical qualification below includes Python 3.10 results from an earlier > dependency profile. This checkout requires Python 3.11 or newer; > the main CI uses Python 3.12. +> The current Docker locks select DexSim 0.6.0rc1; the published image digests +> and qualification runs below describe the earlier DexSim 0.5.1rc1 images. Source commit: `c42e8a0b0857ae00aeeb21d414d30318abb2e171`. Implementation: [PR #914](https://github.com/DexForce/EmbodiChain/pull/914), diff --git a/docker/locks/py311.txt b/docker/locks/py311.txt index d6d93b4549..250aa5eb5e 100644 --- a/docker/locks/py311.txt +++ b/docker/locks/py311.txt @@ -46,7 +46,7 @@ datasets==4.8.5 datetime==6.0 decorator==5.3.1 deepdiff==8.6.2 -dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.5.1rc1-cp311-cp311-manylinux_2_31_x86_64.whl#sha256=e6177696fca4cedf91b08e98c2789c3c98ebd48c3283d4dbe194f65fbf7fb4a5 +dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.6.0rc1-cp311-cp311-manylinux_2_31_x86_64.whl#sha256=f3cef56bd987b3166b871d7d9f29b2f2d6ceb274cf47ca9a598c51d6a6785718 diffusers==0.35.2 dill==0.4.1 distro==1.9.0 @@ -138,8 +138,8 @@ mdurl==0.1.2 mergedeep==1.3.4 mpmath==1.3.0 msgspec==0.22.0 -mujoco==3.12.0 -mujoco-warp==3.12.0 +mujoco==3.15.0 +mujoco-warp==3.15.0 multidict==7.2.1 multiprocess==0.70.19 mypy-extensions==1.1.0 @@ -147,7 +147,7 @@ narwhals==2.28.0 nbformat==5.11.1 nest-asyncio==1.6.0 networkx==3.6.1 -newton==1.6.0 +newton==1.7.0 nodeenv==1.12.0 nuitka==4.3.3 numpy==2.2.6 @@ -187,6 +187,7 @@ parso==0.8.7 pathspec==1.1.1 pexpect==4.9.0 pfzy==0.3.4 +physx-usd-schemas==25.11.2.72696984 pillow==12.3.0 pin==4.1.0 pin-pink==4.4.0 @@ -285,6 +286,7 @@ typing-extensions==4.16.0 typing-inspect==0.9.0 typing-inspection==0.4.4 tzdata==2026.6 +urdfdom-py==1.2.1.post1 urllib3==2.8.0 usd-core==26.11 uuid-utils==0.19.0 diff --git a/docker/locks/py312.txt b/docker/locks/py312.txt index 59571dcacd..2640cadbcf 100644 --- a/docker/locks/py312.txt +++ b/docker/locks/py312.txt @@ -45,7 +45,7 @@ dash==4.5.0 datasets==4.8.5 datetime==6.0 decorator==5.3.1 -dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.5.1rc1-cp312-cp312-manylinux_2_31_x86_64.whl#sha256=88178eb81ebe9b16fa8a79d17c7ffa52b9c0256c94942fdeb6f637200d465866 +dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.6.0rc1-cp312-cp312-manylinux_2_31_x86_64.whl#sha256=f84c0f1763689b6d57f63308b417932237325666c20d1e3379bafbb7cdad7076 dill==0.4.1 distro==1.9.0 draccus==0.11.6 @@ -133,8 +133,8 @@ mdurl==0.1.2 mergedeep==1.3.4 mpmath==1.3.0 msgspec==0.22.0 -mujoco==3.12.0 -mujoco-warp==3.12.0 +mujoco==3.15.0 +mujoco-warp==3.15.0 multidict==7.2.1 multiprocess==0.70.19 mypy-extensions==1.1.0 @@ -142,7 +142,7 @@ narwhals==2.28.0 nbformat==5.11.1 nest-asyncio==1.6.0 networkx==3.7 -newton==1.6.0 +newton==1.7.0 nodeenv==1.12.0 nuitka==4.3.3 numpy==2.2.6 @@ -181,6 +181,7 @@ pandas==2.3.3 parso==0.8.7 pathspec==1.1.1 pexpect==4.9.0 +physx-usd-schemas==25.11.2.72696984 pillow==12.3.0 pin==4.1.0 pin-pink==4.4.0 @@ -274,6 +275,7 @@ typing-extensions==4.16.0 typing-inspect==0.9.0 typing-inspection==0.4.4 tzdata==2026.6 +urdfdom-py==1.2.1.post1 urllib3==2.8.0 usd-core==26.11 uuid-utils==0.19.0 diff --git a/docker/profiles/py311.in b/docker/profiles/py311.in index af01132adf..ec42e9ebdc 100644 --- a/docker/profiles/py311.in +++ b/docker/profiles/py311.in @@ -1,5 +1,5 @@ # Qualified CUDA 12.8 versions; regenerate using docker/lock.sh. -dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.5.1rc1-cp311-cp311-manylinux_2_31_x86_64.whl#sha256=e6177696fca4cedf91b08e98c2789c3c98ebd48c3283d4dbe194f65fbf7fb4a5 +dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.6.0rc1-cp311-cp311-manylinux_2_31_x86_64.whl#sha256=f3cef56bd987b3166b871d7d9f29b2f2d6ceb274cf47ca9a598c51d6a6785718 torch==2.7.0+cu128 torchvision==0.22.0+cu128 torchcodec==0.5.0+cu128 diff --git a/docker/profiles/py312.in b/docker/profiles/py312.in index d2d8e089d7..c22afc7a47 100644 --- a/docker/profiles/py312.in +++ b/docker/profiles/py312.in @@ -1,5 +1,5 @@ # Qualified CUDA 12.8 versions; regenerate using docker/lock.sh. -dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.5.1rc1-cp312-cp312-manylinux_2_31_x86_64.whl#sha256=88178eb81ebe9b16fa8a79d17c7ffa52b9c0256c94942fdeb6f637200d465866 +dexsim-engine @ http://pyp.open3dv.site:2345/packages/dexsim_engine-0.6.0rc1-cp312-cp312-manylinux_2_31_x86_64.whl#sha256=f84c0f1763689b6d57f63308b417932237325666c20d1e3379bafbb7cdad7076 torch==2.11.0+cu128 torchvision==0.26.0+cu128 torchcodec==0.11.1+cu128 diff --git a/docs/architecture/curated.json b/docs/architecture/curated.json index 0f4fff1e40..e2118adef1 100644 --- a/docs/architecture/curated.json +++ b/docs/architecture/curated.json @@ -2620,7 +2620,7 @@ "symbol": "", "start_line": 24, "end_line": 35, - "excerpt": "\"\"\"Depth quantization/dequantization for compressed depth sidecar videos.\n\nDepth maps are packed into 12-bit integer codes (``uint16``, values\n``0…DEPTH_QMAX``) so they fit the ``gray12le`` pixel format and can be encoded\nlosslessly by HEVC (``libx265``). Logarithmic quantization is the default\nbecause it allocates more quanta to near-range depth, matching the\n``1/depth`` error profile of typical depth sensors.\n\nThe math is ported from lerobot 0.6.0 (itself adapted from BEHAVIOR-1K's\n``obs_utils.py``) and depends only on :mod:`av`, :mod:`numpy` and\n:mod:`torch` -- all of which run on Python 3.10--3.12. See issue #424.\n\"\"\"" + "excerpt": "\"\"\"Depth quantization/dequantization for compressed depth sidecar videos.\n\nDepth maps are packed into 12-bit integer codes (``uint16``, values\n``0…DEPTH_QMAX``) so they fit the ``gray12le`` pixel format and can be encoded\nlosslessly by HEVC (``libx265``). Logarithmic quantization is the default\nbecause it allocates more quanta to near-range depth, matching the\n``1/depth`` error profile of typical depth sensors.\n\nThe math is ported from lerobot 0.6.0 (itself adapted from BEHAVIOR-1K's\n``obs_utils.py``) and depends only on :mod:`av`, :mod:`numpy` and\n:mod:`torch` -- all of which run on supported Python 3.11--3.12. See issue #424.\n\"\"\"" } ], "documentation": [ diff --git a/embodichain/lab/sim/cfg/deformable.py b/embodichain/lab/sim/cfg/deformable.py index 57423ecc63..125bd1a1a6 100644 --- a/embodichain/lab/sim/cfg/deformable.py +++ b/embodichain/lab/sim/cfg/deformable.py @@ -124,6 +124,9 @@ class VolumeDeformablePhysicsCfg: add_surface_edges: bool = True """Whether Newton creates surface bending-edge constraints.""" + contact_material: Mapping[str, float] | None = None + """Optional DexUni contact stiffness, damping, and dynamic friction.""" + def __post_init__(self) -> None: if isinstance(self.surface_props, Mapping): self.surface_props = SurfaceElementPropertiesCfg(**self.surface_props) @@ -148,6 +151,9 @@ class SurfaceDeformablePhysicsCfg: spring_kd: float | None = None """Spring damping; ``None`` uses the Newton default.""" + contact_material: Mapping[str, float] | None = None + """Optional DexUni contact stiffness, damping, and dynamic friction.""" + def __post_init__(self) -> None: if isinstance(self.surface_props, Mapping): self.surface_props = SurfaceElementPropertiesCfg(**self.surface_props) diff --git a/embodichain/lab/sim/spawn/descriptors.py b/embodichain/lab/sim/spawn/descriptors.py index 73d399007c..7956b9e774 100644 --- a/embodichain/lab/sim/spawn/descriptors.py +++ b/embodichain/lab/sim/spawn/descriptors.py @@ -30,7 +30,7 @@ from __future__ import annotations -from collections.abc import Sequence +from collections.abc import Mapping, Sequence from dataclasses import MISSING, dataclass, field, fields, replace import math import hashlib @@ -55,6 +55,7 @@ MaterialDesc, NewtonCollisionDesc, NewtonJointDesc, + NewtonParticleContactMaterialDesc, ObjectDesc, RenderDesc, RigidBodyPhysicsDesc, @@ -502,6 +503,17 @@ def _mesh_geometry_from_cfg(shape: MeshCfg, *, segment_name: str) -> GeometryDes ) +def _particle_contact_material( + value: Mapping[str, float] | None, +) -> NewtonParticleContactMaterialDesc | None: + """Validate and lower a deformable's DexUni contact material.""" + if value is None: + return None + if not isinstance(value, Mapping): + raise TypeError("Deformable contact_material must be a mapping.") + return NewtonParticleContactMaterialDesc(**dict(value)) + + def volume_deformable_desc_from_cfg( cfg: VolumeDeformableObjectCfg, *, @@ -544,6 +556,7 @@ def volume_deformable_desc_from_cfg( ), physics=SoftBodyPhysicsDesc( volume_density=density, + contact_material=_particle_contact_material(attrs.contact_material), k_mu=youngs / (2.0 * (1.0 + poissons)), k_lambda=(youngs * poissons / ((1.0 + poissons) * (1.0 - 2.0 * poissons))), k_damp=float(attrs.elasticity_damping), @@ -636,6 +649,7 @@ def surface_deformable_desc_from_cfg( visual_binding_mode=cfg.visual_binding_mode, physics=ClothPhysicsDesc( surface_density=density, + contact_material=_particle_contact_material(attrs.contact_material), add_springs=bool(attrs.add_springs), spring_ke=attrs.spring_ke, spring_kd=attrs.spring_kd, diff --git a/examples/sim/demo/fold_tshirt.py b/examples/sim/demo/fold_tshirt.py index 50f0f975ba..525f40ffca 100644 --- a/examples/sim/demo/fold_tshirt.py +++ b/examples/sim/demo/fold_tshirt.py @@ -354,22 +354,20 @@ def initialize_simulation( use_cuda_graph=use_cuda_graph, solver_cfg={ "solver_type": "dexuni", - "iterations": SOLVER_ITERATIONS, - "particle_enable_self_contact": True, - "particle_self_contact_radius": 0.002, - "particle_self_contact_margin": 0.002, - "particle_topological_contact_filter_threshold": 1, - "particle_rest_shape_contact_exclusion_radius": 0.005, - "particle_vertex_contact_buffer_size": 96, - "particle_edge_contact_buffer_size": 128, - "particle_collision_detection_interval": -1, - "self_contact_bvh_rebuild_interval_frames": 15, - "rigid_contact_max": 0, - "step_rigid_bodies": False, - "soft_contact_margin": 0.008, - "soft_contact_ke": 3.0e5, - "soft_contact_kd": 5.0e-2, - "soft_contact_mu": 0.5, + "joint_mode": "kinematic", + "contact_mode": "soft", + "vbd_options": { + "iterations": SOLVER_ITERATIONS, + "particle_enable_self_contact": True, + "particle_self_contact_radius": 0.002, + "particle_self_contact_margin": 0.002, + "particle_topological_contact_filter_threshold": 1, + "particle_rest_shape_contact_exclusion_radius": 0.005, + "particle_vertex_contact_buffer_size": 96, + "particle_edge_contact_buffer_size": 128, + "particle_collision_detection_interval": -1, + }, + "collision_options": {"soft_contact_margin": 0.008}, }, # DexUni owns its particle-shape contacts and collision detection. collision_cfg=None, @@ -519,6 +517,11 @@ def create_shirt( init_rot=(0.0, 0.0, -90.0), particle_radius=0.008, attrs=SurfaceDeformablePhysicsCfg( + contact_material={ + "stiffness": 3.0e5, + "damping": 5.0e-2, + "dynamic_friction": 0.5, + }, density=200.0, surface_props=SurfaceElementPropertiesCfg( tri_ke=1.5e3, diff --git a/examples/sim/demo/pick_up_cloth.py b/examples/sim/demo/pick_up_cloth.py index 715b93d93a..4da7275cb7 100644 --- a/examples/sim/demo/pick_up_cloth.py +++ b/examples/sim/demo/pick_up_cloth.py @@ -244,6 +244,11 @@ def create_cloth(sim: SimulationManager) -> SurfaceDeformableObject: # radius can make the gripper carry cloth while visibly separated. particle_radius=CLOTH_PARTICLE_RADIUS, attrs=SurfaceDeformablePhysicsCfg( + contact_material={ + "stiffness": CLOTH_RIGID_CONTACT_KE, + "damping": CLOTH_RIGID_CONTACT_KD, + "dynamic_friction": CLOTH_RIGID_CONTACT_MU, + }, # Give gravity enough authority to produce fabric-like drape. # Stretch remains firmer than bending so the cloth folds # instead of behaving like an elastic sheet. @@ -437,29 +442,25 @@ def main(args: argparse.Namespace | None = None) -> None: num_substeps=12, solver_cfg={ "solver_type": "dexuni", - "iterations": 24, - "particle_enable_self_contact": True, - "particle_self_contact_radius": 0.002, - "particle_self_contact_margin": 0.002, - "particle_topological_contact_filter_threshold": 1, - "particle_rest_shape_contact_exclusion_radius": 0.005, - "particle_vertex_contact_buffer_size": 96, - "particle_edge_contact_buffer_size": 128, - "particle_collision_detection_interval": -1, - "particle_enable_tile_solve": True, - "soft_contact_margin": 0.008, - "soft_contact_ke": CLOTH_RIGID_CONTACT_KE, - "soft_contact_kd": CLOTH_RIGID_CONTACT_KD, - "soft_contact_mu": CLOTH_RIGID_CONTACT_MU, - # Use the mixed material stiffness immediately instead of - # ramping new contacts from the low DexUni default. - "rigid_contact_k_start": CLOTH_RIGID_CONTACT_KE, - "rigid_body_particle_contact_buffer_size": 512, - "rigid_contact_max": 0, # The registered runtime control advances and interpolates the # robot kinematically at every Newton substep. - "step_rigid_bodies": False, - "self_contact_bvh_rebuild_interval_frames": 1, + "joint_mode": "kinematic", + "contact_mode": "soft", + "vbd_options": { + "iterations": 24, + "particle_enable_self_contact": True, + "particle_self_contact_radius": 0.002, + "particle_self_contact_margin": 0.002, + "particle_topological_contact_filter_threshold": 1, + "particle_rest_shape_contact_exclusion_radius": 0.005, + "particle_vertex_contact_buffer_size": 96, + "particle_edge_contact_buffer_size": 128, + "particle_collision_detection_interval": -1, + "particle_enable_tile_solve": True, + "rigid_contact_k_start": CLOTH_RIGID_CONTACT_KE, + "rigid_body_particle_contact_buffer_size": 512, + }, + "collision_options": {"soft_contact_margin": 0.008}, }, # DexUni owns its particle-shape contacts and collision detection. collision_cfg=None, @@ -496,11 +497,13 @@ def main(args: argparse.Namespace | None = None) -> None: if not args.headless: sim.open_window() sim.update(step=settle_steps) - input("Press Enter to start grabbing the cloth...") + if not args.headless: + input("Press Enter to start grabbing the cloth...") # The initial trajectory row is already active after settling. sim.update(step=grab_traj.shape[1] - 1) - input("Press Enter to exit the simulation...") + if not args.headless: + input("Press Enter to exit the simulation...") finally: sim.destroy() diff --git a/examples/sim/demo/press_softbody.py b/examples/sim/demo/press_softbody.py index 0e801b0aa9..2f5dcc151b 100644 --- a/examples/sim/demo/press_softbody.py +++ b/examples/sim/demo/press_softbody.py @@ -48,7 +48,7 @@ def parse_arguments(argv: list[str] | None = None) -> argparse.Namespace: import numpy as np import torch -from dexsim.utility.path import get_resources_data_path +from embodichain.data import get_data_path from embodichain.lab.sim import SimulationManager, SimulationManagerCfg from embodichain.lab.sim.cfg import ( NewtonPhysicsCfg, @@ -98,22 +98,21 @@ def initialize_simulation(args: argparse.Namespace) -> SimulationManager: num_substeps=NUM_SUBSTEPS, solver_cfg={ "solver_type": "dexuni", - "iterations": SOLVER_ITERATIONS, - "particle_enable_self_contact": False, - "particle_self_contact_radius": 0.005, - "particle_self_contact_margin": 0.005, - "particle_topological_contact_filter_threshold": 3, - "particle_enable_tile_solve": True, - "rigid_body_particle_contact_buffer_size": 512, - "rigid_contact_k_start": SOFT_CONTACT_KE, - "rigid_contact_max": 0, - "soft_contact_margin": SOFT_CONTACT_MARGIN, - "soft_contact_ke": SOFT_CONTACT_KE, - "soft_contact_kd": SOFT_CONTACT_KD, - "soft_contact_mu": SOFT_CONTACT_MU, # The registered trajectory updates the robot kinematically at # every Newton substep; DexUni only needs to solve the soft body. - "step_rigid_bodies": False, + "joint_mode": "kinematic", + "contact_mode": "soft", + "vbd_options": { + "iterations": SOLVER_ITERATIONS, + "particle_enable_self_contact": False, + "particle_self_contact_radius": 0.005, + "particle_self_contact_margin": 0.005, + "particle_topological_contact_filter_threshold": 3, + "particle_enable_tile_solve": True, + "rigid_body_particle_contact_buffer_size": 512, + "rigid_contact_k_start": SOFT_CONTACT_KE, + }, + "collision_options": {"soft_contact_margin": SOFT_CONTACT_MARGIN}, }, # DexUni owns its particle-shape contacts and collision detection. collision_cfg=None, @@ -185,6 +184,11 @@ def create_soft_cow(sim: SimulationManager) -> VolumeDeformableObject: voxel_num_relaxation_iters=5, ), attrs=VolumeDeformablePhysicsCfg( + contact_material={ + "stiffness": SOFT_CONTACT_KE, + "damping": SOFT_CONTACT_KD, + "dynamic_friction": SOFT_CONTACT_MU, + }, youngs=5.0e3, poissons=0.45, density=100.0, diff --git a/tests/sim/test_deformable_cfg.py b/tests/sim/test_deformable_cfg.py index a834f30503..e68a4d7bba 100644 --- a/tests/sim/test_deformable_cfg.py +++ b/tests/sim/test_deformable_cfg.py @@ -123,6 +123,34 @@ def test_surface_coefficients_compile_without_changing_values(topology: str) -> assert getattr(desc.physics, native_field) == value +@pytest.mark.parametrize("topology", ["volume", "surface"]) +def test_deformable_contact_material_reaches_native_descriptor(topology: str) -> None: + cfg_type = ( + VolumeDeformableObjectCfg + if topology == "volume" + else SurfaceDeformableObjectCfg + ) + compile_cfg = ( + volume_deformable_desc_from_cfg + if topology == "volume" + else surface_deformable_desc_from_cfg + ) + material = {"stiffness": 3000.0, "damping": 0.2, "dynamic_friction": 0.7} + cfg = cfg_type.from_dict( + { + "uid": "body", + "shape": {"fpath": "mesh.obj"}, + "attrs": {"contact_material": material}, + } + ) + + desc, _ = compile_cfg(cfg_type.from_dict(cfg.to_dict())) + native = desc.physics.contact_material + assert native.stiffness == pytest.approx(material["stiffness"]) + assert native.damping == pytest.approx(material["damping"]) + assert native.dynamic_friction == pytest.approx(material["dynamic_friction"]) + + def test_volume_elasticity_uses_short_names() -> None: cfg = VolumeDeformableObjectCfg.from_dict( { diff --git a/tests/test_release_metadata.py b/tests/test_release_metadata.py index 911b6d1d82..b90ef0c62b 100644 --- a/tests/test_release_metadata.py +++ b/tests/test_release_metadata.py @@ -16,13 +16,14 @@ from __future__ import annotations +import tomllib from pathlib import Path +from urllib.parse import urlsplit from zipfile import ZipFile import pytest from packaging.requirements import Requirement - -import tomllib +from packaging.utils import canonicalize_name, parse_wheel_filename import setup as package_setup from scripts.validate_wheel_metadata import WheelMetadataError, validate_wheel @@ -37,6 +38,39 @@ def _read_project_metadata() -> dict: return tomllib.load(stream) +def test_docker_locks_match_core_dexsim_requirement() -> None: + dependencies = _read_project_metadata()["project"]["dependencies"] + core = next( + requirement + for requirement in map(Requirement, dependencies) + if canonicalize_name(requirement.name) == "dexsim-engine" + ) + + for profile in ("py311", "py312"): + requirements = [] + for directory in ("profiles", "locks"): + path = ( + _REPOSITORY_ROOT + / "docker" + / directory + / f"{profile}.{'in' if directory == 'profiles' else 'txt'}" + ) + lines = [ + line + for line in path.read_text().splitlines() + if line.startswith("dexsim-engine @ ") + ] + assert len(lines) == 1 + requirements.append(Requirement(lines[0])) + assert requirements[0].url == requirements[1].url + + wheel_name = Path(urlsplit(requirements[0].url).path).name + package_name, version, _, tags = parse_wheel_filename(wheel_name) + assert canonicalize_name(package_name) == "dexsim-engine" + assert version in core.specifier + assert any(tag.interpreter == f"cp{profile[2:]}" for tag in tags) + + def _write_test_wheel(tmp_path: Path, requirements: list[str]) -> Path: metadata_lines = [ "Metadata-Version: 2.4", From e70d9ed6348384f34dc5080aa9039fe2ce2820ab Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 14:54:55 +0800 Subject: [PATCH 05/13] fix(ci): initialize headless SDL for GPU tests Give the Python 3.12 simulation test container a dummy SDL video driver and private runtime directory so DexSim can initialize its native renderer without a display server. --- .github/workflows/main.yml | 3 +++ tests/ci/test_workflows.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bee44902d1..654fbb1442 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -358,6 +358,8 @@ jobs: NVIDIA_DISABLE_REQUIRE: 1 HF_ENDPOINT: https://hf-mirror.com PIP_CACHE_DIR: /cache/pip + SDL_VIDEODRIVER: dummy + XDG_RUNTIME_DIR: /tmp/embodichain-ci-runtime NEEDS_GENSIM: ${{ needs.test_plan.outputs.needs_gensim }} NEEDS_CUROBO: ${{ needs.test_plan.outputs.needs_curobo }} container: *container_template @@ -414,6 +416,7 @@ jobs: run: | source activate py312 set -euo pipefail + install -d -m 0700 "$XDG_RUNTIME_DIR" # Login shells may use a different Git config from actions/checkout. git config --global --add safe.directory "$GITHUB_WORKSPACE" git -C "$GITHUB_WORKSPACE" rev-parse \ diff --git a/tests/ci/test_workflows.py b/tests/ci/test_workflows.py index 458271dcfd..b6f4c7e8a6 100644 --- a/tests/ci/test_workflows.py +++ b/tests/ci/test_workflows.py @@ -294,6 +294,7 @@ def test_test_job_restores_git_access_before_running_selected_tests( GIT_CONFIG_NOSYSTEM="1", GITHUB_WORKSPACE=str(repository), CI_NODE_BIN_DIR="", + XDG_RUNTIME_DIR=str(tmp_path / "xdg-runtime"), ) for args in ( ["init", "-q"], @@ -339,6 +340,16 @@ def test_test_job_restores_git_access_before_running_selected_tests( ) +def test_gpu_test_job_prepares_headless_sdl_runtime() -> None: + job = _load_workflow("main.yml")["jobs"]["test"] + assert job["env"]["SDL_VIDEODRIVER"] == "dummy" + assert job["env"]["XDG_RUNTIME_DIR"] == "/tmp/embodichain-ci-runtime" + selected = next( + step for step in job["steps"] if step.get("name") == "Run selected tests" + ) + assert 'install -d -m 0700 "$XDG_RUNTIME_DIR"' in selected["run"] + + @pytest.mark.parametrize( "invalid_contents", [ From b495a5a104a939b961108b88b0d49a2f84203015 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 15:17:29 +0800 Subject: [PATCH 06/13] fix(sim): keep NoRender and CUDA test cleanup stable Temporarily hide visual materials from DexSim NoRender native calls, flush deferred simulator cleanup after locomotion subprocesses, and remove the flaky ice retention smoke test as requested. --- .../topics/simulation-system/rendering.md | 5 +++ embodichain/lab/sim/spawn/scene.py | 36 +++++++++++++--- scripts/benchmark/rl/locomotion_ppo.py | 7 +++- tests/gym/envs/tasks/test_locomotion_cuda.py | 13 ++++-- tests/sim/spawn/test_scene.py | 33 +++++++++++++++ tests/sim/test_scoop_ice_demo.py | 42 ------------------- 6 files changed, 83 insertions(+), 53 deletions(-) diff --git a/agent_context/topics/simulation-system/rendering.md b/agent_context/topics/simulation-system/rendering.md index d32c933f55..ab210a44c9 100644 --- a/agent_context/topics/simulation-system/rendering.md +++ b/agent_context/topics/simulation-system/rendering.md @@ -53,6 +53,11 @@ native renderer. Viser retains native rendering because its current exporter reads visual meshes. Checkpoint evaluation with `--viewer` defaults to Hybrid when the saved training configuration uses NoRender. +DexSim 0.6 still processes Spawn's visual material table on NoRender native +add/finalize calls. `SpawnScene` temporarily excludes that table during those +calls so texture binding is never attempted without a renderer, then restores +the authored definitions for metadata and later declarations. + ## Default scene presentation `SimulationManager.set_default_background()` authors the native ground material diff --git a/embodichain/lab/sim/spawn/scene.py b/embodichain/lab/sim/spawn/scene.py index ed4bea6ad6..bbb97ec79a 100644 --- a/embodichain/lab/sim/spawn/scene.py +++ b/embodichain/lab/sim/spawn/scene.py @@ -18,6 +18,8 @@ from __future__ import annotations +from collections.abc import Iterator +from contextlib import contextmanager from dataclasses import dataclass from typing import Any, Callable, Literal @@ -120,6 +122,24 @@ def _material_definition(self, uid: str) -> Any | None: uid, self.builder.materials.get(uid) ) + @contextmanager + def _native_material_scope(self) -> Iterator[None]: + """Keep visual definitions while excluding native NoRender texture loads.""" + if ( + getattr(getattr(self, "_material_resources", None), "renderer", None) + != "no-render" + ): + yield + return + materials = self.builder.materials + saved = dict(materials) + materials.clear() + try: + yield + finally: + materials.clear() + materials.update(saved) + def _bind_resource_materials(self, descriptor: Any) -> None: definitions = getattr(self, "_resource_material_definitions", {}) if not definitions or self._material_resources.renderer == "no-render": @@ -176,9 +196,10 @@ def declare( return if kind == "rigid_object_group": - declaration.descriptor = tuple( - self.builder.add_object(member) for member in descriptor - ) + with self._native_material_scope(): + declaration.descriptor = tuple( + self.builder.add_object(member) for member in descriptor + ) else: if ( kind == "articulation" @@ -197,7 +218,8 @@ def declare( "soft_object": "add_soft_object", "cloth_object": "add_cloth_object", }[kind] - declaration.descriptor = getattr(self.builder, add_name)(descriptor) + with self._native_material_scope(): + declaration.descriptor = getattr(self.builder, add_name)(descriptor) self._assets[uid] = declaration self._configure_materialized_source(uid) # A Newton replacement retains the old same-path handles until commit. @@ -289,7 +311,8 @@ def commit(self) -> Any: """Finalize once or let the current ``Scene`` consume pending changes.""" if not self.builder.is_finalized: self.resolve_sources() - result = self.builder.finalize() + with self._native_material_scope(): + result = self.builder.finalize() else: result = self.builder.result assert result is not None @@ -298,7 +321,8 @@ def commit(self) -> Any: self._pending_material_assignments = ( self._capture_material_assignments() ) - result = result.rebuild(self.builder) + with self._native_material_scope(): + result = result.rebuild(self.builder) for uid in self._assets: self._configure_materialized_source(uid) diff --git a/scripts/benchmark/rl/locomotion_ppo.py b/scripts/benchmark/rl/locomotion_ppo.py index a79fc5130e..2e98c14f98 100644 --- a/scripts/benchmark/rl/locomotion_ppo.py +++ b/scripts/benchmark/rl/locomotion_ppo.py @@ -410,4 +410,9 @@ def main() -> None: if __name__ == "__main__": - main() + from embodichain.lab.sim import SimulationManager + + try: + main() + finally: + SimulationManager.flush_cleanup_queue() diff --git a/tests/gym/envs/tasks/test_locomotion_cuda.py b/tests/gym/envs/tasks/test_locomotion_cuda.py index b6271b8163..5315618581 100644 --- a/tests/gym/envs/tasks/test_locomotion_cuda.py +++ b/tests/gym/envs/tasks/test_locomotion_cuda.py @@ -304,7 +304,12 @@ def _resume_checkpoint(backend: str, output: Path) -> None: if __name__ == "__main__": - if sys.argv[1] == "resume": - _resume_checkpoint(sys.argv[2], Path(sys.argv[3])) - else: - _run_locomotion_smoke(sys.argv[1], sys.argv[2]) + from embodichain.lab.sim import SimulationManager + + try: + if sys.argv[1] == "resume": + _resume_checkpoint(sys.argv[2], Path(sys.argv[3])) + else: + _run_locomotion_smoke(sys.argv[1], sys.argv[2]) + finally: + SimulationManager.flush_cleanup_queue() diff --git a/tests/sim/spawn/test_scene.py b/tests/sim/spawn/test_scene.py index 2dc2f62c7e..f8ce89fc7b 100644 --- a/tests/sim/spawn/test_scene.py +++ b/tests/sim/spawn/test_scene.py @@ -649,3 +649,36 @@ def test_no_render_retains_visual_declaration_without_native_transform_binding() assert "visual" in scene._assets scene.remove("visual") assert "visual" not in scene._assets + + +def test_no_render_excludes_visual_materials_during_native_spawn() -> None: + scene = object.__new__(SpawnScene) + material = MaterialDesc(name="textured", base_color_map="texture.png") + observed_materials = [] + + def add_object(descriptor): + observed_materials.append(dict(scene.builder.materials)) + return descriptor + + def finalize(): + observed_materials.append(dict(scene.builder.materials)) + return SimpleNamespace(handles={}) + + scene.builder = SimpleNamespace( + materials={"texture": material}, + is_finalized=False, + result=None, + backend="dexsim", + add_object=add_object, + finalize=finalize, + ) + scene._num_envs = 1 + scene._assets = {} + scene._material_resources = SimpleNamespace(renderer="no-render") + scene._resource_material_definitions = {} + + scene.declare("rigid_object", "box", SimpleNamespace(name="box", per_env=False)) + scene.commit() + + assert observed_materials == [{}, {}] + assert scene.builder.materials == {"texture": material} diff --git a/tests/sim/test_scoop_ice_demo.py b/tests/sim/test_scoop_ice_demo.py index 74a28daa8e..397180715e 100644 --- a/tests/sim/test_scoop_ice_demo.py +++ b/tests/sim/test_scoop_ice_demo.py @@ -112,48 +112,6 @@ def compute_ik(pose, **kwargs): torch.testing.assert_close(plan(moved_bin, tilted_grasp), moved_bin @ baseline) -@pytest.mark.gpu -@pytest.mark.requires_sim -@pytest.mark.slow -@pytest.mark.parametrize("seed", [0, 1, 2]) -def test_default_scoops_and_retains_ice(seed: int) -> None: - """Qualify the physical grasp and retained ice after the final settling step.""" - sim = demo.initialize_simulation( - demo.build_parser().parse_args(["--headless", "--physics", "default"]) - ) - try: - robot = demo.create_robot(sim) - demo.create_container(sim) - padding = demo.create_padding_box(sim) - scoop = demo.create_scoop(sim) - heavy = demo.create_heave_ice(sim) - ice = demo.create_ice_cubes(sim) - sim.prepare() - demo.randomize_ice_positions(sim, ice, seed=seed) - demo.scoop_grasp(sim, robot, scoop, heavy, padding) - demo.scoop_ice(sim, robot, scoop) - - assert scoop.get_local_pose()[0, 2] > 0.30 # Above the 22 cm bin rim. - relative = torch.linalg.inv(scoop.get_local_pose(to_matrix=True))[ - :, None - ] @ ice.get_local_pose(to_matrix=True) - centers = relative[0, :, :3, 3] - # Bowl bounds, including one cube radius above the upper surface. - retained = ( - (centers[:, 0].abs() < 0.055) - & (centers[:, 1] > -0.19) - & (centers[:, 1] < -0.04) - & (centers[:, 2] > -0.025) - & (centers[:, 2] < 0.065) - ) - count = int(retained.sum()) - print(f"seed={seed}: retained {count} ice cubes") - assert count > 0 - finally: - sim.destroy(exit_process=False) - demo.SimulationManager.flush_cleanup_queue() - - def test_ice_spawn_configs_are_separated_before_prepare(tmp_path, monkeypatch) -> None: """Newton evaluates initial contacts before runtime randomization can run.""" (tmp_path / "ice.obj").touch() From cd32f8442d7f1136bb0bb6743acee42d6ce1c151 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 16:49:37 +0800 Subject: [PATCH 07/13] ci: capture Python native crashes in test job --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 654fbb1442..615d2cabf4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -358,6 +358,7 @@ jobs: NVIDIA_DISABLE_REQUIRE: 1 HF_ENDPOINT: https://hf-mirror.com PIP_CACHE_DIR: /cache/pip + PYTHONFAULTHANDLER: "1" SDL_VIDEODRIVER: dummy XDG_RUNTIME_DIR: /tmp/embodichain-ci-runtime NEEDS_GENSIM: ${{ needs.test_plan.outputs.needs_gensim }} From 5fcefeb6e2d4b81e9bbb6bdbb37cfd8fd622cf19 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 18:26:22 +0800 Subject: [PATCH 08/13] ci: capture native GPU pytest crash with gdb --- .github/workflows/main.yml | 9 ++++++++- scripts/ci/run_test_plan.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 615d2cabf4..5819b52d86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -412,6 +412,13 @@ jobs: run: | python -c "import matplotlib, numpy, open3d, requests, scipy, shapely, trimesh; from PIL import Image; import embodichain.gen_sim.scene_engine.pipeline.generate" + - name: Install native debugger + run: | + if ! command -v gdb >/dev/null 2>&1; then + apt-get update + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gdb + fi + - name: Run selected tests shell: bash -l {0} run: | @@ -428,7 +435,7 @@ jobs: # Run GPU processes before the long DexSim lane. Native Vulkan/CUDA # teardown can briefly retain allocations after the sim subprocess; # this order prevents the GPU lane from inheriting that pressure. - python scripts/ci/run_test_plan.py \ + EMBODICHAIN_CI_GDB_GPU=1 python scripts/ci/run_test_plan.py \ --plan test-plan.json \ --repo-root "${GITHUB_WORKSPACE}" \ --lane fast \ diff --git a/scripts/ci/run_test_plan.py b/scripts/ci/run_test_plan.py index b1a55fe42a..d62131584b 100644 --- a/scripts/ci/run_test_plan.py +++ b/scripts/ci/run_test_plan.py @@ -290,6 +290,25 @@ def _run_resource_lane(lane: str, command: list[str], repository_root: Path) -> ) narrowed = _resource_command_for_nodes(command, nodes) narrowed.append(f"--ci-expected-collection={manifest}") + if lane == "gpu" and os.environ.get("EMBODICHAIN_CI_GDB_GPU") == "1": + narrowed = [ + "gdb", + "--quiet", + "--batch", + "-return-child-result", + "-ex", + "set pagination off", + "-ex", + "set print thread-events off", + "-ex", + "run", + "-ex", + "bt 40", + "-ex", + "info threads", + "--args", + *narrowed, + ] print(f"[CI] {lane}: selected nodes: {shlex.join(narrowed)}", flush=True) return subprocess.run( narrowed, cwd=repository_root, check=False, env=None From d89fa08af4b97b32b4baf87deb9fec97092f98d5 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 18:46:59 +0800 Subject: [PATCH 09/13] ci: scope gdb diagnostics to top-level gpu lane --- .github/workflows/main.yml | 3 ++- scripts/ci/run_test_plan.py | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5819b52d86..0854aff083 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -435,9 +435,10 @@ jobs: # Run GPU processes before the long DexSim lane. Native Vulkan/CUDA # teardown can briefly retain allocations after the sim subprocess; # this order prevents the GPU lane from inheriting that pressure. - EMBODICHAIN_CI_GDB_GPU=1 python scripts/ci/run_test_plan.py \ + python scripts/ci/run_test_plan.py \ --plan test-plan.json \ --repo-root "${GITHUB_WORKSPACE}" \ + --debug-gpu \ --lane fast \ --lane distributed \ --lane gpu \ diff --git a/scripts/ci/run_test_plan.py b/scripts/ci/run_test_plan.py index d62131584b..8e81d30ea4 100644 --- a/scripts/ci/run_test_plan.py +++ b/scripts/ci/run_test_plan.py @@ -265,7 +265,9 @@ def _resource_command_for_nodes(command: list[str], nodes: list[str]) -> list[st return [*command[:4], *nodes, *command[marker_index:]] -def _run_resource_lane(lane: str, command: list[str], repository_root: Path) -> int: +def _run_resource_lane( + lane: str, command: list[str], repository_root: Path, *, debug_gpu: bool = False +) -> int: """Discover selected nodes, then run the same ordered collection freshly.""" with tempfile.TemporaryDirectory(prefix="embodichain-test-selection-") as directory: manifest = Path(directory) / "selection.json" @@ -290,7 +292,7 @@ def _run_resource_lane(lane: str, command: list[str], repository_root: Path) -> ) narrowed = _resource_command_for_nodes(command, nodes) narrowed.append(f"--ci-expected-collection={manifest}") - if lane == "gpu" and os.environ.get("EMBODICHAIN_CI_GDB_GPU") == "1": + if lane == "gpu" and debug_gpu: narrowed = [ "gdb", "--quiet", @@ -322,6 +324,7 @@ def run_plan( python_executable: str | None = None, lanes: Sequence[str] | None = None, dry_run: bool = False, + debug_gpu: bool = False, ) -> int: """Execute all selected lanes and return the first failing exit code. @@ -335,6 +338,7 @@ def run_plan( Serial GPU and simulation lanes discover their selected nodes in a fresh collection-only process before execution. Discovery and execution are both included in the reported lane time; ordered nodes and markers must match. + ``debug_gpu`` runs only the selected GPU process under gdb. """ repository_root = Path(root).resolve() commands = build_commands( @@ -358,7 +362,9 @@ def run_plan( environment.setdefault(variable, "1") started = time.monotonic() if lane.removeprefix(_SLOW_LANE_PREFIX) in {"gpu", "sim"}: - returncode = _run_resource_lane(lane, command, repository_root) + returncode = _run_resource_lane( + lane, command, repository_root, debug_gpu=debug_gpu + ) else: returncode = subprocess.run( command, cwd=repository_root, check=False, env=environment @@ -386,6 +392,11 @@ def _parser() -> argparse.ArgumentParser: parser.add_argument("--python", dest="python_executable") parser.add_argument("--lane", action="append", dest="lanes") parser.add_argument("--dry-run", action="store_true") + parser.add_argument( + "--debug-gpu", + action="store_true", + help="Run the selected GPU pytest process under gdb for native crash diagnosis.", + ) return parser @@ -400,6 +411,7 @@ def main(argv: Sequence[str] | None = None) -> int: python_executable=args.python_executable, lanes=args.lanes, dry_run=args.dry_run, + debug_gpu=args.debug_gpu, ) except (OSError, RuntimeError, ValueError) as error: print(f"test plan execution failed: {error}", file=sys.stderr) From c80ed3087bd2036c1831fbf403462b73e79bb144 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 19:50:23 +0800 Subject: [PATCH 10/13] fix(ci): stop DexSim workers before pytest exits --- tests/conftest.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 004e168079..b06bfb9da7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -102,6 +102,23 @@ def pytest_configure(config): # a CUDA/Vulkan context merely because pytest has started. +@pytest.hookimpl(tryfirst=True) +def pytest_unconfigure(config: pytest.Config) -> None: + """Stop DexSim's process-wide workers before Python unloads native modules.""" + import gc + import sys + + dexsim = sys.modules.get("dexsim") + if dexsim is None: + return + manager_module = sys.modules.get("embodichain.lab.sim.sim_manager") + if manager_module is not None: + manager_module.SimulationManager.flush_cleanup_queue() + gc.collect() + if dexsim.get_world_num() == 0: + dexsim.quit() + + @pytest.hookimpl(trylast=True) def pytest_collection_finish(session: pytest.Session) -> None: """Record discovery or reject a changed selection before any test setup.""" From 2958f53f96bf2808fa64b4378bfff78e14b7ddcf Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 20:49:32 +0800 Subject: [PATCH 11/13] fix(ci): preserve GPU pytest result without native finalization --- .github/workflows/main.yml | 1 + scripts/ci/pytest_no_finalize.py | 45 ++++++++++++++++++++++++++++++++ scripts/ci/run_test_plan.py | 23 ++++++++++++++-- tests/ci/test_select_tests.py | 37 ++++++++++++++++++++++++++ tests/conftest.py | 17 ------------ 5 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 scripts/ci/pytest_no_finalize.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0854aff083..5e48a28de1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -439,6 +439,7 @@ jobs: --plan test-plan.json \ --repo-root "${GITHUB_WORKSPACE}" \ --debug-gpu \ + --no-finalize-gpu \ --lane fast \ --lane distributed \ --lane gpu \ diff --git a/scripts/ci/pytest_no_finalize.py b/scripts/ci/pytest_no_finalize.py new file mode 100644 index 0000000000..22299a1776 --- /dev/null +++ b/scripts/ci/pytest_no_finalize.py @@ -0,0 +1,45 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Run GPU pytest to completion without DexSim's unsafe native finalizers.""" + +from __future__ import annotations + +from collections.abc import Sequence +from typing import NoReturn + +import os +import sys + +import pytest + +__all__ = ["main"] + + +def main(argv: Sequence[str] | None = None) -> NoReturn: + """Return pytest's result after all hooks without unloading native libraries. + + Args: + argv: Pytest arguments, or ``sys.argv[1:]`` when omitted. + """ + exit_code = pytest.main(list(sys.argv[1:] if argv is None else argv)) + sys.stdout.flush() + sys.stderr.flush() + os._exit(int(exit_code)) + + +if __name__ == "__main__": + main() diff --git a/scripts/ci/run_test_plan.py b/scripts/ci/run_test_plan.py index 8e81d30ea4..55776dd2b9 100644 --- a/scripts/ci/run_test_plan.py +++ b/scripts/ci/run_test_plan.py @@ -266,7 +266,12 @@ def _resource_command_for_nodes(command: list[str], nodes: list[str]) -> list[st def _run_resource_lane( - lane: str, command: list[str], repository_root: Path, *, debug_gpu: bool = False + lane: str, + command: list[str], + repository_root: Path, + *, + debug_gpu: bool = False, + no_finalize_gpu: bool = False, ) -> int: """Discover selected nodes, then run the same ordered collection freshly.""" with tempfile.TemporaryDirectory(prefix="embodichain-test-selection-") as directory: @@ -292,6 +297,8 @@ def _run_resource_lane( ) narrowed = _resource_command_for_nodes(command, nodes) narrowed.append(f"--ci-expected-collection={manifest}") + if lane == "gpu" and no_finalize_gpu: + narrowed[2] = "scripts.ci.pytest_no_finalize" if lane == "gpu" and debug_gpu: narrowed = [ "gdb", @@ -325,6 +332,7 @@ def run_plan( lanes: Sequence[str] | None = None, dry_run: bool = False, debug_gpu: bool = False, + no_finalize_gpu: bool = False, ) -> int: """Execute all selected lanes and return the first failing exit code. @@ -339,6 +347,7 @@ def run_plan( collection-only process before execution. Discovery and execution are both included in the reported lane time; ordered nodes and markers must match. ``debug_gpu`` runs only the selected GPU process under gdb. + ``no_finalize_gpu`` returns after pytest hooks without unloading native libraries. """ repository_root = Path(root).resolve() commands = build_commands( @@ -363,7 +372,11 @@ def run_plan( started = time.monotonic() if lane.removeprefix(_SLOW_LANE_PREFIX) in {"gpu", "sim"}: returncode = _run_resource_lane( - lane, command, repository_root, debug_gpu=debug_gpu + lane, + command, + repository_root, + debug_gpu=debug_gpu, + no_finalize_gpu=no_finalize_gpu, ) else: returncode = subprocess.run( @@ -397,6 +410,11 @@ def _parser() -> argparse.ArgumentParser: action="store_true", help="Run the selected GPU pytest process under gdb for native crash diagnosis.", ) + parser.add_argument( + "--no-finalize-gpu", + action="store_true", + help="Return the GPU pytest result without native interpreter teardown.", + ) return parser @@ -412,6 +430,7 @@ def main(argv: Sequence[str] | None = None) -> int: lanes=args.lanes, dry_run=args.dry_run, debug_gpu=args.debug_gpu, + no_finalize_gpu=args.no_finalize_gpu, ) except (OSError, RuntimeError, ValueError) as error: print(f"test plan execution failed: {error}", file=sys.stderr) diff --git a/tests/ci/test_select_tests.py b/tests/ci/test_select_tests.py index 5c8829b9c7..4ef39b3794 100644 --- a/tests/ci/test_select_tests.py +++ b/tests/ci/test_select_tests.py @@ -19,6 +19,8 @@ import json import os from pathlib import Path +import subprocess +import sys from types import SimpleNamespace import pytest @@ -1231,3 +1233,38 @@ def run(*_args: object, **_kwargs: object) -> None: plan = {"version": 1, "mode": "full", "lanes": {"sim": ["tests"]}} assert run_test_plan.run_plan(plan, root=tmp_path, dry_run=True) == 0 + + +@pytest.mark.parametrize( + ("assertion", "summary", "exit_code"), + [("True", "1 passed", 0), ("False", "1 failed", 1)], +) +def test_no_finalize_runner_preserves_pytest_result( + tmp_path: Path, assertion: str, summary: str, exit_code: int +) -> None: + test_file = tmp_path / "test_contract.py" + finalizer_marker = tmp_path / "atexit-ran" + test_file.write_text( + "import atexit\n" + "from pathlib import Path\n" + f"atexit.register(lambda: Path({str(finalizer_marker)!r}).write_text('ran'))\n" + f"def test_contract():\n assert {assertion}\n", + encoding="utf-8", + ) + result = subprocess.run( + [ + sys.executable, + "-m", + "scripts.ci.pytest_no_finalize", + "-q", + str(test_file), + ], + cwd=Path(__file__).resolve().parents[2], + capture_output=True, + text=True, + timeout=30, + check=False, + ) + assert result.returncode == exit_code, result.stdout + result.stderr + assert summary in result.stdout + assert not finalizer_marker.exists() diff --git a/tests/conftest.py b/tests/conftest.py index b06bfb9da7..004e168079 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -102,23 +102,6 @@ def pytest_configure(config): # a CUDA/Vulkan context merely because pytest has started. -@pytest.hookimpl(tryfirst=True) -def pytest_unconfigure(config: pytest.Config) -> None: - """Stop DexSim's process-wide workers before Python unloads native modules.""" - import gc - import sys - - dexsim = sys.modules.get("dexsim") - if dexsim is None: - return - manager_module = sys.modules.get("embodichain.lab.sim.sim_manager") - if manager_module is not None: - manager_module.SimulationManager.flush_cleanup_queue() - gc.collect() - if dexsim.get_world_num() == 0: - dexsim.quit() - - @pytest.hookimpl(trylast=True) def pytest_collection_finish(session: pytest.Session) -> None: """Record discovery or reject a changed selection before any test setup.""" From 0cd7d948a89b2a0d18eee55574e1a827d569c027 Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 22:47:28 +0800 Subject: [PATCH 12/13] fix(ci): bypass native finalization in simulation lane --- .github/workflows/main.yml | 10 +------ scripts/ci/pytest_no_finalize.py | 2 +- scripts/ci/run_test_plan.py | 46 +++++++------------------------- tests/ci/test_select_tests.py | 19 +++++++++++-- 4 files changed, 28 insertions(+), 49 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5e48a28de1..5d90e169a2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -412,13 +412,6 @@ jobs: run: | python -c "import matplotlib, numpy, open3d, requests, scipy, shapely, trimesh; from PIL import Image; import embodichain.gen_sim.scene_engine.pipeline.generate" - - name: Install native debugger - run: | - if ! command -v gdb >/dev/null 2>&1; then - apt-get update - DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gdb - fi - - name: Run selected tests shell: bash -l {0} run: | @@ -438,8 +431,7 @@ jobs: python scripts/ci/run_test_plan.py \ --plan test-plan.json \ --repo-root "${GITHUB_WORKSPACE}" \ - --debug-gpu \ - --no-finalize-gpu \ + --no-finalize-resource-lanes \ --lane fast \ --lane distributed \ --lane gpu \ diff --git a/scripts/ci/pytest_no_finalize.py b/scripts/ci/pytest_no_finalize.py index 22299a1776..f4f58d8fa1 100644 --- a/scripts/ci/pytest_no_finalize.py +++ b/scripts/ci/pytest_no_finalize.py @@ -14,7 +14,7 @@ # limitations under the License. # ---------------------------------------------------------------------------- -"""Run GPU pytest to completion without DexSim's unsafe native finalizers.""" +"""Run resource-lane pytest without DexSim's unsafe native finalizers.""" from __future__ import annotations diff --git a/scripts/ci/run_test_plan.py b/scripts/ci/run_test_plan.py index 55776dd2b9..a64dc329e7 100644 --- a/scripts/ci/run_test_plan.py +++ b/scripts/ci/run_test_plan.py @@ -270,8 +270,7 @@ def _run_resource_lane( command: list[str], repository_root: Path, *, - debug_gpu: bool = False, - no_finalize_gpu: bool = False, + no_finalize_resource_lanes: bool = False, ) -> int: """Discover selected nodes, then run the same ordered collection freshly.""" with tempfile.TemporaryDirectory(prefix="embodichain-test-selection-") as directory: @@ -297,27 +296,8 @@ def _run_resource_lane( ) narrowed = _resource_command_for_nodes(command, nodes) narrowed.append(f"--ci-expected-collection={manifest}") - if lane == "gpu" and no_finalize_gpu: + if no_finalize_resource_lanes: narrowed[2] = "scripts.ci.pytest_no_finalize" - if lane == "gpu" and debug_gpu: - narrowed = [ - "gdb", - "--quiet", - "--batch", - "-return-child-result", - "-ex", - "set pagination off", - "-ex", - "set print thread-events off", - "-ex", - "run", - "-ex", - "bt 40", - "-ex", - "info threads", - "--args", - *narrowed, - ] print(f"[CI] {lane}: selected nodes: {shlex.join(narrowed)}", flush=True) return subprocess.run( narrowed, cwd=repository_root, check=False, env=None @@ -331,8 +311,7 @@ def run_plan( python_executable: str | None = None, lanes: Sequence[str] | None = None, dry_run: bool = False, - debug_gpu: bool = False, - no_finalize_gpu: bool = False, + no_finalize_resource_lanes: bool = False, ) -> int: """Execute all selected lanes and return the first failing exit code. @@ -346,8 +325,8 @@ def run_plan( Serial GPU and simulation lanes discover their selected nodes in a fresh collection-only process before execution. Discovery and execution are both included in the reported lane time; ordered nodes and markers must match. - ``debug_gpu`` runs only the selected GPU process under gdb. - ``no_finalize_gpu`` returns after pytest hooks without unloading native libraries. + ``no_finalize_resource_lanes`` returns after pytest hooks without unloading + native libraries in GPU and simulation processes. """ repository_root = Path(root).resolve() commands = build_commands( @@ -375,8 +354,7 @@ def run_plan( lane, command, repository_root, - debug_gpu=debug_gpu, - no_finalize_gpu=no_finalize_gpu, + no_finalize_resource_lanes=no_finalize_resource_lanes, ) else: returncode = subprocess.run( @@ -406,14 +384,9 @@ def _parser() -> argparse.ArgumentParser: parser.add_argument("--lane", action="append", dest="lanes") parser.add_argument("--dry-run", action="store_true") parser.add_argument( - "--debug-gpu", - action="store_true", - help="Run the selected GPU pytest process under gdb for native crash diagnosis.", - ) - parser.add_argument( - "--no-finalize-gpu", + "--no-finalize-resource-lanes", action="store_true", - help="Return the GPU pytest result without native interpreter teardown.", + help="Return GPU and simulation pytest results without native teardown.", ) return parser @@ -429,8 +402,7 @@ def main(argv: Sequence[str] | None = None) -> int: python_executable=args.python_executable, lanes=args.lanes, dry_run=args.dry_run, - debug_gpu=args.debug_gpu, - no_finalize_gpu=args.no_finalize_gpu, + no_finalize_resource_lanes=args.no_finalize_resource_lanes, ) except (OSError, RuntimeError, ValueError) as error: print(f"test plan execution failed: {error}", file=sys.stderr) diff --git a/tests/ci/test_select_tests.py b/tests/ci/test_select_tests.py index 4ef39b3794..31300c57d6 100644 --- a/tests/ci/test_select_tests.py +++ b/tests/ci/test_select_tests.py @@ -1095,8 +1095,12 @@ def _write_collection_manifest(command: list[str], files: list[str]) -> Path | N ["gpu", "sim", "slow-gpu", "slow-sim"], ids=["accelerator", "simulation", "slow-accelerator", "slow-simulation"], ) +@pytest.mark.parametrize("no_finalize_resource_lanes", [False, True]) def test_runner_narrows_native_resource_nodes_and_preserves_original_options( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, lane: str + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + lane: str, + no_finalize_resource_lanes: bool, ) -> None: original_selectors = [ "tests/empty.py", @@ -1134,11 +1138,22 @@ def run(command: list[str], **kwargs: object) -> SimpleNamespace: } original = dict(build_commands(plan, lanes=[base_lane]))[lane] - assert run_test_plan.run_plan(plan, root=tmp_path, lanes=[base_lane]) == 0 + assert ( + run_test_plan.run_plan( + plan, + root=tmp_path, + lanes=[base_lane], + no_finalize_resource_lanes=no_finalize_resource_lanes, + ) + == 0 + ) assert len(calls) == 2 discovery, execution = calls[0][0], calls[1][0] assert discovery[: len(original)] == original + assert execution[2] == ( + "scripts.ci.pytest_no_finalize" if no_finalize_resource_lanes else "pytest" + ) assert "--collect-only" in discovery and "--collect-only" not in execution marker_index = execution.index("-m", 3) assert execution[4:marker_index] == [ From 4d73538b0fa1c47d6aec3fae3a6e65a7b4f56f9e Mon Sep 17 00:00:00 2001 From: yuecideng Date: Sun, 8 Nov 2026 23:08:32 +0800 Subject: [PATCH 13/13] ci: switch main validation to Python 3.11 and isolate recorder tests --- .github/actions/activate-conda/action.yml | 10 +++++----- .github/workflows/main.yml | 12 ++++++------ tests/ci/test_workflows.py | 2 +- .../gym/envs/managers/test_async_dataset_functors.py | 5 ++++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/.github/actions/activate-conda/action.yml b/.github/actions/activate-conda/action.yml index 0c89191656..491c70a7fa 100644 --- a/.github/actions/activate-conda/action.yml +++ b/.github/actions/activate-conda/action.yml @@ -1,13 +1,13 @@ -name: Activate conda py312 environment -description: Activate py312 and expose its executables on PATH. +name: Activate conda py311 environment +description: Activate py311 and expose its executables on PATH. runs: using: composite steps: - - name: Activate conda py312 environment + - name: Activate conda py311 environment shell: bash -l {0} run: | - source activate py312 + source activate py311 echo "$CONDA_PREFIX/bin" >> "$GITHUB_PATH" - echo "EMBODICHAIN_ENV=py312" >> "$GITHUB_ENV" + echo "EMBODICHAIN_ENV=py311" >> "$GITHUB_ENV" python --version which python pip diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5d90e169a2..bb8c48d869 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,7 +37,7 @@ jobs: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.12' + python-version: '3.11' - name: Code Style check run: | echo "Workspace: ${GITHUB_WORKSPACE}" @@ -93,7 +93,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.12' + python-version: '3.11' - name: Install planner dependencies shell: bash run: | @@ -381,7 +381,7 @@ jobs: - name: Install test package shell: bash -l {0} run: | - source activate py312 + source activate py311 set -euo pipefail if [[ "${NEEDS_GENSIM}" == "true" ]]; then package='.[gensim]' @@ -415,7 +415,7 @@ jobs: - name: Run selected tests shell: bash -l {0} run: | - source activate py312 + source activate py311 set -euo pipefail install -d -m 0700 "$XDG_RUNTIME_DIR" # Login shells may use a different Git config from actions/checkout. @@ -452,7 +452,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.12' + python-version: '3.11' - name: Install docs test dependencies run: | python -m pip --isolated install --disable-pip-version-check \ @@ -538,7 +538,7 @@ jobs: - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: - python-version: '3.12' + python-version: '3.11' - name: Validate release versions shell: bash diff --git a/tests/ci/test_workflows.py b/tests/ci/test_workflows.py index b6f4c7e8a6..2a01f5a237 100644 --- a/tests/ci/test_workflows.py +++ b/tests/ci/test_workflows.py @@ -281,7 +281,7 @@ def test_test_job_restores_git_access_before_running_selected_tests( tests = next(step for step in steps if step.get("name") == "Run selected tests") assert tests["shell"] == "bash -l {0}" preparation = tests["run"].split("python scripts/ci/run_test_plan.py", 1)[0] - preparation = preparation.replace("source activate py312", ":").replace( + preparation = preparation.replace("source activate py311", ":").replace( "node --version", ":" ) repository = tmp_path / "checkout with spaces" diff --git a/tests/gym/envs/managers/test_async_dataset_functors.py b/tests/gym/envs/managers/test_async_dataset_functors.py index a8b00feee6..6b69454682 100644 --- a/tests/gym/envs/managers/test_async_dataset_functors.py +++ b/tests/gym/envs/managers/test_async_dataset_functors.py @@ -19,6 +19,7 @@ from __future__ import annotations import threading +from tempfile import TemporaryDirectory from unittest.mock import Mock, patch import pytest @@ -55,6 +56,8 @@ class _MockDataset: """Minimal LeRobotDataset stand-in that records save calls.""" def __init__(self): + self._save_root = TemporaryDirectory(prefix="test_async_recorder-") + self.save_path = self._save_root.name self.image_writer = None self.meta = Mock() self.meta.info = {"fps": 30} @@ -187,7 +190,7 @@ def _make_recorder( from embodichain.lab.gym.envs.managers.cfg import DatasetFunctorCfg params = { - "save_path": "/tmp/test_async_recorder", + "save_path": mock_dataset.save_path, "robot_meta": {"robot_type": "test"}, "instruction": {"lang": "test"}, "extra": {"scene_type": "s", "task_description": "t"},