Repository navigation
Fixes config resolution when presets are not specified - #4796
Conversation
Greptile SummaryThis PR fixes config resolution for environments that use Key changes:
Confidence Score: 3/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Env init or parse_env_cfg] --> B[cfg.validate]
B --> C[resolve_cfg_presets]
C --> D{Field is PresetCfg subclass via MRO?}
D -- Yes --> E[field = field.default]
D -- No --> F{Field has presets dict with default key?}
F -- Yes --> G[field = field.presets default]
F -- No --> H[Recurse into field]
E --> I[Recurse into resolved field]
G --> I
H --> C
C --> J[Concrete cfg passed to scene and managers]
K[InteractiveScene direct construction] --> L{asset_cfg has presets dict?}
L -- Yes --> M[asset_cfg = presets default]
L -- New-style PresetCfg --> N[Warning: Unresolved - MRO check missing]
O[SimulationContext] --> P{no class_type AND has default attr?}
P -- Yes --> Q[physics = physics.default]
P -- No --> R[Heuristic may false-positive]
Last reviewed commit: 50b4806 |
…aac-sim#4803) Conflict in parse_cfg.py: kept HEAD's hydra-import-free PresetCfg resolution (_is_preset_cfg / _resolve_presets_to_default) to avoid the circular-import that would arise from importing resolve_preset_defaults from hydra.py (which itself imports load_cfg_from_registry from parse_cfg). Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
| # Resolve old-style preset wrappers: configclass with a ``presets`` dict and a ``'default'`` key. | ||
| # These are multi-backend selector objects (e.g. VelocityEnvContactSensorCfg) that hold several | ||
| # alternative asset configs in a dict and are not themselves asset configs. | ||
| if hasattr(asset_cfg, "presets") and isinstance(asset_cfg.presets, dict) and "default" in asset_cfg.presets: | ||
| asset_cfg = asset_cfg.presets["default"] | ||
| setattr(self.cfg, asset_name, asset_cfg) |
There was a problem hiding this comment.
New-style PresetCfg wrappers not handled in InteractiveScene
The inline preset resolution here only handles the old-style presets dict pattern. If an asset config is wrapped in a new-style PresetCfg subclass (identified via MRO name matching, as used everywhere else in this PR), it will not be resolved here. If InteractiveScene is created directly (e.g. in unit tests) with a new-style preset in a scene field, the wrapper object will be forwarded to asset builders instead of the concrete config.
The check should mirror the pattern used in resolve_cfg_presets() in configclass.py:
# New-style PresetCfg
if any(cls.__name__ == "PresetCfg" for cls in type(asset_cfg).__mro__):
asset_cfg = asset_cfg.default
setattr(self.cfg, asset_name, asset_cfg)
# Old-style preset
elif hasattr(asset_cfg, "presets") and isinstance(asset_cfg.presets, dict) and "default" in asset_cfg.presets:
asset_cfg = asset_cfg.presets["default"]
setattr(self.cfg, asset_name, asset_cfg)| # If physics is a PresetCfg wrapper (has a 'default' field but no 'class_type'), | ||
| # resolve to the default preset so downstream code always sees a concrete PhysicsCfg. | ||
| if not hasattr(self._physics, "class_type") and hasattr(self._physics, "default"): | ||
| self._physics = self._physics.default | ||
| self.cfg.physics = self._physics |
There was a problem hiding this comment.
Fragile PresetCfg detection heuristic
The detection relies on the absence of class_type combined with the presence of default:
if not hasattr(self._physics, "class_type") and hasattr(self._physics, "default"):This is a proxy heuristic rather than the authoritative MRO name check used in resolve_cfg_presets() elsewhere in this PR. Any SimulationCfg-derived config that happens to carry a default attribute but lacks class_type would be silently replaced — which could produce a confusing AttributeError downstream rather than a clear error.
For consistency and robustness, prefer the same detection pattern used in configclass.py:
if any(cls.__name__ == "PresetCfg" for cls in type(self._physics).__mro__):
self._physics = self._physics.default
self.cfg.physics = self._physics| # NOTE: import here (not at module level) to avoid loading heavy env classes before Isaac Sim is initialized. | ||
| from isaaclab.envs import DirectMARLEnv, DirectRLEnv, ManagerBasedRLEnv | ||
|
|
||
| if not isinstance(env.unwrapped, (ManagerBasedRLEnv, DirectRLEnv, DirectMARLEnv)): | ||
| raise ValueError( |
There was a problem hiding this comment.
Stale error message after adding DirectMARLEnv to the check
The isinstance check was extended to include DirectMARLEnv, but the error message still only mentions ManagerBasedRLEnv and DirectRLEnv:
raise ValueError(
"The environment must be inherited from ManagerBasedRLEnv or DirectRLEnv. Environment type:"
f" {type(env)}"
)| # NOTE: import here (not at module level) to avoid loading heavy env classes before Isaac Sim is initialized. | |
| from isaaclab.envs import DirectMARLEnv, DirectRLEnv, ManagerBasedRLEnv | |
| if not isinstance(env.unwrapped, (ManagerBasedRLEnv, DirectRLEnv, DirectMARLEnv)): | |
| raise ValueError( | |
| raise ValueError( | |
| "The environment must be inherited from ManagerBasedRLEnv, DirectRLEnv, or DirectMARLEnv." | |
| f" Environment type: {type(env)}" | |
| ) |
| def _is_preset_cfg(obj: object) -> bool: | ||
| """Return True if *obj* is an instance of a PresetCfg subclass (new typed-field style). | ||
|
|
||
| Uses MRO class-name matching so that this module has no dependency on the | ||
| hydra-gated ``isaaclab_tasks.utils.hydra`` import path. | ||
| """ | ||
| return any(cls.__name__ == "PresetCfg" for cls in type(obj).__mro__) | ||
|
|
||
|
|
||
| def _is_old_style_preset(obj: object) -> bool: | ||
| """Return True if *obj* is an old-style preset wrapper (has ``presets`` dict with a ``'default'`` key).""" | ||
| presets = getattr(obj, "presets", None) | ||
| return hasattr(obj, "__dataclass_fields__") and isinstance(presets, dict) and "default" in presets | ||
|
|
||
|
|
||
| def _resolve_presets_to_default(cfg: object) -> object: | ||
| """Recursively replace preset wrapper fields with their *default* preset. | ||
|
|
||
| Handles two preset patterns used in IsaacLab task configs: | ||
|
|
||
| * **New style** (``PresetCfg`` subclass): typed fields where ``default`` is a class attribute. | ||
| * **Old style** (``presets`` dict): configclass with ``presets: dict[str, Cfg]`` and a ``'default'`` key. | ||
|
|
||
| Both are resolved in-place so the config can be used without a Hydra CLI override (e.g. in tests). | ||
| """ | ||
| if not hasattr(cfg, "__dataclass_fields__"): | ||
| return cfg | ||
| for field_name in list(cfg.__dataclass_fields__): | ||
| value = getattr(cfg, field_name, None) | ||
| if value is None or not hasattr(value, "__dataclass_fields__"): | ||
| continue | ||
| if _is_preset_cfg(value): | ||
| resolved = value.default | ||
| setattr(cfg, field_name, resolved) | ||
| _resolve_presets_to_default(resolved) | ||
| elif _is_old_style_preset(value): | ||
| resolved = value.presets["default"] | ||
| setattr(cfg, field_name, resolved) | ||
| _resolve_presets_to_default(resolved) | ||
| else: | ||
| _resolve_presets_to_default(value) | ||
| return cfg |
There was a problem hiding this comment.
Duplicate preset-resolution logic between parse_cfg.py and configclass.py
_is_preset_cfg(), _is_old_style_preset(), and _resolve_presets_to_default() here are functionally identical to resolve_cfg_presets() added to configclass.py in this same PR. The duplication is acknowledged in the PR description as a workaround for a circular import, but it creates a maintenance hazard: future changes to the resolution algorithm must be applied in both places.
A lighter alternative would be a deferred import inside _resolve_presets_to_default(), or extracting the resolution logic into a small, import-free utility module that both call sites can import without triggering a cycle. That would eliminate the duplicated logic while keeping the circular-import fix.
| if isinstance(cfg, dict): | ||
| raise RuntimeError(f"Configuration for the task: '{task_name}' is not a class. Please provide a class.") | ||
|
|
||
| # resolve PresetCfg fields (e.g. physics) to their default values | ||
| from isaaclab_tasks.utils.hydra import resolve_preset_defaults | ||
|
|
||
| cfg = resolve_preset_defaults(cfg) | ||
| # If the top-level cfg is itself a PresetCfg wrapper, resolve to the default preset before | ||
| # attempting any attribute access (e.g. cfg.sim, cfg.scene). | ||
| if _is_preset_cfg(cfg): | ||
| cfg = cfg.default | ||
|
|
||
| # Resolve any PresetCfg wrappers to their default preset so the config | ||
| # is usable without a Hydra CLI override (e.g. in tests). | ||
| # Must happen BEFORE attribute overrides, otherwise overrides on PresetCfg wrapper | ||
| # fields (e.g. cfg.scene when scene is a PresetCfg) get discarded when the wrapper | ||
| # is replaced by its .default. | ||
| _resolve_presets_to_default(cfg) | ||
|
|
||
| # simulation device | ||
| cfg.sim.device = device |
There was a problem hiding this comment.
Top-level old-style preset not handled before attribute access
_is_preset_cfg(cfg) handles the case where the top-level cfg is a new-style PresetCfg wrapper and resolves it before calling _resolve_presets_to_default(). However, there is no matching guard for the old-style pattern (presets dict with a "default" key). If a top-level task config is itself an old-style preset wrapper, the code will proceed to call _resolve_presets_to_default(cfg) on the wrapper — where the wrapper's fields (e.g. cfg.sim) most likely don't exist — and then reach cfg.sim.device = device (a few lines further down), raising an AttributeError rather than surfacing a clear error.
if _is_preset_cfg(cfg):
cfg = cfg.default
# Add the symmetric guard for the old-style pattern:
elif _is_old_style_preset(cfg):
cfg = cfg.presets["default"]
Description
Fixes config resolution for environments that use PresetCfg or presets-dict wrappers (multi-backend selectors for PhysX/Newton) when no Hydra CLI override is provided (e.g. in unit tests).
Core changes:
Added resolve_cfg_presets() in configclass.py to recursively resolve preset wrappers to their default variant; called at construction in all three env base classes
Replaced the circular-import-prone resolve_preset_defaults call in parse_cfg.py with a local implementation
SimulationContext and InteractiveScene now also resolve preset wrappers inline
Bug fixes:
Automate tasks: fixed broken file path (was calling os.path.basename() on a remote URL instead of using the return value of retrieve_file_path())
dexsuite MDP: moved asset imports under TYPE_CHECKING to fix circular imports
rl_games: added DirectMARLEnvCfg to valid cfg type check
Test/infra:
Reduced num_envs from 32 → 2 across all environment/sensor tests (faster CI, less GPU memory)
Fixed app.name in all .kit files ("Isaac Lab" → "IsaacLab")
Type of change
Checklist
pre-commitchecks with./isaaclab.sh --formatconfig/extension.tomlfileCONTRIBUTORS.mdor my name already exists there