Implementation:Haosulab ManiSkill GymUtils
| Knowledge Sources | |
|---|---|
| Domains | Robotics, Simulation, Gymnasium Integration |
| Last Updated | 2026-02-15 08:00 GMT |
Overview
Concrete tool for Gymnasium/OpenAI Gym utilities including observation/action space manipulation, scalar extraction, and action normalization.
Description
The gym_utils.py module provides various utility functions for working with Gymnasium observation and action spaces, commonly needed when integrating ManiSkill environments with reinforcement learning frameworks.
Environment utilities:
find_max_episode_steps_value(env)-- Traverses the wrapper chain to find the max_episode_steps parameter, handling SyncVectorEnv, ManiSkillVectorEnv, and standard wrappers.
Scalar extraction:
extract_scalars_from_info(info, blacklist, batch_size)-- Recursively extracts scalar metrics from info dictionaries returned byenv.step(). Supports both single and batched environments.
Action normalization:
clip_and_scale_action(action, low, high)-- Clips actions to [-1, 1] and scales to [low, high] range.inv_scale_action(action, low, high)-- Inverse of clip_and_scale_action (without clipping).normalize_action_space(action_space)-- Converts a Box action space to normalized [-1, 1] range.
Type utilities:
get_dtype_bounds(dtype)-- Gets min/max values for numpy dtypes (floating, integer, bool).convert_observation_to_space(observation, prefix, unbatched)-- Recursively converts a sample observation (dict, array, scalar) into a corresponding Gymnasium observation space.
Version detection:
IS_GYMNASIUM_1-- Boolean flag detecting if gymnasium version > 1.0.0.
Usage
Import from mani_skill.utils.gym_utils when building training pipelines, converting observation spaces, or normalizing actions for RL algorithms.
Code Reference
Source Location
- Repository: Haosulab_ManiSkill
- File: mani_skill/utils/gym_utils.py
Signature
def find_max_episode_steps_value(env) -> Optional[int]: ...
def extract_scalars_from_info(info: dict, blacklist=(), batch_size=1) -> dict[str, float]: ...
def clip_and_scale_action(action, low, high) -> torch.Tensor: ...
def inv_scale_action(action, low, high) -> np.ndarray: ...
def normalize_action_space(action_space: spaces.Box) -> spaces.Box: ...
def get_dtype_bounds(dtype: np.dtype) -> tuple: ...
def convert_observation_to_space(observation, prefix="", unbatched=False) -> spaces.Space: ...
Import
from mani_skill.utils.gym_utils import (
clip_and_scale_action,
convert_observation_to_space,
extract_scalars_from_info,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| action | torch.Tensor | Yes | Action tensor in [-1, 1] range |
| low | float/np.ndarray | Yes | Lower bound of action range |
| high | float/np.ndarray | Yes | Upper bound of action range |
| observation | dict/np.ndarray | Yes | Sample observation for space inference |
Outputs
| Name | Type | Description |
|---|---|---|
| scaled_action | torch.Tensor | Action scaled to [low, high] range |
| space | spaces.Space | Inferred Gymnasium observation space |
| scalars | dict[str, float] | Extracted scalar metrics |
Usage Examples
Basic Usage
import torch
from mani_skill.utils.gym_utils import clip_and_scale_action, extract_scalars_from_info
# Scale normalized actions to joint limits
action = torch.tensor([0.5, -0.3])
scaled = clip_and_scale_action(action, low=-3.14, high=3.14)
# Extract scalar metrics from info dict
info = {"success": True, "dist": 0.05, "nested": {"metric": 1.0}}
scalars = extract_scalars_from_info(info)
# {"success": 1.0, "dist": 0.05, "nested.metric": 1.0}