File size: 11,394 Bytes
23a59ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# curiosity.py
"""
MPC-based curiosity exploration policy for active data collection.

Uses CEM (Cross-Entropy Method) to plan action sequences that maximize a
pluggable per-candidate "curiosity score", computed from dynamics-model
predictions. The scorer is injected by the caller (collect_data.py), so the
CEM plumbing here is scorer-agnostic; the published curiosity-driven
collection uses the motion-normalized u_r_norm predictor (see uncertainty.py).
"""
from typing import Callable, Dict, Any, Optional

import torch

from model import Dynamics
from train_dynamics import sample_one_timestep_packed
from uncertainty import sample_predictions_for_actions


# A scorer takes (predictions_KN, z_prev_K) and returns (K,) scalar scores.
# predictions_KN: (K, N, Sz, Dz) float
# z_prev_K:       (K, Sz, Dz)    float — last observed latent per candidate
#                                        (used for motion normalization)
ScoreFn = Callable[[torch.Tensor, torch.Tensor], torch.Tensor]


@torch.no_grad()
def curiosity_mpc_action(
    dyn: Dynamics,
    *,
    past_packed: torch.Tensor,                     # (1, t, Sz, Dz)
    past_actions: torch.Tensor,                    # (1, t, A) — actions aligned to past frames
    scorer: ScoreFn,
    k_max: int,
    sched: Dict[str, Any],
    act_mask: Optional[torch.Tensor] = None,       # (A,) active action dimensions
    lang_emb: Optional[torch.Tensor] = None,       # (1, lang_dim)
    tau_ctx: float = 0.1,
    n_candidates: int = 256,
    horizon: int = 1,
    n_samples: int = 4,
    n_elite: int = 32,
    n_cem_iters: int = 4,
    cem_init_std: float = 1.0,
    cem_min_std: float = 0.05,
    action_dim: int = 16,
    device: Optional[torch.device] = None,
    use_kv_cache: bool = False,
) -> Dict[str, torch.Tensor]:
    """
    Select an action (or action sequence) that maximizes the given per-candidate
    score via CEM.

    Returns dict with:
      "action":           (A,)           — first action of the chosen plan in [-1, 1]
      "action_sequence":  (H, A)         — full plan (only present for horizon > 1)
      "uncertainty":      scalar         — score of the chosen plan
      "mean_uncertainty": scalar         — mean score across the last CEM iter's candidates
      "max_uncertainty":  scalar         — max score across the last CEM iter's candidates
    ("uncertainty" is the generic name for the injected scorer's signal.)
    """
    if device is None:
        device = past_packed.device

    dtype = next(dyn.parameters()).dtype
    past_packed = past_packed.to(dtype)
    past_actions = past_actions.to(dtype)

    A = action_dim
    if act_mask is not None:
        if act_mask.dim() == 1:
            active_mask = act_mask                     # (A,)
        else:
            active_mask = act_mask[0, 0]               # (A,) from (B, T, A)
    else:
        active_mask = torch.ones(A, device=device)

    if horizon == 1:
        return _cem_single_step(
            dyn=dyn,
            past_packed=past_packed,
            past_actions=past_actions,
            scorer=scorer,
            k_max=k_max, sched=sched,
            act_mask=act_mask, lang_emb=lang_emb, tau_ctx=tau_ctx,
            n_candidates=n_candidates, n_samples=n_samples,
            n_elite=n_elite, n_cem_iters=n_cem_iters,
            init_std=cem_init_std, min_std=cem_min_std,
            action_dim=A, active_mask=active_mask, device=device,
        )
    return _cem_multi_step(
        dyn=dyn,
        past_packed=past_packed,
        past_actions=past_actions,
        scorer=scorer,
        k_max=k_max, sched=sched,
        act_mask=act_mask, lang_emb=lang_emb, tau_ctx=tau_ctx,
        n_candidates=n_candidates, horizon=horizon, n_samples=n_samples,
        n_elite=n_elite, n_cem_iters=n_cem_iters,
        init_std=cem_init_std, min_std=cem_min_std,
        action_dim=A, active_mask=active_mask, device=device,
        use_kv_cache=use_kv_cache,
    )


@torch.no_grad()
def _cem_single_step(
    dyn, past_packed, past_actions, scorer,
    k_max, sched, act_mask, lang_emb, tau_ctx,
    n_candidates, n_samples, n_elite, n_cem_iters,
    init_std, min_std,
    action_dim, active_mask, device,
) -> Dict[str, torch.Tensor]:
    # Gaussian CEM (same scheme as plan_cem.py): sample (mu + std * noise).clamp(-1, 1),
    # refit mean/std on elites, floor std at min_std.
    mu = torch.zeros(action_dim, device=device)
    sigma = torch.full((action_dim,), float(init_std), device=device)

    best_action = None
    best_score = torch.tensor(-float("inf"), device=device)

    # Motion reference: last observed latent, broadcast to K at score time.
    z_last = past_packed[0, -1].float()  # (Sz, Dz)

    for cem_iter in range(n_cem_iters):
        noise = torch.randn(n_candidates, action_dim, device=device)
        candidate_acts = (mu + sigma * noise).clamp(-1, 1)
        candidate_acts = candidate_acts * active_mask.unsqueeze(0)

        past_acts_K = past_actions.expand(n_candidates, -1, -1)                # (K, t, A)
        new_act = candidate_acts.unsqueeze(1)                                   # (K, 1, A)
        full_actions = torch.cat([past_acts_K, new_act], dim=1)                # (K, t+1, A)

        predictions = sample_predictions_for_actions(
            dyn,
            past_packed=past_packed,
            candidate_actions=full_actions,
            k_max=k_max, sched=sched,
            act_mask=act_mask, tau_ctx=tau_ctx,
            lang_emb=lang_emb, n_samples=n_samples,
        )                                                                       # (K, N, Sz, Dz)
        z_prev_K = z_last.unsqueeze(0).expand(n_candidates, -1, -1)            # (K, Sz, Dz)
        scores = scorer(predictions, z_prev_K)                                  # (K,)

        max_idx = scores.argmax()
        if scores[max_idx] > best_score:
            best_score = scores[max_idx]
            best_action = candidate_acts[max_idx]

        _, elite_idx = scores.topk(n_elite)
        elite_acts = candidate_acts[elite_idx]
        mu = elite_acts.mean(dim=0)
        sigma = elite_acts.std(dim=0).clamp_min(float(min_std))

    return {
        "action": best_action,
        "uncertainty": best_score,
        "mean_uncertainty": scores.mean(),
        "max_uncertainty": scores.max(),
    }


@torch.no_grad()
def _cem_multi_step(
    dyn, past_packed, past_actions, scorer,
    k_max, sched, act_mask, lang_emb, tau_ctx,
    n_candidates, horizon, n_samples, n_elite, n_cem_iters,
    init_std, min_std,
    action_dim, active_mask, device,
    use_kv_cache: bool = False,
) -> Dict[str, torch.Tensor]:
    t = past_packed.shape[1]

    # Gaussian CEM (same scheme as plan_cem.py) with per-timestep (mu, std):
    # sample (mu + std * noise).clamp(-1, 1), refit on elites, floor std at min_std.
    mu = torch.zeros(horizon, action_dim, device=device)
    sigma = torch.full((horizon, action_dim), float(init_std), device=device)

    best_action_seq = None
    best_total = torch.tensor(-float("inf"), device=device)

    for cem_iter in range(n_cem_iters):
        noise = torch.randn(n_candidates, horizon, action_dim, device=device)
        cand_seqs = (mu.unsqueeze(0) + sigma.unsqueeze(0) * noise).clamp(-1, 1)
        cand_seqs = cand_seqs * active_mask.unsqueeze(0).unsqueeze(0)

        totals = _rollout_scores(
            dyn=dyn,
            past_packed=past_packed,
            past_actions=past_actions,
            candidate_seqs=cand_seqs,
            scorer=scorer,
            k_max=k_max, sched=sched,
            act_mask=act_mask, lang_emb=lang_emb, tau_ctx=tau_ctx,
            n_samples=n_samples,
            max_ctx=t,
            use_kv_cache=use_kv_cache,
        )                                                                       # (K,)

        max_idx = totals.argmax()
        if totals[max_idx] > best_total:
            best_total = totals[max_idx]
            best_action_seq = cand_seqs[max_idx]

        _, elite_idx = totals.topk(n_elite)
        elite_seqs = cand_seqs[elite_idx]
        mu = elite_seqs.mean(dim=0)
        sigma = elite_seqs.std(dim=0).clamp_min(float(min_std))

    return {
        "action": best_action_seq[0],
        "action_sequence": best_action_seq,
        "uncertainty": best_total,
        "mean_uncertainty": totals.mean(),
        "max_uncertainty": totals.max(),
    }


@torch.no_grad()
def _rollout_scores(
    dyn, past_packed, past_actions, candidate_seqs, scorer,
    k_max, sched, act_mask, lang_emb, tau_ctx, n_samples,
    max_ctx: int = 0,
    use_kv_cache: bool = False,
) -> torch.Tensor:
    """
    Roll out K candidate action sequences through the dynamics model,
    scoring per horizon step and returning an aggregated score per candidate.

    Aggregation: 0.5 * mean_per_step + 0.5 * max_per_step (balances average
    vs. peak score along the plan).
    """
    K, H, A = candidate_seqs.shape
    t = past_packed.shape[1]
    Sz, Dz = past_packed.shape[2], past_packed.shape[3]

    past_K = past_packed.expand(K, -1, -1, -1)                                  # (K, t, Sz, Dz)
    past_acts_K = past_actions.expand(K, -1, -1)                                # (K, t, A)
    lang_K = None if lang_emb is None else lang_emb.expand(K, -1)

    z_history = [past_K[:, i] for i in range(t)]
    act_history = [past_acts_K[:, i] for i in range(t)]

    sum_score = torch.zeros(K, device=past_packed.device)
    max_score = torch.zeros(K, device=past_packed.device)

    for h in range(H):
        act_h = candidate_seqs[:, h]                                            # (K, A)

        if max_ctx > 0 and len(z_history) > max_ctx:
            z_win = z_history[-max_ctx:]
            act_win = act_history[-max_ctx:]
        else:
            z_win = z_history
            act_win = act_history

        ctx_len = len(z_win)
        z_seq = torch.stack(z_win, dim=1)                                       # (K, ctx_len, Sz, Dz)
        acts_seq = torch.stack(act_win + [act_h], dim=1)                        # (K, ctx_len+1, A)

        # Run N samples per candidate in one batch (K*N).
        KN = K * n_samples
        z_KN = z_seq.unsqueeze(1).expand(-1, n_samples, -1, -1, -1).reshape(KN, ctx_len, Sz, Dz)
        acts_KN = acts_seq.unsqueeze(1).expand(-1, n_samples, -1, -1).reshape(KN, ctx_len + 1, A)
        lang_KN = None if lang_K is None else lang_K.unsqueeze(1).expand(-1, n_samples, -1).reshape(KN, -1)

        z_next = sample_one_timestep_packed(
            dyn,
            past_packed=z_KN,
            k_max=k_max, sched=sched,
            actions=acts_KN, act_mask=act_mask, tau_ctx=tau_ctx,
            lang_emb=lang_KN,
            use_kv_cache=use_kv_cache,
        )                                                                       # (K*N, Sz, Dz)
        predictions = z_next.float().reshape(K, n_samples, Sz, Dz)

        z_prev_K = z_win[-1].float()                                            # (K, Sz, Dz)
        score_h = scorer(predictions, z_prev_K)                                 # (K,)
        sum_score = sum_score + score_h
        max_score = torch.max(max_score, score_h)

        # Mean prediction drives AR continuation.
        z_mean = predictions.mean(dim=1).to(past_packed.dtype)                   # (K, Sz, Dz)
        z_history.append(z_mean)
        act_history.append(act_h)

    return 0.5 * (sum_score / max(H, 1)) + 0.5 * max_score