289 lines
12 KiB
Python
289 lines
12 KiB
Python
"""T1 tar 读取、预处理、推理后端抽象。所有任务共用。"""
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
import io
|
||
import json
|
||
import tarfile
|
||
from dataclasses import dataclass
|
||
from pathlib import Path
|
||
|
||
import numpy as np
|
||
|
||
IMAGENET_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
||
IMAGENET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
||
|
||
|
||
@dataclass
|
||
class T1:
|
||
manifest: dict
|
||
tar: tarfile.TarFile
|
||
|
||
@classmethod
|
||
def open(cls, path: str | Path, expect_sha: str | None = None) -> "T1":
|
||
p = Path(path)
|
||
if expect_sha and expect_sha != "None":
|
||
h = hashlib.sha256()
|
||
with open(p, "rb") as f:
|
||
for chunk in iter(lambda: f.read(1 << 20), b""):
|
||
h.update(chunk)
|
||
if h.hexdigest() != expect_sha:
|
||
raise RuntimeError(f"T1 sha 不一致: {p} {h.hexdigest()[:12]} != {expect_sha[:12]}")
|
||
t = tarfile.open(p, "r")
|
||
man = json.loads(t.extractfile("manifest.json").read())
|
||
return cls(man, t)
|
||
|
||
@property
|
||
def items(self) -> list[dict]:
|
||
return self.manifest["items"]
|
||
|
||
def image(self, item: dict):
|
||
from PIL import Image
|
||
data = self.tar.extractfile(item["file"]).read()
|
||
return Image.open(io.BytesIO(data)).convert("RGB")
|
||
|
||
def g2_subset(self, n: int, seed: int = 20260914) -> list[dict]:
|
||
rng = np.random.default_rng(seed)
|
||
idx = rng.permutation(len(self.items))[:n]
|
||
return [self.items[i] for i in sorted(idx)]
|
||
|
||
|
||
# ---------------------------------------------------------------- 预处理(口径钉死,fp32 与 quant 同一份)
|
||
|
||
def preprocess_cls(img, size: int = 224, resize: int = 256, model: str = "") -> np.ndarray:
|
||
if model:
|
||
from ..adapters import get
|
||
adapter = get(model)
|
||
if transform := getattr(adapter, "preprocess", None):
|
||
return transform(img, f"1x3x{size}x{size}")
|
||
from PIL import Image
|
||
w, h = img.size
|
||
s = resize / min(w, h)
|
||
img = img.resize((max(1, round(w * s)), max(1, round(h * s))), Image.BILINEAR)
|
||
w, h = img.size
|
||
l, t = (w - size) // 2, (h - size) // 2
|
||
img = img.crop((l, t, l + size, t + size))
|
||
x = np.asarray(img, dtype=np.float32) / 255.0
|
||
x = (x - IMAGENET_MEAN) / IMAGENET_STD
|
||
return x.transpose(2, 0, 1)[None] # 1x3xHxW
|
||
|
||
|
||
def letterbox(img, size: int = 640):
|
||
"""YOLO 风格 letterbox。返回 (1x3xSxS float32 [0,1], scale, (padx, pady))。"""
|
||
from PIL import Image
|
||
w, h = img.size
|
||
s = size / max(w, h)
|
||
nw, nh = round(w * s), round(h * s)
|
||
img = img.resize((nw, nh), Image.BILINEAR)
|
||
canvas = Image.new("RGB", (size, size), (114, 114, 114))
|
||
px, py = (size - nw) // 2, (size - nh) // 2
|
||
canvas.paste(img, (px, py))
|
||
x = np.asarray(canvas, dtype=np.float32) / 255.0
|
||
return x.transpose(2, 0, 1)[None], s, (px, py)
|
||
|
||
|
||
# ---------------------------------------------------------------- 推理后端
|
||
|
||
class TorchRunner:
|
||
def __init__(self, model, source, weights, input_spec):
|
||
import torch
|
||
from harness.tools.export_onnx import load_model
|
||
self.torch = torch
|
||
self.m = load_model(model, source, Path(weights), input_spec)
|
||
self.dev = "cuda" if torch.cuda.is_available() else "cpu"
|
||
self.m.to(self.dev)
|
||
|
||
def __call__(self, x: np.ndarray) -> list[np.ndarray]:
|
||
with self.torch.no_grad():
|
||
out = self.m(self.torch.from_numpy(x).to(self.dev))
|
||
outs = out if isinstance(out, (list, tuple)) else [out]
|
||
flat = []
|
||
for o in outs:
|
||
if isinstance(o, (list, tuple)):
|
||
flat.extend(o)
|
||
else:
|
||
flat.append(o)
|
||
return [o.detach().float().cpu().numpy() for o in flat if hasattr(o, "detach")]
|
||
|
||
|
||
class HbmRunner:
|
||
"""x86 侧 hbm 推理。hbm 运行时(hbm_infer)在 runtime venv 里,本进程在 bench venv 里,
|
||
所以按块起子进程:一块 64 张 → npz → runtime venv 的 harness.tools.infer_hbm → npz。
|
||
单张调用也走这条路(慢),评测器应该用 batch()。"""
|
||
|
||
CHUNK = 64
|
||
|
||
def __init__(self, hbm: str, runtime_python: str, workdir: str | None = None):
|
||
import subprocess, tempfile
|
||
self.hbm = str(hbm)
|
||
self.py = runtime_python
|
||
self._temp = tempfile.TemporaryDirectory(prefix="hbm-eval-") if workdir is None else None
|
||
self.tmp = Path(workdir or self._temp.name)
|
||
self.subprocess = subprocess
|
||
self._n = 0
|
||
|
||
def batch(self, xs: list[np.ndarray]) -> list[list[np.ndarray]]:
|
||
outs: list[list[np.ndarray]] = []
|
||
for i in range(0, len(xs), self.CHUNK):
|
||
chunk = xs[i:i + self.CHUNK]
|
||
self._n += 1
|
||
fin, fout = self.tmp / f"in{self._n:04d}.npz", self.tmp / f"out{self._n:04d}.npz"
|
||
np.savez(fin, **{f"s{j:04d}": x for j, x in enumerate(chunk)})
|
||
env = dict(**__import__("os").environ)
|
||
env["PYTHONPATH"] = str(Path(__file__).resolve().parents[2])
|
||
r = self.subprocess.run([self.py, "-m", "harness.tools.infer_hbm", "--model", self.hbm,
|
||
"--inputs", str(fin), "--out", str(fout)], capture_output=True, text=True, env=env)
|
||
if r.returncode != 0:
|
||
raise RuntimeError(f"infer_hbm failed rc={r.returncode}: {r.stderr[-2000:]}")
|
||
o = np.load(fout)
|
||
for j in range(len(chunk)):
|
||
keys = sorted(k for k in o.files if k.startswith(f"s{j:04d}/"))
|
||
outs.append([o[k] for k in keys])
|
||
fin.unlink(missing_ok=True); fout.unlink(missing_ok=True)
|
||
return outs
|
||
|
||
def __call__(self, x: np.ndarray) -> list[np.ndarray]:
|
||
return self.batch([x])[0]
|
||
|
||
def close(self):
|
||
if self._temp is not None:
|
||
self._temp.cleanup()
|
||
|
||
def __enter__(self):
|
||
return self
|
||
|
||
def __exit__(self, *exc):
|
||
self.close()
|
||
|
||
|
||
def run_batched(runner, xs: list[np.ndarray]) -> list[list[np.ndarray]]:
|
||
"""TorchRunner 逐张、HbmRunner 分块——评测器只认这个入口。"""
|
||
if hasattr(runner, "batch"):
|
||
return runner.batch(xs)
|
||
return [runner(x) for x in xs]
|
||
|
||
|
||
class RecipeRunner:
|
||
"""A data-only proxy: model callbacks never enter the scoring process."""
|
||
|
||
CHUNK = 16
|
||
|
||
def __init__(self, recipe: dict, python: str, work: Path, dataset: str, *,
|
||
reference: bool = False, backend=None, graph: Path | None = None):
|
||
from types import SimpleNamespace
|
||
self.recipe, self.python, self.work = recipe, python, Path(work)
|
||
self.reference, self.backend, self.graph = reference, backend, graph
|
||
self._calls = 0
|
||
self._context = {}
|
||
self._output_bytes = None
|
||
self.host_seconds = {"preprocess": 0.0, "decode": 0.0}
|
||
self.input_spec = recipe["reference"]["input_spec"] if reference else recipe["input_spec"]
|
||
preprocess = recipe["reference"]["preprocess"] if reference else recipe["preprocess"]
|
||
self.recipe_adapter = SimpleNamespace(MODEL_SPEC={"dataset": dataset},
|
||
PREPROCESS=preprocess, preprocess=self.preprocess)
|
||
|
||
@property
|
||
def metadata(self):
|
||
return getattr(self.backend, "metadata", {})
|
||
|
||
def _call(self, operation, inputs, *, graph=False):
|
||
import time
|
||
from .. import recipe_runtime
|
||
self._calls += 1
|
||
recipe = self.recipe
|
||
if graph:
|
||
recipe = {"root": str(self.graph.parent), "onnx": self.graph.name}
|
||
started = time.monotonic()
|
||
result = recipe_runtime.call(Path(self.python), recipe, operation, inputs,
|
||
self.work / f"call-{self._calls:05d}",
|
||
reference=self.reference if not graph else False, context=self._context)
|
||
if operation in self.host_seconds:
|
||
self.host_seconds[operation] += time.monotonic() - started
|
||
return result
|
||
|
||
def prepare(self, images) -> list[np.ndarray]:
|
||
shape = tuple(map(int, self.input_spec.split("x")))
|
||
size = int(np.prod(shape)) * np.dtype(np.float32).itemsize
|
||
arrays = [np.asarray(image, dtype=np.uint8) for image in images]
|
||
result, start = [], 0
|
||
while start < len(arrays):
|
||
count = self._batch_count(arrays[start:], size)
|
||
values = {f"s{i:04d}": x for i, x in enumerate(arrays[start:start + count])}
|
||
prepared = self._call("preprocess", values)
|
||
if set(prepared) != set(values):
|
||
raise ValueError("recipe preprocessing changed sample identities")
|
||
chunk = [prepared[key] for key in values]
|
||
if any(x.shape != shape or x.dtype != np.float32 for x in chunk):
|
||
raise ValueError("recipe preprocessing must return declared float32 NCHW inputs")
|
||
result.extend(chunk)
|
||
start += count
|
||
return result
|
||
|
||
def _batch_count(self, values, output_bytes):
|
||
from ..recipe_runtime import MAX_BYTES
|
||
# Leave room for NPZ headers. Unknown model output size is measured on
|
||
# one sample before selecting subsequent batches; the limit stays fixed.
|
||
size = max(max(x.nbytes for x in values[:self.CHUNK]), output_bytes or 0, 1)
|
||
return max(1, min(self.CHUNK, (MAX_BYTES - 1024 * 1024) // size))
|
||
|
||
def preprocess(self, image, input_spec):
|
||
if input_spec != self.input_spec:
|
||
raise ValueError("recipe preprocessing input specification mismatch")
|
||
return self.prepare([image])[0]
|
||
|
||
@staticmethod
|
||
def _group(values, count):
|
||
result = []
|
||
consumed = set()
|
||
for i in range(count):
|
||
keys = sorted((k for k in values if k.startswith(f"s{i:04d}/out")),
|
||
key=lambda k: int(k.rsplit("out", 1)[1]))
|
||
if keys != [f"s{i:04d}/out{j}" for j in range(len(keys))] or not keys:
|
||
raise ValueError("recipe output identities are missing or non-contiguous")
|
||
result.append([values[k] for k in keys])
|
||
consumed.update(keys)
|
||
if consumed != set(values):
|
||
raise ValueError("unexpected recipe output identities")
|
||
return result
|
||
|
||
def batch(self, xs):
|
||
result, start = [], 0
|
||
while start < len(xs):
|
||
count = self._batch_count(xs[start:], self._output_bytes) if self._output_bytes is not None else 1
|
||
chunk = xs[start:start + count]
|
||
inputs = {f"s{i:04d}": x for i, x in enumerate(chunk)}
|
||
if self.backend is not None:
|
||
raw = run_batched(self.backend, chunk)
|
||
if len(raw) != len(chunk):
|
||
raise ValueError("board runner changed sample count")
|
||
outputs = {f"s{i:04d}/out{j}": x for i, values in enumerate(raw) for j, x in enumerate(values)}
|
||
else:
|
||
outputs = self._call("predict_onnx" if self.graph else "predict", inputs,
|
||
graph=self.graph is not None)
|
||
raw = self._group(outputs, len(chunk))
|
||
decoded = self._call("decode", outputs)
|
||
cooked = self._group(decoded, len(chunk))
|
||
self._output_bytes = max(self._output_bytes or 0,
|
||
*(sum(x.nbytes for x in sample) for sample in raw + cooked))
|
||
result.extend(cooked)
|
||
start += len(chunk)
|
||
return result
|
||
|
||
def __call__(self, x):
|
||
return self.batch([x])[0]
|
||
|
||
def __enter__(self):
|
||
if self.backend is not None:
|
||
self.backend.__enter__()
|
||
return self
|
||
|
||
def __exit__(self, *exc):
|
||
if self.backend is not None:
|
||
return self.backend.__exit__(*exc)
|
||
|
||
|
||
def cosine(a: np.ndarray, b: np.ndarray) -> float:
|
||
a = a.astype(np.float64).ravel(); b = b.astype(np.float64).ravel()
|
||
na, nb = np.linalg.norm(a), np.linalg.norm(b)
|
||
return float(a @ b / (na * nb)) if na and nb else 0.0
|