#!/usr/bin/env python3 """Data Factory — SupportQueryData + privileged grounding fields. Adds, for factory episodes only (has_sup=True), the candidate patch masks of every free object at the query frame, the target candidate index, and the target patch mask. These come from simulator poses recorded at generation time and are TRAINING SUPERVISION ONLY (spec v2.0 5.4/6.2); the policy inputs are unchanged. D0 samples get zero masks and has_sup=False, so the grounding losses are masked out for them. """ import sys from pathlib import Path import torch sys.path.insert(0, str(Path.home() / "sera_v4_experiments" / "trace_vla")) from trace_vla.data import SupportQueryData # noqa: E402 K = 6 class FactoryData(SupportQueryData): def __init__(self, cache, config=None): super().__init__(cache, config) self.obj_masks = cache.get("obj_masks") def get_batch(self, indices, device, generator=None): batch = super().get_batch(indices, device, generator) b = len(indices) prog = torch.zeros(b) for bi, idx in enumerate(indices): ei, t = self.samples[idx] prog[bi] = t / max(1, self.episodes[ei]["length"] - 1) batch["query_progress"] = prog.to(device) cand = torch.zeros(b, K, 128) cvalid = torch.zeros(b, K, dtype=torch.bool) tmask = torch.zeros(b, 128) tidx = torch.full((b,), -1, dtype=torch.long) has = torch.zeros(b, dtype=torch.bool) if self.obj_masks is not None: for bi, idx in enumerate(indices): ei, t = self.samples[idx] ep = self.episodes[ei] if not ep.get("has_sup"): continue m = self.obj_masks[ep["start"] + t].float() cand[bi] = m k = len(ep["cand_names"]) cvalid[bi, :k] = True tidx[bi] = int(ep["target_idx"]) tmask[bi] = m[int(ep["target_idx"])] has[bi] = True batch["cand_mask"] = cand.to(device) batch["cand_valid"] = cvalid.to(device) batch["target_idx"] = tidx.to(device) batch["target_mask"] = tmask.to(device) batch["has_sup"] = has.to(device) return batch