File size: 4,746 Bytes
95e723d
 
79acb1d
44a0d59
 
5e1eb19
44a0d59
f76c3bf
 
44a0d59
 
5e1eb19
79acb1d
 
 
c512e69
 
 
 
 
 
95e723d
79acb1d
5e1eb19
44a0d59
79acb1d
44a0d59
 
 
79acb1d
44a0d59
 
79acb1d
 
44a0d59
fe976b4
44a0d59
 
79acb1d
 
f76c3bf
79acb1d
 
44a0d59
62310ab
 
 
 
 
 
 
 
 
 
 
f76c3bf
 
 
 
 
 
 
79acb1d
 
f76c3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
95e723d
f76c3bf
 
 
79acb1d
 
f76c3bf
 
 
 
 
 
44a0d59
 
ee16287
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Flagship Space (deploy/GNN_SPACE.md "Flagship Space"): GDS -> full Maxwell capacitance matrix with three models (QCap default,
QCap-Lite, QCap-Fast) retrained on all on-stack data, through geomgnn.flagship. Names, tiers and numbers come from registry.yaml
(flagship_hub, flagship_tiers)."""

import os
import platform
import sys
import threading
import traceback
from pathlib import Path

if platform.machine() in ("x86_64", "AMD64"):  # OpenBLAS SkylakeX Cholesky kernel segfaulted on a v3 MoM system (LIMITS.md)
    os.environ.setdefault("OPENBLAS_CORETYPE", "Haswell")
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
os.environ.setdefault("NUMBA_CACHE_DIR", os.path.join(os.environ.get("TMPDIR", "/tmp"), "numba_cache"))  # writable on the Space
# Speed (agents/embedspeed): in-memory LRU of finished responses and the per-body embedding cache on /tmp, both size-bounded
# (uploads are held only while the Space runs; nothing is written to permanent storage).
os.environ.setdefault("TLSQ_RESPONSE_CACHE", "64")
os.environ.setdefault("TLSQ_RESPONSE_CACHE_MB", "1024")
os.environ.setdefault("TLSQ_EMBED_CACHE", os.path.join(os.environ.get("TMPDIR", "/tmp"), "tlsq_embed_cache"))
os.environ.setdefault("TLSQ_EMBED_CACHE_MB", "1024")
os.environ.setdefault("TLSQ_PRIOR_LEVEL_CACHE_MB", "256")  # solved prior levels (QCap and QCap-Lite share 1.5) and meshes (substrate changes)
os.environ.pop("TLSQ_GNN_PRIOR_MODE", None)  # every tier names its own solver mode (registry flagship_hub.prior_stacks)

HERE = Path(__file__).resolve().parent
if (HERE / "geomgnn").exists():  # built bundle (deploy/sync.py --flagship): vendored packages, registry and weights
    sys.path.insert(0, str(HERE))
    os.environ.setdefault("TLSQ_REGISTRY", str(HERE / "registry.yaml"))
    os.environ.setdefault("TLSQ_MODEL_DIR", str(HERE / "models"))
    os.environ.setdefault("TLSQ_OFFLINE", "1")
else:  # repository checkout
    sys.path.insert(0, str(HERE.parents[1] / "tlsq_infer" / "src"))
    sys.path.insert(0, str(HERE.parents[1] / "geomgnn" / "src"))
    os.environ.setdefault("TLSQ_REGISTRY", str(HERE.parents[1] / "registry.yaml"))

from tlsq_infer import set_num_threads  # noqa: E402
from tlsq_infer.registry import load_registry  # noqa: E402

os.environ.setdefault("TLSQ_NUM_THREADS", str(set_num_threads()))

from geomgnn import app_flagship as G  # noqa: E402
from geomgnn.app_flagship import build_app  # noqa: E402

reg = load_registry()
ex = HERE / "examples"
if not ex.exists():  # repository checkout: the bundle's examples (sync.py --flagship adds the lattices from data_local)
    import shutil
    import tempfile

    ex = Path(tempfile.mkdtemp()) / "examples"
    shutil.copytree(HERE.parent / "PlanarCapacitanceModel" / "examples", ex, ignore=shutil.ignore_patterns("__pycache__", "client.py"))
    for name, src in (("lattice_1q.gds", "grid_qubit_1.gds"), ("lattice_5q.gds", "grid_qubit_5.gds")):
        f = HERE.parents[2] / "data_local" / "release_core" / "gds" / "ref" / "grid" / src
        if f.exists():
            shutil.copy(f, ex / name)
BAKE = Path(os.environ.get("TLSQ_RESPONSE_BAKE", HERE / "bake" / "respcache.pkl"))  # the examples' results, baked at image build


def preload():
    """Load every tier (SHA checks) + numba warmup + one small prediction; then the baked example results, if they verify here."""
    from geomgnn import flagship as FS
    from geomgnn import paper as PA

    FS.preload(reg, example=ex / "interdigital.gds")
    if BAKE.exists() and int(os.environ.get("TLSQ_RESPONSE_CACHE", "0")) > 0:
        PA.STARTUP["bake"] = G.load_bake(BAKE, ex)


def _preload_background():
    try:
        preload()
    except BaseException:  # noqa: BLE001  (a Space that cannot load its models must not serve)
        traceback.print_exc()
        os._exit(1)
    G.READY.set()


def bake():
    """Image build (warm.py, after the import's preload): every example x model into the response cache, pickled to BAKE."""
    return G.bake(ex, BAKE)


demo = build_app(reg["flagship_hub"]["title"], examples_dir=ex)
demo.queue(default_concurrency_limit=int(os.environ.get("TLSQ_CONCURRENCY", 1)), max_size=32)
if os.environ.get("TLSQ_PRELOAD", "1") != "0":
    if __name__ == "__main__":  # the server: the page is up while the tiers load; predictions wait for them (G.READY)
        G.READY.clear()
        threading.Thread(target=_preload_background, name="preload", daemon=True).start()
    else:  # imported (warm.py, tests): load before returning
        preload()

if __name__ == "__main__":
    from geomgnn import ui as UI

    demo.launch(server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"), server_port=int(os.environ.get("PORT", 7860)),
                favicon_path=str(UI.FAVICON_PNG))