feat(obs): add support for extra observations from gym_config#205
feat(obs): add support for extra observations from gym_config#205
Conversation
Add extra parameter to FunctorCfg to allow setting metadata like output shape for observation functors. Extract extra observations in 'add' mode in init_rollout_buffer_from_config to pre-allocate buffers based on shapes. Changes: - Add extra: dict[str, Any] to FunctorCfg - Add extra attribute documentation to ObservationCfg - Modify init_rollout_buffer_from_config to parse and add extra observations - Add comprehensive unit tests in tests/gym/utils/test_gym_utils.py Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds support for pre-allocating rollout-buffer slots for custom (“extra”) observations defined in gym_config.env.observations functors, enabling buffer initialization without needing to run the environment to infer observation shapes.
Changes:
- Extend
init_rollout_buffer_from_configto parseenv.observationsinaddmode and pre-allocate tensors whenextra.shapeis provided. - Add
extrametadata field toFunctorCfgto carry auxiliary configuration (e.g., output shape). - Add a comprehensive test suite covering extra observation pre-allocation behavior and nested observation names.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
embodichain/lab/gym/utils/gym_utils.py |
Parse env.observations (add mode) and pre-allocate buffers for entries that declare extra.shape, then insert them into rollout_buffer["obs"]. |
embodichain/lab/gym/envs/managers/cfg.py |
Add FunctorCfg.extra to support storing metadata like observation output shape. |
tests/gym/utils/test_gym_utils.py |
New tests verifying buffer initialization for extra observations (various shapes, modes, nested keys) and interaction with sensor allocations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "sensor": [ | ||
| { | ||
| "uid": "camera", | ||
| "width": 320, | ||
| "height": 240, | ||
| "enable_mask": True, | ||
| } | ||
| ], | ||
| "env": { | ||
| "observations": { | ||
| "extra_vec": { | ||
| "mode": "add", | ||
| "extra": {"shape": [10]}, | ||
| } | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| buffer = init_rollout_buffer_from_config( | ||
| config=config, | ||
| max_episode_steps=100, | ||
| batch_size=4, | ||
| state_dim=7, | ||
| device="cpu", |
There was a problem hiding this comment.
test_sensor_and_extra_obs_together allocates a very large buffer (batch_size=4, max_episode_steps=100, 240x320 color + mask), which is ~245MB just for the sensor tensors. This can make the unit test suite slow and memory-hungry in CI. Consider reducing width/height and/or max_episode_steps/batch_size in this test while still asserting the same shape logic (e.g., use smaller dimensions like 32x24 and fewer steps).
Description
This PR adds support for pre-allocating extra observations from gym_config's observation functors.
Previously,
init_rollout_buffer_from_configonly pre-allocated buffers for basic robot observations (qpos, qvel, qf) and sensor data. This made it difficult to add custom observations without running the environment first to discover the observation space.Now, observation functors in
addmode can specify ashapein theirextraparameter, which will be used to pre-allocate buffers during rollout buffer initialization. This enables efficient buffer allocation for custom observations without requiring runtime introspection.Example usage:
Dependencies: None
Type of change
Screenshots
N/A
Checklist
🤖 Generated with Claude Code