TRACE-VLA code snapshot 2026-11-22 11:47 +07: 456 canonical files + SHA256 manifest + sync report (part 2)
ae1ed83 verified Download code/trace_validation/scripts/dpre_retrieval.py from phammminhhieu/trace-vla-code: direct link, hf CLI and curl.
- Browser
- Download file 4.87 kB
-
https://huggingface.co/phammminhhieu/trace-vla-code/resolve/main/code/trace_validation/scripts/dpre_retrieval.py
- Command line
-
hf download hf://phammminhhieu/trace-vla-code/code/trace_validation/scripts/dpre_retrieval.py
-
curl -L -o dpre_retrieval.py https://huggingface.co/phammminhhieu/trace-vla-code/resolve/main/code/trace_validation/scripts/dpre_retrieval.py
4.87 kB
| #!/usr/bin/env python3 | |
| """Phase A - D-pre: zero-training representation retrieval. | |
| Feeds the D00-D06 comparisons of TRACE-VLA v1.1 section 17.2 at the | |
| feature level (no module is trained). From the frozen-feature cache | |
| (21 tasks x 3 episodes, main camera), each episode is sampled to 64 | |
| frames and cut into 15 windows of 8 frames (stride 4), exactly as | |
| DELTA does. A query is one window of one episode; the gallery is all | |
| windows of all OTHER episodes. Metric: Recall@1 / Recall@5 of the | |
| top matches belonging to the same task (different episode). | |
| Representations (per window, from patch-mean frame features f_i): | |
| - raw (D05 proxy): mean of f over the window. | |
| - start_end (D02): [f_first; f_last]. | |
| - endpoint_res (D03): f_last - f_first. | |
| - start_end_res (D04): [f_first; f_last; f_last - f_first]. | |
| - ordered_res (D06 proxy): [f_first; r1; r2; r3; r4; f_last] where | |
| r_k is the mean signed local residual (f_{i+1} - f_i) over the k-th | |
| quarter of the window - coarse temporal order is retained. | |
| Each block is L2-normalized before concatenation. | |
| Also: episode-level static retrieval (D00 proxy) with one mean | |
| vector per episode. | |
| """ | |
| import json | |
| from pathlib import Path | |
| import torch | |
| EXP = Path.home() / "sera_v4_experiments" | |
| OUT = EXP / "trace_validation" / "results" / "dpre_retrieval.json" | |
| def norm(x): | |
| return torch.nn.functional.normalize(x, dim=-1) | |
| def window_vectors(f): | |
| """f [64, 768] patch-mean features of the sampled demo frames.""" | |
| reps = {} | |
| for w in range(15): | |
| s = w * 4 | |
| win = f[s:s + 8] | |
| first, last = win[0], win[-1] | |
| local = win[1:] - win[:-1] # [7, 768] | |
| quarters = [local[i * 2:(i + 1) * 2].mean(0) if i < 3 | |
| else local[6:].mean(0) for i in range(4)] | |
| reps.setdefault("raw", []).append(norm(win.mean(0, keepdim=True))[0]) | |
| reps.setdefault("start_end", []).append( | |
| norm(torch.cat([norm(first[None])[0], norm(last[None])[0]])[None])[0]) | |
| reps.setdefault("endpoint_res", []).append(norm((last - first)[None])[0]) | |
| reps.setdefault("start_end_res", []).append(norm(torch.cat( | |
| [norm(first[None])[0], norm(last[None])[0], | |
| norm((last - first)[None])[0]])[None])[0]) | |
| reps.setdefault("ordered_res", []).append(norm(torch.cat( | |
| [norm(first[None])[0]] | |
| + [norm(q[None])[0] for q in quarters] | |
| + [norm(last[None])[0]])[None])[0]) | |
| return {k: torch.stack(v) for k, v in reps.items()} | |
| def recall_at_k(sim, same_task, ks=(1, 5)): | |
| out = {} | |
| n = sim.shape[0] | |
| for k in ks: | |
| topk = sim.topk(min(k, n - 1), dim=1).indices | |
| hit = same_task.gather(1, topk).any(dim=1) | |
| out[f"recall@{k}"] = round(float(hit.float().mean()), 4) | |
| return out | |
| def main(): | |
| cache = torch.load(EXP / "trace_vla" / "feature_cache.pt", | |
| weights_only=False) | |
| feats, episodes = cache["features"], cache["episodes"] | |
| per_ep = [] # (task_index, {rep: [15, D]}, ep_mean [768]) | |
| for ep in episodes: | |
| f3 = feats[ep["start"]:ep["start"] + ep["length"], 0].float() | |
| f = f3.mean(1) # [L, 768] patch-mean | |
| idx = torch.unique(torch.linspace(0, ep["length"] - 1, 64) | |
| .round().long()) | |
| fs = f[idx] # [64, 768] | |
| per_ep.append((ep["task_index"], window_vectors(fs), | |
| norm(f.mean(0, keepdim=True))[0])) | |
| n_ep = len(per_ep) | |
| tasks = torch.tensor([t for t, _, _ in per_ep]) | |
| report = {"episodes": n_ep, "windows_per_episode": 15} | |
| rep_names = list(per_ep[0][1].keys()) | |
| for rep in rep_names: | |
| vecs, owners, wtask = [], [], [] | |
| for ei, (task, reps, _) in enumerate(per_ep): | |
| vecs.append(reps[rep]) | |
| owners += [ei] * 15 | |
| wtask += [task] * 15 | |
| v = torch.cat(vecs) # [945, D] | |
| owners = torch.tensor(owners) | |
| wtask = torch.tensor(wtask) | |
| sim = v @ v.T | |
| sim[owners.unsqueeze(0) == owners.unsqueeze(1)] = -2.0 | |
| same = wtask.unsqueeze(0) == wtask.unsqueeze(1) | |
| report[rep] = recall_at_k(sim, same) | |
| print(rep, report[rep]) | |
| # Episode-level static retrieval (D00 proxy). | |
| v = torch.stack([m for _, _, m in per_ep]) | |
| sim = v @ v.T | |
| sim.fill_diagonal_(-2.0) | |
| same = tasks.unsqueeze(0) == tasks.unsqueeze(1) | |
| report["static_episode"] = recall_at_k(sim, same) | |
| chance = float((same.sum() - n_ep) / (n_ep * (n_ep - 1))) | |
| report["chance_same_task_rate"] = round(chance, 4) | |
| print("static_episode", report["static_episode"], "chance", chance) | |
| OUT.parent.mkdir(parents=True, exist_ok=True) | |
| OUT.write_text(json.dumps(report, indent=1)) | |
| print("saved", OUT) | |
| if __name__ == "__main__": | |
| main() | |