diff --git a/AGENTS.md b/AGENTS.md index 427e0bc9b0b5..854ac7b8b290 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -322,3 +322,38 @@ To debug Warp kernel behavior: 1. **Write a standalone reproduction script** and run it directly with `./isaaclab.sh -p -c "..."` or `./isaaclab.sh -p script.py`. This keeps stdout visible and avoids the test framework entirely. 2. **Use high-precision format strings** for floating-point debugging (e.g., `wp.printf("val=%.15e\n", x)`) — the default `%f` format hides values smaller than ~1e-6 that can still affect control flow. 3. **Remove all `wp.printf` calls before committing.** + +### Warp mask-first boundaries + +Boolean environment masks are the canonical selection in Warp-first code; host-side +mask-to-ID compaction (`nonzero()` on device data) is a GPU-to-host synchronization +with a data-dependent shape, which both stalls the pipeline and permanently +disqualifies the containing code path from CUDA graph capture. + +1. **Syncs only inside named, sanctioned boundary helpers.** Host syncs + (`nonzero`, `argwhere`, `item`, `cpu`, `tolist`, `numpy`) on the mask-native + path live in small single-purpose functions listed in + `SANCTIONED_BOUNDARIES` of + `source/isaaclab_experimental/test/utils/test_mask_boundary_gate.py` — no + inline marker comments. The gate scans the experimental production tree in + full and `*mask*`-named functions in core/Newton; the list is exact, so + adding or removing a boundary is a conscious, reviewable test change. + Legitimate boundaries are host consumers: recorders, legacy compatibility + fallbacks, checkpoint/restore, and empty-reset predicates. +2. **Runtime backstop: `ISAACLAB_SYNC_DEBUG=1`.** Warp stages then execute under + `torch.cuda.set_sync_debug_mode("error")` (see `WarpGraphCache`), so a hidden + sync in any term — including future ones — raises at the exact call site. + Zero cost when the variable is unset. +3. **Capturability is annotated with `@WarpCapturable(False, reason=...)`** (the + single decorator form; works on term functions and class terms). Every + opt-out is pinned by the inventory test next to the gate. Unannotated terms + default to capturable and are covered by the runtime backstop. +4. **Mask-native Torch means elementwise masked ops.** Use `masked_fill_` or + `torch.where`; boolean advanced indexing (`buf[:, mask] = 0`) gathers indices + with a data-dependent allocation and is not capture-safe. +5. **Dual reset APIs.** Keep the ID-based `reset(env_ids)` signature untouched and + add a sibling `reset_mask(env_mask)`; the base-class fallback compacts (a + sanctioned boundary), mask-native implementations override it. Backends whose + assets accept but ignore `env_mask` (classic PhysX, a develop-era condition) + are a documented hazard on `InteractiveScene.reset`, not guarded machinery — + the warp env only drives Newton scenes. diff --git a/docs/source/overview/core-concepts/physical-backends/newton/warp-environments.rst b/docs/source/overview/core-concepts/physical-backends/newton/warp-environments.rst index 05c5bc848422..610d2af98fa1 100644 --- a/docs/source/overview/core-concepts/physical-backends/newton/warp-environments.rst +++ b/docs/source/overview/core-concepts/physical-backends/newton/warp-environments.rst @@ -31,38 +31,37 @@ Available Environments Direct Warp Environments ^^^^^^^^^^^^^^^^^^^^^^^^ -- ``Isaac-Cartpole-Direct-Warp-v0`` — Cartpole balance -- ``Isaac-Ant-Direct-Warp-v0`` — Ant locomotion -- ``Isaac-Humanoid-Direct-Warp-v0`` — Humanoid locomotion -- ``Isaac-Reorient-Cube-Allegro-Direct-Warp-v0`` — Allegro hand cube reorient +Direct tasks share the stable task configuration: the stable registration +declares its warp implementation through the ``warp_entry_point`` kwarg +(mirroring ``env_cfg_entry_point``), and ``--frontend warp`` swaps only the +environment class. Stable tasks with a declared warp implementation: +- ``Isaac-Cartpole-Direct`` — Cartpole balance +- ``Isaac-Ant-Direct`` — Ant locomotion +- ``Isaac-Humanoid-Direct`` — Humanoid locomotion +- ``Isaac-Reorient-Cube-Allegro-Direct`` — Allegro hand cube reorient -Manager-Based Warp Environments -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -**Classic** +Manager-Based Warp Execution (``--frontend warp``) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- ``Isaac-Cartpole-Warp-v0`` -- ``Isaac-Ant-Warp-v0`` -- ``Isaac-Humanoid-Warp-v0`` +Manager-based tasks do not need a parallel warp registration: the shared RL +CLI exposes ``--frontend {torch,warp}``, and ``--frontend warp`` adapts the +*stable* task configuration onto the warp runtime at build time (swapping each +MDP term for its warp twin). Select the Newton solver explicitly with +``presets=newton_mjwarp``. Stable tasks with full twin coverage: -**Locomotion (Flat)** +- ``Isaac-Cartpole`` +- ``Isaac-Ant`` +- ``Isaac-Humanoid`` +- ``Isaac-Reach-Franka``, ``Isaac-Reach-UR10`` +- ``Isaac-Velocity-Flat-AnymalD``, ``-Cassie``, ``-G1``, ``-H1``, ``-UnitreeGo2`` + (and their ``-Play`` variants) -- ``Isaac-Velocity-Flat-AnymalB-Warp-v0`` -- ``Isaac-Velocity-Flat-AnymalC-Warp-v0`` -- ``Isaac-Velocity-Flat-AnymalD-Warp-v0`` -- ``Isaac-Velocity-Flat-Cassie-Warp-v0`` -- ``Isaac-Velocity-Flat-G1-Warp-v0`` -- ``Isaac-Velocity-Flat-G1-Warp-v1`` -- ``Isaac-Velocity-Flat-H1-Warp-v0`` -- ``Isaac-Velocity-Flat-UnitreeA1-Warp-v0`` -- ``Isaac-Velocity-Flat-UnitreeGo1-Warp-v0`` -- ``Isaac-Velocity-Flat-UnitreeGo2-Warp-v0`` - -**Manipulation** - -- ``Isaac-Reach-Franka-Warp-v0`` -- ``Isaac-Reach-UR10-Warp-v0`` +A missing twin is a hard error listing the affected terms, so a partially +covered task fails at build time rather than silently changing behavior. +Rough-terrain velocity tasks remain unsupported until +:class:`~isaaclab.terrains.TerrainImporter` gains Warp APIs. Quick Start @@ -70,13 +69,17 @@ Quick Start .. code-block:: bash - # Direct workflow + # Direct workflow: stable task, warp env implementation ./isaaclab.sh train --rl_library rsl_rl \ - --task Isaac-Cartpole-Direct-Warp-v0 --num_envs 4096 + --task Isaac-Cartpole-Direct --frontend warp presets=newton_mjwarp --num_envs 4096 - # Manager-based workflow + # Manager-based workflow: stable task on the warp runtime ./isaaclab.sh train --rl_library rsl_rl \ - --task Isaac-Velocity-Flat-AnymalC-Warp-v0 --num_envs 4096 + --task Isaac-Cartpole --frontend warp presets=newton_mjwarp --num_envs 4096 + + # Manager-based workflow: velocity task on the warp runtime + ./isaaclab.sh train --rl_library rsl_rl \ + --task Isaac-Velocity-Flat-AnymalD --frontend warp presets=newton_mjwarp --num_envs 4096 All RL libraries with warp-compatible wrappers are supported: RSL-RL, RL Games, SKRL, and Stable-Baselines3. @@ -143,16 +146,6 @@ both running on the Newton physics backend. Measured over 300 iterations with 40 - 11,458 - 7,813 - -31.83% - * - AnymalB - - Manager - - 29,188 - - 21,781 - - -25.38% - * - AnymalC - - Manager - - 30,938 - - 22,228 - - -28.15% * - AnymalD - Manager - 32,294 @@ -173,17 +166,7 @@ both running on the Newton physics backend. Measured over 300 iterations with 40 - 22,202 - 15,864 - -28.55% - * - A1 - - Manager - - 15,257 - - 9,907 - - -35.07% - * - Go1 - - Manager - - 16,515 - - 11,869 - - -28.13% - * - Go2 + * - UnitreeGo2 - Manager - 15,221 - 9,966 @@ -200,7 +183,7 @@ overhead. Reading the table above: - **Manager-based classic RL** (Cartpole, Ant) — biggest gains (-52% to -54%). Many small reward / observation terms with low compute per term, so per-launch CPU overhead dominated the stable baseline. -- **Manager-based locomotion** (Anymal, G1, H1, Cassie, Unitree) — consistent -25% to -38% +- **Manager-based locomotion** (AnymalD, G1, H1, Cassie, UnitreeGo2) — consistent -21% to -38% range. The MDP has more terms but the underlying physics step is heavier, so the relative Python savings shrink. - **Direct workflow** — gains scale with how much the env's step body was Python (Ant -51%, @@ -228,7 +211,7 @@ specific to warp envs; for Newton physics limitations see :doc:`supported-featur ``class_type`` fields resolve to ``isaaclab_physx.*`` classes that depend on ``omni.physics.tensors`` (a Kit module the warp runtime does not initialise), and several warp APIs (env-mask reset, CUDA graph capture) require the Newton articulation. Configure - the cfg with a Newton physics block (or ``physics=newton_mjwarp``). + the cfg with a Newton physics block (or ``presets=newton_mjwarp``). **MDP coverage** @@ -266,25 +249,32 @@ to estimate the gain for your own task before committing to a migration. **Single-task A/B** +Run the same stable task id twice, differing only in ``--frontend``. Pass +``presets=newton_mjwarp`` to *both* runs so the physics backend is identical and the +measured difference isolates the frontend (manager pipeline + CUDA graph capture): + .. code-block:: bash - # Stable variant + # Torch frontend (stable managers) uv run isaaclab benchmark training \ --rl_library rsl_rl \ - --task -v0 \ + --task \ --num_envs 4096 \ --max_iterations 500 \ --benchmark_formatter summary \ - --output_path benchmarks/stable + --output_path benchmarks/torch \ + presets=newton_mjwarp - # Warp variant — same task with -Warp- suffix + # Warp frontend (warp managers, CUDA-graph captured) — same task id uv run isaaclab benchmark training \ --rl_library rsl_rl \ - --task -Warp-v0 \ + --task \ + --frontend warp \ --num_envs 4096 \ --max_iterations 500 \ --benchmark_formatter summary \ - --output_path benchmarks/warp + --output_path benchmarks/warp \ + presets=newton_mjwarp The ``summary`` formatter prints step time (min / mean / max) and total throughput. Compare "step time" between the two runs to estimate the gain per env step. @@ -292,7 +282,7 @@ The ``summary`` formatter prints step time (min / mean / max) and total throughp **Sweep across all available tasks** Run ``isaaclab benchmark training`` for each task in the stable set (cartpole, ant, humanoid, -locomotion, manipulation) and again with the ``-Warp-`` suffixed task ids, then diff the two output +locomotion, manipulation) and again with ``--frontend warp``, then diff the two output directories. **What to look at in the output** diff --git a/docs/source/overview/environments.rst b/docs/source/overview/environments.rst index 8e612678792f..b1ed9639915b 100644 --- a/docs/source/overview/environments.rst +++ b/docs/source/overview/environments.rst @@ -887,6 +887,14 @@ For environments that have a different task name listed under ``Inference Task N provided when running ``play.py`` or any inferencing workflows. These tasks provide more suitable configurations for inferencing, including reading from an already trained checkpoint and disabling runtime perturbations used for training. +.. note:: + + Warp-native environment implementations no longer register separate ``-Warp`` task ids. + The same stable tasks run on the Warp runtime by passing ``--frontend warp`` (together + with ``presets=newton_mjwarp``) to the training and play entrypoints; direct tasks + declare their Warp implementation through the ``warp_entry_point`` registration. See + :doc:`/overview/core-concepts/physical-backends/newton/warp-environments` for details. + .. START-AUTO-GENERATED: comprehensive-environment-list .. list-table:: @@ -907,16 +915,6 @@ inferencing, including reading from an already trained checkpoint and disabling - Direct - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``ovphysx``, ``physx`` - * - Isaac-Ant-Direct-Warp-v0 - - - - Direct - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - - - * - Isaac-Ant-Warp-v0 - - - - Manager Based - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO), **sb3** (PPO) - - * - Isaac-Cartpole - - Manager Based @@ -941,16 +939,6 @@ inferencing, including reading from an already trained checkpoint and disabling - Direct - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO), **sb3** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``ovphysx``, ``physx`` - * - Isaac-Cartpole-Direct-Warp-v0 - - - - Direct - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO), **sb3** (PPO) - - - * - Isaac-Cartpole-Warp-v0 - - - - Manager Based - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO), **sb3** (PPO) - - * - Isaac-Fourbar-Pole-Swingup - - Manager Based @@ -966,16 +954,6 @@ inferencing, including reading from an already trained checkpoint and disabling - Direct - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_mjwarp``, ``ovphysx``, ``physx`` - * - Isaac-Humanoid-Direct-Warp-v0 - - - - Direct - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - - - * - Isaac-Humanoid-Warp-v0 - - - - Manager Based - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO), **sb3** (PPO) - - * - Isaac-Lift-Cloth-Franka - - Manager Based @@ -1040,11 +1018,6 @@ inferencing, including reading from an already trained checkpoint and disabling - Manager Based - **rsl_rl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``physx`` - * - Isaac-Reach-Franka-Warp-v0 - - Isaac-Reach-Franka-Warp-Play-v0 - - Manager Based - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Reach-UR10 - Isaac-Reach-UR10-Play - Manager Based @@ -1060,11 +1033,6 @@ inferencing, including reading from an already trained checkpoint and disabling - Direct - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_mjwarp``, ``ovphysx``, ``physx`` - * - Isaac-Reorient-Cube-Allegro-Direct-Warp-v0 - - - - Direct - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Reorient-Cube-Shadow-Camera-Direct - Isaac-Reorient-Cube-Shadow-Camera-Direct-Play - Direct @@ -1111,36 +1079,16 @@ inferencing, including reading from an already trained checkpoint and disabling - Direct - **rl_games** (PPO), **skrl** (PPO, IPPO, MAPPO) - **physics=** ``newton_mjwarp``, ``physx`` - * - Isaac-Velocity-Flat-AnymalB-Warp-v0 - - Isaac-Velocity-Flat-AnymalB-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - - * - Isaac-Velocity-Flat-AnymalC-Warp-v0 - - Isaac-Velocity-Flat-AnymalC-Warp-Play-v0 - - Manager Based - - **rl_games** (PPO), **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Flat-AnymalD - Isaac-Velocity-Flat-AnymalD-Play - Manager Based - **rsl_rl** (PPO, DISTILLATION, DISTILLATION_RECURRENT, RECURRENT), **skrl** (PPO) - **physics=** ``newton_mjwarp``, ``ovphysx``, ``physx`` - * - Isaac-Velocity-Flat-AnymalD-Warp-v0 - - Isaac-Velocity-Flat-AnymalD-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Flat-Cassie - Isaac-Velocity-Flat-Cassie-Play - Manager Based - **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``physx`` - * - Isaac-Velocity-Flat-Cassie-Warp-v0 - - Isaac-Velocity-Flat-Cassie-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Flat-Digit - Isaac-Velocity-Flat-Digit-Play - Manager Based @@ -1151,46 +1099,21 @@ inferencing, including reading from an already trained checkpoint and disabling - Manager Based - **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``physx`` - * - Isaac-Velocity-Flat-G1-Warp-v0 - - Isaac-Velocity-Flat-G1-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Flat-H1 - Isaac-Velocity-Flat-H1-Play - Manager Based - **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``physx`` - * - Isaac-Velocity-Flat-H1-Warp-v0 - - Isaac-Velocity-Flat-H1-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Flat-Spot - Isaac-Velocity-Flat-Spot-Play - Manager Based - **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``physx`` - * - Isaac-Velocity-Flat-UnitreeA1-Warp-v0 - - Isaac-Velocity-Flat-UnitreeA1-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO), **sb3** (PPO) - - - * - Isaac-Velocity-Flat-UnitreeGo1-Warp-v0 - - Isaac-Velocity-Flat-UnitreeGo1-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Flat-UnitreeGo2 - Isaac-Velocity-Flat-UnitreeGo2-Play - Manager Based - **rsl_rl** (PPO), **skrl** (PPO) - **physics=** ``newton_kamino``, ``newton_mjwarp``, ``physx`` - * - Isaac-Velocity-Flat-UnitreeGo2-Warp-v0 - - Isaac-Velocity-Flat-UnitreeGo2-Warp-Play-v0 - - Manager Based - - **rsl_rl** (PPO), **skrl** (PPO) - - * - Isaac-Velocity-Rough-AnymalD - Isaac-Velocity-Rough-AnymalD-Play - Manager Based diff --git a/scripts/benchmarks/runtime.py b/scripts/benchmarks/runtime.py index e22875097655..7c2f2198f7e9 100644 --- a/scripts/benchmarks/runtime.py +++ b/scripts/benchmarks/runtime.py @@ -42,6 +42,17 @@ def _parse_args(argv: list[str]) -> tuple[argparse.Namespace, list[str]]: parser.add_argument("--task", type=str, required=True, help="Gym task id to benchmark.") parser.add_argument("--num_envs", type=int, default=None, help="Number of parallel environments.") parser.add_argument("--num_frames", type=int, default=100, help="Number of environment steps to benchmark.") + parser.add_argument("--warmup_frames", type=int, default=0, help="Untimed environment steps before measurement.") + parser.add_argument( + "--synchronize_steps", + action="store_true", + help="Synchronize CUDA before and after each step to measure completed-step latency.", + ) + parser.add_argument( + "--reuse_action_buffer", + action="store_true", + help="Reuse one action allocation and randomize its contents in-place between steps.", + ) parser.add_argument("--seed", type=int, default=None, help="Environment seed.") parser.add_argument("--output_path", type=str, default=".", help="Directory to write the output JSON.") parser.add_argument( @@ -123,6 +134,9 @@ def run(argv: list[str]) -> None: {"name": "task", "data": args.task}, {"name": "num_envs", "data": args.num_envs}, {"name": "num_frames", "data": args.num_frames}, + {"name": "warmup_frames", "data": args.warmup_frames}, + {"name": "synchronize_steps", "data": args.synchronize_steps}, + {"name": "reuse_action_buffer", "data": args.reuse_action_buffer}, {"name": "presets", "data": ",".join(cfg.presets)}, ] }, @@ -135,24 +149,31 @@ def run(argv: list[str]) -> None: num_envs = env.unwrapped.num_envs with BenchmarkMonitor(benchmark, interval=1.0): - step_times_s = stepping.run_runtime_loop(env, args.num_frames) + timing = stepping.measure_runtime_loop( + env, + args.num_frames, + warmup_frames=args.warmup_frames, + synchronize_steps=args.synchronize_steps, + reuse_action_buffer=args.reuse_action_buffer, + ) benchmark.update_manual_recorders() startup = StartupTime( app_launch=(app_t1 - app_t0) / 1e9, env_creation=(env_t1 - env_t0) / 1e9, - first_step=(step_times_s[0] if step_times_s else 0.0), + first_step=timing.first_step_s, python_imports=(imports_t1 - imports_t0) / 1e9, task_config=(task_config_t1 - task_config_t0) / 1e9, ) - fps = [num_envs / t for t in step_times_s if t > 0] + step_times_s = timing.step_times_s + instantaneous_fps = [num_envs / t for t in step_times_s if t > 0] runtime = builders.build_runtime( startup_time_s=startup, iteration_times_s=step_times_s, - collection_fps=fps, - total_fps=fps, + collection_fps=instantaneous_fps, + total_fps=instantaneous_fps, steps_per_iteration=num_envs, ) diff --git a/scripts/benchmarks/test/test_warp_launch_modes.py b/scripts/benchmarks/test/test_warp_launch_modes.py new file mode 100644 index 000000000000..7736f952900c --- /dev/null +++ b/scripts/benchmarks/test/test_warp_launch_modes.py @@ -0,0 +1,128 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Lightweight tests for the mixed Warp launch-mode benchmark.""" + +import argparse +import importlib.util +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest + +ROOT = Path(__file__).resolve().parents[3] +RUNNER = ROOT / "scripts" / "benchmarks" / "warp_launch_modes.py" + + +@pytest.fixture(scope="module") +def modes_module(): + """Load the standalone benchmark as a module without running its CLI.""" + spec = importlib.util.spec_from_file_location("warp_launch_modes_test_module", RUNNER) + assert spec is not None and spec.loader is not None + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + return module + + +def test_dry_run_builds_deterministic_cycles_and_launch_counts(modes_module, capsys: pytest.CaptureFixture[str]): + """The dry-run matrix should preserve the four-kernel cycle structure.""" + arguments = [ + "--dry_run", + "--threads", + "256,1024", + "--stage_repeats", + "1,4", + "--modes", + "eager,cache_eager", + "--case_order", + "randomized", + "--case_seed", + "17", + ] + assert modes_module.run(arguments) == 0 + first_output = capsys.readouterr().out + assert modes_module.run(arguments) == 0 + second_output = capsys.readouterr().out + + assert first_output == second_output + assert "Matrix cases: 8" in first_output + assert "eager__threads_256__nodes_4" in first_output + assert "cache_eager__threads_1024__nodes_16" in first_output + + +@pytest.mark.parametrize( + ("mode", "total_executions", "expected_action_checksum"), + [ + ("eager", 3, 2.0), + ("bare_dynamic_normal", 2, 2.0), + ("bare_dynamic_normal", 3, -2.0), + ("bare_dynamic_ctype_constructed", 3, -2.0), + ("bare_dynamic_ctype_prepacked", 3, -2.0), + ], +) +def test_semantic_expectations_track_dynamic_action_bank( + modes_module, mode: str, total_executions: int, expected_action_checksum: float +): + """Analytical checksums should account for alternating dynamic action pointers.""" + case = modes_module._Case(mode=mode, threads=8, stage_repeats=1) + expected = modes_module._semantic_expectations(case, total_executions) + + assert expected == { + "action_checksum": expected_action_checksum, + "observation_checksum": 4.8, + "reward_checksum": 7.896, + "reset_count": 0, + } + + +def test_mode_parser_exposes_explicit_no_conversion_variants(modes_module): + """Descriptor construction and prepacking must remain distinct CLI modes.""" + modes = modes_module._mode_list("bare_dynamic_ctype_constructed,bare_dynamic_ctype_prepacked,cache_eager") + assert modes == ( + "bare_dynamic_ctype_constructed", + "bare_dynamic_ctype_prepacked", + "cache_eager", + ) + with pytest.raises(argparse.ArgumentTypeError, match="unknown modes"): + modes_module._mode_list("bare_dynamic_ctype") + with pytest.raises(argparse.ArgumentTypeError, match="unknown modes"): + modes_module._mode_list("cache_dynamic_changed") + + +def test_default_case_order_is_randomized(modes_module): + """The benchmark should randomize cases by default to limit time-order bias.""" + assert modes_module._parse_args([]).case_order == "randomized" + + +def test_dynamic_probe_rejects_a_noop_setter(modes_module): + """Two same-sign outputs must not pass the explicit dynamic-pointer probe.""" + case = modes_module._Case(mode="bare_dynamic_normal", threads=8, stage_repeats=1) + + modes_module._validate_dynamic_probe(case, (-2.0, 2.0)) + with pytest.raises(RuntimeError, match="Dynamic action probe failed"): + modes_module._validate_dynamic_probe(case, (2.0, 2.0)) + + +def test_graph_activity_selection_prefers_graph_events(modes_module): + """Graph modes should prefer one graph event when child kernel events are also available.""" + case = modes_module._Case(mode="graph_eager", threads=8, stage_repeats=1) + kernel_results = [SimpleNamespace(elapsed=0.25) for _ in range(case.nodes_per_step)] + graph_results = [SimpleNamespace(elapsed=0.5)] + + selected = modes_module._select_gpu_activity(case, kernel_results, graph_results) + + assert selected == (1.0, 0.5, 0.5, "graph", 1) + + +def test_graph_activity_selection_accepts_child_kernel_events(modes_module): + """Graph modes should fall back to child kernels when no graph event is reported.""" + case = modes_module._Case(mode="graph_replay", threads=8, stage_repeats=1) + kernel_results = [SimpleNamespace(elapsed=0.25) for _ in range(case.nodes_per_step)] + + selected = modes_module._select_gpu_activity(case, kernel_results, []) + + assert selected == (1.0, None, 1.0, "kernel", case.nodes_per_step) diff --git a/scripts/benchmarks/warp_launch_modes.py b/scripts/benchmarks/warp_launch_modes.py new file mode 100644 index 000000000000..0dca666d9f1b --- /dev/null +++ b/scripts/benchmarks/warp_launch_modes.py @@ -0,0 +1,836 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Benchmark Warp launch modes on a mixed production-like kernel sequence. + +The sequence models the recurring action, observation, reward, and termination +kernel roles in the experimental Warp Cartpole task. It compares eager launches, +bare recorded commands, typed and documented no-conversion command updates, +the Isaac Lab ``WarpLaunchCache`` wrapper, and CUDA graphs. One-time +recording/capture costs, host submission, synchronized wall time, and GPU +activity are reported separately. + +Example:: + + ./isaaclab.sh -p scripts/benchmarks/warp_launch_modes.py \ + --device cuda:0 --threads 256,1024,4096,16384 \ + --stage_repeats 1,4,16,64 --output_dir results/warp_launch_modes +""" + +from __future__ import annotations + +import argparse +import csv +import dataclasses +import datetime +import hashlib +import itertools +import json +import math +import os +import pathlib +import platform +import random +import subprocess +import sys +import time +from collections.abc import Callable, Sequence +from typing import Any + +import warp as wp + +_REPOSITORY_ROOT = pathlib.Path(__file__).resolve().parents[2] +_ISAACLAB_SOURCE = _REPOSITORY_ROOT / "source" / "isaaclab" +if str(_ISAACLAB_SOURCE) not in sys.path: + sys.path.insert(0, str(_ISAACLAB_SOURCE)) + +from isaaclab.utils.warp import WarpLaunchCache # noqa: E402 + + +@wp.kernel +def update_actions( + input_actions: wp.array2d(dtype=wp.float32), + actions: wp.array2d(dtype=wp.float32), + action_scale: wp.float32, + cart_dof_idx: wp.int32, +): + """Apply one persistent action buffer to a Cartpole-shaped output.""" + env_index = wp.tid() + actions[env_index, cart_dof_idx] = action_scale * input_actions[env_index, 0] + + +@wp.kernel +def get_observations( + joint_pos: wp.array2d(dtype=wp.float32), + joint_vel: wp.array2d(dtype=wp.float32), + cart_dof_idx: wp.int32, + pole_dof_idx: wp.int32, + observations: wp.array(dtype=wp.vec4f), +): + """Gather four Cartpole-shaped state values.""" + env_index = wp.tid() + observations[env_index][0] = joint_pos[env_index, pole_dof_idx] + observations[env_index][1] = joint_vel[env_index, pole_dof_idx] + observations[env_index][2] = joint_pos[env_index, cart_dof_idx] + observations[env_index][3] = joint_vel[env_index, cart_dof_idx] + + +@wp.kernel +def compute_rewards( + rew_scale_alive: wp.float32, + rew_scale_terminated: wp.float32, + rew_scale_pole_pos: wp.float32, + rew_scale_cart_vel: wp.float32, + rew_scale_pole_vel: wp.float32, + joint_pos: wp.array2d(dtype=wp.float32), + joint_vel: wp.array2d(dtype=wp.float32), + cart_dof_idx: wp.int32, + pole_dof_idx: wp.int32, + reset_terminated: wp.array(dtype=wp.bool), + reward: wp.array(dtype=wp.float32), +): + """Compute a representative multi-input reward kernel.""" + env_index = wp.tid() + alive = wp.where(reset_terminated[env_index], wp.float32(0.0), rew_scale_alive) + terminated = wp.where(reset_terminated[env_index], rew_scale_terminated, wp.float32(0.0)) + pole_pos = joint_pos[env_index, pole_dof_idx] + reward[env_index] = ( + alive + + terminated + + rew_scale_pole_pos * pole_pos * pole_pos + + rew_scale_cart_vel * wp.abs(joint_vel[env_index, cart_dof_idx]) + + rew_scale_pole_vel * wp.abs(joint_vel[env_index, pole_dof_idx]) + ) + + +@wp.kernel +def get_dones( + joint_pos: wp.array2d(dtype=wp.float32), + episode_length: wp.array(dtype=wp.int32), + cart_dof_idx: wp.int32, + pole_dof_idx: wp.int32, + max_episode_length: wp.int32, + max_cart_pos: wp.float32, + out_of_bounds: wp.array(dtype=wp.bool), + time_out: wp.array(dtype=wp.bool), + reset: wp.array(dtype=wp.bool), +): + """Compute representative termination flags.""" + env_index = wp.tid() + out_of_bounds[env_index] = (wp.abs(joint_pos[env_index, cart_dof_idx]) > max_cart_pos) or ( + wp.abs(joint_pos[env_index, pole_dof_idx]) > wp.pi / 2.0 + ) + time_out[env_index] = episode_length[env_index] >= max_episode_length - 1 + reset[env_index] = out_of_bounds[env_index] or time_out[env_index] + + +_BASE_NODE_COUNT = 4 +_ALL_MODES = ( + "eager", + "cache_eager", + "bare_replay", + "bare_dynamic_normal", + "bare_dynamic_ctype_constructed", + "bare_dynamic_ctype_prepacked", + "cache_static", + "graph_eager", + "graph_replay", +) +_CSV_FIELDS = ( + "timestamp_utc", + "case_id", + "status", + "message", + "sample_index", + "mode", + "threads", + "stage_repeats", + "cycles_per_step", + "nodes_per_step", + "dynamic_nodes_per_step", + "dynamic_updates_per_step", + "setter_method", + "warmup_executions", + "record_setup_ms", + "capture_setup_ms", + "first_execution_batch_ms", + "cpu_submit_batch_ms", + "cpu_submit_us_per_node", + "synchronized_wall_batch_ms", + "synchronized_wall_us_per_node", + "gpu_kernel_batch_ms", + "gpu_graph_batch_ms", + "gpu_activity_batch_ms", + "gpu_activity_us_per_node", + "gpu_activity_type", + "gpu_activity_count", + "action_checksum", + "observation_checksum", + "reward_checksum", + "reset_count", + "dynamic_probe_first_action_checksum", + "dynamic_probe_second_action_checksum", + "semantic_validation", + "device", +) + + +@dataclasses.dataclass(frozen=True) +class _Case: + """One point in the mixed-sequence benchmark matrix.""" + + mode: str + threads: int + stage_repeats: int + + @property + def nodes_per_step(self) -> int: + return _BASE_NODE_COUNT * self.stage_repeats + + @property + def case_id(self) -> str: + return f"{self.mode}__threads_{self.threads}__nodes_{self.nodes_per_step}" + + +@dataclasses.dataclass +class _Node: + """One retained kernel call in the production-like sequence.""" + + kernel: wp.Kernel + arguments: list[Any] + dynamic_index: int | None = None + + +@dataclasses.dataclass +class _Workload: + """Buffers and nodes retained for one benchmark case.""" + + nodes: list[_Node] + action_banks: tuple[wp.array, wp.array] + actions: wp.array + reward: wp.array + observations: wp.array + reset_mask: wp.array + + +@dataclasses.dataclass +class _PreparedMode: + """Prepared steady-state callback and one-time setup metadata.""" + + execute: Callable[[], None] + record_setup_ms: float + capture_setup_ms: float + dynamic_nodes_per_step: int + dynamic_updates_per_step: int + setter_method: str + + +def run(argv: Sequence[str] | None = None) -> int: + """Run the selected matrix and return a process exit code.""" + args = _parse_args(argv) + cases = [ + _Case(mode=mode, threads=threads, stage_repeats=stage_repeats) + for mode, threads, stage_repeats in itertools.product(args.modes, args.threads, args.stage_repeats) + ] + if args.case_order == "randomized": + random.Random(args.case_seed).shuffle(cases) + if args.dry_run: + for case in cases: + print(case.case_id) + print(f"Matrix cases: {len(cases)}") + return 0 + + wp.init() + device = wp.get_device(args.device) + if not device.is_cuda: + raise RuntimeError("This benchmark requires a CUDA device because two modes use CUDA graphs.") + + started_utc = _utc_now() + run_name = args.run_name or f"warp_launch_modes_{started_utc.replace('-', '').replace(':', '')[:15]}Z" + _validate_run_name(run_name) + output_dir = pathlib.Path(args.output_dir).expanduser().resolve() + output_dir.mkdir(parents=True, exist_ok=True) + csv_path = output_dir / f"{run_name}.csv" + json_path = output_dir / f"{run_name}.json" + if csv_path.exists() or json_path.exists(): + raise FileExistsError(f"Refusing to overwrite existing benchmark output for run {run_name!r}.") + + kernel_source = pathlib.Path(__file__).resolve() + cache_source = pathlib.Path(sys.modules[WarpLaunchCache.__module__].__file__).resolve() + manifest: dict[str, Any] = { + "schema_version": 1, + "status": "running", + "started_utc": started_utc, + "completed_utc": None, + "command": [sys.executable, str(pathlib.Path(__file__).resolve()), *sys.argv[1:]], + "arguments": vars(args), + "matrix": {"total_cases": len(cases), "completed_cases": 0, "failed_cases": 0}, + "software": { + "python": platform.python_version(), + "warp": wp.__version__, + "warp_path": str(pathlib.Path(wp.__file__).resolve()), + "git_commit": _git_commit(), + }, + "sources": { + "benchmark": _file_metadata(pathlib.Path(__file__)), + "warp_launch_cache": _file_metadata(cache_source), + }, + "device": _device_metadata(device), + "kernel_sequence": { + "source": str(kernel_source), + "source_sha256": hashlib.sha256(kernel_source.read_bytes()).hexdigest(), + "base_nodes": ["update_actions", "get_observations", "compute_rewards", "get_dones"], + }, + "metric_notes": { + "cpu_submit_batch_ms": "Host time to submit one complete sequence; trailing synchronization excluded.", + "synchronized_wall_batch_ms": "Wall time with device synchronization before and after the sequence.", + "gpu_activity_batch_ms": "Warp CUDA activity attributed to kernels or the graph during the sequence.", + "record_setup_ms": ( + "Host time to record every command/replay-cache entry; replay-cache setup also submits its first call." + ), + "capture_setup_ms": "Host wall time to construct the CUDA graph after kernel precompilation.", + "dynamic_updates_per_step": ( + "Actual setter calls expected in steady state; unchanged cache tokens are elided." + ), + "bare_dynamic_ctype_constructed": ( + "Constructs an array descriptor inside every update, then uses Warp's documented " + "set_param_at_index_from_ctype no-conversion API." + ), + "bare_dynamic_ctype_prepacked": ( + "Reuses retained array descriptors constructed before steady-state timing, then uses Warp's " + "documented set_param_at_index_from_ctype no-conversion API." + ), + }, + "rows": [], + } + _write_outputs(csv_path, json_path, manifest, []) + + _precompile(device) + rows: list[dict[str, Any]] = [] + failed_cases = 0 + for case_index, case in enumerate(cases, start=1): + print(f"[{case_index}/{len(cases)}] {case.case_id}", flush=True) + try: + case_rows = _run_case(case, args.warmup_steps, args.warmup_seconds, args.repeats, device) + except Exception as exc: + failed_cases += 1 + case_rows = [ + _make_row( + case, + status="failed", + message=f"{type(exc).__name__}: {exc}", + sample_index=None, + device=device.alias, + ) + ] + print(f"FAILED {case.case_id}: {type(exc).__name__}: {exc}", file=sys.stderr, flush=True) + rows.extend(case_rows) + manifest["rows"] = rows + manifest["matrix"]["completed_cases"] = case_index - failed_cases + manifest["matrix"]["failed_cases"] = failed_cases + _write_outputs(csv_path, json_path, manifest, rows) + + manifest["status"] = "completed" if failed_cases == 0 else "completed_with_failures" + manifest["completed_utc"] = _utc_now() + _write_outputs(csv_path, json_path, manifest, rows) + print(f"Cases: {len(cases) - failed_cases} completed, {failed_cases} failed") + print(f"CSV: {csv_path}") + return 0 if failed_cases == 0 else 1 + + +def _parse_args(argv: Sequence[str] | None) -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Benchmark a mixed sequence of recurring Warp Cartpole kernels.") + parser.add_argument("--device", default="cuda:0") + parser.add_argument("--threads", type=_positive_int_list, default=(256, 1024, 4096, 16384)) + parser.add_argument("--stage_repeats", type=_positive_int_list, default=(1, 4, 16, 64)) + parser.add_argument("--modes", type=_mode_list, default=_ALL_MODES) + parser.add_argument("--warmup_steps", type=_nonnegative_int, default=5) + parser.add_argument( + "--warmup_seconds", + type=_nonnegative_float, + default=1.0, + help="Minimum synchronized warm-up duration per case to stabilize adaptive clocks.", + ) + parser.add_argument("--repeats", type=_positive_int, default=10) + parser.add_argument("--case_order", choices=("grouped", "randomized"), default="randomized") + parser.add_argument("--case_seed", type=int, default=271828) + parser.add_argument("--output_dir", default="results/warp_launch_modes") + parser.add_argument("--run_name", default=None) + parser.add_argument("--dry_run", action="store_true") + return parser.parse_args(argv) + + +def _positive_int(value: str) -> int: + parsed = int(value) + if parsed <= 0: + raise argparse.ArgumentTypeError("value must be positive") + return parsed + + +def _nonnegative_int(value: str) -> int: + parsed = int(value) + if parsed < 0: + raise argparse.ArgumentTypeError("value must be nonnegative") + return parsed + + +def _nonnegative_float(value: str) -> float: + parsed = float(value) + if parsed < 0.0: + raise argparse.ArgumentTypeError("value must be nonnegative") + return parsed + + +def _positive_int_list(value: str) -> tuple[int, ...]: + parsed = tuple(dict.fromkeys(_positive_int(item.strip()) for item in value.split(",") if item.strip())) + if not parsed: + raise argparse.ArgumentTypeError("at least one positive integer is required") + return parsed + + +def _mode_list(value: str) -> tuple[str, ...]: + parsed = tuple(dict.fromkeys(item.strip() for item in value.split(",") if item.strip())) + unknown = [mode for mode in parsed if mode not in _ALL_MODES] + if unknown: + raise argparse.ArgumentTypeError(f"unknown modes {unknown}; choose from {', '.join(_ALL_MODES)}") + if not parsed: + raise argparse.ArgumentTypeError("at least one mode is required") + return parsed + + +def _precompile(device: wp.Device) -> None: + """Compile every production kernel before setup timing.""" + workload = _make_workload(1, 1, device) + for node in workload.nodes: + wp.launch(node.kernel, dim=1, inputs=node.arguments, device=device) + wp.synchronize_device(device) + + +def _make_workload(threads: int, stage_repeats: int, device: wp.Device) -> _Workload: + """Allocate Cartpole-shaped buffers and construct the repeated node sequence.""" + joint_pos = wp.full((threads, 2), 0.1, dtype=wp.float32, device=device) + joint_vel = wp.full((threads, 2), 0.2, dtype=wp.float32, device=device) + action_banks = ( + wp.full((threads, 1), 0.25, dtype=wp.float32, device=device), + wp.full((threads, 1), -0.25, dtype=wp.float32, device=device), + ) + actions = wp.zeros((threads, 2), dtype=wp.float32, device=device) + observations = wp.zeros(threads, dtype=wp.vec4f, device=device) + episode_length = wp.zeros(threads, dtype=wp.int32, device=device) + out_of_bounds = wp.zeros(threads, dtype=wp.bool, device=device) + time_out = wp.zeros(threads, dtype=wp.bool, device=device) + reset_mask = wp.zeros(threads, dtype=wp.bool, device=device) + reward = wp.zeros(threads, dtype=wp.float32, device=device) + + nodes: list[_Node] = [] + for _ in range(stage_repeats): + nodes.extend( + ( + _Node( + update_actions, + [action_banks[0], actions, 1.0, 0], + dynamic_index=0, + ), + _Node( + get_observations, + [joint_pos, joint_vel, 0, 1, observations], + ), + _Node( + compute_rewards, + [1.0, -2.0, -1.0, -0.01, -0.005, joint_pos, joint_vel, 0, 1, out_of_bounds, reward], + ), + _Node( + get_dones, + [joint_pos, episode_length, 0, 1, 500, 3.0, out_of_bounds, time_out, reset_mask], + ), + ) + ) + return _Workload( + nodes=nodes, + action_banks=action_banks, + actions=actions, + reward=reward, + observations=observations, + reset_mask=reset_mask, + ) + + +def _record_commands(workload: _Workload, threads: int, device: wp.Device) -> tuple[list[wp.Launch], float]: + wp.synchronize_device(device) + start_ns = time.perf_counter_ns() + commands = [ + wp.launch(node.kernel, dim=threads, inputs=node.arguments, device=device, record_cmd=True) + for node in workload.nodes + ] + record_setup_ms = (time.perf_counter_ns() - start_ns) / 1.0e6 + if any(command is None for command in commands): + raise RuntimeError("wp.launch(record_cmd=True) did not return a command for every sequence node.") + return commands, record_setup_ms # type: ignore[return-value] + + +def _prepare_mode(case: _Case, workload: _Workload, device: wp.Device) -> _PreparedMode: + """Prepare one execution path and time its one-time setup.""" + record_setup_ms = 0.0 + capture_setup_ms = 0.0 + dynamic_nodes = sum(node.dynamic_index is not None for node in workload.nodes) + + if case.mode == "eager": + + def execute() -> None: + for node in workload.nodes: + wp.launch(node.kernel, dim=case.threads, inputs=node.arguments, device=device) + + return _PreparedMode(execute, 0.0, 0.0, 0, 0, "none") + + if case.mode in { + "bare_replay", + "bare_dynamic_normal", + "bare_dynamic_ctype_constructed", + "bare_dynamic_ctype_prepacked", + "graph_replay", + }: + commands, record_setup_ms = _record_commands(workload, case.threads, device) + else: + commands = [] + + if case.mode == "bare_replay": + + def execute() -> None: + for command in commands: + command.launch() + + return _PreparedMode(execute, record_setup_ms, 0.0, 0, 0, "none") + + if case.mode in { + "bare_dynamic_normal", + "bare_dynamic_ctype_constructed", + "bare_dynamic_ctype_prepacked", + }: + execution_index = 0 + action_descriptors = tuple(action.__ctype__() for action in workload.action_banks) + + def execute() -> None: + nonlocal execution_index + action_index = (execution_index + 1) & 1 + action = workload.action_banks[action_index] + for node, command in zip(workload.nodes, commands): + if node.dynamic_index is not None: + if case.mode == "bare_dynamic_normal": + command.set_param_at_index(node.dynamic_index, action) + elif case.mode == "bare_dynamic_ctype_constructed": + command.set_param_at_index_from_ctype(node.dynamic_index, action.__ctype__()) + else: + command.set_param_at_index_from_ctype(node.dynamic_index, action_descriptors[action_index]) + command.launch() + execution_index += 1 + + setter = { + "bare_dynamic_normal": "set_param_at_index", + "bare_dynamic_ctype_constructed": "set_param_at_index_from_ctype + array.__ctype__", + "bare_dynamic_ctype_prepacked": "set_param_at_index_from_ctype (prepacked)", + }[case.mode] + return _PreparedMode(execute, record_setup_ms, 0.0, dynamic_nodes, dynamic_nodes, setter) + + if case.mode in {"cache_eager", "cache_static"}: + cache = WarpLaunchCache( + mode="eager" if case.mode == "cache_eager" else "replay", + debug=False, + device=device, + ) + if case.mode != "cache_eager": + wp.synchronize_device(device) + start_ns = time.perf_counter_ns() + for node in workload.nodes: + cache.launch( + node.kernel, + dim=case.threads, + inputs=node.arguments, + ) + record_setup_ms = (time.perf_counter_ns() - start_ns) / 1.0e6 + + def execute() -> None: + for node in workload.nodes: + cache.launch( + node.kernel, + dim=case.threads, + inputs=node.arguments, + ) + + return _PreparedMode(execute, record_setup_ms, 0.0, 0, 0, "none") + + if case.mode in {"graph_eager", "graph_replay"}: + wp.synchronize_device(device) + start_ns = time.perf_counter_ns() + with wp.ScopedCapture(device=device) as capture: + if case.mode == "graph_eager": + for node in workload.nodes: + wp.launch(node.kernel, dim=case.threads, inputs=node.arguments, device=device) + else: + for command in commands: + command.launch() + capture_setup_ms = (time.perf_counter_ns() - start_ns) / 1.0e6 + graph = capture.graph + + def execute() -> None: + wp.capture_launch(graph) + + return _PreparedMode(execute, record_setup_ms, capture_setup_ms, 0, 0, "none") + + raise ValueError(f"Unknown mode: {case.mode}") + + +def _run_case( + case: _Case, + warmup_steps: int, + warmup_seconds: float, + repeats: int, + device: wp.Device, +) -> list[dict[str, Any]]: + workload = _make_workload(case.threads, case.stage_repeats, device) + prepared = _prepare_mode(case, workload, device) + + with wp.ScopedTimer("first_execution", print=False, synchronize=True) as first_timer: + prepared.execute() + warmup_executions = _warm_up(prepared.execute, warmup_steps, warmup_seconds, device) + + rows: list[dict[str, Any]] = [] + for sample_index in range(repeats): + wp.synchronize_device(device) + submit_start_ns = time.perf_counter_ns() + prepared.execute() + cpu_submit_batch_ms = (time.perf_counter_ns() - submit_start_ns) / 1.0e6 + wp.synchronize_device(device) + + with wp.ScopedTimer( + "synchronized_sequence", + print=False, + synchronize=True, + cuda_filter=wp.TIMING_KERNEL | wp.TIMING_GRAPH, + ) as synchronized_timer: + prepared.execute() + + kernel_results = [timing for timing in synchronized_timer.timing_results if timing.filter & wp.TIMING_KERNEL] + graph_results = [timing for timing in synchronized_timer.timing_results if timing.filter & wp.TIMING_GRAPH] + ( + gpu_kernel_batch_ms, + gpu_graph_batch_ms, + gpu_activity_batch_ms, + gpu_activity_type, + gpu_activity_count, + ) = _select_gpu_activity(case, kernel_results, graph_results) + expected_activity_count = ( + 1 if case.mode.startswith("graph_") and gpu_activity_type == "graph" else case.nodes_per_step + ) + if gpu_activity_count != expected_activity_count: + raise RuntimeError( + f"GPU activity count mismatch for {case.case_id}: measured {gpu_activity_count}, " + f"expected {expected_activity_count}." + ) + + rows.append( + _make_row( + case, + status="ok", + sample_index=sample_index, + dynamic_nodes_per_step=prepared.dynamic_nodes_per_step, + dynamic_updates_per_step=prepared.dynamic_updates_per_step, + setter_method=prepared.setter_method, + warmup_executions=warmup_executions, + record_setup_ms=prepared.record_setup_ms, + capture_setup_ms=prepared.capture_setup_ms, + first_execution_batch_ms=first_timer.elapsed, + cpu_submit_batch_ms=cpu_submit_batch_ms, + cpu_submit_us_per_node=cpu_submit_batch_ms * 1_000.0 / case.nodes_per_step, + synchronized_wall_batch_ms=synchronized_timer.elapsed, + synchronized_wall_us_per_node=synchronized_timer.elapsed * 1_000.0 / case.nodes_per_step, + gpu_kernel_batch_ms=gpu_kernel_batch_ms, + gpu_graph_batch_ms=gpu_graph_batch_ms, + gpu_activity_batch_ms=gpu_activity_batch_ms, + gpu_activity_us_per_node=( + gpu_activity_batch_ms * 1_000.0 / case.nodes_per_step if gpu_activity_batch_ms is not None else None + ), + gpu_activity_type=gpu_activity_type, + gpu_activity_count=gpu_activity_count, + device=device.alias, + ) + ) + + action_checksum = float(workload.actions.numpy().sum()) + observation_checksum = float(workload.observations.numpy().sum()) + reward_checksum = float(workload.reward.numpy().sum()) + reset_count = int(workload.reset_mask.numpy().sum()) + total_executions = 1 + warmup_executions + 2 * repeats + expected = _semantic_expectations(case, total_executions) + actual = { + "action_checksum": action_checksum, + "observation_checksum": observation_checksum, + "reward_checksum": reward_checksum, + "reset_count": reset_count, + } + if not all(math.isfinite(value) for value in (action_checksum, observation_checksum, reward_checksum)): + raise RuntimeError(f"Non-finite semantic checksum: {actual}") + if ( + not math.isclose(action_checksum, expected["action_checksum"], rel_tol=1.0e-5, abs_tol=1.0e-5) + or not math.isclose(observation_checksum, expected["observation_checksum"], rel_tol=1.0e-5, abs_tol=1.0e-5) + or not math.isclose(reward_checksum, expected["reward_checksum"], rel_tol=1.0e-5, abs_tol=1.0e-5) + or reset_count != expected["reset_count"] + ): + raise RuntimeError(f"Semantic validation failed: actual={actual}, expected={expected}") + + dynamic_probe_checksums: tuple[float | None, float | None] = (None, None) + if _is_changed_dynamic_mode(case.mode): + probe_checksums = [] + for _ in range(2): + prepared.execute() + wp.synchronize_device(device) + probe_checksums.append(float(workload.actions.numpy().sum())) + _validate_dynamic_probe(case, probe_checksums) + dynamic_probe_checksums = (probe_checksums[0], probe_checksums[1]) + for row in rows: + row.update(actual) + row["dynamic_probe_first_action_checksum"] = dynamic_probe_checksums[0] + row["dynamic_probe_second_action_checksum"] = dynamic_probe_checksums[1] + row["semantic_validation"] = "passed" + return rows + + +def _warm_up(execute: Callable[[], None], minimum_steps: int, minimum_seconds: float, device: wp.Device) -> int: + """Warm up a case until both count and elapsed-time thresholds are met.""" + executions = 0 + start = time.perf_counter() + while executions < minimum_steps or time.perf_counter() - start < minimum_seconds: + execute() + executions += 1 + if executions % 64 == 0: + wp.synchronize_device(device) + wp.synchronize_device(device) + return executions + + +def _select_gpu_activity( + case: _Case, + kernel_results: Sequence[Any], + graph_results: Sequence[Any], +) -> tuple[float | None, float | None, float | None, str, int]: + """Select the GPU activity representation appropriate for an execution mode.""" + gpu_kernel_batch_ms = sum(timing.elapsed for timing in kernel_results) if kernel_results else None + gpu_graph_batch_ms = sum(timing.elapsed for timing in graph_results) if graph_results else None + if case.mode.startswith("graph_") and gpu_graph_batch_ms is not None: + return gpu_kernel_batch_ms, gpu_graph_batch_ms, gpu_graph_batch_ms, "graph", len(graph_results) + if gpu_kernel_batch_ms is not None: + return gpu_kernel_batch_ms, gpu_graph_batch_ms, gpu_kernel_batch_ms, "kernel", len(kernel_results) + if gpu_graph_batch_ms is not None: + return gpu_kernel_batch_ms, gpu_graph_batch_ms, gpu_graph_batch_ms, "graph", len(graph_results) + return None, None, None, "none", 0 + + +def _semantic_expectations(case: _Case, total_executions: int) -> dict[str, float | int]: + """Return analytical checksums for the final state of a benchmark case.""" + final_action_sign = -1.0 if _is_changed_dynamic_mode(case.mode) and total_executions % 2 else 1.0 + return { + "action_checksum": final_action_sign * 0.25 * case.threads, + "observation_checksum": 0.6 * case.threads, + "reward_checksum": 0.987 * case.threads, + "reset_count": 0, + } + + +def _is_changed_dynamic_mode(mode: str) -> bool: + """Return whether an execution mode alternates its dynamic action pointer.""" + return mode in { + "bare_dynamic_normal", + "bare_dynamic_ctype_constructed", + "bare_dynamic_ctype_prepacked", + } + + +def _validate_dynamic_probe(case: _Case, checksums: Sequence[float]) -> None: + """Validate that two explicit post-timing executions used opposite action banks.""" + expected_magnitude = 0.25 * case.threads + if len(checksums) != 2 or not ( + math.isclose(checksums[0], -checksums[1], rel_tol=1.0e-5, abs_tol=1.0e-5) + and math.isclose(abs(checksums[0]), expected_magnitude, rel_tol=1.0e-5, abs_tol=1.0e-5) + ): + raise RuntimeError( + f"Dynamic action probe failed for {case.case_id}: measured {checksums}, " + f"expected alternating +/-{expected_magnitude}." + ) + + +def _make_row(case: _Case, **updates: Any) -> dict[str, Any]: + row: dict[str, Any] = dict.fromkeys(_CSV_FIELDS) + row.update( + { + "timestamp_utc": _utc_now(), + "case_id": case.case_id, + "mode": case.mode, + "threads": case.threads, + "stage_repeats": case.stage_repeats, + "cycles_per_step": case.stage_repeats, + "nodes_per_step": case.nodes_per_step, + } + ) + row.update(updates) + return row + + +def _write_outputs( + csv_path: pathlib.Path, + json_path: pathlib.Path, + manifest: dict[str, Any], + rows: list[dict[str, Any]], +) -> None: + csv_temporary = csv_path.with_name(f".{csv_path.name}.{os.getpid()}.tmp") + json_temporary = json_path.with_name(f".{json_path.name}.{os.getpid()}.tmp") + with csv_temporary.open("w", newline="", encoding="utf-8") as csv_file: + writer = csv.DictWriter(csv_file, fieldnames=_CSV_FIELDS) + writer.writeheader() + writer.writerows(rows) + os.replace(csv_temporary, csv_path) + with json_temporary.open("w", encoding="utf-8") as json_file: + json.dump(manifest, json_file, indent=2, sort_keys=True, allow_nan=False) + json_file.write("\n") + os.replace(json_temporary, json_path) + + +def _device_metadata(device: wp.Device) -> dict[str, Any]: + return { + "alias": device.alias, + "name": device.name, + "arch": getattr(device, "arch", None), + "ordinal": getattr(device, "ordinal", None), + "uuid": str(getattr(device, "uuid", "")), + "is_cuda": device.is_cuda, + } + + +def _git_commit() -> str | None: + result = subprocess.run( + ["git", "rev-parse", "HEAD"], + cwd=_REPOSITORY_ROOT, + check=False, + capture_output=True, + text=True, + ) + return result.stdout.strip() if result.returncode == 0 else None + + +def _file_metadata(path: pathlib.Path) -> dict[str, str]: + """Return an absolute source path and its content digest.""" + path = path.resolve() + return {"path": str(path), "sha256": hashlib.sha256(path.read_bytes()).hexdigest()} + + +def _validate_run_name(run_name: str) -> None: + if not run_name or pathlib.Path(run_name).name != run_name: + raise ValueError("run_name must be a non-empty filename stem without path separators") + + +def _utc_now() -> str: + return datetime.datetime.now(datetime.UTC).isoformat() + + +if __name__ == "__main__": + raise SystemExit(run(sys.argv[1:])) diff --git a/source/isaaclab/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst b/source/isaaclab/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst new file mode 100644 index 000000000000..6e203e14eb99 --- /dev/null +++ b/source/isaaclab/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst @@ -0,0 +1,15 @@ +Added +^^^^^ + +* Added :attr:`~isaaclab.terrains.TerrainImporter.env_origins_wp`, + :attr:`~isaaclab.terrains.TerrainImporter.terrain_levels_wp`, and + :meth:`~isaaclab.terrains.TerrainImporter.update_env_origins_mask` for zero-copy Warp terrain + access and mask-based curriculum updates. +* Added :attr:`~isaaclab.scene.InteractiveScene.env_origins_wp` and a boolean ``env_mask`` + option to :meth:`~isaaclab.scene.InteractiveScene.reset` for mask-based scene resets. + +Changed +^^^^^^^ + +* Changed the uniform pose and velocity command debug visualization to shared private mixins + reused by the Warp-native command terms. diff --git a/source/isaaclab/changelog.d/jichuanh-warp-frontend-execution.minor.rst b/source/isaaclab/changelog.d/jichuanh-warp-frontend-execution.minor.rst new file mode 100644 index 000000000000..115a38a8d515 --- /dev/null +++ b/source/isaaclab/changelog.d/jichuanh-warp-frontend-execution.minor.rst @@ -0,0 +1,5 @@ +Added +^^^^^ + +* Added :class:`~isaaclab.utils.warp.WarpLaunchCache` for owner-scoped eager + and recorded Warp kernel execution that composes with CUDA graph capture. diff --git a/source/isaaclab/changelog.d/warp-manager-bridge.rst b/source/isaaclab/changelog.d/warp-manager-bridge.rst new file mode 100644 index 000000000000..8aa615a5294a --- /dev/null +++ b/source/isaaclab/changelog.d/warp-manager-bridge.rst @@ -0,0 +1,8 @@ +Changed +^^^^^^^ + +* Relaxed ``func`` annotation on :class:`~isaaclab.managers.ObservationTermCfg`, + :class:`~isaaclab.managers.RewardTermCfg`, and + :class:`~isaaclab.managers.TerminationTermCfg` to ``Callable[..., torch.Tensor | None]`` + so kernel-style ``func(env, out) -> None`` terms type-check alongside the + existing torch return-tensor form. diff --git a/source/isaaclab/changelog.d/warp-scene-mask-native.minor.rst b/source/isaaclab/changelog.d/warp-scene-mask-native.minor.rst new file mode 100644 index 000000000000..b335a88a62ca --- /dev/null +++ b/source/isaaclab/changelog.d/warp-scene-mask-native.minor.rst @@ -0,0 +1,16 @@ +Added +^^^^^ + +* Added :meth:`~isaaclab.actuators.ActuatorBase.reset_mask` with a compacting + compatibility fallback and mask-native overrides for + :class:`~isaaclab.actuators.ActuatorNetLSTM` and + :class:`~isaaclab.actuators.ActuatorNetMLP`. +* Added :attr:`~isaaclab.scene.InteractiveScene.reset_capture_safe` and + :attr:`~isaaclab.sensors.SensorBase.reset_capture_safe` composition queries for + CUDA-graph capture eligibility of scene resets. + +Fixed +^^^^^ + +* Fixed the ray-caster masked reset to resample drift without materializing + compact environment IDs on the host. diff --git a/source/isaaclab/isaaclab/actuators/actuator_base.py b/source/isaaclab/isaaclab/actuators/actuator_base.py index 414437b41459..28c6e8856b53 100644 --- a/source/isaaclab/isaaclab/actuators/actuator_base.py +++ b/source/isaaclab/isaaclab/actuators/actuator_base.py @@ -269,6 +269,14 @@ def joint_indices(self) -> slice | torch.Tensor: Operations. """ + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + # A model that redefines :meth:`reset` without redefining :meth:`reset_mask` + # must not inherit an unrelated mask override (e.g. a stateless parent's + # no-op), so restore the compacting base fallback for it. + if "reset" in cls.__dict__ and "reset_mask" not in cls.__dict__: + cls.reset_mask = ActuatorBase.reset_mask + @abstractmethod def reset(self, env_ids: Sequence[int]): """Reset the internals within the group. @@ -278,6 +286,19 @@ def reset(self, env_ids: Sequence[int]): """ raise NotImplementedError + def reset_mask(self, env_mask: torch.Tensor) -> None: + """Reset the internals within the group for masked environments. + + The base implementation materializes compact environment IDs and delegates to + :meth:`reset`, so models that only implement the ID-based interface stay + compatible. Mask-native models override this method with elementwise masked + writes, which keeps the reset free of host synchronization. + + Args: + env_mask: Boolean mask selecting environments to reset, shape (num_envs,). + """ + self.reset(env_mask.nonzero(as_tuple=False).squeeze(-1)) # mask-boundary: legacy actuator API + @abstractmethod def compute( self, control_action: ArticulationActions, joint_pos: torch.Tensor, joint_vel: torch.Tensor diff --git a/source/isaaclab/isaaclab/actuators/actuator_net.py b/source/isaaclab/isaaclab/actuators/actuator_net.py index 2274d1b78db3..72cdb2ca8f1f 100644 --- a/source/isaaclab/isaaclab/actuators/actuator_net.py +++ b/source/isaaclab/isaaclab/actuators/actuator_net.py @@ -74,6 +74,14 @@ def reset(self, env_ids: Sequence[int]): self.sea_hidden_state_per_env[:, env_ids] = 0.0 self.sea_cell_state_per_env[:, env_ids] = 0.0 + def reset_mask(self, env_mask: torch.Tensor) -> None: + # Elementwise masked fill keeps the reset free of host synchronization and + # data-dependent allocation (boolean advanced indexing would gather indices). + with torch.no_grad(): + mask = env_mask.view(1, -1, 1, 1) + self.sea_hidden_state_per_env.masked_fill_(mask, 0.0) + self.sea_cell_state_per_env.masked_fill_(mask, 0.0) + def compute( self, control_action: ArticulationActions, joint_pos: torch.Tensor, joint_vel: torch.Tensor ) -> ArticulationActions: @@ -143,6 +151,13 @@ def reset(self, env_ids: Sequence[int]): self._joint_pos_error_history[env_ids] = 0.0 self._joint_vel_history[env_ids] = 0.0 + def reset_mask(self, env_mask: torch.Tensor) -> None: + # Elementwise masked fill keeps the reset free of host synchronization and + # data-dependent allocation (boolean advanced indexing would gather indices). + mask = env_mask.view(-1, 1, 1) + self._joint_pos_error_history.masked_fill_(mask, 0.0) + self._joint_vel_history.masked_fill_(mask, 0.0) + def compute( self, control_action: ArticulationActions, joint_pos: torch.Tensor, joint_vel: torch.Tensor ) -> ArticulationActions: diff --git a/source/isaaclab/isaaclab/actuators/actuator_pd.py b/source/isaaclab/isaaclab/actuators/actuator_pd.py index e9ed37d342a9..fa3e998a6d59 100644 --- a/source/isaaclab/isaaclab/actuators/actuator_pd.py +++ b/source/isaaclab/isaaclab/actuators/actuator_pd.py @@ -114,6 +114,10 @@ def reset(self, *args, **kwargs): # This is a no-op. There is no state to reset for implicit actuators. pass + def reset_mask(self, env_mask: torch.Tensor) -> None: + # This is a no-op. There is no state to reset for implicit actuators. + pass + def compute( self, control_action: ArticulationActions, joint_pos: torch.Tensor, joint_vel: torch.Tensor ) -> ArticulationActions: @@ -183,6 +187,10 @@ class IdealPDActuator(ActuatorBase): def reset(self, env_ids: Sequence[int]): pass + def reset_mask(self, env_mask: torch.Tensor) -> None: + # This is a no-op. There is no state to reset for ideal PD actuators. + pass + def compute( self, control_action: ArticulationActions, joint_pos: torch.Tensor, joint_vel: torch.Tensor ) -> ArticulationActions: diff --git a/source/isaaclab/isaaclab/envs/mdp/commands/_debug_vis.py b/source/isaaclab/isaaclab/envs/mdp/commands/_debug_vis.py new file mode 100644 index 000000000000..6e6c6301f101 --- /dev/null +++ b/source/isaaclab/isaaclab/envs/mdp/commands/_debug_vis.py @@ -0,0 +1,112 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Private debug-visualization mixins for command terms.""" + +from __future__ import annotations + +import torch + +import isaaclab.utils.math as math_utils +from isaaclab.markers import VisualizationMarkers + + +class _VelocityCommandDebugVis: + """Debug-visualization mixin shared by uniform velocity command terms.""" + + def _set_debug_vis_impl(self, debug_vis: bool): + # set visibility of markers + # note: parent only deals with callbacks. not their visibility + if debug_vis: + # create markers if necessary for the first time + if not hasattr(self, "goal_vel_visualizer"): + # -- goal + self.goal_vel_visualizer = VisualizationMarkers(self.cfg.goal_vel_visualizer_cfg) + # -- current + self.current_vel_visualizer = VisualizationMarkers(self.cfg.current_vel_visualizer_cfg) + # set their visibility to true + self.goal_vel_visualizer.set_visibility(True) + self.current_vel_visualizer.set_visibility(True) + else: + if hasattr(self, "goal_vel_visualizer"): + self.goal_vel_visualizer.set_visibility(False) + self.current_vel_visualizer.set_visibility(False) + + def _debug_vis_callback(self, event): + # check if robot is initialized + # note: this is needed in-case the robot is de-initialized. we can't access the data + if not self.robot.is_initialized: + return + # get marker location + # -- base state + base_pos_w = self.robot.data.root_pos_w.torch.clone() + base_pos_w[:, 2] += 0.5 + # -- resolve the scales and quaternions + vel_des_arrow_scale, vel_des_arrow_quat = self._resolve_xy_velocity_to_arrow(self.command[:, :2]) + vel_arrow_scale, vel_arrow_quat = self._resolve_xy_velocity_to_arrow( + self.robot.data.root_lin_vel_b.torch[:, :2] + ) + # display markers + self.goal_vel_visualizer.visualize(base_pos_w, vel_des_arrow_quat, vel_des_arrow_scale) + self.current_vel_visualizer.visualize(base_pos_w, vel_arrow_quat, vel_arrow_scale) + + def _resolve_xy_velocity_to_arrow(self, xy_velocity: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: + """Converts the XY base velocity command to arrow direction rotation.""" + # obtain default scale of the marker + default_scale = self.goal_vel_visualizer.cfg.markers["arrow"].scale + # arrow-scale + arrow_scale = torch.tensor(default_scale, device=self.device).repeat(xy_velocity.shape[0], 1) + arrow_scale[:, 0] *= torch.linalg.norm(xy_velocity, dim=1) * 3.0 + # arrow-direction + heading_angle = torch.atan2(xy_velocity[:, 1], xy_velocity[:, 0]) + zeros = torch.zeros_like(heading_angle) + arrow_quat = math_utils.quat_from_euler_xyz(zeros, zeros, heading_angle) + # convert everything back from base to world frame + base_quat_w = self.robot.data.root_quat_w.torch + arrow_quat = math_utils.quat_mul(base_quat_w, arrow_quat) + + return arrow_scale, arrow_quat + + +class _PoseCommandDebugVis: + """Debug-visualization mixin shared by uniform pose command terms.""" + + def _set_debug_vis_impl(self, debug_vis: bool): + # create markers if necessary for the first time + if debug_vis: + if not hasattr(self, "goal_pose_visualizer"): + # -- goal pose + self.goal_pose_visualizer = VisualizationMarkers(self.cfg.goal_pose_visualizer_cfg) + # -- current body pose + self.current_pose_visualizer = VisualizationMarkers(self.cfg.current_pose_visualizer_cfg) + # set their visibility to true + self.goal_pose_visualizer.set_visibility(True) + self.current_pose_visualizer.set_visibility(True) + else: + if hasattr(self, "goal_pose_visualizer"): + self.goal_pose_visualizer.set_visibility(False) + self.current_pose_visualizer.set_visibility(False) + + def _debug_vis_callback(self, event): + # check if robot is initialized + # note: this is needed in-case the robot is de-initialized. we can't access the data + if not self.robot.is_initialized: + return + # update the markers + # -- goal pose + pose_command_w = self._debug_pose_command_w() + self.goal_pose_visualizer.visualize(pose_command_w[:, :3], pose_command_w[:, 3:]) + # -- current body pose + body_link_pose_w = self.robot.data.body_link_pose_w.torch[:, self.body_idx] + self.current_pose_visualizer.visualize(body_link_pose_w[:, :3], body_link_pose_w[:, 3:7]) + + def _debug_pose_command_w(self) -> torch.Tensor: + """Return the world-frame pose command consumed by the goal marker. + + The value is the desired pose in the simulation world frame [m, m, m, quaternion xyzw], + shape ``(num_envs, 7)``. Warp-native terms override this to expose the Torch alias of + their pointer-stable Warp buffer. + """ + return self.pose_command_w diff --git a/source/isaaclab/isaaclab/envs/mdp/commands/pose_command.py b/source/isaaclab/isaaclab/envs/mdp/commands/pose_command.py index d98a6b5e7f81..efecc3e2a1d0 100644 --- a/source/isaaclab/isaaclab/envs/mdp/commands/pose_command.py +++ b/source/isaaclab/isaaclab/envs/mdp/commands/pose_command.py @@ -14,17 +14,18 @@ from isaaclab.assets import Articulation from isaaclab.managers import CommandTerm -from isaaclab.markers import VisualizationMarkers from isaaclab.utils.leapp import POSE7_ELEMENT_NAMES from isaaclab.utils.math import combine_frame_transforms, compute_pose_error, quat_from_euler_xyz, quat_unique +from ._debug_vis import _PoseCommandDebugVis + if TYPE_CHECKING: from isaaclab.envs import ManagerBasedEnv from .commands_cfg import UniformPoseCommandCfg -class UniformPoseCommand(CommandTerm): +class UniformPoseCommand(_PoseCommandDebugVis, CommandTerm): """Command generator for generating pose commands uniformly. The command generator generates poses by sampling positions uniformly within specified @@ -151,31 +152,3 @@ def _resample_command(self, env_ids: Sequence[int]): def _update_command(self): pass - - def _set_debug_vis_impl(self, debug_vis: bool): - # create markers if necessary for the first time - if debug_vis: - if not hasattr(self, "goal_pose_visualizer"): - # -- goal pose - self.goal_pose_visualizer = VisualizationMarkers(self.cfg.goal_pose_visualizer_cfg) - # -- current body pose - self.current_pose_visualizer = VisualizationMarkers(self.cfg.current_pose_visualizer_cfg) - # set their visibility to true - self.goal_pose_visualizer.set_visibility(True) - self.current_pose_visualizer.set_visibility(True) - else: - if hasattr(self, "goal_pose_visualizer"): - self.goal_pose_visualizer.set_visibility(False) - self.current_pose_visualizer.set_visibility(False) - - def _debug_vis_callback(self, event): - # check if robot is initialized - # note: this is needed in-case the robot is de-initialized. we can't access the data - if not self.robot.is_initialized: - return - # update the markers - # -- goal pose - self.goal_pose_visualizer.visualize(self.pose_command_w[:, :3], self.pose_command_w[:, 3:]) - # -- current body pose - body_link_pose_w = self.robot.data.body_link_pose_w.torch[:, self.body_idx] - self.current_pose_visualizer.visualize(body_link_pose_w[:, :3], body_link_pose_w[:, 3:7]) diff --git a/source/isaaclab/isaaclab/envs/mdp/commands/velocity_command.py b/source/isaaclab/isaaclab/envs/mdp/commands/velocity_command.py index 930f663f65d3..31fad8296bc4 100644 --- a/source/isaaclab/isaaclab/envs/mdp/commands/velocity_command.py +++ b/source/isaaclab/isaaclab/envs/mdp/commands/velocity_command.py @@ -16,7 +16,8 @@ import isaaclab.utils.math as math_utils from isaaclab.assets import Articulation from isaaclab.managers import CommandTerm -from isaaclab.markers import VisualizationMarkers + +from ._debug_vis import _VelocityCommandDebugVis if TYPE_CHECKING: from isaaclab.envs import ManagerBasedEnv @@ -27,7 +28,7 @@ logger = logging.getLogger(__name__) -class UniformVelocityCommand(CommandTerm): +class UniformVelocityCommand(_VelocityCommandDebugVis, CommandTerm): r"""Command generator that generates a velocity command in SE(2) from uniform distribution. The command comprises of a linear velocity in x and y direction and an angular velocity around @@ -197,63 +198,6 @@ def _update_command(self): standing_env_ids = self.is_standing_env.nonzero(as_tuple=False).flatten() self.vel_command_b[standing_env_ids, :] = 0.0 - def _set_debug_vis_impl(self, debug_vis: bool): - # set visibility of markers - # note: parent only deals with callbacks. not their visibility - if debug_vis: - # create markers if necessary for the first time - if not hasattr(self, "goal_vel_visualizer"): - # -- goal - self.goal_vel_visualizer = VisualizationMarkers(self.cfg.goal_vel_visualizer_cfg) - # -- current - self.current_vel_visualizer = VisualizationMarkers(self.cfg.current_vel_visualizer_cfg) - # set their visibility to true - self.goal_vel_visualizer.set_visibility(True) - self.current_vel_visualizer.set_visibility(True) - else: - if hasattr(self, "goal_vel_visualizer"): - self.goal_vel_visualizer.set_visibility(False) - self.current_vel_visualizer.set_visibility(False) - - def _debug_vis_callback(self, event): - # check if robot is initialized - # note: this is needed in-case the robot is de-initialized. we can't access the data - if not self.robot.is_initialized: - return - # get marker location - # -- base state - base_pos_w = self.robot.data.root_pos_w.torch.clone() - base_pos_w[:, 2] += 0.5 - # -- resolve the scales and quaternions - vel_des_arrow_scale, vel_des_arrow_quat = self._resolve_xy_velocity_to_arrow(self.command[:, :2]) - vel_arrow_scale, vel_arrow_quat = self._resolve_xy_velocity_to_arrow( - self.robot.data.root_lin_vel_b.torch[:, :2] - ) - # display markers - self.goal_vel_visualizer.visualize(base_pos_w, vel_des_arrow_quat, vel_des_arrow_scale) - self.current_vel_visualizer.visualize(base_pos_w, vel_arrow_quat, vel_arrow_scale) - - """ - Internal helpers. - """ - - def _resolve_xy_velocity_to_arrow(self, xy_velocity: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: - """Converts the XY base velocity command to arrow direction rotation.""" - # obtain default scale of the marker - default_scale = self.goal_vel_visualizer.cfg.markers["arrow"].scale - # arrow-scale - arrow_scale = torch.tensor(default_scale, device=self.device).repeat(xy_velocity.shape[0], 1) - arrow_scale[:, 0] *= torch.linalg.norm(xy_velocity, dim=1) * 3.0 - # arrow-direction - heading_angle = torch.atan2(xy_velocity[:, 1], xy_velocity[:, 0]) - zeros = torch.zeros_like(heading_angle) - arrow_quat = math_utils.quat_from_euler_xyz(zeros, zeros, heading_angle) - # convert everything back from base to world frame - base_quat_w = self.robot.data.root_quat_w.torch - arrow_quat = math_utils.quat_mul(base_quat_w, arrow_quat) - - return arrow_scale, arrow_quat - class NormalVelocityCommand(UniformVelocityCommand): """Command generator that generates a velocity command in SE(2) from a normal distribution. diff --git a/source/isaaclab/isaaclab/envs/mdp/curriculums.py b/source/isaaclab/isaaclab/envs/mdp/curriculums.py index 8438e5bec925..d48505736152 100644 --- a/source/isaaclab/isaaclab/envs/mdp/curriculums.py +++ b/source/isaaclab/isaaclab/envs/mdp/curriculums.py @@ -294,3 +294,16 @@ def __init__(self, cfg, env): super().__init__(cfg, env) # overwrite the simplified address with the full manager path self._address = self._address.replace("s.", "_manager.cfg.", 1) + + def _process_accessors(self, root: ManagerBasedRLEnv, path: str) -> tuple[callable, callable]: + """Build accessors that invalidate cached Warp work after a term changes.""" + get_value, set_value = super()._process_accessors(root, path) + invalidate_wp_graphs = getattr(root, "invalidate_wp_graphs", None) + if not callable(invalidate_wp_graphs): + return get_value, set_value + + def set_value_and_invalidate(value): + set_value(value) + invalidate_wp_graphs() + + return get_value, set_value_and_invalidate diff --git a/source/isaaclab/isaaclab/managers/manager_term_cfg.py b/source/isaaclab/isaaclab/managers/manager_term_cfg.py index 29e2281c3c60..3fad3881679d 100644 --- a/source/isaaclab/isaaclab/managers/manager_term_cfg.py +++ b/source/isaaclab/isaaclab/managers/manager_term_cfg.py @@ -152,7 +152,7 @@ class CurriculumTermCfg(ManagerTermBaseCfg): class ObservationTermCfg(ManagerTermBaseCfg): """Configuration for an observation term.""" - func: Callable[..., torch.Tensor] = MISSING + func: Callable[..., torch.Tensor | None] = MISSING """The name of the function to be called. This function should take the environment object and any other parameters @@ -335,7 +335,7 @@ class EventTermCfg(ManagerTermBaseCfg): class RewardTermCfg(ManagerTermBaseCfg): """Configuration for a reward term.""" - func: Callable[..., torch.Tensor] = MISSING + func: Callable[..., torch.Tensor | None] = MISSING """The name of the function to be called. This function should take the environment object and any other parameters @@ -363,7 +363,7 @@ class RewardTermCfg(ManagerTermBaseCfg): class TerminationTermCfg(ManagerTermBaseCfg): """Configuration for a termination term.""" - func: Callable[..., torch.Tensor] = MISSING + func: Callable[..., torch.Tensor | None] = MISSING """The name of the function to be called. This function should take the environment object and any other parameters diff --git a/source/isaaclab/isaaclab/scene/interactive_scene.py b/source/isaaclab/isaaclab/scene/interactive_scene.py index 15b693197a46..65923fb1858b 100644 --- a/source/isaaclab/isaaclab/scene/interactive_scene.py +++ b/source/isaaclab/isaaclab/scene/interactive_scene.py @@ -190,6 +190,12 @@ def __init__(self, cfg: InteractiveSceneCfg): self._aggregate_scene_data_requirements(requested_viz_types) + # The terrain importer owns its origins buffer together with its Warp view. + # For clone-plan origins, :attr:`env_origins_wp` caches a zero-copy view on + # first access (manager/term initialization, outside capture), so scene + # construction does not require a published clone plan. + self._clone_origins_wp = None + # Collision filtering is PhysX-only (matches both physx and ovphysx). if self.cfg.filter_collisions and "physx" in self.physics_backend and self._is_scene_setup_from_cfg(): self.filter_collisions(self._global_prim_paths) @@ -371,6 +377,38 @@ def env_origins(self) -> torch.Tensor: return self._terrain.env_origins return self.sim.get_clone_plan().positions + @property + def env_origins_wp(self) -> wp.array(dtype=wp.vec3f): + """Zero-copy Warp view of environment origins [m], shape ``(num_envs,)``. + + Terrain-backed scenes forward the terrain importer's cached view so both + accessors always share one storage owner. Clone-plan origins are wrapped + lazily on first access and cached. + """ + if self._terrain is not None: + return self._terrain.env_origins_wp + if self._clone_origins_wp is None or self._clone_origins_wp.ptr != self.env_origins.data_ptr(): + self._clone_origins_wp = wp.from_torch(self.env_origins, dtype=wp.vec3f) + return self._clone_origins_wp + + @property + def reset_capture_safe(self) -> bool: + """Whether :meth:`reset` dispatches only mask-native, capture-safe work. + + Conservative composition query: every entity must declare itself reset + capture-safe. Host-side consumers (surface grippers, deformable objects, + legacy actuator or sensor resets) keep the scene eager. + """ + if self._surface_grippers or self._deformable_objects: + return False + entities = [ + *self._articulations.values(), + *self._rigid_objects.values(), + *self._rigid_object_collections.values(), + *self._sensors.values(), + ] + return all(getattr(entity, "reset_capture_safe", False) for entity in entities) + @property def terrain(self) -> TerrainImporter | None: """The terrain in the scene. If None, then the scene has no terrain. @@ -450,27 +488,49 @@ def state(self) -> dict[str, dict[str, dict[str, torch.Tensor]]]: Operations. """ - def reset(self, env_ids: Sequence[int] | None = None): + def reset( + self, + env_ids: Sequence[int] | None = None, + env_mask: wp.array(dtype=wp.bool) | None = None, + ): """Resets the scene entities. Args: env_ids: The indices of the environments to reset. Defaults to None (all instances). + env_mask: Boolean Warp mask selecting environments. When provided, + it takes precedence over :paramref:`env_ids`. + + .. caution:: + ``env_mask`` is honored by mask-native assets (the Newton backend). + Classic PhysX assets currently accept and ignore it, so passing a + mask in such scenes resets unintended environments — use + :paramref:`env_ids` there. + """ + reset_kwargs = {"env_mask": env_mask} if env_mask is not None else {"env_ids": env_ids} # -- assets for articulation in self._articulations.values(): - articulation.reset(env_ids) + articulation.reset(**reset_kwargs) for deformable_object in self._deformable_objects.values(): - deformable_object.reset(env_ids) + deformable_object.reset(**reset_kwargs) for rigid_object in self._rigid_objects.values(): - rigid_object.reset(env_ids) - for surface_gripper in self._surface_grippers.values(): - surface_gripper.reset(env_ids) + rigid_object.reset(**reset_kwargs) + if env_mask is not None: + # Surface grippers are CPU-only assets driven through a Torch API. + # Hand them the boolean view and let their compatibility layer own + # any index materialization. + gripper_env_mask = wp.to_torch(env_mask) if self._surface_grippers else None + for surface_gripper in self._surface_grippers.values(): + surface_gripper.reset_mask(gripper_env_mask) + else: + for surface_gripper in self._surface_grippers.values(): + surface_gripper.reset(env_ids) for rigid_object_collection in self._rigid_object_collections.values(): - rigid_object_collection.reset(env_ids) + rigid_object_collection.reset(**reset_kwargs) # -- sensors for sensor in self._sensors.values(): - sensor.reset(env_ids) + sensor.reset(**reset_kwargs) def write_data_to_sim(self): """Writes the data of the scene entities to the simulation.""" diff --git a/source/isaaclab/isaaclab/sensors/camera/camera.py b/source/isaaclab/isaaclab/sensors/camera/camera.py index 58cf4423eaf5..f01588184001 100644 --- a/source/isaaclab/isaaclab/sensors/camera/camera.py +++ b/source/isaaclab/isaaclab/sensors/camera/camera.py @@ -420,7 +420,8 @@ def set_world_poses_from_view( up_axis = UsdGeom.GetStageUpAxis(self.stage) # set camera poses using the view; degenerate rows (eye == target) come back as NaN rotation_matrix = create_rotation_matrix_from_view(eyes, targets, up_axis, device=self._device) - valid_indices = (~torch.isnan(rotation_matrix).any(dim=(-2, -1))).nonzero(as_tuple=True)[0] + valid_rows = ~torch.isnan(rotation_matrix).any(dim=(-2, -1)) + valid_indices = valid_rows.nonzero(as_tuple=True)[0] # mask-boundary: input validation n_valid = valid_indices.numel() n_total = rotation_matrix.shape[0] if n_valid == 0: diff --git a/source/isaaclab/isaaclab/sensors/ray_caster/base_ray_caster.py b/source/isaaclab/isaaclab/sensors/ray_caster/base_ray_caster.py index 5a9534061120..e07ede2f8a38 100644 --- a/source/isaaclab/isaaclab/sensors/ray_caster/base_ray_caster.py +++ b/source/isaaclab/isaaclab/sensors/ray_caster/base_ray_caster.py @@ -107,12 +107,24 @@ def data(self) -> RayCasterData: def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None = None): # reset the timers and counters super().reset(env_ids, env_mask) + range_list = [self.cfg.ray_cast_drift_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z"]] + ranges = torch.tensor(range_list, device=self.device) + if env_mask is not None: + # Mask-native path: full-size resample with an elementwise select keeps + # the reset free of host synchronization and data-dependent allocation. + mask = wp.to_torch(env_mask).view(-1, 1) + drift = self.drift.torch + new_drift = torch.empty_like(drift).uniform_(*self.cfg.drift_range) + drift.copy_(torch.where(mask, new_drift, drift)) + ray_cast_drift = self.ray_cast_drift.torch + new_ray_cast_drift = math_utils.sample_uniform( + ranges[:, 0], ranges[:, 1], tuple(ray_cast_drift.shape), device=self.device + ) + ray_cast_drift.copy_(torch.where(mask, new_ray_cast_drift, ray_cast_drift)) + return # resolve to indices for torch indexing if env_ids is not None: num_envs_ids = len(env_ids) - elif env_mask is not None: - env_ids = wp.to_torch(env_mask).nonzero(as_tuple=False).squeeze(-1) - num_envs_ids = len(env_ids) else: env_ids = slice(None) num_envs_ids = self._view_count @@ -120,8 +132,6 @@ def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None r = torch.empty(num_envs_ids, 3, device=self.device) self.drift.torch[env_ids] = r.uniform_(*self.cfg.drift_range) # resample the ray cast drift - range_list = [self.cfg.ray_cast_drift_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z"]] - ranges = torch.tensor(range_list, device=self.device) self.ray_cast_drift.torch[env_ids] = math_utils.sample_uniform( ranges[:, 0], ranges[:, 1], (num_envs_ids, 3), device=self.device ) diff --git a/source/isaaclab/isaaclab/sensors/sensor_base.py b/source/isaaclab/isaaclab/sensors/sensor_base.py index 4d011f647d1c..3c60a87bf3cd 100644 --- a/source/isaaclab/isaaclab/sensors/sensor_base.py +++ b/source/isaaclab/isaaclab/sensors/sensor_base.py @@ -169,6 +169,15 @@ def set_debug_vis(self, debug_vis: bool) -> bool: # return success return True + @property + def reset_capture_safe(self) -> bool: + """Whether :meth:`reset` runs only mask-native kernel work. + + Conservative: a sensor that overrides :meth:`reset` must opt in explicitly by + overriding this property after validating its reset path for CUDA graph capture. + """ + return type(self).reset is SensorBase.reset + def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None = None) -> None: """Resets the sensor internals. diff --git a/source/isaaclab/isaaclab/terrains/terrain_importer.py b/source/isaaclab/isaaclab/terrains/terrain_importer.py index c78f9ae3344e..7ebfa58d4e5a 100644 --- a/source/isaaclab/isaaclab/terrains/terrain_importer.py +++ b/source/isaaclab/isaaclab/terrains/terrain_importer.py @@ -11,6 +11,7 @@ import numpy as np import torch import trimesh +import warp as wp import isaaclab.sim as sim_utils from isaaclab.markers import VisualizationMarkers @@ -25,6 +26,34 @@ logger = logging.getLogger(__name__) +@wp.kernel +def _update_env_origins_mask( + env_mask: wp.array(dtype=wp.bool), + move_up: wp.array(dtype=wp.bool), + move_down: wp.array(dtype=wp.bool), + rng_state: wp.array(dtype=wp.uint32), + terrain_levels: wp.array(dtype=wp.int64), + terrain_types: wp.array(dtype=wp.int64), + terrain_origins: wp.array(dtype=wp.vec3f, ndim=2), + env_origins: wp.array(dtype=wp.vec3f), + max_terrain_level: int, +): + """Update terrain levels and origins selected by an environment mask.""" + env_id = wp.tid() + if env_mask[env_id]: + state = rng_state[env_id] + random_level = wp.randi(state, 0, max_terrain_level) + # Keep shared terrain indices int64 so their zero-copy Torch views support advanced indexing. + level = terrain_levels[env_id] + wp.int64(move_up[env_id]) - wp.int64(move_down[env_id]) + if level >= wp.int64(max_terrain_level): + level = wp.int64(random_level) + elif level < wp.int64(0): + level = wp.int64(0) + terrain_levels[env_id] = level + env_origins[env_id] = terrain_origins[level, terrain_types[env_id]] + rng_state[env_id] = state + + class TerrainImporter: r"""A class to handle terrain meshes and import them into the simulator. @@ -46,14 +75,14 @@ class TerrainImporter: """A list containing the USD prim paths to the imported terrains.""" terrain_origins: torch.Tensor | None - """The origins of the sub-terrains in the added terrain mesh. Shape is (num_rows, num_cols, 3). + """Sub-terrain origins [m], shape ``(num_rows, num_cols, 3)``. If terrain origins is not None, the environment origins are computed based on the terrain origins. Otherwise, the environment origins are computed based on the grid spacing. """ env_origins: torch.Tensor - """The origins of the environments. Shape is (num_envs, 3).""" + """Environment origins [m], shape ``(num_envs, 3)``.""" def __init__(self, cfg: TerrainImporterCfg): """Initialize the terrain importer. @@ -77,6 +106,10 @@ def __init__(self, cfg: TerrainImporterCfg): self.terrain_prim_paths = list() self.terrain_origins = None self.env_origins = None # assigned later when `configure_env_origins` is called + self._terrain_levels_wp = None + self._terrain_types_wp = None + self._terrain_origins_wp = None + self._env_origins_wp = None # private variables self._terrain_flat_patches = dict() @@ -144,6 +177,18 @@ def terrain_names(self) -> list[str]: """A list of names of the imported terrains.""" return [f"'{path.split('/')[-1]}'" for path in self.terrain_prim_paths] + @property + def terrain_levels_wp(self) -> wp.array(dtype=wp.int64): + """Cached zero-copy Warp view of terrain levels, shape ``(num_envs,)``.""" + if self._terrain_levels_wp is None: + raise RuntimeError("Terrain levels are unavailable for grid-like environment origins.") + return self._terrain_levels_wp + + @property + def env_origins_wp(self) -> wp.array(dtype=wp.vec3f): + """Cached zero-copy Warp view of environment origins [m], shape ``(num_envs,)``.""" + return self._env_origins_wp + """ Operations - Visibility. """ @@ -288,31 +333,47 @@ def import_usd(self, name: str, usd_path: str): Operations - Origins. """ - def configure_env_origins(self, origins: np.ndarray | torch.Tensor | None = None): + def configure_env_origins(self, origins: np.ndarray | torch.Tensor | None = None) -> None: """Configure the origins of the environments based on the added terrain. + Reconfiguration copies into the existing origin buffers when layouts match, + so cached Torch tensors and Warp views held by consumers stay valid. + Args: - origins: The origins of the sub-terrains. Shape is (num_rows, num_cols, 3). + origins: The origins [m] of the sub-terrains. Shape is (num_rows, num_cols, 3). """ # decide whether to compute origins in a grid or based on curriculum if origins is not None: - # convert to numpy + # convert to torch if isinstance(origins, np.ndarray): origins = torch.from_numpy(origins) # store the origins - self.terrain_origins = origins.to(self.device, dtype=torch.float) + self.terrain_origins = self._assign_pointer_stable( + self.terrain_origins, origins.to(self.device, dtype=torch.float).contiguous() + ) # compute environment origins - self.env_origins = self._compute_env_origins_curriculum(self.cfg.num_envs, self.terrain_origins) + self.env_origins = self._assign_pointer_stable( + self.env_origins, self._compute_env_origins_curriculum(self.cfg.num_envs, self.terrain_origins) + ) else: self.terrain_origins = None # check if env spacing is valid if self.cfg.env_spacing is None: raise ValueError("Environment spacing must be specified for configuring grid-like origins.") # compute environment origins - self.env_origins = self._compute_env_origins_grid(self.cfg.num_envs, self.cfg.env_spacing) + self.env_origins = self._assign_pointer_stable( + self.env_origins, self._compute_env_origins_grid(self.cfg.num_envs, self.cfg.env_spacing) + ) + self._configure_warp_origin_views() - def update_env_origins(self, env_ids: torch.Tensor, move_up: torch.Tensor, move_down: torch.Tensor): - """Update the environment origins based on the terrain levels.""" + def update_env_origins(self, env_ids: torch.Tensor, move_up: torch.Tensor, move_down: torch.Tensor) -> None: + """Update environment origins through the Torch ID-based interface. + + Args: + env_ids: Environment indices to update. + move_up: Flags that increment the selected terrain levels. + move_down: Flags that decrement the selected terrain levels. + """ # check if grid-like spawning if self.terrain_origins is None: return @@ -328,10 +389,99 @@ def update_env_origins(self, env_ids: torch.Tensor, move_up: torch.Tensor, move_ # update the env origins self.env_origins[env_ids] = self.terrain_origins[self.terrain_levels[env_ids], self.terrain_types[env_ids]] + def update_env_origins_mask( + self, + env_mask: wp.array(dtype=wp.bool), + move_up: wp.array(dtype=wp.bool), + move_down: wp.array(dtype=wp.bool), + rng_state: wp.array(dtype=wp.uint32), + ) -> None: + """Update terrain levels and origins through the Warp mask-based interface. + + Moving past the maximum level wraps to a random level, moving below zero clamps to zero, and + unselected environments remain unchanged. + + Args: + env_mask: Boolean mask selecting environments, shape ``(num_envs,)``. + move_up: Flags that increment terrain levels, shape ``(num_envs,)``. + move_down: Flags that decrement terrain levels, shape ``(num_envs,)``. + rng_state: Per-environment random-number-generator state, shape ``(num_envs,)``. + + Raises: + TypeError: If an input is not a Warp array with the required data type. + ValueError: If an input has the wrong shape or device. + """ + if self.terrain_origins is None: + return + num_envs = self.terrain_levels.shape[0] + arrays = { + "env_mask": (env_mask, wp.bool), + "move_up": (move_up, wp.bool), + "move_down": (move_down, wp.bool), + "rng_state": (rng_state, wp.uint32), + } + for name, (array, dtype) in arrays.items(): + if not isinstance(array, wp.array) or array.dtype != dtype: + raise TypeError(f"{name} must be a Warp array with dtype {dtype}; received {array}.") + if array.ndim != 1 or array.shape[0] != num_envs: + raise ValueError(f"{name} must have shape ({num_envs},); received {array.shape}.") + if array.device != wp.get_device(self.device): + raise ValueError(f"{name} must be on device {self.device}; received {array.device}.") + + wp.launch( + kernel=_update_env_origins_mask, + dim=num_envs, + inputs=[ + env_mask, + move_up, + move_down, + rng_state, + self._terrain_levels_wp, + self._terrain_types_wp, + self._terrain_origins_wp, + self._env_origins_wp, + self.max_terrain_level, + ], + device=self.device, + ) + """ Internal helpers. """ + @staticmethod + def _assign_pointer_stable(current: torch.Tensor | None, new: torch.Tensor) -> torch.Tensor: + """Copy into the existing buffer when layouts match so cached views stay valid.""" + if ( + current is not None + and current.shape == new.shape + and current.dtype == new.dtype + and current.device == new.device + ): + current.copy_(new) + return current + return new + + def _configure_warp_origin_views(self) -> None: + """Create persistent zero-copy Warp views after configuring the Torch buffers. + + Existing views are kept when reconfiguration reuses the underlying Torch + storage, so consumers holding these views stay valid. + """ + if self._env_origins_wp is None or self._env_origins_wp.ptr != self.env_origins.data_ptr(): + self._env_origins_wp = wp.from_torch(self.env_origins, dtype=wp.vec3f) + if self.terrain_origins is None: + self._terrain_levels_wp = None + self._terrain_types_wp = None + self._terrain_origins_wp = None + return + if self._terrain_levels_wp is None or self._terrain_levels_wp.ptr != self.terrain_levels.data_ptr(): + self._terrain_levels_wp = wp.from_torch(self.terrain_levels, dtype=wp.int64) + if self._terrain_types_wp is None or self._terrain_types_wp.ptr != self.terrain_types.data_ptr(): + self._terrain_types_wp = wp.from_torch(self.terrain_types, dtype=wp.int64) + if self._terrain_origins_wp is None or self._terrain_origins_wp.ptr != self.terrain_origins.data_ptr(): + self._terrain_origins_wp = wp.from_torch(self.terrain_origins, dtype=wp.vec3f) + def _compute_env_origins_curriculum(self, num_envs: int, origins: torch.Tensor) -> torch.Tensor: """Compute the origins of the environments defined by the sub-terrains origins.""" # extract number of rows and cols @@ -344,10 +494,16 @@ def _compute_env_origins_curriculum(self, num_envs: int, origins: torch.Tensor) # store maximum terrain level possible self.max_terrain_level = num_rows # define all terrain levels and types available - self.terrain_levels = torch.randint(0, max_init_level + 1, (num_envs,), device=self.device) - self.terrain_types = torch.div( - torch.arange(num_envs, device=self.device), (num_envs / num_cols), rounding_mode="floor" - ).to(torch.long) + self.terrain_levels = self._assign_pointer_stable( + getattr(self, "terrain_levels", None), + torch.randint(0, max_init_level + 1, (num_envs,), device=self.device), + ) + self.terrain_types = self._assign_pointer_stable( + getattr(self, "terrain_types", None), + torch.div(torch.arange(num_envs, device=self.device), (num_envs / num_cols), rounding_mode="floor").to( + torch.long + ), + ) # create tensor based on number of environments env_origins = torch.zeros(num_envs, 3, device=self.device) env_origins[:] = origins[self.terrain_levels, self.terrain_types] diff --git a/source/isaaclab/isaaclab/test/benchmark/stepping.py b/source/isaaclab/isaaclab/test/benchmark/stepping.py index 9536d626765d..735c0e087ecd 100644 --- a/source/isaaclab/isaaclab/test/benchmark/stepping.py +++ b/source/isaaclab/isaaclab/test/benchmark/stepping.py @@ -13,6 +13,7 @@ from __future__ import annotations import time +from dataclasses import dataclass from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -59,32 +60,142 @@ def sample_random_actions(env) -> torch.Tensor | dict[str, torch.Tensor]: return 2.0 * torch.rand(u.num_envs, u.single_action_space.shape[0], device=u.device) - 1.0 -def run_runtime_loop(env, num_frames: int) -> list[float]: - """Step the environment ``num_frames`` times and record per-step wall times [s]. +@dataclass(frozen=True) +class RuntimeLoopTiming: + """Timing samples collected while stepping an environment.""" - Calls ``env.reset()`` once before the loop, then on each frame samples - random actions via :func:`sample_random_actions`, steps the environment, - and records the elapsed wall-clock time for that step. + first_step_s: float + """Wall time of the first environment step [s].""" + + step_times_s: list[float] + """Environment step wall times collected after the requested warm-up [s].""" + + +def measure_runtime_loop( + env, + num_frames: int, + *, + warmup_frames: int = 0, + synchronize_steps: bool = False, + reuse_action_buffer: bool = False, +) -> RuntimeLoopTiming: + """Step an environment and measure startup and per-step wall times. + + Calls ``env.reset()`` once, runs untimed warm-up frames, then records the + requested frames. Action sampling stays outside the timed region. Optional + CUDA synchronization measures completed-step latency rather than host + submission latency. Optional action-buffer reuse changes values in-place so + pointer-sensitive frontends can be measured independently from allocation. Args: env: A Gym-compatible environment. num_frames: Number of environment steps to run. + warmup_frames: Number of untimed environment steps before measurement. + synchronize_steps: Whether to synchronize the environment CUDA device + immediately before and after every measured step. + reuse_action_buffer: Whether to reuse one action allocation and randomize + its contents in-place between steps. Returns: - A list of length ``num_frames`` containing per-step wall times [s]. + The first-step time and a list of length ``num_frames`` containing + per-step wall times after the requested warm-up [s]. With no warm-up, + the first measured sample is also the reported first step. """ - env.reset() + if num_frames < 0: + raise ValueError(f"num_frames must be non-negative, got {num_frames}.") + if warmup_frames < 0: + raise ValueError(f"warmup_frames must be non-negative, got {warmup_frames}.") + env.reset() + reusable_actions = sample_random_actions(env) if reuse_action_buffer else None step_times: list[float] = [] + def next_actions(): + if reusable_actions is None: + return sample_random_actions(env) + _randomize_actions_in_place(reusable_actions) + return reusable_actions + + first_step_s = 0.0 + for warmup_index in range(warmup_frames): + actions = next_actions() + if warmup_index == 0: + if synchronize_steps: + _synchronize_env_device(env) + t0 = time.perf_counter_ns() + env.step(actions) + if synchronize_steps: + _synchronize_env_device(env) + t1 = time.perf_counter_ns() + first_step_s = (t1 - t0) / 1e9 + else: + env.step(actions) + if synchronize_steps: + _synchronize_env_device(env) + for _ in range(num_frames): - actions = sample_random_actions(env) + actions = next_actions() + if synchronize_steps: + _synchronize_env_device(env) t0 = time.perf_counter_ns() env.step(actions) + if synchronize_steps: + _synchronize_env_device(env) t1 = time.perf_counter_ns() step_times.append((t1 - t0) / 1e9) + if first_step_s == 0.0 and len(step_times) == 1: + first_step_s = step_times[0] + + return RuntimeLoopTiming(first_step_s=first_step_s, step_times_s=step_times) + + +def run_runtime_loop( + env, + num_frames: int, + *, + warmup_frames: int = 0, + synchronize_steps: bool = False, + reuse_action_buffer: bool = False, +) -> list[float]: + """Step an environment and return per-step wall times after warm-up [s]. + + This compatibility wrapper delegates to :func:`measure_runtime_loop`. + + Args: + env: A Gym-compatible environment. + num_frames: Number of environment steps to measure. + warmup_frames: Number of untimed environment steps before measurement. + synchronize_steps: Whether to synchronize the environment CUDA device + immediately before and after every measured step. + reuse_action_buffer: Whether to reuse one action allocation and randomize + its contents in-place between steps. + + Returns: + A list of length ``num_frames`` containing per-step wall times [s]. + """ + return measure_runtime_loop( + env, + num_frames, + warmup_frames=warmup_frames, + synchronize_steps=synchronize_steps, + reuse_action_buffer=reuse_action_buffer, + ).step_times_s + + +def _randomize_actions_in_place(actions: torch.Tensor | dict[str, torch.Tensor]) -> None: + """Fill reusable single-agent or multi-agent action tensors in-place.""" + tensors = actions.values() if isinstance(actions, dict) else (actions,) + for tensor in tensors: + tensor.uniform_(-1.0, 1.0) + + +def _synchronize_env_device(env) -> None: + """Synchronize the environment CUDA device when it uses CUDA.""" + import torch # noqa: PLC0415 - return step_times + device = torch.device(env.unwrapped.device) + if device.type == "cuda": + torch.cuda.synchronize(device) def _extract_success(extras) -> float | None: diff --git a/source/isaaclab/isaaclab/utils/warp/__init__.pyi b/source/isaaclab/isaaclab/utils/warp/__init__.pyi index 7be5ce3ed1c8..fb401ac1a128 100644 --- a/source/isaaclab/isaaclab/utils/warp/__init__.pyi +++ b/source/isaaclab/isaaclab/utils/warp/__init__.pyi @@ -6,6 +6,7 @@ __all__ = [ "ParticleMeshCounter", "ProxyArray", + "WarpLaunchCache", "convert_to_warp_mesh", "make_box_region_mesh", "make_frustum_region_mesh", @@ -17,3 +18,4 @@ __all__ = [ from .ops import convert_to_warp_mesh, raycast_dynamic_meshes, raycast_mesh, raycast_single_mesh from .particle_mesh import ParticleMeshCounter, make_box_region_mesh, make_frustum_region_mesh from .proxy_array import ProxyArray +from .warp_launch_cache import WarpLaunchCache diff --git a/source/isaaclab/isaaclab/utils/warp/warp_launch_cache.py b/source/isaaclab/isaaclab/utils/warp/warp_launch_cache.py new file mode 100644 index 000000000000..23e98192a4fd --- /dev/null +++ b/source/isaaclab/isaaclab/utils/warp/warp_launch_cache.py @@ -0,0 +1,349 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Cached Warp kernel launches for low-overhead record-and-replay execution.""" + +from __future__ import annotations + +import operator +import os +import struct +from collections.abc import Hashable, Sequence +from dataclasses import dataclass +from typing import Any, Literal + +import warp as wp + +_WARP_LAUNCH_MODE_ENV = "ISAACLAB_WARP_LAUNCH_MODE" +_WARP_LAUNCH_DEBUG_ENV = "ISAACLAB_WARP_LAUNCH_DEBUG" + + +class WarpLaunchCache: + """Launch Warp kernels eagerly or replay a command recorded on first use. + + This utility is the per-kernel counterpart to a CUDA graph cache. Each + ``(kernel, site)`` pair is recorded independently, so pointer-stable kernels + can replay even when neighboring work cannot be captured or recorded. The + launch interface contains only the arguments used by persistent Isaac Lab + kernels. + + Recorded arguments are static. Their storage, layout, and scalar values must + remain unchanged for the lifetime of the cached command. Use a distinct + ``site`` when one owner invokes the same kernel with multiple persistent + argument sets. Leave launches with changing argument storage on + :func:`warp.launch`. + + The launch dimension is checked before replay. Debug mode additionally checks + the static-argument contract on every replay. It is meant for tests and + validation; normal replay only performs the command lookup, an optional + dimension update, and :meth:`warp.Launch.launch`. + + Recorded launches may be replayed inside CUDA graph capture. CUDA graph + nodes retain their own launch data, so the Python command need not outlive + the graph. The argument storage is still caller-owned and must remain alive + and pointer-stable until every graph that references it is invalidated and + drained. Invalidate those graphs before resetting this cache if it is the + only remaining owner of their argument storage. + + Args: + mode: Execution mode. ``"auto"`` replays commands on CUDA and launches + eagerly on CPU, ``"replay"`` requires command replay, and + ``"eager"`` calls :func:`warp.launch` directly. If omitted, reads + ``ISAACLAB_WARP_LAUNCH_MODE`` and defaults to ``"auto"``. + debug: Whether to validate recorded arguments on every replay. If + omitted, reads ``ISAACLAB_WARP_LAUNCH_DEBUG`` and defaults to + ``False``. + device: Fixed device for the cache owner. + """ + + @dataclass(frozen=True) + class _ArrayDescriptor: + """Pointer and layout properties of one array argument.""" + + ptr: int + grad_ptr: int + device: str + dtype: str + shape: tuple[int, ...] + strides: tuple[int, ...] + + @dataclass + class _Entry: + """One recorded command and its static launch contract.""" + + command: wp.Launch + site: Hashable | None + dim: tuple[int, ...] + argument_counts: tuple[int, int] + argument_tokens: tuple[tuple[Any, ...], ...] | None + argument_owners: tuple[Any, ...] + + def __init__( + self, + mode: Literal["auto", "eager", "replay"] | None = None, + debug: bool | None = None, + *, + device: str | wp.Device, + ): + if mode is None: + mode = os.environ.get(_WARP_LAUNCH_MODE_ENV, "auto") + mode = mode.lower() + if mode not in {"auto", "eager", "replay"}: + raise ValueError( + f"Invalid Warp launch mode {mode!r}. Set {_WARP_LAUNCH_MODE_ENV} to 'auto', 'eager', or 'replay'." + ) + + if debug is None: + debug = self._read_debug_environment() + + self._mode = mode + self._debug = debug + self._device = wp.get_device(device) + self._entries: dict[Hashable, WarpLaunchCache._Entry] = {} + + def launch( + self, + kernel: wp.Kernel, + dim: int | Sequence[int], + inputs: Sequence[Any] = (), + outputs: Sequence[Any] = (), + *, + stream: wp.Stream | None = None, + site: Hashable | None = None, + ) -> None: + """Launch a Warp kernel eagerly or through a cached command. + + Args: + kernel: Warp kernel to launch. + dim: Number or shape of threads to launch. A zero-sized launch is a + no-op and is not cached. + inputs: Forward input arguments. + outputs: Forward output arguments. + stream: Optional stream on the owner's device. + site: Optional stable identity for one persistent argument set. + + Raises: + TypeError: If ``site`` is not hashable. + ValueError: If the launch dimension is invalid. + RuntimeError: In debug mode, if a recorded static argument changes. + """ + if self._mode == "eager" or (self._mode == "auto" and not self._device.is_cuda): + wp.launch( + kernel, + dim=dim, + inputs=inputs, + outputs=outputs, + device=self._device, + stream=stream, + ) + return + + logical_key: Hashable = kernel if site is None else (kernel, site) + try: + entry = self._entries.get(logical_key) + except TypeError as exc: + raise TypeError(f"Warp launch site must be hashable, got {type(site).__name__}.") from exc + + if entry is not None: + if isinstance(dim, int): + dim_matches = len(entry.dim) == 1 and dim == entry.dim[0] + else: + dim_matches = isinstance(dim, tuple) and dim == entry.dim + if not dim_matches: + normalized_dim, total_dim = self._normalize_dim(dim) + if total_dim == 0: + return + if normalized_dim != entry.dim: + entry.command.set_dim(normalized_dim) + entry.dim = normalized_dim + if self._debug: + self._validate_entry(entry, inputs, outputs) + entry.command.launch(stream=stream) + return + + normalized_dim, total_dim = self._normalize_dim(dim) + if total_dim == 0: + return + command = wp.launch( + kernel, + dim=normalized_dim, + inputs=inputs, + outputs=outputs, + device=self._device, + stream=stream, + record_cmd=True, + ) + if command is None: + raise RuntimeError(f"Warp did not return a Launch while recording {logical_key!r} on {self._device}.") + + arguments = (*inputs, *outputs) + entry = self._Entry( + command=command, + site=site, + dim=normalized_dim, + argument_counts=(len(inputs), len(outputs)), + argument_tokens=self._argument_tokens(arguments) if self._debug else None, + argument_owners=arguments, + ) + self._entries[logical_key] = entry + command.launch(stream=stream) + + def invalidate(self, site: Hashable | None = None) -> None: + """Discard recorded commands. + + Args: + site: Optional site to invalidate across kernels. If omitted, every + command is discarded. + + Invalidate captured graphs that reference these commands before calling + this method if the cache is their argument storage's only remaining + owner. Outstanding work on the owner device is synchronized before the + cached commands' retained argument owners are released. An empty cache + does not synchronize. + """ + if site is None: + self.reset() + return + stale_keys = [key for key, entry in self._entries.items() if self._sites_match(entry.site, site)] + if stale_keys: + wp.synchronize_device(self._device) + for key in stale_keys: + del self._entries[key] + + def reset(self) -> None: + """Drain dependent work and discard every recorded command. + + Invalidate captured graphs that reference these commands before calling + this method if the cache is their argument storage's only remaining + owner. The owner device is synchronized only when the cache contains + recorded commands. An empty cache returns immediately. + """ + if self._entries: + wp.synchronize_device(self._device) + self._entries.clear() + + @staticmethod + def _sites_match(recorded: Hashable | None, requested: Hashable) -> bool: + """Compare site keys without forcing array-like equality to a boolean.""" + if recorded is requested: + return True + try: + if hash(recorded) != hash(requested): + return False + return bool(recorded == requested) + except (RuntimeError, TypeError, ValueError): + return False + + def _validate_entry( + self, + entry: WarpLaunchCache._Entry, + inputs: Sequence[Any], + outputs: Sequence[Any], + ) -> None: + """Validate the static command contract in debug mode.""" + counts = (len(inputs), len(outputs)) + if counts != entry.argument_counts: + raise RuntimeError( + f"Warp launch argument counts changed: recorded {entry.argument_counts}, received {counts}. " + "Use a distinct site or invalidate the cache." + ) + arguments = (*inputs, *outputs) + tokens = self._argument_tokens(arguments) + if tokens != entry.argument_tokens: + mismatch = next( + index for index, pair in enumerate(zip(entry.argument_tokens or (), tokens)) if pair[0] != pair[1] + ) + raise RuntimeError( + f"Warp launch static argument {mismatch} changed. Recorded commands require persistent argument " + "storage and values; use a distinct site, invalidate the cache, or leave this launch eager." + ) + + @staticmethod + def _normalize_dim(dim: int | Sequence[int]) -> tuple[tuple[int, ...], int]: + """Normalize Warp launch dimensions and compute their product.""" + if isinstance(dim, int): + normalized = (dim,) + else: + try: + normalized = tuple(operator.index(extent) for extent in dim) + except TypeError: + normalized = (operator.index(dim),) + + if len(normalized) > 4: + raise ValueError(f"Warp launch dimensions must have at most 4 axes, got {len(normalized)}.") + + total = 1 + for extent in normalized: + if extent < 0: + raise ValueError(f"Warp launch dimensions must be non-negative, got {normalized}.") + total *= extent + return normalized, total + + @classmethod + def _argument_tokens(cls, arguments: Sequence[Any]) -> tuple[tuple[Any, ...], ...]: + """Snapshot static arguments for debug validation.""" + return tuple(cls._argument_token(value) for value in arguments) + + @classmethod + def _argument_token(cls, value: Any) -> tuple[Any, ...]: + """Return a stable pointer, layout, or value token for one argument.""" + descriptor = cls._array_descriptor_or_none(value) + if descriptor is not None: + return ("array", descriptor) + if type(value) is float: + return ("float", struct.pack("=d", value)) + if type(value) is complex: + return ("complex", struct.pack("=dd", value.real, value.imag)) + if value is None or isinstance(value, (bool, int, str, bytes)): + return ("value", type(value), value) + if isinstance(value, tuple): + return ("tuple", type(value), tuple(cls._argument_token(item) for item in value)) + if hasattr(type(value), "_wp_scalar_type_"): + try: + return ("warp_value", type(value), tuple(value)) + except TypeError: + pass + if hasattr(value, "value"): + return ("value_attribute", type(value), value.value) + return ("object", type(value), id(value)) + + @classmethod + def _array_descriptor_or_none(cls, value: Any) -> WarpLaunchCache._ArrayDescriptor | None: + """Return a descriptor for a Warp array, ProxyArray, or Torch tensor.""" + array = value if isinstance(value, wp.array) else getattr(value, "warp", None) + if isinstance(array, wp.array): + grad = array.grad + return cls._ArrayDescriptor( + ptr=0 if array.ptr is None else int(array.ptr), + grad_ptr=0 if grad is None or grad.ptr is None else int(grad.ptr), + device=str(array.device), + dtype=str(array.dtype), + shape=tuple(int(extent) for extent in array.shape), + strides=tuple(int(stride) for stride in array.strides), + ) + + data_ptr = getattr(value, "data_ptr", None) + stride = getattr(value, "stride", None) + element_size = getattr(value, "element_size", None) + if callable(data_ptr) and callable(stride) and callable(element_size) and hasattr(value, "shape"): + return cls._ArrayDescriptor( + ptr=int(data_ptr()), + grad_ptr=0, + device=str(getattr(value, "device", "cpu")), + dtype=str(getattr(value, "dtype", type(value))), + shape=tuple(int(extent) for extent in value.shape), + strides=tuple(int(value_stride) * int(element_size()) for value_stride in stride()), + ) + return None + + @staticmethod + def _read_debug_environment() -> bool: + """Read the boolean debug switch from the environment.""" + value = os.environ.get(_WARP_LAUNCH_DEBUG_ENV, "0").lower() + if value in {"0", "false", "no", "off"}: + return False + if value in {"1", "true", "yes", "on"}: + return True + raise ValueError(f"Invalid {_WARP_LAUNCH_DEBUG_ENV} value {value!r}; use 0/1, false/true, no/yes, or off/on.") diff --git a/source/isaaclab/test/actuators/test_actuator_reset_mask.py b/source/isaaclab/test/actuators/test_actuator_reset_mask.py new file mode 100644 index 000000000000..54ca472b482f --- /dev/null +++ b/source/isaaclab/test/actuators/test_actuator_reset_mask.py @@ -0,0 +1,95 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for mask-based actuator resets.""" + +from __future__ import annotations + +import pytest +import torch + +from isaaclab.actuators.actuator_base import ActuatorBase +from isaaclab.actuators.actuator_net import ActuatorNetLSTM, ActuatorNetMLP +from isaaclab.actuators.actuator_pd import DelayedPDActuator, IdealPDActuator, ImplicitActuator + + +def _fail_host_compaction(*args, **kwargs): + raise AssertionError("mask-native reset_mask must not materialize IDs") + + +class _RecordingActuator(ActuatorBase): + """Legacy model implementing only the ID-based reset.""" + + def __init__(self): + self.reset_env_ids = None + + def reset(self, env_ids): + self.reset_env_ids = env_ids + + def compute(self, control_action, joint_pos, joint_vel): + return control_action + + +def test_base_reset_mask_compacts_and_delegates(): + """The base fallback should hand legacy models compact IDs matching the mask.""" + actuator = _RecordingActuator() + + actuator.reset_mask(torch.tensor([True, False, True])) + + torch.testing.assert_close(actuator.reset_env_ids, torch.tensor([0, 2])) + + +def test_reset_only_override_restores_compacting_fallback(): + """A subclass overriding reset without reset_mask must not inherit a stateless no-op.""" + assert DelayedPDActuator.reset_mask is ActuatorBase.reset_mask + assert ImplicitActuator.reset_mask is not ActuatorBase.reset_mask + assert IdealPDActuator.reset_mask is not ActuatorBase.reset_mask + + class _StatefulImplicit(ImplicitActuator): + def reset(self, env_ids): + pass + + assert _StatefulImplicit.reset_mask is ActuatorBase.reset_mask + + +def test_stateless_models_skip_host_compaction(monkeypatch: pytest.MonkeyPatch): + """Stateless models must not pay a host synchronization on masked resets.""" + implicit = ImplicitActuator.__new__(ImplicitActuator) + + with monkeypatch.context() as context: + context.setattr(torch.Tensor, "nonzero", _fail_host_compaction) + implicit.reset_mask(torch.tensor([True, False])) + + +def test_lstm_reset_mask_zeroes_only_selected(monkeypatch: pytest.MonkeyPatch): + """The LSTM override should zero hidden state for masked environments only.""" + actuator = ActuatorNetLSTM.__new__(ActuatorNetLSTM) + actuator.sea_hidden_state_per_env = torch.ones(2, 4, 3, 8) + actuator.sea_cell_state_per_env = torch.ones(2, 4, 3, 8) + expected = torch.ones(2, 4, 3, 8) + expected[:, [0, 2]] = 0.0 + + with monkeypatch.context() as context: + context.setattr(torch.Tensor, "nonzero", _fail_host_compaction) + actuator.reset_mask(torch.tensor([True, False, True, False])) + + torch.testing.assert_close(actuator.sea_hidden_state_per_env, expected) + torch.testing.assert_close(actuator.sea_cell_state_per_env, expected) + + +def test_mlp_reset_mask_zeroes_only_selected(monkeypatch: pytest.MonkeyPatch): + """The MLP override should zero joint history for masked environments only.""" + actuator = ActuatorNetMLP.__new__(ActuatorNetMLP) + actuator._joint_pos_error_history = torch.ones(4, 3, 6) + actuator._joint_vel_history = torch.ones(4, 3, 6) + expected = torch.ones(4, 3, 6) + expected[[1, 3]] = 0.0 + + with monkeypatch.context() as context: + context.setattr(torch.Tensor, "nonzero", _fail_host_compaction) + actuator.reset_mask(torch.tensor([False, True, False, True])) + + torch.testing.assert_close(actuator._joint_pos_error_history, expected) + torch.testing.assert_close(actuator._joint_vel_history, expected) diff --git a/source/isaaclab/test/benchmark/test_stepping.py b/source/isaaclab/test/benchmark/test_stepping.py index d2c53117230c..93fe0c707e9d 100644 --- a/source/isaaclab/test/benchmark/test_stepping.py +++ b/source/isaaclab/test/benchmark/test_stepping.py @@ -6,9 +6,11 @@ """Tests for the runtime stepping helpers.""" import numpy as np +import pytest import torch -from isaaclab.test.benchmark.stepping import run_runtime_loop, sample_random_actions +from isaaclab.test.benchmark import stepping +from isaaclab.test.benchmark.stepping import measure_runtime_loop, run_runtime_loop, sample_random_actions class _Space: @@ -26,12 +28,14 @@ def __init__(self): self.unwrapped = _Env._U() self.reset_called = False self.steps = 0 + self.action_ids = [] def reset(self): self.reset_called = True def step(self, actions): self.steps += 1 + self.action_ids.append(id(actions)) return (None, None, None, {}) @@ -49,6 +53,47 @@ def test_run_runtime_loop_steps_and_times(): assert len(times) == 5 and all(t >= 0.0 for t in times) +def test_run_runtime_loop_excludes_warmup_and_reuses_actions(): + env = _Env() + times = run_runtime_loop(env, num_frames=3, warmup_frames=2, reuse_action_buffer=True) + assert env.steps == 5 + assert len(times) == 3 + assert len(set(env.action_ids)) == 1 + + +def test_measure_runtime_loop_reports_first_step_separately(): + env = _Env() + timing = measure_runtime_loop(env, num_frames=3, warmup_frames=2) + assert timing.first_step_s >= 0.0 + assert len(timing.step_times_s) == 3 + assert env.steps == 5 + + +def test_measure_runtime_loop_excludes_startup_action_sampling_and_synchronizes(monkeypatch): + """The startup sample should time one completed step without action-generation work.""" + events = [] + clock = iter((100, 200)) + env = _Env() + + monkeypatch.setattr(stepping, "sample_random_actions", lambda _env: events.append("sample") or torch.zeros((4, 3))) + monkeypatch.setattr(stepping, "_synchronize_env_device", lambda _env: events.append("sync")) + monkeypatch.setattr(stepping.time, "perf_counter_ns", lambda: events.append("clock") or next(clock)) + original_step = env.step + env.step = lambda actions: events.append("step") or original_step(actions) + + timing = measure_runtime_loop(env, num_frames=0, warmup_frames=1, synchronize_steps=True) + + assert timing.first_step_s == pytest.approx(1e-7) + assert events[:6] == ["sample", "sync", "clock", "step", "sync", "clock"] + + +def test_run_runtime_loop_rejects_negative_frame_counts(): + env = _Env() + for kwargs in ({"num_frames": -1}, {"num_frames": 1, "warmup_frames": -1}): + with pytest.raises(ValueError): + run_runtime_loop(env, **kwargs) + + class _MASpace: def __init__(self, n): self._n = n diff --git a/source/isaaclab/test/envs/test_curriculums.py b/source/isaaclab/test/envs/test_curriculums.py new file mode 100644 index 000000000000..36bf735fa0c4 --- /dev/null +++ b/source/isaaclab/test/envs/test_curriculums.py @@ -0,0 +1,42 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Unit tests for manager-based environment curriculum terms.""" + +from types import SimpleNamespace + +import isaaclab.envs.mdp as mdp +from isaaclab.managers import CurriculumTermCfg + + +def test_modify_term_cfg_invalidates_warp_caches_on_change(): + """A changed term parameter should invalidate cached Warp work exactly once.""" + + class DummyEnv: + def __init__(self): + term_cfg = SimpleNamespace(params={"scale": 1.0}) + self.reward_manager = SimpleNamespace(cfg=SimpleNamespace(term=term_cfg)) + self.invalidations = 0 + + def invalidate_wp_graphs(self): + self.invalidations += 1 + + env = DummyEnv() + cfg = CurriculumTermCfg( + func=mdp.modify_term_cfg, + params={ + "address": "rewards.term.params.scale", + "modify_fn": lambda env, env_ids, data: 2.0, + }, + ) + term = mdp.modify_term_cfg(cfg, env) + + term(env, [], **cfg.params) + assert env.reward_manager.cfg.term.params["scale"] == 2.0 + assert env.invalidations == 1 + + cfg.params["modify_fn"] = lambda env, env_ids, data: mdp.modify_term_cfg.NO_CHANGE + term(env, [], **cfg.params) + assert env.invalidations == 1 diff --git a/source/isaaclab/test/scene/test_interactive_scene.py b/source/isaaclab/test/scene/test_interactive_scene.py index 41aaaf7f9c3d..a3c5905cef80 100644 --- a/source/isaaclab/test/scene/test_interactive_scene.py +++ b/source/isaaclab/test/scene/test_interactive_scene.py @@ -13,9 +13,11 @@ """Rest everything follows.""" from types import SimpleNamespace +from unittest.mock import Mock import pytest import torch +import warp as wp import isaaclab.sim as sim_utils from isaaclab.actuators import ImplicitActuatorCfg @@ -132,6 +134,81 @@ def test_reset_to_env_ids_input_types(device, setup_scene): assert_state_equal(prev_state, scene.get_state()) +@pytest.mark.parametrize("device", ["cuda:0", "cpu"]) +def test_env_origins_wp_is_cached_zero_copy_view(device, setup_scene): + """The scene should expose one persistent Warp view of its Torch origins.""" + make_scene, sim = setup_scene + scene = InteractiveScene(make_scene(num_envs=4)) + sim.reset() + + assert scene.env_origins_wp is scene.env_origins_wp + assert scene.env_origins_wp.dtype == wp.vec3f + assert scene.env_origins_wp.shape == (scene.num_envs,) + assert scene.env_origins_wp.ptr == scene.env_origins.data_ptr() + + +def test_reset_dispatches_warp_mask_to_supported_entities() -> None: + """Mask reset should remain mask-based across assets and sensors.""" + scene = object.__new__(InteractiveScene) + articulation = Mock() + deformable = Mock() + rigid_object = Mock() + rigid_collection = Mock() + sensor = Mock() + scene._articulations = {"articulation": articulation} + scene._deformable_objects = {"deformable": deformable} + scene._rigid_objects = {"rigid_object": rigid_object} + scene._rigid_object_collections = {"collection": rigid_collection} + scene._sensors = {"sensor": sensor} + scene._surface_grippers = {} + env_mask = wp.array([True, False, True], dtype=wp.bool, device="cpu") + + scene.reset(env_mask=env_mask) + + for entity in (articulation, deformable, rigid_object, rigid_collection, sensor): + entity.reset.assert_called_once_with(env_mask=env_mask) + + +def test_scene_reset_capture_safe_reflects_entity_composition() -> None: + """Capture safety should require every entity to declare a mask-native reset.""" + scene = object.__new__(InteractiveScene) + safe_entity = SimpleNamespace(reset_capture_safe=True) + scene._articulations = {"articulation": safe_entity} + scene._rigid_objects = {"rigid_object": safe_entity} + scene._rigid_object_collections = {"collection": safe_entity} + scene._sensors = {"sensor": SimpleNamespace(reset_capture_safe=True)} + scene._deformable_objects = {} + scene._surface_grippers = {} + assert scene.reset_capture_safe + + scene._sensors = {"sensor": SimpleNamespace()} + assert not scene.reset_capture_safe + + scene._sensors = {} + scene._surface_grippers = {"gripper": object()} + assert not scene.reset_capture_safe + + +def test_mask_reset_forwards_boolean_mask_to_surface_grippers() -> None: + """Mask reset should hand surface grippers a boolean Torch view, not compact IDs.""" + scene = object.__new__(InteractiveScene) + gripper = Mock() + scene._articulations = {} + scene._deformable_objects = {} + scene._rigid_objects = {} + scene._rigid_object_collections = {} + scene._sensors = {} + scene._surface_grippers = {"gripper": gripper} + env_mask = wp.array([True, False, True], dtype=wp.bool, device="cpu") + + scene.reset(env_mask=env_mask) + + gripper.reset.assert_not_called() + gripper_env_mask = gripper.reset_mask.call_args.args[0] + assert gripper_env_mask.dtype == torch.bool + assert torch.equal(gripper_env_mask, torch.tensor([True, False, True])) + + def test_scene_publishes_plan_via_replicate(monkeypatch: pytest.MonkeyPatch): """A cfg-driven scene forwards the right plan and stage to cloner.replicate. diff --git a/source/isaaclab/test/terrains/test_terrain_importer.py b/source/isaaclab/test/terrains/test_terrain_importer.py index 553f01f79233..4ced50804d2e 100644 --- a/source/isaaclab/test/terrains/test_terrain_importer.py +++ b/source/isaaclab/test/terrains/test_terrain_importer.py @@ -52,6 +52,14 @@ def test_terrain_importer_env_origins(device, env_spacing, num_envs): # obtain env origins using terrain importer terrain_importer_origins = terrain_importer.env_origins + # check that the cached Warp view shares the canonical Torch storage + assert isinstance(terrain_importer.env_origins, torch.Tensor) + assert terrain_importer.env_origins_wp.dtype == wp.vec3f + assert terrain_importer.env_origins_wp.shape == (num_envs,) + assert terrain_importer_origins.shape == (num_envs, 3) + assert terrain_importer.env_origins_wp is terrain_importer.env_origins_wp + assert terrain_importer_origins.data_ptr() == terrain_importer.env_origins_wp.ptr + # obtain env origins using Lab's grid_transforms lab_grid_origins, _ = lab_cloner.grid_transforms(num_envs, spacing=env_spacing, device=sim.device) diff --git a/source/isaaclab/test/utils/warp/test_warp_launch_cache.py b/source/isaaclab/test/utils/warp/test_warp_launch_cache.py new file mode 100644 index 000000000000..3955b7429576 --- /dev/null +++ b/source/isaaclab/test/utils/warp/test_warp_launch_cache.py @@ -0,0 +1,295 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for WarpLaunchCache record-and-replay behavior.""" + +from __future__ import annotations + +import os +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +import numpy as np +import torch +import warp as wp + +from isaaclab.utils.warp import ProxyArray, WarpLaunchCache + + +@wp.kernel +def _affine( + source: wp.array(dtype=wp.float32), + scale: float, + output: wp.array(dtype=wp.float32), +): + """Apply a scalar multiplier to an array.""" + index = wp.tid() + output[index] = source[index] * scale + + +@wp.kernel +def _increment(output: wp.array(dtype=wp.float32)): + """Increment every launched output element.""" + index = wp.tid() + output[index] = output[index] + 1.0 + + +@wp.kernel +def _write_index(output: wp.array(dtype=wp.float32)): + """Write one-based thread indices to an output array.""" + index = wp.tid() + output[index] = wp.float32(index + 1) + + +class TestWarpLaunchCache(unittest.TestCase): + """Tests for :class:`WarpLaunchCache`.""" + + @classmethod + def setUpClass(cls): + wp.init() + if not wp.is_cuda_available(): + raise unittest.SkipTest("CUDA is required for Warp launch replay tests.") + + def setUp(self): + self.device = wp.get_device("cuda:0") + self.cache = WarpLaunchCache(device=self.device) + + def tearDown(self): + wp.synchronize_device(self.device) + + def test_static_launch_records_once_then_replays(self): + """The first call should record and execute, then later calls should replay.""" + output = wp.zeros(8, dtype=wp.float32, device=self.device) + + self.cache.launch(_increment, dim=8, inputs=[output]) + self.cache.launch(_increment, dim=8, inputs=[output]) + + self.assertEqual(len(self.cache._entries), 1) + np.testing.assert_array_equal(output.numpy(), np.full(8, 2.0, dtype=np.float32)) + + def test_fixed_device_cache_launches_from_its_owner(self): + """An owner should retain and use one fixed-device launcher.""" + owner = SimpleNamespace(_warp_launch=WarpLaunchCache(device=self.device)) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + + owner._warp_launch.launch(_increment, dim=4, inputs=[output]) + owner._warp_launch.launch(_increment, dim=4, inputs=[output]) + + np.testing.assert_array_equal(output.numpy(), np.full(4, 2.0, dtype=np.float32)) + + def test_sites_distinguish_persistent_argument_sets(self): + """One kernel should own independent commands for distinct stable sites.""" + output_a = wp.zeros(4, dtype=wp.float32, device=self.device) + output_b = wp.zeros(4, dtype=wp.float32, device=self.device) + + self.cache.launch(_increment, dim=4, inputs=[output_a], site=output_a) + self.cache.launch(_increment, dim=4, inputs=[output_b], site=output_b) + self.cache.launch(_increment, dim=4, inputs=[output_a], site=output_a) + self.cache.launch(_increment, dim=4, inputs=[output_b], site=output_b) + + self.assertEqual(len(self.cache._entries), 2) + np.testing.assert_array_equal(output_a.numpy(), np.full(4, 2.0, dtype=np.float32)) + np.testing.assert_array_equal(output_b.numpy(), np.full(4, 2.0, dtype=np.float32)) + + def test_dim_change_updates_recorded_bounds(self): + """A changed launch dimension should update the cached command bounds.""" + output = wp.zeros(8, dtype=wp.float32, device=self.device) + + self.cache.launch(_write_index, dim=4, inputs=[output]) + self.cache.launch(_write_index, dim=8, inputs=[output]) + + np.testing.assert_array_equal(output.numpy(), np.arange(1, 9, dtype=np.float32)) + + def test_eager_environment_mode_uses_current_arguments(self): + """The eager switch should bypass recording and use each call's arguments.""" + source = wp.full(4, value=2.0, dtype=wp.float32, device=self.device) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + + with patch.dict(os.environ, {"ISAACLAB_WARP_LAUNCH_MODE": "eager"}): + cache = WarpLaunchCache(device=self.device) + cache.launch(_affine, dim=4, inputs=[source, 2.0], outputs=[output]) + cache.launch(_affine, dim=4, inputs=[source, 5.0], outputs=[output]) + + self.assertFalse(cache._entries) + np.testing.assert_array_equal(output.numpy(), np.full(4, 10.0, dtype=np.float32)) + + def test_explicit_eager_mode_uses_current_arguments(self): + """Explicit eager mode should use the current launch arguments.""" + cache = WarpLaunchCache(device=self.device, mode="eager") + source = wp.full(4, value=2.0, dtype=wp.float32, device=self.device) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + + cache.launch(_affine, dim=4, inputs=[source, 2.0], outputs=[output]) + cache.launch(_affine, dim=4, inputs=[source, 3.0], outputs=[output]) + + self.assertFalse(cache._entries) + np.testing.assert_array_equal(output.numpy(), np.full(4, 6.0, dtype=np.float32)) + + def test_auto_mode_uses_eager_launch_on_cpu(self): + """Auto mode should avoid retaining replay commands on CPU.""" + device = wp.get_device("cpu") + cache = WarpLaunchCache(device=device, mode="auto") + output = wp.zeros(4, dtype=wp.float32, device=device) + + cache.launch(_increment, dim=4, inputs=[output]) + + self.assertFalse(cache._entries) + np.testing.assert_array_equal(output.numpy(), np.ones(4, dtype=np.float32)) + + def test_invalid_mode_is_rejected(self): + """The constructor should reject unknown execution modes.""" + with self.assertRaisesRegex(ValueError, "Invalid Warp launch mode"): + WarpLaunchCache(device=self.device, mode="capture") # type: ignore[arg-type] + + def test_debug_mode_rejects_changed_static_array(self): + """Debug mode should detect replacement of persistent array storage.""" + source_a = wp.ones(4, dtype=wp.float32, device=self.device) + source_b = wp.ones(4, dtype=wp.float32, device=self.device) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + cache = WarpLaunchCache(device=self.device, debug=True) + + cache.launch(_affine, dim=4, inputs=[source_a, 1.0], outputs=[output]) + with self.assertRaisesRegex(RuntimeError, "static argument 0 changed"): + cache.launch(_affine, dim=4, inputs=[source_b, 1.0], outputs=[output]) + + def test_debug_mode_rejects_repointed_torch_tensor(self): + """Debug mode should detect a tensor object repointed to new storage.""" + source = torch.ones(4, dtype=torch.float32, device=str(self.device)) + replacement = torch.full_like(source, 3.0) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + cache = WarpLaunchCache(device=self.device, debug=True) + + cache.launch(_affine, dim=4, inputs=[source, 2.0], outputs=[output]) + source.set_(replacement) + + with self.assertRaisesRegex(RuntimeError, "static argument 0 changed"): + cache.launch(_affine, dim=4, inputs=[source, 2.0], outputs=[output]) + + def test_debug_mode_rejects_changed_static_scalar(self): + """Debug mode should reject a changed recorded scalar value.""" + source = wp.ones(4, dtype=wp.float32, device=self.device) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + cache = WarpLaunchCache(device=self.device, debug=True) + + cache.launch(_affine, dim=4, inputs=[source, 1.0], outputs=[output]) + with self.assertRaisesRegex(RuntimeError, "static argument 1 changed"): + cache.launch(_affine, dim=4, inputs=[source, 2.0], outputs=[output]) + + def test_debug_mode_accepts_new_proxy_for_same_array(self): + """Debug mode should compare ProxyArray storage instead of wrapper identity.""" + source = wp.ones(4, dtype=wp.float32, device=self.device) + output = wp.zeros(4, dtype=wp.float32, device=self.device) + cache = WarpLaunchCache(device=self.device, debug=True) + + cache.launch(_affine, dim=4, inputs=[ProxyArray(source), 2.0], outputs=[output]) + cache.launch(_affine, dim=4, inputs=[ProxyArray(source), 2.0], outputs=[output]) + + np.testing.assert_array_equal(output.numpy(), np.full(4, 2.0, dtype=np.float32)) + + def test_zero_dim_is_noop_and_does_not_populate_cache(self): + """A zero-sized launch should neither execute nor reserve a command.""" + output = wp.zeros(4, dtype=wp.float32, device=self.device) + + self.cache.launch(_increment, dim=(4, 0), inputs=[output]) + + self.assertFalse(self.cache._entries) + np.testing.assert_array_equal(output.numpy(), np.zeros(4, dtype=np.float32)) + + def test_fixed_device_fast_path_skips_device_lookup(self): + """A fixed-device cache should not resolve its device after construction.""" + output = wp.zeros(4, dtype=wp.float32, device=self.device) + self.cache.launch(_increment, dim=4, inputs=[output]) + + with patch.object(wp, "get_device", side_effect=AssertionError("unexpected device lookup")): + self.cache.launch(_increment, dim=4, inputs=[output]) + + np.testing.assert_array_equal(output.numpy(), np.full(4, 2.0, dtype=np.float32)) + + def test_invalidate_site_and_reset(self): + """Invalidation should drop one stable site or every recorded command.""" + output_a = wp.zeros(1, dtype=wp.float32, device=self.device) + output_b = wp.zeros(1, dtype=wp.float32, device=self.device) + self.cache.launch(_increment, dim=1, inputs=[output_a], site="a") + self.cache.launch(_increment, dim=1, inputs=[output_b], site="b") + + self.cache.invalidate(site="a") + self.assertEqual(len(self.cache._entries), 1) + self.cache.reset() + self.assertFalse(self.cache._entries) + + def test_invalidate_accepts_tensor_site_identity(self): + """Invalidation should accept a tensor site without evaluating elementwise equality.""" + output = wp.zeros(1, dtype=wp.float32, device=self.device) + site = torch.arange(2, device=str(self.device)) + self.cache.launch(_increment, dim=1, inputs=[output], site=site) + + self.cache.invalidate(site=site) + + self.assertFalse(self.cache._entries) + + def test_reset_synchronizes_only_nonempty_cache_device(self): + """Reset should drain recorded commands without syncing an empty cache.""" + empty_cache = WarpLaunchCache(device=self.device, mode="eager") + with patch.object(wp, "synchronize_device", wraps=wp.synchronize_device) as synchronize: + empty_cache.reset() + synchronize.assert_not_called() + + output = wp.zeros(1, dtype=wp.float32, device=self.device) + self.cache.launch(_increment, dim=1, inputs=[output]) + self.cache.reset() + synchronize.assert_called_once_with(self.device) + + def test_replay_can_be_recorded_inside_cuda_capture(self): + """A warmed command replay should compose with CUDA graph capture.""" + output = wp.zeros(4, dtype=wp.float32, device=self.device) + self.cache.launch(_increment, dim=4, inputs=[output]) + + with wp.ScopedCapture(device=self.device) as capture: + self.cache.launch(_increment, dim=4, inputs=[output]) + + wp.capture_launch(capture.graph) + np.testing.assert_array_equal(output.numpy(), np.full(4, 2.0, dtype=np.float32)) + + def test_cold_recording_composes_with_cuda_capture(self): + """A first recorded launch should become a node in an outer capture.""" + output = wp.zeros(4, dtype=wp.float32, device=self.device) + wp.load_module(device=self.device) + + with wp.ScopedCapture(device=self.device) as capture: + self.cache.launch(_increment, dim=4, inputs=[output]) + + self.assertEqual(len(self.cache._entries), 1) + wp.capture_launch(capture.graph) + np.testing.assert_array_equal(output.numpy(), np.ones(4, dtype=np.float32)) + + def test_replay_composes_with_capture_on_explicit_stream(self): + """A recorded launch should use the stream selected by an outer capture.""" + output = wp.zeros(4, dtype=wp.float32, device=self.device) + stream = wp.Stream(self.device) + self.cache.launch(_increment, dim=4, inputs=[output], stream=stream) + + with wp.ScopedCapture(stream=stream) as capture: + self.cache.launch(_increment, dim=4, inputs=[output], stream=stream) + + wp.capture_launch(capture.graph, stream=stream) + wp.synchronize_stream(stream) + np.testing.assert_array_equal(output.numpy(), np.full(4, 2.0, dtype=np.float32)) + + def test_captured_graph_survives_command_reset_while_arguments_live(self): + """A graph should retain launch data while its argument storage remains alive.""" + output = wp.zeros(4, dtype=wp.float32, device=self.device) + self.cache.launch(_increment, dim=4, inputs=[output]) + + with wp.ScopedCapture(device=self.device) as capture: + self.cache.launch(_increment, dim=4, inputs=[output]) + + self.cache.reset() # ``output`` remains the graph argument's strong owner. + wp.capture_launch(capture.graph) + np.testing.assert_array_equal(output.numpy(), np.full(4, 2.0, dtype=np.float32)) + + +if __name__ == "__main__": + unittest.main() diff --git a/source/isaaclab_experimental/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst b/source/isaaclab_experimental/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst new file mode 100644 index 000000000000..03bcd096bfc8 --- /dev/null +++ b/source/isaaclab_experimental/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst @@ -0,0 +1,37 @@ +Added +^^^^^ + +* Added Warp-native uniform pose and velocity command terms with pointer-stable + command arrays, and a Warp-native :class:`NullCommand` term for environments + without commands. +* Added a Warp-native ``height_scan`` observation term (eager until sensor + capture-readiness lands) with ray-count output-dimension inference. +* Added opportunistic curriculum adaptation: curriculum terms swap to warp + twins when one exists (e.g. terrain levels), keeping the legacy ID-based + fallback for stable terms without twins. +* Added :class:`~isaaclab_experimental.managers.CurriculumManager` and a + boolean-mask reset path for Warp manager-based environments, retaining compact + environment IDs only for legacy host consumers. +* Added Warp-native terrain curricula backed by cached zero-copy Warp views of + terrain and scene origins. +* Added a boolean ``env_mask`` option to :meth:`ManagerBasedEnvWarp.reset_to`, the + ``ISAACLAB_WARP_CAPTURE`` environment variable to force eager execution, and + :attr:`WarpGraphCache.captured_stages` introspection for capture-coverage checks. + +Changed +^^^^^^^ + +* **Breaking:** Removed the experimental ``ManagerCallSwitch`` and + ``MANAGER_CALL_CONFIG`` interface. Warp environments now instantiate Warp + managers directly and automatically keep managers with non-capturable terms + on eager execution. + +Fixed +^^^^^ + +* Fixed Warp event state leaking across environments and configurations, and + corrected center-of-mass sampling and termination metrics for partial resets. +* Fixed captured velocity-command terms to read current root state on every + replay instead of stale lazy-derived buffers. +* Fixed identity quaternion initialization for the Warp-native uniform pose + command term. diff --git a/source/isaaclab_experimental/changelog.d/jichuanh-warp-frontend-execution.minor.rst b/source/isaaclab_experimental/changelog.d/jichuanh-warp-frontend-execution.minor.rst new file mode 100644 index 000000000000..731d818472de --- /dev/null +++ b/source/isaaclab_experimental/changelog.d/jichuanh-warp-frontend-execution.minor.rst @@ -0,0 +1,5 @@ +Added +^^^^^ + +* Added mask-native recorded launch replay and selective CUDA graph capture to + the Warp manager-based and direct environment frontends. diff --git a/source/isaaclab_experimental/changelog.d/warp-manager-bridge.rst b/source/isaaclab_experimental/changelog.d/warp-manager-bridge.rst new file mode 100644 index 000000000000..5f47328da3b0 --- /dev/null +++ b/source/isaaclab_experimental/changelog.d/warp-manager-bridge.rst @@ -0,0 +1,50 @@ +Added +^^^^^ + +* Added ``--frontend {torch,warp}`` to the shared reinforcement-learning + training and play CLIs (all supported RL libraries) for selecting the + environment runtime; default ``torch`` is unchanged. The ``rlinf`` + integration constructs environments inside the external framework and is + not frontend-routable. +* Added :mod:`isaaclab_experimental.envs.frontend` runtime selector and + :meth:`isaaclab_experimental.managers.SceneEntityCfg.from_stable` used by + ``--frontend=warp`` to adapt stable cfgs onto the warp runtime. +* Added ``warp_entry_point`` registration support: a stable direct task may + declare its warp environment class, which ``--frontend=warp`` constructs + with the stable configuration. +* Added observation-noise conversion to the warp frontend: stable noise cfgs + (constant/uniform/gaussian) swap to their warp-native twins during cfg + adaptation; cfgs without a twin (class-based noise models, customized or + tensor-valued parameters) are a hard error instead of being silently + ignored. The Warp observation manager rejects non-warp noise cfgs and + plumbs the shared per-env RNG state to function-style noise kernels. +* Added warp adapters for the stable + :class:`~isaaclab.envs.mdp.events.randomize_rigid_body_material` and + :class:`~isaaclab.envs.mdp.events.randomize_rigid_body_mass` startup event + terms, so tasks using them run under ``--frontend=warp`` with randomization + active. +* Added deterministic warp twin resolution: a stable symbol's twin is looked + up on the mirrored experimental package tree (``isaaclab.* ↔ + isaaclab_experimental.*``, ``isaaclab_tasks.* ↔ + isaaclab_tasks_experimental.*``); missing twins are collected and reported + in one failure. No registration is needed. + +Changed +^^^^^^^ + +* Changed :class:`~isaaclab_experimental.envs.ManagerBasedRLEnvWarp` to adapt + its configuration in ``__init__`` via + :meth:`~isaaclab_experimental.envs.frontend.WarpFrontend.adapt_cfg`, so + registered warp task variants can derive from stable configurations instead + of duplicating them. +* Changed the experimental warp ``RewardManager`` to merge dictionaries + returned by class-based reward term ``reset()`` into its episode-log extras + (persistent Warp buffer views, CUDA-graph safe); used to report + ``Metrics/success_rate`` under ``--frontend warp``. + +Fixed +^^^^^ + +* Fixed :class:`~isaaclab_experimental.envs.ManagerBasedRLEnvWarp` to probe + :meth:`~isaaclab.sim.SimulationContext.has_active_visualizers` after the + ``get_setting`` API was reshaped. diff --git a/source/isaaclab_experimental/changelog.d/warp-scene-mask-native.rst b/source/isaaclab_experimental/changelog.d/warp-scene-mask-native.rst new file mode 100644 index 000000000000..d27d57e11576 --- /dev/null +++ b/source/isaaclab_experimental/changelog.d/warp-scene-mask-native.rst @@ -0,0 +1,5 @@ +Added +^^^^^ + +* Added scene reset capture-eligibility registration with the environment-owned + Warp graph cache, derived from scene composition. diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/__init__.pyi b/source/isaaclab_experimental/isaaclab_experimental/envs/__init__.pyi index ef3d01d6b4ec..109fa5f8d06c 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/__init__.pyi +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/__init__.pyi @@ -6,13 +6,11 @@ __all__ = [ "mdp", "DirectRLEnvWarp", - "InteractiveSceneWarp", "ManagerBasedEnvWarp", "ManagerBasedRLEnvWarp", ] from . import mdp from .direct_rl_env_warp import DirectRLEnvWarp -from .interactive_scene_warp import InteractiveSceneWarp from .manager_based_env_warp import ManagerBasedEnvWarp from .manager_based_rl_env_warp import ManagerBasedRLEnvWarp diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/direct_rl_env_warp.py b/source/isaaclab_experimental/isaaclab_experimental/envs/direct_rl_env_warp.py index d69b13a8feec..77c08de942f5 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/direct_rl_env_warp.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/direct_rl_env_warp.py @@ -27,17 +27,19 @@ from isaaclab.envs.common import VecEnvObs, VecEnvStepReturn from isaaclab.envs.direct_rl_env import DirectRLEnv from isaaclab.envs.direct_rl_env_cfg import DirectRLEnvCfg +from isaaclab.envs.ui import ViewportCameraController from isaaclab.envs.utils.spaces import sample_space, spec_to_gym_space - -# from isaaclab.envs.ui import ViewportCameraController from isaaclab.managers import EventManager +from isaaclab.scene import InteractiveScene from isaaclab.sim import SimulationContext from isaaclab.sim.utils import use_stage from isaaclab.utils.noise import NoiseModel from isaaclab.utils.seed import configure_seed from isaaclab.utils.timer import Timer +from isaaclab.utils.version import has_kit +from isaaclab.utils.warp import WarpLaunchCache -from isaaclab_experimental.envs.interactive_scene_warp import InteractiveSceneWarp +from isaaclab_experimental.utils.warp import increment_all_int32, zero_masked_int32 from isaaclab_experimental.utils.warp_graph_cache import WarpGraphCache # from isaacsim.core.simulation_manager import SimulationManager @@ -51,26 +53,10 @@ """Enable outer step() timer only. Set DEBUG_TIMER_STEP=1 env var to enable.""" DEBUG_TIMERS = os.environ.get("DEBUG_TIMERS", "0") == "1" -"""Enable all fine-grained inner timers (adds wp.synchronize per sub-phase). Set DEBUG_TIMERS=1 env var to enable.""" - - -@wp.kernel -def zero_mask_int32( - mask: wp.array(dtype=wp.bool), - data: wp.array(dtype=wp.int32), -): - env_index = wp.tid() - if mask[env_index]: - data[env_index] = 0 - -@wp.kernel -def add_to_env( - data: wp.array(dtype=wp.int32), - value: wp.int32, -): - env_index = wp.tid() - data[env_index] += value +WARP_DIRECT_CAPTURE = os.environ.get("ISAACLAB_WARP_DIRECT_CAPTURE", "0") == "1" +"""Enable direct-environment stage capture. Eager execution is the correctness-first default.""" +"""Enable all fine-grained inner timers (adds wp.synchronize per sub-phase). Set DEBUG_TIMERS=1 env var to enable.""" class DirectRLEnvWarp(DirectRLEnv): @@ -142,6 +128,10 @@ def __init__(self, cfg: DirectRLEnvCfg, render_mode: str | None = None, **kwargs if "cuda" in self.device: torch.cuda.set_device(self.device) + # One owner-held launcher serves both uncaptured replay and kernels + # submitted while an outer direct-stage graph is being captured. + self._warp_launch = WarpLaunchCache(device=self.device) + # print useful information print("[INFO]: Base environment:") print(f"\tEnvironment device : {self.device}") @@ -162,21 +152,16 @@ def __init__(self, cfg: DirectRLEnvCfg, render_mode: str | None = None, **kwargs with Timer("[INFO]: Time taken for scene creation", "scene_creation"): # set the stage context for scene creation steps which use the stage with use_stage(self.sim.stage): - self.scene = InteractiveSceneWarp(self.cfg.scene) + self.scene = InteractiveScene(self.cfg.scene) self._setup_scene() self.scene.initialize_renderers() # attach_stage_to_usd_context() print("[INFO]: Scene manager: ", self.scene) - # set up camera viewport controller - # viewport is not available in other rendering modes so the function will throw a warning - # FIXME: This needs to be fixed in the future when we unify the UI functionalities even for - # non-rendering modes. - has_gui = bool(self.sim.get_setting("/isaaclab/has_gui")) - offscreen_render = bool(self.sim.get_setting("/isaaclab/render/offscreen")) - if has_gui or offscreen_render: - # self.viewport_camera_controller = ViewportCameraController(self, self.cfg.viewer) - self.viewport_camera_controller = None + # Initialize when a Kit viewport exists. ViewportCameraController uses omni.kit + # (renderer camera); skip in kitless Newton-only runs where no Kit app is running. + if (self.sim.has_gui or self.sim.has_active_visualizers()) and has_kit(): + self.viewport_camera_controller = ViewportCameraController(self, self.cfg.viewer) else: self.viewport_camera_controller = None @@ -243,8 +228,9 @@ def __init__(self, cfg: DirectRLEnvCfg, render_mode: str | None = None, **kwargs self.torch_reset_time_outs: torch.Tensor = None self.torch_episode_length_buf: torch.Tensor = None - # Warp CUDA graph cache for capture-or-replay - self._graph_cache = WarpGraphCache() + # Direct stages are uncaptured by default. The owner-held launch cache + # still replays their pointer-stable kernels without call-site branches. + self._warp_graph_cache = WarpGraphCache(enabled=WARP_DIRECT_CAPTURE, device=self.device) # setup the action and observation spaces for Gym self._configure_gym_env_spaces() @@ -427,10 +413,10 @@ def step(self, action: torch.Tensor) -> VecEnvStepReturn: # set actions into buffers # simulate with Timer(name="apply_action", msg="Action processing step took:", enable=DEBUG_TIMERS): - self._graph_cache.capture_or_replay("action", self.step_warp_action) + self._warp_graph_cache.call("DirectAction_step", self.step_warp_action) - # write_data_to_sim runs outside the CUDA graph because _apply_actuator_model - # uses torch ops (wp.to_torch + torch arithmetic) that cross CUDA streams. + # Keep scene writes outside the task graph until scene, sensor, and + # actuator capturability have been validated as one backend boundary. with Timer(name="write_data_to_sim_loop", msg="Write data to sim (loop) took:", enable=DEBUG_TIMERS): self.scene.write_data_to_sim() @@ -446,13 +432,16 @@ def step(self, action: torch.Tensor) -> VecEnvStepReturn: self.scene.update(dt=self.physics_dt) self.common_step_counter += 1 # total step (common for all envs) - with Timer(name="end_pre_graph", msg="End pre-graph took:", enable=DEBUG_TIMERS): - self._graph_cache.capture_or_replay("end_pre", self._step_warp_end_pre) - # write_data_to_sim runs uncaptured — it uses torch ops that cross CUDA streams. + # Reset can cross the legacy actuator Torch-ID boundary. Keep the whole + # stage outside the graph for now; its persistent kernels still use the + # owner-held recorded-launch cache. + with Timer(name="end_pre", msg="End pre-step took:", enable=DEBUG_TIMERS): + self._step_warp_end_pre() + # Keep the post-reset scene write at the explicit backend boundary. with Timer(name="write_data_to_sim_post", msg="Write data to sim (post-reset) took:", enable=DEBUG_TIMERS): self.scene.write_data_to_sim() with Timer(name="end_post_graph", msg="End post-graph took:", enable=DEBUG_TIMERS): - self._graph_cache.capture_or_replay("end_post", self._step_warp_end_post) + self._warp_graph_cache.call("DirectEndPost_step", self._step_warp_end_post) # Visualization hook — runs after CUDA graph scope. Override in subclass # to update markers or other non-graphable visual elements. @@ -479,20 +468,19 @@ def _post_step_visualize(self) -> None: def step_warp_action(self) -> None: self._apply_action() - # Note: scene.write_data_to_sim() is called separately outside the CUDA graph - # capture scope because it invokes _apply_actuator_model() which uses torch - # arithmetic (wp.to_torch + torch ops). This would cause a CUDA stream crossing - # error during graph capture. Moving it outside is safe since it runs every step. + # Scene writes remain a separate backend stage. This keeps the Warp-first + # task path correct without making capture support a prerequisite. def _step_warp_end_pre(self) -> None: - """Capturable portion before write_data_to_sim (pure warp kernels).""" - wp.launch( - add_to_env, + """Run the pre-write Warp stage, including the uncaptured reset boundary.""" + self._warp_launch.launch( + increment_all_int32, dim=self.num_envs, inputs=[ self._episode_length_buf_wp, 1, ], + site=("direct_rl_env", "episode_length_increment"), ) self._get_dones() self._get_rewards() @@ -570,13 +558,8 @@ def render(self, recompute: bool = False) -> np.ndarray | None: if self.render_mode == "human" or self.render_mode is None: return None elif self.render_mode == "rgb_array": - # check that if any render could have happened - has_gui = bool(self.sim.get_setting("/isaaclab/has_gui")) - offscreen_render = bool(self.sim.get_setting("/isaaclab/render/offscreen")) - # Rendering is possible if we have GUI or offscreen rendering enabled - can_render = has_gui or offscreen_render - - if not can_render: + # rendering requires a GUI or offscreen rendering (mirrors the stable env) + if not (self.sim.has_gui or self.sim.has_offscreen_render): render_mode_name = "NO_GUI_OR_RENDERING" raise RuntimeError( f"Cannot render '{self.render_mode}' when the simulation render mode is" @@ -614,6 +597,8 @@ def render(self, recompute: bool = False) -> np.ndarray | None: def close(self): """Cleanup for the environment.""" if not self._is_closed: + self._warp_graph_cache.invalidate() + self._warp_launch.reset() # close entities related to the environment # note: this is order-sensitive to avoid any dangling references if self.cfg.events: @@ -740,13 +725,14 @@ def _reset_idx(self, mask: wp.array | None = None): # self._observation_noise_model.reset(env_ids) # reset the episode length buffer - wp.launch( - zero_mask_int32, + self._warp_launch.launch( + zero_masked_int32, dim=self.num_envs, inputs=[ mask, self._episode_length_buf_wp, ], + site=("direct_rl_env", "episode_length_reset", mask), ) """ diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/frontend.py b/source/isaaclab_experimental/isaaclab_experimental/envs/frontend.py new file mode 100644 index 000000000000..91f65daef0d0 --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/frontend.py @@ -0,0 +1,589 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp runtime frontend for IsaacLab tasks (``--frontend warp``). + +:class:`WarpFrontend` encapsulates everything the warp runtime needs from a +stable task definition: + +* Manager-based tasks: adapt the stable cfg in place (Newton physics check, + :class:`SceneEntityCfg` promotion, MDP twin swap) and construct + :class:`ManagerBasedRLEnvWarp`. All warp MDP twins (functions *and* classes) + must exist for the chosen task — a missing twin is a hard failure, not a + silent drop. +* Direct tasks: construct the warp env class the stable registration declares + via its ``warp_entry_point`` kwarg, sharing the stable cfg. + +Twin resolution is purely mechanical: the experimental packages mirror the +stable tree (``isaaclab.* ↔ isaaclab_experimental.*``, +``isaaclab_tasks.* ↔ isaaclab_tasks_experimental.*``), and a stable symbol's +twin is looked up by name on the nearest ``.mdp`` package of the mirrored +path. There is no registration; anything unresolved is collected and reported +in one hard failure. The torch runtime never touches this module — the shared +RL CLI dispatches to plain ``gym.make`` before importing it, so this +(optional) package stays warp-only. +""" + +from __future__ import annotations + +import importlib +import importlib.util +import logging +from collections.abc import Iterable, Iterator +from enum import StrEnum +from types import ModuleType +from typing import Any, ClassVar + +import gymnasium as gym +import torch + +from isaaclab.envs import DirectMARLEnvCfg, DirectRLEnvCfg, ManagerBasedRLEnvCfg +from isaaclab.managers.scene_entity_cfg import SceneEntityCfg as _StableSceneEntityCfg + +logger = logging.getLogger(__name__) + + +__all__ = [ + "FrontendIncompatibleError", + "WarpFrontend", + "Workflow", +] + + +class Workflow(StrEnum): + """Manager-based vs direct env workflow.""" + + MANAGER_BASED = "manager_based" + DIRECT = "direct" + + +class FrontendIncompatibleError(RuntimeError): + """Raised when the warp frontend can't run the requested task.""" + + +class WarpFrontend: + """Builds environments on the warp runtime from stable task definitions. + + The class is a stateless namespace: twin resolution is a pure function of + the stable module paths and the mirrored package tree (:attr:`MIRROR_ROOTS`); + there is no registry and no import-order dependence. + """ + + # Stable package roots mirrored by their warp implementation packages. Twin + # resolution is purely mechanical: mirror the root, then look up the nearest + # enclosing ``.mdp`` package on the mirrored path. + MIRROR_ROOTS: ClassVar[dict[str, str]] = { + "isaaclab_tasks": "isaaclab_tasks_experimental", + "isaaclab": "isaaclab_experimental", + } + + # Prefixes that identify warp-origin symbols and registrations. + WARP_ROOT_PREFIXES: ClassVar[tuple[str, ...]] = tuple(MIRROR_ROOTS.values()) + + # Top-level cfg groups whose managers run warp-first. Only terms under these + # are adapted (SceneEntityCfg promotion + MDP twin swap). The event manager + # is warp-first too — it invokes term funcs with a Warp env-mask, so a stable + # event func (which expects torch ``env_ids``) breaks at runtime; its funcs + # must be swapped to warp twins. The command manager is the warp-native + # ``CommandManager`` (captured), so command term ``class_type``s must swap + # to their warp twins as well. A stable term left on a warp manager would + # break, so a missing twin in these groups is a hard error. The recorder + # manager runs on the stable implementation and is skipped entirely; + # curriculum is adapted opportunistically (see ``OPTIONAL_WARP_GROUPS``). + WARP_MANAGED_GROUPS: ClassVar[frozenset[str]] = frozenset( + {"observations", "rewards", "terminations", "actions", "events", "commands"} + ) + + # Groups adapted opportunistically: a warp twin is swapped in when one exists, + # otherwise the stable term stays on the manager's legacy (host-ID) fallback. + # Curriculum is the only such group: its manager supports stable terms by + # design, at the cost of eager execution and one ID materialization per reset. + OPTIONAL_WARP_GROUPS: ClassVar[frozenset[str]] = frozenset({"curriculum"}) + + # ------------------------------------------------------------------ + # Env construction + # ------------------------------------------------------------------ + + @classmethod + def build_env(cls, env_cfg: Any, task_id: str, **construct_kwargs: Any) -> gym.Env: + """Construct the warp env for ``task_id`` from a stable env cfg. + + Args: + env_cfg: Stable env cfg. Manager-based cfgs are mutated in place. + task_id: Gym registration id, e.g. ``"Isaac-Cartpole"``. + **construct_kwargs: Forwarded to the env constructor (``render_mode``, …). + + Raises: + FrontendIncompatibleError: If the warp runtime can't run the task + (wrong physics, missing MDP twins, direct task without a warp + implementation). + """ + if cls._detect_workflow(env_cfg) is Workflow.DIRECT: + return cls._build_direct_env(env_cfg, task_id, **construct_kwargs) + return cls._build_manager_env(env_cfg, **construct_kwargs) + + @classmethod + def _build_manager_env(cls, env_cfg: Any, **construct_kwargs: Any) -> gym.Env: + """Construct :class:`ManagerBasedRLEnvWarp`; the env adapts the cfg itself. + + Imported lazily so that ``--frontend=torch`` callers don't pay the + ``isaaclab_experimental.envs`` import cost. The env runs + :meth:`adapt_cfg` in ``__init__``, so stable cfgs and warp-native cfgs + take the same path. + """ + from isaaclab_experimental.envs import ManagerBasedRLEnvWarp + + return ManagerBasedRLEnvWarp(cfg=env_cfg, **construct_kwargs) + + @classmethod + def _build_direct_env(cls, env_cfg: Any, task_id: str, **construct_kwargs: Any) -> gym.Env: + """Construct a direct warp env. + + Direct workflows aren't cfg-adapted: a hand-written warp env class + implements the task. Preferred path: the stable registration declares + it via ``warp_entry_point`` and shares the stable cfg. Fallback: the + task itself is registered under the warp packages (warp-native cfg). + """ + env_class = cls._resolve_direct_warp_class(task_id) + if env_class is None: + # No warp_entry_point: the task itself must be a warp-native registration. + cls._assert_direct_warp_registration(task_id) + return gym.make(task_id, cfg=env_cfg, **construct_kwargs) + # Declared warp class + stable cfg: swap only the env class. + cls._require_newton_physics(env_cfg, type(env_cfg).__name__) + return env_class(cfg=env_cfg, **construct_kwargs) + + # ------------------------------------------------------------------ + # Cfg adaptation (manager-based) + # ------------------------------------------------------------------ + + @classmethod + def adapt_cfg(cls, cfg: Any) -> None: + """Mutate a stable manager-based ``cfg`` in place for the warp managers. + + Idempotent: re-running on an already-adapted cfg is a no-op (the steps + below skip warp-origin symbols and already-promoted entities). + + Three steps, each independently testable: + + 1. :meth:`_require_newton_physics` — hard check that ``cfg.sim.physics`` + is :class:`~isaaclab_newton.physics.NewtonCfg`. The user is + responsible for selecting the Newton variant of the task's + :class:`PresetCfg` via ``presets=newton_mjwarp``; we don't auto-inject. + 2. :meth:`_promote_scene_entity_cfgs` — replace stable + :class:`~isaaclab.managers.SceneEntityCfg` instances under each + term's ``params`` with the warp variant (which adds warp-cached + ``joint_mask``, ``joint_ids_wp``, ``body_ids_wp`` fields). + 3. :meth:`_swap_mdp` — for every MDP term found anywhere in the cfg tree + (discovered by :meth:`_walk_terms` via :class:`ManagerTermBaseCfg` + subclassing, not by hard-coded attribute names), replace any stable + ``func`` *or* ``class_type`` with its same-named warp twin. A missing + twin raises :class:`FrontendIncompatibleError` — partial coverage is + unsafe under the warp managers' kernel-only signature. + """ + label = type(cfg).__name__ + cls._require_newton_physics(cfg, label) + cls._promote_scene_entity_cfgs(cfg) + cls._swap_mdp(cfg, label) + cls._swap_noise_cfgs(cfg, label) + + @staticmethod + def _require_newton_physics(cfg: Any, label: str) -> None: + """Block unless ``cfg.sim.physics`` is :class:`NewtonCfg`. + + The warp managers' assets read state through :class:`NewtonManager`; + a :class:`PhysxCfg` (or unresolved :class:`PresetCfg`) is a hard + incompatibility. The fix is to pass ``presets=newton_mjwarp`` on the CLI + so Hydra resolves the task's :class:`PresetCfg` wrapper to the Newton + field before construction. + """ + from isaaclab_newton.physics import NewtonCfg + + physics = getattr(getattr(cfg, "sim", None), "physics", None) + if isinstance(physics, NewtonCfg): + return + raise FrontendIncompatibleError( + f"warp env {label!r}: expected cfg.sim.physics to be NewtonCfg," + f" got {type(physics).__name__!r}. Pass `presets=newton_mjwarp` on the CLI so" + f" Hydra resolves the task's PresetCfg wrapper to the Newton variant." + ) + + @classmethod + def _promote_scene_entity_cfgs(cls, cfg: Any) -> None: + """Replace stable :class:`SceneEntityCfg` instances with the warp variant. + + Iterates every term cfg in the tree (via :meth:`_walk_terms`) and + rebuilds any stable :class:`SceneEntityCfg` value under ``term.params`` + through :meth:`isaaclab_experimental.managers.SceneEntityCfg.from_stable`. + The warp variant subclasses the stable one, so type checks elsewhere + stay valid; the new fields (``joint_mask`` / ``joint_ids_wp`` / + ``body_ids_wp``) are filled at :meth:`resolve` time by the warp scene. + """ + from isaaclab_experimental.managers.scene_entity_cfg import SceneEntityCfg as _WarpSceneEntityCfg + + promoted: list[str] = [] + for path, term in cls._walk_terms(cfg): + if not path or path[0] not in (cls.WARP_MANAGED_GROUPS | cls.OPTIONAL_WARP_GROUPS): + continue # term runs on a stable manager; keep the stable entity + params = getattr(term, "params", None) + if not isinstance(params, dict): + continue + for key, value in list(params.items()): + # Skip non-entities and already-promoted values (idempotence). + if isinstance(value, _WarpSceneEntityCfg) or not isinstance(value, _StableSceneEntityCfg): + continue + params[key] = _WarpSceneEntityCfg.from_stable(value) + promoted.append(f"{'.'.join(path)}.params[{key!r}] ({value.name!r})") + if promoted: + logger.info( + "frontend.warp: promoted %d SceneEntityCfg instance(s) to warp variant:\n %s", + len(promoted), + "\n ".join(promoted), + ) + + @classmethod + def _swap_mdp(cls, cfg: Any, label: str) -> None: + """Replace ``term.func`` and ``term.class_type`` with their warp twins. + + Iterates every term cfg in the tree (via :meth:`_walk_terms`) and on + each term swaps whichever of ``func`` / ``class_type`` is a + stable-origin callable. Twin lookup is name-based, in the order given by + :meth:`_twin_modules`: the warp mirror of the cfg's own task MDP + namespace first (task twins win, even for symbols defined in core + packages), then the mirror of the symbol's defining package (covers + terms borrowed from another task family, e.g. the Ant task reusing + Humanoid MDP terms), then the shared fallback. Any missing twin raises + :class:`FrontendIncompatibleError` listing every affected term — + partial swaps would leave torch-style callables in the cfg and the warp + managers would call them with the wrong signature. + + The warp-side declarations (``out_dim``, ``axes``, ``observation_type``) + that the warp managers need at init are *not* supplied by this swap; + they travel with the warp twin function itself via its own + ``@generic_io_descriptor_warp(out_dim=…)`` decorator. This method only + substitutes the callable; the manager reads the new func's annotations + when it parses the term cfg. + """ + # Highest-priority twin source: warp mirrors of the cfg's own task namespace. + cfg_route_modules = cls._cfg_route_modules(cfg) + module_cache: dict[str, list[ModuleType]] = {} # defining module -> modules to search + searched: set[str] = set() # module names, for the error message + + swapped = 0 + missing: list[tuple[str, str, str]] = [] # (location, attr, symbol) + for path, term in cls._walk_terms(cfg): + if not path or path[0] not in (cls.WARP_MANAGED_GROUPS | cls.OPTIONAL_WARP_GROUPS): + continue # term runs on a stable manager; leave it stable + optional = path[0] in cls.OPTIONAL_WARP_GROUPS + location = ".".join(path) + for attr in ("func", "class_type"): # a term implements via either attr + stable = getattr(term, attr, None) + if stable is None or not cls._is_swap_candidate(stable): + continue + origin = getattr(stable, "__module__", "") or "" + if origin not in module_cache: + module_cache[origin] = cls._twin_modules(origin, cfg_route_modules) + searched.update(m.__name__ for m in module_cache[origin]) + twin = cls._resolve_warp_twin(stable.__name__, module_cache[origin]) + if twin is None: + if not optional: + missing.append((location, attr, stable.__name__)) # collect every miss; report once below + continue # optional group: the stable term stays on the legacy fallback + setattr(term, attr, twin) + swapped += 1 + + if missing: + lines = "\n ".join(f"{loc}.{attr}: no warp twin for {sym!r}" for loc, attr, sym in missing) + raise FrontendIncompatibleError( + f"warp env {label!r}: missing warp MDP twins (searched {sorted(searched)}):\n {lines}" + ) + + logger.info("frontend.warp: swapped %d MDP symbol(s) to warp twins", swapped) + + # ------------------------------------------------------------------ + # Workflow detection and direct-task resolution + # ------------------------------------------------------------------ + + @staticmethod + def _detect_workflow(cfg: Any) -> Workflow: + """Classify the env cfg into manager-based or direct (used to pick the build path). + + Note: + The env cfg roots (ManagerBasedRLEnvCfg, DirectRLEnvCfg, + DirectMARLEnvCfg) do not share a common base class. When a new cfg + root is added, extend the isinstance tuples below. + """ + if isinstance(cfg, ManagerBasedRLEnvCfg): + return Workflow.MANAGER_BASED + if isinstance(cfg, (DirectRLEnvCfg, DirectMARLEnvCfg)): + return Workflow.DIRECT + raise FrontendIncompatibleError( + f"Unrecognised env cfg type {type(cfg).__name__!r};" + f" expected ManagerBasedRLEnvCfg / DirectRLEnvCfg / DirectMARLEnvCfg subclass." + ) + + @staticmethod + def _resolve_direct_warp_class(task_id: str) -> type | None: + """Return the warp env class a stable direct task declares, if any. + + Stable direct registrations may carry a ``warp_entry_point`` kwarg of + the form ``"module.path:ClassName"`` (mirroring ``env_cfg_entry_point``) + naming the warp implementation of the same task. The class is + constructed with the *stable* cfg, so the task needs no parallel warp + registration or duplicated configuration. + """ + try: + spec = gym.spec(task_id) + except gym.error.NameNotFound as exc: + raise FrontendIncompatibleError( + f"--frontend=warp: task {task_id!r} is not registered with gymnasium." + ) from exc + target = (spec.kwargs or {}).get("warp_entry_point") + if not isinstance(target, str): + return None + module_name, _, class_name = target.partition(":") + try: + module = importlib.import_module(module_name) + except ImportError as exc: + raise FrontendIncompatibleError( + f"--frontend=warp: task {task_id!r} declares warp_entry_point {target!r}," + f" but it failed to import: {exc}" + ) from exc + env_class = getattr(module, class_name, None) + if env_class is None: + raise FrontendIncompatibleError( + f"--frontend=warp: task {task_id!r} declares warp_entry_point {target!r}," + f" but {class_name!r} is not defined in {module_name!r}." + ) + return env_class + + @classmethod + def _assert_direct_warp_registration(cls, task_id: str) -> None: + """For direct workflows without a ``warp_entry_point``, the task must be warp-registered.""" + spec = gym.spec(task_id) + ep = spec.entry_point + module = ep if isinstance(ep, str) else (getattr(ep, "__module__", "") or "") + if not module.startswith(cls.WARP_ROOT_PREFIXES): + raise FrontendIncompatibleError( + f"--frontend=warp on direct task {task_id!r}: entry_point {ep!r}" + f" is not under {list(cls.WARP_ROOT_PREFIXES)}. Direct tasks must either" + f" declare a `warp_entry_point` in their stable registration or be" + f" registered as a warp env class." + ) + + # ------------------------------------------------------------------ + # Twin resolution + # ------------------------------------------------------------------ + + @classmethod + def _mirror_module(cls, module: str) -> str | None: + """Mechanical mirror of a stable module path, or ``None`` if not mirrored.""" + if not isinstance(module, str) or not module: + return None + root, _, rest = module.partition(".") # only the top-level package root is swapped + mirror_root = cls.MIRROR_ROOTS.get(root) + if mirror_root is None: + return None + return f"{mirror_root}.{rest}" if rest else mirror_root + + @staticmethod + def _nearest_mdp_module(mirrored: str) -> str | None: + """Deepest existing ``.mdp`` package on a mirrored module path. + + If the path already contains an ``.mdp`` segment, the search starts at + that boundary (``...velocity.mdp.rewards`` resolves to + ``...velocity.mdp``); otherwise the path is walked upward until a + mirrored ``.mdp`` package exists. Existence is probed with + :func:`importlib.util.find_spec`; a prefix that does not exist simply + ends that probe, so resolution is a pure function of the installed + package tree. + """ + parts = mirrored.split(".") + if "mdp" in parts: + parts = parts[: parts.index("mdp")] # start the walk at the .mdp boundary + for depth in range(len(parts), 0, -1): # deepest prefix first + candidate = ".".join([*parts[:depth], "mdp"]) + try: + if importlib.util.find_spec(candidate) is not None: + return candidate + except ModuleNotFoundError: + continue + return None + + @classmethod + def _swap_noise_cfgs(cls, cfg: Any, label: str) -> None: + """Replace stable observation-noise cfgs with their warp-native twins. + + The warp observation manager applies noise through the experimental noise + cfg family (in-place Warp kernels); a stable noise cfg would otherwise be + silently ignored, training against a different MDP. Twins resolve by class + name; data fields are copied while ``func`` keeps the twin's warp default. + Noise cfgs without a warp twin (e.g. class-based ``NoiseModelCfg``) are a + hard error — silently changing the MDP is never acceptable. + """ + import dataclasses + + from isaaclab.utils import noise as stable_noise + + from isaaclab_experimental.utils import noise as warp_noise + + unsupported: list[str] = [] + swapped = 0 + for path, term in cls._walk_terms(cfg): + if not path or path[0] != "observations": + continue + noise_cfg = getattr(term, "noise", None) + if noise_cfg is None: + continue + if type(noise_cfg).__module__.startswith(cls.WARP_ROOT_PREFIXES): + continue # already warp-native + twin_cls = getattr(warp_noise, type(noise_cfg).__name__, None) + if ( + twin_cls is None + or not twin_cls.__module__.startswith(cls.WARP_ROOT_PREFIXES) + or not isinstance(noise_cfg, stable_noise.NoiseCfg) + ): + unsupported.append(f"{'.'.join(path)}: {type(noise_cfg).__name__}") + continue + if noise_cfg.func != type(noise_cfg)().func: + # A user-customized noise func has no warp twin; replacing it with the + # twin's default would silently change the MDP. + unsupported.append(f"{'.'.join(path)}: {type(noise_cfg).__name__} with custom func {noise_cfg.func!r}") + continue + fields = {f.name: getattr(noise_cfg, f.name) for f in dataclasses.fields(noise_cfg) if f.name != "func"} + if any(isinstance(value, torch.Tensor) for value in fields.values()): + # The warp noise kernels take scalar parameters; tensor-valued ranges + # (per-element noise) have no warp twin yet. + unsupported.append(f"{'.'.join(path)}: {type(noise_cfg).__name__} with tensor-valued parameters") + continue + term.noise = twin_cls(**fields) + swapped += 1 + if unsupported: + raise FrontendIncompatibleError( + f"warp env {label!r}: observation noise cfgs without warp twins:\n " + "\n ".join(unsupported) + ) + if swapped: + logger.info("frontend.warp: swapped %d observation noise cfg(s) to warp twins", swapped) + + @staticmethod + def _import_twin_module(target: str) -> ModuleType: + """Import a resolved twin module; a module that exists but breaks is a hard error.""" + try: + return importlib.import_module(target) + except ImportError as exc: + raise FrontendIncompatibleError(f"warp twin module {target!r} exists but failed to import: {exc}") from exc + + @classmethod + def _cfg_route_modules(cls, cfg: Any) -> list[ModuleType]: + """Warp MDP mirrors of the cfg's class hierarchy, subclass first. + + A stable cfg consumes terms through its task's ``mdp`` namespace, which + re-exports symbols that may be *defined* in core or shared packages. The + warp mirror of that namespace is therefore the primary place to resolve + twins; it is found by mirroring the modules of ``type(cfg).__mro__`` — + the subclass module first (robot-specific cfgs), then base-cfg modules + (task-family bases). + """ + modules: list[ModuleType] = [] + for klass in type(cfg).__mro__: # subclass first: robot cfg wins over task-family base + mirrored = cls._mirror_module(getattr(klass, "__module__", "") or "") + if mirrored is None: + continue + target = cls._nearest_mdp_module(mirrored) + if target is None: + continue + module = cls._import_twin_module(target) + if module not in modules: + modules.append(module) + return modules + + @classmethod + def _twin_modules(cls, symbol_module: str, cfg_route_modules: list[ModuleType]) -> list[ModuleType]: + """Warp modules to consult for a stable symbol's twin, in preference order. + + 1. The warp mirrors of the cfg's own task MDP namespace + (:meth:`_cfg_route_modules`) — task-specific twins win, including + twins for symbols defined in core/shared packages. + 2. The mirror of the symbol's defining package — covers terms borrowed + from another task family and, for core-defined symbols, the shared + :mod:`isaaclab_experimental.envs.mdp` twins (the mirror of + :mod:`isaaclab.envs.mdp`). + """ + modules = list(cfg_route_modules) + # Fallback: the mirror of the package that defines the symbol. + mirrored = cls._mirror_module(symbol_module) + if mirrored is not None: + target = cls._nearest_mdp_module(mirrored) + if target is not None: + module = cls._import_twin_module(target) + if module not in modules: + modules.append(module) + return modules + + @classmethod + def _resolve_warp_twin(cls, name: str, modules: Iterable[ModuleType]) -> Any | None: + """Return the same-named symbol from ``modules`` that originates under the warp packages.""" + for module in modules: + candidate = getattr(module, name, None) + if candidate is None: + continue + origin = getattr(candidate, "__module__", "") or "" + if origin.startswith(cls.WARP_ROOT_PREFIXES): # re-exported stable symbols don't count + return candidate + return None + + @classmethod + def _is_swap_candidate(cls, value: Any) -> bool: + """Heuristic: callable or class whose origin is *not already* warp.""" + if not callable(value): + return False + origin = getattr(value, "__module__", "") or "" + if origin.startswith(cls.WARP_ROOT_PREFIXES): + return False # already a warp twin (idempotent) + return True + + @staticmethod + def _walk_terms(node: Any, path: tuple[str, ...] = ()) -> Iterator[tuple[tuple[str, ...], Any]]: + """Yield ``(path, term)`` for every MDP term cfg in the cfg tree. + + A "term" is a :class:`ManagerTermBaseCfg` (observation/reward/ + termination/event/curriculum) *or* an :class:`ActionTermCfg` / + :class:`CommandTermCfg` — the latter two are separate bases that are + **not** ``ManagerTermBaseCfg`` subclasses, yet carry a swappable + ``class_type``, so they must be matched explicitly. + + Behavior at each node: + + * Match (a term cfg instance): yield ``(path, node)`` and stop — do not + descend into ``term.params`` / ``term.func`` / ``term.class_type``. + * Configclass: don't yield; recurse into every non-underscore *instance* + attribute (``vars(node)``), extending the path. ``observations``, + ``rewards``, ``events``, ``actions``, sub-groups like + ``observations.policy``, and anything nested deeper are reached + transparently. + * Anything else (plain Python data, callables, non-configclass objects): + stop. No yield, no recursion. + + Iterating the instance ``__dict__`` mirrors how the warp managers + consume group cfgs (they iterate ``cfg.__dict__.items()``), so the + walker sees exactly the terms the managers will see — including terms + assigned in ``__post_init__`` — while never descending into methods or + nested class objects, which live on the class rather than the instance. + + Driven entirely by type — no attribute names are hardcoded — so future + cfg layouts (extra observation groups, new nesting, etc.) are picked up + automatically as long as their terms subclass one of the term base cfgs. + """ + from isaaclab.managers.manager_term_cfg import ActionTermCfg, CommandTermCfg, ManagerTermBaseCfg + + if isinstance(node, (ManagerTermBaseCfg, ActionTermCfg, CommandTermCfg)): + yield path, node # a term: yield and stop; never descend into params/func + return + if not hasattr(node, "__dataclass_fields__"): + return # plain data / callables: not a cfg subtree + for name, value in vars(node).items(): # instance attrs — the same view the managers iterate + if name.startswith("_") or value is None: + continue + yield from WarpFrontend._walk_terms(value, path + (name,)) diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/interactive_scene_warp.py b/source/isaaclab_experimental/isaaclab_experimental/envs/interactive_scene_warp.py deleted file mode 100644 index f792b994f67b..000000000000 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/interactive_scene_warp.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Warp-native interactive scene with env_mask support for reset.""" - -from __future__ import annotations - -from collections.abc import Sequence - -import warp as wp - -from isaaclab.scene import InteractiveScene - - -class InteractiveSceneWarp(InteractiveScene): - """Interactive scene with warp-native env_mask support for reset. - - Extends :class:`InteractiveScene` to accept a boolean warp mask for selective resets, - avoiding the need to convert between env_ids and masks. - """ - - def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None = None): - """Reset scene entities using either env_ids or a boolean env_mask. - - Args: - env_ids: The indices of the environments to reset. Defaults to None (all instances). - env_mask: Boolean warp mask of shape (num_envs,). Defaults to None. - """ - # -- assets (support env_mask) - for articulation in self._articulations.values(): - articulation.reset(env_ids, env_mask=env_mask) - for deformable_object in self._deformable_objects.values(): - deformable_object.reset(env_ids) - for rigid_object in self._rigid_objects.values(): - rigid_object.reset(env_ids, env_mask=env_mask) - for surface_gripper in self._surface_grippers.values(): - surface_gripper.reset(env_ids) - for rigid_object_collection in self._rigid_object_collections.values(): - rigid_object_collection.reset(env_ids, env_mask=env_mask) - # -- sensors (no env_mask support) - for sensor in self._sensors.values(): - sensor.reset(env_ids) diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_env_warp.py b/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_env_warp.py index 2b4791d22a16..ee33598aba89 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_env_warp.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_env_warp.py @@ -16,11 +16,9 @@ # import builtins import contextlib -import importlib import logging import warnings from collections.abc import Sequence -from copy import deepcopy from typing import Any import torch @@ -30,16 +28,20 @@ from isaaclab.envs.manager_based_env_cfg import ManagerBasedEnvCfg from isaaclab.envs.ui import ViewportCameraController from isaaclab.envs.utils.io_descriptors import export_articulations_data, export_scene_data +from isaaclab.managers import RecorderManager +from isaaclab.scene import InteractiveScene from isaaclab.sim import SimulationContext from isaaclab.sim.utils import use_stage from isaaclab.ui.widgets import ManagerLiveVisualizer from isaaclab.utils.seed import configure_seed from isaaclab.utils.timer import Timer from isaaclab.utils.version import has_kit +from isaaclab.utils.warp import WarpLaunchCache -from isaaclab_experimental.envs.interactive_scene_warp import InteractiveSceneWarp as InteractiveScene -from isaaclab_experimental.utils.manager_call_switch import ManagerCallMode, ManagerCallSwitch +from isaaclab_experimental.managers import ActionManager, EventManager, ObservationManager +from isaaclab_experimental.utils.torch_utils import clone_obs_buffer from isaaclab_experimental.utils.warp import resolve_1d_mask +from isaaclab_experimental.utils.warp_graph_cache import WarpGraphCache # import logger logger = logging.getLogger(__name__) @@ -52,6 +54,10 @@ def initialize_rng_state( # output state: wp.array(dtype=wp.uint32), ): + """Initialize each env's persistent RNG state as an independent stream of the + + global ``seed`` (one ``wp.rand_init`` stream per env id). + """ env_id = wp.tid() state[env_id] = wp.rand_init(seed, wp.int32(env_id)) @@ -79,11 +85,8 @@ def __init__(self, cfg: ManagerBasedEnvCfg): self.cfg = cfg # initialize internal variables self._is_closed = False - # temporary debug runtime config for manager source/call switching. - cfg_source: dict | str | None = getattr(self.cfg, "manager_call_config", None) - max_modes: dict[str, int] | None = getattr(self.cfg, "manager_call_max_mode", None) - self._manager_call_switch = ManagerCallSwitch(cfg_source, max_modes=max_modes) - self._apply_manager_term_cfg_profile() + # Manager stages register their capture safety with this environment-owned cache. + self._warp_graph_cache = WarpGraphCache(device=self.cfg.sim.device) # set the seed for the environment if self.cfg.seed is not None: @@ -107,6 +110,10 @@ def __init__(self, cfg: ManagerBasedEnvCfg): if "cuda" in self.device: torch.cuda.set_device(self.device) + # Managers and MDP terms share one launcher so persistent call sites can + # replay outside graphs and naturally compose with outer stage capture. + self._warp_launch = WarpLaunchCache(device=self.device) + # print useful information print("[INFO]: Base environment:") print(f"\tEnvironment device : {self.device}") @@ -138,6 +145,10 @@ def __init__(self, cfg: ManagerBasedEnvCfg): self.scene.initialize_renderers() print("[INFO]: Scene manager: ", self.scene) + # Scene reset capture eligibility is a property of scene composition. The + # registration is forward-looking: scene resets currently run eagerly. + self._warp_graph_cache.register_capturability("Scene", getattr(self.scene, "reset_capture_safe", False)) + # Shared per-env Warp RNG state (accessible to all managers/terms via `env`). # This is a single stream per env (no lookup) and is initialized once when `num_envs` is known. self.rng_state_wp = wp.zeros((self.num_envs,), dtype=wp.uint32, device=self.device) @@ -154,6 +165,9 @@ def __init__(self, cfg: ManagerBasedEnvCfg): # Pre-allocated env masks (shared across managers/terms via `env`). self.ALL_ENV_MASK = wp.ones((self.num_envs,), dtype=wp.bool, device=self.device) self.ENV_MASK = wp.zeros((self.num_envs,), dtype=wp.bool, device=self.device) + # Reset graphs always read this owner-held pointer, while callers may + # provide a different mask object on each reset. + self.reset_mask_wp = wp.zeros((self.num_envs,), dtype=wp.bool, device=self.device) # Persistent scalar buffer for global env step count (stable pointer for capture). self._global_env_step_count_wp = wp.zeros((1,), dtype=wp.int32, device=self.device) @@ -172,7 +186,7 @@ def __init__(self, cfg: ManagerBasedEnvCfg): # create event manager # note: this is needed here (rather than after simulation play) to allow USD-related randomization events # that must happen before the simulation starts. Example: randomizing mesh scale - self.event_manager = self._manager_call_switch.resolve_manager_class("EventManager")(self.cfg.events, self) + self.event_manager = EventManager(self.cfg.events, self) # apply USD-related randomization events if "prestartup" in self.event_manager.available_modes: @@ -273,15 +287,9 @@ def device(self): return self.sim.device @property - def env_origins_wp(self) -> wp.array: - """Scene env origins as a warp ``vec3f`` array. Cached on first access.""" - if not hasattr(self, "_env_origins_wp"): - origins = self.scene.env_origins - if isinstance(origins, wp.array): - self._env_origins_wp = origins - else: - self._env_origins_wp = wp.from_torch(origins, dtype=wp.vec3f) - return self._env_origins_wp + def env_origins_wp(self) -> wp.array(dtype=wp.vec3f): + """Warp-owned environment origins [m] provided by the scene.""" + return self.scene.env_origins_wp def resolve_env_mask( self, @@ -360,21 +368,28 @@ def load_managers(self): :meth:`SimulationContext.reset_async` and it isn't possible to call async functions in the constructor. """ + # Reloading managers replaces pointer-stable buffers captured by prior stages. + self._warp_graph_cache.invalidate() # prepare the managers # -- event manager (we print it here to make the logging consistent) print("[INFO] Event Manager: ", self.event_manager) # -- recorder manager - self.recorder_manager = self._manager_call_switch.resolve_manager_class("RecorderManager")( - self.cfg.recorders, self - ) + self.recorder_manager = RecorderManager(self.cfg.recorders, self) + self._has_recorders = bool(self.recorder_manager.active_terms) print("[INFO] Recorder Manager: ", self.recorder_manager) # -- action manager - self.action_manager = self._manager_call_switch.resolve_manager_class("ActionManager")(self.cfg.actions, self) + self.action_manager = ActionManager(self.cfg.actions, self) + # Manager-dependent persistent buffer: reuse the storage across manager + # reloads when the action dimension is unchanged so captured graphs keep + # reading the same pointer. + action_shape = (self.num_envs, self.action_manager.total_action_dim) + if getattr(self, "_action_in_wp", None) is None or tuple(self._action_in_wp.shape) != action_shape: + self._action_in_wp = wp.zeros(action_shape, dtype=wp.float32, device=self.device) + else: + self._action_in_wp.zero_() print("[INFO] Action Manager: ", self.action_manager) # -- observation manager - self.observation_manager = self._manager_call_switch.resolve_manager_class("ObservationManager")( - self.cfg.observations, self - ) + self.observation_manager = ObservationManager(self.cfg.observations, self) print("[INFO] Observation Manager:", self.observation_manager) # perform events at the start of the simulation @@ -396,11 +411,15 @@ def setup_manager_visualizers(self): """ def reset( - self, seed: int | None = None, env_ids: Sequence[int] | None = None, options: dict[str, Any] | None = None + self, + seed: int | None = None, + env_ids: Sequence[int] | None = None, + options: dict[str, Any] | None = None, + env_mask: wp.array | torch.Tensor | None = None, ) -> tuple[VecEnvObs, dict]: """Resets the specified environments and returns observations. - This function calls the :meth:`_reset_idx` function to reset the specified environments. + This function calls the :meth:`_reset_mask` function to reset the specified environments. However, certain operations, such as procedural terrain generation, that happened during initialization are not repeated. @@ -412,14 +431,19 @@ def reset( Note: This argument is used for compatibility with Gymnasium environment definition. + env_mask: Boolean environment mask. This is the canonical Warp + frontend selection; :paramref:`env_ids` remains supported for + API compatibility. + Returns: A tuple containing the observations and extras. """ - if env_ids is None: - env_ids = torch.arange(self.num_envs, dtype=torch.int64, device=self.device) - - # trigger recorder terms for pre-reset calls - self.recorder_manager.record_pre_reset(env_ids) + reset_mask = self.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + if self._has_recorders: + recorder_env_ids = env_ids + if recorder_env_ids is None or isinstance(recorder_env_ids, wp.array): + recorder_env_ids = wp.to_torch(reset_mask).nonzero(as_tuple=False).squeeze(-1) + self.recorder_manager.record_pre_reset(recorder_env_ids) # set the seed if seed is not None: @@ -434,8 +458,9 @@ def reset( device=self.device, ) - # reset state of scene - self._reset_idx(env_ids) + self._reset_mask(env_mask=reset_mask) + if self._has_recorders: + self.extras["log"].update(self.recorder_manager.reset(recorder_env_ids)) # update articulation kinematics self.scene.write_data_to_sim() @@ -445,11 +470,17 @@ def reset( for _ in range(self.cfg.num_rerenders_on_reset): self.sim.render() - # trigger recorder terms for post-reset calls - self.recorder_manager.record_post_reset(env_ids) + if self._has_recorders: + self.recorder_manager.record_post_reset(recorder_env_ids) # compute observations - self.obs_buf = self.observation_manager.compute(update_history=True) + self.obs_buf = self._warp_graph_cache.call( + "ObservationManager_compute_reset", + self.observation_manager.compute, + update_history=True, + return_cloned_output=False, + output=clone_obs_buffer, + ) # return observations return self.obs_buf, self.extras @@ -460,6 +491,7 @@ def reset_to( env_ids: Sequence[int] | None, seed: int | None = None, is_relative: bool = False, + env_mask: wp.array | None = None, ): """Resets specified environments to provided states. @@ -477,19 +509,27 @@ def reset_to( seed: The seed to use for randomization. Defaults to None, in which case the seed is not set. is_relative: If set to True, the state is considered relative to the environment origins. Defaults to False. + env_mask: Boolean Warp mask selecting environments. Takes precedence over + :paramref:`env_ids`. State setting and recorders are host consumers, so + compact IDs are still materialized once inside this method. """ # reset all envs in the scene if env_ids is None - if env_ids is None: + if env_mask is not None: + env_ids = wp.to_torch(env_mask).nonzero(as_tuple=False).squeeze(-1) + elif env_ids is None: env_ids = torch.arange(self.num_envs, dtype=torch.int64, device=self.device) - # trigger recorder terms for pre-reset calls - self.recorder_manager.record_pre_reset(env_ids) + if self._has_recorders: + self.recorder_manager.record_pre_reset(env_ids) # set the seed if seed is not None: self.seed(seed) - self._reset_idx(env_ids) + env_mask = self.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + self._reset_mask(env_mask=env_mask) + if self._has_recorders: + self.extras["log"].update(self.recorder_manager.reset(env_ids)) # set the state self.scene.reset_to(state, env_ids, is_relative=is_relative) @@ -502,11 +542,17 @@ def reset_to( for _ in range(self.cfg.num_rerenders_on_reset): self.sim.render() - # trigger recorder terms for post-reset calls - self.recorder_manager.record_post_reset(env_ids) + if self._has_recorders: + self.recorder_manager.record_post_reset(env_ids) # compute observations - self.obs_buf = self.observation_manager.compute(update_history=True) + self.obs_buf = self._warp_graph_cache.call( + "ObservationManager_compute_reset", + self.observation_manager.compute, + update_history=True, + return_cloned_output=False, + output=clone_obs_buffer, + ) # return observations return self.obs_buf, self.extras @@ -527,15 +573,16 @@ def step(self, action: torch.Tensor) -> tuple[VecEnvObs, dict]: A tuple containing the observations and extras. """ # process actions - action_device = action.to(self.device) - if action_device.dtype != torch.float32: - action_device = action_device.float() - if not action_device.is_contiguous(): - action_device = action_device.contiguous() - action_wp = wp.from_torch(action_device, dtype=wp.float32) - self.action_manager.process_action(action_wp) + action_device = action.to(device=self.device, dtype=torch.float32).contiguous() + wp.copy(self._action_in_wp, wp.from_torch(action_device, dtype=wp.float32)) + self._warp_graph_cache.call( + "ActionManager_process_action", + self.action_manager.process_action, + action=self._action_in_wp, + ) - self.recorder_manager.record_pre_step() + if self._has_recorders: + self.recorder_manager.record_pre_step() # check if we need to do rendering within the physics loop # note: hoisted out of the decimation loop; is_rendering does live settings lookups @@ -545,7 +592,7 @@ def step(self, action: torch.Tensor) -> tuple[VecEnvObs, dict]: for _ in range(self.cfg.decimation): self._sim_step_counter += 1 # set actions into buffers - self.action_manager.apply_action() + self._warp_graph_cache.call("ActionManager_apply_action", self.action_manager.apply_action) # set actions into simulator self.scene.write_data_to_sim() # simulate @@ -560,11 +607,23 @@ def step(self, action: torch.Tensor) -> tuple[VecEnvObs, dict]: # post-step: step interval event if "interval" in self.event_manager.available_modes: - self.event_manager.apply(mode="interval", dt=self.step_dt) + self._warp_graph_cache.call( + "EventManager_apply_interval", + self.event_manager.apply, + mode="interval", + dt=self.step_dt, + ) # -- compute observations - self.obs_buf = self.observation_manager.compute(update_history=True) - self.recorder_manager.record_post_step() + self.obs_buf = self._warp_graph_cache.call( + "ObservationManager_compute_update_history", + self.observation_manager.compute, + update_history=True, + return_cloned_output=False, + output=clone_obs_buffer, + ) + if self._has_recorders: + self.recorder_manager.record_post_step() # return observations and extras return self.obs_buf, self.extras @@ -592,6 +651,7 @@ def seed(seed: int = -1) -> int: def close(self): """Cleanup for the environment.""" if not self._is_closed: + self.invalidate_wp_graphs() # destructor is order-sensitive del self.viewport_camera_controller del self.action_manager @@ -609,131 +669,61 @@ def close(self): # update closing status self._is_closed = True - """ - Helper functions. - """ - - def _resolve_stable_cfg_counterpart(self) -> ManagerBasedEnvCfg | None: - """Resolve a stable task config counterpart for the current experimental task config. + def invalidate_wp_graphs(self) -> None: + """Invalidate cached Warp graphs and recorded launches. - The lookup follows a module-name mirror convention: - ``isaaclab_tasks_experimental...`` -> ``isaaclab_tasks...`` with the same config class name. + Call this after changing launch topology, static scalar arguments, or + persistent array storage. Device work is drained before retained + argument owners are released. """ - cfg_cls = self.cfg.__class__ - cfg_module_name = cfg_cls.__module__ - if "isaaclab_tasks_experimental" not in cfg_module_name: - return None + self._warp_graph_cache.invalidate() + self._warp_launch.reset() - stable_module_name = cfg_module_name.replace("isaaclab_tasks_experimental", "isaaclab_tasks", 1) - try: - stable_module = importlib.import_module(stable_module_name) - except Exception as exc: - logger.warning( - "Failed to import stable task cfg module '%s' for manager_call_config stable mode: %s", - stable_module_name, - exc, - ) - return None - - stable_cfg_cls = getattr(stable_module, cfg_cls.__name__, None) - if stable_cfg_cls is None: - logger.warning( - "Stable task cfg class '%s' not found in module '%s'.", - cfg_cls.__name__, - stable_module_name, - ) - return None - - try: - return stable_cfg_cls() - except Exception as exc: - logger.warning( - "Failed to instantiate stable task cfg '%s.%s': %s", - stable_module_name, - cfg_cls.__name__, - exc, - ) - return None - - def _apply_manager_term_cfg_profile(self) -> None: - """Align term configs with manager modes for stable manager selections. - - When a manager is configured as STABLE (0), swap its corresponding config subtree - from the stable task counterpart to keep manager-term type/signature compatibility. - """ - manager_to_cfg_attr = { - "ActionManager": "actions", - "ObservationManager": "observations", - "EventManager": "events", - "RecorderManager": "recorders", - "CommandManager": "commands", - "TerminationManager": "terminations", - "RewardManager": "rewards", - "CurriculumManager": "curriculum", - } - - stable_manager_names = [ - manager_name - for manager_name in manager_to_cfg_attr - if self._manager_call_switch.get_mode_for_manager(manager_name) == ManagerCallMode.STABLE - ] - if not stable_manager_names: - return - - stable_cfg = self._resolve_stable_cfg_counterpart() - if stable_cfg is None: - logger.warning( - "Stable managers requested (%s), but no stable cfg counterpart could be resolved." - " Keeping experimental term configs.", - ", ".join(stable_manager_names), - ) - return - - replaced_items: list[str] = [] - for manager_name, cfg_attr in manager_to_cfg_attr.items(): - if self._manager_call_switch.get_mode_for_manager(manager_name) != ManagerCallMode.STABLE: - continue - if not hasattr(self.cfg, cfg_attr) or not hasattr(stable_cfg, cfg_attr): - continue - setattr(self.cfg, cfg_attr, deepcopy(getattr(stable_cfg, cfg_attr))) - replaced_items.append(f"{manager_name} -> cfg.{cfg_attr}") - - if replaced_items: - print("[INFO] Applied stable term config profile for managers:") - for item in replaced_items: - print(f" - {item}") + """ + Helper functions. + """ - def _reset_idx(self, env_ids: Sequence[int]): - """Reset environments based on specified indices. + def _reset_mask( + self, + *, + env_mask: wp.array(dtype=wp.bool), + ) -> None: + """Reset Warp-owned state for selected environments. Args: - env_ids: List of environment ids which must be reset + env_mask: Boolean Warp mask selecting environments to reset. """ + if env_mask is not self.reset_mask_wp: + wp.copy(self.reset_mask_wp, env_mask) + env_mask = self.reset_mask_wp + # reset the internal buffers of the scene elements - self.scene.reset(env_ids) + self.scene.reset(env_mask=env_mask) # apply events such as randomization for environments that need a reset if "reset" in self.event_manager.available_modes: env_step_count = self._sim_step_counter // self.cfg.decimation self._global_env_step_count_wp.fill_(env_step_count) - self.event_manager.apply( - mode="reset", env_ids=env_ids, global_env_step_count=self._global_env_step_count_wp + self._warp_graph_cache.call( + "EventManager_apply_reset", + self.event_manager.apply, + mode="reset", + env_mask_wp=env_mask, + global_env_step_count=self._global_env_step_count_wp, ) # iterate over all managers and reset them # this returns a dictionary of information which is stored in the extras # note: This is order-sensitive! Certain things need be reset before others. self.extras["log"] = dict() - env_mask = self.resolve_env_mask(env_ids=env_ids) # -- observation manager - info = self.observation_manager.reset(env_mask=env_mask) + info = self._warp_graph_cache.call( + "ObservationManager_reset", self.observation_manager.reset, env_mask=env_mask + ) self.extras["log"].update(info) # -- action manager - info = self.action_manager.reset(env_mask=env_mask) + info = self._warp_graph_cache.call("ActionManager_reset", self.action_manager.reset, env_mask=env_mask) self.extras["log"].update(info) # -- event manager - info = self.event_manager.reset(env_mask=env_mask) - self.extras["log"].update(info) - # -- recorder manager - info = self.recorder_manager.reset(env_ids) + info = self._warp_graph_cache.call("EventManager_reset", self.event_manager.reset, env_mask=env_mask) self.extras["log"].update(info) diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_rl_env_warp.py b/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_rl_env_warp.py index 2e1894e54003..b83be4e7d39d 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_rl_env_warp.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/manager_based_rl_env_warp.py @@ -15,7 +15,6 @@ import math import os -from collections.abc import Sequence from typing import Any, ClassVar import gymnasium as gym @@ -25,12 +24,12 @@ from isaaclab.envs.common import VecEnvStepReturn from isaaclab.envs.manager_based_rl_env_cfg import ManagerBasedRLEnvCfg -from isaaclab.managers import CommandManager from isaaclab.ui.widgets import ManagerLiveVisualizer from isaaclab.utils.timer import Timer -from isaaclab_experimental.utils.manager_call_switch import ManagerCallMode +from isaaclab_experimental.managers import CommandManager, CurriculumManager, RewardManager, TerminationManager from isaaclab_experimental.utils.torch_utils import clone_obs_buffer +from isaaclab_experimental.utils.warp import any_env_set, increment_all_int64, zero_masked_int64 from .manager_based_env_warp import ManagerBasedEnvWarp @@ -93,6 +92,14 @@ def __init__(self, cfg: ManagerBasedRLEnvCfg, render_mode: str | None = None, ** render_mode: The render mode for the environment. Defaults to None, which is similar to ``"human"``. """ + # Adapt the cfg for the warp managers (Newton physics check, SceneEntityCfg + # promotion, MDP twin swap). Idempotent: a warp-native cfg passes through + # unchanged, and a stable-derived cfg (``--frontend=warp`` or a registered + # warp task variant subclassing a stable cfg) is adapted in place. + from isaaclab_experimental.envs.frontend import WarpFrontend + + WarpFrontend.adapt_cfg(cfg) + # -- counter for curriculum self.common_step_counter = 0 @@ -106,16 +113,6 @@ def __init__(self, cfg: ManagerBasedRLEnvCfg, render_mode: str | None = None, ** # store the render mode self.render_mode = render_mode - # The persistent reset mask needed for warp capture - # The intended use is to copy into this mask whenever capture is needed - # TODO: termination manager provides the same mask, investigate whether this can be replaced. - self.reset_mask_wp = wp.zeros(cfg.scene.num_envs, dtype=wp.bool, device=cfg.sim.device) - - # Persistent action input buffer to keep pointer stable for captured graphs. - self._action_in_wp: wp.array = wp.zeros( - (self.num_envs, self.action_manager.total_action_dim), dtype=wp.float32, device=self.device - ) - # initialize data and constants # -- set the framerate of the gym video recorder wrapper so that the playback speed # of the produced video matches the simulation @@ -155,7 +152,7 @@ def max_episode_length(self) -> int: def load_managers(self): # note: this order is important since observation manager needs to know the command and action managers # and the reward manager needs to know the termination manager - # -- command manager (stable impl — not routed through ManagerCallSwitch) + # -- Warp-first command manager self.command_manager = CommandManager(self.cfg.commands, self) print("[INFO] Command Manager: ", self.command_manager) @@ -164,17 +161,13 @@ def load_managers(self): # prepare the managers # -- termination manager - self.termination_manager = self._manager_call_switch.resolve_manager_class("TerminationManager")( - self.cfg.terminations, self - ) + self.termination_manager = TerminationManager(self.cfg.terminations, self) print("[INFO] Termination Manager: ", self.termination_manager) - # -- reward manager (experimental fork; Warp-compatible rewards) - self.reward_manager = self._manager_call_switch.resolve_manager_class("RewardManager")(self.cfg.rewards, self) + # -- reward manager + self.reward_manager = RewardManager(self.cfg.rewards, self) print("[INFO] Reward Manager: ", self.reward_manager) - # -- curriculum manager - self.curriculum_manager = self._manager_call_switch.resolve_manager_class("CurriculumManager")( - self.cfg.curriculum, self - ) + # -- Warp-first curriculum manager + self.curriculum_manager = CurriculumManager(self.cfg.curriculum, self) print("[INFO] Curriculum Manager: ", self.curriculum_manager) # setup the action and observation spaces for Gym @@ -200,13 +193,6 @@ def setup_manager_visualizers(self): Operations - MDP """ - def invalidate_wp_graphs(self) -> None: - """Invalidate all cached Warp graphs. - - Call this if the captured launch topology changes (e.g. different term list, shapes, etc.). - """ - self._manager_call_switch.invalidate_graphs() - def step_warp_termination_compute(self) -> None: """Captured stage: compute terminations (env-step frequency).""" self.reset_buf = self.termination_manager.compute() @@ -238,18 +224,18 @@ def step(self, action: torch.Tensor) -> VecEnvStepReturn: # IMPORTANT: Do NOT re-wrap/replace the `wp.array` used by captured graphs each step. # Instead, copy the latest actions into the persistent buffer. with Timer(name="action_preprocess", msg="Action preprocessing took:", enable=DEBUG_TIMER_STEP, time_unit="us"): - if self._action_in_wp is None: - raise RuntimeError("Action buffer not initialized. Call reset() before step().") - action_device = action.to(self.device) + action_device = action.to(device=self.device, dtype=torch.float32).contiguous() wp.copy(self._action_in_wp, wp.from_torch(action_device, dtype=wp.float32)) - self._manager_call_switch.call_stage( - stage="ActionManager_process_action", - warp_call={"fn": self.action_manager.process_action, "kwargs": {"action": self._action_in_wp}}, + self._warp_graph_cache.call( + "ActionManager_process_action", + self.action_manager.process_action, + action=self._action_in_wp, timer=DEBUG_TIMER_STEP, ) - self.recorder_manager.record_pre_step() + if self._has_recorders: + self.recorder_manager.record_pre_step() # check if we need to do rendering within the physics loop # note: checked here once to avoid multiple checks within the loop @@ -259,21 +245,24 @@ def step(self, action: torch.Tensor) -> VecEnvStepReturn: for _ in range(self.cfg.decimation): self._sim_step_counter += 1 # set actions into buffers - self._manager_call_switch.call_stage( - stage="ActionManager_apply_action", - warp_call={"fn": self.action_manager.apply_action}, - timer=DEBUG_TIMER_STEP, - ) - self._manager_call_switch.call_stage( - stage="Scene_write_data_to_sim", - warp_call={"fn": self.scene.write_data_to_sim}, + self._warp_graph_cache.call( + "ActionManager_apply_action", + self.action_manager.apply_action, timer=DEBUG_TIMER_STEP, ) + with Timer( + name="Scene_write_data_to_sim", + msg="Scene write took:", + enable=DEBUG_TIMER_STEP, + time_unit="us", + ): + self.scene.write_data_to_sim() # simulate with Timer(name="simulate", msg="Newton simulation step took:", enable=DEBUG_TIMER_STEP, time_unit="us"): self.sim.step(render=False) - self.recorder_manager.record_post_physics_decimation_step() + if self._has_recorders: + self.recorder_manager.record_post_physics_decimation_step() # render between steps only if the GUI or an RTX sensor needs it # note: we assume the render interval to be the shortest accepted rendering interval. # If a camera needs rendering at a faster frequency, this will lead to unexpected behavior. @@ -290,89 +279,65 @@ def step(self, action: torch.Tensor) -> VecEnvStepReturn: # post-step: # -- update env counters (used for curriculum generation) - self.episode_length_buf += 1 # step in current episode (per env) + self._warp_launch.launch( + increment_all_int64, + dim=self.num_envs, + inputs=[self._episode_length_buf_wp, 1], + site=("manager_based_rl_env", "episode_length_increment"), + ) self.common_step_counter += 1 # total step (common for all envs) # -- post-processing (termination + reward) as independently configurable stages - self._manager_call_switch.call_stage( - stage="TerminationManager_compute", - warp_call={"fn": self.step_warp_termination_compute}, + self._warp_graph_cache.call( + "TerminationManager_compute", + self.step_warp_termination_compute, timer=DEBUG_TIMER_STEP, ) - self.reward_buf = self._manager_call_switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": self.reward_manager.compute, "kwargs": {"dt": float(self.step_dt)}}, + self.reward_buf = self._warp_graph_cache.call( + "RewardManager_compute", + self.reward_manager.compute, + dt=self.step_dt, timer=DEBUG_TIMER_STEP, ) - if len(self.recorder_manager.active_terms) > 0: + if self._has_recorders: # update observations for recording if needed - self._manager_call_switch.call_stage( - stage="ObservationManager_compute_no_history", - warp_call={"fn": self.observation_manager.compute, "kwargs": {"return_cloned_output": False}}, + self._warp_graph_cache.call( + "ObservationManager_compute_no_history", + self.observation_manager.compute, + return_cloned_output=False, timer=DEBUG_TIMER_STEP, ) self.recorder_manager.record_post_step() - # -- reset envs that terminated/timed-out and log the episode information - # NOTE: Interim path (intentional). - # We still compact `reset_buf` into `env_ids` here because several reset-time managers/recorders - # are still `env_ids`-based. Do NOT remove/replace this until mask-based reset is end-to-end. - with Timer( - name="reset_selection", - msg="Reset selection took:", - enable=DEBUG_TIMER_STEP, - time_unit="us", - ): - # Keep the reset-mask handoff fully in Warp when experimental termination buffers exist. - # Stable termination manager path exposes torch-only dones/reset buffers. - termination_manager_mode = self._manager_call_switch.get_mode_for_manager("TerminationManager") - if termination_manager_mode == ManagerCallMode.STABLE: - # copy still needed as mask will be used if manager is set to mode > 0 - wp.copy(self.reset_mask_wp, wp.from_torch(self.reset_buf, dtype=wp.bool)) - else: - wp.copy(self.reset_mask_wp, self.termination_manager.dones_wp) - reset_env_ids = self.reset_buf.nonzero(as_tuple=False).squeeze(-1) - if len(reset_env_ids) > 0: - # trigger recorder terms for pre-reset calls - self.recorder_manager.record_pre_reset(reset_env_ids) - - with Timer( - name="reset_idx", - msg="Reset idx took:", - enable=DEBUG_TIMER_STEP, - time_unit="us", - ): - self._reset_idx(env_ids=reset_env_ids, env_mask=self.reset_mask_wp) - - # if sensors are added to the scene, make sure we render to reflect changes in reset - if self.has_rtx_sensors and self.cfg.num_rerenders_on_reset > 0: - for _ in range(self.cfg.num_rerenders_on_reset): - self.sim.render() - - # trigger recorder terms for post-reset calls - self.recorder_manager.record_post_reset(reset_env_ids) + self._reset_terminated_envs() # -- update command - self.command_manager.compute(dt=float(self.step_dt)) + self._warp_graph_cache.call( + "CommandManager_compute", + self.command_manager.compute, + dt=self.step_dt, + timer=DEBUG_TIMER_STEP, + ) # -- step interval events if "interval" in self.event_manager.available_modes: - self._manager_call_switch.call_stage( - stage="EventManager_apply_interval", - warp_call={"fn": self.event_manager.apply, "kwargs": {"mode": "interval", "dt": float(self.step_dt)}}, + self._warp_graph_cache.call( + "EventManager_apply_interval", + self.event_manager.apply, + mode="interval", + dt=self.step_dt, timer=DEBUG_TIMER_STEP, ) # -- compute observations # note: done after reset to get the correct observations for reset envs - self.obs_buf = self._manager_call_switch.call_stage( - stage="ObservationManager_compute_update_history", - warp_call={ - "fn": self.observation_manager.compute, - "kwargs": {"update_history": True, "return_cloned_output": False}, - "output": lambda r: clone_obs_buffer(r), - }, + self.obs_buf = self._warp_graph_cache.call( + "ObservationManager_compute_update_history", + self.observation_manager.compute, + update_history=True, + return_cloned_output=False, + output=clone_obs_buffer, timer=DEBUG_TIMER_STEP, ) # return observations, rewards, resets and extras @@ -408,10 +373,8 @@ def render(self, recompute: bool = False) -> np.ndarray | None: if self.render_mode == "human" or self.render_mode is None: return None elif self.render_mode == "rgb_array": - # check that if any render could have happened - has_gui = bool(self.sim.get_setting("/isaaclab/has_gui")) - offscreen_render = bool(self.sim.get_setting("/isaaclab/render/offscreen")) - if not (has_gui or offscreen_render): + # rendering requires a GUI or offscreen rendering (mirrors the stable env) + if not (self.sim.has_gui or self.sim.has_offscreen_render): raise RuntimeError( f"Cannot render '{self.render_mode}' when the simulation render mode does not support" " rendering. Please set the simulation render mode to 'PARTIAL_RENDERING' or" @@ -445,6 +408,7 @@ def render(self, recompute: bool = False) -> np.ndarray | None: def close(self): if not self._is_closed: + self.invalidate_wp_graphs() # destructor is order-sensitive del self.command_manager del self.reward_manager @@ -485,68 +449,49 @@ def _configure_gym_env_spaces(self): self.observation_space = gym.vector.utils.batch_space(self.single_observation_space, self.num_envs) self.action_space = gym.vector.utils.batch_space(self.single_action_space, self.num_envs) - def _reset_idx( + def _reset_mask( self, - env_ids: Sequence[int] | slice | torch.Tensor, *, - env_mask: wp.array | None = None, - ): - """Reset environments based on specified indices. - - IMPORTANT: - This function always uses the **TerminationManager-produced Warp env mask** (`self.reset_buf`) to select - which envs to reset. The ids/mask conversion is performed in `step()` before calling this function. - - In other words: - - If `env_mask` is provided, it **must** be `self.reset_buf` (Warp bool mask) - - If `env_mask` is not provided, this function will populate `self.reset_buf` from `env_ids` - - When `env_mask` is provided, `env_ids` **must** correspond to the same mask + env_mask: wp.array(dtype=wp.bool), + env_ids: torch.Tensor | None = None, + ) -> None: + """Reset Warp-owned RL state for selected environments. Args: - env_ids: Environment indices to reset. - env_mask: Warp boolean env mask selecting envs to reset. Must be `self.reset_buf`. - If None, uses and populates `self.reset_buf` from `env_ids`. + env_mask: Boolean Warp mask selecting environments to reset. + env_ids: Compact environment IDs matching :paramref:`env_mask`, when a + host consumer (e.g. the recorder boundary) already materialized them. """ - if env_mask is None: - # Base `reset()` / `reset_to()` call-path provides only `env_ids`. - # Populate the stable TerminationManager-owned mask (`self.reset_buf`) from ids. - env_mask = self.reset_mask_wp - # Use the centralized env-id/mask resolution from the base Warp env, then copy into the - # stable TerminationManager-owned buffer (`self.reset_buf`) used by captured graphs. - resolved_mask = self.resolve_env_mask(env_ids=env_ids) - wp.copy(env_mask, resolved_mask) - - if not isinstance(env_mask, wp.array): - raise TypeError(f"env_mask must be a wp.array (got {type(env_mask)}).") - - # update the curriculum for environments that need a reset - with Timer( - name="curriculum_manager.compute_reset", - msg="CurriculumManager.compute (reset) took:", - enable=DEBUG_TIMER_RESET, - time_unit="us", - ): - self.curriculum_manager.compute(env_ids=env_ids) - - # reset the internal buffers of the scene elements - self._manager_call_switch.call_stage( - stage="Scene_reset", - warp_call={"fn": self.scene.reset, "kwargs": {"env_ids": env_ids, "env_mask": env_mask}}, + if env_mask is not self.reset_mask_wp: + wp.copy(self.reset_mask_wp, env_mask) + env_mask = self.reset_mask_wp + + # Legacy curriculum terms consume compact IDs. Materialize them at most + # once per reset and share the recorder boundary's IDs when available. + curriculum_kwargs: dict[str, Any] = {"env_mask": env_mask} + if self.curriculum_manager.requires_host_ids: + if env_ids is None: + env_ids = wp.to_torch(env_mask).nonzero(as_tuple=False).squeeze(-1) + curriculum_kwargs["env_ids"] = env_ids + + self._warp_graph_cache.call( + "CurriculumManager_compute", + self.curriculum_manager.compute, timer=DEBUG_TIMER_RESET, + **curriculum_kwargs, ) + with Timer(name="Scene_reset", msg="Scene reset took:", enable=DEBUG_TIMER_RESET, time_unit="us"): + self.scene.reset(env_mask=env_mask) + if "reset" in self.event_manager.available_modes: self._global_env_step_count_wp.fill_(self._sim_step_counter // self.cfg.decimation) - self._manager_call_switch.call_stage( - stage="EventManager_apply_reset", - warp_call={ - "fn": self.event_manager.apply, - "kwargs": { - "mode": "reset", - "env_mask_wp": env_mask, - "global_env_step_count": self._global_env_step_count_wp, - }, - }, + self._warp_graph_cache.call( + "EventManager_apply_reset", + self.event_manager.apply, + mode="reset", + env_mask_wp=env_mask, + global_env_step_count=self._global_env_step_count_wp, timer=DEBUG_TIMER_RESET, ) @@ -554,51 +499,58 @@ def _reset_idx( # this returns a dictionary of information which is stored in the extras # note: This is order-sensitive! Certain things need be reset before others. # -- observation manager + action + reward managers - obs_info = self._manager_call_switch.call_stage( - stage="ObservationManager_reset", - warp_call={"fn": self.observation_manager.reset, "kwargs": {"env_mask": env_mask}}, + obs_info = self._warp_graph_cache.call( + "ObservationManager_reset", + self.observation_manager.reset, + env_mask=env_mask, timer=DEBUG_TIMER_RESET, ) - action_info = self._manager_call_switch.call_stage( - stage="ActionManager_reset", - warp_call={"fn": self.action_manager.reset, "kwargs": {"env_mask": env_mask}}, + action_info = self._warp_graph_cache.call( + "ActionManager_reset", + self.action_manager.reset, + env_mask=env_mask, timer=DEBUG_TIMER_RESET, ) - reward_info = self._manager_call_switch.call_stage( - stage="RewardManager_reset", - warp_call={"fn": self.reward_manager.reset, "kwargs": {"env_mask": env_mask}}, + reward_info = self._warp_graph_cache.call( + "RewardManager_reset", + self.reward_manager.reset, + env_mask=env_mask, timer=DEBUG_TIMER_RESET, ) - - # -- curriculum manager - with Timer( - name="curriculum_manager.reset", - msg="CurriculumManager.reset took:", - enable=DEBUG_TIMER_RESET, - time_unit="us", - ): - curriculum_info = self.curriculum_manager.reset(env_ids=env_ids) + curriculum_info = self._warp_graph_cache.call( + "CurriculumManager_reset", + self.curriculum_manager.reset, + timer=DEBUG_TIMER_RESET, + **curriculum_kwargs, + ) # -- command + event + termination managers - command_info = self.command_manager.reset(env_ids=env_ids) - event_info = self._manager_call_switch.call_stage( - stage="EventManager_reset", - warp_call={"fn": self.event_manager.reset, "kwargs": {"env_mask": env_mask}}, - stable_call={"fn": self.event_manager.reset, "kwargs": {"env_ids": env_ids}}, + command_info = self._warp_graph_cache.call( + "CommandManager_reset", + self.command_manager.reset, + env_mask=env_mask, timer=DEBUG_TIMER_RESET, ) - termination_info = self._manager_call_switch.call_stage( - stage="TerminationManager_reset", - warp_call={"fn": self.termination_manager.reset, "kwargs": {"env_mask": env_mask}}, - stable_call={"fn": self.termination_manager.reset, "kwargs": {"env_ids": env_ids}}, + event_info = self._warp_graph_cache.call( + "EventManager_reset", + self.event_manager.reset, + env_mask=env_mask, + timer=DEBUG_TIMER_RESET, + ) + termination_info = self._warp_graph_cache.call( + "TerminationManager_reset", + self.termination_manager.reset, + env_mask=env_mask, timer=DEBUG_TIMER_RESET, ) - - # -- recorder manager - recorder_info = self.recorder_manager.reset(env_ids=env_ids) # reset the episode length buffer - self.episode_length_buf[env_ids] = 0 + self._warp_launch.launch( + zero_masked_int64, + dim=self.num_envs, + inputs=[env_mask, self._episode_length_buf_wp], + site=("manager_based_rl_env", "episode_length_reset", env_mask), + ) # aggregate logging info log: dict[str, Any] = {} @@ -610,7 +562,45 @@ def _reset_idx( command_info, event_info, termination_info, - recorder_info, ): log.update(info) self.extras["log"] = log + + def _reset_terminated_envs(self) -> None: + """Reset terminated environments using the canonical Warp mask.""" + reset_mask = self.termination_manager.dones_wp + # Keep the mask as the canonical selection, but use one host predicate + # to avoid dispatching the complete reset pipeline when it is empty. + if not any_env_set(self.reset_buf): + return + + # Same-step autoreset exposes terminal observations before any selected + # environment is reset, matching the stable manager-based environment. + if self.cfg.compute_final_obs: + self.extras["final_obs"] = self.observation_manager.compute() + + recorder_env_ids = None + if self._has_recorders: + with Timer( + name="reset_selection_host", + msg="Recorder reset selection took:", + enable=DEBUG_TIMER_STEP, + time_unit="us", + ): + recorder_env_ids = self.reset_buf.nonzero(as_tuple=False).squeeze(-1) + self.recorder_manager.record_pre_reset(recorder_env_ids) + + with Timer( + name="reset_mask", + msg="Reset mask took:", + enable=DEBUG_TIMER_STEP, + time_unit="us", + ): + self._reset_mask(env_mask=reset_mask, env_ids=recorder_env_ids) + + if self._has_recorders: + self.extras["log"].update(self.recorder_manager.reset(recorder_env_ids)) + self.recorder_manager.record_post_reset(recorder_env_ids) + if self.has_rtx_sensors and self.cfg.num_rerenders_on_reset > 0: + for _ in range(self.cfg.num_rerenders_on_reset): + self.sim.render() diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/__init__.pyi b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/__init__.pyi index a6dadcfce5bc..9459feae7a18 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/__init__.pyi +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/__init__.pyi @@ -19,6 +19,14 @@ from .actions import ( # noqa: F401 JointPositionAction, JointPositionActionCfg, ) +from .commands import ( # noqa: F401 + NullCommand, + NullCommandCfg, + UniformPoseCommand, + UniformPoseCommandCfg, + UniformVelocityCommand, + UniformVelocityCommandCfg, +) # Override stable terms with experimental Warp-first implementations. These leaf # modules are import-clean (no eager backend imports), so re-exporting them here diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/actions/joint_actions.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/actions/joint_actions.py index f379f66c1900..8abc477a6b06 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/actions/joint_actions.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/actions/joint_actions.py @@ -231,8 +231,8 @@ def IO_descriptor(self) -> GenericActionIODescriptor: """ def process_actions(self, actions: wp.array, action_offset: int = 0): - wp.launch( - kernel=_process_joint_actions_kernel, + self._env._warp_launch.launch( + _process_joint_actions_kernel, dim=(self.num_envs, self.action_dim), inputs=[ actions, @@ -243,7 +243,7 @@ def process_actions(self, actions: wp.array, action_offset: int = 0): self._raw_actions, self._processed_actions, ], - device=self.device, + site=(self, int(action_offset)), ) def reset(self, env_mask: wp.array | None = None) -> None: @@ -251,11 +251,11 @@ def reset(self, env_mask: wp.array | None = None) -> None: if env_mask is None: self._raw_actions.fill_(0.0) return - wp.launch( - kernel=zero_masked_2d, + self._env._warp_launch.launch( + zero_masked_2d, dim=(self.num_envs, self.action_dim), inputs=[env_mask, self._raw_actions], - device=self.device, + site=(self, env_mask), ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/__init__.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/__init__.py similarity index 67% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/__init__.py rename to source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/__init__.py index 3e2b7945ebde..57c6a813798d 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/__init__.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/__init__.py @@ -3,8 +3,8 @@ # # SPDX-License-Identifier: BSD-3-Clause -""" -Direct workflow environments. -""" +"""Warp-native command terms.""" -import gymnasium as gym +from isaaclab.utils.module import lazy_export + +lazy_export() diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/__init__.pyi b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/__init__.pyi new file mode 100644 index 000000000000..dcc7b0b99b82 --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/__init__.pyi @@ -0,0 +1,18 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +__all__ = [ + "NullCommandCfg", + "UniformPoseCommandCfg", + "UniformVelocityCommandCfg", + "NullCommand", + "UniformPoseCommand", + "UniformVelocityCommand", +] + +from .commands_cfg import NullCommandCfg, UniformPoseCommandCfg, UniformVelocityCommandCfg +from .null_command import NullCommand +from .pose_command import UniformPoseCommand +from .velocity_command import UniformVelocityCommand diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/commands_cfg.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/commands_cfg.py new file mode 100644 index 000000000000..483f62cbd65c --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/commands_cfg.py @@ -0,0 +1,41 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Configuration classes for Warp-native command terms.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from isaaclab.envs.mdp.commands.commands_cfg import NullCommandCfg as _NullCommandCfg +from isaaclab.envs.mdp.commands.commands_cfg import UniformPoseCommandCfg as _UniformPoseCommandCfg +from isaaclab.envs.mdp.commands.commands_cfg import UniformVelocityCommandCfg as _UniformVelocityCommandCfg +from isaaclab.utils.configclass import configclass + +if TYPE_CHECKING: + from .null_command import NullCommand + from .pose_command import UniformPoseCommand + from .velocity_command import UniformVelocityCommand + + +@configclass +class NullCommandCfg(_NullCommandCfg): + """Configuration for the Warp-native null command generator.""" + + class_type: type[NullCommand] | str = "{DIR}.null_command:NullCommand" + + +@configclass +class UniformVelocityCommandCfg(_UniformVelocityCommandCfg): + """Configuration for the Warp-native uniform velocity command generator.""" + + class_type: type[UniformVelocityCommand] | str = "{DIR}.velocity_command:UniformVelocityCommand" + + +@configclass +class UniformPoseCommandCfg(_UniformPoseCommandCfg): + """Configuration for the Warp-native uniform pose command generator.""" + + class_type: type[UniformPoseCommand] | str = "{DIR}.pose_command:UniformPoseCommand" diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/null_command.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/null_command.py new file mode 100644 index 000000000000..b210daeefb55 --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/null_command.py @@ -0,0 +1,62 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp-native command term that does nothing.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import warp as wp + +from isaaclab_experimental.managers import CommandTerm + +if TYPE_CHECKING: + from .commands_cfg import NullCommandCfg + + +class NullCommand(CommandTerm): + """Command generator that does nothing. + + Warp-native twin of :class:`isaaclab.envs.mdp.commands.NullCommand`. It does not + generate any commands and is used for environments that do not require one. All + inherited bookkeeping runs as device-side kernels on empty metrics with an + infinite resampling time, so the term is trivially graph-capturable. + """ + + cfg: NullCommandCfg + """Configuration for the command generator.""" + + def __str__(self) -> str: + msg = "NullCommand:\n" + msg += "\tCommand dimension: N/A\n" + msg += f"\tResampling time range: {self.cfg.resampling_time_range}" + return msg + + """ + Properties + """ + + @property + def command(self): + """Null command. + + Raises: + RuntimeError: No command is generated. Always raises this error. + """ + raise RuntimeError("NullCommandTerm does not generate any commands.") + + """ + Implementation specific functions. + """ + + def _update_metrics(self): + pass + + def _resample_command(self, env_mask: wp.array): + pass + + def _update_command(self): + pass diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/pose_command.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/pose_command.py new file mode 100644 index 000000000000..981671c8d944 --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/pose_command.py @@ -0,0 +1,235 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp-native pose command generator.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import torch +import warp as wp + +from isaaclab.assets import Articulation +from isaaclab.envs.mdp.commands._debug_vis import _PoseCommandDebugVis +from isaaclab.utils.leapp import POSE7_ELEMENT_NAMES + +from isaaclab_experimental.managers import CommandTerm + +if TYPE_CHECKING: + from isaaclab.envs import ManagerBasedEnv + + from .commands_cfg import UniformPoseCommandCfg + + +@wp.kernel +def _resample_pose_command( + env_mask: wp.array(dtype=wp.bool), + rng_state: wp.array(dtype=wp.uint32), + command: wp.array(dtype=wp.float32, ndim=2), + pos_x_min: float, + pos_x_max: float, + pos_y_min: float, + pos_y_max: float, + pos_z_min: float, + pos_z_max: float, + roll_min: float, + roll_max: float, + pitch_min: float, + pitch_max: float, + yaw_min: float, + yaw_max: float, + make_quat_unique: bool, +): + """Draw a new body-frame pose command for selected envs: uniform position [m] + + and roll/pitch/yaw [rad] composed into a quaternion (optionally + sign-canonicalized to the w >= 0 hemisphere). RNG state is written back so + the sequence advances across replays. + """ + env_id = wp.tid() + if env_mask[env_id]: + state = rng_state[env_id] + command[env_id, 0] = wp.randf(state, pos_x_min, pos_x_max) + command[env_id, 1] = wp.randf(state, pos_y_min, pos_y_max) + command[env_id, 2] = wp.randf(state, pos_z_min, pos_z_max) + + roll = wp.randf(state, roll_min, roll_max) + pitch = wp.randf(state, pitch_min, pitch_max) + yaw = wp.randf(state, yaw_min, yaw_max) + qx = wp.quat_from_axis_angle(wp.vec3f(1.0, 0.0, 0.0), roll) + qy = wp.quat_from_axis_angle(wp.vec3f(0.0, 1.0, 0.0), pitch) + qz = wp.quat_from_axis_angle(wp.vec3f(0.0, 0.0, 1.0), yaw) + quat = wp.mul(wp.mul(qz, qy), qx) + if make_quat_unique and quat[3] < 0.0: + quat = wp.quatf(-quat[0], -quat[1], -quat[2], -quat[3]) + command[env_id, 3] = quat[0] + command[env_id, 4] = quat[1] + command[env_id, 5] = quat[2] + command[env_id, 6] = quat[3] + rng_state[env_id] = state + + +@wp.kernel +def _update_pose_metrics( + command_b: wp.array(dtype=wp.float32, ndim=2), + root_pos_w: wp.array(dtype=wp.vec3f), + root_quat_w: wp.array(dtype=wp.quatf), + body_pos_w: wp.array(dtype=wp.vec3f, ndim=2), + body_quat_w: wp.array(dtype=wp.quatf, ndim=2), + body_idx: int, + command_w: wp.array(dtype=wp.float32, ndim=2), + position_error: wp.array(dtype=wp.float32), + orientation_error: wp.array(dtype=wp.float32), + success_rate: wp.array(dtype=wp.float32), + track_success: bool, + position_success_threshold: float, +): + """Refresh the world-frame goal pose (root pose composed with the body-frame + + command) and the tracking metrics: end-effector position [m] / orientation + [rad] errors vs the goal, and a position-threshold success flag when success + tracking is enabled. + """ + env_id = wp.tid() + position_b = wp.vec3f(command_b[env_id, 0], command_b[env_id, 1], command_b[env_id, 2]) + orientation_b = wp.quatf(command_b[env_id, 3], command_b[env_id, 4], command_b[env_id, 5], command_b[env_id, 6]) + desired_position_w = root_pos_w[env_id] + wp.quat_rotate(root_quat_w[env_id], position_b) + desired_orientation_w = root_quat_w[env_id] * orientation_b + + command_w[env_id, 0] = desired_position_w[0] + command_w[env_id, 1] = desired_position_w[1] + command_w[env_id, 2] = desired_position_w[2] + command_w[env_id, 3] = desired_orientation_w[0] + command_w[env_id, 4] = desired_orientation_w[1] + command_w[env_id, 5] = desired_orientation_w[2] + command_w[env_id, 6] = desired_orientation_w[3] + + position_delta = body_pos_w[env_id, body_idx] - desired_position_w + position_error_value = wp.length(position_delta) + orientation_delta = body_quat_w[env_id, body_idx] * wp.quat_inverse(desired_orientation_w) + orientation_error_value = 2.0 * wp.acos(wp.clamp(wp.abs(orientation_delta[3]), 0.0, 1.0)) + position_error[env_id] = position_error_value + orientation_error[env_id] = orientation_error_value + if track_success and position_error_value < position_success_threshold: + success_rate[env_id] = 1.0 + + +class UniformPoseCommand(_PoseCommandDebugVis, CommandTerm): + """Generate a pose command from uniform position and Euler-angle distributions.""" + + cfg: UniformPoseCommandCfg + """Configuration for the command generator.""" + + def __init__(self, cfg: UniformPoseCommandCfg, env: ManagerBasedEnv): + """Initialize the command generator. + + Args: + cfg: Configuration for the command generator. + env: Environment containing the commanded articulation. + """ + super().__init__(cfg, env) + + # -- robot / body resolution + self.robot: Articulation = env.scene[cfg.asset_name] + self.body_idx = self.robot.find_bodies(cfg.body_name)[0][0] + + # -- command buffers (Warp-native, pointer-stable) + self.pose_command_b = wp.zeros((self.num_envs, 7), dtype=wp.float32, device=self.device) + self.pose_command_w = wp.zeros((self.num_envs, 7), dtype=wp.float32, device=self.device) + + # -- metrics buffers + self.metrics["position_error"] = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self.metrics["orientation_error"] = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self._success_rate = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self._track_success = cfg.position_success_threshold is not None + if self._track_success: + self.metrics["success_rate"] = self._success_rate + + # adds (optional) cmd kind and element names for leapp export + self.cfg.cmd_kind = self.cfg.cmd_kind or "command/body/pose" + self.cfg.element_names = self.cfg.element_names or POSE7_ELEMENT_NAMES + + # -- Torch views (zero-copy aliases for stable consumers) + self.pose_command_b_torch = wp.to_torch(self.pose_command_b) + self.pose_command_w_torch = wp.to_torch(self.pose_command_w) + self.pose_command_b_torch[:, 6] = 1.0 + + def __str__(self) -> str: + """Return a string representation of the command generator.""" + msg = "UniformPoseCommand:\n" + msg += f"\tCommand dimension: {tuple(self.command.shape[1:])}\n" + msg += f"\tResampling time range: {self.cfg.resampling_time_range}\n" + return msg + + @property + def command(self) -> torch.Tensor: + """Desired pose command [m, m, m, quaternion xyzw], shape ``(num_envs, 7)``.""" + return self.pose_command_b_torch + + @property + def command_wp(self) -> wp.array(dtype=wp.float32, ndim=2): + """Pointer-stable desired pose command [m, m, m, quaternion xyzw].""" + return self.pose_command_b + + def _update_metrics(self): + body_idx = int(self.body_idx) + track_success = bool(self._track_success) + position_success_threshold = float(self.cfg.position_success_threshold or 0.0) + self._env._warp_launch.launch( + _update_pose_metrics, + dim=self.num_envs, + inputs=[ + self.pose_command_b, + self.robot.data.root_pos_w.warp, + self.robot.data.root_quat_w.warp, + self.robot.data.body_pos_w.warp, + self.robot.data.body_quat_w.warp, + body_idx, + self.pose_command_w, + self.metrics["position_error"], + self.metrics["orientation_error"], + self._success_rate, + track_success, + position_success_threshold, + ], + site=(self, "update_metrics", body_idx, track_success, position_success_threshold), + ) + + def _resample_command(self, env_mask: wp.array): + ranges = self.cfg.ranges + launch_scalars = ( + float(ranges.pos_x[0]), + float(ranges.pos_x[1]), + float(ranges.pos_y[0]), + float(ranges.pos_y[1]), + float(ranges.pos_z[0]), + float(ranges.pos_z[1]), + float(ranges.roll[0]), + float(ranges.roll[1]), + float(ranges.pitch[0]), + float(ranges.pitch[1]), + float(ranges.yaw[0]), + float(ranges.yaw[1]), + bool(self.cfg.make_quat_unique), + ) + self._env._warp_launch.launch( + _resample_pose_command, + dim=self.num_envs, + inputs=[ + env_mask, + self._env.rng_state_wp, + self.pose_command_b, + *launch_scalars, + ], + site=(self, "resample", env_mask, *launch_scalars), + ) + + def _update_command(self): + pass + + def _debug_pose_command_w(self) -> torch.Tensor: + """Return the Torch alias of the pointer-stable world-frame pose command buffer.""" + return self.pose_command_w_torch diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/velocity_command.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/velocity_command.py new file mode 100644 index 000000000000..8100c3772f6b --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/commands/velocity_command.py @@ -0,0 +1,343 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp-native velocity command generator.""" + +from __future__ import annotations + +import logging +from collections.abc import Sequence +from typing import TYPE_CHECKING + +import torch +import warp as wp +from isaaclab_newton.kernels.state_kernels import body_ang_vel_from_root, body_lin_vel_from_root + +from isaaclab.assets import Articulation +from isaaclab.envs.mdp.commands._debug_vis import _VelocityCommandDebugVis + +from isaaclab_experimental.managers import CommandTerm +from isaaclab_experimental.utils.warp import wrap_to_pi + +if TYPE_CHECKING: + from isaaclab.envs import ManagerBasedEnv + + from .commands_cfg import UniformVelocityCommandCfg + +logger = logging.getLogger(__name__) + + +@wp.kernel +def _accumulate_velocity_metrics( + command: wp.array(dtype=wp.float32, ndim=2), + root_pose_w: wp.array(dtype=wp.transformf), + root_vel_w: wp.array(dtype=wp.spatial_vectorf), + error_xy_sum: wp.array(dtype=wp.float32), + error_yaw_sum: wp.array(dtype=wp.float32), + step_count: wp.array(dtype=wp.float32), +): + """Accumulate per-step command-tracking error sums for logging: planar (xy) + + linear-velocity error [m/s] and yaw-rate error [rad/s] vs the command, plus + the step count used later for averaging. Body-frame velocities are derived + inline from the root pose/velocity (capture-safe, no lazy buffers). + """ + env_id = wp.tid() + root_lin_vel_b = body_lin_vel_from_root(root_pose_w[env_id], root_vel_w[env_id]) + root_ang_vel_b = body_ang_vel_from_root(root_pose_w[env_id], root_vel_w[env_id]) + dx = command[env_id, 0] - root_lin_vel_b[0] + dy = command[env_id, 1] - root_lin_vel_b[1] + error_xy_sum[env_id] += wp.sqrt(dx * dx + dy * dy) + error_yaw_sum[env_id] += wp.abs(command[env_id, 2] - root_ang_vel_b[2]) + step_count[env_id] += 1.0 + + +@wp.kernel +def _finalize_velocity_metrics( + env_mask: wp.array(dtype=wp.bool), + error_xy_sum: wp.array(dtype=wp.float32), + error_yaw_sum: wp.array(dtype=wp.float32), + step_count: wp.array(dtype=wp.float32), + error_xy: wp.array(dtype=wp.float32), + error_yaw: wp.array(dtype=wp.float32), + success_rate: wp.array(dtype=wp.float32), + error_xy_threshold: float, + error_yaw_threshold: float, +): + """Turn selected envs' accumulated error sums into episode means and a binary + + success flag (both mean errors under their thresholds), then zero the + accumulators for the next episode. + """ + env_id = wp.tid() + if env_mask[env_id]: + denominator = wp.max(step_count[env_id], 1.0) + mean_error_xy = error_xy_sum[env_id] / denominator + mean_error_yaw = error_yaw_sum[env_id] / denominator + error_xy[env_id] = mean_error_xy + error_yaw[env_id] = mean_error_yaw + success_rate[env_id] = wp.where( + (mean_error_xy < error_xy_threshold) and (mean_error_yaw < error_yaw_threshold), 1.0, 0.0 + ) + error_xy_sum[env_id] = 0.0 + error_yaw_sum[env_id] = 0.0 + step_count[env_id] = 0.0 + + +@wp.kernel +def _resample_velocity_command( + env_mask: wp.array(dtype=wp.bool), + rng_state: wp.array(dtype=wp.uint32), + command: wp.array(dtype=wp.float32, ndim=2), + heading_target: wp.array(dtype=wp.float32), + is_heading_env: wp.array(dtype=wp.bool), + is_standing_env: wp.array(dtype=wp.bool), + lin_vel_x_min: float, + lin_vel_x_max: float, + lin_vel_y_min: float, + lin_vel_y_max: float, + ang_vel_z_min: float, + ang_vel_z_max: float, + heading_min: float, + heading_max: float, + heading_command: bool, + rel_heading_envs: float, + rel_standing_envs: float, +): + """Draw a new SE(2) velocity command (vx, vy [m/s], wz [rad/s]) for selected + + envs, plus their heading target [rad] and heading/standing role flags. + The per-env device RNG state is written back so the random sequence advances + across CUDA-graph replays. + """ + env_id = wp.tid() + if env_mask[env_id]: + state = rng_state[env_id] + command[env_id, 0] = wp.randf(state, lin_vel_x_min, lin_vel_x_max) + command[env_id, 1] = wp.randf(state, lin_vel_y_min, lin_vel_y_max) + command[env_id, 2] = wp.randf(state, ang_vel_z_min, ang_vel_z_max) + if heading_command: + heading_target[env_id] = wp.randf(state, heading_min, heading_max) + is_heading_env[env_id] = wp.randf(state, 0.0, 1.0) <= rel_heading_envs + else: + is_heading_env[env_id] = False + is_standing_env[env_id] = wp.randf(state, 0.0, 1.0) <= rel_standing_envs + rng_state[env_id] = state + + +@wp.kernel +def _update_velocity_command( + command: wp.array(dtype=wp.float32, ndim=2), + heading_target: wp.array(dtype=wp.float32), + is_heading_env: wp.array(dtype=wp.bool), + is_standing_env: wp.array(dtype=wp.bool), + root_pose_w: wp.array(dtype=wp.transformf), + heading_command: bool, + heading_control_stiffness: float, + ang_vel_z_min: float, + ang_vel_z_max: float, +): + """Per-step command shaping: heading-controlled envs convert heading error into + + a clamped yaw-rate command (proportional control); standing envs zero their + command entirely. + """ + env_id = wp.tid() + if heading_command and is_heading_env[env_id]: + root_quat_w = wp.transform_get_rotation(root_pose_w[env_id]) + forward_w = wp.quat_rotate(root_quat_w, wp.vec3f(1.0, 0.0, 0.0)) + heading_w = wp.atan2(forward_w[1], forward_w[0]) + heading_error = wrap_to_pi(heading_target[env_id] - heading_w) + command[env_id, 2] = wp.clamp( + heading_control_stiffness * heading_error, + ang_vel_z_min, + ang_vel_z_max, + ) + if is_standing_env[env_id]: + command[env_id, 0] = 0.0 + command[env_id, 1] = 0.0 + command[env_id, 2] = 0.0 + + +class UniformVelocityCommand(_VelocityCommandDebugVis, CommandTerm): + """Generate an SE(2) velocity command from uniform distributions.""" + + cfg: UniformVelocityCommandCfg + """Configuration for the command generator.""" + + def __init__(self, cfg: UniformVelocityCommandCfg, env: ManagerBasedEnv): + """Initialize the command generator. + + Args: + cfg: Configuration for the command generator. + env: Environment containing the commanded articulation. + + Raises: + ValueError: If heading commands are enabled without a heading range. + """ + super().__init__(cfg, env) + + # -- config validation + if cfg.heading_command and cfg.ranges.heading is None: + raise ValueError( + "The velocity command has heading commands active (heading_command=True) but the `ranges.heading`" + " parameter is set to None." + ) + if cfg.ranges.heading and not cfg.heading_command: + logger.warning( + f"The velocity command has the 'ranges.heading' attribute set to '{cfg.ranges.heading}'" + " but the heading command is not active. Consider setting the flag for the heading command to True." + ) + + # -- robot resolution + self.robot: Articulation = env.scene[cfg.asset_name] + + # -- command buffers (Warp-native, pointer-stable) + self.vel_command_b = wp.zeros((self.num_envs, 3), dtype=wp.float32, device=self.device) + self.heading_target = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self.is_heading_env = wp.zeros(self.num_envs, dtype=wp.bool, device=self.device) + self.is_standing_env = wp.zeros(self.num_envs, dtype=wp.bool, device=self.device) + + # -- metrics buffers + self.metrics["error_vel_xy"] = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self.metrics["error_vel_yaw"] = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self.metrics["success_rate"] = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self._error_xy_sum = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self._error_yaw_sum = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + self._step_count = wp.zeros(self.num_envs, dtype=wp.float32, device=self.device) + + # adds (optional) cmd kind and element names for leapp export + self.cfg.cmd_kind = self.cfg.cmd_kind or "command/body/velocity" + self.cfg.element_names = self.cfg.element_names or ["lin_vel_x", "lin_vel_y", "ang_vel_z"] + + # -- Torch views (zero-copy aliases for stable consumers) + self.vel_command_b_torch = wp.to_torch(self.vel_command_b) + self.heading_target_torch = wp.to_torch(self.heading_target) + self.is_heading_env_torch = wp.to_torch(self.is_heading_env) + self.is_standing_env_torch = wp.to_torch(self.is_standing_env) + + def __str__(self) -> str: + """Return a string representation of the command generator.""" + msg = "UniformVelocityCommand:\n" + msg += f"\tCommand dimension: {tuple(self.command.shape[1:])}\n" + msg += f"\tResampling time range: {self.cfg.resampling_time_range}\n" + msg += f"\tHeading command: {self.cfg.heading_command}\n" + if self.cfg.heading_command: + msg += f"\tHeading probability: {self.cfg.rel_heading_envs}\n" + msg += f"\tStanding probability: {self.cfg.rel_standing_envs}" + return msg + + @property + def command(self) -> torch.Tensor: + """Desired base velocity command [m/s, m/s, rad/s], shape ``(num_envs, 3)``.""" + return self.vel_command_b_torch + + @property + def command_wp(self) -> wp.array(dtype=wp.float32, ndim=2): + """Pointer-stable desired base velocity command [m/s, m/s, rad/s].""" + return self.vel_command_b + + def reset( + self, + env_ids: Sequence[int] | torch.Tensor | None = None, + *, + env_mask: wp.array | None = None, + ) -> dict[str, torch.Tensor]: + """Finalize episode metrics and reset selected command state. + + Args: + env_ids: Environment indices used by compatibility call sites. + env_mask: Boolean Warp mask selecting environments to reset. Takes precedence over ``env_ids``. + + Returns: + Persistent scalar metric views for logging. + """ + env_mask = self._resolve_reset_mask(env_ids, env_mask) + metric_thresholds = ( + float(self.cfg.vel_xy_success_threshold), + float(self.cfg.vel_yaw_success_threshold), + ) + self._env._warp_launch.launch( + _finalize_velocity_metrics, + dim=self.num_envs, + inputs=[ + env_mask, + self._error_xy_sum, + self._error_yaw_sum, + self._step_count, + self.metrics["error_vel_xy"], + self.metrics["error_vel_yaw"], + self.metrics["success_rate"], + *metric_thresholds, + ], + site=(self, "finalize_metrics", env_mask, *metric_thresholds), + ) + return super().reset(env_mask=env_mask) + + def _update_metrics(self): + self._env._warp_launch.launch( + _accumulate_velocity_metrics, + dim=self.num_envs, + inputs=[ + self.vel_command_b, + self.robot.data.root_pose_w.warp, + self.robot.data.root_vel_w.warp, + self._error_xy_sum, + self._error_yaw_sum, + self._step_count, + ], + site=(self, "accumulate_metrics"), + ) + + def _resample_command(self, env_mask: wp.array): + heading_range = self.cfg.ranges.heading if self.cfg.ranges.heading is not None else (0.0, 0.0) + launch_scalars = ( + float(self.cfg.ranges.lin_vel_x[0]), + float(self.cfg.ranges.lin_vel_x[1]), + float(self.cfg.ranges.lin_vel_y[0]), + float(self.cfg.ranges.lin_vel_y[1]), + float(self.cfg.ranges.ang_vel_z[0]), + float(self.cfg.ranges.ang_vel_z[1]), + float(heading_range[0]), + float(heading_range[1]), + bool(self.cfg.heading_command), + float(self.cfg.rel_heading_envs), + float(self.cfg.rel_standing_envs), + ) + self._env._warp_launch.launch( + _resample_velocity_command, + dim=self.num_envs, + inputs=[ + env_mask, + self._env.rng_state_wp, + self.vel_command_b, + self.heading_target, + self.is_heading_env, + self.is_standing_env, + *launch_scalars, + ], + site=(self, "resample", env_mask, *launch_scalars), + ) + + def _update_command(self): + launch_scalars = ( + bool(self.cfg.heading_command), + float(self.cfg.heading_control_stiffness), + float(self.cfg.ranges.ang_vel_z[0]), + float(self.cfg.ranges.ang_vel_z[1]), + ) + self._env._warp_launch.launch( + _update_velocity_command, + dim=self.num_envs, + inputs=[ + self.vel_command_b, + self.heading_target, + self.is_heading_env, + self.is_standing_env, + self.robot.data.root_pose_w.warp, + *launch_scalars, + ], + site=(self, "update_command", *launch_scalars), + ) diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/events.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/events.py index d9e61041a7ad..c6909eb22055 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/events.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/events.py @@ -5,7 +5,7 @@ """Warp-first overrides for common event terms. -These functions are intended to be used with the experimental Warp-first +These terms are intended to be used with the experimental Warp-first :class:`isaaclab_experimental.managers.EventManager` (mask-based interval/reset). Why this exists: @@ -17,6 +17,9 @@ These Warp-first implementations avoid that by writing directly into the sim-bound Warp state buffers (`asset.data.joint_pos` / `asset.data.joint_vel`) for the selected envs/joints. +Stateful terms use :class:`~isaaclab_experimental.managers.ManagerTermBase` so +persistent buffers and parsed constants are created once during manager setup. + Notes: - These terms assume the Newton/Warp backend (Warp arrays are available for joint state and defaults). - For best performance, pass :class:`isaaclab_experimental.managers.SceneEntityCfg` so `joint_ids_wp` is cached. @@ -26,13 +29,66 @@ from typing import TYPE_CHECKING +import torch import warp as wp -from isaaclab_experimental.managers import SceneEntityCfg +from isaaclab.envs.mdp.events import randomize_rigid_body_mass as _StableRandomizeRigidBodyMass +from isaaclab.envs.mdp.events import randomize_rigid_body_material as _StableRandomizeRigidBodyMaterial + +from isaaclab_experimental.managers import EventTermCfg, ManagerTermBase, SceneEntityCfg +from isaaclab_experimental.managers import ManagerTermBase as _WarpManagerTermBase from isaaclab_experimental.utils.warp import WarpCapturable +__all__ = [ + "apply_external_force_torque", + "push_by_setting_velocity", + "randomize_rigid_body_com", + "randomize_rigid_body_mass", + "randomize_rigid_body_material", + "reset_joints_by_offset", + "reset_joints_by_scale", + "reset_root_state_uniform", +] + if TYPE_CHECKING: - from isaaclab.assets import Articulation + from isaaclab.assets import Articulation, RigidObject + from isaaclab.envs import ManagerBasedEnv + + +def _resolve_body_ids(asset_cfg: SceneEntityCfg, num_bodies: int) -> list[int]: + """Resolve a configured body selection without allocating device storage.""" + if asset_cfg.body_ids is None or asset_cfg.body_ids == slice(None): + return list(range(num_bodies)) + if isinstance(asset_cfg.body_ids, int): + return [asset_cfg.body_ids] + return list(asset_cfg.body_ids) + + +def _launch_recorded( + env: ManagerBasedEnv, + term: object, + kernel: wp.Kernel, + dim: int, + inputs: list, + env_mask: wp.array(dtype=wp.bool), +) -> None: + """Launch a persistent event kernel eagerly or through the environment launch cache. + + Args: + env: Environment owning the recorded-launch cache. + term: Term instance owning the persistent kernel arguments. + kernel: Warp kernel to launch. + dim: Launch dimension. + inputs: Kernel inputs. Must be pointer-stable for recorded replay. + env_mask: Selection mask included in the recording identity, so the same + term records separately per persistent mask buffer. + """ + launch_cache = getattr(env, "_warp_launch", None) + if launch_cache is None: + wp.launch(kernel=kernel, dim=dim, inputs=inputs, device=env.device) + return + launch_cache.launch(kernel, dim, inputs=inputs, site=(term, env_mask)) + # --------------------------------------------------------------------------- # Randomize rigid body center of mass @@ -43,66 +99,92 @@ def _randomize_com_kernel( env_mask: wp.array(dtype=wp.bool), rng_state: wp.array(dtype=wp.uint32), + default_body_com_pos_b: wp.array(dtype=wp.vec3f, ndim=2), body_com_pos_b: wp.array(dtype=wp.vec3f, ndim=2), body_ids: wp.array(dtype=wp.int32), com_lo: wp.vec3f, com_hi: wp.vec3f, ): - """Add random offset to center of mass positions for selected bodies.""" + """Add random offsets to the default center-of-mass positions for selected bodies.""" env_id = wp.tid() if not env_mask[env_id]: return state = rng_state[env_id] + dx = wp.randf(state, com_lo[0], com_hi[0]) + dy = wp.randf(state, com_lo[1], com_hi[1]) + dz = wp.randf(state, com_lo[2], com_hi[2]) for k in range(body_ids.shape[0]): b = body_ids[k] - v = body_com_pos_b[env_id, b] - dx = wp.randf(state, com_lo[0], com_hi[0]) - dy = wp.randf(state, com_lo[1], com_hi[1]) - dz = wp.randf(state, com_lo[2], com_hi[2]) + v = default_body_com_pos_b[env_id, b] body_com_pos_b[env_id, b] = wp.vec3f(v[0] + dx, v[1] + dy, v[2] + dz) rng_state[env_id] = state @WarpCapturable(False, reason="set_coms_mask calls SimulationManager.add_model_change") -def randomize_rigid_body_com( - env, - env_mask: wp.array, - com_range: dict[str, tuple[float, float]], - asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), -): - """Randomize the center of mass (CoM) of rigid bodies by adding random offsets. +class randomize_rigid_body_com(ManagerTermBase): + """Randomize rigid-body centers of mass from a persistent default baseline. - Warp-first override of :func:`isaaclab.envs.mdp.events.randomize_rigid_body_com`. - Writes directly into the sim-bound ``body_com_pos_b`` buffer, then notifies the solver - via :meth:`set_coms_mask` so it recomputes inertial properties. + This term is not CUDA-graph capturable because notifying the solver of changed + inertial properties calls :meth:`SimulationManager.add_model_change`. """ - asset: Articulation = env.scene[asset_cfg.name] - - fn = randomize_rigid_body_com - if not getattr(fn, "_is_warmed_up", False) or fn._asset_name != asset_cfg.name: - fn._asset_name = asset_cfg.name - r = [com_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z"]] - fn._com_lo = wp.vec3f(r[0][0], r[1][0], r[2][0]) - fn._com_hi = wp.vec3f(r[0][1], r[1][1], r[2][1]) - fn._is_warmed_up = True - wp.launch( - kernel=_randomize_com_kernel, - dim=env.num_envs, - inputs=[ + def __init__(self, cfg: EventTermCfg, env: ManagerBasedEnv) -> None: + """Initialize persistent center-of-mass randomization state. + + Args: + cfg: Event term configuration. + env: Environment containing the randomized asset. + """ + super().__init__(cfg, env) + asset_cfg: SceneEntityCfg = cfg.params["asset_cfg"] + self._asset: RigidObject | Articulation = env.scene[asset_cfg.name] + # Startup terms may adjust the center of mass after this term is constructed; + # the randomization baseline is captured lazily on the first call instead. + self._default_com: wp.array | None = None + body_ids = _resolve_body_ids(asset_cfg, self._asset.num_bodies) + self._body_ids = wp.array(body_ids, dtype=wp.int32, device=env.device) + + com_range = cfg.params["com_range"] + ranges = [com_range.get(key, (0.0, 0.0)) for key in ("x", "y", "z")] + self._com_lo = wp.vec3f(ranges[0][0], ranges[1][0], ranges[2][0]) + self._com_hi = wp.vec3f(ranges[0][1], ranges[1][1], ranges[2][1]) + + def __call__( + self, + env: ManagerBasedEnv, + env_mask: wp.array(dtype=wp.bool), + com_range: dict[str, tuple[float, float]], + asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), + ) -> None: + """Randomize selected center-of-mass offsets [m]. + + Args: + env: Environment containing the randomized asset. + env_mask: Boolean Warp mask selecting environments. + com_range: Per-axis offset ranges [m]. Parsed during initialization. + asset_cfg: Scene entity selection. Resolved during initialization. + """ + if self._default_com is None: + self._default_com = wp.clone(self._asset.data.body_com_pos_b.warp) + _launch_recorded( + env, + self, + _randomize_com_kernel, + env.num_envs, + [ + env_mask, + env.rng_state_wp, + self._default_com, + self._asset.data.body_com_pos_b.warp, + self._body_ids, + self._com_lo, + self._com_hi, + ], env_mask, - env.rng_state_wp, - asset.data.body_com_pos_b.warp, - asset_cfg.body_ids_wp, - fn._com_lo, - fn._com_hi, - ], - device=env.device, - ) + ) - # Notify the solver that inertial properties changed (COM position affects inertia). - asset.set_coms_mask(coms=asset.data.body_com_pos_b.warp, env_mask=env_mask) + self._asset.set_coms_mask(coms=self._asset.data.body_com_pos_b.warp, env_mask=env_mask) # --------------------------------------------------------------------------- @@ -114,6 +196,7 @@ def randomize_rigid_body_com( def _apply_external_force_torque_kernel( env_mask: wp.array(dtype=wp.bool), rng_state: wp.array(dtype=wp.uint32), + body_ids: wp.array(dtype=wp.int32), force_out: wp.array(dtype=wp.vec3f, ndim=2), torque_out: wp.array(dtype=wp.vec3f, ndim=2), force_lo: float, @@ -121,16 +204,17 @@ def _apply_external_force_torque_kernel( torque_lo: float, torque_hi: float, ): + """Sample uniform random external force [N] and torque [N·m] vectors for + + selected envs' target bodies into the asset's wrench composer buffers. + """ env_id = wp.tid() if not env_mask[env_id]: - # zero out unmasked envs so they don't accumulate stale forces - for b in range(force_out.shape[1]): - force_out[env_id, b] = wp.vec3f(0.0, 0.0, 0.0) - torque_out[env_id, b] = wp.vec3f(0.0, 0.0, 0.0) return state = rng_state[env_id] - for b in range(force_out.shape[1]): + for body_index in range(body_ids.shape[0]): + b = body_ids[body_index] force_out[env_id, b] = wp.vec3f( wp.randf(state, force_lo, force_hi), wp.randf(state, force_lo, force_hi), @@ -144,50 +228,83 @@ def _apply_external_force_torque_kernel( rng_state[env_id] = state -def apply_external_force_torque( - env, - env_mask: wp.array, - force_range: tuple[float, float], - torque_range: tuple[float, float], - asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), -): - """Randomize external forces and torques applied to the asset's bodies. - - Warp-first override of :func:`isaaclab.envs.mdp.events.apply_external_force_torque`. - """ - asset: Articulation = env.scene[asset_cfg.name] - - # First-call: allocate scratch and pre-convert constant arguments. - if not getattr(apply_external_force_torque, "_is_warmed_up", False): - apply_external_force_torque._scratch_forces = wp.zeros( - (env.num_envs, asset.num_bodies), dtype=wp.vec3f, device=env.device - ) - apply_external_force_torque._scratch_torques = wp.zeros( - (env.num_envs, asset.num_bodies), dtype=wp.vec3f, device=env.device - ) - apply_external_force_torque._is_warmed_up = True - - wp.launch( - kernel=_apply_external_force_torque_kernel, - dim=env.num_envs, - inputs=[ +class apply_external_force_torque(ManagerTermBase): + """Apply random external forces and torques using persistent wrench buffers.""" + + def __init__(self, cfg: EventTermCfg, env: ManagerBasedEnv) -> None: + """Initialize persistent external-wrench state. + + Args: + cfg: Event term configuration. + env: Environment containing the randomized asset. + """ + super().__init__(cfg, env) + asset_cfg: SceneEntityCfg = cfg.params["asset_cfg"] + self._asset: RigidObject | Articulation = env.scene[asset_cfg.name] + self._forces = wp.zeros((env.num_envs, self._asset.num_bodies), dtype=wp.vec3f, device=env.device) + self._torques = wp.zeros((env.num_envs, self._asset.num_bodies), dtype=wp.vec3f, device=env.device) + + body_ids = _resolve_body_ids(asset_cfg, self._asset.num_bodies) + body_mask = [False] * self._asset.num_bodies + for body_id in body_ids: + body_mask[body_id] = True + self._body_ids = wp.array(body_ids, dtype=wp.int32, device=env.device) + self._body_mask = wp.array(body_mask, dtype=wp.bool, device=env.device) + + force_range = cfg.params["force_range"] + torque_range = cfg.params["torque_range"] + self._force_lo = float(force_range[0]) + self._force_hi = float(force_range[1]) + self._torque_lo = float(torque_range[0]) + self._torque_hi = float(torque_range[1]) + # Zero-width ranges configure a placeholder term. Skipping the whole call + # preserves composer wrench state written by other sources across resets. + self._is_noop = self._force_lo == self._force_hi == 0.0 and self._torque_lo == self._torque_hi == 0.0 + + def __call__( + self, + env: ManagerBasedEnv, + env_mask: wp.array(dtype=wp.bool), + force_range: tuple[float, float], + torque_range: tuple[float, float], + asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), + ) -> None: + """Apply sampled forces [N] and torques [N·m] to selected bodies. + + Args: + env: Environment containing the randomized asset. + env_mask: Boolean Warp mask selecting environments. + force_range: Component-wise force range [N]. Parsed during initialization. + torque_range: Component-wise torque range [N·m]. Parsed during initialization. + asset_cfg: Scene entity selection. Resolved during initialization. + """ + if self._is_noop: + return + _launch_recorded( + env, + self, + _apply_external_force_torque_kernel, + env.num_envs, + [ + env_mask, + env.rng_state_wp, + self._body_ids, + self._forces, + self._torques, + self._force_lo, + self._force_hi, + self._torque_lo, + self._torque_hi, + ], env_mask, - env.rng_state_wp, - apply_external_force_torque._scratch_forces, - apply_external_force_torque._scratch_torques, - force_range[0], - force_range[1], - torque_range[0], - torque_range[1], - ], - device=env.device, - ) + ) - asset.permanent_wrench_composer.set_forces_and_torques_mask( - forces=apply_external_force_torque._scratch_forces, - torques=apply_external_force_torque._scratch_torques, - env_mask=env_mask, - ) + self._asset.permanent_wrench_composer.set_forces_and_torques_mask( + forces=self._forces, + torques=self._torques, + body_mask=self._body_mask, + env_mask=env_mask, + ) # --------------------------------------------------------------------------- @@ -206,6 +323,10 @@ def _push_by_setting_velocity_kernel( ang_lo: wp.vec3f, ang_hi: wp.vec3f, ): + """Add a uniform random velocity kick [m/s, rad/s] to selected envs' current + + root velocity, writing the result for the masked sim write. + """ env_id = wp.tid() if not env_mask[env_id]: return @@ -225,45 +346,62 @@ def _push_by_setting_velocity_kernel( rng_state[env_id] = state -def push_by_setting_velocity( - env, - env_mask: wp.array, - velocity_range: dict[str, tuple[float, float]], - asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), -): - """Push the asset by setting the root velocity to a random value within the given ranges. - - Warp-first override of :func:`isaaclab.envs.mdp.events.push_by_setting_velocity`. - """ - asset: Articulation = env.scene[asset_cfg.name] - - # First-call: allocate scratch and pre-parse constant range arguments. - if not getattr(push_by_setting_velocity, "_is_warmed_up", False): - push_by_setting_velocity._scratch_vel = wp.zeros((env.num_envs,), dtype=wp.spatial_vectorf, device=env.device) - r = [velocity_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z", "roll", "pitch", "yaw"]] - push_by_setting_velocity._lin_lo = wp.vec3f(r[0][0], r[1][0], r[2][0]) - push_by_setting_velocity._lin_hi = wp.vec3f(r[0][1], r[1][1], r[2][1]) - push_by_setting_velocity._ang_lo = wp.vec3f(r[3][0], r[4][0], r[5][0]) - push_by_setting_velocity._ang_hi = wp.vec3f(r[3][1], r[4][1], r[5][1]) - push_by_setting_velocity._is_warmed_up = True - - wp.launch( - kernel=_push_by_setting_velocity_kernel, - dim=env.num_envs, - inputs=[ +class push_by_setting_velocity(ManagerTermBase): + """Push an asset by sampling into a persistent root-velocity buffer.""" + + def __init__(self, cfg: EventTermCfg, env: ManagerBasedEnv) -> None: + """Initialize persistent root-velocity push state. + + Args: + cfg: Event term configuration. + env: Environment containing the pushed asset. + """ + super().__init__(cfg, env) + asset_cfg: SceneEntityCfg = cfg.params["asset_cfg"] + self._asset: RigidObject | Articulation = env.scene[asset_cfg.name] + self._velocity = wp.zeros(env.num_envs, dtype=wp.spatial_vectorf, device=env.device) + + velocity_range = cfg.params["velocity_range"] + ranges = [velocity_range.get(key, (0.0, 0.0)) for key in ("x", "y", "z", "roll", "pitch", "yaw")] + self._lin_lo = wp.vec3f(ranges[0][0], ranges[1][0], ranges[2][0]) + self._lin_hi = wp.vec3f(ranges[0][1], ranges[1][1], ranges[2][1]) + self._ang_lo = wp.vec3f(ranges[3][0], ranges[4][0], ranges[5][0]) + self._ang_hi = wp.vec3f(ranges[3][1], ranges[4][1], ranges[5][1]) + + def __call__( + self, + env: ManagerBasedEnv, + env_mask: wp.array(dtype=wp.bool), + velocity_range: dict[str, tuple[float, float]], + asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), + ) -> None: + """Add sampled linear [m/s] and angular [rad/s] root velocities. + + Args: + env: Environment containing the pushed asset. + env_mask: Boolean Warp mask selecting environments. + velocity_range: Per-axis velocity ranges [m/s or rad/s]. Parsed during initialization. + asset_cfg: Scene entity selection. Resolved during initialization. + """ + _launch_recorded( + env, + self, + _push_by_setting_velocity_kernel, + env.num_envs, + [ + env_mask, + env.rng_state_wp, + self._asset.data.root_vel_w.warp, + self._velocity, + self._lin_lo, + self._lin_hi, + self._ang_lo, + self._ang_hi, + ], env_mask, - env.rng_state_wp, - asset.data.root_vel_w.warp, - push_by_setting_velocity._scratch_vel, - push_by_setting_velocity._lin_lo, - push_by_setting_velocity._lin_hi, - push_by_setting_velocity._ang_lo, - push_by_setting_velocity._ang_hi, - ], - device=env.device, - ) + ) - asset.write_root_velocity_to_sim_mask(root_velocity=push_by_setting_velocity._scratch_vel, env_mask=env_mask) + self._asset.write_root_velocity_to_sim_mask(root_velocity=self._velocity, env_mask=env_mask) # --------------------------------------------------------------------------- @@ -289,6 +427,11 @@ def _reset_root_state_uniform_kernel( vel_ang_lo: wp.vec3f, vel_ang_hi: wp.vec3f, ): + """Compose selected envs' reset root state: default pose offset by the env + + origin plus uniform position [m] / roll-pitch-yaw [rad] noise, and default + velocity plus uniform noise [m/s, rad/s]. + """ env_id = wp.tid() if not env_mask[env_id]: return @@ -335,62 +478,83 @@ def _reset_root_state_uniform_kernel( rng_state[env_id] = state -def reset_root_state_uniform( - env, - env_mask: wp.array, - pose_range: dict[str, tuple[float, float]], - velocity_range: dict[str, tuple[float, float]], - asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), -): - """Reset the asset root state to a random position and velocity uniformly within the given ranges. - - Warp-first override of :func:`isaaclab.envs.mdp.events.reset_root_state_uniform`. - """ - asset: Articulation = env.scene[asset_cfg.name] - - # First-call: allocate scratch and pre-parse range dicts. - if not getattr(reset_root_state_uniform, "_is_warmed_up", False): - reset_root_state_uniform._scratch_pose = wp.zeros((env.num_envs,), dtype=wp.transformf, device=env.device) - reset_root_state_uniform._scratch_vel = wp.zeros((env.num_envs,), dtype=wp.spatial_vectorf, device=env.device) - # Pre-parse pose_range dict - p = [pose_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z", "roll", "pitch", "yaw"]] - reset_root_state_uniform._pos_lo = wp.vec3f(p[0][0], p[1][0], p[2][0]) - reset_root_state_uniform._pos_hi = wp.vec3f(p[0][1], p[1][1], p[2][1]) - reset_root_state_uniform._rot_lo = wp.vec3f(p[3][0], p[4][0], p[5][0]) - reset_root_state_uniform._rot_hi = wp.vec3f(p[3][1], p[4][1], p[5][1]) - # Pre-parse velocity_range dict - v = [velocity_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z", "roll", "pitch", "yaw"]] - reset_root_state_uniform._vel_lin_lo = wp.vec3f(v[0][0], v[1][0], v[2][0]) - reset_root_state_uniform._vel_lin_hi = wp.vec3f(v[0][1], v[1][1], v[2][1]) - reset_root_state_uniform._vel_ang_lo = wp.vec3f(v[3][0], v[4][0], v[5][0]) - reset_root_state_uniform._vel_ang_hi = wp.vec3f(v[3][1], v[4][1], v[5][1]) - reset_root_state_uniform._is_warmed_up = True - - wp.launch( - kernel=_reset_root_state_uniform_kernel, - dim=env.num_envs, - inputs=[ +class reset_root_state_uniform(ManagerTermBase): + """Reset root pose and velocity using persistent Warp output buffers.""" + + def __init__(self, cfg: EventTermCfg, env: ManagerBasedEnv) -> None: + """Initialize persistent root-state reset state. + + Args: + cfg: Event term configuration. + env: Environment containing the reset asset. + """ + super().__init__(cfg, env) + asset_cfg: SceneEntityCfg = cfg.params["asset_cfg"] + self._asset: RigidObject | Articulation = env.scene[asset_cfg.name] + self._default_root_pose = self._asset.data.default_root_pose.warp + self._default_root_velocity = self._asset.data.default_root_vel.warp + self._env_origins = env.env_origins_wp + self._pose = wp.zeros(env.num_envs, dtype=wp.transformf, device=env.device) + self._velocity = wp.zeros(env.num_envs, dtype=wp.spatial_vectorf, device=env.device) + + pose_range = cfg.params["pose_range"] + pose_ranges = [pose_range.get(key, (0.0, 0.0)) for key in ("x", "y", "z", "roll", "pitch", "yaw")] + self._pos_lo = wp.vec3f(pose_ranges[0][0], pose_ranges[1][0], pose_ranges[2][0]) + self._pos_hi = wp.vec3f(pose_ranges[0][1], pose_ranges[1][1], pose_ranges[2][1]) + self._rot_lo = wp.vec3f(pose_ranges[3][0], pose_ranges[4][0], pose_ranges[5][0]) + self._rot_hi = wp.vec3f(pose_ranges[3][1], pose_ranges[4][1], pose_ranges[5][1]) + + velocity_range = cfg.params["velocity_range"] + velocity_ranges = [velocity_range.get(key, (0.0, 0.0)) for key in ("x", "y", "z", "roll", "pitch", "yaw")] + self._vel_lin_lo = wp.vec3f(velocity_ranges[0][0], velocity_ranges[1][0], velocity_ranges[2][0]) + self._vel_lin_hi = wp.vec3f(velocity_ranges[0][1], velocity_ranges[1][1], velocity_ranges[2][1]) + self._vel_ang_lo = wp.vec3f(velocity_ranges[3][0], velocity_ranges[4][0], velocity_ranges[5][0]) + self._vel_ang_hi = wp.vec3f(velocity_ranges[3][1], velocity_ranges[4][1], velocity_ranges[5][1]) + + def __call__( + self, + env: ManagerBasedEnv, + env_mask: wp.array(dtype=wp.bool), + pose_range: dict[str, tuple[float, float]], + velocity_range: dict[str, tuple[float, float]], + asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), + ) -> None: + """Reset root pose [m, rad] and velocity [m/s, rad/s]. + + Args: + env: Environment containing the reset asset. + env_mask: Boolean Warp mask selecting environments. + pose_range: Position and Euler-angle ranges [m or rad]. Parsed during initialization. + velocity_range: Linear and angular velocity ranges [m/s or rad/s]. Parsed during initialization. + asset_cfg: Scene entity selection. Resolved during initialization. + """ + _launch_recorded( + env, + self, + _reset_root_state_uniform_kernel, + env.num_envs, + [ + env_mask, + env.rng_state_wp, + self._default_root_pose, + self._default_root_velocity, + self._env_origins, + self._pose, + self._velocity, + self._pos_lo, + self._pos_hi, + self._rot_lo, + self._rot_hi, + self._vel_lin_lo, + self._vel_lin_hi, + self._vel_ang_lo, + self._vel_ang_hi, + ], env_mask, - env.rng_state_wp, - asset.data.default_root_pose.warp, - asset.data.default_root_vel.warp, - env.env_origins_wp, - reset_root_state_uniform._scratch_pose, - reset_root_state_uniform._scratch_vel, - reset_root_state_uniform._pos_lo, - reset_root_state_uniform._pos_hi, - reset_root_state_uniform._rot_lo, - reset_root_state_uniform._rot_hi, - reset_root_state_uniform._vel_lin_lo, - reset_root_state_uniform._vel_lin_hi, - reset_root_state_uniform._vel_ang_lo, - reset_root_state_uniform._vel_ang_hi, - ], - device=env.device, - ) + ) - asset.write_root_pose_to_sim_mask(root_pose=reset_root_state_uniform._scratch_pose, env_mask=env_mask) - asset.write_root_velocity_to_sim_mask(root_velocity=reset_root_state_uniform._scratch_vel, env_mask=env_mask) + self._asset.write_root_pose_to_sim_mask(root_pose=self._pose, env_mask=env_mask) + self._asset.write_root_velocity_to_sim_mask(root_velocity=self._velocity, env_mask=env_mask) # --------------------------------------------------------------------------- @@ -414,6 +578,11 @@ def _reset_joints_by_offset_kernel( vel_lo: float, vel_hi: float, ): + """Reset selected envs' selected joints to defaults plus uniform offsets + + [rad or m, depending on joint type], clamped to the soft position and + velocity limits. + """ env_id = wp.tid() if not env_mask[env_id]: return @@ -444,12 +613,12 @@ def _reset_joints_by_offset_kernel( def reset_joints_by_offset( - env, - env_mask: wp.array, + env: ManagerBasedEnv, + env_mask: wp.array(dtype=wp.bool), position_range: tuple[float, float], velocity_range: tuple[float, float], asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), -): +) -> None: """Warp-first reset of joint state by random offsets around defaults. This overrides the stable `isaaclab.envs.mdp.events.reset_joints_by_offset` when importing @@ -470,8 +639,10 @@ def reset_joints_by_offset( "Use ManagerBasedEnvWarp or ManagerBasedRLEnvWarp as the base environment." ) - wp.launch( - kernel=_reset_joints_by_offset_kernel, + position_range = (float(position_range[0]), float(position_range[1])) + velocity_range = (float(velocity_range[0]), float(velocity_range[1])) + env._warp_launch.launch( + _reset_joints_by_offset_kernel, dim=env.num_envs, inputs=[ env_mask, @@ -483,12 +654,19 @@ def reset_joints_by_offset( asset.data.joint_vel.warp, asset.data.soft_joint_pos_limits.warp, asset.data.soft_joint_vel_limits.warp, - float(position_range[0]), - float(position_range[1]), - float(velocity_range[0]), - float(velocity_range[1]), + position_range[0], + position_range[1], + velocity_range[0], + velocity_range[1], ], - device=env.device, + site=( + "reset_joints_by_offset", + asset, + asset_cfg.joint_ids_wp, + env_mask, + position_range, + velocity_range, + ), ) # Sync derived buffers (_previous_joint_vel, joint_acc) for reset envs. @@ -512,6 +690,10 @@ def _reset_joints_by_scale_kernel( vel_lo: float, vel_hi: float, ): + """Reset selected envs' selected joints to defaults scaled by uniform random + + factors, clamped to the soft position and velocity limits. + """ env_id = wp.tid() if not env_mask[env_id]: return @@ -540,12 +722,12 @@ def _reset_joints_by_scale_kernel( def reset_joints_by_scale( - env, - env_mask: wp.array, + env: ManagerBasedEnv, + env_mask: wp.array(dtype=wp.bool), position_range: tuple[float, float], velocity_range: tuple[float, float], asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), -): +) -> None: """Warp-first reset of joint state by scaling defaults with random factors.""" asset: Articulation = env.scene[asset_cfg.name] @@ -561,8 +743,10 @@ def reset_joints_by_scale( "Use ManagerBasedEnvWarp or ManagerBasedRLEnvWarp as the base environment." ) - wp.launch( - kernel=_reset_joints_by_scale_kernel, + position_range = (float(position_range[0]), float(position_range[1])) + velocity_range = (float(velocity_range[0]), float(velocity_range[1])) + env._warp_launch.launch( + _reset_joints_by_scale_kernel, dim=env.num_envs, inputs=[ env_mask, @@ -574,14 +758,108 @@ def reset_joints_by_scale( asset.data.joint_vel.warp, asset.data.soft_joint_pos_limits.warp, asset.data.soft_joint_vel_limits.warp, - float(position_range[0]), - float(position_range[1]), - float(velocity_range[0]), - float(velocity_range[1]), + position_range[0], + position_range[1], + velocity_range[0], + velocity_range[1], ], - device=env.device, + site=( + "reset_joints_by_scale", + asset, + asset_cfg.joint_ids_wp, + env_mask, + position_range, + velocity_range, + ), ) # Sync derived buffers (_previous_joint_vel, joint_acc) for reset envs. asset.write_joint_position_to_sim_mask(position=asset.data.joint_pos.warp, env_mask=env_mask) asset.write_joint_velocity_to_sim_mask(velocity=asset.data.joint_vel.warp, env_mask=env_mask) + + +# --------------------------------------------------------------------------- +# Startup-mode randomization (mask-signature adapters) +# --------------------------------------------------------------------------- +# +# The stable material/mass randomization terms already dispatch to the active +# physics backend (Newton included); only their calling convention differs — +# the warp EventManager invokes terms with a Warp env-mask while the stable +# class terms expect torch env indices. Both terms run in ``startup`` mode, +# once at construction and outside any captured path, so a host-side +# mask-to-ids conversion is acceptable. + + +def _mask_to_env_ids(env_mask: wp.array) -> torch.Tensor: + """Convert a Warp boolean env-mask to the torch index tensor the stable terms expect.""" + return torch.nonzero(wp.to_torch(env_mask), as_tuple=False).squeeze(-1) + + +class randomize_rigid_body_material(_StableRandomizeRigidBodyMaterial, _WarpManagerTermBase): + """Warp adapter for the stable, backend-dispatched material randomization term. + + Converts the warp event manager's env-mask calling convention to the stable + term's env-ids convention and delegates. Startup mode only. Inherits the + warp :class:`ManagerTermBase` so the warp managers accept it as a class term. + """ + + def __call__( + self, + env: ManagerBasedEnv, + env_mask: wp.array, + static_friction_range: tuple[float, float], + dynamic_friction_range: tuple[float, float], + restitution_range: tuple[float, float], + num_buckets: int, + asset_cfg: SceneEntityCfg, + make_consistent: bool = False, + ) -> None: + super().__call__( + env, + _mask_to_env_ids(env_mask), + static_friction_range, + dynamic_friction_range, + restitution_range, + num_buckets, + asset_cfg, + make_consistent, + ) + + def reset(self, env_mask: wp.array | None = None) -> None: + """Nothing to reset; materials are randomized once at startup.""" + return + + +class randomize_rigid_body_mass(_StableRandomizeRigidBodyMass, _WarpManagerTermBase): + """Warp adapter for the stable, backend-dispatched mass randomization term. + + Converts the warp event manager's env-mask calling convention to the stable + term's env-ids convention and delegates. Startup mode only. Inherits the + warp :class:`ManagerTermBase` so the warp managers accept it as a class term. + """ + + def __call__( + self, + env: ManagerBasedEnv, + env_mask: wp.array, + asset_cfg: SceneEntityCfg, + mass_distribution_params: tuple[float, float], + operation: str, + distribution: str = "uniform", + recompute_inertia: bool = True, + min_mass: float = 1e-6, + ) -> None: + super().__call__( + env, + _mask_to_env_ids(env_mask), + asset_cfg, + mass_distribution_params, + operation, + distribution, + recompute_inertia, + min_mass, + ) + + def reset(self, env_mask: wp.array | None = None) -> None: + """Nothing to reset; masses are randomized once at startup.""" + return diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/observations.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/observations.py index 808231c8faf6..6d7a0a0d1425 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/observations.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/observations.py @@ -36,10 +36,12 @@ record_shape, ) from isaaclab_experimental.managers import SceneEntityCfg +from isaaclab_experimental.utils.warp import WarpCapturable if TYPE_CHECKING: from isaaclab.assets import Articulation from isaaclab.envs import ManagerBasedEnv + from isaaclab.sensors import RayCaster # --------------------------------------------------------------------------- @@ -52,6 +54,7 @@ def _vec3_to_out3_kernel( src: wp.array(dtype=wp.vec3f), out: wp.array(dtype=wp.float32, ndim=2), ): + """Scatter a vec3 array into three float32 columns of a term's ``out`` block.""" env_id = wp.tid() v = src[env_id] out[env_id, 0] = v[0] @@ -65,6 +68,10 @@ def _joint_gather_kernel( joint_ids: wp.array(dtype=wp.int32), out: wp.array(dtype=wp.float32, ndim=2), ): + """Gather selected joints' values into contiguous term columns. + + Launch with dim=(num_envs, num_selected_joints). + """ env_id, k = wp.tid() j = joint_ids[k] out[env_id, k] = src[env_id, j] @@ -80,6 +87,7 @@ def _base_pos_z_kernel( root_pos_w: wp.array(dtype=wp.vec3f), out: wp.array(dtype=wp.float32, ndim=2), ): + """Write the root height above the world origin [m] into the term's single column.""" env_id = wp.tid() out[env_id, 0] = root_pos_w[env_id][2] @@ -90,11 +98,11 @@ def _base_pos_z_kernel( def base_pos_z(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Root height in the simulation world frame.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_pos_z_kernel, + env._warp_launch.launch( + _base_pos_z_kernel, dim=env.num_envs, inputs=[asset.data.root_pos_w.warp, out], - device=env.device, + site=out, ) @@ -112,6 +120,10 @@ def _base_lin_vel_kernel( root_vel_w: wp.array(dtype=wp.spatial_vectorf), out: wp.array(dtype=wp.float32, ndim=2), ): + """Root linear velocity [m/s] in the body frame, derived inline from the root + + pose and CoM velocity (Tier-1 access; avoids non-capturable lazy buffers). + """ i = wp.tid() v = body_lin_vel_from_root(root_pose_w[i], root_vel_w[i]) out[i, 0] = v[0] @@ -125,11 +137,11 @@ def _base_lin_vel_kernel( def base_lin_vel(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Root linear velocity in the asset's root frame.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_lin_vel_kernel, + env._warp_launch.launch( + _base_lin_vel_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.root_com_vel_w.warp, out], - device=env.device, + site=out, ) @@ -139,6 +151,10 @@ def _base_ang_vel_kernel( root_vel_w: wp.array(dtype=wp.spatial_vectorf), out: wp.array(dtype=wp.float32, ndim=2), ): + """Root angular velocity [rad/s] in the body frame, derived inline from the root + + pose and CoM velocity (Tier-1 access; avoids non-capturable lazy buffers). + """ i = wp.tid() v = body_ang_vel_from_root(root_pose_w[i], root_vel_w[i]) out[i, 0] = v[0] @@ -152,11 +168,11 @@ def _base_ang_vel_kernel( def base_ang_vel(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Root angular velocity in the asset's root frame.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_ang_vel_kernel, + env._warp_launch.launch( + _base_ang_vel_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.root_com_vel_w.warp, out], - device=env.device, + site=out, ) @@ -166,6 +182,7 @@ def _projected_gravity_kernel( gravity_w: wp.array(dtype=wp.vec3f), out: wp.array(dtype=wp.float32, ndim=2), ): + """Unit gravity direction rotated into the body frame (flat pose -> (0, 0, -1)).""" i = wp.tid() g = rotate_vec_to_body_frame(wp.normalize(gravity_w[i]), root_pose_w[i]) out[i, 0] = g[0] @@ -179,11 +196,11 @@ def _projected_gravity_kernel( def projected_gravity(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Gravity projection on the asset's root frame.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_projected_gravity_kernel, + env._warp_launch.launch( + _projected_gravity_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.GRAVITY_VEC_W.warp, out], - device=env.device, + site=out, ) @@ -204,11 +221,11 @@ def joint_pos(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntity "SceneEntityCfg.joint_ids_wp is required for subset joint observations in Warp-first observations. " "Pass `asset_cfg` via term cfg params so it is resolved at manager init." ) - wp.launch( - kernel=_joint_gather_kernel, + env._warp_launch.launch( + _joint_gather_kernel, dim=(env.num_envs, out.shape[1]), inputs=[asset.data.joint_pos.warp, joint_ids_wp, out], - device=env.device, + site=("joint_pos", out), ) @@ -219,6 +236,10 @@ def _joint_rel_gather_kernel( joint_ids: wp.array(dtype=wp.int32), out: wp.array(dtype=wp.float32, ndim=2), ): + """Gather selected joints' values relative to their per-env defaults into + + contiguous term columns. Launch with dim=(num_envs, num_selected_joints). + """ env_id, k = wp.tid() j = joint_ids[k] out[env_id, k] = values[env_id, j] - defaults[env_id, j] @@ -240,11 +261,11 @@ def joint_pos_rel(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEn "SceneEntityCfg.joint_ids_wp is required for subset joint observations in Warp-first observations. " "Pass `asset_cfg` via term cfg params so it is resolved at manager init." ) - wp.launch( - kernel=_joint_rel_gather_kernel, + env._warp_launch.launch( + _joint_rel_gather_kernel, dim=(env.num_envs, out.shape[1]), inputs=[asset.data.joint_pos.warp, asset.data.default_joint_pos.warp, joint_ids_wp, out], - device=env.device, + site=("joint_pos_rel", out), ) @@ -255,6 +276,10 @@ def _joint_pos_limit_normalized_kernel( joint_ids: wp.array(dtype=wp.int32), out: wp.array(dtype=wp.float32, ndim=2), ): + """Selected joints' positions normalized to [-1, 1] within their soft limits. + + Launch with dim=(num_envs, num_selected_joints). + """ env_id, k = wp.tid() j = joint_ids[k] pos = joint_pos[env_id, j] @@ -274,11 +299,11 @@ def joint_pos_limit_normalized(env: ManagerBasedEnv, out, asset_cfg: SceneEntity "SceneEntityCfg.joint_ids_wp is required for subset joint observations in Warp-first observations. " "Pass `asset_cfg` via term cfg params so it is resolved at manager init." ) - wp.launch( - kernel=_joint_pos_limit_normalized_kernel, + env._warp_launch.launch( + _joint_pos_limit_normalized_kernel, dim=(env.num_envs, out.shape[1]), inputs=[asset.data.joint_pos.warp, asset.data.soft_joint_pos_limits.warp, joint_ids_wp, out], - device=env.device, + site=out, ) @@ -294,11 +319,11 @@ def joint_vel(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntity "SceneEntityCfg.joint_ids_wp is required for subset joint observations in Warp-first observations. " "Pass `asset_cfg` via term cfg params so it is resolved at manager init." ) - wp.launch( - kernel=_joint_gather_kernel, + env._warp_launch.launch( + _joint_gather_kernel, dim=(env.num_envs, out.shape[1]), inputs=[asset.data.joint_vel.warp, joint_ids_wp, out], - device=env.device, + site=("joint_vel", out), ) @@ -318,11 +343,11 @@ def joint_vel_rel(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEn "SceneEntityCfg.joint_ids_wp is required for subset joint observations in Warp-first observations. " "Pass `asset_cfg` via term cfg params so it is resolved at manager init." ) - wp.launch( - kernel=_joint_rel_gather_kernel, + env._warp_launch.launch( + _joint_rel_gather_kernel, dim=(env.num_envs, out.shape[1]), inputs=[asset.data.joint_vel.warp, asset.data.default_joint_vel.warp, joint_ids_wp, out], - device=env.device, + site=("joint_vel_rel", out), ) @@ -353,17 +378,91 @@ def generated_commands(env: ManagerBasedEnv, out, command_name: str) -> None: """The generated command from the command manager. Writes into ``out``. Warp-first override of :func:`isaaclab.envs.mdp.observations.generated_commands`. - Uses ``wp.from_torch`` to create a zero-copy warp view of the command tensor on first call. + Reads the command manager's pointer-stable Warp storage directly. + """ + wp.copy(out, env.command_manager.get_command_wp(command_name)) + + +""" +Sensors. +""" + + +@wp.kernel +def _body_incoming_wrench_kernel( + force: wp.array(dtype=wp.vec3f, ndim=2), + torque: wp.array(dtype=wp.vec3f, ndim=2), + body_ids: wp.array(dtype=wp.int32), + out: wp.array(dtype=wp.float32, ndim=2), +): + """Gather selected bodies' incoming force [N] and torque [N·m] into per-body + + ``[fx, fy, fz, tx, ty, tz]`` column blocks. + """ + env_id = wp.tid() + for k in range(body_ids.shape[0]): + b = body_ids[k] + f = force[env_id, b] + t = torque[env_id, b] + out[env_id, k * 6 + 0] = f[0] + out[env_id, k * 6 + 1] = f[1] + out[env_id, k * 6 + 2] = f[2] + out[env_id, k * 6 + 3] = t[0] + out[env_id, k * 6 + 4] = t[1] + out[env_id, k * 6 + 5] = t[2] + + +@generic_io_descriptor_warp(out_dim="body:6", observation_type="BodyState", on_inspect=[record_shape, record_dtype]) +def body_incoming_wrench(env: ManagerBasedEnv, out, sensor_cfg: SceneEntityCfg) -> None: + """Incoming spatial wrench [N, N·m] on selected bodies, laid out per body as ``[fx, fy, fz, tx, ty, tz]``. + + Warp-first override of :func:`isaaclab.envs.mdp.observations.body_incoming_wrench`. Reads the + Newton joint-wrench sensor and gathers the force/torque of the bodies selected by ``sensor_cfg`` + into ``out`` (shape ``(num_envs, 6 * num_selected_bodies)``). """ - # TODO(warp-migration): Cross-manager access (observation → command). Replace with direct - # warp getter once all managers are guaranteed to be warp-native. - fn = generated_commands - if not getattr(fn, "_is_warmed_up", False) or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - if isinstance(cmd, wp.array): - fn._cmd_wp = cmd - else: - fn._cmd_wp = wp.from_torch(cmd) - fn._cmd_name = command_name - fn._is_warmed_up = True - wp.copy(out, fn._cmd_wp) + sensor = env.scene.sensors[sensor_cfg.name] + if sensor.data.force is None or sensor.data.torque is None: + raise RuntimeError("Joint wrench sensor data is not initialized. Call sim.reset() before reading observations.") + wp.launch( + kernel=_body_incoming_wrench_kernel, + dim=env.num_envs, + inputs=[sensor.data.force.warp, sensor.data.torque.warp, sensor_cfg.body_ids_wp, out], + device=env.device, + ) + + +@wp.kernel +def _height_scan_kernel( + pos_w: wp.array(dtype=wp.vec3f), + ray_hits_w: wp.array2d(dtype=wp.vec3f), + offset: wp.float32, + out: wp.array2d(dtype=wp.float32), +): + """Height of the scan frame above each ray hit [m]: sensor_z - hit_z - offset. + + Launch with dim=(num_envs, num_rays). Missed hits carry ``inf`` and produce + ``-inf`` heights, matching the stable term (handled by the term's clip). + """ + env_id, ray_id = wp.tid() + out[env_id, ray_id] = pos_w[env_id][2] - ray_hits_w[env_id, ray_id][2] - offset + + +# Sensor reads go through the lazy-update path, whose host-side timestamp bookkeeping +# decides when rays are re-cast and has not been audited for graph capture. +@WarpCapturable(False, reason="sensor lazy-update host bookkeeping is not capture-audited (Part 3 scope)") +@generic_io_descriptor_warp(units="m", out_dim="sensor:rays", observation_type="SensorState", on_inspect=[record_shape]) +def height_scan(env: ManagerBasedEnv, out, sensor_cfg: SceneEntityCfg, offset: float = 0.5) -> None: + """Height scan from the given sensor w.r.t. the sensor's frame [m]. Writes into ``out``. + + Warp-first override of :func:`isaaclab.envs.mdp.observations.height_scan`. + The provided offset (Defaults to 0.5) is subtracted from the returned values. + Missed rays carry ``inf`` hit positions, matching the stable term's semantics + (the resulting ``-inf`` heights are handled by the term's ``clip`` setting). + """ + sensor: RayCaster = env.scene.sensors[sensor_cfg.name] + wp.launch( + kernel=_height_scan_kernel, + dim=(env.num_envs, sensor.num_rays), + inputs=[sensor.data.pos_w.warp, sensor.data.ray_hits_w.warp, float(offset), out], + device=env.device, + ) diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/rewards.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/rewards.py index 338d27c99c5e..da8e28935b7d 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/rewards.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/rewards.py @@ -38,33 +38,35 @@ @wp.kernel def _is_alive_kernel(terminated: wp.array(dtype=wp.bool), out: wp.array(dtype=wp.float32)): + """1.0 for envs still running, 0.0 for terminated ones.""" i = wp.tid() out[i] = wp.where(terminated[i], 0.0, 1.0) def is_alive(env: ManagerBasedRLEnv, out: wp.array(dtype=wp.float32)) -> None: """Reward for being alive. Writes into ``out`` (shape: (num_envs,)).""" - wp.launch( - kernel=_is_alive_kernel, + env._warp_launch.launch( + _is_alive_kernel, dim=env.num_envs, inputs=[env.termination_manager.terminated_wp, out], - device=env.device, + site=out, ) @wp.kernel def _is_terminated_kernel(terminated: wp.array(dtype=wp.bool), out: wp.array(dtype=wp.float32)): + """1.0 for envs terminated by a non-timeout cause, else 0.0.""" i = wp.tid() out[i] = wp.where(terminated[i], 1.0, 0.0) def is_terminated(env: ManagerBasedRLEnv, out) -> None: """Penalize terminated episodes. Writes into ``out``.""" - wp.launch( - kernel=_is_terminated_kernel, + env._warp_launch.launch( + _is_terminated_kernel, dim=env.num_envs, inputs=[env.termination_manager.terminated_wp, out], - device=env.device, + site=out, ) @@ -87,6 +89,10 @@ def _lin_vel_z_l2_kernel( root_vel_w: wp.array(dtype=wp.spatial_vectorf), out: wp.array(dtype=wp.float32), ): + """Squared body-frame vertical velocity [(m/s)^2] per env, derived inline from + + the root state. + """ i = wp.tid() vz = body_lin_vel_from_root(root_pose_w[i], root_vel_w[i])[2] out[i] = vz * vz @@ -95,11 +101,11 @@ def _lin_vel_z_l2_kernel( def lin_vel_z_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize z-axis base linear velocity using L2 squared kernel.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_lin_vel_z_l2_kernel, + env._warp_launch.launch( + _lin_vel_z_l2_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.root_com_vel_w.warp, out], - device=env.device, + site=out, ) @@ -109,6 +115,7 @@ def _ang_vel_xy_l2_kernel( root_vel_w: wp.array(dtype=wp.spatial_vectorf), out: wp.array(dtype=wp.float32), ): + """Sum of squared body-frame roll/pitch rates [(rad/s)^2] per env.""" i = wp.tid() v = body_ang_vel_from_root(root_pose_w[i], root_vel_w[i]) out[i] = v[0] * v[0] + v[1] * v[1] @@ -117,11 +124,11 @@ def _ang_vel_xy_l2_kernel( def ang_vel_xy_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize xy-axis base angular velocity using L2 squared kernel.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_ang_vel_xy_l2_kernel, + env._warp_launch.launch( + _ang_vel_xy_l2_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.root_com_vel_w.warp, out], - device=env.device, + site=out, ) @@ -131,6 +138,10 @@ def _flat_orientation_l2_kernel( gravity_w: wp.array(dtype=wp.vec3f), out: wp.array(dtype=wp.float32), ): + """Sum of squared xy components of body-frame projected gravity (zero when the + + base is perfectly flat). + """ # ``gravity_w`` is per-env and may carry magnitude (Newton, m/s^2), so index # per env and normalize before projecting. i = wp.tid() @@ -141,11 +152,11 @@ def _flat_orientation_l2_kernel( def flat_orientation_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize non-flat base orientation using L2 squared kernel.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_flat_orientation_l2_kernel, + env._warp_launch.launch( + _flat_orientation_l2_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.GRAVITY_VEC_W.warp, out], - device=env.device, + site=out, ) @@ -160,6 +171,7 @@ def flat_orientation_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = def _sum_sq_masked_kernel( x: wp.array(dtype=wp.float32, ndim=2), joint_mask: wp.array(dtype=wp.bool), out: wp.array(dtype=wp.float32) ): + """Per-env sum of squares over mask-selected joint columns.""" i = wp.tid() s = float(0.0) for j in range(x.shape[1]): @@ -171,11 +183,11 @@ def _sum_sq_masked_kernel( def joint_torques_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize joint torques applied on the articulation using L2 squared kernel.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_sum_sq_masked_kernel, + env._warp_launch.launch( + _sum_sq_masked_kernel, dim=env.num_envs, inputs=[asset.data.applied_torque.warp, asset_cfg.joint_mask, out], - device=env.device, + site=("joint_torques_l2", out), ) @@ -184,6 +196,7 @@ def joint_torques_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = Sc def _sum_abs_masked_kernel( x: wp.array(dtype=wp.float32, ndim=2), joint_mask: wp.array(dtype=wp.bool), out: wp.array(dtype=wp.float32) ): + """Per-env L1 sum over mask-selected joint columns.""" i = wp.tid() s = float(0.0) for j in range(x.shape[1]): @@ -195,33 +208,33 @@ def _sum_abs_masked_kernel( def joint_vel_l1(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg) -> None: """Penalize joint velocities on the articulation using an L1-kernel. Writes into ``out``.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_sum_abs_masked_kernel, + env._warp_launch.launch( + _sum_abs_masked_kernel, dim=env.num_envs, inputs=[asset.data.joint_vel.warp, asset_cfg.joint_mask, out], - device=env.device, + site=out, ) def joint_vel_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize joint velocities on the articulation using L2 squared kernel.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_sum_sq_masked_kernel, + env._warp_launch.launch( + _sum_sq_masked_kernel, dim=env.num_envs, inputs=[asset.data.joint_vel.warp, asset_cfg.joint_mask, out], - device=env.device, + site=("joint_vel_l2", out), ) def joint_acc_l2(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize joint accelerations on the articulation using L2 squared kernel.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_sum_sq_masked_kernel, + env._warp_launch.launch( + _sum_sq_masked_kernel, dim=env.num_envs, inputs=[asset.data.joint_acc.warp, asset_cfg.joint_mask, out], - device=env.device, + site=("joint_acc_l2", out), ) @@ -233,6 +246,7 @@ def _sum_abs_diff_masked_kernel( joint_mask: wp.array(dtype=wp.bool), out: wp.array(dtype=wp.float32), ): + """Per-env L1 sum of ``a - b`` over mask-selected joint columns.""" i = wp.tid() s = float(0.0) for j in range(a.shape[1]): @@ -244,11 +258,11 @@ def _sum_abs_diff_masked_kernel( def joint_deviation_l1(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize joint positions that deviate from the default one.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_sum_abs_diff_masked_kernel, + env._warp_launch.launch( + _sum_abs_diff_masked_kernel, dim=env.num_envs, inputs=[asset.data.joint_pos.warp, asset.data.default_joint_pos.warp, asset_cfg.joint_mask, out], - device=env.device, + site=out, ) @@ -260,6 +274,11 @@ def _joint_pos_limits_kernel( joint_mask: wp.array(dtype=wp.bool), out: wp.array(dtype=wp.float32), ): + """Per-env sum of soft-limit violations [rad or m, depending on joint type]: + + distance each selected joint sits below its lower or above its upper soft + limit (zero inside the limits). + """ i = wp.tid() s = float(0.0) for j in range(joint_pos.shape[1]): @@ -282,11 +301,11 @@ def _joint_pos_limits_kernel( def joint_pos_limits(env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Penalize joint positions if they cross the soft limits.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_joint_pos_limits_kernel, + env._warp_launch.launch( + _joint_pos_limits_kernel, dim=env.num_envs, inputs=[asset.data.joint_pos.warp, asset.data.soft_joint_pos_limits.warp, asset_cfg.joint_mask, out], - device=env.device, + site=out, ) @@ -302,6 +321,7 @@ def _sum_sq_diff_2d_kernel( b: wp.array(dtype=wp.float32, ndim=2), out: wp.array(dtype=wp.float32), ): + """Per-env sum of squared differences across all columns (action-rate penalty).""" i = wp.tid() s = float(0.0) for j in range(a.shape[1]): @@ -312,17 +332,18 @@ def _sum_sq_diff_2d_kernel( def action_rate_l2(env: ManagerBasedRLEnv, out) -> None: """Penalize the rate of change of the actions using L2 squared kernel.""" - wp.launch( - kernel=_sum_sq_diff_2d_kernel, + env._warp_launch.launch( + _sum_sq_diff_2d_kernel, dim=env.num_envs, inputs=[env.action_manager.action, env.action_manager.prev_action, out], - device=env.device, + site=out, ) # TODO(warp-migration): Revisit 2D kernel + wp.atomic_add vs 1D inner loop. @wp.kernel def _sum_sq_2d_kernel(x: wp.array(dtype=wp.float32, ndim=2), out: wp.array(dtype=wp.float32)): + """Per-env sum of squares across all columns (action-magnitude penalty).""" i = wp.tid() s = float(0.0) for j in range(x.shape[1]): @@ -332,11 +353,11 @@ def _sum_sq_2d_kernel(x: wp.array(dtype=wp.float32, ndim=2), out: wp.array(dtype def action_l2(env: ManagerBasedRLEnv, out) -> None: """Penalize the actions using L2 squared kernel.""" - wp.launch( - kernel=_sum_sq_2d_kernel, + env._warp_launch.launch( + _sum_sq_2d_kernel, dim=env.num_envs, inputs=[env.action_manager.action, out], - device=env.device, + site=out, ) @@ -374,11 +395,11 @@ def undesired_contacts(env: ManagerBasedRLEnv, out, threshold: float, sensor_cfg Warp-first override of :func:`isaaclab.envs.mdp.rewards.undesired_contacts`. """ contact_sensor = env.scene.sensors[sensor_cfg.name] - wp.launch( - kernel=_undesired_contacts_kernel, + env._warp_launch.launch( + _undesired_contacts_kernel, dim=env.num_envs, inputs=[contact_sensor.data.net_forces_w_history.warp, sensor_cfg.body_ids_wp, threshold, out], - device=env.device, + site=(out, float(threshold)), ) @@ -395,6 +416,10 @@ def _track_lin_vel_xy_exp_kernel( std_sq_inv: float, out: wp.array(dtype=wp.float32), ): + """Exponential tracking shaping ``exp(-err^2 / std^2)`` of planar body-frame + + linear velocity vs the command's (vx, vy). + """ i = wp.tid() v = body_lin_vel_from_root(root_pose_w[i], root_vel_w[i]) dx = command[i, 0] - v[0] @@ -415,28 +440,18 @@ def track_lin_vel_xy_exp( Warp-first override of :func:`isaaclab.envs.mdp.rewards.track_lin_vel_xy_exp`. """ asset: Articulation = env.scene[asset_cfg.name] - # cache the warp view of the command tensor on first call (zero-copy) - # TODO(warp-migration): Cross-manager access (reward → command). Replace with direct - # warp getter once all managers are guaranteed to be warp-native. - if not getattr(track_lin_vel_xy_exp, "_is_warmed_up", False) or track_lin_vel_xy_exp._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - if isinstance(cmd, wp.array): - track_lin_vel_xy_exp._cmd_wp = cmd - else: - track_lin_vel_xy_exp._cmd_wp = wp.from_torch(cmd) - track_lin_vel_xy_exp._cmd_name = command_name - track_lin_vel_xy_exp._is_warmed_up = True - wp.launch( - kernel=_track_lin_vel_xy_exp_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _track_lin_vel_xy_exp_kernel, dim=env.num_envs, inputs=[ asset.data.root_link_pose_w.warp, asset.data.root_com_vel_w.warp, - track_lin_vel_xy_exp._cmd_wp, + command, 1.0 / (std * std), out, ], - device=env.device, + site=(out, command_name, float(std)), ) @@ -449,6 +464,10 @@ def _track_ang_vel_z_exp_kernel( std_sq_inv: float, out: wp.array(dtype=wp.float32), ): + """Exponential tracking shaping ``exp(-err^2 / std^2)`` of body-frame yaw rate + + vs the command's wz. + """ i = wp.tid() dz = command[i, cmd_col] - body_ang_vel_from_root(root_pose_w[i], root_vel_w[i])[2] out[i] = wp.exp(-dz * dz * std_sq_inv) @@ -466,26 +485,17 @@ def track_ang_vel_z_exp( Warp-first override of :func:`isaaclab.envs.mdp.rewards.track_ang_vel_z_exp`. """ asset: Articulation = env.scene[asset_cfg.name] - # TODO(warp-migration): Cross-manager access (reward → command). Replace with direct - # warp getter once all managers are guaranteed to be warp-native. - if not getattr(track_ang_vel_z_exp, "_is_warmed_up", False) or track_ang_vel_z_exp._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - if isinstance(cmd, wp.array): - track_ang_vel_z_exp._cmd_wp = cmd - else: - track_ang_vel_z_exp._cmd_wp = wp.from_torch(cmd) - track_ang_vel_z_exp._cmd_name = command_name - track_ang_vel_z_exp._is_warmed_up = True - wp.launch( - kernel=_track_ang_vel_z_exp_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _track_ang_vel_z_exp_kernel, dim=env.num_envs, inputs=[ asset.data.root_link_pose_w.warp, asset.data.root_com_vel_w.warp, - track_ang_vel_z_exp._cmd_wp, + command, 2, 1.0 / (std * std), out, ], - device=env.device, + site=(out, command_name, float(std)), ) diff --git a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/terminations.py b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/terminations.py index d309a70538bb..9e586d3a52f4 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/terminations.py +++ b/source/isaaclab_experimental/isaaclab_experimental/envs/mdp/terminations.py @@ -41,11 +41,11 @@ def _time_out_kernel( def time_out(env: ManagerBasedRLEnv, out) -> None: """Terminate the episode when episode length exceeds the maximum episode length.""" - wp.launch( - kernel=_time_out_kernel, + env._warp_launch.launch( + _time_out_kernel, dim=env.num_envs, inputs=[env._episode_length_buf_wp, env.max_episode_length, out], - device=env.device, + site=(out, int(env.max_episode_length)), ) @@ -69,11 +69,11 @@ def root_height_below_minimum( ) -> None: """Terminate when the asset's root height is below the minimum height.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_root_height_below_min_kernel, + env._warp_launch.launch( + _root_height_below_min_kernel, dim=env.num_envs, inputs=[asset.data.root_pos_w.warp, minimum_height, out], - device=env.device, + site=(out, float(minimum_height)), ) @@ -113,11 +113,11 @@ def joint_pos_out_of_manual_limit( f"joint_mask length ({asset_cfg.joint_mask.shape[0]}) does not match " f"joint_pos dim ({asset.data.joint_pos.warp.shape[1]}) for asset '{asset_cfg.name}'." ) - wp.launch( - kernel=_joint_pos_out_of_manual_limit_kernel, + env._warp_launch.launch( + _joint_pos_out_of_manual_limit_kernel, dim=(env.num_envs, asset.data.joint_pos.warp.shape[1]), inputs=[asset.data.joint_pos.warp, asset_cfg.joint_mask, bounds[0], bounds[1], out], - device=env.device, + site=(out, float(bounds[0]), float(bounds[1])), ) @@ -152,9 +152,9 @@ def illegal_contact(env: ManagerBasedRLEnv, out, threshold: float, sensor_cfg: S Warp-first override of :func:`isaaclab.envs.mdp.terminations.illegal_contact`. """ contact_sensor = env.scene.sensors[sensor_cfg.name] - wp.launch( - kernel=_illegal_contact_kernel, + env._warp_launch.launch( + _illegal_contact_kernel, dim=env.num_envs, inputs=[contact_sensor.data.net_forces_w_history.warp, sensor_cfg.body_ids_wp, threshold, out], - device=env.device, + site=(out, float(threshold)), ) diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/__init__.pyi b/source/isaaclab_experimental/isaaclab_experimental/managers/__init__.pyi index 18431a745b31..29c0dfa1b494 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/__init__.pyi +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/__init__.pyi @@ -8,6 +8,7 @@ __all__ = [ "ActionTerm", "CommandManager", "CommandTerm", + "CurriculumManager", "EventManager", "ManagerBase", "ManagerTermBase", @@ -31,6 +32,7 @@ from isaaclab.managers import * # noqa: F401, F403 from .action_manager import ActionManager, ActionTerm from .command_manager import CommandManager, CommandTerm +from .curriculum_manager import CurriculumManager from .event_manager import EventManager from .manager_base import ManagerBase, ManagerTermBase from .manager_term_cfg import ( diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/action_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/action_manager.py index 672af82bc8ab..57cb790a2ac3 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/action_manager.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/action_manager.py @@ -20,9 +20,9 @@ from isaaclab.assets import AssetBase from isaaclab.envs.utils.io_descriptors import GenericActionIODescriptor +from isaaclab.managers.manager_term_cfg import ActionTermCfg from .manager_base import ManagerBase, ManagerTermBase -from .manager_term_cfg import ActionTermCfg if TYPE_CHECKING: from isaaclab.envs import ManagerBasedEnv @@ -373,32 +373,21 @@ def reset( Returns: An empty dictionary. """ - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - if wp.get_device().is_capturing: - raise RuntimeError( - "ActionManager.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) # reset the action history - if env_mask is None: - self._prev_action.fill_(0.0) - self._action.fill_(0.0) - else: - wp.launch( - kernel=_zero_masked_2d, - dim=(self.num_envs, self.total_action_dim), - inputs=[env_mask, self._prev_action], - device=self.device, - ) - wp.launch( - kernel=_zero_masked_2d, - dim=(self.num_envs, self.total_action_dim), - inputs=[env_mask, self._action], - device=self.device, - ) + self._env._warp_launch.launch( + kernel=_zero_masked_2d, + dim=(self.num_envs, self.total_action_dim), + inputs=[env_mask, self._prev_action], + site=("action_manager", self, "reset_previous", env_mask), + ) + self._env._warp_launch.launch( + kernel=_zero_masked_2d, + dim=(self.num_envs, self.total_action_dim), + inputs=[env_mask, self._action], + site=("action_manager", self, "reset_current", env_mask), + ) # reset all action terms for term in self._terms.values(): @@ -484,6 +473,7 @@ def _prepare_terms(self): ) # create the action term term = term_cfg.class_type(term_cfg, self._env) + self._register_term_capturability(term_cfg.class_type) # sanity check if term is valid type if not isinstance(term, ActionTerm): raise TypeError(f"Returned object for the term '{term_name}' is not of type ActionType.") diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/command_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/command_manager.py index 8b4e8f83dbe7..b5a80a16bb5d 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/command_manager.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/command_manager.py @@ -8,7 +8,6 @@ from __future__ import annotations import inspect -import weakref from abc import abstractmethod from collections.abc import Sequence from typing import TYPE_CHECKING @@ -17,10 +16,11 @@ import warp as wp from prettytable import PrettyTable +from isaaclab.managers.manager_term_cfg import CommandTermCfg + from isaaclab_experimental.utils.warp.kernels import compute_reset_scale, count_masked from .manager_base import ManagerBase, ManagerTermBase -from .manager_term_cfg import CommandTermCfg # import omni.kit.app @@ -36,6 +36,11 @@ def _sum_and_zero_masked( metric: wp.array(dtype=wp.float32), out_mean: wp.array(dtype=wp.float32), ): + """Fold selected envs' episode metric into a running mean and zero it for the next episode. + + ``scale[0]`` carries the 1/count normalization; the atomic add reduces across all + selected envs into ``out_mean[0]``. + """ env_id = wp.tid() if mask[env_id]: wp.atomic_add(out_mean, 0, metric[env_id] * scale[0]) @@ -44,6 +49,7 @@ def _sum_and_zero_masked( @wp.kernel def _zero_counter_masked(mask: wp.array(dtype=wp.bool), counter: wp.array(dtype=wp.int32)): + """Zero the per-env resample counter for envs selected by ``mask``.""" env_id = wp.tid() if mask[env_id]: counter[env_id] = 0 @@ -55,6 +61,7 @@ def _step_time_left_and_build_resample_mask( dt: wp.float32, out_mask: wp.array(dtype=wp.bool), ): + """Advance each env's resample countdown by ``dt`` [s] and flag expired envs in ``out_mask``.""" env_id = wp.tid() t = time_left[env_id] - dt time_left[env_id] = t @@ -70,6 +77,11 @@ def _resample_time_left_and_increment_counter( lower: wp.float32, upper: wp.float32, ): + """Draw a new resampling interval [s] for flagged envs and count the resample. + + Uses the per-env device RNG state with write-back so the random sequence + advances across CUDA-graph replays. + """ env_id = wp.tid() if mask[env_id]: s = rng_state[env_id] @@ -124,9 +136,11 @@ def __init__(self, cfg: CommandTermCfg, env: ManagerBasedRLEnv): def __del__(self): """Unsubscribe from the callbacks.""" - if self._debug_vis_handle: - self._debug_vis_handle.unsubscribe() - self._debug_vis_handle = None + env = getattr(self, "_env", None) + sim = getattr(env, "sim", None) + registry = getattr(sim, "vis_marker_registry", None) + if registry is not None: + registry.clear_debug_vis_callback(self) """ Properties @@ -138,6 +152,21 @@ def command(self) -> torch.Tensor | wp.array: """The command tensor. Shape is (num_envs, command_dim).""" raise NotImplementedError + @property + def command_wp(self) -> wp.array(dtype=wp.float32, ndim=2): + """Pointer-stable Warp command storage with shape ``(num_envs, command_dim)``. + + Existing custom terms that only implement :attr:`command` receive a + lazily cached zero-copy Warp view. Warp-native terms may override this + property to return their owner-held array directly. + """ + command = self.command + if isinstance(command, wp.array): + return command + if not hasattr(self, "_command_wp_compat"): + self._command_wp_compat = wp.from_torch(command, dtype=wp.float32) + return self._command_wp_compat + @property def has_debug_vis_implementation(self) -> bool: """Whether the command generator has a debug visualization implemented.""" @@ -171,25 +200,10 @@ def set_debug_vis(self, debug_vis: bool) -> bool: self._set_debug_vis_impl(debug_vis) # toggle debug visualization handles if debug_vis: - # only enable debug_vis if omniverse is available - from isaaclab.sim.simulation_context import SimulationContext - - sim_context = SimulationContext.instance() - if not sim_context.has_omniverse_visualizer(): - return False - # create a subscriber for the post update event if it doesn't exist if self._debug_vis_handle is None: - import omni.kit.app - - app_interface = omni.kit.app.get_app_interface() - self._debug_vis_handle = app_interface.get_post_update_event_stream().create_subscription_to_pop( - lambda event, obj=weakref.proxy(self): obj._debug_vis_callback(event) - ) + self._debug_vis_handle = self._env.sim.vis_marker_registry.add_debug_vis_callback(self) else: - # remove the subscriber if it exists - if self._debug_vis_handle is not None: - self._debug_vis_handle.unsubscribe() - self._debug_vis_handle = None + self._env.sim.vis_marker_registry.clear_debug_vis_callback(self) # return success return True @@ -213,43 +227,41 @@ def reset( Returns: A dictionary containing the information to log under the "{name}" key. """ - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - if wp.get_device().is_capturing: - raise RuntimeError( - "CommandTerm.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) # compute selected count and reset scale self._reset_count_wp.zero_() self._reset_scale_wp.zero_() - wp.launch(kernel=count_masked, dim=self.num_envs, inputs=[env_mask, self._reset_count_wp], device=self.device) - wp.launch( + self._env._warp_launch.launch( + kernel=count_masked, + dim=self.num_envs, + inputs=[env_mask, self._reset_count_wp], + site=("command_term", self, "reset_count", env_mask), + ) + self._env._warp_launch.launch( kernel=compute_reset_scale, dim=1, inputs=[self._reset_count_wp, 1.0, self._reset_scale_wp], - device=self.device, + site=("command_term", self, "reset_scale"), ) # update pre-allocated reset extras and clear selected metric rows for metric_name, metric_value_wp in self.metrics.items(): out_mean_wp = self._reset_metric_mean_wp[metric_name] out_mean_wp.zero_() - wp.launch( + self._env._warp_launch.launch( kernel=_sum_and_zero_masked, dim=self.num_envs, inputs=[env_mask, self._reset_scale_wp, metric_value_wp, out_mean_wp], - device=self.device, + site=("command_term", self, "reset_metric", metric_name, env_mask), ) # set the command counter to zero - wp.launch( + self._env._warp_launch.launch( kernel=_zero_counter_masked, dim=self.num_envs, inputs=[env_mask, self.command_counter_wp], - device=self.device, + site=("command_term", self, "reset_counter", env_mask), ) # resample the command self._resample(env_mask=env_mask) @@ -288,11 +300,11 @@ def compute(self, dt: float): # update the metrics based on current state self._update_metrics() # reduce the time left before resampling and build resample mask - wp.launch( + self._env._warp_launch.launch( kernel=_step_time_left_and_build_resample_mask, dim=self.num_envs, inputs=[self.time_left_wp, float(dt), self._resample_mask_wp], - device=self.device, + site=("command_term", self, "step_time_left", float(dt)), ) # resample masked envs self._resample(env_mask=self._resample_mask_wp) @@ -320,7 +332,9 @@ def _resample(self, env_mask: wp.array): raise RuntimeError("Environment rng_state_wp is not initialized.") # resample time-left and increment command-counter for masked envs - wp.launch( + lower = float(self.cfg.resampling_time_range[0]) + upper = float(self.cfg.resampling_time_range[1]) + self._env._warp_launch.launch( kernel=_resample_time_left_and_increment_counter, dim=self.num_envs, inputs=[ @@ -328,10 +342,10 @@ def _resample(self, env_mask: wp.array): self.time_left_wp, self.command_counter_wp, self._env.rng_state_wp, - float(self.cfg.resampling_time_range[0]), - float(self.cfg.resampling_time_range[1]), + lower, + upper, ], - device=self.device, + site=("command_term", self, "resample_time_left", env_mask, lower, upper), ) # resample command values for masked envs self._resample_command(env_mask) @@ -410,9 +424,14 @@ def __init__(self, cfg: object, env: ManagerBasedRLEnv): # reset logging extras (persistent holder for orchestrator aggregation) self._reset_extras: dict[str, torch.Tensor] = {} + success_term_count = sum("success_rate" in term.reset_extras for term in self._terms.values()) for term_name, term in self._terms.items(): for metric_name, metric_value in term.reset_extras.items(): - self._reset_extras[f"Metrics/{term_name}/{metric_name}"] = metric_value + if metric_name == "success_rate" and success_term_count == 1: + metric_key = "Metrics/success_rate" + else: + metric_key = f"Metrics/{term_name}/{metric_name}" + self._reset_extras[metric_key] = metric_value def __str__(self) -> str: """Returns: A string representation for the command manager.""" @@ -513,14 +532,7 @@ def reset( Returns: A dictionary containing the information to log under the "Metrics/{term_name}/{metric_name}" key. """ - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - if wp.get_device().is_capturing: - raise RuntimeError( - "CommandManager.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) for term in self._terms.values(): # reset the command term @@ -556,6 +568,17 @@ def get_command(self, name: str) -> torch.Tensor: return wp.to_torch(command) return command + def get_command_wp(self, name: str) -> wp.array(dtype=wp.float32, ndim=2): + """Return pointer-stable Warp storage for a command term. + + Args: + name: The name of the command term. + + Returns: + The command array with shape ``(num_envs, command_dim)``. + """ + return self._terms[name].command_wp + def get_term(self, name: str) -> CommandTerm: """Returns the command term with the specified name. @@ -590,6 +613,7 @@ def _prepare_terms(self): ) # create the action term term = term_cfg.class_type(term_cfg, self._env) + self._register_term_capturability(term_cfg.class_type) # sanity check if term is valid type if not isinstance(term, CommandTerm): raise TypeError(f"Returned object for the term '{term_name}' is not of type CommandType.") diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/curriculum_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/curriculum_manager.py new file mode 100644 index 000000000000..6c358e1c061e --- /dev/null +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/curriculum_manager.py @@ -0,0 +1,247 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp-first curriculum manager with mask-native term dispatch.""" + +from __future__ import annotations + +import inspect +from collections.abc import Sequence +from numbers import Real +from typing import TYPE_CHECKING + +import torch +import warp as wp +from prettytable import PrettyTable + +from isaaclab.managers import CurriculumTermCfg as StableCurriculumTermCfg +from isaaclab.managers import ManagerTermBase as StableManagerTermBase +from isaaclab.utils import string_to_callable + +from .manager_base import ManagerBase, ManagerTermBase + +if TYPE_CHECKING: + from isaaclab.envs import ManagerBasedRLEnv + + +class CurriculumManager(ManagerBase): + """Manage curriculum terms that write state into persistent Warp scalar outputs.""" + + _env: ManagerBasedRLEnv + """The environment instance.""" + + def __init__(self, cfg: object, env: ManagerBasedRLEnv): + """Initialize the curriculum manager. + + Args: + cfg: Configuration object or dictionary of curriculum terms. + env: Environment instance. + """ + self._term_names: list[str] = [] + self._term_cfgs: list[StableCurriculumTermCfg] = [] + self._term_modes: list[str] = [] + + super().__init__(cfg, env) + + num_terms = len(self._term_names) + self._term_states_wp = wp.zeros(num_terms, dtype=wp.float32, device=self.device) + self._term_state_views_wp: list[wp.array] = [] + if num_terms > 0: + stride = self._term_states_wp.strides[0] + for term_idx, term_cfg in enumerate(self._term_cfgs): + out_view = wp.array( + ptr=self._term_states_wp.ptr + term_idx * stride, + dtype=wp.float32, + shape=(1,), + strides=(stride,), + device=self.device, + ) + self._term_state_views_wp.append(out_view) + term_cfg.out = out_view + + self._term_states = wp.to_torch(self._term_states_wp) + self._reset_extras = { + f"Curriculum/{term_name}": self._term_states[term_idx] + for term_idx, term_name in enumerate(self._term_names) + } + + def __str__(self) -> str: + """Return a string representation of the curriculum manager.""" + msg = f" contains {len(self._term_names)} active terms.\n" + table = PrettyTable() + table.title = "Active Curriculum Terms" + table.field_names = ["Index", "Name"] + table.align["Name"] = "l" + for index, name in enumerate(self._term_names): + table.add_row([index, name]) + return msg + table.get_string() + "\n" + + @property + def active_terms(self) -> list[str]: + """Names of active curriculum terms.""" + return self._term_names + + @property + def reset_extras(self) -> dict[str, torch.Tensor]: + """Persistent scalar views containing the latest curriculum states.""" + return self._reset_extras + + def reset( + self, + env_mask: wp.array(dtype=wp.bool), + env_ids: torch.Tensor | None = None, + ) -> dict[str, torch.Tensor]: + """Reset selected class terms and return persistent curriculum logging outputs. + + Args: + env_mask: Boolean Warp mask selecting environments to reset. + env_ids: Precomputed compact environment IDs matching :paramref:`env_mask`. + When omitted, legacy terms materialize IDs once at this boundary. + + Returns: + Persistent scalar curriculum states keyed by their logging paths. + """ + env_mask = self._resolve_reset_mask(None, env_mask) + # Dispatch by term kind: Warp class terms consume the mask directly; stable + # class terms receive compact IDs, materialized at most once per call. + compact_env_ids = env_ids + for term_cfg, mode in zip(self._term_cfgs, self._term_modes): + if isinstance(term_cfg.func, ManagerTermBase): + term_cfg.func.reset(env_mask=env_mask) + elif isinstance(term_cfg.func, StableManagerTermBase): + if compact_env_ids is None: + compact_env_ids = self._compact_legacy_env_ids(env_mask) + term_cfg.func.reset(env_ids=compact_env_ids) + return self._reset_extras + + @property + def requires_host_ids(self) -> bool: + """Whether any active legacy term genuinely consumes compact environment IDs.""" + return "legacy" in self._term_modes + + @property + def requires_host_boundary(self) -> bool: + """Whether any active legacy term must run only when a reset occurs.""" + return any(mode != "mask" for mode in self._term_modes) + + def compute( + self, + env_mask: wp.array(dtype=wp.bool), + env_ids: torch.Tensor | None = None, + ) -> None: + """Update curriculum terms for selected environments. + + Args: + env_mask: Boolean Warp mask selecting environments to update. + env_ids: Precomputed compact environment IDs matching :paramref:`env_mask`. + When omitted, legacy terms materialize IDs once at this boundary. + """ + env_mask = self._resolve_reset_mask(None, env_mask) + # Logging states are recomputed every call; mask-native terms write their + # scalar slot on-device through the pointer-stable ``term_cfg.out`` view. + self._term_states_wp.zero_() + compact_env_ids = env_ids + # Term modes: "mask" = Warp-native ``(env, env_mask, out)``; + # "legacy" = stable ``(env, env_ids)`` with compact IDs materialized at most once. + for term_idx, (term_cfg, mode) in enumerate(zip(self._term_cfgs, self._term_modes)): + if mode == "mask": + term_cfg.func(self._env, env_mask, term_cfg.out, **term_cfg.params) + continue + if compact_env_ids is None: + compact_env_ids = self._compact_legacy_env_ids(env_mask) + state = term_cfg.func(self._env, compact_env_ids, **term_cfg.params) + if state is not None: + if isinstance(state, torch.Tensor) and state.numel() != 1: + raise TypeError( + f"Curriculum term '{self._term_names[term_idx]}' must return a scalar state;" + f" received tensor shape {tuple(state.shape)}." + ) + if not isinstance(state, (Real, torch.Tensor)): + raise TypeError( + f"Curriculum term '{self._term_names[term_idx]}' returned {type(state).__name__}." + " Warp CurriculumManager supports scalar logging states only." + ) + self._term_states[term_idx] = state + + def get_active_iterable_terms(self, env_idx: int) -> Sequence[tuple[str, Sequence[float]]]: + """Return curriculum states for debug inspection. + + Args: + env_idx: Unused environment index retained for the manager interface. + + Returns: + Curriculum term names and their scalar states. + """ + del env_idx + states = self._term_states.detach().cpu().tolist() + return [(name, [states[index]]) for index, name in enumerate(self._term_names)] + + def _prepare_terms(self): + cfg_items = self.cfg.items() if isinstance(self.cfg, dict) else self.cfg.__dict__.items() + for term_name, term_cfg in cfg_items: + if term_cfg is None: + continue + if not isinstance(term_cfg, StableCurriculumTermCfg): + raise TypeError( + f"Configuration for the term '{term_name}' is not of type CurriculumTermCfg." + f" Received: '{type(term_cfg)}'." + ) + if isinstance(term_cfg.func, str): + term_cfg.func = string_to_callable(term_cfg.func) + if self._is_mask_term(term_cfg.func): + mode = "mask" + self._resolve_common_term_cfg(term_name, term_cfg, min_argc=3) + else: + mode = "legacy" + self._resolve_legacy_term_cfg(term_name, term_cfg) + self._term_names.append(term_name) + self._term_cfgs.append(term_cfg) + self._term_modes.append(mode) + + @staticmethod + def _is_mask_term(func) -> bool: + """Return whether a term follows ``(env, env_mask, out, ...)``.""" + func_static = func.__call__ if inspect.isclass(func) else func + parameters = list(inspect.signature(func_static).parameters) + if inspect.isclass(func): + parameters = parameters[1:] + return parameters[:3] == ["env", "env_mask", "out"] + + def _resolve_legacy_term_cfg(self, term_name: str, term_cfg: StableCurriculumTermCfg) -> None: + """Validate and initialize a stable ``(env, env_ids, ...)`` curriculum term.""" + if not callable(term_cfg.func): + raise AttributeError(f"The term '{term_name}' is not callable. Received: {term_cfg.func}") + is_class = inspect.isclass(term_cfg.func) + func_static = term_cfg.func.__call__ if is_class else term_cfg.func + min_argc = 3 if is_class else 2 + signature = inspect.signature(func_static) + parameters = list(signature.parameters.values()) + for parameter in parameters[min_argc:]: + if ( + parameter.default is not inspect.Parameter.empty + and parameter.name not in term_cfg.params + and hasattr(parameter.default, "__dataclass_fields__") + ): + term_cfg.params[parameter.name] = parameter.default.copy() + required = { + parameter.name for parameter in parameters[min_argc:] if parameter.default is inspect.Parameter.empty + } + accepted = {parameter.name for parameter in parameters[min_argc:]} + provided = set(term_cfg.params) + if not required.issubset(provided) or not provided.issubset(accepted): + raise ValueError( + f"The legacy curriculum term '{term_name}' expects parameters {sorted(accepted)}," + f" but received {sorted(provided)}." + ) + graph_cache = getattr(self._env, "_warp_graph_cache", None) + if graph_cache is not None: + graph_cache.register_capturability(type(self).__name__, False) + if self._env.sim.is_playing(): + self._process_term_cfg_at_play(term_name, term_cfg) + + @staticmethod + def _compact_legacy_env_ids(env_mask: wp.array(dtype=wp.bool)) -> torch.Tensor: + """Materialize compact Torch IDs at the legacy curriculum boundary.""" + return wp.to_torch(env_mask).nonzero(as_tuple=False).squeeze(-1) diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/event_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/event_manager.py index ab8a0264ea9b..0f0aab296b38 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/event_manager.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/event_manager.py @@ -29,8 +29,9 @@ import warp as wp from prettytable import PrettyTable -from .manager_base import ManagerBase -from .manager_term_cfg import EventTermCfg +from isaaclab.managers.manager_term_cfg import EventTermCfg + +from .manager_base import ManagerBase, ManagerTermBase logger = logging.getLogger(__name__) @@ -42,6 +43,7 @@ def _interval_init_per_env( lower: wp.float32, upper: wp.float32, ): + """Seed each env's interval-event countdown with a uniform draw from [``lower``, ``upper``] [s].""" env_id = wp.tid() s = rng_state[env_id] time_left[env_id] = wp.randf(s, lower, upper) @@ -55,6 +57,7 @@ def _interval_init_global( lower: wp.float32, upper: wp.float32, ): + """Seed the single shared countdown of a ``is_global_time`` interval event.""" # single element s = rng_state[0] time_left[0] = wp.randf(s, lower, upper) @@ -70,6 +73,10 @@ def _interval_step_per_env( lower: wp.float32, upper: wp.float32, ): + """Tick each env's interval countdown by ``dt`` [s]; on expiry, flag the env in + + ``trigger_mask`` and immediately draw its next interval (RNG state written back). + """ env_id = wp.tid() t = time_left[env_id] - dt if t < wp.float32(1.0e-6): @@ -91,6 +98,10 @@ def _interval_step_global( lower: wp.float32, upper: wp.float32, ): + """Tick the shared countdown of a ``is_global_time`` interval event; on expiry set + + ``trigger_flag[0]`` and draw the next interval. + """ t = time_left[0] - dt if t < wp.float32(1.0e-6): trigger_flag[0] = True @@ -110,6 +121,7 @@ def _interval_reset_selected( lower: wp.float32, upper: wp.float32, ): + """Re-draw interval countdowns for envs selected by ``env_mask`` (episode-reset path).""" env_id = wp.tid() if env_mask[env_id]: s = rng_state[env_id] @@ -122,6 +134,10 @@ def _seed_global_rng_from_env_rng( env_rng_state: wp.array(dtype=wp.uint32), global_rng_state: wp.array(dtype=wp.uint32), ): + """Derive the global-event RNG stream from env 0's per-env state so global draws + + stay seed-reproducible without a host round-trip. + """ global_rng_state[0] = wp.rand_init(wp.int32(env_rng_state[0]), wp.int32(0)) @@ -134,6 +150,13 @@ def _reset_compute_valid_mask( global_step_count_buf: wp.array(dtype=wp.int32), min_step_count: wp.int32, ): + """Gate a reset-event mask by trigger spacing entirely on device. + + An env stays selected only if at least ``min_step_count`` steps passed since it + last triggered, or it has never triggered (stable-parity: first reset always + fires). Passing envs update ``last_triggered_step``/``triggered_once`` in the + same launch, keeping the spacing bookkeeping replay-safe. + """ env_id = wp.tid() if not in_mask[env_id]: out_mask[env_id] = False @@ -222,14 +245,57 @@ def available_modes(self) -> list[str]: return list(self._mode_term_names.keys()) def set_term_cfg(self, term_name: str, cfg: EventTermCfg): - term_found = False + """Set the configuration of an existing event term. + + Args: + term_name: Name of the event term. + cfg: Replacement event-term configuration. + + Raises: + ValueError: If the term does not exist or the replacement changes its mode. + """ for mode, terms in self._mode_term_names.items(): if term_name in terms: - self._mode_term_cfgs[mode][terms.index(term_name)] = cfg - term_found = True - break - if not term_found: - raise ValueError(f"Event term '{term_name}' not found.") + if cfg.mode != mode: + raise ValueError( + f"Event term '{term_name}' belongs to mode '{mode}', but the replacement uses '{cfg.mode}'." + ) + + term_index = terms.index(term_name) + old_cfg = self._mode_term_cfgs[mode][term_index] + if mode == "interval" and cfg.is_global_time != old_cfg.is_global_time: + raise ValueError( + f"Event term '{term_name}' cannot change is_global_time at runtime because its timer " + "storage is already allocated." + ) + if mode == "interval" and cfg.interval_range_s is None: + raise ValueError( + f"Event term '{term_name}' has mode 'interval' but 'interval_range_s' is not specified." + ) + if mode == "reset" and cfg.min_step_count_between_reset < 0: + raise ValueError( + f"Event term '{term_name}' has mode 'reset' but 'min_step_count_between_reset' is" + f" negative: {cfg.min_step_count_between_reset}. Please provide a non-negative value." + ) + self._resolve_common_term_cfg(term_name, cfg, min_argc=2) + self._env.invalidate_wp_graphs() + # Deferred scene-entity resolution iterates the manager's source config. + if isinstance(self.cfg, dict): + self.cfg[term_name] = cfg + else: + setattr(self.cfg, term_name, cfg) + self._mode_term_cfgs[mode][term_index] = cfg + if mode == "interval": + assert cfg.interval_range_s is not None + lower, upper = cfg.interval_range_s + self._interval_term_ranges[term_index] = (float(lower), float(upper)) + self._mode_class_term_cfgs[mode] = [ + mode_cfg + for mode_cfg in self._mode_term_cfgs[mode] + if inspect.isclass(mode_cfg.func) or isinstance(mode_cfg.func, ManagerTermBase) + ] + return + raise ValueError(f"Event term '{term_name}' not found.") def get_term_cfg(self, term_name: str) -> EventTermCfg: for mode, terms in self._mode_term_names.items(): @@ -243,15 +309,7 @@ def reset( *, env_mask: wp.array | torch.Tensor | None = None, ) -> dict[str, float]: - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - # Keep all id->mask resolution strictly outside capture. - if wp.get_device().is_capturing: - raise RuntimeError( - "EventManager.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) # reset class terms (mask-based) for mode_cfg in self._mode_class_term_cfgs.values(): @@ -266,7 +324,7 @@ def reset( if term_cfg.is_global_time or not term_cfg.resample_interval_on_reset: continue lower, upper = self._interval_term_ranges[i] - wp.launch( + self._env._warp_launch.launch( kernel=_interval_reset_selected, dim=self.num_envs, inputs=[ @@ -276,7 +334,7 @@ def reset( float(lower), float(upper), ], - device=self.device, + site=("event_manager", "interval_reset", i, env_mask, float(lower), float(upper)), ) return {} @@ -342,7 +400,7 @@ def _apply_interval(self, dt: float) -> None: "EventManager._apply_interval: _interval_global_rng_state_wp is not initialized." ) # update scalar time_left and scalar flag (mask is a broadcast view of the flag) - wp.launch( + self._env._warp_launch.launch( kernel=_interval_step_global, dim=1, inputs=[ @@ -353,11 +411,11 @@ def _apply_interval(self, dt: float) -> None: float(lower), float(upper), ], - device=self.device, + site=("event_manager", "interval_step_global", i, float(dt), float(lower), float(upper)), ) term_cfg.func(self._env, self._scratch_interval_trigger_mask_view_wp, **term_cfg.params) else: - wp.launch( + self._env._warp_launch.launch( kernel=_interval_step_per_env, dim=self.num_envs, inputs=[ @@ -368,7 +426,7 @@ def _apply_interval(self, dt: float) -> None: float(lower), float(upper), ], - device=self.device, + site=("event_manager", "interval_step_per_env", i, float(dt), float(lower), float(upper)), ) term_cfg.func(self._env, self._scratch_term_mask_wp, **term_cfg.params) @@ -379,7 +437,7 @@ def _apply_reset(self, env_mask_wp: wp.array, global_env_step_count_wp: wp.array # iterate over all the reset terms for index, term_cfg in enumerate(self._mode_term_cfgs["reset"]): min_step_count = int(term_cfg.min_step_count_between_reset) - wp.launch( + self._env._warp_launch.launch( kernel=_reset_compute_valid_mask, dim=self.num_envs, inputs=[ @@ -390,7 +448,7 @@ def _apply_reset(self, env_mask_wp: wp.array, global_env_step_count_wp: wp.array global_env_step_count_wp, int(min_step_count), ], - device=self.device, + site=("event_manager", "reset_valid_mask", index, env_mask_wp, min_step_count), ) term_cfg.func(self._env, self._scratch_term_mask_wp, **term_cfg.params) @@ -444,7 +502,7 @@ def _prepare_terms(self): self._mode_term_names[term_cfg.mode].append(term_name) self._mode_term_cfgs[term_cfg.mode].append(term_cfg) - if inspect.isclass(term_cfg.func): + if inspect.isclass(term_cfg.func) or isinstance(term_cfg.func, ManagerTermBase): self._mode_class_term_cfgs[term_cfg.mode].append(term_cfg) # per-mode Warp buffers diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/manager_base.py b/source/isaaclab_experimental/isaaclab_experimental/managers/manager_base.py index 6caea3eebcbb..bb2decbd77e8 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/manager_base.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/manager_base.py @@ -20,29 +20,54 @@ import inspect import logging from abc import ABC, abstractmethod -from collections.abc import Sequence +from collections.abc import Callable, Sequence from typing import TYPE_CHECKING, Any import warp as wp import isaaclab.utils.string as string_utils +from isaaclab.managers.manager_term_cfg import ManagerTermBaseCfg from isaaclab.utils import class_to_dict, string_to_callable -from isaaclab_experimental.utils.warp import is_warp_capturable +from isaaclab_experimental.utils.warp import WarpCapturable -from .manager_term_cfg import ManagerTermBaseCfg from .scene_entity_cfg import SceneEntityCfg # import omni.timeline if TYPE_CHECKING: + import torch + from isaaclab.envs import ManagerBasedEnv # import logger logger = logging.getLogger(__name__) +def _resolve_reset_mask( + owner: ManagerBase | ManagerTermBase, + env_ids: Sequence[int] | None, + env_mask: wp.array(dtype=wp.bool) | None, +) -> wp.array(dtype=wp.bool): + """Resolve reset input once while keeping ID conversion outside capture.""" + if isinstance(env_mask, wp.array): + if env_mask.dtype != wp.bool or env_mask.ndim != 1 or env_mask.shape[0] != owner.num_envs: + raise ValueError( + f"env_mask must be a Warp boolean array with shape ({owner.num_envs},), received {env_mask}." + ) + expected_device = wp.get_device(owner.device) + if env_mask.device != expected_device: + raise ValueError(f"env_mask must be on {expected_device}, received {env_mask.device}.") + return env_mask + if wp.get_device(owner.device).is_capturing: + raise RuntimeError( + f"{type(owner).__name__}.reset requires env_mask(wp.array[bool]) during capture. " + "Resolve environment IDs before entering the captured stage." + ) + return owner._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + + class ManagerTermBase(ABC): """Base class for manager terms. @@ -107,14 +132,28 @@ def __name__(self) -> str: Operations. """ - def reset(self, env_mask: wp.array | None = None) -> None: + def reset(self, env_mask: wp.array | None = None) -> dict[str, torch.Tensor] | None: """Resets the manager term (mask-based). Args: env_mask: Boolean mask of shape (num_envs,) indicating which envs to reset. If None, all envs are considered. + + Returns: + An optional dictionary of logging values to merge into the owning manager's + reset extras. To stay CUDA-graph capturable, values must be persistent + tensor views over device buffers written by kernels (no host readback); + the same objects must be returned on every call. """ - pass + return None + + def _resolve_reset_mask( + self, + env_ids: Sequence[int] | None, + env_mask: wp.array(dtype=wp.bool) | None, + ) -> wp.array(dtype=wp.bool): + """Resolve a legacy reset selection to the canonical Warp mask.""" + return _resolve_reset_mask(self, env_ids, env_mask) def serialize(self) -> dict: """General serialization call. Includes the configuration dict.""" @@ -226,7 +265,11 @@ def active_terms(self) -> list[str] | dict[str, list[str]]: Operations. """ - def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None = None) -> dict[str, float]: + def reset( + self, + env_ids: Sequence[int] | None = None, + env_mask: wp.array(dtype=wp.bool) | None = None, + ) -> dict[str, float]: """Resets the manager and returns logging information for the current time-step. Args: @@ -238,6 +281,14 @@ def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None """ return {} + def _resolve_reset_mask( + self, + env_ids: Sequence[int] | None, + env_mask: wp.array(dtype=wp.bool) | None, + ) -> wp.array(dtype=wp.bool): + """Resolve a legacy reset selection to the canonical Warp mask.""" + return _resolve_reset_mask(self, env_ids, env_mask) + def find_terms(self, name_keys: str | Sequence[str]) -> list[str]: """Find terms in the manager based on the names. @@ -403,17 +454,24 @@ def _resolve_common_term_cfg(self, term_name: str, term_cfg: ManagerTermBaseCfg, f" and optional parameters: {args_with_defaults}, but received: {term_params}." ) - # register non-capturable terms with the call switch for mode=2 fallback - if not is_warp_capturable(term_cfg.func): - switch = getattr(self._env, "_manager_call_switch", None) - if switch is not None: - switch.register_manager_capturability(type(self).__name__, False) + self._register_term_capturability(term_cfg.func) # process attributes at runtime # these properties are only resolvable once the simulation starts playing if self._env.sim.is_playing(): self._process_term_cfg_at_play(term_name, term_cfg) + def _register_term_capturability(self, term: Callable) -> None: + """Keep the complete manager eager when a configured term is unsafe.""" + # TODO(#6611): granularity is whole-manager — one non-capturable term forces the + # entire manager eager. Per-term record/replay could keep the capturable terms + # on graphs while only the unsafe term runs eagerly; that is execution-layer + # scope and deliberately not part of the mask-first PR. + if not WarpCapturable.is_capturable(term): + graph_cache = getattr(self._env, "_warp_graph_cache", None) + if graph_cache is not None: + graph_cache.register_capturability(type(self).__name__, False) + def _process_term_cfg_at_play(self, term_name: str, term_cfg: ManagerTermBaseCfg): """Process the term configuration at runtime. diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/manager_term_cfg.py b/source/isaaclab_experimental/isaaclab_experimental/managers/manager_term_cfg.py index ce354be66a2a..9dad78dc25c9 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/manager_term_cfg.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/manager_term_cfg.py @@ -3,92 +3,50 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Configuration terms for different managers (experimental, Warp-first). +"""Manager term configuration classes used by the Warp-first managers. -This module is a passthrough to :mod:`isaaclab.managers.manager_term_cfg` except for -the following term configs which are overridden for Warp-first execution: +Passthrough to :mod:`isaaclab.managers.manager_term_cfg`: the Warp-first managers +accept the same configuration shapes as the stable managers, so re-exporting the +stable classes preserves type identity and lets a stable task configuration be +adapted in place (by the warp frontend) without rebuilding every term. At +runtime, the adapted term callables still use the Warp-first +``func(env, out, **params) -> None`` signature. -- :class:`ObservationTermCfg` -- :class:`RewardTermCfg` -- :class:`TerminationTermCfg` +:class:`CurriculumTermCfg` is the one override: it extends the stable class with +the Warp-first curriculum term configuration. """ from __future__ import annotations -from collections.abc import Callable -from dataclasses import MISSING - -from isaaclab.managers.manager_term_cfg import * # noqa: F401,F403 -from isaaclab.managers.manager_term_cfg import ManagerTermBaseCfg as _ManagerTermBaseCfg +from isaaclab.managers.manager_term_cfg import ( + ActionTermCfg, + CommandTermCfg, + EventTermCfg, + ManagerTermBaseCfg, + ObservationGroupCfg, + ObservationTermCfg, + RecorderTermCfg, + RewardTermCfg, + TerminationTermCfg, +) +from isaaclab.managers.manager_term_cfg import CurriculumTermCfg as _CurriculumTermCfg from isaaclab.utils.configclass import configclass @configclass -class RewardTermCfg(_ManagerTermBaseCfg): - """Configuration for a reward term. - - The function is expected to write the (unweighted) reward values into a - pre-allocated Warp buffer provided by the manager. - - Expected signature: - - - ``func(env, out, **params) -> None`` - - where ``out`` is a Warp array of shape ``(num_envs,)`` with float32 dtype. - """ - - func: Callable[..., None] = MISSING - """The function to be called to fill the pre-allocated reward buffer.""" - - weight: float = MISSING - """The weight of the reward term.""" - - -@configclass -class TerminationTermCfg(_ManagerTermBaseCfg): - """Configuration for a termination term (experimental, Warp-first). - - The function is expected to write termination flags into a pre-allocated Warp buffer. - - Expected signature: - - - ``func(env, out, **params) -> None`` - - where ``out`` is a Warp array of shape ``(num_envs,)`` with boolean dtype. - """ - - func: Callable[..., None] = MISSING - """The function to be called to fill the pre-allocated termination buffer.""" - - time_out: bool = False - """Whether the termination term contributes towards episodic timeouts. Defaults to False.""" - - -@configclass -class ObservationTermCfg(_ManagerTermBaseCfg): - """Configuration for an observation term (experimental, Warp-first). - - The function is expected to write observation values into a pre-allocated Warp buffer provided - by the observation manager. - - Expected signature: - - - ``func(env, out, **params) -> None`` - - where ``out`` is a Warp array of shape ``(num_envs, obs_term_dim)`` with float32 dtype. - - Notes: - - The stable fields (noise/modifiers/history) are kept for config compatibility, but the - experimental Warp-first observation manager may not support all of them initially. - """ - - func: Callable[..., None] = MISSING - """The function to be called to fill the pre-allocated observation buffer.""" - - # Keep stable configuration fields for compatibility with existing task configs. - modifiers: list[ModifierCfg] | None = None # noqa: F405 - noise: NoiseCfg | NoiseModelCfg | None = None # noqa: F405 - clip: tuple[float, float] | None = None - scale: tuple[float, ...] | float | None = None - history_length: int = 0 - flatten_history_dim: bool = True +class CurriculumTermCfg(_CurriculumTermCfg): + """Configuration for a Warp-mask or legacy curriculum term.""" + + +__all__ = [ + "ActionTermCfg", + "CommandTermCfg", + "CurriculumTermCfg", + "EventTermCfg", + "ManagerTermBaseCfg", + "ObservationGroupCfg", + "ObservationTermCfg", + "RecorderTermCfg", + "RewardTermCfg", + "TerminationTermCfg", +] diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/observation_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/observation_manager.py index 74d0a9a955be..518e9d580411 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/observation_manager.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/observation_manager.py @@ -48,6 +48,7 @@ import inspect from collections.abc import Sequence +from copy import copy from typing import TYPE_CHECKING, Any import numpy as np @@ -55,6 +56,7 @@ import warp as wp from prettytable import PrettyTable +from isaaclab.managers.manager_term_cfg import ObservationGroupCfg, ObservationTermCfg from isaaclab.utils import class_to_dict from isaaclab_experimental.utils import modifiers, noise @@ -62,7 +64,6 @@ from isaaclab_experimental.utils.torch_utils import clone_obs_buffer from .manager_base import ManagerBase, ManagerTermBase -from .manager_term_cfg import ObservationGroupCfg, ObservationTermCfg if TYPE_CHECKING: from isaaclab.envs import ManagerBasedEnv @@ -70,6 +71,7 @@ @wp.kernel def _apply_clip(out: wp.array(dtype=wp.float32, ndim=2), clip_lo: wp.float32, clip_hi: wp.float32): + """Clamp every column of a term's ``out`` block to [``clip_lo``, ``clip_hi``] in place.""" env_id = wp.tid() for j in range(out.shape[1]): out[env_id, j] = wp.clamp(out[env_id, j], clip_lo, clip_hi) @@ -77,6 +79,7 @@ def _apply_clip(out: wp.array(dtype=wp.float32, ndim=2), clip_lo: wp.float32, cl @wp.kernel def _apply_scale(out: wp.array(dtype=wp.float32, ndim=2), scale: wp.array(dtype=wp.float32)): + """Multiply each column of a term's ``out`` block by its per-column ``scale`` in place.""" env_id = wp.tid() for j in range(out.shape[1]): out[env_id, j] = out[env_id, j] * scale[j] @@ -382,15 +385,7 @@ def reset( *, env_mask: wp.array | None = None, ) -> dict[str, float]: - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - # Keep all id->mask resolution strictly outside capture. - if wp.get_device().is_capturing: - raise RuntimeError( - "ObservationManager.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) # call all terms that are classes for group_name, group_cfg in self._group_obs_class_term_cfgs.items(): @@ -501,26 +496,29 @@ def compute_group(self, group_name: str, update_history: bool = False) -> torch. for modifier in term_cfg.modifiers: modifier.func(term_cfg.out_wp, **modifier.params) - # apply noise (Warp in-place on out_wp) - if isinstance(term_cfg.noise, noise.NoiseCfg): - term_cfg.noise.func(term_cfg.out_wp, term_cfg.noise) - elif isinstance(term_cfg.noise, noise.NoiseModelCfg) and term_cfg.noise.func is not None: - term_cfg.noise.func(term_cfg.out_wp) + # apply the manager-owned prepared noise runtime (Warp in-place on out_wp) + noise_runtime = self._group_obs_noise_runtime[group_name].get(term_name) + if isinstance(noise_runtime, noise.NoiseCfg): + noise_runtime.func(term_cfg.out_wp, noise_runtime) + elif noise_runtime is not None: + noise_runtime(term_cfg.out_wp) # clip then scale (stable semantics); implementation may use Warp kernels if term_cfg.clip is not None: - wp.launch( + clip_lo = float(term_cfg.clip[0]) + clip_hi = float(term_cfg.clip[1]) + self._env._warp_launch.launch( kernel=_apply_clip, dim=self.num_envs, - inputs=[term_cfg.out_wp, float(term_cfg.clip[0]), float(term_cfg.clip[1])], - device=self.device, + inputs=[term_cfg.out_wp, clip_lo, clip_hi], + site=("observation_manager", group_name, term_name, "clip", clip_lo, clip_hi), ) if term_cfg.scale is not None: - wp.launch( + self._env._warp_launch.launch( kernel=_apply_scale, dim=self.num_envs, inputs=[term_cfg.out_wp, term_cfg.scale_wp], - device=self.device, + site=("observation_manager", group_name, term_name, "scale"), ) # TODO(jichuanh): This is not migrated yet. Need revisit. @@ -596,6 +594,7 @@ def _prepare_terms(self): # noqa: C901 self._group_obs_concatenate_dim: dict[str, int] = dict() self._group_obs_term_history_buffer: dict[str, dict] = dict() + self._group_obs_noise_runtime: dict[str, dict[str, noise.NoiseCfg | noise.NoiseModel]] = dict() # create a list to store classes instances, e.g., for modifiers and noise models # we store it as a separate list to only call reset on them and prevent unnecessary calls self._group_obs_class_instances: list[modifiers.ModifierBase | noise.NoiseModel] = list() @@ -638,6 +637,7 @@ def _prepare_terms(self): # noqa: C901 self._group_obs_term_dim[group_name] = list() self._group_obs_term_cfgs[group_name] = list() self._group_obs_class_term_cfgs[group_name] = list() + self._group_obs_noise_runtime[group_name] = {} group_entry_history_buffer: dict[str, CircularBuffer] = dict() # read common config for the group self._group_obs_concatenate[group_name] = group_cfg.concatenate_terms @@ -679,6 +679,16 @@ def _prepare_terms(self): # noqa: C901 # check noise settings if not group_cfg.enable_corruption: term_cfg.noise = None + elif term_cfg.noise is not None and not type(term_cfg.noise).__module__.startswith( + "isaaclab_experimental." + ): + raise TypeError( + f"Observation term '{term_name}' uses noise cfg" + f" '{type(term_cfg.noise).__module__}.{type(term_cfg.noise).__name__}', which the Warp" + " observation manager would silently ignore. Use the warp-native noise cfgs from" + " isaaclab_experimental.utils.noise (the warp frontend converts stable cfgs" + " automatically)." + ) # check group history params and override terms if group_cfg.history_length is not None: term_cfg.history_length = group_cfg.history_length @@ -760,21 +770,36 @@ def _prepare_terms(self): # noqa: C901 f" and optional parameters: {args_with_defaults}, but received: {term_params}." ) + # Prepare manager-owned noise runtime state without mutating the serializable config. + if isinstance(term_cfg.noise, noise.NoiseCfg): + noise_runtime_cfg = copy(term_cfg.noise) + noise_runtime_cfg.rng_state_wp = self._env.rng_state_wp + noise_runtime_cfg._warp_launch = self._env._warp_launch + self._group_obs_noise_runtime[group_name][term_name] = noise_runtime_cfg + # prepare noise model classes if term_cfg.noise is not None and isinstance(term_cfg.noise, noise.NoiseModelCfg): - # plumb the shared per-env RNG state so Warp noise kernels can consume it - term_cfg.noise.rng_state_wp = self._env.rng_state_wp + if not isinstance(term_cfg.noise.noise_cfg, noise.NoiseCfg): + raise TypeError( + f"Noise model for observation term '{term_name}' in group '{group_name}' must use an" + " isaaclab_experimental Warp NoiseCfg. Stable Torch noise configs are not supported by" + " ObservationManager." + ) + noise_model_cfg = copy(term_cfg.noise) + noise_model_cfg.noise_cfg = copy(term_cfg.noise.noise_cfg) + noise_model_cfg.noise_cfg.rng_state_wp = self._env.rng_state_wp + noise_model_cfg.noise_cfg._warp_launch = self._env._warp_launch noise_model_cls = term_cfg.noise.class_type if not issubclass(noise_model_cls, noise.NoiseModel): raise TypeError( f"Class type for observation term '{term_name}' NoiseModelCfg" f" is not a subclass of 'NoiseModel'. Received: '{type(noise_model_cls)}'." ) - # initialize func to be the noise model class instance - term_cfg.noise.func = noise_model_cls( - term_cfg.noise, num_envs=self._env.num_envs, device=self._env.device + noise_runtime = noise_model_cls( + noise_model_cfg, num_envs=self._env.num_envs, device=self._env.device ) - self._group_obs_class_instances.append(term_cfg.noise.func) + self._group_obs_noise_runtime[group_name][term_name] = noise_runtime + self._group_obs_class_instances.append(noise_runtime) # create history buffers and calculate history term dimensions if term_cfg.history_length > 0: @@ -905,10 +930,18 @@ def _resolve_out_dim(self, out_dim: int | str, term_cfg: ObservationTermCfg) -> - ``"body:N"``: ``N`` components per selected body from ``asset_cfg``. - ``"command"``: query ``command_manager.get_command(name).shape[-1]``. - ``"action"``: query ``action_manager.action.shape[-1]``. + - ``"sensor:rays"``: number of rays of the ray-caster sensor from ``sensor_cfg``. """ if isinstance(out_dim, int): return out_dim + if out_dim == "sensor:rays": + sensor_cfg = term_cfg.params.get("sensor_cfg") + if sensor_cfg is None: + raise ValueError("out_dim='sensor:rays' requires a 'sensor_cfg' entry in the term's params.") + sensor = self._env.scene.sensors[sensor_cfg.name] + return int(sensor.num_rays) + if out_dim == "joint": asset_cfg = term_cfg.params.get("asset_cfg") asset = self._env.scene[asset_cfg.name] @@ -922,11 +955,12 @@ def _resolve_out_dim(self, out_dim: int | str, term_cfg: ObservationTermCfg) -> if isinstance(out_dim, str) and out_dim.startswith("body:"): per_body = int(out_dim.split(":")[1]) - asset_cfg = term_cfg.params.get("asset_cfg") - body_ids = getattr(asset_cfg, "body_ids", None) + # Body selection may live on an asset (articulation) or a sensor entity. + entity_cfg = term_cfg.params.get("asset_cfg") or term_cfg.params.get("sensor_cfg") + body_ids = getattr(entity_cfg, "body_ids", None) if body_ids is None or body_ids == slice(None): - asset = self._env.scene[asset_cfg.name] - return per_body * len(asset.body_names) + entity = self._env.scene[entity_cfg.name] + return per_body * len(entity.body_names) return per_body * len(body_ids) if out_dim == "command": diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/reward_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/reward_manager.py index 67dcbc055c2f..22c5cd958d97 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/reward_manager.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/reward_manager.py @@ -18,10 +18,11 @@ import warp as wp from prettytable import PrettyTable +from isaaclab.managers.manager_term_cfg import RewardTermCfg + from isaaclab_experimental.utils.warp.kernels import compute_reset_scale, count_masked from .manager_base import ManagerBase, ManagerTermBase -from .manager_term_cfg import RewardTermCfg if TYPE_CHECKING: from isaaclab.envs import ManagerBasedRLEnv @@ -37,6 +38,13 @@ def _sum_and_zero_masked( # output out_avg: wp.array(dtype=wp.float32), ): + """Fold selected envs' per-term episode reward sums into per-term means and zero + + them for the next episode. + Launch with dim=(num_terms, num_envs); ``scale[0]`` carries the 1/count + normalization and the atomic add reduces across selected envs into + ``out_avg[term]``. + """ term_idx, env_id = wp.tid() if mask[env_id]: wp.atomic_add(out_avg, term_idx, episode_sums[term_idx, env_id] * scale[0]) @@ -54,6 +62,13 @@ def _reward_finalize( episode_sums: wp.array(dtype=wp.float32, ndim=2), step_reward: wp.array(dtype=wp.float32, ndim=2), ): + """Combine per-term outputs into the env step reward and episode bookkeeping. + + Per env: ``step_reward`` records each term's weighted reward rate, + ``reward_buf`` accumulates ``weight * out * dt`` across terms, and + ``episode_sums`` grows by the same amount for episode metrics. Zero-weight + terms are skipped so disabled terms cost nothing. + """ env_id = wp.tid() total = wp.float32(0.0) @@ -127,6 +142,7 @@ def __init__(self, cfg: object, env: ManagerBasedRLEnv): # call the base class constructor (this will parse the terms config) super().__init__(cfg, env) self._term_name_to_term_idx = {name: i for i, name in enumerate(self._term_names)} + self._device_weight_terms: set[str] = set() num_terms = len(self._term_names) self._num_terms = num_terms @@ -176,6 +192,17 @@ def __init__(self, cfg: object, env: ManagerBasedRLEnv): self._term_weights_wp = wp.array( [float(term_cfg.weight) for term_cfg in self._term_cfgs], dtype=wp.float32, device=self.device ) + self._term_weight_views_wp: dict[str, wp.array] = {} + if num_terms > 0: + stride = self._term_weights_wp.strides[0] + for term_idx, term_name in enumerate(self._term_names): + self._term_weight_views_wp[term_name] = wp.array( + ptr=self._term_weights_wp.ptr + term_idx * stride, + dtype=wp.float32, + shape=(1,), + strides=(stride,), + device=self.device, + ) # persistent reset-time logging buffers (warp buffers) self._episode_sum_avg_wp = wp.zeros((num_terms,), dtype=wp.float32, device=self.device) @@ -242,38 +269,42 @@ def reset( Returns: A dictionary containing the information to log under the "Reward/{term_name}" key. """ - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - if wp.get_device().is_capturing: - raise RuntimeError( - "RewardManager.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) self._episode_sum_avg_wp.zero_() self._reset_count_wp.zero_() self._reset_scale_wp.zero_() - wp.launch(kernel=count_masked, dim=self.num_envs, inputs=[env_mask, self._reset_count_wp], device=self.device) - wp.launch( + self._env._warp_launch.launch( + kernel=count_masked, + dim=self.num_envs, + inputs=[env_mask, self._reset_count_wp], + site=("reward_manager", "reset_count", env_mask), + ) + max_episode_length_s = float(self._env.max_episode_length_s) + self._env._warp_launch.launch( kernel=compute_reset_scale, dim=1, - inputs=[self._reset_count_wp, float(self._env.max_episode_length_s), self._reset_scale_wp], - device=self.device, + inputs=[self._reset_count_wp, max_episode_length_s, self._reset_scale_wp], + site=("reward_manager", "reset_scale", max_episode_length_s), ) if self._num_terms > 0: - wp.launch( + self._env._warp_launch.launch( kernel=_sum_and_zero_masked, dim=(self._num_terms, self.num_envs), inputs=[env_mask, self._reset_scale_wp, self._episode_sums_wp, self._episode_sum_avg_wp], - device=self.device, + site=("reward_manager", "reset_sums", env_mask), ) - # reset all the reward terms + # reset all the reward terms; class terms may expose logging values as + # persistent tensor views (see :meth:`ManagerTermBase.reset`), which are + # merged into the manager's reset extras. Merging the same views again on + # subsequent resets is a no-op, so this stays capture-safe. for term_cfg in self._class_term_cfgs: - term_cfg.func.reset(env_mask=env_mask) + term_extras = term_cfg.func.reset(env_mask=env_mask) + if term_extras: + self._reset_extras.update(term_extras) return self._reset_extras @@ -291,23 +322,24 @@ def compute(self, dt: float) -> torch.Tensor: """ # TODO: Investigate performance diff between two .fill_ and kernel launch # reset computation (Warp buffers) in a single kernel launch - wp.launch( + self._env._warp_launch.launch( kernel=_reward_pre_compute_reset, dim=self.num_envs, inputs=[self._reward_wp, self._step_reward_wp, self._term_outs_wp], - device=self.device, + site=("reward_manager", "compute_reset"), ) # iterate over all the reward terms (Python loop; per-term math is warp) - for term_cfg in self._term_cfgs: - # skip if weight is zero (kind of a micro-optimization) - if term_cfg.weight == 0.0: + for term_name, term_cfg in zip(self._term_names, self._term_cfgs): + # Static zero-weight terms remain free. Terms whose weight is owned + # on-device must still run so a curriculum can enable them later. + if term_cfg.weight == 0.0 and term_name not in self._device_weight_terms: continue # compute term into the persistent warp buffer (raw, unweighted) # NOTE: `out` is pre-zeroed every step by `_reward_pre_compute_reset`. term_cfg.func(self._env, term_cfg.out, **term_cfg.params) # update total reward, episodic sums and step rewards in a single kernel launch - wp.launch( + self._env._warp_launch.launch( kernel=_reward_finalize, dim=self.num_envs, inputs=[ @@ -318,7 +350,7 @@ def compute(self, dt: float) -> torch.Tensor: self._episode_sums_wp, self._step_reward_wp, ], - device=self.device, + site=("reward_manager", "finalize", float(dt)), ) return self._reward_tensor_view @@ -347,6 +379,7 @@ def set_term_cfg(self, term_name: str, cfg: RewardTermCfg): self._term_cfgs[term_idx] = cfg # keep on-device weights in sync (call this to update weights used in compute) self._term_weights_tensor_view[term_idx] = float(cfg.weight) + self._env.invalidate_wp_graphs() def get_term_cfg(self, term_name: str) -> RewardTermCfg: """Gets the configuration for the specified term. @@ -362,8 +395,30 @@ def get_term_cfg(self, term_name: str) -> RewardTermCfg: """ if term_name not in self._term_names: raise ValueError(f"Reward term '{term_name}' not found.") - # return the configuration - return self._term_cfgs[self._term_names.index(term_name)] + term_idx = self._term_names.index(term_name) + term_cfg = self._term_cfgs[term_idx] + if term_name in self._device_weight_terms: + # Explicit config inspection is a control-plane operation, so it is + # the appropriate place to synchronize a device-owned scalar. + term_cfg.weight = float(self._term_weights_tensor_view[term_idx].item()) + return term_cfg + + def get_term_weight_wp(self, term_name: str) -> wp.array(dtype=wp.float32): + """Return the pointer-stable Warp view of a reward term's scalar weight. + + Args: + term_name: Name of the reward term. + + Returns: + One-element Warp array containing the reward weight. + + Raises: + ValueError: If the term name is not found. + """ + if term_name not in self._term_weight_views_wp: + raise ValueError(f"Reward term '{term_name}' not found.") + self._device_weight_terms.add(term_name) + return self._term_weight_views_wp[term_name] def get_active_iterable_terms(self, env_idx: int) -> Sequence[tuple[str, Sequence[float]]]: """Returns the active terms as iterable sequence of tuples. diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/scene_entity_cfg.py b/source/isaaclab_experimental/isaaclab_experimental/managers/scene_entity_cfg.py index ae97f7f526fb..f6d30489eeca 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/scene_entity_cfg.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/scene_entity_cfg.py @@ -38,6 +38,15 @@ class SceneEntityCfg(_SceneEntityCfg): """Integer indices of selected bodies — used for subset-sized body gathers.""" body_ids_wp: wp.array | None = None + @classmethod + def from_stable(cls, stable: _SceneEntityCfg) -> SceneEntityCfg: + """Build a warp scene-entity cfg from a stable one. + + Copies every field declared on the stable cfg; the warp-specific fields + stay ``None`` and are filled by :meth:`resolve` at scene build time. + """ + return cls(**{name: getattr(stable, name) for name in _SceneEntityCfg.__dataclass_fields__}) + def resolve(self, scene: InteractiveScene): # run the stable resolution first (fills joint_ids/body_ids from names/regex) super().resolve(scene) diff --git a/source/isaaclab_experimental/isaaclab_experimental/managers/termination_manager.py b/source/isaaclab_experimental/isaaclab_experimental/managers/termination_manager.py index 8a768651b292..5396730df4c7 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/managers/termination_manager.py +++ b/source/isaaclab_experimental/isaaclab_experimental/managers/termination_manager.py @@ -22,8 +22,11 @@ import warp as wp from prettytable import PrettyTable +from isaaclab.managers.manager_term_cfg import TerminationTermCfg + +from isaaclab_experimental.utils.warp.kernels import compute_reset_scale, count_masked + from .manager_base import ManagerBase, ManagerTermBase -from .manager_term_cfg import TerminationTermCfg if TYPE_CHECKING: from isaaclab.envs import ManagerBasedRLEnv @@ -92,15 +95,16 @@ def _termination_finalize( # TODO(jichuanh): Look into wp.tile for better performance @wp.kernel -def _termination_reset_mean_all_2d( +def _termination_reset_mean_masked_2d( + env_mask: wp.array(dtype=wp.bool), last_episode_dones: wp.array(dtype=wp.bool, ndim=2), + scale: wp.array(dtype=wp.float32), term_done_avg: wp.array(dtype=wp.float32), ): - """Compute mean(done) per term with 2D parallel accumulation.""" + """Compute mean(done) per term over selected environments.""" env_id, term_idx = wp.tid() - num_envs = last_episode_dones.shape[0] - if num_envs > 0 and last_episode_dones[env_id, term_idx]: - wp.atomic_add(term_done_avg, term_idx, 1.0 / float(num_envs)) + if env_mask[env_id] and last_episode_dones[env_id, term_idx]: + wp.atomic_add(term_done_avg, term_idx, scale[0]) class TerminationManager(ManagerBase): @@ -129,6 +133,8 @@ def __init__(self, cfg: object, env: ManagerBasedRLEnv): num_terms = len(self._term_names) self._term_dones_wp = wp.zeros((self.num_envs, num_terms), dtype=wp.bool, device=self.device) self._term_done_avg_wp = wp.zeros((num_terms,), dtype=wp.float32, device=self.device) + self._reset_count_wp = wp.zeros((1,), dtype=wp.int32, device=self.device) + self._reset_scale_wp = wp.zeros((1,), dtype=wp.float32, device=self.device) self._last_episode_dones_wp = wp.zeros((self.num_envs, num_terms), dtype=wp.bool, device=self.device) self._truncated_wp = wp.zeros((self.num_envs,), dtype=wp.bool, device=self.device) self._terminated_wp = wp.zeros((self.num_envs,), dtype=wp.bool, device=self.device) @@ -249,21 +255,28 @@ def reset( Returns: A dictionary containing the information to log under the "Termination/{term_name}" key. """ - # Mask-first path: captured callers must provide env_mask. - if env_mask is None or not isinstance(env_mask, wp.array): - if wp.get_device().is_capturing: - raise RuntimeError( - "TerminationManager.reset requires env_mask(wp.array[bool]) during capture. " - "Do not pass env_ids on captured paths." - ) - env_mask = self._env.resolve_env_mask(env_ids=env_ids, env_mask=env_mask) + env_mask = self._resolve_reset_mask(env_ids, env_mask) if len(self._term_names) > 0: self._term_done_avg_wp.zero_() - wp.launch( - kernel=_termination_reset_mean_all_2d, + self._reset_count_wp.zero_() + self._reset_scale_wp.zero_() + self._env._warp_launch.launch( + kernel=count_masked, + dim=self.num_envs, + inputs=[env_mask, self._reset_count_wp], + site=("termination_manager", "reset_count", env_mask), + ) + self._env._warp_launch.launch( + kernel=compute_reset_scale, + dim=1, + inputs=[self._reset_count_wp, 1.0, self._reset_scale_wp], + site=("termination_manager", "reset_scale"), + ) + self._env._warp_launch.launch( + kernel=_termination_reset_mean_masked_2d, dim=(self.num_envs, len(self._term_names)), - inputs=[self._last_episode_dones_wp, self._term_done_avg_wp], - device=self.device, + inputs=[env_mask, self._last_episode_dones_wp, self._reset_scale_wp, self._term_done_avg_wp], + site=("termination_manager", "reset_means", env_mask), ) for term_cfg in self._class_term_cfgs: term_cfg.func.reset(env_mask=env_mask) @@ -281,11 +294,11 @@ def compute(self) -> torch.Tensor: The combined termination signal of shape (num_envs,). """ # reset computation (Warp buffers) in a single kernel launch - wp.launch( + self._env._warp_launch.launch( kernel=_termination_pre_compute_reset, dim=self.num_envs, inputs=[self._term_dones_wp, self._truncated_wp, self._terminated_wp, self._dones_wp], - device=self.device, + site=("termination_manager", "compute_reset"), ) # iterate over all the termination terms (fixed list; per-term math is Warp) @@ -293,7 +306,7 @@ def compute(self) -> torch.Tensor: term_cfg.func(self._env, term_cfg.out, **term_cfg.params) # finalize dones and update last-episode term flags (single kernel launch) - wp.launch( + self._env._warp_launch.launch( kernel=_termination_finalize, dim=self.num_envs, inputs=[ @@ -304,7 +317,7 @@ def compute(self) -> torch.Tensor: self._dones_wp, self._last_episode_dones_wp, ], - device=self.device, + site=("termination_manager", "finalize"), ) return self._dones_tensor_view diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/__init__.pyi b/source/isaaclab_experimental/isaaclab_experimental/utils/__init__.pyi index de2860732514..00660ba9775e 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/__init__.pyi +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/__init__.pyi @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause __all__ = [ - "ManagerCallMode", - "ManagerCallSwitch", "WarpGraphCache", "clone_obs_buffer", "buffers", @@ -14,7 +12,6 @@ __all__ = [ "warp", ] -from .manager_call_switch import ManagerCallMode, ManagerCallSwitch from .torch_utils import clone_obs_buffer from .warp_graph_cache import WarpGraphCache from . import buffers, modifiers, noise, warp diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/buffers/circular_buffer.py b/source/isaaclab_experimental/isaaclab_experimental/utils/buffers/circular_buffer.py index f0dcf57b2a30..96bcf21aaf10 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/buffers/circular_buffer.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/buffers/circular_buffer.py @@ -42,6 +42,9 @@ def __init__(self, max_len: int, batch_size: int, device: str): self._device = device self._ALL_INDICES = torch.arange(batch_size, device=device) + # max length as a host int: the property is read on the per-step append path, + # so it must not synchronize with the device tensor below. + self._max_len_int = int(max_len) # max length tensor for comparisons self._max_len = torch.full((batch_size,), max_len, dtype=torch.int, device=device) # number of data pushes passed since the last call to :meth:`reset` @@ -69,7 +72,7 @@ def device(self) -> str: @property def max_length(self) -> int: """The maximum length of the ring buffer.""" - return int(self._max_len[0].item()) + return self._max_len_int @property def current_length(self) -> torch.Tensor: diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/manager_call_switch.py b/source/isaaclab_experimental/isaaclab_experimental/utils/manager_call_switch.py deleted file mode 100644 index 89924c789bc3..000000000000 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/manager_call_switch.py +++ /dev/null @@ -1,262 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Manager call switch for routing manager stage calls through stable/warp/captured paths.""" - -from __future__ import annotations - -import importlib -import json -import os -from enum import IntEnum -from typing import Any - -from isaaclab.utils.timer import Timer - -from isaaclab_experimental.utils.warp_graph_cache import WarpGraphCache - - -class ManagerCallMode(IntEnum): - """Execution mode for manager stage calls. - - * ``STABLE`` (0): Call stable Python manager implementations from :mod:`isaaclab.managers`. - * ``WARP_NOT_CAPTURED`` (1): Call Warp-compatible implementations without CUDA graph capture. - * ``WARP_CAPTURED`` (2): Call Warp implementations with CUDA graph capture/replay. - """ - - STABLE = 0 - WARP_NOT_CAPTURED = 1 - WARP_CAPTURED = 2 - - -class ManagerCallSwitch: - """Per-manager call switch for stable/warp/captured execution. - - Routes each manager stage call through the configured execution path: - stable Python, Warp (eager), or Warp (captured CUDA graph). Optionally - wraps each call in a :class:`Timer` context for profiling. - """ - - DEFAULT_CONFIG: dict[str, int] = {"default": 2} - DEFAULT_KEY = "default" - MANAGER_NAMES: tuple[str, ...] = ( - "ActionManager", - "ObservationManager", - "EventManager", - "RecorderManager", - "CommandManager", - "TerminationManager", - "RewardManager", - "CurriculumManager", - "Scene", - ) - # FIXME: Scene_write_data_to_sim calls articulation._apply_actuator_model which - # uses wp.to_torch + torch indexing -- not capture-safe on this branch. - # Cap Scene stages to WARP_NOT_CAPTURED until the articulation layer is capture-ready. - MAX_MODE_OVERRIDES: dict[str, int] = {"Scene": ManagerCallMode.WARP_NOT_CAPTURED} - - ENV_VAR = "MANAGER_CALL_CONFIG" - """Environment variable name for the JSON config string. - - Example usage:: - - MANAGER_CALL_CONFIG='{"RewardManager": 0, "default": 2}' python train.py ... - """ - - def __init__( - self, - cfg_source: dict | str | None = None, - *, - max_modes: dict[str, int] | None = None, - ): - self._graph_cache = WarpGraphCache() - # Merge caller-supplied max_modes with the class-level MAX_MODE_OVERRIDES. - self._max_modes = dict(self.MAX_MODE_OVERRIDES) - if max_modes is not None: - self._max_modes.update(max_modes) - # Resolve config: prefer explicit cfg_source, fall back to env var. - if cfg_source is None: - cfg_source = os.environ.get(self.ENV_VAR) - self._cfg = self._load_cfg(cfg_source) - print("[INFO] ManagerCallSwitch configuration:") - print(f" - {self.DEFAULT_KEY}: {self._cfg[self.DEFAULT_KEY]}") - for manager_name in self.MANAGER_NAMES: - mode = int(self.get_mode_for_manager(manager_name)) - cap = self._max_modes.get(manager_name) - cap_str = f" (cap={cap})" if cap is not None else "" - print(f" - {manager_name}: {mode}{cap_str}") - - # ------------------------------------------------------------------ - # Graph management - # ------------------------------------------------------------------ - - def invalidate_graphs(self) -> None: - """Invalidate cached capture graphs and their cached return values.""" - self._graph_cache.invalidate() - - # ------------------------------------------------------------------ - # Stage dispatch - # ------------------------------------------------------------------ - - def call_stage( - self, - *, - stage: str, - warp_call: dict[str, Any], - stable_call: dict[str, Any] | None = None, - timer: bool = False, - ) -> Any: - """Run the stage according to configured mode, optionally wrapped in a :class:`Timer`. - - A call spec dict supports the following keys: - - * ``fn`` (required): The callable to invoke. - * ``args`` (optional): Positional arguments tuple. - * ``kwargs`` (optional): Keyword arguments dict. - * ``output`` (optional): A ``Callable[[Any], Any]`` that transforms the raw - return value into the final output. For captured stages the raw value is - ``None``. When omitted, the raw return value is used as-is. - - Args: - stage: Stage identifier in the form ``"ManagerName_function_name"``. - warp_call: Call spec for the warp path (eager or captured). - stable_call: Call spec for the stable (torch) path. Defaults to ``None``. - timer: Whether to wrap execution in a :class:`Timer`. Defaults to ``True`` - (controlled by the global :attr:`Timer.enable` class-level toggle). - Pass a module-level flag like ``TIMER_ENABLED_STEP`` to make timing - conditional on that flag. - - Returns: - The (possibly transformed) return value of the stage. - """ - with Timer(name=stage, msg=f"{stage} took:", enable=timer, time_unit="us"): - return self._dispatch(stage, stable_call, warp_call) - - def _dispatch( - self, - stage: str, - stable_call: dict[str, Any] | None, - warp_call: dict[str, Any], - ) -> Any: - """Select call path based on mode, execute, and apply output.""" - mode = self.get_mode_for_manager(self._manager_name_from_stage(stage)) - if mode == ManagerCallMode.STABLE: - if stable_call is None: - raise ValueError(f"Stage '{stage}' is configured as STABLE (mode=0) but no stable_call was provided.") - call, result = stable_call, self._run_call(stable_call) - elif mode == ManagerCallMode.WARP_CAPTURED: - call, result = warp_call, self._wp_capture_or_launch(stage, warp_call) - else: - call, result = warp_call, self._run_call(warp_call) - - output_fn = call.get("output") - return output_fn(result) if output_fn is not None else result - - # ------------------------------------------------------------------ - # Manager resolution - # ------------------------------------------------------------------ - - def _manager_name_from_stage(self, stage: str) -> str: - if "_" not in stage: - raise ValueError(f"Invalid stage '{stage}'. Expected '{{manager_name}}_{{function_name}}'.") - return stage.split("_", 1)[0] - - def get_mode_for_manager(self, manager_name: str) -> ManagerCallMode: - """Return the resolved execution mode for the given manager. - - Looks up the manager in the config dict, falls back to the default, - then caps by :attr:`_max_modes` (static overrides + dynamic registrations). - """ - default_key = next(iter(self.DEFAULT_CONFIG)) - mode_value = self._cfg.get(manager_name, self._cfg[default_key]) - cap = self._max_modes.get(manager_name) - if cap is not None: - mode_value = min(mode_value, cap) - return ManagerCallMode(mode_value) - - def resolve_manager_class(self, manager_name: str, mode_override: ManagerCallMode | int | None = None) -> type: - """Import and return the manager class for the configured mode.""" - mode = self.get_mode_for_manager(manager_name) if mode_override is None else ManagerCallMode(mode_override) - module_name = "isaaclab.managers" if mode == ManagerCallMode.STABLE else "isaaclab_experimental.managers" - module = importlib.import_module(module_name) - if not hasattr(module, manager_name): - raise AttributeError(f"Manager '{manager_name}' not found in module '{module_name}'.") - return getattr(module, manager_name) - - def register_manager_capturability(self, manager_name: str, capturable: bool) -> None: - """Register that a manager has non-capturable terms, capping its mode. - - Called by :class:`ManagerBase` during term preparation when a term - is decorated with ``@warp_capturable(False)``. - """ - if not capturable: - self._max_modes[manager_name] = min( - self._max_modes.get(manager_name, ManagerCallMode.WARP_CAPTURED), - ManagerCallMode.WARP_NOT_CAPTURED, - ) - - # ------------------------------------------------------------------ - # Internal helpers - # ------------------------------------------------------------------ - - def _run_call(self, call: dict[str, Any]) -> Any: - """Execute a single call spec eagerly.""" - return call["fn"](*call.get("args", ()), **call.get("kwargs", {})) - - def _wp_capture_or_launch(self, stage: str, call: dict[str, Any]) -> Any: - """Capture Warp CUDA graph on first call, then replay. - - Delegates to :class:`WarpGraphCache` which handles warm-up, capture, - caching the return value, and replay. - """ - return self._graph_cache.capture_or_replay( - stage, - call["fn"], - args=call.get("args", ()), - kwargs=call.get("kwargs", {}), - ) - - def _load_cfg(self, cfg_source: dict | str | None) -> dict[str, int]: - if cfg_source is None: - cfg = dict(self.DEFAULT_CONFIG) - elif isinstance(cfg_source, dict): - cfg = dict(cfg_source) - if self.DEFAULT_KEY not in cfg: - cfg[self.DEFAULT_KEY] = self.DEFAULT_CONFIG[self.DEFAULT_KEY] - elif isinstance(cfg_source, str): - if cfg_source.strip() == "": - cfg = dict(self.DEFAULT_CONFIG) - else: - parsed = json.loads(cfg_source) - if not isinstance(parsed, dict): - raise TypeError("manager_call_config must decode to a dict.") - cfg = dict(parsed) - if self.DEFAULT_KEY not in cfg: - cfg[self.DEFAULT_KEY] = self.DEFAULT_CONFIG[self.DEFAULT_KEY] - else: - raise TypeError(f"cfg_source must be a dict, string, or None, got: {type(cfg_source)}") - - # validation - for manager_name, mode_value in cfg.items(): - if not isinstance(mode_value, int): - raise TypeError( - f"manager_call_config value for '{manager_name}' must be int (0/1/2), got: {type(mode_value)}" - ) - try: - ManagerCallMode(mode_value) - except ValueError as exc: - raise ValueError( - f"Invalid manager_call_config value for '{manager_name}': {mode_value}. Expected 0/1/2." - ) from exc - - # Apply max mode caps: bake caps into the resolved config so - # get_mode_for_manager never needs per-call branching. - default_mode = cfg[self.DEFAULT_KEY] - for name, max_mode in self._max_modes.items(): - resolved = cfg.get(name, default_mode) - if resolved > max_mode: - cfg[name] = max_mode - - return cfg diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_cfg.py b/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_cfg.py index 767fbb0451f9..b938f7e446e0 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_cfg.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_cfg.py @@ -25,6 +25,9 @@ class NoiseCfg: Experimental fork of :class:`isaaclab.utils.noise.NoiseCfg` adapted for the Warp-first calling convention where noise functions operate **in-place** on a ``wp.array`` buffer and return ``None``. + + The observation manager creates a private runtime copy that owns its persistent + RNG state and launch cache; the user configuration remains serializable. """ func: Callable[[wp.array, NoiseCfg], None] = MISSING diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_model.py b/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_model.py index acba926ea488..01a1059eb28f 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_model.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/noise/noise_model.py @@ -6,13 +6,14 @@ """Warp-native noise functions and models (experimental). Each noise function takes a ``wp.array`` as its first argument and operates **in-place** -via ``wp.launch``. The calling convention mirrors the stable noise interface:: +through a Warp kernel. The calling convention mirrors the stable noise interface:: noise_cfg.func(data_wp, noise_cfg) -> None Random noise kernels (gaussian, uniform) consume the shared per-env Warp RNG state -(``rng_state_wp``) that is set on the config at manager prep time from -``env.rng_state_wp``. See :func:`initialize_rng_state` in +(``rng_state_wp``) that is set on a manager-owned runtime copy at preparation time +from ``env.rng_state_wp``. Prepared noise kernels also use the environment's launch cache. +See :func:`initialize_rng_state` in ``isaaclab_experimental.envs.manager_based_env_warp`` for the initialization pattern. """ @@ -31,6 +32,22 @@ _OPERATION_MAP: dict[str, int] = {"add": 0, "scale": 1, "abs": 2} + +def _launch_noise( + data: wp.array, + cfg: noise_cfg.NoiseCfg, + kernel: wp.Kernel, + inputs: list, + site: tuple, +) -> None: + """Launch a noise kernel through its prepared owner cache when available.""" + launch_cache = getattr(cfg, "_warp_launch", None) + if launch_cache is None: + wp.launch(kernel, dim=data.shape[0], inputs=inputs, device=data.device) + return + launch_cache.launch(kernel, dim=data.shape[0], inputs=inputs, site=site) + + ## # Noise as functions. ## @@ -64,11 +81,13 @@ def constant_noise(data: wp.array, cfg: noise_cfg.ConstantNoiseCfg) -> None: data: The data buffer to modify. Shape ``(num_envs, D)``. cfg: The configuration parameters for constant noise. """ - wp.launch( + operation = _OPERATION_MAP[cfg.operation] + _launch_noise( + data, + cfg, _apply_constant_noise, - dim=data.shape[0], - inputs=[data, float(cfg.bias), _OPERATION_MAP[cfg.operation]], - device=data.device, + inputs=[data, float(cfg.bias), operation], + site=("constant_noise", data, float(cfg.bias), operation), ) @@ -105,11 +124,13 @@ def uniform_noise(data: wp.array, cfg: noise_cfg.UniformNoiseCfg) -> None: data: The data buffer to modify. Shape ``(num_envs, D)``. cfg: The configuration parameters for uniform noise. """ - wp.launch( + operation = _OPERATION_MAP[cfg.operation] + _launch_noise( + data, + cfg, _apply_uniform_noise, - dim=data.shape[0], - inputs=[data, cfg.rng_state_wp, float(cfg.n_min), float(cfg.n_max), _OPERATION_MAP[cfg.operation]], - device=data.device, + inputs=[data, cfg.rng_state_wp, float(cfg.n_min), float(cfg.n_max), operation], + site=("uniform_noise", data, cfg.rng_state_wp, float(cfg.n_min), float(cfg.n_max), operation), ) @@ -146,11 +167,13 @@ def gaussian_noise(data: wp.array, cfg: noise_cfg.GaussianNoiseCfg) -> None: data: The data buffer to modify. Shape ``(num_envs, D)``. cfg: The configuration parameters for gaussian noise. """ - wp.launch( + operation = _OPERATION_MAP[cfg.operation] + _launch_noise( + data, + cfg, _apply_gaussian_noise, - dim=data.shape[0], - inputs=[data, cfg.rng_state_wp, float(cfg.mean), float(cfg.std), _OPERATION_MAP[cfg.operation]], - device=data.device, + inputs=[data, cfg.rng_state_wp, float(cfg.mean), float(cfg.std), operation], + site=("gaussian_noise", data, cfg.rng_state_wp, float(cfg.mean), float(cfg.std), operation), ) diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/warp/__init__.py b/source/isaaclab_experimental/isaaclab_experimental/utils/warp/__init__.py index 2d071b823a46..c7e33acf47bf 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/warp/__init__.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/warp/__init__.py @@ -5,12 +5,21 @@ """Warp utility functions and shared kernels for isaaclab_experimental.""" -from .kernels import compute_reset_scale, count_masked +from isaaclab.utils.warp.utils import resolve_1d_mask + +from .kernels import ( + compute_reset_scale, + count_masked, + increment_all_int32, + increment_all_int64, + zero_masked_int32, + zero_masked_int64, +) from .utils import ( + SYNC_DEBUG_ENV_VAR, WarpCapturable, - is_warp_capturable, - resolve_1d_mask, - warp_capturable, + any_env_set, + sync_debug_enabled, wrap_to_pi, zero_masked_2d, ) diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/warp/kernels.py b/source/isaaclab_experimental/isaaclab_experimental/utils/warp/kernels.py index 8d3f9e65d49a..dbfd3740a61b 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/warp/kernels.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/warp/kernels.py @@ -10,6 +10,36 @@ import warp as wp +@wp.kernel +def increment_all_int32(values: wp.array(dtype=wp.int32), increment: wp.int32): + """Increment every value in a one-dimensional int32 array.""" + index = wp.tid() + values[index] = values[index] + increment + + +@wp.kernel +def increment_all_int64(values: wp.array(dtype=wp.int64), increment: wp.int64): + """Increment every value in a one-dimensional int64 array.""" + index = wp.tid() + values[index] = values[index] + increment + + +@wp.kernel +def zero_masked_int32(mask: wp.array(dtype=wp.bool), values: wp.array(dtype=wp.int32)): + """Zero selected values in a one-dimensional int32 array.""" + index = wp.tid() + if mask[index]: + values[index] = 0 + + +@wp.kernel +def zero_masked_int64(mask: wp.array(dtype=wp.bool), values: wp.array(dtype=wp.int64)): + """Zero selected values in a one-dimensional int64 array.""" + index = wp.tid() + if mask[index]: + values[index] = wp.int64(0) + + @wp.kernel def count_masked( mask: wp.array(dtype=wp.bool), diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/warp/utils.py b/source/isaaclab_experimental/isaaclab_experimental/utils/warp/utils.py index a90f6b2d6fc6..87c4833f89e9 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/warp/utils.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/warp/utils.py @@ -5,126 +5,36 @@ from __future__ import annotations -from collections.abc import Sequence +import os import torch import warp as wp +SYNC_DEBUG_ENV_VAR = "ISAACLAB_SYNC_DEBUG" +"""Set ``ISAACLAB_SYNC_DEBUG=1`` to trap hidden GPU->host syncs in warp stages (CI/debug).""" -@wp.kernel -def _set_mask_from_ids( - mask: wp.array(dtype=wp.bool), - ids: wp.array(dtype=wp.int32), -): - """Set ``mask[ids[i]] = True`` for each thread *i*.""" - i = wp.tid() - mask[ids[i]] = True - - -def resolve_1d_mask( - *, - ids: Sequence[int] | slice | wp.array | torch.Tensor | None, - mask: wp.array | torch.Tensor | None, - all_mask: wp.array, - scratch_mask: wp.array, - device: str, -) -> wp.array: - """Resolve ids/mask into a warp boolean mask. - - Matches the contract of ``ArticulationData._resolve_1d_mask`` on dev/newton. - Callers must provide pre-allocated ``all_mask`` (all-True) and ``scratch_mask`` - (reusable working buffer). No allocations happen inside this function. - - Args: - ids: Indices to set to ``True``. ``None`` or ``slice(None)`` means all. - mask: Explicit boolean mask. If provided, returned directly (after - torch->warp normalization if needed). Takes precedence over *ids*. - all_mask: Pre-allocated all-True mask of shape ``(size,)``, returned - when both *ids* and *mask* are ``None``. - scratch_mask: Pre-allocated scratch mask of shape ``(size,)``, filled - in-place when *ids* are provided. - device: Warp device string. - - Returns: - A ``wp.array(dtype=wp.bool)`` -- ``mask``, ``all_mask``, or ``scratch_mask``. - """ - # Fast path: explicit mask provided. - if mask is not None: - if isinstance(mask, torch.Tensor): - if mask.dtype != torch.bool: - mask = mask.to(dtype=torch.bool) - if str(mask.device) != device: - mask = mask.to(device) - return wp.from_torch(mask, dtype=wp.bool) - return mask - - # Fast path: all ids. - if ids is None or (isinstance(ids, slice) and ids == slice(None)): - return all_mask - - # Normalize slice into explicit indices. - if isinstance(ids, slice): - start, stop, step = ids.indices(scratch_mask.shape[0]) - ids = list(range(start, stop, step)) - elif not isinstance(ids, (torch.Tensor, wp.array)): - ids = list(ids) - - # Prepare output mask. - scratch_mask.fill_(False) - - # Normalize ids to wp.int32 array and launch kernel. - if isinstance(ids, torch.Tensor): - if ids.numel() == 0: - return scratch_mask - if str(ids.device) != device: - ids = ids.to(device) - if ids.dtype != torch.int32: - ids = ids.to(dtype=torch.int32) - if not ids.is_contiguous(): - ids = ids.contiguous() - ids_wp = wp.from_torch(ids, dtype=wp.int32) - elif isinstance(ids, wp.array): - if ids.shape[0] == 0: - return scratch_mask - ids_wp = ids - else: - if len(ids) == 0: - return scratch_mask - ids_wp = wp.array(ids, dtype=wp.int32, device=device) - - wp.launch(kernel=_set_mask_from_ids, dim=ids_wp.shape[0], inputs=[scratch_mask, ids_wp], device=device) - return scratch_mask - - -def warp_capturable(capturable: bool): - """Annotate an MDP term's CUDA-graph capturability. - - No-wrapper decorator: sets ``_warp_capturable`` directly on the function - and returns it unchanged. Safe to stack with any other decorator in any order. - - By default all MDP terms are assumed capturable (True). Use - ``@warp_capturable(False)`` on terms that call non-capturable external APIs. - """ - def decorator(func): - func._warp_capturable = capturable - return func +def sync_debug_enabled() -> bool: + """Whether the sync-debug trap is requested for this process.""" + return os.environ.get(SYNC_DEBUG_ENV_VAR, "0") == "1" - return decorator +def any_env_set(mask: torch.Tensor) -> bool: + """Host predicate: does the boolean mask select any environment? -def is_warp_capturable(func) -> bool: - """Check if a term function is CUDA-graph-capturable. - - Checks ``_warp_capturable`` on the function and its ``__wrapped__`` target. - Returns True (capturable) by default if no annotation is found. + This is the single sanctioned per-step host sync of the mask-native reset + pipeline (one predicate instead of dispatching an empty reset). It suspends + the sync-debug trap around itself so the exemption is code, not annotation. """ - for f in (func, getattr(func, "__wrapped__", None)): - if f is not None: - val = getattr(f, "_warp_capturable", None) - if val is not None: - return val - return True + if mask.is_cuda: + prev = torch.cuda.get_sync_debug_mode() + if prev != 0: + torch.cuda.set_sync_debug_mode(0) + try: + return bool(mask.any().item()) + finally: + torch.cuda.set_sync_debug_mode(prev) + return bool(mask.any().item()) @wp.func @@ -140,32 +50,40 @@ def wrap_to_pi(angle: float) -> float: class WarpCapturable: """CUDA graph capture safety: decorator, annotation checker, and runtime guard. - Decorator usage:: - - @WarpCapturable(False) - def reset_root_state_uniform(env, env_mask, ...): - ... + The single capturability annotation (see ``is_capturable``). Decorator usage:: @WarpCapturable(False, reason="calls write_root_pose_to_sim") def push_by_setting_velocity(env, env_mask, ...): ... + @WarpCapturable(False, reason="notifies the solver of model changes") + class randomize_rigid_body_com(ManagerTermBase): + ... + - ``@WarpCapturable(True)`` or no decorator: capturable, returned unwrapped. - - ``@WarpCapturable(False)``: sets ``func._warp_capturable = False``, wraps with - runtime guard that raises if ``wp.get_device().is_capturing`` is ``True``. + - ``@WarpCapturable(False)`` on a function: sets ``_warp_capturable = False`` and + wraps it with a runtime guard that raises if called during CUDA graph capture. + - ``@WarpCapturable(False)`` on a class term: annotates the class (so manager + registration sees it) and guards its ``__call__``. """ def __init__(self, capturable: bool, *, reason: str | None = None): self._capturable = capturable self._reason = reason - def __call__(self, func): - """Decorate *func* with capture safety annotation and optional runtime guard.""" - import functools - - func._warp_capturable = self._capturable + def __call__(self, target): + """Decorate a term function or class with the annotation and optional guard.""" + target._warp_capturable = self._capturable if self._capturable: - return func + return target + if isinstance(target, type): + target.__call__ = self._guarded(target.__call__) + return target + return self._guarded(target) + + def _guarded(self, func): + """Wrap *func* so calling it during CUDA graph capture raises.""" + import functools reason = self._reason @@ -182,12 +100,14 @@ def wrapper(*args, **kwargs): return wrapper @staticmethod - def is_capturable(func) -> bool: - """Check capturability annotation. Default: ``True``. + def is_capturable(term) -> bool: + """Check the capturability annotation on a term function, class, or instance. Checks ``__wrapped__`` for decorated functions to handle stacked decorators. + Unannotated terms default to capturable; hidden syncs in them are trapped by + the ``ISAACLAB_SYNC_DEBUG`` stage sweep and by CUDA graph capture itself. """ - for f in (func, getattr(func, "__wrapped__", None)): + for f in (term, getattr(term, "__wrapped__", None)): if f is not None: val = getattr(f, "_warp_capturable", None) if val is not None: diff --git a/source/isaaclab_experimental/isaaclab_experimental/utils/warp_graph_cache.py b/source/isaaclab_experimental/isaaclab_experimental/utils/warp_graph_cache.py index d446be28d099..e999749072e6 100644 --- a/source/isaaclab_experimental/isaaclab_experimental/utils/warp_graph_cache.py +++ b/source/isaaclab_experimental/isaaclab_experimental/utils/warp_graph_cache.py @@ -5,20 +5,53 @@ """Warp CUDA graph capture-or-replay utility.""" +import os from collections.abc import Callable from typing import Any +import torch import warp as wp +from isaaclab.utils.timer import Timer + +SYNC_DEBUG_ENV_VAR = "ISAACLAB_SYNC_DEBUG" +"""Set ``ISAACLAB_SYNC_DEBUG=1`` to run eager and warm-up stage invocations under +``torch.cuda.set_sync_debug_mode("error")`` so hidden GPU->host syncs raise (CI/debug).""" + +_SYNC_DEBUG = os.environ.get(SYNC_DEBUG_ENV_VAR, "0") == "1" + + +def _invoke_stage(fn: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any]) -> Any: + """Run one stage callable, optionally under the sync-debug trap. + + The trap is the choke point that guards every current and future term inside a + warp stage without per-function annotations; opt-outs suspend it explicitly + (see :func:`isaaclab_experimental.utils.warp.any_env_set`). + """ + if not _SYNC_DEBUG: + return fn(*args, **kwargs) + prev = torch.cuda.get_sync_debug_mode() + torch.cuda.set_sync_debug_mode(2) + try: + return fn(*args, **kwargs) + finally: + torch.cuda.set_sync_debug_mode(prev) + + +CAPTURE_ENV_VAR = "ISAACLAB_WARP_CAPTURE" +"""Set ``ISAACLAB_WARP_CAPTURE=0`` to force every stage eager (debug / A-B validation).""" + class WarpGraphCache: - """Caches Warp CUDA graphs by stage name: captures on first call, replays after. + """Execute Warp stages eagerly or through cached CUDA graphs. - On the very first call for a given stage, an **eager warm-up** run - executes *before* graph capture. This lets one-time initialisation - code (memory allocations, torch dtype casts, ``hasattr`` guards, etc.) - run outside the capture context. Only the steady-state kernel - launches are then recorded into the graph. + :meth:`call` uses the first invocation as an eager warm-up, captures the + second invocation, and replays later invocations. This lets one-time + initialization run outside capture without executing stateful manager work + twice in one environment step. :meth:`capture_or_replay` captures its stage + on the first invocation and launches the new graph to perform the first + transition; one-time setup must happen before that call, preferably in the + owning object's constructor. The return value from the capture run is cached and returned on every subsequent replay, ensuring captured stages return the same references @@ -27,20 +60,88 @@ class WarpGraphCache: Usage:: cache = WarpGraphCache() - result = cache.capture_or_replay("my_stage", my_warp_function) + result = cache.call("MyManager_step", my_warp_function) # uncaptured work here ... - result2 = cache.capture_or_replay("my_stage_post", my_other_function) + result2 = cache.call("OtherManager_step", my_other_function) """ - def __init__(self): + def __init__(self, *, enabled: bool = True, device: wp.DeviceLike = None): + """Initialize the cache. + + Args: + enabled: Whether eligible stages may use CUDA graph capture. The + :data:`CAPTURE_ENV_VAR` environment variable can force this off. + device: Warp device used by cached stages. CPU devices always run eagerly. + """ + self._enabled = enabled and os.environ.get(CAPTURE_ENV_VAR, "1") != "0" + self._device = wp.get_device(device) if device is not None else None self._graphs: dict[str, Any] = {} self._results: dict[str, Any] = {} + self._capturable: dict[str, bool] = {} + self._warmed: set[str] = set() + + @property + def captured_stages(self) -> tuple[str, ...]: + """Stages currently backed by a captured CUDA graph, sorted by name.""" + return tuple(sorted(self._graphs)) + + @property + def eager_groups(self) -> tuple[str, ...]: + """Stage groups registered as non-capturable, sorted by name.""" + return tuple(sorted(group for group, capturable in self._capturable.items() if not capturable)) + + def call( + self, + stage: str, + fn: Callable[..., Any], + /, + *args: Any, + output: Callable[[Any], Any] | None = None, + timer: bool = False, + **kwargs: Any, + ) -> Any: + """Run a Warp frontend stage eagerly or through its cached CUDA graph. + + The stage prefix before the first underscore identifies its capturability + group. A group stays eager once any of its terms is registered as unsafe. + Eligible stages run eagerly once, capture on their second invocation, and + replay thereafter. + + Args: + stage: Stage identifier in the form ``"GroupName_function_name"``. + fn: Callable implementing the stage. + *args: Positional arguments forwarded to :paramref:`fn`. + output: Optional transform applied to the stage result after execution. + timer: Whether to time the stage execution. + **kwargs: Keyword arguments forwarded to :paramref:`fn`. + + Returns: + The stage result, optionally transformed by :paramref:`output`. + """ + group = stage.partition("_")[0] + with Timer(name=stage, msg=f"{stage} took:", enable=timer, time_unit="us"): + if not self._capture_enabled or not self.is_capturable(group): + result = _invoke_stage(fn, args, kwargs) + elif stage in self._graphs: + wp.capture_launch(self._graphs[stage]) + result = self._results[stage] + elif stage not in self._warmed: + # The first real call doubles as warm-up. Stateful manager stages + # must execute exactly once per environment step. + result = _invoke_stage(fn, args, kwargs) + self._warmed.add(stage) + else: + result = self._capture(stage, fn, args, kwargs) + # CUDA graph capture records launches without executing them. Launch + # the new graph so this logical manager call still advances once. + wp.capture_launch(self._graphs[stage]) + return output(result) if output is not None else result def capture_or_replay( self, stage: str, fn: Callable[..., Any], - args: tuple = (), + args: tuple[Any, ...] = (), kwargs: dict[str, Any] | None = None, ) -> Any: """Capture *fn* into a CUDA graph on the first call, then replay. @@ -58,24 +159,76 @@ def capture_or_replay( """ if kwargs is None: kwargs = {} + if not self._capture_enabled: + return _invoke_stage(fn, args, kwargs) graph = self._graphs.get(stage) if graph is not None: wp.capture_launch(graph) return self._results[stage] - # Warm-up: run eagerly to flush first-call allocations / hasattr guards. - fn(*args, **kwargs) - # Capture: allocations already done, only wp.launch calls are recorded. - with wp.ScopedCapture() as capture: + result = self._capture(stage, fn, args, kwargs) + # CUDA graph capture records launches without executing them. Launch the + # new graph so the first logical invocation still advances exactly once. + wp.capture_launch(self._graphs[stage]) + return result + + def _capture( + self, + stage: str, + fn: Callable[..., Any], + args: tuple[Any, ...], + kwargs: dict[str, Any], + ) -> Any: + """Record one stage invocation into a CUDA graph without executing it.""" + with wp.ScopedCapture(device=self._device) as capture: result = fn(*args, **kwargs) self._graphs[stage] = capture.graph self._results[stage] = result + self._warmed.add(stage) + # One grep-able line per stage so runs can assert capture coverage. + print(f"[INFO] WarpGraphCache: captured stage '{stage}'") return result + def register_capturability(self, key: str, capturable: bool) -> None: + """Register conservative capture eligibility for a stage group. + + Args: + key: Stage group identifier. + capturable: Whether every operation registered under the group is safe + during CUDA graph capture. + """ + self._capturable[key] = self._capturable.get(key, True) and capturable + + def is_capturable(self, key: str) -> bool: + """Return whether a stage group is eligible for capture.""" + return self._capturable.get(key, True) + + @property + def _capture_enabled(self) -> bool: + """Return whether this cache can capture on its configured device.""" + device = self._device if self._device is not None else wp.get_device() + return self._enabled and device.is_cuda + def invalidate(self, stage: str | None = None) -> None: - """Drop cached graph(s). If *stage* is ``None``, drop all.""" + """Drain and drop one captured graph or every captured graph. + + Args: + stage: Stage to invalidate. If ``None``, invalidate every stage. + """ if stage is None: + self._synchronize_graphs(tuple(self._graphs.values())) self._graphs.clear() self._results.clear() + self._warmed.clear() else: + graph = self._graphs.get(stage) + self._synchronize_graphs(() if graph is None else (graph,)) self._graphs.pop(stage, None) self._results.pop(stage, None) + self._warmed.discard(stage) + + @staticmethod + def _synchronize_graphs(graphs: tuple[Any, ...]) -> None: + """Synchronize each device referenced by a collection of graphs.""" + devices = {str(graph.device): graph.device for graph in graphs} + for device in devices.values(): + wp.synchronize_device(device) diff --git a/source/isaaclab_experimental/test/envs/mdp/commands/test_commands.py b/source/isaaclab_experimental/test/envs/mdp/commands/test_commands.py new file mode 100644 index 000000000000..9c09c9668662 --- /dev/null +++ b/source/isaaclab_experimental/test/envs/mdp/commands/test_commands.py @@ -0,0 +1,372 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp-native registered command terms.""" + +from __future__ import annotations + +from types import SimpleNamespace +from unittest.mock import Mock + +import numpy as np +import pytest +import torch +import warp as wp +from isaaclab_experimental.envs import mdp as experimental_mdp +from isaaclab_experimental.envs.mdp.commands import ( + UniformPoseCommand, + UniformPoseCommandCfg, + UniformVelocityCommand, + UniformVelocityCommandCfg, +) +from isaaclab_experimental.managers import CommandManager, CommandTerm + +from isaaclab.envs.mdp.commands import UniformPoseCommandCfg as StableUniformPoseCommandCfg +from isaaclab.envs.mdp.commands import UniformVelocityCommandCfg as StableUniformVelocityCommandCfg +from isaaclab.utils.math import combine_frame_transforms, compute_pose_error, quat_from_euler_xyz, quat_unique +from isaaclab.utils.warp import WarpLaunchCache + + +class _ArrayProxy: + """Minimal backend-neutral data proxy used by command terms.""" + + def __init__(self, values: np.ndarray, dtype, device: str = "cpu"): + self.warp = wp.array(values, dtype=dtype, device=device) + self.torch = wp.to_torch(self.warp) + + +class _Robot: + """Minimal articulation data needed by velocity and pose commands.""" + + def __init__(self, num_envs: int, device: str = "cpu"): + identity_quat = np.zeros((num_envs, 4), dtype=np.float32) + identity_quat[:, 3] = 1.0 + identity_pose = np.zeros((num_envs, 7), dtype=np.float32) + identity_pose[:, 6] = 1.0 + self.data = SimpleNamespace( + root_pose_w=_ArrayProxy(identity_pose, wp.transformf, device), + root_vel_w=_ArrayProxy(np.zeros((num_envs, 6), dtype=np.float32), wp.spatial_vectorf, device), + root_pos_w=_ArrayProxy(np.zeros((num_envs, 3), dtype=np.float32), wp.vec3f, device), + root_quat_w=_ArrayProxy(identity_quat, wp.quatf, device), + root_lin_vel_b=_ArrayProxy(np.zeros((num_envs, 3), dtype=np.float32), wp.vec3f, device), + root_ang_vel_b=_ArrayProxy(np.zeros((num_envs, 3), dtype=np.float32), wp.vec3f, device), + heading_w=_ArrayProxy(np.zeros(num_envs, dtype=np.float32), wp.float32, device), + body_pos_w=_ArrayProxy(np.zeros((num_envs, 1, 3), dtype=np.float32), wp.vec3f, device), + body_quat_w=_ArrayProxy(identity_quat[:, None, :], wp.quatf, device), + ) + self.is_initialized = True + + def find_bodies(self, body_name: str) -> tuple[list[int], list[str]]: + return [0], [body_name] + + +class _Env: + """Minimal manager-based environment used by command terms.""" + + def __init__(self, num_envs: int = 4, device: str = "cpu"): + self.num_envs = num_envs + self.device = device + self._warp_launch = WarpLaunchCache(device=self.device) + self.scene = {"robot": _Robot(num_envs, device)} + self.rng_state_wp = wp.array(np.arange(num_envs, dtype=np.uint32) + 41, device=self.device) + self.extras = {} + self.sim = SimpleNamespace( + vis_marker_registry=SimpleNamespace( + add_debug_vis_callback=lambda term: None, + clear_debug_vis_callback=lambda term: None, + ) + ) + + +def _velocity_cfg() -> UniformVelocityCommandCfg: + return UniformVelocityCommandCfg( + asset_name="robot", + resampling_time_range=(2.0, 2.0), + heading_command=True, + heading_control_stiffness=2.0, + rel_heading_envs=1.0, + rel_standing_envs=0.0, + vel_xy_success_threshold=0.5, + vel_yaw_success_threshold=0.4, + ranges=UniformVelocityCommandCfg.Ranges( + lin_vel_x=(1.0, 1.0), + lin_vel_y=(2.0, 2.0), + ang_vel_z=(0.3, 0.3), + heading=(0.7, 0.7), + ), + debug_vis=False, + ) + + +def _pose_cfg() -> UniformPoseCommandCfg: + return UniformPoseCommandCfg( + asset_name="robot", + body_name="tool", + resampling_time_range=(3.0, 3.0), + make_quat_unique=True, + position_success_threshold=0.25, + ranges=UniformPoseCommandCfg.Ranges( + pos_x=(1.0, 1.0), + pos_y=(2.0, 2.0), + pos_z=(3.0, 3.0), + roll=(0.2, 0.2), + pitch=(-0.3, -0.3), + yaw=(4.0, 4.0), + ), + debug_vis=False, + ) + + +def test_configs_exports_and_command_accessors_preserve_shared_contracts(): + """Configs should inherit stable fields and terms should expose debug UI and persistent storage.""" + assert issubclass(UniformVelocityCommandCfg, StableUniformVelocityCommandCfg) + assert issubclass(UniformPoseCommandCfg, StableUniformPoseCommandCfg) + assert experimental_mdp.UniformVelocityCommandCfg is UniformVelocityCommandCfg + assert experimental_mdp.UniformPoseCommandCfg is UniformPoseCommandCfg + assert str(_velocity_cfg().class_type).endswith(".commands.velocity_command:UniformVelocityCommand") + assert str(_pose_cfg().class_type).endswith(".commands.pose_command:UniformPoseCommand") + assert UniformVelocityCommand._set_debug_vis_impl.__module__ == "isaaclab.envs.mdp.commands._debug_vis" + assert UniformPoseCommand._set_debug_vis_impl.__module__ == "isaaclab.envs.mdp.commands._debug_vis" + + env = _Env() + velocity_term = UniformVelocityCommand(_velocity_cfg(), env) + pose_term = UniformPoseCommand(_pose_cfg(), env) + manager = CommandManager.__new__(CommandManager) + manager._terms = {"velocity": velocity_term, "pose": pose_term} + + assert velocity_term.command_wp is velocity_term.command_wp + assert pose_term.command_wp is pose_term.command_wp + assert manager.get_command_wp("velocity") is velocity_term.command_wp + assert manager.get_command_wp("pose") is pose_term.command_wp + assert manager.get_command("velocity") is velocity_term.command + assert manager.get_command("pose") is pose_term.command + torch.testing.assert_close(pose_term.command[:, :6], torch.zeros(4, 6)) + torch.testing.assert_close(pose_term.command[:, 6], torch.ones(4)) + + +def test_debug_visualization_uses_current_simulation_registry(): + """Command terms should register visualization through the current simulation API.""" + registry = SimpleNamespace(add_debug_vis_callback=Mock(return_value="handle"), clear_debug_vis_callback=Mock()) + term = SimpleNamespace( + has_debug_vis_implementation=True, + _set_debug_vis_impl=Mock(), + _debug_vis_handle=None, + _env=SimpleNamespace(sim=SimpleNamespace(vis_marker_registry=registry)), + ) + + assert CommandTerm.set_debug_vis(term, True) + assert term._debug_vis_handle == "handle" + registry.add_debug_vis_callback.assert_called_once_with(term) + + assert CommandTerm.set_debug_vis(term, False) + registry.clear_debug_vis_callback.assert_called_once_with(term) + + CommandTerm.__del__(term) + assert registry.clear_debug_vis_callback.call_count == 2 + + +def test_uniform_velocity_command_updates_and_resets_selected_rows(): + """Velocity commands should update in Warp and preserve unselected rows during sparse reset.""" + env = _Env() + term = UniformVelocityCommand(_velocity_cfg(), env) + term._prepare_reset_extras() + command_pointer = term.command.data_ptr() + command_wp = term.command_wp + + term.command[:] = torch.tensor([[1.0, 2.0, 0.3], [2.0, -1.0, -0.2], [0.0, 0.5, 0.1], [-1.0, -2.0, 0.0]]) + env.scene["robot"].data.root_vel_w.torch[:, :3] = torch.tensor( + [[0.0, 0.0, 0.0], [1.0, -1.0, 0.0], [0.0, 0.0, 0.0], [-2.0, -2.0, 0.0]] + ) + env.scene["robot"].data.root_vel_w.torch[:, 5] = torch.tensor([0.1, -0.1, 0.4, 0.0]) + term._update_metrics() + wp.synchronize() + torch.testing.assert_close( + wp.to_torch(term._error_xy_sum), + torch.tensor([np.sqrt(5.0), 1.0, 0.5, 1.0], dtype=torch.float32), + ) + torch.testing.assert_close(wp.to_torch(term._error_yaw_sum), torch.tensor([0.2, 0.1, 0.3, 0.0])) + + term.command.fill_(-9.0) + wp.to_torch(term._error_xy_sum)[:] = torch.tensor([0.2, 9.0, 2.0, 8.0]) + wp.to_torch(term._error_yaw_sum)[:] = torch.tensor([0.1, 9.0, 2.0, 8.0]) + wp.to_torch(term._step_count)[:] = torch.tensor([1.0, 1.0, 2.0, 1.0]) + wp.to_torch(term.time_left_wp)[:] = torch.tensor([-1.0, 6.0, -1.0, 8.0]) + wp.to_torch(term.command_counter_wp)[:] = torch.tensor([5, 6, 7, 8], dtype=torch.int32) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device="cpu") + + extras = term.reset(env_mask=env_mask) + wp.synchronize() + + assert term.command_wp is command_wp + assert term.command.data_ptr() == command_pointer + torch.testing.assert_close(term.command[[0, 2]], torch.tensor([[1.0, 2.0, 0.3], [1.0, 2.0, 0.3]])) + torch.testing.assert_close(term.command[[1, 3]], torch.full((2, 3), -9.0)) + torch.testing.assert_close(wp.to_torch(term.time_left_wp), torch.tensor([2.0, 6.0, 2.0, 8.0])) + torch.testing.assert_close(wp.to_torch(term.command_counter_wp), torch.tensor([1, 6, 1, 8], dtype=torch.int32)) + torch.testing.assert_close(extras["error_vel_xy"], torch.tensor(0.6)) + torch.testing.assert_close(extras["error_vel_yaw"], torch.tensor(0.55)) + torch.testing.assert_close(extras["success_rate"], torch.tensor(0.5)) + torch.testing.assert_close(wp.to_torch(term._error_xy_sum), torch.tensor([0.0, 9.0, 0.0, 8.0])) + torch.testing.assert_close(wp.to_torch(term._step_count), torch.tensor([0.0, 1.0, 0.0, 1.0])) + + term.command[:] = torch.tensor([[1.0, 2.0, 0.4]]).repeat(4, 1) + term.heading_target_torch[:] = torch.tensor([np.pi / 2, 0.0, np.pi / 2, 0.0]) + term.is_heading_env_torch[:] = torch.tensor([True, False, True, False]) + term.is_standing_env_torch[:] = torch.tensor([False, False, True, True]) + term._update_command() + wp.synchronize() + torch.testing.assert_close( + term.command, + torch.tensor([[1.0, 2.0, 0.3], [1.0, 2.0, 0.4], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), + ) + + +def test_uniform_velocity_command_replay_reads_current_root_state(): + """Captured command kernels should read current Tier-1 root state on every replay.""" + env = _Env() + cfg = _velocity_cfg() + cfg.ranges.ang_vel_z = (-2.0, 2.0) + term = UniformVelocityCommand(cfg, env) + robot_data = env.scene["robot"].data + + term.command.zero_() + term.command[:, 0] = 1.0 + term.heading_target_torch.fill_(np.pi / 2) + term.is_heading_env_torch.fill_(True) + term.is_standing_env_torch.fill_(False) + + with wp.ScopedCapture(device=env.device) as metrics_capture: + term._update_metrics() + with wp.ScopedCapture(device=env.device) as command_capture: + term._update_command() + + # Identity pose with matching world-frame velocity has zero body-frame XY error. + robot_data.root_vel_w.torch[:, 0] = 1.0 + wp.capture_launch(metrics_capture.graph) + wp.synchronize() + torch.testing.assert_close(wp.to_torch(term._error_xy_sum), torch.zeros(env.num_envs)) + + # A 90-degree root yaw exactly matches the heading target, so replay should command zero yaw rate. + half_sqrt = np.sqrt(0.5) + robot_data.root_pose_w.torch[:, 5] = half_sqrt + robot_data.root_pose_w.torch[:, 6] = half_sqrt + wp.capture_launch(command_capture.graph) + wp.synchronize() + torch.testing.assert_close(term.command[:, 2], torch.zeros(env.num_envs), atol=1.0e-6, rtol=1.0e-6) + + +def test_uniform_pose_command_matches_stable_math_and_tracks_sticky_success(): + """Pose commands should use xyzw math, update metrics, and reset sticky success by mask.""" + env = _Env() + term = UniformPoseCommand(_pose_cfg(), env) + term._prepare_reset_extras() + command_pointer = term.command.data_ptr() + command_wp = term.command_wp + term.command.fill_(-9.0) + wp.to_torch(term.time_left_wp)[:] = torch.tensor([-1.0, 6.0, -1.0, 8.0]) + wp.to_torch(term.command_counter_wp)[:] = torch.tensor([5, 6, 7, 8], dtype=torch.int32) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device="cpu") + + term.reset(env_mask=env_mask) + wp.synchronize() + + expected_quat = quat_unique(quat_from_euler_xyz(torch.tensor([0.2]), torch.tensor([-0.3]), torch.tensor([4.0])))[0] + assert term.command_wp is command_wp + assert term.command.data_ptr() == command_pointer + torch.testing.assert_close(term.command[[0, 2], :3], torch.tensor([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]])) + torch.testing.assert_close(term.command[[0, 2], 3:], expected_quat.repeat(2, 1), atol=1.0e-6, rtol=1.0e-6) + torch.testing.assert_close(term.command[[1, 3]], torch.full((2, 7), -9.0)) + torch.testing.assert_close(wp.to_torch(term.time_left_wp), torch.tensor([3.0, 6.0, 3.0, 8.0])) + torch.testing.assert_close(wp.to_torch(term.command_counter_wp), torch.tensor([1, 6, 1, 8], dtype=torch.int32)) + + term.command[[1, 3]] = torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]) + + roll = torch.tensor([0.0, 0.1, -0.2, 0.3]) + pitch = torch.tensor([0.0, -0.1, 0.2, -0.3]) + yaw = torch.tensor([0.5, -0.4, 0.3, -0.2]) + root_quat = quat_from_euler_xyz(roll, pitch, yaw) + env.scene["robot"].data.root_pos_w.torch[:] = torch.tensor( + [[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [-5.0, 1.0, 2.0], [1.0, 2.0, 3.0]] + ) + env.scene["robot"].data.root_quat_w.torch[:] = root_quat + expected_position_w, expected_orientation_w = combine_frame_transforms( + env.scene["robot"].data.root_pos_w.torch, + root_quat, + term.command[:, :3], + term.command[:, 3:], + ) + offsets = torch.tensor([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.1, 0.0, 0.0], [0.5, 0.0, 0.0]]) + env.scene["robot"].data.body_pos_w.torch[:, 0] = expected_position_w + offsets + identity_quat = torch.zeros(4, 4) + identity_quat[:, 3] = 1.0 + env.scene["robot"].data.body_quat_w.torch[:, 0] = identity_quat + + term._update_metrics() + wp.synchronize() + + expected_position_delta, expected_orientation_delta = compute_pose_error( + expected_position_w, + expected_orientation_w, + env.scene["robot"].data.body_pos_w.torch[:, 0], + env.scene["robot"].data.body_quat_w.torch[:, 0], + ) + torch.testing.assert_close(term.pose_command_w_torch[:, :3], expected_position_w, atol=1.0e-5, rtol=1.0e-5) + torch.testing.assert_close(term.pose_command_w_torch[:, 3:], expected_orientation_w, atol=1.0e-5, rtol=1.0e-5) + torch.testing.assert_close( + wp.to_torch(term.metrics["position_error"]), torch.linalg.norm(expected_position_delta, dim=-1) + ) + torch.testing.assert_close( + wp.to_torch(term.metrics["orientation_error"]), + torch.linalg.norm(expected_orientation_delta, dim=-1), + atol=1.0e-5, + rtol=1.0e-5, + ) + torch.testing.assert_close(wp.to_torch(term._success_rate), torch.tensor([1.0, 0.0, 1.0, 0.0])) + + env.scene["robot"].data.body_pos_w.torch[0, 0] += 2.0 + term._update_metrics() + wp.synchronize() + torch.testing.assert_close(wp.to_torch(term._success_rate), torch.tensor([1.0, 0.0, 1.0, 0.0])) + + reset_mask = wp.array([True, False, False, True], dtype=wp.bool, device="cpu") + extras = term.reset(env_mask=reset_mask) + wp.synchronize() + torch.testing.assert_close(extras["success_rate"], torch.tensor(0.5)) + torch.testing.assert_close(wp.to_torch(term._success_rate), torch.tensor([0.0, 0.0, 1.0, 0.0])) + + +@pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") +def test_command_replay_uses_changed_configuration_scalars(): + """Recorded command launches should specialize changed range and heading scalars.""" + env = _Env(device="cuda:0") + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=env.device) + env_mask = wp.ones(env.num_envs, dtype=wp.bool, device=env.device) + + pose_term = UniformPoseCommand(_pose_cfg(), env) + pose_term._resample_command(env_mask) + wp.synchronize() + torch.testing.assert_close(pose_term.command[:, 0], torch.ones(env.num_envs, device=env.device)) + + pose_term.cfg.ranges.pos_x = (4.0, 4.0) + pose_term._resample_command(env_mask) + wp.synchronize() + torch.testing.assert_close(pose_term.command[:, 0], torch.full((env.num_envs,), 4.0, device=env.device)) + + velocity_cfg = _velocity_cfg() + velocity_cfg.ranges.ang_vel_z = (-10.0, 10.0) + velocity_term = UniformVelocityCommand(velocity_cfg, env) + velocity_term.command.zero_() + velocity_term.heading_target.fill_(0.5) + velocity_term.is_heading_env.fill_(True) + velocity_term.is_standing_env.fill_(False) + velocity_term._update_command() + wp.synchronize() + torch.testing.assert_close(velocity_term.command[:, 2], torch.ones(env.num_envs, device=env.device)) + + velocity_term.cfg.heading_control_stiffness = 4.0 + velocity_term.command[:, 2] = 0.0 + velocity_term._update_command() + wp.synchronize() + torch.testing.assert_close(velocity_term.command[:, 2], torch.full((env.num_envs,), 2.0, device=env.device)) diff --git a/source/isaaclab_experimental/test/envs/mdp/parity_helpers.py b/source/isaaclab_experimental/test/envs/mdp/parity_helpers.py index 4ceec2ecb60b..86a352958a15 100644 --- a/source/isaaclab_experimental/test/envs/mdp/parity_helpers.py +++ b/source/isaaclab_experimental/test/envs/mdp/parity_helpers.py @@ -383,7 +383,16 @@ class MockScene: def __init__(self, assets: dict, env_origins, sensors=None): self._assets = assets - self.env_origins = env_origins + if env_origins is None: + # Action-term tests don't need origins; keep the attributes present but empty. + self.env_origins = None + self.env_origins_wp = None + elif isinstance(env_origins, wp.array): + self.env_origins_wp = env_origins + self.env_origins = wp.to_torch(env_origins) + else: + self.env_origins = env_origins + self.env_origins_wp = wp.from_torch(env_origins, dtype=wp.vec3f) self.sensors = sensors or {} self.articulations = dict(assets) self.rigid_objects = {} @@ -533,11 +542,15 @@ class MockCommandManager: def __init__(self, command_tensor: torch.Tensor, cmd_term: MockCommandTerm): self._cmd = command_tensor + self._cmd_wp = wp.from_torch(command_tensor, dtype=wp.float32) self._term = cmd_term def get_command(self, name: str) -> torch.Tensor: return self._cmd + def get_command_wp(self, name: str) -> wp.array(dtype=wp.float32, ndim=2): + return self._cmd_wp + def get_term(self, name: str): return self._term diff --git a/source/isaaclab_experimental/test/envs/mdp/test_actions_warp_parity.py b/source/isaaclab_experimental/test/envs/mdp/test_actions_warp_parity.py index f2db4f64fbe9..3c7d8ed2bf33 100644 --- a/source/isaaclab_experimental/test/envs/mdp/test_actions_warp_parity.py +++ b/source/isaaclab_experimental/test/envs/mdp/test_actions_warp_parity.py @@ -12,6 +12,8 @@ import torch import warp as wp +from isaaclab.utils.warp import WarpLaunchCache + wp.init() pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") @@ -42,6 +44,7 @@ def __init__(self, asset): self.scene = MockScene({"robot": asset}, env_origins=None) self.num_envs = NUM_ENVS self.device = DEVICE + self._warp_launch = WarpLaunchCache(device=DEVICE) # ============================================================================ diff --git a/source/isaaclab_experimental/test/envs/mdp/test_events_warp_parity.py b/source/isaaclab_experimental/test/envs/mdp/test_events_warp_parity.py index 24b9eb022665..f7405ecb4a84 100644 --- a/source/isaaclab_experimental/test/envs/mdp/test_events_warp_parity.py +++ b/source/isaaclab_experimental/test/envs/mdp/test_events_warp_parity.py @@ -7,16 +7,21 @@ from __future__ import annotations +from collections.abc import Hashable + import numpy as np import pytest import torch import warp as wp +from isaaclab.utils.warp import WarpLaunchCache + # Skip entire module if no CUDA device available wp.init() pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") import isaaclab_experimental.envs.mdp.events as warp_evt +from isaaclab_experimental.managers import EventTermCfg, ManagerTermBase, SceneEntityCfg from parity_helpers import ( DEVICE, NUM_ACTIONS, @@ -37,23 +42,29 @@ # ============================================================================ -@pytest.fixture(autouse=True) -def _clear_function_caches(): - """Clear first-call caches on warp MDP functions so each test starts fresh. +class _LaunchRecorder: + """Record launch sites while executing kernels eagerly.""" - Functions that cache warp views via the ``hasattr`` pattern need clearing - between tests to avoid stale references from prior fixtures. - """ - yield - for fn in ( - warp_evt.push_by_setting_velocity, - warp_evt.apply_external_force_torque, - warp_evt.reset_root_state_uniform, - warp_evt.randomize_rigid_body_com, - ): - for attr in list(vars(fn)): - if attr.startswith("_"): - delattr(fn, attr) + def __init__(self, device: str): + self._device = device + self.sites: list[Hashable | None] = [] + self.invalidated_sites: list[Hashable | None] = [] + + def launch( + self, + kernel: wp.Kernel, + dim: int, + inputs=(), + outputs=(), + *, + stream: wp.Stream | None = None, + site: Hashable | None = None, + ) -> None: + self.sites.append(site) + wp.launch(kernel, dim=dim, inputs=inputs, outputs=outputs, device=self._device, stream=stream) + + def invalidate(self, site: Hashable | None = None) -> None: + self.invalidated_sites.append(site) @pytest.fixture() @@ -99,14 +110,24 @@ class _Env: env.action_manager = MockActionManagerWarp(action_wp[0], action_wp[1]) env.num_envs = NUM_ENVS env.device = DEVICE + env.env_origins_wp = scene.env_origins_wp env.episode_length_buf = episode_length_buf env.step_dt = 0.02 env.max_episode_length_s = 10.0 # RNG state for events (seeded deterministically) env.rng_state_wp = wp.array(np.arange(NUM_ENVS, dtype=np.uint32) + 42, device=DEVICE) + env._warp_launch = _LaunchRecorder(env.device) return env +@pytest.fixture() +def replay_warp_env(warp_env): + """Warp environment with recorded-launch debug validation enabled.""" + warp_env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=warp_env.device) + yield warp_env + warp_env._warp_launch.reset() + + @pytest.fixture() def stable_env(scene, action_wp, episode_length_buf): """Env with torch action manager (for stable functions).""" @@ -130,6 +151,38 @@ def all_joints_cfg(): return MockSceneEntityCfg("robot", list(range(NUM_JOINTS)), NUM_JOINTS, DEVICE) +def _make_event_env(seed: int, *, num_bodies: int = 1): + """Create an independent Warp event environment for ownership tests.""" + + class _Env: + pass + + data = MockArticulationData(NUM_ENVS, NUM_JOINTS, DEVICE, seed=seed, num_bodies=num_bodies) + asset = MockArticulation(data, num_bodies=num_bodies) + origins = wp.zeros(NUM_ENVS, dtype=wp.vec3f, device=DEVICE) + env = _Env() + env.scene = MockScene({"robot": asset}, origins) + env.num_envs = NUM_ENVS + env.device = DEVICE + env.env_origins_wp = env.scene.env_origins_wp + env.rng_state_wp = wp.array(np.arange(NUM_ENVS, dtype=np.uint32) + seed, device=DEVICE) + env._warp_launch = _LaunchRecorder(env.device) + return env, data, asset + + +def _make_event_term( + term_type: type[ManagerTermBase], + env: object, + *, + mode: str = "reset", + **params: object, +) -> ManagerTermBase: + """Create a persistent Warp event term for a mock environment.""" + params.setdefault("asset_cfg", SceneEntityCfg("robot")) + cfg = EventTermCfg(func=term_type, mode=mode, params=params) + return term_type(cfg, env) + + # ============================================================================ # Event parity tests: deterministic (zero-width range) warp vs stable # ============================================================================ @@ -170,6 +223,17 @@ def test_reset_joints_by_offset_parity(self, warp_env, stable_env, art_data, all assert_close(warp_pos, expected_pos) assert_close(warp_vel, expected_vel) + asset = warp_env.scene[cfg.name] + assert warp_env._warp_launch.sites == [ + ( + "reset_joints_by_offset", + asset, + cfg.joint_ids_wp, + mask, + (0.0, 0.0), + (0.0, 0.0), + ) + ] def test_reset_joints_by_scale_parity(self, warp_env, stable_env, art_data, all_joints_cfg): """Scale=1.0: both warp and stable should produce clamped defaults.""" @@ -197,6 +261,17 @@ def test_reset_joints_by_scale_parity(self, warp_env, stable_env, art_data, all_ assert_close(warp_pos, expected_pos) assert_close(warp_vel, expected_vel) + asset = warp_env.scene[cfg.name] + assert warp_env._warp_launch.sites == [ + ( + "reset_joints_by_scale", + asset, + cfg.joint_ids_wp, + mask, + (1.0, 1.0), + (1.0, 1.0), + ) + ] # ============================================================================ @@ -209,8 +284,9 @@ class TestEventCapturedDataMutation: # -- reset_joints_by_offset ------------------------------------------------- - def test_reset_joints_by_offset(self, warp_env, art_data, all_joints_cfg): - """With zero-width offset, result == defaults. Mutate defaults -> result tracks.""" + def test_reset_joints_by_offset(self, replay_warp_env, art_data, all_joints_cfg): + """Debug-validated replay inside capture should read mutated defaults.""" + warp_env = replay_warp_env cfg = all_joints_cfg mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) @@ -240,8 +316,9 @@ def test_reset_joints_by_offset(self, warp_env, art_data, all_joints_cfg): # -- reset_joints_by_scale -------------------------------------------------- - def test_reset_joints_by_scale(self, warp_env, art_data, all_joints_cfg): - """With scale=1.0, result == defaults. Mutate defaults -> result tracks.""" + def test_reset_joints_by_scale(self, replay_warp_env, art_data, all_joints_cfg): + """Debug-validated replay inside capture should read mutated defaults.""" + warp_env = replay_warp_env cfg = all_joints_cfg mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) @@ -268,6 +345,8 @@ def test_reset_joints_by_scale(self, warp_env, art_data, all_joints_cfg): def test_push_by_setting_velocity(self, warp_env, art_data, all_joints_cfg): """With zero-width velocity range, scratch == root_vel_w. Mutate root_vel_w -> scratch tracks.""" mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + captured = {} + warp_env.scene["robot"].write_root_velocity_to_sim_mask = lambda **kwargs: captured.update(kwargs) zero_range = { "x": (0.0, 0.0), "y": (0.0, 0.0), @@ -276,10 +355,16 @@ def test_push_by_setting_velocity(self, warp_env, art_data, all_joints_cfg): "pitch": (0.0, 0.0), "yaw": (0.0, 0.0), } + term = _make_event_term( + warp_evt.push_by_setting_velocity, + warp_env, + mode="interval", + velocity_range=zero_range, + ) - warp_evt.push_by_setting_velocity(warp_env, mask, velocity_range=zero_range) + term(warp_env, mask, **term.cfg.params) with wp.ScopedCapture() as cap: - warp_evt.push_by_setting_velocity(warp_env, mask, velocity_range=zero_range) + term(warp_env, mask, **term.cfg.params) # Mutate root_vel_w new_vel = np.tile([1.0, 2.0, 3.0, 0.1, 0.2, 0.3], (NUM_ENVS, 1)).astype(np.float32) @@ -288,30 +373,80 @@ def test_push_by_setting_velocity(self, warp_env, art_data, all_joints_cfg): wp.capture_launch(cap.graph) wp.synchronize() - scratch = wp.to_torch(warp_evt.push_by_setting_velocity._scratch_vel) + scratch = wp.to_torch(captured["root_velocity"]) expected = torch.tensor([1.0, 2.0, 3.0, 0.1, 0.2, 0.3], device=DEVICE).expand(NUM_ENVS, -1) assert_close(scratch, expected) # -- apply_external_force_torque -------------------------------------------- - def test_apply_external_force_torque(self, warp_env, art_data, all_joints_cfg): - """With zero-width ranges, forces/torques are zero. Non-zero ranges produce non-zero output.""" + def test_apply_external_force_torque_zero_ranges_are_no_op(self, warp_env, art_data, all_joints_cfg): + """Zero wrench ranges should preserve existing composer state by skipping the setter.""" mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + calls = [] + warp_env.scene["robot"].permanent_wrench_composer.set_forces_and_torques_mask = lambda **kwargs: calls.append( + kwargs + ) + zero_range = (0.0, 0.0) + term = _make_event_term( + warp_evt.apply_external_force_torque, + warp_env, + force_range=zero_range, + torque_range=zero_range, + ) # Zero-range: forces and torques should be zero - warp_evt.apply_external_force_torque(warp_env, mask, force_range=(0.0, 0.0), torque_range=(0.0, 0.0)) + term(warp_env, mask, **term.cfg.params) with wp.ScopedCapture() as cap: - warp_evt.apply_external_force_torque(warp_env, mask, force_range=(0.0, 0.0), torque_range=(0.0, 0.0)) + term(warp_env, mask, **term.cfg.params) wp.capture_launch(cap.graph) wp.synchronize() - forces = wp.to_torch(warp_evt.apply_external_force_torque._scratch_forces) - torques = wp.to_torch(warp_evt.apply_external_force_torque._scratch_torques) - assert_close(forces, torch.zeros_like(forces)) - assert_close(torques, torch.zeros_like(torques)) + assert calls == [] + assert warp_env._warp_launch.sites == [] # -- reset_root_state_uniform ----------------------------------------------- + def test_reset_root_state_uniform_composes_orientation_like_stable(self): + """Pin the quaternion path: ``final = default ∘ euler_xyz(roll, pitch, yaw)``. + + Zero-width ranges at non-zero angles make the uniform draw deterministic, + so the composition order, Euler convention, and xyzw storage layout are + all pinned against the stable math reference. + """ + import isaaclab.utils.math as math_utils + + env, data, asset = _make_event_env(1234) + # Non-identity default orientation (90 deg about x), xyzw layout. + default_pose = np.zeros((NUM_ENVS, 7), dtype=np.float32) + default_pose[:, 3] = np.sin(np.pi / 4.0) + default_pose[:, 6] = np.cos(np.pi / 4.0) + copy_np_to_wp(data.default_root_pose, default_pose) + copy_np_to_wp(data.default_root_vel, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured = {} + asset.write_root_pose_to_sim_mask = lambda **kwargs: captured.update(kwargs) + asset.write_root_velocity_to_sim_mask = lambda **kwargs: None + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + roll, pitch, yaw = 0.3, -0.4, 1.1 + term = _make_event_term( + warp_evt.reset_root_state_uniform, + env, + pose_range={"roll": (roll, roll), "pitch": (pitch, pitch), "yaw": (yaw, yaw)}, + velocity_range={}, + ) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + result_xyzw = wp.to_torch(captured["root_pose"])[:, 3:7] + angles = torch.tensor([[roll], [pitch], [yaw]], device=DEVICE) + # Core math utils use the (x, y, z, w) layout, matching the warp buffers. + delta_xyzw = math_utils.quat_from_euler_xyz(angles[0], angles[1], angles[2]) + default_xyzw = torch.tensor([[np.sin(np.pi / 4.0), 0.0, 0.0, np.cos(np.pi / 4.0)]], device=DEVICE) + expected_xyzw = math_utils.quat_mul(default_xyzw, delta_xyzw).expand(NUM_ENVS, 4) + # Quaternions double-cover rotations: align signs before comparing. + sign = torch.sign((result_xyzw * expected_xyzw).sum(dim=1, keepdim=True)) + assert_close(result_xyzw * sign, expected_xyzw) + # -- env_mask selectivity --------------------------------------------------- def test_reset_joints_mask_selectivity(self, warp_env, art_data, all_joints_cfg): @@ -338,3 +473,376 @@ def test_reset_joints_mask_selectivity(self, warp_env, art_data, all_joints_cfg) assert_close(result[: NUM_ENVS // 2], torch.zeros(NUM_ENVS // 2, NUM_JOINTS, device=DEVICE)) # Unmasked envs: still 999.0 assert_close(result[NUM_ENVS // 2 :], torch.full((NUM_ENVS // 2, NUM_JOINTS), 999.0, device=DEVICE)) + + +class TestEventClassTermOwnership: + """Verify class event terms allocate persistent state only during initialization.""" + + def test_push_term_reuses_velocity_buffer(self, monkeypatch): + env, data, asset = _make_event_env(81) + copy_np_to_wp(data.root_vel_w, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured = [] + asset.write_root_velocity_to_sim_mask = lambda **kwargs: captured.append(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.push_by_setting_velocity, + env, + velocity_range={"x": (1.0, 1.0)}, + ) + monkeypatch.setattr(wp, "zeros", lambda *args, **kwargs: pytest.fail("allocated during term call")) + + term(env, env_mask, **term.cfg.params) + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + assert captured[0]["root_velocity"].ptr == captured[1]["root_velocity"].ptr + assert env._warp_launch.sites == [(term, env_mask), (term, env_mask)] + + def test_external_wrench_term_reuses_buffers(self, monkeypatch): + env, _, asset = _make_event_env(82, num_bodies=2) + captured = [] + asset.permanent_wrench_composer.set_forces_and_torques_mask = lambda **kwargs: captured.append(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.apply_external_force_torque, + env, + force_range=(1.0, 1.0), + torque_range=(2.0, 2.0), + ) + monkeypatch.setattr(wp, "zeros", lambda *args, **kwargs: pytest.fail("allocated during term call")) + + term(env, env_mask, **term.cfg.params) + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + assert captured[0]["forces"].ptr == captured[1]["forces"].ptr + assert captured[0]["torques"].ptr == captured[1]["torques"].ptr + assert env._warp_launch.sites == [(term, env_mask), (term, env_mask)] + + def test_root_reset_term_reuses_buffers(self, monkeypatch): + env, data, asset = _make_event_env(83) + default_pose = np.zeros((NUM_ENVS, 7), dtype=np.float32) + default_pose[:, 6] = 1.0 + copy_np_to_wp(data.default_root_pose, default_pose) + copy_np_to_wp(data.default_root_vel, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + poses = [] + velocities = [] + asset.write_root_pose_to_sim_mask = lambda **kwargs: poses.append(kwargs) + asset.write_root_velocity_to_sim_mask = lambda **kwargs: velocities.append(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.reset_root_state_uniform, + env, + pose_range={}, + velocity_range={}, + ) + monkeypatch.setattr(wp, "zeros", lambda *args, **kwargs: pytest.fail("allocated during term call")) + + term(env, env_mask, **term.cfg.params) + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + assert poses[0]["root_pose"].ptr == poses[1]["root_pose"].ptr + assert velocities[0]["root_velocity"].ptr == velocities[1]["root_velocity"].ptr + assert env._warp_launch.sites == [(term, env_mask), (term, env_mask)] + + def test_com_term_captures_first_call_baseline_without_accumulation(self, monkeypatch): + env, data, asset = _make_event_env(84, num_bodies=2) + baseline = np.zeros((NUM_ENVS, 2, 3), dtype=np.float32) + baseline[..., 0] = 0.25 + copy_np_to_wp(data.body_com_pos_b, baseline) + asset.set_coms_mask = lambda **kwargs: None + asset_cfg = SceneEntityCfg("robot", body_ids=[0, 1]) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.randomize_rigid_body_com, + env, + asset_cfg=asset_cfg, + com_range={"x": (1.0, 1.0)}, + ) + + # Startup terms may update COM after term construction but before its first call. + baseline[..., 0] = 0.75 + copy_np_to_wp(data.body_com_pos_b, baseline) + + term(env, env_mask, **term.cfg.params) + monkeypatch.setattr(wp, "clone", lambda *args, **kwargs: pytest.fail("baseline cloned more than once")) + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + assert_close(data.body_com_pos_b.torch[..., 0], torch.full((NUM_ENVS, 2), 1.75, device=DEVICE)) + assert env._warp_launch.sites == [(term, env_mask), (term, env_mask)] + + +class TestLegacyEventFunctionOwnership: + """Verify legacy event functions retain isolated state and eager-call compatibility.""" + + def test_com_term_is_marked_non_capturable(self): + assert warp_evt.randomize_rigid_body_com._warp_capturable is False + + def test_push_state_is_not_shared_between_environments(self): + env_a, data_a, asset_a = _make_event_env(101) + env_b, data_b, asset_b = _make_event_env(202) + copy_np_to_wp(data_a.root_vel_w, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + copy_np_to_wp(data_b.root_vel_w, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured_a = {} + captured_b = {} + asset_a.write_root_velocity_to_sim_mask = lambda **kwargs: captured_a.update(kwargs) + asset_b.write_root_velocity_to_sim_mask = lambda **kwargs: captured_b.update(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + range_a = {"x": (1.0, 1.0)} + range_b = {"x": (2.0, 2.0)} + term_a = _make_event_term(warp_evt.push_by_setting_velocity, env_a, mode="interval", velocity_range=range_a) + term_b = _make_event_term(warp_evt.push_by_setting_velocity, env_b, mode="interval", velocity_range=range_b) + + term_a(env_a, env_mask, **term_a.cfg.params) + term_b(env_b, env_mask, **term_b.cfg.params) + wp.synchronize() + + velocity_a = captured_a["root_velocity"] + velocity_b = captured_b["root_velocity"] + assert velocity_a.ptr != velocity_b.ptr + assert_close(wp.to_torch(velocity_a)[:, 0], torch.ones(NUM_ENVS, device=DEVICE)) + assert_close(wp.to_torch(velocity_b)[:, 0], torch.full((NUM_ENVS,), 2.0, device=DEVICE)) + + def test_push_state_is_not_shared_between_configurations(self): + env, data, asset = _make_event_env(252) + copy_np_to_wp(data.root_vel_w, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured = [] + asset.write_root_velocity_to_sim_mask = lambda **kwargs: captured.append(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + range_a = {"x": (1.0, 1.0)} + range_b = {"x": (2.0, 2.0)} + term_a = _make_event_term(warp_evt.push_by_setting_velocity, env, mode="interval", velocity_range=range_a) + term_b = _make_event_term(warp_evt.push_by_setting_velocity, env, mode="interval", velocity_range=range_b) + + term_a(env, env_mask, **term_a.cfg.params) + term_b(env, env_mask, **term_b.cfg.params) + wp.synchronize() + + velocity_a = captured[0]["root_velocity"] + velocity_b = captured[1]["root_velocity"] + assert velocity_a.ptr != velocity_b.ptr + assert_close(wp.to_torch(velocity_a)[:, 0], torch.ones(NUM_ENVS, device=DEVICE)) + assert_close(wp.to_torch(velocity_b)[:, 0], torch.full((NUM_ENVS,), 2.0, device=DEVICE)) + + def test_push_term_snapshots_range_during_initialization(self): + env, data, asset = _make_event_env(277) + copy_np_to_wp(data.root_vel_w, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured = {} + asset.write_root_velocity_to_sim_mask = lambda **kwargs: captured.update(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + velocity_range = {"x": (1.0, 1.0)} + term = _make_event_term( + warp_evt.push_by_setting_velocity, + env, + mode="interval", + velocity_range=velocity_range, + ) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + assert_close(wp.to_torch(captured["root_velocity"])[:, 0], torch.ones(NUM_ENVS, device=DEVICE)) + + velocity_range["x"] = (4.0, 4.0) + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + assert_close(wp.to_torch(captured["root_velocity"])[:, 0], torch.ones(NUM_ENVS, device=DEVICE)) + + def test_push_state_respects_sparse_mask(self): + env, data, asset = _make_event_env(303) + copy_np_to_wp(data.root_vel_w, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured = {} + asset.write_root_velocity_to_sim_mask = lambda **kwargs: captured.update(kwargs) + mask_np = np.arange(NUM_ENVS) % 3 == 0 + env_mask = wp.array(mask_np, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.push_by_setting_velocity, + env, + mode="interval", + velocity_range={"x": (3.0, 3.0)}, + ) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + velocity = wp.to_torch(captured["root_velocity"]) + mask = torch.from_numpy(mask_np).to(device=DEVICE) + assert_close(velocity[mask, 0], torch.full((int(mask_np.sum()),), 3.0, device=DEVICE)) + assert_close(velocity[~mask], torch.zeros((int((~mask_np).sum()), 6), device=DEVICE)) + + def test_external_wrench_state_is_not_shared_between_environments(self): + env_a, _, asset_a = _make_event_env(404, num_bodies=2) + env_b, _, asset_b = _make_event_env(505, num_bodies=2) + captured_a = {} + captured_b = {} + asset_a.permanent_wrench_composer.set_forces_and_torques_mask = lambda **kwargs: captured_a.update(kwargs) + asset_b.permanent_wrench_composer.set_forces_and_torques_mask = lambda **kwargs: captured_b.update(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + force_range_a = (1.0, 1.0) + force_range_b = (2.0, 2.0) + torque_range = (0.0, 0.0) + term_a = _make_event_term( + warp_evt.apply_external_force_torque, + env_a, + force_range=force_range_a, + torque_range=torque_range, + ) + term_b = _make_event_term( + warp_evt.apply_external_force_torque, + env_b, + force_range=force_range_b, + torque_range=torque_range, + ) + + term_a(env_a, env_mask, **term_a.cfg.params) + term_b(env_b, env_mask, **term_b.cfg.params) + wp.synchronize() + + forces_a = captured_a["forces"] + forces_b = captured_b["forces"] + assert forces_a.ptr != forces_b.ptr + assert_close(wp.to_torch(forces_a), torch.ones((NUM_ENVS, 2, 3), device=DEVICE)) + assert_close(wp.to_torch(forces_b), torch.full((NUM_ENVS, 2, 3), 2.0, device=DEVICE)) + + def test_external_wrench_respects_body_selection(self): + env, _, asset = _make_event_env(550, num_bodies=3) + captured = {} + asset.permanent_wrench_composer.set_forces_and_torques_mask = lambda **kwargs: captured.update(kwargs) + asset_cfg = SceneEntityCfg("robot", body_ids=[1]) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.apply_external_force_torque, + env, + force_range=(2.0, 2.0), + torque_range=(0.0, 0.0), + asset_cfg=asset_cfg, + ) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + expected_body_mask = torch.tensor([False, True, False], dtype=torch.bool, device=DEVICE) + assert torch.equal(wp.to_torch(captured["body_mask"]), expected_body_mask) + forces = wp.to_torch(captured["forces"]) + assert_close(forces[:, 0], torch.zeros((NUM_ENVS, 3), device=DEVICE)) + assert_close(forces[:, 1], torch.full((NUM_ENVS, 3), 2.0, device=DEVICE)) + assert_close(forces[:, 2], torch.zeros((NUM_ENVS, 3), device=DEVICE)) + + def test_root_reset_state_is_not_shared_between_environments(self): + env_a, data_a, asset_a = _make_event_env(606) + env_b, data_b, asset_b = _make_event_env(707) + default_pose = np.zeros((NUM_ENVS, 7), dtype=np.float32) + default_pose[:, 6] = 1.0 + copy_np_to_wp(data_a.default_root_pose, default_pose) + copy_np_to_wp(data_b.default_root_pose, default_pose) + copy_np_to_wp(data_a.default_root_vel, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + copy_np_to_wp(data_b.default_root_vel, np.zeros((NUM_ENVS, 6), dtype=np.float32)) + captured_a = {} + captured_b = {} + asset_a.write_root_pose_to_sim_mask = lambda **kwargs: captured_a.update(kwargs) + asset_b.write_root_pose_to_sim_mask = lambda **kwargs: captured_b.update(kwargs) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + pose_range_a = {"x": (1.0, 1.0)} + pose_range_b = {"x": (2.0, 2.0)} + velocity_range = {} + term_a = _make_event_term( + warp_evt.reset_root_state_uniform, + env_a, + pose_range=pose_range_a, + velocity_range=velocity_range, + ) + term_b = _make_event_term( + warp_evt.reset_root_state_uniform, + env_b, + pose_range=pose_range_b, + velocity_range=velocity_range, + ) + + term_a(env_a, env_mask, **term_a.cfg.params) + term_b(env_b, env_mask, **term_b.cfg.params) + wp.synchronize() + + pose_a = captured_a["root_pose"] + pose_b = captured_b["root_pose"] + assert pose_a.ptr != pose_b.ptr + assert_close(wp.to_torch(pose_a)[:, 0], torch.ones(NUM_ENVS, device=DEVICE)) + assert_close(wp.to_torch(pose_b)[:, 0], torch.full((NUM_ENVS,), 2.0, device=DEVICE)) + + def test_com_ranges_are_not_shared_between_environments(self): + env_a, data_a, asset_a = _make_event_env(808, num_bodies=2) + env_b, data_b, asset_b = _make_event_env(909, num_bodies=2) + copy_np_to_wp(data_a.body_com_pos_b, np.zeros((NUM_ENVS, 2, 3), dtype=np.float32)) + copy_np_to_wp(data_b.body_com_pos_b, np.zeros((NUM_ENVS, 2, 3), dtype=np.float32)) + asset_a.set_coms_mask = lambda **kwargs: None + asset_b.set_coms_mask = lambda **kwargs: None + asset_cfg_a = SceneEntityCfg("robot") + asset_cfg_b = SceneEntityCfg("robot") + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + com_range_a = {"x": (1.0, 1.0)} + com_range_b = {"x": (2.0, 2.0)} + term_a = _make_event_term( + warp_evt.randomize_rigid_body_com, + env_a, + com_range=com_range_a, + asset_cfg=asset_cfg_a, + ) + term_b = _make_event_term( + warp_evt.randomize_rigid_body_com, + env_b, + com_range=com_range_b, + asset_cfg=asset_cfg_b, + ) + + term_a(env_a, env_mask, **term_a.cfg.params) + term_b(env_b, env_mask, **term_b.cfg.params) + wp.synchronize() + + assert term_a._default_com.ptr != term_b._default_com.ptr + assert_close(data_a.body_com_pos_b.torch[..., 0], torch.ones((NUM_ENVS, 2), device=DEVICE)) + assert_close(data_b.body_com_pos_b.torch[..., 0], torch.full((NUM_ENVS, 2), 2.0, device=DEVICE)) + + def test_com_randomization_uses_persistent_baseline(self): + env, data, asset = _make_event_env(1001, num_bodies=2) + baseline = np.zeros((NUM_ENVS, 2, 3), dtype=np.float32) + baseline[..., 0] = 0.25 + copy_np_to_wp(data.body_com_pos_b, baseline) + asset.set_coms_mask = lambda **kwargs: None + asset_cfg = SceneEntityCfg("robot", body_ids=[0, 1]) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + com_range = {"x": (1.0, 1.0)} + term = _make_event_term( + warp_evt.randomize_rigid_body_com, + env, + com_range=com_range, + asset_cfg=asset_cfg, + ) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + assert_close(data.body_com_pos_b.torch[..., 0], torch.full((NUM_ENVS, 2), 1.25, device=DEVICE)) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + assert_close(data.body_com_pos_b.torch[..., 0], torch.full((NUM_ENVS, 2), 1.25, device=DEVICE)) + + def test_com_randomization_broadcasts_one_offset_across_selected_bodies(self): + env, data, asset = _make_event_env(1111, num_bodies=3) + baseline = np.zeros((NUM_ENVS, 3, 3), dtype=np.float32) + copy_np_to_wp(data.body_com_pos_b, baseline) + asset.set_coms_mask = lambda **kwargs: None + asset_cfg = SceneEntityCfg("robot", body_ids=[0, 2]) + env_mask = wp.array([True] * NUM_ENVS, dtype=wp.bool, device=DEVICE) + term = _make_event_term( + warp_evt.randomize_rigid_body_com, + env, + com_range={"x": (-1.0, 1.0), "y": (-2.0, 2.0), "z": (-3.0, 3.0)}, + asset_cfg=asset_cfg, + ) + + term(env, env_mask, **term.cfg.params) + wp.synchronize() + + assert_close(data.body_com_pos_b.torch[:, 0], data.body_com_pos_b.torch[:, 2]) + assert_close(data.body_com_pos_b.torch[:, 1], torch.zeros((NUM_ENVS, 3), device=DEVICE)) diff --git a/source/isaaclab_experimental/test/envs/mdp/test_observations_warp_parity.py b/source/isaaclab_experimental/test/envs/mdp/test_observations_warp_parity.py index 9f1eea23bf8d..ed0473f0793c 100644 --- a/source/isaaclab_experimental/test/envs/mdp/test_observations_warp_parity.py +++ b/source/isaaclab_experimental/test/envs/mdp/test_observations_warp_parity.py @@ -12,6 +12,8 @@ import torch import warp as wp +from isaaclab.utils.warp import WarpLaunchCache + # Skip entire module if no CUDA device available wp.init() pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") @@ -47,15 +49,6 @@ # ============================================================================ -@pytest.fixture(autouse=True) -def _clear_caches(): - yield - for fn in [warp_obs.generated_commands]: - for attr in list(vars(fn)): - if attr.startswith("_"): - delattr(fn, attr) - - @pytest.fixture() def art_data(): return MockArticulationData(NUM_ENVS, NUM_JOINTS, DEVICE) @@ -127,6 +120,7 @@ class _Env: env.action_manager = MockActionManagerWarp(action_wp[0], action_wp[1]) env.num_envs = NUM_ENVS env.device = DEVICE + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=DEVICE) env.episode_length_buf = episode_length_buf env.step_dt = 0.02 env.max_episode_length_s = 10.0 @@ -165,6 +159,7 @@ class _Env: env.command_manager = MockCommandManager(cmd_tensor, cmd_term) env.num_envs = NUM_ENVS env.device = DEVICE + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=DEVICE) env.episode_length_buf = episode_length_buf env.step_dt = 0.02 env.max_episode_length = 500 @@ -283,6 +278,24 @@ def test_joint_vel_subset(self, warp_env, stable_env, subset_cfg): assert_close(actual, expected) assert_close(actual_cap, expected) + @pytest.mark.parametrize( + ("first_fn", "second_fn", "stable_second_fn"), + [ + (warp_obs.joint_pos, warp_obs.joint_vel, stable_obs.joint_vel), + (warp_obs.joint_pos_rel, warp_obs.joint_vel_rel, stable_obs.joint_vel_rel), + ], + ) + def test_joint_helpers_reuse_output( + self, warp_env, stable_env, all_joints_cfg, first_fn, second_fn, stable_second_fn + ): + """Distinct public helpers sharing a kernel may safely reuse one output buffer.""" + out = wp.empty((NUM_ENVS, NUM_JOINTS), dtype=wp.float32, device=DEVICE) + + first_fn(warp_env, out, asset_cfg=all_joints_cfg) + second_fn(warp_env, out, asset_cfg=all_joints_cfg) + + assert_close(wp.to_torch(out).clone(), stable_second_fn(stable_env, asset_cfg=all_joints_cfg)) + # -- Normalized joint position ---------------------------------------------- def test_joint_pos_limit_normalized(self, warp_env, stable_env, all_joints_cfg): diff --git a/source/isaaclab_experimental/test/envs/mdp/test_rewards_warp_parity.py b/source/isaaclab_experimental/test/envs/mdp/test_rewards_warp_parity.py index 69cabcd14a36..497c96e92a96 100644 --- a/source/isaaclab_experimental/test/envs/mdp/test_rewards_warp_parity.py +++ b/source/isaaclab_experimental/test/envs/mdp/test_rewards_warp_parity.py @@ -12,6 +12,8 @@ import torch import warp as wp +from isaaclab.utils.warp import WarpLaunchCache + # Skip entire module if no CUDA device available wp.init() pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") @@ -55,7 +57,7 @@ @pytest.fixture(autouse=True) def _clear_caches(): yield - for fn in [warp_rew.track_lin_vel_xy_exp, warp_rew.track_ang_vel_z_exp, warp_rew.undesired_contacts]: + for fn in [warp_rew.undesired_contacts]: for attr in list(vars(fn)): if attr.startswith("_"): delattr(fn, attr) @@ -138,6 +140,7 @@ class _Env: env.termination_manager = term_mgr env.num_envs = NUM_ENVS env.device = DEVICE + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=DEVICE) env.episode_length_buf = episode_length_buf env.step_dt = 0.02 env.max_episode_length_s = 10.0 @@ -177,6 +180,7 @@ class _Env: env.command_manager = MockCommandManager(cmd_tensor, cmd_term) env.num_envs = NUM_ENVS env.device = DEVICE + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=DEVICE) env.episode_length_buf = episode_length_buf env.step_dt = 0.02 env.max_episode_length = 500 @@ -299,6 +303,16 @@ def test_joint_torques_l2(self, warp_env, stable_env, all_joints_cfg): assert_close(actual, expected) assert_close(actual_cap, expected) + def test_sum_squared_helpers_reuse_output(self, warp_env, stable_env, all_joints_cfg): + """Distinct public reductions sharing a kernel may safely reuse one output buffer.""" + out = wp.empty((NUM_ENVS,), dtype=wp.float32, device=DEVICE) + + warp_rew.joint_torques_l2(warp_env, out, asset_cfg=all_joints_cfg) + warp_rew.joint_vel_l2(warp_env, out, asset_cfg=all_joints_cfg) + warp_rew.joint_acc_l2(warp_env, out, asset_cfg=all_joints_cfg) + + assert_close(wp.to_torch(out).clone(), stable_rew.joint_acc_l2(stable_env, asset_cfg=all_joints_cfg)) + # -- Joint L1 penalties (masked) -------------------------------------------- def test_joint_vel_l1(self, warp_env, stable_env, all_joints_cfg): @@ -368,6 +382,18 @@ def test_track_lin_vel_xy_exp(self, warp_env_bodies, stable_env_bodies, body_cfg assert_close(actual, expected) assert_close(actual_cap, expected) + def test_track_lin_vel_xy_exp_rerecords_changed_std(self, warp_env_bodies, stable_env_bodies): + """A changed scalar term parameter records a command with the new value.""" + cfg = MockBodyCfg("robot") + cfg.joint_ids = list(range(NUM_JOINTS)) + out = wp.empty((NUM_ENVS,), dtype=wp.float32, device=DEVICE) + + warp_rew.track_lin_vel_xy_exp(warp_env_bodies, out, std=0.25, command_name="vel", asset_cfg=cfg) + warp_rew.track_lin_vel_xy_exp(warp_env_bodies, out, std=0.75, command_name="vel", asset_cfg=cfg) + + expected = stable_rew.track_lin_vel_xy_exp(stable_env_bodies, std=0.75, command_name="vel", asset_cfg=cfg) + assert_close(wp.to_torch(out).clone(), expected) + def test_track_ang_vel_z_exp(self, warp_env_bodies, stable_env_bodies, body_cfg): cfg = MockBodyCfg("robot") cfg.joint_ids = list(range(NUM_JOINTS)) diff --git a/source/isaaclab_experimental/test/envs/mdp/test_terminations_warp_parity.py b/source/isaaclab_experimental/test/envs/mdp/test_terminations_warp_parity.py index d82339e01228..75744ce13903 100644 --- a/source/isaaclab_experimental/test/envs/mdp/test_terminations_warp_parity.py +++ b/source/isaaclab_experimental/test/envs/mdp/test_terminations_warp_parity.py @@ -12,6 +12,8 @@ import torch import warp as wp +from isaaclab.utils.warp import WarpLaunchCache + # Skip entire module if no CUDA device available wp.init() pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") @@ -131,6 +133,7 @@ class _Env: env.action_manager = MockActionManagerWarp(action_wp[0], action_wp[1]) env.num_envs = NUM_ENVS env.device = DEVICE + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=DEVICE) env.episode_length_buf = episode_length_buf env.step_dt = 0.02 env.max_episode_length_s = 10.0 @@ -169,6 +172,7 @@ class _Env: env.command_manager = MockCommandManager(cmd_tensor, cmd_term) env.num_envs = NUM_ENVS env.device = DEVICE + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=DEVICE) env.episode_length_buf = episode_length_buf env._episode_length_buf_wp = wp.from_torch(episode_length_buf) env.step_dt = 0.02 @@ -230,6 +234,16 @@ def test_root_height_below_minimum(self, warp_env, stable_env, all_joints_cfg): assert_equal(actual, expected) assert_equal(actual_cap, expected) + def test_root_height_rerecords_changed_minimum(self, warp_env, stable_env, all_joints_cfg): + """A changed scalar term parameter records a command with the new value.""" + out = wp.empty((NUM_ENVS,), dtype=wp.bool, device=DEVICE) + + warp_term.root_height_below_minimum(warp_env, out, minimum_height=-100.0, asset_cfg=all_joints_cfg) + warp_term.root_height_below_minimum(warp_env, out, minimum_height=100.0, asset_cfg=all_joints_cfg) + + expected = stable_term.root_height_below_minimum(stable_env, minimum_height=100.0, asset_cfg=all_joints_cfg) + assert_equal(wp.to_torch(out).clone(), expected) + def test_joint_pos_out_of_manual_limit(self, warp_env, stable_env, all_joints_cfg): cfg = all_joints_cfg bounds = (-1.0, 1.0) diff --git a/source/isaaclab_experimental/test/envs/test_frontend.py b/source/isaaclab_experimental/test/envs/test_frontend.py new file mode 100644 index 000000000000..20c043180434 --- /dev/null +++ b/source/isaaclab_experimental/test/envs/test_frontend.py @@ -0,0 +1,579 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for :mod:`isaaclab_experimental.envs.frontend`. + +Pure-Python unit tests; no app launch. Covers: + +* :class:`Workflow` enum surface. +* :func:`SceneEntityCfg.from_stable` field copy. +* :func:`_require_newton_physics` hard-check. +* :func:`_walk_terms` recursive ManagerTermBaseCfg discovery over instance + attributes (nested class objects are not descended into). +* :meth:`_mirror_module` / :meth:`_nearest_mdp_module` deterministic mirror + resolution and :meth:`_cfg_route_modules` cfg-hierarchy resolution. +* :func:`_promote_scene_entity_cfgs` walks ``term.params`` dicts. +* :func:`_swap_mdp` swaps ``func`` *and* ``class_type``; raises with a path + list when twins are missing. +* :func:`_resolve_warp_twin` rejects stable-origin re-exports. +* :func:`_assert_direct_warp_registration` accepts warp-rooted entry + points and rejects stable ones. +""" + +from __future__ import annotations + +import contextlib +import types +from typing import Any +from unittest.mock import patch + +import gymnasium as gym +import isaaclab_experimental.envs.frontend as fe +import pytest +from isaaclab_experimental.envs.frontend import ( + FrontendIncompatibleError, + WarpFrontend, + Workflow, +) +from isaaclab_experimental.managers.scene_entity_cfg import SceneEntityCfg as WarpSceneEntityCfg +from isaaclab_newton.physics import NewtonCfg +from isaaclab_physx.physics import PhysxCfg + +from isaaclab.envs import ManagerBasedRLEnvCfg +from isaaclab.managers.manager_term_cfg import EventTermCfg, ObservationTermCfg, RewardTermCfg +from isaaclab.managers.scene_entity_cfg import SceneEntityCfg as StableSceneEntityCfg +from isaaclab.utils.configclass import configclass + +# ====================================================================== +# Fixtures: fake stable/warp symbols and configclass trees. +# +# These mirror the real cfg shape so :func:`_walk_terms` descends into them +# (it only descends into objects with ``__dataclass_fields__``). Term cfgs +# use the real :class:`EventTermCfg`/:class:`RewardTermCfg`/:class:`ObservationTermCfg` +# so the walker's ``isinstance(ManagerTermBaseCfg)`` discriminator yields them. +# ====================================================================== + + +def _stable_func(env, **params): + return None + + +class _StableActionCls: + pass + + +_stable_func.__module__ = "isaaclab_tasks.fake_task.mdp" +_StableActionCls.__module__ = "isaaclab_tasks.fake_task.mdp" + + +def _warp_twin_func(env, out, **params): + return None + + +class _WarpActionCls: + pass + + +_warp_twin_func.__module__ = "isaaclab_experimental.envs.mdp" +_WarpActionCls.__module__ = "isaaclab_experimental.envs.mdp" + + +@configclass +class _PolicyObsGroup: + """Stand-in for a per-task ObservationsCfg sub-group (e.g. PolicyCfg).""" + + o1: ObservationTermCfg | None = None + o2: ObservationTermCfg | None = None + + +@configclass +class _ExtraObsGroup: + """A second obs group (named arbitrarily) to exercise multi-group walks.""" + + o3: ObservationTermCfg | None = None + + +@configclass +class _ObservationsCfg: + policy: _PolicyObsGroup | None = None + perception: _ExtraObsGroup | None = None + + +@configclass +class _RewardsCfg: + r1: RewardTermCfg | None = None + r2: RewardTermCfg | None = None + + +@configclass +class _EventsCfg: + e1: EventTermCfg | None = None + + +@configclass +class _CurriculumCfg: + c1: EventTermCfg | None = None + + +@configclass +class _CfgFixture: + observations: _ObservationsCfg | None = None + rewards: _RewardsCfg | None = None + events: _EventsCfg | None = None + curriculum: _CurriculumCfg | None = None + + +def _term(func=None, params: dict | None = None) -> RewardTermCfg: + """Cheap RewardTermCfg builder for tests; the cfg class is irrelevant for swap/walk logic.""" + return RewardTermCfg(func=func or _stable_func, weight=1.0, params=params or {}) + + +# ====================================================================== +# Enums +# ====================================================================== + + +def test_workflow_values(): + assert Workflow.MANAGER_BASED == "manager_based" + assert Workflow.DIRECT == "direct" + + +# ====================================================================== +# build() +# ====================================================================== + + +def test_warp_manager_build_constructs_warp_env_with_cfg(): + # Cfg adaptation happens inside ManagerBasedRLEnvWarp.__init__, so build() + # only selects the env class and forwards the cfg and construct kwargs. + import isaaclab_experimental.envs as envs + + cfg = object.__new__(ManagerBasedRLEnvCfg) + expected_env = object() + calls: list[tuple[Any, Any]] = [] + + def fake_env(*, cfg: Any, **kwargs: Any) -> Any: + calls.append((cfg, kwargs)) + return expected_env + + with patch.object(envs, "ManagerBasedRLEnvWarp", fake_env): + env = WarpFrontend.build_env(cfg, "Isaac-Test", render_mode="rgb_array") + + assert env is expected_env + assert calls == [(cfg, {"render_mode": "rgb_array"})] + + +# ====================================================================== +# SceneEntityCfg.from_stable +# ====================================================================== + + +def test_from_stable_copies_minimum_fields(): + stable = StableSceneEntityCfg(name="robot") + warp = WarpSceneEntityCfg.from_stable(stable) + assert isinstance(warp, WarpSceneEntityCfg) + assert warp.name == "robot" + # Warp-only fields stay None until :meth:`resolve` runs. + assert warp.joint_mask is None + assert warp.joint_ids_wp is None + assert warp.body_ids_wp is None + + +def test_from_stable_copies_all_selection_fields(): + stable = StableSceneEntityCfg( + name="robot", + joint_names=["lf_hip"], + joint_ids=[0, 1, 2], + fixed_tendon_names=["tendon_a"], + fixed_tendon_ids=[5], + body_names=["base", "lf_foot"], + body_ids=[0, 4], + object_collection_names=["objs"], + object_collection_ids=[7], + preserve_order=True, + ) + warp = WarpSceneEntityCfg.from_stable(stable) + for field in ( + "name", + "joint_names", + "joint_ids", + "fixed_tendon_names", + "fixed_tendon_ids", + "body_names", + "body_ids", + "object_collection_names", + "object_collection_ids", + "preserve_order", + ): + assert getattr(warp, field) == getattr(stable, field), f"field {field!r} mismatch" + + +# ====================================================================== +# _require_newton_physics +# ====================================================================== + + +def _cfg_with_physics(physics: Any) -> Any: + cfg = types.SimpleNamespace() + cfg.sim = types.SimpleNamespace(physics=physics) + return cfg + + +def test_require_newton_passes_for_newton(): + fe.WarpFrontend._require_newton_physics(_cfg_with_physics(NewtonCfg()), "Isaac-Test-v0") # no raise + + +def test_require_newton_rejects_physx(): + with pytest.raises(FrontendIncompatibleError) as exc: + fe.WarpFrontend._require_newton_physics(_cfg_with_physics(PhysxCfg()), "Isaac-Test-v0") + assert "presets=newton_mjwarp" in str(exc.value) + assert "PhysxCfg" in str(exc.value) + + +def test_require_newton_rejects_none(): + with pytest.raises(FrontendIncompatibleError): + fe.WarpFrontend._require_newton_physics(_cfg_with_physics(None), "Isaac-Test-v0") + + +# ====================================================================== +# _walk_terms +# ====================================================================== + + +def test_walk_terms_yields_each_term_with_its_path(): + cfg = _CfgFixture( + rewards=_RewardsCfg(r1=_term(), r2=_term()), + events=_EventsCfg(e1=EventTermCfg(func=_stable_func, mode="reset")), + ) + # Configclass instances aren't hashable, so collect paths only. + paths = {".".join(p) for p, _ in fe.WarpFrontend._walk_terms(cfg)} + assert paths == {"rewards.r1", "rewards.r2", "events.e1"} + + +def test_walk_terms_descends_into_obs_subgroups(): + cfg = _CfgFixture( + observations=_ObservationsCfg( + policy=_PolicyObsGroup(o1=ObservationTermCfg(func=_stable_func)), + perception=_ExtraObsGroup(o3=ObservationTermCfg(func=_stable_func)), + ), + ) + paths = {".".join(p) for p, _ in fe.WarpFrontend._walk_terms(cfg)} + # Discovery is purely type-driven; no obs group name is hardcoded. + assert paths == {"observations.policy.o1", "observations.perception.o3"} + + +def test_walk_terms_stops_at_terms(): + # The walker must not descend into term.params / term.func — yields the term itself. + nested_se_cfg = StableSceneEntityCfg(name="robot") + cfg = _CfgFixture(rewards=_RewardsCfg(r1=_term(params={"asset_cfg": nested_se_cfg}))) + terms = list(fe.WarpFrontend._walk_terms(cfg)) + assert len(terms) == 1 + _, term = terms[0] + assert isinstance(term, RewardTermCfg) + + +def test_walk_terms_skips_non_configclass_attrs(): + # A namespace without __dataclass_fields__ is not descended into. + cfg = types.SimpleNamespace(some_plain_attr="hello") + assert list(fe.WarpFrontend._walk_terms(cfg)) == [] + + +def test_walk_terms_skips_none_subtrees(): + cfg = _CfgFixture(rewards=None, events=None) + assert list(fe.WarpFrontend._walk_terms(cfg)) == [] + + +@configclass +class _GroupWithNestedClass: + """Mirrors the real ``ObservationsCfg.PolicyCfg`` nested-class pattern.""" + + @configclass + class TemplateCfg: + t1: ObservationTermCfg | None = None + + o1: ObservationTermCfg | None = None + + +def test_walk_terms_ignores_nested_class_objects(): + # The walker iterates instance attributes only (mirroring the warp managers' + # ``cfg.__dict__`` consumption), so a nested class object — reachable as an + # attribute on the instance — must never contribute paths. + cfg = _GroupWithNestedClass(o1=ObservationTermCfg(func=_stable_func)) + paths = {".".join(p) for p, _ in fe.WarpFrontend._walk_terms(cfg)} + assert paths == {"o1"} + + +# ====================================================================== +# Mirror resolution +# ====================================================================== + + +def test_mirror_module_maps_stable_roots(): + assert ( + fe.WarpFrontend._mirror_module("isaaclab_tasks.core.velocity.mdp.rewards") + == "isaaclab_tasks_experimental.core.velocity.mdp.rewards" + ) + assert fe.WarpFrontend._mirror_module("isaaclab.envs.mdp.events") == "isaaclab_experimental.envs.mdp.events" + assert fe.WarpFrontend._mirror_module("isaaclab_tasks") == "isaaclab_tasks_experimental" + # Unmirrored roots are not resolvable. + assert fe.WarpFrontend._mirror_module("torch.nn.functional") is None + assert fe.WarpFrontend._mirror_module("") is None + + +def test_nearest_mdp_module_truncates_at_mdp_boundary(): + # A symbol module resolves to its own mdp package on the mirrored path. + assert ( + fe.WarpFrontend._nearest_mdp_module("isaaclab_tasks_experimental.core.locomotion.mdp.rewards") + == "isaaclab_tasks_experimental.core.locomotion.mdp" + ) + assert fe.WarpFrontend._nearest_mdp_module("isaaclab_experimental.envs.mdp.events") == ( + "isaaclab_experimental.envs.mdp" + ) + + +def test_nearest_mdp_module_walks_up_cfg_paths(): + # A cfg module path walks up to the task family's mdp package. + assert ( + fe.WarpFrontend._nearest_mdp_module("isaaclab_tasks_experimental.core.velocity.config.anymal_d.flat_env_cfg") + == "isaaclab_tasks_experimental.core.velocity.mdp" + ) + # Nothing on the path provides an mdp package. + assert fe.WarpFrontend._nearest_mdp_module("isaaclab_tasks_experimental.nonexistent.task_pkg") is None + + +def test_cfg_route_modules_resolves_mirrored_hierarchy(): + # A class whose module mirrors into the experimental tree resolves to the + # task family's mdp package; unmirrored classes contribute nothing. + fixture_cls = type("FakeVelocityCfg", (), {}) + fixture_cls.__module__ = "isaaclab_tasks.core.velocity.config.anymal_d.flat_env_cfg" + modules = fe.WarpFrontend._cfg_route_modules(fixture_cls()) + assert [m.__name__ for m in modules] == ["isaaclab_tasks_experimental.core.velocity.mdp"] + + assert fe.WarpFrontend._cfg_route_modules(_CfgFixture()) == [] + + +# ====================================================================== +# _swap_mdp +# ====================================================================== + + +class _FakeMdpModule: + """Stand-in for a warp mdp module containing twins.""" + + __name__ = "test_fake_warp_mdp" + + +def _fake_mdp_module(name_to_symbol: dict[str, Any]) -> _FakeMdpModule: + module = _FakeMdpModule() + for name, symbol in name_to_symbol.items(): + setattr(module, name, symbol) + return module + + +def _patch_twin_modules(monkeypatch: pytest.MonkeyPatch, modules: list[Any]) -> None: + """Pin twin resolution to ``modules`` and keep unit tests hermetic.""" + monkeypatch.setattr(fe.WarpFrontend, "_cfg_route_modules", classmethod(lambda cls, cfg: [])) + monkeypatch.setattr( + fe.WarpFrontend, "_twin_modules", classmethod(lambda cls, symbol_module, cfg_route_modules: modules) + ) + + +def test_swap_mdp_swaps_func_and_class_type(monkeypatch: pytest.MonkeyPatch): + fake = _fake_mdp_module({"_stable_func": _warp_twin_func, "_StableActionCls": _WarpActionCls}) + _patch_twin_modules(monkeypatch, [fake]) + term_reward = _term(func=_stable_func) + term_action = _term() + term_action.class_type = _StableActionCls # set attr to exercise class_type swap + cfg = _CfgFixture(rewards=_RewardsCfg(r1=term_reward, r2=term_action)) + fe.WarpFrontend._swap_mdp(cfg, "Isaac-Test-v0") + assert cfg.rewards.r1.func is _warp_twin_func + assert cfg.rewards.r2.class_type is _WarpActionCls + + +def test_swap_mdp_missing_twin_raises_with_path_list(monkeypatch: pytest.MonkeyPatch): + _patch_twin_modules(monkeypatch, [_fake_mdp_module({})]) + cfg = _CfgFixture(rewards=_RewardsCfg(r1=_term(func=_stable_func))) + with pytest.raises(FrontendIncompatibleError) as exc: + fe.WarpFrontend._swap_mdp(cfg, "Isaac-Test-v0") + msg = str(exc.value) + assert "rewards.r1.func" in msg + assert "_stable_func" in msg + # The cfg term wasn't mutated for the missing twin. + assert cfg.rewards.r1.func is _stable_func + + +def test_swap_mdp_skips_already_warp(monkeypatch: pytest.MonkeyPatch): + _patch_twin_modules(monkeypatch, [_fake_mdp_module({})]) + cfg = _CfgFixture(rewards=_RewardsCfg(r1=_term(func=_warp_twin_func))) + fe.WarpFrontend._swap_mdp(cfg, "Isaac-Test-v0") # no raise + assert cfg.rewards.r1.func is _warp_twin_func + + +# ====================================================================== +# _promote_scene_entity_cfgs +# ====================================================================== + + +def test_promote_scene_entity_cfgs_promotes_in_params(): + cfg = _CfgFixture( + rewards=_RewardsCfg( + r1=_term(params={"asset_cfg": StableSceneEntityCfg(name="robot", joint_names=["lf_hip"]), "scale": 1.0}) + ) + ) + fe.WarpFrontend._promote_scene_entity_cfgs(cfg) + promoted = cfg.rewards.r1.params["asset_cfg"] + assert isinstance(promoted, WarpSceneEntityCfg) + assert promoted.name == "robot" + assert promoted.joint_names == ["lf_hip"] + # Non-SceneEntityCfg params are untouched. + assert cfg.rewards.r1.params["scale"] == 1.0 + + +def test_promote_scene_entity_cfgs_skips_already_warp(): + # configclass init deep-copies params, so identity won't hold across + # construction; what we actually want to assert is "no re-promotion": + # the asset_cfg remains a WarpSceneEntityCfg (i.e., wasn't passed + # back through `from_stable`). + warp = WarpSceneEntityCfg(name="robot", joint_names=["lf_hip"]) + cfg = _CfgFixture(rewards=_RewardsCfg(r1=_term(params={"asset_cfg": warp}))) + before = cfg.rewards.r1.params["asset_cfg"] + fe.WarpFrontend._promote_scene_entity_cfgs(cfg) + after = cfg.rewards.r1.params["asset_cfg"] + assert isinstance(after, WarpSceneEntityCfg) + # The asset_cfg object was not replaced by another from_stable call. + assert after is before + + +def test_promote_scene_entity_cfgs_walks_all_term_groups(): + cfg = _CfgFixture( + rewards=_RewardsCfg(r1=_term(params={"asset_cfg": StableSceneEntityCfg(name="r")})), + events=_EventsCfg( + e1=EventTermCfg(func=_stable_func, mode="reset", params={"asset_cfg": StableSceneEntityCfg(name="e")}) + ), + observations=_ObservationsCfg( + policy=_PolicyObsGroup( + o1=ObservationTermCfg(func=_stable_func, params={"asset_cfg": StableSceneEntityCfg(name="o-pol")}) + ), + perception=_ExtraObsGroup( + o3=ObservationTermCfg(func=_stable_func, params={"asset_cfg": StableSceneEntityCfg(name="o-per")}) + ), + ), + curriculum=_CurriculumCfg( + c1=EventTermCfg(func=_stable_func, mode="reset", params={"asset_cfg": StableSceneEntityCfg(name="c")}) + ), + ) + fe.WarpFrontend._promote_scene_entity_cfgs(cfg) + # Warp-managed groups (rewards, observations incl. sub-groups, events) are promoted. + assert isinstance(cfg.rewards.r1.params["asset_cfg"], WarpSceneEntityCfg) + assert isinstance(cfg.observations.policy.o1.params["asset_cfg"], WarpSceneEntityCfg) + # The perception sub-group is reached even though its attribute name is + # not hardcoded in the framework. + assert isinstance(cfg.observations.perception.o3.params["asset_cfg"], WarpSceneEntityCfg) + # The event manager is warp-first, so its terms are promoted too. + assert isinstance(cfg.events.e1.params["asset_cfg"], WarpSceneEntityCfg) + # Curriculum is adapted opportunistically (terms swap to warp twins when one + # exists), so its entities are promoted as well; the warp SceneEntityCfg + # subclasses the stable one, keeping legacy fallback terms valid. + assert isinstance(cfg.curriculum.c1.params["asset_cfg"], WarpSceneEntityCfg) + + +# ====================================================================== +# Twin resolution and swap-candidate heuristic +# ====================================================================== + + +def test_resolve_warp_twin_accepts_warp_origin(): + module = types.SimpleNamespace(foo=_warp_twin_func) + assert fe.WarpFrontend._resolve_warp_twin("foo", [module]) is _warp_twin_func + + +def test_resolve_warp_twin_rejects_stable_origin(): + module = types.SimpleNamespace(foo=_stable_func) # same name, stable origin + assert fe.WarpFrontend._resolve_warp_twin("foo", [module]) is None + + +def test_resolve_warp_twin_returns_none_when_absent(): + assert fe.WarpFrontend._resolve_warp_twin("missing", [types.SimpleNamespace()]) is None + + +def test_is_swap_candidate_stable_callable(): + assert fe.WarpFrontend._is_swap_candidate(_stable_func) + + +def test_is_swap_candidate_rejects_warp_callable(): + assert not fe.WarpFrontend._is_swap_candidate(_warp_twin_func) + + +def test_is_swap_candidate_rejects_non_callables(): + assert not fe.WarpFrontend._is_swap_candidate(42) + assert not fe.WarpFrontend._is_swap_candidate("string") + + +# ====================================================================== +# Direct workflow guard +# ====================================================================== + + +_DIRECT_TEST_TASKS = { + "_Frontend-Test-Warp-Direct-v0": ("isaaclab_tasks_experimental.fake:DirectEnv", None), + "_Frontend-Test-Stable-Direct-v0": ("isaaclab_tasks.fake:DirectEnv", None), + "_Frontend-Test-Declared-Direct-v0": ("isaaclab_tasks.fake:DirectEnv", "types:SimpleNamespace"), + "_Frontend-Test-Broken-Direct-v0": ("isaaclab_tasks.fake:DirectEnv", "definitely_not_a_module:Env"), + "_Frontend-Test-Missing-Class-Direct-v0": ("isaaclab_tasks.fake:DirectEnv", "types:NoSuchClass"), +} + + +@pytest.fixture(scope="module", autouse=True) +def _register_direct_stub_tasks(): + # Register stub tasks so ``gym.spec`` resolves them. Entry-point strings + # are never invoked (the guard only inspects them), so a fake import + # path is fine. ``warp_entry_point`` targets are imported, so the valid + # stub points at a real stdlib symbol. + registered = [] + for task_id, (entry_point, warp_entry_point) in _DIRECT_TEST_TASKS.items(): + kwargs = {"warp_entry_point": warp_entry_point} if warp_entry_point else {} + with contextlib.suppress(gym.error.Error): + gym.register(id=task_id, entry_point=entry_point, disable_env_checker=True, kwargs=kwargs) + registered.append(task_id) + yield + # Unregister so later test modules see the real, stub-free registry. + for task_id in registered: + gym.registry.pop(task_id, None) + + +def test_direct_guard_accepts_warp_rooted(): + fe.WarpFrontend._assert_direct_warp_registration("_Frontend-Test-Warp-Direct-v0") # no raise + + +def test_direct_guard_rejects_stable_rooted(): + with pytest.raises(FrontendIncompatibleError) as exc: + fe.WarpFrontend._assert_direct_warp_registration("_Frontend-Test-Stable-Direct-v0") + assert "isaaclab_experimental" in str(exc.value) + + +def test_resolve_direct_warp_class_returns_declared_class(): + import types as types_module + + env_class = fe.WarpFrontend._resolve_direct_warp_class("_Frontend-Test-Declared-Direct-v0") + assert env_class is types_module.SimpleNamespace + + +def test_resolve_direct_warp_class_returns_none_without_declaration(): + assert fe.WarpFrontend._resolve_direct_warp_class("_Frontend-Test-Stable-Direct-v0") is None + + +def test_resolve_direct_warp_class_rejects_broken_module(): + with pytest.raises(FrontendIncompatibleError): + fe.WarpFrontend._resolve_direct_warp_class("_Frontend-Test-Broken-Direct-v0") + + +def test_resolve_direct_warp_class_rejects_missing_class(): + with pytest.raises(FrontendIncompatibleError): + fe.WarpFrontend._resolve_direct_warp_class("_Frontend-Test-Missing-Class-Direct-v0") + + +def test_resolve_direct_warp_class_rejects_unknown_task(): + with pytest.raises(FrontendIncompatibleError): + fe.WarpFrontend._resolve_direct_warp_class("Frontend-Test-NotRegistered-v0") diff --git a/source/isaaclab_experimental/test/envs/test_frontend_cfg_conversion.py b/source/isaaclab_experimental/test/envs/test_frontend_cfg_conversion.py new file mode 100644 index 000000000000..82ec8489de9c --- /dev/null +++ b/source/isaaclab_experimental/test/envs/test_frontend_cfg_conversion.py @@ -0,0 +1,254 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Coverage tests for warp task configuration adaptation. + +Two sweeps, both running :meth:`WarpFrontend.adapt_cfg` — the same adaptation +:class:`~isaaclab_experimental.envs.ManagerBasedRLEnvWarp` runs in its +``__init__``: + +* Every *stable* task id with declared warp twin coverage: resolve the + ``newton_mjwarp`` preset and adapt the stable cfg, exactly what + ``--frontend warp`` does. +* Every registered manager-based ``*-Warp-v0`` variant (velocity deltas whose + stable cfgs still contain terms without warp twins): load its env cfg, + resolve presets, and adapt. + +A dedicated stable Cartpole case also guards the stable-to-experimental +module routing used by ``--frontend warp``. +""" + +from __future__ import annotations + +import importlib + +import gymnasium as gym +import isaaclab_tasks_experimental # noqa: F401 +import pytest +from isaaclab_experimental.envs.frontend import WarpFrontend +from isaaclab_newton.physics import NewtonCfg + +# Registering the task packages is the whole point — import for side effects. +import isaaclab_tasks # noqa: F401 +from isaaclab_tasks.utils.hydra import resolve_presets + +# Stable task ids whose MDP terms are fully twinned: ``--frontend warp`` adapts +# their cfgs directly, with no parallel ``*-Warp-v0`` registration. Extend this +# list when a new task family gains full twin coverage. +_STABLE_TASKS_WITH_WARP_COVERAGE = [ + "Isaac-Ant", + "Isaac-Cartpole", + "Isaac-Humanoid", + "Isaac-Reach-Franka", + "Isaac-Reach-Franka-Play", + "Isaac-Reach-UR10", + "Isaac-Reach-UR10-Play", + "Isaac-Velocity-Flat-AnymalD", + "Isaac-Velocity-Flat-AnymalD-Play", + "Isaac-Velocity-Flat-Cassie", + "Isaac-Velocity-Flat-Cassie-Play", + "Isaac-Velocity-Flat-G1", + "Isaac-Velocity-Flat-G1-Play", + "Isaac-Velocity-Flat-H1", + "Isaac-Velocity-Flat-H1-Play", + "Isaac-Velocity-Flat-UnitreeGo2", + "Isaac-Velocity-Flat-UnitreeGo2-Play", +] + +# Manager-based warp tasks are exactly those whose entry point is the shared warp +# env class; direct tasks provide their own env class (via ``warp_entry_point`` +# or a warp-native registration) and are not cfg-adapted, so they are excluded here. +_MANAGER_WARP_ENTRY_POINT = "isaaclab_experimental.envs:ManagerBasedRLEnvWarp" + +_WARP_ROOTS = ("isaaclab_experimental", "isaaclab_tasks_experimental") + + +def _manager_warp_tasks() -> list[tuple[str, str]]: + """Return ``(task_id, env_cfg_entry_point)`` for every manager-based warp task.""" + tasks: list[tuple[str, str]] = [] + for task_id, spec in gym.registry.items(): + if spec.entry_point != _MANAGER_WARP_ENTRY_POINT: + continue + cfg_entry = (spec.kwargs or {}).get("env_cfg_entry_point") + if isinstance(cfg_entry, str): + tasks.append((task_id, cfg_entry)) + return sorted(tasks) + + +def _load_adapted_cfg(cfg_entry_point: str): + """Instantiate an env cfg, resolve the Newton preset, and adapt it for warp.""" + module_path, class_name = cfg_entry_point.split(":") + cfg = getattr(importlib.import_module(module_path), class_name)() + cfg = resolve_presets(cfg, selected=("newton_mjwarp",)) + assert isinstance(cfg.sim.physics, NewtonCfg), "task does not provide a newton_mjwarp physics preset" + # Raises FrontendIncompatibleError if any warp-managed term lacks a warp twin. + WarpFrontend.adapt_cfg(cfg) + return cfg + + +def _cfg_entry_point(task_id: str) -> str: + cfg_entry = (gym.spec(task_id).kwargs or {}).get("env_cfg_entry_point") + assert isinstance(cfg_entry, str), f"{task_id}: no env_cfg_entry_point" + return cfg_entry + + +def test_no_manager_warp_variants_remain(): + """End-state pin: manager-based warp execution needs no parallel registrations.""" + assert _manager_warp_tasks() == [], "unexpected ManagerBasedRLEnvWarp registrations reappeared" + + +def test_no_warp_task_registrations_remain(): + """End-state pin: warp execution needs no parallel ``-Warp`` task ids at all.""" + warp_ids = sorted(task_id for task_id in gym.registry if "-Warp" in task_id) + assert warp_ids == [], f"unexpected warp task registrations: {warp_ids}" + + +@pytest.mark.parametrize("task_id", _STABLE_TASKS_WITH_WARP_COVERAGE, ids=_STABLE_TASKS_WITH_WARP_COVERAGE) +def test_stable_task_cfg_adapts_to_warp(task_id: str): + """Each covered stable task adapts without a missing twin (the --frontend warp path).""" + cfg = _load_adapted_cfg(_cfg_entry_point(task_id)) + + # Action terms carry a ``class_type`` (not a ``func``) and live on a base that + # is not a ManagerTermBaseCfg; guard that the adapter still swaps them to the + # warp ActionTerm, otherwise the warp ActionManager rejects them at runtime. + actions = getattr(cfg, "actions", None) + if actions is not None: + for name, term in vars(actions).items(): + class_type = getattr(term, "class_type", None) + if class_type is not None: + assert class_type.__module__.startswith(_WARP_ROOTS), ( + f"{task_id}: action term '{name}' class_type was not swapped to a warp twin" + f" (got {class_type.__module__}.{class_type.__name__})" + ) + + +def _direct_tasks_with_warp_entry_point() -> list[tuple[str, str]]: + """Return ``(task_id, warp_entry_point)`` for stable direct tasks declaring a warp env.""" + tasks: list[tuple[str, str]] = [] + for task_id, spec in gym.registry.items(): + target = (spec.kwargs or {}).get("warp_entry_point") + if isinstance(target, str): + tasks.append((task_id, target)) + return sorted(tasks) + + +_DIRECT_WARP_TASKS = _direct_tasks_with_warp_entry_point() + + +def test_direct_tasks_declare_expected_warp_entry_points(): + """Sanity: the stable direct tasks with warp implementations declare them.""" + declared = {task_id for task_id, _ in _DIRECT_WARP_TASKS} + assert { + "Isaac-Cartpole-Direct", + "Isaac-Ant-Direct", + "Isaac-Humanoid-Direct", + "Isaac-Reorient-Cube-Allegro-Direct", + } <= declared + + +@pytest.mark.parametrize( + "task_id, warp_entry_point", + [pytest.param(task_id, target, id=task_id) for task_id, target in _DIRECT_WARP_TASKS], +) +def test_declared_direct_warp_entry_point_resolves(task_id: str, warp_entry_point: str): + """Each declared ``warp_entry_point`` imports and names a warp-rooted class.""" + module_path, class_name = warp_entry_point.split(":") + env_class = getattr(importlib.import_module(module_path), class_name) + assert isinstance(env_class, type), f"{task_id}: {warp_entry_point} is not a class" + assert env_class.__module__.startswith(_WARP_ROOTS) + + +def test_stable_cartpole_cfg_adapts_to_current_warp_module_layout(): + """The stable Cartpole cfg resolves task-specific twins in the current package layout.""" + from isaaclab_experimental.managers.action_manager import ActionTerm + + cfg = _load_adapted_cfg(_cfg_entry_point("Isaac-Cartpole")) + + assert cfg.rewards.pole_pos.func.__module__.startswith("isaaclab_tasks_experimental.core.cartpole.mdp") + assert cfg.rewards.success_rate.func.__module__.startswith("isaaclab_tasks_experimental.core.cartpole.mdp") + assert issubclass(cfg.actions.joint_effort.class_type, ActionTerm) + + +def _iter_obs_terms(cfg): + """Yield (name, term cfg) for every observation term that carries a noise slot.""" + from isaaclab.managers.manager_term_cfg import ObservationGroupCfg + + for group in vars(cfg.observations).values(): + if not isinstance(group, ObservationGroupCfg): + continue + for name, term in vars(group).items(): + if hasattr(term, "noise"): + yield name, term + + +def test_stable_observation_noise_converts_to_warp_twins(): + """Stable noise cfgs (e.g. Unoise) become warp-native twins with the same parameters.""" + from isaaclab.utils import noise as stable_noise + + entry = _cfg_entry_point("Isaac-Velocity-Flat-UnitreeGo2") + module_path, class_name = entry.split(":") + raw = getattr(importlib.import_module(module_path), class_name)() + stable_params = { + name: (term.noise.n_min, term.noise.n_max) + for name, term in _iter_obs_terms(raw) + if isinstance(term.noise, stable_noise.UniformNoiseCfg) + } + assert stable_params, "expected uniform noise on the stable velocity observations" + + cfg = _load_adapted_cfg(entry) + converted = dict(_iter_obs_terms(cfg)) + for name, (n_min, n_max) in stable_params.items(): + twin = converted[name].noise + assert type(twin).__module__.startswith("isaaclab_experimental."), name + assert (twin.n_min, twin.n_max) == (n_min, n_max), name + + +def test_noise_cfg_without_warp_twin_is_a_hard_error(): + """Class-based noise models have no warp twin yet: adapting must fail loudly.""" + from isaaclab_experimental.envs.frontend import FrontendIncompatibleError + + from isaaclab.utils.noise import NoiseModelCfg, UniformNoiseCfg + + entry = _cfg_entry_point("Isaac-Velocity-Flat-UnitreeGo2") + module_path, class_name = entry.split(":") + cfg = getattr(importlib.import_module(module_path), class_name)() + cfg = resolve_presets(cfg, selected=("newton_mjwarp",)) + name, term = next(iter(_iter_obs_terms(cfg))) + term.noise = NoiseModelCfg(noise_cfg=UniformNoiseCfg()) + with pytest.raises(FrontendIncompatibleError, match="noise"): + WarpFrontend.adapt_cfg(cfg) + + +@pytest.mark.parametrize( + "task_id", + ["Isaac-Velocity-Rough-AnymalD", "Isaac-Reach-Franka"], + ids=["rough-terrain-levels", "reach-weight-modifiers"], +) +def test_curriculum_terms_swap_to_mask_native_twins(task_id: str): + """Curriculum terms resolve to the mask-native warp classes, not legacy ID paths.""" + cfg = _load_adapted_cfg(_cfg_entry_point(task_id)) + terms = {n: t for n, t in vars(cfg.curriculum).items() if t is not None and hasattr(t, "func")} + assert terms, f"expected curriculum terms on {task_id}" + for name, term in terms.items(): + assert not isinstance(term.func, str) and term.func.__module__.startswith(_WARP_ROOTS), ( + f"curriculum term '{name}' was not swapped to a warp twin (got {term.func!r})" + ) + assert isinstance(term.func, type), ( + f"curriculum term '{name}' resolved to a legacy function instead of a mask-native class" + ) + + +def test_curriculum_term_without_twin_stays_stable(): + """Optional group: a curriculum term lacking a twin stays on the legacy fallback, no error.""" + import isaaclab.utils.math as math_utils + + entry = _cfg_entry_point("Isaac-Velocity-Rough-AnymalD") + module_path, class_name = entry.split(":") + cfg = getattr(importlib.import_module(module_path), class_name)() + cfg = resolve_presets(cfg, selected=("newton_mjwarp",)) + term = next(t for t in vars(cfg.curriculum).values() if t is not None and hasattr(t, "func")) + term.func = math_utils.quat_mul # stable-rooted symbol with no warp mirror + WarpFrontend.adapt_cfg(cfg) + assert term.func is math_utils.quat_mul diff --git a/source/isaaclab_experimental/test/envs/test_manager_based_rl_env_warp.py b/source/isaaclab_experimental/test/envs/test_manager_based_rl_env_warp.py new file mode 100644 index 000000000000..692884d9cba0 --- /dev/null +++ b/source/isaaclab_experimental/test/envs/test_manager_based_rl_env_warp.py @@ -0,0 +1,228 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for mask-first manager-based RL reset orchestration.""" + +from __future__ import annotations + +from types import SimpleNamespace +from unittest.mock import Mock + +import numpy as np +import torch +import warp as wp +from isaaclab_experimental.envs.manager_based_rl_env_warp import ManagerBasedRLEnvWarp + +from isaaclab.utils.warp import WarpLaunchCache + + +def test_reset_without_host_consumers_does_not_compact_ids() -> None: + """The normal Warp reset path should not call :meth:`torch.Tensor.nonzero`.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + reset_mask = wp.array([False, True, False], dtype=wp.bool, device="cpu") + env.termination_manager = SimpleNamespace(dones_wp=reset_mask) + env.reset_buf = Mock() + env.reset_buf.any.return_value.item.return_value = True + env.reset_buf.nonzero.side_effect = AssertionError("reset IDs should not be materialized") + env._reset_mask = Mock() + env._has_recorders = False + env.has_rtx_sensors = False + env.cfg = SimpleNamespace(compute_final_obs=False, num_rerenders_on_reset=0) + env.extras = {"log": {}} + + env._reset_terminated_envs() + + env.reset_buf.nonzero.assert_not_called() + env._reset_mask.assert_called_once_with(env_mask=reset_mask, env_ids=None) + + +def test_curriculum_uses_mask_before_scene_reset() -> None: + """Curriculum updates should stay mask-native and precede scene reset.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + stages: list[tuple[str, dict]] = [] + + def call(stage, fn, /, *args, **kwargs): + del fn, args + stages.append((stage, kwargs)) + return None if stage.endswith("_compute") else {} + + env._warp_graph_cache = SimpleNamespace(call=call) + env.sim = SimpleNamespace(device="cpu") + env.common_step_counter = 7 + env._global_env_step_count_wp = wp.zeros(1, dtype=wp.int32, device="cpu") + env._warp_launch = WarpLaunchCache(device="cpu") + env.reset_mask_wp = wp.zeros(3, dtype=wp.bool, device="cpu") + env.scene = SimpleNamespace( + num_envs=3, + reset=Mock(side_effect=lambda **kwargs: stages.append(("Scene_reset", kwargs))), + surface_grippers={}, + ) + env.curriculum_manager = SimpleNamespace(compute=Mock(), reset=Mock(), requires_host_ids=False) + env.event_manager = SimpleNamespace(available_modes=[], reset=Mock()) + env.observation_manager = SimpleNamespace(reset=Mock()) + env.action_manager = SimpleNamespace(reset=Mock()) + env.reward_manager = SimpleNamespace(reset=Mock()) + env.command_manager = SimpleNamespace(reset=Mock()) + env.termination_manager = SimpleNamespace(reset=Mock()) + env._episode_length_buf_wp = wp.ones(3, dtype=wp.int64, device="cpu") + env.extras = {} + reset_mask = wp.array([False, True, False], dtype=wp.bool, device="cpu") + env._reset_mask(env_mask=reset_mask) + + assert [stage for stage, _ in stages[:2]] == ["CurriculumManager_compute", "Scene_reset"] + assert stages[0][1]["env_mask"] is env.reset_mask_wp + np.testing.assert_array_equal(stages[0][1]["env_mask"].numpy(), reset_mask.numpy()) + assert "env_ids" not in stages[0][1] + assert "CurriculumManager_reset" in [stage for stage, _ in stages] + + +def test_reset_stages_reuse_owner_mask_for_sparse_and_empty_selections() -> None: + """Every reset dispatch should receive the stable owner-held mask pointer.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + seen: list[tuple[wp.array, np.ndarray]] = [] + + def call(stage, fn, /, *args, **kwargs): + del fn, args + if stage == "CurriculumManager_compute": + mask = kwargs["env_mask"] + seen.append((mask, mask.numpy().copy())) + return None if stage.endswith("_compute") or stage == "Scene_reset" else {} + + env._warp_graph_cache = SimpleNamespace(call=call) + env.sim = SimpleNamespace(device="cpu") + env.common_step_counter = 7 + env._global_env_step_count_wp = wp.zeros(1, dtype=wp.int32, device="cpu") + env._warp_launch = WarpLaunchCache(device="cpu") + env.reset_mask_wp = wp.zeros(3, dtype=wp.bool, device="cpu") + env.scene = SimpleNamespace(num_envs=3, reset=Mock(), surface_grippers={}) + env.curriculum_manager = SimpleNamespace(compute=Mock(), reset=Mock(), requires_host_ids=False) + env.event_manager = SimpleNamespace(available_modes=[], reset=Mock()) + env.observation_manager = SimpleNamespace(reset=Mock()) + env.action_manager = SimpleNamespace(reset=Mock()) + env.reward_manager = SimpleNamespace(reset=Mock()) + env.command_manager = SimpleNamespace(reset=Mock()) + env.termination_manager = SimpleNamespace(reset=Mock()) + env._episode_length_buf_wp = wp.ones(3, dtype=wp.int64, device="cpu") + env.extras = {} + + env._reset_mask(env_mask=wp.array([False, True, False], dtype=wp.bool, device="cpu")) + env._reset_mask(env_mask=wp.zeros(3, dtype=wp.bool, device="cpu")) + + assert seen[0][0] is env.reset_mask_wp + assert seen[1][0] is env.reset_mask_wp + np.testing.assert_array_equal(seen[0][1], [False, True, False]) + np.testing.assert_array_equal(seen[1][1], [False, False, False]) + + +def test_reset_threads_one_id_materialization_to_legacy_curriculum() -> None: + """Legacy curriculum consumers should share one compact-ID materialization per reset.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + stages: dict[str, dict] = {} + + def call(stage, fn, /, *args, **kwargs): + del fn, args + stages[stage] = kwargs + return None if stage.endswith("_compute") else {} + + env._warp_graph_cache = SimpleNamespace(call=call) + env._warp_launch = WarpLaunchCache(device="cpu") + env.sim = SimpleNamespace(device="cpu") + env.reset_mask_wp = wp.zeros(3, dtype=wp.bool, device="cpu") + env.scene = SimpleNamespace(num_envs=3, reset=Mock(), surface_grippers={}) + env.curriculum_manager = SimpleNamespace(compute=Mock(), reset=Mock(), requires_host_ids=True) + env.event_manager = SimpleNamespace(available_modes=[], reset=Mock()) + env.observation_manager = SimpleNamespace(reset=Mock()) + env.action_manager = SimpleNamespace(reset=Mock()) + env.reward_manager = SimpleNamespace(reset=Mock()) + env.command_manager = SimpleNamespace(reset=Mock()) + env.termination_manager = SimpleNamespace(reset=Mock()) + env._episode_length_buf_wp = wp.ones(3, dtype=wp.int64, device="cpu") + env.extras = {} + + env._reset_mask(env_mask=wp.array([False, True, True], dtype=wp.bool, device="cpu")) + + compute_ids = stages["CurriculumManager_compute"]["env_ids"] + torch.testing.assert_close(compute_ids, torch.tensor([1, 2])) + assert stages["CurriculumManager_reset"]["env_ids"] is compute_ids + + +def test_reset_compacts_ids_only_for_active_recorders() -> None: + """Recorder callbacks should receive IDs while the Warp reset keeps its mask.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + reset_mask = wp.array([False, True, False], dtype=wp.bool, device="cpu") + env.termination_manager = SimpleNamespace(dones_wp=reset_mask) + env.reset_buf = torch.tensor([False, True, False]) + env._reset_mask = Mock() + env.recorder_manager = Mock() + env.recorder_manager.reset.return_value = {"recorder": 1.0} + env._has_recorders = True + env.has_rtx_sensors = False + env.cfg = SimpleNamespace(compute_final_obs=False, num_rerenders_on_reset=0) + env.extras = {"log": {}} + + env._reset_terminated_envs() + + reset_ids = env.recorder_manager.record_pre_reset.call_args.args[0] + torch.testing.assert_close(reset_ids, torch.tensor([1])) + env._reset_mask.assert_called_once_with(env_mask=reset_mask, env_ids=reset_ids) + env.recorder_manager.reset.assert_called_once_with(reset_ids) + env.recorder_manager.record_post_reset.assert_called_once_with(reset_ids) + assert env.extras["log"]["recorder"] == 1.0 + + +def test_rtx_rerender_does_not_require_compact_reset_ids() -> None: + """RTX reset rendering should use the nonempty predicate without materializing IDs.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + reset_mask = wp.array([False, True, False], dtype=wp.bool, device="cpu") + env.termination_manager = SimpleNamespace(dones_wp=reset_mask) + env.reset_buf = torch.tensor([False, True, False]) + env._reset_mask = Mock() + env._has_recorders = False + env.has_rtx_sensors = True + env.cfg = SimpleNamespace(compute_final_obs=False, num_rerenders_on_reset=2) + env.sim = SimpleNamespace(render=Mock()) + env.extras = {"log": {}} + + env._reset_terminated_envs() + + env._reset_mask.assert_called_once_with(env_mask=reset_mask, env_ids=None) + assert env.sim.render.call_count == 2 + + +def test_empty_reset_skips_warp_pipeline_without_compacting_ids() -> None: + """An empty mask should skip eager reset stages without materializing IDs.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + reset_mask = wp.zeros(3, dtype=wp.bool, device="cpu") + env.termination_manager = SimpleNamespace(dones_wp=reset_mask) + env.reset_buf = torch.zeros(3, dtype=torch.bool) + env._reset_mask = Mock() + env._has_recorders = False + env.extras = {"log": {"previous": 1.0}} + + env._reset_terminated_envs() + + env._reset_mask.assert_not_called() + assert env.extras["log"] == {"previous": 1.0} + + +def test_reset_captures_final_observation_before_reset() -> None: + """Same-step autoreset should expose the terminal observation before reset.""" + env = ManagerBasedRLEnvWarp.__new__(ManagerBasedRLEnvWarp) + reset_mask = wp.array([False, True, False], dtype=wp.bool, device="cpu") + final_obs = {"policy": torch.tensor([[1.0], [2.0], [3.0]])} + env.termination_manager = SimpleNamespace(dones_wp=reset_mask) + env.reset_buf = torch.tensor([False, True, False]) + env.observation_manager = SimpleNamespace(compute=Mock(return_value=final_obs)) + env._reset_mask = Mock() + env._has_recorders = False + env.has_rtx_sensors = False + env.cfg = SimpleNamespace(compute_final_obs=True, num_rerenders_on_reset=0) + env.extras = {"log": {}} + + env._reset_terminated_envs() + + assert env.extras["final_obs"] is final_obs + env.observation_manager.compute.assert_called_once_with() + env._reset_mask.assert_called_once_with(env_mask=reset_mask, env_ids=None) diff --git a/source/isaaclab_experimental/test/managers/test_command_action_manager.py b/source/isaaclab_experimental/test/managers/test_command_action_manager.py new file mode 100644 index 000000000000..ba0c05381749 --- /dev/null +++ b/source/isaaclab_experimental/test/managers/test_command_action_manager.py @@ -0,0 +1,130 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for recurring command and action manager launch replay.""" + +from __future__ import annotations + +from collections.abc import Hashable, Sequence +from types import SimpleNamespace +from unittest.mock import Mock + +import numpy as np +import warp as wp +from isaaclab_experimental.managers import ActionManager, CommandTerm + + +class _LaunchRecorder: + """Record cached-launch sites while executing kernels eagerly.""" + + def __init__(self, device: str): + self._device = device + self.sites: list[Hashable | None] = [] + + def launch( + self, + kernel: wp.Kernel, + dim: int | Sequence[int], + inputs=(), + outputs=(), + *, + stream: wp.Stream | None = None, + site: Hashable | None = None, + ) -> None: + self.sites.append(site) + wp.launch(kernel, dim=dim, inputs=inputs, outputs=outputs, device=self._device, stream=stream) + + +class _Env: + """Minimal environment owner for manager launch tests.""" + + def __init__(self, num_envs: int = 4): + self.num_envs = num_envs + self.device = "cpu" + self._warp_launch = _LaunchRecorder(self.device) + self.rng_state_wp = wp.array(range(11, 11 + num_envs), dtype=wp.uint32, device=self.device) + self.sim = SimpleNamespace( + vis_marker_registry=SimpleNamespace( + add_debug_vis_callback=lambda term: None, + clear_debug_vis_callback=lambda term: None, + ) + ) + + +class _CommandTerm(CommandTerm): + """Minimal command term exposing the base recurring launches.""" + + def __init__(self, env: _Env): + cfg = SimpleNamespace(debug_vis=False, resampling_time_range=(1.0, 1.0)) + super().__init__(cfg, env) + self._command = wp.zeros((self.num_envs, 1), dtype=wp.float32, device=self.device) + self.metrics["error"] = wp.ones(self.num_envs, dtype=wp.float32, device=self.device) + self._prepare_reset_extras() + self.resample_masks: list[wp.array] = [] + + @property + def command(self) -> wp.array: + return self._command + + def _update_metrics(self) -> None: + pass + + def _resample_command(self, env_mask: wp.array) -> None: + self.resample_masks.append(env_mask) + + def _update_command(self) -> None: + pass + + +def test_command_terms_use_instance_and_mask_specific_launch_sites() -> None: + """Command replay sites should not collide across term instances or persistent masks.""" + env = _Env() + first_term = _CommandTerm(env) + second_term = _CommandTerm(env) + reset_mask = wp.array([True, False, True, False], dtype=wp.bool, device=env.device) + + assert env._warp_launch.sites == [] + + first_term.reset(env_mask=reset_mask) + first_term.compute(dt=0.1) + second_term.compute(dt=0.1) + wp.synchronize() + + assert env._warp_launch.sites == [ + ("command_term", first_term, "reset_count", reset_mask), + ("command_term", first_term, "reset_scale"), + ("command_term", first_term, "reset_metric", "error", reset_mask), + ("command_term", first_term, "reset_counter", reset_mask), + ("command_term", first_term, "resample_time_left", reset_mask, 1.0, 1.0), + ("command_term", first_term, "step_time_left", 0.1), + ("command_term", first_term, "resample_time_left", first_term._resample_mask_wp, 1.0, 1.0), + ("command_term", second_term, "step_time_left", 0.1), + ("command_term", second_term, "resample_time_left", second_term._resample_mask_wp, 1.0, 1.0), + ] + assert first_term.resample_masks == [reset_mask, first_term._resample_mask_wp] + assert second_term.resample_masks == [second_term._resample_mask_wp] + + +def test_action_manager_uses_mask_specific_launch_sites() -> None: + """Action history reset should replay independently for each persistent mask.""" + env = _Env() + action_term = SimpleNamespace(action_dim=2, reset=Mock()) + manager = ActionManager.__new__(ActionManager) + manager._env = env + manager._terms = {"action": action_term} + manager._action = wp.full((env.num_envs, 2), 1.0, dtype=wp.float32, device=env.device) + manager._prev_action = wp.full((env.num_envs, 2), 2.0, dtype=wp.float32, device=env.device) + reset_mask = wp.array([True, False, True, False], dtype=wp.bool, device=env.device) + + manager.reset(env_mask=reset_mask) + wp.synchronize() + + assert env._warp_launch.sites == [ + ("action_manager", manager, "reset_previous", reset_mask), + ("action_manager", manager, "reset_current", reset_mask), + ] + action_term.reset.assert_called_once_with(env_mask=reset_mask) + np.testing.assert_array_equal(manager._action.numpy(), [[0.0, 0.0], [1.0, 1.0], [0.0, 0.0], [1.0, 1.0]]) + np.testing.assert_array_equal(manager._prev_action.numpy(), [[0.0, 0.0], [2.0, 2.0], [0.0, 0.0], [2.0, 2.0]]) diff --git a/source/isaaclab_experimental/test/managers/test_curriculum_manager.py b/source/isaaclab_experimental/test/managers/test_curriculum_manager.py new file mode 100644 index 000000000000..618ae8d24994 --- /dev/null +++ b/source/isaaclab_experimental/test/managers/test_curriculum_manager.py @@ -0,0 +1,472 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for the Warp-first curriculum manager and terrain-level update path.""" + +from __future__ import annotations + +from types import SimpleNamespace + +import numpy as np +import pytest +import torch +import warp as wp +from isaaclab_experimental.managers import CurriculumManager, CurriculumTermCfg +from isaaclab_tasks_experimental.core.reach.mdp import modify_reward_weight +from isaaclab_tasks_experimental.core.velocity.mdp import terrain_levels_vel + +from isaaclab.managers import ManagerTermBase as StableManagerTermBase +from isaaclab.terrains import TerrainImporter +from isaaclab.utils.warp import WarpLaunchCache + + +class _GlobalCurriculumTerm(StableManagerTermBase): + """Legacy global term that intentionally ignores compact environment IDs.""" + + def __call__(self, env, env_ids, value: float) -> float: + del env_ids # a stable term is free to ignore its selection + return value + + +class _LegacyIdCurriculumTerm(StableManagerTermBase): + """Legacy term that records the compact IDs received by compute and reset.""" + + def __init__(self, cfg, env): + super().__init__(cfg, env) + self.compute_env_ids = None + self.reset_env_ids = None + + def __call__(self, env, env_ids, scale: float) -> float: + del env + self.compute_env_ids = env_ids.clone() + return len(env_ids) * scale + + def reset(self, env_ids=None) -> None: + self.reset_env_ids = env_ids.clone() + + +def _structured_state(env, env_ids) -> dict[str, float]: + """Legacy curriculum state unsupported by the scalar Warp logging buffer.""" + del env, env_ids + return {"value": 1.0} + + +class _ArrayProxy: + """Minimal Torch/Warp array proxy.""" + + def __init__(self, values: np.ndarray, dtype): + self.warp = wp.array(values, dtype=dtype, device="cpu") + self.torch = wp.to_torch(self.warp) + + +class _Robot: + """Minimal robot exposing root positions.""" + + def __init__(self, root_positions: np.ndarray): + self.cfg = SimpleNamespace() + self.data = SimpleNamespace(root_pos_w=_ArrayProxy(root_positions, wp.vec3f)) + + +class _CommandManager: + """Minimal command manager exposing persistent Warp storage.""" + + def __init__(self, commands: np.ndarray): + self.command_wp = wp.array(commands, dtype=wp.float32, device="cpu") + + def get_command_wp(self, name: str) -> wp.array(dtype=wp.float32, ndim=2): + assert name == "base_velocity" + return self.command_wp + + +class _RewardManager: + """Minimal reward manager used by the registered Reach curricula.""" + + def __init__(self): + self._term_names = ["action_rate", "joint_vel"] + self._cfgs = { + "action_rate": SimpleNamespace(weight=-0.01), + "joint_vel": SimpleNamespace(weight=-0.0001), + } + self._weights_wp = wp.array([-0.01, -0.0001], dtype=wp.float32, device="cpu") + stride = self._weights_wp.strides[0] + self._weight_views_wp = { + name: wp.array( + ptr=self._weights_wp.ptr + term_idx * stride, + dtype=wp.float32, + shape=(1,), + strides=(stride,), + device="cpu", + ) + for term_idx, name in enumerate(self._term_names) + } + + def get_term_cfg(self, name: str): + return self._cfgs[name] + + def set_term_cfg(self, name: str, cfg) -> None: + self._cfgs[name] = cfg + + def get_term_weight_wp(self, name: str) -> wp.array(dtype=wp.float32): + return self._weight_views_wp[name] + + +class _Scene: + """Minimal scene mapping with terrain-backed environment origins.""" + + def __init__(self, robot: _Robot, terrain: TerrainImporter): + self._entities = {"robot": robot} + self.terrain = terrain + self.device = "cpu" + + @property + def env_origins(self) -> torch.Tensor: + return self.terrain.env_origins + + @property + def env_origins_wp(self) -> wp.array(dtype=wp.vec3f): + return self.terrain.env_origins_wp + + def __getitem__(self, name: str): + return self._entities[name] + + def keys(self): + return self._entities.keys() + + +class _Env: + """Minimal environment for curriculum manager execution.""" + + def __init__(self, terrain: TerrainImporter, root_positions: np.ndarray, commands: np.ndarray): + self.num_envs = root_positions.shape[0] + self.device = "cpu" + self.scene = _Scene(_Robot(root_positions), terrain) + self.command_manager = _CommandManager(commands) + self.rng_state_wp = wp.array(np.arange(self.num_envs, dtype=np.uint32) + 101, device=self.device) + self._global_env_step_count_wp = wp.zeros(1, dtype=wp.int32, device=self.device) + self._warp_launch = WarpLaunchCache(mode="eager", device=self.device) + self.max_episode_length_s = 10.0 + self.sim = SimpleNamespace(is_playing=lambda: True) + + @property + def env_origins_wp(self) -> wp.array(dtype=wp.vec3f): + """Return the scene's persistent Warp origin view.""" + return self.scene.env_origins_wp + + +def _init_terrain_buffers(terrain: TerrainImporter) -> None: + """Mirror the buffer initializers of :meth:`TerrainImporter.__init__`.""" + terrain.terrain_origins = None + terrain.env_origins = None + terrain._terrain_levels_wp = None + terrain._terrain_types_wp = None + terrain._terrain_origins_wp = None + terrain._env_origins_wp = None + + +def _make_terrain(levels: list[int]) -> TerrainImporter: + num_envs = len(levels) + terrain = TerrainImporter.__new__(TerrainImporter) + terrain.device = "cpu" + terrain.cfg = SimpleNamespace( + num_envs=num_envs, + max_init_terrain_level=2, + terrain_generator=SimpleNamespace(size=(8.0, 8.0)), + ) + _init_terrain_buffers(terrain) + level_values = np.asarray(levels, dtype=np.int64) + type_values = np.asarray([index % 2 for index in range(num_envs)], dtype=np.int64) + origin_values = np.zeros((3, 2, 3), dtype=np.float32) + for level in range(3): + for terrain_type in range(2): + origin_values[level, terrain_type] = [ + 100.0 * level + 10.0 * terrain_type, + float(level), + float(terrain_type), + ] + terrain.configure_env_origins(origin_values) + terrain.terrain_levels.copy_(torch.from_numpy(level_values)) + terrain.terrain_types.copy_(torch.from_numpy(type_values)) + terrain.env_origins.copy_(terrain.terrain_origins[terrain.terrain_levels, terrain.terrain_types]) + return terrain + + +def test_terrain_importer_exposes_persistent_warp_views(): + """Terrain importer should retain one zero-copy Warp view for each Torch buffer.""" + terrain = _make_terrain([0, 1, 2, 1]) + + assert isinstance(terrain.terrain_origins, torch.Tensor) + assert isinstance(terrain.env_origins, torch.Tensor) + assert isinstance(terrain.terrain_levels, torch.Tensor) + assert isinstance(terrain.terrain_types, torch.Tensor) + assert terrain.terrain_origins.data_ptr() == terrain._terrain_origins_wp.ptr + assert terrain.env_origins.data_ptr() == terrain.env_origins_wp.ptr + assert terrain.terrain_levels.data_ptr() == terrain.terrain_levels_wp.ptr + assert terrain.terrain_types.data_ptr() == terrain._terrain_types_wp.ptr + + terrain.terrain_levels[0] = 2 + assert terrain.terrain_levels_wp.numpy()[0] == 2 + replacement_levels = wp.array([1, 0, 2, 1], dtype=wp.int64, device="cpu") + wp.copy(terrain.terrain_levels_wp, replacement_levels) + wp.synchronize() + torch.testing.assert_close(terrain.terrain_levels, torch.tensor([1, 0, 2, 1])) + + +def test_terrain_importer_grid_origins_cache_only_environment_view(): + """Grid origins should cache a Warp view without allocating curriculum buffers.""" + terrain = TerrainImporter.__new__(TerrainImporter) + terrain.device = "cpu" + terrain.cfg = SimpleNamespace(num_envs=4, env_spacing=2.0) + _init_terrain_buffers(terrain) + terrain.configure_env_origins() + + assert terrain.terrain_origins is None + assert isinstance(terrain.env_origins, torch.Tensor) + assert terrain.env_origins.shape == (4, 3) + assert terrain.env_origins_wp.shape == (4,) + assert terrain.env_origins.data_ptr() == terrain.env_origins_wp.ptr + assert terrain._terrain_levels_wp is None + assert terrain._terrain_types_wp is None + assert terrain._terrain_origins_wp is None + + +def test_terrain_importer_reconfigure_preserves_cached_views(): + """Reconfiguring origins must reuse buffer storage so cached views stay valid.""" + terrain = _make_terrain([0, 1, 2, 1]) + env_origins_wp = terrain.env_origins_wp + levels_wp = terrain.terrain_levels_wp + env_origins_ptr = terrain.env_origins.data_ptr() + shifted_origins = terrain.terrain_origins.clone() + 1.0 + + terrain.configure_env_origins(shifted_origins) + + assert terrain.env_origins_wp is env_origins_wp + assert terrain.terrain_levels_wp is levels_wp + assert terrain.env_origins.data_ptr() == env_origins_ptr + torch.testing.assert_close(terrain.terrain_origins, shifted_origins) + + +def test_terrain_importer_mask_update_preserves_sparse_and_stable_semantics(): + """Mask updates should handle up/down/clamp/wrap while stable IDs remain supported.""" + terrain = _make_terrain([0, 1, 2, 2, 1, 0]) + env_mask = wp.array([True, True, True, True, False, False], dtype=wp.bool, device="cpu") + move_up = wp.array([False, True, True, False, True, False], dtype=wp.bool, device="cpu") + move_down = wp.array([True, False, False, True, False, True], dtype=wp.bool, device="cpu") + rng_state = wp.array(np.arange(6, dtype=np.uint32) + 71, device="cpu") + rng_before = rng_state.numpy().copy() + origins_before = terrain.env_origins.clone() + levels_wp = terrain.terrain_levels_wp + origins_wp = terrain.env_origins_wp + levels_ptr = levels_wp.ptr + origins_ptr = origins_wp.ptr + assert terrain.terrain_levels.data_ptr() == levels_ptr + assert terrain.env_origins.data_ptr() == origins_ptr + + terrain.update_env_origins_mask(env_mask, move_up, move_down, rng_state) + wp.synchronize() + + levels = terrain.terrain_levels.tolist() + assert levels[0] == 0 + assert levels[1] == 2 + assert 0 <= levels[2] < terrain.max_terrain_level + assert levels[3] == 1 + assert levels[4:] == [1, 0] + torch.testing.assert_close( + terrain.env_origins[:4], + terrain.terrain_origins[terrain.terrain_levels[:4], terrain.terrain_types[:4]], + ) + torch.testing.assert_close(terrain.env_origins[4:], origins_before[4:]) + assert np.all(rng_state.numpy()[:4] != rng_before[:4]) + np.testing.assert_array_equal(rng_state.numpy()[4:], rng_before[4:]) + assert terrain.terrain_levels_wp is levels_wp + assert terrain.env_origins_wp is origins_wp + assert terrain.terrain_levels_wp.ptr == levels_ptr + assert terrain.env_origins_wp.ptr == origins_ptr + + levels_before_stable_update = terrain.terrain_levels.clone() + env_ids = torch.tensor([1, 3], dtype=torch.int64) + terrain.update_env_origins( + env_ids, + move_up=torch.tensor([False, False]), + move_down=torch.tensor([True, True]), + ) + expected_levels = levels_before_stable_update.clone() + expected_levels[env_ids] -= 1 + torch.testing.assert_close(terrain.terrain_levels, expected_levels) + assert terrain.terrain_levels_wp is levels_wp + assert terrain.env_origins_wp is origins_wp + assert terrain.terrain_levels_wp.ptr == levels_ptr + assert terrain.env_origins_wp.ptr == origins_ptr + + +def test_curriculum_manager_updates_masked_levels_and_logs_without_host_compaction(monkeypatch: pytest.MonkeyPatch): + """Manager compute/reset should remain mask-native and expose persistent scalar logging.""" + terrain = _make_terrain([0, 1, 2, 2, 1, 0]) + root_positions = terrain.env_origins.numpy().copy() + root_positions[:, 0] += np.array([5.0, 1.0, 3.0, 5.0, 9.0, 0.0], dtype=np.float32) + commands = np.array( + [[0.1, 0.0, 0.0], [1.0, 0.0, 0.0], [0.2, 0.0, 0.0], [2.0, 0.0, 0.0], [0.1, 0.0, 0.0], [0.1, 0.0, 0.0]], + dtype=np.float32, + ) + env = _Env(terrain, root_positions, commands) + manager = CurriculumManager( + { + "terrain_levels": CurriculumTermCfg(func=terrain_levels_vel), + }, + env, + ) + term = manager._term_cfgs[0].func + env_mask = wp.array([True, True, True, True, False, False], dtype=wp.bool, device="cpu") + move_up_ptr = term._move_up_wp.ptr + move_down_ptr = term._move_down_wp.ptr + state_ptr = manager._term_states_wp.ptr + extras_ref = manager.reset_extras + assert not manager.requires_host_ids + # a purely mask-native manager needs no host boundary at all + assert not manager.requires_host_boundary + + def _fail_host_compaction(*args, **kwargs): + raise AssertionError("Torch host compaction/scalar extraction reached the Warp curriculum path") + + with monkeypatch.context() as context: + context.setattr(torch.Tensor, "nonzero", _fail_host_compaction) + context.setattr(torch.Tensor, "item", _fail_host_compaction) + manager.compute(env_mask) + extras = manager.reset(env_mask) + wp.synchronize() + + levels = terrain.terrain_levels.tolist() + assert levels[0] == 1 + assert levels[1] == 0 + assert levels[2] == 2 + assert 0 <= levels[3] < terrain.max_terrain_level + assert levels[4:] == [1, 0] + np.testing.assert_array_equal(term._move_up_wp.numpy(), [True, False, False, True, False, False]) + np.testing.assert_array_equal(term._move_down_wp.numpy(), [False, True, False, False, False, False]) + assert extras is extras_ref + assert extras["Curriculum/terrain_levels"] is manager.reset_extras["Curriculum/terrain_levels"] + torch.testing.assert_close(extras["Curriculum/terrain_levels"], terrain.terrain_levels.float().mean()) + assert term._move_up_wp.ptr == move_up_ptr + assert term._move_down_wp.ptr == move_down_ptr + assert manager._term_states_wp.ptr == state_ptr + + +def test_curriculum_manager_compacts_ids_inside_legacy_term_boundary(): + """Legacy terms should receive compact IDs without polluting the environment reset API.""" + env = SimpleNamespace( + num_envs=4, + device="cpu", + sim=SimpleNamespace(is_playing=lambda: True), + ) + manager = CurriculumManager( + { + "id_count": CurriculumTermCfg(func=_LegacyIdCurriculumTerm, params={"scale": 2.0}), + "global_state": CurriculumTermCfg(func=_GlobalCurriculumTerm, params={"value": 7.0}), + }, + env, + ) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device="cpu") + term = manager._term_cfgs[0].func + + assert manager.requires_host_ids + assert manager.requires_host_boundary + manager.compute(env_mask) + extras = manager.reset(env_mask) + + torch.testing.assert_close(term.compute_env_ids, torch.tensor([0, 2])) + torch.testing.assert_close(term.reset_env_ids, torch.tensor([0, 2])) + torch.testing.assert_close(extras["Curriculum/id_count"], torch.tensor(4.0)) + torch.testing.assert_close(extras["Curriculum/global_state"], torch.tensor(7.0)) + + +def test_curriculum_manager_consumes_threaded_ids_without_compaction(monkeypatch: pytest.MonkeyPatch): + """Precomputed compact IDs should reach legacy terms without another host materialization.""" + env = SimpleNamespace( + num_envs=4, + device="cpu", + sim=SimpleNamespace(is_playing=lambda: True), + ) + manager = CurriculumManager( + {"id_count": CurriculumTermCfg(func=_LegacyIdCurriculumTerm, params={"scale": 2.0})}, + env, + ) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device="cpu") + precomputed_ids = torch.tensor([0, 2]) + term = manager._term_cfgs[0].func + + def _fail_host_compaction(*args, **kwargs): + raise AssertionError("threaded IDs must not be re-materialized") + + with monkeypatch.context() as context: + context.setattr(torch.Tensor, "nonzero", _fail_host_compaction) + manager.compute(env_mask, env_ids=precomputed_ids) + extras = manager.reset(env_mask, env_ids=precomputed_ids) + + torch.testing.assert_close(term.compute_env_ids, precomputed_ids) + torch.testing.assert_close(term.reset_env_ids, precomputed_ids) + torch.testing.assert_close(extras["Curriculum/id_count"], torch.tensor(4.0)) + + +def test_registered_reach_curricula_update_weights_without_a_host_boundary(): + """Registered Reach reward schedules should update only on a device-selected reset.""" + terrain = _make_terrain([0, 1, 2, 2]) + env = _Env( + terrain, + terrain.env_origins.numpy().copy(), + np.zeros((4, 3), dtype=np.float32), + ) + env.reward_manager = _RewardManager() + env.common_step_counter = 0 + manager = CurriculumManager( + { + "action_rate": CurriculumTermCfg( + func=modify_reward_weight, + params={"term_name": "action_rate", "weight": -0.005, "num_steps": 4500}, + ), + "joint_vel": CurriculumTermCfg( + func=modify_reward_weight, + params={"term_name": "joint_vel", "weight": -0.001, "num_steps": 4500}, + ), + }, + env, + ) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device="cpu") + + assert not manager.requires_host_ids + assert not manager.requires_host_boundary + manager.compute(env_mask) + extras = manager.reset(env_mask) + torch.testing.assert_close(extras["Curriculum/action_rate"], torch.tensor(-0.01)) + torch.testing.assert_close(extras["Curriculum/joint_vel"], torch.tensor(-0.0001)) + + env._global_env_step_count_wp.fill_(4500) + manager.compute(env_mask) + extras = manager.reset(env_mask) + torch.testing.assert_close(extras["Curriculum/action_rate"], torch.tensor(-0.01)) + torch.testing.assert_close(extras["Curriculum/joint_vel"], torch.tensor(-0.0001)) + + env._global_env_step_count_wp.fill_(4501) + empty_mask = wp.zeros(4, dtype=wp.bool, device="cpu") + manager.compute(empty_mask) + np.testing.assert_allclose(env.reward_manager._weights_wp.numpy(), [-0.01, -0.0001]) + + manager.compute(env_mask) + extras = manager.reset(env_mask) + torch.testing.assert_close(extras["Curriculum/action_rate"], torch.tensor(-0.005)) + torch.testing.assert_close(extras["Curriculum/joint_vel"], torch.tensor(-0.001)) + np.testing.assert_allclose(env.reward_manager._weights_wp.numpy(), [-0.005, -0.001]) + + +def test_curriculum_manager_rejects_structured_legacy_state() -> None: + """Unsupported fallback logging should fail explicitly instead of narrowing silently.""" + terrain = _make_terrain([0, 1]) + env = _Env(terrain, terrain.env_origins.numpy().copy(), np.zeros((2, 3), dtype=np.float32)) + manager = CurriculumManager( + {"structured": CurriculumTermCfg(func=_structured_state)}, + env, + ) + env_mask = wp.array([True, False], dtype=wp.bool, device="cpu") + + with pytest.raises(TypeError, match="scalar logging states"): + manager.compute(env_mask) diff --git a/source/isaaclab_experimental/test/managers/test_event_manager.py b/source/isaaclab_experimental/test/managers/test_event_manager.py new file mode 100644 index 000000000000..1bda238a9c7b --- /dev/null +++ b/source/isaaclab_experimental/test/managers/test_event_manager.py @@ -0,0 +1,247 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp-first event manager launch replay.""" + +from __future__ import annotations + +from collections.abc import Hashable +from types import SimpleNamespace + +import pytest +import warp as wp +from isaaclab_experimental.managers import EventManager, EventTermCfg, ManagerTermBase + + +class _LaunchRecorder: + """Record cached-launch sites while executing the kernels eagerly.""" + + def __init__(self, device: str): + self._device = device + self.sites: list[Hashable | None] = [] + self.reset_count = 0 + + def launch( + self, + kernel: wp.Kernel, + dim: int, + inputs=(), + outputs=(), + *, + stream: wp.Stream | None = None, + site: Hashable | None = None, + ) -> None: + self.sites.append(site) + wp.launch(kernel, dim=dim, inputs=inputs, outputs=outputs, device=self._device, stream=stream) + + def reset(self) -> None: + self.reset_count += 1 + + +class _Env: + """Minimal owner for event manager tests.""" + + def __init__(self, num_envs: int = 4, *, is_playing: bool = True): + self.num_envs = num_envs + self.device = "cpu" + self.is_playing = is_playing + self.sim = SimpleNamespace(is_playing=lambda: self.is_playing) + self.scene = SimpleNamespace(cfg=SimpleNamespace(replicate_physics=False)) + self.rng_state_wp = wp.array(range(11, 11 + num_envs), dtype=wp.uint32, device=self.device) + self._warp_launch = _LaunchRecorder(self.device) + self.class_reset_masks: list[wp.array] = [] + self.class_reset_tags: list[str] = [] + self.cache_invalidations = 0 + + def invalidate_wp_graphs(self) -> None: + self.cache_invalidations += 1 + self._warp_launch.reset() + + +def _record_event(env: _Env, env_mask: wp.array(dtype=wp.bool)) -> None: + del env, env_mask + + +class _StatefulResetTerm(ManagerTermBase): + """Stateful reset term used to verify class-term registration.""" + + def reset(self, env_mask: wp.array(dtype=wp.bool) | None = None) -> None: + self._env.class_reset_masks.append(env_mask) + + def __call__(self, env: _Env, env_mask: wp.array(dtype=wp.bool)) -> None: + del env, env_mask + + +class _TaggedResetTerm(ManagerTermBase): + """Stateful reset term used to verify configuration replacement.""" + + def __init__(self, cfg: EventTermCfg, env: _Env): + super().__init__(cfg, env) + self._tag: str = cfg.params["tag"] + + def reset(self, env_mask: wp.array(dtype=wp.bool) | None = None) -> None: + del env_mask + self._env.class_reset_tags.append(self._tag) + + def __call__(self, env: _Env, env_mask: wp.array(dtype=wp.bool), tag: str) -> None: + del env, env_mask, tag + + +def test_recurring_bookkeeping_uses_stable_launch_sites() -> None: + """Only recurring event bookkeeping should use the environment launch cache.""" + env = _Env() + manager = EventManager( + { + "global_interval": EventTermCfg( + func=_record_event, + mode="interval", + interval_range_s=(0.1, 0.1), + is_global_time=True, + ), + "local_interval": EventTermCfg( + func=_record_event, + mode="interval", + interval_range_s=(0.1, 0.1), + ), + "reset_first": EventTermCfg(func=_record_event, mode="reset"), + "reset_second": EventTermCfg( + func=_record_event, + mode="reset", + min_step_count_between_reset=2, + ), + }, + env, + ) + + # Timer allocation, initialization, and RNG seeding are one-shot eager launches. + assert env._warp_launch.sites == [] + + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device=env.device) + global_step_count = wp.array([3], dtype=wp.int32, device=env.device) + manager.apply("interval", dt=0.1) + manager.reset(env_mask=env_mask) + manager.apply("reset", env_mask_wp=env_mask, global_env_step_count=global_step_count) + wp.synchronize() + + assert env._warp_launch.sites == [ + ("event_manager", "interval_step_global", 0, 0.1, 0.1, 0.1), + ("event_manager", "interval_step_per_env", 1, 0.1, 0.1, 0.1), + ("event_manager", "interval_reset", 1, env_mask, 0.1, 0.1), + ("event_manager", "reset_valid_mask", 0, env_mask, 0), + ("event_manager", "reset_valid_mask", 1, env_mask, 2), + ] + + +def test_instantiated_class_term_remains_registered_for_reset() -> None: + """A class term instantiated while parsing should still receive manager resets.""" + env = _Env() + manager = EventManager( + {"stateful_reset": EventTermCfg(func=_StatefulResetTerm, mode="reset")}, + env, + ) + env_mask = wp.array([False, True, False, True], dtype=wp.bool, device=env.device) + + assert isinstance(manager.get_term_cfg("stateful_reset").func, _StatefulResetTerm) + assert manager._mode_class_term_cfgs["reset"] == [manager.get_term_cfg("stateful_reset")] + + manager.reset(env_mask=env_mask) + + assert env.class_reset_masks == [env_mask] + + +def test_set_term_cfg_replaces_instantiated_class_term() -> None: + """Replacing a class term should update reset ownership and invalidate cached work.""" + env = _Env() + manager = EventManager( + {"stateful_reset": EventTermCfg(func=_TaggedResetTerm, mode="reset", params={"tag": "old"})}, + env, + ) + old_term = manager.get_term_cfg("stateful_reset").func + replacement = EventTermCfg(func=_TaggedResetTerm, mode="reset", params={"tag": "new"}) + + manager.set_term_cfg("stateful_reset", replacement) + + new_cfg = manager.get_term_cfg("stateful_reset") + assert isinstance(new_cfg.func, _TaggedResetTerm) + assert new_cfg.func is not old_term + assert manager._mode_class_term_cfgs["reset"] == [new_cfg] + assert env.cache_invalidations == 1 + assert env._warp_launch.reset_count == 1 + + manager.reset(env_mask=wp.array([True] * env.num_envs, dtype=wp.bool, device=env.device)) + + assert env.class_reset_tags == ["new"] + + +@pytest.mark.parametrize("use_dict_cfg", [False, True], ids=["config-object", "dictionary"]) +def test_set_term_cfg_before_play_updates_deferred_resolution_source(use_dict_cfg: bool) -> None: + """A pre-play replacement should be the term instantiated during deferred resolution.""" + env = _Env(is_playing=False) + initial = EventTermCfg(func=_TaggedResetTerm, mode="reset", params={"tag": "old"}) + cfg = {"stateful_reset": initial} if use_dict_cfg else SimpleNamespace(stateful_reset=initial) + manager = EventManager(cfg, env) + replacement = EventTermCfg(func=_TaggedResetTerm, mode="reset", params={"tag": "new"}) + + manager.set_term_cfg("stateful_reset", replacement) + env.is_playing = True + manager._resolve_terms_callback(None) + + resolved_cfg = manager.get_term_cfg("stateful_reset") + source_cfg = manager.cfg["stateful_reset"] if use_dict_cfg else manager.cfg.stateful_reset + assert source_cfg is resolved_cfg + assert isinstance(resolved_cfg.func, _TaggedResetTerm) + + env_mask = wp.array([True] * env.num_envs, dtype=wp.bool, device=env.device) + manager.apply( + "reset", + env_mask_wp=env_mask, + global_env_step_count=wp.zeros(1, dtype=wp.int32, device=env.device), + ) + manager.reset(env_mask=env_mask) + + assert env.class_reset_tags == ["new"] + + +def test_set_interval_term_rejects_missing_range_without_side_effects() -> None: + """An invalid interval replacement should not mutate the term or invalidate caches.""" + env = _Env() + manager = EventManager( + { + "interval": EventTermCfg( + func=_record_event, + mode="interval", + interval_range_s=(0.1, 0.2), + ) + }, + env, + ) + old_cfg = manager.get_term_cfg("interval") + + with pytest.raises(ValueError, match="interval_range_s"): + manager.set_term_cfg("interval", EventTermCfg(func=_record_event, mode="interval")) + + assert manager.get_term_cfg("interval") is old_cfg + assert env.cache_invalidations == 0 + assert env._warp_launch.reset_count == 0 + + +def test_set_reset_term_rejects_negative_step_count_without_side_effects() -> None: + """An invalid reset replacement should not mutate the term or invalidate caches.""" + env = _Env() + manager = EventManager( + {"reset": EventTermCfg(func=_record_event, mode="reset")}, + env, + ) + old_cfg = manager.get_term_cfg("reset") + + with pytest.raises(ValueError, match="min_step_count_between_reset"): + manager.set_term_cfg( + "reset", + EventTermCfg(func=_record_event, mode="reset", min_step_count_between_reset=-1), + ) + + assert manager.get_term_cfg("reset") is old_cfg + assert env.cache_invalidations == 0 + assert env._warp_launch.reset_count == 0 diff --git a/source/isaaclab_experimental/test/managers/test_manager_base.py b/source/isaaclab_experimental/test/managers/test_manager_base.py new file mode 100644 index 000000000000..bef4ad902216 --- /dev/null +++ b/source/isaaclab_experimental/test/managers/test_manager_base.py @@ -0,0 +1,42 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp manager reset-mask contracts.""" + +from types import SimpleNamespace +from unittest.mock import Mock + +import pytest +import warp as wp +from isaaclab_experimental.managers.action_manager import ActionManager +from isaaclab_experimental.managers.manager_base import _resolve_reset_mask +from isaaclab_experimental.utils.warp import WarpCapturable + + +def test_reset_mask_rejects_wrong_device(monkeypatch: pytest.MonkeyPatch) -> None: + """Manager kernels should fail before receiving a mask from another device.""" + owner = SimpleNamespace(num_envs=2, device="expected") + env_mask = wp.zeros(2, dtype=wp.bool, device="cpu") + expected_device = SimpleNamespace(alias="expected") + monkeypatch.setattr(wp, "get_device", lambda device: expected_device) + + with pytest.raises(ValueError, match="env_mask must be on"): + _resolve_reset_mask(owner, None, env_mask) + + +def test_class_term_capturability_is_registered() -> None: + """Class-based manager terms should honor explicit capture metadata.""" + + @WarpCapturable(False) + class HostTerm: + pass + + graph_cache = Mock() + manager = object.__new__(ActionManager) + manager._env = SimpleNamespace(_warp_graph_cache=graph_cache) + + manager._register_term_capturability(HostTerm) + + graph_cache.register_capturability.assert_called_once_with("ActionManager", False) diff --git a/source/isaaclab_experimental/test/managers/test_reward_manager.py b/source/isaaclab_experimental/test/managers/test_reward_manager.py new file mode 100644 index 000000000000..b80a7c9f033d --- /dev/null +++ b/source/isaaclab_experimental/test/managers/test_reward_manager.py @@ -0,0 +1,95 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for device-owned Warp reward weights.""" + +from types import SimpleNamespace +from unittest.mock import Mock + +import pytest +import torch +import warp as wp +from isaaclab_experimental.managers import RewardManager, RewardTermCfg + +from isaaclab.utils.warp import WarpLaunchCache + + +@wp.kernel +def _fill_unit_reward(out: wp.array(dtype=wp.float32)): + """Test helper: write 1.0 into every env's term output.""" + out[wp.tid()] = 1.0 + + +def _unit_reward(env, out: wp.array(dtype=wp.float32)) -> None: + env.term_call_count += 1 + wp.launch(_fill_unit_reward, dim=env.num_envs, inputs=[out], device=env.device) + + +def test_device_owned_weight_can_enable_zero_weight_term() -> None: + """A curriculum-owned weight should be authoritative over the initial config scalar.""" + env = SimpleNamespace( + num_envs=4, + device="cpu", + sim=SimpleNamespace(is_playing=lambda: False), + term_call_count=0, + ) + env._warp_launch = WarpLaunchCache(device=env.device) + manager = RewardManager({"scheduled": RewardTermCfg(func=_unit_reward, weight=0.0)}, env) + + torch.testing.assert_close(manager.compute(dt=1.0), torch.zeros(4)) + assert env.term_call_count == 0 + + manager.get_term_weight_wp("scheduled").fill_(2.0) + torch.testing.assert_close(manager.compute(dt=1.0), torch.full((4,), 2.0)) + assert env.term_call_count == 1 + assert manager.get_term_cfg("scheduled").weight == 2.0 + + +def test_set_term_cfg_invalidates_recorded_work() -> None: + """Setting a term config should invalidate graphs and recorded launches.""" + env = SimpleNamespace( + num_envs=4, + device="cpu", + sim=SimpleNamespace(is_playing=lambda: False), + term_call_count=0, + invalidate_wp_graphs=Mock(), + ) + env._warp_launch = WarpLaunchCache(device=env.device) + manager = RewardManager({"scheduled": RewardTermCfg(func=_unit_reward, weight=1.0)}, env) + + replacement = RewardTermCfg(func=_unit_reward, weight=2.0) + manager.set_term_cfg("scheduled", replacement) + + assert manager.get_term_cfg("scheduled") is replacement + env.invalidate_wp_graphs.assert_called_once_with() + + env.invalidate_wp_graphs.reset_mock() + replacement.weight = 3.0 + manager.set_term_cfg("scheduled", replacement) + env.invalidate_wp_graphs.assert_called_once_with() + + +@pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") +def test_reward_reset_replay_uses_changed_episode_length() -> None: + """Reward reset normalization should specialize replay when the episode duration changes.""" + env = SimpleNamespace( + num_envs=4, + device="cuda:0", + sim=SimpleNamespace(is_playing=lambda: False), + term_call_count=0, + max_episode_length_s=2.0, + ) + env._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=env.device) + manager = RewardManager({"scheduled": RewardTermCfg(func=_unit_reward, weight=1.0)}, env) + env_mask = wp.ones(env.num_envs, dtype=wp.bool, device=env.device) + + manager._episode_sums_wp.fill_(4.0) + first = manager.reset(env_mask=env_mask)["Episode_Reward/scheduled"].clone() + torch.testing.assert_close(first, torch.tensor(2.0, device=env.device)) + + env.max_episode_length_s = 4.0 + manager._episode_sums_wp.fill_(4.0) + second = manager.reset(env_mask=env_mask)["Episode_Reward/scheduled"].clone() + torch.testing.assert_close(second, torch.tensor(1.0, device=env.device)) diff --git a/source/isaaclab_experimental/test/managers/test_termination_manager.py b/source/isaaclab_experimental/test/managers/test_termination_manager.py new file mode 100644 index 000000000000..73de8b50ae40 --- /dev/null +++ b/source/isaaclab_experimental/test/managers/test_termination_manager.py @@ -0,0 +1,40 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for the Warp-first termination manager.""" + +from __future__ import annotations + +from types import SimpleNamespace + +import numpy as np +import warp as wp +from isaaclab_experimental.managers.termination_manager import TerminationManager + +from isaaclab.utils.warp import WarpLaunchCache + + +class TestTerminationManager: + """Tests for :class:`TerminationManager`.""" + + def test_reset_statistics_only_include_selected_environments(self): + """Partial reset statistics should be averaged over selected environments only.""" + manager = TerminationManager.__new__(TerminationManager) + manager._env = SimpleNamespace(num_envs=4, device="cpu") + manager._env._warp_launch = WarpLaunchCache(device=manager._env.device) + manager._term_names = ["fall", "timeout"] + manager._last_episode_dones_wp = wp.array( + [[True, False], [False, True], [True, True], [False, False]], dtype=wp.bool, device="cpu" + ) + manager._term_done_avg_wp = wp.zeros(2, dtype=wp.float32, device="cpu") + manager._reset_count_wp = wp.zeros(1, dtype=wp.int32, device="cpu") + manager._reset_scale_wp = wp.zeros(1, dtype=wp.float32, device="cpu") + manager._class_term_cfgs = [] + manager._reset_extras = {} + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device="cpu") + + manager.reset(env_mask=env_mask) + + np.testing.assert_allclose(manager._term_done_avg_wp.numpy(), [1.0, 0.5]) diff --git a/source/isaaclab_experimental/test/utils/test_manager_call_switch.py b/source/isaaclab_experimental/test/utils/test_manager_call_switch.py deleted file mode 100644 index b4745939eaa0..000000000000 --- a/source/isaaclab_experimental/test/utils/test_manager_call_switch.py +++ /dev/null @@ -1,475 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Tests for ManagerCallSwitch config loading, mode resolution, and dispatch.""" - -from __future__ import annotations - -import json -import os -import unittest - -import warp as wp -from isaaclab_experimental.utils.manager_call_switch import ManagerCallMode, ManagerCallSwitch - - -@wp.kernel -def _add_one(a: wp.array(dtype=wp.float32), b: wp.array(dtype=wp.float32)): - i = wp.tid() - b[i] = a[i] + 1.0 - - -class TestManagerCallMode(unittest.TestCase): - """Tests for the ManagerCallMode enum.""" - - def test_enum_values(self): - self.assertEqual(ManagerCallMode.STABLE, 0) - self.assertEqual(ManagerCallMode.WARP_NOT_CAPTURED, 1) - self.assertEqual(ManagerCallMode.WARP_CAPTURED, 2) - - def test_ordering(self): - self.assertLess(ManagerCallMode.STABLE, ManagerCallMode.WARP_NOT_CAPTURED) - self.assertLess(ManagerCallMode.WARP_NOT_CAPTURED, ManagerCallMode.WARP_CAPTURED) - - -# ====================================================================== -# Config loading -# ====================================================================== - - -class TestConfigLoading(unittest.TestCase): - """Tests for ManagerCallSwitch config parsing from dict, JSON, env var, and None.""" - - def test_none_uses_default(self): - switch = ManagerCallSwitch(cfg_source=None) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.WARP_CAPTURED) - - def test_dict_config(self): - switch = ManagerCallSwitch(cfg_source={"default": 1}) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.WARP_NOT_CAPTURED) - - def test_dict_per_manager_override(self): - switch = ManagerCallSwitch(cfg_source={"default": 2, "RewardManager": 0}) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.STABLE) - self.assertEqual(switch.get_mode_for_manager("ActionManager"), ManagerCallMode.WARP_CAPTURED) - - def test_dict_without_default_key(self): - """A dict missing 'default' should inherit from DEFAULT_CONFIG.""" - switch = ManagerCallSwitch(cfg_source={"RewardManager": 0}) - # default should be 2 (from DEFAULT_CONFIG) - self.assertEqual(switch.get_mode_for_manager("ActionManager"), ManagerCallMode.WARP_CAPTURED) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.STABLE) - - def test_json_string_config(self): - cfg_str = json.dumps({"default": 1, "ObservationManager": 0}) - switch = ManagerCallSwitch(cfg_source=cfg_str) - self.assertEqual(switch.get_mode_for_manager("ObservationManager"), ManagerCallMode.STABLE) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.WARP_NOT_CAPTURED) - - def test_empty_string_uses_default(self): - switch = ManagerCallSwitch(cfg_source="") - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.WARP_CAPTURED) - - def test_env_var_fallback(self): - """When cfg_source is None, should read from MANAGER_CALL_CONFIG env var.""" - old = os.environ.get(ManagerCallSwitch.ENV_VAR) - try: - os.environ[ManagerCallSwitch.ENV_VAR] = json.dumps({"default": 0}) - switch = ManagerCallSwitch(cfg_source=None) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.STABLE) - finally: - if old is None: - os.environ.pop(ManagerCallSwitch.ENV_VAR, None) - else: - os.environ[ManagerCallSwitch.ENV_VAR] = old - - def test_invalid_json_raises(self): - with self.assertRaises(json.JSONDecodeError): - ManagerCallSwitch(cfg_source="not valid json") - - def test_invalid_mode_value_raises(self): - with self.assertRaises(ValueError): - ManagerCallSwitch(cfg_source={"default": 99}) - - def test_non_int_mode_raises(self): - with self.assertRaises(TypeError): - ManagerCallSwitch(cfg_source={"default": "fast"}) - - def test_invalid_cfg_type_raises(self): - with self.assertRaises(TypeError): - ManagerCallSwitch(cfg_source=42) - - -# ====================================================================== -# Mode resolution and capping -# ====================================================================== - - -class TestModeResolution(unittest.TestCase): - """Tests for mode resolution, MAX_MODE_OVERRIDES, and dynamic capturability.""" - - def test_max_mode_override_caps_default(self): - """Scene is capped to WARP_NOT_CAPTURED by MAX_MODE_OVERRIDES.""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - # Scene has a static cap of WARP_NOT_CAPTURED - self.assertEqual( - switch.get_mode_for_manager("Scene"), - ManagerCallMode.WARP_NOT_CAPTURED, - ) - # Other managers should not be capped - self.assertEqual( - switch.get_mode_for_manager("RewardManager"), - ManagerCallMode.WARP_CAPTURED, - ) - - def test_max_modes_kwarg(self): - """max_modes kwarg should cap managers beyond MAX_MODE_OVERRIDES.""" - switch = ManagerCallSwitch( - cfg_source={"default": 2}, - max_modes={"RewardManager": ManagerCallMode.WARP_NOT_CAPTURED}, - ) - self.assertEqual( - switch.get_mode_for_manager("RewardManager"), - ManagerCallMode.WARP_NOT_CAPTURED, - ) - - def test_register_manager_capturability_downgrades(self): - """register_manager_capturability(False) should cap a manager to WARP_NOT_CAPTURED.""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.WARP_CAPTURED) - - switch.register_manager_capturability("RewardManager", capturable=False) - self.assertEqual( - switch.get_mode_for_manager("RewardManager"), - ManagerCallMode.WARP_NOT_CAPTURED, - ) - - def test_register_capturability_true_is_noop(self): - """register_manager_capturability(True) should not change anything.""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - switch.register_manager_capturability("RewardManager", capturable=True) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.WARP_CAPTURED) - - def test_register_capturability_does_not_upgrade(self): - """If a manager is already capped to NOT_CAPTURED, registering capturable=False again shouldn't change it.""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - switch.register_manager_capturability("RewardManager", capturable=False) - switch.register_manager_capturability("RewardManager", capturable=False) - self.assertEqual( - switch.get_mode_for_manager("RewardManager"), - ManagerCallMode.WARP_NOT_CAPTURED, - ) - - def test_capturability_interacts_with_static_cap(self): - """Dynamic capturability should respect existing static caps.""" - switch = ManagerCallSwitch( - cfg_source={"default": 2}, - max_modes={"RewardManager": ManagerCallMode.WARP_NOT_CAPTURED}, - ) - # Already capped, register_capturability(False) should be harmless - switch.register_manager_capturability("RewardManager", capturable=False) - self.assertEqual( - switch.get_mode_for_manager("RewardManager"), - ManagerCallMode.WARP_NOT_CAPTURED, - ) - - def test_mode_0_not_affected_by_cap(self): - """A manager explicitly set to STABLE (0) should stay at 0 even with cap at 1.""" - switch = ManagerCallSwitch(cfg_source={"default": 2, "RewardManager": 0}) - switch.register_manager_capturability("RewardManager", capturable=False) - self.assertEqual(switch.get_mode_for_manager("RewardManager"), ManagerCallMode.STABLE) - - -# ====================================================================== -# Stage dispatch -# ====================================================================== - - -class TestStageDispatch(unittest.TestCase): - """Tests for call_stage routing through stable / warp-eager / warp-captured paths.""" - - def test_stable_mode_calls_stable_fn(self): - switch = ManagerCallSwitch(cfg_source={"default": 0}) - called = {"stable": False, "warp": False} - - def stable_fn(): - called["stable"] = True - return "stable_result" - - def warp_fn(): - called["warp"] = True - return "warp_result" - - result = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": warp_fn}, - stable_call={"fn": stable_fn}, - ) - self.assertTrue(called["stable"]) - self.assertFalse(called["warp"]) - self.assertEqual(result, "stable_result") - - def test_stable_mode_without_stable_call_raises(self): - switch = ManagerCallSwitch(cfg_source={"default": 0}) - with self.assertRaises(ValueError): - switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": lambda: None}, - stable_call=None, - ) - - def test_warp_eager_mode_calls_warp_fn(self): - switch = ManagerCallSwitch(cfg_source={"default": 1}) - called = {"stable": False, "warp": False} - - def stable_fn(): - called["stable"] = True - return "stable_result" - - def warp_fn(): - called["warp"] = True - return "warp_result" - - result = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": warp_fn}, - stable_call={"fn": stable_fn}, - ) - self.assertFalse(called["stable"]) - self.assertTrue(called["warp"]) - self.assertEqual(result, "warp_result") - - def test_output_transform(self): - """The 'output' key in call spec should transform the return value.""" - switch = ManagerCallSwitch(cfg_source={"default": 1}) - - def warp_fn(): - return [1, 2, 3] - - result = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": warp_fn, "output": len}, - ) - self.assertEqual(result, 3) - - def test_args_and_kwargs_forwarded(self): - """call_stage should forward args and kwargs from the call spec.""" - switch = ManagerCallSwitch(cfg_source={"default": 1}) - received = {} - - def warp_fn(a, b, key=None): - received["a"] = a - received["b"] = b - received["key"] = key - - switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": warp_fn, "args": (1, 2), "kwargs": {"key": "val"}}, - ) - self.assertEqual(received, {"a": 1, "b": 2, "key": "val"}) - - -class TestStageDispatchCaptured(unittest.TestCase): - """Tests for WARP_CAPTURED mode (requires GPU).""" - - def setUp(self): - self.device = "cuda:0" - - def test_captured_mode_produces_correct_output(self): - """WARP_CAPTURED should capture and replay a warp kernel correctly.""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - src = wp.full(4, value=5.0, dtype=wp.float32, device=self.device) - dst = wp.zeros(4, dtype=wp.float32, device=self.device) - - def warp_fn(): - wp.launch(_add_one, dim=4, inputs=[src, dst], device=self.device) - return dst - - # First call: warm-up + capture - result = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": warp_fn}, - ) - self.assertAlmostEqual(result.numpy()[0], 6.0, places=5) - - # Replay - wp.copy(src, wp.full(4, value=10.0, dtype=wp.float32, device=self.device)) - result2 = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": warp_fn}, - ) - self.assertIs(result, result2, "Replay must return same reference") - self.assertAlmostEqual(result2.numpy()[0], 11.0, places=5) - - def test_captured_warmup_call_count(self): - """WARP_CAPTURED first call should invoke fn exactly 2 times (warm-up + capture).""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - src = wp.zeros(4, dtype=wp.float32, device=self.device) - dst = wp.zeros(4, dtype=wp.float32, device=self.device) - call_count = [0] - - def warp_fn(): - call_count[0] += 1 - wp.launch(_add_one, dim=4, inputs=[src, dst], device=self.device) - return dst - - # First call_stage: warm-up (1) + capture (2) - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 2, "First captured call should invoke fn twice (warm-up + capture)") - - # Second call_stage: replay only — no new fn invocation - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 2, "Replay should not invoke fn again") - - # Third call_stage: still replay - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 2) - - def test_captured_warmup_handles_hasattr_guard(self): - """Warm-up should flush first-call allocations so capture doesn't record them. - - This simulates the real-world pattern where MDP terms allocate scratch - buffers on first call using hasattr guards. - """ - switch = ManagerCallSwitch(cfg_source={"default": 2}) - src = wp.ones(8, dtype=wp.float32, device=self.device) - holder = {} - - def fn_with_guard(): - if "buf" not in holder: - # First-call allocation — must happen during warm-up, not capture - holder["buf"] = wp.zeros(8, dtype=wp.float32, device=self.device) - wp.launch(_add_one, dim=8, inputs=[src, holder["buf"]], device=self.device) - return holder["buf"] - - # Should not raise — warm-up handles the allocation outside capture context - result = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": fn_with_guard}, - ) - result_np = result.numpy() - for val in result_np: - self.assertAlmostEqual(val, 2.0, places=5) - - # Replay should also work (allocation already done, only kernel replays) - wp.copy(src, wp.full(8, value=5.0, dtype=wp.float32, device=self.device)) - result2 = switch.call_stage( - stage="RewardManager_compute", - warp_call={"fn": fn_with_guard}, - ) - for val in result2.numpy(): - self.assertAlmostEqual(val, 6.0, places=5) - - def test_warp_eager_no_warmup(self): - """WARP_NOT_CAPTURED mode should call fn exactly once per call (no warm-up).""" - switch = ManagerCallSwitch(cfg_source={"default": 1}) - call_count = [0] - - def warp_fn(): - call_count[0] += 1 - - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 1, "Eager mode should call fn exactly once") - - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 2, "Eager mode should call fn again each time") - - -# ====================================================================== -# Graph invalidation -# ====================================================================== - - -class TestGraphInvalidation(unittest.TestCase): - """Tests for invalidate_graphs on ManagerCallSwitch.""" - - def setUp(self): - self.device = "cuda:0" - - def test_invalidate_forces_recapture(self): - switch = ManagerCallSwitch(cfg_source={"default": 2}) - src = wp.full(4, value=1.0, dtype=wp.float32, device=self.device) - dst = wp.zeros(4, dtype=wp.float32, device=self.device) - - call_count = [0] - - def warp_fn(): - call_count[0] += 1 - wp.launch(_add_one, dim=4, inputs=[src, dst], device=self.device) - return dst - - # First capture - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 2) # warm-up + capture - - # Replay — no new calls - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 2) - - # Invalidate - switch.invalidate_graphs() - - # Should re-warm-up + re-capture - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": warp_fn}) - self.assertEqual(call_count[0], 4) - - -# ====================================================================== -# resolve_manager_class -# ====================================================================== - - -class TestResolveManagerClass(unittest.TestCase): - """Tests for resolve_manager_class.""" - - def test_stable_resolves_to_isaaclab_managers(self): - switch = ManagerCallSwitch(cfg_source={"default": 0}) - cls = switch.resolve_manager_class("RewardManager") - self.assertTrue(cls.__module__.startswith("isaaclab.managers")) - - def test_warp_resolves_to_experimental_managers(self): - switch = ManagerCallSwitch(cfg_source={"default": 1}) - cls = switch.resolve_manager_class("RewardManager") - self.assertTrue(cls.__module__.startswith("isaaclab_experimental")) - - def test_mode_override(self): - """mode_override should bypass the config for class resolution.""" - switch = ManagerCallSwitch(cfg_source={"default": 2}) - cls = switch.resolve_manager_class("RewardManager", mode_override=ManagerCallMode.STABLE) - self.assertTrue(cls.__module__.startswith("isaaclab.managers")) - - def test_invalid_manager_raises(self): - switch = ManagerCallSwitch(cfg_source={"default": 0}) - with self.assertRaises(AttributeError): - switch.resolve_manager_class("NonexistentManager") - - -# ====================================================================== -# Manager name parsing -# ====================================================================== - - -class TestManagerNameParsing(unittest.TestCase): - """Tests for stage name → manager name extraction.""" - - def test_valid_stage_name(self): - switch = ManagerCallSwitch(cfg_source={"default": 1}) - # Dispatch a stage with valid name format - called = [False] - - def fn(): - called[0] = True - - switch.call_stage(stage="RewardManager_compute", warp_call={"fn": fn}) - self.assertTrue(called[0]) - - def test_invalid_stage_name_raises(self): - switch = ManagerCallSwitch(cfg_source={"default": 1}) - with self.assertRaises(ValueError): - switch.call_stage(stage="nounderscore", warp_call={"fn": lambda: None}) - - -if __name__ == "__main__": - unittest.main() diff --git a/source/isaaclab_experimental/test/utils/test_mask_boundary_gate.py b/source/isaaclab_experimental/test/utils/test_mask_boundary_gate.py new file mode 100644 index 000000000000..f88857015830 --- /dev/null +++ b/source/isaaclab_experimental/test/utils/test_mask_boundary_gate.py @@ -0,0 +1,214 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Static gate for the mask-native execution path. + +The warp frontend's contract is that mask-native code performs no data-dependent +GPU->host synchronization outside sanctioned boundaries. Instead of inline marker +comments, the gate is structural: + +* **Scope** — every function in the experimental production tree that sits on the + step/reset pipeline, plus every ``*mask*``-named function in the core and Newton + trees (the functions that *are* the warp path there). Torch-first core code is + deliberately out of scope. +* **Sanctioned boundaries** — small, named helper functions listed in + ``SANCTIONED_BOUNDARIES``; the list is exact (a stale entry fails the gate). +* **Runtime backstop** — ``ISAACLAB_SYNC_DEBUG=1`` runs every warp stage under + ``torch.cuda.set_sync_debug_mode("error")`` (see ``WarpGraphCache``), trapping + syncs this static scan cannot see. + +The companion inventory test pins every ``@WarpCapturable(False)`` opt-out so +non-capturable terms are documented in exactly one reviewable place. +""" + +from __future__ import annotations + +import ast +from pathlib import Path + +_REPO_ROOT = Path(__file__).resolve().parents[4] + +# Attribute calls that synchronize (or copy) device memory to the host. +SYNC_ATTRS = {"nonzero", "argwhere", "item", "cpu", "tolist", "numpy"} + +# Trees scanned in full (production only; every function is in scope unless excluded). +FULL_SCAN_ROOTS = [ + "source/isaaclab_experimental/isaaclab_experimental/envs", + "source/isaaclab_experimental/isaaclab_experimental/managers", + "source/isaaclab_experimental/isaaclab_experimental/utils", +] + +# Trees where only ``*mask*``-named functions are in scope (the warp path inside +# otherwise torch-first packages). +MASK_SCAN_ROOTS = [ + "source/isaaclab/isaaclab/actuators", + "source/isaaclab/isaaclab/scene", + "source/isaaclab/isaaclab/sensors", + "source/isaaclab/isaaclab/terrains", + "source/isaaclab_newton/isaaclab_newton", +] + +# Functions that never run on the per-step path: construction, reporting, and +# debug surfaces. Host syncs there are init-time or human-facing. +NON_STEP_FUNCTIONS = { + "__init__", + "__post_init__", + "__str__", + "__repr__", + "__del__", + "_prepare_terms", + "serialize", + "get_active_iterable_terms", + "_debug_vis_callback", + "_set_debug_vis_impl", + # reporting / config-access surfaces (export- or user-facing, not per-step) + "IO_descriptor", + "get_IO_descriptors", + "get_term_cfg", +} + +# The sanctioned mask->ID / host-sync boundaries, by (path suffix, function name). +# Every entry must exist and contain a sync call; anything else that syncs fails. +SANCTIONED_BOUNDARIES = { + ("managers/curriculum_manager.py", "_compact_legacy_env_ids"), + ("envs/mdp/events.py", "_mask_to_env_ids"), + ("envs/manager_based_env_warp.py", "reset_to"), + # Coarse-grained by review preference: the recorder / legacy-term compactions + # stay inline in their reset functions rather than one-line helpers; the + # ISAACLAB_SYNC_DEBUG runtime trap is the fine-grained net inside them. + ("envs/manager_based_env_warp.py", "reset"), + ("envs/manager_based_rl_env_warp.py", "_reset_terminated_envs"), + ("envs/manager_based_rl_env_warp.py", "_reset_mask"), + ("utils/warp/utils.py", "any_env_set"), + # camera's empty-reset predicate (the sensor-side analogue of any_env_set) + ("isaaclab/sensors/camera/camera.py", "_env_mask_has_any"), + # joint-limit writes mutate the solver model (host-side by nature, event-driven) + ("isaaclab_newton/assets/articulation/articulation.py", "write_joint_position_limit_to_sim_mask"), + # legacy-actuator compatibility fallback: mask-native actuator models override + # this; legacy models compact once per reset (event-driven, not per-step). + ("isaaclab/actuators/actuator_base.py", "reset_mask"), +} + + +def _sync_calls(func_node: ast.AST) -> list[str]: + """Names of synchronizing attribute calls inside a function body.""" + hits = [] + for node in ast.walk(func_node): + if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr in SYNC_ATTRS: + hits.append(node.func.attr) + return hits + + +def _iter_functions(tree: ast.Module): + """Yield (function node, name) for every def in the module.""" + for node in ast.walk(tree): + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): + yield node, node.name + + +def _scan(root: Path, mask_only: bool) -> dict[tuple[str, str], list[str]]: + """Map (relative path, function) -> sync hits for in-scope functions under root.""" + findings: dict[tuple[str, str], list[str]] = {} + for path in sorted(root.rglob("*.py")): + rel = path.relative_to(_REPO_ROOT).as_posix() + if "/test/" in rel: + continue + tree = ast.parse(path.read_text(), filename=rel) + for func, name in _iter_functions(tree): + if name in NON_STEP_FUNCTIONS: + continue + if mask_only and "mask" not in name: + continue + hits = _sync_calls(func) + if hits: + findings[(rel, name)] = hits + return findings + + +def _is_sanctioned(rel: str, name: str) -> bool: + return any(rel.endswith(suffix) and name == func for suffix, func in SANCTIONED_BOUNDARIES) + + +def test_mask_native_code_has_no_unsanctioned_host_syncs(): + """Every sync in mask-native scope is one of the named, sanctioned boundaries.""" + findings: dict[tuple[str, str], list[str]] = {} + for root in FULL_SCAN_ROOTS: + findings.update(_scan(_REPO_ROOT / root, mask_only=False)) + for root in MASK_SCAN_ROOTS: + findings.update(_scan(_REPO_ROOT / root, mask_only=True)) + + violations = {k: v for k, v in findings.items() if not _is_sanctioned(*k)} + assert not violations, "Unsanctioned host syncs on the mask-native path:\n" + "\n".join( + f" {rel}::{name} -> {hits}" for (rel, name), hits in sorted(violations.items()) + ) + + # The sanctioned list is exact: every entry must still exist and still sync. + matched = {k for k in findings if _is_sanctioned(*k)} + stale = { + (suffix, func) + for suffix, func in SANCTIONED_BOUNDARIES + if not any(rel.endswith(suffix) and name == func for rel, name in matched) + } + assert not stale, f"Stale sanctioned-boundary entries (function gone or no longer syncs): {sorted(stale)}" + + +# Expected capturability opt-outs — ``@WarpCapturable(False)`` decorations or +# ``_warp_capturable = False`` class attributes: (path suffix, name). +EXPECTED_NON_CAPTURABLE = { + ("isaaclab_experimental/envs/mdp/events.py", "randomize_rigid_body_com"), + ("isaaclab_experimental/envs/mdp/observations.py", "height_scan"), +} + +NON_CAPTURABLE_SCAN_ROOTS = [ + "source/isaaclab_experimental/isaaclab_experimental", + "source/isaaclab_tasks_experimental/isaaclab_tasks_experimental", +] + + +def _warp_capturable_false_targets(tree: ast.Module) -> list[str]: + """Names annotated non-capturable: ``@WarpCapturable(False, ...)`` decorations + or ``_warp_capturable = False`` class attributes.""" + out = [] + for node in ast.walk(tree): + if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + continue + for deco in node.decorator_list: + if ( + isinstance(deco, ast.Call) + and isinstance(deco.func, ast.Name) + and deco.func.id == "WarpCapturable" + and deco.args + and isinstance(deco.args[0], ast.Constant) + and deco.args[0].value is False + ): + out.append(node.name) + if isinstance(node, ast.ClassDef): + for stmt in node.body: + if ( + isinstance(stmt, ast.Assign) + and any(isinstance(t, ast.Name) and t.id == "_warp_capturable" for t in stmt.targets) + and isinstance(stmt.value, ast.Constant) + and stmt.value.value is False + ): + out.append(node.name) + return out + + +def test_non_capturable_terms_are_inventoried(): + """Every ``@WarpCapturable(False)`` opt-out is documented here, and only these.""" + found = set() + for scan_root in NON_CAPTURABLE_SCAN_ROOTS: + for path in sorted((_REPO_ROOT / scan_root).rglob("*.py")): + rel = path.relative_to(_REPO_ROOT).as_posix() + if "/test/" in rel: + continue + for name in _warp_capturable_false_targets(ast.parse(path.read_text(), filename=rel)): + found.add((rel, name)) + expected = {(f"source/{suffix.split('/')[0]}/{suffix}", name) for suffix, name in EXPECTED_NON_CAPTURABLE} + assert found == expected, ( + "Non-capturable inventory drift.\n" + f" unexpected: {sorted(found - expected)}\n" + f" missing: {sorted(expected - found)}" + ) diff --git a/source/isaaclab_experimental/test/utils/test_noise_warp.py b/source/isaaclab_experimental/test/utils/test_noise_warp.py new file mode 100644 index 000000000000..154788fb3e99 --- /dev/null +++ b/source/isaaclab_experimental/test/utils/test_noise_warp.py @@ -0,0 +1,182 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp-native observation noise record and replay.""" + +from types import SimpleNamespace + +import numpy as np +import pytest +import torch +import warp as wp +from isaaclab_experimental.managers import ObservationGroupCfg, ObservationTermCfg +from isaaclab_experimental.managers.observation_manager import ObservationManager +from isaaclab_experimental.utils import noise + +from isaaclab.utils import class_to_dict +from isaaclab.utils.noise import UniformNoiseCfg as StableUniformNoiseCfg +from isaaclab.utils.warp import WarpLaunchCache + +wp.init() +pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") + +_DEVICE = "cuda:0" +_NUM_ENVS = 8 +_OBS_DIM = 3 + + +def _make_rng_state() -> wp.array: + """Create deterministic per-environment RNG state.""" + return wp.array(np.arange(_NUM_ENVS, dtype=np.uint32) + 42, dtype=wp.uint32, device=_DEVICE) + + +def _bind_noise(cfg: noise.NoiseCfg, cache: WarpLaunchCache, rng_state: wp.array) -> None: + """Bind a standalone noise config as the observation manager does.""" + cfg.rng_state_wp = rng_state + cfg._warp_launch = cache + + +def _fill_zeros(env, out: wp.array, out_dim: int) -> None: + """Fill one synthetic observation term.""" + del env, out_dim + out.zero_() + + +def _fill_twos(env, out: wp.array, out_dim: int) -> None: + """Fill one synthetic observation term above its clip range.""" + del env, out_dim + out.fill_(2.0) + + +def _make_observation_manager( + mode: str, noise_cfg: noise.NoiseCfg | StableUniformNoiseCfg | None = None +) -> tuple[ObservationManager, SimpleNamespace]: + """Create a minimal observation manager with uniform corruption enabled.""" + if noise_cfg is None: + noise_cfg = noise.UniformNoiseCfg(n_min=-0.25, n_max=0.5) + term_cfg = ObservationTermCfg( + func=_fill_zeros, + params={"out_dim": _OBS_DIM}, + noise=noise_cfg, + ) + group_cfg = ObservationGroupCfg() + group_cfg.enable_corruption = True + group_cfg.sample = term_cfg + env = SimpleNamespace( + num_envs=_NUM_ENVS, + device=_DEVICE, + sim=SimpleNamespace(is_playing=lambda: True), + scene={}, + rng_state_wp=_make_rng_state(), + _warp_launch=WarpLaunchCache(mode=mode, debug=True, device=_DEVICE), + ) + return ObservationManager({"policy": group_cfg}, env), env + + +def test_constant_noise_replays_static_variants(): + """Constant noise should replay and re-record changed static values.""" + cfg = noise.ConstantNoiseCfg(bias=0.5, operation="add") + cache = WarpLaunchCache(mode="replay", debug=True, device=_DEVICE) + _bind_noise(cfg, cache, _make_rng_state()) + data = wp.ones((_NUM_ENVS, _OBS_DIM), dtype=wp.float32, device=_DEVICE) + + cfg.func(data, cfg) + torch.testing.assert_close(wp.to_torch(data), torch.full((_NUM_ENVS, _OBS_DIM), 1.5, device=_DEVICE)) + + data.fill_(2.0) + cfg.func(data, cfg) + torch.testing.assert_close(wp.to_torch(data), torch.full((_NUM_ENVS, _OBS_DIM), 2.5, device=_DEVICE)) + + cfg.bias = -1.0 + cfg.func(data, cfg) + torch.testing.assert_close(wp.to_torch(data), torch.full((_NUM_ENVS, _OBS_DIM), 1.5, device=_DEVICE)) + + +@pytest.mark.parametrize( + "cfg_factory", + [ + lambda: noise.UniformNoiseCfg(n_min=-0.5, n_max=0.75), + lambda: noise.GaussianNoiseCfg(mean=0.2, std=0.4), + ], + ids=["uniform", "gaussian"], +) +def test_random_noise_replay_matches_eager(cfg_factory): + """Random-noise replay should advance persistent RNG exactly like eager launches.""" + replay_cfg = cfg_factory() + eager_cfg = cfg_factory() + replay_rng = _make_rng_state() + eager_rng = _make_rng_state() + _bind_noise(replay_cfg, WarpLaunchCache(mode="replay", debug=True, device=_DEVICE), replay_rng) + _bind_noise(eager_cfg, WarpLaunchCache(mode="eager", debug=True, device=_DEVICE), eager_rng) + replay_data = wp.zeros((_NUM_ENVS, _OBS_DIM), dtype=wp.float32, device=_DEVICE) + eager_data = wp.zeros((_NUM_ENVS, _OBS_DIM), dtype=wp.float32, device=_DEVICE) + + for _ in range(3): + replay_data.zero_() + eager_data.zero_() + replay_cfg.func(replay_data, replay_cfg) + eager_cfg.func(eager_data, eager_cfg) + torch.testing.assert_close(wp.to_torch(replay_data), wp.to_torch(eager_data)) + torch.testing.assert_close(wp.to_torch(replay_rng), wp.to_torch(eager_rng)) + + +def test_observation_manager_binds_noise_and_replays_inside_capture(): + """Manager-prepared uniform noise should replay inside CUDA graph capture.""" + replay_manager, replay_env = _make_observation_manager("replay") + eager_manager, _ = _make_observation_manager("eager") + serialized_cfg = replay_manager._group_obs_term_cfgs["policy"][0].noise + prepared_cfg = replay_manager._group_obs_noise_runtime["policy"]["sample"] + + assert prepared_cfg.rng_state_wp is replay_env.rng_state_wp + assert prepared_cfg._warp_launch is replay_env._warp_launch + assert not hasattr(serialized_cfg, "rng_state_wp") + assert not hasattr(serialized_cfg, "_warp_launch") + assert class_to_dict(serialized_cfg)["n_min"] == -0.25 + + replay_first = replay_manager.compute()["policy"].clone() + eager_first = eager_manager.compute()["policy"].clone() + torch.testing.assert_close(replay_first, eager_first) + + replay_second = replay_manager.compute()["policy"].clone() + eager_second = eager_manager.compute()["policy"].clone() + torch.testing.assert_close(replay_second, eager_second) + + prepared_term = replay_manager._group_obs_term_cfgs["policy"][0] + with wp.ScopedCapture() as capture: + prepared_term.out_wp.zero_() + prepared_cfg.func(prepared_term.out_wp, prepared_cfg) + expected = eager_manager.compute()["policy"].clone() + wp.capture_launch(capture.graph) + torch.testing.assert_close(replay_manager._group_out_torch["policy"], expected) + + +def test_observation_manager_rejects_stable_torch_noise_cfg(): + """Stable Torch noise configs should fail fast instead of silently skipping corruption.""" + with pytest.raises(TypeError, match="which the Warp observation manager would silently ignore"): + _make_observation_manager("replay", StableUniformNoiseCfg(n_min=-0.25, n_max=0.5)) + + +def test_observation_clip_replay_uses_changed_bounds(): + """Observation clipping should specialize replay when configured bounds change.""" + term_cfg = ObservationTermCfg(func=_fill_twos, params={"out_dim": _OBS_DIM}, clip=(-1.0, 1.0)) + group_cfg = ObservationGroupCfg() + group_cfg.sample = term_cfg + env = SimpleNamespace( + num_envs=_NUM_ENVS, + device=_DEVICE, + sim=SimpleNamespace(is_playing=lambda: True), + scene={}, + rng_state_wp=_make_rng_state(), + _warp_launch=WarpLaunchCache(mode="replay", debug=True, device=_DEVICE), + ) + manager = ObservationManager({"policy": group_cfg}, env) + prepared_term_cfg = manager._group_obs_term_cfgs["policy"][0] + + first = manager.compute_group("policy").clone() + torch.testing.assert_close(first, torch.ones((_NUM_ENVS, _OBS_DIM), device=_DEVICE)) + + prepared_term_cfg.clip = (-1.0, 0.25) + second = manager.compute_group("policy").clone() + torch.testing.assert_close(second, torch.full((_NUM_ENVS, _OBS_DIM), 0.25, device=_DEVICE)) diff --git a/source/isaaclab_experimental/test/utils/test_warp_graph_cache.py b/source/isaaclab_experimental/test/utils/test_warp_graph_cache.py index 1b9c10a76895..d5a64b9adf5c 100644 --- a/source/isaaclab_experimental/test/utils/test_warp_graph_cache.py +++ b/source/isaaclab_experimental/test/utils/test_warp_graph_cache.py @@ -3,14 +3,16 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Tests for WarpGraphCache capture/replay and warm-up behavior.""" +"""Tests for WarpGraphCache capture/replay and preparation behavior.""" from __future__ import annotations +import os import unittest +import unittest.mock import warp as wp -from isaaclab_experimental.utils.warp_graph_cache import WarpGraphCache +from isaaclab_experimental.utils.warp_graph_cache import CAPTURE_ENV_VAR, WarpGraphCache @wp.kernel @@ -20,23 +22,156 @@ def _add_one(a: wp.array(dtype=wp.float32), b: wp.array(dtype=wp.float32)): b[i] = a[i] + 1.0 +@wp.kernel +def _increment(value: wp.array(dtype=wp.int32)): + """Increment a scalar state buffer once.""" + value[0] += 1 + + class TestWarpGraphCache(unittest.TestCase): """Tests for :class:`WarpGraphCache`.""" def setUp(self): self.device = "cuda:0" - self.cache = WarpGraphCache() + self.cache = WarpGraphCache(device=self.device) # ------------------------------------------------------------------ - # Warm-up + # Capture policy # ------------------------------------------------------------------ - def test_warmup_runs_before_capture(self): - """The function should be called eagerly (warm-up) before graph capture. + def test_env_var_forces_eager(self): + """Setting the capture environment variable to 0 should force every stage eager.""" + with unittest.mock.patch.dict(os.environ, {CAPTURE_ENV_VAR: "0"}): + cache = WarpGraphCache(device=self.device) + call_count = 0 - We verify this by counting total invocations on the first call. - Warm-up = 1, capture = 1, so fn should be called exactly 2 times. - """ + def counted_call(): + nonlocal call_count + call_count += 1 + + for _ in range(3): + cache.call("RewardManager_compute", counted_call) + + self.assertEqual(call_count, 3) + self.assertEqual(cache.captured_stages, ()) + + def test_captured_stages_and_eager_groups_reflect_state(self): + """Introspection should report captured stages and non-capturable groups.""" + self.cache.register_capturability("Scene", False) + src = wp.full(4, value=1.0, dtype=wp.float32, device=self.device) + dst = wp.zeros(4, dtype=wp.float32, device=self.device) + + def launch(): + wp.launch(_add_one, dim=4, inputs=[src, dst], device=self.device) + + self.cache.call("RewardManager_compute", launch) + self.cache.call("RewardManager_compute", launch) + + self.assertEqual(self.cache.captured_stages, ("RewardManager_compute",)) + self.assertEqual(self.cache.eager_groups, ("Scene",)) + + def test_non_capturable_group_stays_eager(self): + """Every stage in a non-capturable group should execute eagerly.""" + call_count = 0 + + def counted_call(): + nonlocal call_count + call_count += 1 + + self.cache.register_capturability("RewardManager", False) + self.cache.call("RewardManager_compute", counted_call) + self.cache.call("RewardManager_reset", counted_call) + + self.assertEqual(call_count, 2) + + def test_cpu_device_stays_eager(self): + """CPU stages should never attempt CUDA graph capture.""" + cache = WarpGraphCache(device="cpu") + call_count = 0 + + def counted_call(): + nonlocal call_count + call_count += 1 + + for _ in range(3): + cache.call("RewardManager_compute", counted_call) + + self.assertEqual(call_count, 3) + + def test_disabled_cache_stays_eager(self): + """An owner should be able to keep an unvalidated stage boundary eager.""" + cache = WarpGraphCache(enabled=False, device=self.device) + call_count = 0 + + def counted_call(): + nonlocal call_count + call_count += 1 + + for _ in range(3): + cache.call("direct_stage", counted_call) + + self.assertEqual(call_count, 3) + + def test_capturability_registration_is_conservative(self): + """A positive registration must not override a known unsafe operation.""" + self.cache.register_capturability("EventManager", False) + self.cache.register_capturability("EventManager", True) + + self.assertFalse(self.cache.is_capturable("EventManager")) + + def test_call_forwards_arguments_and_applies_output(self): + """The frontend call should forward normal arguments and transform output.""" + self.cache.register_capturability("RewardManager", False) + + result = self.cache.call( + "RewardManager_compute", + lambda value, *, scale: value * scale, + 3, + scale=4, + output=lambda value: value + 1, + ) + + self.assertEqual(result, 13) + + def test_call_captures_by_default(self): + """A capturable stage should warm up, capture, and then replay across calls.""" + call_count = 0 + src = wp.full(4, value=2.0, dtype=wp.float32, device=self.device) + dst = wp.zeros(4, dtype=wp.float32, device=self.device) + + def counted_launch(): + nonlocal call_count + call_count += 1 + wp.launch(_add_one, dim=4, inputs=[src, dst], device=self.device) + return dst + + result = self.cache.call("RewardManager_compute", counted_launch) + captured = self.cache.call("RewardManager_compute", counted_launch) + replay = self.cache.call("RewardManager_compute", counted_launch) + + self.assertEqual(call_count, 2) + self.assertIs(result, captured) + self.assertIs(result, replay) + self.assertAlmostEqual(replay.numpy()[0], 3.0, places=5) + + def test_call_executes_stateful_stage_once_per_logical_call(self): + """Warm-up and capture should not advance manager state twice in one call.""" + state = wp.zeros(1, dtype=wp.int32, device=self.device) + + def increment_state(): + wp.launch(_increment, dim=1, inputs=[state], device=self.device) + return state + + for expected in (1, 2, 3): + result = self.cache.call("RewardManager_stateful", increment_state) + self.assertEqual(result.numpy()[0], expected) + + # ------------------------------------------------------------------ + # Preparation + # ------------------------------------------------------------------ + + def test_first_capture_invokes_callable_once(self): + """The first captured transition should invoke its Python callable once.""" call_count = [0] src = wp.zeros(4, dtype=wp.float32, device=self.device) dst = wp.zeros(4, dtype=wp.float32, device=self.device) @@ -46,37 +181,13 @@ def counted_launch(): wp.launch(_add_one, dim=4, inputs=[src, dst], device=self.device) return dst - # First call: warm-up + capture + # First call: capture once, then launch the resulting graph. self.cache.capture_or_replay("stage_a", counted_launch) - self.assertEqual(call_count[0], 2, "First call should invoke fn twice (warm-up + capture)") + self.assertEqual(call_count[0], 1, "First call should invoke fn exactly once") # Second call: replay only self.cache.capture_or_replay("stage_a", counted_launch) - self.assertEqual(call_count[0], 2, "Replay should NOT invoke fn again") - - def test_warmup_flushes_first_call_allocations(self): - """Warm-up should handle first-call allocations so capture is clean. - - Simulates a hasattr guard pattern: allocate a buffer on first call only. - Without warm-up, the allocation would be recorded in the graph. - """ - holder = {} - src = wp.ones(8, dtype=wp.float32, device=self.device) - - def fn_with_hasattr_guard(): - if "buf" not in holder: - holder["buf"] = wp.zeros(8, dtype=wp.float32, device=self.device) - wp.launch(_add_one, dim=8, inputs=[src, holder["buf"]], device=self.device) - return holder["buf"] - - # Should not raise — warm-up handles the allocation outside capture - result = self.cache.capture_or_replay("guarded", fn_with_hasattr_guard) - self.assertIsNotNone(result) - - # Verify the kernel produced correct output - result_np = result.numpy() - for val in result_np: - self.assertAlmostEqual(val, 2.0, places=5) + self.assertEqual(call_count[0], 1, "Replay should not invoke fn again") # ------------------------------------------------------------------ # Capture / replay correctness @@ -179,13 +290,13 @@ def my_fn(): return dst self.cache.capture_or_replay("s1", my_fn) - self.assertEqual(call_count[0], 2) # warm-up + capture + self.assertEqual(call_count[0], 1) self.cache.invalidate() - # After invalidation, next call should re-warm-up and re-capture + # After invalidation, the next call should capture again. self.cache.capture_or_replay("s1", my_fn) - self.assertEqual(call_count[0], 4) # 2 more (warm-up + capture) + self.assertEqual(call_count[0], 2) def test_invalidate_single_stage(self): """invalidate(stage) should only drop the named stage.""" @@ -208,16 +319,16 @@ def fn_b(): self.cache.capture_or_replay("a", fn_a) self.cache.capture_or_replay("b", fn_b) - self.assertEqual(count_a[0], 2) - self.assertEqual(count_b[0], 2) + self.assertEqual(count_a[0], 1) + self.assertEqual(count_b[0], 1) # Invalidate only "a" self.cache.invalidate("a") self.cache.capture_or_replay("a", fn_a) self.cache.capture_or_replay("b", fn_b) - self.assertEqual(count_a[0], 4, "Stage 'a' should re-capture after invalidation") - self.assertEqual(count_b[0], 2, "Stage 'b' should replay (not re-capture)") + self.assertEqual(count_a[0], 2, "Stage 'a' should re-capture after invalidation") + self.assertEqual(count_b[0], 1, "Stage 'b' should replay (not re-capture)") def test_invalidate_nonexistent_stage_is_noop(self): """Invalidating a stage that was never captured should not raise.""" diff --git a/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst b/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst new file mode 100644 index 000000000000..7ed70c412633 --- /dev/null +++ b/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst @@ -0,0 +1,5 @@ +Added +^^^^^ + +* Added boolean environment-mask resets for Newton actuator and external-wrench + state, avoiding host index conversion in Warp environments. diff --git a/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-execution-newton-replay.minor.rst b/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-execution-newton-replay.minor.rst new file mode 100644 index 000000000000..b1e5c54c53ec --- /dev/null +++ b/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-execution-newton-replay.minor.rst @@ -0,0 +1,6 @@ +Added +^^^^^ + +* Added :attr:`~isaaclab_newton.physics.NewtonCfg.use_warp_launch_cache` to + enable recorded replay for pointer-stable articulation target publication, + lazy derived-state, and actuator adapter kernels outside the solver graph. diff --git a/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-execution.rst b/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-execution.rst new file mode 100644 index 000000000000..871e3d5151fd --- /dev/null +++ b/source/isaaclab_newton/changelog.d/jichuanh-warp-frontend-execution.rst @@ -0,0 +1,4 @@ +Fixed +^^^^^ + +* Fixed Newton articulation resets to avoid unnecessary host synchronization for stateless actuator models. diff --git a/source/isaaclab_newton/changelog.d/warp-scene-mask-native.minor.rst b/source/isaaclab_newton/changelog.d/warp-scene-mask-native.minor.rst new file mode 100644 index 000000000000..cad29d240904 --- /dev/null +++ b/source/isaaclab_newton/changelog.d/warp-scene-mask-native.minor.rst @@ -0,0 +1,15 @@ +Added +^^^^^ + +* Added masked body-write kernels so the rigid-object-collection + ``write_*_to_sim_mask`` methods execute mask-natively and become eligible for + CUDA-graph capture. +* Added ``reset_capture_safe`` declarations to Newton assets for the scene-level + capture-eligibility query. + +Changed +^^^^^^^ + +* Changed masked articulation resets to forward the boolean mask to Lab + actuators through :meth:`~isaaclab.actuators.ActuatorBase.reset_mask` instead + of materializing compact environment IDs in the asset. diff --git a/source/isaaclab_newton/isaaclab_newton/actuators/adapter.py b/source/isaaclab_newton/isaaclab_newton/actuators/adapter.py index 689232b81e60..ea9e04401c22 100644 --- a/source/isaaclab_newton/isaaclab_newton/actuators/adapter.py +++ b/source/isaaclab_newton/isaaclab_newton/actuators/adapter.py @@ -26,11 +26,13 @@ import warp as wp from newton.actuators import Actuator, Clamping, Delay +from isaaclab.utils.warp import WarpLaunchCache +from isaaclab.utils.warp.utils import resolve_1d_mask + from .kernels import ( build_implicit_dof_mask, build_per_dof_env_mask_kernel, scatter_gain_kernel, - set_mask_kernel, zero_at_indices_kernel, ) @@ -85,6 +87,8 @@ def __init__( num_joints: int, dof_offset: int, device: str, + *, + warp_launch: WarpLaunchCache | None = None, ): self.actuators = actuators self.num_joints = num_joints @@ -92,6 +96,7 @@ def __init__( self._num_envs = num_envs self._dof_offset = dof_offset self._device = device + self._warp_launch = warp_launch if warp_launch is not None else WarpLaunchCache(mode="eager", device=device) # Collect the set of local DOFs covered by some actuator. Only the # env-0 slice of each actuator's flat ``indices`` array is needed — @@ -112,6 +117,8 @@ def __init__( self._states_a = [act.state() for act in actuators] self._states_b = [act.state() for act in actuators] + self._reset_env_mask = wp.zeros(num_envs, dtype=wp.bool, device=device) + self._reset_dof_masks = [wp.zeros(act.indices.shape[0], dtype=wp.bool, device=device) for act in actuators] # Pre-clamp computed effort buffer. Each Newton actuator scatter-adds # its raw controller output to ``sim_control.joint_computed_f`` when @@ -157,23 +164,30 @@ def step(self, sim_state: Any, sim_control: Any, dt: float) -> None: # Zero before scatter-add (actuators accumulate into this buffer). self._computed_effort.zero_() for act in self.actuators: - wp.launch( + self._warp_launch.launch( zero_at_indices_kernel, dim=act.indices.shape[0], inputs=[sim_control.joint_f, act.indices], + site=act, ) for act, sa, sb in zip(self.actuators, self._states_a, self._states_b): act.step(sim_state, sim_control, sa, sb, dt=dt) self._states_a, self._states_b = self._states_b, self._states_a - def reset(self, env_ids: Sequence[int] | torch.Tensor | None = None) -> None: + def reset( + self, + env_ids: Sequence[int] | slice | torch.Tensor | wp.array | None = None, + env_mask: wp.array(dtype=wp.bool) | None = None, + ) -> None: """Reset actuator states for the given environments. Args: - env_ids: Environment indices to reset. ``None`` (or - ``slice(None)``, which IsaacLab callers sometimes pass) - resets all environments. Otherwise expects a torch tensor - or sequence of int indices. + env_ids: Environment indices to reset. Accepts a sequence, slice, + Torch tensor, or ``wp.int32`` array. ``None`` and + ``slice(None)`` reset all environments. + env_mask: Boolean Warp mask selecting environments to reset. When + provided, it takes precedence over :paramref:`env_ids`. Shape + is ``(num_envs,)`` on the adapter device. Newton's :meth:`Actuator.State.reset` expects a per-DOF boolean mask of length ``num_actuators`` (= ``num_envs * dofs_per_actuator``), @@ -182,7 +196,7 @@ def reset(self, env_ids: Sequence[int] | torch.Tensor | None = None) -> None: etc.). We therefore build a per-actuator per-DOF mask from the env mask before delegating to each state. """ - if env_ids is None or env_ids == slice(None): + if env_mask is None and (env_ids is None or (isinstance(env_ids, slice) and env_ids == slice(None))): for sa, sb in zip(self._states_a, self._states_b): if sa is not None: sa.reset(None) @@ -190,19 +204,22 @@ def reset(self, env_ids: Sequence[int] | torch.Tensor | None = None) -> None: sb.reset(None) return - if isinstance(env_ids, torch.Tensor): - if env_ids.numel() == 0: - return - idx = wp.from_torch(env_ids.to(device=self._device).contiguous().to(torch.int32), dtype=wp.int32) - else: - if len(env_ids) == 0: - return - idx = wp.array(list(env_ids), dtype=wp.int32, device=self._device) - env_mask = wp.zeros(self._num_envs, dtype=wp.bool, device=self._device) - wp.launch(set_mask_kernel, dim=idx.shape[0], inputs=[env_mask, idx], device=self._device) - - for act, sa, sb in zip(self.actuators, self._states_a, self._states_b): - per_dof_mask = wp.zeros(act.indices.shape[0], dtype=wp.bool, device=self._device) + if env_mask is None: + env_mask = resolve_1d_mask( + ids=env_ids, + all_mask=self._reset_env_mask, + scratch_mask=self._reset_env_mask, + device=self._device, + ) + if ( + env_mask.dtype != wp.bool + or env_mask.ndim != 1 + or env_mask.shape[0] != self._num_envs + or env_mask.device != wp.get_device(self._device) + ): + raise ValueError(f"env_mask must have shape ({self._num_envs},) and dtype wp.bool, received {env_mask}.") + + for act, sa, sb, per_dof_mask in zip(self.actuators, self._states_a, self._states_b, self._reset_dof_masks): wp.launch( build_per_dof_env_mask_kernel, dim=act.indices.shape[0], @@ -325,6 +342,15 @@ def from_usd( ) return cls(actuators, num_envs, num_joints, dof_offset=0, device=device) + def _invalidate_launch_cache(self) -> None: + """Release recorded launches after the owning Newton graph is destroyed. + + Callers clear the Newton graph reference before invoking this method. Device + synchronization drains any recorded command work before retained argument + owners are released. Disabled caches return without synchronizing. + """ + self._warp_launch.reset() + # --------------------------------------------------------------------------- # Per-articulation initial-gain snapshot — consumed by diff --git a/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation.py b/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation.py index e0c26072c1da..39792fad9183 100644 --- a/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation.py +++ b/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation.py @@ -36,6 +36,7 @@ from isaaclab.utils.string import resolve_matching_names, resolve_matching_names_values from isaaclab.utils.types import ArticulationActions from isaaclab.utils.version import get_isaac_sim_version, has_kit +from isaaclab.utils.warp import WarpLaunchCache from isaaclab.utils.wrench_composer import WrenchComposer from isaaclab_newton.assets import kernels as shared_kernels @@ -133,6 +134,11 @@ def __init__(self, cfg: ArticulationCfg): sim_ctx = SimulationContext.instance() self._sim_cfg = sim_ctx.cfg if sim_ctx is not None else None + physics_cfg = self._sim_cfg.physics if self._sim_cfg is not None else None + self._warp_launch = WarpLaunchCache( + mode=None if getattr(physics_cfg, "use_warp_launch_cache", False) else "eager", + device=sim_ctx.device if sim_ctx is not None else None, + ) """ Properties @@ -257,6 +263,19 @@ def permanent_wrench_composer(self) -> WrenchComposer: Operations. """ + @property + def reset_capture_safe(self) -> bool: + """Whether :meth:`reset` consumes masks without host-side work. + + Newton-native actuator state resets by mask in the global adapter. In legacy + mode, every Lab actuator must provide a mask-native + :meth:`~isaaclab.actuators.ActuatorBase.reset_mask` override; the compacting + base fallback keeps the reset host-bound. + """ + if getattr(self, "_has_newton_actuators", False): + return True + return all(type(actuator).reset_mask is not ActuatorBase.reset_mask for actuator in self.actuators.values()) + def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None = None) -> None: """Reset the articulation. @@ -270,9 +289,22 @@ def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None # use ellipses object to skip initial indices. if (env_ids is None) or (env_ids == slice(None)): env_ids = slice(None) - # reset Lab actuators registered on this articulation - for actuator in self.actuators.values(): - actuator.reset(env_ids) + # Reset Lab actuators only outside the Newton-native mode. In native mode + # the per-env actuator state (delay queues, network hidden states) lives + # in the global adapter, which consumes the mask below, and the Lab + # actuator objects only carry configuration; resetting them here would + # touch unselected environments. + if not getattr(self, "_has_newton_actuators", False) and self.actuators: + if env_mask is not None: + # Forward the boolean view; mask-native models reset without host + # synchronization and legacy models compact inside their own + # compatibility fallback. + torch_mask = wp.to_torch(env_mask) + for actuator in self.actuators.values(): + actuator.reset_mask(torch_mask) + else: + for actuator in self.actuators.values(): + actuator.reset(env_ids) # reset the global Newton actuator adapter (its ``_states_a/_b`` buffers # carry per-env state — delay queues, neural hidden states — that must # be cleared for the resetting envs). The adapter spans the whole model, @@ -281,7 +313,7 @@ def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None # ``getattr`` guards subclasses (e.g. ``Multirotor``) that override # ``_process_actuators_cfg`` and never initialize ``_has_newton_actuators``. if getattr(self, "_has_newton_actuators", False) and SimulationManager._adapter is not None: - SimulationManager._adapter.reset(env_ids) + SimulationManager._adapter.reset(env_ids, env_mask=env_mask) # reset external wrenches. self._instantaneous_wrench_composer.reset(env_ids, env_mask) self._permanent_wrench_composer.reset(env_ids, env_mask) @@ -340,7 +372,7 @@ def write_data_to_sim(self): # joint_effort. Under identity ordering ``_joint_backend_to_user_map`` # returns the identity arange, so this single gather also serves the # no-reorder case without a dedicated per-buffer copy. - wp.launch( + self._warp_launch.launch( ordering_kernels.reorder_joint_targets_user_to_backend, dim=(self.num_instances, self.num_joints), inputs=[ @@ -359,7 +391,7 @@ def write_data_to_sim(self): self.data._sim_bind_joint_velocity_target, self.data._sim_bind_joint_act, ], - device=self.device, + site="newton_joint_targets", ) else: # Standard Lab actuator path @@ -369,7 +401,7 @@ def write_data_to_sim(self): # write_joint_act is off, so it is never indexed. Under identity # ordering ``_joint_backend_to_user_map`` returns the identity arange, # so this single gather also serves the no-reorder case. - wp.launch( + self._warp_launch.launch( ordering_kernels.reorder_joint_targets_user_to_backend, dim=(self.num_instances, self.num_joints), inputs=[ @@ -388,7 +420,7 @@ def write_data_to_sim(self): self.data._sim_bind_joint_velocity_target, self.data._sim_bind_joint_effort, ], - device=self.device, + site="lab_joint_targets", ) def update(self, dt: float): @@ -3392,7 +3424,7 @@ def has_articulation_root_api(prim) -> bool: SimulationManager.get_physics_sim_view().append(self._root_view) # container for data access - self._data = ArticulationData(self.root_view, self.device) + self._data = ArticulationData(self.root_view, self.device, warp_launch=self._warp_launch) # Register callback to rebind simulation data after a full reset (model/state recreation). self._physics_ready_handle = SimulationManager.register_callback( @@ -3538,6 +3570,7 @@ def _process_cfg(self): def _invalidate_initialize_callback(self, event): """Invalidates the scene elements.""" + self._warp_launch.invalidate() # call parent super()._invalidate_initialize_callback(event) self._root_view = None @@ -3892,33 +3925,51 @@ def _apply_actuator_model(self): joint_vel=self._data.joint_vel.torch[:, actuator.joint_indices], ) # update targets (these are set into the simulation) - joint_indices = actuator.joint_indices - if actuator.joint_indices == slice(None) or actuator.joint_indices is None: + actuator_joint_indices = actuator.joint_indices + is_full_joint_group = actuator_joint_indices is None or ( + isinstance(actuator_joint_indices, slice) and actuator_joint_indices == slice(None) + ) + joint_indices = actuator_joint_indices + if is_full_joint_group: joint_indices = self._ALL_JOINT_INDICES if hasattr(actuator, "gear_ratio"): gear_ratio = actuator.gear_ratio else: gear_ratio = None - wp.launch( - articulation_kernels.update_targets, - dim=(self.num_instances, joint_indices.shape[0]), - inputs=[ - control_action.joint_positions, - control_action.joint_velocities, - control_action.joint_efforts, - joint_indices, - ], - outputs=[ - self._joint_pos_target_sim, - self._joint_vel_target_sim, - self._joint_effort_target_sim, - ], - device=self.device, - ) + launch_dim = (self.num_instances, joint_indices.shape[0]) + target_inputs = [ + control_action.joint_positions, + control_action.joint_velocities, + control_action.joint_efforts, + joint_indices, + ] + target_outputs = [ + self._joint_pos_target_sim, + self._joint_vel_target_sim, + self._joint_effort_target_sim, + ] + if getattr(actuator, "is_implicit_model", False) and is_full_joint_group: + self._warp_launch.launch( + articulation_kernels.update_targets, + dim=launch_dim, + inputs=target_inputs, + outputs=target_outputs, + site=actuator, + ) + else: + # Explicit models may replace target tensors, while partial-group + # indexing allocates fresh Torch tensors on every call. + wp.launch( + articulation_kernels.update_targets, + dim=launch_dim, + inputs=target_inputs, + outputs=target_outputs, + device=self.device, + ) # update state of the actuator model wp.launch( articulation_kernels.update_actuator_state_model, - dim=(self.num_instances, joint_indices.shape[0]), + dim=launch_dim, inputs=[ actuator.computed_effort, actuator.applied_effort, diff --git a/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation_data.py b/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation_data.py index 4c27091dd3cb..118f44366624 100644 --- a/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation_data.py +++ b/source/isaaclab_newton/isaaclab_newton/assets/articulation/articulation_data.py @@ -16,7 +16,7 @@ from isaaclab.assets.articulation.base_articulation_data import BaseArticulationData from isaaclab.utils.buffers import TimestampedBufferWarp as TimestampedBuffer from isaaclab.utils.buffers import reset_timestamps -from isaaclab.utils.warp import ProxyArray +from isaaclab.utils.warp import ProxyArray, WarpLaunchCache from isaaclab.utils.warp.utils import capture_unsafe from isaaclab_newton.assets import kernels as shared_kernels @@ -58,18 +58,26 @@ class ArticulationData(BaseArticulationData): __backend_name__: str = "newton" """The name of the backend for the articulation data.""" - def __init__(self, root_view: ArticulationView, device: str): + def __init__( + self, + root_view: ArticulationView, + device: str, + *, + warp_launch: WarpLaunchCache | None = None, + ): """Initializes the articulation data. Args: root_view: The root articulation view. device: The device used for processing. + warp_launch: Optional owner launcher for recorded Warp launches. """ super().__init__(root_view, device) # Set the root articulation view # note: this is stored as a weak reference to avoid circular references between the asset class # and the data container. This is important to avoid memory leaks. self._root_view: ArticulationView = weakref.proxy(root_view) + self._warp_launch = warp_launch if warp_launch is not None else WarpLaunchCache(mode="eager", device=device) # Set initial time stamp self._sim_timestamp = 0.0 @@ -678,18 +686,17 @@ def root_link_vel_w(self) -> ProxyArray: relative to the world. """ if self._root_link_vel_w.timestamp < self._sim_timestamp: - wp.launch( + inputs = [ + self.root_com_vel_w.warp, + self.root_link_pose_w.warp, + self._sim_bind_body_com_pos_b, + ] + outputs = [self._root_link_vel_w.data] + self._warp_launch.launch( shared_kernels.get_root_link_vel_from_root_com_vel, dim=self._num_instances, - inputs=[ - self.root_com_vel_w.warp, - self.root_link_pose_w.warp, - self._sim_bind_body_com_pos_b, - ], - outputs=[ - self._root_link_vel_w.data, - ], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._root_link_vel_w.timestamp = self._sim_timestamp @@ -707,17 +714,13 @@ def root_com_pose_w(self) -> ProxyArray: """ if self._root_com_pose_w.timestamp < self._sim_timestamp: # apply local transform to center of mass frame - wp.launch( + inputs = [self.root_link_pose_w.warp, self._sim_bind_body_com_pos_b] + outputs = [self._root_com_pose_w.data] + self._warp_launch.launch( shared_kernels.get_root_com_pose_from_root_link_pose, dim=self._num_instances, - inputs=[ - self.root_link_pose_w.warp, - self._sim_bind_body_com_pos_b, - ], - outputs=[ - self._root_com_pose_w.data, - ], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._root_com_pose_w.timestamp = self._sim_timestamp @@ -783,18 +786,17 @@ def body_link_vel_w(self) -> ProxyArray: relative to the world. """ if self._body_link_vel_w.timestamp < self._sim_timestamp: - wp.launch( + inputs = [ + self.body_com_vel_w.warp, + self.body_link_pose_w.warp, + self.body_com_pos_b.warp, + ] + outputs = [self._body_link_vel_w.data] + self._warp_launch.launch( shared_kernels.get_body_link_vel_from_body_com_vel, dim=(self._num_instances, self._num_bodies), - inputs=[ - self.body_com_vel_w.warp, - self.body_link_pose_w.warp, - self.body_com_pos_b.warp, - ], - outputs=[ - self._body_link_vel_w.data, - ], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._body_link_vel_w.timestamp = self._sim_timestamp @@ -813,17 +815,13 @@ def body_com_pose_w(self) -> ProxyArray: """ self._ensure_fk_fresh() if self._body_com_pose_w.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.body_link_pose_w.warp, self.body_com_pos_b.warp] + outputs = [self._body_com_pose_w.data] + self._warp_launch.launch( shared_kernels.get_body_com_pose_from_body_link_pose, dim=(self._num_instances, self._num_bodies), - inputs=[ - self.body_link_pose_w.warp, - self.body_com_pos_b.warp, - ], - outputs=[ - self._body_com_pose_w.data, - ], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._body_com_pose_w.timestamp = self._sim_timestamp @@ -1104,12 +1102,13 @@ def projected_gravity_b(self) -> ProxyArray: Shape is (num_instances), dtype = wp.vec3f. In torch this resolves to (num_instances, 3). """ if self._projected_gravity_b.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.GRAVITY_VEC_W.warp, self.root_link_quat_w.warp] + outputs = [self._projected_gravity_b.data] + self._warp_launch.launch( shared_kernels.projected_gravity_b_kernel, dim=self._num_instances, - inputs=[self.GRAVITY_VEC_W.warp, self.root_link_quat_w.warp], - outputs=[self._projected_gravity_b.data], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._projected_gravity_b.timestamp = self._sim_timestamp return self._projected_gravity_b_ta @@ -1126,12 +1125,13 @@ def heading_w(self) -> ProxyArray: frame is along x-direction, i.e. :math:`(1, 0, 0)`. """ if self._heading_w.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.FORWARD_VEC_B.warp, self.root_link_quat_w.warp] + outputs = [self._heading_w.data] + self._warp_launch.launch( shared_kernels.root_heading_w, dim=self._num_instances, - inputs=[self.FORWARD_VEC_B.warp, self.root_link_quat_w.warp], - outputs=[self._heading_w.data], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._heading_w.timestamp = self._sim_timestamp return self._heading_w_ta @@ -1152,12 +1152,13 @@ def root_link_lin_vel_b(self) -> ProxyArray: ) self._root_link_lin_vel_b_ta = ProxyArray(self._root_link_lin_vel_b.data) if self._root_link_lin_vel_b.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.root_link_lin_vel_w.warp, self.root_link_quat_w.warp] + outputs = [self._root_link_lin_vel_b.data] + self._warp_launch.launch( shared_kernels.quat_apply_inverse_1D_kernel, dim=self._num_instances, - inputs=[self.root_link_lin_vel_w.warp, self.root_link_quat_w.warp], - outputs=[self._root_link_lin_vel_b.data], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._root_link_lin_vel_b.timestamp = self._sim_timestamp return self._root_link_lin_vel_b_ta @@ -1524,6 +1525,11 @@ def _create_simulation_bindings(self) -> None: .. caution:: This is possible if and only if the properties that we access are strided from newton and not indexed. Newton willing this is the case all the time, but we should pay attention to this if things look off. """ + # A full simulation reset replaces state/model pointers after the Newton + # graph has been destroyed. Drain recorded work, then release commands + # and owners recorded against the previous storage. + self._warp_launch.reset() + # Short-hand for the number of instances, number of links, and number of joints. self._num_instances = self._root_view.count self._num_joints = self._root_view.joint_dof_count diff --git a/source/isaaclab_newton/isaaclab_newton/assets/kernels.py b/source/isaaclab_newton/isaaclab_newton/assets/kernels.py index b1b742292bce..62ae6447a63c 100644 --- a/source/isaaclab_newton/isaaclab_newton/assets/kernels.py +++ b/source/isaaclab_newton/isaaclab_newton/assets/kernels.py @@ -906,6 +906,138 @@ def set_body_link_velocity_to_sim( body_acc_w[env_ids[i], body_ids[j]] = wp.spatial_vectorf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) +@wp.kernel +def set_body_link_pose_to_sim_mask( + data: wp.array2d(dtype=wp.transformf), + env_mask: wp.array(dtype=wp.bool), + body_mask: wp.array(dtype=wp.bool), + body_link_pose_w: wp.array2d(dtype=wp.transformf), +): + """Write masked body link pose data to simulation buffers. + + Launched over the full ``(num_envs, num_bodies)`` grid with an in-kernel mask + test, so the write is CUDA-graph capturable. + + Args: + data: Input array of body link poses. Shape is (num_envs, num_bodies). + env_mask: Boolean environment selection. Shape is (num_envs,). + body_mask: Boolean body selection. Shape is (num_bodies,). + body_link_pose_w: Output array where body link poses are written. + Shape is (num_envs, num_bodies). + """ + i, j = wp.tid() + if env_mask[i] and body_mask[j]: + body_link_pose_w[i, j] = data[i, j] + + +@wp.kernel +def set_body_com_pose_to_sim_mask( + data: wp.array2d(dtype=wp.transformf), + body_com_pos_b: wp.array2d(dtype=wp.vec3f), + env_mask: wp.array(dtype=wp.bool), + body_mask: wp.array(dtype=wp.bool), + body_com_pose_w: wp.array2d(dtype=wp.transformf), + body_link_pose_w: wp.array2d(dtype=wp.transformf), +): + """Write masked body COM pose data to simulation buffers. + + Launched over the full ``(num_envs, num_bodies)`` grid with an in-kernel mask + test, so the write is CUDA-graph capturable. Link poses are derived from the + written COM poses for the selected cells. + + Args: + data: Input array of body COM poses. Shape is (num_envs, num_bodies). + body_com_pos_b: Input array of body COM positions in body frame. + Shape is (num_envs, num_bodies). + env_mask: Boolean environment selection. Shape is (num_envs,). + body_mask: Boolean body selection. Shape is (num_bodies,). + body_com_pose_w: Output array where body COM poses are written. + Shape is (num_envs, num_bodies). + body_link_pose_w: Output array where body link poses (derived from COM) + are written. Shape is (num_envs, num_bodies). + """ + i, j = wp.tid() + if env_mask[i] and body_mask[j]: + body_com_pose_w[i, j] = data[i, j] + # Get the link pose from com pose + body_link_pose_w[i, j] = get_com_pose_in_link_frame_func(body_com_pose_w[i, j], body_com_pos_b[i, j]) + + +@wp.kernel +def set_body_com_velocity_to_sim_mask( + data: wp.array2d(dtype=wp.spatial_vectorf), + env_mask: wp.array(dtype=wp.bool), + body_mask: wp.array(dtype=wp.bool), + body_com_velocity_w: wp.array2d(dtype=wp.spatial_vectorf), + body_acc_w: wp.array2d(dtype=wp.spatial_vectorf), +): + """Write masked body COM velocity data to simulation buffers. + + Launched over the full ``(num_envs, num_bodies)`` grid with an in-kernel mask + test, so the write is CUDA-graph capturable. Accelerations of the selected + cells are zeroed to prevent reporting old values. + + Args: + data: Input array of body COM spatial velocities. Shape is (num_envs, num_bodies). + env_mask: Boolean environment selection. Shape is (num_envs,). + body_mask: Boolean body selection. Shape is (num_bodies,). + body_com_velocity_w: Output array where body COM velocities are written. + Shape is (num_envs, num_bodies). + body_acc_w: Output array where body accelerations are zeroed. + Shape is (num_envs, num_bodies). + """ + i, j = wp.tid() + if env_mask[i] and body_mask[j]: + body_com_velocity_w[i, j] = data[i, j] + # Make the acceleration zero to prevent reporting old values + body_acc_w[i, j] = wp.spatial_vectorf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + + +@wp.kernel +def set_body_link_velocity_to_sim_mask( + data: wp.array2d(dtype=wp.spatial_vectorf), + body_com_pos_b: wp.array2d(dtype=wp.vec3f), + body_link_pose_w: wp.array2d(dtype=wp.transformf), + env_mask: wp.array(dtype=wp.bool), + body_mask: wp.array(dtype=wp.bool), + body_link_velocity_w: wp.array2d(dtype=wp.spatial_vectorf), + body_com_velocity_w: wp.array2d(dtype=wp.spatial_vectorf), + body_acc_w: wp.array2d(dtype=wp.spatial_vectorf), +): + """Write masked body link velocity data to simulation buffers. + + Launched over the full ``(num_envs, num_bodies)`` grid with an in-kernel mask + test, so the write is CUDA-graph capturable. COM velocities are derived from + the written link velocities and accelerations are zeroed for the selected cells. + + Args: + data: Input array of body link spatial velocities. Shape is (num_envs, num_bodies). + body_com_pos_b: Input array of body COM positions in body frame. + Shape is (num_envs, num_bodies). + body_link_pose_w: Input array of body link poses in world frame. + Shape is (num_envs, num_bodies). + env_mask: Boolean environment selection. Shape is (num_envs,). + body_mask: Boolean body selection. Shape is (num_bodies,). + body_link_velocity_w: Output array where body link velocities are written. + Shape is (num_envs, num_bodies). + body_com_velocity_w: Output array where body COM velocities (derived from link) + are written. Shape is (num_envs, num_bodies). + body_acc_w: Output array where body accelerations are zeroed. + Shape is (num_envs, num_bodies). + """ + i, j = wp.tid() + if env_mask[i] and body_mask[j]: + body_link_velocity_w[i, j] = data[i, j] + # Get the link velocity in the com frame + body_com_velocity_w[i, j] = get_link_velocity_in_com_frame_func( + body_link_velocity_w[i, j], + body_link_pose_w[i, j], + body_com_pos_b[i, j], + ) + # Make the acceleration zero to prevent reporting old values + body_acc_w[i, j] = wp.spatial_vectorf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + + """ Generic buffer-writing kernels (used by Articulation + RigidObject + RigidObjectCollection). """ @@ -1262,6 +1394,12 @@ def update_wrench_array_with_force_and_torque( env_ids: wp.array(dtype=wp.bool), body_ids: wp.array(dtype=wp.bool), ): + """Compose the external wrench [N, N·m] from the force/torque composer buffers + + for bodies selected by the env x body boolean masks. + Launch with dim=(num_envs, num_bodies); unselected entries keep their + current wrench. + """ env_index, body_index = wp.tid() if env_ids[env_index] and body_ids[body_index]: wrench[env_index, body_index] = update_wrench_with_force_and_torque( diff --git a/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object.py b/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object.py index 3265ad2896dd..d2d0c3734dd6 100644 --- a/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object.py +++ b/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object.py @@ -23,6 +23,7 @@ from isaaclab.physics import PhysicsEvent from isaaclab.sim.utils.queries import resolve_matching_prims_from_source from isaaclab.utils.version import has_kit +from isaaclab.utils.warp import WarpLaunchCache from isaaclab.utils.wrench_composer import WrenchComposer from isaaclab_newton.assets import kernels as shared_kernels @@ -61,11 +62,20 @@ def __init__(self, cfg: RigidObjectCfg): Args: cfg: A configuration instance. """ + from isaaclab.sim import SimulationContext # noqa: PLC0415 + super().__init__(cfg) if has_kit(): queue_usd_replication(cfg) queue_newton_physics_replication(cfg) + sim_ctx = SimulationContext.instance() + physics_cfg = sim_ctx.cfg.physics if sim_ctx is not None else None + self._warp_launch = WarpLaunchCache( + mode=None if getattr(physics_cfg, "use_warp_launch_cache", False) else "eager", + device=sim_ctx.device if sim_ctx is not None else None, + ) + """ Properties """ @@ -125,6 +135,9 @@ def permanent_wrench_composer(self) -> WrenchComposer: Operations. """ + reset_capture_safe: bool = True + """Whether :meth:`reset` runs only mask-native kernel work (wrench-composer resets).""" + def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None = None) -> None: """Reset the rigid object. @@ -136,8 +149,8 @@ def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None if (env_ids is None) or (env_ids == slice(None)): env_ids = slice(None) # reset external wrench - self._instantaneous_wrench_composer.reset(env_ids) - self._permanent_wrench_composer.reset(env_ids) + self._instantaneous_wrench_composer.reset(env_ids, env_mask) + self._permanent_wrench_composer.reset(env_ids, env_mask) def write_data_to_sim(self) -> None: """Write external wrench to the simulation. @@ -1031,7 +1044,7 @@ def has_rigid_body_api(prim) -> bool: ) # container for data access - self._data = RigidObjectData(self.root_view, self.device) + self._data = RigidObjectData(self.root_view, self.device, warp_launch=self._warp_launch) # Register callback to rebind simulation data after a full reset (model/state recreation). self._physics_ready_handle = SimulationManager.register_callback( @@ -1127,6 +1140,7 @@ def _resolve_body_ids(self, body_ids: Sequence[int] | torch.Tensor | wp.array | def _invalidate_initialize_callback(self, event): """Invalidates the scene elements.""" + self._warp_launch.invalidate() # call parent super()._invalidate_initialize_callback(event) # set all existing views to None to invalidate them diff --git a/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object_data.py b/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object_data.py index c568427bac4c..865db2845f31 100644 --- a/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object_data.py +++ b/source/isaaclab_newton/isaaclab_newton/assets/rigid_object/rigid_object_data.py @@ -15,7 +15,7 @@ from isaaclab.assets.rigid_object.base_rigid_object_data import BaseRigidObjectData from isaaclab.utils.buffers import TimestampedBufferWarp as TimestampedBuffer from isaaclab.utils.buffers import reset_timestamps -from isaaclab.utils.warp import ProxyArray +from isaaclab.utils.warp import ProxyArray, WarpLaunchCache from isaaclab.utils.warp.utils import capture_unsafe from isaaclab_newton.assets import kernels as shared_kernels @@ -61,18 +61,26 @@ class RigidObjectData(BaseRigidObjectData): __backend_name__: str = "newton" """The name of the backend for the rigid object data.""" - def __init__(self, root_view: ArticulationView, device: str): + def __init__( + self, + root_view: ArticulationView, + device: str, + *, + warp_launch: WarpLaunchCache | None = None, + ): """Initializes the rigid object data. Args: root_view: The root rigid body view. device: The device used for processing. + warp_launch: Optional owner launcher for recorded Warp launches. """ super().__init__(root_view, device) # Set the root rigid body view # note: this is stored as a weak reference to avoid circular references between the asset class # and the data container. This is important to avoid memory leaks. self._root_view: ArticulationView = weakref.proxy(root_view) + self._warp_launch = warp_launch if warp_launch is not None else WarpLaunchCache(mode="eager", device=device) # Set initial time stamp self._sim_timestamp = 0.0 @@ -280,18 +288,17 @@ def root_link_vel_w(self) -> ProxyArray: """ if self._root_link_vel_w.timestamp < self._sim_timestamp: # read the CoM velocity and compute link velocity - wp.launch( + inputs = [ + self.root_com_vel_w.warp, + self.root_link_pose_w.warp, + self.body_com_pos_b.warp, + ] + outputs = [self._root_link_vel_w.data] + self._warp_launch.launch( shared_kernels.get_root_link_vel_from_root_com_vel, dim=self._num_instances, - inputs=[ - self.root_com_vel_w.warp, - self.root_link_pose_w.warp, - self.body_com_pos_b.warp, - ], - outputs=[ - self._root_link_vel_w.data, - ], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._root_link_vel_w.timestamp = self._sim_timestamp @@ -308,17 +315,13 @@ def root_com_pose_w(self) -> ProxyArray: """ if self._root_com_pose_w.timestamp < self._sim_timestamp: # apply local transform to center of mass frame - wp.launch( + inputs = [self.root_link_pose_w.warp, self.body_com_pos_b.warp] + outputs = [self._root_com_pose_w.data] + self._warp_launch.launch( shared_kernels.get_root_com_pose_from_root_link_pose, dim=self._num_instances, - inputs=[ - self.root_link_pose_w.warp, - self.body_com_pos_b.warp, - ], - outputs=[ - self._root_com_pose_w.data, - ], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._root_com_pose_w.timestamp = self._sim_timestamp @@ -486,12 +489,13 @@ def projected_gravity_b(self) -> ProxyArray: Shape is (num_instances,), dtype = wp.vec3f. In torch this resolves to (num_instances, 3). """ if self._projected_gravity_b.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.GRAVITY_VEC_W.warp, self.root_link_quat_w.warp] + outputs = [self._projected_gravity_b.data] + self._warp_launch.launch( shared_kernels.projected_gravity_b_kernel, dim=self._num_instances, - inputs=[self.GRAVITY_VEC_W.warp, self.root_link_quat_w.warp], - outputs=[self._projected_gravity_b.data], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._projected_gravity_b.timestamp = self._sim_timestamp return self._projected_gravity_b_ta @@ -508,12 +512,13 @@ def heading_w(self) -> ProxyArray: frame is along x-direction, i.e. :math:`(1, 0, 0)`. """ if self._heading_w.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.FORWARD_VEC_B.warp, self.root_link_quat_w.warp] + outputs = [self._heading_w.data] + self._warp_launch.launch( shared_kernels.root_heading_w, dim=self._num_instances, - inputs=[self.FORWARD_VEC_B.warp, self.root_link_quat_w.warp], - outputs=[self._heading_w.data], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._heading_w.timestamp = self._sim_timestamp return self._heading_w_ta @@ -533,12 +538,13 @@ def root_link_lin_vel_b(self) -> ProxyArray: ) self._root_link_lin_vel_b_ta = ProxyArray(self._root_link_lin_vel_b.data) if self._root_link_lin_vel_b.timestamp < self._sim_timestamp: - wp.launch( + inputs = [self.root_link_lin_vel_w.warp, self.root_link_quat_w.warp] + outputs = [self._root_link_lin_vel_b.data] + self._warp_launch.launch( shared_kernels.quat_apply_inverse_1D_kernel, dim=self._num_instances, - inputs=[self.root_link_lin_vel_w.warp, self.root_link_quat_w.warp], - outputs=[self._root_link_lin_vel_b.data], - device=self.device, + inputs=inputs, + outputs=outputs, ) self._root_link_lin_vel_b.timestamp = self._sim_timestamp return self._root_link_lin_vel_b_ta @@ -872,6 +878,11 @@ def _create_simulation_bindings(self) -> None: .. caution:: This is possible if and only if the properties that we access are strided from newton and not indexed. Newton willing this is the case all the time, but we should pay attention to this if things look off. """ + # A full simulation reset replaces state/model pointers after the Newton + # graph has been destroyed. Drain recorded work, then release commands + # and owners recorded against the previous storage. + self._warp_launch.reset() + # Short-hand for the number of instances, number of links, and number of joints. self._num_instances = self._root_view.count self._num_bodies = self._root_view.link_count diff --git a/source/isaaclab_newton/isaaclab_newton/assets/rigid_object_collection/rigid_object_collection.py b/source/isaaclab_newton/isaaclab_newton/assets/rigid_object_collection/rigid_object_collection.py index 57512ce1b33e..a6d11d369e9f 100644 --- a/source/isaaclab_newton/isaaclab_newton/assets/rigid_object_collection/rigid_object_collection.py +++ b/source/isaaclab_newton/isaaclab_newton/assets/rigid_object_collection/rigid_object_collection.py @@ -23,6 +23,7 @@ from isaaclab.cloner import queue_usd_replication from isaaclab.physics import PhysicsEvent from isaaclab.utils.version import has_kit +from isaaclab.utils.warp.utils import resolve_1d_mask from isaaclab.utils.wrench_composer import WrenchComposer from isaaclab_newton.assets import kernels as shared_kernels @@ -161,6 +162,9 @@ def permanent_wrench_composer(self) -> WrenchComposer: Operations. """ + reset_capture_safe: bool = True + """Whether :meth:`reset` runs only mask-native kernel work (wrench-composer resets).""" + def reset( self, env_ids: Sequence[int] | torch.Tensor | wp.array | None = None, @@ -182,8 +186,8 @@ def reset( if object_ids is None: object_ids = self._ALL_BODY_INDICES # reset external wrench - self._instantaneous_wrench_composer.reset(env_ids) - self._permanent_wrench_composer.reset(env_ids) + self._instantaneous_wrench_composer.reset(env_ids, env_mask) + self._permanent_wrench_composer.reset(env_ids, env_mask) def write_data_to_sim(self) -> None: """Write external wrench to the simulation. @@ -308,16 +312,8 @@ def write_body_pose_to_sim_mask( skip_forward: Whether to skip invalidating cached data after the write. When True, the caller must invalidate stale cached data before reading it back. Defaults to False. """ - if env_mask is not None: - env_ids = self._resolve_env_mask(env_mask) - else: - env_ids = self._ALL_ENV_INDICES - if body_mask is not None: - body_ids = self._resolve_body_mask(body_mask) - else: - body_ids = self._ALL_BODY_INDICES - self.write_body_link_pose_to_sim_index( - body_poses=body_poses, env_ids=env_ids, body_ids=body_ids, full_data=True, skip_forward=skip_forward + self.write_body_link_pose_to_sim_mask( + body_poses=body_poses, env_mask=env_mask, body_mask=body_mask, skip_forward=skip_forward ) def write_body_velocity_to_sim_index( @@ -386,20 +382,8 @@ def write_body_velocity_to_sim_mask( skip_forward: Whether to skip invalidating cached data after the write. When True, the caller must invalidate stale cached data before reading it back. Defaults to False. """ - if env_mask is not None: - env_ids = self._resolve_env_mask(env_mask) - else: - env_ids = self._ALL_ENV_INDICES - if body_mask is not None: - body_ids = self._resolve_body_mask(body_mask) - else: - body_ids = self._ALL_BODY_INDICES - self.write_body_com_velocity_to_sim_index( - body_velocities=body_velocities, - env_ids=env_ids, - body_ids=body_ids, - full_data=True, - skip_forward=skip_forward, + self.write_body_com_velocity_to_sim_mask( + body_velocities=body_velocities, env_mask=env_mask, body_mask=body_mask, skip_forward=skip_forward ) def write_body_link_pose_to_sim_index( @@ -466,6 +450,7 @@ def write_body_link_pose_to_sim_mask( body_poses: torch.Tensor | wp.array, env_mask: wp.array | None = None, body_ids: Sequence[int] | torch.Tensor | wp.array | slice | None = None, + body_mask: wp.array | None = None, skip_forward: bool = False, ) -> None: """Set the body link pose over selected environment mask into the simulation. @@ -487,17 +472,31 @@ def write_body_link_pose_to_sim_mask( or (num_instances, num_bodies) with dtype wp.transformf. env_mask: Environment mask. If None, then all the instances are updated. Shape is (num_instances,). body_ids: Body indices. If None, then all indices are used. + body_mask: Body mask. Takes precedence over :paramref:`body_ids`. Shape is (num_bodies,). skip_forward: Whether to skip invalidating cached data after the write. When True, the caller must invalidate stale cached data before reading it back. Defaults to False. """ - if env_mask is not None: - env_ids = self._resolve_env_mask(env_mask) - else: + if env_mask is None: env_mask = self._ALL_ENV_MASK - env_ids = self._ALL_ENV_INDICES - self.write_body_link_pose_to_sim_index( - body_poses=body_poses, env_ids=env_ids, body_ids=body_ids, full_data=True, skip_forward=skip_forward + body_mask = self._resolve_body_selection_mask(body_ids, body_mask) + self.assert_shape_and_dtype(body_poses, (self.num_instances, self.num_bodies), wp.transformf, "body_poses") + # Write to consolidated buffer + wp.launch( + shared_kernels.set_body_link_pose_to_sim_mask, + dim=(self.num_instances, self.num_bodies), + inputs=[ + body_poses, + env_mask, + body_mask, + ], + outputs=[ + self.data.body_link_pose_w, + ], + device=self.device, ) + # Invalidate dependent timestamps + if not skip_forward: + self.data._reset_pose(env_mask=env_mask) def write_body_com_pose_to_sim_index( self, @@ -566,6 +565,7 @@ def write_body_com_pose_to_sim_mask( body_poses: torch.Tensor | wp.array, env_mask: wp.array | None = None, body_ids: Sequence[int] | torch.Tensor | wp.array | slice | None = None, + body_mask: wp.array | None = None, skip_forward: bool = False, ) -> None: """Set the body center of mass pose over selected environment mask into the simulation. @@ -588,18 +588,33 @@ def write_body_com_pose_to_sim_mask( or (num_instances, num_bodies) with dtype wp.transformf. env_mask: Environment mask. If None, then all the instances are updated. Shape is (num_instances,). body_ids: Body indices. If None, then all indices are used. + body_mask: Body mask. Takes precedence over :paramref:`body_ids`. Shape is (num_bodies,). skip_forward: Whether to skip invalidating cached data after the write. When True, the caller must invalidate stale cached data before reading it back. Defaults to False. """ - if env_mask is not None: - env_ids = self._resolve_env_mask(env_mask) - else: + if env_mask is None: env_mask = self._ALL_ENV_MASK - env_ids = self._ALL_ENV_INDICES - # The index writer owns the invalidation (with from_link=False); forward skip_forward to it. - self.write_body_com_pose_to_sim_index( - body_poses=body_poses, env_ids=env_ids, body_ids=body_ids, full_data=True, skip_forward=skip_forward + body_mask = self._resolve_body_selection_mask(body_ids, body_mask) + self.assert_shape_and_dtype(body_poses, (self.num_instances, self.num_bodies), wp.transformf, "body_poses") + # Write to consolidated buffers (updates both com_pose_w and link_pose_w) + wp.launch( + shared_kernels.set_body_com_pose_to_sim_mask, + dim=(self.num_instances, self.num_bodies), + inputs=[ + body_poses, + self.data.body_com_pos_b, + env_mask, + body_mask, + ], + outputs=[ + self.data.body_com_pose_w, + self.data.body_link_pose_w, + ], + device=self.device, ) + # Invalidate dependent timestamps. The com poses were just written, so they must not be invalidated. + if not skip_forward: + self.data._reset_pose(env_mask=env_mask, from_link=False) def write_body_com_velocity_to_sim_index( self, @@ -673,6 +688,7 @@ def write_body_com_velocity_to_sim_mask( body_velocities: torch.Tensor | wp.array, env_mask: wp.array | None = None, body_ids: Sequence[int] | torch.Tensor | wp.array | slice | None = None, + body_mask: wp.array | None = None, skip_forward: bool = False, ) -> None: """Set the body center of mass velocity over selected environment mask into the simulation. @@ -698,22 +714,34 @@ def write_body_com_velocity_to_sim_mask( or (num_instances, num_bodies) with dtype wp.spatial_vectorf. env_mask: Environment mask. If None, then all the instances are updated. Shape is (num_instances,). body_ids: Body indices. If None, then all indices are used. + body_mask: Body mask. Takes precedence over :paramref:`body_ids`. Shape is (num_bodies,). skip_forward: Whether to skip invalidating cached data after the write. When True, the caller must invalidate stale cached data before reading it back. Defaults to False. """ - if env_mask is not None: - env_ids = self._resolve_env_mask(env_mask) - else: + if env_mask is None: env_mask = self._ALL_ENV_MASK - env_ids = self._ALL_ENV_INDICES - # The index writer owns the invalidation; forward skip_forward to it. - self.write_body_com_velocity_to_sim_index( - body_velocities=body_velocities, - env_ids=env_ids, - body_ids=body_ids, - full_data=True, - skip_forward=skip_forward, + body_mask = self._resolve_body_selection_mask(body_ids, body_mask) + self.assert_shape_and_dtype( + body_velocities, (self.num_instances, self.num_bodies), wp.spatial_vectorf, "body_velocities" ) + # Write to consolidated buffer + wp.launch( + shared_kernels.set_body_com_velocity_to_sim_mask, + dim=(self.num_instances, self.num_bodies), + inputs=[ + body_velocities, + env_mask, + body_mask, + ], + outputs=[ + self.data.body_com_vel_w, + self.data.body_com_acc_w, + ], + device=self.device, + ) + # Invalidate dependent timestamps + if not skip_forward: + self.data._reset_velocity(env_mask=env_mask) def write_body_link_velocity_to_sim_index( self, @@ -790,6 +818,7 @@ def write_body_link_velocity_to_sim_mask( body_velocities: torch.Tensor | wp.array, env_mask: wp.array | None = None, body_ids: Sequence[int] | torch.Tensor | wp.array | slice | None = None, + body_mask: wp.array | None = None, skip_forward: bool = False, ) -> None: """Set the body link velocity over selected environment mask into the simulation. @@ -814,22 +843,37 @@ def write_body_link_velocity_to_sim_mask( or (num_instances, num_bodies) with dtype wp.spatial_vectorf. env_mask: Environment mask. If None, then all the instances are updated. Shape is (num_instances,). body_ids: Body indices. If None, then all indices are used. + body_mask: Body mask. Takes precedence over :paramref:`body_ids`. Shape is (num_bodies,). skip_forward: Whether to skip invalidating cached data after the write. When True, the caller must invalidate stale cached data before reading it back. Defaults to False. """ - if env_mask is not None: - env_ids = self._resolve_env_mask(env_mask) - else: + if env_mask is None: env_mask = self._ALL_ENV_MASK - env_ids = self._ALL_ENV_INDICES - # The index writer owns the invalidation (with from_com=False); forward skip_forward to it. - self.write_body_link_velocity_to_sim_index( - body_velocities=body_velocities, - env_ids=env_ids, - body_ids=body_ids, - full_data=True, - skip_forward=skip_forward, + body_mask = self._resolve_body_selection_mask(body_ids, body_mask) + self.assert_shape_and_dtype( + body_velocities, (self.num_instances, self.num_bodies), wp.spatial_vectorf, "body_velocities" + ) + # Access body_com_pos_b and body_link_pose_w to ensure they are current. + wp.launch( + shared_kernels.set_body_link_velocity_to_sim_mask, + dim=(self.num_instances, self.num_bodies), + inputs=[ + body_velocities, + self.data.body_com_pos_b, + self.data.body_link_pose_w, + env_mask, + body_mask, + ], + outputs=[ + self.data.body_link_vel_w, + self.data.body_com_vel_w, + self.data.body_com_acc_w, + ], + device=self.device, ) + # Invalidate dependent timestamps. + if not skip_forward: + self.data._reset_velocity(env_mask=env_mask, from_com=False) """ Operations - Setters. @@ -1203,6 +1247,8 @@ def _create_buffers(self): ) self._ALL_ENV_MASK = wp.ones((self.num_instances,), dtype=wp.bool, device=self.device) self._ALL_BODY_MASK = wp.ones((self.num_bodies,), dtype=wp.bool, device=self.device) + # reusable working buffer for normalizing body-ID selections into masks + self._scratch_body_mask = wp.zeros((self.num_bodies,), dtype=wp.bool, device=self.device) # external wrench composer self._instantaneous_wrench_composer = WrenchComposer(self) @@ -1277,25 +1323,27 @@ def _resolve_body_ids(self, body_ids) -> wp.array: return wp.from_torch(body_ids, dtype=wp.int32) return body_ids - def _resolve_env_mask(self, env_mask: wp.array | None) -> wp.array | torch.Tensor: - """Resolve environment mask to indices via torch.nonzero.""" - if env_mask is not None: - if isinstance(env_mask, wp.array): - env_mask = wp.to_torch(env_mask) - env_ids = torch.nonzero(env_mask)[:, 0].to(torch.int32) - else: - env_ids = self._ALL_ENV_INDICES - return env_ids + def _resolve_body_selection_mask( + self, + body_ids: Sequence[int] | torch.Tensor | wp.array | slice | None, + body_mask: wp.array | None, + ) -> wp.array: + """Resolve a body selection to a boolean mask without host synchronization. - def _resolve_body_mask(self, body_mask: wp.array | None) -> wp.array | torch.Tensor: - """Resolve body mask to indices via torch.nonzero.""" - if body_mask is not None: - if isinstance(body_mask, wp.array): - body_mask = wp.to_torch(body_mask) - body_ids = torch.nonzero(body_mask)[:, 0].to(torch.int32) - else: - body_ids = self._ALL_BODY_INDICES - return body_ids + Args: + body_ids: Body indices. ``None`` selects all bodies. + body_mask: Boolean body mask. Takes precedence over :paramref:`body_ids`. + + Returns: + A boolean Warp mask with shape (num_bodies,). + """ + return resolve_1d_mask( + ids=body_ids, + mask=body_mask, + all_mask=self._ALL_BODY_MASK, + scratch_mask=self._scratch_body_mask, + device=self.device, + ) @staticmethod def _build_combined_pattern(prim_path_exprs: list[str]) -> str: diff --git a/source/isaaclab_newton/isaaclab_newton/physics/newton_manager.py b/source/isaaclab_newton/isaaclab_newton/physics/newton_manager.py index d82f53e697d7..60ce57ba444b 100644 --- a/source/isaaclab_newton/isaaclab_newton/physics/newton_manager.py +++ b/source/isaaclab_newton/isaaclab_newton/physics/newton_manager.py @@ -76,6 +76,7 @@ def _paused_gc(): from isaaclab.utils.string import resolve_matching_names from isaaclab.utils.timer import Timer from isaaclab.utils.version import has_kit +from isaaclab.utils.warp import WarpLaunchCache from isaaclab_newton.cloner.newton_clone_utils import replicate_builder_mapping from isaaclab_newton.physics.visualization_builder import build_visualization_builder_from_stage_envs @@ -307,6 +308,7 @@ def provides_implicit_damping(cls) -> bool: # Newton actuator adapter (owns actuators and double-buffered states) _adapter: NewtonActuatorAdapter | None = None + _warp_launch: WarpLaunchCache | None = None # In-graph hooks invoked after the actuator step and before the solver # substeps, in registration order. Multiple articulations register their # implicit-DOF telemetry / FF-routing kernels here. @@ -819,6 +821,13 @@ def step(cls) -> None: @classmethod def close(cls) -> None: """Clean up Newton physics resources.""" + # Asset STOP callbacks invalidate recorded commands. A solver graph may + # contain those commands, so release the graph and drain device work + # before PhysicsManager dispatches STOP. + NewtonManager._graph = None + NewtonManager._graph_capture_pending = False + if PhysicsManager._device is not None: + wp.synchronize_device(PhysicsManager._device) super().close() cls.clear() @@ -861,6 +870,10 @@ def clear(cls): """Clear all Newton-specific state (callbacks cleared by super().close()).""" if cls._cubric is not None and cls._cubric_adapter is not None: cls._cubric.release_adapter(cls._cubric_adapter) + # Recorded commands may be embedded in the graph and retain pointers + # owned by the adapter cache. Destroy the graph before that cache. + NewtonManager._graph = None + NewtonManager._graph_capture_pending = False NewtonManager._cubric = None NewtonManager._cubric_adapter = None NewtonManager._cubric_bound_fabric_id = None @@ -881,7 +894,10 @@ def clear(cls): NewtonManager._newton_imu_sensors = [] NewtonManager._report_contacts = False NewtonManager._supports_contact_sensors = True + if NewtonManager._adapter is not None: + NewtonManager._adapter._invalidate_launch_cache() NewtonManager._adapter = None + NewtonManager._warp_launch = None NewtonManager._post_actuator_callbacks = [] NewtonManager._post_step_callbacks = [] # Set by an articulation that took the ``use_newton_actuators=True`` @@ -893,8 +909,6 @@ def clear(cls): # Per-world reset masks NewtonManager._world_reset_mask = None NewtonManager._fk_reset_mask = None - NewtonManager._graph = None - NewtonManager._graph_capture_pending = False NewtonManager._sensor_tasks = {} NewtonManager._invalidate_sensor_graph() NewtonManager._sensor_state = None @@ -1275,6 +1289,11 @@ def start_simulation(cls) -> None: Note: Collision pipeline is initialized later in initialize_solver() after we determine whether the solver needs external collision detection. """ + # A previous graph may embed recorded adapter launches. Destroy it + # before replacing model/state storage or the owning adapter cache. + NewtonManager._graph = None + NewtonManager._graph_capture_pending = False + logger.debug(f"Builder: {cls._builder}") cls._drain_stale_cuda_error() @@ -1319,7 +1338,10 @@ def start_simulation(cls) -> None: # articulation after this point. Assign through the explicit base # class so external readers (which import ``NewtonManager`` directly) # observe the canonical state regardless of which subclass is active. + if NewtonManager._adapter is not None: + NewtonManager._adapter._invalidate_launch_cache() NewtonManager._adapter = None + NewtonManager._warp_launch = None NewtonManager._use_newton_actuators_active = False # Allocate per-world reset masks (used by all solvers for masked FK, and by Kamino for masked reset). @@ -2354,12 +2376,18 @@ def activate_newton_actuator_path(cls) -> None: from isaaclab_newton.actuators import NewtonActuatorAdapter # noqa: PLC0415 dofs_per_env = cls._model.joint_dof_count // cls._num_envs + cfg = PhysicsManager._cfg + NewtonManager._warp_launch = WarpLaunchCache( + mode=None if getattr(cfg, "use_warp_launch_cache", False) else "eager", + device=PhysicsManager._device, + ) NewtonManager._adapter = NewtonActuatorAdapter( actuators=list(cls._model.actuators), num_envs=cls._num_envs, num_joints=dofs_per_env, dof_offset=0, device=PhysicsManager._device, + warp_launch=NewtonManager._warp_launch, ) cls._adapter.finalize(cls._control) diff --git a/source/isaaclab_newton/isaaclab_newton/physics/newton_manager_cfg.py b/source/isaaclab_newton/isaaclab_newton/physics/newton_manager_cfg.py index 8747d667d3ce..21016f105a05 100644 --- a/source/isaaclab_newton/isaaclab_newton/physics/newton_manager_cfg.py +++ b/source/isaaclab_newton/isaaclab_newton/physics/newton_manager_cfg.py @@ -134,6 +134,20 @@ class NewtonCfg(PhysicsCfg): If set to False, the simulation performance will be severely degraded. """ + use_warp_launch_cache: bool = False + """Whether to replay recorded Warp launches outside the Newton solver graph. + + When enabled, cached :class:`warp.Launch` commands reduce Python launch + preparation for pointer-stable articulation target publication, eager + Newton-actuator stepping, and capture-unsafe lazy derived-state properties. + It does not make Python timestamp branches, Torch actuator computation, or + the complete Isaac Lab actuator publication path CUDA-graph-safe. + + The execution mode of the enabled caches follows ``ISAACLAB_WARP_LAUNCH_MODE`` + (default ``auto``: replay on CUDA, eager on CPU). When disabled, these paths + always launch eagerly. + """ + solver_cfg: NewtonSolverCfg | None = None """Solver configuration. If None (default), MJWarpSolverCfg is used by default.""" diff --git a/source/isaaclab_newton/isaaclab_newton/sensors/pva/pva.py b/source/isaaclab_newton/isaaclab_newton/sensors/pva/pva.py index 3eb06e007201..2be89399d03a 100644 --- a/source/isaaclab_newton/isaaclab_newton/sensors/pva/pva.py +++ b/source/isaaclab_newton/isaaclab_newton/sensors/pva/pva.py @@ -219,7 +219,8 @@ def _debug_vis_callback(self, event): up_axis = UsdGeom.GetStageUpAxis(self.stage) pos_w_torch = self._data.pos_w.torch accel_w = math_utils.quat_apply(self._data.quat_w.torch, self._data.lin_acc_b.torch) - valid_indices = (torch.linalg.norm(accel_w, dim=-1) > 1e-5).nonzero(as_tuple=True)[0] + accel_valid = torch.linalg.norm(accel_w, dim=-1) > 1e-5 + valid_indices = accel_valid.nonzero(as_tuple=True)[0] if valid_indices.numel() == 0: return pos_filtered = pos_w_torch.index_select(0, valid_indices) diff --git a/source/isaaclab_newton/test/actuators/test_adapter_mask_reset.py b/source/isaaclab_newton/test/actuators/test_adapter_mask_reset.py new file mode 100644 index 000000000000..e33235ba28c7 --- /dev/null +++ b/source/isaaclab_newton/test/actuators/test_adapter_mask_reset.py @@ -0,0 +1,65 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for mask-based Newton actuator adapter resets.""" + +from __future__ import annotations + +from unittest.mock import Mock + +import numpy as np +import warp as wp +from isaaclab_newton.actuators.adapter import NewtonActuatorAdapter + + +def test_adapter_expands_environment_mask_without_allocating() -> None: + """Adapter state masks should select every actuator DOF in masked environments.""" + adapter = NewtonActuatorAdapter.__new__(NewtonActuatorAdapter) + actuator = Mock() + actuator.indices = wp.array([0, 1, 2, 3], dtype=wp.uint32, device="cpu") + state_a = Mock() + state_b = Mock() + adapter.actuators = [actuator] + adapter._states_a = [state_a] + adapter._states_b = [state_b] + adapter._num_envs = 2 + adapter._dof_offset = 0 + adapter.num_joints = 2 + adapter._device = "cpu" + adapter._reset_env_mask = wp.zeros(2, dtype=wp.bool, device="cpu") + adapter._reset_dof_masks = [wp.zeros(4, dtype=wp.bool, device="cpu")] + env_mask = wp.array([False, True], dtype=wp.bool, device="cpu") + + adapter.reset(env_mask=env_mask) + + state_a.reset.assert_called_once() + state_b.reset.assert_called_once() + np.testing.assert_array_equal(state_a.reset.call_args.args[0].numpy(), [False, False, True, True]) + assert state_a.reset.call_args.args[0] is adapter._reset_dof_masks[0] + + +def test_adapter_reuses_id_mask_without_stale_selection() -> None: + """Sequential ID resets should clear the reusable environment mask.""" + adapter = NewtonActuatorAdapter.__new__(NewtonActuatorAdapter) + actuator = Mock() + actuator.indices = wp.array([0, 1, 2, 3], dtype=wp.uint32, device="cpu") + state_a = Mock() + adapter.actuators = [actuator] + adapter._states_a = [state_a] + adapter._states_b = [None] + adapter._num_envs = 2 + adapter._dof_offset = 0 + adapter.num_joints = 2 + adapter._device = "cpu" + adapter._reset_env_mask = wp.zeros(2, dtype=wp.bool, device="cpu") + adapter._reset_dof_masks = [wp.zeros(4, dtype=wp.bool, device="cpu")] + + adapter.reset(env_ids=[0]) + first_mask = state_a.reset.call_args.args[0].numpy().copy() + adapter.reset(env_ids=[1]) + second_mask = state_a.reset.call_args.args[0].numpy().copy() + + np.testing.assert_array_equal(first_mask, [True, True, False, False]) + np.testing.assert_array_equal(second_mask, [False, False, True, True]) diff --git a/source/isaaclab_newton/test/assets/test_articulation_warp_launch_cache.py b/source/isaaclab_newton/test/assets/test_articulation_warp_launch_cache.py new file mode 100644 index 000000000000..182b68ff6539 --- /dev/null +++ b/source/isaaclab_newton/test/assets/test_articulation_warp_launch_cache.py @@ -0,0 +1,281 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp launch handling at the Newton Isaac Lab actuator boundary.""" + +from types import SimpleNamespace + +import isaaclab_newton.assets.articulation.articulation as articulation_module +import isaaclab_newton.assets.rigid_object.rigid_object as rigid_object_module +import pytest +import torch +import warp as wp +from isaaclab_newton.assets.articulation.articulation import Articulation +from isaaclab_newton.assets.rigid_object.rigid_object import RigidObject +from isaaclab_newton.physics import NewtonCfg + +from isaaclab.sim import SimulationContext +from isaaclab.utils.types import ArticulationActions +from isaaclab.utils.warp import WarpLaunchCache + + +class _AllocatingActuator: + """Minimal explicit actuator that returns new Torch allocations per call.""" + + def __init__(self, num_envs: int, num_joints: int, device: str): + self.is_implicit_model = False + self.joint_indices = slice(None) + self.gear_ratio = None + self.velocity_limit = torch.full((num_envs, num_joints), 10.0, device=device) + self.computed_effort = torch.zeros((num_envs, num_joints), device=device) + self.applied_effort = torch.zeros_like(self.computed_effort) + self.output_pointers: list[tuple[int, int, int, int]] = [] + + def compute( + self, + control_action: ArticulationActions, + joint_pos: torch.Tensor, + joint_vel: torch.Tensor, + ) -> ArticulationActions: + del joint_pos, joint_vel + self.computed_effort = control_action.joint_efforts + 1.0 + self.applied_effort = self.computed_effort * 2.0 + if self.output_pointers: + self.gear_ratio = self.computed_effort + 10.0 + control_action.joint_positions = self.applied_effort + 100.0 + else: + self.gear_ratio = None + control_action.joint_positions = None + self.velocity_limit = self.computed_effort + 20.0 + self.output_pointers.append( + ( + self.computed_effort.data_ptr(), + self.applied_effort.data_ptr(), + 0 if self.gear_ratio is None else self.gear_ratio.data_ptr(), + self.velocity_limit.data_ptr(), + ) + ) + control_action.joint_velocities = None + control_action.joint_efforts = self.applied_effort + return control_action + + +class _AllocatingImplicitActuator: + """Minimal implicit actuator with persistent targets and allocating telemetry.""" + + is_implicit_model = True + + def __init__(self, num_envs: int, num_joints: int, device: str): + self.joint_indices = slice(None) + self.gear_ratio = None + self.velocity_limit = torch.full((num_envs, num_joints), 10.0, device=device) + self.computed_effort = torch.zeros((num_envs, num_joints), device=device) + self.applied_effort = torch.zeros_like(self.computed_effort) + self.output_pointers: list[tuple[int, int]] = [] + + def compute( + self, + control_action: ArticulationActions, + joint_pos: torch.Tensor, + joint_vel: torch.Tensor, + ) -> ArticulationActions: + del joint_pos, joint_vel + self.computed_effort = control_action.joint_efforts + 1.0 + self.applied_effort = self.computed_effort * 2.0 + self.output_pointers.append((self.computed_effort.data_ptr(), self.applied_effort.data_ptr())) + return control_action + + +def _make_articulation_stub( + actuator: _AllocatingActuator | _AllocatingImplicitActuator, + num_envs: int, + num_joints: int, + device: str, +) -> tuple[SimpleNamespace, torch.Tensor]: + """Create the minimum articulation state needed by the actuator publication path.""" + shape = (num_envs, num_joints) + articulation = SimpleNamespace(device=device, num_instances=num_envs) + articulation._ALL_JOINT_INDICES = wp.array(range(num_joints), dtype=wp.int32, device=device) + articulation._joint_pos_target_sim = wp.zeros(shape, dtype=wp.float32, device=device) + articulation._joint_vel_target_sim = wp.zeros(shape, dtype=wp.float32, device=device) + articulation._joint_effort_target_sim = wp.zeros(shape, dtype=wp.float32, device=device) + articulation._warp_launch = WarpLaunchCache(mode="replay", debug=True, device=device) + articulation.actuators = {"test": actuator} + + joint_effort_target = torch.ones(shape, device=device) + articulation._data = SimpleNamespace( + joint_pos_target=SimpleNamespace(torch=torch.zeros(shape, device=device)), + joint_vel_target=SimpleNamespace(torch=torch.zeros(shape, device=device)), + joint_effort_target=SimpleNamespace(torch=joint_effort_target), + joint_pos=SimpleNamespace(torch=torch.zeros(shape, device=device)), + joint_vel=SimpleNamespace(torch=torch.zeros(shape, device=device)), + computed_torque=wp.zeros(shape, dtype=wp.float32, device=device), + applied_torque=wp.zeros(shape, dtype=wp.float32, device=device), + gear_ratio=wp.zeros(shape, dtype=wp.float32, device=device), + soft_joint_vel_limits=wp.zeros(shape, dtype=wp.float32, device=device), + ) + return articulation, joint_effort_target + + +@pytest.fixture(scope="module") +def cuda_device() -> str: + """Return a CUDA device or skip the module when CUDA is unavailable.""" + wp.init() + if not wp.is_cuda_available(): + pytest.skip("CUDA is required for Warp launch replay tests.") + return "cuda:0" + + +def test_newton_launch_cache_is_disabled_by_default(): + """Newton should preserve eager actuator-boundary launches unless opted in.""" + assert NewtonCfg().use_warp_launch_cache is False + + +@pytest.mark.parametrize( + ("asset_cls", "asset_module"), + [(Articulation, articulation_module), (RigidObject, rigid_object_module)], +) +def test_asset_launch_cache_uses_sim_device_before_asset_initialization( + asset_cls: type, + asset_module, + monkeypatch: pytest.MonkeyPatch, +): + """Asset construction should not read ``asset.device`` before the ready callback sets it.""" + sim_context = SimpleNamespace( + cfg=SimpleNamespace(physics=SimpleNamespace(use_warp_launch_cache=True)), + device="cpu", + ) + monkeypatch.setattr(SimulationContext, "instance", classmethod(lambda cls: sim_context)) + monkeypatch.setattr(asset_cls.__mro__[1], "__init__", lambda self, cfg: setattr(self, "cfg", cfg)) + monkeypatch.setattr(asset_cls, "_clear_callbacks", lambda self: None) + monkeypatch.setattr(asset_module, "has_kit", lambda: False) + monkeypatch.setattr(asset_module, "queue_newton_physics_replication", lambda cfg: None) + + asset = object.__new__(asset_cls) + asset_cls.__init__(asset, SimpleNamespace()) + + assert not hasattr(asset, "_device") + assert asset._warp_launch._device == wp.get_device("cpu") + assert asset._warp_launch._mode == "auto" + del asset + + +def test_actuator_publication_accepts_reallocated_torch_outputs(cuda_device: str): + """Eager publication should use current actuator tensors after their pointers change.""" + num_envs = 8 + num_joints = 3 + actuator = _AllocatingActuator(num_envs, num_joints, cuda_device) + articulation, joint_effort_target = _make_articulation_stub(actuator, num_envs, num_joints, cuda_device) + shape = (num_envs, num_joints) + + Articulation._apply_actuator_model(articulation) + joint_effort_target.fill_(3.0) + Articulation._apply_actuator_model(articulation) + wp.synchronize_device(cuda_device) + + assert actuator.output_pointers[0] != actuator.output_pointers[1] + assert len(articulation._warp_launch._entries) == 0 + torch.testing.assert_close( + wp.to_torch(articulation._joint_pos_target_sim), torch.full(shape, 108.0, device=cuda_device) + ) + torch.testing.assert_close( + wp.to_torch(articulation._joint_effort_target_sim), torch.full(shape, 8.0, device=cuda_device) + ) + torch.testing.assert_close( + wp.to_torch(articulation._data.computed_torque), torch.full(shape, 4.0, device=cuda_device) + ) + torch.testing.assert_close( + wp.to_torch(articulation._data.applied_torque), torch.full(shape, 8.0, device=cuda_device) + ) + torch.testing.assert_close(wp.to_torch(articulation._data.gear_ratio), torch.full(shape, 14.0, device=cuda_device)) + torch.testing.assert_close( + wp.to_torch(articulation._data.soft_joint_vel_limits), torch.full(shape, 24.0, device=cuda_device) + ) + + +def test_full_slice_implicit_targets_use_recorded_launch(cuda_device: str): + """Persistent full-group implicit targets should replay while allocating telemetry stays eager.""" + num_envs = 8 + num_joints = 3 + shape = (num_envs, num_joints) + actuator = _AllocatingImplicitActuator(num_envs, num_joints, cuda_device) + articulation, joint_effort_target = _make_articulation_stub(actuator, num_envs, num_joints, cuda_device) + + Articulation._apply_actuator_model(articulation) + joint_effort_target.fill_(3.0) + Articulation._apply_actuator_model(articulation) + wp.synchronize_device(cuda_device) + + assert actuator.output_pointers[0] != actuator.output_pointers[1] + assert len(articulation._warp_launch._entries) == 1 + torch.testing.assert_close( + wp.to_torch(articulation._joint_effort_target_sim), torch.full(shape, 3.0, device=cuda_device) + ) + torch.testing.assert_close( + wp.to_torch(articulation._data.computed_torque), torch.full(shape, 4.0, device=cuda_device) + ) + torch.testing.assert_close( + wp.to_torch(articulation._data.applied_torque), torch.full(shape, 8.0, device=cuda_device) + ) + + +@pytest.mark.parametrize("newton_actuator_path", [False, True]) +def test_joint_target_reorder_uses_recorded_launch(cuda_device: str, newton_actuator_path: bool): + """Persistent Lab and Newton joint-target reorders should replay current in-place values.""" + num_envs = 4 + num_joints = 3 + shape = (num_envs, num_joints) + source_effort = wp.ones(shape, dtype=wp.float32, device=cuda_device) + source_pos = wp.full(shape, 2.0, dtype=wp.float32, device=cuda_device) + source_vel = wp.full(shape, 3.0, dtype=wp.float32, device=cuda_device) + backend_effort = wp.zeros(shape, dtype=wp.float32, device=cuda_device) + backend_pos = wp.zeros(shape, dtype=wp.float32, device=cuda_device) + backend_vel = wp.zeros(shape, dtype=wp.float32, device=cuda_device) + backend_act = wp.zeros(shape, dtype=wp.float32, device=cuda_device) + backend_to_user = wp.array([2, 0, 1], dtype=wp.int32, device=cuda_device) + + articulation = SimpleNamespace( + _has_newton_actuators=newton_actuator_path, + _has_implicit_actuators=True, + _instantaneous_wrench_composer=SimpleNamespace(active=False), + _permanent_wrench_composer=SimpleNamespace(active=False), + _joint_effort_target_sim=source_effort, + _joint_pos_target_sim=source_pos, + _joint_vel_target_sim=source_vel, + _joint_backend_to_user_map=lambda: backend_to_user, + _apply_actuator_model=lambda: None, + _warp_launch=WarpLaunchCache(mode="replay", debug=True, device=cuda_device), + _data=SimpleNamespace( + _joint_effort_target=source_effort, + _joint_pos_target=source_pos, + _joint_vel_target=source_vel, + ), + data=SimpleNamespace( + _sim_bind_joint_effort=backend_effort, + _sim_bind_joint_position_target=backend_pos, + _sim_bind_joint_velocity_target=backend_vel, + _sim_bind_joint_act=backend_act, + ), + device=cuda_device, + num_instances=num_envs, + num_joints=num_joints, + ) + + Articulation.write_data_to_sim(articulation) + source_effort.fill_(4.0) + source_pos.fill_(5.0) + source_vel.fill_(6.0) + Articulation.write_data_to_sim(articulation) + wp.synchronize_device(cuda_device) + + assert len(articulation._warp_launch._entries) == 1 + entry = next(iter(articulation._warp_launch._entries.values())) + expected_site = "newton_joint_targets" if newton_actuator_path else "lab_joint_targets" + assert entry.site == expected_site + torch.testing.assert_close(wp.to_torch(backend_effort), torch.full(shape, 4.0, device=cuda_device)) + torch.testing.assert_close(wp.to_torch(backend_pos), torch.full(shape, 5.0, device=cuda_device)) + torch.testing.assert_close(wp.to_torch(backend_vel), torch.full(shape, 6.0, device=cuda_device)) + if newton_actuator_path: + torch.testing.assert_close(wp.to_torch(backend_act), torch.full(shape, 4.0, device=cuda_device)) diff --git a/source/isaaclab_newton/test/assets/test_collection_mask_write_kernels.py b/source/isaaclab_newton/test/assets/test_collection_mask_write_kernels.py new file mode 100644 index 000000000000..ce9fd9561ae7 --- /dev/null +++ b/source/isaaclab_newton/test/assets/test_collection_mask_write_kernels.py @@ -0,0 +1,169 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Parity tests between masked and index body-write kernels.""" + +from __future__ import annotations + +import numpy as np +import warp as wp +from isaaclab_newton.assets import kernels as shared_kernels + +_NUM_ENVS = 5 +_NUM_BODIES = 4 + + +def _selection(rng: np.random.Generator): + """Sample matching boolean masks and compact indices for envs and bodies.""" + env_mask = rng.random(_NUM_ENVS) < 0.5 + body_mask = rng.random(_NUM_BODIES) < 0.5 + env_mask[0] = True + body_mask[1] = True + env_ids = np.nonzero(env_mask)[0].astype(np.int32) + body_ids = np.nonzero(body_mask)[0].astype(np.int32) + return env_mask, body_mask, env_ids, body_ids + + +def _random_transforms(rng: np.random.Generator) -> np.ndarray: + positions = rng.uniform(-1.0, 1.0, size=(_NUM_ENVS, _NUM_BODIES, 3)) + quaternions = rng.normal(size=(_NUM_ENVS, _NUM_BODIES, 4)) + quaternions /= np.linalg.norm(quaternions, axis=-1, keepdims=True) + return np.concatenate([positions, quaternions], axis=-1).astype(np.float32) + + +def _random_spatial(rng: np.random.Generator) -> np.ndarray: + return rng.uniform(-1.0, 1.0, size=(_NUM_ENVS, _NUM_BODIES, 6)).astype(np.float32) + + +def _pair(values: np.ndarray, dtype) -> tuple[wp.array, wp.array]: + """Two independent device copies of the same initial buffer contents.""" + return wp.array(values, dtype=dtype, device="cpu"), wp.array(values, dtype=dtype, device="cpu") + + +def test_masked_body_link_pose_kernel_matches_index_kernel(): + rng = np.random.default_rng(7) + env_mask_np, body_mask_np, env_ids_np, body_ids_np = _selection(rng) + data = wp.array(_random_transforms(rng), dtype=wp.transformf, device="cpu") + out_index, out_mask = _pair(_random_transforms(rng), wp.transformf) + env_ids = wp.array(env_ids_np, dtype=wp.int32, device="cpu") + body_ids = wp.array(body_ids_np, dtype=wp.int32, device="cpu") + env_mask = wp.array(env_mask_np, dtype=wp.bool, device="cpu") + body_mask = wp.array(body_mask_np, dtype=wp.bool, device="cpu") + + wp.launch( + shared_kernels.set_body_link_pose_to_sim, + dim=(env_ids_np.size, body_ids_np.size), + inputs=[data, env_ids, body_ids, True], + outputs=[out_index], + device="cpu", + ) + wp.launch( + shared_kernels.set_body_link_pose_to_sim_mask, + dim=(_NUM_ENVS, _NUM_BODIES), + inputs=[data, env_mask, body_mask], + outputs=[out_mask], + device="cpu", + ) + + np.testing.assert_allclose(out_mask.numpy(), out_index.numpy()) + + +def test_masked_body_com_pose_kernel_matches_index_kernel(): + rng = np.random.default_rng(11) + env_mask_np, body_mask_np, env_ids_np, body_ids_np = _selection(rng) + data = wp.array(_random_transforms(rng), dtype=wp.transformf, device="cpu") + com_pos_b = wp.array( + rng.uniform(-0.2, 0.2, size=(_NUM_ENVS, _NUM_BODIES, 3)).astype(np.float32), dtype=wp.vec3f, device="cpu" + ) + com_index, com_mask = _pair(_random_transforms(rng), wp.transformf) + link_index, link_mask = _pair(_random_transforms(rng), wp.transformf) + env_ids = wp.array(env_ids_np, dtype=wp.int32, device="cpu") + body_ids = wp.array(body_ids_np, dtype=wp.int32, device="cpu") + env_mask = wp.array(env_mask_np, dtype=wp.bool, device="cpu") + body_mask = wp.array(body_mask_np, dtype=wp.bool, device="cpu") + + wp.launch( + shared_kernels.set_body_com_pose_to_sim, + dim=(env_ids_np.size, body_ids_np.size), + inputs=[data, com_pos_b, env_ids, body_ids, True], + outputs=[com_index, link_index], + device="cpu", + ) + wp.launch( + shared_kernels.set_body_com_pose_to_sim_mask, + dim=(_NUM_ENVS, _NUM_BODIES), + inputs=[data, com_pos_b, env_mask, body_mask], + outputs=[com_mask, link_mask], + device="cpu", + ) + + np.testing.assert_allclose(com_mask.numpy(), com_index.numpy()) + np.testing.assert_allclose(link_mask.numpy(), link_index.numpy()) + + +def test_masked_body_com_velocity_kernel_matches_index_kernel(): + rng = np.random.default_rng(13) + env_mask_np, body_mask_np, env_ids_np, body_ids_np = _selection(rng) + data = wp.array(_random_spatial(rng), dtype=wp.spatial_vectorf, device="cpu") + vel_index, vel_mask = _pair(_random_spatial(rng), wp.spatial_vectorf) + acc_index, acc_mask = _pair(_random_spatial(rng), wp.spatial_vectorf) + env_ids = wp.array(env_ids_np, dtype=wp.int32, device="cpu") + body_ids = wp.array(body_ids_np, dtype=wp.int32, device="cpu") + env_mask = wp.array(env_mask_np, dtype=wp.bool, device="cpu") + body_mask = wp.array(body_mask_np, dtype=wp.bool, device="cpu") + + wp.launch( + shared_kernels.set_body_com_velocity_to_sim, + dim=(env_ids_np.size, body_ids_np.size), + inputs=[data, env_ids, body_ids, True], + outputs=[vel_index, acc_index], + device="cpu", + ) + wp.launch( + shared_kernels.set_body_com_velocity_to_sim_mask, + dim=(_NUM_ENVS, _NUM_BODIES), + inputs=[data, env_mask, body_mask], + outputs=[vel_mask, acc_mask], + device="cpu", + ) + + np.testing.assert_allclose(vel_mask.numpy(), vel_index.numpy()) + np.testing.assert_allclose(acc_mask.numpy(), acc_index.numpy()) + + +def test_masked_body_link_velocity_kernel_matches_index_kernel(): + rng = np.random.default_rng(17) + env_mask_np, body_mask_np, env_ids_np, body_ids_np = _selection(rng) + data = wp.array(_random_spatial(rng), dtype=wp.spatial_vectorf, device="cpu") + com_pos_b = wp.array( + rng.uniform(-0.2, 0.2, size=(_NUM_ENVS, _NUM_BODIES, 3)).astype(np.float32), dtype=wp.vec3f, device="cpu" + ) + link_pose_w = wp.array(_random_transforms(rng), dtype=wp.transformf, device="cpu") + link_vel_index, link_vel_mask = _pair(_random_spatial(rng), wp.spatial_vectorf) + com_vel_index, com_vel_mask = _pair(_random_spatial(rng), wp.spatial_vectorf) + acc_index, acc_mask = _pair(_random_spatial(rng), wp.spatial_vectorf) + env_ids = wp.array(env_ids_np, dtype=wp.int32, device="cpu") + body_ids = wp.array(body_ids_np, dtype=wp.int32, device="cpu") + env_mask = wp.array(env_mask_np, dtype=wp.bool, device="cpu") + body_mask = wp.array(body_mask_np, dtype=wp.bool, device="cpu") + + wp.launch( + shared_kernels.set_body_link_velocity_to_sim, + dim=(env_ids_np.size, body_ids_np.size), + inputs=[data, com_pos_b, link_pose_w, env_ids, body_ids, True], + outputs=[link_vel_index, com_vel_index, acc_index], + device="cpu", + ) + wp.launch( + shared_kernels.set_body_link_velocity_to_sim_mask, + dim=(_NUM_ENVS, _NUM_BODIES), + inputs=[data, com_pos_b, link_pose_w, env_mask, body_mask], + outputs=[link_vel_mask, com_vel_mask, acc_mask], + device="cpu", + ) + + np.testing.assert_allclose(link_vel_mask.numpy(), link_vel_index.numpy()) + np.testing.assert_allclose(com_vel_mask.numpy(), com_vel_index.numpy()) + np.testing.assert_allclose(acc_mask.numpy(), acc_index.numpy()) diff --git a/source/isaaclab_newton/test/assets/test_newton_mask_reset_forwarding.py b/source/isaaclab_newton/test/assets/test_newton_mask_reset_forwarding.py new file mode 100644 index 000000000000..a5c888eb2d0b --- /dev/null +++ b/source/isaaclab_newton/test/assets/test_newton_mask_reset_forwarding.py @@ -0,0 +1,132 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp reset-mask forwarding to Newton asset wrench storage.""" + +from unittest.mock import Mock, patch + +import torch +import warp as wp +from isaaclab_newton.assets.articulation.articulation import Articulation +from isaaclab_newton.assets.rigid_object.rigid_object import RigidObject +from isaaclab_newton.assets.rigid_object_collection.rigid_object_collection import RigidObjectCollection + + +def test_rigid_object_forwards_reset_mask_to_wrench_composers() -> None: + """Rigid-object resets should preserve mask selection at wrench storage.""" + asset = RigidObject.__new__(RigidObject) + asset._initialize_handle = None + asset._invalidate_initialize_handle = None + asset._prim_deletion_handle = None + asset._instantaneous_wrench_composer = Mock() + asset._permanent_wrench_composer = Mock() + env_mask = wp.array([False, True], dtype=wp.bool, device="cpu") + + asset.reset(env_mask=env_mask) + + asset._instantaneous_wrench_composer.reset.assert_called_once_with(slice(None), env_mask) + asset._permanent_wrench_composer.reset.assert_called_once_with(slice(None), env_mask) + + +def test_rigid_object_collection_forwards_reset_mask_to_wrench_composers() -> None: + """Collection resets should preserve mask selection at wrench storage.""" + asset = RigidObjectCollection.__new__(RigidObjectCollection) + asset._initialize_handle = None + asset._invalidate_initialize_handle = None + asset._prim_deletion_handle = None + asset._instantaneous_wrench_composer = Mock() + asset._permanent_wrench_composer = Mock() + env_ids = [0] + env_mask = wp.array([True, False], dtype=wp.bool, device="cpu") + + asset.reset(env_ids=env_ids, object_ids=slice(None), env_mask=env_mask) + + asset._instantaneous_wrench_composer.reset.assert_called_once_with(env_ids, env_mask) + asset._permanent_wrench_composer.reset.assert_called_once_with(env_ids, env_mask) + + +def test_articulation_without_lab_actuators_keeps_mask_on_device() -> None: + """Mask resets should not materialize IDs when no Torch actuator consumes them.""" + asset = Articulation.__new__(Articulation) + asset._initialize_handle = None + asset._invalidate_initialize_handle = None + asset._prim_deletion_handle = None + asset.actuators = {} + asset._has_newton_actuators = False + asset._instantaneous_wrench_composer = Mock() + asset._permanent_wrench_composer = Mock() + env_mask = object() + + asset.reset(env_mask=env_mask) + + asset._instantaneous_wrench_composer.reset.assert_called_once_with(slice(None), env_mask) + + +def test_articulation_legacy_mode_forwards_mask_to_actuators() -> None: + """Legacy-mode masked resets should hand actuators the boolean view, not compact IDs.""" + asset = Articulation.__new__(Articulation) + asset._initialize_handle = None + asset._invalidate_initialize_handle = None + asset._prim_deletion_handle = None + lab_actuator = Mock() + asset.actuators = {"legs": lab_actuator} + asset._has_newton_actuators = False + asset._instantaneous_wrench_composer = Mock() + asset._permanent_wrench_composer = Mock() + env_mask = wp.array([False, True], dtype=wp.bool, device="cpu") + + asset.reset(env_mask=env_mask) + + lab_actuator.reset.assert_not_called() + forwarded_mask = lab_actuator.reset_mask.call_args.args[0] + assert forwarded_mask.dtype == torch.bool + torch.testing.assert_close(forwarded_mask, torch.tensor([False, True])) + + +def test_articulation_reset_capture_safe_reflects_actuator_capability() -> None: + """Capture safety should require mask-native actuator resets in legacy mode.""" + from isaaclab.actuators import ActuatorBase, ImplicitActuator + + class _LegacyActuator(ActuatorBase): + def reset(self, env_ids): + pass + + def compute(self, control_action, joint_pos, joint_vel): + return control_action + + asset = Articulation.__new__(Articulation) + asset._has_newton_actuators = True + asset.actuators = {} + assert asset.reset_capture_safe + + asset._has_newton_actuators = False + asset.actuators = {"implicit": ImplicitActuator.__new__(ImplicitActuator)} + assert asset.reset_capture_safe + + asset.actuators = {"legacy": _LegacyActuator.__new__(_LegacyActuator)} + assert not asset.reset_capture_safe + + +def test_articulation_native_mode_masked_reset_skips_lab_actuators() -> None: + """Native-mode partial resets must not reset Lab actuator state for all environments.""" + asset = Articulation.__new__(Articulation) + asset._initialize_handle = None + asset._invalidate_initialize_handle = None + asset._prim_deletion_handle = None + lab_actuator = Mock() + asset.actuators = {"legs": lab_actuator} + asset._has_newton_actuators = True + asset._instantaneous_wrench_composer = Mock() + asset._permanent_wrench_composer = Mock() + env_mask = wp.array([False, True], dtype=wp.bool, device="cpu") + adapter = Mock() + + from isaaclab_newton.physics import NewtonManager + + with patch.object(NewtonManager, "_adapter", adapter): + asset.reset(env_mask=env_mask) + + lab_actuator.reset.assert_not_called() + adapter.reset.assert_called_once_with(slice(None), env_mask=env_mask) diff --git a/source/isaaclab_newton/test/assets/test_warp_launch_cache_hot_paths.py b/source/isaaclab_newton/test/assets/test_warp_launch_cache_hot_paths.py new file mode 100644 index 000000000000..64004764163f --- /dev/null +++ b/source/isaaclab_newton/test/assets/test_warp_launch_cache_hot_paths.py @@ -0,0 +1,258 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for recorded replay on capture-unsafe Newton hot paths.""" + +from types import SimpleNamespace + +import numpy as np +import pytest +import warp as wp +from isaaclab_newton.actuators import NewtonActuatorAdapter +from isaaclab_newton.assets import ArticulationData, RigidObjectData +from isaaclab_newton.physics import NewtonManager + +from isaaclab.physics import PhysicsManager +from isaaclab.utils.buffers import TimestampedBufferWarp as TimestampedBuffer +from isaaclab.utils.warp import ProxyArray, WarpLaunchCache + + +class _NoOpActuator: + """Minimal Newton actuator for adapter launch tests.""" + + def __init__(self, indices: wp.array): + self.indices = indices + self.control_computed_output_attr = None + + def state(self): + return None + + def step(self, sim_state, sim_control, state_in, state_out, dt: float) -> None: + del sim_state, sim_control, state_in, state_out, dt + + def is_graphable(self) -> bool: + return False + + +class _RebindReset(RuntimeError): + """Sentinel raised when the rebind hook resets its launch cache.""" + + +class _ResetProbe: + """Cache probe that stops a simulation rebind after observing reset.""" + + def __init__(self, events: list[tuple[str, str | None]]): + self.events = events + + def reset(self) -> None: + self.events.append(("reset", None)) + raise _RebindReset + + +@pytest.fixture(scope="module") +def cuda_device() -> str: + """Return a CUDA device or skip the module when CUDA is unavailable.""" + wp.init() + if not wp.is_cuda_available(): + pytest.skip("CUDA is required for Warp launch replay tests.") + return "cuda:0" + + +def _spatial_vectors(num_envs: int, value: float, device: str) -> wp.array: + """Create constant spatial vectors for a derived-state test.""" + values = np.full((num_envs, 6), value, dtype=np.float32) + return wp.array(values, dtype=wp.spatial_vectorf, device=device) + + +def _make_root_velocity_data(data_cls: type, source: wp.array, warp_launch: WarpLaunchCache, device: str): + """Build the fields used by a data class' lazy root-link velocity property.""" + num_envs = source.shape[0] + root_pose = np.zeros((num_envs, 7), dtype=np.float32) + root_pose[:, 6] = 1.0 + + data = object.__new__(data_cls) + data.device = device + data._warp_launch = warp_launch + data._num_instances = num_envs + data._sim_timestamp = 1.0 + data._root_com_vel_w_ta = ProxyArray(source) + data._root_link_pose_w_ta = ProxyArray(wp.array(root_pose, dtype=wp.transformf, device=device)) + data._body_com_pos_b_ta = ProxyArray(wp.zeros((num_envs, 1), dtype=wp.vec3f, device=device)) + data._sim_bind_body_com_pos_b = data._body_com_pos_b_ta.warp + data._root_link_vel_w = TimestampedBuffer( + shape=(num_envs,), + dtype=wp.spatial_vectorf, + device=device, + ) + data._root_link_vel_w_ta = ProxyArray(data._root_link_vel_w.data) + return data + + +@pytest.mark.parametrize("data_cls", [ArticulationData, RigidObjectData]) +def test_lazy_derived_replay_refreshes_stale_timestamp_and_rebound_source(data_cls: type, cuda_device: str): + """Replay should refresh stale data, while invalidation should admit a rebound source pointer.""" + cache = WarpLaunchCache(mode="replay", debug=True, device=cuda_device) + source = _spatial_vectors(8, 1.0, cuda_device) + data = _make_root_velocity_data(data_cls, source, cache, cuda_device) + + first = data.root_link_vel_w + wp.synchronize_device(cuda_device) + np.testing.assert_allclose(first.warp.numpy(), source.numpy()) + + source.fill_(2.0) + data._sim_timestamp = 2.0 + second = data.root_link_vel_w + wp.synchronize_device(cuda_device) + np.testing.assert_allclose(second.warp.numpy(), source.numpy()) + + rebound_source = _spatial_vectors(8, 3.0, cuda_device) + data._root_com_vel_w_ta = ProxyArray(rebound_source) + cache.invalidate() + data._sim_timestamp = 3.0 + third = data.root_link_vel_w + wp.synchronize_device(cuda_device) + np.testing.assert_allclose(third.warp.numpy(), rebound_source.numpy()) + + +@pytest.mark.parametrize("data_cls", [ArticulationData, RigidObjectData]) +def test_simulation_rebind_delegates_cache_drain(data_cls: type, monkeypatch: pytest.MonkeyPatch): + """Simulation rebinding should delegate conditional synchronization to the cache.""" + events: list[tuple[str, str | None]] = [] + data = object.__new__(data_cls) + data.device = "cuda:0" + data._warp_launch = _ResetProbe(events) + monkeypatch.setattr(wp, "synchronize_device", lambda device: pytest.fail(f"unexpected direct sync on {device}")) + + with pytest.raises(_RebindReset): + data._create_simulation_bindings() + + assert events == [("reset", None)] + + +def test_adapter_teardown_delegates_cache_drain(monkeypatch: pytest.MonkeyPatch): + """Adapter teardown should delegate conditional synchronization to the cache.""" + events: list[tuple[str, str | None]] = [] + adapter = object.__new__(NewtonActuatorAdapter) + adapter._device = "cuda:0" + adapter._warp_launch = _ResetProbe(events) + monkeypatch.setattr(wp, "synchronize_device", lambda device: pytest.fail(f"unexpected direct sync on {device}")) + + with pytest.raises(_RebindReset): + adapter._invalidate_launch_cache() + + assert events == [("reset", None)] + + +def test_newton_close_releases_graph_before_asset_stop_callbacks(monkeypatch: pytest.MonkeyPatch): + """Newton shutdown should release graphs and drain work before asset caches are invalidated.""" + events: list[tuple[str, object]] = [] + graph = object() + saved_graph = NewtonManager._graph + saved_capture_pending = NewtonManager._graph_capture_pending + saved_device = PhysicsManager._device + try: + NewtonManager._graph = graph + NewtonManager._graph_capture_pending = True + PhysicsManager._device = "cuda:0" + monkeypatch.setattr( + wp, + "synchronize_device", + lambda device: events.append(("synchronize", device)), + ) + monkeypatch.setattr( + PhysicsManager, + "close", + classmethod(lambda cls: events.append(("stop", NewtonManager._graph))), + ) + monkeypatch.setattr( + NewtonManager, + "clear", + classmethod(lambda cls: events.append(("clear", NewtonManager._graph))), + ) + + NewtonManager.close() + + assert NewtonManager._graph is None + assert NewtonManager._graph_capture_pending is False + assert events == [("synchronize", "cuda:0"), ("stop", None), ("clear", None)] + finally: + NewtonManager._graph = saved_graph + NewtonManager._graph_capture_pending = saved_capture_pending + PhysicsManager._device = saved_device + + +@pytest.mark.parametrize("use_cache", [False, True]) +def test_adapter_step_zeros_current_effort_with_eager_or_replay(use_cache: bool, cuda_device: str): + """Adapter zeroing should preserve eager behavior and replay correctly when enabled.""" + indices = wp.array([1, 3], dtype=wp.uint32, device=cuda_device) + actuator = _NoOpActuator(indices) + warp_launch = WarpLaunchCache(mode="replay" if use_cache else "eager", debug=True, device=cuda_device) + adapter = NewtonActuatorAdapter( + [actuator], + num_envs=1, + num_joints=4, + dof_offset=0, + device=cuda_device, + warp_launch=warp_launch, + ) + control = SimpleNamespace(joint_f=wp.full(4, 7.0, dtype=wp.float32, device=cuda_device)) + + adapter.step(None, control, 0.01) + wp.synchronize_device(cuda_device) + np.testing.assert_allclose(control.joint_f.numpy(), [7.0, 0.0, 7.0, 0.0]) + + control.joint_f.fill_(9.0) + adapter.step(None, control, 0.01) + wp.synchronize_device(cuda_device) + np.testing.assert_allclose(control.joint_f.numpy(), [9.0, 0.0, 9.0, 0.0]) + + assert bool(warp_launch._entries) is use_cache + adapter._invalidate_launch_cache() + assert not warp_launch._entries + + +@pytest.mark.parametrize("use_cache", [False, True]) +def test_newton_manager_flag_selects_adapter_launch_mode(use_cache: bool, cuda_device: str): + """The Newton manager should share an eager or replay launcher with its adapter.""" + had_active_flag = hasattr(NewtonManager, "_use_newton_actuators_active") + saved_state = { + "adapter": NewtonManager._adapter, + "warp_launch": NewtonManager._warp_launch, + "control": NewtonManager._control, + "device": PhysicsManager._device, + "cfg": PhysicsManager._cfg, + "model": NewtonManager._model, + "num_envs": NewtonManager._num_envs, + "use_newton_actuators_active": getattr(NewtonManager, "_use_newton_actuators_active", False), + } + try: + actuator = _NoOpActuator(wp.array([1, 3], dtype=wp.uint32, device=cuda_device)) + PhysicsManager._cfg = SimpleNamespace(use_warp_launch_cache=use_cache) + PhysicsManager._device = cuda_device + NewtonManager._model = SimpleNamespace(actuators=[actuator], joint_dof_count=4) + NewtonManager._num_envs = 1 + NewtonManager._control = SimpleNamespace() + NewtonManager._adapter = None + NewtonManager._use_newton_actuators_active = False + + NewtonManager.activate_newton_actuator_path() + + assert NewtonManager._adapter is not None + assert NewtonManager._adapter._warp_launch is NewtonManager._warp_launch + assert NewtonManager._warp_launch._mode == ("auto" if use_cache else "eager") + finally: + if NewtonManager._adapter is not None: + NewtonManager._adapter._invalidate_launch_cache() + NewtonManager._adapter = saved_state["adapter"] + NewtonManager._warp_launch = saved_state["warp_launch"] + NewtonManager._control = saved_state["control"] + PhysicsManager._device = saved_state["device"] + PhysicsManager._cfg = saved_state["cfg"] + NewtonManager._model = saved_state["model"] + NewtonManager._num_envs = saved_state["num_envs"] + if had_active_flag: + NewtonManager._use_newton_actuators_active = saved_state["use_newton_actuators_active"] + else: + del NewtonManager._use_newton_actuators_active diff --git a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rl_games.py b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rl_games.py index 7b0e4baff460..eb035bcd0f64 100644 --- a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rl_games.py +++ b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rl_games.py @@ -26,7 +26,12 @@ from isaaclab.utils.dict import print_dict from isaaclab.utils.seed import configure_seed -from isaaclab_rl.entrypoints.common import CHECKPOINT_SELECTORS, resolve_checkpoint_selector +from isaaclab_rl.entrypoints.common import ( + CHECKPOINT_SELECTORS, + add_frontend_args, + create_isaaclab_env, + resolve_checkpoint_selector, +) from isaaclab_rl.rl_games import RlGamesGpuEnv, RlGamesVecEnvWrapper from isaaclab_rl.utils.pretrained_checkpoint import get_published_pretrained_checkpoint @@ -50,6 +55,7 @@ ) parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.") parser.add_argument("--task", type=str, default=None, help="Name of the task.") +add_frontend_args(parser) parser.add_argument( "--agent", type=str, default="rl_games_cfg_entry_point", help="Name of the RL agent configuration entry point." ) @@ -126,12 +132,12 @@ def main(): obs_groups = agent_cfg["params"]["env"].get("obs_groups") concate_obs_groups = agent_cfg["params"]["env"].get("concate_obs_groups", True) - env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None) - - if isinstance(env.unwrapped.cfg, DirectMARLEnvCfg): - from isaaclab.envs import multi_agent_to_single_agent - - env = multi_agent_to_single_agent(env) + env = create_isaaclab_env( + args_cli.task, + env_cfg, + args_cli, + convert_marl_to_single_agent=isinstance(env_cfg, DirectMARLEnvCfg), + ) if args_cli.video: video_kwargs = { diff --git a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rsl_rl.py b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rsl_rl.py index 966010a3bc9f..7b3a1143b969 100644 --- a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rsl_rl.py +++ b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_rsl_rl.py @@ -25,7 +25,12 @@ from isaaclab.utils.string import list_intersection, string_to_callable from isaaclab_rl.entrypoints.backends import cli_args_rsl_rl as cli_args -from isaaclab_rl.entrypoints.common import CHECKPOINT_SELECTORS, resolve_checkpoint_selector +from isaaclab_rl.entrypoints.common import ( + CHECKPOINT_SELECTORS, + add_frontend_args, + create_isaaclab_env, + resolve_checkpoint_selector, +) from isaaclab_rl.rsl_rl import ( RslRlBaseRunnerCfg, RslRlVecEnvWrapper, @@ -55,6 +60,7 @@ ) parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.") parser.add_argument("--task", type=str, default=None, help="Name of the task.") +add_frontend_args(parser) parser.add_argument( "--agent", type=str, default="rsl_rl_cfg_entry_point", help="Name of the RL agent configuration entry point." ) @@ -130,12 +136,12 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen env_cfg.log_dir = log_dir - env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None) - - if isinstance(env.unwrapped.cfg, DirectMARLEnvCfg): - from isaaclab.envs import multi_agent_to_single_agent - - env = multi_agent_to_single_agent(env) + env = create_isaaclab_env( + args_cli.task, + env_cfg, + args_cli, + convert_marl_to_single_agent=isinstance(env_cfg, DirectMARLEnvCfg), + ) if args_cli.video: video_kwargs = { diff --git a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_sb3.py b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_sb3.py index 3bef5d69cb1a..00f949bd4a66 100644 --- a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_sb3.py +++ b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_sb3.py @@ -23,7 +23,12 @@ from isaaclab.utils.dict import print_dict from isaaclab.utils.seed import configure_seed -from isaaclab_rl.entrypoints.common import CHECKPOINT_SELECTORS, resolve_checkpoint_selector +from isaaclab_rl.entrypoints.common import ( + CHECKPOINT_SELECTORS, + add_frontend_args, + create_isaaclab_env, + resolve_checkpoint_selector, +) from isaaclab_rl.sb3 import Sb3VecEnvWrapper, process_sb3_cfg from isaaclab_rl.utils.pretrained_checkpoint import get_published_pretrained_checkpoint @@ -47,6 +52,7 @@ ) parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.") parser.add_argument("--task", type=str, default=None, help="Name of the task.") +add_frontend_args(parser) parser.add_argument( "--agent", type=str, default="sb3_cfg_entry_point", help="Name of the RL agent configuration entry point." ) @@ -116,15 +122,15 @@ def main(): env_cfg.log_dir = log_dir - env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None) + env = create_isaaclab_env( + args_cli.task, + env_cfg, + args_cli, + convert_marl_to_single_agent=isinstance(env_cfg, DirectMARLEnvCfg), + ) agent_cfg = process_sb3_cfg(agent_cfg, env.unwrapped.num_envs) - if isinstance(env.unwrapped.cfg, DirectMARLEnvCfg): - from isaaclab.envs import multi_agent_to_single_agent - - env = multi_agent_to_single_agent(env) - if args_cli.video: video_kwargs = { "video_folder": os.path.join(log_dir, "videos", "play"), diff --git a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_skrl.py b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_skrl.py index cd566c13f23e..6654f28d2364 100644 --- a/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_skrl.py +++ b/source/isaaclab_rl/isaaclab_rl/entrypoints/backends/play_skrl.py @@ -27,7 +27,12 @@ from isaaclab.utils.dict import print_dict from isaaclab.utils.seed import configure_seed -from isaaclab_rl.entrypoints.common import CHECKPOINT_SELECTORS, resolve_checkpoint_selector +from isaaclab_rl.entrypoints.common import ( + CHECKPOINT_SELECTORS, + add_frontend_args, + create_isaaclab_env, + resolve_checkpoint_selector, +) from isaaclab_rl.utils.pretrained_checkpoint import get_published_pretrained_checkpoint import isaaclab_tasks # noqa: F401 @@ -52,6 +57,7 @@ ) parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.") parser.add_argument("--task", type=str, default=None, help="Name of the task.") +add_frontend_args(parser) parser.add_argument( "--agent", type=str, @@ -166,12 +172,12 @@ def main(): env_cfg.log_dir = log_dir - env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None) - - if isinstance(env.unwrapped.cfg, DirectMARLEnvCfg) and algorithm in ["ppo"]: - from isaaclab.envs import multi_agent_to_single_agent - - env = multi_agent_to_single_agent(env) + env = create_isaaclab_env( + args_cli.task, + env_cfg, + args_cli, + convert_marl_to_single_agent=isinstance(env_cfg, DirectMARLEnvCfg) and algorithm in ["ppo"], + ) try: dt = env.step_dt diff --git a/source/isaaclab_rl/isaaclab_rl/entrypoints/common.py b/source/isaaclab_rl/isaaclab_rl/entrypoints/common.py index a46d1f540400..eed6286410ec 100644 --- a/source/isaaclab_rl/isaaclab_rl/entrypoints/common.py +++ b/source/isaaclab_rl/isaaclab_rl/entrypoints/common.py @@ -233,6 +233,30 @@ def resolve_play_checkpoint(checkpoint: str | None, framework: str, task: str) - return path +def add_frontend_args(parser: argparse.ArgumentParser) -> None: + """Add the environment-runtime selector argument. + + The flag is always registered so the CLI surface does not depend on + optional packages; the warp runtime itself is imported only when selected + (see :func:`create_isaaclab_env`). + + Args: + parser: The parser to add the argument to. + """ + parser.add_argument( + "--frontend", + type=str, + choices=["torch", "warp"], + default="torch", + help=( + "Runtime that constructs the environment. 'torch' uses the registered stable environment via" + " gym.make. 'warp' (experimental) adapts a manager-based task config onto the Warp runtime, or" + " dispatches a direct task to its registered Warp environment; requires isaaclab_experimental" + " and `presets=newton_mjwarp`." + ), + ) + + def add_common_train_args( parser: argparse.ArgumentParser, *, @@ -257,6 +281,7 @@ def add_common_train_args( ) parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.") parser.add_argument("--task", type=str, default=None, help="Name of the task.") + add_frontend_args(parser) if include_agent: parser.add_argument("--agent", type=str, default=agent_default, help=agent_help) parser.add_argument("--seed", type=int, default=None, help="Seed used for the environment") @@ -405,7 +430,15 @@ def create_isaaclab_env( Returns: The created Gymnasium environment. """ - env = gym.make(task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None) + render_mode = "rgb_array" if args_cli.video else None + if args_cli.frontend == "torch": + env = gym.make(task, cfg=env_cfg, render_mode=render_mode) + else: + # Imported lazily: the warp frontend lives in the optional + # isaaclab_experimental package, and the torch path must work without it. + from isaaclab_experimental.envs.frontend import WarpFrontend + + env = WarpFrontend.build_env(env_cfg, task, render_mode=render_mode) if convert_marl_to_single_agent and isinstance(env.unwrapped.cfg, DirectMARLEnvCfg): from isaaclab.envs import multi_agent_to_single_agent diff --git a/source/isaaclab_rl/test/test_entrypoints_common.py b/source/isaaclab_rl/test/test_entrypoints_common.py index 415bb56e27c6..6b7ec1a1ef60 100644 --- a/source/isaaclab_rl/test/test_entrypoints_common.py +++ b/source/isaaclab_rl/test/test_entrypoints_common.py @@ -20,6 +20,7 @@ from isaaclab_rl.entrypoints.common import ( CaptureEnvSensors, add_common_train_args, + create_isaaclab_env, dispatch_library_entrypoint, enable_cameras_for_video, wrap_sensor_capture, @@ -204,6 +205,80 @@ def test_enable_cameras_for_video_enables_cameras_for_sensor_capture() -> None: assert args_cli.enable_cameras +def test_common_train_args_register_frontend_with_torch_default() -> None: + """Every RL library CLI exposes ``--frontend`` and defaults to the torch runtime.""" + parser = argparse.ArgumentParser() + add_common_train_args(parser, agent_default=None, agent_help="", include_agent=False) + + assert parser.parse_args([]).frontend == "torch" + assert parser.parse_args(["--frontend", "warp"]).frontend == "warp" + with pytest.raises(SystemExit): + parser.parse_args(["--frontend", "tensorflow"]) + + +def _repo_root() -> Path: + return Path(__file__).resolve().parents[3] + + +def test_play_entrypoints_route_through_frontend_factory() -> None: + """Every dispatched play backend constructs its env via the frontend-aware factory.""" + play_scripts = sorted( + (_repo_root() / "source" / "isaaclab_rl" / "isaaclab_rl" / "entrypoints" / "backends").glob("play_*.py") + ) + # rlinf constructs environments inside the external framework; the frontend cannot + # reach it (documented limitation). + play_scripts = [path for path in play_scripts if path.name != "play_rlinf.py"] + assert len(play_scripts) == 4, sorted(path.name for path in play_scripts) + for script in play_scripts: + source = script.read_text() + assert "create_isaaclab_env(" in source, f"{script.name} bypasses the frontend factory" + assert "gym.make(args_cli.task" not in source, f"{script.name} constructs directly via gym.make" + assert "add_frontend_args(parser)" in source, f"{script.name} does not expose --frontend" + + +def test_create_isaaclab_env_uses_registered_torch_env_by_default(monkeypatch: pytest.MonkeyPatch) -> None: + """The shared factory preserves the existing Gym path when no frontend is selected.""" + expected_env = object() + env_cfg = object() + calls: list[tuple[Any, ...]] = [] + + def fake_make(task: str, **kwargs: Any) -> Any: + calls.append((task, kwargs)) + return expected_env + + monkeypatch.setattr(_rl_common.gym, "make", fake_make) + args_cli = argparse.Namespace(video=False, frontend="torch") + + env = create_isaaclab_env("Isaac-Test", env_cfg, args_cli, convert_marl_to_single_agent=False) + + assert env is expected_env + assert len(calls) == 1 + assert calls[0][0] == "Isaac-Test" + assert calls[0][1]["cfg"] is env_cfg + assert calls[0][1]["render_mode"] is None + + +def test_create_isaaclab_env_uses_selected_warp_frontend(monkeypatch: pytest.MonkeyPatch) -> None: + """The shared factory delegates Warp selection to the experimental frontend.""" + import isaaclab_experimental.envs.frontend as frontend_module + + expected_env = object() + env_cfg = object() + calls: list[tuple[Any, ...]] = [] + + def fake_build_env(cfg: Any, task: str, **kwargs: Any) -> Any: + calls.append((cfg, task, kwargs)) + return expected_env + + monkeypatch.setattr(frontend_module.WarpFrontend, "build_env", fake_build_env) + args_cli = argparse.Namespace(video=True, frontend="warp") + + env = create_isaaclab_env("Isaac-Test", env_cfg, args_cli, convert_marl_to_single_agent=False) + + assert env is expected_env + assert calls == [(env_cfg, "Isaac-Test", {"render_mode": "rgb_array"})] + + def test_dispatch_library_entrypoint_shows_help_without_library( tmp_path: Path, capsys: pytest.CaptureFixture[str] ) -> None: diff --git a/source/isaaclab_tasks/changelog.d/warp-manager-bridge.rst b/source/isaaclab_tasks/changelog.d/warp-manager-bridge.rst new file mode 100644 index 000000000000..7a9bdd89d0b5 --- /dev/null +++ b/source/isaaclab_tasks/changelog.d/warp-manager-bridge.rst @@ -0,0 +1,8 @@ +Added +^^^^^ + +* Added ``warp_entry_point`` declarations to the ``Isaac-Cartpole-Direct``, + ``Isaac-Ant-Direct``, ``Isaac-Humanoid-Direct``, and + ``Isaac-Reorient-Cube-Allegro-Direct`` registrations so ``--frontend warp`` + constructs the Warp implementation of these tasks with the stable task + configuration. diff --git a/source/isaaclab_tasks/isaaclab_tasks/core/cartpole/__init__.py b/source/isaaclab_tasks/isaaclab_tasks/core/cartpole/__init__.py index bdb55030b3b2..cc8bff65c70a 100644 --- a/source/isaaclab_tasks/isaaclab_tasks/core/cartpole/__init__.py +++ b/source/isaaclab_tasks/isaaclab_tasks/core/cartpole/__init__.py @@ -25,6 +25,7 @@ disable_env_checker=True, kwargs={ "env_cfg_entry_point": f"{__name__}.cartpole_direct_env_cfg:CartpoleEnvCfg", + "warp_entry_point": "isaaclab_tasks_experimental.core.cartpole.cartpole_warp_env:CartpoleWarpEnv", "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_direct_ppo_cfg.yaml", "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:CartpoleDirectPPORunnerCfg", "skrl_cfg_entry_point": f"{agents.__name__}:skrl_direct_ppo_cfg.yaml", diff --git a/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/ant/__init__.py b/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/ant/__init__.py index 4adb37ca8eb6..1e506e5ef4ae 100644 --- a/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/ant/__init__.py +++ b/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/ant/__init__.py @@ -25,6 +25,7 @@ disable_env_checker=True, kwargs={ "env_cfg_entry_point": f"{__name__}.ant_direct_env_cfg:AntEnvCfg", + "warp_entry_point": "isaaclab_tasks_experimental.core.locomotion.ant.ant_env_warp:AntWarpEnv", "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_direct_ppo_cfg.yaml", "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AntDirectPPORunnerCfg", "skrl_cfg_entry_point": f"{agents.__name__}:skrl_direct_ppo_cfg.yaml", diff --git a/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/humanoid/__init__.py b/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/humanoid/__init__.py index d13b7697595a..e3cee56589b2 100644 --- a/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/humanoid/__init__.py +++ b/source/isaaclab_tasks/isaaclab_tasks/core/locomotion/humanoid/__init__.py @@ -25,6 +25,7 @@ disable_env_checker=True, kwargs={ "env_cfg_entry_point": f"{__name__}.humanoid_direct_env_cfg:HumanoidEnvCfg", + "warp_entry_point": "isaaclab_tasks_experimental.core.locomotion.humanoid.humanoid_warp_env:HumanoidWarpEnv", "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_direct_ppo_cfg.yaml", "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:HumanoidDirectPPORunnerCfg", "skrl_cfg_entry_point": f"{agents.__name__}:skrl_direct_ppo_cfg.yaml", diff --git a/source/isaaclab_tasks/isaaclab_tasks/core/reorient/config/allegro_hand/__init__.py b/source/isaaclab_tasks/isaaclab_tasks/core/reorient/config/allegro_hand/__init__.py index c5ec71b59cfe..35393451e9e1 100644 --- a/source/isaaclab_tasks/isaaclab_tasks/core/reorient/config/allegro_hand/__init__.py +++ b/source/isaaclab_tasks/isaaclab_tasks/core/reorient/config/allegro_hand/__init__.py @@ -47,6 +47,9 @@ disable_env_checker=True, kwargs={ "env_cfg_entry_point": f"{__name__}.allegro_hand_direct_env_cfg:AllegroHandEnvCfg", + "warp_entry_point": ( + "isaaclab_tasks_experimental.core.reorient.inhand_manipulation_warp_env:InHandManipulationWarpEnv" + ), "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_direct_ppo_cfg.yaml", "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AllegroHandPPORunnerCfg", "skrl_cfg_entry_point": f"{agents.__name__}:skrl_direct_ppo_cfg.yaml", diff --git a/source/isaaclab_tasks_experimental/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst b/source/isaaclab_tasks_experimental/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst new file mode 100644 index 000000000000..6cfc728745b0 --- /dev/null +++ b/source/isaaclab_tasks_experimental/changelog.d/jichuanh-warp-frontend-cleanup.minor.rst @@ -0,0 +1,11 @@ +Added +^^^^^ + +* Added Warp-native terrain and reward-weight curricula and connected locomotion + and reach hot paths to pointer-stable Warp command arrays. + +Fixed +^^^^^ + +* Fixed terrain-boundary termination state leaking between environment + instances with different terrain configurations. diff --git a/source/isaaclab_tasks_experimental/changelog.d/jichuanh-warp-frontend-execution.minor.rst b/source/isaaclab_tasks_experimental/changelog.d/jichuanh-warp-frontend-execution.minor.rst new file mode 100644 index 000000000000..9acad30a6750 --- /dev/null +++ b/source/isaaclab_tasks_experimental/changelog.d/jichuanh-warp-frontend-execution.minor.rst @@ -0,0 +1,5 @@ +Added +^^^^^ + +* Added persistent Warp state and recorded launch replay to the experimental + direct and manager-based Warp tasks. diff --git a/source/isaaclab_tasks_experimental/changelog.d/warp-manager-bridge.minor.rst b/source/isaaclab_tasks_experimental/changelog.d/warp-manager-bridge.minor.rst new file mode 100644 index 000000000000..f29f10f3e6ab --- /dev/null +++ b/source/isaaclab_tasks_experimental/changelog.d/warp-manager-bridge.minor.rst @@ -0,0 +1,39 @@ +Changed +^^^^^^^ + +* **Breaking:** Changed the package layout to mirror :mod:`isaaclab_tasks.core` + (``isaaclab_tasks_experimental.core.``), replacing the previous + ``manager_based``/``direct`` split. Update imports of the old paths to the + ``core`` equivalents (e.g. ``isaaclab_tasks_experimental.core.cartpole.mdp``). + +Removed +^^^^^^^ + +* **Breaking:** Removed all manager-based ``*-Warp-v0`` task registrations — + ``Isaac-Cartpole-Warp-v0``, ``Isaac-Humanoid-Warp-v0``, ``Isaac-Ant-Warp-v0``, + ``Isaac-Reach-Franka-Warp-v0``, ``Isaac-Reach-Franka-Warp-Play-v0``, and the + velocity variants (``Isaac-Velocity-Flat--Warp-v0`` and their + ``-Warp-Play-v0`` forms) — together with their environment configurations. + Run the stable task ids with ``--frontend warp`` and + ``presets=newton_mjwarp`` instead, e.g. + ``--task Isaac-Velocity-Flat-AnymalD --frontend warp presets=newton_mjwarp``. +* **Breaking:** Removed the duplicated direct warp task registrations + ``Isaac-Cartpole-Direct-Warp-v0``, ``Isaac-Ant-Direct-Warp-v0``, + ``Isaac-Humanoid-Direct-Warp-v0``, and + ``Isaac-Reorient-Cube-Allegro-Direct-Warp-v0`` together with their + duplicated environment configurations; the stable registrations declare the + warp environment classes via ``warp_entry_point``. Run the stable task ids + with ``--frontend warp`` and ``presets=newton_mjwarp`` instead, e.g. + ``--task Isaac-Cartpole-Direct --frontend warp presets=newton_mjwarp``. +* Removed the unregistered rough velocity warp configurations; rough-terrain + warp tasks remain unsupported until :class:`~isaaclab.terrains.TerrainImporter` + gains Warp APIs. + +Fixed +^^^^^ + +* Fixed the direct Warp Cartpole task to match the stable task's observations, + reset ranges, termination condition, reward scaling, and scene configuration. +* Fixed the Warp Cartpole ``survival_success_rate`` twin to report the + ``Metrics/success_rate`` value on-device instead of silently dropping the + metric. diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/__init__.py similarity index 68% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/__init__.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/__init__.py index 0857176a3fc7..bf495309161f 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/__init__.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/__init__.py @@ -3,4 +3,4 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Velocity locomotion experimental task registrations (manager-based).""" +"""Warp-first task implementations mirroring the :mod:`isaaclab_tasks.core` layout.""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/__init__.py new file mode 100644 index 000000000000..e8bc71c28dc3 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/__init__.py @@ -0,0 +1,12 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp implementations for the stable Cartpole tasks. + +Holds the MDP twins for the manager-based ``Isaac-Cartpole`` task and the +direct :class:`~isaaclab_tasks_experimental.core.cartpole.cartpole_warp_env.CartpoleWarpEnv` +declared by ``Isaac-Cartpole-Direct``. There is no separate task registration: +run the stable ids with ``--frontend warp`` and ``presets=newton_mjwarp``. +""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/cartpole_warp_env.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/cartpole_warp_env.py similarity index 69% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/cartpole_warp_env.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/cartpole_warp_env.py index 14f776c95b0b..dc88b3d9b154 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/cartpole_warp_env.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/cartpole_warp_env.py @@ -5,11 +5,11 @@ from __future__ import annotations -import math from typing import TYPE_CHECKING import warp as wp from isaaclab_experimental.envs import DirectRLEnvWarp +from isaaclab_experimental.utils.warp.utils import wrap_to_pi import isaaclab.sim as sim_utils from isaaclab import cloner @@ -17,22 +17,24 @@ from isaaclab.sim.spawners.from_files import GroundPlaneCfg, spawn_ground_plane if TYPE_CHECKING: - from .cartpole_warp_env_cfg import CartpoleWarpEnvCfg + from isaaclab_tasks.core.cartpole.cartpole_direct_env_cfg import CartpoleEnvCfg @wp.kernel def get_observations( joint_pos: wp.array2d(dtype=wp.float32), joint_vel: wp.array2d(dtype=wp.float32), + default_joint_pos: wp.array2d(dtype=wp.float32), + default_joint_vel: wp.array2d(dtype=wp.float32), cart_dof_idx: wp.int32, pole_dof_idx: wp.int32, observations: wp.array(dtype=wp.vec4f), ): env_index = wp.tid() - observations[env_index][0] = joint_pos[env_index, pole_dof_idx] - observations[env_index][1] = joint_vel[env_index, pole_dof_idx] - observations[env_index][2] = joint_pos[env_index, cart_dof_idx] - observations[env_index][3] = joint_vel[env_index, cart_dof_idx] + observations[env_index][0] = joint_pos[env_index, cart_dof_idx] - default_joint_pos[env_index, cart_dof_idx] + observations[env_index][1] = joint_pos[env_index, pole_dof_idx] - default_joint_pos[env_index, pole_dof_idx] + observations[env_index][2] = joint_vel[env_index, cart_dof_idx] - default_joint_vel[env_index, cart_dof_idx] + observations[env_index][3] = joint_vel[env_index, pole_dof_idx] - default_joint_vel[env_index, pole_dof_idx] @wp.kernel @@ -51,7 +53,6 @@ def get_dones( joint_pos: wp.array2d(dtype=wp.float32), episode_length_buf: wp.array(dtype=wp.int32), cart_dof_idx: wp.int32, - pole_dof_idx: wp.int32, max_episode_length: wp.int32, max_cart_pos: wp.float32, out_of_bounds: wp.array(dtype=wp.bool), @@ -59,10 +60,8 @@ def get_dones( reset: wp.array(dtype=wp.bool), ): env_index = wp.tid() - out_of_bounds[env_index] = (wp.abs(joint_pos[env_index, cart_dof_idx]) > max_cart_pos) or ( - wp.abs(joint_pos[env_index, pole_dof_idx]) > math.pi / 2.0 - ) - time_out[env_index] = episode_length_buf[env_index] >= (max_episode_length - 1) + out_of_bounds[env_index] = wp.abs(joint_pos[env_index, cart_dof_idx]) > max_cart_pos + time_out[env_index] = episode_length_buf[env_index] >= max_episode_length reset[env_index] = out_of_bounds[env_index] or time_out[env_index] @@ -85,6 +84,7 @@ def compute_rew_pole_pos( rew_scale_pole_pos: wp.float32, pole_pos: wp.float32, ) -> wp.float32: + pole_pos = wrap_to_pi(pole_pos) return rew_scale_pole_pos * pole_pos * pole_pos @@ -116,10 +116,11 @@ def compute_rewards( cart_dof_idx: wp.int32, pole_dof_idx: wp.int32, reset_terminated: wp.array(dtype=wp.bool), + step_dt: wp.float32, reward: wp.array(dtype=wp.float32), ): env_index = wp.tid() - reward[env_index] = ( + reward[env_index] = step_dt * ( compute_rew_alive(rew_scale_alive, reset_terminated[env_index]) + compute_rew_termination(rew_scale_terminated, reset_terminated[env_index]) + compute_rew_pole_pos(rew_scale_pole_pos, joint_pos[env_index, pole_dof_idx]) @@ -134,21 +135,43 @@ def reset( default_joint_vel: wp.array2d(dtype=wp.float32), joint_pos: wp.array2d(dtype=wp.float32), joint_vel: wp.array2d(dtype=wp.float32), + soft_joint_pos_limits: wp.array2d(dtype=wp.vec2f), + soft_joint_vel_limits: wp.array2d(dtype=wp.float32), cart_dof_idx: wp.int32, pole_dof_idx: wp.int32, - initial_pose_angle_range: wp.vec2f, + initial_cart_position_range: wp.vec2f, + initial_cart_velocity_range: wp.vec2f, + initial_pole_angle_range: wp.vec2f, + initial_pole_velocity_range: wp.vec2f, env_mask: wp.array(dtype=wp.bool), state: wp.array(dtype=wp.uint32), ): env_index = wp.tid() if env_mask[env_index]: - joint_pos[env_index, cart_dof_idx] = default_joint_pos[env_index, cart_dof_idx] - joint_pos[env_index, pole_dof_idx] = default_joint_pos[env_index, pole_dof_idx] + wp.randf( - state[env_index], initial_pose_angle_range[0] * wp.pi, initial_pose_angle_range[1] * wp.pi + rng_state = state[env_index] + cart_pos = default_joint_pos[env_index, cart_dof_idx] + wp.randf( + rng_state, initial_cart_position_range[0], initial_cart_position_range[1] + ) + pole_pos = default_joint_pos[env_index, pole_dof_idx] + wp.randf( + rng_state, initial_pole_angle_range[0], initial_pole_angle_range[1] ) - joint_vel[env_index, cart_dof_idx] = default_joint_vel[env_index, cart_dof_idx] - joint_vel[env_index, pole_dof_idx] = default_joint_vel[env_index, pole_dof_idx] - state[env_index] += wp.uint32(1) + cart_vel = default_joint_vel[env_index, cart_dof_idx] + wp.randf( + rng_state, initial_cart_velocity_range[0], initial_cart_velocity_range[1] + ) + pole_vel = default_joint_vel[env_index, pole_dof_idx] + wp.randf( + rng_state, initial_pole_velocity_range[0], initial_pole_velocity_range[1] + ) + + cart_pos_limits = soft_joint_pos_limits[env_index, cart_dof_idx] + pole_pos_limits = soft_joint_pos_limits[env_index, pole_dof_idx] + cart_vel_limit = soft_joint_vel_limits[env_index, cart_dof_idx] + pole_vel_limit = soft_joint_vel_limits[env_index, pole_dof_idx] + + joint_pos[env_index, cart_dof_idx] = wp.clamp(cart_pos, cart_pos_limits[0], cart_pos_limits[1]) + joint_pos[env_index, pole_dof_idx] = wp.clamp(pole_pos, pole_pos_limits[0], pole_pos_limits[1]) + joint_vel[env_index, cart_dof_idx] = wp.clamp(cart_vel, -cart_vel_limit, cart_vel_limit) + joint_vel[env_index, pole_dof_idx] = wp.clamp(pole_vel, -pole_vel_limit, pole_vel_limit) + state[env_index] = rng_state @wp.kernel @@ -161,9 +184,9 @@ def initialize_state( class CartpoleWarpEnv(DirectRLEnvWarp): - cfg: CartpoleWarpEnvCfg + cfg: CartpoleEnvCfg - def __init__(self, cfg: CartpoleWarpEnvCfg, render_mode: str | None = None, **kwargs) -> None: + def __init__(self, cfg: CartpoleEnvCfg, render_mode: str | None = None, **kwargs) -> None: super().__init__(cfg, render_mode, **kwargs) # Get the indices (develop API: find_joints returns (indices, names)) @@ -179,6 +202,7 @@ def __init__(self, cfg: CartpoleWarpEnvCfg, render_mode: str | None = None, **kw # Buffers self.observations = wp.zeros((self.num_envs), dtype=wp.vec4f, device=self.device) + self._input_actions = wp.zeros((self.num_envs, 1), dtype=wp.float32, device=self.device) self.actions = wp.zeros((self.num_envs, self.cartpole.num_joints), dtype=wp.float32, device=self.device) self.rewards = wp.zeros((self.num_envs), dtype=wp.float32, device=self.device) self.states = wp.zeros((self.num_envs), dtype=wp.uint32, device=self.device) @@ -216,31 +240,36 @@ def _setup_scene(self) -> None: # add articulation to scene self.scene.articulations["cartpole"] = self.cartpole # add lights - light_cfg = sim_utils.DomeLightCfg(intensity=2000.0, color=(0.75, 0.75, 0.75)) - light_cfg.func("/World/Light", light_cfg) + light_cfg = sim_utils.DistantLightCfg(intensity=2000.0, color=(1.0, 1.0, 1.0)) + light_orientation = (-0.14644663035869598, -0.3535534143447876, -0.3535534143447876, 0.8535533547401428) + light_cfg.func("/World/Light", light_cfg, orientation=light_orientation) def _pre_physics_step(self, actions: wp.array) -> None: - wp.launch( + wp.copy(self._input_actions, actions) + self._warp_launch.launch( update_actions, dim=self.num_envs, inputs=[ - actions, + self._input_actions, self.actions, self.action_scale, self._cart_dof_idx[0], ], + site=("cartpole", "update_actions"), ) def _apply_action(self) -> None: self.cartpole.set_joint_effort_target_mask(target=self.actions) def _get_observations(self) -> dict: - wp.launch( + self._warp_launch.launch( get_observations, dim=self.num_envs, inputs=[ self.joint_pos, self.joint_vel, + self.cartpole.data.default_joint_pos.warp, + self.cartpole.data.default_joint_vel.warp, self._cart_dof_idx[0], self._pole_dof_idx[0], self.observations, @@ -249,7 +278,7 @@ def _get_observations(self) -> dict: return {"policy": self.torch_obs_buf} def _get_rewards(self) -> None: - wp.launch( + self._warp_launch.launch( compute_rewards, dim=self.num_envs, inputs=[ @@ -263,19 +292,19 @@ def _get_rewards(self) -> None: self._cart_dof_idx[0], self._pole_dof_idx[0], self.reset_terminated, + self.step_dt, self.rewards, ], ) def _get_dones(self) -> None: - wp.launch( + self._warp_launch.launch( get_dones, dim=self.num_envs, inputs=[ self.joint_pos, self._episode_length_buf_wp, self._cart_dof_idx[0], - self._pole_dof_idx[0], self.max_episode_length, self.cfg.max_cart_pos, self.reset_terminated, @@ -290,7 +319,7 @@ def _reset_idx(self, mask: wp.array | None = None) -> None: super()._reset_idx(mask) - wp.launch( + self._warp_launch.launch( reset, dim=self.num_envs, inputs=[ @@ -298,10 +327,16 @@ def _reset_idx(self, mask: wp.array | None = None) -> None: self.cartpole.data.default_joint_vel.warp, self.joint_pos, self.joint_vel, + self.cartpole.data.soft_joint_pos_limits.warp, + self.cartpole.data.soft_joint_vel_limits.warp, self._cart_dof_idx[0], self._pole_dof_idx[0], + self.cfg.initial_cart_position_range, + self.cfg.initial_cart_velocity_range, self.cfg.initial_pole_angle_range, + self.cfg.initial_pole_velocity_range, mask, self.states, ], + site=mask, ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/__init__.py similarity index 100% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/__init__.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/__init__.py diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/__init__.pyi b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/__init__.pyi similarity index 82% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/__init__.pyi rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/__init__.pyi index 377068405c79..657186c2539b 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/__init__.pyi +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/__init__.pyi @@ -5,10 +5,11 @@ __all__ = [ "joint_pos_target_l2", + "survival_success_rate", ] # Forward stable MDP terms and experimental Warp-first overrides lazily, then # override with cartpole-specific terms below. from isaaclab_experimental.envs.mdp import * # noqa: F401, F403 -from .rewards import joint_pos_target_l2 +from .rewards import joint_pos_target_l2, survival_success_rate diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/rewards.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/rewards.py new file mode 100644 index 000000000000..6a1ace8d1354 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/cartpole/mdp/rewards.py @@ -0,0 +1,114 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import warp as wp +from isaaclab_experimental.managers import ManagerTermBase, SceneEntityCfg +from isaaclab_experimental.utils.warp.utils import wrap_to_pi + +if TYPE_CHECKING: + import torch + + from isaaclab.assets import Articulation + from isaaclab.envs import ManagerBasedRLEnv + from isaaclab.managers import RewardTermCfg + + +@wp.kernel +def _joint_pos_target_l2_kernel( + joint_pos: wp.array(dtype=wp.float32, ndim=2), + joint_mask: wp.array(dtype=wp.bool), + out: wp.array(dtype=wp.float32), + target: float, +): + i = wp.tid() + s = float(0.0) + for j in range(joint_pos.shape[1]): + if joint_mask[j]: + a = wrap_to_pi(joint_pos[i, j]) + d = a - target + s += d * d + out[i] = s + + +def joint_pos_target_l2(env: ManagerBasedRLEnv, out, target: float, asset_cfg: SceneEntityCfg) -> None: + """Penalize joint position deviation from a target value. Writes into ``out``.""" + asset: Articulation = env.scene[asset_cfg.name] + assert asset.data.joint_pos.warp.shape[1] == asset_cfg.joint_mask.shape[0] + target = float(target) + env._warp_launch.launch( + _joint_pos_target_l2_kernel, + dim=env.num_envs, + inputs=[asset.data.joint_pos.warp, asset_cfg.joint_mask, out, target], + site=(out, target), + ) + + +@wp.kernel +def _survival_counts_kernel( + env_mask: wp.array(dtype=wp.bool), + time_outs: wp.array(dtype=wp.bool), + counts: wp.array(dtype=wp.int32), +): + env_index = wp.tid() + if env_mask[env_index]: + wp.atomic_add(counts, 0, 1) + if time_outs[env_index]: + wp.atomic_add(counts, 1, 1) + + +@wp.kernel +def _survival_rate_kernel( + counts: wp.array(dtype=wp.int32), + success_rate: wp.array(dtype=wp.float32), +): + if counts[0] > 0: + success_rate[0] = wp.float32(counts[1]) / wp.float32(counts[0]) + else: + success_rate[0] = 0.0 + + +class survival_success_rate(ManagerTermBase): + """Tracks episode survival as the success metric (Warp-first). + + Twin of :class:`isaaclab_tasks.core.cartpole.mdp.rewards.survival_success_rate`. + Returns zero reward (pure metric tracking). On reset, computes the fraction of + just-reset environments that timed out (survived the full episode) entirely + on-device and exposes it as ``Metrics/success_rate`` through the reward + manager's reset extras. Unlike the stable term, there is no host readback, so + the computation stays CUDA-graph capturable. + """ + + def __init__(self, cfg: RewardTermCfg, env: ManagerBasedRLEnv): + super().__init__(cfg, env) + # [0] number of just-reset envs, [1] number of those that timed out + self._counts_wp = wp.zeros((2,), dtype=wp.int32, device=env.device) + self._success_rate_wp = wp.zeros((1,), dtype=wp.float32, device=env.device) + # persistent 0-dim tensor view: kernels refresh the value on every reset/replay + self._reset_extras = {"Metrics/success_rate": wp.to_torch(self._success_rate_wp)[0]} + + def reset(self, env_mask: wp.array | None = None) -> dict[str, torch.Tensor]: + if env_mask is None: + env_mask = self._env.resolve_env_mask() + self._counts_wp.zero_() + wp.launch( + kernel=_survival_counts_kernel, + dim=self.num_envs, + inputs=[env_mask, self._env.termination_manager.time_outs_wp, self._counts_wp], + device=self.device, + ) + wp.launch( + kernel=_survival_rate_kernel, + dim=1, + inputs=[self._counts_wp, self._success_rate_wp], + device=self.device, + ) + return self._reset_extras + + def __call__(self, env: ManagerBasedRLEnv, out: wp.array) -> None: + out.zero_() diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/__init__.py new file mode 100644 index 000000000000..3fba47b723b6 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/__init__.py @@ -0,0 +1,12 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp implementations for the stable locomotion tasks. + +The ``mdp`` package holds the twins for the shared +:mod:`isaaclab_tasks.core.locomotion.mdp` terms used by the Humanoid and Ant +tasks. There is no separate task registration: run the stable ids with +``--frontend warp`` and ``presets=newton_mjwarp``. +""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/ant/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/ant/__init__.py new file mode 100644 index 000000000000..1be981faf7f0 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/ant/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp implementation of the stable direct Ant task. + +The manager-based MDP twins live in the shared +:mod:`isaaclab_tasks_experimental.core.locomotion.mdp` package (mirroring the +stable layout). The stable ``Isaac-Ant-Direct`` registration declares +:class:`~isaaclab_tasks_experimental.core.locomotion.ant.ant_env_warp.AntWarpEnv` +as its ``warp_entry_point``; run the stable ids with ``--frontend warp`` and +``presets=newton_mjwarp``. +""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/ant_env_warp.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/ant/ant_env_warp.py similarity index 54% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/ant_env_warp.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/ant/ant_env_warp.py index 2bd7390039b7..7d8957e4d9c7 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/ant_env_warp.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/ant/ant_env_warp.py @@ -5,13 +5,13 @@ from __future__ import annotations -from isaaclab_tasks_experimental.direct.locomotion.locomotion_env_warp import LocomotionWarpEnv +from isaaclab_tasks.core.locomotion.ant.ant_direct_env_cfg import AntEnvCfg -from .ant_env_warp_cfg import AntWarpEnvCfg +from isaaclab_tasks_experimental.core.locomotion.locomotion_env_warp import LocomotionWarpEnv class AntWarpEnv(LocomotionWarpEnv): - cfg: AntWarpEnvCfg + cfg: AntEnvCfg - def __init__(self, cfg: AntWarpEnvCfg, render_mode: str | None = None, **kwargs): + def __init__(self, cfg: AntEnvCfg, render_mode: str | None = None, **kwargs): super().__init__(cfg, render_mode, **kwargs) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/humanoid/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/humanoid/__init__.py new file mode 100644 index 000000000000..b87d773f7b6d --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/humanoid/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp implementation of the stable direct Humanoid task. + +The manager-based MDP twins live in the shared +:mod:`isaaclab_tasks_experimental.core.locomotion.mdp` package (mirroring the +stable layout). The stable ``Isaac-Humanoid-Direct`` registration declares +:class:`~isaaclab_tasks_experimental.core.locomotion.humanoid.humanoid_warp_env.HumanoidWarpEnv` +as its ``warp_entry_point``; run the stable ids with ``--frontend warp`` and +``presets=newton_mjwarp``. +""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/humanoid_warp_env.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/humanoid/humanoid_warp_env.py similarity index 52% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/humanoid_warp_env.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/humanoid/humanoid_warp_env.py index 2fd0ac8433e2..2a843510717a 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/humanoid_warp_env.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/humanoid/humanoid_warp_env.py @@ -5,13 +5,13 @@ from __future__ import annotations -from isaaclab_tasks_experimental.direct.locomotion.locomotion_env_warp import LocomotionWarpEnv +from isaaclab_tasks.core.locomotion.humanoid.humanoid_direct_env_cfg import HumanoidEnvCfg -from .humanoid_warp_env_cfg import HumanoidWarpEnvCfg +from isaaclab_tasks_experimental.core.locomotion.locomotion_env_warp import LocomotionWarpEnv class HumanoidWarpEnv(LocomotionWarpEnv): - cfg: HumanoidWarpEnvCfg + cfg: HumanoidEnvCfg - def __init__(self, cfg: HumanoidWarpEnvCfg, render_mode: str | None = None, **kwargs): + def __init__(self, cfg: HumanoidEnvCfg, render_mode: str | None = None, **kwargs): super().__init__(cfg, render_mode, **kwargs) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/locomotion/locomotion_env_warp.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/locomotion_env_warp.py similarity index 97% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/locomotion/locomotion_env_warp.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/locomotion_env_warp.py index 62400c4d48fe..e9bdb92c34e6 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/locomotion/locomotion_env_warp.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/locomotion_env_warp.py @@ -385,7 +385,7 @@ def __init__(self, cfg: DirectRLEnvCfg, render_mode: str | None = None, **kwargs self.rpy = wp.zeros((self.num_envs), dtype=wp.vec3f, device=self.sim.device) self.angle_to_target = wp.zeros((self.num_envs), dtype=wp.float32, device=self.sim.device) self.dof_pos_scaled = wp.zeros((self.num_envs, self.robot.num_joints), dtype=wp.float32, device=self.sim.device) - self.env_origins = wp.from_torch(self.scene.env_origins, dtype=wp.vec3f) + self.env_origins = self.scene.env_origins_wp self.actions_mapped = wp.zeros((self.num_envs, self.robot.num_joints), dtype=wp.float32, device=self.sim.device) # Initial states and targets @@ -428,17 +428,17 @@ def _setup_scene(self) -> None: def _pre_physics_step(self, actions: wp.array) -> None: self.actions.assign(actions) - wp.launch( + self._warp_launch.launch( update_actions, dim=(self.num_envs, self.robot.num_joints), - inputs=[actions, self.actions_mapped, self.joint_gears, self.action_scale], + inputs=[self.actions, self.actions_mapped, self.joint_gears, self.action_scale], ) def _apply_action(self) -> None: self.robot.set_joint_effort_target_mask(target=self.actions_mapped) def _compute_intermediate_values(self) -> None: - wp.launch( + self._warp_launch.launch( compute_heading_and_up, dim=self.num_envs, inputs=[ @@ -455,7 +455,7 @@ def _compute_intermediate_values(self) -> None: ], ) - wp.launch( + self._warp_launch.launch( compute_rot, dim=self.num_envs, inputs=[ @@ -467,7 +467,7 @@ def _compute_intermediate_values(self) -> None: self.angle_to_target, ], ) - wp.launch( + self._warp_launch.launch( scale_dof_pos, dim=(self.num_envs, self.robot.num_joints), inputs=[ @@ -478,7 +478,7 @@ def _compute_intermediate_values(self) -> None: ) def _get_observations(self) -> dict: - wp.launch( + self._warp_launch.launch( observations, dim=self.num_envs, inputs=[ @@ -500,7 +500,7 @@ def _get_observations(self) -> dict: return {"policy": self.torch_obs_buf} def _get_rewards(self) -> None: - wp.launch( + self._warp_launch.launch( compute_rewards, dim=self.num_envs, inputs=[ @@ -527,7 +527,7 @@ def _get_rewards(self) -> None: def _get_dones(self) -> None: self._compute_intermediate_values() - wp.launch( + self._warp_launch.launch( get_dones, dim=self.num_envs, inputs=[ @@ -547,7 +547,7 @@ def _reset_idx(self, mask: wp.array | None = None): super()._reset_idx(mask) - wp.launch( + self._warp_launch.launch( reset_root, dim=self.num_envs, inputs=[ @@ -562,8 +562,9 @@ def _reset_idx(self, mask: wp.array | None = None): self.root_vel_w, mask, ], + site=mask, ) - wp.launch( + self._warp_launch.launch( reset_joints, dim=(self.num_envs, self.robot.num_joints), inputs=[ @@ -573,6 +574,7 @@ def _reset_idx(self, mask: wp.array | None = None): self.joint_vel, mask, ], + site=mask, ) self._compute_intermediate_values() diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/__init__.py similarity index 100% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/__init__.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/__init__.py diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/__init__.pyi b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/__init__.pyi similarity index 100% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/__init__.pyi rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/__init__.pyi diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/observations.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/observations.py similarity index 94% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/observations.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/observations.py index 4d9ab39b4ff7..9488110ab48d 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/observations.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/observations.py @@ -51,11 +51,11 @@ def _base_yaw_roll_kernel( def base_yaw_roll(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Yaw and roll of the base in the simulation world frame. Shape: (num_envs, 2).""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_yaw_roll_kernel, + env._warp_launch.launch( + _base_yaw_roll_kernel, dim=env.num_envs, inputs=[asset.data.root_quat_w.warp, out], - device=env.device, + site=out, ) @@ -79,11 +79,11 @@ def _base_up_proj_kernel( def base_up_proj(env: ManagerBasedEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> None: """Projection of the base up vector onto the world up vector. Shape: (num_envs, 1).""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_up_proj_kernel, + env._warp_launch.launch( + _base_up_proj_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.GRAVITY_VEC_W.warp, out], - device=env.device, + site=out, ) @@ -124,8 +124,8 @@ def base_heading_proj( ) -> None: """Dot product between the base forward direction and direction to target. Shape: (num_envs, 1).""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_heading_proj_kernel, + env._warp_launch.launch( + _base_heading_proj_kernel, dim=env.num_envs, inputs=[ asset.data.root_pos_w.warp, @@ -135,7 +135,7 @@ def base_heading_proj( target_pos[2], out, ], - device=env.device, + site=(out, tuple(target_pos)), ) @@ -177,9 +177,9 @@ def base_angle_to_target( ) -> None: """Angle between the base forward vector and the vector to the target. Shape: (num_envs, 1).""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_base_angle_to_target_kernel, + env._warp_launch.launch( + _base_angle_to_target_kernel, dim=env.num_envs, inputs=[asset.data.root_pos_w.warp, asset.data.root_quat_w.warp, target_pos[0], target_pos[1], out], - device=env.device, + site=(out, tuple(target_pos)), ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/rewards.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/rewards.py similarity index 93% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/rewards.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/rewards.py index a090d3a16f69..b1829e415608 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/mdp/rewards.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/locomotion/mdp/rewards.py @@ -57,11 +57,11 @@ def upright_posture_bonus( ) -> None: """Reward for maintaining an upright posture. Writes 1.0 if up_proj > threshold, else 0.0.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_upright_posture_bonus_kernel, + env._warp_launch.launch( + _upright_posture_bonus_kernel, dim=env.num_envs, inputs=[asset.data.root_link_pose_w.warp, asset.data.GRAVITY_VEC_W.warp, threshold, out], - device=env.device, + site=(out, float(threshold)), ) @@ -99,8 +99,8 @@ def move_to_target_bonus( ) -> None: """Reward for heading towards the target.""" asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_move_to_target_bonus_kernel, + env._warp_launch.launch( + _move_to_target_bonus_kernel, dim=env.num_envs, inputs=[ asset.data.root_pos_w.warp, @@ -110,7 +110,7 @@ def move_to_target_bonus( threshold, out, ], - device=env.device, + site=(out, float(threshold), tuple(target_pos)), ) @@ -174,8 +174,8 @@ def reset(self, env_mask: wp.array | None = None) -> None: return asset: Articulation = self._env.scene["robot"] inv_dt = 1.0 / self._env.step_dt - wp.launch( - kernel=_progress_reward_reset_kernel, + self._env._warp_launch.launch( + _progress_reward_reset_kernel, dim=self.num_envs, inputs=[ env_mask, @@ -186,7 +186,7 @@ def reset(self, env_mask: wp.array | None = None) -> None: inv_dt, self.potentials, ], - device=self.device, + site=(self, env_mask, tuple(self._target_pos), float(inv_dt)), ) def __call__( @@ -198,11 +198,11 @@ def __call__( ) -> None: asset: Articulation = env.scene[asset_cfg.name] inv_dt = 1.0 / env.step_dt - wp.launch( - kernel=_progress_reward_kernel, + env._warp_launch.launch( + _progress_reward_kernel, dim=env.num_envs, inputs=[asset.data.root_pos_w.warp, target_pos[0], target_pos[1], inv_dt, self.potentials, out], - device=env.device, + site=(self, out, tuple(target_pos), float(inv_dt)), ) @@ -259,8 +259,8 @@ def __call__( asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), ) -> None: asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_joint_pos_limits_penalty_ratio_kernel, + env._warp_launch.launch( + _joint_pos_limits_penalty_ratio_kernel, dim=env.num_envs, inputs=[ asset.data.joint_pos.warp, @@ -270,7 +270,7 @@ def __call__( 1.0 / (1.0 - threshold), out, ], - device=env.device, + site=(self, out, float(threshold)), ) @@ -313,9 +313,9 @@ def __call__( asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), ) -> None: asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_power_consumption_kernel, + env._warp_launch.launch( + _power_consumption_kernel, dim=env.num_envs, inputs=[env.action_manager.action, asset.data.joint_vel.warp, self._gear_ratio_scaled_wp, out], - device=env.device, + site=(self, out), ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/__init__.py new file mode 100644 index 000000000000..1a9be132962a --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/__init__.py @@ -0,0 +1,12 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp MDP twins for the stable Reach tasks. + +There is no separate task registration: run the stable joint-position reach +tasks (``Isaac-Reach-Franka``, ``Isaac-Reach-UR10``) with ``--frontend warp`` +and ``presets=newton_mjwarp``. The IK and OSC variants require controller +action twins that do not exist yet. +""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/__init__.py similarity index 100% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/__init__.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/__init__.py diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/__init__.pyi b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/__init__.pyi similarity index 88% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/__init__.pyi rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/__init__.pyi index c277ec98e076..0fe12a032cc9 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/__init__.pyi +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/__init__.pyi @@ -4,6 +4,7 @@ # SPDX-License-Identifier: BSD-3-Clause __all__ = [ + "modify_reward_weight", "position_command_error", "position_command_error_tanh", "orientation_command_error", @@ -13,4 +14,5 @@ __all__ = [ # override with reach-specific terms below. from isaaclab_experimental.envs.mdp import * # noqa: F401, F403 +from .curriculums import modify_reward_weight from .rewards import orientation_command_error, position_command_error, position_command_error_tanh diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/curriculums.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/curriculums.py new file mode 100644 index 000000000000..f3f278d03974 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/curriculums.py @@ -0,0 +1,97 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp-first curriculum terms for reach environments.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import warp as wp +from isaaclab_experimental.managers import CurriculumTermCfg, ManagerTermBase + +if TYPE_CHECKING: + from isaaclab.envs import ManagerBasedRLEnv + + +@wp.kernel +def _mark_reset_requested(env_mask: wp.array(dtype=wp.bool), reset_requested: wp.array(dtype=wp.int32)): + env_id = wp.tid() + if env_mask[env_id]: + wp.atomic_max(reset_requested, 0, 1) + + +@wp.kernel +def _update_reward_weight( + reset_requested: wp.array(dtype=wp.int32), + common_step_counter: wp.array(dtype=wp.int32), + num_steps: int, + target_weight: float, + term_weight: wp.array(dtype=wp.float32), + out: wp.array(dtype=wp.float32), +): + if reset_requested[0] != 0: + if common_step_counter[0] > num_steps: + term_weight[0] = target_weight + out[0] = term_weight[0] + reset_requested[0] = 0 + + +class modify_reward_weight(ManagerTermBase): + """Update one reward weight after a reset reaches the configured step threshold.""" + + def __init__(self, cfg: CurriculumTermCfg, env: ManagerBasedRLEnv): + """Initialize persistent device state. + + Args: + cfg: Curriculum term configuration. + env: Environment containing the reward manager. + """ + super().__init__(cfg, env) + self._term_weight_wp = env.reward_manager.get_term_weight_wp(cfg.params["term_name"]) + self._global_env_step_count_wp = env._global_env_step_count_wp + self._reset_requested_wp = wp.zeros(1, dtype=wp.int32, device=self.device) + + def __call__( + self, + env: ManagerBasedRLEnv, + env_mask: wp.array(dtype=wp.bool), + out: wp.array(dtype=wp.float32), + term_name: str, + weight: float, + num_steps: int, + ) -> None: + """Update the reward weight when at least one environment resets. + + Args: + env: Environment containing the persistent launch owner. + env_mask: Boolean mask selecting resetting environments. + out: Persistent one-element output containing the current weight. + term_name: Reward term name, resolved during initialization. + weight: Target reward weight. + num_steps: Step threshold after which the target weight is applied. + """ + del term_name + weight = float(weight) + num_steps = int(num_steps) + env._warp_launch.launch( + kernel=_mark_reset_requested, + dim=self.num_envs, + inputs=[env_mask, self._reset_requested_wp], + site=(self, "mark_reset", env_mask), + ) + env._warp_launch.launch( + kernel=_update_reward_weight, + dim=1, + inputs=[ + self._reset_requested_wp, + self._global_env_step_count_wp, + num_steps, + weight, + self._term_weight_wp, + out, + ], + site=(self, "update_weight", out, num_steps, weight), + ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/rewards.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/rewards.py similarity index 77% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/rewards.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/rewards.py index 7435752528d6..8c32ef8c8357 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/mdp/rewards.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reach/mdp/rewards.py @@ -6,7 +6,6 @@ """Warp-first reward terms for the reach task. All functions follow the ``func(env, out, **params) -> None`` signature. -Command tensors are cached as zero-copy warp views on first call. """ from __future__ import annotations @@ -50,24 +49,20 @@ def _position_command_error_kernel( def position_command_error(env: ManagerBasedRLEnv, out, command_name: str, asset_cfg: SceneEntityCfg) -> None: """Penalize tracking of the position error using L2-norm.""" - fn = position_command_error asset: Articulation = env.scene[asset_cfg.name] - if not hasattr(fn, "_cmd_wp") or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - wp.launch( - kernel=_position_command_error_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _position_command_error_kernel, dim=env.num_envs, inputs=[ asset.data.root_pos_w.warp, asset.data.root_quat_w.warp, asset.data.body_pos_w.warp, - fn._cmd_wp, + command, asset_cfg.body_ids[0], out, ], - device=env.device, + site=(out, command_name, int(asset_cfg.body_ids[0])), ) @@ -101,25 +96,21 @@ def position_command_error_tanh( env: ManagerBasedRLEnv, out, std: float, command_name: str, asset_cfg: SceneEntityCfg ) -> None: """Reward tracking of the position using the tanh kernel.""" - fn = position_command_error_tanh asset: Articulation = env.scene[asset_cfg.name] - if not hasattr(fn, "_cmd_wp") or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - wp.launch( - kernel=_position_command_error_tanh_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _position_command_error_tanh_kernel, dim=env.num_envs, inputs=[ asset.data.root_pos_w.warp, asset.data.root_quat_w.warp, asset.data.body_pos_w.warp, - fn._cmd_wp, + command, asset_cfg.body_ids[0], 1.0 / std, out, ], - device=env.device, + site=(out, command_name, int(asset_cfg.body_ids[0]), float(std)), ) @@ -152,15 +143,17 @@ def _orientation_command_error_kernel( def orientation_command_error(env: ManagerBasedRLEnv, out, command_name: str, asset_cfg: SceneEntityCfg) -> None: """Penalize tracking orientation error using shortest path.""" - fn = orientation_command_error asset: Articulation = env.scene[asset_cfg.name] - if not hasattr(fn, "_cmd_wp") or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - wp.launch( - kernel=_orientation_command_error_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _orientation_command_error_kernel, dim=env.num_envs, - inputs=[asset.data.root_quat_w.warp, asset.data.body_quat_w.warp, fn._cmd_wp, asset_cfg.body_ids[0], out], - device=env.device, + inputs=[ + asset.data.root_quat_w.warp, + asset.data.body_quat_w.warp, + command, + asset_cfg.body_ids[0], + out, + ], + site=(out, command_name, int(asset_cfg.body_ids[0])), ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reorient/__init__.py similarity index 75% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/__init__.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reorient/__init__.py index 6cd56351b6e5..48bb97b31113 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/__init__.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reorient/__init__.py @@ -3,4 +3,4 @@ # # SPDX-License-Identifier: BSD-3-Clause -from .reach import * +"""Warp-first in-hand reorientation task implementations.""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/inhand_manipulation/inhand_manipulation_warp_env.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reorient/inhand_manipulation_warp_env.py similarity index 95% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/inhand_manipulation/inhand_manipulation_warp_env.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reorient/inhand_manipulation_warp_env.py index 2082d470ff9b..1765e0e6003f 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/inhand_manipulation/inhand_manipulation_warp_env.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/reorient/inhand_manipulation_warp_env.py @@ -6,7 +6,6 @@ from __future__ import annotations -from collections.abc import Sequence from typing import TYPE_CHECKING import torch @@ -20,7 +19,7 @@ from isaaclab.sim.spawners.from_files import GroundPlaneCfg, spawn_ground_plane if TYPE_CHECKING: - from isaaclab_tasks_experimental.direct.allegro_hand.allegro_hand_warp_env_cfg import AllegroHandWarpEnvCfg + from isaaclab_tasks.core.reorient.config.allegro_hand.allegro_hand_direct_env_cfg import AllegroHandEnvCfg @wp.kernel @@ -538,10 +537,10 @@ def rotation_distance(object_rot: wp.quatf, target_rot: wp.quatf) -> wp.float32: class InHandManipulationWarpEnv(DirectRLEnvWarp): - cfg: AllegroHandWarpEnvCfg # | ShadowHandWarpEnvCfg + cfg: AllegroHandEnvCfg # | ShadowHandEnvCfg - # def __init__(self, cfg: AllegroHandWarpEnvCfg | ShadowHandWarpEnvCfg, render_mode: str | None = None, **kwargs): - def __init__(self, cfg: AllegroHandWarpEnvCfg, render_mode: str | None = None, **kwargs): + # def __init__(self, cfg: AllegroHandEnvCfg | ShadowHandEnvCfg, render_mode: str | None = None, **kwargs): + def __init__(self, cfg: AllegroHandEnvCfg, render_mode: str | None = None, **kwargs): super().__init__(cfg, render_mode, **kwargs) # --------------------------------------------------------------------- @@ -582,8 +581,7 @@ def __init__(self, cfg: AllegroHandWarpEnvCfg, render_mode: str | None = None, * self.y_unit_vec = wp.vec3f(0.0, 1.0, 0.0) self.z_unit_vec = wp.vec3f(0.0, 0.0, 1.0) - # Per-env origins (Warp view for kernels; Torch env uses `self.scene.env_origins` directly). - self.env_origins = wp.from_torch(self.scene.env_origins, dtype=wp.vec3f) + self.env_origins = self.scene.env_origins_wp # --------------------------------------------------------------------- # Warp buffers @@ -667,6 +665,7 @@ def __init__(self, cfg: AllegroHandWarpEnvCfg, render_mode: str | None = None, * self.torch_reset_terminated = wp.to_torch(self.reset_terminated) self.torch_reset_time_outs = wp.to_torch(self.reset_time_outs) self.torch_episode_length_buf = self.episode_length_buf # already a torch tensor via wp.to_torch + self.torch_consecutive_successes = wp.to_torch(self.consecutive_successes) def _setup_scene(self): # add hand, in-hand object, and goal object @@ -690,7 +689,7 @@ def _pre_physics_step(self, actions: wp.array) -> None: wp.copy(self.actions, actions) def _apply_action(self) -> None: - wp.launch( + self._warp_launch.launch( apply_actions_to_targets, dim=(self.num_envs, self.num_actuated_dofs), inputs=[ @@ -702,7 +701,6 @@ def _apply_action(self) -> None: self.prev_targets, self.cur_targets, ], - device=self.device, ) # Apply position targets using mask method (CUDA graph safe). @@ -728,7 +726,7 @@ def _get_rewards(self) -> None: # wp.assign(self._finished_cons_successes, 0.0) self._num_resets.zero_() self._finished_cons_successes.zero_() - wp.launch( + self._warp_launch.launch( compute_rewards, dim=self.num_envs, inputs=[ @@ -752,11 +750,10 @@ def _get_rewards(self) -> None: self._finished_cons_successes, self.rewards, ], - device=self.device, ) # A separate kernel is needed as Warp does not support thread synchronization for reductions. - wp.launch( + self._warp_launch.launch( update_consecutive_successes_from_stats, dim=1, inputs=[ @@ -765,13 +762,12 @@ def _get_rewards(self) -> None: self.cfg.av_factor, self.consecutive_successes, ], - device=self.device, ) if "log" not in self.extras: self.extras["log"] = dict() # .mean() cannot be called here as it causes problems on stream - self.extras["log"]["consecutive_successes"] = wp.to_torch(self.consecutive_successes) + self.extras["log"]["consecutive_successes"] = self.torch_consecutive_successes # Reset goals for envs that reached the target (mask is `reset_goal_buf`). # This avoids Torch-side index extraction and keeps the step graphable. @@ -780,7 +776,7 @@ def _get_rewards(self) -> None: def _get_dones(self) -> None: self._compute_intermediate_values() - wp.launch( + self._warp_launch.launch( get_dones, dim=self.num_envs, inputs=[ @@ -797,7 +793,6 @@ def _get_dones(self) -> None: self.reset_time_outs, self.reset_buf, ], - device=self.device, ) def _reset_idx(self, mask: wp.array | None = None): @@ -811,7 +806,7 @@ def _reset_idx(self, mask: wp.array | None = None): self._reset_target_pose(mask=mask) # reset object - wp.launch( + self._warp_launch.launch( reset_object, dim=self.num_envs, inputs=[ @@ -825,11 +820,11 @@ def _reset_idx(self, mask: wp.array | None = None): self.object.data.root_link_pose_w.warp, self.object.data.root_com_vel_w.warp, ], - device=self.device, + site=mask, ) # reset hand - wp.launch( + self._warp_launch.launch( reset_hand, dim=self.num_envs, inputs=[ @@ -848,35 +843,28 @@ def _reset_idx(self, mask: wp.array | None = None): self.cur_targets, self.hand_dof_targets, ], - device=self.device, + site=mask, ) self.hand.set_joint_position_target_mask(target=self.cur_targets, env_mask=mask) - wp.launch( + self._warp_launch.launch( reset_successes, dim=self.num_envs, inputs=[ mask, self.successes, ], - device=self.device, + site=mask, ) self._compute_intermediate_values() - def _reset_target_pose(self, env_ids: Sequence[int] | None = None, mask: wp.array | None = None): + def _reset_target_pose(self, mask: wp.array(dtype=wp.bool)) -> None: + """Reset target poses for environments selected by a persistent mask.""" # reset goal rotation - if mask is None: - if env_ids is None: - return - env_mask_list = [False] * self.num_envs - for env_id in env_ids: - env_mask_list[int(env_id)] = True - mask = wp.array(env_mask_list, dtype=wp.bool, device=self.device) - # update goal pose and markers - wp.launch( + self._warp_launch.launch( reset_target_pose, dim=self.num_envs, inputs=[ @@ -890,7 +878,7 @@ def _reset_target_pose(self, env_ids: Sequence[int] | None = None, mask: wp.arra self.reset_goal_buf, self.goal_pos_w, ], - device=self.device, + site=mask, ) def _post_step_visualize(self) -> None: @@ -899,7 +887,7 @@ def _post_step_visualize(self) -> None: def _compute_intermediate_values(self): # data for hand/object (Warp version of the Torch env's `_compute_intermediate_values`) - wp.launch( + self._warp_launch.launch( compute_intermediate_values, dim=self.num_envs, inputs=[ @@ -917,7 +905,6 @@ def _compute_intermediate_values(self): self.object_pose, self.object_vels, ], - device=self.device, ) def compute_reduced_observations(self): @@ -925,7 +912,7 @@ def compute_reduced_observations(self): # Fingertip positions # Object Position, but not orientation # Relative target orientation - wp.launch( + self._warp_launch.launch( compute_reduced_observations, dim=self.num_envs, inputs=[ @@ -937,19 +924,17 @@ def compute_reduced_observations(self): self.cfg.action_space, self.observations, ], - device=self.device, ) # Warp-native non-finite sanitization + print-once. - wp.launch( + self._warp_launch.launch( sanitize_and_print_once, dim=(self.num_envs * self.cfg.observation_space), inputs=[self.observations.flatten(), self.obs_nonfinite_flag], - device=self.device, ) self.obs_nonfinite_flag.zero_() def compute_full_observations(self): - wp.launch( + self._warp_launch.launch( compute_full_observations, dim=self.num_envs, inputs=[ @@ -971,13 +956,11 @@ def compute_full_observations(self): self.cfg.action_space, self.observations, ], - device=self.device, ) # Warp-native non-finite sanitization + print-once. - wp.launch( + self._warp_launch.launch( sanitize_and_print_once, dim=(self.num_envs * self.cfg.observation_space), inputs=[self.observations.flatten(), self.obs_nonfinite_flag], - device=self.device, ) self.obs_nonfinite_flag.zero_() diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/__init__.py new file mode 100644 index 000000000000..f6510ad92c91 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp MDP twins for the stable velocity locomotion tasks. + +There is no separate task registration: run the stable flat velocity ids +(e.g. ``Isaac-Velocity-Flat-AnymalD``) with ``--frontend warp`` and +``presets=newton_mjwarp``. Rough-terrain variants remain unsupported until +:class:`~isaaclab.terrains.TerrainImporter` gains Warp APIs (no +``height_scan`` twin). +""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/__init__.py similarity index 100% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/__init__.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/__init__.py diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/__init__.pyi b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/__init__.pyi similarity index 97% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/__init__.pyi rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/__init__.pyi index ae4b6d7af547..e76bfc598bff 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/__init__.pyi +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/__init__.pyi @@ -5,7 +5,7 @@ __all__ = [ "terrain_levels_vel", - "feet_air_time", + "feet_air_time", "feet_air_time_positive_biped", "feet_slide", "stand_still_joint_deviation_l1", diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/curriculums.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/curriculums.py new file mode 100644 index 000000000000..456c4ec20a25 --- /dev/null +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/curriculums.py @@ -0,0 +1,127 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Warp-first curriculum terms for velocity locomotion environments.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import warp as wp +from isaaclab_experimental.managers import CurriculumTermCfg, ManagerTermBase, SceneEntityCfg + +if TYPE_CHECKING: + from isaaclab.assets import Articulation + from isaaclab.envs import ManagerBasedRLEnv + from isaaclab.terrains import TerrainImporter + + +@wp.kernel +def _compute_terrain_level_updates( + env_mask: wp.array(dtype=wp.bool), + root_pos_w: wp.array(dtype=wp.vec3f), + env_origins: wp.array(dtype=wp.vec3f), + command: wp.array(dtype=wp.float32, ndim=2), + terrain_half_length: float, + max_episode_length_s: float, + move_up: wp.array(dtype=wp.bool), + move_down: wp.array(dtype=wp.bool), +): + env_id = wp.tid() + if env_mask[env_id]: + position_delta = root_pos_w[env_id] - env_origins[env_id] + distance = wp.sqrt(position_delta[0] * position_delta[0] + position_delta[1] * position_delta[1]) + command_distance = ( + wp.sqrt(command[env_id, 0] * command[env_id, 0] + command[env_id, 1] * command[env_id, 1]) + * max_episode_length_s + * 0.5 + ) + should_move_up = distance > terrain_half_length + move_up[env_id] = should_move_up + move_down[env_id] = (distance < command_distance) and (not should_move_up) + else: + move_up[env_id] = False + move_down[env_id] = False + + +@wp.kernel +def _compute_terrain_level_mean( + terrain_levels: wp.array(dtype=wp.int64), + scale: float, + out: wp.array(dtype=wp.float32), +): + env_id = wp.tid() + wp.atomic_add(out, 0, wp.float32(terrain_levels[env_id]) * scale) + + +class terrain_levels_vel(ManagerTermBase): + """Update terrain levels from commanded locomotion progress using a boolean environment mask.""" + + _curriculum_mask_native = True + + def __init__(self, cfg: CurriculumTermCfg, env: ManagerBasedRLEnv): + """Initialize persistent views and decision buffers. + + Args: + cfg: Curriculum term configuration. + env: Environment containing the robot and terrain. + """ + super().__init__(cfg, env) + asset_cfg = cfg.params.get("asset_cfg", SceneEntityCfg("robot")) + self._asset: Articulation = env.scene[asset_cfg.name] + self._terrain: TerrainImporter = env.scene.terrain + self._root_pos_w_wp = self._asset.data.root_pos_w.warp + self._env_origins_wp = env.env_origins_wp + self._command_wp = env.command_manager.get_command_wp("base_velocity") + self._terrain_levels_wp = self._terrain.terrain_levels_wp + self._move_up_wp = wp.zeros(self.num_envs, dtype=wp.bool, device=self.device) + self._move_down_wp = wp.zeros(self.num_envs, dtype=wp.bool, device=self.device) + self._terrain_half_length = float(self._terrain.cfg.terrain_generator.size[0]) * 0.5 + self._max_episode_length_s = float(env.max_episode_length_s) + self._mean_scale = 1.0 / self.num_envs + + def __call__( + self, + env: ManagerBasedRLEnv, + env_mask: wp.array(dtype=wp.bool), + out: wp.array(dtype=wp.float32), + asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), + ) -> None: + """Update selected terrain levels and write the global mean level. + + Args: + env: Environment containing the per-environment random state. + env_mask: Boolean Warp mask selecting environments to update. + out: Persistent scalar output for the mean terrain level. + asset_cfg: Robot scene entity configuration. + """ + del asset_cfg + env._warp_launch.launch( + kernel=_compute_terrain_level_updates, + dim=self.num_envs, + inputs=[ + env_mask, + self._root_pos_w_wp, + self._env_origins_wp, + self._command_wp, + self._terrain_half_length, + self._max_episode_length_s, + self._move_up_wp, + self._move_down_wp, + ], + site=(self, "updates", env_mask), + ) + self._terrain.update_env_origins_mask( + env_mask, + self._move_up_wp, + self._move_down_wp, + env.rng_state_wp, + ) + env._warp_launch.launch( + kernel=_compute_terrain_level_mean, + dim=self.num_envs, + inputs=[self._terrain_levels_wp, self._mean_scale, out], + site=(self, "mean", out), + ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/rewards.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/rewards.py similarity index 76% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/rewards.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/rewards.py index d7e11c517c7d..f4e9af90a6bd 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/rewards.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/rewards.py @@ -6,8 +6,6 @@ """Warp-first reward functions for the velocity locomotion environment. All functions follow the ``func(env, out, **params) -> None`` signature. -Cross-manager torch tensors (contact sensor, commands) are cached as zero-copy -warp views on first call via ``wp.from_torch``. """ from __future__ import annotations @@ -50,28 +48,22 @@ def _feet_air_time_kernel( def feet_air_time(env: ManagerBasedRLEnv, out, command_name: str, sensor_cfg: SceneEntityCfg, threshold: float) -> None: """Reward long steps taken by the feet using L2-kernel.""" - fn = feet_air_time contact_sensor: ContactSensor = env.scene.sensors[sensor_cfg.name] - # Cache command bridge (persistent pointer) - if not getattr(fn, "_is_warmed_up", False) or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - fn._is_warmed_up = True + command = env.command_manager.get_command_wp(command_name) # Newton contact sensor returns persistent wp.arrays — use directly, no wp.from_torch needed first_contact = contact_sensor.compute_first_contact(env.step_dt) - wp.launch( - kernel=_feet_air_time_kernel, + env._warp_launch.launch( + _feet_air_time_kernel, dim=env.num_envs, inputs=[ contact_sensor.data.last_air_time.warp, first_contact, sensor_cfg.body_ids_wp, - fn._cmd_wp, + command, threshold, out, ], - device=env.device, + site=(out, command_name, float(threshold)), ) @@ -114,25 +106,20 @@ def _feet_air_time_positive_biped_kernel( def feet_air_time_positive_biped(env, out, command_name: str, threshold: float, sensor_cfg: SceneEntityCfg) -> None: """Reward long steps taken by the feet for bipeds.""" - fn = feet_air_time_positive_biped contact_sensor: ContactSensor = env.scene.sensors[sensor_cfg.name] - if not getattr(fn, "_is_warmed_up", False) or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - fn._is_warmed_up = True - wp.launch( - kernel=_feet_air_time_positive_biped_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _feet_air_time_positive_biped_kernel, dim=env.num_envs, inputs=[ contact_sensor.data.current_air_time.warp, contact_sensor.data.current_contact_time.warp, sensor_cfg.body_ids_wp, - fn._cmd_wp, + command, threshold, out, ], - device=env.device, + site=(out, command_name, float(threshold)), ) @@ -172,8 +159,8 @@ def feet_slide(env, out, sensor_cfg: SceneEntityCfg, asset_cfg: SceneEntityCfg = """Penalize feet sliding.""" contact_sensor: ContactSensor = env.scene.sensors[sensor_cfg.name] asset = env.scene[asset_cfg.name] - wp.launch( - kernel=_feet_slide_kernel, + env._warp_launch.launch( + _feet_slide_kernel, dim=env.num_envs, inputs=[ asset.data.body_lin_vel_w.warp, @@ -182,7 +169,7 @@ def feet_slide(env, out, sensor_cfg: SceneEntityCfg, asset_cfg: SceneEntityCfg = contact_sensor.data.net_forces_w_history.warp.shape[1], out, ], - device=env.device, + site=(out, int(contact_sensor.data.net_forces_w_history.warp.shape[1])), ) @@ -224,18 +211,19 @@ def track_lin_vel_xy_yaw_frame_exp( env, out, std: float, command_name: str, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot") ) -> None: """Reward tracking of linear velocity commands (xy axes) in the gravity aligned robot frame.""" - fn = track_lin_vel_xy_yaw_frame_exp asset = env.scene[asset_cfg.name] - if not getattr(fn, "_is_warmed_up", False) or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - fn._is_warmed_up = True - wp.launch( - kernel=_track_lin_vel_xy_yaw_frame_exp_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _track_lin_vel_xy_yaw_frame_exp_kernel, dim=env.num_envs, - inputs=[asset.data.root_quat_w.warp, asset.data.root_lin_vel_w.warp, fn._cmd_wp, 1.0 / (std * std), out], - device=env.device, + inputs=[ + asset.data.root_quat_w.warp, + asset.data.root_lin_vel_w.warp, + command, + 1.0 / (std * std), + out, + ], + site=(out, command_name, float(std)), ) @@ -260,18 +248,18 @@ def track_ang_vel_z_world_exp( env, out, command_name: str, std: float, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot") ) -> None: """Reward tracking of angular velocity commands (yaw) in world frame.""" - fn = track_ang_vel_z_world_exp asset = env.scene[asset_cfg.name] - if not getattr(fn, "_is_warmed_up", False) or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - fn._is_warmed_up = True - wp.launch( - kernel=_track_ang_vel_z_world_exp_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _track_ang_vel_z_world_exp_kernel, dim=env.num_envs, - inputs=[asset.data.root_ang_vel_w.warp, fn._cmd_wp, 1.0 / (std * std), out], - device=env.device, + inputs=[ + asset.data.root_ang_vel_w.warp, + command, + 1.0 / (std * std), + out, + ], + site=(out, command_name, float(std)), ) @@ -304,16 +292,17 @@ def stand_still_joint_deviation_l1( env, out, command_name: str, command_threshold: float = 0.06, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot") ) -> None: """Penalize offsets from the default joint positions when the command is very small.""" - fn = stand_still_joint_deviation_l1 asset = env.scene[asset_cfg.name] - if not getattr(fn, "_is_warmed_up", False) or fn._cmd_name != command_name: - cmd = env.command_manager.get_command(command_name) - fn._cmd_wp = cmd if isinstance(cmd, wp.array) else wp.from_torch(cmd) - fn._cmd_name = command_name - fn._is_warmed_up = True - wp.launch( - kernel=_stand_still_joint_deviation_l1_kernel, + command = env.command_manager.get_command_wp(command_name) + env._warp_launch.launch( + _stand_still_joint_deviation_l1_kernel, dim=env.num_envs, - inputs=[asset.data.joint_pos.warp, asset.data.default_joint_pos.warp, fn._cmd_wp, command_threshold, out], - device=env.device, + inputs=[ + asset.data.joint_pos.warp, + asset.data.default_joint_pos.warp, + command, + command_threshold, + out, + ], + site=(out, command_name, float(command_threshold)), ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/terminations.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/terminations.py similarity index 54% rename from source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/terminations.py rename to source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/terminations.py index 4e0c32c58e64..d3c2ce897ede 100644 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/terminations.py +++ b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/core/velocity/mdp/terminations.py @@ -36,31 +36,24 @@ def terrain_out_of_bounds( env: ManagerBasedRLEnv, out, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"), distance_buffer: float = 3.0 ) -> None: """Terminate when the actor moves too close to the edge of the terrain.""" - fn = terrain_out_of_bounds - if not getattr(fn, "_is_warmed_up", False): - terrain_type = env.scene.cfg.terrain.terrain_type - if terrain_type == "plane": - fn._is_plane = True - elif terrain_type == "generator": - fn._is_plane = False - terrain_gen_cfg = env.scene.terrain.cfg.terrain_generator - grid_width, grid_length = terrain_gen_cfg.size - n_rows, n_cols = terrain_gen_cfg.num_rows, terrain_gen_cfg.num_cols - border_width = terrain_gen_cfg.border_width - fn._half_width = 0.5 * (n_rows * grid_width + 2 * border_width) - fn._half_height = 0.5 * (n_cols * grid_length + 2 * border_width) - else: - raise ValueError("Received unsupported terrain type, must be either 'plane' or 'generator'.") - fn._is_warmed_up = True - - if fn._is_plane: + terrain_type = env.scene.cfg.terrain.terrain_type + if terrain_type == "plane": out.zero_() return + if terrain_type != "generator": + raise ValueError("Received unsupported terrain type, must be either 'plane' or 'generator'.") + + terrain_gen_cfg = env.scene.terrain.cfg.terrain_generator + grid_width, grid_length = terrain_gen_cfg.size + n_rows, n_cols = terrain_gen_cfg.num_rows, terrain_gen_cfg.num_cols + border_width = terrain_gen_cfg.border_width + half_width = 0.5 * (n_rows * grid_width + 2 * border_width) + half_height = 0.5 * (n_cols * grid_length + 2 * border_width) asset: Articulation = env.scene[asset_cfg.name] - wp.launch( - kernel=_terrain_out_of_bounds_kernel, + env._warp_launch.launch( + _terrain_out_of_bounds_kernel, dim=env.num_envs, - inputs=[asset.data.root_pos_w.warp, fn._half_width, fn._half_height, distance_buffer, out], - device=env.device, + inputs=[asset.data.root_pos_w.warp, half_width, half_height, distance_buffer, out], + site=(out, float(half_width), float(half_height), float(distance_buffer)), ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/allegro_hand/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/allegro_hand/__init__.py deleted file mode 100644 index e7fc129c633f..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/allegro_hand/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Allegro Inhand Manipulation environment. -""" - -import gymnasium as gym - -## -# Register Gym environments. -## - -inhand_task_entry = "isaaclab_tasks_experimental.direct.inhand_manipulation" -stable_agents = "isaaclab_tasks.core.reorient.config.allegro_hand.agents" - -gym.register( - id="Isaac-Reorient-Cube-Allegro-Direct-Warp-v0", - entry_point=f"{inhand_task_entry}.inhand_manipulation_warp_env:InHandManipulationWarpEnv", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.allegro_hand_warp_env_cfg:AllegroHandWarpEnvCfg", - "rl_games_cfg_entry_point": f"{stable_agents}:rl_games_direct_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{stable_agents}.rsl_rl_ppo_cfg:AllegroHandPPORunnerCfg", - "skrl_cfg_entry_point": f"{stable_agents}:skrl_direct_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/allegro_hand/allegro_hand_warp_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/allegro_hand/allegro_hand_warp_env_cfg.py deleted file mode 100644 index 573b4f1d302d..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/allegro_hand/allegro_hand_warp_env_cfg.py +++ /dev/null @@ -1,135 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -import isaaclab.sim as sim_utils -from isaaclab.assets import ArticulationCfg # , RigidObjectCfg -from isaaclab.envs import DirectRLEnvCfg -from isaaclab.markers import VisualizationMarkersCfg -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.sim.spawners.materials.physics_materials_cfg import RigidBodyMaterialCfg -from isaaclab.utils.assets import ISAAC_NUCLEUS_DIR -from isaaclab.utils.configclass import configclass - -from isaaclab_assets.robots.allegro import ALLEGRO_HAND_CFG - - -@configclass -class AllegroHandWarpEnvCfg(DirectRLEnvCfg): - # env - decimation = 4 - episode_length_s = 10.0 - action_space = 16 - observation_space = 124 # (full) - state_space = 0 - asymmetric_obs = False - obs_type = "full" - - solver_cfg = MJWarpSolverCfg( - solver="newton", - integrator="implicitfast", - njmax=80, - nconmax=70, - impratio=10.0, - cone="elliptic", - update_data_interval=2, - iterations=100, - ls_iterations=15, - # save_to_mjcf="AllegroHand.xml", - ) - - newton_cfg = NewtonCfg( - solver_cfg=solver_cfg, - num_substeps=2, - debug_mode=False, - ) - # simulation - sim: SimulationCfg = SimulationCfg( - dt=1 / 120, - render_interval=decimation, - physics_material=RigidBodyMaterialCfg( - static_friction=1.0, - dynamic_friction=1.0, - ), - physics=newton_cfg, - ) - # robot - robot_cfg: ArticulationCfg = ALLEGRO_HAND_CFG.replace(prim_path="/World/envs/env_.*/Robot") - - actuated_joint_names = [ - "index_joint_0", - "middle_joint_0", - "ring_joint_0", - "thumb_joint_0", - "index_joint_1", - "index_joint_2", - "index_joint_3", - "middle_joint_1", - "middle_joint_2", - "middle_joint_3", - "ring_joint_1", - "ring_joint_2", - "ring_joint_3", - "thumb_joint_1", - "thumb_joint_2", - "thumb_joint_3", - ] - fingertip_body_names = [ - "index_link_3", - "middle_link_3", - "ring_link_3", - "thumb_link_3", - ] - - # in-hand object - object_cfg: ArticulationCfg = ArticulationCfg( - prim_path="/World/envs/env_.*/object", - spawn=sim_utils.UsdFileCfg( - usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/Blocks/DexCube/dex_cube_instanceable.usd", - mass_props=sim_utils.MassPropertiesCfg(density=400.0), - scale=(1.2, 1.2, 1.2), - ), - # FIXME: it does seem to be a bug for ArticulationCfg for handling empty joint list - init_state=ArticulationCfg.InitialStateCfg( - pos=(0.0, -0.17, 0.565), rot=(0.0, 0.0, 0.0, 1.0), joint_pos={}, joint_vel={} - ), - actuators={}, - articulation_root_prim_path="", - ) - # goal object - goal_object_cfg: VisualizationMarkersCfg = VisualizationMarkersCfg( - prim_path="/Visuals/goal_marker", - markers={ - "goal": sim_utils.UsdFileCfg( - usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/Blocks/DexCube/dex_cube_instanceable.usd", - scale=(1.2, 1.2, 1.2), - ) - }, - ) - # scene - scene: InteractiveSceneCfg = InteractiveSceneCfg( - num_envs=8192, env_spacing=0.75, replicate_physics=True, clone_in_fabric=True - ) - # reset - reset_position_noise = 0.01 # range of position at reset - reset_dof_pos_noise = 0.2 # range of dof pos at reset - reset_dof_vel_noise = 0.0 # range of dof vel at reset - # reward scales - dist_reward_scale = -10.0 - rot_reward_scale = 1.0 - rot_eps = 0.1 - action_penalty_scale = -0.0002 - reach_goal_bonus = 250 - fall_penalty = 0 - fall_dist = 0.24 - vel_obs_scale = 0.2 - success_tolerance = 0.2 - max_consecutive_success = 0 - av_factor = 0.1 - act_moving_average = 1.0 - force_torque_obs_scale = 10.0 diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/__init__.py deleted file mode 100644 index 33d5ac926387..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/__init__.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Ant locomotion environment. -""" - -import gymnasium as gym - -## -# Register Gym environments. -## - -stable_agents = "isaaclab_tasks.core.locomotion.ant.agents" - -gym.register( - id="Isaac-Ant-Direct-Warp-v0", - entry_point=f"{__name__}.ant_env_warp:AntWarpEnv", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.ant_env_warp_cfg:AntWarpEnvCfg", - "rl_games_cfg_entry_point": f"{stable_agents}:rl_games_direct_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{stable_agents}.rsl_rl_ppo_cfg:AntDirectPPORunnerCfg", - "skrl_cfg_entry_point": f"{stable_agents}:skrl_direct_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/ant_env_warp_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/ant_env_warp_cfg.py deleted file mode 100644 index 6e8b7fb81a95..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/ant/ant_env_warp_cfg.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from __future__ import annotations - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -import isaaclab.sim as sim_utils -from isaaclab.assets import ArticulationCfg -from isaaclab.envs import DirectRLEnvCfg -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.terrains import TerrainImporterCfg -from isaaclab.utils.configclass import configclass - -from isaaclab_assets import ANT_CFG - - -@configclass -class AntWarpEnvCfg(DirectRLEnvCfg): - # env - episode_length_s = 15.0 - decimation = 2 - action_scale = 0.5 - action_space = 8 - observation_space = 36 - state_space = 0 - - solver_cfg = MJWarpSolverCfg( - njmax=45, - nconmax=25, - cone="pyramidal", - integrator="implicitfast", - impratio=1, - ) - newton_cfg = NewtonCfg( - solver_cfg=solver_cfg, - num_substeps=1, - debug_mode=False, - use_cuda_graph=True, - ) - - # simulation - sim: SimulationCfg = SimulationCfg(dt=1 / 120, render_interval=decimation, physics=newton_cfg) - terrain = TerrainImporterCfg( - prim_path="/World/ground", - terrain_type="plane", - collision_group=-1, - physics_material=sim_utils.RigidBodyMaterialCfg( - friction_combine_mode="average", - restitution_combine_mode="average", - static_friction=1.0, - dynamic_friction=1.0, - restitution=0.0, - ), - debug_vis=False, - ) - - # scene - scene: InteractiveSceneCfg = InteractiveSceneCfg( - num_envs=4096, env_spacing=4.0, replicate_physics=True, clone_in_fabric=True - ) - - # robot - robot: ArticulationCfg = ANT_CFG.replace(prim_path="/World/envs/env_.*/Robot") - joint_gears: list = [15, 15, 15, 15, 15, 15, 15, 15] - - heading_weight: float = 0.5 - up_weight: float = 0.1 - - energy_cost_scale: float = 0.05 - actions_cost_scale: float = 0.005 - alive_reward_scale: float = 0.5 - dof_vel_scale: float = 0.2 - - death_cost: float = -2.0 - termination_height: float = 0.31 - - angular_velocity_scale: float = 1.0 - contact_force_scale: float = 0.1 diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/__init__.py deleted file mode 100644 index a7fc9492fff6..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Cartpole balancing environment. -""" - -import gymnasium as gym - -## -# Register Gym environments. -## - -stable_agents = "isaaclab_tasks.core.cartpole.agents" - -gym.register( - id="Isaac-Cartpole-Direct-Warp-v0", - entry_point=f"{__name__}.cartpole_warp_env:CartpoleWarpEnv", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.cartpole_warp_env_cfg:CartpoleWarpEnvCfg", - "rl_games_cfg_entry_point": f"{stable_agents}:rl_games_direct_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{stable_agents}.rsl_rl_ppo_cfg:CartpoleDirectPPORunnerCfg", - "skrl_cfg_entry_point": f"{stable_agents}:skrl_direct_ppo_cfg.yaml", - "sb3_cfg_entry_point": f"{stable_agents}:sb3_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/cartpole_warp_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/cartpole_warp_env_cfg.py deleted file mode 100644 index 2a99e1cff3e9..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/cartpole/cartpole_warp_env_cfg.py +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from __future__ import annotations - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.assets import ArticulationCfg -from isaaclab.envs import DirectRLEnvCfg -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from isaaclab_assets.robots.cartpole import CARTPOLE_CFG - - -@configclass -class CartpoleWarpEnvCfg(DirectRLEnvCfg): - # env - decimation = 2 - episode_length_s = 5.0 - action_scale = 100.0 # [N] - action_space = 1 - observation_space = 4 - state_space = 0 - - solver_cfg = MJWarpSolverCfg( - njmax=5, - nconmax=3, - cone="pyramidal", - integrator="implicitfast", - impratio=1, - ) - - newton_cfg = NewtonCfg( - solver_cfg=solver_cfg, - num_substeps=1, - debug_mode=False, - use_cuda_graph=True, - ) - - # simulation - sim: SimulationCfg = SimulationCfg(dt=1 / 120, render_interval=decimation, physics=newton_cfg) - - # robot - robot_cfg: ArticulationCfg = CARTPOLE_CFG.replace(prim_path="/World/envs/env_.*/Robot") - cart_dof_name = "slider_to_cart" - pole_dof_name = "cart_to_pole" - - # scene - scene: InteractiveSceneCfg = InteractiveSceneCfg( - num_envs=4096, env_spacing=4.0, replicate_physics=True, clone_in_fabric=True - ) - - # reset - max_cart_pos = 3.0 # the cart is reset if it exceeds that position [m] - initial_pole_angle_range = [-0.25, 0.25] # the range in which the pole angle is sampled from on reset [x pi rad] - - # reward scales - rew_scale_alive = 1.0 - rew_scale_terminated = -2.0 - rew_scale_pole_pos = -1.0 - rew_scale_cart_vel = -0.01 - rew_scale_pole_vel = -0.005 diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/__init__.py deleted file mode 100644 index c38d7dd955e5..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/__init__.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Humanoid locomotion environment. -""" - -import gymnasium as gym - -## -# Register Gym environments. -## - -stable_agents = "isaaclab_tasks.core.locomotion.humanoid.agents" - -gym.register( - id="Isaac-Humanoid-Direct-Warp-v0", - entry_point=f"{__name__}.humanoid_warp_env:HumanoidWarpEnv", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.humanoid_warp_env_cfg:HumanoidWarpEnvCfg", - "rl_games_cfg_entry_point": f"{stable_agents}:rl_games_direct_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{stable_agents}.rsl_rl_ppo_cfg:HumanoidDirectPPORunnerCfg", - "skrl_cfg_entry_point": f"{stable_agents}:skrl_direct_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/humanoid_warp_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/humanoid_warp_env_cfg.py deleted file mode 100644 index d7fa473eda38..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/humanoid/humanoid_warp_env_cfg.py +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from __future__ import annotations - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -import isaaclab.sim as sim_utils -from isaaclab.assets import ArticulationCfg -from isaaclab.envs import DirectRLEnvCfg -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.terrains import TerrainImporterCfg -from isaaclab.utils.configclass import configclass - -from isaaclab_assets import HUMANOID_CFG - - -@configclass -class HumanoidWarpEnvCfg(DirectRLEnvCfg): - # env - episode_length_s = 15.0 - decimation = 2 - action_scale = 1.0 - action_space = 21 - observation_space = 75 - state_space = 0 - - solver_cfg = MJWarpSolverCfg( - njmax=80, - nconmax=25, - cone="pyramidal", - integrator="implicitfast", - update_data_interval=2, - impratio=1, - ) - newton_cfg = NewtonCfg( - solver_cfg=solver_cfg, - num_substeps=2, - debug_mode=False, - use_cuda_graph=True, - ) - - # simulation - sim: SimulationCfg = SimulationCfg(dt=1 / 120, render_interval=decimation, physics=newton_cfg) - terrain = TerrainImporterCfg( - prim_path="/World/ground", - terrain_type="plane", - collision_group=-1, - physics_material=sim_utils.RigidBodyMaterialCfg( - friction_combine_mode="average", - restitution_combine_mode="average", - static_friction=1.0, - dynamic_friction=1.0, - restitution=0.0, - ), - debug_vis=False, - ) - - # scene - scene: InteractiveSceneCfg = InteractiveSceneCfg( - num_envs=4096, env_spacing=4.0, replicate_physics=True, clone_in_fabric=True - ) - - # robot - robot: ArticulationCfg = HUMANOID_CFG.replace(prim_path="/World/envs/env_.*/Robot") - joint_gears: list = [ - 67.5000, # left_upper_arm - 67.5000, # left_upper_arm - 45.0000, # left_lower_arm - 67.5000, # lower_waist - 67.5000, # lower_waist - 67.5000, # pelvis - 45.0000, # left_thigh: x - 135.0000, # left_thigh: y - 45.0000, # left_thigh: z - 90.0000, # left_knee - 22.5, # left_foot - 22.5, # left_foot - 45.0000, # right_thigh: x - 135.0000, # right_thigh: y - 45.0000, # right_thigh: z - 90.0000, # right_knee - 22.5, # right_foot - 22.5, # right_foot - 67.5000, # right_upper_arm - 67.5000, # right_upper_arm - 45.0000, # right_lower_arm - ] - - heading_weight: float = 0.5 - up_weight: float = 0.1 - - energy_cost_scale: float = 0.05 - actions_cost_scale: float = 0.01 - alive_reward_scale: float = 2.0 - dof_vel_scale: float = 0.1 - - death_cost: float = -1.0 - termination_height: float = 0.8 - - angular_velocity_scale: float = 0.25 - contact_force_scale: float = 0.01 diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/inhand_manipulation/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/inhand_manipulation/__init__.py deleted file mode 100644 index 460a30569089..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/inhand_manipulation/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/locomotion/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/locomotion/__init__.py deleted file mode 100644 index 460a30569089..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/direct/locomotion/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/__init__.py deleted file mode 100644 index 7f23883e6332..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Experimental registrations for manager-based tasks. - -We intentionally only register new Gym IDs pointing at experimental entry points. -Task definitions (configs/mdp) remain in `isaaclab_tasks` to avoid duplication. -""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/__init__.py deleted file mode 100644 index 79c13e2aa8f4..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Classic environments for control. - -These environments are based on the MuJoCo environments provided by OpenAI. - -Reference: - https://github.com/openai/gym/tree/master/gym/envs/mujoco -""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/ant/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/ant/__init__.py deleted file mode 100644 index af513e675f03..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/ant/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Ant locomotion environment (experimental manager-based entry point). -""" - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.locomotion.ant import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Ant-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.ant_env_cfg:AntEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AntPPORunnerCfg", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_manager_ppo_cfg.yaml", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_manager_ppo_cfg.yaml", - "sb3_cfg_entry_point": f"{agents.__name__}:sb3_manager_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/ant/ant_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/ant/ant_env_cfg.py deleted file mode 100644 index 66a9131011f3..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/ant/ant_env_cfg.py +++ /dev/null @@ -1,197 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -# Ant reuses humanoid's experimental MDP (mirrors stable pattern). -from isaaclab_experimental.managers import ObservationTermCfg as ObsTerm -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import TerminationTermCfg as DoneTerm -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -import isaaclab.sim as sim_utils -from isaaclab.assets import AssetBaseCfg -from isaaclab.envs import ManagerBasedRLEnvCfg -from isaaclab.managers import EventTermCfg as EventTerm -from isaaclab.managers import ObservationGroupCfg as ObsGroup -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.terrains import TerrainImporterCfg -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.classic.humanoid.mdp as mdp - -## -# Pre-defined configs -## -from isaaclab_assets.robots.ant import ANT_CFG # isort: skip - - -@configclass -class MySceneCfg(InteractiveSceneCfg): - """Configuration for the terrain scene with an ant robot.""" - - # terrain - terrain = TerrainImporterCfg( - prim_path="/World/ground", - terrain_type="plane", - collision_group=-1, - physics_material=sim_utils.RigidBodyMaterialCfg( - friction_combine_mode="average", - restitution_combine_mode="average", - static_friction=1.0, - dynamic_friction=1.0, - restitution=0.0, - ), - debug_vis=False, - ) - - # robot - robot = ANT_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - - # lights - light = AssetBaseCfg( - prim_path="/World/light", - spawn=sim_utils.DistantLightCfg(color=(0.75, 0.75, 0.75), intensity=3000.0), - ) - - -## -# MDP settings -## - - -@configclass -class ActionsCfg: - """Action specifications for the MDP.""" - - joint_effort = mdp.JointEffortActionCfg(asset_name="robot", joint_names=[".*"], scale=7.5) - - -@configclass -class ObservationsCfg: - """Observation specifications for the MDP.""" - - @configclass - class PolicyCfg(ObsGroup): - """Observations for the policy.""" - - base_height = ObsTerm(func=mdp.base_pos_z) - base_lin_vel = ObsTerm(func=mdp.base_lin_vel) - base_ang_vel = ObsTerm(func=mdp.base_ang_vel) - base_yaw_roll = ObsTerm(func=mdp.base_yaw_roll) - base_angle_to_target = ObsTerm(func=mdp.base_angle_to_target, params={"target_pos": (1000.0, 0.0, 0.0)}) - base_up_proj = ObsTerm(func=mdp.base_up_proj) - base_heading_proj = ObsTerm(func=mdp.base_heading_proj, params={"target_pos": (1000.0, 0.0, 0.0)}) - joint_pos_norm = ObsTerm(func=mdp.joint_pos_limit_normalized) - joint_vel_rel = ObsTerm(func=mdp.joint_vel_rel, scale=0.2) - actions = ObsTerm(func=mdp.last_action) - - def __post_init__(self): - self.enable_corruption = False - self.concatenate_terms = True - - # observation groups - policy: PolicyCfg = PolicyCfg() - - -@configclass -class EventCfg: - """Configuration for events.""" - - reset_base = EventTerm( - func=mdp.reset_root_state_uniform, - mode="reset", - params={"pose_range": {}, "velocity_range": {}}, - ) - - reset_robot_joints = EventTerm( - func=mdp.reset_joints_by_offset, - mode="reset", - params={ - "position_range": (-0.2, 0.2), - "velocity_range": (-0.1, 0.1), - }, - ) - - -@configclass -class RewardsCfg: - """Reward terms for the MDP.""" - - # (1) Reward for moving forward - progress = RewTerm(func=mdp.progress_reward, weight=1.0, params={"target_pos": (1000.0, 0.0, 0.0)}) - # (2) Stay alive bonus - alive = RewTerm(func=mdp.is_alive, weight=0.5) - # (3) Reward for non-upright posture - upright = RewTerm(func=mdp.upright_posture_bonus, weight=0.1, params={"threshold": 0.93}) - # (4) Reward for moving in the right direction - move_to_target = RewTerm( - func=mdp.move_to_target_bonus, weight=0.5, params={"threshold": 0.8, "target_pos": (1000.0, 0.0, 0.0)} - ) - # (5) Penalty for large action commands - action_l2 = RewTerm(func=mdp.action_l2, weight=-0.005) - # (6) Penalty for energy consumption - energy = RewTerm(func=mdp.power_consumption, weight=-0.05, params={"gear_ratio": {".*": 15.0}}) - # (7) Penalty for reaching close to joint limits - joint_pos_limits = RewTerm( - func=mdp.joint_pos_limits_penalty_ratio, weight=-0.1, params={"threshold": 0.99, "gear_ratio": {".*": 15.0}} - ) - - -@configclass -class TerminationsCfg: - """Termination terms for the MDP.""" - - # (1) Terminate if the episode length is exceeded - time_out = DoneTerm(func=mdp.time_out, time_out=True) - # (2) Terminate if the robot falls - torso_height = DoneTerm(func=mdp.root_height_below_minimum, params={"minimum_height": 0.31}) - - -## -# Environment configuration -## - - -@configclass -class AntEnvCfg(ManagerBasedRLEnvCfg): - """Configuration for the MuJoCo-style Ant walking environment.""" - - # Simulation settings - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=38, - nconmax=15, - ls_iterations=10, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - # Scene settings - scene: MySceneCfg = MySceneCfg(num_envs=4096, env_spacing=5.0, clone_in_fabric=True) - # Basic settings - observations: ObservationsCfg = ObservationsCfg() - actions: ActionsCfg = ActionsCfg() - # MDP settings - rewards: RewardsCfg = RewardsCfg() - terminations: TerminationsCfg = TerminationsCfg() - events: EventCfg = EventCfg() - - def __post_init__(self): - """Post initialization.""" - # general settings - self.decimation = 2 - self.episode_length_s = 16.0 - # simulation settings - self.sim.dt = 1 / 120.0 - self.sim.render_interval = self.decimation - # default friction material - self.sim.physics_material.static_friction = 1.0 - self.sim.physics_material.dynamic_friction = 1.0 diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/__init__.py deleted file mode 100644 index 9f46551de255..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Cartpole balancing environment (experimental manager-based entry point). -""" - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.cartpole import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Cartpole-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.cartpole_env_cfg:CartpoleEnvCfg", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_manager_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_manager_ppo_cfg:CartpolePPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_manager_ppo_cfg.yaml", - "sb3_cfg_entry_point": f"{agents.__name__}:sb3_manager_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/cartpole_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/cartpole_env_cfg.py deleted file mode 100644 index ae187a593a16..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/cartpole_env_cfg.py +++ /dev/null @@ -1,199 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import math - -from isaaclab_experimental.managers import ObservationTermCfg as ObsTerm -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import SceneEntityCfg -from isaaclab_experimental.managers import TerminationTermCfg as DoneTerm -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -import isaaclab.sim as sim_utils -from isaaclab.assets import ArticulationCfg, AssetBaseCfg -from isaaclab.envs import ManagerBasedRLEnvCfg -from isaaclab.managers import EventTermCfg as EventTerm -from isaaclab.managers import ObservationGroupCfg as ObsGroup -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.classic.cartpole.mdp as mdp - -## -# Pre-defined configs -## -from isaaclab_assets.robots.cartpole import CARTPOLE_CFG # isort:skip - - -## -# Scene definition -## - - -@configclass -class CartpoleSceneCfg(InteractiveSceneCfg): - """Configuration for a cart-pole scene.""" - - # ground plane - # ground = AssetBaseCfg( - # prim_path="/World/ground", - # spawn=sim_utils.GroundPlaneCfg(size=(100.0, 100.0)), - # ) - - # cartpole - robot: ArticulationCfg = CARTPOLE_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - - # lights - dome_light = AssetBaseCfg( - prim_path="/World/DomeLight", - spawn=sim_utils.DomeLightCfg(color=(0.9, 0.9, 0.9), intensity=500.0), - ) - - -## -# MDP settings -## - - -@configclass -class ActionsCfg: - """Action specifications for the MDP.""" - - joint_effort = mdp.JointEffortActionCfg(asset_name="robot", joint_names=["slider_to_cart"], scale=100.0) - - -@configclass -class ObservationsCfg: - """Observation specifications for the MDP.""" - - @configclass - class PolicyCfg(ObsGroup): - """Observations for policy group.""" - - # observation terms (order preserved) - joint_pos_rel = ObsTerm(func=mdp.joint_pos_rel) - joint_vel_rel = ObsTerm(func=mdp.joint_vel_rel) - - def __post_init__(self) -> None: - self.enable_corruption = False - self.concatenate_terms = True - - # observation groups - policy: PolicyCfg = PolicyCfg() - - -@configclass -class EventCfg: - """Configuration for events.""" - - # reset - reset_cart_position = EventTerm( - func=mdp.reset_joints_by_offset, - mode="reset", - params={ - "asset_cfg": SceneEntityCfg("robot", joint_names=["slider_to_cart"]), - "position_range": (-1.0, 1.0), - "velocity_range": (-0.5, 0.5), - }, - ) - - reset_pole_position = EventTerm( - func=mdp.reset_joints_by_offset, - mode="reset", - params={ - "asset_cfg": SceneEntityCfg("robot", joint_names=["cart_to_pole"]), - "position_range": (-0.25 * math.pi, 0.25 * math.pi), - "velocity_range": (-0.25 * math.pi, 0.25 * math.pi), - }, - ) - - -@configclass -class RewardsCfg: - """Reward terms for the MDP.""" - - # (1) Constant running reward - alive = RewTerm(func=mdp.is_alive, weight=1.0) - # (2) Failure penalty - terminating = RewTerm(func=mdp.is_terminated, weight=-2.0) - # (3) Primary task: keep pole upright - pole_pos = RewTerm( - func=mdp.joint_pos_target_l2, - weight=-1.0, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=["cart_to_pole"]), "target": 0.0}, - ) - # (4) Shaping tasks: lower cart velocity - cart_vel = RewTerm( - func=mdp.joint_vel_l1, - weight=-0.01, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=["slider_to_cart"])}, - ) - # (5) Shaping tasks: lower pole angular velocity - pole_vel = RewTerm( - func=mdp.joint_vel_l1, - weight=-0.005, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=["cart_to_pole"])}, - ) - - -@configclass -class TerminationsCfg: - """Termination terms for the MDP.""" - - # (1) Time out - time_out = DoneTerm(func=mdp.time_out, time_out=True) - # (2) Cart out of bounds - cart_out_of_bounds = DoneTerm( - func=mdp.joint_pos_out_of_manual_limit, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=["slider_to_cart"]), "bounds": (-3.0, 3.0)}, - ) - - -## -# Environment configuration -## - - -@configclass -class CartpoleEnvCfg(ManagerBasedRLEnvCfg): - """Configuration for the cartpole environment.""" - - # Scene settings - scene: CartpoleSceneCfg = CartpoleSceneCfg(num_envs=4096, env_spacing=4.0, clone_in_fabric=True) - # Basic settings - observations: ObservationsCfg = ObservationsCfg() - actions: ActionsCfg = ActionsCfg() - events: EventCfg = EventCfg() - # MDP settings - rewards: RewardsCfg = RewardsCfg() - terminations: TerminationsCfg = TerminationsCfg() - # Simulation settings - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=5, - nconmax=3, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - use_cuda_graph=True, - ) - ) - - # Post initialization - def __post_init__(self) -> None: - """Post initialization.""" - # general settings - self.decimation = 2 - self.episode_length_s = 5 - # viewer settings - self.viewer.eye = (8.0, 0.0, 5.0) - # simulation settings - self.sim.dt = 1 / 120 - self.sim.render_interval = self.decimation diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/rewards.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/rewards.py deleted file mode 100644 index 2678ef03de65..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/cartpole/mdp/rewards.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from __future__ import annotations - -from typing import TYPE_CHECKING - -import warp as wp -from isaaclab_experimental.managers import SceneEntityCfg -from isaaclab_experimental.utils.warp.utils import wrap_to_pi - -if TYPE_CHECKING: - from isaaclab.assets import Articulation - from isaaclab.envs import ManagerBasedRLEnv - - -@wp.kernel -def _joint_pos_target_l2_kernel( - joint_pos: wp.array(dtype=wp.float32, ndim=2), - joint_mask: wp.array(dtype=wp.bool), - out: wp.array(dtype=wp.float32), - target: float, -): - i = wp.tid() - s = float(0.0) - for j in range(joint_pos.shape[1]): - if joint_mask[j]: - a = wrap_to_pi(joint_pos[i, j]) - d = a - target - s += d * d - out[i] = s - - -def joint_pos_target_l2(env: ManagerBasedRLEnv, out, target: float, asset_cfg: SceneEntityCfg) -> None: - """Penalize joint position deviation from a target value. Writes into ``out``.""" - asset: Articulation = env.scene[asset_cfg.name] - assert asset.data.joint_pos.warp.shape[1] == asset_cfg.joint_mask.shape[0] - wp.launch( - kernel=_joint_pos_target_l2_kernel, - dim=env.num_envs, - inputs=[asset.data.joint_pos.warp, asset_cfg.joint_mask, out, target], - device=env.device, - ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/__init__.py deleted file mode 100644 index 0bcd020bcd4c..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -""" -Humanoid locomotion environment (experimental manager-based entry point). -""" - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.locomotion.humanoid import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Humanoid-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.humanoid_env_cfg:HumanoidEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:HumanoidPPORunnerCfg", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_manager_ppo_cfg.yaml", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_manager_ppo_cfg.yaml", - "sb3_cfg_entry_point": f"{agents.__name__}:sb3_manager_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/humanoid_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/humanoid_env_cfg.py deleted file mode 100644 index 9de689ffb98e..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/classic/humanoid/humanoid_env_cfg.py +++ /dev/null @@ -1,232 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_experimental.managers import ObservationTermCfg as ObsTerm -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import TerminationTermCfg as DoneTerm -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -import isaaclab.sim as sim_utils -from isaaclab.assets import AssetBaseCfg -from isaaclab.envs import ManagerBasedRLEnvCfg -from isaaclab.managers import EventTermCfg as EventTerm -from isaaclab.managers import ObservationGroupCfg as ObsGroup -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sim import SimulationCfg -from isaaclab.terrains import TerrainImporterCfg -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.classic.humanoid.mdp as mdp - -from isaaclab_assets.robots.humanoid import HUMANOID_CFG # isort:skip - - -## -# Scene definition -## - - -@configclass -class MySceneCfg(InteractiveSceneCfg): - """Configuration for the terrain scene with a humanoid robot.""" - - # terrain - terrain = TerrainImporterCfg( - prim_path="/World/ground", - terrain_type="plane", - collision_group=-1, - physics_material=sim_utils.RigidBodyMaterialCfg(static_friction=1.0, dynamic_friction=1.0, restitution=0.0), - debug_vis=False, - ) - - # robot - robot = HUMANOID_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - - # lights - light = AssetBaseCfg( - prim_path="/World/light", - spawn=sim_utils.DistantLightCfg(color=(0.75, 0.75, 0.75), intensity=3000.0), - ) - - -## -# MDP settings -## - - -@configclass -class ActionsCfg: - """Action specifications for the MDP.""" - - joint_effort = mdp.JointEffortActionCfg( - asset_name="robot", - joint_names=[".*"], - scale={ - ".*_waist.*": 67.5, - ".*_upper_arm.*": 67.5, - "pelvis": 67.5, - ".*_lower_arm": 45.0, - ".*_thigh:0": 45.0, - ".*_thigh:1": 135.0, - ".*_thigh:2": 45.0, - ".*_shin": 90.0, - ".*_foot.*": 22.5, - }, - ) - - -@configclass -class ObservationsCfg: - """Observation specifications for the MDP.""" - - @configclass - class PolicyCfg(ObsGroup): - """Observations for the policy.""" - - base_height = ObsTerm(func=mdp.base_pos_z) - base_lin_vel = ObsTerm(func=mdp.base_lin_vel) - base_ang_vel = ObsTerm(func=mdp.base_ang_vel, scale=0.25) - base_yaw_roll = ObsTerm(func=mdp.base_yaw_roll) - base_angle_to_target = ObsTerm(func=mdp.base_angle_to_target, params={"target_pos": (1000.0, 0.0, 0.0)}) - base_up_proj = ObsTerm(func=mdp.base_up_proj) - base_heading_proj = ObsTerm(func=mdp.base_heading_proj, params={"target_pos": (1000.0, 0.0, 0.0)}) - joint_pos_norm = ObsTerm(func=mdp.joint_pos_limit_normalized) - joint_vel_rel = ObsTerm(func=mdp.joint_vel_rel, scale=0.1) - actions = ObsTerm(func=mdp.last_action) - - def __post_init__(self): - self.enable_corruption = False - self.concatenate_terms = True - - # observation groups - policy: PolicyCfg = PolicyCfg() - - -@configclass -class EventCfg: - """Configuration for events.""" - - reset_base = EventTerm( - func=mdp.reset_root_state_uniform, - mode="reset", - params={"pose_range": {}, "velocity_range": {}}, - ) - - reset_robot_joints = EventTerm( - func=mdp.reset_joints_by_offset, - mode="reset", - params={ - "position_range": (-0.2, 0.2), - "velocity_range": (-0.1, 0.1), - }, - ) - - -@configclass -class RewardsCfg: - """Reward terms for the MDP.""" - - # (1) Reward for moving forward - progress = RewTerm(func=mdp.progress_reward, weight=1.0, params={"target_pos": (1000.0, 0.0, 0.0)}) - # (2) Stay alive bonus - alive = RewTerm(func=mdp.is_alive, weight=2.0) - # (3) Reward for non-upright posture - upright = RewTerm(func=mdp.upright_posture_bonus, weight=0.1, params={"threshold": 0.93}) - # (4) Reward for moving in the right direction - move_to_target = RewTerm( - func=mdp.move_to_target_bonus, weight=0.5, params={"threshold": 0.8, "target_pos": (1000.0, 0.0, 0.0)} - ) - # (5) Penalty for large action commands - action_l2 = RewTerm(func=mdp.action_l2, weight=-0.01) - # (6) Penalty for energy consumption - energy = RewTerm( - func=mdp.power_consumption, - weight=-0.005, - params={ - "gear_ratio": { - ".*_waist.*": 67.5, - ".*_upper_arm.*": 67.5, - "pelvis": 67.5, - ".*_lower_arm": 45.0, - ".*_thigh:0": 45.0, - ".*_thigh:1": 135.0, - ".*_thigh:2": 45.0, - ".*_shin": 90.0, - ".*_foot.*": 22.5, - } - }, - ) - # (7) Penalty for reaching close to joint limits - joint_pos_limits = RewTerm( - func=mdp.joint_pos_limits_penalty_ratio, - weight=-0.25, - params={ - "threshold": 0.98, - "gear_ratio": { - ".*_waist.*": 67.5, - ".*_upper_arm.*": 67.5, - "pelvis": 67.5, - ".*_lower_arm": 45.0, - ".*_thigh:0": 45.0, - ".*_thigh:1": 135.0, - ".*_thigh:2": 45.0, - ".*_shin": 90.0, - ".*_foot.*": 22.5, - }, - }, - ) - - -@configclass -class TerminationsCfg: - """Termination terms for the MDP.""" - - # (1) Terminate if the episode length is exceeded - time_out = DoneTerm(func=mdp.time_out, time_out=True) - # (2) Terminate if the robot falls - torso_height = DoneTerm(func=mdp.root_height_below_minimum, params={"minimum_height": 0.8}) - - -@configclass -class HumanoidEnvCfg(ManagerBasedRLEnvCfg): - """Configuration for the MuJoCo-style Humanoid walking environment.""" - - # Scene settings - scene: MySceneCfg = MySceneCfg(num_envs=4096, env_spacing=5.0, clone_in_fabric=True) - # Basic settings - observations: ObservationsCfg = ObservationsCfg() - actions: ActionsCfg = ActionsCfg() - # MDP settings - rewards: RewardsCfg = RewardsCfg() - terminations: TerminationsCfg = TerminationsCfg() - events: EventCfg = EventCfg() - - def __post_init__(self): - """Post initialization.""" - # general settings - self.decimation = 2 - self.episode_length_s = 16.0 - # simulation settings - self.sim: SimulationCfg = SimulationCfg( - dt=1 / 120.0, - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=80, - nconmax=25, - ls_iterations=15, - cone="pyramidal", - update_data_interval=2, - impratio=1, - integrator="implicitfast", - ), - num_substeps=2, - debug_mode=False, - ), - ) - # self.sim.dt = 1 / 120.0 - self.sim.render_interval = self.decimation - # default friction material - self.sim.physics_material.static_friction = 1.0 - self.sim.physics_material.dynamic_friction = 1.0 diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/__init__.py deleted file mode 100644 index 0660d38f0658..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Locomotion experimental task registrations (manager-based).""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/__init__.py deleted file mode 100644 index 26f3257daef4..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Configurations for velocity-based locomotion environments.""" - -# We leave this file empty since we don't want to expose any configs in this package directly. -# We still need this file to import the "config" module in the parent package. diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/__init__.py deleted file mode 100644 index 5b79532a456e..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.contrib.velocity.config.a1 import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Velocity-Flat-UnitreeA1-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:UnitreeA1FlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UnitreeA1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - "sb3_cfg_entry_point": f"{agents.__name__}:sb3_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-UnitreeA1-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:UnitreeA1FlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UnitreeA1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - "sb3_cfg_entry_point": f"{agents.__name__}:sb3_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/flat_env_cfg.py deleted file mode 100644 index f213286d0cfb..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/flat_env_cfg.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import UnitreeA1RoughEnvCfg - - -@configclass -class UnitreeA1FlatEnvCfg(UnitreeA1RoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=60, - nconmax=30, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - # post init of parent - super().__post_init__() - - # override rewards - self.rewards.flat_orientation_l2.weight = -2.5 - self.rewards.feet_air_time.weight = 0.25 - - # change terrain to flat - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - # no height scan - # self.scene.height_scanner = None - # self.observations.policy.height_scan = None - # no terrain curriculum - self.curriculum.terrain_levels = None - - -class UnitreeA1FlatEnvCfg_PLAY(UnitreeA1FlatEnvCfg): - def __post_init__(self) -> None: - # post init of parent - super().__post_init__() - - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - # disable randomization for play - self.observations.policy.enable_corruption = False - # remove random pushing event - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/rough_env_cfg.py deleted file mode 100644 index ee0f236043a2..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/a1/rough_env_cfg.py +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_experimental.managers import TerminationTermCfg as DoneTerm - -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp as mdp -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import ( - LocomotionVelocityRoughEnvCfg, - TerminationsCfg, -) - -from isaaclab_assets.robots.unitree import UNITREE_A1_CFG # isort: skip - - -class TerminationsCfg_A1(TerminationsCfg): - base_too_low = DoneTerm(func=mdp.root_height_below_minimum, params={"minimum_height": 0.2}) - - -@configclass -class UnitreeA1RoughEnvCfg(LocomotionVelocityRoughEnvCfg): - terminations: TerminationsCfg_A1 = TerminationsCfg_A1() - - def __post_init__(self): - # post init of parent - super().__post_init__() - - self.scene.robot = UNITREE_A1_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.scene.terrain.terrain_generator.sub_terrains["boxes"].grid_height_range = (0.025, 0.1) - self.scene.terrain.terrain_generator.sub_terrains["random_rough"].noise_range = (0.01, 0.06) - self.scene.terrain.terrain_generator.sub_terrains["random_rough"].noise_step = 0.01 - - # reduce action scale - self.actions.joint_pos.scale = 0.25 - - # event - self.events.push_robot = None - # TODO: TEMPORARILY DISABLED - adding this causes NaNs in the simulation - # self.events.add_base_mass.params["mass_distribution_params"] = (-1.0, 3.0) - # self.events.add_base_mass.params["asset_cfg"].body_names = "trunk" - self.events.base_external_force_torque.params["asset_cfg"].body_names = "trunk" - self.events.reset_robot_joints.params["position_range"] = (1.0, 1.0) - self.events.reset_base.params = { - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (0.0, 0.0), - "y": (0.0, 0.0), - "z": (0.0, 0.0), - "roll": (0.0, 0.0), - "pitch": (0.0, 0.0), - "yaw": (0.0, 0.0), - }, - } - self.events.base_com = None - - # rewards - self.rewards.feet_air_time.params["sensor_cfg"].body_names = ".*_foot" - self.rewards.feet_air_time.weight = 0.01 - self.rewards.undesired_contacts.params["sensor_cfg"].body_names = ".*thigh" - self.rewards.dof_torques_l2.weight = -0.0002 - self.rewards.track_lin_vel_xy_exp.weight = 1.5 - self.rewards.track_ang_vel_z_exp.weight = 0.75 - self.rewards.dof_acc_l2.weight = -2.5e-7 - self.terminations.base_contact.params["sensor_cfg"].body_names = "trunk" - - -@configclass -class UnitreeA1RoughEnvCfg_PLAY(UnitreeA1RoughEnvCfg): - def __post_init__(self): - # post init of parent - super().__post_init__() - - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - # spawn the robot randomly in the grid (instead of their terrain levels) - self.scene.terrain.max_init_terrain_level = None - # reduce the number of terrains to save memory - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - - # disable randomization for play - self.observations.policy.enable_corruption = False - # remove random pushing event - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/__init__.py deleted file mode 100644 index a6753cfb95ef..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.contrib.velocity.config.anymal_b import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Velocity-Flat-AnymalB-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalBFlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalBFlatPPORunnerCfg", - "rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalBFlatPPORunnerWithSymmetryCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-AnymalB-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalBFlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalBFlatPPORunnerCfg", - "rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalBFlatPPORunnerWithSymmetryCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/flat_env_cfg.py deleted file mode 100644 index a5c825bf2e98..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/flat_env_cfg.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import AnymalBRoughEnvCfg - - -@configclass -class AnymalBFlatEnvCfg(AnymalBRoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=75, - nconmax=15, - cone="elliptic", - impratio=100, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - # post init of parent - super().__post_init__() - - # override rewards - self.rewards.flat_orientation_l2.weight = -5.0 - self.rewards.dof_torques_l2.weight = -2.5e-5 - self.rewards.feet_air_time.weight = 0.5 - # change terrain to flat - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - # no height scan - self.scene.height_scanner = None - self.observations.policy.height_scan = None - # no terrain curriculum - self.curriculum.terrain_levels = None - - -class AnymalBFlatEnvCfg_PLAY(AnymalBFlatEnvCfg): - def __post_init__(self) -> None: - # post init of parent - super().__post_init__() - - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - # disable randomization for play - self.observations.policy.enable_corruption = False - # remove random pushing event - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/rough_env_cfg.py deleted file mode 100644 index 3a7d80af470f..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_b/rough_env_cfg.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab.utils.configclass import configclass - -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import LocomotionVelocityRoughEnvCfg - -from isaaclab_assets import ANYMAL_B_CFG # isort: skip - - -@configclass -class AnymalBRoughEnvCfg(LocomotionVelocityRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.robot = ANYMAL_B_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.manager_call_max_mode = {"Scene": 1} - - -@configclass -class AnymalBRoughEnvCfg_PLAY(AnymalBRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.scene.terrain.max_init_terrain_level = None - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/__init__.py deleted file mode 100644 index 9c9c2530e118..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/__init__.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.contrib.velocity.config.anymal_c import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Velocity-Flat-AnymalC-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalCFlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalCFlatPPORunnerCfg", - "rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalCFlatPPORunnerWithSymmetryCfg", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_flat_ppo_cfg.yaml", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-AnymalC-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalCFlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalCFlatPPORunnerCfg", - "rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalCFlatPPORunnerWithSymmetryCfg", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_flat_ppo_cfg.yaml", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/flat_env_cfg.py deleted file mode 100644 index 3cc348a97675..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/flat_env_cfg.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import AnymalCRoughEnvCfg - - -@configclass -class AnymalCFlatEnvCfg(AnymalCRoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=120, - nconmax=15, - cone="elliptic", - impratio=100, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - # post init of parent - super().__post_init__() - - # override rewards - self.rewards.flat_orientation_l2.weight = -5.0 - self.rewards.dof_torques_l2.weight = -2.5e-5 - self.rewards.feet_air_time.weight = 0.5 - # change terrain to flat - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - # no terrain curriculum - self.curriculum.terrain_levels = None - - -class AnymalCFlatEnvCfg_PLAY(AnymalCFlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/rough_env_cfg.py deleted file mode 100644 index 1d2f2676c64a..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_c/rough_env_cfg.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab.utils.configclass import configclass - -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import LocomotionVelocityRoughEnvCfg - -## -# Pre-defined configs -## -from isaaclab_assets.robots.anymal import ANYMAL_C_CFG # isort: skip - - -@configclass -class AnymalCRoughEnvCfg(LocomotionVelocityRoughEnvCfg): - def __post_init__(self): - # post init of parent - super().__post_init__() - # switch robot to anymal-c - self.scene.robot = ANYMAL_C_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.scene.robot.actuators["legs"].armature = 0.01 - self.manager_call_max_mode = {"Scene": 1} - - -@configclass -class AnymalCRoughEnvCfg_PLAY(AnymalCRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.scene.terrain.max_init_terrain_level = None - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/__init__.py deleted file mode 100644 index 3722de03ed4e..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/__init__.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.velocity.config.anymal_d import agents - -## -# Register Gym environments. -## - -# Rough env disabled: requires isaaclab_physx which is not yet available on dev/newton. -# The package exists on upstream/develop (commit 308400f1d35) but has not been merged. -# Re-enable once dev/newton picks up isaaclab_physx. -# gym.register( -# id="Isaac-Velocity-Rough-AnymalD-Warp-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.rough_env_cfg:AnymalDRoughEnvCfg", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDRoughPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", -# }, -# ) - -# gym.register( -# id="Isaac-Velocity-Rough-AnymalD-Warp-Play-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.rough_env_cfg:AnymalDRoughEnvCfg_PLAY", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDRoughPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", -# }, -# ) - -gym.register( - id="Isaac-Velocity-Flat-AnymalD-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalDFlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-AnymalD-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalDFlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/flat_env_cfg.py deleted file mode 100644 index 702443e815e2..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/flat_env_cfg.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import AnymalDRoughEnvCfg - - -@configclass -class AnymalDFlatEnvCfg(AnymalDRoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=60, - nconmax=25, - cone="elliptic", - impratio=100.0, - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - super().__post_init__() - self.rewards.flat_orientation_l2.weight = -5.0 - self.rewards.dof_torques_l2.weight = -2.5e-5 - self.rewards.feet_air_time.weight = 0.5 - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - self.curriculum.terrain_levels = None - - -class AnymalDFlatEnvCfg_PLAY(AnymalDFlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/rough_env_cfg.py deleted file mode 100644 index 71f49edd2e68..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/anymal_d/rough_env_cfg.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab.utils.configclass import configclass - -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import LocomotionVelocityRoughEnvCfg - -## -# Pre-defined configs -## -from isaaclab_assets.robots.anymal import ANYMAL_D_CFG # isort: skip - - -@configclass -class AnymalDRoughEnvCfg(LocomotionVelocityRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.robot = ANYMAL_D_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.manager_call_max_mode = {"Scene": 1} - - -@configclass -class AnymalDRoughEnvCfg_PLAY(AnymalDRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.scene.terrain.max_init_terrain_level = None - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/__init__.py deleted file mode 100644 index e3af41334541..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/__init__.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.velocity.config.cassie import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Velocity-Flat-Cassie-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:CassieFlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:CassieFlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-Cassie-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:CassieFlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:CassieFlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/flat_env_cfg.py deleted file mode 100644 index 818c7aa30a1e..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/flat_env_cfg.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import CassieRoughEnvCfg - - -@configclass -class CassieFlatEnvCfg(CassieRoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=52, - nconmax=15, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - super().__post_init__() - self.rewards.flat_orientation_l2.weight = -2.5 - self.rewards.feet_air_time.weight = 5.0 - self.rewards.joint_deviation_hip.params["asset_cfg"].joint_names = ["hip_rotation_.*"] - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - self.curriculum.terrain_levels = None - - -class CassieFlatEnvCfg_PLAY(CassieFlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/rough_env_cfg.py deleted file mode 100644 index 2fc44ea36cdc..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/cassie/rough_env_cfg.py +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import SceneEntityCfg - -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp as mdp -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import ( - LocomotionVelocityRoughEnvCfg, - RewardsCfg, -) - -from isaaclab_assets.robots.cassie import CASSIE_CFG # isort: skip - - -@configclass -class CassieRewardsCfg(RewardsCfg): - termination_penalty = RewTerm(func=mdp.is_terminated, weight=-200.0) - feet_air_time = RewTerm( - func=mdp.feet_air_time_positive_biped, - weight=2.5, - params={ - "sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*toe"), - "command_name": "base_velocity", - "threshold": 0.3, - }, - ) - joint_deviation_hip = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.2, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=["hip_abduction_.*", "hip_rotation_.*"])}, - ) - joint_deviation_toes = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.2, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=["toe_joint_.*"])}, - ) - dof_pos_limits = RewTerm( - func=mdp.joint_pos_limits, - weight=-1.0, - params={"asset_cfg": SceneEntityCfg("robot", joint_names="toe_joint_.*")}, - ) - - -@configclass -class CassieRoughEnvCfg(LocomotionVelocityRoughEnvCfg): - rewards: CassieRewardsCfg = CassieRewardsCfg() - - def __post_init__(self): - super().__post_init__() - self.scene.robot = CASSIE_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.actions.joint_pos.scale = 0.5 - self.events.push_robot = None - # TODO: TEMPORARILY DISABLED - adding this causes NaNs in the simulation - # self.events.add_base_mass = None - self.events.reset_robot_joints.params["position_range"] = (1.0, 1.0) - self.events.base_external_force_torque.params["asset_cfg"].body_names = [".*pelvis"] - self.events.reset_base.params = { - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (0.0, 0.0), - "y": (0.0, 0.0), - "z": (0.0, 0.0), - "roll": (0.0, 0.0), - "pitch": (0.0, 0.0), - "yaw": (0.0, 0.0), - }, - } - self.events.base_com = None - self.terminations.base_contact.params["sensor_cfg"].body_names = [".*pelvis"] - self.rewards.undesired_contacts = None - self.rewards.dof_torques_l2.weight = -5.0e-6 - self.rewards.track_lin_vel_xy_exp.weight = 2.0 - self.rewards.track_ang_vel_z_exp.weight = 1.0 - self.rewards.action_rate_l2.weight *= 1.5 - self.rewards.dof_acc_l2.weight *= 1.5 - - -@configclass -class CassieRoughEnvCfg_PLAY(CassieRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.scene.terrain.max_init_terrain_level = None - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/__init__.py deleted file mode 100644 index f49e103f431a..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/__init__.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.velocity.config.g1 import agents - -## -# Register Gym environments. -## - -# gym.register( -# id="Isaac-Velocity-Rough-G1-Warp-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.rough_env_cfg:G1RoughEnvCfg", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:G1RoughPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", -# }, -# ) - - -# gym.register( -# id="Isaac-Velocity-Rough-G1-Warp-Play-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.rough_env_cfg:G1RoughEnvCfg_PLAY", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:G1RoughPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", -# }, -# ) - -gym.register( - id="Isaac-Velocity-Flat-G1-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:G1FlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:G1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-G1-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:G1FlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:G1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/flat_env_cfg.py deleted file mode 100644 index e99dd95ff82f..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/flat_env_cfg.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_experimental.managers import SceneEntityCfg -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import G1RoughEnvCfg - - -@configclass -class G1FlatEnvCfg(G1RoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=95, - nconmax=10, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - super().__post_init__() - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - self.curriculum.terrain_levels = None - self.rewards.track_ang_vel_z_exp.weight = 1.0 - self.rewards.lin_vel_z_l2.weight = -0.2 - self.rewards.action_rate_l2.weight = -0.005 - self.rewards.dof_acc_l2.weight = -1.0e-7 - self.rewards.feet_air_time.weight = 0.75 - self.rewards.feet_air_time.params["threshold"] = 0.4 - self.rewards.dof_torques_l2.weight = -2.0e-6 - self.rewards.dof_torques_l2.params["asset_cfg"] = SceneEntityCfg( - "robot", joint_names=[".*_hip_.*", ".*_knee_joint"] - ) - self.commands.base_velocity.ranges.lin_vel_x = (0.0, 1.0) - self.commands.base_velocity.ranges.lin_vel_y = (-0.5, 0.5) - self.commands.base_velocity.ranges.ang_vel_z = (-1.0, 1.0) - - -class G1FlatEnvCfg_PLAY(G1FlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/rough_env_cfg.py deleted file mode 100644 index 7f9d12d0f201..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/g1/rough_env_cfg.py +++ /dev/null @@ -1,178 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import SceneEntityCfg - -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp as mdp -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import ( - LocomotionVelocityRoughEnvCfg, - RewardsCfg, -) - -## -# Pre-defined configs -## -from isaaclab_assets import G1_MINIMAL_CFG # isort: skip - - -@configclass -class G1Rewards(RewardsCfg): - """Reward terms for the MDP.""" - - termination_penalty = RewTerm(func=mdp.is_terminated, weight=-200.0) - track_lin_vel_xy_exp = RewTerm( - func=mdp.track_lin_vel_xy_yaw_frame_exp, - weight=1.0, - params={"command_name": "base_velocity", "std": 0.5}, - ) - track_ang_vel_z_exp = RewTerm( - func=mdp.track_ang_vel_z_world_exp, weight=2.0, params={"command_name": "base_velocity", "std": 0.5} - ) - feet_air_time = RewTerm( - func=mdp.feet_air_time_positive_biped, - weight=0.25, - params={ - "command_name": "base_velocity", - "sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*_ankle_roll_link"), - "threshold": 0.4, - }, - ) - feet_slide = RewTerm( - func=mdp.feet_slide, - weight=-0.1, - params={ - "sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*_ankle_roll_link"), - "asset_cfg": SceneEntityCfg("robot", body_names=".*_ankle_roll_link"), - }, - ) - dof_pos_limits = RewTerm( - func=mdp.joint_pos_limits, - weight=-1.0, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=[".*_ankle_pitch_joint", ".*_ankle_roll_joint"])}, - ) - joint_deviation_hip = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.1, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=[".*_hip_yaw_joint", ".*_hip_roll_joint"])}, - ) - joint_deviation_arms = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.1, - params={ - "asset_cfg": SceneEntityCfg( - "robot", - joint_names=[ - ".*_shoulder_pitch_joint", - ".*_shoulder_roll_joint", - ".*_shoulder_yaw_joint", - ".*_elbow_pitch_joint", - ".*_elbow_roll_joint", - ], - ) - }, - ) - joint_deviation_fingers = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.05, - params={ - "asset_cfg": SceneEntityCfg( - "robot", - joint_names=[ - ".*_five_joint", - ".*_three_joint", - ".*_six_joint", - ".*_four_joint", - ".*_zero_joint", - ".*_one_joint", - ".*_two_joint", - ], - ) - }, - ) - joint_deviation_torso = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.1, - params={"asset_cfg": SceneEntityCfg("robot", joint_names="torso_joint")}, - ) - - -@configclass -class G1RoughEnvCfg(LocomotionVelocityRoughEnvCfg): - rewards: G1Rewards = G1Rewards() - - def __post_init__(self): - super().__post_init__() - self.scene.robot = G1_MINIMAL_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.events.push_robot = None - # TODO: TEMPORARILY DISABLED - adding this causes NaNs in the simulation - # self.events.add_base_mass = None - self.events.reset_robot_joints.params["position_range"] = (1.0, 1.0) - self.events.base_external_force_torque.params["asset_cfg"].body_names = ["torso_link"] - self.events.reset_base.params = { - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (0.0, 0.0), - "y": (0.0, 0.0), - "z": (0.0, 0.0), - "roll": (0.0, 0.0), - "pitch": (0.0, 0.0), - "yaw": (0.0, 0.0), - }, - } - self.events.base_com = None - - # Rewards - self.rewards.lin_vel_z_l2.weight = 0.0 - self.rewards.undesired_contacts = None - self.rewards.flat_orientation_l2.weight = -1.0 - self.rewards.action_rate_l2.weight = -0.005 - self.rewards.dof_acc_l2.weight = -1.25e-7 - self.rewards.dof_acc_l2.params["asset_cfg"] = SceneEntityCfg( - "robot", joint_names=[".*_hip_.*", ".*_knee_joint"] - ) - self.rewards.dof_torques_l2.weight = -1.5e-7 - self.rewards.dof_torques_l2.params["asset_cfg"] = SceneEntityCfg( - "robot", joint_names=[".*_hip_.*", ".*_knee_joint", ".*_ankle_.*"] - ) - - # Commands - self.commands.base_velocity.ranges.lin_vel_x = (0.0, 1.0) - self.commands.base_velocity.ranges.lin_vel_y = (-0.0, 0.0) - self.commands.base_velocity.ranges.ang_vel_z = (-1.0, 1.0) - - # terminations - self.terminations.base_contact.params["sensor_cfg"].body_names = "torso_link" - - -@configclass -class G1RoughEnvCfg_PLAY(G1RoughEnvCfg): - def __post_init__(self): - # post init of parent - super().__post_init__() - - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.episode_length_s = 40.0 - # spawn the robot randomly in the grid (instead of their terrain levels) - self.scene.terrain.max_init_terrain_level = None - # reduce the number of terrains to save memory - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - - self.commands.base_velocity.ranges.lin_vel_x = (1.0, 1.0) - self.commands.base_velocity.ranges.lin_vel_y = (0.0, 0.0) - self.commands.base_velocity.ranges.ang_vel_z = (-1.0, 1.0) - self.commands.base_velocity.ranges.heading = (0.0, 0.0) - # disable randomization for play - self.observations.policy.enable_corruption = False - # remove random pushing - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/__init__.py deleted file mode 100644 index 417865511f5f..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/__init__.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.contrib.velocity.config.go1 import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Velocity-Flat-UnitreeGo1-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:UnitreeGo1FlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UnitreeGo1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-UnitreeGo1-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:UnitreeGo1FlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UnitreeGo1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/flat_env_cfg.py deleted file mode 100644 index dd4012882075..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/flat_env_cfg.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import UnitreeGo1RoughEnvCfg - - -@configclass -class UnitreeGo1FlatEnvCfg(UnitreeGo1RoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=60, - nconmax=25, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - super().__post_init__() - self.rewards.flat_orientation_l2.weight = -2.5 - self.rewards.feet_air_time.weight = 0.25 - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - self.curriculum.terrain_levels = None - - -class UnitreeGo1FlatEnvCfg_PLAY(UnitreeGo1FlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/rough_env_cfg.py deleted file mode 100644 index cb3a602394a6..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go1/rough_env_cfg.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab.utils.configclass import configclass - -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import LocomotionVelocityRoughEnvCfg - -from isaaclab_assets.robots.unitree import UNITREE_GO1_CFG # isort: skip - - -@configclass -class UnitreeGo1RoughEnvCfg(LocomotionVelocityRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.robot = UNITREE_GO1_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.manager_call_max_mode = {"Scene": 1} - self.scene.terrain.terrain_generator.sub_terrains["boxes"].grid_height_range = (0.025, 0.1) - self.scene.terrain.terrain_generator.sub_terrains["random_rough"].noise_range = (0.01, 0.06) - self.scene.terrain.terrain_generator.sub_terrains["random_rough"].noise_step = 0.01 - self.actions.joint_pos.scale = 0.25 - self.events.push_robot = None - self.events.base_external_force_torque.params["asset_cfg"].body_names = "trunk" - self.events.reset_robot_joints.params["position_range"] = (1.0, 1.0) - self.events.reset_base.params = { - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (0.0, 0.0), - "y": (0.0, 0.0), - "z": (0.0, 0.0), - "roll": (0.0, 0.0), - "pitch": (0.0, 0.0), - "yaw": (0.0, 0.0), - }, - } - self.events.base_com = None - self.rewards.feet_air_time.params["sensor_cfg"].body_names = ".*_foot" - self.rewards.feet_air_time.weight = 0.01 - self.rewards.undesired_contacts = None - self.rewards.dof_torques_l2.weight = -0.0002 - self.rewards.track_lin_vel_xy_exp.weight = 1.5 - self.rewards.track_ang_vel_z_exp.weight = 0.75 - self.rewards.dof_acc_l2.weight = -2.5e-7 - self.terminations.base_contact.params["sensor_cfg"].body_names = "trunk" - - -@configclass -class UnitreeGo1RoughEnvCfg_PLAY(UnitreeGo1RoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.scene.terrain.max_init_terrain_level = None - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/__init__.py deleted file mode 100644 index 2d7f3b3212b8..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/__init__.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.velocity.config.go2 import agents - -## -# Register Gym environments. -## - -gym.register( - id="Isaac-Velocity-Flat-UnitreeGo2-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:UnitreeGo2FlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UnitreeGo2FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-UnitreeGo2-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:UnitreeGo2FlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UnitreeGo2FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/flat_env_cfg.py deleted file mode 100644 index 349cc450ca9f..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/flat_env_cfg.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import UnitreeGo2RoughEnvCfg - - -@configclass -class UnitreeGo2FlatEnvCfg(UnitreeGo2RoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=65, - nconmax=35, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - super().__post_init__() - self.rewards.flat_orientation_l2.weight = -2.5 - self.rewards.feet_air_time.weight = 0.25 - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - self.curriculum.terrain_levels = None - - -class UnitreeGo2FlatEnvCfg_PLAY(UnitreeGo2FlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/rough_env_cfg.py deleted file mode 100644 index a25748da9885..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/go2/rough_env_cfg.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab.utils.configclass import configclass - -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import LocomotionVelocityRoughEnvCfg - -from isaaclab_assets.robots.unitree import UNITREE_GO2_CFG # isort: skip - - -@configclass -class UnitreeGo2RoughEnvCfg(LocomotionVelocityRoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.robot = UNITREE_GO2_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.scene.terrain.terrain_generator.sub_terrains["boxes"].grid_height_range = (0.025, 0.1) - self.scene.terrain.terrain_generator.sub_terrains["random_rough"].noise_range = (0.01, 0.06) - self.scene.terrain.terrain_generator.sub_terrains["random_rough"].noise_step = 0.01 - self.actions.joint_pos.scale = 0.25 - self.events.push_robot = None - self.events.base_external_force_torque.params["asset_cfg"].body_names = "base" - self.events.reset_robot_joints.params["position_range"] = (1.0, 1.0) - self.events.reset_base.params = { - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (0.0, 0.0), - "y": (0.0, 0.0), - "z": (0.0, 0.0), - "roll": (0.0, 0.0), - "pitch": (0.0, 0.0), - "yaw": (0.0, 0.0), - }, - } - self.events.base_com = None - self.rewards.feet_air_time.params["sensor_cfg"].body_names = ".*_foot" - self.rewards.feet_air_time.weight = 0.01 - self.rewards.undesired_contacts = None - self.rewards.dof_torques_l2.weight = -0.0002 - self.rewards.track_lin_vel_xy_exp.weight = 1.5 - self.rewards.track_ang_vel_z_exp.weight = 0.75 - self.rewards.dof_acc_l2.weight = -2.5e-7 - self.terminations.base_contact.params["sensor_cfg"].body_names = "base" - - -@configclass -class UnitreeGo2RoughEnvCfg_PLAY(UnitreeGo2RoughEnvCfg): - def __post_init__(self): - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.scene.terrain.max_init_terrain_level = None - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/__init__.py deleted file mode 100644 index c731af7ca986..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/__init__.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.velocity.config.h1 import agents - -## -# Register Gym environments. -## - -# gym.register( -# id="Isaac-Velocity-Rough-H1-Warp-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.rough_env_cfg:H1RoughEnvCfg", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:H1RoughPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", -# }, -# ) - -# gym.register( -# id="Isaac-Velocity-Rough-H1-Warp-Play-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.rough_env_cfg:H1RoughEnvCfg_PLAY", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:H1RoughPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", -# }, -# ) - -gym.register( - id="Isaac-Velocity-Flat-H1-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:H1FlatEnvCfg", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:H1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Velocity-Flat-H1-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.flat_env_cfg:H1FlatEnvCfg_PLAY", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:H1FlatPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/flat_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/flat_env_cfg.py deleted file mode 100644 index a7021343464a..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/flat_env_cfg.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -from .rough_env_cfg import H1RoughEnvCfg - - -@configclass -class H1FlatEnvCfg(H1RoughEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=65, - nconmax=15, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - super().__post_init__() - self.scene.terrain.terrain_type = "plane" - self.scene.terrain.terrain_generator = None - self.curriculum.terrain_levels = None - self.rewards.feet_air_time.weight = 1.0 - self.rewards.feet_air_time.params["threshold"] = 0.6 - - -class H1FlatEnvCfg_PLAY(H1FlatEnvCfg): - def __post_init__(self) -> None: - super().__post_init__() - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.observations.policy.enable_corruption = False - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/rough_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/rough_env_cfg.py deleted file mode 100644 index 1b65e018c9e4..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/config/h1/rough_env_cfg.py +++ /dev/null @@ -1,131 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import SceneEntityCfg - -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp as mdp -from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import ( - LocomotionVelocityRoughEnvCfg, - RewardsCfg, -) - -## -# Pre-defined configs -## -from isaaclab_assets import H1_MINIMAL_CFG # isort: skip - - -@configclass -class H1Rewards(RewardsCfg): - """Reward terms for the MDP.""" - - termination_penalty = RewTerm(func=mdp.is_terminated, weight=-200.0) - lin_vel_z_l2 = None - track_lin_vel_xy_exp = RewTerm( - func=mdp.track_lin_vel_xy_yaw_frame_exp, - weight=1.0, - params={"command_name": "base_velocity", "std": 0.5}, - ) - track_ang_vel_z_exp = RewTerm( - func=mdp.track_ang_vel_z_world_exp, weight=1.0, params={"command_name": "base_velocity", "std": 0.5} - ) - feet_air_time = RewTerm( - func=mdp.feet_air_time_positive_biped, - weight=0.25, - params={ - "command_name": "base_velocity", - "sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*ankle_link"), - "threshold": 0.4, - }, - ) - feet_slide = RewTerm( - func=mdp.feet_slide, - weight=-0.25, - params={ - "sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*ankle_link"), - "asset_cfg": SceneEntityCfg("robot", body_names=".*ankle_link"), - }, - ) - dof_pos_limits = RewTerm( - func=mdp.joint_pos_limits, weight=-1.0, params={"asset_cfg": SceneEntityCfg("robot", joint_names=".*_ankle")} - ) - joint_deviation_hip = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.2, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=[".*_hip_yaw", ".*_hip_roll"])}, - ) - joint_deviation_arms = RewTerm( - func=mdp.joint_deviation_l1, - weight=-0.2, - params={"asset_cfg": SceneEntityCfg("robot", joint_names=[".*_shoulder_.*", ".*_elbow"])}, - ) - joint_deviation_torso = RewTerm( - func=mdp.joint_deviation_l1, weight=-0.1, params={"asset_cfg": SceneEntityCfg("robot", joint_names="torso")} - ) - - -@configclass -class H1RoughEnvCfg(LocomotionVelocityRoughEnvCfg): - rewards: H1Rewards = H1Rewards() - - def __post_init__(self): - super().__post_init__() - self.scene.robot = H1_MINIMAL_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - self.events.push_robot = None - self.events.reset_robot_joints.params["position_range"] = (1.0, 1.0) - self.events.base_external_force_torque.params["asset_cfg"].body_names = [".*torso_link"] - self.events.reset_base.params = { - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (0.0, 0.0), - "y": (0.0, 0.0), - "z": (0.0, 0.0), - "roll": (0.0, 0.0), - "pitch": (0.0, 0.0), - "yaw": (0.0, 0.0), - }, - } - self.events.base_com = None - self.rewards.undesired_contacts = None - self.rewards.flat_orientation_l2.weight = -1.0 - self.rewards.dof_torques_l2.weight = 0.0 - self.rewards.action_rate_l2.weight = -0.005 - self.rewards.dof_acc_l2.weight = -1.25e-7 - self.commands.base_velocity.ranges.lin_vel_x = (0.0, 1.0) - self.commands.base_velocity.ranges.lin_vel_y = (0.0, 0.0) - self.commands.base_velocity.ranges.ang_vel_z = (-1.0, 1.0) - self.terminations.base_contact.params["sensor_cfg"].body_names = ".*torso_link" - - -@configclass -class H1RoughEnvCfg_PLAY(H1RoughEnvCfg): - def __post_init__(self): - # post init of parent - super().__post_init__() - - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - self.episode_length_s = 40.0 - # spawn the robot randomly in the grid (instead of their terrain levels) - self.scene.terrain.max_init_terrain_level = None - # reduce the number of terrains to save memory - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.num_rows = 5 - self.scene.terrain.terrain_generator.num_cols = 5 - self.scene.terrain.terrain_generator.curriculum = False - - self.commands.base_velocity.ranges.lin_vel_x = (1.0, 1.0) - self.commands.base_velocity.ranges.lin_vel_y = (0.0, 0.0) - self.commands.base_velocity.ranges.ang_vel_z = (-1.0, 1.0) - self.commands.base_velocity.ranges.heading = (0.0, 0.0) - # disable randomization for play - self.observations.policy.enable_corruption = False - # remove random pushing - self.events.base_external_force_torque = None - self.events.push_robot = None diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/curriculums.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/curriculums.py deleted file mode 100644 index c1aa55114ab7..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/mdp/curriculums.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Curriculum functions for the velocity locomotion environment. - -Curriculum terms are not warp-managed (they run at reset time, not per-step), -so they remain torch-based. -""" - -from __future__ import annotations - -from collections.abc import Sequence -from typing import TYPE_CHECKING - -import torch - -from isaaclab.managers import SceneEntityCfg - -if TYPE_CHECKING: - from isaaclab.assets import Articulation - from isaaclab.envs import ManagerBasedRLEnv - from isaaclab.terrains import TerrainImporter - - -def terrain_levels_vel( - env: ManagerBasedRLEnv, env_ids: Sequence[int], asset_cfg: SceneEntityCfg = SceneEntityCfg("robot") -) -> torch.Tensor: - """Curriculum based on the distance the robot walked when commanded to move at a desired velocity.""" - asset: Articulation = env.scene[asset_cfg.name] - terrain: TerrainImporter = env.scene.terrain - command = env.command_manager.get_command("base_velocity") - distance = torch.norm(asset.data.root_pos_w.torch[env_ids, :2] - env.scene.env_origins[env_ids, :2], dim=1) - move_up = distance > terrain.cfg.terrain_generator.size[0] / 2 - move_down = distance < torch.norm(command[env_ids, :2], dim=1) * env.max_episode_length_s * 0.5 - move_down *= ~move_up - terrain.update_env_origins(env_ids, move_up, move_down) - return torch.mean(terrain.terrain_levels.float()) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/velocity_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/velocity_env_cfg.py deleted file mode 100644 index 52d53c3db268..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/locomotion/velocity/velocity_env_cfg.py +++ /dev/null @@ -1,296 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import math -from dataclasses import MISSING - -from isaaclab_experimental.managers import ObservationTermCfg as ObsTerm -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import SceneEntityCfg -from isaaclab_experimental.managers import TerminationTermCfg as DoneTerm - -import isaaclab.sim as sim_utils -from isaaclab.assets import ArticulationCfg, AssetBaseCfg -from isaaclab.envs import ManagerBasedRLEnvCfg -from isaaclab.managers import CurriculumTermCfg as CurrTerm -from isaaclab.managers import EventTermCfg as EventTerm -from isaaclab.managers import ObservationGroupCfg as ObsGroup -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.sensors import ContactSensorCfg -from isaaclab.terrains import TerrainImporterCfg -from isaaclab.utils.assets import ISAAC_NUCLEUS_DIR, ISAACLAB_NUCLEUS_DIR -from isaaclab.utils.configclass import configclass -from isaaclab.utils.noise import UniformNoiseCfg as Unoise - -import isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp as mdp - -## -# Pre-defined configs -## -from isaaclab.terrains.config.rough import ROUGH_TERRAINS_CFG # isort: skip - - -## -# Scene definition -## - - -@configclass -class MySceneCfg(InteractiveSceneCfg): - """Configuration for the terrain scene with a legged robot.""" - - # ground terrain - terrain = TerrainImporterCfg( - prim_path="/World/ground", - terrain_type="generator", - terrain_generator=ROUGH_TERRAINS_CFG, - max_init_terrain_level=5, - collision_group=-1, - physics_material=sim_utils.RigidBodyMaterialCfg( - friction_combine_mode="multiply", - restitution_combine_mode="multiply", - static_friction=1.0, - dynamic_friction=1.0, - ), - visual_material=sim_utils.MdlFileCfg( - mdl_path=f"{ISAACLAB_NUCLEUS_DIR}/Materials/TilesMarbleSpiderWhiteBrickBondHoned/TilesMarbleSpiderWhiteBrickBondHoned.mdl", - project_uvw=True, - texture_scale=(0.25, 0.25), - ), - debug_vis=False, - ) - # robots - robot: ArticulationCfg = MISSING - # sensors - contact_forces = ContactSensorCfg( - prim_path="{ENV_REGEX_NS}/Robot/.*", - filter_prim_paths_expr=[], - history_length=3, - track_air_time=True, - ) - # lights - sky_light = AssetBaseCfg( - prim_path="/World/skyLight", - spawn=sim_utils.DomeLightCfg( - intensity=750.0, - texture_file=f"{ISAAC_NUCLEUS_DIR}/Materials/Textures/Skies/PolyHaven/kloofendal_43d_clear_puresky_4k.hdr", - ), - ) - - -## -# MDP settings -## - - -@configclass -class CommandsCfg: - """Command specifications for the MDP.""" - - base_velocity = mdp.UniformVelocityCommandCfg( - asset_name="robot", - resampling_time_range=(10.0, 10.0), - rel_standing_envs=0.02, - rel_heading_envs=1.0, - heading_command=True, - heading_control_stiffness=0.5, - debug_vis=True, - ranges=mdp.UniformVelocityCommandCfg.Ranges( - lin_vel_x=(-1.0, 1.0), lin_vel_y=(-1.0, 1.0), ang_vel_z=(-1.0, 1.0), heading=(-math.pi, math.pi) - ), - ) - - -@configclass -class ActionsCfg: - """Action specifications for the MDP.""" - - joint_pos = mdp.JointPositionActionCfg(asset_name="robot", joint_names=[".*"], scale=0.5, use_default_offset=True) - - -@configclass -class PolicyCfg(ObsGroup): - """Observations for policy group.""" - - # observation terms (order preserved) - base_lin_vel = ObsTerm(func=mdp.base_lin_vel, noise=Unoise(n_min=-0.1, n_max=0.1)) - base_ang_vel = ObsTerm(func=mdp.base_ang_vel, noise=Unoise(n_min=-0.2, n_max=0.2)) - projected_gravity = ObsTerm( - func=mdp.projected_gravity, - noise=Unoise(n_min=-0.05, n_max=0.05), - ) - velocity_commands = ObsTerm(func=mdp.generated_commands, params={"command_name": "base_velocity"}) - joint_pos = ObsTerm(func=mdp.joint_pos_rel, noise=Unoise(n_min=-0.01, n_max=0.01)) - joint_vel = ObsTerm(func=mdp.joint_vel_rel, noise=Unoise(n_min=-1.5, n_max=1.5)) - actions = ObsTerm(func=mdp.last_action) - - def __post_init__(self): - self.enable_corruption = True - self.concatenate_terms = True - - -@configclass -class ObservationsCfg: - """Observation specifications for the MDP.""" - - # observation groups - policy: PolicyCfg = PolicyCfg() - - -@configclass -class EventCfg: - """Configuration for events.""" - - # FIXME(warp-migration): COM randomization in exp manager-based locomotion currently causes - # NaNs and is temporarily disabled. - # base_com = EventTerm( - # func=mdp.randomize_rigid_body_com, - # mode="startup", - # params={ - # "asset_cfg": SceneEntityCfg("robot", body_names="base"), - # "com_range": {"x": (-0.05, 0.05), "y": (-0.05, 0.05), "z": (-0.01, 0.01)}, - # }, - # ) - base_com = None - - # reset - base_external_force_torque = EventTerm( - func=mdp.apply_external_force_torque, - mode="reset", - params={ - "asset_cfg": SceneEntityCfg("robot", body_names="base"), - "force_range": (0.0, 0.0), - "torque_range": (-0.0, 0.0), - }, - ) - - reset_base = EventTerm( - func=mdp.reset_root_state_uniform, - mode="reset", - params={ - "pose_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5), "yaw": (-3.14, 3.14)}, - "velocity_range": { - "x": (-0.5, 0.5), - "y": (-0.5, 0.5), - "z": (-0.5, 0.5), - "roll": (-0.5, 0.5), - "pitch": (-0.5, 0.5), - "yaw": (-0.5, 0.5), - }, - }, - ) - - reset_robot_joints = EventTerm( - func=mdp.reset_joints_by_scale, - mode="reset", - params={ - "position_range": (0.5, 1.5), - "velocity_range": (0.0, 0.0), - }, - ) - - # interval - push_robot = EventTerm( - func=mdp.push_by_setting_velocity, - mode="interval", - interval_range_s=(10.0, 15.0), - params={"velocity_range": {"x": (-0.5, 0.5), "y": (-0.5, 0.5)}}, - ) - - -@configclass -class RewardsCfg: - """Reward terms for the MDP.""" - - # -- task - track_lin_vel_xy_exp = RewTerm( - func=mdp.track_lin_vel_xy_exp, weight=1.0, params={"command_name": "base_velocity", "std": math.sqrt(0.25)} - ) - track_ang_vel_z_exp = RewTerm( - func=mdp.track_ang_vel_z_exp, weight=0.5, params={"command_name": "base_velocity", "std": math.sqrt(0.25)} - ) - # -- penalties - lin_vel_z_l2 = RewTerm(func=mdp.lin_vel_z_l2, weight=-2.0) - ang_vel_xy_l2 = RewTerm(func=mdp.ang_vel_xy_l2, weight=-0.05) - dof_torques_l2 = RewTerm(func=mdp.joint_torques_l2, weight=-1.0e-5) - dof_acc_l2 = RewTerm(func=mdp.joint_acc_l2, weight=-2.5e-7) - action_rate_l2 = RewTerm(func=mdp.action_rate_l2, weight=-0.01) - feet_air_time = RewTerm( - func=mdp.feet_air_time, - weight=0.125, - params={ - "sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*FOOT"), - "command_name": "base_velocity", - "threshold": 0.5, - }, - ) - undesired_contacts = RewTerm( - func=mdp.undesired_contacts, - weight=-1.0, - params={"sensor_cfg": SceneEntityCfg("contact_forces", body_names=".*THIGH"), "threshold": 1.0}, - ) - # -- optional penalties - flat_orientation_l2 = RewTerm(func=mdp.flat_orientation_l2, weight=0.0) - dof_pos_limits = RewTerm(func=mdp.joint_pos_limits, weight=0.0) - - -@configclass -class TerminationsCfg: - """Termination terms for the MDP.""" - - time_out = DoneTerm(func=mdp.time_out, time_out=True) - base_contact = DoneTerm( - func=mdp.illegal_contact, - params={"sensor_cfg": SceneEntityCfg("contact_forces", body_names="base"), "threshold": 1.0}, - ) - - -@configclass -class CurriculumCfg: - """Curriculum terms for the MDP.""" - - terrain_levels = CurrTerm(func=mdp.terrain_levels_vel) - - -## -# Environment configuration -## - - -@configclass -class LocomotionVelocityRoughEnvCfg(ManagerBasedRLEnvCfg): - """Configuration for the locomotion velocity-tracking environment.""" - - # Scene settings - scene: MySceneCfg = MySceneCfg(num_envs=4096, env_spacing=2.5, replicate_physics=True) - # Basic settings - observations: ObservationsCfg = ObservationsCfg() - actions: ActionsCfg = ActionsCfg() - commands: CommandsCfg = CommandsCfg() - # MDP settings - rewards: RewardsCfg = RewardsCfg() - terminations: TerminationsCfg = TerminationsCfg() - events: EventCfg = EventCfg() - curriculum: CurriculumCfg = CurriculumCfg() - - def __post_init__(self): - """Post initialization.""" - # general settings - self.decimation = 4 - self.episode_length_s = 20.0 - # simulation settings - self.sim.dt = 1.0 / 200.0 - self.sim.render_interval = self.decimation - self.sim.physics_material = self.scene.terrain.physics_material - # update sensor update periods - if self.scene.contact_forces is not None: - self.scene.contact_forces.update_period = self.sim.dt - # check if terrain levels curriculum is enabled - if getattr(self.curriculum, "terrain_levels", None) is not None: - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.curriculum = True - else: - if self.scene.terrain.terrain_generator is not None: - self.scene.terrain.terrain_generator.curriculum = False diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/__init__.py deleted file mode 100644 index fe34199f2321..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Reach experimental task registrations (manager-based).""" diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/__init__.py deleted file mode 100644 index 460a30569089..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/franka/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/franka/__init__.py deleted file mode 100644 index 9f01c7b2e729..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/franka/__init__.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import gymnasium as gym - -# Reuse agent configs from the stable task package. -from isaaclab_tasks.core.reach.config.franka import agents - -## -# Register Gym environments. -## - -## -# Joint Position Control -## - -gym.register( - id="Isaac-Reach-Franka-Warp-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.joint_pos_env_cfg:FrankaReachEnvCfg", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:FrankaReachPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_ppo_cfg.yaml", - }, -) - -gym.register( - id="Isaac-Reach-Franka-Warp-Play-v0", - entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", - disable_env_checker=True, - kwargs={ - "env_cfg_entry_point": f"{__name__}.joint_pos_env_cfg:FrankaReachEnvCfg_PLAY", - "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_ppo_cfg.yaml", - "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:FrankaReachPPORunnerCfg", - "skrl_cfg_entry_point": f"{agents.__name__}:skrl_ppo_cfg.yaml", - }, -) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/franka/joint_pos_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/franka/joint_pos_env_cfg.py deleted file mode 100644 index d6d36afd15f5..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/franka/joint_pos_env_cfg.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import math - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.manipulation.reach.mdp as mdp -from isaaclab_tasks_experimental.manager_based.manipulation.reach.reach_env_cfg import ReachEnvCfg - -## -# Pre-defined configs -## -from isaaclab_assets import FRANKA_PANDA_CFG # isort: skip - - -## -# Environment configuration -## - - -@configclass -class FrankaReachEnvCfg(ReachEnvCfg): - sim: SimulationCfg = SimulationCfg( - dt=1 / 60, - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=50, - nconmax=20, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ), - ) - - def __post_init__(self): - # post init of parent - super().__post_init__() - - # switch robot to franka - self.scene.robot = FRANKA_PANDA_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - # override rewards - self.rewards.end_effector_position_tracking.params["asset_cfg"].body_names = ["panda_hand"] - self.rewards.end_effector_position_tracking_fine_grained.params["asset_cfg"].body_names = ["panda_hand"] - self.rewards.end_effector_orientation_tracking.params["asset_cfg"].body_names = ["panda_hand"] - - # override actions - self.actions.arm_action = mdp.JointPositionActionCfg( - asset_name="robot", joint_names=["panda_joint.*"], scale=0.5, use_default_offset=True - ) - # override command generator body - # end-effector is along z-direction - self.commands.ee_pose.body_name = "panda_hand" - self.commands.ee_pose.ranges.pitch = (math.pi, math.pi) - - -@configclass -class FrankaReachEnvCfg_PLAY(FrankaReachEnvCfg): - def __post_init__(self): - # post init of parent - super().__post_init__() - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - # disable randomization for play - self.observations.policy.enable_corruption = False diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/ur_10/__init__.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/ur_10/__init__.py deleted file mode 100644 index cdff71bf6523..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/ur_10/__init__.py +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -# UR10 env disabled: USD asset has composition errors (broken asset file). -# Fails on both torch baseline and warp with: -# RuntimeError: USD stage has composition errors while loading provided stage -# Re-enable once the UR10 USD asset is fixed. - -# import gymnasium as gym -# from isaaclab_tasks.core.reach.config.ur_10 import agents - -# gym.register( -# id="Isaac-Reach-UR10-Warp-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.joint_pos_env_cfg:UR10ReachEnvCfg", -# "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_ppo_cfg.yaml", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UR10ReachPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_ppo_cfg.yaml", -# }, -# ) - -# gym.register( -# id="Isaac-Reach-UR10-Warp-Play-v0", -# entry_point="isaaclab_experimental.envs:ManagerBasedRLEnvWarp", -# disable_env_checker=True, -# kwargs={ -# "env_cfg_entry_point": f"{__name__}.joint_pos_env_cfg:UR10ReachEnvCfg_PLAY", -# "rl_games_cfg_entry_point": f"{agents.__name__}:rl_games_ppo_cfg.yaml", -# "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:UR10ReachPPORunnerCfg", -# "skrl_cfg_entry_point": f"{agents.__name__}:skrl_ppo_cfg.yaml", -# }, -# ) diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/ur_10/joint_pos_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/ur_10/joint_pos_env_cfg.py deleted file mode 100644 index 42104e23b1b2..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/config/ur_10/joint_pos_env_cfg.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -import math - -from isaaclab_newton.physics import MJWarpSolverCfg, NewtonCfg - -from isaaclab.sim import SimulationCfg -from isaaclab.utils.configclass import configclass - -import isaaclab_tasks_experimental.manager_based.manipulation.reach.mdp as mdp -from isaaclab_tasks_experimental.manager_based.manipulation.reach.reach_env_cfg import ReachEnvCfg - -## -# Pre-defined configs -## -from isaaclab_assets import UR10_CFG # isort: skip - - -## -# Environment configuration -## - - -@configclass -class UR10ReachEnvCfg(ReachEnvCfg): - sim: SimulationCfg = SimulationCfg( - physics=NewtonCfg( - solver_cfg=MJWarpSolverCfg( - njmax=50, - nconmax=20, - cone="pyramidal", - impratio=1, - integrator="implicitfast", - ), - num_substeps=1, - debug_mode=False, - ) - ) - - def __post_init__(self): - # post init of parent - super().__post_init__() - - # switch robot to ur10 - self.scene.robot = UR10_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") - # override events - self.events.reset_robot_joints.params["position_range"] = (0.75, 1.25) - # override rewards - self.rewards.end_effector_position_tracking.params["asset_cfg"].body_names = ["ee_link"] - self.rewards.end_effector_position_tracking_fine_grained.params["asset_cfg"].body_names = ["ee_link"] - self.rewards.end_effector_orientation_tracking.params["asset_cfg"].body_names = ["ee_link"] - # override actions - self.actions.arm_action = mdp.JointPositionActionCfg( - asset_name="robot", joint_names=[".*"], scale=0.5, use_default_offset=True - ) - # override command generator body - # end-effector is along x-direction - self.commands.ee_pose.body_name = "ee_link" - self.commands.ee_pose.ranges.pitch = (math.pi / 2, math.pi / 2) - - -@configclass -class UR10ReachEnvCfg_PLAY(UR10ReachEnvCfg): - def __post_init__(self): - # post init of parent - super().__post_init__() - # make a smaller scene for play - self.scene.num_envs = 50 - self.scene.env_spacing = 2.5 - # disable randomization for play - self.observations.policy.enable_corruption = False diff --git a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/reach_env_cfg.py b/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/reach_env_cfg.py deleted file mode 100644 index e83a27e44db5..000000000000 --- a/source/isaaclab_tasks_experimental/isaaclab_tasks_experimental/manager_based/manipulation/reach/reach_env_cfg.py +++ /dev/null @@ -1,206 +0,0 @@ -# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). -# All rights reserved. -# -# SPDX-License-Identifier: BSD-3-Clause - -from dataclasses import MISSING - -from isaaclab_experimental.managers import ObservationTermCfg as ObsTerm -from isaaclab_experimental.managers import RewardTermCfg as RewTerm -from isaaclab_experimental.managers import SceneEntityCfg -from isaaclab_experimental.managers import TerminationTermCfg as DoneTerm - -import isaaclab.sim as sim_utils -from isaaclab.assets import ArticulationCfg, AssetBaseCfg -from isaaclab.envs import ManagerBasedRLEnvCfg -from isaaclab.managers import ActionTermCfg as ActionTerm -from isaaclab.managers import CurriculumTermCfg as CurrTerm -from isaaclab.managers import EventTermCfg as EventTerm -from isaaclab.managers import ObservationGroupCfg as ObsGroup -from isaaclab.scene import InteractiveSceneCfg -from isaaclab.utils.configclass import configclass -from isaaclab.utils.noise import UniformNoiseCfg as Unoise - -import isaaclab_tasks_experimental.manager_based.manipulation.reach.mdp as mdp - -## -# Scene definition -## - - -@configclass -class ReachSceneCfg(InteractiveSceneCfg): - """Configuration for the scene with a robotic arm.""" - - # world - ground = AssetBaseCfg( - prim_path="/World/ground", - spawn=sim_utils.GroundPlaneCfg(), - init_state=AssetBaseCfg.InitialStateCfg(pos=(0.0, 0.0, -1.05)), - ) - - # robots - robot: ArticulationCfg = MISSING - - # lights - light = AssetBaseCfg( - prim_path="/World/light", - spawn=sim_utils.DomeLightCfg(color=(0.75, 0.75, 0.75), intensity=2500.0), - ) - - -## -# MDP settings -## - - -@configclass -class CommandsCfg: - """Command terms for the MDP.""" - - ee_pose = mdp.UniformPoseCommandCfg( - asset_name="robot", - body_name=MISSING, - resampling_time_range=(4.0, 4.0), - debug_vis=True, - ranges=mdp.UniformPoseCommandCfg.Ranges( - pos_x=(0.35, 0.65), - pos_y=(-0.2, 0.2), - pos_z=(0.15, 0.5), - roll=(0.0, 0.0), - pitch=MISSING, # depends on end-effector axis - yaw=(-3.14, 3.14), - ), - ) - - -@configclass -class ActionsCfg: - """Action specifications for the MDP.""" - - arm_action: ActionTerm = MISSING - gripper_action: ActionTerm | None = None - - -@configclass -class ObservationsCfg: - """Observation specifications for the MDP.""" - - @configclass - class PolicyCfg(ObsGroup): - """Observations for policy group.""" - - # observation terms (order preserved) - joint_pos = ObsTerm(func=mdp.joint_pos_rel, noise=Unoise(n_min=-0.01, n_max=0.01)) - joint_vel = ObsTerm(func=mdp.joint_vel_rel, noise=Unoise(n_min=-0.01, n_max=0.01)) - pose_command = ObsTerm(func=mdp.generated_commands, params={"command_name": "ee_pose"}) - actions = ObsTerm(func=mdp.last_action) - - def __post_init__(self): - self.enable_corruption = True - self.concatenate_terms = True - - # observation groups - policy: PolicyCfg = PolicyCfg() - - -@configclass -class EventCfg: - """Configuration for events.""" - - reset_base = EventTerm( - func=mdp.reset_root_state_uniform, - mode="reset", - params={"pose_range": {}, "velocity_range": {}}, - ) - - reset_robot_joints = EventTerm( - func=mdp.reset_joints_by_scale, - mode="reset", - params={ - "position_range": (0.5, 1.5), - "velocity_range": (0.0, 0.0), - }, - ) - - -@configclass -class RewardsCfg: - """Reward terms for the MDP.""" - - # task terms - end_effector_position_tracking = RewTerm( - func=mdp.position_command_error, - weight=-0.2, - params={"asset_cfg": SceneEntityCfg("robot", body_names=MISSING), "command_name": "ee_pose"}, - ) - end_effector_position_tracking_fine_grained = RewTerm( - func=mdp.position_command_error_tanh, - weight=0.1, - params={"asset_cfg": SceneEntityCfg("robot", body_names=MISSING), "std": 0.1, "command_name": "ee_pose"}, - ) - end_effector_orientation_tracking = RewTerm( - func=mdp.orientation_command_error, - weight=-0.1, - params={"asset_cfg": SceneEntityCfg("robot", body_names=MISSING), "command_name": "ee_pose"}, - ) - - # action penalty - action_rate = RewTerm(func=mdp.action_rate_l2, weight=-0.0001) - joint_vel = RewTerm( - func=mdp.joint_vel_l2, - weight=-0.0001, - params={"asset_cfg": SceneEntityCfg("robot")}, - ) - - -@configclass -class TerminationsCfg: - """Termination terms for the MDP.""" - - time_out = DoneTerm(func=mdp.time_out, time_out=True) - - -@configclass -class CurriculumCfg: - """Curriculum terms for the MDP.""" - - action_rate = CurrTerm( - func=mdp.modify_reward_weight, params={"term_name": "action_rate", "weight": -0.005, "num_steps": 4500} - ) - - joint_vel = CurrTerm( - func=mdp.modify_reward_weight, params={"term_name": "joint_vel", "weight": -0.001, "num_steps": 4500} - ) - - -## -# Environment configuration -## - - -@configclass -class ReachEnvCfg(ManagerBasedRLEnvCfg): - """Configuration for the reach end-effector pose tracking environment.""" - - # Scene settings - scene: ReachSceneCfg = ReachSceneCfg(num_envs=4096, env_spacing=2.5) - # Basic settings - observations: ObservationsCfg = ObservationsCfg() - actions: ActionsCfg = ActionsCfg() - commands: CommandsCfg = CommandsCfg() - # MDP settings - rewards: RewardsCfg = RewardsCfg() - terminations: TerminationsCfg = TerminationsCfg() - events: EventCfg = EventCfg() - curriculum: CurriculumCfg = CurriculumCfg() - - def __post_init__(self): - """Post initialization.""" - # general settings - self.decimation = 2 - self.sim.render_interval = self.decimation - self.episode_length_s = 12.0 - self.viewer.eye = (3.5, 3.5, 3.5) - # simulation settings - self.sim.dt = 1.0 / 60.0 diff --git a/source/isaaclab_tasks_experimental/test/test_manager_based_warp_launch_replay.py b/source/isaaclab_tasks_experimental/test/test_manager_based_warp_launch_replay.py new file mode 100644 index 000000000000..8aa7f4a09875 --- /dev/null +++ b/source/isaaclab_tasks_experimental/test/test_manager_based_warp_launch_replay.py @@ -0,0 +1,360 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for recorded launches in task-specific manager-based Warp MDP terms.""" + +from types import SimpleNamespace + +import numpy as np +import pytest +import torch +import warp as wp +from isaaclab_experimental.managers import CurriculumManager, CurriculumTermCfg +from isaaclab_tasks_experimental.manager_based.classic.cartpole.mdp.rewards import joint_pos_target_l2 +from isaaclab_tasks_experimental.manager_based.classic.humanoid.mdp.rewards import progress_reward +from isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp.curriculums import TerrainLevelsVel +from isaaclab_tasks_experimental.manager_based.locomotion.velocity.mdp.terminations import terrain_out_of_bounds +from isaaclab_tasks_experimental.manager_based.manipulation.reach.mdp.curriculums import ModifyRewardWeight + +from isaaclab.terrains import TerrainImporter +from isaaclab.utils.warp import ProxyArray, WarpLaunchCache + +from isaaclab_tasks.core.cartpole.mdp.rewards import joint_pos_target_l2 as stable_joint_pos_target_l2 +from isaaclab_tasks.core.locomotion.mdp.rewards import progress_reward as stable_progress_reward +from isaaclab_tasks.core.velocity.mdp.terminations import terrain_out_of_bounds as stable_terrain_out_of_bounds + +wp.init() +pytestmark = pytest.mark.skipif(not wp.is_cuda_available(), reason="CUDA device required") + +_DEVICE = "cuda:0" +_NUM_ENVS = 4 + + +class _Scene(dict): + """Dictionary-backed scene with terrain configuration attributes.""" + + +def _make_launch_cache(mode: str = "replay") -> WarpLaunchCache: + """Create a launch cache with static-argument validation enabled.""" + return WarpLaunchCache(mode=mode, debug=True, device=_DEVICE) + + +def _make_terrain_env( + root_positions: np.ndarray, + *, + terrain_type: str, + size: tuple[float, float] = (4.0, 6.0), + num_rows: int = 2, + num_cols: int = 3, + border_width: float = 1.0, +): + """Create a lightweight environment for the terrain termination.""" + root_pos_w = wp.array(root_positions, dtype=wp.vec3f, device=_DEVICE) + asset = SimpleNamespace(data=SimpleNamespace(root_pos_w=ProxyArray(root_pos_w))) + scene = _Scene(robot=asset) + scene.cfg = SimpleNamespace(terrain=SimpleNamespace(terrain_type=terrain_type)) + scene.terrain = SimpleNamespace( + cfg=SimpleNamespace( + terrain_generator=SimpleNamespace( + size=size, + num_rows=num_rows, + num_cols=num_cols, + border_width=border_width, + ) + ) + ) + env = SimpleNamespace( + scene=scene, + num_envs=_NUM_ENVS, + device=_DEVICE, + _warp_launch=_make_launch_cache(), + ) + return env, root_pos_w + + +def _copy_vec3(array: wp.array, values: np.ndarray) -> None: + """Overwrite a vector array without changing its address.""" + wp.copy(array, wp.array(values, dtype=wp.vec3f, device=_DEVICE)) + + +class _CurriculumRewardManager: + """Minimal reward manager exposing one persistent Warp weight.""" + + def __init__(self): + self.weight_wp = wp.array([-0.01], dtype=wp.float32, device=_DEVICE) + + def get_term_weight_wp(self, name: str) -> wp.array(dtype=wp.float32): + assert name == "action_rate" + return self.weight_wp + + +def _make_reward_curriculum(mode: str) -> tuple[SimpleNamespace, CurriculumManager]: + """Create a one-term Reach reward curriculum in the requested launch mode.""" + env = SimpleNamespace( + num_envs=_NUM_ENVS, + device=_DEVICE, + sim=SimpleNamespace(is_playing=lambda: True), + reward_manager=_CurriculumRewardManager(), + _global_env_step_count_wp=wp.zeros(1, dtype=wp.int32, device=_DEVICE), + _warp_launch=_make_launch_cache(mode), + ) + cfg = CurriculumTermCfg( + func=ModifyRewardWeight, + params={"term_name": "action_rate", "weight": -0.005, "num_steps": 4500}, + ) + return env, CurriculumManager({"action_rate": cfg}, env) + + +def _run_reward_curriculum(mode: str) -> tuple[np.ndarray, ...]: + """Run the threshold and mask sequence for one launch mode.""" + env, manager = _make_reward_curriculum(mode) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device=_DEVICE) + empty_mask = wp.zeros(_NUM_ENVS, dtype=wp.bool, device=_DEVICE) + + env._global_env_step_count_wp.fill_(4500) + manager.compute(env_mask) + wp.synchronize() + at_threshold = env.reward_manager.weight_wp.numpy().copy() + state_at_threshold = manager._term_states_wp.numpy().copy() + + env._global_env_step_count_wp.fill_(4501) + manager.compute(empty_mask) + wp.synchronize() + after_empty_mask = env.reward_manager.weight_wp.numpy().copy() + state_after_empty_mask = manager._term_states_wp.numpy().copy() + + manager.compute(env_mask) + wp.synchronize() + after_selected_mask = env.reward_manager.weight_wp.numpy().copy() + state_after_selected_mask = manager._term_states_wp.numpy().copy() + return ( + at_threshold, + state_at_threshold, + after_empty_mask, + state_after_empty_mask, + after_selected_mask, + state_after_selected_mask, + ) + + +def _run_terrain_curriculum(mode: str) -> tuple[tuple[np.ndarray, ...], tuple[np.ndarray, ...]]: + """Run the same persistent terrain term twice in one launch mode.""" + initial_levels = np.array([0, 1, 1, 2], dtype=np.int64) + terrain = TerrainImporter.__new__(TerrainImporter) + terrain.device = _DEVICE + terrain.cfg = SimpleNamespace(terrain_generator=SimpleNamespace(size=(8.0, 8.0))) + terrain.max_terrain_level = 3 + terrain.terrain_levels = torch.tensor(initial_levels, dtype=torch.int64, device=_DEVICE) + terrain.terrain_types = torch.tensor([0, 1, 0, 1], dtype=torch.int64, device=_DEVICE) + terrain.terrain_origins = torch.zeros((3, 2, 3), dtype=torch.float32, device=_DEVICE) + terrain.env_origins = torch.zeros((_NUM_ENVS, 3), dtype=torch.float32, device=_DEVICE) + terrain._configure_warp_origin_views() + + root_pos_w = wp.array( + [[5.0, 0.0, 0.0], [1.0, 0.0, 0.0], [5.0, 0.0, 0.0], [1.0, 0.0, 0.0]], + dtype=wp.vec3f, + device=_DEVICE, + ) + command_wp = wp.array( + [[0.1, 0.0, 0.0], [1.0, 0.0, 0.0], [0.1, 0.0, 0.0], [1.0, 0.0, 0.0]], + dtype=wp.float32, + device=_DEVICE, + ) + scene = _Scene(robot=SimpleNamespace(data=SimpleNamespace(root_pos_w=ProxyArray(root_pos_w)))) + scene.device = _DEVICE + scene.terrain = terrain + env = SimpleNamespace( + num_envs=_NUM_ENVS, + device=_DEVICE, + scene=scene, + command_manager=SimpleNamespace(get_command_wp=lambda name: command_wp), + env_origins_wp=wp.from_torch(terrain.env_origins, dtype=wp.vec3f), + rng_state_wp=wp.array(np.arange(_NUM_ENVS, dtype=np.uint32) + 101, device=_DEVICE), + max_episode_length_s=10.0, + _warp_launch=_make_launch_cache(mode), + ) + term = TerrainLevelsVel(CurriculumTermCfg(func=TerrainLevelsVel), env) + env_mask = wp.array([True, True, False, False], dtype=wp.bool, device=_DEVICE) + out = wp.zeros(1, dtype=wp.float32, device=_DEVICE) + + def _launch_and_read() -> tuple[np.ndarray, ...]: + out.zero_() + term(env, env_mask, out) + wp.synchronize() + return ( + terrain.terrain_levels_wp.numpy().copy(), + term._move_up_wp.numpy().copy(), + term._move_down_wp.numpy().copy(), + out.numpy().copy(), + ) + + first = _launch_and_read() + wp.copy(terrain.terrain_levels_wp, wp.array(initial_levels, dtype=wp.int64, device=_DEVICE)) + second = _launch_and_read() + return first, second + + +def test_terrain_curriculum_eager_replay_parity_for_masked_selection(): + """Terrain updates should preserve masked semantics after command replay.""" + eager_calls = _run_terrain_curriculum("eager") + replay_calls = _run_terrain_curriculum("replay") + expected = ( + np.array([1, 0, 1, 2], dtype=np.int64), + np.array([True, False, False, False]), + np.array([False, True, False, False]), + np.array([1.0], dtype=np.float32), + ) + + for calls in (eager_calls, replay_calls): + for call in calls: + for actual, expected_value in zip(call, expected): + np.testing.assert_allclose(actual, expected_value) + for eager_call, replay_call in zip(eager_calls, replay_calls): + for eager_value, replay_value in zip(eager_call, replay_call): + np.testing.assert_allclose(replay_value, eager_value) + + +def test_reward_curriculum_eager_replay_parity_for_mask_and_threshold(): + """Reach reward scheduling should retain strict threshold and reset-mask semantics.""" + eager = _run_reward_curriculum("eager") + replay = _run_reward_curriculum("replay") + + for replay_value, eager_value in zip(replay, eager): + np.testing.assert_allclose(replay_value, eager_value) + np.testing.assert_allclose(eager[0], [-0.01]) + np.testing.assert_allclose(eager[1], [-0.01]) + np.testing.assert_allclose(eager[2], [-0.01]) + np.testing.assert_allclose(eager[3], [0.0]) + np.testing.assert_allclose(eager[4], [-0.005]) + np.testing.assert_allclose(eager[5], [-0.005]) + + +def test_reward_curriculum_recorded_launches_compose_with_capture(): + """A warmed Reach curriculum should replay both launches inside CUDA capture.""" + env, manager = _make_reward_curriculum("replay") + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device=_DEVICE) + + env._global_env_step_count_wp.fill_(4500) + manager.compute(env_mask) + wp.synchronize() + env._global_env_step_count_wp.fill_(4501) + + with wp.ScopedCapture() as capture: + manager.compute(env_mask) + wp.capture_launch(capture.graph) + wp.synchronize() + + np.testing.assert_allclose(env.reward_manager.weight_wp.numpy(), [-0.005]) + np.testing.assert_allclose(manager._term_states_wp.numpy(), [-0.005]) + + +def test_terrain_out_of_bounds_replays_inside_capture(): + """Terrain termination should replay with fresh data inside CUDA capture.""" + positions = np.array([[0.0, 0.0, 0.0], [4.1, 0.0, 0.0], [0.0, 9.1, 0.0], [-3.0, -8.0, 0.0]], dtype=np.float32) + env, root_pos_w = _make_terrain_env(positions, terrain_type="generator") + out = wp.zeros(_NUM_ENVS, dtype=wp.bool, device=_DEVICE) + asset_cfg = SimpleNamespace(name="robot") + + terrain_out_of_bounds(env, out, asset_cfg=asset_cfg, distance_buffer=1.0) + torch.testing.assert_close( + wp.to_torch(out), stable_terrain_out_of_bounds(env, asset_cfg=asset_cfg, distance_buffer=1.0) + ) + + replay_positions = np.array([[4.2, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, -9.2, 0.0], [3.5, 8.5, 0.0]], dtype=np.float32) + _copy_vec3(root_pos_w, replay_positions) + terrain_out_of_bounds(env, out, asset_cfg=asset_cfg, distance_buffer=1.0) + torch.testing.assert_close( + wp.to_torch(out), stable_terrain_out_of_bounds(env, asset_cfg=asset_cfg, distance_buffer=1.0) + ) + + with wp.ScopedCapture() as capture: + terrain_out_of_bounds(env, out, asset_cfg=asset_cfg, distance_buffer=1.0) + captured_positions = np.array( + [[0.0, 9.5, 0.0], [-4.5, 0.0, 0.0], [0.0, 0.0, 0.0], [4.5, 9.5, 0.0]], dtype=np.float32 + ) + _copy_vec3(root_pos_w, captured_positions) + wp.capture_launch(capture.graph) + torch.testing.assert_close( + wp.to_torch(out), stable_terrain_out_of_bounds(env, asset_cfg=asset_cfg, distance_buffer=1.0) + ) + + +def test_terrain_out_of_bounds_does_not_share_configuration_between_envs(): + """Terrain geometry from one environment should not leak into another.""" + positions = np.full((_NUM_ENVS, 3), 100.0, dtype=np.float32) + generator_env, _ = _make_terrain_env(positions, terrain_type="generator") + plane_env, _ = _make_terrain_env(positions, terrain_type="plane") + asset_cfg = SimpleNamespace(name="robot") + generator_out = wp.zeros(_NUM_ENVS, dtype=wp.bool, device=_DEVICE) + plane_out = wp.ones(_NUM_ENVS, dtype=wp.bool, device=_DEVICE) + + terrain_out_of_bounds(generator_env, generator_out, asset_cfg=asset_cfg) + terrain_out_of_bounds(plane_env, plane_out, asset_cfg=asset_cfg) + + torch.testing.assert_close(wp.to_torch(generator_out), stable_terrain_out_of_bounds(generator_env, asset_cfg)) + torch.testing.assert_close(wp.to_torch(plane_out), stable_terrain_out_of_bounds(plane_env, asset_cfg)) + + +def test_progress_reward_replays_stateful_reset_and_compute(): + """The stateful progress term should replay against persistent state buffers.""" + positions = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [3.0, 4.0, 0.0]], dtype=np.float32) + root_pos_w = wp.array(positions, dtype=wp.vec3f, device=_DEVICE) + asset = SimpleNamespace(data=SimpleNamespace(root_pos_w=ProxyArray(root_pos_w))) + env = SimpleNamespace( + scene={"robot": asset}, + num_envs=_NUM_ENVS, + device=_DEVICE, + step_dt=0.1, + _warp_launch=_make_launch_cache(), + termination_manager=SimpleNamespace(time_outs=torch.zeros(_NUM_ENVS, dtype=torch.bool, device=_DEVICE)), + extras={}, + ) + target = (3.0, 4.0, 0.0) + cfg = SimpleNamespace(params={"target_pos": target}) + term = progress_reward(cfg, env) + stable_term = stable_progress_reward(env, cfg) + env_mask = wp.array([True, False, True, False], dtype=wp.bool, device=_DEVICE) + env_ids = torch.tensor([0, 2], dtype=torch.int64, device=_DEVICE) + asset_cfg = SimpleNamespace(name="robot") + + term.reset(env_mask=env_mask) + stable_term.reset(env_ids) + torch.testing.assert_close(wp.to_torch(term.potentials), stable_term.potentials) + + reset_positions = np.array([[3.0, 4.0, 0.0], [1.0, 0.0, 0.0], [3.0, 0.0, 0.0], [3.0, 4.0, 0.0]], dtype=np.float32) + _copy_vec3(root_pos_w, reset_positions) + term.reset(env_mask=env_mask) + stable_term.reset(env_ids) + torch.testing.assert_close(wp.to_torch(term.potentials), stable_term.potentials) + + out = wp.zeros(_NUM_ENVS, dtype=wp.float32, device=_DEVICE) + term(env, out, target_pos=target, asset_cfg=asset_cfg) + torch.testing.assert_close(wp.to_torch(out), stable_term(env, target_pos=target, asset_cfg=asset_cfg)) + + next_positions = np.array([[2.0, 4.0, 0.0], [2.0, 4.0, 0.0], [3.0, 2.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float32) + _copy_vec3(root_pos_w, next_positions) + term(env, out, target_pos=target, asset_cfg=asset_cfg) + torch.testing.assert_close(wp.to_torch(out), stable_term(env, target_pos=target, asset_cfg=asset_cfg)) + + +def test_cartpole_reward_records_static_target_variants(): + """Changing a static Cartpole target should select a separate recorded launch.""" + joint_pos = wp.array([[0.0], [2.0], [-1.0], [3.0]], dtype=wp.float32, device=_DEVICE) + joint_mask = wp.array([True], dtype=wp.bool, device=_DEVICE) + out = wp.zeros(_NUM_ENVS, dtype=wp.float32, device=_DEVICE) + asset = SimpleNamespace(data=SimpleNamespace(joint_pos=ProxyArray(joint_pos))) + env = SimpleNamespace( + scene={"robot": asset}, + num_envs=_NUM_ENVS, + device=_DEVICE, + _warp_launch=_make_launch_cache(), + ) + asset_cfg = SimpleNamespace(name="robot", joint_mask=joint_mask, joint_ids=[0]) + + joint_pos_target_l2(env, out, target=0.0, asset_cfg=asset_cfg) + torch.testing.assert_close(wp.to_torch(out), stable_joint_pos_target_l2(env, target=0.0, asset_cfg=asset_cfg)) + + joint_pos_target_l2(env, out, target=1.0, asset_cfg=asset_cfg) + torch.testing.assert_close(wp.to_torch(out), stable_joint_pos_target_l2(env, target=1.0, asset_cfg=asset_cfg)) diff --git a/source/isaaclab_tasks_experimental/test/test_manager_based_warp_noise_cfg.py b/source/isaaclab_tasks_experimental/test/test_manager_based_warp_noise_cfg.py new file mode 100644 index 000000000000..52efdf2f0420 --- /dev/null +++ b/source/isaaclab_tasks_experimental/test/test_manager_based_warp_noise_cfg.py @@ -0,0 +1,38 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp-native noise selection in built-in manager-based tasks.""" + +from isaaclab_experimental.utils.noise import NoiseCfg +from isaaclab_tasks_experimental.manager_based.locomotion.velocity.velocity_env_cfg import ( + PolicyCfg as VelocityPolicyCfg, +) +from isaaclab_tasks_experimental.manager_based.manipulation.reach.reach_env_cfg import ( + ObservationsCfg as ReachObservationsCfg, +) + +from isaaclab.utils.noise import UniformNoiseCfg as StableUniformNoiseCfg + + +def test_stable_uniform_noise_is_not_a_warp_noise_config(): + """Stable Torch noise should not satisfy the experimental manager's Warp type check.""" + assert not isinstance(StableUniformNoiseCfg(), NoiseCfg) + + +def test_builtin_manager_tasks_use_warp_uniform_noise(): + """Every configured velocity and Reach observation corruption should use Warp noise.""" + velocity_policy = VelocityPolicyCfg() + reach_policy = ReachObservationsCfg.PolicyCfg() + noisy_terms = [ + velocity_policy.base_lin_vel, + velocity_policy.base_ang_vel, + velocity_policy.projected_gravity, + velocity_policy.joint_pos, + velocity_policy.joint_vel, + reach_policy.joint_pos, + reach_policy.joint_vel, + ] + + assert all(isinstance(term.noise, NoiseCfg) for term in noisy_terms) diff --git a/source/isaaclab_tasks_experimental/test/test_terrain_out_of_bounds.py b/source/isaaclab_tasks_experimental/test/test_terrain_out_of_bounds.py new file mode 100644 index 000000000000..f04d225433a8 --- /dev/null +++ b/source/isaaclab_tasks_experimental/test/test_terrain_out_of_bounds.py @@ -0,0 +1,41 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Tests for Warp locomotion termination terms.""" + +from types import SimpleNamespace + +import numpy as np +import warp as wp +from isaaclab_tasks_experimental.core.velocity.mdp.terminations import terrain_out_of_bounds + + +class _Scene(dict): + """Minimal scene mapping with terrain configuration.""" + + +def _make_env(terrain_type: str, root_x: float = 0.0): + root_pos_w = wp.array(np.full((2, 3), (root_x, 0.0, 0.0), dtype=np.float32), dtype=wp.vec3f, device="cpu") + scene = _Scene(robot=SimpleNamespace(data=SimpleNamespace(root_pos_w=SimpleNamespace(warp=root_pos_w)))) + scene.cfg = SimpleNamespace(terrain=SimpleNamespace(terrain_type=terrain_type)) + scene.terrain = SimpleNamespace( + cfg=SimpleNamespace( + terrain_generator=SimpleNamespace(size=(2.0, 2.0), num_rows=2, num_cols=2, border_width=0.0) + ) + ) + return SimpleNamespace(scene=scene, num_envs=2, device="cpu") + + +def test_terrain_out_of_bounds_does_not_share_terrain_state_between_envs(): + """A plane environment must not poison bounds used by a later generated-terrain environment.""" + for attribute in ("_is_warmed_up", "_is_plane", "_half_width", "_half_height"): + if hasattr(terrain_out_of_bounds, attribute): + delattr(terrain_out_of_bounds, attribute) + + out = wp.zeros(2, dtype=wp.bool, device="cpu") + terrain_out_of_bounds(_make_env("plane"), out) + terrain_out_of_bounds(_make_env("generator", root_x=2.0), out, distance_buffer=0.5) + + np.testing.assert_array_equal(out.numpy(), np.ones(2, dtype=np.bool_))