-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchunked_policy.py
More file actions
324 lines (271 loc) · 11.4 KB
/
Copy pathchunked_policy.py
File metadata and controls
324 lines (271 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Adapters for upstream policies that predict joint-action chunks.
The public contract is intentionally model-agnostic: a predictor receives one
DM Control observation dictionary and returns one or more 14D joint actions.
``ChunkPredictorAdapter`` handles common NumPy, PyTorch, tuple, and dictionary
outputs, while ``ChunkedPolicyRunner`` turns those chunks into individual
simulator actions at a parameterized execution speed.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Callable
import numpy as np
from constants import PUPPET_GRIPPER_POSITION_NORMALIZE_FN
from ee_sim_env import make_ee_sim_env
from scripted_policy import make_scripted_policy
from sim_env import make_sim_env
from sim_tasks import get_task_spec, normalize_task_name
def _to_numpy(value: Any) -> np.ndarray:
"""Convert common model outputs without importing a framework eagerly."""
detach = getattr(value, "detach", None)
if detach is not None:
value = detach()
cpu = getattr(value, "cpu", None)
if cpu is not None:
value = cpu()
numpy = getattr(value, "numpy", None)
if numpy is not None:
value = numpy()
return np.asarray(value, dtype=np.float64)
def as_action_chunk(output: Any) -> np.ndarray:
"""Normalize an upstream policy output to a finite ``[time, 14]`` array."""
if isinstance(output, dict):
for key in ("actions", "action", "chunk"):
if key in output:
output = output[key]
break
else:
raise ValueError(
"Chunk dictionaries must contain an 'actions', 'action', or 'chunk' key"
)
if isinstance(output, (tuple, list)):
if not output:
raise ValueError("A chunked policy returned an empty sequence")
output = output[0]
actions = _to_numpy(output)
if actions.ndim == 3 and actions.shape[0] == 1:
actions = actions[0]
if actions.ndim == 1 and actions.shape[0] == 14:
actions = actions[None]
if actions.ndim != 2 or actions.shape[1] != 14 or len(actions) == 0:
raise ValueError("Chunked policy output must have shape [time, 14]")
if not np.all(np.isfinite(actions)):
raise ValueError("Chunked policy produced a non-finite action")
return actions
class ChunkPredictorAdapter:
"""Adapt an arbitrary upstream chunk predictor to the public contract.
``predictor`` can be callable or expose ``predict_chunk(observation)``.
Optional adapters make it possible to translate repository-specific
observations and outputs without changing the simulator integration.
"""
def __init__(
self,
predictor: Any,
observation_adapter: Callable[[dict], Any] | None = None,
output_adapter: Callable[[Any], Any] | None = None,
):
predict = getattr(predictor, "predict_chunk", None)
if predict is None and callable(predictor):
predict = predictor
if predict is None:
raise TypeError("predictor must be callable or define predict_chunk()")
self.predictor = predictor
self._predict = predict
self.observation_adapter = observation_adapter or (lambda observation: observation)
self.output_adapter = output_adapter or (lambda output: output)
def reset(self):
reset = getattr(self.predictor, "reset", None)
if reset is not None:
reset()
def advance(self, nominal_steps):
"""Notify predictors that explicitly track nominal demonstration time."""
advance = getattr(self.predictor, "advance", None)
if advance is not None:
advance(float(nominal_steps))
def __call__(self, observation):
model_input = self.observation_adapter(observation)
output = self.output_adapter(self._predict(model_input))
return as_action_chunk(output)
def interpolate_action_chunk(actions, speed=1.0):
"""Resample a ``[time, 14]`` action chunk at a new execution speed."""
actions = as_action_chunk(actions)
if speed <= 0:
raise ValueError("speed must be positive")
sample_times = np.arange(0.0, len(actions), speed)
lower = np.floor(sample_times).astype(int)
upper = np.minimum(lower + 1, len(actions) - 1)
fraction = (sample_times - lower)[:, None]
return actions[lower] + fraction * (actions[upper] - actions[lower])
class ChunkedPolicyRunner:
"""Turn a chunk-predicting policy into one joint action per simulator step.
Speed is expressed in nominal policy timesteps per physics step. It may be
fixed at construction or supplied for every call, allowing a learned speed
policy to change acceleration online without modifying the upstream model.
"""
def __init__(self, predictor, speed=1.0):
self.predictor = (
predictor
if isinstance(predictor, ChunkPredictorAdapter)
else ChunkPredictorAdapter(predictor)
)
self.speed = self._validate_speed(speed)
self._chunk = None
self._chunk_index = 0.0
@staticmethod
def _validate_speed(speed):
speed = float(speed)
if not np.isfinite(speed) or speed <= 0:
raise ValueError("speed must be a finite positive value")
return speed
def reset(self):
self._chunk = None
self._chunk_index = 0.0
reset = getattr(self.predictor, "reset", None)
if reset is not None:
reset()
def begin_decision(self, observation, speed=None):
"""Predict a fresh receding-horizon chunk for one speed decision."""
speed = self.speed if speed is None else self._validate_speed(speed)
self._chunk = self.predictor(observation)
self._chunk_index = 0.0
self._decision_speed = speed
return self._chunk
def action(self, observation, speed=None):
speed = self.speed if speed is None else self._validate_speed(speed)
if self._chunk is None or self._chunk_index >= len(self._chunk):
self.begin_decision(observation, speed=speed)
lower = int(np.floor(self._chunk_index))
upper = min(lower + 1, len(self._chunk) - 1)
fraction = self._chunk_index - lower
action = self._chunk[lower] + fraction * (
self._chunk[upper] - self._chunk[lower]
)
self._chunk_index += speed
self.predictor.advance(speed)
return action.copy()
class TorchChunkPredictor:
"""Pre/post-processing adapter for ACT-compatible PyTorch policies.
The wrapped model must accept normalized ``qpos`` with shape ``[1, 14]`` and
RGB images with shape ``[1, cameras, 3, height, width]``, and return an action
tensor with shape ``[1, chunk, 14]``.
"""
def __init__(
self,
model,
camera_names,
qpos_mean,
qpos_std,
action_mean,
action_std,
device=None,
):
try:
import torch
except ImportError as exc:
raise RuntimeError("Learned policies require: uv sync --extra learned") from exc
self.torch = torch
self.model = model
self.camera_names = tuple(camera_names)
self.device = torch.device(
device or ("cuda" if torch.cuda.is_available() else "cpu")
)
self.qpos_mean = np.asarray(qpos_mean)
self.qpos_std = np.maximum(np.asarray(qpos_std), 1e-6)
self.action_mean = np.asarray(action_mean)
self.action_std = np.asarray(action_std)
for value, name in (
(self.qpos_mean, "qpos_mean"),
(self.qpos_std, "qpos_std"),
(self.action_mean, "action_mean"),
(self.action_std, "action_std"),
):
if value.shape != (14,):
raise ValueError(f"{name} must have shape (14,)")
self.model.to(self.device)
self.model.eval()
def __call__(self, observation):
torch = self.torch
if "images" not in observation:
raise ValueError("The environment must be created with render_images=True")
qpos = (np.asarray(observation["qpos"]) - self.qpos_mean) / self.qpos_std
images = np.stack(
[observation["images"][name] for name in self.camera_names], axis=0
).transpose(0, 3, 1, 2)
qpos_tensor = torch.as_tensor(qpos, dtype=torch.float32, device=self.device)[None]
image_tensor = torch.as_tensor(
images / 255.0, dtype=torch.float32, device=self.device
)[None]
with torch.inference_mode():
output = self.model(qpos_tensor, image_tensor)
chunk = as_action_chunk(output)
return chunk * self.action_std + self.action_mean
@dataclass
class JointDemonstration:
actions: np.ndarray
object_pose: np.ndarray
def collect_scripted_joint_demonstration(task_name, seed=0):
"""Convert the retained EE scripted rollout into joint commands."""
task_name = normalize_task_name(task_name)
spec = get_task_spec(task_name)
env = make_ee_sim_env(task_name, render_images=False, seed=seed)
timestep = env.reset()
episode = [timestep]
policy = make_scripted_policy(task_name)
for _ in range(spec.episode_len):
timestep = env.step(policy(timestep))
episode.append(timestep)
actions = []
for item in episode:
action = item.observation["qpos"].copy()
gripper_ctrl = item.observation["gripper_ctrl"]
action[6] = PUPPET_GRIPPER_POSITION_NORMALIZE_FN(gripper_ctrl[0])
action[13] = PUPPET_GRIPPER_POSITION_NORMALIZE_FN(gripper_ctrl[2])
actions.append(action)
return JointDemonstration(
actions=np.asarray(actions),
object_pose=episode[0].observation["env_state"].copy(),
)
class RecordedChunkPredictor:
"""Deterministic chunk oracle used to validate the learned-policy contract."""
def __init__(self, actions, chunk_size):
self.actions = np.asarray(actions)
self.chunk_size = chunk_size
self.cursor = 0.0
def reset(self):
self.cursor = 0.0
def advance(self, nominal_steps):
self.cursor = min(self.cursor + float(nominal_steps), len(self.actions) - 1)
def __call__(self, observation):
del observation
start = int(np.floor(self.cursor))
end = min(start + self.chunk_size, len(self.actions))
chunk = self.actions[start:end]
if len(chunk) < self.chunk_size:
chunk = np.concatenate(
[chunk, np.repeat(chunk[-1:], self.chunk_size - len(chunk), axis=0)]
)
return chunk
def replay_recorded_chunks(task_name, chunk_size=25, seed=0):
"""Validate chunked-policy replay through the joint-control environment."""
task_name = normalize_task_name(task_name)
demonstration = collect_scripted_joint_demonstration(task_name, seed=seed)
env = make_sim_env(
task_name,
render_images=False,
seed=seed,
object_pose=demonstration.object_pose,
)
timestep = env.reset()
predictor = RecordedChunkPredictor(demonstration.actions, chunk_size)
runner = ChunkedPolicyRunner(predictor)
rewards = []
for _ in range(len(demonstration.actions)):
timestep = env.step(runner.action(timestep.observation))
rewards.append(int(timestep.reward or 0))
return {
"task": task_name,
"success": max(rewards, default=0) == env.task.max_reward,
"max_reward": max(rewards, default=0),
"target_reward": env.task.max_reward,
"chunk_size": chunk_size,
"steps": len(demonstration.actions),
}