File size: 3,623 Bytes
f882f26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# SPDX-License-Identifier: Apache-2.0
"""Device check of the two-engine path (ONE device process; run through bin/devrun): a server with LAYA_MAX_LEN > 512
serves short rows on the megakernel and longer rows on the tt-nn engine, whose first launch of a new (batch, length)
bucket allocates device buffers after the megakernel's trace was captured (tt/traces.py). Alternates short and long
golden cases and checks that every repeat is bitwise identical to the first run and close to the official float32.

    bin/devrun -t 1200 --reset-on-timeout -- python -u -m tt_laya.tests.dev_mixed --profile multilingual
"""
from __future__ import annotations

import argparse
import json
import os
import time

import numpy as np

ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", ".."))
GOLD = os.environ.get("LAYA_GOLDEN_DIR") or os.path.join(ROOT, "assets", "laya", "golden")


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--profile", default="multilingual")
    ap.add_argument("--max-len", type=int, default=4096)
    ap.add_argument("--weights-dir", default=os.environ.get("LAYA_WEIGHTS_DIR"),
                    help="a local snapshot of convaiinnovations/laya (default: the HF cache, pinned revision)")
    ap.add_argument("--order", default="ml00_readme,ml_long_1024,ml00_readme,ml_long_4096,ml01_triage,ml00_readme,"
                                       "ml_long_1024,ml_long_4096,ml01_triage")
    ap.add_argument("--out", default="")
    a = ap.parse_args()

    import torch

    torch.set_num_threads(2)
    from tt_laya import config as C
    from tt_laya.api import Laya

    cfg = C.resolve_config({"LAYA_PROFILE": a.profile, "LAYA_MAX_LEN": str(a.max_len), "LAYA_WARMUP": "0",
                            "LAYA_WEIGHTS_DIR": a.weights_dir})
    agent = Laya.from_pretrained(config=cfg)
    first, rec, rc = {}, [], 0
    try:
        for cid in a.order.split(","):
            doc = json.load(open(os.path.join(GOLD, a.profile, cid + ".json")))
            z = np.load(os.path.join(GOLD, a.profile, cid + ".npz"))
            kw = doc["kwargs"]
            _, per = agent.prepare([doc["state"]], doc["questions"], kw.get("max_len"), kw.get("head_max_len"))
            rows = per[0]
            t0 = time.perf_counter()
            L, _ = agent.infer(rows)
            ms = (time.perf_counter() - t0) * 1e3
            err = max(float(np.abs(L[i] - z["logits_fp32"][i, :len(r.markers)]).max()) for i, r in enumerate(rows))
            drift = max(float(np.abs(z["logits_bf16"][i, :len(r.markers)] - z["logits_fp32"][i, :len(r.markers)]).max())
                        for i, r in enumerate(rows))
            flat = np.concatenate(L)
            same = None
            if cid in first:
                same = bool(np.array_equal(first[cid], flat))
                if not same:
                    rc = 1
            else:
                first[cid] = flat
            longest = max(len(r.ids) for r in rows)
            print("%-14s rows %d longest %5d  %8.1f ms  max|dlogit| %.4f (bf16 %.4f)  repeat-identical %s" % (
                cid, len(rows), longest, ms, err, drift, same), flush=True)
            rec.append({"case": cid, "ms": ms, "err": err, "bf16": drift, "identical": same, "longest": longest})
    finally:
        agent.close()
    print("MIXED " + ("PASS" if rc == 0 else "FAIL: a repeat differs from its first run"), flush=True)
    if a.out:
        with open(a.out, "w") as f:
            json.dump({"profile": a.profile, "runs": rec, "pass": rc == 0}, f, indent=1)
    raise SystemExit(rc)


if __name__ == "__main__":
    main()