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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import os
import ot
import numba
import random
import numpy as np
from tqdm import tqdm
from decord import VideoReader

import torch
import torch.nn as nn
import torchvision.models as models
from torchvision.transforms import Normalize

from diffusion_policy.preference_labeling.alignment_utils import bordered_identity_like, mask_optimal_transport_plan, dtw, dtw_path
from r3m import load_r3m
from liv import load_liv
from vip import load_vip

""" ========================================== All encoders ========================================== """
class ResNet(nn.Module):
    def __init__(self):
        super().__init__()
        model = models.resnet18(weights="ResNet18_Weights.IMAGENET1K_V1")
        self.model = model.eval()
        self.normalizer = Normalize(mean=torch.FloatTensor([0.485, 0.456, 0.406]),
                                    std=torch.FloatTensor([0.229, 0.224, 0.225]))

    def forward(self, obs):
        obs = obs[:, -3:] / 255.0
        h = self.normalizer(obs)
        for m in list(self.model.children())[:-1]:
            h = m(h)
        out = h.view(obs.shape[0], -1)
        return out


class R3M(nn.Module):
    def __init__(self):
        super().__init__()
        self.model_type = "resnet18"
        model = load_r3m(self.model_type).module  # unpack parallel model
        self.model = model.eval()
        self.normalizer = Normalize(mean=torch.FloatTensor([0.485, 0.456, 0.406]),
                                    std=torch.FloatTensor([0.229, 0.224, 0.225]))

    def forward(self, obs):
        h = self.normalizer(obs)
        for m in list(self.model.convnet.children())[:-1]:
            h = m(h)
        out = h.view(obs.shape[0], -1)
        return out


class LIV(nn.Module):
    def __init__(self):
        super().__init__()
        self.model_type = "resnet50"
        model = load_liv(self.model_type).module  # unpack parallel model
        self.model = model.eval()

    def forward(self, obs):
        out = self.model(input=obs, modality="vision")
        return out


class VIP(nn.Module):
    def __init__(self):
        super().__init__()
        self.model_type = "resnet50"
        model = load_vip(self.model_type).module  # unpack parallel model
        self.model = model.eval()

    def forward(self, obs):
        out = self.model(obs)
        return out

""" ================================================================================================== """


def encode_video_with_batch(video_np, model, device, batch_size=128):
    v = torch.from_numpy(video_np).permute(0, 3, 1, 2).contiguous().float()
    feats = []
    with torch.no_grad():
        for start in range(0, len(v), batch_size):
            batch = v[start:start + batch_size].to(device)
            feat = model(batch).cpu()
            feats.append(feat)
            del batch
    if device.type == "cuda":
        torch.cuda.empty_cache()
    return torch.cat(feats, dim=0).numpy()


def load_or_compute_feats(cache_path, video_paths, encoder, device, drop_last=False, use_cached=True, save_cached=True):
    """Load cached features or run ResNet on every video then cache to disk."""
    if use_cached and os.path.exists(cache_path):
        data = np.load(cache_path, allow_pickle=True)
        return list(data["feats"])
    feats = []
    for p in tqdm(video_paths, desc=f"encoding -> {os.path.basename(cache_path)}"):
        vr = VideoReader(p)
        video = vr.get_batch(np.arange(0, len(vr))).asnumpy()
        if drop_last:
            video = video[:-1]   # Some rollout data contains the last state caused by last action, but we don't use
        feats.append(encode_video_with_batch(video, encoder, device))
        del video, vr
    if save_cached:
        np.savez(cache_path, feats=np.array(feats, dtype=object))
    return feats


def get_context_observations(observations, context_num=3):
    """Stack of context_num shifted copies of `observations`. Returns (C, L, D)."""
    observations = np.asarray(observations)
    L = len(observations)
    idx0 = np.arange(L)
    out = [observations[idx0]]
    for i in range(1, context_num):
        idx_i = (idx0 + i).clip(0, L - 1)
        out.append(observations[idx_i])
    return np.stack(out, axis=0)


def get_averaged_cost_matrix(feat_a, feat_b):
    """feat_a: (C, L_a, D), feat_b: (C, L_b, D). Returns (L_a, L_b) cosine cost."""
    assert feat_a.shape[0] == 3 and feat_b.shape[0] == 3
    a = feat_a / (np.linalg.norm(feat_a, axis=-1, keepdims=True) + 1e-8)
    b = feat_b / (np.linalg.norm(feat_b, axis=-1, keepdims=True) + 1e-8)
    sim = np.matmul(a, b.transpose(0, 2, 1))   # (C, L_a, L_b)
    return np.clip(1.0 - sim.mean(axis=0), 0.0, 2.0)


@numba.njit(cache=True, fastmath=True)
def _orca_dp(prob_matrix):
    """JIT-compiled DP recurrence for ORCA ordered-coverage matrix.

    Matches the original recurrence exactly:
        covered[i, j] = max(covered[i-1, j], covered[i, j-1] * prob[i, j])
    with special-case handling of the final column to force occupancy of the
    last subgoal (paper Eq. 6).
    """
    T, S = prob_matrix.shape
    covered = np.zeros_like(prob_matrix)
    covered[0, 0] = prob_matrix[0, 0]
    # Init col 0
    for i in range(1, T):
        a = covered[i - 1, 0]
        b = prob_matrix[i, 0]
        covered[i, 0] = a if a > b else b
    # Init row 0
    for j in range(1, S):
        covered[0, j] = covered[0, j - 1] * prob_matrix[0, j]
    # Main DP (skip last column)
    for i in range(1, T):
        for j in range(1, S - 1):
            a = covered[i - 1, j]
            b = covered[i, j - 1] * prob_matrix[i, j]
            covered[i, j] = a if a > b else b
    # Final column: force occupancy at last subgoal
    for i in range(T):
        covered[i, S - 1] = covered[i, S - 2] * prob_matrix[i, S - 1]
    return covered


""" ==================================== All pseudo-reward supported ==================================== """
def compute_orca_reward(cost_matrix, tau=1.0):
    prob = np.exp(-cost_matrix / tau)
    covered = _orca_dp(prob.astype(np.float64))
    return covered[:, -1], covered


def compute_ot_reward(cost_matrix, ent_reg=.01) -> np.ndarray:
    """
    Entropy regularized optimal transport reward
    """

    # Calculate the OT plan between the reference sequence and the observed sequence
    obs_weight = np.ones(cost_matrix.shape[0]) / cost_matrix.shape[0]
    ref_weight = np.ones(cost_matrix.shape[1]) / cost_matrix.shape[1]

    if ent_reg == 0:
        T = ot.emd(obs_weight, ref_weight, cost_matrix)  # size: (train_freq, ref_seq_len)
    else:
        T = ot.sinkhorn(obs_weight, ref_weight, cost_matrix, reg=ent_reg, log=False)  # size: (train_freq, ref_seq_len)

    # Normalize the path so that each row sums to 1
    normalized_T = T / np.expand_dims(np.sum(T, axis=1), 1)

    # Calculate the OT cost for each timestep
    #   sum by row of (cost matrix * OT plan)
    ot_cost = np.sum(cost_matrix * normalized_T, axis=1)  # size: (train_freq,)

    final_reward = -ot_cost
    return final_reward, {"assignment": normalized_T}


def compute_temporal_ot_reward(cost_matrix, mask_k: int = 10, niter: int = 100, ent_reg: float = 0.01):
    """
    TemporalOT reward, as implemented in (Fu et al., Robot Policy Learning with Temporal Optimal Transport Reward, NeurIPS 2024)
    Code from https://github.com/fuyw/TemporalOT
    """

    # optimal weights
    mask = bordered_identity_like(cost_matrix.shape[0], cost_matrix.shape[1], k=mask_k)
    transport_plan = mask_optimal_transport_plan(cost_matrix, mask, niter, ent_reg)

    ot_cost = np.sum(transport_plan * cost_matrix, axis=1)
    ot_reward = -ot_cost
    return ot_reward, {"assignment": transport_plan}


def compute_dtw_reward(cost_matrix):
    """
    Compute the reward with an assignment matrix that uses dynamic time warping
    """
    _, accumulated_cost_matrix = dtw(cost_matrix)
    path = dtw_path(accumulated_cost_matrix)

    # Normalize the path so that each row sums to 1
    normalized_path = path / np.expand_dims(np.sum(path, axis=1), 1)
    dtw_cost = np.sum(cost_matrix * normalized_path, axis=1)  # size: (train_freq,)
    final_reward = -dtw_cost

    return final_reward, {"assignment": normalized_path}


def compute_tracking_with_threshold_reward(cost_matrix, threshold=0.9):
    """
    Compute the reward by estimating progress along the trajectory using a threshold for each subgoal.
    If the soft probability of occupying the current subgoal is above the threshold, we move to the next subgoal.

    The final reward is the percent of subgoals completed
    """
    prob_matrix = np.exp(-cost_matrix)
    reward_vector = np.zeros(prob_matrix.shape[0])
    subgoal_tracking_matrix = np.zeros_like(
        prob_matrix)  # To use the visualization of assignment matrix from other approaches

    curr_subgoal = 0
    total_subgoals = prob_matrix.shape[1]

    for i in range(prob_matrix.shape[0]):
        # 2 components for the reward
        #   - current subgoal reward
        #   - progress reward
        # We then normalize the reward by the total number of subgoals to keep the reward in the range [0, 1]
        reward_vector[i] = (prob_matrix[i, curr_subgoal] + curr_subgoal) / total_subgoals
        subgoal_tracking_matrix[i][curr_subgoal] = 1

        if prob_matrix[i, curr_subgoal] > threshold:
            # Move to the next subgoal until reaching the last subgoal
            curr_subgoal = min(curr_subgoal + 1, prob_matrix.shape[1] - 1)

        # print(f"timestep: {i}; subgoal: {curr_subgoal}/{total_subgoals-1}; reward: {reward_vector[i]}")

    return reward_vector, {"assignment": subgoal_tracking_matrix}


def compute_final_frame_reward(cost_matrix):
    """
    Reward is the distance from the final reference state, ignoring the sequence
    i.e., R = -d(obs, ref[-1])
    """
    assignment = np.zeros_like(cost_matrix)
    assignment[:, -1] = 1

    final_reward = - np.sum(cost_matrix * assignment, axis=1)  # size: (train_freq,)

    return final_reward, assignment


def compute_even_distribution_reward(cost_matrix, mask_k: int = 10):
    """
    Compute reward based on an assignment matrix that evenly distributes the frames from obs to ref, with an additional border on each side of size mask_k
    i.e., the first N frames from obs will be distributed to the first frame of ref, and so on, where N is len(obs) // len(ref)

    if mask_k == 0 and cost_matrix is square, then this is the identity
    """
    # Calculate the cost matrix between the reference sequence and the observed sequence
    assignment = bordered_identity_like(cost_matrix.shape[0], cost_matrix.shape[1], mask_k)
    normalized_assignment = assignment / np.expand_dims(np.sum(assignment, axis=1), 1)

    even_distributed_cost = np.sum(normalized_assignment * cost_matrix, axis=1)

    final_reward = - even_distributed_cost

    return final_reward, {"assignment": normalized_assignment}

""" ================================================================================================== """


def load_or_create_indices(
    path: str,
    num_queries: int,
    num_episodes_1: int,
    num_episodes_2: int,
    episode_ends_1: np.ndarray,
    episode_ends_2: np.ndarray,
    sequence_length: int,
    seed: int,
    use_cached=True,
    save_cached=True,
) -> np.ndarray:
    #Load pair indices from *path* if it already exists; otherwise sample them randomly, save them, and return.

    # Index layout — array shape ``(num_queries, 4)``:
    # [ep_idx_1, timestep_idx_1, ep_idx_2, timestep_idx_2]

    if use_cached and os.path.isfile(path):
        print(f"=====================> Loading pair indices from {path}")
        data = np.load(path)
        indices = data["indices"]
        assert indices.shape == (num_queries, 4), (
            f"Loaded indices shape {indices.shape} does not match "
        )
        return indices

    # Generate fresh indices
    print(f"=====================> Generating new pair indices → {path}")
    rng = random.Random(seed)

    # Pre-compute per-episode lengths from cumulative episode_ends.
    def episode_length(episode_ends: np.ndarray, idx: int) -> int:
        start = episode_ends[idx - 1] if idx > 0 else 0
        return int(episode_ends[idx]) - int(start)

    indices = np.zeros((num_queries, 4), dtype=np.int64)
    for i in range(num_queries):
        ep_idx_1 = rng.randrange(num_episodes_1)
        ep_idx_2 = rng.randrange(num_episodes_2)

        len_ep1 = episode_length(episode_ends_1, ep_idx_1)
        len_ep2 = episode_length(episode_ends_2, ep_idx_2)

        # Random start index; fall back to 0 when the episode is shorter than
        # the requested sequence length (the caller will pad in that case).
        max_start_1 = max(len_ep1 - sequence_length, 0)
        max_start_2 = max(len_ep2 - sequence_length, 0)

        ts_idx_1 = rng.randint(0, max_start_1)  # inclusive on both ends
        ts_idx_2 = rng.randint(0, max_start_2)

        indices[i] = [ep_idx_1, ts_idx_1, ep_idx_2, ts_idx_2]

    if save_cached:
        os.makedirs(os.path.dirname(path), exist_ok=True)
        np.savez(path, indices=indices)
        print(f"=====================> PbrlLowdimDataset: Saved pair indices to {path}")
    return indices


def precompute_pair_rewards(traj_feats, expert_ctx_list, context_num=3, min_cost=0.05, max_cost=0.30):
    """For every trajectory, compute per-expert per-step rewards."""
    n_exp = len(expert_ctx_list)
    reward_all = []

    for traj_feat in tqdm(traj_feats, desc="precompute rewards"):
        traj_ctx = get_context_observations(traj_feat, context_num=context_num)
        L = traj_feat.shape[0]
        reward_relative_to_expert = np.zeros((n_exp, L), dtype=np.float32)

        for e_idx, exp_ctx in enumerate(expert_ctx_list):
            # cost shape: (L_traj, L_exp)
            cost = get_averaged_cost_matrix(traj_ctx, exp_ctx)

            # Adaptive \tau for each expert trajectory
            # 1. Find the closest distance the learner got to each expert state
            min_costs = np.min(cost, axis=0, keepdims=True)  # Shape: (1, L_exp)

            # 2. Scale it up to create a soft window, but clamp it to safe boundaries
            # Floor (0.05): Prevents division by zero and keeps tight bottlenecks strict
            # Ceiling (0.30): Prevents inflating probabilities for missed states
            tau_local = np.clip(min_costs * 2.0, min_cost, max_cost)

            # Convert cumulative coverage to per-step marginal
            orca_cov, _ = compute_orca_reward(cost, tau=tau_local)
            trajectory_rew = np.concatenate([[orca_cov[0]], np.diff(orca_cov)])
            reward_relative_to_expert[e_idx] = trajectory_rew.astype(np.float32)

        reward_all.append(reward_relative_to_expert)

    return reward_all


def extract_segment_pseudo_reward(rew, start, seq_len):
    """
    Extracts a segment of length seq_len starting at 'start'. Pads short trajectories with 0.

    Args:
        rew: (num_experts, L) array of rewards.
        start: Start index for the segment.
        seq_len: Target sequence length.

    Returns:
        segment_sum: (num_experts,) Sum of the rewards in the segment.
        segment_rewards: (num_experts, seq_len) The individual padded rewards.
    """
    # 1. Extract whatever is available from the start index up to the sequence limit
    segment = rew[:, start: start + seq_len]

    # 2. Calculate how much padding is needed to reach seq_len
    actual_len = segment.shape[-1]
    pad_amount = seq_len - actual_len

    # 3. Apply zero-padding to the time dimension if it's too short
    if pad_amount > 0:
        # pad_width format: ((dim0_before, dim0_after), (dim1_before, dim1_after))
        segment_rewards = np.pad(segment, ((0, 0), (0, pad_amount)), mode='constant', constant_values=0.0)
    else:
        segment_rewards = segment

    # 4. Calculate the sum (summing the zero-padded array is identical to summing the original slice)
    segment_sum = segment_rewards.sum(axis=-1)

    return segment_sum, segment_rewards