Download code/tt_depthpro2/tests/emulate_bf16_drift.py from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/emulate_bf16_drift.py
- Command line
-
hf download hf://changh95/DepthPro2/code/tt_depthpro2/tests/emulate_bf16_drift.py
-
curl -L -o emulate_bf16_drift.py https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/emulate_bf16_drift.py
4.09 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """CPU only: emulate the OFFICIAL bf16 run (torch.autocast bf16 over encoder + reassemble + decoder, head in float32, | |
| as Apple runs it) with the clean-room reference, and write the drift metrics vs the float32 goldens. | |
| Where the official numbers exist (``assets/depthpro2/golden/bf16/*``, ``bf16_drift.py``) this emulation reproduces | |
| them to 6 digits (S img_vga / clip4_240x320: PCC, absrel_valid, p99, max_rel identical), so it stands in for the | |
| official drift of cases the official script did not run (``img_375x500_pad``, ...). | |
| python bundles/depthpro2/code/tt_depthpro2/tests/emulate_bf16_drift.py --sizes S,B,L,H \\ | |
| --cases img_vga,clip4_240x320,img_375x500_pad --out logs/depthpro2/baseline/bf16_drift_emulated.json | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import time | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | |
| import torch # noqa: E402 | |
| import baseline_helpers as H # noqa: E402 | |
| from tt_depthpro2.reference import model as ref # noqa: E402 | |
| def emulate(m: ref.DepthPro2Reference, x: torch.Tensor) -> torch.Tensor: | |
| t, _, h, w = x.shape | |
| with torch.autocast("cpu", dtype=torch.bfloat16): | |
| taps, grid = m.encode(ref.pad_to_multiple(x)) | |
| outs = [] | |
| for i in range(t): | |
| outs.append(ref.decoder(ref.reassemble([f[i:i + 1] for f in taps], grid, m.w), m.w)) | |
| return torch.cat([ref.head(o.float(), m.w) for o in outs])[:, 0, :h, :w] | |
| def emulate_fov(fw, x: torch.Tensor): | |
| """Official ``FOVEstimator(torch.bfloat16)``: the trunk under bf16 autocast, the head in float32 (``tokens.float()``) | |
| -> (fov_deg, f_px). Matches the official bf16 fov_deg of img_vga / clip4 within 0.012 deg (drift 0.06-0.09).""" | |
| h, w = x.shape[-2:] | |
| y = ref.fov_resize(x) | |
| with torch.autocast("cpu", dtype=torch.bfloat16): | |
| tok, grid = ref.fov_trunk(y, fw) | |
| lg = ref.fov_head(tok.float(), grid, fw) | |
| deg = ref.fov_logits_to_deg(lg, (h, w), tuple(y.shape[-2:])) | |
| return deg, float(ref.median_focal_length_px(deg, h, w)) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--sizes", default="S,B,L,H") | |
| ap.add_argument("--cases", default="img_vga,clip4_240x320,img_375x500_pad") | |
| ap.add_argument("--out", default=os.path.join(H.ROOT, "logs/depthpro2/baseline/bf16_drift_emulated.json")) | |
| ap.add_argument("--fov-only", action="store_true", help="only the FOV drift (keys fov__<case>)") | |
| a = ap.parse_args() | |
| torch.set_grad_enabled(False) | |
| res = {} | |
| if os.path.exists(a.out): | |
| with open(a.out) as f: | |
| res = json.load(f) | |
| fw = ref.load_safetensors(os.path.join(H.fov_dir(), "model.safetensors")) | |
| for case in a.cases.split(","): | |
| g, _ = H.load_golden(case, "S") | |
| deg, f = emulate_fov(fw, g["x_norm"]) | |
| res[f"fov__{case}"] = {"fov_deg_maxabs": round(float((deg - g["fov_deg"]).abs().max()), 5), | |
| "f_rel": round(abs(f / float(g["focallength_px"]) - 1), 6)} | |
| print("fov", case, res[f"fov__{case}"], flush=True) | |
| with open(a.out, "w") as f_: | |
| json.dump(res, f_, indent=1) | |
| if a.fov_only: | |
| return | |
| for size in a.sizes.split(","): | |
| m = ref.DepthPro2Reference(size, ref.load_safetensors(os.path.join(H.size_dir(size), "model.safetensors"))) | |
| for case in a.cases.split(","): | |
| if H.golden_path(case, size) is None: | |
| continue | |
| g, _ = H.load_golden(case, size) | |
| t0 = time.time() | |
| inv = emulate(m, g["x_norm"]) | |
| met = H.drift_metrics(inv, g["inverse_depth"].reshape(inv.shape)) | |
| met = {k: round(v, 7) for k, v in met.items()} | |
| met["seconds"] = round(time.time() - t0, 1) | |
| res[f"{case}__{size}"] = met | |
| print(case, size, met, flush=True) | |
| with open(a.out, "w") as f: | |
| json.dump(res, f, indent=1) | |
| if __name__ == "__main__": | |
| main() | |