frnet-p150 / code /scripts /diag /bucket_check.py
changh95's picture
tt-model push frnet-p150 (container)
7a24154 verified
Raw History Blame Contribute Delete
3.54 kB
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""BUCKETS (OPT round 3) bit-identity check: every catalogued public frame, the PandaSet sample and the shipped
synthetic sample of one profile run through the full-capacity trace and through every FRNET_BUCKETS trace that holds
them; the valid rows of ``pred_probs`` must be identical bit for bit.
bin/devrun -t 1200 -- python -u code/scripts/diag/bucket_check.py --variant ot128 --json out.json
``--pad-check`` (OPT round 4, P2P_PAD): the reference is the full trace with the padding gather indices at pixel 0
(``zero``), and every trace that holds the frame, the full one included, runs with ``spread`` padding.
"""
from __future__ import annotations
import argparse
import json
import sys
import numpy as np
sys.path.insert(0, __file__.rsplit("/scripts/", 1)[0])
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--variant", default="ot128")
ap.add_argument("--json", required=True)
ap.add_argument("--pad-check", action="store_true")
a = ap.parse_args()
from tt_frnet import FRNet
from tt_frnet.host.preprocess import build_plan
from tt_frnet.tests import _data
res = {"variant": a.variant, "frames": {}}
with FRNet.from_pretrained(variant=a.variant) as model:
res["buckets"] = [name for name, _ in model.stagings]
plans = {}
for name, (_, _, var) in _data.FRAMES.items():
if var != a.variant:
continue
inputs, _, _ = _data.load_frame(name)
plans[name] = build_plan(inputs, model.config, pre_norm=(model.frnet_weights.pre_norm_scale,
model.frnet_weights.pre_norm_shift))
plans["pandaset_019_40"] = model._prepare(str(_data.SAMPLE), calibration=_data.SAMPLE_CALIB).plan
plans["shipped"] = model._prepare(str(_data.shipped(a.variant)),
calibration=_data.shipped_calib(a.variant)).plan
bad = 0
res["pad_check"] = bool(a.pad_check)
with model._lock:
for name, plan in plans.items():
if a.pad_check:
model.staging.p2p_pad = "zero"
full = np.asarray(model.runner("default", inputs=model.staging.write(plan)), np.float32)
full = full.reshape(-1, full.shape[-1])[: plan.n].copy()
row = {"n": int(plan.n), "m": int(plan.m), "selected": model.select(plan)[0], "buckets": {}}
for bname, st in model.stagings:
if (bname == "default" and not a.pad_check) or not st.fits(plan):
continue
if a.pad_check:
st.p2p_pad = "spread"
got = np.asarray(model.runner(bname, inputs=st.write(plan)), np.float32)
got = got.reshape(-1, got.shape[-1])[: plan.n]
same = bool(np.array_equal(got.view(np.uint32), full.view(np.uint32)))
row["buckets"][bname] = {"bit_identical": same,
"max_abs": float(np.abs(got - full).max()) if plan.n else 0.0}
bad += 0 if same else 1
res["frames"][name] = row
print(name, json.dumps(row), flush=True)
res["mismatches"] = bad
with open(a.json, "w") as f:
json.dump(res, f, indent=1)
print("MISMATCHES", bad, flush=True)
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main()