镜像自地址
https://github.com/MINT-SJTU/Evo-RL.git
已同步 2026-11-20 18:17:06 +00:00
snapshot: sync mint main from current workspace 2026-03-03 16:32:56 +0800
这个提交包含在:
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from lerobot.scripts.lerobot_value_infer import ContiguousDistributedEvalSampler
|
||||
|
||||
|
||||
def test_contiguous_distributed_eval_sampler_has_contiguous_shards_with_tail_padding():
|
||||
dataset_size = 10
|
||||
world_size = 3
|
||||
|
||||
shards: list[list[int]] = []
|
||||
for rank in range(world_size):
|
||||
sampler = ContiguousDistributedEvalSampler(dataset_size=dataset_size, num_replicas=world_size, rank=rank)
|
||||
shard = list(iter(sampler))
|
||||
shards.append(shard)
|
||||
assert len(shard) == len(sampler) == 4
|
||||
|
||||
assert shards[0] == [0, 1, 2, 3]
|
||||
assert shards[1] == [4, 5, 6, 7]
|
||||
assert shards[2] == [8, 9, 9, 9]
|
||||
|
||||
covered = set()
|
||||
for shard in shards:
|
||||
covered.update(shard)
|
||||
assert covered == set(range(dataset_size))
|
||||
|
||||
|
||||
def test_contiguous_distributed_eval_sampler_handles_more_ranks_than_samples():
|
||||
dataset_size = 2
|
||||
world_size = 4
|
||||
|
||||
shards: list[list[int]] = []
|
||||
for rank in range(world_size):
|
||||
sampler = ContiguousDistributedEvalSampler(dataset_size=dataset_size, num_replicas=world_size, rank=rank)
|
||||
shard = list(iter(sampler))
|
||||
shards.append(shard)
|
||||
assert len(shard) == len(sampler) == 1
|
||||
|
||||
assert shards[0] == [0]
|
||||
assert shards[1] == [1]
|
||||
assert shards[2] == [1]
|
||||
assert shards[3] == [1]
|
||||
|
||||
covered = set()
|
||||
for shard in shards:
|
||||
covered.update(shard)
|
||||
assert covered == set(range(dataset_size))
|
||||
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import numpy as np
|
||||
|
||||
from lerobot.scripts.lerobot_value_infer import (
|
||||
_binarize_advantages,
|
||||
_compute_dense_rewards_from_targets,
|
||||
_compute_n_step_advantages,
|
||||
_compute_task_thresholds,
|
||||
)
|
||||
|
||||
|
||||
def test_compute_dense_rewards_from_targets_terminal_handling():
|
||||
episode_indices = np.array([0, 0, 0, 1, 1], dtype=np.int64)
|
||||
frame_indices = np.array([0, 1, 2, 0, 1], dtype=np.int64)
|
||||
targets = np.array([-0.6, -0.4, -0.2, -0.8, -0.5], dtype=np.float32)
|
||||
|
||||
rewards = _compute_dense_rewards_from_targets(targets, episode_indices, frame_indices)
|
||||
expected = np.array([-0.2, -0.2, -0.2, -0.3, -0.5], dtype=np.float32)
|
||||
assert np.allclose(rewards, expected)
|
||||
|
||||
|
||||
def test_compute_n_step_advantages_simple_case():
|
||||
rewards = np.array([-0.2, -0.2, -0.2], dtype=np.float32)
|
||||
values = np.array([-0.5, -0.3, -0.1], dtype=np.float32)
|
||||
episode_indices = np.array([0, 0, 0], dtype=np.int64)
|
||||
frame_indices = np.array([0, 1, 2], dtype=np.int64)
|
||||
|
||||
advantages = _compute_n_step_advantages(
|
||||
rewards=rewards,
|
||||
values=values,
|
||||
episode_indices=episode_indices,
|
||||
frame_indices=frame_indices,
|
||||
n_step=2,
|
||||
)
|
||||
|
||||
expected = np.array([0.0, -0.1, -0.1], dtype=np.float32)
|
||||
assert np.allclose(advantages, expected)
|
||||
|
||||
|
||||
def test_compute_task_thresholds_and_binarize_with_interventions():
|
||||
task_indices = np.array([0, 0, 0, 1, 1], dtype=np.int64)
|
||||
advantages = np.array([-0.4, -0.1, 0.3, -0.2, 0.2], dtype=np.float32)
|
||||
interventions = np.array([0, 1, 0, 0, 0], dtype=np.float32)
|
||||
|
||||
thresholds = _compute_task_thresholds(task_indices, advantages, positive_ratio=0.5)
|
||||
|
||||
indicators = _binarize_advantages(
|
||||
task_indices=task_indices,
|
||||
advantages=advantages,
|
||||
thresholds=thresholds,
|
||||
interventions=interventions,
|
||||
force_intervention_positive=True,
|
||||
)
|
||||
|
||||
assert indicators.tolist() == [0, 1, 1, 0, 1]
|
||||
@ -0,0 +1,252 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from PIL import Image
|
||||
|
||||
from lerobot.configs.value import (
|
||||
ValueInferenceACPConfig,
|
||||
ValueInferenceCheckpointConfig,
|
||||
ValueInferenceDatasetConfig,
|
||||
ValueInferencePipelineConfig,
|
||||
ValueInferenceVizConfig,
|
||||
)
|
||||
from lerobot.scripts import lerobot_value_infer as value_infer
|
||||
from lerobot.scripts import value_infer_viz
|
||||
|
||||
|
||||
class _FakeHFDataset:
|
||||
def __init__(self, data: dict[str, list]):
|
||||
self._data = data
|
||||
self.column_names = list(data.keys())
|
||||
|
||||
def with_format(self, _):
|
||||
return self
|
||||
|
||||
def __len__(self):
|
||||
return len(next(iter(self._data.values())))
|
||||
|
||||
def __getitem__(self, key: str):
|
||||
return self._data[key]
|
||||
|
||||
|
||||
class _FakeAccelerator:
|
||||
def __init__(self):
|
||||
self.is_main_process = True
|
||||
self.num_processes = 1
|
||||
self.device = torch.device("cpu")
|
||||
|
||||
def wait_for_everyone(self):
|
||||
return None
|
||||
|
||||
def end_training(self):
|
||||
return None
|
||||
|
||||
|
||||
class _FakeMeta:
|
||||
camera_keys = ["observation.images.front"]
|
||||
|
||||
def get_video_file_path(self, ep_idx: int, vid_key: str) -> str:
|
||||
return f"videos/{vid_key}/episode-{ep_idx:06d}.mp4"
|
||||
|
||||
|
||||
class _FakeDataset:
|
||||
repo_id = "dummy/repo"
|
||||
root = "/tmp"
|
||||
fps = 30
|
||||
episodes = [64, 101]
|
||||
meta = _FakeMeta()
|
||||
|
||||
def __init__(self):
|
||||
self.hf_dataset = _FakeHFDataset(
|
||||
{
|
||||
"index": [1000, 1001, 1002, 5000, 5001],
|
||||
"episode_index": [64, 64, 64, 101, 101],
|
||||
"frame_index": [0, 1, 2, 0, 1],
|
||||
"complementary_info.value": [-0.8, -0.7, -0.6, -0.3, -0.2],
|
||||
"complementary_info.advantage": [0.1, 0.2, 0.3, 0.4, 0.5],
|
||||
"complementary_info.acp_indicator": [0, 1, 1, 0, 1],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_select_video_key_prefers_front_when_video_key_not_set():
|
||||
keys = [
|
||||
"observation.images.left_wrist",
|
||||
"observation.images.right_wrist",
|
||||
"observation.images.right_front",
|
||||
]
|
||||
assert value_infer_viz._select_video_key(keys, None) == "observation.images.right_front"
|
||||
|
||||
|
||||
def test_export_overlay_videos_uses_episode_index_for_subset_alignment(monkeypatch, tmp_path: Path):
|
||||
dataset = _FakeDataset()
|
||||
|
||||
def _fake_export_single_episode(
|
||||
src,
|
||||
dst,
|
||||
ep_values,
|
||||
ep_advantages,
|
||||
ep_indicators,
|
||||
episode_timestamps_s,
|
||||
fps,
|
||||
vcodec,
|
||||
tolerance_s,
|
||||
video_backend,
|
||||
frame_storage_mode,
|
||||
temp_dir_root,
|
||||
):
|
||||
assert isinstance(src, Path)
|
||||
assert isinstance(dst, Path)
|
||||
assert isinstance(episode_timestamps_s, np.ndarray)
|
||||
assert fps == 30
|
||||
assert vcodec == "libsvtav1"
|
||||
assert tolerance_s > 0
|
||||
assert frame_storage_mode == "memory"
|
||||
assert temp_dir_root == tmp_path
|
||||
assert isinstance(ep_values, np.ndarray)
|
||||
assert isinstance(ep_advantages, np.ndarray)
|
||||
assert isinstance(ep_indicators, np.ndarray)
|
||||
return dst
|
||||
|
||||
monkeypatch.setattr(value_infer_viz, "_export_single_episode", _fake_export_single_episode)
|
||||
|
||||
written = value_infer._export_overlay_videos(
|
||||
dataset=dataset,
|
||||
value_field="complementary_info.value",
|
||||
advantage_field="complementary_info.advantage",
|
||||
indicator_field="complementary_info.acp_indicator",
|
||||
viz_episodes="all",
|
||||
video_key=None,
|
||||
output_dir=tmp_path,
|
||||
overwrite=True,
|
||||
vcodec="libsvtav1",
|
||||
)
|
||||
|
||||
assert len(written) == 2
|
||||
names = [path.name for path in written]
|
||||
assert any("episode_0064" in name for name in names)
|
||||
assert any("episode_0101" in name for name in names)
|
||||
|
||||
|
||||
def test_export_overlay_videos_passes_disk_storage_mode(monkeypatch, tmp_path: Path):
|
||||
dataset = _FakeDataset()
|
||||
|
||||
def _fake_export_single_episode(
|
||||
src,
|
||||
dst,
|
||||
ep_values,
|
||||
ep_advantages,
|
||||
ep_indicators,
|
||||
episode_timestamps_s,
|
||||
fps,
|
||||
vcodec,
|
||||
tolerance_s,
|
||||
video_backend,
|
||||
frame_storage_mode,
|
||||
temp_dir_root,
|
||||
):
|
||||
assert isinstance(episode_timestamps_s, np.ndarray)
|
||||
assert tolerance_s > 0
|
||||
assert frame_storage_mode == "disk"
|
||||
assert temp_dir_root == tmp_path
|
||||
return dst
|
||||
|
||||
monkeypatch.setattr(value_infer_viz, "_export_single_episode", _fake_export_single_episode)
|
||||
|
||||
written = value_infer._export_overlay_videos(
|
||||
dataset=dataset,
|
||||
value_field="complementary_info.value",
|
||||
advantage_field="complementary_info.advantage",
|
||||
indicator_field="complementary_info.acp_indicator",
|
||||
viz_episodes="all",
|
||||
video_key=None,
|
||||
output_dir=tmp_path,
|
||||
overwrite=True,
|
||||
vcodec="h264",
|
||||
frame_storage_mode="disk",
|
||||
)
|
||||
|
||||
assert len(written) == 2
|
||||
|
||||
|
||||
def test_get_video_encode_options_for_h264_nvenc():
|
||||
options, pix_fmt = value_infer._get_video_encode_options("h264_nvenc")
|
||||
assert pix_fmt == "yuv420p"
|
||||
assert options["preset"] == "p4"
|
||||
assert options["rc"] == "vbr"
|
||||
assert options["cq"] == "28"
|
||||
assert options["b"] == "0"
|
||||
assert options["g"] == "60"
|
||||
assert "crf" not in options
|
||||
|
||||
|
||||
def test_get_episode_value_bounds_returns_episode_min_max():
|
||||
y_min, y_max = value_infer._get_episode_value_bounds(np.array([-0.7, -0.3, -0.5], dtype=np.float32))
|
||||
assert np.isclose(y_min, -0.7)
|
||||
assert np.isclose(y_max, -0.3)
|
||||
|
||||
|
||||
def test_acp_disabled_skips_value_inference_and_uses_default_viz_dir(monkeypatch, tmp_path: Path):
|
||||
accelerator = _FakeAccelerator()
|
||||
dataset = _FakeDataset()
|
||||
captured: dict[str, Path] = {}
|
||||
|
||||
def _fake_export_overlay_videos(**kwargs):
|
||||
out_dir = kwargs["output_dir"]
|
||||
captured["output_dir"] = out_dir
|
||||
return [out_dir / "dummy.mp4"]
|
||||
|
||||
def _fail_resolve_pretrained(*args, **kwargs):
|
||||
raise AssertionError("checkpoint resolution should be skipped when acp.enable=false")
|
||||
|
||||
monkeypatch.setattr(value_infer, "_create_accelerator", lambda cfg, acc: accelerator)
|
||||
monkeypatch.setattr(
|
||||
value_infer,
|
||||
"_init_runtime",
|
||||
lambda cfg, accelerator: (tmp_path / "runtime" / "value", torch.device("cpu")),
|
||||
)
|
||||
monkeypatch.setattr(value_infer, "_load_dataset_distributed", lambda cfg, accelerator: dataset)
|
||||
monkeypatch.setattr(value_infer, "_resolve_pretrained_model_dir", _fail_resolve_pretrained)
|
||||
monkeypatch.setattr(value_infer, "_export_overlay_videos", _fake_export_overlay_videos)
|
||||
|
||||
cfg = ValueInferencePipelineConfig(
|
||||
dataset=ValueInferenceDatasetConfig(repo_id="dummy/repo"),
|
||||
inference=ValueInferenceCheckpointConfig(checkpoint_path="unused"),
|
||||
acp=ValueInferenceACPConfig(enable=False),
|
||||
viz=ValueInferenceVizConfig(enable=True, episodes="all"),
|
||||
output_dir=tmp_path / "pipeline_out",
|
||||
job_name="viz_skip_test",
|
||||
)
|
||||
|
||||
result = value_infer.run_value_inference_pipeline(cfg)
|
||||
|
||||
expected_viz_dir = tmp_path / "runtime" / "value" / "viz"
|
||||
assert captured["output_dir"] == expected_viz_dir
|
||||
assert result["main_process"] is True
|
||||
assert result["acp_enabled"] is False
|
||||
assert result["value_inference_skipped"] is True
|
||||
assert result["checkpoint"] is None
|
||||
assert result["viz_outputs"] == [str(expected_viz_dir / "dummy.mp4")]
|
||||
|
||||
|
||||
def test_decode_frames_at_timestamps_scales_float_frames_to_uint8(monkeypatch, tmp_path: Path):
|
||||
fake = torch.full((1, 3, 2, 2), 0.5, dtype=torch.float32)
|
||||
|
||||
def _fake_decode_video_frames(video_path, timestamps, tolerance_s, backend):
|
||||
return fake
|
||||
|
||||
monkeypatch.setattr(value_infer_viz, "decode_video_frames", _fake_decode_video_frames)
|
||||
frames = value_infer_viz._decode_frames_at_timestamps(
|
||||
video_file=tmp_path / "dummy.mp4",
|
||||
timestamps_s=np.array([0.0], dtype=np.float64),
|
||||
tolerance_s=1e-4,
|
||||
backend="pyav",
|
||||
)
|
||||
|
||||
assert len(frames) == 1
|
||||
assert isinstance(frames[0], Image.Image)
|
||||
arr = np.asarray(frames[0], dtype=np.uint8)
|
||||
assert int(arr.mean()) >= 120
|
||||
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from lerobot.values.pistar06.modeling_pistar06 import (
|
||||
EpisodeTargetInfo,
|
||||
build_bin_centers,
|
||||
compute_normalized_value_targets,
|
||||
expected_value_from_logits,
|
||||
project_values_to_bins,
|
||||
)
|
||||
|
||||
|
||||
def test_compute_normalized_value_targets():
|
||||
episode_indices = np.array([0, 0, 0, 1, 1], dtype=np.int64)
|
||||
frame_indices = np.array([0, 1, 2, 0, 1], dtype=np.int64)
|
||||
episode_info = {
|
||||
0: EpisodeTargetInfo(episode_index=0, task_index=0, length=3, success=True),
|
||||
1: EpisodeTargetInfo(episode_index=1, task_index=0, length=2, success=False),
|
||||
}
|
||||
task_max_lengths = {0: 3}
|
||||
|
||||
targets = compute_normalized_value_targets(
|
||||
episode_indices=episode_indices,
|
||||
frame_indices=frame_indices,
|
||||
episode_info=episode_info,
|
||||
task_max_lengths=task_max_lengths,
|
||||
c_fail_coef=1.0,
|
||||
)
|
||||
expected_targets = np.array([-2 / 6, -1 / 6, 0.0, -4 / 6, -3 / 6], dtype=np.float32)
|
||||
assert np.allclose(targets, expected_targets)
|
||||
|
||||
|
||||
def test_project_values_to_bins_interpolates_between_neighbors():
|
||||
centers = build_bin_centers(num_bins=3, bin_min=-1.0, bin_max=0.0)
|
||||
values = torch.tensor([-1.0, -0.5, -0.25, 0.0], dtype=torch.float32)
|
||||
target = project_values_to_bins(values, centers)
|
||||
|
||||
assert target.shape == (4, 3)
|
||||
assert torch.allclose(target[0], torch.tensor([1.0, 0.0, 0.0]), atol=1e-6)
|
||||
assert torch.allclose(target[1], torch.tensor([0.0, 1.0, 0.0]), atol=1e-6)
|
||||
assert torch.allclose(target[2], torch.tensor([0.0, 0.5, 0.5]), atol=1e-6)
|
||||
assert torch.allclose(target[3], torch.tensor([0.0, 0.0, 1.0]), atol=1e-6)
|
||||
assert torch.allclose(target.sum(dim=-1), torch.ones(4), atol=1e-6)
|
||||
|
||||
|
||||
def test_expected_value_from_logits_matches_bin_center_weighting():
|
||||
centers = build_bin_centers(num_bins=3, bin_min=-1.0, bin_max=0.0)
|
||||
logits = torch.tensor(
|
||||
[
|
||||
[100.0, -100.0, -100.0],
|
||||
[-100.0, -100.0, 100.0],
|
||||
],
|
||||
dtype=torch.float32,
|
||||
)
|
||||
expected = expected_value_from_logits(logits, centers)
|
||||
assert torch.allclose(expected, torch.tensor([-1.0, 0.0]), atol=1e-5)
|
||||
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from lerobot.optim.schedulers import CosineDecayWithWarmupSchedulerConfig
|
||||
from lerobot.policies.factory import make_policy_config
|
||||
from lerobot.values.pistar06.configuration_pistar06 import Pistar06Config
|
||||
|
||||
|
||||
def test_pistar06_config_from_dict():
|
||||
payload = {
|
||||
"type": "pistar06",
|
||||
"num_bins": 101,
|
||||
"bin_min": -1.0,
|
||||
"bin_max": 0.0,
|
||||
"task_index_feature": "task_index",
|
||||
"task_field": "task",
|
||||
"camera_features": ["observation.images.front"],
|
||||
"language_repo_id": "google/gemma-3-270m",
|
||||
"vision_repo_id": "google/siglip-so400m-patch14-384",
|
||||
"dropout": 0.2,
|
||||
}
|
||||
cfg = make_policy_config(payload.pop("type"), **payload)
|
||||
assert isinstance(cfg, Pistar06Config)
|
||||
assert cfg.type == "pistar06"
|
||||
assert cfg.num_bins == 101
|
||||
assert cfg.camera_features == ["observation.images.front"]
|
||||
assert cfg.loss_weight_key == "observation.value_loss_weight"
|
||||
|
||||
|
||||
def test_pistar06_preset_uses_cosine_decay_with_warmup():
|
||||
cfg = Pistar06Config()
|
||||
scheduler_cfg = cfg.get_scheduler_preset()
|
||||
assert isinstance(scheduler_cfg, CosineDecayWithWarmupSchedulerConfig)
|
||||
assert scheduler_cfg.peak_lr == cfg.optimizer_lr
|
||||
assert scheduler_cfg.decay_lr == cfg.scheduler_decay_lr
|
||||
assert scheduler_cfg.num_warmup_steps == cfg.scheduler_warmup_steps
|
||||
assert scheduler_cfg.num_decay_steps == cfg.scheduler_decay_steps
|
||||
@ -0,0 +1,489 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
from safetensors.torch import load_file
|
||||
from torch import nn
|
||||
|
||||
import lerobot.processor.tokenizer_processor as tokenizer_processor
|
||||
import lerobot.values.pistar06.modeling_pistar06 as pistar06_modeling
|
||||
import lerobot.values.pistar06.processor_pistar06 as pistar06_processor
|
||||
from lerobot.configs.value_train import ValueTargetsConfig
|
||||
from lerobot.utils.constants import OBS_LANGUAGE_ATTENTION_MASK, OBS_LANGUAGE_TOKENS, OBS_STATE
|
||||
from lerobot.values.pistar06.configuration_pistar06 import Pistar06Config
|
||||
from lerobot.values.pistar06.modeling_pistar06 import (
|
||||
PISTAR06_SAVE_INFO,
|
||||
Pistar06Model,
|
||||
Pistar06Policy,
|
||||
)
|
||||
from lerobot.values.pistar06.processor_pistar06 import (
|
||||
PISTAR06_IMAGE_MASK_KEY,
|
||||
PISTAR06_IMAGES_KEY,
|
||||
Pistar06PrepareTaskPromptProcessorStep,
|
||||
make_pistar06_pre_post_processors,
|
||||
)
|
||||
|
||||
|
||||
class _DummyTokenizer:
|
||||
pad_token_id = 0
|
||||
eos_token = "<eos>"
|
||||
|
||||
@classmethod
|
||||
def from_pretrained(cls, *args, **kwargs):
|
||||
return cls()
|
||||
|
||||
def __call__(self, prompts, max_length, truncation, padding, return_tensors, padding_side="right"):
|
||||
del truncation, padding, return_tensors, padding_side
|
||||
bsize = len(prompts)
|
||||
input_ids = torch.zeros((bsize, max_length), dtype=torch.long)
|
||||
attention_mask = torch.zeros((bsize, max_length), dtype=torch.long)
|
||||
for i, prompt in enumerate(prompts):
|
||||
token_count = min(max_length, max(1, len(prompt.split())))
|
||||
input_ids[i, :token_count] = torch.arange(1, token_count + 1, dtype=torch.long)
|
||||
attention_mask[i, :token_count] = 1
|
||||
return {"input_ids": input_ids, "attention_mask": attention_mask}
|
||||
|
||||
|
||||
class _DummyImageProcessor:
|
||||
size = {"height": 32, "width": 32}
|
||||
image_mean = [0.5, 0.5, 0.5]
|
||||
image_std = [0.5, 0.5, 0.5]
|
||||
|
||||
@classmethod
|
||||
def from_pretrained(cls, *args, **kwargs):
|
||||
return cls()
|
||||
|
||||
|
||||
class _DummyVisionModel(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.image_proj = nn.Linear(3, 16)
|
||||
self.config = SimpleNamespace(hidden_size=16)
|
||||
self.gradient_checkpointing = False
|
||||
|
||||
def gradient_checkpointing_enable(self, gradient_checkpointing_kwargs=None):
|
||||
del gradient_checkpointing_kwargs
|
||||
self.gradient_checkpointing = True
|
||||
|
||||
def get_image_features(self, pixel_values):
|
||||
pooled = pixel_values.mean(dim=(-1, -2))
|
||||
return self.image_proj(pooled)
|
||||
|
||||
|
||||
class _DummyLanguageModel(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.config = SimpleNamespace(hidden_size=32)
|
||||
self.embed = nn.Embedding(2048, 32)
|
||||
self.proj = nn.Linear(32, 32)
|
||||
self.gradient_checkpointing = False
|
||||
|
||||
def gradient_checkpointing_enable(self, gradient_checkpointing_kwargs=None):
|
||||
del gradient_checkpointing_kwargs
|
||||
self.gradient_checkpointing = True
|
||||
|
||||
def forward(self, input_ids, attention_mask, return_dict):
|
||||
del attention_mask, return_dict
|
||||
hidden = self.proj(self.embed(input_ids))
|
||||
return SimpleNamespace(last_hidden_state=hidden)
|
||||
|
||||
|
||||
class _DummyAutoModel:
|
||||
@classmethod
|
||||
def from_pretrained(cls, repo_id, **kwargs):
|
||||
output_loading_info = bool(kwargs.pop("output_loading_info", False))
|
||||
if "siglip" in repo_id.lower():
|
||||
model = _DummyVisionModel()
|
||||
else:
|
||||
model = _DummyLanguageModel()
|
||||
if output_loading_info:
|
||||
return model, {"missing_keys": [], "unexpected_keys": [], "mismatched_keys": []}
|
||||
return model
|
||||
|
||||
|
||||
class _DummyAutoModelForCausalLM:
|
||||
@classmethod
|
||||
def from_pretrained(cls, repo_id, **kwargs):
|
||||
del repo_id
|
||||
output_loading_info = bool(kwargs.pop("output_loading_info", False))
|
||||
model = SimpleNamespace(model=_DummyLanguageModel())
|
||||
if output_loading_info:
|
||||
return model, {"missing_keys": [], "unexpected_keys": [], "mismatched_keys": []}
|
||||
return model
|
||||
|
||||
|
||||
class _DummyAutoConfig:
|
||||
@classmethod
|
||||
def from_pretrained(cls, *args, **kwargs):
|
||||
del args, kwargs
|
||||
return SimpleNamespace(architectures=[])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hf_stubs(monkeypatch):
|
||||
monkeypatch.setattr(tokenizer_processor, "AutoTokenizer", _DummyTokenizer)
|
||||
monkeypatch.setattr(pistar06_modeling, "AutoImageProcessor", _DummyImageProcessor)
|
||||
monkeypatch.setattr(pistar06_modeling, "AutoModel", _DummyAutoModel)
|
||||
monkeypatch.setattr(pistar06_modeling, "AutoModelForCausalLM", _DummyAutoModelForCausalLM)
|
||||
monkeypatch.setattr(pistar06_modeling, "AutoConfig", _DummyAutoConfig)
|
||||
|
||||
|
||||
class _FakeDatasetTable:
|
||||
def __init__(self, data: dict[str, list]):
|
||||
self._data = data
|
||||
self.column_names = list(data.keys())
|
||||
|
||||
def with_format(self, _):
|
||||
return self
|
||||
|
||||
def __len__(self):
|
||||
return len(next(iter(self._data.values())))
|
||||
|
||||
def __getitem__(self, key):
|
||||
if isinstance(key, slice):
|
||||
return self._data
|
||||
return self._data[key]
|
||||
|
||||
|
||||
class _TaskLocAccessor:
|
||||
def __init__(self, mapping: dict[str, int]):
|
||||
self._mapping = mapping
|
||||
|
||||
def __getitem__(self, task_name: str):
|
||||
return SimpleNamespace(task_index=self._mapping[task_name])
|
||||
|
||||
|
||||
class _TaskIndexTable:
|
||||
def __init__(self, mapping: dict[str, int]):
|
||||
self.index = mapping.keys()
|
||||
self.loc = _TaskLocAccessor(mapping)
|
||||
|
||||
|
||||
def test_pistar06_processor_pads_missing_cameras_and_tokenizes(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front", "observation.images.wrist"],
|
||||
tokenizer_max_length=16,
|
||||
)
|
||||
preprocessor, _ = make_pistar06_pre_post_processors(cfg)
|
||||
|
||||
raw_batch = {
|
||||
"task": ["pick bottle", "place bottle"],
|
||||
OBS_STATE: torch.rand(2, 12),
|
||||
"observation.images.front": torch.rand(2, 3, 48, 40),
|
||||
}
|
||||
processed = preprocessor(raw_batch)
|
||||
|
||||
assert processed[OBS_LANGUAGE_TOKENS].shape == (2, 16)
|
||||
assert processed[OBS_LANGUAGE_ATTENTION_MASK].dtype == torch.bool
|
||||
assert processed[PISTAR06_IMAGES_KEY].shape == (2, 2, 3, 48, 40)
|
||||
assert torch.equal(processed[PISTAR06_IMAGE_MASK_KEY][:, 0], torch.ones(2, dtype=torch.bool))
|
||||
assert torch.equal(processed[PISTAR06_IMAGE_MASK_KEY][:, 1], torch.zeros(2, dtype=torch.bool))
|
||||
|
||||
|
||||
def test_pistar06_processor_requires_task_field(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(device="cpu", camera_features=["observation.images.front"])
|
||||
preprocessor, _ = make_pistar06_pre_post_processors(cfg)
|
||||
|
||||
with pytest.raises(KeyError, match="Missing task field"):
|
||||
preprocessor({OBS_STATE: torch.rand(2, 12), "observation.images.front": torch.rand(2, 3, 48, 40)})
|
||||
|
||||
|
||||
def test_pistar06_processor_can_disable_state_in_prompt(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
include_state_in_prompt=False,
|
||||
)
|
||||
preprocessor, _ = make_pistar06_pre_post_processors(cfg)
|
||||
|
||||
raw_batch = {
|
||||
"task": ["pick bottle", "place bottle"],
|
||||
"observation.images.front": torch.rand(2, 3, 48, 40),
|
||||
}
|
||||
processed = preprocessor(raw_batch)
|
||||
assert processed[OBS_LANGUAGE_TOKENS].shape == (2, cfg.tokenizer_max_length)
|
||||
|
||||
|
||||
def test_pistar06_model_forward(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
fusion_hidden_dim=32,
|
||||
fusion_num_heads=8,
|
||||
state_proj_dim=16,
|
||||
num_bins=17,
|
||||
)
|
||||
model = Pistar06Model(cfg)
|
||||
|
||||
outputs = model(
|
||||
input_ids=torch.randint(0, 20, (3, 12), dtype=torch.long),
|
||||
attention_mask=torch.ones(3, 12, dtype=torch.bool),
|
||||
images=torch.rand(3, 1, 3, 32, 32),
|
||||
image_attention_mask=torch.ones(3, 1, dtype=torch.bool),
|
||||
)
|
||||
assert outputs.shape == (3, 17)
|
||||
|
||||
|
||||
def test_pistar06_policy_requires_valid_camera_mask(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
fusion_hidden_dim=32,
|
||||
fusion_num_heads=8,
|
||||
state_proj_dim=16,
|
||||
num_bins=17,
|
||||
)
|
||||
policy = Pistar06Policy(config=cfg)
|
||||
|
||||
with pytest.raises(ValueError, match="at least one valid camera"):
|
||||
policy.model(
|
||||
input_ids=torch.randint(0, 10, (2, 8), dtype=torch.long),
|
||||
attention_mask=torch.ones(2, 8, dtype=torch.bool),
|
||||
images=torch.rand(2, 1, 3, 16, 16),
|
||||
image_attention_mask=torch.zeros(2, 1, dtype=torch.bool),
|
||||
)
|
||||
|
||||
|
||||
def test_pistar06_policy_forward_computes_loss(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
target_key="observation.value_target",
|
||||
num_bins=17,
|
||||
fusion_hidden_dim=32,
|
||||
fusion_num_heads=8,
|
||||
)
|
||||
policy = Pistar06Policy(config=cfg)
|
||||
|
||||
batch = {
|
||||
OBS_LANGUAGE_TOKENS: torch.randint(0, 100, (4, 12), dtype=torch.long),
|
||||
OBS_LANGUAGE_ATTENTION_MASK: torch.ones(4, 12, dtype=torch.bool),
|
||||
PISTAR06_IMAGES_KEY: torch.rand(4, 1, 3, 32, 32),
|
||||
PISTAR06_IMAGE_MASK_KEY: torch.ones(4, 1, dtype=torch.bool),
|
||||
cfg.target_key: torch.tensor([-1.0, -0.8, -0.4, 0.0], dtype=torch.float32),
|
||||
}
|
||||
|
||||
loss, loss_dict = policy.forward(batch)
|
||||
assert isinstance(loss, torch.Tensor)
|
||||
assert loss.ndim == 0
|
||||
assert "loss" in loss_dict
|
||||
assert "value_mae" in loss_dict
|
||||
|
||||
|
||||
def test_pistar06_policy_forward_applies_loss_weights(hf_stubs):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
target_key="observation.value_target",
|
||||
loss_weight_key="observation.value_loss_weight",
|
||||
num_bins=17,
|
||||
fusion_hidden_dim=32,
|
||||
fusion_num_heads=8,
|
||||
dropout=0.0,
|
||||
)
|
||||
policy = Pistar06Policy(config=cfg)
|
||||
policy.eval()
|
||||
|
||||
base_batch = {
|
||||
OBS_LANGUAGE_TOKENS: torch.randint(0, 100, (4, 12), dtype=torch.long),
|
||||
OBS_LANGUAGE_ATTENTION_MASK: torch.ones(4, 12, dtype=torch.bool),
|
||||
PISTAR06_IMAGES_KEY: torch.rand(4, 1, 3, 32, 32),
|
||||
PISTAR06_IMAGE_MASK_KEY: torch.ones(4, 1, dtype=torch.bool),
|
||||
cfg.target_key: torch.tensor([-1.0, -0.8, -0.4, 0.0], dtype=torch.float32),
|
||||
}
|
||||
loss_weight = torch.tensor([2.0, 1.0, 0.5, 0.5], dtype=torch.float32)
|
||||
|
||||
loss_none_unweighted, _ = policy.forward(base_batch, reduction="none")
|
||||
weighted_batch = {**base_batch, cfg.loss_weight_key: loss_weight}
|
||||
loss_none_weighted, _ = policy.forward(weighted_batch, reduction="none")
|
||||
assert torch.allclose(loss_none_weighted, loss_none_unweighted * loss_weight, atol=1e-6)
|
||||
|
||||
loss_mean_weighted, loss_dict = policy.forward(weighted_batch, reduction="mean")
|
||||
assert torch.isclose(loss_mean_weighted, loss_none_weighted.mean(), atol=1e-6)
|
||||
assert "loss_weight_mean" in loss_dict
|
||||
assert "loss_weight_min" in loss_dict
|
||||
assert "loss_weight_max" in loss_dict
|
||||
|
||||
|
||||
def test_pistar06_prompt_step_with_state_has_no_action_suffix():
|
||||
step = Pistar06PrepareTaskPromptProcessorStep(
|
||||
task_key="task",
|
||||
include_state_in_prompt=True,
|
||||
state_feature=OBS_STATE,
|
||||
max_state_dim=4,
|
||||
state_discretization_bins=8,
|
||||
)
|
||||
transition = {
|
||||
"observation": {OBS_STATE: torch.tensor([[0.0, 0.5], [-0.5, 0.2]], dtype=torch.float32)},
|
||||
"action": None,
|
||||
"next.reward": 0.0,
|
||||
"next.done": False,
|
||||
"next.truncated": False,
|
||||
"info": {},
|
||||
"complementary_data": {"task": ["pick bottle", "place bottle"]},
|
||||
}
|
||||
out = step(transition)
|
||||
prompts = out["complementary_data"]["task"]
|
||||
|
||||
assert len(prompts) == 2
|
||||
assert prompts[0].startswith("Task: pick bottle, State:")
|
||||
assert prompts[1].startswith("Task: place bottle, State:")
|
||||
assert "Action:" not in prompts[0]
|
||||
assert "Action:" not in prompts[1]
|
||||
assert prompts[0].endswith("\nValue: ")
|
||||
assert prompts[1].endswith("\nValue: ")
|
||||
|
||||
|
||||
def test_pistar06_prompt_step_without_state_uses_clean_task_only():
|
||||
step = Pistar06PrepareTaskPromptProcessorStep(
|
||||
task_key="task",
|
||||
include_state_in_prompt=False,
|
||||
state_feature=OBS_STATE,
|
||||
)
|
||||
transition = {
|
||||
"observation": {},
|
||||
"action": None,
|
||||
"next.reward": 0.0,
|
||||
"next.done": False,
|
||||
"next.truncated": False,
|
||||
"info": {},
|
||||
"complementary_data": {"task": ["pick_bottle\nnow"]},
|
||||
}
|
||||
out = step(transition)
|
||||
prompts = out["complementary_data"]["task"]
|
||||
|
||||
assert prompts == ["Task: pick bottle now\nValue: "]
|
||||
|
||||
|
||||
def test_pistar06_save_pretrained_skips_frozen_vision_weights_and_loads_partial(hf_stubs, tmp_path):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
freeze_vision_encoder=True,
|
||||
freeze_language_model=False,
|
||||
)
|
||||
policy = Pistar06Policy(config=cfg)
|
||||
|
||||
with torch.no_grad():
|
||||
policy.model.vision_encoder.image_proj.weight.fill_(7.0)
|
||||
policy.model.language_model.proj.weight.fill_(8.0)
|
||||
policy.model.value_head[0].weight.fill_(9.0)
|
||||
|
||||
policy.save_pretrained(tmp_path)
|
||||
|
||||
saved_state = load_file(tmp_path / "model.safetensors")
|
||||
assert not any(key.startswith("model.vision_encoder.") for key in saved_state)
|
||||
assert any(key.startswith("model.language_model.") for key in saved_state)
|
||||
assert any(key.startswith("model.value_head.") for key in saved_state)
|
||||
|
||||
with open(tmp_path / PISTAR06_SAVE_INFO, encoding="utf-8") as f:
|
||||
save_info = json.load(f)
|
||||
assert save_info["weights_mode"] == "partial"
|
||||
assert "model.vision_encoder." in save_info["excluded_prefixes"]
|
||||
|
||||
loaded = Pistar06Policy.from_pretrained(tmp_path)
|
||||
assert torch.allclose(
|
||||
loaded.model.language_model.proj.weight,
|
||||
torch.full_like(loaded.model.language_model.proj.weight, 8.0),
|
||||
)
|
||||
assert torch.allclose(
|
||||
loaded.model.value_head[0].weight,
|
||||
torch.full_like(loaded.model.value_head[0].weight, 9.0),
|
||||
)
|
||||
assert not torch.allclose(
|
||||
loaded.model.vision_encoder.image_proj.weight,
|
||||
torch.full_like(loaded.model.vision_encoder.image_proj.weight, 7.0),
|
||||
)
|
||||
|
||||
|
||||
def test_pistar06_save_pretrained_only_head_when_both_encoders_frozen_and_strict_load_works(
|
||||
hf_stubs, tmp_path
|
||||
):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
freeze_vision_encoder=True,
|
||||
freeze_language_model=True,
|
||||
)
|
||||
policy = Pistar06Policy(config=cfg)
|
||||
|
||||
with torch.no_grad():
|
||||
policy.model.vision_encoder.image_proj.weight.fill_(5.0)
|
||||
policy.model.language_model.proj.weight.fill_(6.0)
|
||||
policy.model.image_projector[0].weight.fill_(11.0)
|
||||
policy.model.value_head[0].weight.fill_(12.0)
|
||||
|
||||
policy.save_pretrained(tmp_path)
|
||||
|
||||
saved_state = load_file(tmp_path / "model.safetensors")
|
||||
assert not any(key.startswith("model.vision_encoder.") for key in saved_state)
|
||||
assert not any(key.startswith("model.language_model.") for key in saved_state)
|
||||
assert any(key.startswith("model.image_projector.") for key in saved_state)
|
||||
assert any(key.startswith("model.value_head.") for key in saved_state)
|
||||
|
||||
loaded = Pistar06Policy.from_pretrained(tmp_path, strict=True)
|
||||
assert torch.allclose(
|
||||
loaded.model.image_projector[0].weight,
|
||||
torch.full_like(loaded.model.image_projector[0].weight, 11.0),
|
||||
)
|
||||
assert torch.allclose(
|
||||
loaded.model.value_head[0].weight,
|
||||
torch.full_like(loaded.model.value_head[0].weight, 12.0),
|
||||
)
|
||||
assert not torch.allclose(
|
||||
loaded.model.vision_encoder.image_proj.weight,
|
||||
torch.full_like(loaded.model.vision_encoder.image_proj.weight, 5.0),
|
||||
)
|
||||
assert not torch.allclose(
|
||||
loaded.model.language_model.proj.weight,
|
||||
torch.full_like(loaded.model.language_model.proj.weight, 6.0),
|
||||
)
|
||||
|
||||
|
||||
def test_pistar06_save_pretrained_keeps_full_weights_when_encoders_not_frozen(hf_stubs, tmp_path):
|
||||
del hf_stubs
|
||||
cfg = Pistar06Config(
|
||||
device="cpu",
|
||||
camera_features=["observation.images.front"],
|
||||
freeze_vision_encoder=False,
|
||||
freeze_language_model=False,
|
||||
)
|
||||
policy = Pistar06Policy(config=cfg)
|
||||
|
||||
with torch.no_grad():
|
||||
policy.model.vision_encoder.image_proj.weight.fill_(2.0)
|
||||
policy.model.language_model.proj.weight.fill_(3.0)
|
||||
|
||||
policy.save_pretrained(tmp_path)
|
||||
|
||||
saved_state = load_file(tmp_path / "model.safetensors")
|
||||
assert any(key.startswith("model.vision_encoder.") for key in saved_state)
|
||||
assert any(key.startswith("model.language_model.") for key in saved_state)
|
||||
|
||||
with open(tmp_path / PISTAR06_SAVE_INFO, encoding="utf-8") as f:
|
||||
save_info = json.load(f)
|
||||
assert save_info["weights_mode"] == "full"
|
||||
assert save_info["excluded_prefixes"] == []
|
||||
|
||||
loaded = Pistar06Policy.from_pretrained(tmp_path)
|
||||
assert torch.allclose(
|
||||
loaded.model.vision_encoder.image_proj.weight,
|
||||
torch.full_like(loaded.model.vision_encoder.image_proj.weight, 2.0),
|
||||
)
|
||||
assert torch.allclose(
|
||||
loaded.model.language_model.proj.weight,
|
||||
torch.full_like(loaded.model.language_model.proj.weight, 3.0),
|
||||
)
|
||||
在新工单中引用
屏蔽一个用户