修改失败重试机制
一些检查失败了
Quality / Style (push) Failing after 3s
Quality / Typos (push) Failing after 3s
Tests / Pytest (push) Failing after 3s
Tests / Pytest (minimal install) (push) Failing after 4s
Tests / End-to-end (push) Failing after 3s
Secret Leaks / trufflehog (push) Failing after 3s
Builds / CPU (push) Has been cancelled
Builds / GPU (push) Has been cancelled
Builds / GPU Dev (push) Has been cancelled
Nightly / CPU (push) Has been cancelled
Nightly / GPU (push) Has been cancelled

这个提交包含在:
2026-05-15 17:31:11 +08:00
父节点 46cb98dd81
当前提交 94244cdc92
共有 4 个文件被更改,包括 86 次插入 和 27 次删除

查看文件

@ -76,6 +76,25 @@ from lerobot.datasets.video_utils import (
CODEBASE_VERSION = "v2.1"
def _normalize_dataset_root(repo_id: str, root: str | Path | None) -> Path:
"""Normalize dataset root paths.
Supports:
- `~` expansion (avoid creating a literal './~' directory).
- Passing either the dataset directory itself OR the cache root directory.
If `root/meta/info.json` doesn't exist but `root/repo_id/meta/info.json` does, we use the latter.
"""
if root is None:
return HF_LEROBOT_HOME / repo_id
root_path = Path(root).expanduser()
if not (root_path / INFO_PATH).is_file():
candidate = root_path / repo_id
if (candidate / INFO_PATH).is_file():
return candidate
return root_path
class LeRobotDatasetMetadata:
def __init__(
self,
@ -86,7 +105,7 @@ class LeRobotDatasetMetadata:
):
self.repo_id = repo_id
self.revision = revision if revision else CODEBASE_VERSION
self.root = Path(root) if root is not None else HF_LEROBOT_HOME / repo_id
self.root = _normalize_dataset_root(repo_id, root)
try:
if force_cache_sync:
@ -311,7 +330,7 @@ class LeRobotDatasetMetadata:
"""Creates metadata for a LeRobotDataset."""
obj = cls.__new__(cls)
obj.repo_id = repo_id
obj.root = Path(root) if root is not None else HF_LEROBOT_HOME / repo_id
obj.root = Path(root).expanduser() if root is not None else HF_LEROBOT_HOME / repo_id
obj.root.mkdir(parents=True, exist_ok=False)
@ -446,7 +465,7 @@ class LeRobotDataset(torch.utils.data.Dataset):
"""
super().__init__()
self.repo_id = repo_id
self.root = Path(root) if root else HF_LEROBOT_HOME / repo_id
self.root = _normalize_dataset_root(repo_id, root)
self.image_transforms = image_transforms
self.delta_timestamps = delta_timestamps
self.episodes = episodes
@ -465,6 +484,16 @@ class LeRobotDataset(torch.utils.data.Dataset):
self.meta = LeRobotDatasetMetadata(
self.repo_id, self.root, self.revision, force_cache_sync=force_cache_sync
)
if self.episodes is not None:
invalid_episodes = [
ep_idx for ep_idx in self.episodes if ep_idx < 0 or ep_idx >= self.meta.total_episodes
]
if invalid_episodes:
raise ValueError(
f"Requested episodes {invalid_episodes} but dataset '{self.repo_id}' only contains "
f"{self.meta.total_episodes} episode(s): valid indices are [0, {self.meta.total_episodes - 1}]."
)
if self.episodes is not None and self.meta._version >= packaging.version.parse("v2.1"):
episodes_stats = [self.meta.episodes_stats[ep_idx] for ep_idx in self.episodes]
self.stats = aggregate_stats(episodes_stats)
@ -1025,7 +1054,7 @@ class MultiLeRobotDataset(torch.utils.data.Dataset):
):
super().__init__()
self.repo_ids = repo_ids
self.root = Path(root) if root else HF_LEROBOT_HOME
self.root = Path(root).expanduser() if root else HF_LEROBOT_HOME
self.tolerances_s = tolerances_s if tolerances_s else dict.fromkeys(repo_ids, 0.0001)
# Construct the underlying datasets passing everything but `transform` and `delta_timestamps` which
# are handled by this class.

查看文件

@ -20,6 +20,7 @@
# https://github.com/astral-sh/ruff/issues/3711
import abc
import contextlib
import logging
from contextlib import contextmanager
from dataclasses import dataclass
@ -466,13 +467,37 @@ class MotorsBus(abc.ABC):
f"{self.__class__.__name__}('{self.port}') is not connected. Try running `{self.__class__.__name__}.connect()` first."
)
if disable_torque:
self.port_handler.clearPort()
self.port_handler.is_using = False
self.disable_torque(num_retry=5)
try:
if disable_torque:
# Best-effort: some motors may report hardware errors (e.g. overload) and reject writes.
# Disconnect should still proceed and must not crash the caller.
with contextlib.suppress(Exception):
self.port_handler.clearPort()
with contextlib.suppress(Exception):
self.port_handler.is_using = False
self.port_handler.closePort()
logger.debug(f"{self.__class__.__name__} disconnected.")
failures: list[str] = []
for motor_name, motor in self.motors.items():
try:
self.disable_torque(motor_name, num_retry=5)
except Exception as e:
failures.append(f"{motor_name}(id={motor.id}): {e}")
if failures:
example = failures[0]
if len(failures) > 1:
logger.warning(
f"Failed to disable torque on {len(failures)} motor(s) before disconnect "
f"(e.g. {example}). Disconnecting anyway."
)
else:
logger.warning(
f"Failed to disable torque before disconnect ({example}). Disconnecting anyway."
)
finally:
with contextlib.suppress(Exception):
self.port_handler.closePort()
logger.debug(f"{self.__class__.__name__} disconnected.")
@classmethod
def scan_port(cls, port: str, *args, **kwargs) -> dict[int, list[int]]:

查看文件

@ -81,21 +81,25 @@ def replay(cfg: ReplayConfig):
actions = dataset.hf_dataset.select_columns("action")
robot.connect()
log_say("Replaying episode", cfg.play_sounds, blocking=True)
for idx in range(dataset.num_frames):
start_episode_t = time.perf_counter()
try:
log_say("Replaying episode", cfg.play_sounds, blocking=True)
for idx in range(dataset.num_frames):
start_episode_t = time.perf_counter()
action_array = actions[idx]["action"]
action = {}
for i, name in enumerate(dataset.features["action"]["names"]):
action[name] = action_array[i]
action_array = actions[idx]["action"]
action = {}
for i, name in enumerate(dataset.features["action"]["names"]):
action[name] = action_array[i]
robot.send_action(action)
robot.send_action(action)
dt_s = time.perf_counter() - start_episode_t
busy_wait(1 / dataset.fps - dt_s)
robot.disconnect()
dt_s = time.perf_counter() - start_episode_t
busy_wait(1 / dataset.fps - dt_s)
finally:
try:
robot.disconnect()
except Exception as e:
logging.warning(f"Robot disconnect failed (ignored): {e}")
if __name__ == "__main__":

查看文件

@ -115,13 +115,14 @@ def init_logging(log_file: Path | None = None, display_pid: bool = False):
def custom_format(record):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
fnameline = f"{record.pathname}:{record.lineno}"
message_text = record.getMessage()
# NOTE: Display PID is useful for multi-process logging.
if display_pid:
pid_str = f"[PID: {os.getpid()}]"
message = f"{record.levelname} {pid_str} {dt} {fnameline[-15:]:>15} {record.msg}"
message = f"{record.levelname} {pid_str} {dt} {fnameline[-15:]:>15} {message_text}"
else:
message = f"{record.levelname} {dt} {fnameline[-15:]:>15} {record.msg}"
message = f"{record.levelname} {dt} {fnameline[-15:]:>15} {message_text}"
return message
logging.basicConfig(level=logging.INFO)
@ -199,9 +200,9 @@ def say(text: str, blocking: bool = False):
cmd = ["say", text]
elif system == "Linux":
cmd = ["spd-say", text]
if blocking:
cmd.append("--wait")
# Disable text-to-speech on Linux by default to avoid Speech Dispatcher dependency
# and related startup noise/errors in headless/embedded environments.
return
elif system == "Windows":
cmd = [