Planar Capacitance Model Space (QCap, QCap-Lite, QCap-Fast) from registry v2 (earlier contents: 906bc45ae05c)
ee16287 verified Download app.py from shanto268/PlanarCapacitanceModel: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/shanto268/PlanarCapacitanceModel/resolve/refs%2Fpr%2F1/app.py
- Command line
-
hf download hf://spaces/shanto268/PlanarCapacitanceModel@refs/pr/1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/shanto268/PlanarCapacitanceModel/resolve/refs%2Fpr%2F1/app.py
4.75 kB
| """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)) | |