LamTNguyen's picture
REPA representation probing: report, results, analysis, code
aaa6483 verified
Raw History Blame Contribute Delete
2.5 kB
"""Shared paths, config loading and small utilities."""
from __future__ import annotations
import json
import os
import sys
import time
from pathlib import Path
import yaml
REPO = Path(__file__).resolve().parents[1]
LDM_DIR = REPO / "ldm"
# All persistent state lives on the VM disk, outside the per-run source snapshot.
ROOT = Path("/workspace/repa_probe")
ASSETS = ROOT / "assets"
PRETRAINED = ASSETS / "pretrained_models"
DATA = ROOT / "data"
CACHE = DATA / "cache"
CKPT = ROOT / "ckpt"
FEATS = ROOT / "feats"
RESULTS = ROOT / "results"
IREPA_REPO = "REPA-E/iREPA-collections"
def setup_env():
"""Make iREPA's ldm/ importable and pin caches to the persistent disk."""
for p in (ROOT, ASSETS, PRETRAINED, DATA, CACHE, CKPT, FEATS, RESULTS):
p.mkdir(parents=True, exist_ok=True)
os.environ.setdefault("HF_HOME", str(ROOT / "hf_home"))
os.environ.setdefault("TORCH_HOME", str(ROOT / "torch_home"))
os.environ.setdefault("DINOV3_REPO_DIR", str(ASSETS / "dinov3"))
os.environ.setdefault("DINOV3_CKPT_DIR", str(PRETRAINED))
os.environ.setdefault("HF_HUB_DISABLE_PROGRESS_BARS", "1")
# iREPA's vision_encoder.py resolves weights from <repo>/pretrained_models
link = REPO / "pretrained_models"
if not link.exists():
link.symlink_to(PRETRAINED)
if str(LDM_DIR) not in sys.path:
sys.path.insert(0, str(LDM_DIR))
def load_config(path: str | Path) -> dict:
with open(path) as f:
return yaml.safe_load(f)
class Timer:
def __init__(self):
self.t = {}
def __call__(self, name):
timer = self
class _Ctx:
def __enter__(self_):
self_.t0 = time.time()
log(f"[start] {name}")
def __exit__(self_, *a):
dt = time.time() - self_.t0
timer.t[name] = timer.t.get(name, 0.0) + dt
log(f"[done ] {name} in {dt:.1f}s")
return _Ctx()
def log(msg: str):
print(time.strftime("%H:%M:%S"), msg, flush=True)
def write_json(path: Path, obj):
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
with open(tmp, "w") as f:
json.dump(obj, f, indent=1, default=str)
os.replace(tmp, path)
def notify(msg: str):
"""Interim result line for OpenResearch (at most one message per 10 min is delivered)."""
p = os.environ.get("ORX_NOTIFY_FILE")
if p:
with open(p, "a") as f:
f.write(msg.rstrip() + "\n")