anhtld commited on
Commit
0f2a5d0
·
verified ·
1 Parent(s): 7d89f68

Auto-sync: 2026-06-26 16:14:42

Browse files
tests/test_maniskill_policy_rollout.py CHANGED
@@ -17,6 +17,7 @@ from dovla_cil.eval.maniskill_policy_rollout import (
17
  _adapt_action_dim,
18
  _load_state_archive,
19
  _numeric_action_values,
 
20
  _summarize_rows,
21
  )
22
 
@@ -111,3 +112,85 @@ def test_policy_rollout_requires_numeric_action_values() -> None:
111
  assert "numeric action chunks" in str(exc)
112
  else: # pragma: no cover
113
  raise AssertionError("symbolic action chunks cannot be used for ManiSkill rollout")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  _adapt_action_dim,
18
  _load_state_archive,
19
  _numeric_action_values,
20
+ _select_action_chunk,
21
  _summarize_rows,
22
  )
23
 
 
112
  assert "numeric action chunks" in str(exc)
113
  else: # pragma: no cover
114
  raise AssertionError("symbolic action chunks cannot be used for ManiSkill rollout")
115
+
116
+
117
+ class _StubModel:
118
+ """Minimal stand-in exposing forward_policy / forward_field for selection tests."""
119
+
120
+ def __init__(self, torch_module, mean, best_offset):
121
+ self._torch = torch_module
122
+ self._mean = mean
123
+ self._best_offset = best_offset
124
+
125
+ def forward_policy(self, observation, instruction):
126
+ del observation, instruction
127
+ return self._mean
128
+
129
+ def forward_field(self, observation, instruction, action):
130
+ del observation, instruction
131
+ # Potential is highest for the candidate closest to mean + best_offset.
132
+ target = self._mean + self._best_offset
133
+ distance = ((action - target) ** 2).reshape(action.shape[0], -1).sum(dim=1)
134
+ return {"potential": -distance}
135
+
136
+
137
+ def test_policy_mode_returns_policy_mean() -> None:
138
+ import torch
139
+
140
+ mean = torch.zeros(2, 4, 7)
141
+ model = _StubModel(torch, mean, best_offset=torch.zeros_like(mean))
142
+ actions, index = _select_action_chunk(
143
+ model,
144
+ observations=torch.zeros(2, 3),
145
+ instructions=["a", "b"],
146
+ torch=torch,
147
+ selection_mode="policy",
148
+ num_candidates=8,
149
+ candidate_sigma=0.2,
150
+ selection_seed=0,
151
+ )
152
+ assert torch.allclose(actions, mean)
153
+ assert index.tolist() == [0, 0]
154
+
155
+
156
+ def test_field_mode_selects_highest_potential_candidate() -> None:
157
+ import torch
158
+
159
+ mean = torch.zeros(1, 2, 7)
160
+ # The field prefers the policy mean exactly, so the deterministic mean (index 0) must win.
161
+ model = _StubModel(torch, mean, best_offset=torch.zeros_like(mean))
162
+ actions, index = _select_action_chunk(
163
+ model,
164
+ observations=torch.zeros(1, 3),
165
+ instructions=["a"],
166
+ torch=torch,
167
+ selection_mode="field",
168
+ num_candidates=16,
169
+ candidate_sigma=0.5,
170
+ selection_seed=123,
171
+ )
172
+ # Selected chunk must be the candidate the field scored best (the mean here).
173
+ assert torch.allclose(actions, mean)
174
+ assert index.tolist() == [0]
175
+
176
+
177
+ def test_field_mode_can_prefer_perturbed_candidate() -> None:
178
+ import torch
179
+
180
+ mean = torch.zeros(1, 1, 3)
181
+ offset = torch.full_like(mean, 0.4)
182
+ model = _StubModel(torch, mean, best_offset=offset)
183
+ actions, index = _select_action_chunk(
184
+ model,
185
+ observations=torch.zeros(1, 3),
186
+ instructions=["a"],
187
+ torch=torch,
188
+ selection_mode="field",
189
+ num_candidates=64,
190
+ candidate_sigma=0.5,
191
+ selection_seed=7,
192
+ )
193
+ # With many samples the field should pull the executed action toward mean+offset,
194
+ # i.e. strictly away from the bare mean.
195
+ assert float(((actions - mean) ** 2).sum()) > 0.0
196
+ assert index.tolist()[0] != 0