opentau.envs.utils
This module contains utility functions for environments.
Functions
|
Adds task feature to the observation dict with respect to the first environment attribute. |
|
Inject pi07 inference-time metadata from |
|
Inject |
Checks if all environments in a vectorized environment are of the same type. |
|
Checks if all environments in a vectorized environment have 'task_description' or 'task' attributes. |
|
|
Close a single environment, a list of environments, or a dictionary of environments. |
|
Convert environment observation to OpenTau format observation. |
- opentau.envs.utils.add_envs_task(env: VectorEnv, observation: dict[str, Any]) dict[str, Any][source]
Adds task feature to the observation dict with respect to the first environment attribute.
- Parameters:
env – A vectorized Gym environment (SyncVectorEnv or AsyncVectorEnv).
observation – A dictionary of observations from the vectorized environment, which will be modified in place.
- Returns:
The updated observation dictionary with the ‘prompt’ key added.
- opentau.envs.utils.add_eval_metadata(observation: dict[str, Any], cfg: TrainPipelineConfig) dict[str, Any][source]
Inject pi07 inference-time metadata from
cfg.env.metadata.Numeric fields (
speed/quality/mistake) are broadcast as(num_envs,)tensors with a parallel{key}_is_pad=Falseflag, using the same dtypes the dataset emits at training time (torch.longforspeed/quality,torch.boolformistake). String fields (robot_type/control_mode) are broadcast aslist[str]of lengthnum_envs. Fields set toNoneon the config are skipped, so the corresponding batch key stays absent and the policy’sprepare_metadatapad default kicks in.fpsis the env’s stepping frequency (EnvConfig.fps, e.g.20for LIBERO) and is broadcast as atorch.longtensor withfps_is_pad=Falsewhenevermeta.emit_fpsisTrue(the default). Setemit_fps=FalseonEnvMetadataConfigto skip it — useful when resuming a checkpoint trained without fps conditioning.cfg.envis guaranteed non-Nonehere —eval_policydereferencescfg.env.typebefore the rollout loop starts.- Parameters:
observation – Observation dict produced by
preprocess_observation(must containstateto source the batch size and device).cfg – Training/eval pipeline config; reads
cfg.env.metadataandcfg.env.fps.
- Returns:
The observation dict, mutated in place.
- opentau.envs.utils.add_subgoal_images(observation: dict[str, Any], generator: SubgoalImageGenerator | None) dict[str, Any][source]
Inject
subgoal{k}/subgoal_is_padkeys from a generator.Mirrors
add_envs_task()/add_eval_metadata(): the generator inspectsobservation(which already carriescamera{k},state,img_is_pad,prompt, and any pi07 metadata keys) and returns the subgoal batch keys the policy’sprepare_subgoal_imagesconsumes.The caller is expected to have invoked
generator.start_episode(prompts)once at the top of the rollout — the per-step__call__only reads the cached choice. WhengeneratorisNone, this is a no-op and the policy’s missing-key fallback (zero subgoals,mask=False) takes over.- Parameters:
observation – Observation dict mutated in place via
.update(...).generator – Subgoal image generator, or
Noneto skip injection.
- Returns:
The observation dict, mutated in place.
- opentau.envs.utils.are_all_envs_same_type(env: VectorEnv) bool[source]
Checks if all environments in a vectorized environment are of the same type.
- Parameters:
env – A vectorized Gym environment (SyncVectorEnv or AsyncVectorEnv).
- Returns:
True if all environments are of the same type, False otherwise.
- opentau.envs.utils.check_env_attributes_and_types(env: VectorEnv) None[source]
Checks if all environments in a vectorized environment have ‘task_description’ or ‘task’ attributes. A warning will be raised if any environment is missing these attributes.
- Parameters:
env – A vectorized Gym environment (SyncVectorEnv or AsyncVectorEnv).
- Raises:
ValueError – If the environment is not a SyncVectorEnv or AsyncVectorEnv.
- opentau.envs.utils.close_envs(obj: Any) None[source]
- opentau.envs.utils.close_envs(env: Mapping) None
- opentau.envs.utils.close_envs(envs: Sequence) None
- opentau.envs.utils.close_envs(env: Env) None
- opentau.envs.utils.close_envs(env: None) None
Close a single environment, a list of environments, or a dictionary of environments.
- opentau.envs.utils.preprocess_observation(np_observations: dict, cfg: TrainPipelineConfig) dict[str, Tensor][source]
Convert environment observation to OpenTau format observation. :param np_observations: Dictionary of observation batches from a Gym vector environment. :param cfg: Training configuration that contains max_state_dim, num_cams, resolution, etc.
- Returns:
Dictionary of observation batches with keys renamed to OpenTau format and values as tensors.