"""Cross-phase data interface. Phase 1 (`prep/prepare.py`) calls `save_cache` to write the prepared dataset. Phases 2/3 (`train.py`) call `load_cache`, `TernDataset`, and `collate_fn` to read it during training. Phase 4 (`evaluate.py`) calls `load_cache` to read the held-out test split. Cache layout on disk: /.train.pt tensor bundle (or legacy list[dict]) /.val.pt tensor bundle (or legacy list[dict]) /.test.pt tensor bundle (or legacy list[dict]) /.manifest.json provenance + counts Large caches use compact columnar tensors. `TernDataset` also accepts legacy per-sample dictionaries so existing caches remain readable. """ import json from pathlib import Path import torch from torch.utils.data import Dataset # ── Cache I/O ──────────────────────────────────────────────────────────────── def split_size(split: list[dict] | dict) -> int: """Number of rows in a compact tensor bundle or legacy list cache.""" return len(split["input_ids"]) if isinstance(split, dict) else len(split) def save_cache( splits: dict[str, list[dict] | dict], manifest: dict, cache_dir: Path, name: str, ) -> None: """Write per-split .pt files + manifest.json. Creates cache_dir if missing.""" cache_dir = Path(cache_dir) cache_dir.mkdir(parents=True, exist_ok=True) for split_name, samples in splits.items(): if isinstance(samples, dict): compact = { key: value.contiguous() if isinstance(value, torch.Tensor) else value for key, value in samples.items() } else: # Legacy list caches contain views into parent tensors, so clone them. compact = [ {k: v.clone() if isinstance(v, torch.Tensor) else v for k, v in s.items()} for s in samples ] path = cache_dir / f"{name}.{split_name}.pt" torch.save(compact, path) size_mb = path.stat().st_size / 1e6 print(f" wrote {path} ({split_size(compact):,} samples, {size_mb:.1f} MB)") manifest_path = cache_dir / f"{name}.manifest.json" with open(manifest_path, "w") as f: json.dump(manifest, f, indent=2, default=str) print(f" wrote {manifest_path}") def load_cache( cache_dir: Path, name: str, ) -> tuple[dict[str, list[dict] | dict], dict]: """Read per-split .pt files + manifest.json.""" cache_dir = Path(cache_dir) manifest_path = cache_dir / f"{name}.manifest.json" if not manifest_path.exists(): raise FileNotFoundError( f"No manifest at {manifest_path}. Did you run prep/prepare.py?" ) with open(manifest_path) as f: manifest = json.load(f) splits: dict[str, list[dict] | dict] = {} for split_name in ("train", "val", "test"): path = cache_dir / f"{name}.{split_name}.pt" if not path.exists(): raise FileNotFoundError(f"Missing split file: {path}") splits[split_name] = torch.load(path, weights_only=False) return splits, manifest def cache_exists(cache_dir: Path, name: str) -> bool: """True if all expected cache files (manifest + 3 splits) are present.""" cache_dir = Path(cache_dir) expected = [ cache_dir / f"{name}.manifest.json", cache_dir / f"{name}.train.pt", cache_dir / f"{name}.val.pt", cache_dir / f"{name}.test.pt", ] return all(p.exists() for p in expected) # ── Runtime (Phases 2/3) ───────────────────────────────────────────────────── class TernDataset(Dataset): """Wraps a compact tensor bundle or legacy list split, indexed by row.""" def __init__(self, samples: list[dict] | dict): self.samples = samples def __len__(self) -> int: return split_size(self.samples) def __getitem__(self, idx: int) -> dict: if isinstance(self.samples, dict): return {key: value[idx] for key, value in self.samples.items()} return self.samples[idx] def collate_fn(batch: list[dict]) -> dict: """Stack per-sample dicts into a batched dict. The `source` field is a string per sample, so it's returned as a list rather than stacked. DataLoader is fine with mixed tensor/non-tensor outputs. """ return { "input_ids": torch.stack([s["input_ids"] for s in batch]).long(), "attention_mask": torch.stack([s["attention_mask"] for s in batch]), "teacher_emb": torch.stack([s["teacher_emb"] for s in batch]), "source": [s["source"] for s in batch], }