File size: 2,242 Bytes
4a1ea7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | """Is the text encoder deterministic on the card for a long prompt? Part 0 (and 1), L=1532, several fresh requests, same inputs.
Usage: python diag_te_long.py [--part 0] [--prompt long] [--precision f16] [--runs 6] [--order]
"""
import argparse
import sys
import numpy as np
sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__)))
from common import MODELS, WORK, cmp, text_rope # noqa: E402
from ovrun import compile_model, infer, make_core # noqa: E402
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--part", type=int, default=0)
ap.add_argument("--prompt", default="long")
ap.add_argument("--precision", default=None)
ap.add_argument("--runs", type=int, default=6)
ap.add_argument("--warm", default="", help="comma list of prompts run first in the same compiled model (e.g. layout)")
ap.add_argument("--cache", type=int, default=1)
args = ap.parse_args()
ref = np.load(f"{WORK}/te_ref_long.npz")
core = make_core(bool(args.cache))
cm, _ = compile_model(core, f"{MODELS}/te/te_part{args.part}.xml", "GPU", args.precision, label=f"te_part{args.part}")
p = args.prompt
L = len(ref[f"{p}_ids"])
cos, sin = text_rope(L)
inp = (ref[f"{p}_embeds"] if args.part == 0 else ref[f"{p}_f32_part{args.part - 1}"])[None].astype(np.float32)
for w in [x for x in args.warm.split(",") if x]:
Lw = len(ref[f"{w}_ids"])
cw, sw = text_rope(Lw)
iw = (ref[f"{w}_embeds"] if args.part == 0 else ref[f"{w}_f32_part{args.part - 1}"])[None].astype(np.float32)
y, _ = infer(cm, {"hidden": iw, "cos": cw, "sin": sw})
cmp(ref[f"{w}_f32_part{args.part}"], y[0], f"warm {w} L={Lw}")
outs = []
for i in range(args.runs):
y, t = infer(cm, {"hidden": inp, "cos": cos, "sin": sin})
r = cmp(ref[f"{p}_f32_part{args.part}"], y[0], f"{p} L={L} part{args.part} run{i} vs fp32 ({t[0]:.3f}s)")
outs.append(y.copy())
for i in range(1, len(outs)):
d = np.abs(outs[i].astype(np.float64) - outs[0].astype(np.float64))
print(f"run{i} vs run0: max|d|={d.max():.5f} rel_l2={np.linalg.norm(d) / np.linalg.norm(outs[0]):.6f}", flush=True)
if __name__ == "__main__":
main()
|