镜像自地址
https://github.com/MINT-SJTU/Evo-RL.git
已同步 2026-11-21 02:27:04 +00:00
feat(policy): add minimal swing policy for hil record smoke runs
这个提交包含在:
@ -20,6 +20,7 @@ from .pi0_fast.configuration_pi0_fast import PI0FastConfig as PI0FastConfig
|
|||||||
from .pi05.configuration_pi05 import PI05Config as PI05Config
|
from .pi05.configuration_pi05 import PI05Config as PI05Config
|
||||||
from .smolvla.configuration_smolvla import SmolVLAConfig as SmolVLAConfig
|
from .smolvla.configuration_smolvla import SmolVLAConfig as SmolVLAConfig
|
||||||
from .smolvla.processor_smolvla import SmolVLANewLineProcessor
|
from .smolvla.processor_smolvla import SmolVLANewLineProcessor
|
||||||
|
from .swing.configuration_swing import SwingConfig as SwingConfig
|
||||||
from .tdmpc.configuration_tdmpc import TDMPCConfig as TDMPCConfig
|
from .tdmpc.configuration_tdmpc import TDMPCConfig as TDMPCConfig
|
||||||
from .vqbet.configuration_vqbet import VQBeTConfig as VQBeTConfig
|
from .vqbet.configuration_vqbet import VQBeTConfig as VQBeTConfig
|
||||||
from .wall_x.configuration_wall_x import WallXConfig as WallXConfig
|
from .wall_x.configuration_wall_x import WallXConfig as WallXConfig
|
||||||
@ -32,6 +33,7 @@ __all__ = [
|
|||||||
"PI05Config",
|
"PI05Config",
|
||||||
"PI0FastConfig",
|
"PI0FastConfig",
|
||||||
"SmolVLAConfig",
|
"SmolVLAConfig",
|
||||||
|
"SwingConfig",
|
||||||
"SARMConfig",
|
"SARMConfig",
|
||||||
"TDMPCConfig",
|
"TDMPCConfig",
|
||||||
"VQBeTConfig",
|
"VQBeTConfig",
|
||||||
|
|||||||
@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from .configuration_swing import SwingConfig as SwingConfig
|
||||||
|
from .modeling_swing import SwingPolicy as SwingPolicy
|
||||||
|
|
||||||
|
__all__ = ["SwingConfig", "SwingPolicy"]
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from lerobot.configs.policies import PreTrainedConfig
|
||||||
|
from lerobot.optim.optimizers import AdamConfig
|
||||||
|
|
||||||
|
|
||||||
|
@PreTrainedConfig.register_subclass("swing")
|
||||||
|
@dataclass
|
||||||
|
class SwingConfig(PreTrainedConfig):
|
||||||
|
n_obs_steps: int = 1
|
||||||
|
swing_joint_index: int = 0
|
||||||
|
swing_amplitude: float = 0.08
|
||||||
|
swing_period_steps: int = 60
|
||||||
|
center_from_observation: bool = True
|
||||||
|
optimizer_lr: float = 1e-4
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
super().__post_init__()
|
||||||
|
if self.swing_period_steps <= 0:
|
||||||
|
raise ValueError("`swing_period_steps` must be > 0.")
|
||||||
|
if self.swing_amplitude < 0:
|
||||||
|
raise ValueError("`swing_amplitude` must be >= 0.")
|
||||||
|
|
||||||
|
def get_optimizer_preset(self) -> AdamConfig:
|
||||||
|
return AdamConfig(lr=self.optimizer_lr)
|
||||||
|
|
||||||
|
def get_scheduler_preset(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def validate_features(self) -> None:
|
||||||
|
if self.action_feature is None:
|
||||||
|
raise ValueError("Swing policy requires an action feature.")
|
||||||
|
if not self.action_feature.shape:
|
||||||
|
raise ValueError("Swing policy action feature shape must be non-empty.")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def observation_delta_indices(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def action_delta_indices(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def reward_delta_indices(self) -> None:
|
||||||
|
return None
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import math
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from torch import Tensor, nn
|
||||||
|
|
||||||
|
from lerobot.policies.pretrained import ActionSelectKwargs, PreTrainedPolicy
|
||||||
|
from lerobot.utils.constants import OBS_STATE
|
||||||
|
|
||||||
|
from .configuration_swing import SwingConfig
|
||||||
|
|
||||||
|
|
||||||
|
class SwingPolicy(PreTrainedPolicy):
|
||||||
|
config_class = SwingConfig
|
||||||
|
name = "swing"
|
||||||
|
|
||||||
|
def __init__(self, config: SwingConfig, *inputs, **kwargs):
|
||||||
|
super().__init__(config, *inputs, **kwargs)
|
||||||
|
self._step = 0
|
||||||
|
self._anchor = nn.Parameter(torch.zeros(1), requires_grad=True)
|
||||||
|
|
||||||
|
def get_optim_params(self) -> dict:
|
||||||
|
return {"params": [self._anchor]}
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self._step = 0
|
||||||
|
|
||||||
|
def forward(self, batch: dict[str, Tensor]) -> tuple[Tensor, dict | None]:
|
||||||
|
loss = self._anchor.sum() * 0.0
|
||||||
|
return loss, None
|
||||||
|
|
||||||
|
def _action_dim(self) -> int:
|
||||||
|
action_feature = self.config.action_feature
|
||||||
|
if action_feature is None or not action_feature.shape:
|
||||||
|
raise ValueError("Swing policy requires a valid action feature shape.")
|
||||||
|
return int(action_feature.shape[0])
|
||||||
|
|
||||||
|
def predict_action_chunk(self, batch: dict[str, Tensor], **kwargs: ActionSelectKwargs) -> Tensor:
|
||||||
|
return self.select_action(batch, **kwargs)
|
||||||
|
|
||||||
|
def select_action(self, batch: dict[str, Tensor], **kwargs: ActionSelectKwargs) -> Tensor:
|
||||||
|
action_dim = self._action_dim()
|
||||||
|
action = torch.zeros((1, action_dim), device=self._anchor.device, dtype=torch.float32)
|
||||||
|
|
||||||
|
if self.config.center_from_observation and OBS_STATE in batch:
|
||||||
|
obs_state = batch[OBS_STATE]
|
||||||
|
if obs_state.dim() == 1:
|
||||||
|
obs_state = obs_state.unsqueeze(0)
|
||||||
|
if obs_state.shape[-1] >= action_dim:
|
||||||
|
action = obs_state[:, :action_dim].to(dtype=torch.float32).clone()
|
||||||
|
|
||||||
|
period = max(self.config.swing_period_steps, 1)
|
||||||
|
phase = 2.0 * math.pi * ((self._step % period) / period)
|
||||||
|
offset = self.config.swing_amplitude if math.sin(phase) >= 0 else -self.config.swing_amplitude
|
||||||
|
joint_idx = max(0, min(self.config.swing_joint_index, action_dim - 1))
|
||||||
|
action[:, joint_idx] = action[:, joint_idx] + offset
|
||||||
|
self._step += 1
|
||||||
|
return action
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from lerobot.processor import (
|
||||||
|
AddBatchDimensionProcessorStep,
|
||||||
|
DeviceProcessorStep,
|
||||||
|
PolicyAction,
|
||||||
|
PolicyProcessorPipeline,
|
||||||
|
RenameObservationsProcessorStep,
|
||||||
|
)
|
||||||
|
from lerobot.processor.converters import policy_action_to_transition, transition_to_policy_action
|
||||||
|
from lerobot.utils.constants import POLICY_POSTPROCESSOR_DEFAULT_NAME, POLICY_PREPROCESSOR_DEFAULT_NAME
|
||||||
|
|
||||||
|
from .configuration_swing import SwingConfig
|
||||||
|
|
||||||
|
|
||||||
|
def make_swing_pre_post_processors(
|
||||||
|
config: SwingConfig,
|
||||||
|
dataset_stats: dict[str, dict[str, Any]] | None = None,
|
||||||
|
) -> tuple[
|
||||||
|
PolicyProcessorPipeline[dict[str, Any], dict[str, Any]],
|
||||||
|
PolicyProcessorPipeline[PolicyAction, PolicyAction],
|
||||||
|
]:
|
||||||
|
del dataset_stats
|
||||||
|
pre_steps = [
|
||||||
|
RenameObservationsProcessorStep(rename_map={}),
|
||||||
|
AddBatchDimensionProcessorStep(),
|
||||||
|
DeviceProcessorStep(device=config.device),
|
||||||
|
]
|
||||||
|
post_steps = [DeviceProcessorStep(device="cpu")]
|
||||||
|
return (
|
||||||
|
PolicyProcessorPipeline[dict[str, Any], dict[str, Any]](
|
||||||
|
steps=pre_steps,
|
||||||
|
name=POLICY_PREPROCESSOR_DEFAULT_NAME,
|
||||||
|
),
|
||||||
|
PolicyProcessorPipeline[PolicyAction, PolicyAction](
|
||||||
|
steps=post_steps,
|
||||||
|
name=POLICY_POSTPROCESSOR_DEFAULT_NAME,
|
||||||
|
to_transition=policy_action_to_transition,
|
||||||
|
to_output=transition_to_policy_action,
|
||||||
|
),
|
||||||
|
)
|
||||||
在新工单中引用
屏蔽一个用户