.. _warp-environments:

Warp Experimental Environments
==============================

.. note::

   The warp environment infrastructure lives in ``isaaclab_experimental`` and
   ``isaaclab_tasks_experimental``. It's an experimental feature.

The experimental extensions introduce **warp-first** environment infrastructure with CUDA graph capture
support. All environment-side computation (observations, rewards, resets, actions) runs as pure Warp
kernels, eliminating Python overhead and enabling CUDA graph capture for maximum throughput.

Throughout this page, **stable** refers to the standard Torch-based implementations in the
``isaaclab`` and ``isaaclab_tasks`` packages (the default runtime, used when ``--frontend warp``
is not passed). Their Warp counterparts live in the ``isaaclab_experimental`` and
``isaaclab_tasks_experimental`` packages.


Workflows
~~~~~~~~~

Two environment workflows are supported:

**Direct workflow** — ``DirectRLEnvWarp`` base class. You implement the step loop, observations,
rewards, and resets directly in your env class using Warp kernels.

**Manager-based workflow** — ``ManagerBasedRLEnvWarp`` base class. You define MDP terms as
standalone Warp-kernel functions and compose them via configuration.


Available Environments
~~~~~~~~~~~~~~~~~~~~~~

Direct Warp Environments
^^^^^^^^^^^^^^^^^^^^^^^^

Direct tasks share the stable task configuration. ``--frontend warp`` resolves
the stable ``<task>_direct_env:<Name>Env`` entry point to the mirrored
``<task>_warp_env:<Name>WarpEnv`` implementation and swaps only the environment
class. Registrations can use ``warp_entry_point`` as an optional override when
the implementation cannot follow this convention. Stable tasks with a 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 Execution (``--frontend warp``)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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:

- ``Isaac-Cartpole``
- ``Isaac-Ant``
- ``Isaac-Humanoid``
- ``Isaac-Reach-Franka``
- ``Isaac-Reach-UR10``
- ``Isaac-Velocity-Flat-AnymalD``
- ``Isaac-Velocity-Flat-Cassie``
- ``Isaac-Velocity-Flat-G1``
- ``Isaac-Velocity-Flat-H1``
- ``Isaac-Velocity-Flat-UnitreeGo2``

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
~~~~~~~~~~~

.. code-block:: bash

    # Direct workflow: stable task, warp env implementation
    uv run isaaclab train --rl_library rsl_rl \
        --task Isaac-Cartpole-Direct --frontend warp presets=newton_mjwarp --num_envs 4096
    uv run isaaclab play --rl_library rsl_rl \
        --task Isaac-Cartpole-Direct --frontend warp presets=newton_mjwarp \
        --num_envs 32 --checkpoint latest --visualizer newton_gl

    # Manager-based workflow: stable task on the warp runtime
    uv run isaaclab train --rl_library rsl_rl \
        --task Isaac-Velocity-Flat-AnymalD --frontend warp presets=newton_mjwarp --num_envs 4096
    uv run isaaclab play --rl_library rsl_rl \
        --task Isaac-Velocity-Flat-AnymalD --frontend warp presets=newton_mjwarp \
        --num_envs 32 --checkpoint latest --visualizer newton_gl

All RL libraries with warp-compatible wrappers are supported: RSL-RL, RL Games, SKRL, and
Stable-Baselines3.

.. note::

   ``--video`` is rejected on the warp path, for both ``train`` and ``play``: video
   recording requires the standard torch frontend. To record a rollout, replay the same
   checkpoint with ``--frontend torch``.


Performance Comparison
~~~~~~~~~~~~~~~~~~~~~~

Step time comparison between the stable (torch/manager) and warp (CUDA graph captured) variants,
both running on the Newton physics backend. Measured over 300 iterations with 4096 environments.

.. note::

   The warp migration is an ongoing effort. Several components (e.g. scene write, actuator models)
   have not yet been migrated to Warp kernels and still run through torch. Further performance
   improvements are expected as these components are migrated.

.. list-table::
   :header-rows: 1
   :widths: 42 10 14 14 12

   * - Env
     - Type
     - Stable Step (us)
     - Warp Step (us)
     - Change
   * - Isaac-Cartpole-Direct
     - Direct
     - 5,274
     - 4,331
     - -17.88%
   * - Isaac-Ant-Direct
     - Direct
     - 6,368
     - 3,128
     - -50.88%
   * - Isaac-Humanoid-Direct
     - Direct
     - 13,937
     - 10,783
     - -22.63%
   * - Isaac-Reorient-Cube-Allegro-Direct
     - Direct
     - 82,950
     - 74,570
     - -10.10%
   * - Isaac-Cartpole
     - Manager
     - 7,971
     - 3,642
     - -54.31%
   * - Isaac-Ant
     - Manager
     - 9,781
     - 4,672
     - -52.23%
   * - Isaac-Humanoid
     - Manager
     - 17,653
     - 12,505
     - -29.16%
   * - Isaac-Reach-Franka
     - Manager
     - 11,458
     - 7,813
     - -31.83%
   * - Isaac-Velocity-Flat-AnymalD
     - Manager
     - 32,294
     - 23,977
     - -25.75%
   * - Isaac-Velocity-Flat-Cassie
     - Manager
     - 17,320
     - 10,706
     - -38.19%
   * - Isaac-Velocity-Flat-G1
     - Manager
     - 34,487
     - 27,300
     - -20.84%
   * - Isaac-Velocity-Flat-H1
     - Manager
     - 22,202
     - 15,864
     - -28.55%
   * - Isaac-Velocity-Flat-UnitreeGo2
     - Manager
     - 15,221
     - 9,966
     - -34.52%


Which Workflows Benefit Most
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The savings come from eliminating Python / torch overhead in the env's step loop, so envs
gain in proportion to how much of their step time was previously dominated by per-kernel CPU
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** (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%,
  Cartpole -18%, Allegro hand -10%). Direct envs that already wrote most of their work as
  GPU kernels see modest gains; ones with substantial Python state machinery see large ones.
- **Compute-heavy / scene-write-heavy envs** (Allegro hand, large humanoids) — see smaller
  relative gains because the warp-side savings are amortised over a heavier step. Components
  that still go through torch (scene write, actuator models) currently bound the floor; this
  is expected to improve as remaining components migrate to warp.

If your env's step time is dominated by physics or scene I/O, expect modest gains. If it has
many small MDP terms or a lot of Python in the step loop, expect large ones. Use the
benchmarking workflow below to measure on your task before committing to a migration.


Limitations
~~~~~~~~~~~

The warp env path is experimental and has the following known constraints. These are
specific to warp envs; for Newton physics maturity and specialist guides see
:ref:`physics-backends-newton`.

**Physics backend**

- **Newton only.** PhysX is not supported under the warp env path. Asset and sensor
  ``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 the typed selector ``physics=newton_mjwarp``,
  which fails loudly if the task has no Newton physics preset).

**MDP coverage**

- Only the terms listed under :ref:`Available Warp MDP Terms <warp-env-migration>` are
  implemented. Stable envs that depend on un-migrated terms cannot be run on the warp path
  until those terms are ported.
- Some scene-side operations (asset write, actuator models, certain sensor types) still go
  through torch. They participate in the step but are not yet captured into the graph; they
  set the lower bound on observed step time.
- Sensors that depend on the Kit RTX renderer (camera-based observations) cannot be combined
  with the warp env path — they need Kit, which the warp runtime does not initialise.

**API differences vs stable**

- Reset events use a boolean ``env_mask`` (``wp.array(dtype=wp.bool)``) instead of an
  ``env_ids`` list. This is required for capture safety: variable-length indexing changes
  graph topology and breaks replay.
- All buffers must be pre-allocated in ``__init__``. There is no dynamic allocation inside
  the captured step loop, so observation / reward / termination output dimensions must be
  known at env init.
- Term functions write into a pre-allocated ``out`` buffer rather than returning a tensor.
  See :doc:`warp_environment_migration` for the kernel + launch pattern.
- Code inside the captured step loop must follow capture-safety rules (no
  ``wp.to_torch``, no torch arithmetic, no lazy-evaluated properties, no Python branching
  on GPU data). See the *Capture Safety* section in :doc:`warp_environment_migration` for the
  full set of rules.


Benchmarking Your Environment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The performance table above was produced with ``isaaclab benchmark training``,
which runs a fixed iteration count and reports step-time statistics. Use the same command
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

    # Torch frontend (stable managers)
    uv run isaaclab benchmark training \
        --rl_library rsl_rl \
        --task <Task-Name> \
        --num_envs 4096 \
        --max_iterations 500 \
        --benchmark_formatter summary \
        --output_path benchmarks/torch \
        presets=newton_mjwarp

    # Warp frontend (warp managers, CUDA-graph captured) — same task id
    uv run isaaclab benchmark training \
        --rl_library rsl_rl \
        --task <Task-Name> \
        --frontend warp \
        --num_envs 4096 \
        --max_iterations 500 \
        --benchmark_formatter summary \
        --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.

**Sweep across all available tasks**

Run ``isaaclab benchmark training`` for each task in the stable set (cartpole, ant, humanoid,
locomotion, manipulation) and again with ``--frontend warp``, then diff the two output
directories.

**What to look at in the output**

- *Step time (min / mean / max)*: the headline number — what each env step costs.
- *Iteration time*: includes policy update; useful for end-to-end training throughput.
- *Capture overhead*: for warp runs, the first few iterations include CUDA graph capture
  cost; exclude those when comparing steady-state numbers.

**Estimating before you migrate**

If you can't run the warp variant yet (e.g. the task isn't ported), measure the stable
step time and look at where it's spent:

- ``num_envs * step_time`` dominated by physics → expect modest warp gains.
- ``step_time`` dominated by ``manager.compute_*`` calls → expect large gains, since those
  are exactly what the warp managers replace with captured kernel launches.

Use ``--num_steps`` on ``runtime.py`` for a no-policy step-time microbenchmark
when you want to isolate env overhead from policy compute.


Migrating Existing Environments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For step-by-step instructions on porting an existing stable env (or writing a new warp
env from scratch) — covering project layout, the kernel + launch pattern shared by
observations / rewards / events / terminations / actions, capture-safety rules, and
parity testing — see :doc:`warp_environment_migration`.
