Auto-sync: 2026-06-28 01:28:06
Browse files
dovla_cil/eval/maniskill_policy_rollout.py
CHANGED
|
@@ -60,6 +60,7 @@ def evaluate_maniskill_policy_rollout(
|
|
| 60 |
field_optim_trust_radius: float = 0.5,
|
| 61 |
field_optim_l2_penalty: float = 0.0,
|
| 62 |
retrieval_neighbors: int = 1,
|
|
|
|
| 63 |
lattice_exclude_types: tuple[str, ...] = (),
|
| 64 |
) -> dict[str, Any]:
|
| 65 |
"""Execute a checkpoint policy from restored ManiSkill CIL states.
|
|
@@ -134,6 +135,8 @@ def evaluate_maniskill_policy_rollout(
|
|
| 134 |
raise ValueError("field_optim_l2_penalty must be non-negative")
|
| 135 |
if retrieval_neighbors <= 0:
|
| 136 |
raise ValueError("retrieval_neighbors must be positive")
|
|
|
|
|
|
|
| 137 |
if selection_mode == "policy":
|
| 138 |
num_candidates = 1
|
| 139 |
checkpoint = torch.load(
|
|
@@ -218,6 +221,7 @@ def evaluate_maniskill_policy_rollout(
|
|
| 218 |
field_optim_step_size=field_optim_step_size,
|
| 219 |
field_optim_trust_radius=field_optim_trust_radius,
|
| 220 |
field_optim_l2_penalty=field_optim_l2_penalty,
|
|
|
|
| 221 |
lattice_exclude_types=lattice_exclude_types,
|
| 222 |
)
|
| 223 |
rows.extend(task_rows)
|
|
@@ -243,7 +247,8 @@ def evaluate_maniskill_policy_rollout(
|
|
| 243 |
"selection_mode": selection_mode,
|
| 244 |
"num_candidates": effective_num_candidates,
|
| 245 |
"candidate_sigma": candidate_sigma
|
| 246 |
-
if selection_mode in {"field", "field_optim"}
|
|
|
|
| 247 |
else 0.0,
|
| 248 |
"field_optim_steps": field_optim_steps
|
| 249 |
if selection_mode == "field_optim"
|
|
@@ -260,6 +265,9 @@ def evaluate_maniskill_policy_rollout(
|
|
| 260 |
"retrieval_neighbors": retrieval_neighbors
|
| 261 |
if selection_mode in {"retrieval_lattice", "retrieval_residual"}
|
| 262 |
else 0,
|
|
|
|
|
|
|
|
|
|
| 263 |
"lattice_exclude_types": list(lattice_exclude_types),
|
| 264 |
"policy_rollout_success_rate": _mean([row["success"] for row in rows]),
|
| 265 |
"policy_rollout_progress": _mean([row["progress"] for row in rows]),
|
|
@@ -508,6 +516,7 @@ def _evaluate_task_cases(
|
|
| 508 |
field_optim_step_size: float = 0.05,
|
| 509 |
field_optim_trust_radius: float = 0.5,
|
| 510 |
field_optim_l2_penalty: float = 0.0,
|
|
|
|
| 511 |
lattice_exclude_types: tuple[str, ...] = (),
|
| 512 |
) -> list[dict[str, Any]]:
|
| 513 |
rows: list[dict[str, Any]] = []
|
|
@@ -570,6 +579,7 @@ def _evaluate_task_cases(
|
|
| 570 |
field_optim_step_size=field_optim_step_size,
|
| 571 |
field_optim_trust_radius=field_optim_trust_radius,
|
| 572 |
field_optim_l2_penalty=field_optim_l2_penalty,
|
|
|
|
| 573 |
action_low=action_low,
|
| 574 |
action_high=action_high,
|
| 575 |
action_candidates=(
|
|
@@ -640,7 +650,12 @@ def _evaluate_task_cases(
|
|
| 640 |
selection_mode=selection_mode,
|
| 641 |
),
|
| 642 |
"selected_candidate_index": int(candidate_index[index]),
|
| 643 |
-
"lattice_candidate_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
"candidate_source_group_id": case.candidate_source_group_id,
|
| 645 |
}
|
| 646 |
)
|
|
@@ -663,6 +678,7 @@ def _select_action_chunk(
|
|
| 663 |
field_optim_step_size: float = 0.05,
|
| 664 |
field_optim_trust_radius: float = 0.5,
|
| 665 |
field_optim_l2_penalty: float = 0.0,
|
|
|
|
| 666 |
action_low: Any | None = None,
|
| 667 |
action_high: Any | None = None,
|
| 668 |
action_candidates: Any | None = None,
|
|
@@ -708,6 +724,10 @@ def _select_action_chunk(
|
|
| 708 |
action_low=action_low,
|
| 709 |
action_high=action_high,
|
| 710 |
candidate_mask=candidate_mask,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
)
|
| 712 |
|
| 713 |
if selection_mode == "policy" or num_candidates <= 1:
|
|
@@ -988,13 +1008,41 @@ def _select_residual_lattice_action_chunk(
|
|
| 988 |
action_low: Any | None,
|
| 989 |
action_high: Any | None,
|
| 990 |
candidate_mask: Any | None,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 991 |
) -> tuple[Any, np.ndarray]:
|
| 992 |
if action_residuals.ndim != 4:
|
| 993 |
raise ValueError("action_residuals must have shape [B,K,H,D]")
|
| 994 |
-
candidates = policy_mean.unsqueeze(1) + action_residuals.to(
|
| 995 |
device=policy_mean.device,
|
| 996 |
dtype=policy_mean.dtype,
|
| 997 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 998 |
return _select_lattice_action_chunk(
|
| 999 |
model,
|
| 1000 |
observations,
|
|
@@ -1015,6 +1063,19 @@ def _select_residual_lattice_action_chunk(
|
|
| 1015 |
)
|
| 1016 |
|
| 1017 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1018 |
def _lattice_candidate_mask(
|
| 1019 |
batch: list[_RolloutCase],
|
| 1020 |
*,
|
|
@@ -1047,7 +1108,7 @@ def _selected_candidate_type(
|
|
| 1047 |
if selection_mode == "retrieval_residual":
|
| 1048 |
if 0 <= selected_index < len(case.candidate_types):
|
| 1049 |
return f"retrieval_residual_{case.candidate_types[selected_index]}"
|
| 1050 |
-
return "
|
| 1051 |
if 0 <= selected_index < len(case.candidate_types):
|
| 1052 |
return f"lattice_{case.candidate_types[selected_index]}"
|
| 1053 |
return "lattice_unknown"
|
|
|
|
| 60 |
field_optim_trust_radius: float = 0.5,
|
| 61 |
field_optim_l2_penalty: float = 0.0,
|
| 62 |
retrieval_neighbors: int = 1,
|
| 63 |
+
retrieval_residual_scale: float = 1.0,
|
| 64 |
lattice_exclude_types: tuple[str, ...] = (),
|
| 65 |
) -> dict[str, Any]:
|
| 66 |
"""Execute a checkpoint policy from restored ManiSkill CIL states.
|
|
|
|
| 135 |
raise ValueError("field_optim_l2_penalty must be non-negative")
|
| 136 |
if retrieval_neighbors <= 0:
|
| 137 |
raise ValueError("retrieval_neighbors must be positive")
|
| 138 |
+
if retrieval_residual_scale < 0:
|
| 139 |
+
raise ValueError("retrieval_residual_scale must be non-negative")
|
| 140 |
if selection_mode == "policy":
|
| 141 |
num_candidates = 1
|
| 142 |
checkpoint = torch.load(
|
|
|
|
| 221 |
field_optim_step_size=field_optim_step_size,
|
| 222 |
field_optim_trust_radius=field_optim_trust_radius,
|
| 223 |
field_optim_l2_penalty=field_optim_l2_penalty,
|
| 224 |
+
retrieval_residual_scale=retrieval_residual_scale,
|
| 225 |
lattice_exclude_types=lattice_exclude_types,
|
| 226 |
)
|
| 227 |
rows.extend(task_rows)
|
|
|
|
| 247 |
"selection_mode": selection_mode,
|
| 248 |
"num_candidates": effective_num_candidates,
|
| 249 |
"candidate_sigma": candidate_sigma
|
| 250 |
+
if selection_mode in {"field", "field_optim", "retrieval_residual"}
|
| 251 |
+
and num_candidates > 1
|
| 252 |
else 0.0,
|
| 253 |
"field_optim_steps": field_optim_steps
|
| 254 |
if selection_mode == "field_optim"
|
|
|
|
| 265 |
"retrieval_neighbors": retrieval_neighbors
|
| 266 |
if selection_mode in {"retrieval_lattice", "retrieval_residual"}
|
| 267 |
else 0,
|
| 268 |
+
"retrieval_residual_scale": retrieval_residual_scale
|
| 269 |
+
if selection_mode == "retrieval_residual"
|
| 270 |
+
else 0.0,
|
| 271 |
"lattice_exclude_types": list(lattice_exclude_types),
|
| 272 |
"policy_rollout_success_rate": _mean([row["success"] for row in rows]),
|
| 273 |
"policy_rollout_progress": _mean([row["progress"] for row in rows]),
|
|
|
|
| 516 |
field_optim_step_size: float = 0.05,
|
| 517 |
field_optim_trust_radius: float = 0.5,
|
| 518 |
field_optim_l2_penalty: float = 0.0,
|
| 519 |
+
retrieval_residual_scale: float = 1.0,
|
| 520 |
lattice_exclude_types: tuple[str, ...] = (),
|
| 521 |
) -> list[dict[str, Any]]:
|
| 522 |
rows: list[dict[str, Any]] = []
|
|
|
|
| 579 |
field_optim_step_size=field_optim_step_size,
|
| 580 |
field_optim_trust_radius=field_optim_trust_radius,
|
| 581 |
field_optim_l2_penalty=field_optim_l2_penalty,
|
| 582 |
+
retrieval_residual_scale=retrieval_residual_scale,
|
| 583 |
action_low=action_low,
|
| 584 |
action_high=action_high,
|
| 585 |
action_candidates=(
|
|
|
|
| 650 |
selection_mode=selection_mode,
|
| 651 |
),
|
| 652 |
"selected_candidate_index": int(candidate_index[index]),
|
| 653 |
+
"lattice_candidate_count": _effective_lattice_candidate_count(
|
| 654 |
+
case,
|
| 655 |
+
selection_mode=selection_mode,
|
| 656 |
+
num_candidates=num_candidates,
|
| 657 |
+
candidate_sigma=candidate_sigma,
|
| 658 |
+
),
|
| 659 |
"candidate_source_group_id": case.candidate_source_group_id,
|
| 660 |
}
|
| 661 |
)
|
|
|
|
| 678 |
field_optim_step_size: float = 0.05,
|
| 679 |
field_optim_trust_radius: float = 0.5,
|
| 680 |
field_optim_l2_penalty: float = 0.0,
|
| 681 |
+
retrieval_residual_scale: float = 1.0,
|
| 682 |
action_low: Any | None = None,
|
| 683 |
action_high: Any | None = None,
|
| 684 |
action_candidates: Any | None = None,
|
|
|
|
| 724 |
action_low=action_low,
|
| 725 |
action_high=action_high,
|
| 726 |
candidate_mask=candidate_mask,
|
| 727 |
+
residual_scale=retrieval_residual_scale,
|
| 728 |
+
num_gaussian_candidates=num_candidates,
|
| 729 |
+
candidate_sigma=candidate_sigma,
|
| 730 |
+
selection_seed=selection_seed,
|
| 731 |
)
|
| 732 |
|
| 733 |
if selection_mode == "policy" or num_candidates <= 1:
|
|
|
|
| 1008 |
action_low: Any | None,
|
| 1009 |
action_high: Any | None,
|
| 1010 |
candidate_mask: Any | None,
|
| 1011 |
+
residual_scale: float,
|
| 1012 |
+
num_gaussian_candidates: int,
|
| 1013 |
+
candidate_sigma: float,
|
| 1014 |
+
selection_seed: int,
|
| 1015 |
) -> tuple[Any, np.ndarray]:
|
| 1016 |
if action_residuals.ndim != 4:
|
| 1017 |
raise ValueError("action_residuals must have shape [B,K,H,D]")
|
| 1018 |
+
candidates = policy_mean.unsqueeze(1) + float(residual_scale) * action_residuals.to(
|
| 1019 |
device=policy_mean.device,
|
| 1020 |
dtype=policy_mean.dtype,
|
| 1021 |
)
|
| 1022 |
+
if num_gaussian_candidates > 1 and candidate_sigma > 0:
|
| 1023 |
+
generator = torch.Generator(device=policy_mean.device)
|
| 1024 |
+
generator.manual_seed(int(selection_seed))
|
| 1025 |
+
noise = torch.randn(
|
| 1026 |
+
(
|
| 1027 |
+
policy_mean.shape[0],
|
| 1028 |
+
num_gaussian_candidates - 1,
|
| 1029 |
+
policy_mean.shape[1],
|
| 1030 |
+
policy_mean.shape[2],
|
| 1031 |
+
),
|
| 1032 |
+
generator=generator,
|
| 1033 |
+
device=policy_mean.device,
|
| 1034 |
+
dtype=policy_mean.dtype,
|
| 1035 |
+
)
|
| 1036 |
+
gaussian = policy_mean.unsqueeze(1) + float(candidate_sigma) * noise
|
| 1037 |
+
candidates = torch.cat([candidates, gaussian], dim=1)
|
| 1038 |
+
if candidate_mask is not None:
|
| 1039 |
+
extra_mask = torch.ones(
|
| 1040 |
+
candidate_mask.shape[0],
|
| 1041 |
+
num_gaussian_candidates - 1,
|
| 1042 |
+
dtype=candidate_mask.dtype,
|
| 1043 |
+
device=candidate_mask.device,
|
| 1044 |
+
)
|
| 1045 |
+
candidate_mask = torch.cat([candidate_mask, extra_mask], dim=1)
|
| 1046 |
return _select_lattice_action_chunk(
|
| 1047 |
model,
|
| 1048 |
observations,
|
|
|
|
| 1063 |
)
|
| 1064 |
|
| 1065 |
|
| 1066 |
+
def _effective_lattice_candidate_count(
|
| 1067 |
+
case: _RolloutCase,
|
| 1068 |
+
*,
|
| 1069 |
+
selection_mode: str,
|
| 1070 |
+
num_candidates: int,
|
| 1071 |
+
candidate_sigma: float,
|
| 1072 |
+
) -> int:
|
| 1073 |
+
count = len(case.candidate_action_values)
|
| 1074 |
+
if selection_mode == "retrieval_residual" and num_candidates > 1 and candidate_sigma > 0:
|
| 1075 |
+
count += num_candidates - 1
|
| 1076 |
+
return count
|
| 1077 |
+
|
| 1078 |
+
|
| 1079 |
def _lattice_candidate_mask(
|
| 1080 |
batch: list[_RolloutCase],
|
| 1081 |
*,
|
|
|
|
| 1108 |
if selection_mode == "retrieval_residual":
|
| 1109 |
if 0 <= selected_index < len(case.candidate_types):
|
| 1110 |
return f"retrieval_residual_{case.candidate_types[selected_index]}"
|
| 1111 |
+
return "retrieval_residual_gaussian"
|
| 1112 |
if 0 <= selected_index < len(case.candidate_types):
|
| 1113 |
return f"lattice_{case.candidate_types[selected_index]}"
|
| 1114 |
return "lattice_unknown"
|