Repository navigation
Select external environments in Experiments by class path - #1730
cvolkcvolk wants to merge 5 commits into
Conversation
c0f5dce to
d690a69
Compare
d690a69 to
7dacb14
Compare
Read each typed environment's argparse config from `EnvironmentRegistry` and remove the `_legacy_argparse_cfg_type` attribute from the 19 environment factories. ## Why this change? The argparse adapter in `isaaclab_arena_environments/cli.py` found a factory's config through a private `_legacy_argparse_cfg_type` class attribute. Each factory already declares its config in its `ArenaEnvironmentFactory[Cfg]` base, and `@register_environment` records it in `EnvironmentRegistry`, so the attribute repeated it. A typed factory registered with `@register_environment` but without the attribute broke the environment CLI. `add_example_environments_cli_args` builds a subparser for every registered environment, so that one factory made the build assert for all of them. This is the first of three PRs that replace #1453 and let an Experiment select an external environment by class path. The next one, #1729, turns the external environment examples into typed factories, which will not set the private attribute. The third, #1730, adds the class path to Experiments. ## What changes? - `_registered_environment_cfg_type` in `cli.py` reads the config from `EnvironmentRegistry.get_environment_cfg_types()` by the factory's `name`. `add_environment_cli_args` and `_environment_cfg_from_cli` use it. - The 19 first-party factories drop `_legacy_argparse_cfg_type`. - `test_every_registered_cli_adapter_uses_its_typed_cfg_defaults` no longer compares the attribute with the registry. It still checks that every registered factory gets its registered config back from the CLI. `--external_environment_class_path` still registers its class without a config, so a typed factory loaded through that flag gets no CLI flags yet. Building the environment subcommands then fails with an error saying that the flag does not support typed factories yet. This includes a subclass of a first-party factory, which on `main` inherited `_legacy_argparse_cfg_type` and worked. The next PR, #1729, makes the flag use the factory's own `@register_environment` registration. ## Validation - For all 19 registered environments, the registry lookup returns the same config class as the removed attribute on `main`. - In the Arena Docker image, the related tests pass when run locally: `test_environment_cfgs.py`, `test_environment_registry.py`, `test_legacy_graph_environment_cli.py`, `test_arena_env_graph_spec.py`, `test_isaaclab_interop.py`, `test_arena_experiment_config_loader.py`, `test_asset_registry.py`, `test_environment_runner.py`, and `test_all_environments_have_default_args`. - Rebased onto `main` at `a170ca3610` (2026-11-27). No `_legacy_argparse_cfg_type` is left. The same tests and `test_external_environment.py` pass locally. --------- Signed-off-by: Clemens Volk <cvolk@nvidia.com>
7dacb14 to
33fca18
Compare
Let environments defined outside Arena use a config dataclass and a
factory class with `build(cfg)`, like the built-in environments, and
delete `ExampleEnvironmentBase`.
Loading the class with `--external_environment_class_path` registers it
with its config, so it doesn't need `@register_environment`. Migrate
both external environment examples.
## Why this change?
Environments defined outside Arena subclass `ExampleEnvironmentBase`. It
builds the environment from an argparse `Namespace` in
`get_env(args_cli)` and declares its flags in `add_cli_args(parser)`.
Both methods are abstract, so every external environment writes its own
argparse flags by hand, one `parser.add_argument` per value, while
built-in environments get their flags generated from their config.
Built-in environments already moved to a config dataclass and
`build(cfg)`. External environments have no config dataclass, so the
Experiment Runner cannot build them.
An external environment also cannot be written as a config dataclass and
a factory class today. `--external_environment_class_path` imports the
class and registers it with plain `EnvironmentRegistry.register()`,
which records no config.
The next PR lets an Experiment select an external environment by class
path. It needs external environments to have a config dataclass.
## What changes?
- **Breaking change:** `ExampleEnvironmentBase` is deleted. An
environment defined outside Arena now needs a config dataclass that
subclasses `ArenaEnvironmentCfg` and a factory that subclasses
`ArenaEnvironmentFactory[Cfg]` and implements `build(cfg)`, as built-in
environments do. It does not need `@register_environment`: loading the
factory by path registers it. `add_environment_cli_args` and
`build_environment_from_cli` in `isaaclab_arena_environments/cli.py`
lose their `ExampleEnvironmentBase` branches. Flags for external
environments are generated from their config fields, as for built-in
ones.
- **Breaking change:** `--external_environment_class_path` takes
`module_path.ClassName`, like `policy.type` and `--policy_type`, instead
of `module_path:ClassName`. A class path with a colon fails with the
dotted form to use, and `import_class_from_path` loses its `separator`
argument.
- `import_external_environment_factory` replaces
`parse_and_return_external_environment_from_string`. It registers the
built-in environments first, imports the class with
`import_class_from_path(path)`, asserts that it is an
`ArenaEnvironmentFactory` subclass, and registers it with its config
under its `name`. A factory that its module already registered with
`@register_environment` is used as is. A factory whose name is taken,
for example by a built-in environment, gets an error that names the
class that owns it. Repeated calls return the same class.
`add_example_environments_cli_args` and the Isaac Lab
`environment_registration_callback` use it. The `--help` text of the
flag now describes the `module_path.ClassName` format instead of asking
for an environment name.
- `basic.py` and `advanced.py` in
`isaaclab_arena_examples/external_environments/` each have a config
dataclass and a factory class. Their config has an `object` field that
defaults to `cracker_box`, so the `--object` flag keeps working. The
commands in their module docstrings now show the generated `--object`
flag.
- `external_environments.rst` opens with what a user can do, describes
the two classes, and shows the run command with one class path example.
The steps for migrating from `ExampleEnvironmentBase` go into the
release migration guide, not this page. The locomanipulation and
reinforcement learning environment setup pages showed outdated
`ExampleEnvironmentBase` copies of their classes. They now include the
real classes with `literalinclude`.
- The flag tests in `test_environment_cfgs.py` import a module written
to `tmp_path`, so only the flag can register it. The Isaac Lab callback
test now selects the advanced example, which no earlier test imports.
`test_typed_experiment_ignores_unconfigured_external_environment` uses a
local factory, because the basic example now registers itself with a
config.
Experiments still cannot select external environments. The next PR adds
that. Until then, use the Policy Runner to evaluate one.
## Usage example
Before:
```python
import argparse
from isaaclab_arena_environments.example_environment_base import ExampleEnvironmentBase
class ExternalFrankaTableEnvironment(ExampleEnvironmentBase):
name: str = "franka_table"
def get_env(self, args_cli: argparse.Namespace):
...
pick_up_object = self.asset_registry.get_asset_by_name(args_cli.object)()
...
@staticmethod
def add_cli_args(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--object", type=str, default="cracker_box")
```
After:
```python
from dataclasses import dataclass
from isaaclab_arena.environments.arena_environment_factory import ArenaEnvironmentCfg, ArenaEnvironmentFactory
@DataClass
class ExternalFrankaTableEnvironmentCfg(ArenaEnvironmentCfg):
object: str = "cracker_box"
class ExternalFrankaTableEnvironment(ArenaEnvironmentFactory[ExternalFrankaTableEnvironmentCfg]):
name: str = "franka_table"
def build(self, cfg: ExternalFrankaTableEnvironmentCfg):
...
pick_up_object = self.asset_registry.get_asset_by_name(cfg.object)()
...
```
The flags come from the config fields, so only the class path in the
command changes, from `module_path:ClassName` to
`module_path.ClassName`:
```bash
python isaaclab_arena/evaluation/policy_runner.py \
--policy_type zero_action --num_steps 10 \
--external_environment_class_path isaaclab_arena_examples.external_environments.basic.ExternalFrankaTableEnvironment \
franka_table --object cracker_box
```
After #1730, an Experiment can also select the environment by its class
path and set `object` in YAML.
## Validation
- On `main` at `21b745cbb6` (#1716 merged). The changes are the same as
when they were tested on #1716's head. Locally,
`test_environment_cfgs.py`, `test_external_environment.py` (which runs
both examples, now without the decorator, through `policy_runner.py`
with the flag, and the Isaac Lab callback),
`test_environment_registry.py`, `test_isaaclab_interop.py`,
`test_arena_experiment_config_loader.py`, `test_environment_runner.py`,
`test_legacy_graph_environment_cli.py`,
`test_all_environments_have_default_args`, and the docs build with `-W`
pass.
- Removing the `import_external_environment_factory` call from
`add_example_environments_cli_args` made the flag test fail with
`invalid choice`, so the test depends on the flag. The test, now
`test_external_environment_flag_registers_the_factory`, loads a factory
only through the flag, with and without `@register_environment`.
- A script checked each field type listed in `external_environments.rst`
against `add_dataclass_cli_args`. The listed types parse correctly. A
plain `Enum`, an `IntEnum`, and `list[Literal[...]]` reject every value.
`list[bool]` and `Literal[True, False]` read `False` as `True`, and a
nested dataclass gets the text in its first field. `tuple`, `dict`, and
`int | str` fields fail while the parser is built.
- The `literalinclude` blocks on the locomanipulation and reinforcement
learning pages show the intended lines.
- Not checked: `environment_runner.py` with
`--external_environment_class_path`, because it needs the Kit GUI, and
Isaac Lab training scripts with an external environment beyond
`test_external_environment_registration_callback`.
---------
Signed-off-by: Clemens Volk <cvolk@nvidia.com>
ecf6507 to
fc326e3
Compare
fc326e3 to
137c8e3
Compare
de0be96 to
c6377fb
Compare
137c8e3 to
c3bfe13
Compare
Merge the two external environment examples, `basic.py` and `advanced.py`, into one: `isaaclab_arena_examples/external_environments/franka_table_environment.py`. Its environment, `ExternalFrankaTableEnvironment` (`franka_table`), defines its own task, embodiment and episode recorder terms. ## Why this change? The two examples shared their config and scene line for line: `basic.py` was `advanced.py` without the custom task, embodiment and recorder terms. This is the example new users copy when they build on Arena, so it should be one file, not called "advanced", and the environment should not be named after its task (`franka_table_with_task`). ## What changes? - `franka_table_environment.py` replaces `basic.py` and `advanced.py`. `ExternalFrankaTableEnvironment` builds the table scene with the custom `SuccessAfterNStepsTask`, the `franka_ik_soft` embodiment and two episode recorder terms. - "Defining Environments in Your Repository" shows the config and factory. "Your Own Tasks and Embodiments" shows the task, the embodiment and the end of `build(cfg)`, and points back to the first page for the run command. Both pages are reworded in plain, short sentences; the second page also shows `record_object_final_height` and no longer says the task goes to the `ArenaEnvBuilder`. - `test_external_environment.py` tests the one example; its two Policy Runner tests become one. - The `author-environment` skill links the new file. - The class paths `isaaclab_arena_examples.external_environments.basic.ExternalFrankaTableEnvironment` and `isaaclab_arena_examples.external_environments.advanced.ExternalFrankaTableWithTaskEnvironment` no longer exist. Use `isaaclab_arena_examples.external_environments.franka_table_environment.ExternalFrankaTableEnvironment`. #1730 refers to the example classes in `external_environments_experiment.yaml`; whichever PR merges second updates the path. ## Validation - Built the docs and checked the rendered code blocks: the first page shows the config and factory of `franka_table_environment.py`, the second the task, the embodiment and the end of `build(cfg)`. --------- Signed-off-by: Clemens Volk <cvolk@nvidia.com>
c6377fb to
2b9bac4
Compare
environment.type now accepts the class path module_path.ClassName of an environment factory, as policy.type already does. Before, an environment defined in the user's own repository could only be evaluated with policy_runner.py and --external_environment_class_path. A registered name is looked up first, so built-in environments keep their names. Otherwise the loader imports the factory with import_external_environment_factory, which registers it with its config and rejects a name that another class owns. A type that is neither a registered name nor a class path still lists the registered names. build_typed_experiment takes an environment_cfg_type_resolver in place of the environment_cfg_types mapping, so each Run's type is looked up when that Run is built. Any exception from a type resolver is now reported as an error that names the Run and the type, with the original exception chained. Importing the user's module can raise anything, such as a NameError, and before this such an error did not say which Run caused it. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
serialize_arena_experiment_to_yaml wrote the registered name for every environment. A process that loads the saved Experiment registers only the environments in isaaclab_arena_environments by name, so a saved Experiment with an external environment did not load again. Write module_path.ClassName for factories outside that package, as is already done for policies outside isaaclab_arena.policy. A nested class has a __qualname__ such as Group.Factory, and the loader cannot import a path built from it. Build the path from __name__ and check that the module attribute it names is the same class. A nested factory or policy now stops serialization with an error that asks to define it at the top level of its module. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Add external_environments_experiment.yaml, which selects the example factory ExternalFrankaTableEnvironment by its dotted class path and evaluates it with the zero_action policy for 50 steps. Replace the policy_runner.py subprocess test with one experiment_runner.py test on that Experiment, which checks that the Run completes. The callback test for Isaac Lab scripts stays. The example module docstring now shows the experiment_runner.py command instead of the policy_runner.py one. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
The first Arena in Your Repository page showed only policy_runner.py commands, and the Experiments concept page said the Policy Runner is still needed to evaluate an environment loaded with --external_environment_class_path. An Experiment now selects such an environment by the class path of its factory. On the first page, show a Run given on the command line that selects the example by its class path, and include the example Experiment file with its experiment_runner.py command. One paragraph keeps the flag documented for the Environment Runner and Isaac Lab scripts, which take the same class path. On the concept page, list the class path where it says what environment.type accepts, and remove the Policy Runner sentence. The run-experiment skill no longer sends external environments to the Policy Runner. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
import_external_environment_factory sat in the section of cli.py that goes away with the argparse frontend, but Experiments now use it, so move it above that section. The serializer saves a factory by name only when its module is a top-level module of isaaclab_arena_environments, because a new process registers only those by name. The unknown-type message calls its list "available names", and a nested policy is now tested like a nested environment factory. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
c3bfe13 to
2667189
Compare
Confidence Score: 4/5
[Medium impact] The changes affect how Arena Experiment runs load and save environment and policy configurations. The PR appears safe to merge; the fresh-process reload test is a non-blocking improvement. Findings
SummaryExperiments can now select external environment factories through
cvolkcvolk explicitly deferred removing ignored shared flags from the Experiment Runner’s parser. Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["environment.type"] --> B{"YAML path?"}
B -->|Yes| C["Build graph environment config"]
B -->|No| D{"Registered name?"}
D -->|Yes| E["Use registered config class"]
D -->|No| F["Import factory class path"]
F --> G["Register factory and check name ownership"]
G --> E
E --> H["Build typed Run config"]
H --> I["Find factory by config class"]
I --> J["Build environment"]
Reviews (1) · Last reviewed commit: "Keep the shared import helper out of the..." · Reviewed by Greptile |
| serialized_path = tmp_path / "serialized_experiment.yaml" | ||
| serialized_path.write_text(serialized_experiment, encoding="utf-8") | ||
| assert load_arena_experiment_from_config_file(serialized_path, device="cuda:0") == experiment_cfg |
There was a problem hiding this comment.
Fresh-process reload goes untested
This reload uses the same process that already imported and registered the external factory. It would still pass if the loader stopped importing an unregistered factory from the saved class path, leaving the fresh-process behavior unprotected.
Reload the saved YAML in a fresh subprocess, with the temporary module on PYTHONPATH, and check the resulting config there.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
|
||
| A class nested in another class or defined in a function has no such path. | ||
| """ | ||
| class_path = f"{class_type.__module__}.{class_type.__name__}" |
There was a problem hiding this comment.
🔵 Classes in __main__ pass this check
A factory or policy defined in a script or notebook has __module__ == "__main__". import_module("__main__") finds it, so the assert passes and the serializer writes __main__.MyEnvironment. The process that loads the YAML, such as the OSMO experiment runner task, imports its own __main__. It then fails with __main__ has no MyEnvironment after the job has been scheduled, not here. Could we reject it here too?
| class_path = f"{class_type.__module__}.{class_type.__name__}" | |
| class_path = f"{class_type.__module__}.{class_type.__name__}" | |
| assert class_type.__module__ != "__main__", ( | |
| f"{class_path} is defined in the running script. Move it to an importable module so a saved Experiment can" | |
| " select it by class path." | |
| ) |
| AssertionError, | ||
| "unknown environment type 'missing_environment'", | ||
| ValueError, | ||
| "Run 'invalid': could not resolve environment type 'missing_environment'", |
There was a problem hiding this comment.
🔵 No test for the new policy-type error
_resolve_cfg_type now also wraps policy.type failures (typed_experiment_loader.py:136). An unknown policy used to raise AssertionError and now raises ValueError("... Run 'x': could not resolve policy type ..."), but only the environment case is tested. Could we add a case to this parametrize list, with environment: {type: pick_and_place_maple_table} and policy: {type: missing_policy}, expecting ValueError and "Run 'invalid': could not resolve policy type 'missing_policy'"? _policy_cfg_type_for_name_or_class_path raises KeyError for it, so this needs no simulator.
🤖 Isaac Lab-Arena Review BotSummaryThis PR lets an Experiment select an external environment by the class path of its factory in Findings🔵 Improvement: 🔵 Improvement: Test Coverage and CI Cost
VerdictMinor fixes needed |
Let `experiment_runner.py` run an Experiment given only on the command line, and let an override change a Run's environment or policy type. ## Why this change? Every run except the smoke test needed a checked-in Experiment YAML file, and an override could not change a Run's `environment.type` or `policy.type`. `policy_runner.py` runs without a file, so this closes one of the gaps before it can be retired. ## What changes? Overrides change the Experiment the same way editing its YAML file would. Without `--experiment_config`, they start from an empty Experiment. Run without a file: ```bash python isaaclab_arena/evaluation/experiment_runner.py \ runs.quick.environment.type=kitchen_pick_and_place \ runs.quick.policy.type=zero_action \ runs.quick.rollout_limit.num_steps=50 ``` Change a Run's environment in a file: ```bash python isaaclab_arena/evaluation/experiment_runner.py \ --experiment_config isaaclab_arena_environments/experiment_configs/getting_started_experiment.yaml \ runs.baseline.environment.type=kitchen_pick_and_place \ runs.baseline.environment.object=mustard_bottle ``` An Experiment given on the command line is named `<timestamp>_experiment` unless `name=` is set. The full override rules are on the Experiments concept page. Changes to existing behavior: - `experiment_runner.py` without a file and without overrides no longer runs a smoke test; it stops with an error. The install check in the README and the installation page passes its Run on the command line, and `smoke_test_experiment.yaml` is removed. - A Run whose `environment.type` or `policy.type` differs from `shared` no longer inherits that `shared` section. Fields both types have, such as `embodiment`, must be set in the Run. - A mapping override replaces the mapping instead of merging into it, and `extend_list(...)` is rejected. - A variation written as a dotted key in one place and nested in another now fails. Before, one of the two values was silently ignored. In the code, `read_untyped_experiment` in the new `isaaclab_arena/hydra/untyped_experiment.py` reads the file and applies the overrides before Isaac Sim starts, and `build_typed_experiment` builds the typed configs from that untyped Experiment. #1730 changes a parameter of `build_typed_experiment`; whichever PR merges second adapts it. ## Validation - The other 56 checked-in Experiment files give the same typed configs as on `main`. - Ran the new install check command end to end. Run `smoke_test` completed. - Ran the no-file example above end to end in the Arena Docker image. Run `quick` completed, and the Experiment was named `2026-11-30_10-34-00_experiment`. - An OSMO `--dry_run` with an `experiment_cfg.*` override renders the workflow with the new value in that Run only. --------- Signed-off-by: Clemens Volk <cvolk@nvidia.com>
|
AI-drafted (Claude Code), reviewed by me. Closing in favor of PR 1804. With |
Let an Experiment select an environment from your own repository by the class path of its factory, for example
environment.type: my_package.my_environment.ExternalFrankaTableEnvironment. Saved Experiments keep that class path, so they load again.Stacked on #1737.
Why this change?
An Experiment could only select built-in environments and graph-spec YAML files. An environment from your own repository is registered only when its module is imported, and nothing in an Experiment imported it. So the only way to evaluate it was
policy_runner.py --external_environment_class_path.#1729 gave external environments a config, like the built-in ones. The Experiment loader only needed a way to import the factory.
What changes?
environment.typeacceptsmodule_path.ClassName. Registered names are looked up first. Otherwise the loader imports the factory withimport_external_environment_factory, the same function the--external_environment_class_pathflag uses.build_typed_experimenttakes anenvironment_cfg_type_resolverinstead of a mapping, so each Run's type is looked up when the Run is built.serialize_arena_experiment_to_yamlwrites the class path for external environments, so the saved file loads again in a new process.external_environments_experiment.yamlruns the example environment, and itsexperiment_runner.pytest replaces thepolicy_runner.pytest. The docs and therun-experimentskill use Experiments for external environments.environment_runner.py,policy_runner.pyand Isaac Lab scripts keep--external_environment_class_path.experiment_runner.pystill accepts that flag and ignores it, like other shared flags; rejecting them is a follow-up.Usage example
Before:
After, as a Run given on the command line (the same Run works in an Experiment YAML file):
Validation
franka_tablecompleted with a success rate of 1.0.franka_tablewas not registered. It gave the same config. Before this PR, the saved file named the environmentfranka_tableand did not load there.