mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-10 19:41:45 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c11d8f1bb6 | |||
| 6001b2c3ad | |||
| a5b29d4301 |
@@ -83,11 +83,11 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Remove Tags with Git dependencies
|
||||
# TODO(Steven): Temporary patch to remove libero and pi from PyPi 0.4.0 release due to its reliance on git dependencies.
|
||||
# TODO(Steven): Temporary patch to remove pi from PyPi 0.4.0 release due to its reliance on git dependencies.
|
||||
run: |
|
||||
echo "::info:: Checking for Git dependencies to remove from pyproject.toml..."
|
||||
grep -E '@ git\+https|lerobot\[pi\]|lerobot\[libero\]' pyproject.toml | sed 's/^/::warning:: Removing line: /' || true
|
||||
sed -E -i '/@ git\+https|lerobot\[pi\]|lerobot\[libero\]/d' pyproject.toml
|
||||
grep -E '@ git\+https|lerobot\[pi\]' pyproject.toml | sed 's/^/::warning:: Removing line: /' || true
|
||||
sed -E -i '/@ git\+https|lerobot\[pi\]/d' pyproject.toml
|
||||
echo "::info:: Git dependencies removed. Proceeding with build."
|
||||
|
||||
- name: Install build dependencies
|
||||
|
||||
@@ -70,7 +70,7 @@ jobs:
|
||||
echo "Dependencies unbound:" && cat pyproject.toml
|
||||
|
||||
- name: Install lerobot with all extras
|
||||
run: uv sync --all-extras
|
||||
run: uv sync --all-extras --no-extra groot # TODO(Steven): Make flash-attn optional
|
||||
|
||||
- name: Run pytest (all extras)
|
||||
run: uv run pytest tests -vv
|
||||
|
||||
@@ -186,7 +186,7 @@ For a full list of optional dependencies, see:
|
||||
https://pypi.org/project/lerobot/
|
||||
|
||||
> [!NOTE]
|
||||
> For lerobot 0.4.0, if you want to install libero or pi tags, you will have to do: `pip install "lerobot[pi,libero]@git+https://github.com/huggingface/lerobot.git"`.
|
||||
> For lerobot 0.4.0, if you want to install pi tags, you will have to do: `pip install "lerobot[pi]@git+https://github.com/huggingface/lerobot.git"`.
|
||||
>
|
||||
> This will be solved in the next patch release
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ For a full list of optional dependencies, see:
|
||||
https://pypi.org/project/lerobot/
|
||||
|
||||
> [!NOTE]
|
||||
> For lerobot 0.4.0, if you want to install libero or pi, you will have to do: `pip install "lerobot[pi,libero]@git+https://github.com/huggingface/lerobot.git"`
|
||||
> For lerobot 0.4.0, if you want to install pi, you will have to do: `pip install "lerobot[pi]@git+https://github.com/huggingface/lerobot.git"`
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
|
||||
@@ -28,11 +28,6 @@ LIBERO is now part of our **multi-eval supported simulation**, meaning you can b
|
||||
To Install LIBERO, after following LeRobot official instructions, just do:
|
||||
`pip install -e ".[libero]"`
|
||||
|
||||
> [!NOTE]
|
||||
> For lerobot 0.4.0, if you want to install libero tag, you will have to do: `pip install "lerobot[libero]@git+https://github.com/huggingface/lerobot.git"`.
|
||||
>
|
||||
> This will be solved in the next patch release
|
||||
|
||||
### Single-suite evaluation
|
||||
|
||||
Evaluate a policy on one LIBERO suite:
|
||||
|
||||
@@ -15,10 +15,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
import shutil
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
@@ -109,7 +107,6 @@ def update_meta_data(
|
||||
dst_meta,
|
||||
meta_idx,
|
||||
data_idx,
|
||||
data_file_map,
|
||||
videos_idx,
|
||||
):
|
||||
"""Updates metadata DataFrame with new chunk, file, and timestamp indices.
|
||||
@@ -130,25 +127,8 @@ def update_meta_data(
|
||||
|
||||
df["meta/episodes/chunk_index"] = df["meta/episodes/chunk_index"] + meta_idx["chunk"]
|
||||
df["meta/episodes/file_index"] = df["meta/episodes/file_index"] + meta_idx["file"]
|
||||
# Remap data chunk/file indices per-source-file using the actual destination
|
||||
# file chosen during data aggregation. A flat offset is incorrect when
|
||||
# multiple source files are concatenated into a single destination file.
|
||||
if data_file_map:
|
||||
new_data_chunk = []
|
||||
new_data_file = []
|
||||
for idx in df.index:
|
||||
src_chunk = int(df.at[idx, "data/chunk_index"]) # original source file location
|
||||
src_file = int(df.at[idx, "data/file_index"]) # original source file location
|
||||
dst_chunk, dst_file = data_file_map.get(
|
||||
(src_chunk, src_file), (src_chunk + data_idx["chunk"], src_file + data_idx["file"])
|
||||
)
|
||||
new_data_chunk.append(dst_chunk)
|
||||
new_data_file.append(dst_file)
|
||||
df["data/chunk_index"] = new_data_chunk
|
||||
df["data/file_index"] = new_data_file
|
||||
else:
|
||||
df["data/chunk_index"] = df["data/chunk_index"] + data_idx["chunk"]
|
||||
df["data/file_index"] = df["data/file_index"] + data_idx["file"]
|
||||
df["data/chunk_index"] = df["data/chunk_index"] + data_idx["chunk"]
|
||||
df["data/file_index"] = df["data/file_index"] + data_idx["file"]
|
||||
for key, video_idx in videos_idx.items():
|
||||
# Store original video file indices before updating
|
||||
orig_chunk_col = f"videos/{key}/chunk_index"
|
||||
@@ -186,7 +166,7 @@ def update_meta_data(
|
||||
return df
|
||||
|
||||
|
||||
def _aggregate_datasets(
|
||||
def aggregate_datasets(
|
||||
repo_ids: list[str],
|
||||
aggr_repo_id: str,
|
||||
roots: list[Path] | None = None,
|
||||
@@ -195,24 +175,39 @@ def _aggregate_datasets(
|
||||
video_files_size_in_mb: float | None = None,
|
||||
chunk_size: int | None = None,
|
||||
):
|
||||
"""Serial aggregation kernel: combines datasets into a destination dataset.
|
||||
"""Aggregates multiple LeRobot datasets into a single unified dataset.
|
||||
|
||||
This function performs a single-process aggregation. It assumes it is the
|
||||
sole writer for its destination `aggr_root`.
|
||||
This is the main function that orchestrates the aggregation process by:
|
||||
1. Loading and validating all source dataset metadata
|
||||
2. Creating a new destination dataset with unified tasks
|
||||
3. Aggregating videos, data, and metadata from all source datasets
|
||||
4. Finalizing the aggregated dataset with proper statistics
|
||||
|
||||
Args:
|
||||
repo_ids: List of repository IDs for the datasets to aggregate.
|
||||
aggr_repo_id: Repository ID for the aggregated output dataset.
|
||||
roots: Optional list of root paths for the source datasets.
|
||||
aggr_root: Optional root path for the aggregated dataset.
|
||||
data_files_size_in_mb: Maximum size for data files in MB (defaults to DEFAULT_DATA_FILE_SIZE_IN_MB)
|
||||
video_files_size_in_mb: Maximum size for video files in MB (defaults to DEFAULT_VIDEO_FILE_SIZE_IN_MB)
|
||||
chunk_size: Maximum number of files per chunk (defaults to DEFAULT_CHUNK_SIZE)
|
||||
"""
|
||||
# Build metadata objects, supporting a per-dataset "root" that may be None.
|
||||
# When root is provided we load from the local filesystem, otherwise from Hub cache.
|
||||
if roots is None:
|
||||
all_metadata = [LeRobotDatasetMetadata(repo_id) for repo_id in repo_ids]
|
||||
else:
|
||||
all_metadata = [
|
||||
(
|
||||
LeRobotDatasetMetadata(repo_id, root=root)
|
||||
if root is not None
|
||||
else LeRobotDatasetMetadata(repo_id)
|
||||
)
|
||||
for repo_id, root in zip(repo_ids, roots, strict=False)
|
||||
logging.info("Start aggregate_datasets")
|
||||
|
||||
if data_files_size_in_mb is None:
|
||||
data_files_size_in_mb = DEFAULT_DATA_FILE_SIZE_IN_MB
|
||||
if video_files_size_in_mb is None:
|
||||
video_files_size_in_mb = DEFAULT_VIDEO_FILE_SIZE_IN_MB
|
||||
if chunk_size is None:
|
||||
chunk_size = DEFAULT_CHUNK_SIZE
|
||||
|
||||
all_metadata = (
|
||||
[LeRobotDatasetMetadata(repo_id) for repo_id in repo_ids]
|
||||
if roots is None
|
||||
else [
|
||||
LeRobotDatasetMetadata(repo_id, root=root) for repo_id, root in zip(repo_ids, roots, strict=False)
|
||||
]
|
||||
)
|
||||
fps, robot_type, features = validate_all_metadata(all_metadata)
|
||||
video_keys = [key for key in features if features[key]["dtype"] == "video"]
|
||||
|
||||
@@ -242,11 +237,9 @@ def _aggregate_datasets(
|
||||
|
||||
for src_meta in tqdm.tqdm(all_metadata, desc="Copy data and videos"):
|
||||
videos_idx = aggregate_videos(src_meta, dst_meta, videos_idx, video_files_size_in_mb, chunk_size)
|
||||
data_idx, data_file_map = aggregate_data(
|
||||
src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_size
|
||||
)
|
||||
data_idx = aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_size)
|
||||
|
||||
meta_idx = aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, data_file_map, videos_idx)
|
||||
meta_idx = aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, videos_idx)
|
||||
|
||||
dst_meta.info["total_episodes"] += src_meta.total_episodes
|
||||
dst_meta.info["total_frames"] += src_meta.total_frames
|
||||
@@ -255,168 +248,6 @@ def _aggregate_datasets(
|
||||
logging.info("Aggregation complete.")
|
||||
|
||||
|
||||
def aggregate_datasets(
|
||||
repo_ids: list[str],
|
||||
aggr_repo_id: str,
|
||||
roots: list[Path] | None = None,
|
||||
aggr_root: Path | None = None,
|
||||
data_files_size_in_mb: float | None = None,
|
||||
video_files_size_in_mb: float | None = None,
|
||||
chunk_size: int | None = None,
|
||||
num_workers: int | None = None,
|
||||
tmp_root: Path | None = None,
|
||||
):
|
||||
"""Aggregates multiple LeRobot datasets into a single unified dataset.
|
||||
|
||||
This is the main function that orchestrates the aggregation process by:
|
||||
1. Loading and validating all source dataset metadata
|
||||
2. Creating a new destination dataset with unified tasks
|
||||
3. Aggregating videos, data, and metadata from all source datasets
|
||||
4. Finalizing the aggregated dataset with proper statistics
|
||||
|
||||
Args:
|
||||
repo_ids: List of repository IDs for the datasets to aggregate.
|
||||
aggr_repo_id: Repository ID for the aggregated output dataset.
|
||||
roots: Optional list of root paths for the source datasets.
|
||||
aggr_root: Optional root path for the aggregated dataset.
|
||||
data_files_size_in_mb: Maximum size for data files in MB (defaults to DEFAULT_DATA_FILE_SIZE_IN_MB)
|
||||
video_files_size_in_mb: Maximum size for video files in MB (defaults to DEFAULT_VIDEO_FILE_SIZE_IN_MB)
|
||||
chunk_size: Maximum number of files per chunk (defaults to DEFAULT_CHUNK_SIZE)
|
||||
num_workers: When > 1, performs a tree-based parallel reduction using a thread pool
|
||||
tmp_root: Optional base directory to store intermediate reduction outputs
|
||||
"""
|
||||
logging.info("Start aggregate_datasets")
|
||||
|
||||
if data_files_size_in_mb is None:
|
||||
data_files_size_in_mb = DEFAULT_DATA_FILE_SIZE_IN_MB
|
||||
if video_files_size_in_mb is None:
|
||||
video_files_size_in_mb = DEFAULT_VIDEO_FILE_SIZE_IN_MB
|
||||
if chunk_size is None:
|
||||
chunk_size = DEFAULT_CHUNK_SIZE
|
||||
|
||||
if num_workers is None or num_workers <= 1:
|
||||
# Run aggregation sequentially
|
||||
_aggregate_datasets(
|
||||
repo_ids=repo_ids,
|
||||
aggr_repo_id=aggr_repo_id,
|
||||
aggr_root=aggr_root,
|
||||
roots=roots,
|
||||
data_files_size_in_mb=data_files_size_in_mb,
|
||||
video_files_size_in_mb=video_files_size_in_mb,
|
||||
chunk_size=chunk_size,
|
||||
)
|
||||
|
||||
# Uses a parallel fan-out/fan-in strategy when num_workers is provided
|
||||
elif num_workers > 1:
|
||||
# Validate across all metadata early to fail fast
|
||||
all_metadata_for_validation = (
|
||||
[LeRobotDatasetMetadata(repo_id) for repo_id in repo_ids]
|
||||
if roots is None
|
||||
else [
|
||||
LeRobotDatasetMetadata(repo_id, root=root)
|
||||
for repo_id, root in zip(repo_ids, roots, strict=False)
|
||||
]
|
||||
)
|
||||
validate_all_metadata(all_metadata_for_validation)
|
||||
|
||||
# Clamp workers to a sensible upper bound (pairs per round)
|
||||
num_workers = min(num_workers, max(1, len(repo_ids) // 2))
|
||||
|
||||
# Choose a base temporary root for intermediate merge results
|
||||
if tmp_root is not None:
|
||||
base_tmp_root = tmp_root
|
||||
elif aggr_root is not None:
|
||||
base_tmp_root = aggr_root.parent / f".{aggr_repo_id}__tmp"
|
||||
else:
|
||||
base_tmp_root = Path.cwd() / f".{aggr_repo_id}__tmp"
|
||||
base_tmp_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
current_repo_ids: list[str] = list(repo_ids)
|
||||
# Always maintain a roots list aligned with repo_ids. Use None for Hub-backed inputs.
|
||||
current_roots: list[Path | None] = list(roots) if roots is not None else [None] * len(repo_ids)
|
||||
|
||||
try:
|
||||
level = 0
|
||||
while len(current_repo_ids) > 1:
|
||||
next_repo_ids: list[str] = []
|
||||
next_roots: list[Path | None] = []
|
||||
futures = []
|
||||
|
||||
with ThreadPoolExecutor(max_workers=num_workers) as executor:
|
||||
group_index = 0
|
||||
i = 0
|
||||
while i < len(current_repo_ids):
|
||||
group_repo_ids = current_repo_ids[i : i + 2]
|
||||
group_roots = current_roots[i : i + 2]
|
||||
|
||||
if len(group_repo_ids) == 1:
|
||||
# Carry over singleton to next level
|
||||
next_repo_ids.append(group_repo_ids[0])
|
||||
next_roots.append(group_roots[0])
|
||||
i += 1
|
||||
continue
|
||||
|
||||
out_repo_id = f"{aggr_repo_id}__reduce_l{level}_g{group_index}"
|
||||
out_root = base_tmp_root / f"reduce_l{level}_g{group_index}"
|
||||
|
||||
futures.append(
|
||||
executor.submit(
|
||||
_aggregate_datasets,
|
||||
group_repo_ids,
|
||||
out_repo_id,
|
||||
group_roots,
|
||||
out_root,
|
||||
data_files_size_in_mb,
|
||||
video_files_size_in_mb,
|
||||
chunk_size,
|
||||
)
|
||||
)
|
||||
|
||||
next_repo_ids.append(out_repo_id)
|
||||
next_roots.append(out_root)
|
||||
|
||||
i += 2
|
||||
group_index += 1
|
||||
|
||||
for f in as_completed(futures):
|
||||
# Bubble up any exception raised inside tasks
|
||||
f.result()
|
||||
|
||||
# Cleanup previous level temporary outputs that won't be used again
|
||||
base_resolved = base_tmp_root.resolve()
|
||||
keep_set = {nr.resolve() for nr in next_roots if nr is not None}
|
||||
for prev_root in current_roots:
|
||||
if prev_root is None:
|
||||
continue
|
||||
# Suppress per-iteration to keep cleaning other roots even if one fails
|
||||
with contextlib.suppress(Exception):
|
||||
pr = prev_root.resolve()
|
||||
if pr not in keep_set and base_resolved in pr.parents:
|
||||
shutil.rmtree(prev_root, ignore_errors=True)
|
||||
|
||||
current_repo_ids = next_repo_ids
|
||||
current_roots = next_roots # aligned list of Path|None after first level
|
||||
level += 1
|
||||
|
||||
# Final copy/aggregation into the desired output
|
||||
_aggregate_datasets(
|
||||
repo_ids=current_repo_ids,
|
||||
aggr_repo_id=aggr_repo_id,
|
||||
roots=current_roots,
|
||||
aggr_root=aggr_root,
|
||||
data_files_size_in_mb=data_files_size_in_mb,
|
||||
video_files_size_in_mb=video_files_size_in_mb,
|
||||
chunk_size=chunk_size,
|
||||
)
|
||||
finally:
|
||||
# Remove all temporary reduction artifacts
|
||||
with contextlib.suppress(Exception):
|
||||
shutil.rmtree(base_tmp_root, ignore_errors=True)
|
||||
|
||||
logging.info("Aggregation complete.")
|
||||
return
|
||||
|
||||
|
||||
def aggregate_videos(src_meta, dst_meta, videos_idx, video_files_size_in_mb, chunk_size):
|
||||
"""Aggregates video chunks from a source dataset into the destination dataset.
|
||||
|
||||
@@ -535,9 +366,6 @@ def aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_si
|
||||
|
||||
unique_chunk_file_ids = sorted(unique_chunk_file_ids)
|
||||
|
||||
# Map source (chunk,file) -> destination (chunk,file) actually used during write
|
||||
src_to_dst_file: dict[tuple[int, int], tuple[int, int]] = {}
|
||||
|
||||
for src_chunk_idx, src_file_idx in unique_chunk_file_ids:
|
||||
src_path = src_meta.root / DEFAULT_DATA_PATH.format(
|
||||
chunk_index=src_chunk_idx, file_index=src_file_idx
|
||||
@@ -545,7 +373,7 @@ def aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_si
|
||||
df = pd.read_parquet(src_path)
|
||||
df = update_data_df(df, src_meta, dst_meta)
|
||||
|
||||
data_idx, used_chunk, used_file = append_or_create_parquet_file(
|
||||
data_idx = append_or_create_parquet_file(
|
||||
df,
|
||||
src_path,
|
||||
data_idx,
|
||||
@@ -555,12 +383,11 @@ def aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_si
|
||||
contains_images=len(dst_meta.image_keys) > 0,
|
||||
aggr_root=dst_meta.root,
|
||||
)
|
||||
src_to_dst_file[(src_chunk_idx, src_file_idx)] = (used_chunk, used_file)
|
||||
|
||||
return data_idx, src_to_dst_file
|
||||
return data_idx
|
||||
|
||||
|
||||
def aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, data_file_map, videos_idx):
|
||||
def aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, videos_idx):
|
||||
"""Aggregates metadata from a source dataset into the destination dataset.
|
||||
|
||||
Reads source metadata files, updates all indices and timestamps,
|
||||
@@ -594,11 +421,10 @@ def aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, data_file_map, vi
|
||||
dst_meta,
|
||||
meta_idx,
|
||||
data_idx,
|
||||
data_file_map,
|
||||
videos_idx,
|
||||
)
|
||||
|
||||
meta_idx, _m_used_chunk, _m_used_file = append_or_create_parquet_file(
|
||||
meta_idx = append_or_create_parquet_file(
|
||||
df,
|
||||
src_path,
|
||||
meta_idx,
|
||||
@@ -652,7 +478,7 @@ def append_or_create_parquet_file(
|
||||
to_parquet_with_hf_images(df, dst_path)
|
||||
else:
|
||||
df.to_parquet(dst_path)
|
||||
return idx, idx["chunk"], idx["file"]
|
||||
return idx
|
||||
|
||||
src_size = get_parquet_file_size_in_mb(src_path)
|
||||
dst_size = get_parquet_file_size_in_mb(dst_path)
|
||||
@@ -663,19 +489,17 @@ def append_or_create_parquet_file(
|
||||
new_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
final_df = df
|
||||
target_path = new_path
|
||||
used_chunk, used_file = idx["chunk"], idx["file"]
|
||||
else:
|
||||
existing_df = pd.read_parquet(dst_path)
|
||||
final_df = pd.concat([existing_df, df], ignore_index=True)
|
||||
target_path = dst_path
|
||||
used_chunk, used_file = idx["chunk"], idx["file"]
|
||||
|
||||
if contains_images:
|
||||
to_parquet_with_hf_images(final_df, target_path)
|
||||
else:
|
||||
final_df.to_parquet(target_path)
|
||||
|
||||
return idx, used_chunk, used_file
|
||||
return idx
|
||||
|
||||
|
||||
def finalize_aggregation(aggr_meta, all_metadata):
|
||||
|
||||
@@ -234,7 +234,6 @@ def merge_datasets(
|
||||
datasets: list[LeRobotDataset],
|
||||
output_repo_id: str,
|
||||
output_dir: str | Path | None = None,
|
||||
num_workers: int | None = None,
|
||||
) -> LeRobotDataset:
|
||||
"""Merge multiple LeRobotDatasets into a single dataset.
|
||||
|
||||
@@ -258,7 +257,6 @@ def merge_datasets(
|
||||
aggr_repo_id=output_repo_id,
|
||||
roots=roots,
|
||||
aggr_root=output_dir,
|
||||
num_workers=num_workers,
|
||||
)
|
||||
|
||||
merged_dataset = LeRobotDataset(
|
||||
@@ -331,7 +329,7 @@ def modify_features(
|
||||
|
||||
if repo_id is None:
|
||||
repo_id = f"{dataset.repo_id}_modified"
|
||||
output_dir = Path(output_dir, exists_ok=True) if output_dir is not None else HF_LEROBOT_HOME / repo_id
|
||||
output_dir = Path(output_dir) if output_dir is not None else HF_LEROBOT_HOME / repo_id
|
||||
|
||||
new_features = dataset.meta.features.copy()
|
||||
|
||||
|
||||
@@ -237,9 +237,10 @@ class LiberoEnv(gym.Env):
|
||||
def reset(self, seed=None, **kwargs):
|
||||
super().reset(seed=seed)
|
||||
self._env.seed(seed)
|
||||
raw_obs = self._env.reset()
|
||||
if self.init_states and self._init_states is not None:
|
||||
self._env.set_init_state(self._init_states[self._init_state_id])
|
||||
raw_obs = self._env.reset()
|
||||
raw_obs = self._env.env._get_observations()
|
||||
|
||||
# After reset, objects may be unstable (slightly floating, intersecting, etc.).
|
||||
# Step the simulator with a no-op action for a few frames so everything settles.
|
||||
|
||||
@@ -103,7 +103,6 @@ class SplitConfig:
|
||||
class MergeConfig:
|
||||
type: str = "merge"
|
||||
repo_ids: list[str] | None = None
|
||||
num_workers: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -216,7 +215,6 @@ def handle_merge(cfg: EditDatasetConfig) -> None:
|
||||
datasets,
|
||||
output_repo_id=cfg.repo_id,
|
||||
output_dir=output_dir,
|
||||
num_workers=cfg.operation.num_workers,
|
||||
)
|
||||
|
||||
logging.info(f"Merged dataset saved to {output_dir}")
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from lerobot.envs.factory import make_env, make_env_config
|
||||
|
||||
# Set MuJoCo rendering backend before importing environment
|
||||
os.environ["MUJOCO_GL"] = "egl"
|
||||
|
||||
|
||||
def assert_observations_equal(obs1, obs2, path="", atol=1e-8):
|
||||
"""
|
||||
Recursively compare two observations and assert they are equal.
|
||||
|
||||
Args:
|
||||
obs1: First observation (dict or numpy array)
|
||||
obs2: Second observation (dict or numpy array)
|
||||
path: Current path in nested structure (for error messages)
|
||||
atol: Absolute tolerance for numpy array comparisons
|
||||
"""
|
||||
if isinstance(obs1, dict) and isinstance(obs2, dict):
|
||||
assert obs1.keys() == obs2.keys(), f"Keys differ at {path}: {obs1.keys()} != {obs2.keys()}"
|
||||
for key in obs1:
|
||||
assert_observations_equal(obs1[key], obs2[key], path=f"{path}.{key}" if path else key, atol=atol)
|
||||
elif isinstance(obs1, np.ndarray) and isinstance(obs2, np.ndarray):
|
||||
assert obs1.shape == obs2.shape, f"Shape mismatch at {path}: {obs1.shape} != {obs2.shape}"
|
||||
assert obs1.dtype == obs2.dtype, f"Dtype mismatch at {path}: {obs1.dtype} != {obs2.dtype}"
|
||||
assert np.allclose(obs1, obs2, atol=atol), (
|
||||
f"Array values differ at {path}: max abs diff = {np.abs(obs1 - obs2).max()}"
|
||||
)
|
||||
else:
|
||||
assert type(obs1) is type(obs2), f"Type mismatch at {path}: {type(obs1)} != {type(obs2)}"
|
||||
assert obs1 == obs2, f"Values differ at {path}: {obs1} != {obs2}"
|
||||
|
||||
|
||||
def test_libero_env_creation():
|
||||
"""Test that the libero environment can be created successfully."""
|
||||
config = make_env_config("libero", task="libero_spatial")
|
||||
envs_dict = make_env(config)
|
||||
|
||||
assert "libero_spatial" in envs_dict
|
||||
assert 0 in envs_dict["libero_spatial"]
|
||||
|
||||
env = envs_dict["libero_spatial"][0]
|
||||
assert env is not None
|
||||
|
||||
# Test basic reset
|
||||
observation, info = env.reset(seed=42)
|
||||
assert observation is not None
|
||||
assert info is not None
|
||||
|
||||
env.close()
|
||||
|
||||
|
||||
def test_libero_reset_determinism():
|
||||
"""Test that resetting with the same seed produces identical observations."""
|
||||
config = make_env_config("libero", task="libero_spatial")
|
||||
envs_dict = make_env(config)
|
||||
env = envs_dict["libero_spatial"][0]
|
||||
|
||||
# Reset multiple times with the same seed
|
||||
obs1, info1 = env.reset(seed=42)
|
||||
obs2, info2 = env.reset(seed=42)
|
||||
obs3, info3 = env.reset(seed=42)
|
||||
|
||||
# All observations should be identical
|
||||
assert_observations_equal(obs1, obs2)
|
||||
assert_observations_equal(obs1, obs3)
|
||||
assert_observations_equal(obs2, obs3)
|
||||
|
||||
env.close()
|
||||
|
||||
|
||||
def test_libero_step_determinism():
|
||||
"""Test that step() is deterministic when resetting with the same seed."""
|
||||
config = make_env_config("libero", task="libero_spatial")
|
||||
envs_dict = make_env(config)
|
||||
env = envs_dict["libero_spatial"][0]
|
||||
|
||||
seed = 42
|
||||
|
||||
# First rollout
|
||||
obs1, info1 = env.reset(seed=seed)
|
||||
action = env.action_space.sample()
|
||||
obs_after_step1, reward1, terminated1, truncated1, info_step1 = env.step(action)
|
||||
|
||||
# Second rollout with identical seed and action
|
||||
obs2, info2 = env.reset(seed=seed)
|
||||
obs_after_step2, reward2, terminated2, truncated2, info_step2 = env.step(action)
|
||||
|
||||
# Initial observations should be identical
|
||||
assert_observations_equal(obs1, obs2)
|
||||
|
||||
# Post-step observations should be identical
|
||||
assert_observations_equal(obs_after_step1, obs_after_step2)
|
||||
|
||||
# Rewards and termination flags should be identical
|
||||
assert np.allclose(reward1, reward2), f"Rewards differ: {reward1} != {reward2}"
|
||||
assert np.array_equal(terminated1, terminated2), (
|
||||
f"Terminated flags differ: {terminated1} != {terminated2}"
|
||||
)
|
||||
assert np.array_equal(truncated1, truncated2), f"Truncated flags differ: {truncated1} != {truncated2}"
|
||||
|
||||
env.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("task", ["libero_spatial", "libero_object", "libero_goal", "libero_10"])
|
||||
def test_libero_tasks(task):
|
||||
"""Test that different libero tasks can be created and used."""
|
||||
config = make_env_config("libero", task=task)
|
||||
envs_dict = make_env(config)
|
||||
|
||||
assert task in envs_dict
|
||||
assert 0 in envs_dict[task]
|
||||
|
||||
env = envs_dict[task][0]
|
||||
observation, info = env.reset(seed=42)
|
||||
|
||||
assert observation is not None
|
||||
assert info is not None
|
||||
|
||||
# Take a step
|
||||
action = env.action_space.sample()
|
||||
obs, reward, terminated, truncated, info = env.step(action)
|
||||
|
||||
assert obs is not None
|
||||
assert reward is not None
|
||||
assert isinstance(terminated, (bool, np.ndarray))
|
||||
assert isinstance(truncated, (bool, np.ndarray))
|
||||
|
||||
env.close()
|
||||
Reference in New Issue
Block a user