Repository navigation
Select external environments in Experiments by class path - #1453
cvolkcvolk wants to merge 7 commits into
Conversation
The argparse adapter looked up each factory's config through a private _legacy_argparse_cfg_type attribute that repeated the config type already registered by @register_environment. Read it from EnvironmentRegistry instead and drop the 19 duplicate declarations. A typed factory defined outside isaaclab_arena_environments can now use the argparse paths without setting the private attribute. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
External environments now use the same contract as built-in ones: an ArenaEnvironmentFactory[Cfg] subclass with build(cfg), decorated with @register_environment. ExampleEnvironmentBase and its argparse-only get_env()/add_cli_args() path are removed. This is a breaking change for environments defined outside Arena. --external_environment_class_path now imports the module and checks that the decorator registered the class under its name, instead of registering it a second time without a config. Repeated imports return the same factory, and a factory that reuses a built-in name is rejected. Migrate both examples in isaaclab_arena_examples/external_environments to typed factories. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
environment.type now accepts the dotted Python class path of an environment factory, the same rule policy.type already follows. When the name is not registered, the loader imports the module after the built-in environments are registered, and checks that the factory's @register_environment decorator registered it under its name. The loader resolves only the environment types its Runs select, instead of looking up every registered environment. A registered environment without a config no longer breaks loading Experiments that do not use it. The serializer writes the class path for factories outside isaaclab_arena_environments, so a serialized Experiment reloads in a fresh process where only built-in environments are registered by name. Add an example Experiment for the two external environment examples and run it through experiment_runner.py in the subprocess tests, replacing the policy_runner.py tests. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Rewrite the Arena in Your Repository pages for the typed factory contract: a config dataclass, an ArenaEnvironmentFactory[Cfg] subclass with build(cfg), and @register_environment. Show how an Experiment selects the factory by its dotted class path, how command-line scripts keep using --external_environment_class_path, and how to migrate an environment from ExampleEnvironmentBase. The Policy Runner is no longer needed for external environments. Update the runner note in the Experiments concept page, the run-experiment skill, and the catalog pages that described environments as ExampleEnvironmentBase subclasses. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Resolving environment.type before composition dropped the Run name from the error, so a typo in one Run of a long Experiment did not say which Run was wrong. _compose_typed_config_from_yaml_selector now calls the resolver itself and reports a failure as a ValueError that names the Run, the section, and the selector, which the loader extends with the Experiment path. This also removes the "Available types" check that could no longer fail. Point out the command-line form module_path:ClassName when it is used as an environment.type, since Experiments use module_path.ClassName. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
The removed Policy Runner subprocess tests were the only coverage of --external_environment_class_path through the environment subcommand parser that environment_runner.py and graph-YAML builds use. Parse the flag, the environment name, and a generated config flag through get_isaaclab_arena_environments_cli_parser instead. Drop comments that still referred to the removed per-environment add_cli_args() and get_env() adapters. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
- List the dotted class path as a form of environment.type on the Experiments concept page. - Add a migration step for values that get_env() read from shared runner flags such as num_envs. - List the field types that command-line flags can be generated for. - State when a factory with a built-in name is rejected. - Add a rollout limit to the example docstring Experiments so they run as shown. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
[High impact] Refactors how environments are selected and loaded in experiments. The PR appears safe to merge, with a non-blocking saved-YAML issue for nested external factories. Findings
SummaryExperiments can select external environment factories by dotted class path.
Acknowledged by
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Run environment.type] --> B{Selector form}
B -->|Graph YAML path| C[Graph configuration]
B -->|Registered name| D[Registered factory]
B -->|Dotted class path| E[Import factory module]
E --> F[Check registered class matches]
F --> D
D --> G[Resolve registered config]
G --> H[Compose typed Run]
C --> H
H --> I[Build environment]
Reviews (1) · Last reviewed commit: "Clarify the external environment docs" · Reviewed by Greptile |
| environment_selector = environment_type.name | ||
| # Only built-in environments are registered by name when a fresh process loads the YAML. | ||
| if not environment_type.__module__.startswith("isaaclab_arena_environments."): | ||
| environment_selector = f"{environment_type.__module__}.{environment_type.__qualname__}" |
There was a problem hiding this comment.
Saved nested factories cannot reload
A nested factory can load through a module-level alias, but saving it writes its dotted __qualname__. For example, my_envs.Factory may point to Group.Factory; saving writes my_envs.Group.Factory. The loader then tries to import my_envs.Group as a module and fails, so the saved Experiment cannot reload.
Make the loader resolve nested class names, or check that the generated path can be imported before writing it.
| pass | ||
| def test_external_environment_flag_adds_the_registered_factory_subcommand(monkeypatch): | ||
| """Select an external typed factory through the environment subcommand parser used by runner scripts.""" | ||
| from isaaclab_arena_examples.external_environments.basic import ( |
There was a problem hiding this comment.
🟡 Flag test passes without the flag
This import runs @register_environment, so franka_table is registered before the parser reads --external_environment_class_path. If the import_external_environment(environment) call in add_example_environments_cli_args were removed, this test would still pass. The same import (and the class-path test in test_arena_experiment_config_loader.py) runs earlier in the Phase 1 process, so test_external_environment_registration_callback no longer proves the flag works either. The Policy Runner subprocess tests that used the flag are removed.
Could this test use a module that only the flag imports, for example one written to tmp_path with monkeypatch.syspath_prepend, and assert the name is not registered before parsing? The callback test could select advanced:ExternalFrankaTableWithTaskEnvironment, which no Phase 1 test imports.
| ensure_environments_registered() | ||
| environment_factory_type = getattr(importlib.import_module(module_path), class_name) | ||
| factory_class_path = f"{module_path}.{class_name}" | ||
| environment_name = getattr(environment_factory_type, "name", None) |
There was a problem hiding this comment.
🔵 Check the factory type instead of reading name with getattr
If the selected class is not a factory, for example the config class ...basic.ExternalFrankaTableEnvironmentCfg, this reports "not registered. Decorate it with @register_environment", and following that advice fails inside the decorator because the config has no name. ArenaEnvironmentFactory declares name, so could we check the type and then read it directly? The registration assert below still covers an undecorated factory.
| environment_name = getattr(environment_factory_type, "name", None) | |
| assert isinstance(environment_factory_type, type) and issubclass( | |
| environment_factory_type, ArenaEnvironmentFactory | |
| ), f"{factory_class_path} is not an ArenaEnvironmentFactory subclass." | |
| environment_name = environment_factory_type.name |
| cfg_type = cfg_type_resolver(selector) | ||
| except (AssertionError, ImportError, AttributeError) as exc: | ||
| # Resolvers do not know which Run selected the type; add it so multi-Run Experiments point at the bad Run. | ||
| raise ValueError(f"Run '{run_name}' selects unknown {section_name} type '{selector}': {exc}") from exc |
There was a problem hiding this comment.
🔵 "unknown" is misleading when the type exists
The resolver also imports the user's module and runs the registration checks, so a missing dependency inside that module, or the duplicate-name check in import_registered_environment_factory, is reported as "selects unknown environment type" even though the type exists. The original message is kept after the colon, so only the wording is off. Could it be neutral? The regex in test_unknown_environment_type_names_the_run would need the same change.
| raise ValueError(f"Run '{run_name}' selects unknown {section_name} type '{selector}': {exc}") from exc | |
| raise ValueError(f"Run '{run_name}' could not resolve {section_name} type '{selector}': {exc}") from exc |
| The fields of the environment's configuration become command-line flags. Flags can be generated | ||
| for fields of type ``bool``, ``int``, ``float``, ``str``, an ``Enum`` or ``Literal``, an optional | ||
| of these, or a list of these: |
There was a problem hiding this comment.
🔵 Listed field types are broader than what works
add_dataclass_cli_args handles fewer types than this lists. A plain Enum rejects every value, because argparse compares the converted member with the member.value choices; only class Mode(str, Enum) works. list[Literal[...]] rejects every value, and list[bool] parses False as True. Types such as tuple[...] or dict[...] fail while the parser is built, and because the parser adds a subcommand for every registered environment, that breaks these scripts for all environments once the module is imported. Could the sentence list only what works and say what happens otherwise?
| The fields of the environment's configuration become command-line flags. Flags can be generated | |
| for fields of type ``bool``, ``int``, ``float``, ``str``, an ``Enum`` or ``Literal``, an optional | |
| of these, or a list of these: | |
| The fields of the environment's configuration become command-line flags. Flags can be generated | |
| for fields of type ``bool``, ``int``, ``float``, ``str``, a ``Literal``, a string-valued enum such | |
| as ``class Mode(str, Enum)``, an optional of these, or a list of ``int``, ``float``, or ``str``. | |
| Other field types, such as ``tuple[...]``, stop these scripts from building their parser once | |
| your module is imported: |
| #. Replace Policy Runner commands that used ``--external_environment_class_path`` with an | ||
| Experiment that selects the factory by its class path, as shown above. Note that the | ||
| Experiment uses a dotted path (``module_path.ClassName``), while the command-line flag uses | ||
| ``module_path:ClassName``. |
There was a problem hiding this comment.
🔵 torchrun runs can't follow this step
The Experiment Runner rejects --distributed, and the Experiments concept page still sends torchrun runs to the Policy Runner, which keeps accepting --external_environment_class_path. Could this step say so?
| #. Replace Policy Runner commands that used ``--external_environment_class_path`` with an | |
| Experiment that selects the factory by its class path, as shown above. Note that the | |
| Experiment uses a dotted path (``module_path.ClassName``), while the command-line flag uses | |
| ``module_path:ClassName``. | |
| #. Replace Policy Runner commands that used ``--external_environment_class_path`` with an | |
| Experiment that selects the factory by its class path, as shown above. Note that the | |
| Experiment uses a dotted path (``module_path.ClassName``), while the command-line flag uses | |
| ``module_path:ClassName``. Multi-GPU runs with ``torchrun`` still use the Policy Runner, | |
| which keeps accepting ``--external_environment_class_path``. |
🤖 Isaac Lab-Arena Review BotSummaryThis PR moves external environments onto the typed Findings🔵 Improvement: Test Coverage and CI Cost🟡 Warning: VerdictMinor fixes needed |
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>
Closed: replaced by three smaller PRs on top of current
main: #1716 (read argparse environment configs from the registry), #1729 (replaceExampleEnvironmentBasewith typed external factories) and #1730 (select external environments in Experiments by class path).Let Arena Experiments select environments defined outside Arena by the dotted class path of their factory, and replace
ExampleEnvironmentBasewith the typed factory contract the built-in environments already use.This is a draft for review and discussion. It will be split into smaller PRs afterwards.
Why this change?
Environments defined in a user's own repository could only be evaluated with
policy_runner.py:load_arena_experiment_from_yamlaccepted anenvironment.typeonly if it was a graph-spec path or a key of the mapping built by_registered_environment_cfg_types(). Nothing imported a user's module.experiment_runner.pyparses--external_environment_class_path, but neither the YAML nor the JSON loader reads it, so no Run can select that environment.ExampleEnvironmentBasewithget_env(args_cli)andadd_cli_args(parser), has no typed config.ExampleEnvironmentBase.build(cfg)raisedNotImplementedError, so even an imported environment could not be built from YAML.There were also two related problems:
_registered_environment_cfg_types()calledEnvironmentRegistry.get_environment_cfg_typefor every registered name before composing any Run. If any registered environment had no config, every YAML load in that process failed with "must register a config".experiment_runner.pydoes not reach this, because it loads the Experiment before building any Run. In this repository it shows up as a test-ordering problem:test_external_environment_registration_callbackleavesfranka_tableregistered without a config, and thetest_osmo_experiment_workflow.pytests that load an Experiment, such astest_embedded_graph_environment_experiment_composes_through_experiment_runner_loader, then fail in the same pytest process. Load typed experiments alongside external environments #1379 addresses this by filtering the lookup; this PR removes the lookup instead.ArenaEnvironmentFactory[Cfg]with@register_environmentcould not be loaded with--external_environment_class_path. The decorator registered it on import, andadd_example_environments_cli_argsandenvironment_registration_callbackthen calledEnvironmentRegistry.register(cls, name)again and hit the duplicate-name assertion. No such external factory existed before this PR; this was shown with a constructed module.What changes?
External environments use the built-in contract. An environment is an
ArenaEnvironmentFactory[Cfg]subclass withbuild(cfg), a config dataclass that subclassesArenaEnvironmentCfg, and@register_environment.ExampleEnvironmentBaseis removed. This is a breaking change for environments defined outside Arena; the "Arena in Your Repository" docs have a migration guide. Both examples inisaaclab_arena_examples/external_environments/are migrated, andexternal_environments_experiment.yamlruns them.Experiments select a factory by class path.
environment.typeaccepts a registered name, a graph-spec YAML path, or a dotted class path such asmy_package.envs.MyEnvironment.policy.typealready accepts a registered name or a dotted class path. For a name that is not registered,_resolve_environment_cfg_type_from_name_or_class_pathcallsimport_registered_environment_factory. It imports the module after the built-in environments are registered and checks that@register_environmentregistered that class under itsname.Each Run resolves only the types it selects.
_compose_typed_config_from_yaml_selectornow calls the environment or policy resolver for the Run's selector. Before, only the policy was resolved per Run, by_build_arena_run_cfg_from_yaml_valuesthrough a one-entry mapping._registered_environment_cfg_types()is gone, so an environment without a config elsewhere in the registry does not affect other Runs. A resolution failure is reported as "Run '' selects unknown environment type '': ". When an Experiment uses the command-line formmodule_path:ClassName, the error says to usemodule_path.ClassName.Serialized Experiments keep the class path.
serialize_arena_experiment_to_yamlwritesmodule.ClassNamefor factories outsideisaaclab_arena_environments, so a serialized Experiment reloads in a fresh process where only built-in environments are registered by name.--external_environment_class_pathstays for the command-line scripts.policy_runner.py,environment_runner.py, and the Isaac Lab scripts that useenvironment_registration_callbackkeep the flag withmodule_path:ClassName.import_external_environmentreplacesparse_and_return_external_environment_from_string. It imports the module and checks the registration instead of registering the class again. Repeated imports return the same factory; graph-spec Runs rebuild the parser fromsys.argvfor every build, so a flag on the command line is imported once per build.add_environment_cli_argsand_environment_cfg_from_cliread configs from the registry. They used a private_legacy_argparse_cfg_typeattribute that repeated the config type@register_environmentalready registered. They now useEnvironmentRegistry.get_environment_cfg_type, and the 19 duplicate declarations are deleted. Without this, an external factory would have to set the private attribute to work with the command-line scripts.Usage example
Before:
After:
Points for discussion
module_path.ClassName, likepolicy.type.--external_environment_class_pathkeepsmodule_path:ClassName. We could accept both forms in both places.ExampleEnvironmentBasesubclasses. Support registered environment placement recording #1386 callsparse_and_return_external_environment_from_string, which this PR replaces withimport_external_environment.Known limitations and follow-ups
build_arena_builder_from_legacy_graphbuilds its parser withget_isaaclab_arena_environments_cli_parser, which readssys.argvand adds an argparse subcommand for every registered environment. An external config with a field type thatadd_dataclass_cli_argsdoes not support, such astuple[float, float, float], therefore breaks graph-spec Runs in the same Experiment. No such config exists in this repository; this was shown with a constructed config. The fix is to build that parser from the Run's own arguments.register_environmentkeeps the first class registered under a name and only prints a warning for later ones. If user code imports its module before Arena registers the built-in environments, a factory with a built-in name replaces the built-in environment instead of being rejected. This was shown with a constructed module.example_workflows/*/step_1_environment_setup.rstpages still show first-party environments asExampleEnvironmentBasesubclasses. Those snippets were already out of date before this PR.Validation
test_external_environment.py,test_experiment_runner.py,test_policy_runner.py,test_variations_in_policy_runner.py, andtest_osmo_experiment_workflow.pypass. Five of the subprocess tests needed a rerun after CUDA out-of-memory errors on a GPU shared with other jobs. The tests include the newexperiment_runner.pyrun of both example environments by class path, and the Isaac Lab callback test with the migrated example.sphinx-build -b htmlfinishes without warnings.