File size: 1,362 Bytes
c60a316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Pack the DEC-MoE megakernel weight arena on the CPU ahead of the first server start (no device needed).

python -I prepare_mk.py <weights dir> <geo.json> [recipe=bfp8] [attk=4]
geo.json: {"grid": [gx, gy], "opt0": [[x, y] x 8], "opt1": [...]} (the device's optimal DRAM-bank workers; the
dev / server scripts write it). The arena lands in $DSOCR_MK_CACHE (default ~/.cache/tt_dsocr/mk_moe).
"""
import json
import os
import sys
import time

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(HERE))
import torch  # noqa: E402

torch.set_num_threads(int(os.environ.get("DSOCR_THREADS", "3")))
from tt_dsocr.reference import model as R  # noqa: E402
from tt_dsocr.tt.mk_moe import arena as A  # noqa: E402
from tt_dsocr.tt.mk_moe import geometry as G  # noqa: E402
from tt_dsocr.tt.mk_moe.program import RECIPES  # noqa: E402

wdir, geo_json = sys.argv[1], sys.argv[2]
recipe = RECIPES[sys.argv[3] if len(sys.argv) > 3 else "bfp8"]
attk = int(sys.argv[4]) if len(sys.argv) > 4 else 4
g = json.load(open(geo_json))
geo = G.build(g["grid"][0], g["grid"][1], [tuple(c) for c in g["opt0"]], [tuple(c) for c in g["opt1"]], attk=attk)
t0 = time.time()
A.build_arena(R.Weights(wdir), geo, recipe, source=os.path.basename(os.path.normpath(wdir)))
print(f"arena ready in {time.time() - t0:.0f} s")