changh95's picture
tt-model push rdt2-base-p150 (container)
9d8d25c verified
Raw History Blame Contribute Delete
2.74 kB
# SPDX-License-Identifier: Apache-2.0
"""Loader and metrics for the RDT2-FM golden dumps (official code on CPU, research/rdt2/golden/golden_driver.py).
Layout: ``<RDT2_GOLDEN_DIR>/{fp32,bf16}/<case>.pt`` + ``manifest.json``. A case is ``<image>__<prompt>__s<seed>`` or
``real_ur5e__s0``. Files of the second noise seed of an (image, prompt) pair carry only the expert taps plus
``cond.mask`` / ``in.input_ids`` and point at the first file through ``meta.vlm_file`` (the VLM taps are identical,
asserted bit-exact by the driver). All float tensors are fp32 (bf16 runs upcast exactly).
"""
from __future__ import annotations
import json
import os
from typing import Dict, List, Optional
import torch
from .config import DEFAULT_DIRS
class GoldenSet:
def __init__(self, run: str = "fp32", root: Optional[str] = None):
self.dir = os.path.join(root or DEFAULT_DIRS["golden"], run)
with open(os.path.join(self.dir, "manifest.json")) as f:
self.manifest = json.load(f)
@property
def cases(self) -> List[str]:
return list(self.manifest["cases"])
def load(self, case: str, with_vlm: bool = True) -> Dict[str, torch.Tensor]:
d = torch.load(os.path.join(self.dir, self.manifest["cases"][case]["file"]), map_location="cpu",
weights_only=True)
if with_vlm and "meta.vlm_file" in d:
base = torch.load(os.path.join(self.dir, d["meta.vlm_file"]), map_location="cpu", weights_only=True)
for k, v in base.items():
if k.startswith(("vis.", "llm.", "cond.", "in.pixel_values", "in.image_grid_thw")) and k not in d:
d[k] = v
return d
def info(self, case: str) -> Dict[str, object]:
return self.manifest["cases"][case]
def pcc(a: torch.Tensor, b: torch.Tensor) -> float:
a = a.detach().reshape(-1).double()
b = b.detach().reshape(-1).double()
a = a - a.mean()
b = b - b.mean()
den = a.norm() * b.norm()
if den == 0:
return 1.0 if torch.equal(a, b) else 0.0
return float((a @ b) / den)
def max_abs(a: torch.Tensor, b: torch.Tensor) -> float:
return float((a.detach().double() - b.detach().double()).abs().max())
def rel_l2(got: torch.Tensor, ref: torch.Tensor) -> float:
ref = ref.detach().double()
return float((got.detach().double() - ref).norm() / ref.norm().clamp_min(1e-30))
def compare(got: torch.Tensor, ref: torch.Tensor) -> Dict[str, float]:
if tuple(got.shape) != tuple(ref.shape):
raise ValueError(f"shape mismatch {tuple(got.shape)} vs {tuple(ref.shape)}")
return dict(pcc=pcc(got, ref), max_abs=max_abs(got, ref), rel_l2=rel_l2(got, ref),
ref_absmax=float(ref.detach().abs().max()))