Auto-sync: 2026-06-30 09:33:44 (part 4)
Browse files- tests/test_maniskill_policy_rollout.py +31 -1
- tests/test_trainer.py +36 -0
tests/test_maniskill_policy_rollout.py
CHANGED
|
@@ -209,10 +209,11 @@ def test_policy_rollout_requires_numeric_action_values() -> None:
|
|
| 209 |
class _StubModel:
|
| 210 |
"""Minimal stand-in exposing forward_policy / forward_field for selection tests."""
|
| 211 |
|
| 212 |
-
def __init__(self, torch_module, mean, best_offset):
|
| 213 |
self._torch = torch_module
|
| 214 |
self._mean = mean
|
| 215 |
self._best_offset = best_offset
|
|
|
|
| 216 |
|
| 217 |
def forward_policy(self, observation, instruction):
|
| 218 |
del observation, instruction
|
|
@@ -225,6 +226,12 @@ class _StubModel:
|
|
| 225 |
distance = ((action - target) ** 2).reshape(action.shape[0], -1).sum(dim=1)
|
| 226 |
return {"potential": -distance}
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def test_policy_mode_returns_policy_mean() -> None:
|
| 230 |
import torch
|
|
@@ -288,6 +295,29 @@ def test_field_mode_can_prefer_perturbed_candidate() -> None:
|
|
| 288 |
assert index.tolist()[0] != 0
|
| 289 |
|
| 290 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
def test_retrieval_residual_margin_can_abstain_to_policy() -> None:
|
| 292 |
import torch
|
| 293 |
|
|
|
|
| 209 |
class _StubModel:
|
| 210 |
"""Minimal stand-in exposing forward_policy / forward_field for selection tests."""
|
| 211 |
|
| 212 |
+
def __init__(self, torch_module, mean, best_offset, proposals=None):
|
| 213 |
self._torch = torch_module
|
| 214 |
self._mean = mean
|
| 215 |
self._best_offset = best_offset
|
| 216 |
+
self._proposals = proposals
|
| 217 |
|
| 218 |
def forward_policy(self, observation, instruction):
|
| 219 |
del observation, instruction
|
|
|
|
| 226 |
distance = ((action - target) ** 2).reshape(action.shape[0], -1).sum(dim=1)
|
| 227 |
return {"potential": -distance}
|
| 228 |
|
| 229 |
+
def forward_proposals(self, observation, instruction):
|
| 230 |
+
del observation, instruction
|
| 231 |
+
if self._proposals is None:
|
| 232 |
+
raise AssertionError("stub proposals were not configured")
|
| 233 |
+
return self._proposals
|
| 234 |
+
|
| 235 |
|
| 236 |
def test_policy_mode_returns_policy_mean() -> None:
|
| 237 |
import torch
|
|
|
|
| 295 |
assert index.tolist()[0] != 0
|
| 296 |
|
| 297 |
|
| 298 |
+
def test_proposal_lattice_mode_scores_model_generated_proposals() -> None:
|
| 299 |
+
import torch
|
| 300 |
+
|
| 301 |
+
mean = torch.zeros(1, 1, 2)
|
| 302 |
+
proposals = torch.tensor([[[[0.1, 0.0]], [[0.4, 0.4]]]], dtype=torch.float32)
|
| 303 |
+
offset = torch.tensor([[[0.4, 0.4]]], dtype=torch.float32)
|
| 304 |
+
model = _StubModel(torch, mean, best_offset=offset, proposals=proposals)
|
| 305 |
+
|
| 306 |
+
actions, index = _select_action_chunk(
|
| 307 |
+
model,
|
| 308 |
+
observations=torch.zeros(1, 3),
|
| 309 |
+
instructions=["a"],
|
| 310 |
+
torch=torch,
|
| 311 |
+
selection_mode="proposal_lattice",
|
| 312 |
+
num_candidates=1,
|
| 313 |
+
candidate_sigma=0.0,
|
| 314 |
+
selection_seed=0,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
assert torch.allclose(actions, proposals[:, 1])
|
| 318 |
+
assert index.tolist() == [1]
|
| 319 |
+
|
| 320 |
+
|
| 321 |
def test_retrieval_residual_margin_can_abstain_to_policy() -> None:
|
| 322 |
import torch
|
| 323 |
|
tests/test_trainer.py
CHANGED
|
@@ -6,6 +6,7 @@ from types import SimpleNamespace
|
|
| 6 |
from dovla_cil.data.schema import RewardInfo
|
| 7 |
from dovla_cil.generation.pipeline import generate_cil_dataset
|
| 8 |
from dovla_cil.tasks.library import built_in_toy_tasks
|
|
|
|
| 9 |
from dovla_cil.training.trainer import (
|
| 10 |
DoVLATrainer,
|
| 11 |
TrainerConfig,
|
|
@@ -56,6 +57,41 @@ def test_trainer_runs_one_epoch_and_writes_checkpoints(tmp_path: Path) -> None:
|
|
| 56 |
assert "best_policy" in metrics
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def test_field_utility_includes_terminal_success_bonus() -> None:
|
| 60 |
records = [
|
| 61 |
SimpleNamespace(
|
|
|
|
| 6 |
from dovla_cil.data.schema import RewardInfo
|
| 7 |
from dovla_cil.generation.pipeline import generate_cil_dataset
|
| 8 |
from dovla_cil.tasks.library import built_in_toy_tasks
|
| 9 |
+
from dovla_cil.training.losses import InterventionalLossWeights
|
| 10 |
from dovla_cil.training.trainer import (
|
| 11 |
DoVLATrainer,
|
| 12 |
TrainerConfig,
|
|
|
|
| 57 |
assert "best_policy" in metrics
|
| 58 |
|
| 59 |
|
| 60 |
+
def test_trainer_can_supervise_typed_proposal_head(tmp_path: Path) -> None:
|
| 61 |
+
dataset_dir = tmp_path / "cil"
|
| 62 |
+
run_dir = tmp_path / "run"
|
| 63 |
+
generate_cil_dataset(
|
| 64 |
+
backend="toy",
|
| 65 |
+
tasks=built_in_toy_tasks()[:2],
|
| 66 |
+
out_dir=dataset_dir,
|
| 67 |
+
num_states_per_task=2,
|
| 68 |
+
k=4,
|
| 69 |
+
seed=9,
|
| 70 |
+
shard_size=8,
|
| 71 |
+
inline_observations=True,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
result = DoVLATrainer(
|
| 75 |
+
TrainerConfig(
|
| 76 |
+
dataset_dir=dataset_dir,
|
| 77 |
+
output_dir=run_dir,
|
| 78 |
+
epochs=1,
|
| 79 |
+
batch_groups=2,
|
| 80 |
+
records_per_group=4,
|
| 81 |
+
hidden_dim=64,
|
| 82 |
+
learning_rate=1e-3,
|
| 83 |
+
seed=9,
|
| 84 |
+
device="cpu",
|
| 85 |
+
proposal_types=("expert", "near_miss"),
|
| 86 |
+
losses=InterventionalLossWeights(proposal=1.0),
|
| 87 |
+
)
|
| 88 |
+
).train()
|
| 89 |
+
|
| 90 |
+
assert "proposal_loss" in result["history"][0]["val"]
|
| 91 |
+
resolved = read_json(run_dir / "resolved_config.json")
|
| 92 |
+
assert resolved["proposal_types"] == ["expert", "near_miss"]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
def test_field_utility_includes_terminal_success_bonus() -> None:
|
| 96 |
records = [
|
| 97 |
SimpleNamespace(
|