v4 flagship Space (3 tiers) from registry v1 (earlier contents: 906bc45ae05c)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .tlsq_deploy +1 -1
- LIMITS.md +0 -201
- README.md +72 -26
- app.py +22 -20
- examples/__pycache__/client.cpython-312.pyc +0 -0
- examples/client.py +0 -42
- examples/lattice_1q.gds +0 -0
- examples/lattice_5q.gds +0 -0
- geomgnn/SOURCES.json +191 -0
- geomgnn/__init__.py +8 -0
- geomgnn/_tlsq/__init__.py +0 -0
- geomgnn/_tlsq/embed/__init__.py +0 -0
- geomgnn/_tlsq/embed/fastprior.py +925 -0
- geomgnn/_tlsq/embed/fastprior_loc.py +210 -0
- geomgnn/_tlsq/embed/proxy.py +944 -0
- geomgnn/_tlsq/geometry.py +256 -0
- geomgnn/_tlsq/uge/__init__.py +5 -0
- geomgnn/_tlsq/uge/blocks.py +149 -0
- geomgnn/_tlsq/uge/graph.py +59 -0
- geomgnn/_tlsq/uge/localtok.py +99 -0
- geomgnn/_tlsq/uge/proxies.py +102 -0
- geomgnn/_tlsq/uge/scene.py +90 -0
- geomgnn/api.py +380 -0
- geomgnn/app_flagship.py +230 -0
- geomgnn/app_gnn.py +311 -0
- geomgnn/app_paper.py +250 -0
- geomgnn/fastmom.py +147 -0
- geomgnn/fastprior_pkg.py +1291 -0
- geomgnn/flagship.py +43 -0
- geomgnn/infer.py +71 -0
- geomgnn/learned_prior.py +46 -0
- geomgnn/lp_surrogate.py +396 -0
- geomgnn/paper.py +314 -0
- geomgnn/pilot_p2.py +214 -0
- geomgnn/priorv2.py +215 -0
- geomgnn/rejtail_fv.py +115 -0
- geomgnn/schema_p2d.json +123 -0
- geomgnn/train_mf.py +190 -0
- geomgnn/x1_core.py +114 -0
- models/{all-data-base/seed0.pt → accurate/acc_v2m_s0.pt} +2 -2
- models/{all-data-base/seed1.pt → accurate/acc_v2m_s1.pt} +2 -2
- models/{all-data-base/seed2.pt → accurate/acc_v2m_s2.pt} +2 -2
- models/accurate/info.json +22 -0
- models/all-data-base/config.json +0 -1
- models/all-data-flagship/config.json +0 -1
- models/all-data-flagship/seed1.pt +0 -3
- models/all-data-flagship/seed2.pt +0 -3
- models/all-data-lite1500/config.json +0 -1
- models/all-data-lite1500/seed0.pt +0 -3
- models/all-data-lite1500/seed1.pt +0 -3
.tlsq_deploy
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
managed by deploy/sync.py (registry v1)
|
|
|
|
| 1 |
+
managed by deploy/sync.py --flagship (registry v1)
|
LIMITS.md
DELETED
|
@@ -1,201 +0,0 @@
|
|
| 1 |
-
# Deployment limits (tlsq_infer 0.1.0)
|
| 2 |
-
|
| 3 |
-
Measured, not estimated. Sources: `deploy/tests/results/stress.json` (local, 2-thread budget),
|
| 4 |
-
`deploy/tests/results/reproduction_*.json` (accuracy vs crop), `deploy/tests/results/robustness.json`.
|
| 5 |
-
|
| 6 |
-
## 1. Local, 2-thread budget (HF free-tier proxy)
|
| 7 |
-
|
| 8 |
-
Apple M-series host, `torch.set_num_threads(2)`, OMP/BLAS = 2 threads, one fresh process per case (peak RSS of that
|
| 9 |
-
process). Model pair-local unless stated. Wall = prepare + MoM prior + features + network.
|
| 10 |
-
|
| 11 |
-
| sweep | value | wall (s) | MoM prior (s) | features (s) | MoM cells | peak RSS (GB) |
|
| 12 |
-
|---|---|---|---|---|---|---|
|
| 13 |
-
| two pads, gap 5 µm, size (µm) | 1,020 | 0.8 | 0.8 | 0.03 | 6,658 | 1.04 |
|
| 14 |
-
| | 1,620 | 0.9 | 0.8 | 0.09 | 4,964 | 0.70 |
|
| 15 |
-
| | 4,820 | 3.8 | 3.3 | 0.53 | 7,244 | 1.24 |
|
| 16 |
-
| | 8,820 | 5.6 | 4.0 | 1.6 | 5,580 | 0.88 |
|
| 17 |
-
| | 20,820 | 11.8 | 4.1 | 7.7 | 4,116 | 0.65 |
|
| 18 |
-
| interdigital comb, 5 µm fingers/gaps, fingers | 10 | 1.4 | 1.2 | 0.19 | 5,900 | 0.87 |
|
| 19 |
-
| | 25 | 3.5 | 3.0 | 0.41 | 7,473 | 1.24 |
|
| 20 |
-
| | 50 | 6.2 | 5.5 | 0.62 | 3,680 | 0.58 |
|
| 21 |
-
| | 100 | 14.4 | 13.0 | 1.4 | 1,916 | 0.43 |
|
| 22 |
-
| | 200 | 38.9 | 35.5 | 3.4 | 6,421 | 1.23 |
|
| 23 |
-
| | 400 | 85.8 | 76.1 | 9.7 | 2,690 | 0.73 |
|
| 24 |
-
| conductor count (pads, 30 µm gaps) | 1 / 3 / 5 / 7 | 0.07 / 0.25 / 0.21 / 0.32 | | | | <= 0.38 |
|
| 25 |
-
| | 8 | rejected (cap: 7 conductors + ground) | | | | |
|
| 26 |
-
| full-chip GDS (10 mm), polygons | 1k / 10k / 100k / 300k | 0.15 / 0.46 / 4.7 / 16.4 | | | | 0.27 / 0.30 / 0.73 / 1.75 |
|
| 27 |
-
| model at comb 25 / 200 | pair-local | 3.4 / 39.2 | | | | 1.24 |
|
| 28 |
-
| | blend-pair-local | 4.3 / 39.8 (+0.08 s charge correction, +0.7 s first GBM load) | | | | 1.37 |
|
| 29 |
-
| | message-passing | 3.5 / 40.3 | | | | 1.25 |
|
| 30 |
-
| | fine-mom-prior (16k cells) | 5.7 / 52.0 | | | | 1.26 |
|
| 31 |
-
|
| 32 |
-
Findings
|
| 33 |
-
* **No failure point was reached** within a 300 s timeout or the 16 GB budget: the largest case (400-finger comb,
|
| 34 |
-
8.8 mm) took 86 s and every case peaked below 1.8 GB RSS. The MoM matrix is capped at 8,000 cells (16,000 fine) by
|
| 35 |
-
default (`max_cells` in `tlsq_infer.registry.PRIORS`, a cost setting), so memory is bounded (~1.4 GB per standard
|
| 36 |
-
request, ~4-5 GB worst case for the fine prior); a larger cap raises both accuracy on long combs and memory.
|
| 37 |
-
* **Latency is driven by boundary complexity, not cell count**: the MoM mesh builder (quadtree + shapely coverage,
|
| 38 |
-
repeated with coarser h_min until <= 8,000 cells) dominates; long, finely gapped boundaries (combs, meanders) cost
|
| 39 |
-
most. Feature extraction samples every boundary at 0.25 µm and grows linearly with total perimeter (7.7 s at 21 mm).
|
| 40 |
-
* The network itself is < 3 ms; charge correction < 0.1 s (it reuses the prior's solve).
|
| 41 |
-
* GDS parsing: ~55 µs per polygon (300k polygons: 16 s, 1.75 GB).
|
| 42 |
-
* Paper-family designs (1 thread, all 926 never-seen designs): median 5.8 s, p90 34 s per design for blend + message
|
| 43 |
-
passing (two MoM solves); a single model is about half.
|
| 44 |
-
|
| 45 |
-
Recommended caps (implemented in `tlsq_infer/app_ui.py`, overridable by env):
|
| 46 |
-
* file <= 25 MB (`TLSQ_MAX_FILE_MB`), <= 200,000 polygons (`TLSQ_MAX_POLYGONS`), <= 30 conductors (demo-server cap only; warning beyond the validated 7 conductors + ground);
|
| 47 |
-
* queue concurrency 2 per Space (`TLSQ_CONCURRENCY`; each request uses ~2 threads and <= 1.5 GB);
|
| 48 |
-
* expect ~1-10 s for single components, up to ~90 s for very long interdigitated or meandered boundaries.
|
| 49 |
-
|
| 50 |
-
## 2. Ground crop and accuracy
|
| 51 |
-
|
| 52 |
-
The training chips extend 150-330 µm (15-220 conductor-ground gaps) beyond the conductors. Cropping the ground tighter
|
| 53 |
-
changes both the physics and the network input. 150 random never-seen designs, all mutual entries, median % / MAE fF /
|
| 54 |
-
RMSE fF (`reproduction_n150_crop_margins.json`, `reproduction_n150_auto.json`):
|
| 55 |
-
|
| 56 |
-
| crop | pair-local | blend-pair-local |
|
| 57 |
-
|---|---|---|
|
| 58 |
-
| none (paper setting) | 1.78 / 0.98 / 1.82 | 1.70 / 0.84 / 1.53 |
|
| 59 |
-
| **auto** (default: max(100 gaps, half the conductor extent, 150 µm) around the conductors and their pocket) | **1.87 / 1.00 / 1.83** | **1.82 / 0.87 / 1.54** |
|
| 60 |
-
| 100 gaps | 1.96 / 1.01 / 1.84 | 1.93 / 0.92 / 1.60 |
|
| 61 |
-
| 50 gaps | 2.61 / 1.32 / 2.27 | 2.56 / 1.31 / 2.30 |
|
| 62 |
-
| 20 gaps | 3.76 / 2.13 / 3.81 | 3.71 / 2.20 / 4.08 |
|
| 63 |
-
| 10 gaps | 4.77 / 2.76 / 4.95 | 4.90 / 2.90 / 5.36 |
|
| 64 |
-
|
| 65 |
-
(Errors against the labels of the uncropped chips, so tight crops also measure the physical change of removing ground.)
|
| 66 |
-
Default is therefore `auto`; tight windows are available but flagged in the docs.
|
| 67 |
-
|
| 68 |
-
## 3. Robustness (219 files, `robustness.json`)
|
| 69 |
-
|
| 70 |
-
| source | files | result | rejected (clean error) | crashes |
|
| 71 |
-
|---|---|---|---|---|
|
| 72 |
-
| Qiskit Metal singletons (all) | 36 | 36 physical matrices | 0 | 0 |
|
| 73 |
-
| Qiskit Metal parametric sweeps (3 per class, 29 classes) | 87 | 87 | 0 | 0 |
|
| 74 |
-
| our families (exp6, exp13-15) | 15 | 15 | 0 | 0 |
|
| 75 |
-
| OOD-60 shapes | 60 | 60 | 0 | 0 |
|
| 76 |
-
| synthetic adversarial | 21 | 15 | 6 (as expected) | 0 |
|
| 77 |
-
|
| 78 |
-
Synthetic cases: non-default layers (clean error with defaults, works with explicit roles), 10 mm full-chip ground with
|
| 79 |
-
48 conductors (clean error; works with `select` / `select_box`, unselected bodies flagged), conductor with holes and an
|
| 80 |
-
island inside, 50 nm gap (resolution-floor warning), 7 conductors (ok) / 8 (clean error), overlapping and edge-sharing
|
| 81 |
-
polygons (merged into one net), conductor touching ground (merged into ground, warning), references + FlexPath
|
| 82 |
-
(flattened), ground split by the window, no ground / empty / non-GDS (clean errors), 8 mm pocket. Invariance:
|
| 83 |
-
translation by 1e5 µm, 90° rotation and nm database units give identical matrices (max rel. diff 0).
|
| 84 |
-
Physical checks: all mutuals negative and self terms positive in every returned matrix. The network self term is
|
| 85 |
-
below the row sum of mutuals (C_ii < sum |C_ij|) in 87/213 matrices (mostly Qiskit Metal parts far from the training
|
| 86 |
-
families): use `self_from_rows=True` when a strictly consistent Maxwell matrix is needed.
|
| 87 |
-
|
| 88 |
-
## 4. Deployed Spaces (HF free tier, cpu-basic)
|
| 89 |
-
|
| 90 |
-
Measured 2026-10-10 after the fine-tier switch to `all-data-flagship` (private Spaces, `examples/client.py` path
|
| 91 |
-
`/predict_gds`, one request at a time; server time = prepare + predict inside the Space; wall adds upload / queue). Every
|
| 92 |
-
returned matrix equals the local prediction of the same bundle to <= 3e-5 relative (max over entries).
|
| 93 |
-
|
| 94 |
-
| layout | coarse tier (`all-data-v3s`), server s | fine tier (`all-data-flagship`), server s | note |
|
| 95 |
-
|---|---|---|---|
|
| 96 |
-
| Xmon example | 1.2 | 1.5 | |
|
| 97 |
-
| interdigital example | 2.4 | 0.8 | |
|
| 98 |
-
| TransmonPocket example | 19.7 (routed to the fine tier) | 19.4 | capped coarse mesh < 0.38 panels/gap: large-layout routing |
|
| 99 |
-
| shielded concentric example | 4.8 | 79.6 | flagship H-matrix solve, Mac 1 thread 14.6 s |
|
| 100 |
-
| two pads, size 20x (stress) | 61.7 (routed) | 60.2 | |
|
| 101 |
-
| comb, 200 fingers (stress) | 224.4 (routed) | 220.0 | |
|
| 102 |
-
|
| 103 |
-
Local, same bundle through the Space API function (`app_ui.api_predict_gds`), Mac M5 Pro, 2 threads, one persistent
|
| 104 |
-
process, fine tier, s: Xmon 0.5, interdigital 0.3, TransmonPocket 5.2, shielded concentric 21.2, never-seen shielded
|
| 105 |
-
concentric exp15/0043 (slowest held-out design) 67.4, comb 200 / 400 fingers 55.6 / 77.9, two pads 20x / 50x 14.7 / 19.8;
|
| 106 |
-
process peak RSS 5.7 GB (H-matrix PCG on every large case; comb_f400 4.65 panels per gap). The Space CPU is 3.4-5.4x slower than the Mac at 1 thread on the flagship solves (comb 200 fingers 64.7 s, two pads 20x 13.5 s, shielded concentric 14.6 s locally). Paper-models Space: same matrices and times
|
| 107 |
-
(e.g. TransmonPocket 18.0 s fine). Gradio has no request timeout; the queue runs 2 requests at once (`TLSQ_CONCURRENCY`)
|
| 108 |
-
but at most one flagship-prior solve (`TLSQ_HEAVY_CONCURRENCY`, default 1; fine tier and coarse requests routed to it),
|
| 109 |
-
so two large solves (up to 8.3 GB each, grid_qubit_9) cannot exceed the 16 GB of cpu-basic.
|
| 110 |
-
|
| 111 |
-
## 5. Known biases and reporting (review fixes, 2026-10-08)
|
| 112 |
-
|
| 113 |
-
* **Self-capacitance from row sums.** `self_from_rows=True` uses C_ii = sum_j |C_ij| / (1 - eps) with eps = 0.0143, the
|
| 114 |
-
training-family median of the row-sum fraction. On the never-seen components the median is 0.0099 (0.986 % over
|
| 115 |
-
2,477 signal conductors of the 926-design evaluation set), so the default overestimates C_Sigma (and underestimates
|
| 116 |
-
E_C) by about 0.45 % there. eps is a parameter (`predict(..., row_sum_eps=...)`); the default is kept at the training
|
| 117 |
-
value and is not tuned on held-out data.
|
| 118 |
-
* **Mesh coarsening.** When a layout needs more than the default cell cap (8,000 for the gap/3 meshes / 16,000 for the gap/6 solve of the fine prior; a setting,
|
| 119 |
-
not a limit of the models), the MoM mesh is coarsened until it fits. This is now reported: `result["prior_mesh"]` (achieved and target h_min, smallest gap, cells
|
| 120 |
-
across it, `cap_hit`; for v3-rich also `extrapolated`), a warning in `warnings` and `expected_error["mesh_warning"]`.
|
| 121 |
-
* **Conductor order.** Networks and the MoM prior are invariant to the polygon order of the file (shuffling test:
|
| 122 |
-
MoM exact to 4e-16, network outputs to 5e-5 float32 round-off). The published blend assigns entry kinds by file order;
|
| 123 |
-
deployment blends use order-invariant kinds (conductor-conductor / conductor-ground) when `blend.kinds: invariant`
|
| 124 |
-
is set in the registry (`deploy/tests/test_review_fixes.py`).
|
| 125 |
-
|
| 126 |
-
## 6. Flagship tiers: `all-data-v3s` (coarse prior, default) and `all-data-v3-rich` (fine prior), 2-thread latency
|
| 127 |
-
|
| 128 |
-
**Update 2026-10-10:** the fine tier is now `all-data-flagship` (same network as `all-data-v3-rich`, flagship prior;
|
| 129 |
-
deploy/MODEL_SELECTION.md Results 3, PRIOR_SCHEME.md section 8.1). Mac M5 Pro, 2 threads, whole `predict`: median /
|
| 130 |
-
p90 / max 0.87 / 11.6 / 14.0 s on the 212 held-out designs and 1.51 / 27.6 / 78.4 s on 1,158 held-out designs (former
|
| 131 |
-
fine tier 4.64 / 8.96 / 18.0 s); peak RSS <= 8.3 GB on the stress and grid layouts (grid_qubit_9, 220k panels), every
|
| 132 |
-
one returns a matrix (the resolution floor L/8192 limits comb_f400 / pads_s20 / pads_s50 to 4.65 / 4.64 / 1.97 panels
|
| 133 |
-
per gap). The table below is the former fine tier (`all-data-v3-rich`) and stays as its record.
|
| 134 |
-
|
| 135 |
-
Naming (2026-10-09): **coarse prior** = one gap/3 solve on the conductor-anchored v3 mesh (was "fast"), **fine prior** =
|
| 136 |
-
gap/3 + gap/6 with Richardson extrapolation (was "accurate"); `predict(model="coarse" | "fine")`, the old words are kept as
|
| 137 |
-
aliases and in the model ids. The chip-anchored baselines are the earlier standard (8k) and earlier 16k priors (the latter
|
| 138 |
-
was "earlier fine"). "Flagship prior" is reserved for a future large-scale scheme and is not released.
|
| 139 |
-
|
| 140 |
-
Measured on HPC main-partition nodes (`torch.set_num_threads(2)`, OMP/BLAS 2 threads, `OPENBLAS_CORETYPE=Haswell`;
|
| 141 |
-
fastest of the runs per case). These nodes are ~2.5x slower than the Apple M host of section 1 (pair-local comb_f400:
|
| 142 |
-
277 s here, 86 s there). End to end from the GDS, s, after the v3 non-positive-definite fix (deploy/MODEL_SELECTION.md,
|
| 143 |
-
amendment 5):
|
| 144 |
-
|
| 145 |
-
| layout | all-data-v3s (coarse) | all-data-v3-rich (fine) | all-data-base (fallback) | pair-local (previous flagship) |
|
| 146 |
-
|---|---|---|---|---|
|
| 147 |
-
| comb, 25 / 50 / 100 fingers | 8.0 / 24.4 / 23.1 | 26.6 / 58.6 / 101.7 | 10.8 / 20.9 / 36.8 | 10.8 / 20.9 / 36.8 |
|
| 148 |
-
| comb, 200 / 400 fingers | 91.0 / 161.4 | 257.3 / 293.2 | 98.3 / 276.7 | 98.3 / 276.7 |
|
| 149 |
-
| two pads, size 5 / 10 / 20 / 50 (x) | 4.4 / 6.5 / 15.9 / 27.8 | 17.2 / 36.9 / 48.1 / 53.4 | 4.0 / 12.0 / 17.9 / 27.1 | 4.0 / 12.1 / 17.9 / 27.1 |
|
| 150 |
-
| 7 conductors | 2.0 | 8.6 | 0.7 | 0.7 |
|
| 151 |
-
| 212 held-out designs: median / p90 / max | 3.0 / 8.0 / 12.8 | 12.8 / 33.3 / 50.4 | 1.9 / 9.7 / 30.9 | 1.9 / 9.8 / 30.9 |
|
| 152 |
-
|
| 153 |
-
Findings
|
| 154 |
-
* **v3 non-positive-definite systems (fixed).** On long fine combs the cell cap coarsens the v3 lattice beyond the finger
|
| 155 |
-
pitch (h = 6.7 µm for 5 µm gaps at 50 fingers, 27-34 µm at 200-400 fingers); the 3 x 3 near-field average then
|
| 156 |
-
over-estimates the coupling of panels closer than their size (smallest eigenvalue -0.15). Only when the Cholesky fails,
|
| 157 |
-
the v3 solve is repeated with the exact potential of a uniformly charged rectangle for near pairs (solves the 50- and
|
| 158 |
-
200-finger combs), and if that still fails, with whole finest cells owned by the majority metal (as the v2 mesh; solves
|
| 159 |
-
the 400-finger comb, where cut panels of different conductors overlap). Priors that solved before are bit-identical.
|
| 160 |
-
`all-data-v3s` no longer needs its fallback model on any stress, held-out or robustness layout; the fallback stays as a
|
| 161 |
-
safety net.
|
| 162 |
-
* **Resolution floor.** At 200-400 fingers every prior (v2 included) is far from converged (0.15-0.75 cells across the
|
| 163 |
-
gap; e.g. 50 fingers: v2 standard C_NS 480 fF, v3 691 fF, v3 with a 4x cell cap 725 fF, earlier 16k 704 fF). The result
|
| 164 |
-
carries the mesh-coarsening warning; treat such layouts as indicative.
|
| 165 |
-
* The fine tier's worst case (400-finger comb, 293 s on these nodes) is close to a 300 s request budget; on the Space
|
| 166 |
-
hardware measure it with `deploy/tests/test_app.py` after deployment.
|
| 167 |
-
* OpenBLAS's SkylakeX Cholesky kernel segfaulted on one v3 system (Qiskit Metal TransmonInterdigitated sample, 15.5k
|
| 168 |
-
panels); the Space apps set `OPENBLAS_CORETYPE=Haswell` on x86 before numpy loads.
|
| 169 |
-
* Robustness (`deploy/tests/results/robustness_all-data-v3s.json`, `robustness_all-data-v3-rich.json`): 219/219 files
|
| 170 |
-
pass for both (matrices or clean rejections, invariance to translation, rotation, units); C_ii < sum |C_ij| in 98
|
| 171 |
-
(coarse) / 80 (fine) of 213 matrices (use `self_from_rows=True` for a consistent Maxwell matrix).
|
| 172 |
-
* Bundle: flagship Space 10.4 MB (both tiers + `all-data-base` + `all-data`, the fine tier's fallback), paper-models
|
| 173 |
-
Space 25.7 MB (`python deploy/sync.py`, dry run).
|
| 174 |
-
|
| 175 |
-
## 7. Per-entry error estimates (`result["per_entry"]`, ERROR_MODEL.md, 2026-10-08)
|
| 176 |
-
|
| 177 |
-
**Not enabled in the release** (user decision 2026-10-08): `artifacts.error_model` in the registry is empty, so
|
| 178 |
-
`per_entry` is `None` and `expected_error` is the global estimate, until the pre-declared v2 has been evaluated on the
|
| 179 |
-
fresh Qiskit Metal atlas and >7-conductor labels. The code, the v1 artifacts and the tests stay in place; enabling is
|
| 180 |
-
one registry line. When enabled, the coarse and fine tiers and `pair-local-fast` return, per mutual entry, `interval_80_fF`, `interval_95_fF`,
|
| 181 |
-
`p_large_error` (P(|err| > 5 %)) and `predicted_median_err_pct`; the Spaces show the matrix with ± the 80 % interval
|
| 182 |
-
(toggle), a per-entry table and a CSV of the intervals. `expected_error` is now derived from the per-entry estimates
|
| 183 |
-
(`source`; the old global estimate is kept under `global_estimate`); the material / thickness terms are added in
|
| 184 |
-
quadrature. Other models keep the global estimate. The matrix itself is unchanged (tests: `test_error_model.py`).
|
| 185 |
-
Cost: < 35 ms per 8-conductor matrix (features + two 300-tree models); artifacts 0.4 MB each.
|
| 186 |
-
|
| 187 |
-
**Measured on held-out designs (never used in fitting or calibration), coarse tier / fine tier:**
|
| 188 |
-
|
| 189 |
-
| set | entries (> 5 %) | network median % / MAE fF / RMSE fF | AUROC P(>5 %) | 80 % coverage | 95 % coverage |
|
| 190 |
-
|---|---|---|---|---|---|
|
| 191 |
-
| OOD-60 | 252 (9 / 6) | 1.06 / 0.449 / 0.898 ; 0.66 / 0.230 / 0.481 | 0.58 ; 0.99 | 77.8 ; 74.6 | 94.0 ; 95.6 |
|
| 192 |
-
| TransmonPocket-3 random | 540 (72 / 81) | 1.89 / 0.319 / 0.563 ; 1.75 / 0.268 / 0.506 | 0.71 ; 0.79 | 74.4 ; 73.9 | 94.6 ; 99.1 |
|
| 193 |
-
| Qiskit Metal new classes | 1136 (208 / 214) | 1.84 / 0.654 / 1.416 ; 1.40 / 0.505 / 1.064 | 0.56 ; 0.68 | 73.0 ; 70.2 | 86.0 ; 88.6 |
|
| 194 |
-
|
| 195 |
-
* **Treat the intervals as indicative.** The pre-declared targets (AUROC >= 0.75, coverage within 5 points) were not
|
| 196 |
-
met: the intervals under-cover by up to 10 points (80 %) / 9 points (95 %) on the Qiskit Metal components.
|
| 197 |
-
* **Small couplings.** Entries below 0.3 fF have a median error of 4.0 % (coarse; MAE 0.004 fF, RMSE 0.007 fF) and their
|
| 198 |
-
80 % intervals cover only ~53 %; above 10 fF the error is 1.6 % (MAE 0.90 fF, RMSE 1.55 fF) and coverage 80 %. A
|
| 199 |
-
relative error flag on a sub-0.3 fF coupling is usually an aF-scale absolute error.
|
| 200 |
-
* `p_large_error` ranks entries only moderately; the predicted |C_ij| alone, the seed spread and the embedding distance
|
| 201 |
-
rank them as well or better on these sets (ERROR_MODEL.md, Results). Verify critical couplings with a field solver.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -8,35 +8,81 @@ sdk_version: 6.29.1
|
|
| 8 |
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
-
short_description:
|
| 12 |
-
models:
|
| 13 |
-
- SQuADDS/2026PaperModels-pair-local
|
| 14 |
-
- SQuADDS/2026PaperModels-message-passing
|
| 15 |
-
- SQuADDS/2026PaperModels-charge-corrected-prior
|
| 16 |
-
- SQuADDS/2026PaperModels-blend-pair-local
|
| 17 |
-
- SQuADDS/2026PaperModels-blend-message-passing
|
| 18 |
-
- SQuADDS/2026PaperModels-geometry-only
|
| 19 |
-
- SQuADDS/2026PaperModels-pair-local-fine
|
| 20 |
-
- SQuADDS/2026PaperModels-message-passing-fine
|
| 21 |
-
- SQuADDS/2026PaperModels-all-data
|
| 22 |
-
- SQuADDS/2026PaperModels-all-data-base
|
| 23 |
-
- SQuADDS/2026PaperModels-all-data-v3s
|
| 24 |
-
- SQuADDS/2026PaperModels-all-data-v3-rich
|
| 25 |
-
- SQuADDS/2026PaperModels-all-data-lite1500
|
| 26 |
-
- SQuADDS/2026PaperModels-all-data-flagship
|
| 27 |
-
- SQuADDS/2026PaperModels-pair-local-fast
|
| 28 |
-
- SQuADDS/2026PaperModels-message-passing-fast
|
| 29 |
-
- SQuADDS/2026PaperModels-pair-local-accurate
|
| 30 |
-
- SQuADDS/2026PaperModels-message-passing-accurate
|
| 31 |
-
- SQuADDS/2026PaperModels-charge-corrected-prior-fast
|
| 32 |
-
- SQuADDS/2026PaperModels-blend-pair-local-fast
|
| 33 |
-
- SQuADDS/2026PaperModels-blend-pair-local-accurate
|
| 34 |
---
|
| 35 |
|
| 36 |
# Planar Capacitance Model
|
| 37 |
|
| 38 |
-
Upload a GDS of a planar superconducting layout (ground 1/0, conductors 1/10, or assign your layers) and get
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
+
short_description: 'Maxwell capacitance of planar layouts: 3 tiers'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
# Planar Capacitance Model
|
| 15 |
|
| 16 |
+
Upload a GDS of a planar superconducting layout (ground 1/0, conductors 1/10, or assign your layers) and get its full Maxwell
|
| 17 |
+
capacitance matrix in fF, self terms included. Three tiers in one selector, each the best recipe of the paper at its speed,
|
| 18 |
+
**retrained on all 45,771 on-stack Q3D layouts** (Si, eps_r 11.45, 350 µm: training, validation and every test
|
| 19 |
+
layout that passed label QC). Private preview while the paper is in preparation. To reproduce the paper's numbers with the paper's own
|
| 20 |
+
models, use the paper Space [shanto268/2026PaperModels](https://huggingface.co/spaces/shanto268/2026PaperModels).
|
| 21 |
|
| 22 |
+
## Accuracy: what can honestly be said
|
| 23 |
+
Trained on everything, **these models have no held-out score of their own.** Each tier lists (a) the paper's held-out numbers for the
|
| 24 |
+
**same recipe** trained on the 35,354-layout TRAIN split (validation = 4,608 layouts of 13 families; test = the paper's test benchmark,
|
| 25 |
+
read once: in family 4,009 layouts, zero-shot macro over nine component cohorts 1,878 layouts; held-out family = leave one family out,
|
| 26 |
+
4,071 layouts) and (b) **one fresh check, B5**: 39 new layouts of one generated family (campaign450 B5, 3-4 conductors) that no
|
| 27 |
+
component of any tier ever saw, against its physics reference. Layout-mean median relative error % (MAE / RMSE fF) over mutual
|
| 28 |
+
entries against Q3D, 0.1 fF floor. "–" = not measured.
|
| 29 |
|
| 30 |
+
| id | tier | paper VAL | paper test in family | paper test zero-shot | paper held-out family | B5 (this model) | B5 physics alone | time on this Space (2 vCPU) | time, x86 node (2 CPUs) |
|
| 31 |
+
|---|---|---|---|---|---|---|---|---|---|
|
| 32 |
+
| `accurate` (default) | Accurate (~1 s) | 0.229 % (0.0674 / 0.0792 fF) | 0.177 % (0.0203 / 0.025 fF) | 0.587 % (1.092 / 1.152 fF) | 0.408 % (0.049 / 0.059 fF) | 1.034 % (0.131 / 0.189 fF) | 1.765 % (0.275 / 0.376 fF) | – | 0.845 / 5.96 s in family, 1.75 / 18.1 s zero-shot (median / p90; x86 cluster, 2 CPUs); B5 0.99 s median |
|
| 33 |
+
| `balanced` | Balanced (~0.5 s) | 0.343 % (0.123 / 0.138 fF) | – | – | – | 1.753 % (0.236 / 0.308 fF) | 2.681 % (0.396 / 0.583 fF) | – | 0.490 / 2.31 s in family, 1.00 / 10.0 s zero-shot (median / p90; x86 cluster, 2 CPUs); B5 0.56 s median |
|
| 34 |
+
| `fast` | Fast (~0.1 s) | 0.846 % (0.491 / 0.549 fF) | 0.63 % (0.113 / 0.1414 fF) | 3.576 % (4.608 / 5.058 fF) | 3.113 % (0.533 / 0.633 fF) | 3.175 % (0.583 / 0.824 fF) | 1.765 % (0.275 / 0.376 fF) | – | 0.091 / 0.274 s in family, 0.30 / 2.45 s zero-shot (median / p90; x86 cluster, 2 CPUs); B5 0.24 s median |
|
| 35 |
+
|
| 36 |
+
* **Accurate** (default) beats the physics it corrects on B5 by 0.73 pp of median error (paired bootstrap 95 % CI [0.35, 1.12]), 0.144 fF
|
| 37 |
+
of MAE and 0.187 fF of RMSE. Use it for anything that matters.
|
| 38 |
+
* **Balanced** is about 2x faster than Accurate at 1.5-1.7x its error (paper VAL 0.343 % (0.123 / 0.138 fF) vs
|
| 39 |
+
0.229 % (0.0674 / 0.0792 fF); B5 1.753 % (0.236 / 0.308 fF) vs 1.034 % (0.131 / 0.189 fF)).
|
| 40 |
+
* **Fast** is for quick screening of the component families it was trained on (interdigital, paddle, concentric, star, multipad star,
|
| 41 |
+
Xmon, TransmonPocket, shielded concentric, two-net, random, procedurally generated and synthetic campaign layouts). **On B5 (new designs
|
| 42 |
+
of a trained family) it was worse than the physics alone**: 3.175 % (0.583 / 0.824 fF) against 1.765 % (0.275 / 0.376 fF); and in
|
| 43 |
+
the paper, on a component family held out of its training, it was worse than the physics alone too: 3.113 % (0.533 / 0.633 fF)
|
| 44 |
+
against 1.554 % (0.166 / 0.205 fF). Its error comes from the learned prior. Paper validation of this surrogate recipe with
|
| 45 |
+
the networks: 0.722 % (0.387 / 0.436 fF).
|
| 46 |
+
|
| 47 |
+
Sources: `agents/flagship_v4/RESULTS.md` (training, B5, timings), paper_v4 (tab:ladder-test, tab:routes) for the reference rows.
|
| 48 |
+
|
| 49 |
+
### `accurate`: Accurate (~1 s): extrapolated physics R(1.5, 3) on the corrected prior + residual networks, compiled solver
|
| 50 |
+
|
| 51 |
+
Two thin-film method-of-moments solves (corrected prior v2m, 1.5 and 3 panels per gap), Richardson-extrapolated, corrected by a 3-seed ensemble of the paper model's networks (89,858 parameters per seed, bounded residual) with a learned row-sum closure: the complete Maxwell matrix, self terms included. Networks retrained on all 45,771 on-stack Q3D layouts.
|
| 52 |
+
|
| 53 |
+
Reference recipe: the paper's model (same networks and prior stack at inference; its networks were trained on 35,354 TRAIN layouts on the uncorrected prior; retrained on the corrected prior, as here, they score validation 0.218 % (0.0774 / 0.0888 fF)).
|
| 54 |
+
|
| 55 |
+
### `balanced`: Balanced (~0.5 s): the same recipe on the coarser pair R(0.75, 1.5) of the corrected prior, networks retrained
|
| 56 |
+
|
| 57 |
+
The Accurate recipe on the coarser Richardson pair (0.75 and 1.5 panels per gap, about half the solve time), networks retrained on that pair on all 45,771 on-stack layouts. About 2x faster than Accurate at 1.5-1.7x its error.
|
| 58 |
+
|
| 59 |
+
Reference recipe: the paper's coarser pair + the model's recipe (trained on 35,354 TRAIN layouts; validation only, never test-scored).
|
| 60 |
+
|
| 61 |
+
### `fast`: Fast (~0.1 s): learned prior surrogate + residual networks, no field solve (trained component families only)
|
| 62 |
+
|
| 63 |
+
A learned surrogate (1.02M parameters, trained on the prior of every on-stack layout plus 432,697 generated layouts labelled by the prior alone) predicts the two uncorrected-prior solves at 1.5 and 3 panels per gap; networks retrained on all 45,771 on-stack layouts on that prior correct them. No field solve: the time is the geometric embedding.
|
| 64 |
+
|
| 65 |
+
Reference recipe: the paper's fast model (learned prior surrogate + the networks on its prior, trained on 35,354 TRAIN layouts).
|
| 66 |
+
|
| 67 |
+
The models were trained on Si (eps_r 11.45), 350 µm only and are served for that stack only. Accurate and Balanced are dominated by the
|
| 68 |
+
two thin-film solves; Fast by the geometric embedding. Ground self term: indicative only (it depends on the chip boundary).
|
| 69 |
+
|
| 70 |
+
## API (gradio_client; the same endpoints over REST at `/gradio_api/call/<name>`)
|
| 71 |
+
* `/models` () -> served tiers with their reference numbers and times.
|
| 72 |
+
* `/inspect` (gds) -> layer table; `/prepare` (gds, layers, crop, margin_gaps, margin_um, select) -> preview and summary.
|
| 73 |
+
* `/predict` (gds, layers, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model).
|
| 74 |
+
* `/predict_gds` (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model) -> result dict
|
| 75 |
+
(`matrix_fF`, `names`, `timing_s`, `prior_stack`, `reference`, `b5`). model = `accurate`, `balanced`, `fast` (default `accurate`).
|
| 76 |
+
Up to 70 conductors + ground per layout.
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
import os
|
| 80 |
+
from gradio_client import Client, handle_file
|
| 81 |
+
c = Client("shanto268/PlanarCapacitanceModel", token=os.environ["HF_TOKEN"]) # token while the Space is private
|
| 82 |
+
r = c.predict(handle_file("layout.gds"), "1/0", "1/10", 0, "Si (cryogenic, training)", 11.45, 350, "accurate",
|
| 83 |
+
api_name="/predict_gds")
|
| 84 |
+
print(r["names"], r["matrix_fF"], r["timing_s"]["total_s"])
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
Privacy: uploads and result files are deleted within 10 minutes; no persistent storage, no analytics, no file names or
|
| 88 |
+
geometry in the logs. Hugging Face keeps ordinary web access logs under its own policy.
|
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-
"""Flagship Space: GDS ->
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import platform
|
|
@@ -6,37 +8,37 @@ import sys
|
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
if platform.machine() in ("x86_64", "AMD64"): # OpenBLAS SkylakeX Cholesky kernel segfaulted on a v3 MoM system (LIMITS.md)
|
| 9 |
-
os.environ.setdefault("OPENBLAS_CORETYPE", "Haswell")
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
HERE = Path(__file__).resolve().parent
|
| 12 |
-
if (HERE / "
|
| 13 |
sys.path.insert(0, str(HERE))
|
| 14 |
os.environ.setdefault("TLSQ_REGISTRY", str(HERE / "registry.yaml"))
|
| 15 |
os.environ.setdefault("TLSQ_MODEL_DIR", str(HERE / "models"))
|
|
|
|
| 16 |
else: # repository checkout
|
| 17 |
sys.path.insert(0, str(HERE.parents[1] / "tlsq_infer" / "src"))
|
|
|
|
|
|
|
| 18 |
|
| 19 |
from tlsq_infer import set_num_threads # noqa: E402
|
| 20 |
-
from tlsq_infer.app_ui import build_app # noqa: E402
|
| 21 |
from tlsq_infer.registry import load_registry # noqa: E402
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
reg = load_registry()
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
header = (f"Zero-shot Maxwell capacitance of planar superconducting layouts, from one capacitor to a multi-qubit chip. Default "
|
| 33 |
-
f"(fast tier) model: **{reg.model(default)['title']}** "
|
| 34 |
-
f"({d['median_pct']} % median error, MAE {d['mae_ff']} fF, RMSE {d['rmse_ff']} fF on {where}). "
|
| 35 |
-
f"All paper models: [{hub['paper_models_space']}](https://huggingface.co/spaces/{hub['paper_models_space']}). "
|
| 36 |
-
f"Weights: [{reg.model(default)['repo_id']}](https://huggingface.co/{reg.model(default)['repo_id']}). "
|
| 37 |
-
f"Programmatic use: `gradio_client.Client(\"{space}\")` (see the API page and examples/client.py).")
|
| 38 |
-
demo = build_app(hub["flagship_title"], models, default, header, examples_dir=HERE / "examples", tiers=True)
|
| 39 |
-
demo.queue(default_concurrency_limit=int(os.environ.get("TLSQ_CONCURRENCY", 2)), max_size=32)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
demo.launch(server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"), server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 1 |
+
"""Flagship Space (deploy/GNN_SPACE.md "Flagship Space"): GDS -> full Maxwell capacitance matrix with three tiers (Accurate default,
|
| 2 |
+
Balanced, Fast) retrained on all on-stack data, through geomgnn.flagship. Names, tiers and numbers come from registry.yaml
|
| 3 |
+
(flagship_hub, flagship_tiers)."""
|
| 4 |
|
| 5 |
import os
|
| 6 |
import platform
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
if platform.machine() in ("x86_64", "AMD64"): # OpenBLAS SkylakeX Cholesky kernel segfaulted on a v3 MoM system (LIMITS.md)
|
| 11 |
+
os.environ.setdefault("OPENBLAS_CORETYPE", "Haswell")
|
| 12 |
+
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
|
| 13 |
+
os.environ.setdefault("NUMBA_CACHE_DIR", os.path.join(os.environ.get("TMPDIR", "/tmp"), "numba_cache")) # writable on the Space
|
| 14 |
+
os.environ.pop("TLSQ_GNN_PRIOR_MODE", None) # every tier names its own solver mode (registry flagship_hub.prior_stacks)
|
| 15 |
|
| 16 |
HERE = Path(__file__).resolve().parent
|
| 17 |
+
if (HERE / "geomgnn").exists(): # built bundle (deploy/sync.py --flagship): vendored packages, registry and weights
|
| 18 |
sys.path.insert(0, str(HERE))
|
| 19 |
os.environ.setdefault("TLSQ_REGISTRY", str(HERE / "registry.yaml"))
|
| 20 |
os.environ.setdefault("TLSQ_MODEL_DIR", str(HERE / "models"))
|
| 21 |
+
os.environ.setdefault("TLSQ_OFFLINE", "1")
|
| 22 |
else: # repository checkout
|
| 23 |
sys.path.insert(0, str(HERE.parents[1] / "tlsq_infer" / "src"))
|
| 24 |
+
sys.path.insert(0, str(HERE.parents[1] / "geomgnn" / "src"))
|
| 25 |
+
os.environ.setdefault("TLSQ_REGISTRY", str(HERE.parents[1] / "registry.yaml"))
|
| 26 |
|
| 27 |
from tlsq_infer import set_num_threads # noqa: E402
|
|
|
|
| 28 |
from tlsq_infer.registry import load_registry # noqa: E402
|
| 29 |
|
| 30 |
+
os.environ.setdefault("TLSQ_NUM_THREADS", str(set_num_threads()))
|
| 31 |
+
|
| 32 |
+
from geomgnn.app_flagship import build_app # noqa: E402
|
| 33 |
+
|
| 34 |
reg = load_registry()
|
| 35 |
+
ex = HERE / "examples" if (HERE / "examples").exists() else HERE.parent / "PlanarCapacitanceModel" / "examples"
|
| 36 |
+
if os.environ.get("TLSQ_PRELOAD", "1") != "0": # load every tier (SHA checks) + numba warmup + one small prediction before the queue opens
|
| 37 |
+
from geomgnn import flagship as FS # noqa: E402
|
| 38 |
+
|
| 39 |
+
FS.preload(reg, example=ex / "interdigital.gds")
|
| 40 |
+
demo = build_app(reg["flagship_hub"]["title"], examples_dir=ex)
|
| 41 |
+
demo.queue(default_concurrency_limit=int(os.environ.get("TLSQ_CONCURRENCY", 1)), max_size=32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
demo.launch(server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"), server_port=int(os.environ.get("PORT", 7860)))
|
examples/__pycache__/client.cpython-312.pyc
DELETED
|
Binary file (4.38 kB)
|
|
|
examples/client.py
DELETED
|
@@ -1,42 +0,0 @@
|
|
| 1 |
-
"""Call the Space from Python (pip install gradio_client).
|
| 2 |
-
|
| 3 |
-
python client.py my_layout.gds [--space shanto268/PlanarCapacitanceModel] [--substrate sapphire...] [--thickness 430]
|
| 4 |
-
|
| 5 |
-
Private Spaces need a token: set HF_TOKEN in the environment (never hard-code it). A local app: --space http://127.0.0.1:7860/
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
import argparse
|
| 9 |
-
import json
|
| 10 |
-
import os
|
| 11 |
-
|
| 12 |
-
from gradio_client import Client, handle_file
|
| 13 |
-
|
| 14 |
-
ap = argparse.ArgumentParser()
|
| 15 |
-
ap.add_argument("gds")
|
| 16 |
-
ap.add_argument("--space", default=os.environ.get("TLSQ_SPACE", "shanto268/PlanarCapacitanceModel"))
|
| 17 |
-
ap.add_argument("--ground", default="1/0", help="ground layer/datatype list, e.g. '1/0' or '1/0,5/0'")
|
| 18 |
-
ap.add_argument("--conductors", default="1/10")
|
| 19 |
-
ap.add_argument("--margin-gaps", type=float, default=0.0, help="ground crop margin in conductor-ground gaps (0: auto, < 0: no crop)")
|
| 20 |
-
ap.add_argument("--substrate", default="Si (cryogenic, training)", help="a substrate name of the Space, or 'custom eps_r'")
|
| 21 |
-
ap.add_argument("--eps", type=float, default=11.45, help="eps_r when --substrate 'custom eps_r'")
|
| 22 |
-
ap.add_argument("--thickness", type=float, default=350.0, help="substrate thickness (µm)")
|
| 23 |
-
ap.add_argument("--model", default="fast", help="fast (default: flagship-fast, also multi-qubit chips) / coarse / fine (most accurate; alias accurate), or a registry model name")
|
| 24 |
-
ap.add_argument("--self-from-rows", action="store_true")
|
| 25 |
-
a = ap.parse_args()
|
| 26 |
-
|
| 27 |
-
client = Client(a.space, token=os.environ.get("HF_TOKEN")) if not a.space.startswith("http") else Client(a.space)
|
| 28 |
-
res = client.predict(handle_file(a.gds), a.ground, a.conductors, a.margin_gaps, a.substrate, a.eps, a.thickness, a.model,
|
| 29 |
-
a.self_from_rows, api_name="/predict_gds")
|
| 30 |
-
names = res["names"]
|
| 31 |
-
print("Maxwell capacitance matrix (fF):")
|
| 32 |
-
print(" " + "".join(f"{n:>12s}" for n in names))
|
| 33 |
-
for n, row in zip(names, res["matrix_fF"]):
|
| 34 |
-
print(f"{n:>6s}" + "".join(f"{'--':>12s}" if v is None else f"{v:12.4f}" for v in row))
|
| 35 |
-
e = res["expected_error"]
|
| 36 |
-
print(f"\nmodel {res['model']} (package {res['package_version']}); eps factor {res['eps_correction_factor']:.4f}")
|
| 37 |
-
if "expected_median_pct" in e:
|
| 38 |
-
print(f"expected error: median {e['expected_median_pct']:.2f} %, MAE {e['expected_mae_ff']:.3f} fF, RMSE {e['expected_rmse_ff']:.3f} fF")
|
| 39 |
-
for w in res["warnings"]:
|
| 40 |
-
print("warning:", w)
|
| 41 |
-
with open("capacitance.json", "w") as f:
|
| 42 |
-
json.dump(res, f, indent=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lattice_1q.gds
ADDED
|
Binary file (7.8 kB). View file
|
|
|
examples/lattice_5q.gds
ADDED
|
Binary file (13.8 kB). View file
|
|
|
geomgnn/SOURCES.json
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"stage": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage",
|
| 3 |
+
"note": "frozen geomfast inference stack (FINALIZATION_V1), namespaced under geomgnn; only import lines rewritten",
|
| 4 |
+
"files": {
|
| 5 |
+
"_tlsq/__init__.py": {
|
| 6 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/__init__.py",
|
| 7 |
+
"source_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 8 |
+
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 9 |
+
"rewrites": []
|
| 10 |
+
},
|
| 11 |
+
"_tlsq/geometry.py": {
|
| 12 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/geometry.py",
|
| 13 |
+
"source_sha256": "0bb8d3ead358870a026eebf8e26a212bdf1a838565bd30251a684fab7c97c99b",
|
| 14 |
+
"sha256": "0bb8d3ead358870a026eebf8e26a212bdf1a838565bd30251a684fab7c97c99b",
|
| 15 |
+
"rewrites": []
|
| 16 |
+
},
|
| 17 |
+
"_tlsq/embed/__init__.py": {
|
| 18 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/__init__.py",
|
| 19 |
+
"source_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 20 |
+
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 21 |
+
"rewrites": []
|
| 22 |
+
},
|
| 23 |
+
"_tlsq/embed/proxy.py": {
|
| 24 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/proxy.py",
|
| 25 |
+
"source_sha256": "78d8dffd2fac75632a91ebee151247afe4157716ffb11e624e6b2932c618c86e",
|
| 26 |
+
"sha256": "78d8dffd2fac75632a91ebee151247afe4157716ffb11e624e6b2932c618c86e",
|
| 27 |
+
"rewrites": []
|
| 28 |
+
},
|
| 29 |
+
"_tlsq/embed/fastprior.py": {
|
| 30 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/fastprior.py",
|
| 31 |
+
"source_sha256": "1755668ca197bc56bcc4ef1d23a01464df75ba27e79eb2f447c8f147152cbbf1",
|
| 32 |
+
"sha256": "1755668ca197bc56bcc4ef1d23a01464df75ba27e79eb2f447c8f147152cbbf1",
|
| 33 |
+
"rewrites": []
|
| 34 |
+
},
|
| 35 |
+
"_tlsq/embed/fastprior_loc.py": {
|
| 36 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/fastprior_loc.py",
|
| 37 |
+
"source_sha256": "3642ebac602a0767cad2cdd51e04dc602055c0045022b71f14d93298af943a2b",
|
| 38 |
+
"sha256": "3642ebac602a0767cad2cdd51e04dc602055c0045022b71f14d93298af943a2b",
|
| 39 |
+
"rewrites": []
|
| 40 |
+
},
|
| 41 |
+
"_tlsq/uge/__init__.py": {
|
| 42 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/__init__.py",
|
| 43 |
+
"source_sha256": "2c9ab4995b49e228e84ff19f44c79f944a3f5d0489db0a688ccaf76546995c22",
|
| 44 |
+
"sha256": "2c9ab4995b49e228e84ff19f44c79f944a3f5d0489db0a688ccaf76546995c22",
|
| 45 |
+
"rewrites": []
|
| 46 |
+
},
|
| 47 |
+
"_tlsq/uge/blocks.py": {
|
| 48 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/blocks.py",
|
| 49 |
+
"source_sha256": "c58313f167f7df0ef0de6b321ab6b7214a661aef10b461e2bf90a60999fb4632",
|
| 50 |
+
"sha256": "c58313f167f7df0ef0de6b321ab6b7214a661aef10b461e2bf90a60999fb4632",
|
| 51 |
+
"rewrites": []
|
| 52 |
+
},
|
| 53 |
+
"_tlsq/uge/graph.py": {
|
| 54 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/graph.py",
|
| 55 |
+
"source_sha256": "aeb8c58cf6c1d2169eab8a58fba362634d7489f3414f260a9fc55a02048217ee",
|
| 56 |
+
"sha256": "aeb8c58cf6c1d2169eab8a58fba362634d7489f3414f260a9fc55a02048217ee",
|
| 57 |
+
"rewrites": []
|
| 58 |
+
},
|
| 59 |
+
"_tlsq/uge/localtok.py": {
|
| 60 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/localtok.py",
|
| 61 |
+
"source_sha256": "8b47bd454353709a29ad996243d0f8b4b0d94991ee43715145af41a912dd9319",
|
| 62 |
+
"sha256": "8b47bd454353709a29ad996243d0f8b4b0d94991ee43715145af41a912dd9319",
|
| 63 |
+
"rewrites": [],
|
| 64 |
+
"note": "byte-identical to the stage's top-level localtok.py, which infer.py loads as tlsq.uge.localtok"
|
| 65 |
+
},
|
| 66 |
+
"_tlsq/uge/proxies.py": {
|
| 67 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/proxies.py",
|
| 68 |
+
"source_sha256": "1726d068105f88f2654fd84a07919b7ecd9852cc5f826f6dd812109e6ce55b38",
|
| 69 |
+
"sha256": "1726d068105f88f2654fd84a07919b7ecd9852cc5f826f6dd812109e6ce55b38",
|
| 70 |
+
"rewrites": []
|
| 71 |
+
},
|
| 72 |
+
"_tlsq/uge/scene.py": {
|
| 73 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/scene.py",
|
| 74 |
+
"source_sha256": "f990d3028092ca59e4bbb274bcc181eb52cc7d0c73c158cbc1b23003d0368285",
|
| 75 |
+
"sha256": "f990d3028092ca59e4bbb274bcc181eb52cc7d0c73c158cbc1b23003d0368285",
|
| 76 |
+
"rewrites": []
|
| 77 |
+
},
|
| 78 |
+
"pilot_p2.py": {
|
| 79 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/pilot_p2.py",
|
| 80 |
+
"source_sha256": "c38264c43ab342ba64338892edf23c8abd4b88503b99bce322431efb7db77c77",
|
| 81 |
+
"sha256": "c38264c43ab342ba64338892edf23c8abd4b88503b99bce322431efb7db77c77",
|
| 82 |
+
"rewrites": []
|
| 83 |
+
},
|
| 84 |
+
"infer.py": {
|
| 85 |
+
"source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/infer.py",
|
| 86 |
+
"source_sha256": "f1829b165b1c1abac5e9e8b6e9ccda8bcf91f977c331ee4b408615455e902d09",
|
| 87 |
+
"sha256": "8aa26ec0da20be8158096448390911c380ee0e8b8faeecf21546e2e4f67b344f",
|
| 88 |
+
"rewrites": [
|
| 89 |
+
[
|
| 90 |
+
"W=Path(__file__).resolve().parent;sys.path.insert(0,str(W/'vendor'))",
|
| 91 |
+
"W=Path(__file__).resolve().parent"
|
| 92 |
+
],
|
| 93 |
+
[
|
| 94 |
+
"from pilot_p2 import build_model",
|
| 95 |
+
"from .pilot_p2 import build_model"
|
| 96 |
+
],
|
| 97 |
+
[
|
| 98 |
+
"from tlsq.uge.scene import scene_from_gds,tlsq_namer",
|
| 99 |
+
"from ._tlsq.uge.scene import scene_from_gds,tlsq_namer"
|
| 100 |
+
],
|
| 101 |
+
[
|
| 102 |
+
"from tlsq.uge.graph import scene_graph",
|
| 103 |
+
"from ._tlsq.uge.graph import scene_graph"
|
| 104 |
+
],
|
| 105 |
+
[
|
| 106 |
+
"from tlsq.uge.proxies import CapacitanceMoM",
|
| 107 |
+
"from ._tlsq.uge.proxies import CapacitanceMoM"
|
| 108 |
+
],
|
| 109 |
+
[
|
| 110 |
+
"spec=importlib.util.spec_from_file_location('tlsq.uge.localtok',W/'localtok.py');lt=importlib.util.module_from_spec(spec);sys.modules[spec.name]=lt;spec.loader.exec_module(lt)",
|
| 111 |
+
"from ._tlsq.uge import localtok as lt"
|
| 112 |
+
],
|
| 113 |
+
[
|
| 114 |
+
" from tlsq.embed import proxy,fastprior",
|
| 115 |
+
" from ._tlsq.embed import proxy,fastprior"
|
| 116 |
+
],
|
| 117 |
+
[
|
| 118 |
+
" from tlsq.embed.fastprior_loc import micro_mom_loc",
|
| 119 |
+
" from ._tlsq.embed.fastprior_loc import micro_mom_loc"
|
| 120 |
+
]
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
"fastmom.py": {
|
| 124 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/geomfast/fastmom.py",
|
| 125 |
+
"source_sha256": "8f3642b22bd81325a782ee2a07d0341761d017351d838f484ee375cda025e85c",
|
| 126 |
+
"sha256": "7447474366ddae654100467125c84ddb7fb503a1c9d4fd1264825bde164de951",
|
| 127 |
+
"rewrites": [
|
| 128 |
+
[
|
| 129 |
+
"from tlsq.embed import proxy as P",
|
| 130 |
+
"from ._tlsq.embed import proxy as P"
|
| 131 |
+
]
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
"priorv2.py": {
|
| 135 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/priorv2/priorv2.py",
|
| 136 |
+
"source_sha256": "1a0bdcb71500c7f11e63238afe531c18caabd5c9f6f7aaa73d9b7dcd0ad61f8c",
|
| 137 |
+
"sha256": "64057e92e37a04a0231dfdc34df52a20542f88c2b2a3e8c2cc00a32d9ec1d21b",
|
| 138 |
+
"rewrites": [
|
| 139 |
+
[
|
| 140 |
+
"from tlsq.embed import fastprior as F, proxy as P",
|
| 141 |
+
"from ._tlsq.embed import fastprior as F, proxy as P"
|
| 142 |
+
]
|
| 143 |
+
],
|
| 144 |
+
"note": "prior v2m (agents/priorv2/FINALIZATION_V2.json module_sha256); vendored 2026-10-17"
|
| 145 |
+
},
|
| 146 |
+
"fastprior_pkg.py": {
|
| 147 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/fastprior_pkg/fastprior_pkg.py",
|
| 148 |
+
"source_sha256": "956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde",
|
| 149 |
+
"sha256": "956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde",
|
| 150 |
+
"rewrites": [],
|
| 151 |
+
"note": "gated fast-solve package (agents/fastprior_pkg/RESULTS.md), verbatim; bound with bind('geomgnn'); vendored 2026-10-17"
|
| 152 |
+
},
|
| 153 |
+
"lp_surrogate.py": {
|
| 154 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/learnedprior/lp_surrogate.py",
|
| 155 |
+
"source_sha256": "55804c9349a2ca4a74f3ae821a19c4637dbaf16d25b65c8ee70c746b767d2707",
|
| 156 |
+
"sha256": "55804c9349a2ca4a74f3ae821a19c4637dbaf16d25b65c8ee70c746b767d2707",
|
| 157 |
+
"rewrites": [],
|
| 158 |
+
"note": "learned prior surrogate (agents/learnedprior; HASHES.json lp_surrogate.py), verbatim; fast tier; vendored 2026-10-18"
|
| 159 |
+
},
|
| 160 |
+
"schema_p2d.json": {
|
| 161 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/learnedprior/schema_p2d.json",
|
| 162 |
+
"source_sha256": "17eba5f7b5d01668019df9301507133dfe4e38689acbde5256f3ade2107df8e9",
|
| 163 |
+
"sha256": "17eba5f7b5d01668019df9301507133dfe4e38689acbde5256f3ade2107df8e9",
|
| 164 |
+
"rewrites": [],
|
| 165 |
+
"note": "P2d feature schema read by lp_surrogate.py (HASHES.json schema_p2d.json), verbatim; vendored 2026-10-18"
|
| 166 |
+
},
|
| 167 |
+
"train_mf.py": {
|
| 168 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/multifid/train_mf.py",
|
| 169 |
+
"source_sha256": "d4df1310cf8758d6e5165c514fc0bac497768b7e37da91dd2bede717fd746676",
|
| 170 |
+
"sha256": "d4df1310cf8758d6e5165c514fc0bac497768b7e37da91dd2bede717fd746676",
|
| 171 |
+
"rewrites": [],
|
| 172 |
+
"note": "multifid network (agents/multifid; HASHES.json train_mf.py), verbatim; paper Space cheap-physics arms; vendored 2026-10-19"
|
| 173 |
+
},
|
| 174 |
+
"x1_core.py": {
|
| 175 |
+
"source": "/Users/shanto/LFL/tl_squadds/scripts/v4test/x1_core.py",
|
| 176 |
+
"source_sha256": "f4ee1cb4074eb7a3dc019072722f36d58a320d6bf711af3c66c7a5cbc96499cc",
|
| 177 |
+
"sha256": "a6702ffcbf8b1b03b99340654f89c707825e10f67a7921e52c21a8702edbeaca",
|
| 178 |
+
"rewrites": [
|
| 179 |
+
[
|
| 180 |
+
" from train_mf import build",
|
| 181 |
+
" from .train_mf import build"
|
| 182 |
+
],
|
| 183 |
+
[
|
| 184 |
+
" import lp_surrogate as S",
|
| 185 |
+
" from . import lp_surrogate as S"
|
| 186 |
+
]
|
| 187 |
+
],
|
| 188 |
+
"note": "X1 shared model code (multifid forward, Richardson c0, to_maxwell); paper Space arms; vendored 2026-10-19, import lines rewritten"
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
geomgnn/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""geomgnn: the frozen geomfast GNN (paper_v4; agents/geomfast/FINALIZATION_V1.json) for the GNN Space.
|
| 2 |
+
|
| 3 |
+
The inference code is the frozen geomfast stage, namespaced (`geomgnn.infer`, `geomgnn.pilot_p2`, `geomgnn._tlsq`; only import
|
| 4 |
+
lines rewritten, see SOURCES.json) so it cannot collide with `tlsq_infer` or the repository's `tlsq`. `geomgnn.api` adapts it
|
| 5 |
+
to `tlsq_infer.layout.PreparedLayout` and to the registry entries `gnn_models` (deploy/registry.yaml).
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
__version__ = "0.1.0"
|
geomgnn/_tlsq/__init__.py
ADDED
|
File without changes
|
geomgnn/_tlsq/embed/__init__.py
ADDED
|
File without changes
|
geomgnn/_tlsq/embed/fastprior.py
ADDED
|
@@ -0,0 +1,925 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Local (windowed) MoM prior: PRIOR_SCHEME.md Part III prototype.
|
| 2 |
+
|
| 3 |
+
The Maxwell entry C_ij is defined with every other conductor at 0 V, and a ground plane screens the field, so the
|
| 4 |
+
entries of a conductor are set by its neighbourhood. The layout is partitioned into windows (recursive bisection of
|
| 5 |
+
the conductor sites until the metal edge length inside each window fits a panel budget); a window models its core
|
| 6 |
+
conductors (whose matrix rows it provides), the halo conductors within R_g of the core as separate nets at 0 V, the
|
| 7 |
+
ground plane cropped by a margin around core + halo, and every farther conductor filled into the ground. Each window
|
| 8 |
+
is a strip-merged conductor-anchored mesh (the flagship mesh family, PRIOR_SCHEME.md Part II) solved densely with the
|
| 9 |
+
exact near field and one right-hand side per core conductor, optionally Richardson-extrapolated (h and 2 h). Windows
|
| 10 |
+
that are translation copies of one another are solved once (geometric hash). Cost: O(number of distinct windows) x
|
| 11 |
+
(a bounded dense solve), independent of the chip size; a layout that fits one window is one global solve.
|
| 12 |
+
Pairs never co-windowed (farther than R_g apart) are not predicted: NaN in C and True in far_mask.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import hashlib
|
| 18 |
+
import time
|
| 19 |
+
from concurrent.futures import ProcessPoolExecutor
|
| 20 |
+
from multiprocessing import get_context
|
| 21 |
+
|
| 22 |
+
import numpy as np
|
| 23 |
+
import shapely
|
| 24 |
+
from scipy.spatial import cKDTree
|
| 25 |
+
|
| 26 |
+
from ..geometry import sample_boundary
|
| 27 |
+
from ..uge.blocks import _kratio
|
| 28 |
+
from . import proxy as P
|
| 29 |
+
|
| 30 |
+
PANELS_PER_EDGE_H = 0.75 # merged panels per (um of metal edge / h), measured on grid_qubit_1 (18.6k at h = 2 um, 50 mm of edges)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _valid(g):
|
| 34 |
+
g = shapely.make_valid(g)
|
| 35 |
+
parts = [p for p in getattr(g, "geoms", [g]) if p.geom_type in ("Polygon", "MultiPolygon")]
|
| 36 |
+
return shapely.union_all(parts) if parts else shapely.Polygon()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def gap_of(metals: list) -> float:
|
| 40 |
+
tree = shapely.STRtree(metals)
|
| 41 |
+
d = np.inf
|
| 42 |
+
for i, g in enumerate(metals):
|
| 43 |
+
j, dist = tree.query_nearest(g, return_distance=True, exclusive=True, all_matches=False)
|
| 44 |
+
if len(dist):
|
| 45 |
+
d = min(d, float(dist[0]))
|
| 46 |
+
return d
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def within(geoms: list, targets: list, R: float) -> np.ndarray:
|
| 50 |
+
"""Boolean (len(geoms),): geometry within distance R of any target (STRtree, O(n log n))."""
|
| 51 |
+
out = np.zeros(len(geoms), bool)
|
| 52 |
+
if not targets or not geoms:
|
| 53 |
+
return out
|
| 54 |
+
tree = shapely.STRtree(geoms)
|
| 55 |
+
for t in targets:
|
| 56 |
+
out[tree.query(t, predicate="dwithin", distance=R)] = True
|
| 57 |
+
return out
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def plan_windows(G, conds: list, h: float, R_g: float, margin: float, panel_budget: int, global_budget: int | None = None,
|
| 61 |
+
split_gain: float = 0.75, level_rings: tuple = ()) -> list:
|
| 62 |
+
"""Windows (dicts: core, halo, box). One global window (every conductor in the core, no halo, no crop) when the
|
| 63 |
+
metal edge length of the whole layout fits global_budget panels; otherwise recursive bisection of the conductor
|
| 64 |
+
sites (bbox centres) until the estimated merged panel count of the window fits panel_budget. The estimate weights
|
| 65 |
+
the metal edge length by the grading level it will be meshed at (1, 1/2, 1/4, ... beyond each of level_rings um
|
| 66 |
+
from the core; ground edges beyond R_g of the members are filled and count 0), so the budget measures the core
|
| 67 |
+
content, not the fixed halo + margin overhead. Each split is snapped to the widest coordinate gap between sites in
|
| 68 |
+
the central half of the sorted order (cores follow the natural cell structure); a split whose child is not below
|
| 69 |
+
split_gain x the parent (a dense cluster) is not made and the window is solved whole (H-matrix above dense_max)."""
|
| 70 |
+
sites = np.array([[(b[0] + b[2]) / 2, (b[1] + b[3]) / 2] for b in shapely.bounds(np.array(conds, dtype=object))])
|
| 71 |
+
per_panel = h / PANELS_PER_EDGE_H
|
| 72 |
+
all_idx = np.arange(len(conds))
|
| 73 |
+
L_all = G.length + sum(c.length for c in conds)
|
| 74 |
+
if L_all <= (global_budget if global_budget is not None else panel_budget) * per_panel:
|
| 75 |
+
return [{"core": all_idx, "halo": np.zeros(0, int), "box": None, "n_core": len(conds), "n_halo": 0}]
|
| 76 |
+
out = []
|
| 77 |
+
Gb = G.boundary
|
| 78 |
+
gtree = shapely.STRtree(list(getattr(Gb, "geoms", [Gb])))
|
| 79 |
+
rings = list(level_rings) + [np.inf]
|
| 80 |
+
|
| 81 |
+
def window_of(core):
|
| 82 |
+
halo = within(conds, [conds[i] for i in core], R_g)
|
| 83 |
+
halo[core] = False
|
| 84 |
+
halo = np.nonzero(halo)[0]
|
| 85 |
+
x0, y0, x1, y1 = shapely.union_all([conds[i] for i in np.concatenate([core, halo])]).bounds
|
| 86 |
+
box = (x0 - margin, y0 - margin, x1 + margin, y1 + margin)
|
| 87 |
+
return halo, box
|
| 88 |
+
|
| 89 |
+
def est_panels(core, halo, box):
|
| 90 |
+
cg = shapely.union_all([conds[i] for i in core])
|
| 91 |
+
members = shapely.union_all([conds[i] for i in np.concatenate([core, halo])])
|
| 92 |
+
b = shapely.box(*box)
|
| 93 |
+
gl = shapely.union_all([gtree.geometries[k] for k in gtree.query(b)]) if len(gtree.geometries) else Gb
|
| 94 |
+
edges = shapely.union_all([shapely.intersection(gl, b).intersection(members.buffer(R_g)), members.boundary])
|
| 95 |
+
L, prev, w = 0.0, None, 1.0
|
| 96 |
+
for r in rings:
|
| 97 |
+
ring = cg.buffer(r) if np.isfinite(r) else b
|
| 98 |
+
if prev is not None:
|
| 99 |
+
ring = ring.difference(prev)
|
| 100 |
+
L += w * shapely.intersection(edges, ring).length
|
| 101 |
+
prev, w = (cg.buffer(r) if np.isfinite(r) else b), w / 2
|
| 102 |
+
return L / per_panel
|
| 103 |
+
|
| 104 |
+
def rec(idx, n_parent=None):
|
| 105 |
+
halo, box = window_of(idx)
|
| 106 |
+
n = est_panels(idx, halo, box)
|
| 107 |
+
if len(idx) == 1 or n <= panel_budget or (n_parent is not None and n > split_gain * n_parent):
|
| 108 |
+
out.append({"core": idx, "halo": halo, "box": box, "est_panels": n})
|
| 109 |
+
return
|
| 110 |
+
s = sites[idx]
|
| 111 |
+
ax = int(np.argmax(s.max(0) - s.min(0)))
|
| 112 |
+
order = idx[np.argsort(s[:, ax], kind="stable")]
|
| 113 |
+
x = sites[order, ax]
|
| 114 |
+
lo = max(1, len(order) // 4)
|
| 115 |
+
hi = max(lo + 1, min(len(order), (3 * len(order)) // 4 + 1))
|
| 116 |
+
gaps = x[lo:hi] - x[lo - 1:hi - 1]
|
| 117 |
+
m = lo + int(np.argmax(gaps))
|
| 118 |
+
rec(order[:m], n), rec(order[m:], n)
|
| 119 |
+
|
| 120 |
+
rec(all_idx)
|
| 121 |
+
for w in out:
|
| 122 |
+
w["n_core"], w["n_halo"] = len(w["core"]), len(w["halo"])
|
| 123 |
+
return out
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def window_metals(G, conds: list, w: dict, fill_um: float = 250.0, keep_um: float = 0.0) -> list:
|
| 127 |
+
"""[ground, core..., halo...] of window w: ground = chip ground cropped to the box plus every farther conductor
|
| 128 |
+
inside the box as ground metal (0 V), whose etched surroundings within fill_um are filled in where they are more
|
| 129 |
+
than keep_um from any member (their edges leave the mesh; nothing changes within keep_um of a member)."""
|
| 130 |
+
members = np.concatenate([w["core"], w["halo"]]).astype(int)
|
| 131 |
+
if w["box"] is None:
|
| 132 |
+
return [G] + [conds[i] for i in members]
|
| 133 |
+
box = shapely.box(*w["box"])
|
| 134 |
+
far = np.setdiff1d(np.nonzero(within(conds, [box], 0.0))[0], members)
|
| 135 |
+
Gw = shapely.intersection(G, box)
|
| 136 |
+
if len(far):
|
| 137 |
+
chip = shapely.box(*G.bounds)
|
| 138 |
+
farg = shapely.union_all([conds[i] for i in far])
|
| 139 |
+
fill = shapely.intersection(shapely.intersection(chip.difference(G), farg.buffer(fill_um)), box)
|
| 140 |
+
fill = fill.difference(shapely.union_all([conds[i] for i in members]).buffer(keep_um))
|
| 141 |
+
Gw = shapely.union_all([Gw, shapely.intersection(farg, box), fill])
|
| 142 |
+
return [_valid(Gw)] + [conds[i] for i in members]
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def anchored_cells_graded(metals: list, h_f: float, core, levels: tuple, grade: float = 2.0, majority: bool = False):
|
| 146 |
+
"""`proxy.anchored_cells` with the finest panel graded by the distance to the core conductors: h_f within levels[0]
|
| 147 |
+
of `core`, 2 h_f up to levels[1], 4 h_f up to levels[2], ... (same lattice; with levels=() identical to the plain
|
| 148 |
+
mesh). Returns centres (n, 2), sizes (n, 2), owner (n,)."""
|
| 149 |
+
edges = shapely.union_all([g.boundary for g in metals])
|
| 150 |
+
sig = metals[1:] if len(metals) > 1 else metals
|
| 151 |
+
ax, ay = shapely.union_all(sig).bounds[:2]
|
| 152 |
+
X0, Y0, X1, Y1 = shapely.union_all(metals).bounds
|
| 153 |
+
span = max(X1 - X0, Y1 - Y0)
|
| 154 |
+
K = max(0, int(np.ceil(np.log2(max(span / 16, h_f) / h_f))))
|
| 155 |
+
h0 = h_f * 2**K
|
| 156 |
+
ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0))
|
| 157 |
+
ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0))
|
| 158 |
+
gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1))
|
| 159 |
+
xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0)
|
| 160 |
+
gq = P._Geoms(list(metals) + [edges, core])
|
| 161 |
+
lv = np.asarray(levels, float)
|
| 162 |
+
tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool)
|
| 163 |
+
out_c, out_h, out_o = [], [], []
|
| 164 |
+
while len(xs):
|
| 165 |
+
boxes = shapely.box(xs, ys, xs + h, ys + h)
|
| 166 |
+
cov, hit, inn, pcs = P._cover(gq, boxes, h, tst, inn)
|
| 167 |
+
keep = cov.sum(0) > 1e-3
|
| 168 |
+
xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep]
|
| 169 |
+
pcs = [p[keep] for p in pcs]
|
| 170 |
+
cx, cy = xs + h / 2, ys + h / 2
|
| 171 |
+
full = cov.max(0) > 0.999
|
| 172 |
+
if len(lv):
|
| 173 |
+
maybe = h <= h_f * 2 ** len(lv) * 1.0001 # cells that can be at their last level
|
| 174 |
+
hl = np.zeros(len(xs))
|
| 175 |
+
if maybe.any():
|
| 176 |
+
dc = P._distance(gq, len(metals) + 1, boxes[maybe])
|
| 177 |
+
hl[maybe] = h_f * 2.0 ** np.searchsorted(lv, dc, side="right")
|
| 178 |
+
last = h <= hl * 1.0001
|
| 179 |
+
else:
|
| 180 |
+
last = h <= h_f * 1.0001
|
| 181 |
+
d = np.full(len(xs), np.inf)
|
| 182 |
+
far = full & ~last
|
| 183 |
+
d[far] = P._distance(gq, len(metals), boxes[far])
|
| 184 |
+
leaf = full & (last | (h <= grade * d + 1e-12))
|
| 185 |
+
out_c.append(np.stack([cx[leaf], cy[leaf]], 1))
|
| 186 |
+
out_h.append(np.stack([h[leaf], h[leaf]], 1))
|
| 187 |
+
out_o.append(cov[:, leaf].argmax(0))
|
| 188 |
+
cut = last & ~leaf
|
| 189 |
+
if majority and cut.any():
|
| 190 |
+
take = cut & (cov.max(0) >= 0.5)
|
| 191 |
+
out_c.append(np.stack([cx[take], cy[take]], 1))
|
| 192 |
+
out_h.append(np.stack([h[take], h[take]], 1))
|
| 193 |
+
out_o.append(cov[:, take].argmax(0))
|
| 194 |
+
cut = cut & False
|
| 195 |
+
if cut.any():
|
| 196 |
+
gs = gq.get()
|
| 197 |
+
for k, g in enumerate(gs[:len(metals)]):
|
| 198 |
+
shapely.prepare(g)
|
| 199 |
+
sel = np.nonzero(cut & (cov[k] > 1e-3))[0]
|
| 200 |
+
if not len(sel):
|
| 201 |
+
continue
|
| 202 |
+
pcs_k = pcs[k][sel]
|
| 203 |
+
bb = shapely.bounds(pcs_k)
|
| 204 |
+
cen = shapely.get_coordinates(shapely.centroid(pcs_k))
|
| 205 |
+
inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1])
|
| 206 |
+
if not inside.all():
|
| 207 |
+
cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside]))
|
| 208 |
+
out_c.append(cen)
|
| 209 |
+
out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6))
|
| 210 |
+
out_o.append(np.full(len(sel), k))
|
| 211 |
+
gq.put(gs)
|
| 212 |
+
split = ~leaf & ~last
|
| 213 |
+
hs = h[split] / 2
|
| 214 |
+
xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs])
|
| 215 |
+
ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs])
|
| 216 |
+
h = np.concatenate([hs] * 4)
|
| 217 |
+
tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4)
|
| 218 |
+
return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o)
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def strip_merge(c, wh, o, metals, h_f: float, lmax_mult: float = 16.0, grade: float = 1.0):
|
| 222 |
+
"""`proxy.strip_merge` with the strip length bounded by lmax_mult x the panel's own lattice level (max(width across
|
| 223 |
+
the edge, h_f), so coarser regions get proportionally longer strips) and by grade x the distance to the nearest
|
| 224 |
+
polygon vertex (coarse square cells therefore merge into longer strips than in proxy.strip_merge)."""
|
| 225 |
+
lines, S, verts = P._segments(metals)
|
| 226 |
+
near = shapely.STRtree(lines).nearest(shapely.points(c))
|
| 227 |
+
d = S[near, 1] - S[near, 0]
|
| 228 |
+
horiz = np.abs(d[:, 0]) >= np.abs(d[:, 1])
|
| 229 |
+
dv, _ = P.cKDTree(verts).query(c, workers=-1)
|
| 230 |
+
allow = np.minimum(lmax_mult * np.maximum(wh.min(1), h_f), grade * dv)
|
| 231 |
+
out_c, out_wh, out_o = [], [], []
|
| 232 |
+
for axis, mask in ((0, horiz), (1, ~horiz)):
|
| 233 |
+
if not mask.any():
|
| 234 |
+
continue
|
| 235 |
+
idx = np.nonzero(mask)[0]
|
| 236 |
+
other = 1 - axis
|
| 237 |
+
key = np.stack([o[idx], np.round(c[idx, other], 4), np.round(wh[idx, other], 4)], 1)
|
| 238 |
+
order = np.lexsort([c[idx, axis], key[:, 2], key[:, 1], key[:, 0]])
|
| 239 |
+
idx, key = idx[order], key[order]
|
| 240 |
+
same = np.all(key[1:] == key[:-1], 1)
|
| 241 |
+
lo, hi = c[idx, axis] - wh[idx, axis] / 2, c[idx, axis] + wh[idx, axis] / 2
|
| 242 |
+
adjacent = same & (np.abs(hi[:-1] - lo[1:]) < 1e-6)
|
| 243 |
+
start = 0
|
| 244 |
+
while start < len(idx):
|
| 245 |
+
end, length, cap = start, wh[idx[start], axis], allow[idx[start]]
|
| 246 |
+
while end + 1 < len(idx) and adjacent[end] and length + wh[idx[end + 1], axis] <= min(cap, allow[idx[end + 1]]):
|
| 247 |
+
end += 1
|
| 248 |
+
length += wh[idx[end], axis]
|
| 249 |
+
cap = min(cap, allow[idx[end]])
|
| 250 |
+
cc, ww = np.zeros(2), np.zeros(2)
|
| 251 |
+
cc[axis], cc[other] = 0.5 * (lo[start] + hi[end]), c[idx[start], other]
|
| 252 |
+
ww[axis], ww[other] = hi[end] - lo[start], wh[idx[start], other]
|
| 253 |
+
out_c.append(cc)
|
| 254 |
+
out_wh.append(ww)
|
| 255 |
+
out_o.append(o[idx[start]])
|
| 256 |
+
start = end + 1
|
| 257 |
+
return np.array(out_c), np.array(out_wh), np.array(out_o)
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
def solve_window_rows(metals: list, n_core: int, h: float, eps_r: float = 11.45, lmax_mult: float = 16.0, merge_grade: float = 1.0,
|
| 261 |
+
grade: float = 2.0, rich: bool = True, dense_max: int = 12000, levels: tuple = ()) -> tuple:
|
| 262 |
+
"""Rows of the Maxwell matrix for the core conductors (metals[1:1+n_core]): (rows (n_core, len(metals)), info).
|
| 263 |
+
levels: distances (um) from the core beyond which the finest panel doubles (core-graded mesh); () = uniform."""
|
| 264 |
+
info = {}
|
| 265 |
+
core = shapely.union_all(metals[1:1 + n_core])
|
| 266 |
+
|
| 267 |
+
def one(hh, tag, majority=False):
|
| 268 |
+
t = time.perf_counter()
|
| 269 |
+
raw = anchored_cells_graded(metals, hh, core, levels, grade, majority=majority)
|
| 270 |
+
c, wh, o = strip_merge(*raw, metals, hh, lmax_mult, merge_grade)
|
| 271 |
+
info[f"mesh_s_{tag}"], info[f"raw_{tag}"], info[f"panels_{tag}"] = time.perf_counter() - t, len(raw[0]), len(c)
|
| 272 |
+
info[f"share_{tag}"] = [float(np.mean(o == 0)), float(np.mean((o >= 1) & (o <= n_core))), float(np.mean(o > n_core))]
|
| 273 |
+
t = time.perf_counter()
|
| 274 |
+
B = (o[:, None] == np.arange(1, 1 + n_core)[None, :]).astype(float)
|
| 275 |
+
if len(c) <= dense_max:
|
| 276 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 277 |
+
info[f"assemble_s_{tag}"] = time.perf_counter() - t
|
| 278 |
+
t = time.perf_counter()
|
| 279 |
+
try:
|
| 280 |
+
Q = P._chol_solve(A, B)
|
| 281 |
+
except np.linalg.LinAlgError: # cut panels of different nets overlap: whole finest cells by majority (as the flagship)
|
| 282 |
+
if majority:
|
| 283 |
+
raise
|
| 284 |
+
del A
|
| 285 |
+
info[f"majority_{tag}"] = True
|
| 286 |
+
return one(hh, tag, majority=True)
|
| 287 |
+
info[f"solver_{tag}"] = "dense"
|
| 288 |
+
else:
|
| 289 |
+
H = P._HMatrix(c, wh, eps_r, P.HMAT["leaf"], P.HMAT["eta"], P.HMAT["eps"])
|
| 290 |
+
info[f"assemble_s_{tag}"] = time.perf_counter() - t
|
| 291 |
+
t = time.perf_counter()
|
| 292 |
+
Xp, _, _ = H.pcg(B[H.perm], P.HMAT["tol"], P.HMAT["maxit"])
|
| 293 |
+
Q = np.empty_like(Xp)
|
| 294 |
+
Q[H.perm] = Xp
|
| 295 |
+
info[f"solver_{tag}"] = "H-matrix PCG"
|
| 296 |
+
info[f"solve_s_{tag}"] = time.perf_counter() - t
|
| 297 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) # (n_core, n_metals): charge on a with core k at 1 V
|
| 298 |
+
|
| 299 |
+
Rf = one(h, "f")
|
| 300 |
+
if not rich:
|
| 301 |
+
return Rf, info
|
| 302 |
+
Rc = one(2 * h, "c")
|
| 303 |
+
return 2 * Rf - Rc, info
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
def canonical_hash(metals: list, n_core: int, h: float) -> str:
|
| 307 |
+
"""Translation-invariant key of a window (lattice anchored to the signal bbox, coordinates rounded to 0.1 nm)."""
|
| 308 |
+
x0, y0 = shapely.union_all(metals[1:]).bounds[:2]
|
| 309 |
+
tr = [shapely.transform(g, lambda xy: np.round(xy - [x0, y0], 4)) for g in metals]
|
| 310 |
+
hsh = hashlib.sha1(f"{n_core}:{h:.9g}:".encode())
|
| 311 |
+
for g in tr:
|
| 312 |
+
hsh.update(shapely.to_wkb(g))
|
| 313 |
+
return hsh.hexdigest()
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def _job(args):
|
| 317 |
+
wi, metals, n_core, h, kw = args
|
| 318 |
+
rows, info = solve_window_rows(metals, n_core, h, **kw)
|
| 319 |
+
return wi, rows, info
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def local_prior(G, conds: list, eps_r: float = 11.45, cells_per_gap: float = 3.0, rich: bool = True, panel_budget: int = 12000,
|
| 323 |
+
halo_gaps: float = 100.0, margin_gaps: float = 100.0, margin_min_um: float = 150.0, fill_um: float = 250.0,
|
| 324 |
+
lmax_mult: float = 16.0, merge_grade: float = 1.0, workers: int = 1, cache: bool = True, dense_max: int = 12000,
|
| 325 |
+
global_budget: int | None = None, level_gaps: tuple = ()) -> tuple:
|
| 326 |
+
"""Local prior of a layout (ground G, conductors conds, never merged): (C (N+1, N+1) fF with NaN for far pairs,
|
| 327 |
+
far_mask, info). Finest panel h = gap / cells_per_gap; with rich=True the Richardson pair is (h, 2 h). Windows
|
| 328 |
+
are solved in parallel on `workers` processes."""
|
| 329 |
+
t_all = time.perf_counter()
|
| 330 |
+
metals_all = [G] + list(conds)
|
| 331 |
+
gap = gap_of(metals_all)
|
| 332 |
+
h = gap / cells_per_gap
|
| 333 |
+
R_g, margin = halo_gaps * gap, max(margin_gaps * gap, margin_min_um)
|
| 334 |
+
t = time.perf_counter()
|
| 335 |
+
wins = plan_windows(G, conds, h, R_g, margin, panel_budget, global_budget, level_rings=tuple(float(g) * gap for g in level_gaps))
|
| 336 |
+
t_plan = time.perf_counter() - t
|
| 337 |
+
t = time.perf_counter()
|
| 338 |
+
mats = [window_metals(G, conds, w, fill_um, keep_um=R_g) for w in wins]
|
| 339 |
+
keys = [canonical_hash(m, w["n_core"], h) if cache else str(i) for i, (m, w) in enumerate(zip(mats, wins))]
|
| 340 |
+
t_build = time.perf_counter() - t
|
| 341 |
+
uniq = {}
|
| 342 |
+
for i, k in enumerate(keys):
|
| 343 |
+
uniq.setdefault(k, i)
|
| 344 |
+
kw = dict(eps_r=eps_r, lmax_mult=lmax_mult, merge_grade=merge_grade, rich=rich, dense_max=dense_max,
|
| 345 |
+
levels=tuple(float(g) * gap for g in level_gaps))
|
| 346 |
+
jobs = [(i, mats[i], wins[i]["n_core"], h, kw) for i in uniq.values()]
|
| 347 |
+
t = time.perf_counter()
|
| 348 |
+
if workers > 1 and len(jobs) > 1:
|
| 349 |
+
with ProcessPoolExecutor(workers, mp_context=get_context("fork")) as ex:
|
| 350 |
+
res = {wi: (rows, info) for wi, rows, info in ex.map(_job, jobs)}
|
| 351 |
+
else:
|
| 352 |
+
res = {wi: (rows, info) for wi, rows, info in map(_job, jobs)}
|
| 353 |
+
t_solve = time.perf_counter() - t
|
| 354 |
+
N = len(conds) + 1
|
| 355 |
+
acc, cnt = np.zeros((N, N)), np.zeros((N, N))
|
| 356 |
+
for i, w in enumerate(wins):
|
| 357 |
+
rows, _ = res[uniq[keys[i]]]
|
| 358 |
+
idx = np.concatenate([[0], w["core"] + 1, w["halo"] + 1]).astype(int)
|
| 359 |
+
for k, ci in enumerate(w["core"] + 1):
|
| 360 |
+
acc[ci, idx] += rows[k]
|
| 361 |
+
cnt[ci, idx] += 1
|
| 362 |
+
C = np.full((N, N), np.nan)
|
| 363 |
+
have = cnt > 0
|
| 364 |
+
C[have] = acc[have] / cnt[have]
|
| 365 |
+
# symmetrise: mean of the two estimates when both exist, the one available otherwise; ground row from the columns
|
| 366 |
+
S = np.where(have & have.T, 0.5 * (np.nan_to_num(C) + np.nan_to_num(C.T)), np.where(have, C, C.T))
|
| 367 |
+
S[0, 0] = np.nan
|
| 368 |
+
far = ~(have | have.T)
|
| 369 |
+
np.fill_diagonal(far, False)
|
| 370 |
+
infos = [res[j][1] for j in uniq.values()]
|
| 371 |
+
info = {"n_windows": len(wins), "n_unique": len(uniq), "h_um": h, "min_gap_um": gap, "R_g_um": R_g, "margin_um": margin,
|
| 372 |
+
"panels_f": [i["panels_f"] for i in infos], "est_panels": [w.get("est_panels", 0) for w in wins], "raw_f": [i["raw_f"] for i in infos],
|
| 373 |
+
"panels_c": [i.get("panels_c", 0) for i in infos], "cores": [w["n_core"] for w in wins], "halos": [w["n_halo"] for w in wins],
|
| 374 |
+
"solvers": sorted({i["solver_f"] for i in infos}), "n_far_pairs": int(far[np.triu_indices(N, 1)].sum()),
|
| 375 |
+
"mesh_s": sum(i["mesh_s_f"] + i.get("mesh_s_c", 0.0) for i in infos),
|
| 376 |
+
"assemble_s": sum(i["assemble_s_f"] + i.get("assemble_s_c", 0.0) for i in infos),
|
| 377 |
+
"solve_s": sum(i["solve_s_f"] + i.get("solve_s_c", 0.0) for i in infos),
|
| 378 |
+
"level_gaps": list(level_gaps), "plan_s": t_plan, "build_s": t_build, "windows_wall_s": t_solve, "total_s": time.perf_counter() - t_all, "rich": rich}
|
| 379 |
+
return S, far, info
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
# ---------------------------------------------------------------- flagship-fast: the cheap prior (PRIOR_SCHEME.md Part III, sections 12 and 16)
|
| 383 |
+
# Hybrid H = micro-MoM (`micro_mom`: the flagship mesh family at a fixed panel budget per window, dense Cholesky) for self,
|
| 384 |
+
# ground and co-windowed pairs + the closed-form edge-coupling prior (`formula_prior`) for pairs never co-windowed.
|
| 385 |
+
# Mirrored verbatim (function sources) in deploy/tlsq_infer/src/tlsq_infer/_vendor/fastprior.py.
|
| 386 |
+
|
| 387 |
+
# formula constants: frozen 2026-10-09, not tuned on labels
|
| 388 |
+
FORMULA_STEP_UM = 0.25 # boundary sample step (as the pair tokens)
|
| 389 |
+
FORMULA_R_GAPS = 150.0 # pairs farther apart than this (in smallest gaps) use the bounding-box form
|
| 390 |
+
FORMULA_SCREEN_UM = 1.0 # the screening factor is evaluated every FORMULA_SCREEN_UM of boundary and held in between
|
| 391 |
+
LITE_REFINE = 3 # rejected window solves: refine h by 2 up to this many times before majority-owned cells
|
| 392 |
+
LITE_LEVEL_GAPS = (10.0, 20.0, 40.0, 80.0) # windows of large layouts: finest panel within 10 gaps of the core, doubling beyond 10/20/40/80
|
| 393 |
+
LITE_WINDOW_BUDGET = 2.0 # windows of large layouts: panel budget = this x the budget of the one-window (component) prior
|
| 394 |
+
LITE_WINDOW_PPG = [None] # windows of large layouts: panels per smallest gap of the finest level instead of a budget (None = budget; PRIOR_SCHEME.md 14.4)
|
| 395 |
+
LITE_MAX_CORE = [None] # windows of large layouts: conductors per core (None = the max_core argument; PRIOR_SCHEME.md 14.4.2)
|
| 396 |
+
LITE_GROWTH = 2.0 # merged panels per halving of h (measured 1.6-2.2 on the training families)
|
| 397 |
+
ALPHA_GAP_SHARE = 0.52 # dipole far pairs: perimeter x gap band share of the effective area; fit 0.519 (median log-ratio 0 vs the global gap/3 MoM, 68 cross-pocket pairs > 50 gaps of grid_qubit_1; PRIOR_SCHEME.md 14.2)
|
| 398 |
+
DIPOLE_K = 8 # nearest boundary samples searched for the nearest other conductor (gbar)
|
| 399 |
+
ROW_SUM_EPS = 0.0143 # training median of 1 - sum_j |C_ij| / C_ii (self = row sum / (1 - eps))
|
| 400 |
+
EPS0_FF_UM = 8.8541878128e-3
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def plan_cores(sites: np.ndarray, max_core: int = 8) -> list:
|
| 404 |
+
"""Cores (index arrays) by recursive bisection of the conductor sites (n, 2) along the longer extent until each core
|
| 405 |
+
has <= max_core conductors; each split is snapped to the widest coordinate gap between sites in the central half
|
| 406 |
+
of the sorted order (ties broken by the other coordinate, so the cores do not depend on the conductor order)."""
|
| 407 |
+
out = []
|
| 408 |
+
|
| 409 |
+
def rec(idx):
|
| 410 |
+
if len(idx) <= max_core:
|
| 411 |
+
out.append(idx)
|
| 412 |
+
return
|
| 413 |
+
s = sites[idx]
|
| 414 |
+
ax = int(np.argmax(s.max(0) - s.min(0)))
|
| 415 |
+
order = idx[np.lexsort((s[:, 1 - ax], s[:, ax]))]
|
| 416 |
+
x = sites[order, ax]
|
| 417 |
+
lo = max(1, len(order) // 4)
|
| 418 |
+
hi = max(lo + 1, min(len(order), (3 * len(order)) // 4 + 1))
|
| 419 |
+
m = lo + int(np.argmax(x[lo:hi] - x[lo - 1:hi - 1]))
|
| 420 |
+
rec(order[:m]), rec(order[m:])
|
| 421 |
+
|
| 422 |
+
rec(np.arange(len(sites)))
|
| 423 |
+
return out
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
def _rot90(k: int):
|
| 427 |
+
"""Exact rotation of coordinates by k x 90 degrees (swaps and sign flips only)."""
|
| 428 |
+
def f(xy):
|
| 429 |
+
x, y = xy[:, 0], xy[:, 1]
|
| 430 |
+
return np.stack([(x, y), (-y, x), (-x, -y), (y, -x)][k % 4], 1)
|
| 431 |
+
return f
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def canonical_frames(G, conds: list) -> list:
|
| 435 |
+
"""The layout in canonical frames, so that the lattice-anchored mesh (and hence the prior) does not depend on the
|
| 436 |
+
position, the 90-degree orientation or the conductor order of the input: rotations by k x 90 degrees whose
|
| 437 |
+
rotation-invariant key (signal bounding box, sorted conductor centroids and convex-hull vertices about the box
|
| 438 |
+
centre, ground bounds, rounded to 1 nm) is smallest (several when the layout is symmetric), translation of the signal
|
| 439 |
+
bounding box corner to the origin, coordinates rounded to 1e-6 um, collinear vertices removed (`shapely.simplify(0)`),
|
| 440 |
+
rings in normal form (`shapely.normalize`), conductors sorted by (centroid, area). Returns [(G, conds, order)]:
|
| 441 |
+
conds[k] of a frame is input conductor order[k]."""
|
| 442 |
+
keys = []
|
| 443 |
+
for k in range(4):
|
| 444 |
+
f = _rot90(k)
|
| 445 |
+
sig = [shapely.transform(c, f) for c in conds]
|
| 446 |
+
b = shapely.bounds(np.array(sig, dtype=object))
|
| 447 |
+
x0, y0, x1, y1 = b[:, 0].min(), b[:, 1].min(), b[:, 2].max(), b[:, 3].max()
|
| 448 |
+
cen = shapely.get_coordinates(shapely.centroid(np.array(sig, dtype=object))) - [(x0 + x1) / 2, (y0 + y1) / 2]
|
| 449 |
+
hull = shapely.get_coordinates(shapely.convex_hull(shapely.union_all(sig)))[:-1] - [(x0 + x1) / 2, (y0 + y1) / 2]
|
| 450 |
+
gb = np.round(np.array(shapely.transform(G, f).bounds) - [(x0 + x1) / 2, (y0 + y1) / 2] * 2, 3)
|
| 451 |
+
key = ((round(x1 - x0, 3), round(y1 - y0, 3)) + tuple(sorted(map(tuple, np.round(cen, 3).tolist())))
|
| 452 |
+
+ tuple(sorted(map(tuple, np.round(hull, 3).tolist()))) + tuple(gb.tolist()))
|
| 453 |
+
keys.append((key, k, x0, y0))
|
| 454 |
+
best = min(kk[0] for kk in keys)
|
| 455 |
+
out = []
|
| 456 |
+
for key, k, x0, y0 in keys:
|
| 457 |
+
if key != best:
|
| 458 |
+
continue
|
| 459 |
+
f = _rot90(k)
|
| 460 |
+
|
| 461 |
+
def to(g, f=f, x0=x0, y0=y0):
|
| 462 |
+
return shapely.normalize(shapely.simplify(shapely.transform(g, lambda xy: np.round(f(xy) - [x0, y0], 6)), 0.0))
|
| 463 |
+
|
| 464 |
+
cs = [to(c) for c in conds]
|
| 465 |
+
cen = shapely.get_coordinates(shapely.centroid(np.array(cs, dtype=object)))
|
| 466 |
+
order = np.lexsort((np.round([c.area for c in cs], 6), np.round(cen[:, 1], 6), np.round(cen[:, 0], 6)))
|
| 467 |
+
out.append((to(G), [cs[i] for i in order], order))
|
| 468 |
+
return out
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
def _gap_size(metals: list) -> tuple:
|
| 472 |
+
"""(smallest gap, layout size) exactly as mom_capacitance_gds_flagship computes them (all pairs; small layouts)."""
|
| 473 |
+
bbox = shapely.union_all(metals).bounds
|
| 474 |
+
size = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
|
| 475 |
+
gap = min(shapely.distance(metals[i], metals[j]) for i in range(len(metals)) for j in range(i + 1, len(metals)))
|
| 476 |
+
return gap, size
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
def _graded_mesh(metals: list, n_core: int, h: float, levels: tuple, majority: bool = False) -> tuple:
|
| 480 |
+
"""Core-graded flagship-family mesh (`anchored_cells_graded` + `strip_merge`): finest panel h within levels[0] of the
|
| 481 |
+
core conductors (metals[1:1 + n_core]), doubling beyond each level. Returns (centres, sizes, owners, h, raw count)."""
|
| 482 |
+
raw = anchored_cells_graded(metals, h, shapely.union_all(metals[1:1 + n_core]), levels, majority=majority)
|
| 483 |
+
c, wh, o = strip_merge(*raw, metals, h)
|
| 484 |
+
return c, wh, o, h, len(raw[0])
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
def _lite_mesh(metals: list, h: float, budget: float, n_core: int = 0, levels: tuple = ()) -> tuple:
|
| 488 |
+
"""Flagship-family mesh at the finest lattice level h 2^k predicted to fit the budget: a probe mesh at the level
|
| 489 |
+
estimated (2 merged panels per h of metal edge) to hold ~budget / 4 panels, then the level LITE_GROWTH^j finer
|
| 490 |
+
predicted to hold <= budget panels (each halving of h multiplies the panels by ~LITE_GROWTH), doubled while it does
|
| 491 |
+
not fit. levels (um from the n_core core conductors): core-graded mesh (`_graded_mesh`); () = uniform
|
| 492 |
+
(`proxy.mesh_flagship`). With an unbounded budget: the mesh at h. Returns (centres, sizes, owners, h, raw count)."""
|
| 493 |
+
def mesh(hh):
|
| 494 |
+
if not levels:
|
| 495 |
+
return P.mesh_flagship(metals, hh, budget)
|
| 496 |
+
while True:
|
| 497 |
+
r = _graded_mesh(metals, n_core, hh, levels)
|
| 498 |
+
if len(r[0]) <= budget:
|
| 499 |
+
return r
|
| 500 |
+
hh *= 2.0
|
| 501 |
+
|
| 502 |
+
if not np.isfinite(budget):
|
| 503 |
+
return mesh(h)
|
| 504 |
+
L = sum(g.length for g in metals)
|
| 505 |
+
k = max(0, int(np.ceil(np.log2(max(2.0 * L / (h * budget / 4), 1.0)))))
|
| 506 |
+
probe = mesh(h * 2.0**k)
|
| 507 |
+
j = min(int(np.floor(np.log(budget / len(probe[0])) / np.log(LITE_GROWTH))), int(round(np.log2(probe[3] / h))))
|
| 508 |
+
return probe if j <= 0 else mesh(probe[3] / 2.0**j)
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def _sym_solve(A, B):
|
| 512 |
+
"""Solve with the lower triangle of the Fortran-ordered A from `proxy._assemble`: Cholesky, or the symmetric
|
| 513 |
+
indefinite (Bunch-Kaufman) factorisation when A is not positive definite."""
|
| 514 |
+
try:
|
| 515 |
+
return P._chol_solve(A.copy(order="F"), B)
|
| 516 |
+
except np.linalg.LinAlgError:
|
| 517 |
+
from scipy.linalg import solve
|
| 518 |
+
return solve(A, B, lower=True, assume_a="sym", overwrite_a=True, check_finite=False)
|
| 519 |
+
|
| 520 |
+
|
| 521 |
+
def _window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: float, all_rhs: bool, levels: tuple = ()) -> tuple:
|
| 522 |
+
"""Flagship-family mesh of one window at the lattice level of h that fits the budget (`_lite_mesh`), exact near
|
| 523 |
+
field, dense Cholesky (H-matrix PCG above DENSE_MAX panels); right-hand sides: every metal (all_rhs, as
|
| 524 |
+
`proxy._cap_flagship`) or the n_core core conductors; levels: core-graded mesh (`_lite_mesh`). A solve is rejected
|
| 525 |
+
when the system is not positive definite, a metal has no panel or (all_rhs) a mutual entry is not negative (cut
|
| 526 |
+
panels of different conductors overlapping on a lattice coarser than the features); the mesh is then refined (h / 2, budget lifted) up to LITE_REFINE times, and finally
|
| 527 |
+
meshed with majority-owned finest cells (as the flagship). Returns (R (n_rhs, n_metals) charges per metal, info)."""
|
| 528 |
+
cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core)
|
| 529 |
+
t_mesh = t_asm = t_sol = 0.0
|
| 530 |
+
hh, cap, refined, majority = h, budget, 0, False
|
| 531 |
+
while True:
|
| 532 |
+
t = time.perf_counter()
|
| 533 |
+
if not refined:
|
| 534 |
+
c, wh, o, hh, n_raw = _lite_mesh(metals, hh, cap, n_core, levels)
|
| 535 |
+
elif levels:
|
| 536 |
+
c, wh, o, hh, n_raw = _graded_mesh(metals, n_core, hh, levels, majority)
|
| 537 |
+
else:
|
| 538 |
+
c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority)
|
| 539 |
+
t_mesh += time.perf_counter() - t
|
| 540 |
+
t = time.perf_counter()
|
| 541 |
+
try:
|
| 542 |
+
if len(c) <= P.DENSE_MAX:
|
| 543 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 544 |
+
t_asm += time.perf_counter() - t
|
| 545 |
+
t = time.perf_counter()
|
| 546 |
+
Bm = (o[:, None] == cols[None, :]).astype(float)
|
| 547 |
+
if all_rhs:
|
| 548 |
+
Q = P._chol_solve(A, Bm)
|
| 549 |
+
else: # windows: an indefinite system (overlapping cut panels on the coarse graded levels) is solved by LDL^T
|
| 550 |
+
Q = _sym_solve(A, Bm)
|
| 551 |
+
else:
|
| 552 |
+
Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols]
|
| 553 |
+
R = np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 554 |
+
off = np.ones(R.shape, bool)
|
| 555 |
+
off[np.arange(len(cols)), cols] = False
|
| 556 |
+
ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0)))
|
| 557 |
+
except np.linalg.LinAlgError:
|
| 558 |
+
ok = False
|
| 559 |
+
t_sol += time.perf_counter() - t
|
| 560 |
+
if ok or majority:
|
| 561 |
+
break
|
| 562 |
+
if refined < LITE_REFINE:
|
| 563 |
+
hh, cap, refined = hh / 2, np.inf, refined + 1
|
| 564 |
+
else:
|
| 565 |
+
majority = True
|
| 566 |
+
return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh,
|
| 567 |
+
"assemble_s": t_asm, "solve_s": t_sol}
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
def _lite_window(G, conds: list, core, R_g: float, margin: float, fill_um: float, tree, etched) -> tuple:
|
| 571 |
+
"""Window of a core: ([ground, core..., halo...], halo). Halo = conductors within R_g of a core conductor (separate
|
| 572 |
+
nets at 0 V); ground = chip ground clipped to the core + halo box widened by margin, plus every farther conductor in
|
| 573 |
+
the box as ground metal with its etched surroundings within fill_um filled where farther than R_g from a member."""
|
| 574 |
+
hit = tree.query([conds[i] for i in core], predicate="dwithin", distance=R_g)[1]
|
| 575 |
+
halo = np.setdiff1d(np.unique(hit), core)
|
| 576 |
+
members = np.concatenate([core, halo]).astype(int)
|
| 577 |
+
b = shapely.bounds(np.array([conds[i] for i in members], dtype=object))
|
| 578 |
+
box = (b[:, 0].min() - margin, b[:, 1].min() - margin, b[:, 2].max() + margin, b[:, 3].max() + margin)
|
| 579 |
+
far = np.setdiff1d(tree.query(shapely.box(*box), predicate="intersects"), members)
|
| 580 |
+
Gw = shapely.clip_by_rect(G, *box)
|
| 581 |
+
if len(far):
|
| 582 |
+
farg = shapely.clip_by_rect(shapely.union_all([conds[i] for i in far]), *box)
|
| 583 |
+
fill = shapely.intersection(shapely.clip_by_rect(etched, *box), farg.buffer(fill_um, quad_segs=4))
|
| 584 |
+
fill = fill.difference(shapely.union_all([conds[i] for i in members]).buffer(R_g, quad_segs=4))
|
| 585 |
+
Gw = shapely.union_all([Gw, farg, fill])
|
| 586 |
+
return [_valid(Gw)] + [conds[i] for i in members], halo
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
_LITE_BUDGET_USED: list = [0.0]
|
| 590 |
+
_LITE: dict = {} # layout of the running micro_mom call, inherited by forked window workers
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
def _lite_job(k: int) -> tuple:
|
| 594 |
+
L = _LITE
|
| 595 |
+
t = time.perf_counter()
|
| 596 |
+
metals, halo = _lite_window(L["G"], L["conds"], L["cores"][k], L["R_g"], L["margin"], L["fill_um"], L["tree"], L["etched"])
|
| 597 |
+
t_build = time.perf_counter() - t
|
| 598 |
+
bx = shapely.bounds(metals[0])
|
| 599 |
+
h = max(L["h"], max(bx[2] - bx[0], bx[3] - bx[1]) / 4096)
|
| 600 |
+
budget = L["budget"]
|
| 601 |
+
if LITE_WINDOW_PPG[0]: # resolution-controlled windows (PRIOR_SCHEME.md 14.4): finest panel gap / ppg, no budget
|
| 602 |
+
h, budget = L["gap"] / LITE_WINDOW_PPG[0], np.inf
|
| 603 |
+
R, info = _window_solve(metals, len(L["cores"][k]), h, budget, L["eps_r"], False, L["levels"])
|
| 604 |
+
info.update(build_s=t_build, n_halo=len(halo), n_core=len(L["cores"][k]))
|
| 605 |
+
return k, halo, R, info
|
| 606 |
+
|
| 607 |
+
|
| 608 |
+
def micro_mom(G, conds: list, eps_r: float = 11.45, budget: float = 1500, cells_per_gap: float = 3.0, max_core: int = 8,
|
| 609 |
+
halo_gaps: float = 100.0, margin_gaps: float = 100.0, margin_min_um: float = 150.0, fill_um: float = 250.0,
|
| 610 |
+
workers: int = 1, level_gaps: tuple = LITE_LEVEL_GAPS, window_budget: float | None = None, canonical: bool = True) -> tuple:
|
| 611 |
+
"""Cheap micro-MoM prior (PRIOR_SCHEME.md section 12, M / hybrid H): (C (N+1, N+1) fF Maxwell, far_mask, info).
|
| 612 |
+
<= max_core conductors: one global window = the layout as given, h = max(gap / cells_per_gap, size / 4096) doubled
|
| 613 |
+
on the flagship lattice until the merged mesh has <= budget panels, every metal excited (with budget = inf this is
|
| 614 |
+
`proxy.mom_capacitance_gds_flagship(rich=False)` bit for bit; `_lite_mesh` picks the level). More conductors: cores of <= max_core by bisection,
|
| 615 |
+
one window per core (halo within halo_gaps, ground clipped at margin_gaps), entry (i, j) = mean of the two window
|
| 616 |
+
estimates when both exist (a non-negative window estimate is not used), ground row from the columns, C_00 = row sum /
|
| 617 |
+
(1 - ROW_SUM_EPS); pairs never co-windowed (far_mask, > halo_gaps apart) in different ground pockets take the screened
|
| 618 |
+
dipole value (`dipole_far`), in the same pocket the closed form (`formula_prior`, quadrature step one smallest gap). level_gaps: core-graded window
|
| 619 |
+
meshes (finest panel within level_gaps[0] smallest gaps of the core, doubling beyond each level); window systems that
|
| 620 |
+
are not positive definite are solved by LDL^T; window budget = window_budget or LITE_WINDOW_BUDGET x budget. Windows
|
| 621 |
+
run on `workers` forked processes. canonical: solved in the `canonical_frames` (mean over them when the layout is
|
| 622 |
+
symmetric; invariant to translation, 90-degree rotation and conductor order), returned in the input order."""
|
| 623 |
+
t_all = time.perf_counter()
|
| 624 |
+
if canonical: # mean over the canonical frames (one unless the layout is symmetric), in the input order
|
| 625 |
+
runs = []
|
| 626 |
+
for Gc, cc, order in canonical_frames(G, conds):
|
| 627 |
+
C, far, info = micro_mom(Gc, cc, eps_r, budget, cells_per_gap, max_core, halo_gaps, margin_gaps, margin_min_um, fill_um,
|
| 628 |
+
workers, level_gaps, window_budget, canonical=False)
|
| 629 |
+
inv = np.r_[0, np.argsort(order) + 1]
|
| 630 |
+
runs.append((C[np.ix_(inv, inv)], far[np.ix_(inv, inv)], info))
|
| 631 |
+
C = runs[0][0] if len(runs) == 1 else sum(r[0] for r in runs) / len(runs)
|
| 632 |
+
far = np.logical_or.reduce([r[1] for r in runs])
|
| 633 |
+
info = runs[0][2]
|
| 634 |
+
for k in ("mesh_s", "assemble_s", "solve_s", "build_s", "formula_s"):
|
| 635 |
+
info[k] = sum(r[2][k] for r in runs)
|
| 636 |
+
info["n_frames"] = len(runs)
|
| 637 |
+
info["total_s"] = time.perf_counter() - t_all
|
| 638 |
+
return C, far, info
|
| 639 |
+
metals = [G] + list(conds)
|
| 640 |
+
N = len(metals)
|
| 641 |
+
far = np.zeros((N, N), bool)
|
| 642 |
+
if len(conds) <= max_core:
|
| 643 |
+
gap, size = _gap_size(metals)
|
| 644 |
+
h = max(gap / cells_per_gap, size / 4096)
|
| 645 |
+
R, wi = _window_solve(metals, len(conds), h, budget, eps_r, True)
|
| 646 |
+
C = 0.5 * (R + R.T)
|
| 647 |
+
info = {"n_windows": 1, "min_gap_um": gap, "h_um": [wi["h_um"]], "panels": [wi["panels"]], "panels_per_gap": [gap / wi["h_um"]],
|
| 648 |
+
"majority": [wi["majority"]], "refined": [wi["refined"]], "mesh_s": wi["mesh_s"], "assemble_s": wi["assemble_s"], "solve_s": wi["solve_s"],
|
| 649 |
+
"build_s": 0.0, "plan_s": 0.0, "formula_s": 0.0, "n_far_pairs": 0, "budget": budget}
|
| 650 |
+
info["total_s"] = time.perf_counter() - t_all
|
| 651 |
+
return C, far, info
|
| 652 |
+
t = time.perf_counter()
|
| 653 |
+
gap = gap_of(metals)
|
| 654 |
+
R_g, margin = halo_gaps * gap, max(margin_gaps * gap, margin_min_um)
|
| 655 |
+
b = shapely.bounds(np.array(conds, dtype=object))
|
| 656 |
+
cores = plan_cores(np.stack([(b[:, 0] + b[:, 2]) / 2, (b[:, 1] + b[:, 3]) / 2], 1), LITE_MAX_CORE[0] or max_core)
|
| 657 |
+
tree = shapely.STRtree(conds)
|
| 658 |
+
etched = shapely.box(*G.bounds).difference(G)
|
| 659 |
+
_LITE.clear()
|
| 660 |
+
_LITE.update(G=G, conds=conds, cores=cores, R_g=R_g, margin=margin, fill_um=fill_um, tree=tree, etched=etched,
|
| 661 |
+
h=gap / cells_per_gap, gap=gap, budget=LITE_WINDOW_BUDGET * budget if window_budget is None else window_budget, eps_r=eps_r,
|
| 662 |
+
levels=tuple(float(x) * gap for x in level_gaps))
|
| 663 |
+
_LITE_BUDGET_USED[:] = [_LITE["budget"]]
|
| 664 |
+
t_plan = time.perf_counter() - t
|
| 665 |
+
t = time.perf_counter()
|
| 666 |
+
if workers > 1 and len(cores) > 1:
|
| 667 |
+
with ProcessPoolExecutor(min(workers, len(cores)), mp_context=get_context("fork")) as ex:
|
| 668 |
+
res = list(ex.map(_lite_job, range(len(cores))))
|
| 669 |
+
else:
|
| 670 |
+
res = [_lite_job(k) for k in range(len(cores))]
|
| 671 |
+
t_win = time.perf_counter() - t
|
| 672 |
+
_LITE.clear()
|
| 673 |
+
acc, cnt = np.zeros((N, N)), np.zeros((N, N))
|
| 674 |
+
for k, halo, R, _ in res:
|
| 675 |
+
idx = np.concatenate([[0], cores[k] + 1, halo + 1]).astype(int)
|
| 676 |
+
for r, ci in enumerate(cores[k] + 1):
|
| 677 |
+
use = (R[r] < 0) | (idx == ci) # a non-negative mutual estimate of a window (tiny far entries) is not used
|
| 678 |
+
acc[ci, idx[use]] += R[r][use]
|
| 679 |
+
cnt[ci, idx[use]] += 1
|
| 680 |
+
have = cnt > 0
|
| 681 |
+
Cw = np.where(have, acc / np.where(have, cnt, 1), 0.0)
|
| 682 |
+
C = np.where(have & have.T, 0.5 * (Cw + Cw.T), np.where(have, Cw, Cw.T))
|
| 683 |
+
far = ~(have | have.T)
|
| 684 |
+
np.fill_diagonal(far, False)
|
| 685 |
+
t = time.perf_counter()
|
| 686 |
+
iu = np.nonzero(np.triu(far, 1))
|
| 687 |
+
t_dip, n_dip = 0.0, 0
|
| 688 |
+
if len(iu[0]):
|
| 689 |
+
pid = np.r_[-10**9, pocket_ids(G, conds)]
|
| 690 |
+
cross = pid[iu[0]] != pid[iu[1]] # different ground pockets: screened dipole; same pocket: closed form
|
| 691 |
+
if (~cross).any():
|
| 692 |
+
F, _ = formula_prior(G, conds, eps_r, step_um=max(FORMULA_STEP_UM, gap), pairs=np.stack(iu, 1)[~cross], gap=gap, workers=workers)
|
| 693 |
+
C[iu[0][~cross], iu[1][~cross]] = F[iu[0][~cross], iu[1][~cross]]
|
| 694 |
+
if cross.any():
|
| 695 |
+
t_dip = time.perf_counter()
|
| 696 |
+
C[iu[0][cross], iu[1][cross]] = dipole_far(G, conds, np.stack(iu, 1)[cross], eps_r, gap)
|
| 697 |
+
t_dip, n_dip = time.perf_counter() - t_dip, int(cross.sum())
|
| 698 |
+
C[iu[1], iu[0]] = C[iu]
|
| 699 |
+
t_form = time.perf_counter() - t
|
| 700 |
+
C[0, 0] = np.abs(C[0, 1:]).sum() / (1 - ROW_SUM_EPS)
|
| 701 |
+
infos = [r[3] for r in res]
|
| 702 |
+
info = {"n_windows": len(cores), "min_gap_um": gap, "R_g_um": R_g, "margin_um": margin, "h_um": [i["h_um"] for i in infos],
|
| 703 |
+
"panels": [i["panels"] for i in infos], "panels_per_gap": [gap / i["h_um"] for i in infos],
|
| 704 |
+
"majority": [i["majority"] for i in infos], "refined": [i["refined"] for i in infos], "cores": [i["n_core"] for i in infos], "halos": [i["n_halo"] for i in infos],
|
| 705 |
+
"mesh_s": sum(i["mesh_s"] for i in infos), "assemble_s": sum(i["assemble_s"] for i in infos),
|
| 706 |
+
"solve_s": sum(i["solve_s"] for i in infos), "build_s": sum(i["build_s"] for i in infos), "plan_s": t_plan,
|
| 707 |
+
"windows_wall_s": t_win, "formula_s": t_form, "n_far_pairs": int(len(iu[0])), "dipole_s": t_dip,
|
| 708 |
+
"n_dipole_pairs": n_dip, "budget": _LITE_BUDGET_USED[0],
|
| 709 |
+
"workers": workers, "level_gaps": list(level_gaps)}
|
| 710 |
+
info["total_s"] = time.perf_counter() - t_all
|
| 711 |
+
return C, far, info
|
| 712 |
+
|
| 713 |
+
|
| 714 |
+
def pocket_ids(G, conds: list) -> np.ndarray:
|
| 715 |
+
"""Ground pocket of every conductor: index of the interior ring of the ground metal that contains it (STRtree of the
|
| 716 |
+
rings' polygons, `within`); a conductor in no ring gets its own id (-1 - k)."""
|
| 717 |
+
rings = [shapely.Polygon(r) for p in getattr(G, "geoms", [G]) for r in p.interiors]
|
| 718 |
+
out = -1 - np.arange(len(conds))
|
| 719 |
+
if rings:
|
| 720 |
+
c, r = shapely.STRtree(rings).query(list(conds), predicate="within")
|
| 721 |
+
out[c] = r
|
| 722 |
+
return out
|
| 723 |
+
|
| 724 |
+
|
| 725 |
+
def _edge_samples(geoms: list, step: float) -> tuple:
|
| 726 |
+
"""Points every <= step along every polygon edge of geoms (vertices included), with the index of their geometry:
|
| 727 |
+
(points (n, 2), owner (n,)). Vectorised over all edges (no per-ring interpolation)."""
|
| 728 |
+
segs, sown = [], []
|
| 729 |
+
for k, g in enumerate(geoms):
|
| 730 |
+
for p in getattr(g, "geoms", [g]):
|
| 731 |
+
for ring in [p.exterior, *p.interiors]:
|
| 732 |
+
xy = np.asarray(ring.coords)
|
| 733 |
+
segs.append(np.stack([xy[:-1], xy[1:]], 1))
|
| 734 |
+
sown.append(np.full(len(xy) - 1, k))
|
| 735 |
+
S, sown = np.concatenate(segs), np.concatenate(sown)
|
| 736 |
+
L = np.sqrt(((S[:, 1] - S[:, 0]) ** 2).sum(1))
|
| 737 |
+
n = np.maximum(1, np.ceil(L / step).astype(int))
|
| 738 |
+
seg = np.repeat(np.arange(len(S)), n)
|
| 739 |
+
t = (np.arange(n.sum()) - np.repeat(np.cumsum(n) - n, n)) / np.repeat(n, n)
|
| 740 |
+
return S[seg, 0] + t[:, None] * (S[seg, 1] - S[seg, 0]), sown[seg]
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
def dipole_far(G, conds: list, pairs, eps_r: float = 11.45, gap: float | None = None) -> np.ndarray:
|
| 744 |
+
"""Screened-dipole far couplings (PRIOR_SCHEME.md 14.2): for metal-index pairs (k, 2) (1 = first conductor),
|
| 745 |
+
C_ij = -(1 + eps_r) eps0 At_i At_j / (2 pi d_eff^3), d_eff^2 = d^2 + a_i^2 + a_j^2, a = sqrt(At / pi), d = centroid
|
| 746 |
+
distance, At = A + ALPHA_GAP_SHARE x P x gbar with gbar the mean distance from i's edge samples (step one gap) to the
|
| 747 |
+
nearest other metal: ground edge samples (step gap / 4) by one k-d tree, other conductors by one k-d tree of all
|
| 748 |
+
conductor samples with owner labels (DIPOLE_K neighbours). Returns the values (k,)."""
|
| 749 |
+
metals = [G] + list(conds)
|
| 750 |
+
gap = gap_of(metals) if gap is None else gap
|
| 751 |
+
pairs = np.asarray(pairs, int).reshape(-1, 2)
|
| 752 |
+
pts, own = _edge_samples(list(conds), gap)
|
| 753 |
+
own = own + 1
|
| 754 |
+
sel = np.isin(own, np.unique(pairs))
|
| 755 |
+
q = pts[sel]
|
| 756 |
+
dg, _ = cKDTree(_edge_samples([G], gap / 4)[0]).query(q)
|
| 757 |
+
d, idx = cKDTree(pts).query(q, k=min(DIPOLE_K, len(own)))
|
| 758 |
+
if d.ndim == 1:
|
| 759 |
+
d, idx = d[:, None], idx[:, None]
|
| 760 |
+
d = np.where(own[np.minimum(idx, len(own) - 1)] != own[sel][:, None], d, np.inf).min(1)
|
| 761 |
+
gbar = np.bincount(own[sel], np.minimum(dg, d), len(metals)) / np.maximum(np.bincount(own[sel], minlength=len(metals)), 1)
|
| 762 |
+
area = np.array([g.area for g in metals])
|
| 763 |
+
perim = np.array([g.length for g in metals])
|
| 764 |
+
At = area + ALPHA_GAP_SHARE * perim * gbar
|
| 765 |
+
cen = shapely.get_coordinates(shapely.centroid(np.array(metals, dtype=object)))
|
| 766 |
+
a, b = pairs[:, 0], pairs[:, 1]
|
| 767 |
+
d2 = ((cen[a] - cen[b]) ** 2).sum(1) + (At[a] + At[b]) / np.pi
|
| 768 |
+
return -(1 + eps_r) * EPS0_FF_UM * At[a] * At[b] / (2 * np.pi * d2 ** 1.5)
|
| 769 |
+
|
| 770 |
+
|
| 771 |
+
def _ground_segments(G):
|
| 772 |
+
"""Edge segments (m, 2, 2) of the ground metal and an STRtree over them."""
|
| 773 |
+
_, S, _ = P._segments([G])
|
| 774 |
+
return S, shapely.STRtree(shapely.linestrings(S))
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
def _metal_length(A: np.ndarray, B: np.ndarray, S: np.ndarray, tree) -> np.ndarray:
|
| 778 |
+
"""Length of ground metal crossed by each segment A[k] -> B[k] (endpoints off the metal): crossings with the ground
|
| 779 |
+
edges (STRtree) sorted along the segment, alternate inside / outside intervals summed."""
|
| 780 |
+
out = np.zeros(len(A))
|
| 781 |
+
if not len(A):
|
| 782 |
+
return out
|
| 783 |
+
li, gi = tree.query(shapely.linestrings(np.stack([A, B], 1)), predicate="intersects")
|
| 784 |
+
if not len(li):
|
| 785 |
+
return out
|
| 786 |
+
p, r = A[li], B[li] - A[li]
|
| 787 |
+
q, s = S[gi, 0], S[gi, 1] - S[gi, 0]
|
| 788 |
+
den = r[:, 0] * s[:, 1] - r[:, 1] * s[:, 0]
|
| 789 |
+
ok = np.abs(den) > 1e-12
|
| 790 |
+
li, t = li[ok], np.clip(((q - p)[ok, 0] * s[ok, 1] - (q - p)[ok, 1] * s[ok, 0]) / den[ok], 0.0, 1.0)
|
| 791 |
+
order = np.lexsort((t, li))
|
| 792 |
+
li, t = li[order], t[order]
|
| 793 |
+
start = np.r_[0, np.nonzero(np.diff(li))[0] + 1]
|
| 794 |
+
n = np.diff(np.r_[start, len(li)])
|
| 795 |
+
rank = np.arange(len(li)) - np.repeat(start, n)
|
| 796 |
+
keep = rank < np.repeat(n // 2 * 2, n)
|
| 797 |
+
sgn = np.where(rank % 2 == 1, 1.0, -1.0)
|
| 798 |
+
out += np.bincount(li[keep], weights=(sgn * t)[keep], minlength=len(A))
|
| 799 |
+
return out * np.sqrt(((B - A) ** 2).sum(1))
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
def _cps_m(d, w1, w2):
|
| 803 |
+
"""Coplanar-strip modulus (as `blocks.edge_features`; w = inf for the ground)."""
|
| 804 |
+
if np.isinf(w1) or np.isinf(w2):
|
| 805 |
+
wf = w2 if np.isinf(w1) else w1
|
| 806 |
+
return d / (d + wf)
|
| 807 |
+
return d * (d + w1 + w2) / ((d + w1) * (d + w2))
|
| 808 |
+
|
| 809 |
+
|
| 810 |
+
_FORM: dict = {} # samples and trees of the running formula_prior call, inherited by forked row workers
|
| 811 |
+
|
| 812 |
+
|
| 813 |
+
def _formula_row(i: int) -> tuple:
|
| 814 |
+
"""Row i of the directed sums I[i, j] of `formula_prior` (j = the near metals of i; ground last term included)."""
|
| 815 |
+
F = _FORM
|
| 816 |
+
pts, kd, near, width, R, k, step_um, every = F["pts"], F["kd"], F["near"], F["width"], F["R"], F["k"], F["step"], F["every"]
|
| 817 |
+
I = np.zeros(near.shape[0])
|
| 818 |
+
dmin_other, dG = np.full(len(pts[i]), np.inf), None
|
| 819 |
+
for j in np.nonzero(near[i])[0]:
|
| 820 |
+
d, idx = kd[j].query(pts[i], distance_upper_bound=R if j > 0 else np.inf)
|
| 821 |
+
ok = np.isfinite(d)
|
| 822 |
+
if j > 0:
|
| 823 |
+
dmin_other = np.minimum(dmin_other, d)
|
| 824 |
+
else:
|
| 825 |
+
dG = d
|
| 826 |
+
if not ok.any():
|
| 827 |
+
continue
|
| 828 |
+
dd = np.maximum(d[ok], 0.05)
|
| 829 |
+
if j == 0:
|
| 830 |
+
S = np.ones(ok.sum())
|
| 831 |
+
else:
|
| 832 |
+
sel = np.arange(0, ok.sum(), every)
|
| 833 |
+
A = pts[i][ok][sel]
|
| 834 |
+
lg = _metal_length(A, pts[j][idx[ok][sel]], F["SG"], F["gtree"])
|
| 835 |
+
S = np.exp(-lg / (dd[sel] + min(width[i], width[j])))[np.arange(ok.sum()) // every]
|
| 836 |
+
I[j] = k * np.sum(step_um * _kratio(_cps_m(dd, width[i], width[j])) * S)
|
| 837 |
+
if dG is not None:
|
| 838 |
+
share = float(np.mean(dG <= dmin_other))
|
| 839 |
+
I[0] += 8 * EPS0_FF_UM * (1 + F["eps_r"]) / 2 * np.sqrt(F["area"][i] / np.pi) * share
|
| 840 |
+
return i, I
|
| 841 |
+
|
| 842 |
+
|
| 843 |
+
def formula_prior(G, conds: list, eps_r: float = 11.45, step_um: float = FORMULA_STEP_UM, R_gaps: float = FORMULA_R_GAPS,
|
| 844 |
+
pairs=None, gap: float | None = None, screen_um: float = FORMULA_SCREEN_UM, workers: int = 1,
|
| 845 |
+
max_core: int = 8) -> tuple:
|
| 846 |
+
"""Closed-form edge-coupling prior (PRIOR_SCHEME.md section 12, F): (C (N+1, N+1) fF Maxwell, info). No mesh, no solve.
|
| 847 |
+
C_ij = eps0 (1 + eps_r) / 2 x mean over the directions (i, j), (j, i) of sum_p K(k') / K(k) S(p) step over the boundary
|
| 848 |
+
samples p of i (modulus of the local widths 2 A / P and the distance to the nearest sample of j, within R_gaps
|
| 849 |
+
smallest gaps; the ground without a bound), S(p) = exp(-l_G / (d + w_min)) with l_G the ground metal crossed on p -> nearest point of j. C_iG: the
|
| 850 |
+
same sum against the ground edge (S = 1) + 8 eps0 eps_eff sqrt(A_i / pi) x (share of i's samples nearest to the
|
| 851 |
+
ground). Pairs farther than R_gaps: the same form on the bounding boxes (facing length (P_i + P_j) / 2, distance of
|
| 852 |
+
the boxes, l_G = ground area fraction x distance). Self terms: row sums / (1 - ROW_SUM_EPS). pairs (k, 2) of metal
|
| 853 |
+
indices (0 = ground): only those mutual entries (the rest 0, no self terms). More than max_core conductors: the
|
| 854 |
+
quadrature step is at least gap / 4 and the screening spacing at least one gap (numerically equivalent: median
|
| 855 |
+
0.02 %, max 1.2 % on the grids vs 0.25 um), rows on `workers` forked processes."""
|
| 856 |
+
t0 = time.perf_counter()
|
| 857 |
+
metals = [G] + list(conds)
|
| 858 |
+
N = len(metals)
|
| 859 |
+
gap = gap_of(metals) if gap is None else gap
|
| 860 |
+
if len(conds) > max_core:
|
| 861 |
+
step_um, screen_um = max(step_um, gap / 4), max(screen_um, gap)
|
| 862 |
+
R = R_gaps * gap
|
| 863 |
+
k = EPS0_FF_UM * (1 + eps_r) / 2
|
| 864 |
+
want = np.zeros((N, N), bool)
|
| 865 |
+
if pairs is None:
|
| 866 |
+
want[np.triu_indices(N, 1)] = True
|
| 867 |
+
else:
|
| 868 |
+
pairs = np.sort(np.asarray(pairs, int).reshape(-1, 2), 1)
|
| 869 |
+
want[pairs[:, 0], pairs[:, 1]] = True
|
| 870 |
+
area = np.array([g.area for g in metals])
|
| 871 |
+
perim = np.array([g.length for g in metals])
|
| 872 |
+
width = np.r_[np.inf, 2 * area[1:] / perim[1:]]
|
| 873 |
+
tree = shapely.STRtree(list(conds))
|
| 874 |
+
near = np.zeros((N, N), bool)
|
| 875 |
+
a, b = tree.query(list(conds), predicate="dwithin", distance=R)
|
| 876 |
+
near[a + 1, b + 1] = True
|
| 877 |
+
near[0, 1:] = near[1:, 0] = True
|
| 878 |
+
np.fill_diagonal(near, False)
|
| 879 |
+
near &= want | want.T
|
| 880 |
+
gx0, gy0, gx1, gy1 = G.bounds
|
| 881 |
+
need = np.unique(np.nonzero(near)[0])
|
| 882 |
+
pts, kd = {}, {}
|
| 883 |
+
for i in np.unique(np.r_[need, np.nonzero(near.any(0))[0]]):
|
| 884 |
+
p = sample_boundary(metals[i], step_um)
|
| 885 |
+
if i == 0: # ground edge = boundary of the etched region (not the chip outline)
|
| 886 |
+
on = (np.abs(p[:, 0] - gx0) < 1e-6) | (np.abs(p[:, 0] - gx1) < 1e-6) | (np.abs(p[:, 1] - gy0) < 1e-6) | (np.abs(p[:, 1] - gy1) < 1e-6)
|
| 887 |
+
p = p[~on]
|
| 888 |
+
pts[i] = p
|
| 889 |
+
kd[i] = cKDTree(p)
|
| 890 |
+
SG, gtree = _ground_segments(G)
|
| 891 |
+
_FORM.clear()
|
| 892 |
+
_FORM.update(pts=pts, kd=kd, near=near, width=width, area=area, R=R, k=k, step=step_um, eps_r=eps_r,
|
| 893 |
+
every=max(1, int(round(screen_um / step_um))), SG=SG, gtree=gtree)
|
| 894 |
+
rows = [i for i in need if i > 0]
|
| 895 |
+
if workers > 1 and len(rows) > 1:
|
| 896 |
+
with ProcessPoolExecutor(min(workers, len(rows)), mp_context=get_context("fork")) as ex:
|
| 897 |
+
res = list(ex.map(_formula_row, rows, chunksize=max(1, len(rows) // (4 * workers))))
|
| 898 |
+
else:
|
| 899 |
+
res = [_formula_row(i) for i in rows]
|
| 900 |
+
_FORM.clear()
|
| 901 |
+
I = np.zeros((N, N))
|
| 902 |
+
for i, row in res:
|
| 903 |
+
I[i] = row
|
| 904 |
+
C = np.zeros((N, N))
|
| 905 |
+
iu = np.nonzero(np.triu(near, 1))
|
| 906 |
+
gnd = iu[0] == 0
|
| 907 |
+
C[iu] = np.where(gnd, I[iu[1], iu[0]], 0.5 * (I[iu] + I[iu[1], iu[0]]))
|
| 908 |
+
fa, fb = np.nonzero(np.triu(want & ~near & ~near.T, 1))
|
| 909 |
+
if len(fa):
|
| 910 |
+
bb = shapely.bounds(np.array(metals, dtype=object))
|
| 911 |
+
dx = np.maximum(0.0, np.maximum(bb[fa, 0] - bb[fb, 2], bb[fb, 0] - bb[fa, 2]))
|
| 912 |
+
dy = np.maximum(0.0, np.maximum(bb[fa, 1] - bb[fb, 3], bb[fb, 1] - bb[fa, 3]))
|
| 913 |
+
dd = np.maximum(np.hypot(dx, dy), 0.05)
|
| 914 |
+
fG = G.area / max(shapely.box(*G.bounds).area, 1e-12)
|
| 915 |
+
w1, w2 = width[fa], width[fb]
|
| 916 |
+
m = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2))
|
| 917 |
+
S = np.exp(-fG * dd / (dd + np.minimum(w1, w2)))
|
| 918 |
+
C[fa, fb] = k * 0.5 * (perim[fa] + perim[fb]) * _kratio(m) * S
|
| 919 |
+
C = -(C + C.T)
|
| 920 |
+
info = {"n_sampled_pairs": int(len(iu[0])), "n_bbox_pairs": int(len(fa)), "min_gap_um": gap, "step_um": step_um,
|
| 921 |
+
"samples": int(sum(len(p) for p in pts.values()))}
|
| 922 |
+
if pairs is None:
|
| 923 |
+
np.fill_diagonal(C, np.abs(C).sum(1) / (1 - ROW_SUM_EPS))
|
| 924 |
+
info["total_s"] = time.perf_counter() - t0
|
| 925 |
+
return C, info
|
geomgnn/_tlsq/embed/fastprior_loc.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Local-gap (graded) lite micro-MoM prior: PRIOR_SCHEME.md Amendment MESH (2026-10-11).
|
| 2 |
+
|
| 3 |
+
The lite prior meshes every metal edge at h = gap / ppg with gap the layout's global smallest conductor gap. A sliver
|
| 4 |
+
(exp12: the 35 nm end slit of a pad lead, 10.2 um long) therefore sets the whole mesh, and h sits on the size / 4096
|
| 5 |
+
floor at every ppg (the resolution knob is dead). Here the panel size is set by the LOCAL gap of each quadtree cell:
|
| 6 |
+
|
| 7 |
+
h(cell) = clamp(local_gap(cell) / ppg, h_floor, H), H = G_ref / ppg, h_floor = size / FLOOR_DIV
|
| 8 |
+
|
| 9 |
+
local_gap(cell) = min over the metals k the cell touches and the other metals j of distance(cell & metal_k, metal_j)
|
| 10 |
+
(the gap of the edge stretch inside the cell, never smaller than the global smallest gap); G_ref = the smallest pair
|
| 11 |
+
gap among conductor pairs whose gap is >= SLIVER_UM ("sliver" pairs only refine their own neighbourhood). Same lattice
|
| 12 |
+
(H 2^-k, anchored to the signal bounding box), exact cut panels, strip merge bounded by each panel's own size, slab
|
| 13 |
+
kernel and exact near field as `fastprior.micro_mom`. A layout without a sliver pair is passed to `fastprior.micro_mom`
|
| 14 |
+
unchanged (bit-identical prior). Resolution descriptor: proxy_min_gap_um = G_ref, proxy_h_min_um = H (log ppg).
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import time
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
import shapely
|
| 23 |
+
|
| 24 |
+
from . import fastprior as F
|
| 25 |
+
from . import proxy as P
|
| 26 |
+
|
| 27 |
+
SLIVER_UM = 0.25 # pair gaps below this are slivers (no labelled layout has a gap in [0.04, 1.99] um; exp12 slits 34.9-35.6 nm)
|
| 28 |
+
FLOOR_DIV = 4096 # h_floor = layout size / FLOOR_DIV (the existing floor)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def ref_gap(metals: list, sliver: float = SLIVER_UM) -> tuple:
|
| 32 |
+
"""(G_ref, smallest gap, layout size, number of sliver pairs). G_ref = smallest pair gap >= sliver; when every pair
|
| 33 |
+
is a sliver, the edge-length-weighted 5th percentile of the local gap of the boundary (0.25 um samples)."""
|
| 34 |
+
gmin, size = F._gap_size(metals)
|
| 35 |
+
n = len(metals)
|
| 36 |
+
d = np.array([shapely.distance(metals[i], metals[j]) for i in range(n) for j in range(i + 1, n)])
|
| 37 |
+
ok = d >= sliver
|
| 38 |
+
if ok.any():
|
| 39 |
+
return float(d[ok].min()), float(gmin), float(size), int((~ok).sum())
|
| 40 |
+
lg = []
|
| 41 |
+
for i, g in enumerate(metals):
|
| 42 |
+
others = shapely.union_all([metals[k] for k in range(n) if k != i])
|
| 43 |
+
for ln in getattr(g.boundary, "geoms", [g.boundary]):
|
| 44 |
+
pts = shapely.line_interpolate_point(ln, np.arange(0, ln.length, 0.25))
|
| 45 |
+
lg.append(shapely.distance(pts, others))
|
| 46 |
+
return float(max(np.percentile(np.concatenate(lg), 5), gmin)), float(gmin), float(size), int((~ok).sum())
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _local_gap(metals: list, boxes, hit, inn, pcs, sel) -> np.ndarray:
|
| 50 |
+
"""local_gap of the cells sel (indices): min over touched metals k and other metals j of distance(cell & k, j)."""
|
| 51 |
+
lg = np.full(len(sel), np.inf)
|
| 52 |
+
m = len(metals)
|
| 53 |
+
for k in range(m):
|
| 54 |
+
t = hit[k, sel]
|
| 55 |
+
if not t.any():
|
| 56 |
+
continue
|
| 57 |
+
ix = sel[t]
|
| 58 |
+
geo = np.where(inn[k, ix], boxes[ix], pcs[k][ix])
|
| 59 |
+
bad = np.array([g is None for g in geo])
|
| 60 |
+
geo[bad] = boxes[ix][bad]
|
| 61 |
+
dk = np.full(len(ix), np.inf)
|
| 62 |
+
for j in range(m):
|
| 63 |
+
if j != k:
|
| 64 |
+
dk = np.minimum(dk, shapely.distance(geo, metals[j]))
|
| 65 |
+
lg[t] = np.minimum(lg[t], dk)
|
| 66 |
+
return lg
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def anchored_cells_local(metals: list, H: float, h_floor: float, ppg: float, grade: float = 2.0, majority: bool = False):
|
| 70 |
+
"""`proxy.anchored_cells` on the lattice of H (anchored to the signal bounding box) with the finest level of every
|
| 71 |
+
cell set by its local gap: a cell (cut or whole) is final once h <= clamp(local_gap / ppg, h_floor, H). Returns
|
| 72 |
+
centres (n, 2), sizes (n, 2), owner (n,)."""
|
| 73 |
+
edges = shapely.union_all([g.boundary for g in metals])
|
| 74 |
+
sig = metals[1:] if len(metals) > 1 else metals
|
| 75 |
+
ax, ay = shapely.union_all(sig).bounds[:2]
|
| 76 |
+
X0, Y0, X1, Y1 = shapely.union_all(metals).bounds
|
| 77 |
+
span = max(X1 - X0, Y1 - Y0)
|
| 78 |
+
K = max(0, int(np.ceil(np.log2(max(span / 16, H) / H))))
|
| 79 |
+
h0 = H * 2**K
|
| 80 |
+
ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0))
|
| 81 |
+
ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0))
|
| 82 |
+
gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1))
|
| 83 |
+
xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0)
|
| 84 |
+
gq = P._Geoms(list(metals) + [edges])
|
| 85 |
+
tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool)
|
| 86 |
+
out_c, out_h, out_o = [], [], []
|
| 87 |
+
while len(xs):
|
| 88 |
+
boxes = shapely.box(xs, ys, xs + h, ys + h)
|
| 89 |
+
cov, hit, inn, pcs = P._cover(gq, boxes, h, tst, inn)
|
| 90 |
+
keep = cov.sum(0) > 1e-3
|
| 91 |
+
xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep]
|
| 92 |
+
pcs = [p[keep] for p in pcs]
|
| 93 |
+
cx, cy = xs + h / 2, ys + h / 2
|
| 94 |
+
full = cov.max(0) > 0.999
|
| 95 |
+
cand = h <= H * 1.0001
|
| 96 |
+
last = cand & (h <= h_floor * 1.0001)
|
| 97 |
+
need = np.nonzero(cand & ~last)[0]
|
| 98 |
+
if len(need):
|
| 99 |
+
tgt = np.clip(_local_gap(metals, boxes, hit, inn, pcs, need) / ppg, h_floor, H)
|
| 100 |
+
last[need] = h[need] <= tgt * 1.0001
|
| 101 |
+
d = np.full(len(xs), np.inf)
|
| 102 |
+
far = full & ~last
|
| 103 |
+
d[far] = P._distance(gq, len(metals), boxes[far])
|
| 104 |
+
leaf = full & (last | (h <= grade * d + 1e-12))
|
| 105 |
+
out_c.append(np.stack([cx[leaf], cy[leaf]], 1))
|
| 106 |
+
out_h.append(np.stack([h[leaf], h[leaf]], 1))
|
| 107 |
+
out_o.append(cov[:, leaf].argmax(0))
|
| 108 |
+
cut = last & ~leaf
|
| 109 |
+
if majority and cut.any():
|
| 110 |
+
take = cut & (cov.max(0) >= 0.5)
|
| 111 |
+
out_c.append(np.stack([cx[take], cy[take]], 1))
|
| 112 |
+
out_h.append(np.stack([h[take], h[take]], 1))
|
| 113 |
+
out_o.append(cov[:, take].argmax(0))
|
| 114 |
+
cut = cut & False
|
| 115 |
+
if cut.any():
|
| 116 |
+
gs = gq.get()
|
| 117 |
+
for k, g in enumerate(gs[:len(metals)]):
|
| 118 |
+
shapely.prepare(g)
|
| 119 |
+
sel = np.nonzero(cut & (cov[k] > 1e-3))[0]
|
| 120 |
+
if not len(sel):
|
| 121 |
+
continue
|
| 122 |
+
pcs_k = pcs[k][sel]
|
| 123 |
+
bb = shapely.bounds(pcs_k)
|
| 124 |
+
cen = shapely.get_coordinates(shapely.centroid(pcs_k))
|
| 125 |
+
inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1])
|
| 126 |
+
if not inside.all():
|
| 127 |
+
cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside]))
|
| 128 |
+
out_c.append(cen)
|
| 129 |
+
out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6))
|
| 130 |
+
out_o.append(np.full(len(sel), k))
|
| 131 |
+
gq.put(gs)
|
| 132 |
+
split = ~leaf & ~last
|
| 133 |
+
hs = h[split] / 2
|
| 134 |
+
xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs])
|
| 135 |
+
ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs])
|
| 136 |
+
h = np.concatenate([hs] * 4)
|
| 137 |
+
tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4)
|
| 138 |
+
return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def mesh_local(metals: list, H: float, h_floor: float, ppg: float, majority: bool = False) -> tuple:
|
| 142 |
+
"""Strip-merged local mesh (strip length <= 16 x the panel's own size and <= the distance to the nearest vertex).
|
| 143 |
+
Returns (centres, sizes, owners, raw count)."""
|
| 144 |
+
raw = anchored_cells_local(metals, H, h_floor, ppg, majority=majority)
|
| 145 |
+
c, wh, o = F.strip_merge(*raw, metals, h_floor)
|
| 146 |
+
return c, wh, o, len(raw[0])
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def _solve(metals: list, H: float, h_floor: float, ppg: float, eps_r: float) -> tuple:
|
| 150 |
+
"""`fastprior._window_solve(all_rhs=True)` on the local mesh: rejected solves (not PD, a metal without a panel, a
|
| 151 |
+
non-negative mutual entry) refine (H, h_floor) / 2 up to LITE_REFINE times, then majority-owned cut cells."""
|
| 152 |
+
t_mesh = t_sol = 0.0
|
| 153 |
+
cols = np.arange(len(metals))
|
| 154 |
+
refined, majority = 0, False
|
| 155 |
+
while True:
|
| 156 |
+
t = time.perf_counter()
|
| 157 |
+
c, wh, o, n_raw = mesh_local(metals, H, h_floor, ppg, majority)
|
| 158 |
+
t_mesh += time.perf_counter() - t
|
| 159 |
+
t = time.perf_counter()
|
| 160 |
+
try:
|
| 161 |
+
if len(c) <= P.DENSE_MAX:
|
| 162 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 163 |
+
Q = P._chol_solve(A, (o[:, None] == cols[None, :]).astype(float))
|
| 164 |
+
del A
|
| 165 |
+
else:
|
| 166 |
+
Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)
|
| 167 |
+
R = np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 168 |
+
off = ~np.eye(len(metals), dtype=bool)
|
| 169 |
+
ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and np.all(R[off] < 0))
|
| 170 |
+
except np.linalg.LinAlgError:
|
| 171 |
+
ok = False
|
| 172 |
+
t_sol += time.perf_counter() - t
|
| 173 |
+
if ok or majority:
|
| 174 |
+
break
|
| 175 |
+
if refined < F.LITE_REFINE:
|
| 176 |
+
H, h_floor, refined = H / 2, h_floor / 2, refined + 1
|
| 177 |
+
else:
|
| 178 |
+
majority = True
|
| 179 |
+
return R, {"H_um": H, "h_floor_um": h_floor, "h_min_um": float(wh.min()), "panels": len(c), "raw": n_raw,
|
| 180 |
+
"refined": refined, "majority": majority, "mesh_s": t_mesh, "solve_s": t_sol}
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def micro_mom_loc(G, conds: list, eps_r: float = 11.45, ppg: float = 3.0, sliver: float = SLIVER_UM,
|
| 184 |
+
floor_div: float = FLOOR_DIV, canonical: bool = True) -> tuple:
|
| 185 |
+
"""Local-gap lite prior of a <= 8-conductor layout: (C (N+1, N+1) fF, far_mask (all False), info). Layouts without a
|
| 186 |
+
sliver pair: `fastprior.micro_mom(budget=inf, cells_per_gap=ppg)` (the existing liteppg prior, bit-identical).
|
| 187 |
+
Mean over the canonical frames of symmetric layouts, returned in the input order (as micro_mom)."""
|
| 188 |
+
t_all = time.perf_counter()
|
| 189 |
+
metals = [G] + list(conds)
|
| 190 |
+
gref, gmin, size, n_sl = ref_gap(metals, sliver)
|
| 191 |
+
if n_sl == 0 or len(conds) > 8: # no sliver (or windows: out of scope) -> the existing prior
|
| 192 |
+
C, far, info = F.micro_mom(G, conds, eps_r, budget=np.inf, cells_per_gap=ppg)
|
| 193 |
+
info.update(local=False, G_ref_um=float(info["min_gap_um"]), H_um=float(min(info["h_um"])), n_sliver_pairs=n_sl)
|
| 194 |
+
return C, far, info
|
| 195 |
+
runs = []
|
| 196 |
+
frames = F.canonical_frames(G, conds) if canonical else [(G, list(conds), np.arange(len(conds)))]
|
| 197 |
+
for Gc, cc, order in frames:
|
| 198 |
+
R, wi = _solve([Gc] + list(cc), gref / ppg, size / floor_div, ppg, eps_r)
|
| 199 |
+
C = 0.5 * (R + R.T)
|
| 200 |
+
inv = np.r_[0, np.argsort(order) + 1]
|
| 201 |
+
runs.append((C[np.ix_(inv, inv)], wi))
|
| 202 |
+
C = sum(r[0] for r in runs) / len(runs)
|
| 203 |
+
wi = runs[0][1]
|
| 204 |
+
info = {"local": True, "n_windows": 1, "n_frames": len(runs), "min_gap_um": gref, "G_ref_um": gref, "gmin_um": gmin,
|
| 205 |
+
"n_sliver_pairs": n_sl, "H_um": wi["H_um"], "h_floor_um": wi["h_floor_um"], "h_um": [wi["H_um"]],
|
| 206 |
+
"h_min_um": wi["h_min_um"], "panels": [wi["panels"]], "panels_per_gap": [gref / wi["H_um"]],
|
| 207 |
+
"majority": [wi["majority"]], "refined": [wi["refined"]],
|
| 208 |
+
"mesh_s": sum(r[1]["mesh_s"] for r in runs), "solve_s": sum(r[1]["solve_s"] for r in runs)}
|
| 209 |
+
info["total_s"] = time.perf_counter() - t_all
|
| 210 |
+
return C, np.zeros(C.shape, bool), info
|
geomgnn/_tlsq/embed/proxy.py
ADDED
|
@@ -0,0 +1,944 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Cheap physics proxy: thin-film method-of-moments capacitance on the metal masks.
|
| 2 |
+
|
| 3 |
+
Planar conductors of negligible thickness on a dielectric half-space: the quasi-static Green's function is
|
| 4 |
+
1 / (4 pi eps0 eps_eff r) with eps_eff = (eps_r + 1) / 2. The metal is discretised into square cells (pulse basis,
|
| 5 |
+
point matching); a uniformly charged square of side a has self-potential q / (4 pi eps0 eps_eff) * 4 ln(1 + sqrt 2) / a.
|
| 6 |
+
Solving P q = v for each conductor at 1 V (others at 0 V) gives the full Maxwell capacitance matrix, including
|
| 7 |
+
shielding and other non-local effects that local descriptors cannot represent. Truncation: the ground plane is
|
| 8 |
+
represented only inside the design window (a proxy, not a replacement for Q3D).
|
| 9 |
+
|
| 10 |
+
v2 (`mom_capacitance_gds`) works on the exact GDS polygons with the whole ground plane and an adaptive quadtree mesh
|
| 11 |
+
(cells no larger than `grade` x their distance to a metal edge, edge cells down to ~1/3 of the smallest gap). On the
|
| 12 |
+
gallery designs it reaches 5-6 % (NCAP, star) and 12 % (concentric) median error with no training, vs 14-32 % for v1.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
import queue
|
| 19 |
+
import threading
|
| 20 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 21 |
+
from functools import partial
|
| 22 |
+
|
| 23 |
+
import numpy as np
|
| 24 |
+
import shapely
|
| 25 |
+
from scipy.linalg import cho_factor, cho_solve
|
| 26 |
+
from scipy.spatial import cKDTree
|
| 27 |
+
|
| 28 |
+
EPS0_FF_PER_UM = 8.8541878128e-3
|
| 29 |
+
SELF = 4 * np.log(1 + np.sqrt(2))
|
| 30 |
+
NUM_THREADS = 0 # threads of the mesh / assembly pool and of the concurrent v3-rich solves; 0 = torch.get_num_threads()
|
| 31 |
+
_LOCK = threading.Lock()
|
| 32 |
+
_POOL: list = [] # [(threads, executor)]
|
| 33 |
+
if hasattr(os, "register_at_fork"):
|
| 34 |
+
os.register_at_fork(after_in_child=_POOL.clear) # a forked worker must not reuse the parent's pool threads
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _nthreads() -> int:
|
| 38 |
+
if NUM_THREADS:
|
| 39 |
+
return NUM_THREADS
|
| 40 |
+
try:
|
| 41 |
+
import torch
|
| 42 |
+
except ImportError:
|
| 43 |
+
return 1
|
| 44 |
+
return torch.get_num_threads()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _run(tasks: list) -> list:
|
| 48 |
+
"""Results of the zero-argument callables in order, on the thread pool when more than one thread is allowed (numpy
|
| 49 |
+
ufuncs and shapely's vectorised functions release the GIL). Tasks never wait on other pool tasks."""
|
| 50 |
+
n = _nthreads()
|
| 51 |
+
if n < 2 or len(tasks) < 2:
|
| 52 |
+
return [t() for t in tasks]
|
| 53 |
+
with _LOCK:
|
| 54 |
+
if not _POOL or _POOL[0][0] != n:
|
| 55 |
+
_POOL[:] = [(n, ThreadPoolExecutor(n, thread_name_prefix="tlsq-mom"))]
|
| 56 |
+
pool = _POOL[0][1]
|
| 57 |
+
return list(pool.map(lambda t: t(), tasks))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _spans(n: int, size: int) -> list:
|
| 61 |
+
return [(a, min(a + size, n)) for a in range(0, n, size)]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class _Geoms:
|
| 65 |
+
"""Private copies of geometries (exact WKB round trip) handed out to one pool task at a time and made on demand: GEOS
|
| 66 |
+
prepared geometries keep mutable search state and must not be shared between threads (nor with a concurrent solve)."""
|
| 67 |
+
|
| 68 |
+
def __init__(self, geoms: list):
|
| 69 |
+
self.wkb, self.free = shapely.to_wkb(geoms), queue.SimpleQueue()
|
| 70 |
+
|
| 71 |
+
def get(self) -> list:
|
| 72 |
+
try:
|
| 73 |
+
return self.free.get_nowait()
|
| 74 |
+
except queue.Empty:
|
| 75 |
+
return list(shapely.from_wkb(self.wkb))
|
| 76 |
+
|
| 77 |
+
def put(self, gs: list) -> None:
|
| 78 |
+
self.free.put(gs)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _cover(gq, boxes, h, tst, inn):
|
| 82 |
+
"""Coverage area(box & metal) / h^2 of every box by every metal (m, n), evaluated exactly only where a box straddles a
|
| 83 |
+
metal: a box properly inside a metal covers 1 and a box disjoint from it 0 (same decisions as the exact areas). tst
|
| 84 |
+
(m, n): metals the box can touch (its parent did); inn (m, n): metals the box lies properly inside (from its parent).
|
| 85 |
+
Returns cov, hit (box intersects the metal), inn, and per metal the pieces box & metal where they were computed."""
|
| 86 |
+
m, n = tst.shape
|
| 87 |
+
cov, hit, inn = inn.astype(float), inn.copy(), inn.copy()
|
| 88 |
+
pcs = [np.empty(n, dtype=object) for _ in range(m)]
|
| 89 |
+
work = [(k, ix[a:b]) for k in range(m) for ix in [np.nonzero(tst[k])[0]] for a, b in _spans(len(ix), 256)]
|
| 90 |
+
|
| 91 |
+
def task(k, ix):
|
| 92 |
+
gs = gq.get()
|
| 93 |
+
try:
|
| 94 |
+
g = gs[k]
|
| 95 |
+
shapely.prepare(g)
|
| 96 |
+
b = boxes[ix]
|
| 97 |
+
ht = shapely.intersects(g, b)
|
| 98 |
+
it = shapely.contains_properly(g, b)
|
| 99 |
+
p = shapely.intersection(b[ht & ~it], g)
|
| 100 |
+
return ht, it, p, shapely.area(p)
|
| 101 |
+
finally:
|
| 102 |
+
gq.put(gs)
|
| 103 |
+
|
| 104 |
+
for (k, ix), (ht, it, p, a) in zip(work, _run([partial(task, k, ix) for k, ix in work])):
|
| 105 |
+
hit[k, ix], inn[k, ix] = ht, it
|
| 106 |
+
cov[k, ix[it]] = 1.0
|
| 107 |
+
s = ix[ht & ~it]
|
| 108 |
+
cov[k, s] = a / h[s] ** 2
|
| 109 |
+
pcs[k][s] = p
|
| 110 |
+
return cov, hit, inn, pcs
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def _distance(gq, j: int, geoms) -> np.ndarray:
|
| 114 |
+
"""shapely.distance(geoms, geometry j of the queue), in chunks on the pool."""
|
| 115 |
+
def task(a, b):
|
| 116 |
+
gs = gq.get()
|
| 117 |
+
try:
|
| 118 |
+
return shapely.distance(geoms[a:b], gs[j])
|
| 119 |
+
finally:
|
| 120 |
+
gq.put(gs)
|
| 121 |
+
return np.concatenate([np.zeros(0)] + _run([partial(task, a, b) for a, b in _spans(len(geoms), 256)]))
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def mom_capacitance(masks: np.ndarray, window_um: float, eps_r: float = 11.45, cells: int = 64) -> np.ndarray:
|
| 127 |
+
"""masks: (n_cond, R, R) coverage in [0, 1] (channel 0 = ground). Returns the (n_cond, n_cond) Maxwell C in fF."""
|
| 128 |
+
nc, R, _ = masks.shape
|
| 129 |
+
f = R // cells
|
| 130 |
+
cov = masks.reshape(nc, cells, f, cells, f).mean((2, 4)) # coverage per coarse cell
|
| 131 |
+
owner = np.where(cov.max(0) >= 0.5, cov.argmax(0), -1)
|
| 132 |
+
ii, jj = np.nonzero(owner >= 0)
|
| 133 |
+
lab = owner[ii, jj]
|
| 134 |
+
a = window_um / cells
|
| 135 |
+
xy = np.stack([(jj + 0.5) * a, (ii + 0.5) * a], 1)
|
| 136 |
+
d = np.sqrt(((xy[:, None, :] - xy[None, :, :]) ** 2).sum(-1))
|
| 137 |
+
np.fill_diagonal(d, 1.0)
|
| 138 |
+
P = 1.0 / d
|
| 139 |
+
np.fill_diagonal(P, SELF / a)
|
| 140 |
+
P /= 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2
|
| 141 |
+
B = (lab[:, None] == np.arange(nc)[None, :]).astype(float) # conductor b at 1 V
|
| 142 |
+
Q = cho_solve(cho_factor(P, lower=True), B)
|
| 143 |
+
C = np.stack([Q[lab == a_].sum(0) for a_ in range(nc)]) # C[a, b] = charge on a with b at 1 V
|
| 144 |
+
return 0.5 * (C + C.T)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def quadtree_cells(metals: list, bbox, h0: float, h_min: float, grade: float = 2.0, limit: int | None = None):
|
| 148 |
+
"""Square cells covering the metal polygons: a cell is kept whole when it lies inside one conductor and is no larger
|
| 149 |
+
than `grade` x its distance to the nearest metal edge; edge cells are split down to h_min, where they go to the
|
| 150 |
+
conductor covering >= 50 % of them. Returns centres (n, 2), sides (n,), owner (n,); None once more than `limit` cells."""
|
| 151 |
+
edges = shapely.union_all([g.boundary for g in metals])
|
| 152 |
+
x0, y0, x1, y1 = bbox
|
| 153 |
+
nx, ny = int(np.ceil((x1 - x0) / h0)), int(np.ceil((y1 - y0) / h0))
|
| 154 |
+
gx, gy = np.meshgrid(x0 + h0 * np.arange(nx), y0 + h0 * np.arange(ny))
|
| 155 |
+
xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0)
|
| 156 |
+
gq = _Geoms(list(metals) + [edges])
|
| 157 |
+
tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool)
|
| 158 |
+
out_c, out_h, out_o, count = [], [], [], 0
|
| 159 |
+
while len(xs):
|
| 160 |
+
boxes = shapely.box(xs, ys, xs + h, ys + h)
|
| 161 |
+
cov, hit, inn, _ = _cover(gq, boxes, h, tst, inn)
|
| 162 |
+
keep = cov.sum(0) > 1e-3
|
| 163 |
+
xs, ys, h, cov, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], hit[:, keep], inn[:, keep]
|
| 164 |
+
cx, cy = xs + h / 2, ys + h / 2
|
| 165 |
+
full = cov.max(0) > 0.999
|
| 166 |
+
d = np.full(len(xs), np.inf) # the edge distance only decides for cells inside one conductor
|
| 167 |
+
d[full] = _distance(gq, len(metals), shapely.points(cx[full], cy[full]))
|
| 168 |
+
leaf = full & (h <= grade * d + 1e-12)
|
| 169 |
+
last = h <= h_min * 1.0001
|
| 170 |
+
take_last = last & ~leaf & (cov.max(0) >= 0.5)
|
| 171 |
+
acc = leaf | take_last
|
| 172 |
+
out_c.append(np.stack([cx[acc], cy[acc]], 1))
|
| 173 |
+
out_h.append(h[acc])
|
| 174 |
+
out_o.append(cov[:, acc].argmax(0))
|
| 175 |
+
count += int(acc.sum())
|
| 176 |
+
if limit is not None and count > limit:
|
| 177 |
+
return None
|
| 178 |
+
split = ~acc & ~last
|
| 179 |
+
hs = h[split] / 2
|
| 180 |
+
xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs])
|
| 181 |
+
ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs])
|
| 182 |
+
h = np.concatenate([hs] * 4)
|
| 183 |
+
tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4)
|
| 184 |
+
return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def anchored_cells(metals: list, h_f: float, grade: float = 2.0, majority: bool = False, limit: int | None = None):
|
| 188 |
+
"""v3 mesh: quadtree on a lattice anchored to the signal conductors, independent of the chip size and position.
|
| 189 |
+
|
| 190 |
+
Finest cells have side h_f; coarse cells are h_f * 2^k on the same lattice (origin = lower-left corner of the
|
| 191 |
+
signal conductors' bounding box), so the cells near the conductors do not change when the ground plane grows,
|
| 192 |
+
shrinks or the layout is translated. A cell is kept whole when it lies inside one conductor and is no larger than
|
| 193 |
+
`grade` x the distance from the cell to the nearest metal edge (cells touching an edge reach h_f); finest-level cells that straddle an edge are cut along the exact
|
| 194 |
+
polygon boundaries into one panel per metal (no rounding of gaps to the grid).
|
| 195 |
+
Returns centres (n, 2), sizes (n, 2) = (width, height) of each panel, owner (n,); None once the mesh has more
|
| 196 |
+
than `limit` cells."""
|
| 197 |
+
edges = shapely.union_all([g.boundary for g in metals])
|
| 198 |
+
sig = metals[1:] if len(metals) > 1 else metals
|
| 199 |
+
ax, ay = shapely.union_all(sig).bounds[:2]
|
| 200 |
+
X0, Y0, X1, Y1 = shapely.union_all(metals).bounds
|
| 201 |
+
span = max(X1 - X0, Y1 - Y0)
|
| 202 |
+
K = max(0, int(np.ceil(np.log2(max(span / 16, h_f) / h_f))))
|
| 203 |
+
h0 = h_f * 2**K
|
| 204 |
+
ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0))
|
| 205 |
+
ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0))
|
| 206 |
+
gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1))
|
| 207 |
+
xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0)
|
| 208 |
+
gq = _Geoms(list(metals) + [edges])
|
| 209 |
+
tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool)
|
| 210 |
+
out_c, out_h, out_o, count = [], [], [], 0
|
| 211 |
+
while len(xs):
|
| 212 |
+
boxes = shapely.box(xs, ys, xs + h, ys + h)
|
| 213 |
+
cov, hit, inn, pcs = _cover(gq, boxes, h, tst, inn)
|
| 214 |
+
keep = cov.sum(0) > 1e-3
|
| 215 |
+
xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep]
|
| 216 |
+
pcs = [p[keep] for p in pcs]
|
| 217 |
+
cx, cy = xs + h / 2, ys + h / 2
|
| 218 |
+
full = cov.max(0) > 0.999
|
| 219 |
+
last = h <= h_f * 1.0001
|
| 220 |
+
d = np.full(len(xs), np.inf) # the edge distance only decides for whole cells above the finest level
|
| 221 |
+
far = full & ~last
|
| 222 |
+
d[far] = _distance(gq, len(metals), boxes[far])
|
| 223 |
+
leaf = full & (last | (h <= grade * d + 1e-12))
|
| 224 |
+
out_c.append(np.stack([cx[leaf], cy[leaf]], 1))
|
| 225 |
+
out_h.append(np.stack([h[leaf], h[leaf]], 1))
|
| 226 |
+
out_o.append(cov[:, leaf].argmax(0))
|
| 227 |
+
count += int(leaf.sum())
|
| 228 |
+
cut = last & ~leaf
|
| 229 |
+
if majority and cut.any():
|
| 230 |
+
take = cut & (cov.max(0) >= 0.5)
|
| 231 |
+
out_c.append(np.stack([cx[take], cy[take]], 1))
|
| 232 |
+
out_h.append(np.stack([h[take], h[take]], 1))
|
| 233 |
+
out_o.append(cov[:, take].argmax(0))
|
| 234 |
+
count += int(take.sum())
|
| 235 |
+
cut = cut & False
|
| 236 |
+
if cut.any():
|
| 237 |
+
gs = gq.get()
|
| 238 |
+
for k, g in enumerate(gs[:-1]):
|
| 239 |
+
shapely.prepare(g)
|
| 240 |
+
sel = np.nonzero(cut & (cov[k] > 1e-3))[0]
|
| 241 |
+
if not len(sel):
|
| 242 |
+
continue
|
| 243 |
+
pcs_k = pcs[k][sel] # box & metal, computed by _cover (straddling cells only reach this level uncut)
|
| 244 |
+
bb = shapely.bounds(pcs_k)
|
| 245 |
+
cen = shapely.get_coordinates(shapely.centroid(pcs_k))
|
| 246 |
+
inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1])
|
| 247 |
+
if not inside.all():
|
| 248 |
+
rp = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside]))
|
| 249 |
+
cen[~inside] = rp
|
| 250 |
+
out_c.append(cen)
|
| 251 |
+
out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6))
|
| 252 |
+
out_o.append(np.full(len(sel), k))
|
| 253 |
+
count += len(sel)
|
| 254 |
+
gq.put(gs)
|
| 255 |
+
if limit is not None and count > limit:
|
| 256 |
+
return None
|
| 257 |
+
split = ~leaf & ~last
|
| 258 |
+
hs = h[split] / 2
|
| 259 |
+
xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs])
|
| 260 |
+
ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs])
|
| 261 |
+
h = np.concatenate([hs] * 4)
|
| 262 |
+
tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4)
|
| 263 |
+
return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def mom_mesh(metals: list, max_cells: int = 8000, h_min: float | None = None, grade: float = 2.0, cells_per_gap: float = 3.0,
|
| 267 |
+
mesh: str = "v2", offset: tuple = (0.0, 0.0)):
|
| 268 |
+
"""Mesh for the thin-film MoM. mesh="v3": conductor-anchored lattice with exact edge panels
|
| 269 |
+
(`anchored_cells`); mesh="v2" (default, published priors): the original chip-anchored quadtree (reproduces the published v2 priors).
|
| 270 |
+
Returns (centres, sides, owner, h_min)."""
|
| 271 |
+
bbox = shapely.union_all(metals).bounds
|
| 272 |
+
size = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
|
| 273 |
+
if h_min is None:
|
| 274 |
+
gap = min(shapely.distance(metals[i], metals[j]) for i in range(len(metals)) for j in range(i + 1, len(metals)))
|
| 275 |
+
h_min = max(gap / cells_per_gap, size / 4096)
|
| 276 |
+
if mesh == "v2" and any(offset):
|
| 277 |
+
h0 = size / 16
|
| 278 |
+
bbox = (bbox[0] - offset[0] * h0, bbox[1] - offset[1] * h0, bbox[2] + h0, bbox[3] + h0)
|
| 279 |
+
while True:
|
| 280 |
+
if mesh == "v3":
|
| 281 |
+
r = anchored_cells(metals, h_min, grade, limit=max_cells)
|
| 282 |
+
else:
|
| 283 |
+
r = quadtree_cells(metals, bbox, size / 16, h_min, grade, limit=max_cells)
|
| 284 |
+
if r is not None:
|
| 285 |
+
return (*r, h_min)
|
| 286 |
+
h_min *= 1.5 if mesh == "v2" else 2.0 # v3: stay on the same lattice
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
def rect_self(a, b):
|
| 290 |
+
"""Potential at the centre of a uniformly charged a x b rectangle per unit charge, times 4 pi eps (= SELF / a for a
|
| 291 |
+
square)."""
|
| 292 |
+
r = np.sqrt(a**2 + b**2)
|
| 293 |
+
return (2 * a * np.log((b + r) / a) + 2 * b * np.log((a + r) / b)) / (a * b)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def _mom_solve_torch(c, h, o, n_metals, eps_r, dev):
|
| 297 |
+
"""Same assembly and Cholesky solve as `mom_solve`, in float64 torch on `dev` (opt-in via TLSQ_MOM_DEVICE)."""
|
| 298 |
+
import torch
|
| 299 |
+
|
| 300 |
+
t = lambda x: torch.as_tensor(np.ascontiguousarray(x), dtype=torch.float64, device=dev) # noqa: E731
|
| 301 |
+
wh = np.stack([h, h], 1) if h.ndim == 1 else h
|
| 302 |
+
C, WH = t(c), t(wh)
|
| 303 |
+
hm = WH.max(1).values
|
| 304 |
+
d = torch.cdist(C, C)
|
| 305 |
+
d.fill_diagonal_(1.0)
|
| 306 |
+
P = 1.0 / d
|
| 307 |
+
near = d < 1.5 * torch.maximum(hm[:, None], hm[None, :])
|
| 308 |
+
near.fill_diagonal_(False)
|
| 309 |
+
del d
|
| 310 |
+
ii, jj = torch.nonzero(near, as_tuple=True)
|
| 311 |
+
del near
|
| 312 |
+
if len(ii):
|
| 313 |
+
off = (torch.arange(3, dtype=torch.float64, device=dev) - 1) / 3
|
| 314 |
+
ox, oy = torch.meshgrid(off, off, indexing="xy")
|
| 315 |
+
sx = C[jj, 0, None] + ox.reshape(-1)[None] * WH[jj, 0, None]
|
| 316 |
+
sy = C[jj, 1, None] + oy.reshape(-1)[None] * WH[jj, 1, None]
|
| 317 |
+
r = torch.sqrt((C[ii, 0, None] - sx) ** 2 + (C[ii, 1, None] - sy) ** 2)
|
| 318 |
+
P[ii, jj] = (1.0 / torch.clamp(r, min=1e-9)).mean(1)
|
| 319 |
+
P = 0.5 * (P + P.T)
|
| 320 |
+
diag = t(rect_self(wh[:, 0], wh[:, 1]) if h.ndim == 2 else SELF / h)
|
| 321 |
+
P.diagonal().copy_(diag)
|
| 322 |
+
P /= 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2
|
| 323 |
+
B = t((o[:, None] == np.arange(n_metals)[None, :]).astype(float))
|
| 324 |
+
L, info = torch.linalg.cholesky_ex(P)
|
| 325 |
+
del P
|
| 326 |
+
if int(info):
|
| 327 |
+
raise np.linalg.LinAlgError(f"{int(info)}-th leading minor not positive definite")
|
| 328 |
+
return torch.cholesky_solve(B, L).cpu().numpy()
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def _mom_solve_gpu_slot(c, h, o, n_metals, eps_r):
|
| 332 |
+
"""GPU solve if one of TLSQ_MOM_GPU_SLOTS slots (file locks shared by the worker processes) is free and the solve
|
| 333 |
+
fits; None means: solve on the CPU instead."""
|
| 334 |
+
import fcntl
|
| 335 |
+
import os
|
| 336 |
+
import tempfile
|
| 337 |
+
|
| 338 |
+
import torch
|
| 339 |
+
|
| 340 |
+
slots = int(os.environ.get("TLSQ_MOM_GPU_SLOTS", "3"))
|
| 341 |
+
tag = os.environ.get("SLURM_JOB_ID", "local")
|
| 342 |
+
for k in range(slots):
|
| 343 |
+
f = open(os.path.join(tempfile.gettempdir(), f"tlsq_gpu_{tag}_{k}.lock"), "w")
|
| 344 |
+
try:
|
| 345 |
+
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
| 346 |
+
except OSError:
|
| 347 |
+
f.close()
|
| 348 |
+
continue
|
| 349 |
+
try:
|
| 350 |
+
return _mom_solve_torch(c, h, o, n_metals, eps_r, os.environ["TLSQ_MOM_DEVICE"])
|
| 351 |
+
except RuntimeError as e: # includes torch.OutOfMemoryError
|
| 352 |
+
if "out of memory" not in str(e).lower():
|
| 353 |
+
raise
|
| 354 |
+
return None
|
| 355 |
+
finally:
|
| 356 |
+
torch.cuda.empty_cache()
|
| 357 |
+
fcntl.flock(f, fcntl.LOCK_UN)
|
| 358 |
+
f.close()
|
| 359 |
+
return None
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
def _G(x, y):
|
| 363 |
+
"""Antiderivative of 1 / sqrt(x^2 + y^2) over x and y: x asinh(y / |x|) + y asinh(x / |y|) (0 on the axes)."""
|
| 364 |
+
ax, ay = np.abs(x), np.abs(y)
|
| 365 |
+
with np.errstate(divide="ignore", invalid="ignore"):
|
| 366 |
+
g1 = np.where(ax > 0, x * np.arcsinh(y / np.where(ax > 0, ax, 1.0)), 0.0)
|
| 367 |
+
g2 = np.where(ay > 0, y * np.arcsinh(x / np.where(ay > 0, ay, 1.0)), 0.0)
|
| 368 |
+
return g1 + g2
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def rect_potential(p, cq, wh):
|
| 372 |
+
"""Exact potential (x 4 pi eps) at points p (n, 2) of uniformly charged rectangles (unit total charge) centred at cq
|
| 373 |
+
with sizes wh (n, 2): (1 / (w h)) * integral of 1 / r over the rectangle. Equals rect_self at the centre."""
|
| 374 |
+
x1, x2 = cq[:, 0] - wh[:, 0] / 2 - p[:, 0], cq[:, 0] + wh[:, 0] / 2 - p[:, 0]
|
| 375 |
+
y1, y2 = cq[:, 1] - wh[:, 1] / 2 - p[:, 1], cq[:, 1] + wh[:, 1] / 2 - p[:, 1]
|
| 376 |
+
return (_G(x2, y2) - _G(x1, y2) - _G(x2, y1) + _G(x1, y1)) / (wh[:, 0] * wh[:, 1])
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
def _mom_solve_exact_near(c, wh, o, n_metals, eps_r):
|
| 380 |
+
"""v3 panels: exact rectangle potential for near pairs (d < 1.5 max side) instead of the 3x3 point average. Used only
|
| 381 |
+
when the 3x3 system is not positive definite (sliver edge panels on long fine combs), so priors that solved before
|
| 382 |
+
are unchanged."""
|
| 383 |
+
P = _assemble(c, wh, rect_self(wh[:, 0], wh[:, 1]), eps_r, rect_potential)
|
| 384 |
+
B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float)
|
| 385 |
+
return _chol_solve(P, B)
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def mom_solve(c, h, o, n_metals: int, eps_r: float = 11.45):
|
| 389 |
+
"""mom_solve_quad; for v3 panels (h of shape (n, 2)) a non-positive-definite system is re-solved with the exact
|
| 390 |
+
near field (`_mom_solve_exact_near`)."""
|
| 391 |
+
try:
|
| 392 |
+
return mom_solve_quad(c, h, o, n_metals, eps_r)
|
| 393 |
+
except np.linalg.LinAlgError:
|
| 394 |
+
if h.ndim != 2:
|
| 395 |
+
raise
|
| 396 |
+
print("v3: 3x3 near-field system not positive definite, exact near field", flush=True)
|
| 397 |
+
return _mom_solve_exact_near(c, h, o, n_metals, eps_r)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
def mom_solve_quad(c, h, o, n_metals: int, eps_r: float = 11.45):
|
| 401 |
+
"""Point-matching solve on a mesh from `mom_mesh`: cell charges Q (n_cells, n_metals) for each conductor at 1 V.
|
| 402 |
+
h: square sides (n,) or panel (width, height) (n, 2). Set TLSQ_MOM_DEVICE=cuda to assemble and solve on a GPU."""
|
| 403 |
+
import os
|
| 404 |
+
|
| 405 |
+
if os.environ.get("TLSQ_MOM_DEVICE") and SUBSTRATE["model"] == "halfspace": # the torch path has no image correction
|
| 406 |
+
Q = _mom_solve_gpu_slot(c, h, o, n_metals, eps_r)
|
| 407 |
+
if Q is not None:
|
| 408 |
+
return Q
|
| 409 |
+
wh = np.stack([h, h], 1) if h.ndim == 1 else h
|
| 410 |
+
P = _assemble(c, wh, rect_self(wh[:, 0], wh[:, 1]) if h.ndim == 2 else SELF / h, eps_r, _near_quad)
|
| 411 |
+
B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float)
|
| 412 |
+
return _chol_solve(P, B)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def _near_quad(p, cq, wh):
|
| 416 |
+
"""Potential (x 4 pi eps) at points p of the panels centred at cq with sizes wh: mean of 1 / r over a 3x3 sample of the
|
| 417 |
+
panel (point matching is poor for close, unequal cells)."""
|
| 418 |
+
off = (np.arange(3) - 1) / 3
|
| 419 |
+
ox, oy = np.meshgrid(off, off)
|
| 420 |
+
sx = cq[:, 0, None] + ox.ravel()[None] * wh[:, 0, None]
|
| 421 |
+
sy = cq[:, 1, None] + oy.ravel()[None] * wh[:, 1, None]
|
| 422 |
+
r = np.sqrt((p[:, 0, None] - sx) ** 2 + (p[:, 1, None] - sy) ** 2)
|
| 423 |
+
return (1.0 / np.maximum(r, 1e-9)).mean(1)
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
def _near_pairs(c, hm):
|
| 427 |
+
"""Pairs i < j with |c_i - c_j| < 1.5 max(hm_i, hm_j): candidates from k-d trees per cell-size class, kept by the same
|
| 428 |
+
floating-point test as the dense mask."""
|
| 429 |
+
n = len(c)
|
| 430 |
+
tree, lev, key = cKDTree(c), np.floor(np.log2(hm)), [np.zeros(0, np.int64)]
|
| 431 |
+
for L in np.unique(lev):
|
| 432 |
+
sel = np.nonzero(lev == L)[0]
|
| 433 |
+
pr = cKDTree(c[sel]).sparse_distance_matrix(tree, 1.5 * hm[sel].max() * (1 + 1e-9), output_type="ndarray")
|
| 434 |
+
a, b = sel[pr["i"]], pr["j"]
|
| 435 |
+
key.append(np.minimum(a, b) * n + np.maximum(a, b))
|
| 436 |
+
key = np.unique(np.concatenate(key))
|
| 437 |
+
ii, jj = key // n, key % n
|
| 438 |
+
ok = (ii != jj) & (np.sqrt((c[ii, 0] - c[jj, 0]) ** 2 + (c[ii, 1] - c[jj, 1]) ** 2) < 1.5 * np.maximum(hm[ii], hm[jj]))
|
| 439 |
+
return ii[ok], jj[ok]
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
SUBSTRATE = {"model": "slab_open", "t_um": 350.0} # MoM Green's function (PRIOR_SCHEME.md 14.6): "slab_open" (default since 2026-10-10: 350 um substrate, vacuum below), "halfspace" (released models' priors), "slab_pec"
|
| 443 |
+
_IMG_CACHE: dict = {}
|
| 444 |
+
_IMG_GRID: dict = {}
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
class substrate:
|
| 448 |
+
"""Context manager: the MoM Green's function inside the block (`SUBSTRATE`): model "slab_open" (default since
|
| 449 |
+
2026-10-10, PRIOR_SCHEME.md 14.6), "halfspace" (the kernel of the priors the released models were trained on) or
|
| 450 |
+
"slab_pec"; t_um the substrate thickness (None: unchanged)."""
|
| 451 |
+
|
| 452 |
+
def __init__(self, model: str | None, t_um: float | None = None):
|
| 453 |
+
self.new = {k: v for k, v in (("model", model), ("t_um", t_um)) if v is not None}
|
| 454 |
+
|
| 455 |
+
def __enter__(self):
|
| 456 |
+
self.old = dict(SUBSTRATE)
|
| 457 |
+
SUBSTRATE.update(self.new)
|
| 458 |
+
return self
|
| 459 |
+
|
| 460 |
+
def __exit__(self, *exc):
|
| 461 |
+
SUBSTRATE.clear()
|
| 462 |
+
SUBSTRATE.update(self.old)
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def _image_correction(d, eps_r: float, exact: bool = False):
|
| 466 |
+
"""Finite-substrate correction to the half-space kernel 1 / d (same units): sum_n w_n / sqrt(d^2 + D_n^2) for panels on the
|
| 467 |
+
top face of a slab of thickness t (SUBSTRATE): "slab_open" = vacuum below (w_n = K^2n + K^(2n-1), D_n = 2 n t,
|
| 468 |
+
K = (eps_r - 1) / (eps_r + 1); weights sum to eps_eff - 1, so the kernel tends to the vacuum one for d >> eps_r t),
|
| 469 |
+
"slab_pec" = metal backside (w_n = -(1 + K) (-K)^(n-1); weights sum to -1). Images up to K^2n < 1e-4, the remaining
|
| 470 |
+
weight as one more image; tabulated on a uniform grid of t / 1000 up to 100 mm and interpolated linearly (dense
|
| 471 |
+
assembly), or summed exactly (exact=True: the H-matrix kernel, whose ACA blocks need a smooth kernel). None for the
|
| 472 |
+
half-space (the published kernel)."""
|
| 473 |
+
m = SUBSTRATE["model"]
|
| 474 |
+
if m == "halfspace":
|
| 475 |
+
return None
|
| 476 |
+
key = (m, float(SUBSTRATE["t_um"]), float(eps_r))
|
| 477 |
+
if key not in _IMG_CACHE:
|
| 478 |
+
K, t = (eps_r - 1) / (eps_r + 1), float(SUBSTRATE["t_um"])
|
| 479 |
+
n = np.arange(1, 400)
|
| 480 |
+
nmax = int(np.argmax(K ** (2 * n) < 1e-4)) + 1
|
| 481 |
+
n = n[:nmax]
|
| 482 |
+
if m == "slab_open":
|
| 483 |
+
w, total = K ** (2 * n) + K ** (2 * n - 1), K / (1 - K)
|
| 484 |
+
else:
|
| 485 |
+
w, total = -(1 + K) * (-K) ** (n - 1), -1.0
|
| 486 |
+
w, D = np.r_[w, total - w.sum()], np.r_[2 * n * t, 2 * (nmax + 1) * t]
|
| 487 |
+
du = t / 1000
|
| 488 |
+
u = np.arange(0.0, 1e5 + 2 * du, du)
|
| 489 |
+
T = (w[None, :] / np.sqrt(u[:, None] ** 2 + D[None, :] ** 2)).sum(1)
|
| 490 |
+
_IMG_CACHE[key] = (du, T, w, D)
|
| 491 |
+
du, T = _IMG_CACHE[key][:2]
|
| 492 |
+
if exact:
|
| 493 |
+
w, D = _IMG_CACHE[key][2:]
|
| 494 |
+
d2 = np.asarray(d, float) ** 2
|
| 495 |
+
return sum(wn / np.sqrt(d2 + Dn * Dn) for wn, Dn in zip(w, D))
|
| 496 |
+
if key not in _IMG_GRID:
|
| 497 |
+
_IMG_GRID[key] = du * np.arange(len(T))
|
| 498 |
+
return np.interp(d, _IMG_GRID[key], T)
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
def _assemble(c, wh, diag, eps_r: float, near):
|
| 502 |
+
"""Interaction matrix / (4 pi eps0 eps_eff) for the lower Cholesky factorisation: far pairs 1 / d, near pairs (d < 1.5
|
| 503 |
+
max side) the symmetrised near(p, cq, wh) potential, diagonal `diag`; the same numbers as the dense construction.
|
| 504 |
+
Built in row blocks on the pool threads without n x n temporaries, and only the half the factorisation reads: returns
|
| 505 |
+
a Fortran-ordered matrix whose lower triangle holds the entries (its upper triangle is not initialised)."""
|
| 506 |
+
n = len(c)
|
| 507 |
+
k = 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2
|
| 508 |
+
A = np.empty((n, n))
|
| 509 |
+
x, y = c[:, 0], c[:, 1]
|
| 510 |
+
|
| 511 |
+
def rows(r0, r1):
|
| 512 |
+
d = x[r0:r1, None] - x[None, r0:]
|
| 513 |
+
np.multiply(d, d, out=d)
|
| 514 |
+
t = y[r0:r1, None] - y[None, r0:]
|
| 515 |
+
np.multiply(t, t, out=t)
|
| 516 |
+
d += t
|
| 517 |
+
np.sqrt(d, out=d)
|
| 518 |
+
corr = _image_correction(d, eps_r)
|
| 519 |
+
d[np.arange(r1 - r0), np.arange(r1 - r0)] = 1.0
|
| 520 |
+
out = A[r0:r1, r0:]
|
| 521 |
+
np.divide(1.0, d, out=out)
|
| 522 |
+
if corr is not None:
|
| 523 |
+
out += corr
|
| 524 |
+
out /= k
|
| 525 |
+
|
| 526 |
+
_run([partial(rows, a, b) for a, b in _spans(n, 64)])
|
| 527 |
+
ii, jj = _near_pairs(c, wh.max(1))
|
| 528 |
+
img = _image_correction(np.zeros(1), eps_r)
|
| 529 |
+
if len(ii):
|
| 530 |
+
A[ii, jj] = 0.5 * (near(c[ii], c[jj], wh[jj]) + near(c[jj], c[ii], wh[ii])) / k
|
| 531 |
+
if img is not None:
|
| 532 |
+
A[ii, jj] += _image_correction(np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)), eps_r) / k
|
| 533 |
+
A[np.arange(n), np.arange(n)] = diag / k if img is None else (diag + img[0]) / k
|
| 534 |
+
return A.T
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
def _chol_solve(P, B):
|
| 538 |
+
"""Cholesky solve with the lower triangle of the Fortran-ordered P from `_assemble`, factorised in place (no copy)."""
|
| 539 |
+
return cho_solve(cho_factor(P, lower=True, overwrite_a=True, check_finite=False), B, check_finite=False)
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
def mom_capacitance_gds(metals: list, eps_r: float = 11.45, max_cells: int = 8000, h_min: float | None = None, grade: float = 2.0,
|
| 543 |
+
cells_per_gap: float = 3.0, offset: tuple = (0.0, 0.0), mesh: str = "v2"):
|
| 544 |
+
"""Thin-film MoM on the exact GDS polygons (ground to the chip edge), adaptive quadtree mesh (see `mom_mesh`).
|
| 545 |
+
|
| 546 |
+
metals: shapely geometries, one per conductor (index 0 = ground). `offset` (v2 mesh only) shifts the quadtree origin
|
| 547 |
+
by that fraction of the coarsest cell. Returns (C fF in the same order, n_cells, h_min)."""
|
| 548 |
+
c, h, o, h_min = mom_mesh(metals, max_cells, h_min, grade, cells_per_gap, mesh, offset)
|
| 549 |
+
try:
|
| 550 |
+
Q = mom_solve(c, h, o, len(metals), eps_r) # v3: exact near field if the 3x3 system is not positive definite
|
| 551 |
+
except np.linalg.LinAlgError:
|
| 552 |
+
if mesh != "v3":
|
| 553 |
+
raise
|
| 554 |
+
# cell cap coarser than the finger pitch: cut-panel boxes of different conductors overlap; finest cells as whole
|
| 555 |
+
# squares owned by the metal covering >= 50 % (as the v2 mesh), exact near field
|
| 556 |
+
print("v3: still not positive definite, majority finest cells", flush=True)
|
| 557 |
+
c, h, o = anchored_cells(metals, h_min, grade, majority=True)
|
| 558 |
+
Q = _mom_solve_exact_near(c, h, o, len(metals), eps_r)
|
| 559 |
+
C = np.stack([Q[o == a].sum(0) for a in range(len(metals))])
|
| 560 |
+
return 0.5 * (C + C.T), len(c), h_min
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
def mom_capacitance_gds_avg(metals: list, n_offsets: int = 4, **kw):
|
| 564 |
+
"""MoM averaged over quadtree origins (offsets on a diagonal grid of the coarsest cell): removes the part of the
|
| 565 |
+
discretisation error that jumps with mesh alignment as the geometry is swept."""
|
| 566 |
+
offs = [(i / n_offsets, ((i * 3) % n_offsets) / n_offsets) for i in range(n_offsets)]
|
| 567 |
+
runs = [mom_capacitance_gds(metals, offset=o, mesh="v2", **kw) for o in offs]
|
| 568 |
+
return np.mean([r[0] for r in runs], 0), int(np.mean([r[1] for r in runs])), runs[0][2]
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
def _both(f, g):
|
| 572 |
+
"""(f(), g()), a LinAlgError in place of a result; concurrently (g on a helper thread) when more than one thread is
|
| 573 |
+
allowed: the gap/6 and gap/3 solves of v3-rich are independent."""
|
| 574 |
+
def call(fn):
|
| 575 |
+
try:
|
| 576 |
+
return fn()
|
| 577 |
+
except np.linalg.LinAlgError as e:
|
| 578 |
+
return e
|
| 579 |
+
if _nthreads() < 2:
|
| 580 |
+
return call(f), call(g)
|
| 581 |
+
with ThreadPoolExecutor(1, thread_name_prefix="tlsq-rich") as ex:
|
| 582 |
+
fut = ex.submit(call, g)
|
| 583 |
+
a = call(f)
|
| 584 |
+
return a, fut.result()
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
def mom_capacitance_gds_rich(metals: list, eps_r: float = 11.45, cells_per_gap: float = 3.0, max_cells: int = 8000,
|
| 588 |
+
max_cells_fine: int = 16000, grade: float = 2.0):
|
| 589 |
+
"""v3 MoM with Richardson extrapolation: solves at gap/cells_per_gap and at half that cell size on the same
|
| 590 |
+
conductor-anchored lattice and extrapolates the first-order discretisation error to zero cell size. If the cell
|
| 591 |
+
cap forces both meshes to the same resolution, returns the finer solve. If the coarse system is not positive
|
| 592 |
+
definite (sliver edge panels badly resolved by the 3x3 near-field rule; ~0.2 % of designs), returns the other
|
| 593 |
+
solve alone (fine if the coarse one fails, coarse if the fine one fails). The two solves run concurrently when more
|
| 594 |
+
than one thread is allowed. Returns (C, total cells, h of the fine mesh)."""
|
| 595 |
+
rf, rc = _both(partial(mom_capacitance_gds, metals, eps_r, max_cells_fine, None, grade, 2 * cells_per_gap, mesh="v3"),
|
| 596 |
+
partial(mom_capacitance_gds, metals, eps_r, max_cells, None, grade, cells_per_gap, mesh="v3"))
|
| 597 |
+
if isinstance(rf, np.linalg.LinAlgError):
|
| 598 |
+
print("v3-rich: fine mesh not positive definite, coarse solve only", flush=True)
|
| 599 |
+
if isinstance(rc, np.linalg.LinAlgError):
|
| 600 |
+
raise rc
|
| 601 |
+
return rc
|
| 602 |
+
Cf, nf, hf = rf
|
| 603 |
+
if isinstance(rc, np.linalg.LinAlgError):
|
| 604 |
+
print("v3-rich: coarse mesh not positive definite, fine solve only", flush=True)
|
| 605 |
+
return Cf, nf, hf
|
| 606 |
+
Cc, nc, hc = rc
|
| 607 |
+
r = hc / hf
|
| 608 |
+
C = Cf if r < 1.01 else Cf + (Cf - Cc) / (r - 1)
|
| 609 |
+
return C, nc + nf, hf
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
# ---------------------------------------------------------------- flagship prior: strip-merged mesh, H-matrix solve, Richardson
|
| 613 |
+
# Resolution-controlled (panels per gap, no small cap), the same conductor-anchored panels as v3 merged along straight
|
| 614 |
+
# edges, the exact rectangle potential for near pairs, and for large systems an H-matrix (ACA far field) with
|
| 615 |
+
# preconditioned CG instead of the dense Cholesky: O(n log n) time and memory.
|
| 616 |
+
|
| 617 |
+
DENSE_MAX = int(os.environ.get("TLSQ_MOM_DENSE_MAX", 12000)) # panels up to which the dense Cholesky is used (faster below this size)
|
| 618 |
+
HMAT = dict(leaf=128, eta=2.0, eps=1e-9, tol=1e-12, maxit=1000) # ACA block tolerance (relative Frobenius) and PCG residual
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
def _segments(metals):
|
| 622 |
+
segs, verts = [], []
|
| 623 |
+
for g in metals:
|
| 624 |
+
for p in getattr(g, "geoms", [g]):
|
| 625 |
+
for ring in [p.exterior, *p.interiors]:
|
| 626 |
+
xy = np.asarray(ring.coords)
|
| 627 |
+
verts.append(xy[:-1])
|
| 628 |
+
segs.append(np.stack([xy[:-1], xy[1:]], 1))
|
| 629 |
+
S = np.concatenate(segs)
|
| 630 |
+
return shapely.linestrings(S), S, np.concatenate(verts)
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
def strip_merge(c, wh, o, metals, h_f, lmax_mult: float = 16.0, grade: float = 1.0):
|
| 634 |
+
"""Merge runs of equal-width panels along the direction of their nearest polygon edge (where the charge density is
|
| 635 |
+
translation-invariant) into panels of length <= lmax_mult * h_f and <= grade * distance to the nearest vertex;
|
| 636 |
+
the size across the edge is kept. Returns merged (centres, sizes, owners)."""
|
| 637 |
+
lines, S, verts = _segments(metals)
|
| 638 |
+
near = shapely.STRtree(lines).nearest(shapely.points(c))
|
| 639 |
+
d = S[near, 1] - S[near, 0]
|
| 640 |
+
horiz = np.abs(d[:, 0]) >= np.abs(d[:, 1])
|
| 641 |
+
dv, _ = cKDTree(verts).query(c, workers=-1)
|
| 642 |
+
allow = np.minimum(lmax_mult * h_f, grade * dv)
|
| 643 |
+
out_c, out_wh, out_o = [], [], []
|
| 644 |
+
for axis, mask in ((0, horiz), (1, ~horiz)):
|
| 645 |
+
if not mask.any():
|
| 646 |
+
continue
|
| 647 |
+
idx = np.nonzero(mask)[0]
|
| 648 |
+
other = 1 - axis
|
| 649 |
+
key = np.stack([o[idx], np.round(c[idx, other], 4), np.round(wh[idx, other], 4)], 1)
|
| 650 |
+
order = np.lexsort([c[idx, axis], key[:, 2], key[:, 1], key[:, 0]])
|
| 651 |
+
idx, key = idx[order], key[order]
|
| 652 |
+
same = np.all(key[1:] == key[:-1], 1)
|
| 653 |
+
lo, hi = c[idx, axis] - wh[idx, axis] / 2, c[idx, axis] + wh[idx, axis] / 2
|
| 654 |
+
adjacent = same & (np.abs(hi[:-1] - lo[1:]) < 1e-6)
|
| 655 |
+
start = 0
|
| 656 |
+
while start < len(idx):
|
| 657 |
+
end, length, cap = start, wh[idx[start], axis], allow[idx[start]]
|
| 658 |
+
while end + 1 < len(idx) and adjacent[end] and length + wh[idx[end + 1], axis] <= min(cap, allow[idx[end + 1]]):
|
| 659 |
+
end += 1
|
| 660 |
+
length += wh[idx[end], axis]
|
| 661 |
+
cap = min(cap, allow[idx[end]])
|
| 662 |
+
cc, ww = np.zeros(2), np.zeros(2)
|
| 663 |
+
cc[axis], cc[other] = 0.5 * (lo[start] + hi[end]), c[idx[start], other]
|
| 664 |
+
ww[axis], ww[other] = hi[end] - lo[start], wh[idx[start], other]
|
| 665 |
+
out_c.append(cc)
|
| 666 |
+
out_wh.append(ww)
|
| 667 |
+
out_o.append(o[idx[start]])
|
| 668 |
+
start = end + 1
|
| 669 |
+
return np.array(out_c), np.array(out_wh), np.array(out_o)
|
| 670 |
+
|
| 671 |
+
|
| 672 |
+
def mesh_flagship(metals: list, h: float, max_cells: int, grade: float = 2.0, lmax_mult: float = 16.0, merge_grade: float = 1.0,
|
| 673 |
+
majority: bool = False):
|
| 674 |
+
"""Strip-merged v3 mesh at finest panel h, doubled (same lattice) until the merged mesh has at most max_cells panels.
|
| 675 |
+
Returns (centres, sizes, owners, h, raw panel count)."""
|
| 676 |
+
while True:
|
| 677 |
+
raw = anchored_cells(metals, h, grade, majority=majority, limit=8 * max_cells)
|
| 678 |
+
if raw is not None:
|
| 679 |
+
c, wh, o = strip_merge(*raw, metals, h, lmax_mult, merge_grade)
|
| 680 |
+
if len(c) <= max_cells:
|
| 681 |
+
return c, wh, o, h, len(raw[0])
|
| 682 |
+
h *= 2.0
|
| 683 |
+
|
| 684 |
+
|
| 685 |
+
class _Kernel:
|
| 686 |
+
"""Entries of P / (4 pi eps0 eps_eff) in a permuted panel order: 1 / d for far pairs, the exact rectangle potential
|
| 687 |
+
for near pairs (d < 1.5 max side) and rect_self on the diagonal; near and diagonal corrections to 1 / d are held in
|
| 688 |
+
a CSR matrix so that any block is 1 / d + N[I, J] (the same numbers as `_assemble` with `rect_potential`)."""
|
| 689 |
+
|
| 690 |
+
def __init__(self, c, wh, eps_r, perm):
|
| 691 |
+
from scipy.sparse import csr_matrix
|
| 692 |
+
|
| 693 |
+
self.x, self.y = c[perm, 0].copy(), c[perm, 1].copy()
|
| 694 |
+
self.k = 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2
|
| 695 |
+
self.eps_r = eps_r
|
| 696 |
+
self.img = _image_correction(np.zeros(1), eps_r) is not None # finite-substrate image term (exact series in blocks / rows)
|
| 697 |
+
n = len(c)
|
| 698 |
+
ii, jj = _near_pairs(c, wh.max(1))
|
| 699 |
+
d = np.sqrt(((c[ii] - c[jj]) ** 2).sum(1))
|
| 700 |
+
near = 0.5 * (rect_potential(c[ii], c[jj], wh[jj]) + rect_potential(c[jj], c[ii], wh[ii])) - 1.0 / d
|
| 701 |
+
inv = np.empty(n, np.int64)
|
| 702 |
+
inv[perm] = np.arange(n)
|
| 703 |
+
rows = inv[np.concatenate([ii, jj, np.arange(n)])]
|
| 704 |
+
cols = inv[np.concatenate([jj, ii, np.arange(n)])]
|
| 705 |
+
vals = np.concatenate([near, near, rect_self(wh[:, 0], wh[:, 1]) - 1.0])
|
| 706 |
+
self.N = csr_matrix((vals, (rows, cols)), shape=(n, n))
|
| 707 |
+
|
| 708 |
+
def block(self, a0, a1, b0, b1):
|
| 709 |
+
d = np.sqrt((self.x[a0:a1, None] - self.x[None, b0:b1]) ** 2 + (self.y[a0:a1, None] - self.y[None, b0:b1]) ** 2)
|
| 710 |
+
corr = _image_correction(d, self.eps_r, exact=True)
|
| 711 |
+
if a0 == b0:
|
| 712 |
+
d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0
|
| 713 |
+
A = 1.0 / d
|
| 714 |
+
if corr is not None:
|
| 715 |
+
A += corr
|
| 716 |
+
Nb = self.N[a0:a1, b0:b1]
|
| 717 |
+
if Nb.nnz:
|
| 718 |
+
A += Nb.toarray()
|
| 719 |
+
return A / self.k
|
| 720 |
+
|
| 721 |
+
def row(self, i, b0, b1):
|
| 722 |
+
d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2)
|
| 723 |
+
r = 1.0 / d
|
| 724 |
+
if self.img:
|
| 725 |
+
r += _image_correction(d, self.eps_r, exact=True)
|
| 726 |
+
Nr = self.N[i, b0:b1]
|
| 727 |
+
if Nr.nnz:
|
| 728 |
+
r += Nr.toarray()[0]
|
| 729 |
+
return r / self.k
|
| 730 |
+
|
| 731 |
+
|
| 732 |
+
def _cluster_tree(c, leaf: int):
|
| 733 |
+
"""Recursive bisection of the panel centres along the longer side of their bounding box at the median: a permutation
|
| 734 |
+
making every cluster a contiguous range, and nodes (a, b, lo, hi, left, right)."""
|
| 735 |
+
perm, nodes = np.arange(len(c)), []
|
| 736 |
+
|
| 737 |
+
def rec(a, b):
|
| 738 |
+
idx = perm[a:b]
|
| 739 |
+
lo, hi = c[idx].min(0), c[idx].max(0)
|
| 740 |
+
k = len(nodes)
|
| 741 |
+
nodes.append([a, b, lo, hi, -1, -1])
|
| 742 |
+
if b - a > leaf:
|
| 743 |
+
ax = int(np.argmax(hi - lo))
|
| 744 |
+
perm[a:b] = idx[np.argsort(c[idx, ax], kind="stable")]
|
| 745 |
+
m = a + (b - a) // 2
|
| 746 |
+
nodes[k][4], nodes[k][5] = rec(a, m), rec(m, b)
|
| 747 |
+
return k
|
| 748 |
+
|
| 749 |
+
rec(0, len(c))
|
| 750 |
+
return perm, nodes
|
| 751 |
+
|
| 752 |
+
|
| 753 |
+
def _aca(K, a0, a1, b0, b1, eps, max_rank):
|
| 754 |
+
"""Partial-pivoting adaptive cross approximation of the block (a0:a1, b0:b1) to relative Frobenius accuracy eps:
|
| 755 |
+
(U, V) with block ~ U V^T, or None when the rank would exceed max_rank."""
|
| 756 |
+
m, n = a1 - a0, b1 - b0
|
| 757 |
+
U, V, used_r, used_c = [], [], np.zeros(m, bool), np.zeros(n, bool)
|
| 758 |
+
i, norm2 = 0, 0.0
|
| 759 |
+
for _ in range(min(m, n, max_rank)):
|
| 760 |
+
r = K.row(a0 + i, b0, b1)
|
| 761 |
+
for u, v in zip(U, V):
|
| 762 |
+
r -= u[i] * v
|
| 763 |
+
rm = np.where(used_c, 0.0, np.abs(r))
|
| 764 |
+
j = int(np.argmax(rm))
|
| 765 |
+
if rm[j] == 0.0:
|
| 766 |
+
break
|
| 767 |
+
v = r / r[j]
|
| 768 |
+
u = K.row(b0 + j, a0, a1) # column j of the block = row j of the symmetric kernel
|
| 769 |
+
for uu, vv in zip(U, V):
|
| 770 |
+
u -= vv[j] * uu
|
| 771 |
+
used_r[i], used_c[j] = True, True
|
| 772 |
+
nu, nv = float(u @ u), float(v @ v)
|
| 773 |
+
norm2 += nu * nv + 2 * sum(float(u @ uu) * float(v @ vv) for uu, vv in zip(U, V))
|
| 774 |
+
U.append(u)
|
| 775 |
+
V.append(v)
|
| 776 |
+
if nu * nv <= eps**2 * norm2:
|
| 777 |
+
break
|
| 778 |
+
um = np.where(used_r, 0.0, np.abs(u))
|
| 779 |
+
i = int(np.argmax(um))
|
| 780 |
+
if um[i] == 0.0:
|
| 781 |
+
break
|
| 782 |
+
else:
|
| 783 |
+
return None
|
| 784 |
+
return np.stack(U, 1), np.stack(V, 1)
|
| 785 |
+
|
| 786 |
+
|
| 787 |
+
class _HMatrix:
|
| 788 |
+
"""Hierarchical matrix of the panel system: dense blocks for leaf pairs that are not well separated, ACA low-rank
|
| 789 |
+
blocks for admissible pairs (min diameter <= eta x distance); only the upper block triangle is stored (symmetric)."""
|
| 790 |
+
|
| 791 |
+
def __init__(self, c, wh, eps_r, leaf=128, eta=2.0, eps=1e-9):
|
| 792 |
+
self.n = len(c)
|
| 793 |
+
self.perm, nodes = _cluster_tree(c, leaf)
|
| 794 |
+
self.K = _Kernel(c, wh, eps_r, self.perm)
|
| 795 |
+
self.eps = eps
|
| 796 |
+
plan = []
|
| 797 |
+
|
| 798 |
+
def leafq(k):
|
| 799 |
+
return nodes[k][4] < 0
|
| 800 |
+
|
| 801 |
+
def rec(s, t):
|
| 802 |
+
a0, a1, lo_s, hi_s, sl, sr = nodes[s]
|
| 803 |
+
b0, b1, lo_t, hi_t, tl, tr = nodes[t]
|
| 804 |
+
if s == t:
|
| 805 |
+
if leafq(s):
|
| 806 |
+
plan.append(("d", a0, a1, a0, a1))
|
| 807 |
+
else:
|
| 808 |
+
rec(sl, sl), rec(sr, sr), rec(sl, sr)
|
| 809 |
+
return
|
| 810 |
+
gap = np.maximum(0.0, np.maximum(lo_s - hi_t, lo_t - hi_s))
|
| 811 |
+
diam = min(np.sqrt(((hi_s - lo_s) ** 2).sum()), np.sqrt(((hi_t - lo_t) ** 2).sum()))
|
| 812 |
+
if diam <= eta * np.sqrt((gap**2).sum()):
|
| 813 |
+
plan.append(("l", a0, a1, b0, b1))
|
| 814 |
+
elif leafq(s) and leafq(t):
|
| 815 |
+
plan.append(("d", a0, a1, b0, b1))
|
| 816 |
+
elif leafq(s):
|
| 817 |
+
rec(s, tl), rec(s, tr)
|
| 818 |
+
elif leafq(t):
|
| 819 |
+
rec(sl, t), rec(sr, t)
|
| 820 |
+
else:
|
| 821 |
+
for u in (sl, sr):
|
| 822 |
+
for v in (tl, tr):
|
| 823 |
+
rec(u, v)
|
| 824 |
+
|
| 825 |
+
rec(0, 0)
|
| 826 |
+
self.dense, self.low = [], []
|
| 827 |
+
|
| 828 |
+
def build(chunk):
|
| 829 |
+
out = []
|
| 830 |
+
for kind, a0, a1, b0, b1 in chunk:
|
| 831 |
+
if kind == "l":
|
| 832 |
+
r = _aca(self.K, a0, a1, b0, b1, eps, max(2, min(a1 - a0, b1 - b0) // 2))
|
| 833 |
+
if r is not None:
|
| 834 |
+
out.append(("l", a0, a1, b0, b1, r))
|
| 835 |
+
continue
|
| 836 |
+
out.append(("d", a0, a1, b0, b1, self.K.block(a0, a1, b0, b1)))
|
| 837 |
+
return out
|
| 838 |
+
|
| 839 |
+
for res in _run([partial(build, plan[a:b]) for a, b in _spans(len(plan), 64)]):
|
| 840 |
+
for kind, a0, a1, b0, b1, M in res:
|
| 841 |
+
(self.low if kind == "l" else self.dense).append((a0, a1, b0, b1, M))
|
| 842 |
+
self.pre = [(a0, a1, cho_factor(D, lower=True)) for a0, a1, b0, _, D in self.dense if a0 == b0] # block Jacobi
|
| 843 |
+
self.n_threads = max(1, _nthreads())
|
| 844 |
+
|
| 845 |
+
|
| 846 |
+
def nbytes(self):
|
| 847 |
+
return sum(D.nbytes for *_, D in self.dense) + sum(U.nbytes + V.nbytes for *_, (U, V) in self.low)
|
| 848 |
+
|
| 849 |
+
def matvec(self, X):
|
| 850 |
+
def part(dense, low):
|
| 851 |
+
Y = np.zeros_like(X)
|
| 852 |
+
for a0, a1, b0, b1, D in dense:
|
| 853 |
+
Y[a0:a1] += D @ X[b0:b1]
|
| 854 |
+
if a0 != b0:
|
| 855 |
+
Y[b0:b1] += D.T @ X[a0:a1]
|
| 856 |
+
for a0, a1, b0, b1, (U, V) in low:
|
| 857 |
+
Y[a0:a1] += U @ (V.T @ X[b0:b1])
|
| 858 |
+
Y[b0:b1] += V @ (U.T @ X[a0:a1])
|
| 859 |
+
return Y
|
| 860 |
+
|
| 861 |
+
T = self.n_threads
|
| 862 |
+
if T < 2:
|
| 863 |
+
return part(self.dense, self.low)
|
| 864 |
+
return sum(_run([partial(part, self.dense[k::T], self.low[k::T]) for k in range(T)]))
|
| 865 |
+
|
| 866 |
+
def precondition(self, R):
|
| 867 |
+
Z = np.empty_like(R)
|
| 868 |
+
for a0, a1, cf in self.pre:
|
| 869 |
+
Z[a0:a1] = cho_solve(cf, R[a0:a1], check_finite=False)
|
| 870 |
+
return Z
|
| 871 |
+
|
| 872 |
+
def pcg(self, B, tol, maxit):
|
| 873 |
+
"""Conjugate gradients on every right-hand side column at once (independent columns), block-Jacobi preconditioned,
|
| 874 |
+
until every column's relative residual is below tol. B and the result are in the permuted order."""
|
| 875 |
+
X, R = np.zeros_like(B), B.copy()
|
| 876 |
+
Z = self.precondition(R)
|
| 877 |
+
P, rz = Z.copy(), (R * Z).sum(0)
|
| 878 |
+
bn = np.sqrt((B * B).sum(0))
|
| 879 |
+
for it in range(maxit):
|
| 880 |
+
AP = self.matvec(P)
|
| 881 |
+
alpha = rz / (P * AP).sum(0)
|
| 882 |
+
X += alpha * P
|
| 883 |
+
R -= alpha * AP
|
| 884 |
+
res = (np.sqrt((R * R).sum(0)) / bn).max()
|
| 885 |
+
if res < tol:
|
| 886 |
+
return X, it + 1, res
|
| 887 |
+
Z = self.precondition(R)
|
| 888 |
+
rz_new = (R * Z).sum(0)
|
| 889 |
+
P = Z + (rz_new / rz) * P
|
| 890 |
+
rz = rz_new
|
| 891 |
+
raise np.linalg.LinAlgError(f"H-matrix PCG did not converge in {maxit} iterations (residual {res:.1e})")
|
| 892 |
+
|
| 893 |
+
|
| 894 |
+
def mom_solve_fast(c, wh, o, n_metals: int, eps_r: float = 11.45, dense_max: int | None = None):
|
| 895 |
+
"""Panel charges Q (n, n_metals) for unit excitation of every conductor, exact near field: dense Cholesky for
|
| 896 |
+
n <= dense_max panels (`_mom_solve_exact_near`), otherwise the H-matrix / PCG solve of the same system."""
|
| 897 |
+
n = len(c)
|
| 898 |
+
if n <= (DENSE_MAX if dense_max is None else dense_max):
|
| 899 |
+
return _mom_solve_exact_near(c, wh, o, n_metals, eps_r)
|
| 900 |
+
H = _HMatrix(c, wh, eps_r, HMAT["leaf"], HMAT["eta"], HMAT["eps"])
|
| 901 |
+
B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float)[H.perm]
|
| 902 |
+
Xp, _, _ = H.pcg(B, HMAT["tol"], HMAT["maxit"])
|
| 903 |
+
Q = np.empty_like(Xp)
|
| 904 |
+
Q[H.perm] = Xp
|
| 905 |
+
return Q
|
| 906 |
+
|
| 907 |
+
|
| 908 |
+
def _cap_flagship(metals, eps_r, h, max_cells, grade, lmax_mult, merge_grade):
|
| 909 |
+
c, wh, o, h, n_raw = mesh_flagship(metals, h, max_cells, grade, lmax_mult, merge_grade)
|
| 910 |
+
try:
|
| 911 |
+
Q = mom_solve_fast(c, wh, o, len(metals), eps_r)
|
| 912 |
+
except np.linalg.LinAlgError: # cut panels of different conductors overlap (lattice coarser than the finger pitch)
|
| 913 |
+
print("flagship: system not positive definite, majority finest cells", flush=True)
|
| 914 |
+
c, wh, o, h, n_raw = mesh_flagship(metals, h, max_cells, grade, lmax_mult, merge_grade, majority=True)
|
| 915 |
+
Q = mom_solve_fast(c, wh, o, len(metals), eps_r)
|
| 916 |
+
C = np.stack([Q[o == a].sum(0) for a in range(len(metals))])
|
| 917 |
+
return 0.5 * (C + C.T), len(c), h, n_raw
|
| 918 |
+
|
| 919 |
+
|
| 920 |
+
def mom_capacitance_gds_flagship(metals: list, eps_r: float = 11.45, cells_per_gap: float = 3.0, max_cells: int = 250000,
|
| 921 |
+
grade: float = 2.0, rich: bool = True, lmax_mult: float = 16.0, merge_grade: float = 1.0):
|
| 922 |
+
"""Flagship prior: strip-merged conductor-anchored mesh at h_f = max(gap / (2 cells_per_gap), size / 8192) and, with
|
| 923 |
+
rich=True, Richardson extrapolation with the mesh at 2 h_f (same lattice), both solved with `mom_solve_fast`. The
|
| 924 |
+
resolution is set by cells_per_gap (the fine mesh has 2 cells_per_gap panels across the smallest gap; the coarse
|
| 925 |
+
one cells_per_gap); max_cells is only a safety cap on the merged panel count (h doubles until it fits). The two solves
|
| 926 |
+
run concurrently when more than one thread is allowed. Returns (C, total panels, h of the fine mesh, info dict)."""
|
| 927 |
+
bbox = shapely.union_all(metals).bounds
|
| 928 |
+
size = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
|
| 929 |
+
gap = min(shapely.distance(metals[i], metals[j]) for i in range(len(metals)) for j in range(i + 1, len(metals)))
|
| 930 |
+
hf = max(gap / (2 * cells_per_gap), size / 8192)
|
| 931 |
+
if not rich:
|
| 932 |
+
C, n, h, n_raw = _cap_flagship(metals, eps_r, 2 * hf, max_cells, grade, lmax_mult, merge_grade)
|
| 933 |
+
return C, n, h, {"h_target_um": 2 * hf, "min_gap_um": gap, "cap_hit": h > 2 * hf * 1.0001, "raw_cells": n_raw, "extrapolated": False}
|
| 934 |
+
rf, rc = _both(partial(_cap_flagship, metals, eps_r, hf, max_cells, grade, lmax_mult, merge_grade),
|
| 935 |
+
partial(_cap_flagship, metals, eps_r, 2 * hf, max_cells, grade, lmax_mult, merge_grade))
|
| 936 |
+
for r in (rf, rc):
|
| 937 |
+
if isinstance(r, np.linalg.LinAlgError):
|
| 938 |
+
raise r
|
| 939 |
+
(Cf, nf, h_f, raw_f), (Cc, nc, h_c, raw_c) = rf, rc
|
| 940 |
+
r = h_c / h_f
|
| 941 |
+
C = Cf if r < 1.01 else Cf + (Cf - Cc) / (r - 1)
|
| 942 |
+
info = {"h_target_um": hf, "min_gap_um": gap, "cap_hit": h_f > hf * 1.0001, "raw_cells": raw_f, "coarse_cells": nc,
|
| 943 |
+
"extrapolated": bool(r >= 1.01)}
|
| 944 |
+
return C, nf + nc, h_f, info
|
geomgnn/_tlsq/geometry.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""GDS conductor geometry: exact polygon metrics, separation spectra and spectral shape descriptors.
|
| 2 |
+
|
| 3 |
+
Only metal layers are used: (1,0) ground and (1,10) signal conductors. Port stubs (2,0)/(3,0) are ignored.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from dataclasses import dataclass, field
|
| 9 |
+
|
| 10 |
+
import gdstk
|
| 11 |
+
import numpy as np
|
| 12 |
+
import shapely
|
| 13 |
+
from scipy import sparse
|
| 14 |
+
from scipy.sparse.linalg import eigsh
|
| 15 |
+
from scipy.spatial import cKDTree
|
| 16 |
+
from shapely.geometry import MultiPolygon, Polygon
|
| 17 |
+
|
| 18 |
+
GROUND_LAYER = (1, 0)
|
| 19 |
+
SIGNAL_LAYER = (1, 10)
|
| 20 |
+
BOUNDARY_STEP_UM = 0.25
|
| 21 |
+
SEP_BINS_UM = np.geomspace(0.5, 2000.0, 41)
|
| 22 |
+
N_FOURIER = 16
|
| 23 |
+
MIC_TOL = [0.01] # inscribed-circle tolerance (um); 0.1 for the approximate token variants (PRIOR_SCHEME.md 14.3.1)
|
| 24 |
+
RASTER_PX = 512
|
| 25 |
+
N_EIG = 16
|
| 26 |
+
K_BINS = 48
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@dataclass
|
| 30 |
+
class Conductors:
|
| 31 |
+
N: Polygon
|
| 32 |
+
S: Polygon
|
| 33 |
+
G: Polygon | MultiPolygon
|
| 34 |
+
pocket: Polygon | MultiPolygon # etched region inside the chip (contains N and S)
|
| 35 |
+
gap: Polygon | MultiPolygon # pocket minus signal metal (dielectric surface)
|
| 36 |
+
chip: Polygon
|
| 37 |
+
pads: list = field(default_factory=list) # extra 0 V bodies (merged into G)
|
| 38 |
+
|
| 39 |
+
def metals(self) -> dict:
|
| 40 |
+
return {"N": self.N, "S": self.S, "G": self.G}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _valid(geom):
|
| 44 |
+
g = shapely.make_valid(geom)
|
| 45 |
+
polys = [p for p in getattr(g, "geoms", [g]) if isinstance(p, (Polygon, MultiPolygon))]
|
| 46 |
+
return shapely.union_all(polys)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def load_conductors(gds_path) -> Conductors:
|
| 50 |
+
lib = gdstk.read_gds(str(gds_path))
|
| 51 |
+
cell = lib.top_level()[0]
|
| 52 |
+
to_um = lib.unit / 1e-6
|
| 53 |
+
sig = [p for p in cell.polygons if (p.layer, p.datatype) == SIGNAL_LAYER]
|
| 54 |
+
gnd = [p for p in cell.polygons if (p.layer, p.datatype) == GROUND_LAYER]
|
| 55 |
+
if len(sig) < 2 or len(gnd) != 1:
|
| 56 |
+
raise ValueError(f"expected >=2 signal + 1 ground polygons, got {len(sig)} + {len(gnd)}")
|
| 57 |
+
polys = [_valid(Polygon(p.points * to_um)) for p in sig]
|
| 58 |
+
if len(polys) == 2:
|
| 59 |
+
N, S = polys
|
| 60 |
+
pads = []
|
| 61 |
+
else:
|
| 62 |
+
# multi-terminal (exp12): GDS order is not the net order. S = star at the origin; N = body fed by the
|
| 63 |
+
# single port stub; the remaining pads are held at 0 V in the solve and are merged into ground.
|
| 64 |
+
port = shapely.union_all([Polygon(p.points * to_um) for p in cell.polygons if (p.layer, p.datatype) != SIGNAL_LAYER and p.layer != GROUND_LAYER[0]])
|
| 65 |
+
s = next((i for i, g in enumerate(polys) if g.contains(shapely.Point(0, 0))), int(np.argmax([g.area for g in polys])))
|
| 66 |
+
n = min((i for i in range(len(polys)) if i != s), key=lambda i: polys[i].distance(port))
|
| 67 |
+
N, S = polys[n], polys[s]
|
| 68 |
+
pads = [g for i, g in enumerate(polys) if i not in (n, s)]
|
| 69 |
+
G0 = _valid(Polygon(gnd[0].points * to_um))
|
| 70 |
+
if abs(G0.area - gnd[0].area() * to_um**2) > 1e-6 * G0.area:
|
| 71 |
+
raise ValueError("ground polygon area changed during repair")
|
| 72 |
+
chip = shapely.box(*G0.bounds)
|
| 73 |
+
pocket = chip.difference(G0)
|
| 74 |
+
G = shapely.union_all([G0, *pads]) if pads else G0
|
| 75 |
+
gap = pocket.difference(N).difference(S)
|
| 76 |
+
for g in pads:
|
| 77 |
+
gap = gap.difference(g)
|
| 78 |
+
return Conductors(N, S, G, pocket, gap, chip, pads)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def sample_boundary(geom, step: float = BOUNDARY_STEP_UM) -> np.ndarray:
|
| 82 |
+
rings = []
|
| 83 |
+
for poly in getattr(geom, "geoms", [geom]):
|
| 84 |
+
rings += [poly.exterior, *poly.interiors]
|
| 85 |
+
pts = []
|
| 86 |
+
for r in rings:
|
| 87 |
+
n = max(int(np.ceil(r.length / step)), 4)
|
| 88 |
+
pts.append(shapely.get_coordinates(shapely.line_interpolate_point(r, np.linspace(0, r.length, n, endpoint=False))))
|
| 89 |
+
return np.concatenate(pts)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def boundaries(c: Conductors) -> dict[str, np.ndarray]:
|
| 93 |
+
# ground edge = boundary of the etched pocket
|
| 94 |
+
g = [sample_boundary(c.pocket)] + [sample_boundary(p) for p in c.pads]
|
| 95 |
+
return {"N": sample_boundary(c.N), "S": sample_boundary(c.S), "G": np.concatenate(g)}
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def shape_metrics(poly) -> dict:
|
| 99 |
+
hull = poly.convex_hull
|
| 100 |
+
x0, y0, x1, y1 = poly.bounds
|
| 101 |
+
cx, cy = poly.centroid.x, poly.centroid.y
|
| 102 |
+
ixx, iyy, ixy = _second_moments(poly, cx, cy)
|
| 103 |
+
evals = np.linalg.eigvalsh([[ixx, ixy], [ixy, iyy]])
|
| 104 |
+
return {
|
| 105 |
+
"area": poly.area,
|
| 106 |
+
"perimeter": poly.length,
|
| 107 |
+
"convex_area": hull.area,
|
| 108 |
+
"solidity": poly.area / hull.area,
|
| 109 |
+
"compactness": 4 * np.pi * poly.area / poly.length**2,
|
| 110 |
+
"bbox_w": x1 - x0,
|
| 111 |
+
"bbox_h": y1 - y0,
|
| 112 |
+
"cx": cx,
|
| 113 |
+
"cy": cy,
|
| 114 |
+
"I_major": evals[1],
|
| 115 |
+
"I_minor": evals[0],
|
| 116 |
+
"elongation": np.sqrt(evals[1] / max(evals[0], 1e-30)),
|
| 117 |
+
"inscribed_radius": shapely.maximum_inscribed_circle(poly, MIC_TOL[0]).length if isinstance(poly, Polygon) else np.nan,
|
| 118 |
+
"n_vertices": int(shapely.get_num_coordinates(poly)),
|
| 119 |
+
"n_holes": int(sum(len(p.interiors) for p in getattr(poly, "geoms", [poly]))),
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def _second_moments(geom, cx, cy) -> tuple[float, float, float]:
|
| 124 |
+
ixx = iyy = ixy = 0.0
|
| 125 |
+
for poly in getattr(geom, "geoms", [geom]):
|
| 126 |
+
for k, ring in enumerate([poly.exterior, *poly.interiors]):
|
| 127 |
+
xy = np.asarray(ring.coords) - (cx, cy)
|
| 128 |
+
x, y = xy[:-1, 0], xy[:-1, 1]
|
| 129 |
+
xn, yn = xy[1:, 0], xy[1:, 1]
|
| 130 |
+
a = x * yn - xn * y
|
| 131 |
+
sgn = 1.0 if ring.is_ccw == (k == 0) else -1.0
|
| 132 |
+
ixx += sgn * np.sum(a * (y**2 + y * yn + yn**2)) / 12
|
| 133 |
+
iyy += sgn * np.sum(a * (x**2 + x * xn + xn**2)) / 12
|
| 134 |
+
ixy += sgn * np.sum(a * (x * yn + 2 * x * y + 2 * xn * yn + xn * y)) / 24
|
| 135 |
+
return abs(ixx), abs(iyy), ixy
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def fourier_descriptors(poly, n_samples: int = 512) -> np.ndarray:
|
| 139 |
+
"""|c_k|/|c_1| for k=-N..N of the outer contour (translation/rotation/scale invariant)."""
|
| 140 |
+
ring = max(getattr(poly, "geoms", [poly]), key=lambda p: p.area).exterior
|
| 141 |
+
if not ring.is_ccw:
|
| 142 |
+
ring = shapely.reverse(ring)
|
| 143 |
+
pts = shapely.get_coordinates(shapely.line_interpolate_point(ring, np.linspace(0, 1, n_samples, endpoint=False), normalized=True))
|
| 144 |
+
c = np.fft.fft(pts[:, 0] + 1j * pts[:, 1]) / n_samples
|
| 145 |
+
k = np.r_[np.arange(-N_FOURIER, 0), np.arange(0, N_FOURIER + 1)]
|
| 146 |
+
mags = np.abs(c[k])
|
| 147 |
+
mags[k == 0] = 0.0
|
| 148 |
+
return mags / mags[k == 1][0]
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def separation_spectrum(a_pts: np.ndarray, b_pts: np.ndarray) -> tuple[np.ndarray, dict]:
|
| 152 |
+
d, _ = cKDTree(b_pts).query(a_pts)
|
| 153 |
+
hist, _ = np.histogram(d, bins=SEP_BINS_UM, weights=np.full(len(d), BOUNDARY_STEP_UM))
|
| 154 |
+
dmin = float(d.min())
|
| 155 |
+
s = {"dmin": dmin, "P_inv": float(np.sum(BOUNDARY_STEP_UM / np.maximum(d, 0.05)))}
|
| 156 |
+
for f in (1.05, 1.5, 2.0, 3.0):
|
| 157 |
+
s[f"L_{f:g}dmin"] = float(np.sum(d <= f * dmin) * BOUNDARY_STEP_UM)
|
| 158 |
+
for d0 in (5.0, 10.0, 20.0):
|
| 159 |
+
s[f"L_le{d0:g}um"] = float(np.sum(d <= d0) * BOUNDARY_STEP_UM)
|
| 160 |
+
return hist, s
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def window(c: Conductors) -> tuple[float, float, float]:
|
| 164 |
+
"""Square raster window around the pocket and signal metal: (x0, y0, side) in µm."""
|
| 165 |
+
x0, y0, x1, y1 = shapely.union(c.N, c.S).buffer(0).bounds
|
| 166 |
+
gx0, gy0, gx1, gy1 = c.pocket.bounds
|
| 167 |
+
cx, cy = (gx0 + gx1) / 2, (gy0 + gy1) / 2
|
| 168 |
+
side = 1.05 * max(gx1 - gx0, gy1 - gy0, x1 - x0, y1 - y0)
|
| 169 |
+
return cx - side / 2, cy - side / 2, side
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def raster(c: Conductors, npx: int = RASTER_PX) -> tuple[dict[str, np.ndarray], float, tuple[float, float]]:
|
| 173 |
+
wx0, wy0, side = window(c)
|
| 174 |
+
cx, cy = wx0 + side / 2, wy0 + side / 2
|
| 175 |
+
px = side / npx
|
| 176 |
+
xs = cx - side / 2 + (np.arange(npx) + 0.5) * px
|
| 177 |
+
ys = cy - side / 2 + (np.arange(npx) + 0.5) * px
|
| 178 |
+
X, Y = np.meshgrid(xs, ys)
|
| 179 |
+
masks = {}
|
| 180 |
+
for k, g in c.metals().items():
|
| 181 |
+
shapely.prepare(g)
|
| 182 |
+
masks[k] = shapely.contains_xy(g, X, Y)
|
| 183 |
+
return masks, px, (cx - side / 2, cy - side / 2)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def radial_power_spectrum(img: np.ndarray, px: float) -> tuple[np.ndarray, np.ndarray]:
|
| 187 |
+
n = img.shape[0]
|
| 188 |
+
F = np.fft.fftshift(np.fft.fft2(img - img.mean()))
|
| 189 |
+
P = np.abs(F) ** 2 / img.size
|
| 190 |
+
k = np.fft.fftshift(np.fft.fftfreq(n, d=px))
|
| 191 |
+
KX, KY = np.meshgrid(k, k)
|
| 192 |
+
kr = np.hypot(KX, KY).ravel()
|
| 193 |
+
edges = np.geomspace(1.0 / (n * px), 0.5 / px, K_BINS + 1)
|
| 194 |
+
idx = np.digitize(kr, edges) - 1
|
| 195 |
+
ok = (idx >= 0) & (idx < K_BINS)
|
| 196 |
+
s = np.bincount(idx[ok], weights=P.ravel()[ok], minlength=K_BINS)
|
| 197 |
+
cnt = np.bincount(idx[ok], minlength=K_BINS)
|
| 198 |
+
return np.sqrt(edges[1:] * edges[:-1]), np.where(cnt > 0, s / np.maximum(cnt, 1), np.nan)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def dirichlet_eigenvalues(mask: np.ndarray, px: float, k: int = N_EIG) -> np.ndarray:
|
| 202 |
+
"""Lowest Dirichlet Laplacian eigenvalues of a pixel mask (Shape-DNA), in um^-2."""
|
| 203 |
+
idx = -np.ones(mask.shape, dtype=np.int64)
|
| 204 |
+
ii, jj = np.nonzero(mask)
|
| 205 |
+
n = len(ii)
|
| 206 |
+
if n < 4 * k:
|
| 207 |
+
return np.full(k, np.nan)
|
| 208 |
+
idx[ii, jj] = np.arange(n)
|
| 209 |
+
rows, cols = [np.arange(n)], [np.arange(n)]
|
| 210 |
+
vals = [np.full(n, 4.0)]
|
| 211 |
+
for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
|
| 212 |
+
ni, nj = ii + di, jj + dj
|
| 213 |
+
inside = (ni >= 0) & (ni < mask.shape[0]) & (nj >= 0) & (nj < mask.shape[1])
|
| 214 |
+
nb = np.full(n, -1)
|
| 215 |
+
nb[inside] = idx[ni[inside], nj[inside]]
|
| 216 |
+
ok = nb >= 0
|
| 217 |
+
rows.append(np.arange(n)[ok])
|
| 218 |
+
cols.append(nb[ok])
|
| 219 |
+
vals.append(-np.ones(ok.sum()))
|
| 220 |
+
L = sparse.csc_matrix((np.concatenate(vals), (np.concatenate(rows), np.concatenate(cols))), shape=(n, n))
|
| 221 |
+
ev = eigsh(L, k=k, sigma=0, which="LM", return_eigenvectors=False)
|
| 222 |
+
return np.sort(ev) / px**2
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def geometry_record(gds_path) -> tuple[dict, dict]:
|
| 226 |
+
return geometry_record_conductors(load_conductors(gds_path))
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def geometry_record_conductors(c: Conductors) -> tuple[dict, dict]:
|
| 230 |
+
"""Descriptors of a (N, S, G) conductor triple; for multi-conductor queries N/S are the queried pair, G the rest."""
|
| 231 |
+
scalars, arrays = {}, {}
|
| 232 |
+
for name, g in (("N", c.N), ("S", c.S), ("pocket", c.pocket)):
|
| 233 |
+
for k, v in shape_metrics(g).items():
|
| 234 |
+
scalars[f"{name}_{k}"] = float(v)
|
| 235 |
+
arrays[f"fourier_{name}"] = fourier_descriptors(g)
|
| 236 |
+
scalars["gap_area"] = c.gap.area
|
| 237 |
+
scalars["chip_w"], scalars["chip_h"] = c.chip.bounds[2] - c.chip.bounds[0], c.chip.bounds[3] - c.chip.bounds[1]
|
| 238 |
+
scalars["G_area"] = c.G.area
|
| 239 |
+
|
| 240 |
+
b = boundaries(c)
|
| 241 |
+
for a, t in (("N", "S"), ("S", "N"), ("N", "G"), ("G", "N"), ("S", "G"), ("G", "S")):
|
| 242 |
+
hist, s = separation_spectrum(b[a], b[t])
|
| 243 |
+
arrays[f"sep_{a}{t}"] = hist
|
| 244 |
+
scalars.update({f"sep_{a}{t}_{k}": v for k, v in s.items()})
|
| 245 |
+
|
| 246 |
+
masks, px, origin = raster(c)
|
| 247 |
+
scalars["raster_px_um"] = px
|
| 248 |
+
metal = (masks["N"] | masks["S"] | masks["G"]).astype(np.float64)
|
| 249 |
+
k, P = radial_power_spectrum(metal, px)
|
| 250 |
+
arrays["psd_k"], arrays["psd_metal"] = k, P
|
| 251 |
+
arrays["psd_NS"] = radial_power_spectrum(masks["N"].astype(float) - masks["S"].astype(float), px)[1]
|
| 252 |
+
for name in ("N", "S"):
|
| 253 |
+
arrays[f"eig_{name}"] = dirichlet_eigenvalues(masks[name], px)
|
| 254 |
+
scalars[f"{name}_raster_area_err"] = float(masks[name].sum() * px**2 / getattr(c, name).area - 1)
|
| 255 |
+
arrays["sep_bins_um"] = SEP_BINS_UM
|
| 256 |
+
return scalars, arrays
|
geomgnn/_tlsq/uge/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Universal GDS Embedding framework: GDS -> scene -> scene graph (+ physics-proxy plugins) -> universal vector / GNN.
|
| 2 |
+
|
| 3 |
+
The recipe is dataset-agnostic; what a user adapts is (1) the layer map / net naming of their GDS, (2) the physics proxy
|
| 4 |
+
plugin that fits their observable (capacitance, resonance frequency, kappa, ...), and (3) the target adapter.
|
| 5 |
+
"""
|
geomgnn/_tlsq/uge/blocks.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Physics-agnostic geometry blocks: node (per conductor), edge (per conductor pair) and global features, all in
|
| 2 |
+
physical units (log µm, ratios) so that nothing is fitted to a dataset."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import shapely
|
| 8 |
+
from scipy.special import ellipk
|
| 9 |
+
from scipy.spatial import cKDTree
|
| 10 |
+
|
| 11 |
+
from ..embed.proxy import _nthreads
|
| 12 |
+
from ..geometry import fourier_descriptors, sample_boundary, shape_metrics
|
| 13 |
+
from .scene import Scene
|
| 14 |
+
|
| 15 |
+
GRAN_R_UM = np.geomspace(0.25, 100.0, 16)
|
| 16 |
+
SEP_EDGES_UM = np.geomspace(0.5, 2000.0, 11)
|
| 17 |
+
MOMENTS = (0.5, 1.0, 2.0)
|
| 18 |
+
STEP_UM = 0.25
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _log(x):
|
| 22 |
+
return float(np.log(max(float(x), 1e-9)))
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def region(scene: Scene, i: int):
|
| 26 |
+
"""The shape that carries conductor i's edge: the etched pocket for the ground net, the metal otherwise."""
|
| 27 |
+
return scene.pocket if scene.conductors[i].role == "ground" else scene.conductors[i].geom
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def granulometry(g) -> np.ndarray:
|
| 31 |
+
a0 = g.area
|
| 32 |
+
return np.array([g.buffer(-r, quad_segs=4).area / a0 if a0 > 0 else 0.0 for r in GRAN_R_UM])
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def node_features(scene: Scene, i: int) -> dict:
|
| 36 |
+
g = region(scene, i)
|
| 37 |
+
m = shape_metrics(g)
|
| 38 |
+
f = {"role_ground": float(scene.conductors[i].role == "ground"),
|
| 39 |
+
"log_area": _log(m["area"]), "log_perimeter": _log(m["perimeter"]), "compactness": m["compactness"],
|
| 40 |
+
"solidity": m["solidity"], "log_min_halfwidth": _log(m["inscribed_radius"]),
|
| 41 |
+
"log_aspect": abs(_log(m["bbox_w"]) - _log(m["bbox_h"])), "log_elongation": _log(m["elongation"]),
|
| 42 |
+
"log_Imajor_over_A2": _log(m["I_major"] / max(m["area"], 1e-9) ** 2), "n_holes": float(m["n_holes"]),
|
| 43 |
+
"log_width": _log(2 * m["area"] / m["perimeter"])}
|
| 44 |
+
for j, v in enumerate(fourier_descriptors(g)):
|
| 45 |
+
f[f"fourier_{j:02d}"] = float(np.log1p(50 * v))
|
| 46 |
+
for j, v in enumerate(granulometry(g)):
|
| 47 |
+
f[f"gran_{j:02d}"] = float(v)
|
| 48 |
+
return f
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _kratio(m):
|
| 52 |
+
m = np.clip(m, 1e-12, 1 - 1e-12)
|
| 53 |
+
return ellipk(1 - m) / ellipk(m)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
TOKEN_VARIANTS = {"exact": None, "td16": dict(d_far=100.0, k=16), "td64": dict(d_far=100.0, k=64), "tc600": dict(crop=600.0)}
|
| 57 |
+
TOKEN_VARIANT: dict = {"name": "exact"} # PRIOR_SCHEME.md 14.3; "exact" = the published tokens
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def set_token_variant(name: str) -> None:
|
| 61 |
+
from ..geometry import MIC_TOL
|
| 62 |
+
|
| 63 |
+
assert name in TOKEN_VARIANTS, name
|
| 64 |
+
TOKEN_VARIANT["name"] = name
|
| 65 |
+
MIC_TOL[0] = 0.01 if name == "exact" else 0.1
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _block_distances(pts: np.ndarray, tree) -> tuple:
|
| 69 |
+
"""Distances from the samples of a to b (k-d tree) for the active token variant, with weights (multiples of the
|
| 70 |
+
sample step): the samples are grouped into consecutive blocks of k; a block whose first sample is farther than d_far
|
| 71 |
+
from b is represented by that sample with weight = block size, the other blocks sample by sample. crop: blocks whose
|
| 72 |
+
first sample is farther than the crop distance are dropped (their representatives kept if every block is dropped)."""
|
| 73 |
+
v = TOKEN_VARIANTS[TOKEN_VARIANT["name"]]
|
| 74 |
+
k = v.get("k", 64)
|
| 75 |
+
r = v.get("crop", v.get("d_far"))
|
| 76 |
+
rep = np.arange(0, len(pts), k)
|
| 77 |
+
d_rep, _ = tree.query(pts[rep], workers=_nthreads())
|
| 78 |
+
size = np.diff(np.r_[rep, len(pts)])
|
| 79 |
+
far = d_rep > r
|
| 80 |
+
near_pts = np.repeat(~far, size)
|
| 81 |
+
d_near, _ = tree.query(pts[near_pts], workers=_nthreads())
|
| 82 |
+
if "crop" in v:
|
| 83 |
+
if not near_pts.any():
|
| 84 |
+
return d_rep, size.astype(float)
|
| 85 |
+
return d_near, np.ones(len(d_near))
|
| 86 |
+
return np.r_[d_rep[far], d_near], np.r_[size[far].astype(float), np.ones(len(d_near))]
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def edge_features(scene: Scene, i: int, j: int, bpts: dict, widths: dict) -> dict:
|
| 90 |
+
"""Symmetric pair features: separation spectrum (both directions averaged), minimum gap, coupling moments and the
|
| 91 |
+
coplanar-strip conformal-map coupling (Gevorgian modulus; the ground net is treated as infinitely wide)."""
|
| 92 |
+
f = {"is_ground_pair": float("ground" in (scene.conductors[i].role, scene.conductors[j].role))}
|
| 93 |
+
hist = np.zeros(len(SEP_EDGES_UM) - 1)
|
| 94 |
+
mom = np.zeros(len(MOMENTS))
|
| 95 |
+
cps = 0.0
|
| 96 |
+
dmin = np.inf
|
| 97 |
+
for a, b in ((i, j), (j, i)):
|
| 98 |
+
if TOKEN_VARIANT["name"] != "exact": # approximate tokens (PRIOR_SCHEME.md 14.3)
|
| 99 |
+
d, wq = _block_distances(bpts[a], cKDTree(bpts[b]))
|
| 100 |
+
dmin = min(dmin, d.min())
|
| 101 |
+
hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=STEP_UM * wq)[0]
|
| 102 |
+
dd = np.maximum(d, 0.05)
|
| 103 |
+
mom += 0.5 * np.array([np.sum(STEP_UM * wq * dd ** (-p)) for p in MOMENTS])
|
| 104 |
+
w1, w2 = widths[a], widths[b]
|
| 105 |
+
if np.isinf(w1) or np.isinf(w2):
|
| 106 |
+
wf = w2 if np.isinf(w1) else w1
|
| 107 |
+
mm = dd / (dd + wf)
|
| 108 |
+
else:
|
| 109 |
+
mm = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2))
|
| 110 |
+
cps += 0.5 * np.sum(STEP_UM * wq * _kratio(mm))
|
| 111 |
+
continue
|
| 112 |
+
d, _ = cKDTree(bpts[b]).query(bpts[a], workers=_nthreads())
|
| 113 |
+
dmin = min(dmin, d.min())
|
| 114 |
+
hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=np.full(len(d), STEP_UM))[0]
|
| 115 |
+
dd = np.maximum(d, 0.05)
|
| 116 |
+
mom += 0.5 * np.array([np.sum(STEP_UM * dd ** (-p)) for p in MOMENTS])
|
| 117 |
+
w1, w2 = widths[a], widths[b]
|
| 118 |
+
if np.isinf(w1) or np.isinf(w2):
|
| 119 |
+
wf = w2 if np.isinf(w1) else w1
|
| 120 |
+
mm = dd / (dd + wf)
|
| 121 |
+
else:
|
| 122 |
+
mm = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2))
|
| 123 |
+
cps += 0.5 * np.sum(STEP_UM * _kratio(mm))
|
| 124 |
+
f["log_dmin"] = _log(dmin)
|
| 125 |
+
for k, v in enumerate(hist):
|
| 126 |
+
f[f"sep_{k:02d}"] = float(np.log1p(v))
|
| 127 |
+
for p, v in zip(MOMENTS, mom):
|
| 128 |
+
f[f"mom_{p:g}"] = _log(v)
|
| 129 |
+
f["log_cps"] = _log(cps)
|
| 130 |
+
f["_cps"] = cps # raw, used for shielding shares
|
| 131 |
+
return f
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def global_features(scene: Scene) -> dict:
|
| 135 |
+
st = scene.stack
|
| 136 |
+
n_sig = sum(c.role == "signal" for c in scene.conductors)
|
| 137 |
+
return {"n_metal_layers": float(st["n_metal_layers"]), "log_metal_t": _log(st["metal_thickness_um"]),
|
| 138 |
+
"log_substrate_t": _log(st["substrate_thickness_um"]), "eps_eff": (st["substrate_eps_r"] + 1) / 2,
|
| 139 |
+
"n_signal": float(n_sig), "log_chip_area": _log(scene.chip.area),
|
| 140 |
+
"log_pocket_area": _log(scene.pocket.area)}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def boundary_points(scene: Scene) -> tuple[dict, dict]:
|
| 144 |
+
pts, widths = {}, {}
|
| 145 |
+
for i, c in enumerate(scene.conductors):
|
| 146 |
+
g = region(scene, i)
|
| 147 |
+
pts[i] = sample_boundary(g, STEP_UM)
|
| 148 |
+
widths[i] = np.inf if c.role == "ground" else 2 * g.area / g.length
|
| 149 |
+
return pts, widths
|
geomgnn/_tlsq/uge/graph.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Scene graph (nodes = conductors, edges = all conductor pairs) and its permutation-invariant pooled vector."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from itertools import combinations
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from .blocks import boundary_points, edge_features, global_features, node_features
|
| 10 |
+
from .proxies import Proxy
|
| 11 |
+
from .scene import Scene
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def scene_graph(scene: Scene, proxy: Proxy | None = None) -> dict:
|
| 15 |
+
bpts, widths = boundary_points(scene)
|
| 16 |
+
nodes = [node_features(scene, i) for i in range(scene.n)]
|
| 17 |
+
edges = {(i, j): edge_features(scene, i, j, bpts, widths) for i, j in combinations(range(scene.n), 2)}
|
| 18 |
+
# shielding: share of each endpoint's total geometric coupling carried by this edge
|
| 19 |
+
tot = np.zeros(scene.n)
|
| 20 |
+
for (i, j), f in edges.items():
|
| 21 |
+
tot[i] += f["_cps"]
|
| 22 |
+
tot[j] += f["_cps"]
|
| 23 |
+
for (i, j), f in edges.items():
|
| 24 |
+
si, sj = f["_cps"] / max(tot[i], 1e-12), f["_cps"] / max(tot[j], 1e-12)
|
| 25 |
+
f["share_mean"], f["share_absdiff"] = 0.5 * (si + sj), abs(si - sj)
|
| 26 |
+
g = {"names": scene.names(), "global": global_features(scene), "nodes": nodes, "edges": edges}
|
| 27 |
+
if proxy is not None:
|
| 28 |
+
px = proxy.compute(scene, edges)
|
| 29 |
+
for i, v in px["node"].items():
|
| 30 |
+
nodes[i].update({f"proxy_{k}": x for k, x in v.items()})
|
| 31 |
+
for e, v in px["edge"].items():
|
| 32 |
+
edges[e].update({f"proxy_{k}": x for k, x in v.items()})
|
| 33 |
+
g["global"].update({f"proxy_{k}": x for k, x in px["global"].items()})
|
| 34 |
+
if "matrix" in px:
|
| 35 |
+
g["proxy_matrix"] = px["matrix"]
|
| 36 |
+
for f in edges.values():
|
| 37 |
+
f.pop("_cps")
|
| 38 |
+
return g
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def pooled_vector(g: dict) -> dict:
|
| 42 |
+
"""Fixed-length universal vector for any number of conductors: global block, the ground node, sum/mean/max over
|
| 43 |
+
signal nodes, and sum/mean/max over signal-signal and signal-ground edges."""
|
| 44 |
+
out = {f"global.{k}": v for k, v in g["global"].items()}
|
| 45 |
+
roles = ["ground" if n["role_ground"] else "signal" for n in g["nodes"]]
|
| 46 |
+
keys = [k for k in g["nodes"][0] if k != "role_ground"]
|
| 47 |
+
gi = roles.index("ground")
|
| 48 |
+
out.update({f"ground.{k}": g["nodes"][gi][k] for k in keys})
|
| 49 |
+
S = np.array([[n[k] for k in keys] for n, r in zip(g["nodes"], roles) if r == "signal"])
|
| 50 |
+
for agg, fn in (("mean", np.mean), ("max", np.max), ("min", np.min)):
|
| 51 |
+
out.update({f"signal.{agg}.{k}": v for k, v in zip(keys, fn(S, 0))})
|
| 52 |
+
ekeys = [k for k in next(iter(g["edges"].values())) if k != "is_ground_pair"]
|
| 53 |
+
for typ, flag in (("ss", 0.0), ("sg", 1.0)):
|
| 54 |
+
E = np.array([[f[k] for k in ekeys] for f in g["edges"].values() if f["is_ground_pair"] == flag])
|
| 55 |
+
if len(E) == 0:
|
| 56 |
+
E = np.full((1, len(ekeys)), np.nan)
|
| 57 |
+
for agg, fn in (("mean", np.nanmean), ("max", np.nanmax), ("min", np.nanmin)):
|
| 58 |
+
out.update({f"edge_{typ}.{agg}.{k}": v for k, v in zip(ekeys, fn(E, 0))})
|
| 59 |
+
return out
|
geomgnn/_tlsq/uge/localtok.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Local, window-invariant context tokens (PRIOR_SCHEME.md 14.5 E4; replace the chip / pocket areas): for every signal
|
| 2 |
+
conductor and radius R, the fractions of its surrounding annulus (conductor buffered by R, minus the conductor) that are
|
| 3 |
+
ground metal, other-conductor metal and etched chip; the rest of the annulus lies outside the chip (no metal). Pair
|
| 4 |
+
tokens = mean over the pair's signal ends (a ground pair: its signal end). Identical copy: tlsq_infer/localtok.py."""
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import shapely
|
| 10 |
+
from scipy.spatial import cKDTree
|
| 11 |
+
|
| 12 |
+
from ..geometry import sample_boundary
|
| 13 |
+
from .blocks import MOMENTS, SEP_EDGES_UM, STEP_UM, _kratio
|
| 14 |
+
|
| 15 |
+
RADII_UM = (50.0, 150.0, 400.0)
|
| 16 |
+
KEYS = [f"lt_{k}{int(r)}" for r in RADII_UM for k in ("g", "o", "e")]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def node_tokens(scene) -> np.ndarray:
|
| 20 |
+
"""(n, len(KEYS)) per conductor (ground row = NaN)."""
|
| 21 |
+
conds = scene.conductors
|
| 22 |
+
n = len(conds)
|
| 23 |
+
G = next(c.geom for c in conds if c.role == "ground")
|
| 24 |
+
sig = [k for k, c in enumerate(conds) if c.role != "ground"]
|
| 25 |
+
tree = shapely.STRtree([conds[k].geom for k in sig])
|
| 26 |
+
out = np.full((n, len(KEYS)), np.nan)
|
| 27 |
+
for k in sig:
|
| 28 |
+
g = conds[k].geom
|
| 29 |
+
vals = []
|
| 30 |
+
for R in RADII_UM:
|
| 31 |
+
ring = g.buffer(R, quad_segs=4).difference(g)
|
| 32 |
+
A = ring.area
|
| 33 |
+
box = ring.bounds
|
| 34 |
+
gm = shapely.intersection(shapely.clip_by_rect(G, *box), ring).area
|
| 35 |
+
near = [sig[i] for i in tree.query(ring, predicate="intersects") if sig[i] != k]
|
| 36 |
+
om = shapely.intersection(shapely.union_all([conds[i].geom for i in near]), ring).area if near else 0.0
|
| 37 |
+
inchip = shapely.intersection(scene.chip, ring).area
|
| 38 |
+
vals += [gm / A, om / A, max(inchip - gm - om, 0.0) / A]
|
| 39 |
+
out[k] = vals
|
| 40 |
+
return out
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
LG_R_UM = 100.0
|
| 44 |
+
TOK = ["log_dmin"] + [f"sep_{k:02d}" for k in range(10)] + ["mom_0.5", "mom_1", "mom_2", "log_cps"]
|
| 45 |
+
LG_KEYS = [f"lg_{k}" for k in TOK]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def ground_tokens(scene) -> np.ndarray:
|
| 49 |
+
"""(n, len(LG_KEYS)) per signal conductor: the ground-pair tokens of blocks.edge_features (separation spectrum, minimum
|
| 50 |
+
gap, coupling moments, coplanar-strip coupling) from the ground-edge samples within LG_R_UM of the conductor only
|
| 51 |
+
(fixed radius: independent of the ground window)."""
|
| 52 |
+
conds = scene.conductors
|
| 53 |
+
out = np.full((len(conds), len(LG_KEYS)), np.nan)
|
| 54 |
+
for k, c in enumerate(conds):
|
| 55 |
+
if c.role == "ground":
|
| 56 |
+
continue
|
| 57 |
+
g = c.geom
|
| 58 |
+
b = g.bounds
|
| 59 |
+
P = shapely.clip_by_rect(scene.pocket, b[0] - LG_R_UM, b[1] - LG_R_UM, b[2] + LG_R_UM, b[3] + LG_R_UM)
|
| 60 |
+
pj = sample_boundary(g, STEP_UM)
|
| 61 |
+
pg = sample_boundary(P, STEP_UM) if not P.is_empty else np.zeros((0, 2))
|
| 62 |
+
if len(pg):
|
| 63 |
+
pg = pg[cKDTree(pj).query(pg)[0] < LG_R_UM] # drops the clip-box edges (>= LG_R_UM away) and the far ground
|
| 64 |
+
if not len(pg):
|
| 65 |
+
continue
|
| 66 |
+
wj = 2 * g.area / g.length
|
| 67 |
+
hist = np.zeros(len(SEP_EDGES_UM) - 1)
|
| 68 |
+
mom = np.zeros(len(MOMENTS))
|
| 69 |
+
cps, dmin = 0.0, np.inf
|
| 70 |
+
for a_, b_ in ((pj, pg), (pg, pj)):
|
| 71 |
+
d = cKDTree(b_).query(a_)[0]
|
| 72 |
+
dmin = min(dmin, d.min())
|
| 73 |
+
hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=np.full(len(d), STEP_UM))[0]
|
| 74 |
+
dd = np.maximum(d, 0.05)
|
| 75 |
+
mom += 0.5 * np.array([np.sum(STEP_UM * dd ** (-p)) for p in MOMENTS])
|
| 76 |
+
cps += 0.5 * np.sum(STEP_UM * _kratio(dd / (dd + wj)))
|
| 77 |
+
out[k] = [np.log(max(dmin, 1e-9))] + list(np.log1p(hist)) + [np.log(max(v, 1e-9)) for v in mom] + [np.log(max(cps, 1e-9))]
|
| 78 |
+
return out
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def add_tokens(scene, row: dict) -> dict:
|
| 82 |
+
"""Row (scene graph dict) with the KEYS added to every edge (shallow copy)."""
|
| 83 |
+
return add_from_tokens(row, node_tokens(scene), ground_tokens(scene), [c.role != "ground" for c in scene.conductors])
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def add_from_tokens(row: dict, T: np.ndarray, Lg: np.ndarray, sig) -> dict:
|
| 87 |
+
"""Edges + KEYS (mean over the signal ends) + LG_KEYS (ground pairs: the fixed-radius tokens of the signal end; other
|
| 88 |
+
pairs: their own tokens, unchanged)."""
|
| 89 |
+
sig = np.asarray(sig)
|
| 90 |
+
edges = {}
|
| 91 |
+
for (i, j), f in row["edges"].items():
|
| 92 |
+
ends = [e for e in (i, j) if sig[e]]
|
| 93 |
+
new = {k: float(x) for k, x in zip(KEYS, T[ends].mean(0))}
|
| 94 |
+
if len(ends) == 1 and np.all(np.isfinite(Lg[ends[0]])):
|
| 95 |
+
new.update({k: float(x) for k, x in zip(LG_KEYS, Lg[ends[0]])})
|
| 96 |
+
else:
|
| 97 |
+
new.update({f"lg_{k}": float(f[k]) for k in TOK})
|
| 98 |
+
edges[(i, j)] = {**f, **new}
|
| 99 |
+
return {**row, "edges": edges}
|
geomgnn/_tlsq/uge/proxies.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Physics-proxy plugins. A proxy maps a Scene to extra node / edge / global quantities that are cheap to compute and
|
| 2 |
+
physically aligned with the target. Swap the proxy to move the recipe to another observable (e.g. a resonator dataset
|
| 3 |
+
would plug in a quasi-TEM transmission-line proxy for f0 and kappa); the rest of the pipeline is unchanged."""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from ..embed.proxy import mom_capacitance_gds, mom_capacitance_gds_avg, mom_capacitance_gds_flagship, mom_capacitance_gds_rich
|
| 10 |
+
from .scene import Scene
|
| 11 |
+
|
| 12 |
+
EPS0 = 8.8541878128e-3 # fF/µm
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class Proxy:
|
| 16 |
+
name = "none"
|
| 17 |
+
|
| 18 |
+
def compute(self, scene: Scene, edges: dict) -> dict:
|
| 19 |
+
"""Return {"node": {i: {...}}, "edge": {(i, j): {...}}, "global": {...}}; `edges` holds the geometry edge features."""
|
| 20 |
+
return {"node": {}, "edge": {}, "global": {}}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class NoProxy(Proxy):
|
| 24 |
+
name = "none"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class ConformalCoupling(Proxy):
|
| 28 |
+
"""Solver-free: per-pair coplanar-strip conformal-map coupling (already a geometry edge feature) turned into a
|
| 29 |
+
capacitance estimate C_ij ~ eps0 eps_eff * sum L K(k')/K(k), and node totals."""
|
| 30 |
+
|
| 31 |
+
name = "conformal"
|
| 32 |
+
|
| 33 |
+
def compute(self, scene, edges):
|
| 34 |
+
e_eff = (scene.stack["substrate_eps_r"] + 1) / 2
|
| 35 |
+
out = {"node": {i: {"log_Cself": -np.inf} for i in range(scene.n)}, "edge": {}, "global": {}}
|
| 36 |
+
tot = np.zeros(scene.n)
|
| 37 |
+
for (i, j), f in edges.items():
|
| 38 |
+
c = EPS0 * e_eff * f["_cps"]
|
| 39 |
+
out["edge"][(i, j)] = {"log_C": float(np.log(max(c, 1e-9)))}
|
| 40 |
+
tot[i] += c
|
| 41 |
+
tot[j] += c
|
| 42 |
+
for i in range(scene.n):
|
| 43 |
+
out["node"][i] = {"log_Cself": float(np.log(max(tot[i], 1e-9)))}
|
| 44 |
+
return out
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class CapacitanceMoM(Proxy):
|
| 48 |
+
"""Thin-film method of moments on the exact polygons (quadtree mesh, whole ground plane): full Maxwell matrix."""
|
| 49 |
+
|
| 50 |
+
name = "mom"
|
| 51 |
+
|
| 52 |
+
def __init__(self, max_cells: int = 8000, cells_per_gap: float = 3.0, n_offsets: int = 1, mesh: str = "v2",
|
| 53 |
+
substrate: str = "slab_open"):
|
| 54 |
+
self.max_cells, self.cells_per_gap, self.n_offsets, self.mesh = max_cells, cells_per_gap, n_offsets, mesh
|
| 55 |
+
self.substrate = substrate # MoM Green's function (embed.proxy.SUBSTRATE; PRIOR_SCHEME.md 14.6)
|
| 56 |
+
|
| 57 |
+
def compute(self, scene, edges):
|
| 58 |
+
from ..embed.proxy import substrate
|
| 59 |
+
|
| 60 |
+
with substrate(self.substrate, scene.stack.get("substrate_thickness_um")):
|
| 61 |
+
return self._compute(scene, edges)
|
| 62 |
+
|
| 63 |
+
def _compute(self, scene, edges):
|
| 64 |
+
metals, kw = [c.geom for c in scene.conductors], dict(eps_r=scene.stack["substrate_eps_r"], max_cells=self.max_cells,
|
| 65 |
+
cells_per_gap=self.cells_per_gap)
|
| 66 |
+
if self.mesh == "v3-rich": # conductor-anchored mesh, Richardson-extrapolated (gap/3 and gap/6)
|
| 67 |
+
C, n, hm = mom_capacitance_gds_rich(metals, kw["eps_r"], self.cells_per_gap, self.max_cells, 2 * self.max_cells)
|
| 68 |
+
elif self.mesh in ("lite", "formula"): # flagship-fast cheap priors (PRIOR_SCHEME.md Part III): micro-MoM at a panel budget / closed form
|
| 69 |
+
from ..embed.fastprior import formula_prior, micro_mom
|
| 70 |
+
|
| 71 |
+
if self.mesh == "lite":
|
| 72 |
+
C, _, info = micro_mom(metals[0], metals[1:], kw["eps_r"], budget=self.max_cells, cells_per_gap=self.cells_per_gap)
|
| 73 |
+
n, hm = int(sum(info["panels"])), float(min(info["h_um"]))
|
| 74 |
+
mg = float(info["min_gap_um"]) # the mesh's smallest conductor gap (h = gap / ppg off the floor)
|
| 75 |
+
else:
|
| 76 |
+
C, info = formula_prior(metals[0], metals[1:], kw["eps_r"])
|
| 77 |
+
n, hm = 0, float(info["min_gap_um"])
|
| 78 |
+
elif self.mesh == "flagship": # strip-merged, resolution-controlled Richardson pair, H-matrix solve (PRIOR_SCHEME.md Part II)
|
| 79 |
+
C, n, hm, _ = mom_capacitance_gds_flagship(metals, kw["eps_r"], self.cells_per_gap, self.max_cells)
|
| 80 |
+
else:
|
| 81 |
+
if self.mesh != "v2":
|
| 82 |
+
kw["mesh"] = self.mesh
|
| 83 |
+
try:
|
| 84 |
+
C, n, hm = mom_capacitance_gds_avg(metals, self.n_offsets, **kw) if self.n_offsets > 1 else mom_capacitance_gds(metals, **kw)
|
| 85 |
+
except np.linalg.LinAlgError:
|
| 86 |
+
if self.mesh != "v3":
|
| 87 |
+
raise
|
| 88 |
+
# v3 single solve: sliver edge panels can make the gap/3 system indefinite (~0.2 % of designs); use gap/6
|
| 89 |
+
kw.update(cells_per_gap=2 * self.cells_per_gap, max_cells=2 * self.max_cells)
|
| 90 |
+
C, n, hm = mom_capacitance_gds(metals, **kw)
|
| 91 |
+
out = {"node": {i: {"log_Cself": float(np.log(max(C[i, i], 1e-9)))} for i in range(scene.n)}, "edge": {},
|
| 92 |
+
"global": {"cells": float(n), "h_min_um": float(hm)}, "matrix": C}
|
| 93 |
+
if self.mesh != "lite": # every variant records the layout's smallest conductor gap (review W1); lite: the mesh's own
|
| 94 |
+
from ..embed.fastprior import gap_of
|
| 95 |
+
mg = float(info["min_gap_um"]) if self.mesh == "formula" else float(gap_of(metals))
|
| 96 |
+
out["global"]["min_gap_um"] = mg
|
| 97 |
+
for (i, j) in edges:
|
| 98 |
+
out["edge"][(i, j)] = {"log_C": float(np.log(max(-C[i, j], 1e-9)))}
|
| 99 |
+
return out
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
PROXIES = {p.name: p for p in (NoProxy, ConformalCoupling, CapacitanceMoM)}
|
geomgnn/_tlsq/uge/scene.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Scene: the metal of a layout as a list of conductors (one ground net + any number of signal bodies), plus the stack."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from dataclasses import dataclass, field
|
| 6 |
+
from typing import Callable
|
| 7 |
+
|
| 8 |
+
import gdstk
|
| 9 |
+
import numpy as np
|
| 10 |
+
import shapely
|
| 11 |
+
from shapely.geometry import Polygon
|
| 12 |
+
|
| 13 |
+
DEFAULT_LAYERS = {"ground": [(1, 0)], "signal": [(1, 10)]}
|
| 14 |
+
DEFAULT_STACK = {"n_metal_layers": 1, "metal_thickness_um": 0.25, "substrate_thickness_um": 350.0, "substrate_eps_r": 11.45}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class Conductor:
|
| 19 |
+
name: str
|
| 20 |
+
role: str # "ground" | "signal"
|
| 21 |
+
geom: object # shapely (Multi)Polygon, µm
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class Scene:
|
| 26 |
+
conductors: list[Conductor] # index 0 is the ground net
|
| 27 |
+
chip: Polygon
|
| 28 |
+
pocket: object # chip minus ground metal: the etched region (its edge is the ground's active edge)
|
| 29 |
+
stack: dict = field(default_factory=lambda: dict(DEFAULT_STACK))
|
| 30 |
+
markers: dict = field(default_factory=dict) # non-metal helper shapes by (layer, datatype), e.g. port stubs; naming only
|
| 31 |
+
|
| 32 |
+
@property
|
| 33 |
+
def n(self) -> int:
|
| 34 |
+
return len(self.conductors)
|
| 35 |
+
|
| 36 |
+
def names(self) -> list[str]:
|
| 37 |
+
return [c.name for c in self.conductors]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _valid(g):
|
| 41 |
+
g = shapely.make_valid(g)
|
| 42 |
+
parts = [p for p in getattr(g, "geoms", [g]) if p.geom_type in ("Polygon", "MultiPolygon")]
|
| 43 |
+
return shapely.union_all(parts)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def scene_from_gds(path, layers: dict = DEFAULT_LAYERS, stack: dict | None = None,
|
| 47 |
+
namer: Callable[[list, object], list[str]] | None = None) -> Scene:
|
| 48 |
+
"""Read only metal layers. Each signal polygon is one conductor; all ground polygons form one net.
|
| 49 |
+
`namer(signal_geoms, markers)` returns a name per signal body (dataset convention); default S0, S1, ..."""
|
| 50 |
+
lib = gdstk.read_gds(str(path))
|
| 51 |
+
cell = lib.top_level()[0]
|
| 52 |
+
u = lib.unit / 1e-6
|
| 53 |
+
lay = {k: {tuple(x) for x in v} for k, v in layers.items()}
|
| 54 |
+
sig, gnd, other = [], [], {}
|
| 55 |
+
for p in cell.polygons:
|
| 56 |
+
key = (p.layer, p.datatype)
|
| 57 |
+
g = Polygon(p.points * u)
|
| 58 |
+
if key in lay["signal"]:
|
| 59 |
+
sig.append(g)
|
| 60 |
+
elif key in lay["ground"]:
|
| 61 |
+
gnd.append(g)
|
| 62 |
+
else:
|
| 63 |
+
other.setdefault(key, []).append(g)
|
| 64 |
+
G = _valid(shapely.union_all([_valid(g) for g in gnd]))
|
| 65 |
+
S = [_valid(g) for g in sig]
|
| 66 |
+
chip = shapely.box(*G.bounds)
|
| 67 |
+
markers = {k: shapely.union_all(v) for k, v in other.items()}
|
| 68 |
+
names = namer(S, markers) if namer else [f"S{i}" for i in range(len(S))]
|
| 69 |
+
conds = [Conductor("G", "ground", G)] + [Conductor(n, "signal", g) for n, g in zip(names, S)]
|
| 70 |
+
return Scene(conds, chip, chip.difference(G), {**DEFAULT_STACK, **(stack or {})}, markers)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def tlsq_namer(signals: list, markers: dict, n_slots: int = 5) -> list[str]:
|
| 74 |
+
"""This dataset's convention: N = body fed by the north port stub (2,0); S = body fed by the south stub (3,0) if
|
| 75 |
+
present, else the body containing the origin (star, inner pad); remaining bodies P1, P2, ... ordered by angle."""
|
| 76 |
+
north, south = markers.get((2, 0)), markers.get((3, 0))
|
| 77 |
+
n = min(range(len(signals)), key=lambda i: signals[i].distance(north))
|
| 78 |
+
rest = [i for i in range(len(signals)) if i != n]
|
| 79 |
+
if south is not None:
|
| 80 |
+
s = min(rest, key=lambda i: signals[i].distance(south))
|
| 81 |
+
else:
|
| 82 |
+
s = next((i for i in rest if signals[i].contains(shapely.Point(0, 0))), max(rest, key=lambda i: signals[i].area))
|
| 83 |
+
names = [""] * len(signals)
|
| 84 |
+
names[s], names[n] = "S", "N"
|
| 85 |
+
a0 = np.degrees(np.arctan2(signals[n].centroid.y, signals[n].centroid.x))
|
| 86 |
+
for i in rest:
|
| 87 |
+
if i != s:
|
| 88 |
+
a = np.degrees(np.arctan2(signals[i].centroid.y, signals[i].centroid.x))
|
| 89 |
+
names[i] = f"P{int(round(((a - a0) % 360) / (360 / n_slots))) % n_slots}"
|
| 90 |
+
return names
|
geomgnn/api.py
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Registry-driven GNN prediction for the Space (deploy/registry.yaml: gnn_hub, gnn_models).
|
| 2 |
+
|
| 3 |
+
predict_gnn(prepared, name) runs the frozen Predictor (geomgnn.infer, FINALIZATION_V1 checkpoints) on the scene built from a
|
| 4 |
+
tlsq_infer PreparedLayout. The steps after the scene are those of infer.Predictor.from_gds, statement for statement;
|
| 5 |
+
deploy/tests/test_gnn_space.py checks the result against the frozen stage's from_gds on the raw TRAIN GDS files.
|
| 6 |
+
Entries with `learned_prior` (the fast tier) replace the two field solves by the learned prior surrogate
|
| 7 |
+
(geomgnn.learned_prior = scripts/learnedprior/lp_bench.py Learned.from_gds); the networks are the same frozen weights.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import contextlib
|
| 13 |
+
import hashlib
|
| 14 |
+
import json
|
| 15 |
+
import os
|
| 16 |
+
import threading
|
| 17 |
+
import time
|
| 18 |
+
import warnings
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
import shapely
|
| 23 |
+
|
| 24 |
+
from . import __version__
|
| 25 |
+
from . import infer as I
|
| 26 |
+
from ._tlsq.embed import fastprior, proxy
|
| 27 |
+
from ._tlsq.uge.graph import scene_graph
|
| 28 |
+
from ._tlsq.uge.scene import DEFAULT_STACK, Conductor, Scene
|
| 29 |
+
|
| 30 |
+
lt = I.lt
|
| 31 |
+
EPS_TRAIN, T_TRAIN_UM = 11.45, 350.0
|
| 32 |
+
PRIOR_IMPLS = ("frozen", "cpu_fast")
|
| 33 |
+
_HMAT0 = dict(proxy.HMAT)
|
| 34 |
+
_STATE = {"prior_impl": "frozen", "patches": [], "mode": None, "stack": None}
|
| 35 |
+
_CACHE: dict = {}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def gnn_names(reg) -> list[str]:
|
| 39 |
+
return [k for k, v in (reg.data.get("gnn_models") or {}).items() if v.get("status", "available") == "available"]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def set_prior_impl(impl: str) -> None:
|
| 43 |
+
"""frozen: the solver as frozen. cpu_fast: scripts/geomfast/fastmom.py (vectorised H-matrix kernel) with ACA eps 1e-6 and
|
| 44 |
+
PCG tol 1e-8 (agents/geomfast/FASTPRIOR_STATUS.md); process-wide."""
|
| 45 |
+
if impl not in PRIOR_IMPLS:
|
| 46 |
+
raise ValueError(f"prior_impl {impl!r} not in {PRIOR_IMPLS}")
|
| 47 |
+
if impl == _STATE["prior_impl"]:
|
| 48 |
+
return
|
| 49 |
+
from . import fastmom
|
| 50 |
+
if impl == "cpu_fast":
|
| 51 |
+
fastmom.install()
|
| 52 |
+
proxy.HMAT.update(eps=1e-6, tol=1e-8)
|
| 53 |
+
else:
|
| 54 |
+
fastmom.uninstall()
|
| 55 |
+
proxy.HMAT.clear()
|
| 56 |
+
proxy.HMAT.update(_HMAT0)
|
| 57 |
+
_STATE["prior_impl"] = impl
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
VALUE_PRESERVING = ("rejtail_fv",) # bit-identical priors
|
| 61 |
+
VALUE_CHANGING = ("priorv2m", "priorv2") # change the prior: need prior.value_changing_approved in the registry entry
|
| 62 |
+
# priorv2.installed() arguments of each value-changing patch: v2m = the corrected prior of the model; v2 = version 2 (no margin
|
| 63 |
+
# widening), the prior of the paper's "retrained, version 2" ablation (paper Space only)
|
| 64 |
+
V2_KWARGS = {"priorv2m": dict(policy="fc", dip=True, margin=True), "priorv2": dict(policy="fc", dip=True, margin=False)}
|
| 65 |
+
PRIOR_PATCHES = VALUE_PRESERVING + VALUE_CHANGING
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
SPEED_MODES = ("exact", "exact_compiled", "certified") # geomgnn.fastprior_pkg modes (agents/fastprior_pkg/RESULTS.md)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def speed_mode(pr: dict):
|
| 72 |
+
"""Solver mode of the prior: TLSQ_GNN_PRIOR_MODE (opt-in override, e.g. 'certified' or 'frozen') else registry prior.mode;
|
| 73 |
+
None / 'frozen' = the vendor solver as frozen (no fastprior_pkg)."""
|
| 74 |
+
m = os.environ.get("TLSQ_GNN_PRIOR_MODE") or pr.get("mode")
|
| 75 |
+
if m in (None, "", "frozen"):
|
| 76 |
+
return None
|
| 77 |
+
if m not in SPEED_MODES:
|
| 78 |
+
raise ValueError(f"prior mode {m!r} not in {SPEED_MODES} (or 'frozen')")
|
| 79 |
+
return m
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _v2m_value_patch(F, P, FL, patch="priorv2m"):
|
| 83 |
+
"""fastprior_pkg value hook: prior v2m (geomgnn.priorv2 installed(policy='fc', dip=True, margin=True)) on top of the
|
| 84 |
+
solver components (patch="priorv2": version 2, margin=False); returns the undo callable."""
|
| 85 |
+
from . import priorv2
|
| 86 |
+
assert priorv2.F is F and priorv2.P is P, "priorv2 and fastprior_pkg must patch the same vendor modules"
|
| 87 |
+
ctx = priorv2.installed(**V2_KWARGS[patch])
|
| 88 |
+
ctx.__enter__()
|
| 89 |
+
return lambda: ctx.__exit__(None, None, None)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def _teardown() -> None:
|
| 93 |
+
from . import rejtail_fv
|
| 94 |
+
if _STATE.get("mode"):
|
| 95 |
+
from . import fastprior_pkg
|
| 96 |
+
fastprior_pkg.uninstall() # also undoes its value patch
|
| 97 |
+
elif set(V2_KWARGS) & set(_STATE["patches"]):
|
| 98 |
+
_STATE.pop("v2ctx").__exit__(None, None, None)
|
| 99 |
+
rejtail_fv.uninstall()
|
| 100 |
+
_STATE.update(patches=[], mode=None, stack=None)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def set_prior_stack(pr: dict, threads: int = 2) -> None:
|
| 104 |
+
"""Process-wide prior stack of a registry prior: patches (rejtail_fv | priorv2m) and the solver mode (fastprior_pkg).
|
| 105 |
+
rejtail_fv: geomgnn.rejtail_fv (value-preserving rejection-tail fix; fastprior_pkg's fv replaces it, so not with a mode).
|
| 106 |
+
priorv2m: prior v2m (agents/priorv2/FINALIZATION_V2.json): priorv2.installed(policy='fc', dip=True, margin=True), as
|
| 107 |
+
fastprior_pkg's value_patch when a mode is set; it replaces _window_solve itself, so never with rejtail_fv.
|
| 108 |
+
mode: fastprior_pkg.install(mode, cpus=threads, target='geomgnn', value_patch=v2m) + warmup() (numba compile)."""
|
| 109 |
+
patches = sorted(pr.get("patches") or [])
|
| 110 |
+
mode = speed_mode(pr)
|
| 111 |
+
bad = set(patches) - set(PRIOR_PATCHES)
|
| 112 |
+
if bad:
|
| 113 |
+
raise ValueError(f"unknown prior patches {bad}; known: {PRIOR_PATCHES}")
|
| 114 |
+
v2 = [p for p in patches if p in V2_KWARGS]
|
| 115 |
+
if v2 and "rejtail_fv" in patches:
|
| 116 |
+
raise ValueError("priorv2m already contains the rejection-tail handling; do not combine it with rejtail_fv")
|
| 117 |
+
if len(v2) > 1:
|
| 118 |
+
raise ValueError(f"one prior version per stack, got {v2}")
|
| 119 |
+
if mode and "rejtail_fv" in patches:
|
| 120 |
+
raise ValueError("fastprior_pkg contains fv; list rejtail_fv only without a prior mode")
|
| 121 |
+
unapproved = [p for p in patches if p in VALUE_CHANGING and not pr.get("value_changing_approved")]
|
| 122 |
+
if unapproved:
|
| 123 |
+
raise ValueError(f"value-changing prior patches {unapproved} need prior.value_changing_approved in the registry")
|
| 124 |
+
key = (tuple(patches), mode, int(threads))
|
| 125 |
+
if key == _STATE.get("stack"):
|
| 126 |
+
return
|
| 127 |
+
_teardown()
|
| 128 |
+
if mode:
|
| 129 |
+
from . import fastprior_pkg as fp
|
| 130 |
+
fp.install(mode=mode, cpus=int(threads), workers=os.environ.get("TLSQ_PRIOR_WORKERS"), target="geomgnn", blas_threads=int(threads),
|
| 131 |
+
value_patch=(lambda F, P, FL: _v2m_value_patch(F, P, FL, v2[0])) if v2 else None, warm=False)
|
| 132 |
+
if mode in ("exact_compiled", "certified"):
|
| 133 |
+
_STATE["warmup_s"] = fp.warmup()
|
| 134 |
+
else:
|
| 135 |
+
from . import priorv2, rejtail_fv
|
| 136 |
+
if "rejtail_fv" in patches:
|
| 137 |
+
rejtail_fv.install()
|
| 138 |
+
if v2:
|
| 139 |
+
ctx = priorv2.installed(**V2_KWARGS[v2[0]])
|
| 140 |
+
ctx.__enter__()
|
| 141 |
+
_STATE["v2ctx"] = ctx
|
| 142 |
+
_STATE.update(patches=patches, mode=mode, stack=key)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def _sha(p) -> str:
|
| 146 |
+
return hashlib.sha256(Path(p).read_bytes()).hexdigest()
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def surrogate_file(reg, name: str, model_dir=None) -> Path | None:
|
| 150 |
+
"""The learned prior checkpoint of an entry (bundle copy named in info.json "surrogate", else the registry path)."""
|
| 151 |
+
lp = reg["gnn_models"][name].get("learned_prior")
|
| 152 |
+
if not lp:
|
| 153 |
+
return None
|
| 154 |
+
md = os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir
|
| 155 |
+
if md and (Path(md) / name / "info.json").exists():
|
| 156 |
+
info = json.loads((Path(md) / name / "info.json").read_text())
|
| 157 |
+
if info.get("surrogate"):
|
| 158 |
+
return Path(md) / name / info["surrogate"]
|
| 159 |
+
return reg.root / lp["checkpoint"]["path"]
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def checkpoint_files(reg, name: str, model_dir=None) -> list[Path]:
|
| 163 |
+
"""Bundle copies (<model_dir>/<name>/info.json) or the registry's source paths; SHA256 checked against the registry."""
|
| 164 |
+
m = reg["gnn_models"][name]
|
| 165 |
+
md = os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir
|
| 166 |
+
if md and (Path(md) / name / "info.json").exists():
|
| 167 |
+
info = json.loads((Path(md) / name / "info.json").read_text())
|
| 168 |
+
files = [Path(md) / name / f for f in info["files"]]
|
| 169 |
+
else:
|
| 170 |
+
files = [reg.root / c["path"] for c in m["checkpoints"]]
|
| 171 |
+
return files
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def available(name: str, reg, model_dir=None) -> bool:
|
| 175 |
+
s = surrogate_file(reg, name, model_dir)
|
| 176 |
+
return all(f.exists() for f in checkpoint_files(reg, name, model_dir)) and (s is None or s.exists())
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def load(name: str, reg, model_dir=None, threads: int | None = None) -> I.Predictor:
|
| 180 |
+
m = reg["gnn_models"][name]
|
| 181 |
+
threads = int(threads or os.environ.get("TLSQ_NUM_THREADS") or 2)
|
| 182 |
+
key = (str(reg.path), name, str(os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir), threads)
|
| 183 |
+
if key in _CACHE:
|
| 184 |
+
_apply_prior(m, threads)
|
| 185 |
+
return _CACHE[key]
|
| 186 |
+
files = checkpoint_files(reg, name, model_dir)
|
| 187 |
+
for f, c in zip(files, m["checkpoints"], strict=True):
|
| 188 |
+
if _sha(f) != c["sha256"]:
|
| 189 |
+
raise RuntimeError(f"{name}: {f.name} SHA256 differs from the registry (frozen checkpoint {c['sha256'][:12]})")
|
| 190 |
+
P = I.Predictor(files, threads=threads)
|
| 191 |
+
if P.arm != m["arm"]:
|
| 192 |
+
raise RuntimeError(f"{name}: checkpoints are arm {P.arm!r}, registry says {m['arm']!r}")
|
| 193 |
+
pr = m.get("prior")
|
| 194 |
+
lpr = m.get("learned_prior")
|
| 195 |
+
phys = P.models[0][0].get("schema", {}).get("physics", {})
|
| 196 |
+
P.surrogate = None
|
| 197 |
+
if P.direct:
|
| 198 |
+
if pr or lpr:
|
| 199 |
+
raise RuntimeError(f"{name}: direct arm has no prior, registry lists one")
|
| 200 |
+
elif lpr:
|
| 201 |
+
if pr or P.fast or [float(x) for x in lpr["ppg"]] != [1.5, 3.0]:
|
| 202 |
+
raise RuntimeError(f"{name}: a learned prior replaces the 1.5 / 3 ppg field solves of a full_gnn arm only ({P.arm}, {lpr})")
|
| 203 |
+
sf = surrogate_file(reg, name, model_dir)
|
| 204 |
+
if _sha(sf) != lpr["checkpoint"]["sha256"]:
|
| 205 |
+
raise RuntimeError(f"{name}: {sf.name} SHA256 differs from the registry (learned prior {lpr['checkpoint']['sha256'][:12]})")
|
| 206 |
+
from . import learned_prior
|
| 207 |
+
model, prep, _ = learned_prior.load(sf)
|
| 208 |
+
P.surrogate = (model, prep)
|
| 209 |
+
else:
|
| 210 |
+
if not pr or abs(float(pr["alpha"]) - float(phys.get("alpha"))) > 0 or (pr["method"] == "local_gap") != (phys.get("prior_method") == "local_gap") \
|
| 211 |
+
or [float(x) for x in pr["ppg"]] != ([0.75, 1.5] if P.fast else [1.5, 3.0]):
|
| 212 |
+
raise RuntimeError(f"{name}: registry prior {pr} does not match the checkpoint physics {phys}")
|
| 213 |
+
_apply_prior(m, threads)
|
| 214 |
+
_CACHE[key] = P
|
| 215 |
+
return P
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def _apply_prior(m: dict, threads: int = 2) -> None:
|
| 219 |
+
"""Process-wide prior settings of a gnn_models entry (on every load: entries may differ, the patches are global)."""
|
| 220 |
+
pr = m.get("prior")
|
| 221 |
+
if pr:
|
| 222 |
+
set_prior_impl(pr.get("prior_impl", "frozen"))
|
| 223 |
+
set_prior_stack(pr, threads)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
STARTUP: dict = {} # preload record (reported by /models)
|
| 227 |
+
_TLS = threading.local()
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def _thread_limits(threads: int) -> None:
|
| 231 |
+
"""numba and OpenMP thread counts are per thread: apply fastprior_pkg's limits once in every thread that predicts (the
|
| 232 |
+
preload runs in the main thread, requests in the server's worker threads)."""
|
| 233 |
+
if not _STATE.get("mode") or getattr(_TLS, "key", None) == (_STATE["stack"], threads):
|
| 234 |
+
return
|
| 235 |
+
from . import fastprior_pkg
|
| 236 |
+
fastprior_pkg._set_threads(int(threads))
|
| 237 |
+
_TLS.key = (_STATE["stack"], threads)
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def preload(name: str, reg, example=None) -> dict:
|
| 241 |
+
"""Server start: load the default model (SHA check, prior stack, numba warmup) and, if given, predict one small example so
|
| 242 |
+
the first user request runs at normal speed. Synchronous (about 20-30 s on the 2-vCPU Space), before the queue opens. The
|
| 243 |
+
learned-prior entries (no prior stack) are loaded and run on the example too."""
|
| 244 |
+
t0 = time.perf_counter()
|
| 245 |
+
load(name, reg)
|
| 246 |
+
STARTUP.update(model=name, load_and_warmup_s=time.perf_counter() - t0, numba_warmup_s=_STATE.get("warmup_s"), mode=_STATE.get("mode"))
|
| 247 |
+
also = [k for k in gnn_names(reg) if k != name and reg["gnn_models"][k].get("learned_prior") and available(k, reg)]
|
| 248 |
+
if example is not None and Path(example).exists():
|
| 249 |
+
from tlsq_infer.layout import prepare
|
| 250 |
+
t = time.perf_counter()
|
| 251 |
+
predict_gnn(prepare(str(example)), name, reg)
|
| 252 |
+
STARTUP["example_s"] = time.perf_counter() - t
|
| 253 |
+
for k in also:
|
| 254 |
+
t = time.perf_counter()
|
| 255 |
+
predict_gnn(prepare(str(example)), k, reg)
|
| 256 |
+
STARTUP.setdefault("also_s", {})[k] = time.perf_counter() - t
|
| 257 |
+
else:
|
| 258 |
+
for k in also:
|
| 259 |
+
load(k, reg)
|
| 260 |
+
STARTUP["total_s"] = time.perf_counter() - t0
|
| 261 |
+
print("preload " + json.dumps({k: (round(v, 2) if isinstance(v, float) else v) for k, v in STARTUP.items()}), flush=True)
|
| 262 |
+
return dict(STARTUP)
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def scene_from_prepared(lay) -> Scene:
|
| 266 |
+
"""The Scene scene_from_gds builds (ground = net 0, signal bodies in file order, chip = ground bounding box, training stack),
|
| 267 |
+
from the prepared ground and conductor bodies."""
|
| 268 |
+
G = lay.ground
|
| 269 |
+
chip = shapely.box(*G.bounds)
|
| 270 |
+
conds = [Conductor("G", "ground", G)] + [Conductor(n, "signal", g) for n, g in zip(lay.names, lay.conductors)]
|
| 271 |
+
return Scene(conds, chip, chip.difference(G), dict(DEFAULT_STACK), {})
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def predict_scene(P: I.Predictor, sc: Scene, alpha=None, local_gap=None) -> dict:
|
| 275 |
+
"""infer.Predictor.from_gds after the scene (same statements)."""
|
| 276 |
+
physics = P.models[0][0].get("schema", {}).get("physics", {})
|
| 277 |
+
if alpha is None:
|
| 278 |
+
alpha = physics.get("alpha")
|
| 279 |
+
if local_gap is None:
|
| 280 |
+
local_gap = physics.get("prior_method") == "local_gap"
|
| 281 |
+
proxy.NUM_THREADS = P.threads
|
| 282 |
+
if set(V2_KWARGS) & set(_STATE["patches"]):
|
| 283 |
+
from . import priorv2
|
| 284 |
+
priorv2.clear() # its per-window traces would otherwise grow for the life of the server
|
| 285 |
+
start = time.perf_counter()
|
| 286 |
+
t1 = time.perf_counter()
|
| 287 |
+
row = lt.add_tokens(sc, scene_graph(sc))
|
| 288 |
+
t2 = time.perf_counter()
|
| 289 |
+
priors = {}
|
| 290 |
+
if getattr(P, "surrogate", None) is not None:
|
| 291 |
+
from . import learned_prior
|
| 292 |
+
priors = learned_prior.priors(*P.surrogate, row)
|
| 293 |
+
elif not P.direct:
|
| 294 |
+
old = fastprior.ALPHA_GAP_SHARE
|
| 295 |
+
try:
|
| 296 |
+
if alpha is not None:
|
| 297 |
+
fastprior.ALPHA_GAP_SHARE = alpha
|
| 298 |
+
for p in ([.75, 1.5] if P.fast else [1.5, 3.]):
|
| 299 |
+
if local_gap:
|
| 300 |
+
from ._tlsq.embed.fastprior_loc import micro_mom_loc
|
| 301 |
+
with proxy.substrate("slab_open", sc.stack["substrate_thickness_um"]), _quiet_mixed_cast():
|
| 302 |
+
mat, _, _ = micro_mom_loc(sc.conductors[0].geom, [c.geom for c in sc.conductors[1:]],
|
| 303 |
+
eps_r=sc.stack["substrate_eps_r"], ppg=p)
|
| 304 |
+
priors[p] = -mat
|
| 305 |
+
else:
|
| 306 |
+
priors[p] = -I.CapacitanceMoM(max_cells=10**9, mesh="lite", cells_per_gap=p,
|
| 307 |
+
substrate="slab_open").compute(sc, list(row["edges"]))["matrix"]
|
| 308 |
+
finally:
|
| 309 |
+
fastprior.ALPHA_GAP_SHARE = old
|
| 310 |
+
t3 = time.perf_counter()
|
| 311 |
+
C = P.from_features(row, priors)
|
| 312 |
+
t4 = time.perf_counter()
|
| 313 |
+
return dict(C=C, names=row["names"], timing=dict(scene=t1 - start, geometry=t2 - t1, prior=t3 - t2, network_and_pack=t4 - t3,
|
| 314 |
+
total=t4 - start))
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
@contextlib.contextmanager
|
| 318 |
+
def _quiet_mixed_cast():
|
| 319 |
+
"""fastprior_pkg's mixed Cholesky casts the system to float32 first; on some windows the cast overflows and numpy warns
|
| 320 |
+
'invalid value / overflow encountered in cast' before the package falls back to the float64 solve (expected; the result is checked
|
| 321 |
+
by iterative refinement). Silence exactly that warning, from that module, during the prior call only."""
|
| 322 |
+
with warnings.catch_warnings():
|
| 323 |
+
warnings.filterwarnings("ignore", message="(invalid value|overflow) encountered in cast", category=RuntimeWarning,
|
| 324 |
+
module=r"geomgnn\.fastprior_pkg")
|
| 325 |
+
yield
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
def physical_checks(C: np.ndarray) -> dict:
|
| 329 |
+
n = len(C)
|
| 330 |
+
off = C[~np.eye(n, dtype=bool)]
|
| 331 |
+
sig = C[1:, 1:]
|
| 332 |
+
return {"symmetric_max_abs": float(np.max(np.abs(C - C.T))), "offdiag_all_negative": bool(np.all(off < 0)),
|
| 333 |
+
"signal_block_min_eig": float(np.linalg.eigvalsh(sig).min()) if n > 1 else None}
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def predict_gnn(prepared, name: str, reg, substrate="Si (cryogenic, training)", thickness_um: float = 350.0, model_dir=None,
|
| 337 |
+
threads: int | None = None) -> dict:
|
| 338 |
+
"""Full N x N Maxwell matrix (fF; ground row/column 0, self terms learned) of a prepared layout with a gnn_models entry."""
|
| 339 |
+
m = reg["gnn_models"][name]
|
| 340 |
+
eps = reg.eps_of(substrate)
|
| 341 |
+
if abs(eps - EPS_TRAIN) > 1e-9 or abs(float(thickness_um) - T_TRAIN_UM) > 1e-9:
|
| 342 |
+
raise ValueError(f"the GNN models were trained on Si (eps_r {EPS_TRAIN:g}), {T_TRAIN_UM:g} µm only and are served for that stack "
|
| 343 |
+
f"only (requested eps_r {eps:g}, {float(thickness_um):g} µm); use E3 or C0 for other substrates")
|
| 344 |
+
t = time.perf_counter()
|
| 345 |
+
P = load(name, reg, model_dir, threads)
|
| 346 |
+
_thread_limits(P.threads)
|
| 347 |
+
load_s = time.perf_counter() - t
|
| 348 |
+
sc = scene_from_prepared(prepared)
|
| 349 |
+
try:
|
| 350 |
+
r = predict_scene(P, sc)
|
| 351 |
+
except AssertionError as e:
|
| 352 |
+
raise ValueError(f"{name}: the prior or the features of this layout are not finite (no matrix returned): {e}") from e
|
| 353 |
+
C = np.asarray(r["C"], float)
|
| 354 |
+
chk = physical_checks(C)
|
| 355 |
+
labels = ["G"] + list(prepared.names)
|
| 356 |
+
pr = m.get("prior")
|
| 357 |
+
lpr = m.get("learned_prior")
|
| 358 |
+
warnings = [w for w in prepared.warnings if "beyond the validated range" not in w]
|
| 359 |
+
if m.get("warning"):
|
| 360 |
+
warnings.insert(0, m["warning"])
|
| 361 |
+
if not (chk["symmetric_max_abs"] <= 1e-6 * max(1.0, float(np.abs(C).max())) and chk["offdiag_all_negative"]
|
| 362 |
+
and (chk["signal_block_min_eig"] is None or chk["signal_block_min_eig"] > 0)):
|
| 363 |
+
warnings.append(f"physical check failed: {chk}")
|
| 364 |
+
tm = r["timing"]
|
| 365 |
+
return {"names": labels, "matrix_fF": C.tolist(), "units": "fF",
|
| 366 |
+
"convention": "Maxwell matrix: diagonal = self (learned), off-diagonal = -mutual; row/column 0 = ground",
|
| 367 |
+
"self_capacitance": "predicted by the network (learned positive row-sum closure; trained). The ground self term is "
|
| 368 |
+
"trained at low weight and depends on the chip boundary: indicative only",
|
| 369 |
+
"model": name, "model_title": m.get("title"), "recipe": "GNN", "arm": m["arm"], "role": m.get("role"),
|
| 370 |
+
"ensemble": m.get("ensemble"), "weights_sha256": [c["sha256"] for c in m["checkpoints"]],
|
| 371 |
+
"prior": ({**pr, "prior_impl": _STATE["prior_impl"], "patches": _STATE.get("patches", []), "mode": _STATE.get("mode") or "frozen",
|
| 372 |
+
"numba_warmup_s": _STATE.get("warmup_s")} if pr else None),
|
| 373 |
+
"learned_prior": ({"name": lpr["name"], "sha256": lpr["checkpoint"]["sha256"], "ppg": lpr["ppg"]} if lpr else None),
|
| 374 |
+
"package_version": f"geomgnn {__version__}",
|
| 375 |
+
"registry_version": reg["version"], "substrate": "Si (cryogenic, training)", "eps_r": EPS_TRAIN, "eps_correction_factor": 1.0,
|
| 376 |
+
"thickness_um": T_TRAIN_UM, "training_stack": {"eps_r": EPS_TRAIN, "thickness_um": T_TRAIN_UM},
|
| 377 |
+
"timing_s": {"load_s": load_s, "geometry_s": tm["geometry"], "prior_s": tm["prior"], "network_s": tm["network_and_pack"],
|
| 378 |
+
"total_s": tm["total"], "threads": P.threads},
|
| 379 |
+
"accuracy": m.get("accuracy") or {}, "checks": chk, "trust": None, "expected_error": {}, "warnings": warnings,
|
| 380 |
+
"stored": False}
|
geomgnn/app_flagship.py
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio front end of the flagship Space (deploy/GNN_SPACE.md "Flagship Space"; registry flagship_hub, flagship_tiers): three tiers in
|
| 2 |
+
one selector (Accurate default, Balanced, Fast), served through geomgnn.flagship (= the paper Space's code path). Endpoints and argument
|
| 3 |
+
order as the GNN Space:
|
| 4 |
+
|
| 5 |
+
/models () -> served tiers with their reference numbers
|
| 6 |
+
/inspect (gds file) -> layer table, layer plot
|
| 7 |
+
/prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS
|
| 8 |
+
/predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model) -> matrix, CSV, JSON, report
|
| 9 |
+
/predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model) -> result dict;
|
| 10 |
+
model = accurate (default) | balanced | fast
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import time
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
from tlsq_infer import __version__ as TLSQ_VERSION
|
| 20 |
+
from tlsq_infer.app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _path, _safe, inspect_fn, matrix_table
|
| 21 |
+
from tlsq_infer.app_v2 import PURGE_AGE_S, _margin_api, _outdir, _prepare, prepare_fn
|
| 22 |
+
from tlsq_infer.layout import LayoutError, prepare
|
| 23 |
+
from tlsq_infer.predict import matrix_csv
|
| 24 |
+
from tlsq_infer.registry import load_registry
|
| 25 |
+
|
| 26 |
+
from . import __version__
|
| 27 |
+
from . import flagship as FS
|
| 28 |
+
|
| 29 |
+
ORDER = ["accurate", "balanced", "fast"]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def served(reg=None) -> list[str]:
|
| 33 |
+
ks = FS.served(reg or load_registry())
|
| 34 |
+
return sorted(ks, key=lambda k: ORDER.index(k) if k in ORDER else len(ORDER))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def resolve(reg, model) -> str:
|
| 38 |
+
m = str(model or reg["flagship_hub"]["default_arm"]).strip().lower()
|
| 39 |
+
if m not in served(reg):
|
| 40 |
+
raise ValueError(f"model {model!r} is not served here; choose one of {served(reg)}")
|
| 41 |
+
return m
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def pct(e) -> str:
|
| 45 |
+
"""'0.177 % (0.0203 / 0.0250 fF)': every % with its MAE / RMSE."""
|
| 46 |
+
return "n/a" if not e else f"{e['median_pct']:g} % ({e['mae_ff']:g} / {e['rmse_ff']:g} fF)"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def numbers(t) -> list[str]:
|
| 50 |
+
r = t.get("reference") or {}
|
| 51 |
+
rows = [("paper, validation (4,608 layouts)", r.get("val")), ("paper, test in family (4,009)", r.get("test_infamily")),
|
| 52 |
+
("paper, test zero-shot macro (1,878)", r.get("test_zeroshot")), ("paper, held-out family (4,071)", r.get("heldout_family")),
|
| 53 |
+
("paper, held-out family: physics alone", r.get("heldout_family_physics")),
|
| 54 |
+
("paper, validation: " + (r.get("val_same_surrogate") or {}).get("note", ""), r.get("val_same_surrogate")),
|
| 55 |
+
("B5 fresh check (39 new layouts, never seen)", t.get("b5")),
|
| 56 |
+
(f"B5: {(t.get('b5_physics') or {}).get('name', 'physics alone')}", t.get("b5_physics"))]
|
| 57 |
+
out = [f"| {k} | {pct(v)} |" for k, v in rows if v]
|
| 58 |
+
if t.get("time_space"):
|
| 59 |
+
out.append(f"| time on this Space (2 vCPU) | {t['time_space']} |")
|
| 60 |
+
if t.get("time_x86"):
|
| 61 |
+
out.append(f"| time, x86 cluster node | {t['time_x86']} |")
|
| 62 |
+
return ["| reference | layout-mean median % (MAE / RMSE fF) |", "|---|---|"] + out
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def tier_md(reg, k) -> str:
|
| 66 |
+
t = FS.tiers(reg)[k]
|
| 67 |
+
lines = [f"**{t['title']}**", "", t.get("summary", "")]
|
| 68 |
+
if t.get("warning"):
|
| 69 |
+
lines += ["", f"> **Warning.** {t['warning']}"]
|
| 70 |
+
lines += ["", f"Reference recipe: {(t.get('reference') or {}).get('recipe', '')}.", "", *numbers(t), "",
|
| 71 |
+
"These networks are trained on all 45,771 on-stack layouts, so they have no held-out score of their own: the paper rows are "
|
| 72 |
+
"the same recipe trained on the TRAIN split; B5 is one fresh check (one generated family, 3-4 conductors). Layout-mean median "
|
| 73 |
+
"relative error over mutual entries against Q3D, 0.1 fF floor."]
|
| 74 |
+
return "\n".join(lines)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _predict(lay, model, substrate, thickness_um):
|
| 78 |
+
reg = load_registry()
|
| 79 |
+
name = resolve(reg, model)
|
| 80 |
+
cap = int(reg["flagship_hub"]["max_conductors"])
|
| 81 |
+
if lay.n_conductors > cap:
|
| 82 |
+
raise LayoutError(f"{lay.n_conductors} conductors: this server is capped at {cap}; run the package locally for larger layouts")
|
| 83 |
+
fn = lambda: FS.predict(lay, name, reg, substrate=substrate, thickness_um=thickness_um) # noqa: E731
|
| 84 |
+
if lay.n_conductors <= 8:
|
| 85 |
+
return fn()
|
| 86 |
+
with HEAVY:
|
| 87 |
+
return fn()
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def report_md(res: dict, reg) -> str:
|
| 91 |
+
t = res["timing_s"]
|
| 92 |
+
st = res.get("prior_stack")
|
| 93 |
+
lv = res.get("levels_ppg") or []
|
| 94 |
+
lines = [f"**Tier** `{res['tier']}`: {res['tier_title']}, {res['package_version']}, registry v{res['registry_version']}",
|
| 95 |
+
"**Physics** " + (f"thin-film solves at {', '.join(f'{p:g}' for p in lv)} panels per gap, prior `{st['name']}` "
|
| 96 |
+
f"(patches {st['patches'] or 'none'}, solver `{st['mode']}`)" if st else "learned prior surrogate (no field solve)"),
|
| 97 |
+
"**Substrate** training stack only: Si, eps_r 11.45, 350 µm",
|
| 98 |
+
"**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float)) + f" ({t.get('threads')} threads)",
|
| 99 |
+
f"**Physical checks** symmetric (max |C - Cᵀ| {res['checks']['symmetric_max_abs']:.2g} fF), off-diagonals negative: "
|
| 100 |
+
f"{res['checks']['offdiag_all_negative']}, smallest eigenvalue of the signal block {res['checks']['signal_block_min_eig']:.4g} fF",
|
| 101 |
+
"", tier_md(reg, res["tier"])]
|
| 102 |
+
if res["warnings"]:
|
| 103 |
+
lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]]
|
| 104 |
+
lines += ["", f"Self capacitance: {res['self_capacitance']}.", "Planar single-layer layouts only (zero-thickness metal). Nothing you "
|
| 105 |
+
f"upload is stored: result files are deleted within {PURGE_AGE_S // 60} minutes."]
|
| 106 |
+
return "\n".join(lines)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, model):
|
| 110 |
+
reg = load_registry()
|
| 111 |
+
lay = _prepare(f, table, crop, margin_gaps, margin_um, select)
|
| 112 |
+
res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness))
|
| 113 |
+
d = _outdir()
|
| 114 |
+
(d / "capacitance_fF.csv").write_text(matrix_csv(res))
|
| 115 |
+
(d / "capacitance.json").write_text(json.dumps(res, indent=1, default=float))
|
| 116 |
+
return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)",
|
| 120 |
+
custom_eps=11.45, thickness_um=350.0, model="accurate"):
|
| 121 |
+
t = time.perf_counter()
|
| 122 |
+
lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers),
|
| 123 |
+
margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"])
|
| 124 |
+
res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness_um))
|
| 125 |
+
res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t
|
| 126 |
+
return res
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def models_fn():
|
| 130 |
+
reg = load_registry()
|
| 131 |
+
h = reg["flagship_hub"]
|
| 132 |
+
out = []
|
| 133 |
+
for k in served(reg):
|
| 134 |
+
t = FS.tiers(reg)[k]
|
| 135 |
+
out.append({"id": k, "title": t["title"], "summary": t.get("summary"), "warning": t.get("warning"), "reference": t.get("reference") or {},
|
| 136 |
+
"b5": t.get("b5"), "b5_physics": t.get("b5_physics"), "time_space": t.get("time_space"), "time_x86": t.get("time_x86"),
|
| 137 |
+
"stack": t.get("stack"), "levels_ppg": [float(p) for p in t.get("ppg") or []], "default": k == h["default_arm"],
|
| 138 |
+
"max_conductors": int(h["max_conductors"])})
|
| 139 |
+
from . import paper as PA
|
| 140 |
+
return out + [{"geomgnn_version": __version__, "tlsq_infer_version": TLSQ_VERSION, "registry_version": reg["version"],
|
| 141 |
+
"training_layouts": h.get("training_layouts"), "paper_space": h.get("paper_space"), "startup": dict(PA.STARTUP)}]
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def header(reg) -> str:
|
| 145 |
+
h = reg["flagship_hub"]
|
| 146 |
+
ps = h.get("paper_space")
|
| 147 |
+
return ("Full **Maxwell capacitance matrix** (fF, self terms included) of a planar superconducting layout from its GDS. Three tiers, "
|
| 148 |
+
"each the best recipe of the paper at its speed, **retrained on all 45,771 on-stack Q3D layouts** (Si, eps_r 11.45, 350 µm): "
|
| 149 |
+
"**Accurate** (default, ~1 s: two thin-film solves + networks), **Balanced** (~0.5 s: a coarser solve pair + networks; about 2x "
|
| 150 |
+
"faster at 1.5-1.7x the error) and **Fast** (~0.1 s: a learned prior, no field solve; quick screening of the trained component "
|
| 151 |
+
"families only, worse than the physics alone on new designs). Use **Accurate** for anything that matters. Trained on everything, "
|
| 152 |
+
"these models have no held-out score of their own: each tier shows the paper's held-out numbers for the same recipe and one fresh "
|
| 153 |
+
f"check on 39 never-seen layouts. To reproduce the paper, use [{ps}](https://huggingface.co/spaces/{ps}). **Private preview**.")
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def build_app(title: str, examples_dir=None):
|
| 157 |
+
import gradio as gr
|
| 158 |
+
|
| 159 |
+
reg = load_registry()
|
| 160 |
+
subs = list(reg.substrates()) + [CUSTOM]
|
| 161 |
+
ms = served(reg)
|
| 162 |
+
default = reg["flagship_hub"]["default_arm"]
|
| 163 |
+
ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else []
|
| 164 |
+
label = {"accurate": "Accurate (~1 s, default)", "balanced": "Balanced (~0.5 s)", "fast": "Fast (~0.1 s; trained families only)"}
|
| 165 |
+
with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo:
|
| 166 |
+
gr.Markdown(f"# {title}\n{header(reg)}")
|
| 167 |
+
with gr.Accordion("Planar circuits and the layer convention", open=False):
|
| 168 |
+
gr.Markdown(INTRO.replace("A thin-film method-of-moments solve\nprovides a physics prior and a graph network trained on 40,153 Q3D simulations corrects it.",
|
| 169 |
+
"See the tier descriptions below."))
|
| 170 |
+
gr.Markdown("## Tier")
|
| 171 |
+
model = gr.Radio([(label.get(k, k), k) for k in ms], value=default, label="model", interactive=True)
|
| 172 |
+
model_info = gr.Markdown(tier_md(reg, default))
|
| 173 |
+
model.change(lambda k: tier_md(reg, k), model, model_info, api_visibility="private")
|
| 174 |
+
gr.Markdown("## 1. Upload a GDS and assign layer roles")
|
| 175 |
+
with gr.Row():
|
| 176 |
+
with gr.Column(scale=1):
|
| 177 |
+
gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"])
|
| 178 |
+
if ex:
|
| 179 |
+
gr.Examples([[str(p)] for p in ex], inputs=[gds], label="or start from an example")
|
| 180 |
+
layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"],
|
| 181 |
+
label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True,
|
| 182 |
+
value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]])
|
| 183 |
+
layer_plot = gr.Plot(label="layers")
|
| 184 |
+
gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)")
|
| 185 |
+
with gr.Row():
|
| 186 |
+
with gr.Column(scale=1):
|
| 187 |
+
select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="")
|
| 188 |
+
with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False):
|
| 189 |
+
crop = gr.Checkbox(False, label="crop the ground plane around the conductors")
|
| 190 |
+
margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)")
|
| 191 |
+
margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider")
|
| 192 |
+
prep_btn = gr.Button("Prepare", variant="secondary")
|
| 193 |
+
warn_md = gr.Markdown()
|
| 194 |
+
summary = gr.JSON(label="prepared layout")
|
| 195 |
+
prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)")
|
| 196 |
+
prep_plot = gr.Plot(label="prepared layout")
|
| 197 |
+
gr.Markdown("## 3. Substrate\nThe models were trained on Si (eps_r 11.45), 350 µm only and are served for that stack only.")
|
| 198 |
+
with gr.Row():
|
| 199 |
+
substrate = gr.Dropdown(subs, value=subs[0], label="substrate")
|
| 200 |
+
custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')")
|
| 201 |
+
thickness = gr.Number(value=350, label="substrate thickness (µm)")
|
| 202 |
+
pred_btn = gr.Button("Predict", variant="primary")
|
| 203 |
+
gr.Markdown("## 4. Maxwell capacitance matrix (fF)")
|
| 204 |
+
with gr.Row():
|
| 205 |
+
with gr.Column(scale=2):
|
| 206 |
+
mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False)
|
| 207 |
+
with gr.Column():
|
| 208 |
+
csv_f = gr.File(label="CSV")
|
| 209 |
+
json_f = gr.File(label="JSON (matrix + metadata)")
|
| 210 |
+
report = gr.Markdown()
|
| 211 |
+
res_state = gr.State(None)
|
| 212 |
+
with gr.Row(visible=False):
|
| 213 |
+
a_g = gr.Textbox("1/0")
|
| 214 |
+
a_c = gr.Textbox("1/10")
|
| 215 |
+
a_m = gr.Number(0)
|
| 216 |
+
a_out = gr.JSON()
|
| 217 |
+
a_btn = gr.Button("api")
|
| 218 |
+
m_btn = gr.Button("models")
|
| 219 |
+
m_out = gr.JSON()
|
| 220 |
+
gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect")
|
| 221 |
+
prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds],
|
| 222 |
+
api_name="prepare")
|
| 223 |
+
pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model],
|
| 224 |
+
[mat, csv_f, json_f, report, res_state], api_name="predict")
|
| 225 |
+
a_btn.click(_safe(api_predict_gds, 1), [gds, a_g, a_c, a_m, substrate, custom_eps, thickness, model], a_out, api_name="predict_gds")
|
| 226 |
+
m_btn.click(models_fn, None, m_out, api_name="models")
|
| 227 |
+
gr.Markdown(f"<small>geomgnn {__version__}, tlsq_infer {TLSQ_VERSION}. Demo-server caps (not limits of the models): "
|
| 228 |
+
f"{LIMITS['max_file_mb']:g} MB per file, {LIMITS['max_polygons']:,} polygons, {reg['flagship_hub']['max_conductors']} conductors "
|
| 229 |
+
"+ ground. One request runs at a time; a 66-qubit lattice takes minutes on Accurate.</small>")
|
| 230 |
+
return demo
|
geomgnn/app_gnn.py
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio front end of the GNN Space (deploy/GNN_SPACE.md): the paper-models Space (tlsq_infer.app_v2) with the geomfast GNN
|
| 2 |
+
(geomgnn.api, registry gnn_models) in two tiers, accurate (default, field-solve prior) and fast (learned prior), and E3 / C0
|
| 3 |
+
(tlsq_infer.final, registry final_models) kept for comparison.
|
| 4 |
+
Same endpoints and argument order as app_v2:
|
| 5 |
+
|
| 6 |
+
/models () -> served models: id, title, recipe, role, accuracy
|
| 7 |
+
/inspect (gds file) -> layer table, layer plot
|
| 8 |
+
/prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS
|
| 9 |
+
/predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model, self_from_rows,
|
| 10 |
+
trust_rule) -> matrix table, CSV, JSON, report
|
| 11 |
+
/predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model, self_from_rows, trust_rule)
|
| 12 |
+
-> result dict. model: "GNN" (accurate, default), "GNN-LP" (fast), "E3", "C0" (the server's radio accepts these ids
|
| 13 |
+
only; in process also "accurate" / "gnn" / "fast" / "model" / "physics").
|
| 14 |
+
GNN models: training stack only (Si 11.45, 350 um); self_from_rows and trust_rule apply to E3 / C0 only.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import time
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
|
| 23 |
+
from tlsq_infer import __version__ as TLSQ_VERSION
|
| 24 |
+
from tlsq_infer import final as F
|
| 25 |
+
from tlsq_infer.app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _path, _safe, inspect_fn, matrix_table
|
| 26 |
+
from tlsq_infer.app_v2 import NETWORK_CAP, PURGE_AGE_S, _margin_api, _outdir, _prepare, prepare_fn
|
| 27 |
+
from tlsq_infer.app_v2 import report_md as report_md_v2
|
| 28 |
+
from tlsq_infer.layout import LayoutError, prepare
|
| 29 |
+
from tlsq_infer.predict import matrix_csv
|
| 30 |
+
from tlsq_infer.registry import load_registry
|
| 31 |
+
|
| 32 |
+
from . import __version__
|
| 33 |
+
from . import api as A
|
| 34 |
+
|
| 35 |
+
ALIASES = {"gnn": "GNN", "accurate": "GNN", "fast": "GNN-LP", "gnn-lp": "GNN-LP", "learned prior": "GNN-LP", "model": "E3",
|
| 36 |
+
"physics": "C0", "physics only": "C0"}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def compare_names(reg) -> list[str]:
|
| 40 |
+
return [k for k in (reg["gnn_hub"].get("compare_models") or []) if k in (reg.data.get("final_models") or {})]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def served(reg=None) -> list[str]:
|
| 44 |
+
reg = reg or load_registry()
|
| 45 |
+
return [k for k in A.gnn_names(reg) if A.available(k, reg)] + [k for k in compare_names(reg) if F.available(k, reg)]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def resolve(reg, model) -> str:
|
| 49 |
+
m = str(model or reg["gnn_hub"]["default_model"]).strip()
|
| 50 |
+
m = ALIASES.get(m.lower(), m)
|
| 51 |
+
if m not in served(reg):
|
| 52 |
+
old = (reg.data.get("gnn_models") or {}).get(m) or {}
|
| 53 |
+
why = f" ({m} is retired: {old['retired']})" if old.get("retired") else ""
|
| 54 |
+
raise ValueError(f"model {model!r} is not served here{why}; choose one of {served(reg)} (or accurate / fast / model / physics)")
|
| 55 |
+
return m
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def is_gnn(reg, name) -> bool:
|
| 59 |
+
return name in (reg.data.get("gnn_models") or {})
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _predict(lay, model, substrate, thickness_um, self_from_rows=True, trust_rule=None):
|
| 63 |
+
reg = load_registry()
|
| 64 |
+
name = resolve(reg, model)
|
| 65 |
+
if lay.n_conductors > NETWORK_CAP and (is_gnn(reg, name) or F.load_model(name, reg).recipe == "E3"):
|
| 66 |
+
raise LayoutError(f"{lay.n_conductors} conductors: the network models are capped at {NETWORK_CAP} on this server; use the "
|
| 67 |
+
"physics-only C0 recipe, or run the package locally")
|
| 68 |
+
if is_gnn(reg, name):
|
| 69 |
+
fn = lambda: A.predict_gnn(lay, name, reg, substrate=substrate, thickness_um=thickness_um) # noqa: E731
|
| 70 |
+
else:
|
| 71 |
+
tr = bool(reg["final_defaults"].get("trust_rule", True)) if trust_rule is None else bool(trust_rule)
|
| 72 |
+
fn = lambda: F.predict_final(lay, name, substrate=substrate, thickness_um=thickness_um, # noqa: E731
|
| 73 |
+
self_from_rows=bool(self_from_rows), trust_rule=tr)
|
| 74 |
+
if lay.n_conductors <= 8:
|
| 75 |
+
return fn()
|
| 76 |
+
with HEAVY:
|
| 77 |
+
return fn()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def _fmt(x, nd=3) -> str:
|
| 81 |
+
return "n/a" if x is None else (f"{x:.{nd}f}" if abs(x) < 100 else f"{x:.1f}")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
DEFAULT_ACC_NOTE = ("paper_v4 Table 2: layout-mean median relative error, MAE and RMSE over mutual entries, 0.1 fF floor; frozen "
|
| 85 |
+
"before any test set was read and scored once.")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def acc_note(m: dict) -> str:
|
| 89 |
+
return m.get("accuracy_note") or DEFAULT_ACC_NOTE
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def extra_acc(m: dict) -> list[str]:
|
| 93 |
+
"""Signal-self rows, test cohorts, held-out-family rows and the prior-v1 test cohorts of an entry, when the registry has them."""
|
| 94 |
+
out = []
|
| 95 |
+
if m.get("accuracy_signal_self"):
|
| 96 |
+
out += ["", "Signal self terms:", ""] + acc_lines({"accuracy": m["accuracy_signal_self"]})
|
| 97 |
+
if m.get("accuracy_test"):
|
| 98 |
+
out += ["", m.get("accuracy_test_note", ""), ""] + acc_lines({"accuracy": m["accuracy_test"]})
|
| 99 |
+
if m.get("accuracy_lofo"):
|
| 100 |
+
out += ["", m.get("accuracy_lofo_note", ""), ""] + acc_lines({"accuracy": m["accuracy_lofo"]})
|
| 101 |
+
if m.get("accuracy_test_prior_v1") and not m.get("accuracy_test"):
|
| 102 |
+
out += ["", m.get("accuracy_test_prior_v1_note", ""), ""] + acc_lines({"accuracy": m["accuracy_test_prior_v1"]})
|
| 103 |
+
return out
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def acc_lines(m: dict) -> list[str]:
|
| 107 |
+
acc = m.get("accuracy") or {}
|
| 108 |
+
if not acc:
|
| 109 |
+
return ["No test number applies to this model."]
|
| 110 |
+
rows = ["| test cohort | layouts | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|---|"]
|
| 111 |
+
rows += [f"| {v['set']} | {v.get('layouts', '')} | {_fmt(v['median_pct'])} | {_fmt(v['mae_ff'], 4)} | {_fmt(v['rmse_ff'], 4)} |"
|
| 112 |
+
for v in acc.values()]
|
| 113 |
+
return rows
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def lat_test_lines(m: dict) -> list[str]:
|
| 117 |
+
lat = m.get("latency_test") or {}
|
| 118 |
+
if not lat:
|
| 119 |
+
return []
|
| 120 |
+
rows = ["| test latency | layouts | median (s) | p90 (s) | sum (s) |", "|---|---|---|---|---|"]
|
| 121 |
+
rows += [f"| {v['set']} | {v['layouts']} | {_fmt(v.get('median_s'))} | {_fmt(v.get('p90_s'), 2)} | {_fmt(v.get('sum_s'), 0)} |" for v in lat.values()]
|
| 122 |
+
return rows + ["", m.get("latency_test_note", "")]
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def lat_lines(m: dict) -> list[str]:
|
| 126 |
+
lat = m.get("latency") or {}
|
| 127 |
+
if not lat:
|
| 128 |
+
return []
|
| 129 |
+
rows = ["| latency set | layouts | total median (s) | total p95 (s) | geometry (s) | prior (s) | network (s) |", "|---|---|---|---|---|---|---|"]
|
| 130 |
+
rows += [f"| {v['set']} | {v['layouts']} | {v['total_median_s']:.3f} | {v['total_p95_s']:.2f} | {v['geometry_median_s']:.3f} | "
|
| 131 |
+
f"{v['prior_median_s']:.3f} | {v['network_median_s']:.4f} |" for v in lat.values()]
|
| 132 |
+
machine = next(iter(lat.values())).get("machine", "")
|
| 133 |
+
return rows + ["", f"Wall time from the layout file to the matrix, median over layouts; {machine}."]
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def report_md(res: dict, reg) -> str:
|
| 137 |
+
if not is_gnn(reg, res["model"]):
|
| 138 |
+
return report_md_v2(res, reg)
|
| 139 |
+
m = reg["gnn_models"][res["model"]]
|
| 140 |
+
t = res["timing_s"]
|
| 141 |
+
pr = res.get("prior")
|
| 142 |
+
lp = res.get("learned_prior")
|
| 143 |
+
lines = [f"**Model** `{res['model']}`: {res['model_title']} ({res['role']}), {res['package_version']}, registry v{res['registry_version']}",
|
| 144 |
+
"**Prior** " + (f"{pr['method']} micro-MoM at {pr['ppg'][0]:g} and {pr['ppg'][1]:g} panels per gap, alpha {pr['alpha']}, "
|
| 145 |
+
f"implementation `{pr['prior_impl']}`, patches {pr.get('patches') or 'none'}, solver mode `{pr.get('mode')}`"
|
| 146 |
+
if pr else f"learned prior `{lp['name']}` (no field solve; predicts the 1.5 and 3 panels-per-gap priors)"
|
| 147 |
+
if lp else "none (solver-free)"),
|
| 148 |
+
f"**Substrate** training stack only: Si, eps_r {res['eps_r']:g}, {res['thickness_um']:g} µm",
|
| 149 |
+
"**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float))
|
| 150 |
+
+ f" ({t.get('threads')} threads)",
|
| 151 |
+
f"**Physical checks** symmetric (max |C - Cᵀ| {res['checks']['symmetric_max_abs']:.2g} fF), off-diagonals negative: "
|
| 152 |
+
f"{res['checks']['offdiag_all_negative']}, smallest eigenvalue of the signal block {res['checks']['signal_block_min_eig']:.4g} fF",
|
| 153 |
+
"", f"**Accuracy of this model.** {acc_note(m)}", ""] + acc_lines(m) + extra_acc(m)
|
| 154 |
+
if res["warnings"]:
|
| 155 |
+
lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]]
|
| 156 |
+
lines += ["", f"Self capacitance: {res['self_capacitance']}.",
|
| 157 |
+
"Planar single-layer layouts only (zero-thickness metal). Nothing you upload is stored: result files are deleted "
|
| 158 |
+
f"within {PURGE_AGE_S // 60} minutes."]
|
| 159 |
+
return "\n".join(lines)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_rule=True):
|
| 163 |
+
reg = load_registry()
|
| 164 |
+
lay = _prepare(f, table, crop, margin_gaps, margin_um, select)
|
| 165 |
+
res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness), self_from_rows=bool(self_rows), trust_rule=trust_rule)
|
| 166 |
+
d = _outdir()
|
| 167 |
+
(d / "capacitance_fF.csv").write_text(matrix_csv(res))
|
| 168 |
+
(d / "capacitance.json").write_text(json.dumps(res, indent=1))
|
| 169 |
+
return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)",
|
| 173 |
+
custom_eps=11.45, thickness_um=350.0, model="GNN", self_from_rows=True, trust_rule=True):
|
| 174 |
+
t = time.perf_counter()
|
| 175 |
+
lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers),
|
| 176 |
+
margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"])
|
| 177 |
+
res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness_um), self_from_rows=bool(self_from_rows), trust_rule=trust_rule)
|
| 178 |
+
res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t
|
| 179 |
+
return res
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def _entry(reg, k) -> dict:
|
| 183 |
+
return reg["gnn_models"][k] if is_gnn(reg, k) else reg["final_models"][k]
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def _lp_public(lp):
|
| 187 |
+
"""The learned prior without its file path (a {"path": ...} dict would be read as a file by gradio clients)."""
|
| 188 |
+
return None if not lp else {"name": lp["name"], "sha256": lp["checkpoint"]["sha256"], "ppg": lp.get("ppg"), "training": lp.get("training")}
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def models_fn():
|
| 192 |
+
reg = load_registry()
|
| 193 |
+
out = []
|
| 194 |
+
for k in served(reg):
|
| 195 |
+
m = _entry(reg, k)
|
| 196 |
+
d = {"id": k, "title": m["title"], "recipe": "GNN" if is_gnn(reg, k) else m["recipe"], "role": m.get("role"),
|
| 197 |
+
"accuracy": m.get("accuracy") or {}, "warning": m.get("warning"), "default": k == reg["gnn_hub"]["default_model"]}
|
| 198 |
+
if is_gnn(reg, k):
|
| 199 |
+
d |= {"arm": m["arm"], "prior": m.get("prior"), "learned_prior": _lp_public(m.get("learned_prior")), "accuracy_test": m.get("accuracy_test") or {},
|
| 200 |
+
"latency": m.get("latency") or {}, "ensemble": m.get("ensemble"),
|
| 201 |
+
"substrate": "training stack only (Si 11.45, 350 um)"}
|
| 202 |
+
else:
|
| 203 |
+
d["ledger_id"] = m.get("ledger_id")
|
| 204 |
+
out.append(d)
|
| 205 |
+
return out + [{"geomgnn_version": __version__, "tlsq_infer_version": TLSQ_VERSION, "registry_version": reg["version"], "startup": dict(A.STARTUP)}]
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def _model_md(reg, k) -> str:
|
| 209 |
+
m = _entry(reg, k)
|
| 210 |
+
if not is_gnn(reg, k):
|
| 211 |
+
acc = (m.get("accuracy") or {}).values()
|
| 212 |
+
rows = ["| test set | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|"]
|
| 213 |
+
rows += [f"| {v['set']} | {v['median_pct']:.3f} | {v['mae_ff']:.3f} | {v['rmse_ff']:.3f} |" for v in acc]
|
| 214 |
+
return "\n".join([f"**{m['title']}** ({m.get('role')}; for comparison). {m.get('summary', '')}", "",
|
| 215 |
+
"Test results as reported in paper_v3 (pooled over entries; not the paired paper_v4 cohorts of the GNN rows):", ""]
|
| 216 |
+
+ (rows if acc else ["n/a"]))
|
| 217 |
+
return "\n".join([f"**{m['title']}** ({m.get('role')}). {m.get('summary', '')}"] + ([f"\n> **Warning.** {m['warning']}"] if m.get("warning") else [])
|
| 218 |
+
+ ["", acc_note(m), "", *acc_lines(m), *extra_acc(m), "", *lat_lines(m)] + ([m["latency_note"]] if m.get("latency_note") else [])
|
| 219 |
+
+ ["", *lat_test_lines(m)])
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def header(reg) -> str:
|
| 223 |
+
h = reg["gnn_hub"]
|
| 224 |
+
return (f"Zero-shot **Maxwell capacitance matrix** of a planar superconducting layout from its GDS. The geomfast GNN, a "
|
| 225 |
+
f"message-passing network on the full geometric embedding that corrects a two-resolution thin-film MoM prior and predicts "
|
| 226 |
+
f"the complete N x N matrix (self terms included), in two tiers: **Accurate** (default) solves the prior, **Fast** predicts "
|
| 227 |
+
f"it with a learned surrogate (no field solve: 6-10x faster, 3.6x the median error in family). **Use Fast only for the "
|
| 228 |
+
f"component families it was trained on** (listed under the model); on a component family it never saw it is worse than "
|
| 229 |
+
f"the physics prior alone, so **for a new component type use Accurate** (numbers under each model). "
|
| 230 |
+
f"{h.get('comparison_note', '')} **Private preview** (paper in preparation).")
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def build_app(title: str, examples_dir=None):
|
| 234 |
+
import gradio as gr
|
| 235 |
+
|
| 236 |
+
reg = load_registry()
|
| 237 |
+
subs = list(reg.substrates()) + [CUSTOM]
|
| 238 |
+
ms = served(reg)
|
| 239 |
+
default = reg["gnn_hub"]["default_model"]
|
| 240 |
+
ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else []
|
| 241 |
+
with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo:
|
| 242 |
+
gr.Markdown(f"# {title}\n{header(reg)}")
|
| 243 |
+
with gr.Accordion("Planar circuits and the layer convention", open=False):
|
| 244 |
+
gr.Markdown(INTRO.replace("A thin-film method-of-moments solve\nprovides a physics prior and a graph network trained on 40,153 Q3D simulations corrects it.",
|
| 245 |
+
"See the model descriptions below."))
|
| 246 |
+
gr.Markdown("## Model")
|
| 247 |
+
model = gr.Radio([(_entry(reg, k)["title"], k) for k in ms], value=default, label="model")
|
| 248 |
+
model_info = gr.Markdown(_model_md(reg, default))
|
| 249 |
+
model.change(lambda k: _model_md(reg, k), model, model_info, api_visibility="private")
|
| 250 |
+
gr.Markdown("## 1. Upload a GDS and assign layer roles")
|
| 251 |
+
with gr.Row():
|
| 252 |
+
with gr.Column(scale=1):
|
| 253 |
+
gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"])
|
| 254 |
+
if ex:
|
| 255 |
+
gr.Examples([[str(p)] for p in ex], inputs=[gds], label="or start from an example")
|
| 256 |
+
layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"],
|
| 257 |
+
label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True,
|
| 258 |
+
value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]])
|
| 259 |
+
layer_plot = gr.Plot(label="layers")
|
| 260 |
+
gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)")
|
| 261 |
+
with gr.Row():
|
| 262 |
+
with gr.Column(scale=1):
|
| 263 |
+
select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="")
|
| 264 |
+
with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False):
|
| 265 |
+
crop = gr.Checkbox(False, label="crop the ground plane around the conductors")
|
| 266 |
+
margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)")
|
| 267 |
+
margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider")
|
| 268 |
+
prep_btn = gr.Button("Prepare", variant="secondary")
|
| 269 |
+
warn_md = gr.Markdown()
|
| 270 |
+
summary = gr.JSON(label="prepared layout")
|
| 271 |
+
prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)")
|
| 272 |
+
prep_plot = gr.Plot(label="prepared layout")
|
| 273 |
+
gr.Markdown("## 3. Substrate\nThe GNN models are served for their training stack only (Si, eps_r 11.45, 350 µm); other substrates "
|
| 274 |
+
"are available with E3 / C0 (half-space factor (1+eps)/(1+11.45)).")
|
| 275 |
+
with gr.Row():
|
| 276 |
+
substrate = gr.Dropdown(subs, value=subs[0], label="substrate")
|
| 277 |
+
custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')")
|
| 278 |
+
thickness = gr.Number(value=350, label="substrate thickness (µm)")
|
| 279 |
+
self_rows = gr.Checkbox(True, label="E3 / C0: self-capacitance from row sums (the GNN predicts its self terms)")
|
| 280 |
+
trust_cb = gr.Checkbox(bool(reg["final_defaults"].get("trust_rule", True)),
|
| 281 |
+
label="E3: apply the trust rule (default on: fall back to physics-only C0 when the networks disagree)")
|
| 282 |
+
pred_btn = gr.Button("Predict", variant="primary")
|
| 283 |
+
gr.Markdown("## 4. Maxwell capacitance matrix (fF)")
|
| 284 |
+
with gr.Row():
|
| 285 |
+
with gr.Column(scale=2):
|
| 286 |
+
mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False)
|
| 287 |
+
with gr.Column():
|
| 288 |
+
csv_f = gr.File(label="CSV")
|
| 289 |
+
json_f = gr.File(label="JSON (matrix + metadata)")
|
| 290 |
+
report = gr.Markdown()
|
| 291 |
+
res_state = gr.State(None)
|
| 292 |
+
with gr.Row(visible=False):
|
| 293 |
+
a_g = gr.Textbox("1/0")
|
| 294 |
+
a_c = gr.Textbox("1/10")
|
| 295 |
+
a_m = gr.Number(0)
|
| 296 |
+
a_out = gr.JSON()
|
| 297 |
+
a_btn = gr.Button("api")
|
| 298 |
+
m_btn = gr.Button("models")
|
| 299 |
+
m_out = gr.JSON()
|
| 300 |
+
gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect")
|
| 301 |
+
prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds],
|
| 302 |
+
api_name="prepare")
|
| 303 |
+
pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_cb],
|
| 304 |
+
[mat, csv_f, json_f, report, res_state], api_name="predict")
|
| 305 |
+
a_btn.click(_safe(api_predict_gds, 1), [gds, a_g, a_c, a_m, substrate, custom_eps, thickness, model, self_rows, trust_cb], a_out,
|
| 306 |
+
api_name="predict_gds")
|
| 307 |
+
m_btn.click(models_fn, None, m_out, api_name="models")
|
| 308 |
+
gr.Markdown(f"<small>geomgnn {__version__}, tlsq_infer {TLSQ_VERSION}. Demo-server caps (not limits of the models): "
|
| 309 |
+
f"{LIMITS['max_file_mb']:g} MB per file, {LIMITS['max_polygons']:,} polygons, {LIMITS['max_conductors']} conductors + "
|
| 310 |
+
f"ground, {NETWORK_CAP} for the network models (GNN, E3).</small>")
|
| 311 |
+
return demo
|
geomgnn/app_paper.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio front end of the paper Space (deploy/GNN_SPACE.md "Paper Space"; registry paper_hub, paper_arms): one selector over every
|
| 2 |
+
arm of the paper's ladder and routes tables that runs on a CPU (geomgnn.paper). Same endpoints and argument order as the GNN Space:
|
| 3 |
+
|
| 4 |
+
/models () -> served arms: id, paper name, route, numbers
|
| 5 |
+
/inspect (gds file) -> layer table, layer plot
|
| 6 |
+
/prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS
|
| 7 |
+
/predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, arm, self_from_rows,
|
| 8 |
+
trust_rule) -> matrix table, CSV, JSON, report
|
| 9 |
+
/predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, arm, self_from_rows, trust_rule)
|
| 10 |
+
-> result dict; arm = an id of /models (default "model")
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import time
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
from tlsq_infer import __version__ as TLSQ_VERSION
|
| 20 |
+
from tlsq_infer.app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _path, _safe, inspect_fn, matrix_table
|
| 21 |
+
from tlsq_infer.app_v2 import NETWORK_CAP, PURGE_AGE_S, _margin_api, _outdir, _prepare, prepare_fn
|
| 22 |
+
from tlsq_infer.app_v2 import report_md as report_md_v2
|
| 23 |
+
from tlsq_infer.layout import LayoutError, prepare
|
| 24 |
+
from tlsq_infer.predict import matrix_csv
|
| 25 |
+
from tlsq_infer.registry import load_registry
|
| 26 |
+
|
| 27 |
+
from . import __version__
|
| 28 |
+
from . import paper as PA
|
| 29 |
+
|
| 30 |
+
ROUTES = ["(d) good physics", "(c) cheap physics", "(b) learned physics", "(a) no physics", "baseline"]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def served(reg=None) -> list[str]:
|
| 34 |
+
reg = reg or load_registry()
|
| 35 |
+
ks = [k for k in PA.live(reg) if PA.available(k, reg)]
|
| 36 |
+
return sorted(ks, key=lambda k: (ROUTES.index(PA.arms(reg)[k].get("route", "baseline")), list(PA.arms(reg)).index(k)))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def resolve(reg, arm) -> str:
|
| 40 |
+
a = str(arm or reg["paper_hub"]["default_arm"]).strip()
|
| 41 |
+
if a not in served(reg):
|
| 42 |
+
raise ValueError(f"arm {arm!r} is not served here; choose one of {served(reg)}")
|
| 43 |
+
return a
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def cap(reg, a) -> int:
|
| 47 |
+
"""Signal conductors per layout on this Space: the prior-generation network keeps the GNN Space's network cap (dense pair
|
| 48 |
+
tensors); every other arm runs up to paper_hub.max_conductors (the nine-qubit lattice of the paper)."""
|
| 49 |
+
return NETWORK_CAP if (a.get("kind") == "final" and a.get("final_model") == "E3") else int(reg["paper_hub"]["max_conductors"])
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def _predict(lay, arm, substrate, thickness_um, self_from_rows=True, trust_rule=None):
|
| 53 |
+
reg = load_registry()
|
| 54 |
+
name = resolve(reg, arm)
|
| 55 |
+
a = PA.arms(reg)[name]
|
| 56 |
+
if lay.n_conductors > cap(reg, a):
|
| 57 |
+
raise LayoutError(f"{lay.n_conductors} conductors: the arm '{name}' is capped at {cap(reg, a)} on this server; run the package "
|
| 58 |
+
"locally for larger layouts")
|
| 59 |
+
fn = lambda: PA.predict(lay, name, reg, substrate=substrate, thickness_um=thickness_um, self_from_rows=self_from_rows, # noqa: E731
|
| 60 |
+
trust_rule=trust_rule)
|
| 61 |
+
if lay.n_conductors <= 8:
|
| 62 |
+
return fn()
|
| 63 |
+
with HEAVY:
|
| 64 |
+
return fn()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def pct(e) -> str:
|
| 68 |
+
"""'0.177 % (0.0203 / 0.0250 fF)': every % with its MAE / RMSE."""
|
| 69 |
+
return "n/a" if not e else f"{e['median_pct']:g} % ({e['mae_ff']:g} / {e['rmse_ff']:g} fF)"
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def numbers(a) -> list[str]:
|
| 73 |
+
p = a.get("paper") or {}
|
| 74 |
+
rows = [("validation (4,608 layouts)", p.get("val")), ("test, in family (4,009)", p.get("test_infamily")),
|
| 75 |
+
("test, zero-shot macro (1,878)", p.get("test_zeroshot")), ("held-out family (4,071 validation layouts)", p.get("heldout_family"))]
|
| 76 |
+
out = [f"| {k} | {pct(v)}{' — ' + v['note'] if v and v.get('note') else ''} |" for k, v in rows if v]
|
| 77 |
+
if p.get("time"):
|
| 78 |
+
out.append(f"| time per layout (median / 90th percentile) | {p['time']} |")
|
| 79 |
+
if p.get("note"):
|
| 80 |
+
out.append(f"| note | {p['note']} |")
|
| 81 |
+
return (["| paper | layout-mean median % (MAE / RMSE fF) |", "|---|---|"] + out) if out else []
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def arm_md(reg, k) -> str:
|
| 85 |
+
a = PA.arms(reg)[k]
|
| 86 |
+
tabs = ", ".join((a.get("paper") or {}).get("tables") or [])
|
| 87 |
+
lines = [f"**{a['paper_name']}** ({a.get('route')}; paper: {tabs}). {a.get('summary', '')}"]
|
| 88 |
+
if a.get("warning"):
|
| 89 |
+
lines.append(f"\n> **Warning.** {a['warning']}")
|
| 90 |
+
lines += ["", *numbers(a), "", "Layout-mean median relative error over mutual entries against Q3D, 0.1 fF floor; MAE / RMSE in fF. "
|
| 91 |
+
"Test = the paper's test benchmark, read once; validation = development data."]
|
| 92 |
+
if a["kind"] == "final":
|
| 93 |
+
m = reg["final_models"][a["final_model"]]
|
| 94 |
+
acc = (m.get("accuracy") or {}).values()
|
| 95 |
+
if acc:
|
| 96 |
+
lines += ["", "Prior-generation evaluation (pooled over entries, not the layout-mean of the table above):", "",
|
| 97 |
+
"| set | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|"]
|
| 98 |
+
lines += [f"| {v['set']} | {v['median_pct']:.3f} | {v['mae_ff']:.3f} | {v['rmse_ff']:.3f} |" for v in acc]
|
| 99 |
+
return "\n".join(lines)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def report_md(res: dict, reg) -> str:
|
| 103 |
+
a = PA.arms(reg)[res["arm"]]
|
| 104 |
+
if a["kind"] == "final":
|
| 105 |
+
return report_md_v2(res, reg)
|
| 106 |
+
t = res["timing_s"]
|
| 107 |
+
st = res.get("prior_stack")
|
| 108 |
+
lv = res.get("levels_ppg") or []
|
| 109 |
+
lines = [f"**Arm** `{res['arm']}`: {res['paper_name']} ({res['route']}), {res['package_version']}, registry v{res['registry_version']}",
|
| 110 |
+
"**Physics** " + (f"thin-film solves at {', '.join(f'{p:g}' for p in lv)} panels per gap, prior `{st['name']}` "
|
| 111 |
+
f"(patches {st['patches'] or 'none'}, solver `{st['mode']}`)" if st else
|
| 112 |
+
"learned prior surrogate (no field solve)" if res.get("learned_prior_sha256") else "none (solver-free)"),
|
| 113 |
+
"**Substrate** training stack only: Si, eps_r 11.45, 350 µm",
|
| 114 |
+
"**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float))
|
| 115 |
+
+ f" ({t.get('threads')} threads)",
|
| 116 |
+
f"**Physical checks** symmetric (max |C - Cᵀ| {res['checks']['symmetric_max_abs']:.2g} fF), off-diagonals negative: "
|
| 117 |
+
f"{res['checks']['offdiag_all_negative']}, smallest eigenvalue of the signal block {res['checks']['signal_block_min_eig']:.4g} fF",
|
| 118 |
+
"", arm_md(reg, res["arm"])]
|
| 119 |
+
if res["warnings"]:
|
| 120 |
+
lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]]
|
| 121 |
+
lines += ["", f"Self capacitance: {res['self_capacitance']}.", "Planar single-layer layouts only (zero-thickness metal). Nothing you "
|
| 122 |
+
f"upload is stored: result files are deleted within {PURGE_AGE_S // 60} minutes."]
|
| 123 |
+
return "\n".join(lines)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, arm, self_rows, trust_rule=True):
|
| 127 |
+
reg = load_registry()
|
| 128 |
+
lay = _prepare(f, table, crop, margin_gaps, margin_um, select)
|
| 129 |
+
res = _predict(lay, arm, _eps(substrate, custom_eps), float(thickness), self_from_rows=bool(self_rows), trust_rule=trust_rule)
|
| 130 |
+
d = _outdir()
|
| 131 |
+
(d / "capacitance_fF.csv").write_text(matrix_csv(res))
|
| 132 |
+
(d / "capacitance.json").write_text(json.dumps(res, indent=1, default=float))
|
| 133 |
+
return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)",
|
| 137 |
+
custom_eps=11.45, thickness_um=350.0, arm="model", self_from_rows=True, trust_rule=True):
|
| 138 |
+
t = time.perf_counter()
|
| 139 |
+
lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers),
|
| 140 |
+
margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"])
|
| 141 |
+
res = _predict(lay, arm, _eps(substrate, custom_eps), float(thickness_um), self_from_rows=bool(self_from_rows), trust_rule=trust_rule)
|
| 142 |
+
res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t
|
| 143 |
+
return res
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def models_fn():
|
| 147 |
+
reg = load_registry()
|
| 148 |
+
out = []
|
| 149 |
+
for k in served(reg):
|
| 150 |
+
a = PA.arms(reg)[k]
|
| 151 |
+
out.append({"id": k, "paper_name": a["paper_name"], "title": a["title"], "route": a.get("route"), "kind": a["kind"],
|
| 152 |
+
"summary": a.get("summary"), "warning": a.get("warning"), "paper": a.get("paper") or {},
|
| 153 |
+
"stack": a.get("stack"), "levels_ppg": PA.levels_of(a), "default": k == reg["paper_hub"]["default_arm"],
|
| 154 |
+
"max_conductors": cap(reg, a)})
|
| 155 |
+
return out + [{"geomgnn_version": __version__, "tlsq_infer_version": TLSQ_VERSION, "registry_version": reg["version"],
|
| 156 |
+
"checkpoints_repo": reg["paper_hub"].get("checkpoints_repo"), "startup": dict(PA.STARTUP)}]
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def header(reg) -> str:
|
| 160 |
+
h = reg["paper_hub"]
|
| 161 |
+
ck = h.get("checkpoints_repo")
|
| 162 |
+
return ("Full **Maxwell capacitance matrix** of a planar superconducting layout from its GDS, with **every route of the paper that "
|
| 163 |
+
"runs on a CPU**: the model, the fast model, the solver-free network, the cheap-physics networks, the mesh ladder of single "
|
| 164 |
+
"solves and Richardson pairs, the extrapolated physics alone, the ablations and controls, the solver modes and the "
|
| 165 |
+
"prior-generation baseline. Each arm shows its paper numbers (layout-mean median %, MAE / RMSE fF). Every other checkpoint the "
|
| 166 |
+
f"paper reports (k-curves, learning curves, leave-one-family-out folds, the solver-free series, ...) with its training and "
|
| 167 |
+
f"scoring scripts is in the model repository [{ck}](https://huggingface.co/{ck}). **Private preview** (paper in preparation).")
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def build_app(title: str, examples_dir=None):
|
| 171 |
+
import gradio as gr
|
| 172 |
+
|
| 173 |
+
reg = load_registry()
|
| 174 |
+
subs = list(reg.substrates()) + [CUSTOM]
|
| 175 |
+
ms = served(reg)
|
| 176 |
+
default = reg["paper_hub"]["default_arm"]
|
| 177 |
+
ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else []
|
| 178 |
+
choices = [(f"{PA.arms(reg)[k].get('route')}: {PA.arms(reg)[k]['paper_name']}", k) for k in ms]
|
| 179 |
+
with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo:
|
| 180 |
+
gr.Markdown(f"# {title}\n{header(reg)}")
|
| 181 |
+
with gr.Accordion("Planar circuits and the layer convention", open=False):
|
| 182 |
+
gr.Markdown(INTRO.replace("A thin-film method-of-moments solve\nprovides a physics prior and a graph network trained on 40,153 Q3D simulations corrects it.",
|
| 183 |
+
"See the arm descriptions below."))
|
| 184 |
+
gr.Markdown("## Arm (route of the paper)")
|
| 185 |
+
model = gr.Dropdown(choices, value=default, label="arm", interactive=True)
|
| 186 |
+
model_info = gr.Markdown(arm_md(reg, default))
|
| 187 |
+
model.change(lambda k: arm_md(reg, k), model, model_info, api_visibility="private")
|
| 188 |
+
gr.Markdown("## 1. Upload a GDS and assign layer roles")
|
| 189 |
+
with gr.Row():
|
| 190 |
+
with gr.Column(scale=1):
|
| 191 |
+
gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"])
|
| 192 |
+
if ex:
|
| 193 |
+
gr.Examples([[str(p)] for p in ex], inputs=[gds], label="or start from an example (the lattices are the paper's 1-, 5- and 9-qubit layouts)")
|
| 194 |
+
layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"],
|
| 195 |
+
label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True,
|
| 196 |
+
value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]])
|
| 197 |
+
layer_plot = gr.Plot(label="layers")
|
| 198 |
+
gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)")
|
| 199 |
+
with gr.Row():
|
| 200 |
+
with gr.Column(scale=1):
|
| 201 |
+
select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="")
|
| 202 |
+
with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False):
|
| 203 |
+
crop = gr.Checkbox(False, label="crop the ground plane around the conductors")
|
| 204 |
+
margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)")
|
| 205 |
+
margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider")
|
| 206 |
+
prep_btn = gr.Button("Prepare", variant="secondary")
|
| 207 |
+
warn_md = gr.Markdown()
|
| 208 |
+
summary = gr.JSON(label="prepared layout")
|
| 209 |
+
prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)")
|
| 210 |
+
prep_plot = gr.Plot(label="prepared layout")
|
| 211 |
+
gr.Markdown("## 3. Substrate\nThe paper's networks and priors are served for their training stack only (Si, eps_r 11.45, 350 µm); "
|
| 212 |
+
"the prior-generation baseline also takes other substrates (half-space factor (1+eps)/(1+11.45)).")
|
| 213 |
+
with gr.Row():
|
| 214 |
+
substrate = gr.Dropdown(subs, value=subs[0], label="substrate")
|
| 215 |
+
custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')")
|
| 216 |
+
thickness = gr.Number(value=350, label="substrate thickness (µm)")
|
| 217 |
+
self_rows = gr.Checkbox(True, label="prior-generation baseline: self-capacitance from row sums")
|
| 218 |
+
trust_cb = gr.Checkbox(bool(reg["final_defaults"].get("trust_rule", True)),
|
| 219 |
+
label="prior-generation model: apply its trust rule (default on)")
|
| 220 |
+
pred_btn = gr.Button("Predict", variant="primary")
|
| 221 |
+
gr.Markdown("## 4. Maxwell capacitance matrix (fF)")
|
| 222 |
+
with gr.Row():
|
| 223 |
+
with gr.Column(scale=2):
|
| 224 |
+
mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False)
|
| 225 |
+
with gr.Column():
|
| 226 |
+
csv_f = gr.File(label="CSV")
|
| 227 |
+
json_f = gr.File(label="JSON (matrix + metadata)")
|
| 228 |
+
report = gr.Markdown()
|
| 229 |
+
res_state = gr.State(None)
|
| 230 |
+
with gr.Row(visible=False):
|
| 231 |
+
a_g = gr.Textbox("1/0")
|
| 232 |
+
a_c = gr.Textbox("1/10")
|
| 233 |
+
a_m = gr.Number(0)
|
| 234 |
+
a_out = gr.JSON()
|
| 235 |
+
a_btn = gr.Button("api")
|
| 236 |
+
m_btn = gr.Button("models")
|
| 237 |
+
m_out = gr.JSON()
|
| 238 |
+
gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect")
|
| 239 |
+
prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds],
|
| 240 |
+
api_name="prepare")
|
| 241 |
+
pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_cb],
|
| 242 |
+
[mat, csv_f, json_f, report, res_state], api_name="predict")
|
| 243 |
+
a_btn.click(_safe(api_predict_gds, 1), [gds, a_g, a_c, a_m, substrate, custom_eps, thickness, model, self_rows, trust_cb], a_out,
|
| 244 |
+
api_name="predict_gds")
|
| 245 |
+
m_btn.click(models_fn, None, m_out, api_name="models")
|
| 246 |
+
gr.Markdown(f"<small>geomgnn {__version__}, tlsq_infer {TLSQ_VERSION}. Demo-server caps (not limits of the models): "
|
| 247 |
+
f"{LIMITS['max_file_mb']:g} MB per file, {LIMITS['max_polygons']:,} polygons, {reg['paper_hub']['max_conductors']} conductors "
|
| 248 |
+
f"+ ground ({NETWORK_CAP} for the prior-generation network). One request runs at a time; the nine-qubit lattice takes minutes "
|
| 249 |
+
"on the accurate arms.</small>")
|
| 250 |
+
return demo
|
geomgnn/fastmom.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Vectorised drop-in replacements for the H-matrix kernel of tlsq.embed.proxy (same mathematics, no per-row scipy
|
| 2 |
+
sparse slicing, no Python loop over image charges or ACA ranks). install() patches the loaded module in place."""
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from ._tlsq.embed import proxy as P
|
| 6 |
+
|
| 7 |
+
_orig = {}
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _images(eps_r):
|
| 11 |
+
P._image_correction(np.zeros(1), eps_r)
|
| 12 |
+
key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r))
|
| 13 |
+
w, D = P._IMG_CACHE[key][2:]
|
| 14 |
+
return np.asarray(w, float), np.asarray(D, float) ** 2
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _kernel_init(self, c, wh, eps_r, perm):
|
| 18 |
+
_orig["Kernel.__init__"](self, c, wh, eps_r, perm)
|
| 19 |
+
N = self.N.tocsr()
|
| 20 |
+
N.sort_indices()
|
| 21 |
+
self.indptr, self.indices, self.data = N.indptr, N.indices, N.data
|
| 22 |
+
self.w, self.D2 = _images(eps_r) if self.img else (None, None)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _corr(self, d):
|
| 26 |
+
return np.sqrt((d * d)[..., None] + self.D2) ** -1 @ self.w
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _row(self, i, b0, b1):
|
| 30 |
+
d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2)
|
| 31 |
+
r = 1.0 / d
|
| 32 |
+
if self.img:
|
| 33 |
+
r += _corr(self, d)
|
| 34 |
+
s, e = self.indptr[i], self.indptr[i + 1]
|
| 35 |
+
if e > s:
|
| 36 |
+
ix = self.indices[s:e]
|
| 37 |
+
lo, hi = np.searchsorted(ix, (b0, b1))
|
| 38 |
+
if hi > lo:
|
| 39 |
+
r[ix[lo:hi] - b0] += self.data[s + lo:s + hi]
|
| 40 |
+
return r / self.k
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _block(self, a0, a1, b0, b1):
|
| 44 |
+
d = np.sqrt((self.x[a0:a1, None] - self.x[None, b0:b1]) ** 2 + (self.y[a0:a1, None] - self.y[None, b0:b1]) ** 2)
|
| 45 |
+
corr = _corr(self, d) if self.img else None
|
| 46 |
+
if a0 == b0:
|
| 47 |
+
d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0
|
| 48 |
+
A = 1.0 / d
|
| 49 |
+
if corr is not None:
|
| 50 |
+
A += corr
|
| 51 |
+
s, e = self.indptr[a0], self.indptr[a1]
|
| 52 |
+
if e > s:
|
| 53 |
+
ix = self.indices[s:e]
|
| 54 |
+
rr = np.repeat(np.arange(a1 - a0), np.diff(self.indptr[a0:a1 + 1]))
|
| 55 |
+
m = (ix >= b0) & (ix < b1)
|
| 56 |
+
np.add.at(A, (rr[m], ix[m] - b0), self.data[s:e][m])
|
| 57 |
+
return A / self.k
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _aca(K, a0, a1, b0, b1, eps, max_rank):
|
| 61 |
+
m, n = a1 - a0, b1 - b0
|
| 62 |
+
R = min(m, n, max_rank)
|
| 63 |
+
U, V = np.empty((m, R)), np.empty((n, R))
|
| 64 |
+
used_r, used_c = np.zeros(m, bool), np.zeros(n, bool)
|
| 65 |
+
i, norm2, k = 0, 0.0, 0
|
| 66 |
+
for _ in range(R):
|
| 67 |
+
r = K.row(a0 + i, b0, b1)
|
| 68 |
+
if k:
|
| 69 |
+
r -= V[:, :k] @ U[i, :k]
|
| 70 |
+
rm = np.where(used_c, 0.0, np.abs(r))
|
| 71 |
+
j = int(np.argmax(rm))
|
| 72 |
+
if rm[j] == 0.0:
|
| 73 |
+
break
|
| 74 |
+
v = r / r[j]
|
| 75 |
+
u = K.row(b0 + j, a0, a1)
|
| 76 |
+
if k:
|
| 77 |
+
u -= U[:, :k] @ V[j, :k]
|
| 78 |
+
used_r[i], used_c[j] = True, True
|
| 79 |
+
nu, nv = float(u @ u), float(v @ v)
|
| 80 |
+
norm2 += nu * nv + (2 * float((U[:, :k].T @ u) @ (V[:, :k].T @ v)) if k else 0.0)
|
| 81 |
+
U[:, k], V[:, k] = u, v
|
| 82 |
+
k += 1
|
| 83 |
+
if nu * nv <= eps**2 * norm2:
|
| 84 |
+
break
|
| 85 |
+
um = np.where(used_r, 0.0, np.abs(u))
|
| 86 |
+
i = int(np.argmax(um))
|
| 87 |
+
if um[i] == 0.0:
|
| 88 |
+
break
|
| 89 |
+
else:
|
| 90 |
+
return None
|
| 91 |
+
return U[:, :k].copy(), V[:, :k].copy()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _image_correction(d, eps_r, exact=False):
|
| 95 |
+
if exact or P.SUBSTRATE["model"] == "halfspace":
|
| 96 |
+
return _orig["image"](d, eps_r, exact)
|
| 97 |
+
_orig["image"](np.zeros(1), eps_r)
|
| 98 |
+
key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r))
|
| 99 |
+
du, T = P._IMG_CACHE[key][:2]
|
| 100 |
+
u = np.asarray(d, float) / du
|
| 101 |
+
j = np.minimum(u.astype(np.int64), len(T) - 2)
|
| 102 |
+
f = u - j
|
| 103 |
+
t0 = T[j]
|
| 104 |
+
return t0 + f * (T[j + 1] - t0)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
REFINE = dict(steps=6, tol=1e-13)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _chol_solve(A, B):
|
| 111 |
+
from scipy.linalg import cho_factor, cho_solve
|
| 112 |
+
from scipy.linalg.blas import dsymm
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
cf = cho_factor(A.astype(np.float32, order="F"), lower=True, overwrite_a=True, check_finite=False)
|
| 116 |
+
except np.linalg.LinAlgError:
|
| 117 |
+
return _orig["chol_solve"](A, B)
|
| 118 |
+
B = np.asfortranarray(B, dtype=float)
|
| 119 |
+
X = cho_solve(cf, B.astype(np.float32), check_finite=False).astype(float)
|
| 120 |
+
bn = np.linalg.norm(B, axis=0)
|
| 121 |
+
for _ in range(REFINE["steps"]):
|
| 122 |
+
R = B - dsymm(1.0, A, X, side=0, lower=1)
|
| 123 |
+
if (np.linalg.norm(R, axis=0) <= REFINE["tol"] * bn).all():
|
| 124 |
+
return X
|
| 125 |
+
X += cho_solve(cf, R.astype(np.float32), check_finite=False)
|
| 126 |
+
return _orig["chol_solve"](A, B)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def install(dense_max=None, mixed=False):
|
| 130 |
+
if not _orig:
|
| 131 |
+
_orig["Kernel.__init__"] = P._Kernel.__init__
|
| 132 |
+
_orig.update(row=P._Kernel.row, block=P._Kernel.block, aca=P._aca, dense_max=P.DENSE_MAX, image=P._image_correction,
|
| 133 |
+
chol_solve=P._chol_solve)
|
| 134 |
+
P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = _kernel_init, _row, _block, _aca
|
| 135 |
+
P._image_correction = _image_correction
|
| 136 |
+
P._chol_solve = _chol_solve if mixed else _orig["chol_solve"]
|
| 137 |
+
if dense_max is not None:
|
| 138 |
+
P.DENSE_MAX = dense_max
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def uninstall():
|
| 142 |
+
if _orig:
|
| 143 |
+
P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = (_orig["Kernel.__init__"], _orig["row"], _orig["block"],
|
| 144 |
+
_orig["aca"])
|
| 145 |
+
P._image_correction, P._chol_solve = _orig["image"], _orig["chol_solve"]
|
| 146 |
+
P.DENSE_MAX = _orig["dense_max"]
|
| 147 |
+
_orig.clear()
|
geomgnn/fastprior_pkg.py
ADDED
|
@@ -0,0 +1,1291 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""fastprior_pkg: switchable speed components for the FROZEN physical prior of the P2d GNN (micro_mom_loc at ppg 1.5
|
| 2 |
+
and 3, alpha 0.49685219816506904), installed by monkeypatching the loaded frozen vendor modules (never edited).
|
| 3 |
+
|
| 4 |
+
import fastprior_pkg as fp
|
| 5 |
+
fp.install(mode="exact") # fv + parallel + hmat_vec (+ gpu if CUDA) (+ mixed if gated)
|
| 6 |
+
fp.install(mode="exact_compiled") # exact + numba H-matrix operator (closer to the dense solve; VAL-tested, A5)
|
| 7 |
+
fp.install(mode="certified") # exact + compiled + Rayleigh-Ritz solves with an error estimate (fp.certificates())
|
| 8 |
+
fp.install(components=["fv", "compiled"]) # explicit set
|
| 9 |
+
fp.uninstall()
|
| 10 |
+
|
| 11 |
+
Components (protocol and gates: agents/fastprior_pkg/PROTOCOL.md; sources cited per section, code reused verbatim
|
| 12 |
+
where possible, only module bindings changed):
|
| 13 |
+
fv value-preserving rejection-tail fix of the windowed solve (rejtail; bit-identical)
|
| 14 |
+
parallel forked window workers (the vendor's micro_mom(workers=k)); threads per worker limited (bit-identical)
|
| 15 |
+
hmat_vec the frozen H-matrix PCG (> 12,000 panels) with a vectorised kernel, frozen tolerances (<= 1e-6)
|
| 16 |
+
compiled numba H-matrix operator for the H-matrix PCG path: closer to the exact dense solve than the frozen
|
| 17 |
+
H-matrix, but up to 1.4e-5 from it (fails strict parity; opt-in, used by certified)
|
| 18 |
+
gpu dense assembly + Cholesky on CUDA (only when torch.cuda is available)
|
| 19 |
+
mixed float32 Cholesky + float64 iterative refinement (only if gated, see MIXED_GATED)
|
| 20 |
+
ritz Rayleigh-Ritz window / local solves (template start, tau 1e-3, dense <= 3,000 panels), certified
|
| 21 |
+
Parallel windows fork (the vendor's pool); on macOS they are always off (Accelerate is not fork-safe) and the prior
|
| 22 |
+
runs serially. Environment: TLSQ_PRIOR_CPUS (CPUs; default cgroup / affinity aware), TLSQ_PRIOR_WORKERS (window workers; default =
|
| 23 |
+
CPUs), TLSQ_PRIOR_MODE (default mode of install() without arguments).
|
| 24 |
+
Value hook: install(..., value_patch=fn) with fn(F, P, FL) -> undo callable (e.g. a prior-v2 patch once it passes)."""
|
| 25 |
+
from __future__ import annotations
|
| 26 |
+
|
| 27 |
+
import contextlib
|
| 28 |
+
import inspect
|
| 29 |
+
import json
|
| 30 |
+
import os
|
| 31 |
+
import sys
|
| 32 |
+
import tempfile
|
| 33 |
+
import time
|
| 34 |
+
|
| 35 |
+
import numpy as np
|
| 36 |
+
from scipy.linalg import cho_factor, cho_solve, eigh
|
| 37 |
+
|
| 38 |
+
FROZEN_DENSE_MAX = 12000 # the frozen proxy.DENSE_MAX: decides which attempts the frozen code could not solve (fv)
|
| 39 |
+
RITZ_CAL = 1.825367354114339 # varritz S0 calibration of the block-Jacobi estimate (s0_choice.json)
|
| 40 |
+
MIXED_GATED = True # PROTOCOL.md G3 passed on Discovery x86 (A5): prior <= 4.4e-10, final matrix <= 4.5e-7
|
| 41 |
+
COMPONENTS = ("fv", "parallel", "hmat_vec", "compiled", "gpu", "mixed", "ritz")
|
| 42 |
+
CFG = dict(device="cuda", gpu_min=2000, gpu_dense_max=60000, rows=4096, ritz_tau=1e-3, ritz_maxit=12, ritz_dense_n=3000,
|
| 43 |
+
ritz_start="tmpl", refine_steps=6, refine_tol=1e-13)
|
| 44 |
+
STATE = dict(mode=None, components=(), cpus=1, workers=1, gpu=False, value_patch=None, target=None, parallel_off=None)
|
| 45 |
+
F = P = FL = None # bound vendor modules: fastprior, proxy, fastprior_loc
|
| 46 |
+
_ORIG: dict = {}
|
| 47 |
+
_UNDO: list = []
|
| 48 |
+
_PARENT = [0]
|
| 49 |
+
_CHILD = [False]
|
| 50 |
+
_TP: list = []
|
| 51 |
+
STATS = dict(compiled_calls=0) # per-process counters
|
| 52 |
+
LOG: list = [] # Ritz / exact solve records of the running prior call (certified mode)
|
| 53 |
+
CERTS: list = [] # one certificate per micro_mom_loc call (certified mode)
|
| 54 |
+
_CERT_FILE = [None]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ------------------------------------------------------------------------------------------------ binding / CPUs
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def bind(target="auto"):
|
| 61 |
+
"""Bind the frozen vendor modules: 'stage' (tlsq.embed of the geomfast stage), 'geomgnn' (the GNN Space bundle,
|
| 62 |
+
geomgnn._tlsq.embed), or a tuple (fastprior, proxy, fastprior_loc). 'auto': whichever is loaded, else stage."""
|
| 63 |
+
global F, P, FL
|
| 64 |
+
if isinstance(target, tuple):
|
| 65 |
+
F, P, FL = target
|
| 66 |
+
STATE["target"] = "modules"
|
| 67 |
+
return F, P, FL
|
| 68 |
+
if target == "auto":
|
| 69 |
+
target = "geomgnn" if "geomgnn._tlsq.embed.fastprior" in sys.modules else "stage"
|
| 70 |
+
if target == "stage" and "tlsq.embed.fastprior" not in sys.modules:
|
| 71 |
+
try:
|
| 72 |
+
import tlsq.embed.fastprior # noqa: F401
|
| 73 |
+
except ImportError:
|
| 74 |
+
target = "geomgnn"
|
| 75 |
+
if target == "geomgnn":
|
| 76 |
+
from geomgnn._tlsq.embed import fastprior as F_, fastprior_loc as FL_, proxy as P_
|
| 77 |
+
else:
|
| 78 |
+
from tlsq.embed import fastprior as F_, fastprior_loc as FL_, proxy as P_
|
| 79 |
+
F, P, FL = F_, P_, FL_
|
| 80 |
+
STATE["target"] = target
|
| 81 |
+
return F, P, FL
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def _cgroup_cpus():
|
| 85 |
+
"""CPU quota of this process's cgroup (v2 cpu.max along the cgroup path, or v1 cfs quota / period); None if none."""
|
| 86 |
+
best = None
|
| 87 |
+
try:
|
| 88 |
+
lines = open("/proc/self/cgroup").read().splitlines()
|
| 89 |
+
except OSError:
|
| 90 |
+
lines = []
|
| 91 |
+
paths = []
|
| 92 |
+
for ln in lines:
|
| 93 |
+
parts = ln.split(":", 2)
|
| 94 |
+
if len(parts) == 3 and parts[0] == "0" and parts[1] == "":
|
| 95 |
+
rel = parts[2].strip("/")
|
| 96 |
+
segs = rel.split("/") if rel else []
|
| 97 |
+
for k in range(len(segs), -1, -1):
|
| 98 |
+
paths.append(os.path.join("/sys/fs/cgroup", *segs[:k], "cpu.max"))
|
| 99 |
+
paths.append("/sys/fs/cgroup/cpu.max")
|
| 100 |
+
for p in paths:
|
| 101 |
+
try:
|
| 102 |
+
q, per = open(p).read().split()[:2]
|
| 103 |
+
except (OSError, ValueError):
|
| 104 |
+
continue
|
| 105 |
+
if q != "max" and float(per) > 0:
|
| 106 |
+
v = float(q) / float(per)
|
| 107 |
+
best = v if best is None else min(best, v)
|
| 108 |
+
for d in ("/sys/fs/cgroup/cpu", "/sys/fs/cgroup/cpu,cpuacct"):
|
| 109 |
+
try:
|
| 110 |
+
q = int(open(f"{d}/cpu.cfs_quota_us").read())
|
| 111 |
+
per = int(open(f"{d}/cpu.cfs_period_us").read())
|
| 112 |
+
except (OSError, ValueError):
|
| 113 |
+
continue
|
| 114 |
+
if q > 0 and per > 0:
|
| 115 |
+
best = q / per if best is None else min(best, q / per)
|
| 116 |
+
return best
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def detect_cpus() -> int:
|
| 120 |
+
"""CPUs this process may use: TLSQ_PRIOR_CPUS, else min(cgroup quota, affinity, os.cpu_count())."""
|
| 121 |
+
env = os.environ.get("TLSQ_PRIOR_CPUS")
|
| 122 |
+
if env:
|
| 123 |
+
return max(1, int(env))
|
| 124 |
+
cands = [os.cpu_count() or 1]
|
| 125 |
+
q = _cgroup_cpus()
|
| 126 |
+
if q:
|
| 127 |
+
cands.append(max(1, int(q)))
|
| 128 |
+
if hasattr(os, "sched_getaffinity"):
|
| 129 |
+
cands.append(len(os.sched_getaffinity(0)))
|
| 130 |
+
return max(1, min(cands))
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _set_threads(n: int) -> None:
|
| 134 |
+
"""Limit BLAS / OpenMP (threadpoolctl), numba and the vendor's thread pool to n threads in this process."""
|
| 135 |
+
try:
|
| 136 |
+
from threadpoolctl import threadpool_limits
|
| 137 |
+
_TP[:] = [threadpool_limits(n)]
|
| 138 |
+
except Exception: # noqa: BLE001 (no threadpoolctl / Accelerate: env vars at process start decide)
|
| 139 |
+
pass
|
| 140 |
+
if P is not None:
|
| 141 |
+
P.NUM_THREADS = n
|
| 142 |
+
try:
|
| 143 |
+
import numba
|
| 144 |
+
numba.set_num_threads(max(1, min(n, numba.config.NUMBA_NUM_THREADS)))
|
| 145 |
+
except Exception: # noqa: BLE001
|
| 146 |
+
pass
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# ------------------------------------------------------------------------------------------------ fv
|
| 150 |
+
# deploy/geomgnn/src/geomgnn/rejtail_fv.py (= scripts/rejtail/rt.py policy fv), keep_last_solved=True (frozen stage),
|
| 151 |
+
# with the "doomed" test on the frozen DENSE_MAX (a GPU-raised DENSE_MAX must not change which attempts solve).
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def _fv_solve(c, wh, o, metals, cols, eps_r):
|
| 155 |
+
t = time.perf_counter()
|
| 156 |
+
t_asm = 0.0
|
| 157 |
+
if len(c) <= P.DENSE_MAX:
|
| 158 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 159 |
+
t_asm = time.perf_counter() - t
|
| 160 |
+
Bm = (o[:, None] == cols[None, :]).astype(float)
|
| 161 |
+
Q = F._sym_solve(A, Bm)
|
| 162 |
+
else:
|
| 163 |
+
Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols]
|
| 164 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1), t_asm
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def fv_window_solve(metals, n_core, h, budget, eps_r, all_rhs, levels=()):
|
| 168 |
+
if all_rhs:
|
| 169 |
+
return _ORIG["window_solve"](metals, n_core, h, budget, eps_r, all_rhs, levels)
|
| 170 |
+
cols = np.arange(1, 1 + n_core)
|
| 171 |
+
t_mesh = t_asm = t_sol = 0.0
|
| 172 |
+
hh, cap, refined, majority = h, budget, 0, False
|
| 173 |
+
hist = []
|
| 174 |
+
while True:
|
| 175 |
+
t = time.perf_counter()
|
| 176 |
+
if not refined:
|
| 177 |
+
c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels)
|
| 178 |
+
elif levels:
|
| 179 |
+
c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority)
|
| 180 |
+
else:
|
| 181 |
+
c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority)
|
| 182 |
+
t_mesh += time.perf_counter() - t
|
| 183 |
+
if not np.all(np.bincount(o, minlength=len(metals)) > 0):
|
| 184 |
+
hist.append(("doomed",) if len(c) > FROZEN_DENSE_MAX else ("mesh", c, wh, o))
|
| 185 |
+
ok = False
|
| 186 |
+
else:
|
| 187 |
+
t = time.perf_counter()
|
| 188 |
+
try:
|
| 189 |
+
R, ta = _fv_solve(c, wh, o, metals, cols, eps_r)
|
| 190 |
+
t_asm += ta
|
| 191 |
+
hist.append(("R", R))
|
| 192 |
+
ok = True
|
| 193 |
+
except np.linalg.LinAlgError:
|
| 194 |
+
hist.append(("err",))
|
| 195 |
+
ok = False
|
| 196 |
+
t_sol += time.perf_counter() - t
|
| 197 |
+
if ok or majority:
|
| 198 |
+
break
|
| 199 |
+
if refined < F.LITE_REFINE:
|
| 200 |
+
hh, cap, refined = hh / 2, np.inf, refined + 1
|
| 201 |
+
else:
|
| 202 |
+
majority = True
|
| 203 |
+
if ok:
|
| 204 |
+
R = hist[-1][1]
|
| 205 |
+
else:
|
| 206 |
+
R = None
|
| 207 |
+
t = time.perf_counter()
|
| 208 |
+
for e in reversed(hist):
|
| 209 |
+
if e[0] == "R":
|
| 210 |
+
R = e[1]
|
| 211 |
+
break
|
| 212 |
+
if e[0] == "mesh":
|
| 213 |
+
try:
|
| 214 |
+
R = _fv_solve(e[1], e[2], e[3], metals, cols, eps_r)[0]
|
| 215 |
+
break
|
| 216 |
+
except np.linalg.LinAlgError:
|
| 217 |
+
continue
|
| 218 |
+
t_sol += time.perf_counter() - t
|
| 219 |
+
assert R is not None, "no attempt solved (the frozen code raises here too)"
|
| 220 |
+
return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh,
|
| 221 |
+
"assemble_s": t_asm, "solve_s": t_sol}
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
# ------------------------------------------------------------------------------------------------ parallel windows
|
| 225 |
+
# The vendor's micro_mom(workers=k) (ProcessPoolExecutor, fork); windows inherit the layout through fastprior._LITE.
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def _micro_mom(*args, **kw):
|
| 229 |
+
ba = _ORIG["micro_mom_sig"].bind(*args, **kw)
|
| 230 |
+
if sys.platform == "darwin": # never fork on macOS, whatever the caller asked for
|
| 231 |
+
ba.arguments["workers"] = 1
|
| 232 |
+
if STATE["workers"] > 1 and ba.arguments.get("workers", 1) <= 1:
|
| 233 |
+
ba.arguments["workers"] = STATE["workers"]
|
| 234 |
+
_PARENT[0] = os.getpid()
|
| 235 |
+
return _ORIG["micro_mom"](*ba.args, **ba.kwargs)
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def _child_setup():
|
| 239 |
+
"""First window job in a forked worker: threads per worker = CPUs // workers; CUDA is never used after fork."""
|
| 240 |
+
if _CHILD[0] or os.getpid() == _PARENT[0]:
|
| 241 |
+
return
|
| 242 |
+
_CHILD[0] = True
|
| 243 |
+
nw = max(1, min(STATE["workers"], len(F._LITE.get("cores", [0]))))
|
| 244 |
+
_set_threads(max(1, STATE["cpus"] // nw))
|
| 245 |
+
if STATE["gpu"]:
|
| 246 |
+
STATE["gpu"] = False
|
| 247 |
+
P.DENSE_MAX = FROZEN_DENSE_MAX
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def _lite_job(k):
|
| 251 |
+
_child_setup()
|
| 252 |
+
if _CHILD[0] and _CERT_FILE[0]:
|
| 253 |
+
i0 = len(LOG)
|
| 254 |
+
out = _ORIG["lite_job"](k)
|
| 255 |
+
recs = LOG[i0:]
|
| 256 |
+
for r in recs:
|
| 257 |
+
r["window"] = int(k)
|
| 258 |
+
with open(_CERT_FILE[0], "a") as f:
|
| 259 |
+
f.write("".join(json.dumps(_jsonable(r)) + "\n" for r in recs))
|
| 260 |
+
return out
|
| 261 |
+
i0 = len(LOG)
|
| 262 |
+
out = _ORIG["lite_job"](k)
|
| 263 |
+
for r in LOG[i0:]:
|
| 264 |
+
r["window"] = int(k)
|
| 265 |
+
return out
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def _jsonable(r):
|
| 269 |
+
return {k: (v.tolist() if isinstance(v, np.ndarray) else (float(v) if isinstance(v, np.floating) else v)) for k, v in r.items()}
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
# ------------------------------------------------------------------------------------------------ compiled operator
|
| 273 |
+
# scripts/varritz/hmat.py and scripts/varritz/fastop.py (numba H-matrix of the far kernel, exact near field, FFT image
|
| 274 |
+
# term, block-Jacobi leaf factors on the frozen cluster tree); bodies verbatim, `P` bound late.
|
| 275 |
+
|
| 276 |
+
os.environ.setdefault("NUMBA_THREADING_LAYER", "workqueue") # fork-safe; GNU OpenMP aborts in forked window workers
|
| 277 |
+
try:
|
| 278 |
+
import numba as nb
|
| 279 |
+
nb.config.THREADING_LAYER = os.environ["NUMBA_THREADING_LAYER"]
|
| 280 |
+
except ImportError: # compiled / ritz unavailable without numba
|
| 281 |
+
nb = None
|
| 282 |
+
|
| 283 |
+
if nb is not None:
|
| 284 |
+
@nb.njit(fastmath=True, cache=True)
|
| 285 |
+
def _krow(x, y, i, b0, b1, out):
|
| 286 |
+
for j in range(b0, b1):
|
| 287 |
+
dx, dy = x[i] - x[j], y[i] - y[j]
|
| 288 |
+
r2 = dx * dx + dy * dy
|
| 289 |
+
out[j - b0] = 1.0 / np.sqrt(r2) if r2 > 0 else 0.0
|
| 290 |
+
|
| 291 |
+
@nb.njit(fastmath=True, cache=True)
|
| 292 |
+
def _aca(x, y, a0, a1, b0, b1, eps, kmax, U, V):
|
| 293 |
+
m, n = a1 - a0, b1 - b0
|
| 294 |
+
used_r = np.zeros(m, np.bool_)
|
| 295 |
+
used_c = np.zeros(n, np.bool_)
|
| 296 |
+
r = np.empty(n)
|
| 297 |
+
u = np.empty(m)
|
| 298 |
+
norm2 = 0.0
|
| 299 |
+
i = 0
|
| 300 |
+
k = 0
|
| 301 |
+
while k < min(m, n, kmax):
|
| 302 |
+
_krow(x, y, a0 + i, b0, b1, r)
|
| 303 |
+
for q in range(k):
|
| 304 |
+
ui = U[i, q]
|
| 305 |
+
for j in range(n):
|
| 306 |
+
r[j] -= ui * V[j, q]
|
| 307 |
+
used_r[i] = True
|
| 308 |
+
jbest, vbest = -1, 0.0
|
| 309 |
+
for j in range(n):
|
| 310 |
+
if not used_c[j] and abs(r[j]) > vbest:
|
| 311 |
+
vbest, jbest = abs(r[j]), j
|
| 312 |
+
if jbest < 0 or vbest == 0.0:
|
| 313 |
+
return k
|
| 314 |
+
piv = r[jbest]
|
| 315 |
+
for j in range(n):
|
| 316 |
+
V[j, k] = r[j] / piv
|
| 317 |
+
_krow(x, y, b0 + jbest, a0, a1, u)
|
| 318 |
+
for q in range(k):
|
| 319 |
+
vj = V[jbest, q]
|
| 320 |
+
for ii in range(m):
|
| 321 |
+
u[ii] -= vj * U[ii, q]
|
| 322 |
+
for ii in range(m):
|
| 323 |
+
U[ii, k] = u[ii]
|
| 324 |
+
used_c[jbest] = True
|
| 325 |
+
nu, nv = 0.0, 0.0
|
| 326 |
+
for ii in range(m):
|
| 327 |
+
nu += u[ii] * u[ii]
|
| 328 |
+
for j in range(n):
|
| 329 |
+
nv += V[j, k] * V[j, k]
|
| 330 |
+
cross = 0.0
|
| 331 |
+
for q in range(k):
|
| 332 |
+
su, sv = 0.0, 0.0
|
| 333 |
+
for ii in range(m):
|
| 334 |
+
su += u[ii] * U[ii, q]
|
| 335 |
+
for j in range(n):
|
| 336 |
+
sv += V[j, k] * V[j, q]
|
| 337 |
+
cross += su * sv
|
| 338 |
+
norm2 += nu * nv + 2.0 * cross
|
| 339 |
+
k += 1
|
| 340 |
+
if nu * nv <= eps * eps * norm2:
|
| 341 |
+
return k
|
| 342 |
+
ibest, ub = -1, 0.0
|
| 343 |
+
for ii in range(m):
|
| 344 |
+
if not used_r[ii] and abs(u[ii]) > ub:
|
| 345 |
+
ub, ibest = abs(u[ii]), ii
|
| 346 |
+
if ibest < 0:
|
| 347 |
+
return k
|
| 348 |
+
i = ibest
|
| 349 |
+
return -1 if k >= kmax and k < min(m, n) else k
|
| 350 |
+
|
| 351 |
+
@nb.njit(parallel=True, cache=True)
|
| 352 |
+
def _build(x, y, lows, eps, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata):
|
| 353 |
+
nl = lows.shape[0]
|
| 354 |
+
for b in nb.prange(nl):
|
| 355 |
+
a0, a1, b0, b1 = lows[b, 0], lows[b, 1], lows[b, 2], lows[b, 3]
|
| 356 |
+
m, n = a1 - a0, b1 - b0
|
| 357 |
+
U = Ubuf[uoff[b]:uoff[b] + m * kmax].reshape(m, kmax)
|
| 358 |
+
V = Vbuf[voff[b]:voff[b] + n * kmax].reshape(n, kmax)
|
| 359 |
+
ranks[b] = _aca(x, y, a0, a1, b0, b1, eps, kmax, U, V)
|
| 360 |
+
nd = dense.shape[0]
|
| 361 |
+
for b in nb.prange(nd):
|
| 362 |
+
a0, a1, b0, b1 = dense[b, 0], dense[b, 1], dense[b, 2], dense[b, 3]
|
| 363 |
+
n = b1 - b0
|
| 364 |
+
row = np.empty(n)
|
| 365 |
+
for i in range(a0, a1):
|
| 366 |
+
_krow(x, y, i, b0, b1, row)
|
| 367 |
+
ddata[doff[b] + (i - a0) * n:doff[b] + (i - a0 + 1) * n] = row
|
| 368 |
+
|
| 369 |
+
@nb.njit(parallel=True, cache=True)
|
| 370 |
+
def _apply(X, lows, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata, nthr, Y):
|
| 371 |
+
n, mc = X.shape
|
| 372 |
+
buf = np.zeros((nthr, n, mc))
|
| 373 |
+
nl = lows.shape[0]
|
| 374 |
+
nd = dense.shape[0]
|
| 375 |
+
tot = nl + nd
|
| 376 |
+
for t in nb.prange(nthr):
|
| 377 |
+
B = buf[t]
|
| 378 |
+
for b in range(t, tot, nthr):
|
| 379 |
+
if b < nl:
|
| 380 |
+
a0, a1, b0, b1 = lows[b, 0], lows[b, 1], lows[b, 2], lows[b, 3]
|
| 381 |
+
m, nn = a1 - a0, b1 - b0
|
| 382 |
+
k = ranks[b]
|
| 383 |
+
U = Ubuf[uoff[b]:uoff[b] + m * kmax].reshape(m, kmax)[:, :k]
|
| 384 |
+
V = Vbuf[voff[b]:voff[b] + nn * kmax].reshape(nn, kmax)[:, :k]
|
| 385 |
+
B[a0:a1] += U @ (V.T @ X[b0:b1])
|
| 386 |
+
B[b0:b1] += V @ (U.T @ X[a0:a1])
|
| 387 |
+
else:
|
| 388 |
+
q = b - nl
|
| 389 |
+
a0, a1, b0, b1 = dense[q, 0], dense[q, 1], dense[q, 2], dense[q, 3]
|
| 390 |
+
D = ddata[doff[q]:doff[q] + (a1 - a0) * (b1 - b0)].reshape(a1 - a0, b1 - b0)
|
| 391 |
+
B[a0:a1] += D @ X[b0:b1]
|
| 392 |
+
if a0 != b0:
|
| 393 |
+
B[b0:b1] += D.T @ X[a0:a1]
|
| 394 |
+
for i in nb.prange(n):
|
| 395 |
+
for c in range(mc):
|
| 396 |
+
s = 0.0
|
| 397 |
+
for t in range(nthr):
|
| 398 |
+
s += buf[t, i, c]
|
| 399 |
+
Y[i, c] = s
|
| 400 |
+
|
| 401 |
+
@nb.njit(parallel=True, fastmath=True, cache=True)
|
| 402 |
+
def _far(x, y, Xt, Y, T=64):
|
| 403 |
+
m, n = Xt.shape
|
| 404 |
+
nt = (n + T - 1) // T
|
| 405 |
+
for it in nb.prange(nt):
|
| 406 |
+
r = np.empty(n)
|
| 407 |
+
for i in range(it * T, min(n, it * T + T)):
|
| 408 |
+
xi, yi = x[i], y[i]
|
| 409 |
+
for j in range(n):
|
| 410 |
+
dx, dy = xi - x[j], yi - y[j]
|
| 411 |
+
r[j] = dx * dx + dy * dy
|
| 412 |
+
r[i] = 1.0
|
| 413 |
+
for j in range(n):
|
| 414 |
+
r[j] = 1.0 / np.sqrt(r[j])
|
| 415 |
+
r[i] = 0.0
|
| 416 |
+
for k in range(m):
|
| 417 |
+
s = 0.0
|
| 418 |
+
for j in range(n):
|
| 419 |
+
s += r[j] * Xt[k, j]
|
| 420 |
+
Y[i, k] = s
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def _numba():
|
| 424 |
+
if nb is None:
|
| 425 |
+
raise ImportError("numba is required for the compiled / ritz components")
|
| 426 |
+
return dict(nb=nb, build=_build, apply=_apply, far=_far)
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
class HFar:
|
| 430 |
+
"""Far kernel sum_{j != i} x_j / d_ij as an H-matrix (frozen block plan: leaf 128, eta 2). (varritz/hmat.py)"""
|
| 431 |
+
|
| 432 |
+
def __init__(self, c, leaf=128, eta=2.0, eps=1e-7, kmax=48, nthr=None):
|
| 433 |
+
K = _numba()
|
| 434 |
+
self.perm, nodes = P._cluster_tree(c, leaf)
|
| 435 |
+
self.x, self.y = np.ascontiguousarray(c[self.perm, 0]), np.ascontiguousarray(c[self.perm, 1])
|
| 436 |
+
plan_l, plan_d = [], []
|
| 437 |
+
|
| 438 |
+
def leafq(k):
|
| 439 |
+
return nodes[k][4] < 0
|
| 440 |
+
|
| 441 |
+
def rec(s, t):
|
| 442 |
+
a0, a1, lo_s, hi_s, sl, sr = nodes[s]
|
| 443 |
+
b0, b1, lo_t, hi_t, tl, tr = nodes[t]
|
| 444 |
+
if s == t:
|
| 445 |
+
if leafq(s):
|
| 446 |
+
plan_d.append((a0, a1, a0, a1))
|
| 447 |
+
else:
|
| 448 |
+
rec(sl, sl), rec(sr, sr), rec(sl, sr)
|
| 449 |
+
return
|
| 450 |
+
gap = np.maximum(0.0, np.maximum(lo_s - hi_t, lo_t - hi_s))
|
| 451 |
+
diam = min(np.sqrt(((hi_s - lo_s) ** 2).sum()), np.sqrt(((hi_t - lo_t) ** 2).sum()))
|
| 452 |
+
if diam <= eta * np.sqrt((gap**2).sum()):
|
| 453 |
+
plan_l.append((a0, a1, b0, b1))
|
| 454 |
+
elif leafq(s) and leafq(t):
|
| 455 |
+
plan_d.append((a0, a1, b0, b1))
|
| 456 |
+
elif leafq(s):
|
| 457 |
+
rec(s, tl), rec(s, tr)
|
| 458 |
+
elif leafq(t):
|
| 459 |
+
rec(sl, t), rec(sr, t)
|
| 460 |
+
else:
|
| 461 |
+
for u in (sl, sr):
|
| 462 |
+
for v in (tl, tr):
|
| 463 |
+
rec(u, v)
|
| 464 |
+
|
| 465 |
+
rec(0, 0)
|
| 466 |
+
lows = np.array(plan_l, np.int64).reshape(-1, 4)
|
| 467 |
+
dense = np.array(plan_d, np.int64).reshape(-1, 4)
|
| 468 |
+
m = lows[:, 1] - lows[:, 0]
|
| 469 |
+
nn = lows[:, 3] - lows[:, 2]
|
| 470 |
+
uoff = np.r_[0, np.cumsum(m * kmax)][:-1]
|
| 471 |
+
voff = np.r_[0, np.cumsum(nn * kmax)][:-1]
|
| 472 |
+
Ubuf, Vbuf = np.zeros(int((m * kmax).sum())), np.zeros(int((nn * kmax).sum()))
|
| 473 |
+
dsz = (dense[:, 1] - dense[:, 0]) * (dense[:, 3] - dense[:, 2])
|
| 474 |
+
doff = np.r_[0, np.cumsum(dsz)][:-1]
|
| 475 |
+
ddata = np.zeros(int(dsz.sum()))
|
| 476 |
+
ranks = np.zeros(len(lows), np.int64)
|
| 477 |
+
K["build"](self.x, self.y, lows, eps, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata)
|
| 478 |
+
bad = ranks < 0
|
| 479 |
+
if bad.any(): # rank above kmax: these blocks become dense
|
| 480 |
+
extra = lows[bad]
|
| 481 |
+
lows = lows[~bad]
|
| 482 |
+
keep_u, keep_v = uoff[~bad], voff[~bad]
|
| 483 |
+
ranks = ranks[~bad]
|
| 484 |
+
uoff, voff = keep_u, keep_v
|
| 485 |
+
esz = (extra[:, 1] - extra[:, 0]) * (extra[:, 3] - extra[:, 2])
|
| 486 |
+
eoff = np.r_[0, np.cumsum(esz)][:-1]
|
| 487 |
+
edata = np.zeros(int(esz.sum()))
|
| 488 |
+
K["build"](self.x, self.y, np.zeros((0, 4), np.int64), eps, kmax, Ubuf, Vbuf, uoff[:0], voff[:0], ranks[:0], extra, eoff, edata)
|
| 489 |
+
doff = np.r_[doff, eoff + len(ddata)]
|
| 490 |
+
ddata = np.r_[ddata, edata]
|
| 491 |
+
dense = np.r_[dense, extra]
|
| 492 |
+
self.args = (lows, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata)
|
| 493 |
+
self.nthr = nthr or K["nb"].get_num_threads()
|
| 494 |
+
self.n_low, self.n_dense, self.mean_rank = len(lows), len(dense), float(ranks.mean()) if len(ranks) else 0.0
|
| 495 |
+
self.n_converted = int(bad.sum())
|
| 496 |
+
|
| 497 |
+
def __call__(self, X):
|
| 498 |
+
Xp = np.ascontiguousarray(X[self.perm])
|
| 499 |
+
Yp = np.empty_like(Xp)
|
| 500 |
+
_numba()["apply"](Xp, *self.args, self.nthr, Yp)
|
| 501 |
+
Y = np.empty_like(Yp)
|
| 502 |
+
Y[self.perm] = Yp
|
| 503 |
+
return Y
|
| 504 |
+
|
| 505 |
+
|
| 506 |
+
def _lagrange(u, q):
|
| 507 |
+
i0 = np.floor(u).astype(np.int64) - (q // 2 - 1)
|
| 508 |
+
t = u - i0
|
| 509 |
+
w = np.ones((len(u), q))
|
| 510 |
+
for a in range(q):
|
| 511 |
+
for b in range(q):
|
| 512 |
+
if a != b:
|
| 513 |
+
w[:, a] *= (t - b) / (a - b)
|
| 514 |
+
return i0, w
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
class ImageConv:
|
| 518 |
+
"""Smooth finite-substrate image kernel (proxy._image_correction, exact series) by grid convolution. (fastop.py)"""
|
| 519 |
+
|
| 520 |
+
def __init__(self, c, eps_r, spacing_frac=1 / 16, q=6):
|
| 521 |
+
from scipy.sparse import csr_matrix
|
| 522 |
+
P._image_correction(np.zeros(1), eps_r)
|
| 523 |
+
key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r))
|
| 524 |
+
_, _, w, D = P._IMG_CACHE[key]
|
| 525 |
+
self.w, self.D = w, D
|
| 526 |
+
h = spacing_frac * D.min()
|
| 527 |
+
lo = c.min(0) - (q // 2 + 1) * h
|
| 528 |
+
u = (c - lo) / h
|
| 529 |
+
ix0, wx = _lagrange(u[:, 0], q)
|
| 530 |
+
iy0, wy = _lagrange(u[:, 1], q)
|
| 531 |
+
nx, ny = int(ix0.max()) + q + 1, int(iy0.max()) + q + 1
|
| 532 |
+
rows = np.repeat(np.arange(len(c)), q * q)
|
| 533 |
+
gx = (ix0[:, None, None] + np.arange(q)[None, :, None]) * np.ones((1, 1, q), np.int64)
|
| 534 |
+
gy = (iy0[:, None, None] + np.arange(q)[None, None, :]) * np.ones((1, q, 1), np.int64)
|
| 535 |
+
vals = (wx[:, :, None] * wy[:, None, :]).ravel()
|
| 536 |
+
self.W = csr_matrix((vals, (rows, (gx * ny + gy).ravel())), shape=(len(c), nx * ny))
|
| 537 |
+
self.nx, self.ny = nx, ny
|
| 538 |
+
Lx, Ly = 2 * nx, 2 * ny
|
| 539 |
+
ax = np.minimum(np.arange(Lx), Lx - np.arange(Lx)) * h
|
| 540 |
+
ay = np.minimum(np.arange(Ly), Ly - np.arange(Ly)) * h
|
| 541 |
+
rho = np.sqrt(ax[:, None] ** 2 + ay[None, :] ** 2)
|
| 542 |
+
Kk = sum(wn / np.sqrt(rho**2 + Dn * Dn) for wn, Dn in zip(w, D))
|
| 543 |
+
self.Kf = np.fft.rfft2(Kk)
|
| 544 |
+
self.L = (Lx, Ly)
|
| 545 |
+
|
| 546 |
+
def __call__(self, X):
|
| 547 |
+
G = (self.W.T @ X).reshape(self.nx, self.ny, -1)
|
| 548 |
+
out = np.empty((self.nx * self.ny, X.shape[1]))
|
| 549 |
+
for k in range(X.shape[1]):
|
| 550 |
+
f = np.fft.irfft2(np.fft.rfft2(G[:, :, k], s=self.L) * self.Kf, s=self.L)[: self.nx, : self.ny]
|
| 551 |
+
out[:, k] = f.ravel()
|
| 552 |
+
return self.W @ out
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
class FastOp:
|
| 556 |
+
"""The frozen panel operator, matrix-free; bj(): block-Jacobi on the frozen cluster tree's leaves. (fastop.py)"""
|
| 557 |
+
|
| 558 |
+
def __init__(self, c, wh, o, eps_r, M, leaf=128, h_min_n=4000, h_eps=1e-8):
|
| 559 |
+
from scipy.sparse import csr_matrix
|
| 560 |
+
t = time.perf_counter()
|
| 561 |
+
self.H = None
|
| 562 |
+
if len(c) >= h_min_n:
|
| 563 |
+
self.H = HFar(c, eps=h_eps)
|
| 564 |
+
self.t_hmat = time.perf_counter() - t
|
| 565 |
+
t = time.perf_counter()
|
| 566 |
+
self.c, self.wh, self.o, self.n, self.M = c, wh, o, len(c), M
|
| 567 |
+
self.B = (o[:, None] == np.arange(M)[None]).astype(float)
|
| 568 |
+
self.k = 4 * np.pi * P.EPS0_FF_PER_UM * (eps_r + 1) / 2
|
| 569 |
+
self.x, self.y = np.ascontiguousarray(c[:, 0]), np.ascontiguousarray(c[:, 1])
|
| 570 |
+
ii, jj = P._near_pairs(c, wh.max(1))
|
| 571 |
+
d = np.sqrt(((c[ii] - c[jj]) ** 2).sum(1))
|
| 572 |
+
near = 0.5 * (P.rect_potential(c[ii], c[jj], wh[jj]) + P.rect_potential(c[jj], c[ii], wh[ii])) - 1.0 / d
|
| 573 |
+
dg = P.rect_self(wh[:, 0], wh[:, 1])
|
| 574 |
+
n = self.n
|
| 575 |
+
self.N = csr_matrix((np.r_[near, near, dg], (np.r_[ii, jj, np.arange(n)], np.r_[jj, ii, np.arange(n)])), shape=(n, n))
|
| 576 |
+
self.t_near = time.perf_counter() - t
|
| 577 |
+
t = time.perf_counter()
|
| 578 |
+
self.img = ImageConv(c, eps_r) if P._image_correction(np.zeros(1), eps_r) is not None else None
|
| 579 |
+
self.t_img = time.perf_counter() - t
|
| 580 |
+
t = time.perf_counter()
|
| 581 |
+
perm, nodes = P._cluster_tree(c, leaf)
|
| 582 |
+
self.leaves = []
|
| 583 |
+
img0 = P._image_correction
|
| 584 |
+
for a0, a1, lo, hi, l, r in nodes:
|
| 585 |
+
if l >= 0:
|
| 586 |
+
continue
|
| 587 |
+
ix = np.sort(perm[a0:a1])
|
| 588 |
+
cc = c[ix]
|
| 589 |
+
dd = np.sqrt(((cc[:, None] - cc[None]) ** 2).sum(-1))
|
| 590 |
+
np.fill_diagonal(dd, 1.0)
|
| 591 |
+
A = 1.0 / dd
|
| 592 |
+
np.fill_diagonal(A, 0.0)
|
| 593 |
+
A += self.N[ix][:, ix].toarray()
|
| 594 |
+
if self.img is not None:
|
| 595 |
+
np.fill_diagonal(dd, 0.0)
|
| 596 |
+
A += img0(dd, eps_r, exact=True)
|
| 597 |
+
self.leaves.append((ix, cho_factor(A / self.k, lower=True)))
|
| 598 |
+
self.t_blocks = time.perf_counter() - t
|
| 599 |
+
|
| 600 |
+
def matvec(self, X):
|
| 601 |
+
X = np.ascontiguousarray(X, dtype=np.float64)
|
| 602 |
+
if self.H is not None:
|
| 603 |
+
Y = self.H(X)
|
| 604 |
+
else:
|
| 605 |
+
Y = np.empty_like(X)
|
| 606 |
+
_numba()["far"](self.x, self.y, np.ascontiguousarray(X.T), Y)
|
| 607 |
+
Y += self.N @ X
|
| 608 |
+
if self.img is not None:
|
| 609 |
+
Y += self.img(X)
|
| 610 |
+
return Y / self.k
|
| 611 |
+
|
| 612 |
+
def bj(self, R):
|
| 613 |
+
Z = np.zeros_like(R)
|
| 614 |
+
for ix, cf in self.leaves:
|
| 615 |
+
Z[ix] = cho_solve(cf, R[ix], check_finite=False)
|
| 616 |
+
return Z
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
def compiled_pcg(c, wh, o, n_metals, eps_r):
|
| 620 |
+
"""Panel charges (n, n_metals) by the frozen block-Jacobi PCG (proxy._HMatrix.pcg: all columns, stop when every
|
| 621 |
+
column's relative residual < HMAT tol, LinAlgError after maxit) on the compiled operator. A zero right-hand-side
|
| 622 |
+
column (a metal without panels) gives residual 0/0 = NaN forever, so it raises at once instead of after maxit."""
|
| 623 |
+
STATS["compiled_calls"] += 1
|
| 624 |
+
B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float)
|
| 625 |
+
bn = np.sqrt((B * B).sum(0))
|
| 626 |
+
if not np.all(bn > 0):
|
| 627 |
+
raise np.linalg.LinAlgError("H-matrix PCG: a metal without panels (zero right-hand side; frozen: NaN residual)")
|
| 628 |
+
op = FastOp(c, wh, o, eps_r, n_metals, h_eps=P.HMAT["eps"])
|
| 629 |
+
tol, maxit = P.HMAT["tol"], P.HMAT["maxit"]
|
| 630 |
+
X, R = np.zeros_like(B), B.copy()
|
| 631 |
+
Z = op.bj(R)
|
| 632 |
+
Pd, rz = Z.copy(), (R * Z).sum(0)
|
| 633 |
+
res = np.inf
|
| 634 |
+
for it in range(maxit):
|
| 635 |
+
AP = op.matvec(Pd)
|
| 636 |
+
al = rz / (Pd * AP).sum(0)
|
| 637 |
+
X += al * Pd
|
| 638 |
+
R -= al * AP
|
| 639 |
+
res = (np.sqrt((R * R).sum(0)) / bn).max()
|
| 640 |
+
if res < tol:
|
| 641 |
+
return X
|
| 642 |
+
if not np.isfinite(res):
|
| 643 |
+
break
|
| 644 |
+
Z = op.bj(R)
|
| 645 |
+
rzn = (R * Z).sum(0)
|
| 646 |
+
Pd = Z + (rzn / rz) * Pd
|
| 647 |
+
rz = rzn
|
| 648 |
+
raise np.linalg.LinAlgError(f"compiled H-matrix PCG did not converge (residual {res:.1e})")
|
| 649 |
+
|
| 650 |
+
|
| 651 |
+
def _mom_solve_fast(c, wh, o, n_metals, eps_r=11.45, dense_max=None):
|
| 652 |
+
n = len(c)
|
| 653 |
+
if n <= (P.DENSE_MAX if dense_max is None else dense_max) or "compiled" not in STATE["components"]:
|
| 654 |
+
return _ORIG["mom_solve_fast"](c, wh, o, n_metals, eps_r, dense_max)
|
| 655 |
+
return compiled_pcg(c, wh, o, n_metals, eps_r)
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
# ------------------------------------------------------------------------------------------------ hmat_vec (fastmom.py)
|
| 659 |
+
# scripts/geomfast/fastmom.py: the frozen H-matrix (same cluster tree, ACA, block-Jacobi PCG, FROZEN tolerances) with a
|
| 660 |
+
# vectorised kernel (no per-row scipy sparse slicing, no Python loop over image charges or ACA ranks). Only the H-matrix
|
| 661 |
+
# kernel is patched (proxy._image_correction and the dense path are untouched).
|
| 662 |
+
|
| 663 |
+
|
| 664 |
+
def _hv_images(eps_r):
|
| 665 |
+
P._image_correction(np.zeros(1), eps_r)
|
| 666 |
+
key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r))
|
| 667 |
+
w, D = P._IMG_CACHE[key][2:]
|
| 668 |
+
return np.asarray(w, float), np.asarray(D, float) ** 2
|
| 669 |
+
|
| 670 |
+
|
| 671 |
+
def _hv_kernel_init(self, c, wh, eps_r, perm):
|
| 672 |
+
_ORIG["Kernel.__init__"](self, c, wh, eps_r, perm)
|
| 673 |
+
N = self.N.tocsr()
|
| 674 |
+
N.sort_indices()
|
| 675 |
+
self.indptr, self.indices, self.data = N.indptr, N.indices, N.data
|
| 676 |
+
self.w, self.D2 = _hv_images(eps_r) if self.img else (None, None)
|
| 677 |
+
|
| 678 |
+
|
| 679 |
+
def _hv_corr(self, d):
|
| 680 |
+
return np.sqrt((d * d)[..., None] + self.D2) ** -1 @ self.w
|
| 681 |
+
|
| 682 |
+
|
| 683 |
+
def _hv_row(self, i, b0, b1):
|
| 684 |
+
d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2)
|
| 685 |
+
r = 1.0 / d
|
| 686 |
+
if self.img:
|
| 687 |
+
r += _hv_corr(self, d)
|
| 688 |
+
s, e = self.indptr[i], self.indptr[i + 1]
|
| 689 |
+
if e > s:
|
| 690 |
+
ix = self.indices[s:e]
|
| 691 |
+
lo, hi = np.searchsorted(ix, (b0, b1))
|
| 692 |
+
if hi > lo:
|
| 693 |
+
r[ix[lo:hi] - b0] += self.data[s + lo:s + hi]
|
| 694 |
+
return r / self.k
|
| 695 |
+
|
| 696 |
+
|
| 697 |
+
def _hv_block(self, a0, a1, b0, b1):
|
| 698 |
+
d = np.sqrt((self.x[a0:a1, None] - self.x[None, b0:b1]) ** 2 + (self.y[a0:a1, None] - self.y[None, b0:b1]) ** 2)
|
| 699 |
+
corr = _hv_corr(self, d) if self.img else None
|
| 700 |
+
if a0 == b0:
|
| 701 |
+
d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0
|
| 702 |
+
A = 1.0 / d
|
| 703 |
+
if corr is not None:
|
| 704 |
+
A += corr
|
| 705 |
+
s, e = self.indptr[a0], self.indptr[a1]
|
| 706 |
+
if e > s:
|
| 707 |
+
ix = self.indices[s:e]
|
| 708 |
+
rr = np.repeat(np.arange(a1 - a0), np.diff(self.indptr[a0:a1 + 1]))
|
| 709 |
+
m = (ix >= b0) & (ix < b1)
|
| 710 |
+
np.add.at(A, (rr[m], ix[m] - b0), self.data[s:e][m])
|
| 711 |
+
return A / self.k
|
| 712 |
+
|
| 713 |
+
|
| 714 |
+
def _hv_aca(K, a0, a1, b0, b1, eps, max_rank):
|
| 715 |
+
m, n = a1 - a0, b1 - b0
|
| 716 |
+
R = min(m, n, max_rank)
|
| 717 |
+
U, V = np.empty((m, R)), np.empty((n, R))
|
| 718 |
+
used_r, used_c = np.zeros(m, bool), np.zeros(n, bool)
|
| 719 |
+
i, norm2, k = 0, 0.0, 0
|
| 720 |
+
for _ in range(R):
|
| 721 |
+
r = K.row(a0 + i, b0, b1)
|
| 722 |
+
if k:
|
| 723 |
+
r -= V[:, :k] @ U[i, :k]
|
| 724 |
+
rm = np.where(used_c, 0.0, np.abs(r))
|
| 725 |
+
j = int(np.argmax(rm))
|
| 726 |
+
if rm[j] == 0.0:
|
| 727 |
+
break
|
| 728 |
+
v = r / r[j]
|
| 729 |
+
u = K.row(b0 + j, a0, a1)
|
| 730 |
+
if k:
|
| 731 |
+
u -= U[:, :k] @ V[j, :k]
|
| 732 |
+
used_r[i], used_c[j] = True, True
|
| 733 |
+
nu, nv = float(u @ u), float(v @ v)
|
| 734 |
+
norm2 += nu * nv + (2 * float((U[:, :k].T @ u) @ (V[:, :k].T @ v)) if k else 0.0)
|
| 735 |
+
U[:, k], V[:, k] = u, v
|
| 736 |
+
k += 1
|
| 737 |
+
if nu * nv <= eps**2 * norm2:
|
| 738 |
+
break
|
| 739 |
+
um = np.where(used_r, 0.0, np.abs(u))
|
| 740 |
+
i = int(np.argmax(um))
|
| 741 |
+
if um[i] == 0.0:
|
| 742 |
+
break
|
| 743 |
+
else:
|
| 744 |
+
return None
|
| 745 |
+
return U[:, :k].copy(), V[:, :k].copy()
|
| 746 |
+
|
| 747 |
+
|
| 748 |
+
def _hv_mom_solve_fast(c, wh, o, n_metals, eps_r=11.45, dense_max=None):
|
| 749 |
+
"""The frozen mom_solve_fast (vectorised kernel); a zero right-hand-side column raises at once (frozen: after maxit)."""
|
| 750 |
+
if len(c) > (P.DENSE_MAX if dense_max is None else dense_max) and not np.all(np.bincount(o, minlength=n_metals) > 0):
|
| 751 |
+
raise np.linalg.LinAlgError("H-matrix PCG: a metal without panels (zero right-hand side; frozen: NaN residual)")
|
| 752 |
+
return _ORIG["mom_solve_fast"](c, wh, o, n_metals, eps_r, dense_max)
|
| 753 |
+
|
| 754 |
+
|
| 755 |
+
# ------------------------------------------------------------------------------------------------ gpu (fastgpu.py)
|
| 756 |
+
|
| 757 |
+
|
| 758 |
+
class _Dev:
|
| 759 |
+
def __init__(self, t):
|
| 760 |
+
self.t = t
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
def _gpu_assemble(c, wh, diag, eps_r, near):
|
| 764 |
+
n = len(c)
|
| 765 |
+
if not STATE["gpu"] or n < CFG["gpu_min"]:
|
| 766 |
+
return _ORIG["assemble"](c, wh, diag, eps_r, near)
|
| 767 |
+
import torch
|
| 768 |
+
dev = CFG["device"]
|
| 769 |
+
k = 4 * np.pi * P.EPS0_FF_PER_UM * (eps_r + 1) / 2
|
| 770 |
+
img = P._image_correction(np.zeros(1), eps_r) is not None
|
| 771 |
+
if img:
|
| 772 |
+
key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r))
|
| 773 |
+
du, T = P._IMG_CACHE[key][:2]
|
| 774 |
+
T = torch.as_tensor(T, dtype=torch.float64, device=dev)
|
| 775 |
+
x = torch.as_tensor(c[:, 0], dtype=torch.float64, device=dev)
|
| 776 |
+
y = torch.as_tensor(c[:, 1], dtype=torch.float64, device=dev)
|
| 777 |
+
A = torch.empty((n, n), dtype=torch.float64, device=dev)
|
| 778 |
+
for r0 in range(0, n, CFG["rows"]):
|
| 779 |
+
r1 = min(r0 + CFG["rows"], n)
|
| 780 |
+
d = torch.sqrt((x[r0:r1, None] - x[None, :]) ** 2 + (y[r0:r1, None] - y[None, :]) ** 2)
|
| 781 |
+
if img:
|
| 782 |
+
u = d / du
|
| 783 |
+
j = torch.clamp(u.floor().long(), 0, len(T) - 2)
|
| 784 |
+
f = u - j
|
| 785 |
+
corr = T[j] + f * (T[j + 1] - T[j])
|
| 786 |
+
d[torch.arange(r1 - r0, device=dev), torch.arange(r0, r1, device=dev)] = 1.0
|
| 787 |
+
blk = 1.0 / d
|
| 788 |
+
if img:
|
| 789 |
+
blk += corr
|
| 790 |
+
A[r0:r1] = blk / k
|
| 791 |
+
ii, jj = P._near_pairs(c, wh.max(1))
|
| 792 |
+
if len(ii):
|
| 793 |
+
v = 0.5 * (near(c[ii], c[jj], wh[jj]) + near(c[jj], c[ii], wh[ii])) / k
|
| 794 |
+
if img:
|
| 795 |
+
v = v + P._image_correction(np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)), eps_r) / k
|
| 796 |
+
vi = torch.as_tensor(v, dtype=torch.float64, device=dev)
|
| 797 |
+
ti, tj = torch.as_tensor(ii, device=dev), torch.as_tensor(jj, device=dev)
|
| 798 |
+
A[ti, tj] = vi
|
| 799 |
+
A[tj, ti] = vi
|
| 800 |
+
dg = diag / k if not img else (diag + P._image_correction(np.zeros(1), eps_r)[0]) / k
|
| 801 |
+
A[torch.arange(n, device=dev), torch.arange(n, device=dev)] = torch.as_tensor(dg, dtype=torch.float64, device=dev)
|
| 802 |
+
return _Dev(A)
|
| 803 |
+
|
| 804 |
+
|
| 805 |
+
def _gpu_chol(A, B):
|
| 806 |
+
import torch
|
| 807 |
+
L, info = torch.linalg.cholesky_ex(A.t)
|
| 808 |
+
if int(info) > 0:
|
| 809 |
+
raise np.linalg.LinAlgError("not positive definite (device Cholesky)")
|
| 810 |
+
Bt = torch.as_tensor(np.ascontiguousarray(B), dtype=torch.float64, device=A.t.device)
|
| 811 |
+
X = torch.cholesky_solve(Bt, L).cpu().numpy()
|
| 812 |
+
A.t = None # the matrix is not used after a successful solve: release it and the allocator cache (shared GPU)
|
| 813 |
+
del L, Bt
|
| 814 |
+
torch.cuda.empty_cache()
|
| 815 |
+
return X
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
# ------------------------------------------------------------------------------------------------ mixed (fastmom.py)
|
| 819 |
+
|
| 820 |
+
|
| 821 |
+
def _mixed_chol(A, B):
|
| 822 |
+
from scipy.linalg.blas import dsymm
|
| 823 |
+
try:
|
| 824 |
+
cf = cho_factor(A.astype(np.float32, order="F"), lower=True, overwrite_a=True, check_finite=False)
|
| 825 |
+
except np.linalg.LinAlgError:
|
| 826 |
+
return _ORIG["chol_solve"](A, B)
|
| 827 |
+
B = np.asfortranarray(B, dtype=float)
|
| 828 |
+
X = cho_solve(cf, B.astype(np.float32), check_finite=False).astype(float)
|
| 829 |
+
bn = np.linalg.norm(B, axis=0)
|
| 830 |
+
for _ in range(CFG["refine_steps"]):
|
| 831 |
+
R = B - dsymm(1.0, A, X, side=0, lower=1)
|
| 832 |
+
if (np.linalg.norm(R, axis=0) <= CFG["refine_tol"] * bn).all():
|
| 833 |
+
return X
|
| 834 |
+
X += cho_solve(cf, R.astype(np.float32), check_finite=False)
|
| 835 |
+
return _ORIG["chol_solve"](A, B)
|
| 836 |
+
|
| 837 |
+
|
| 838 |
+
def _chol_solve(A, B):
|
| 839 |
+
if isinstance(A, _Dev):
|
| 840 |
+
return _gpu_chol(A, B)
|
| 841 |
+
if "mixed" in STATE["components"]:
|
| 842 |
+
return _mixed_chol(A, B)
|
| 843 |
+
return _ORIG["chol_solve"](A, B)
|
| 844 |
+
|
| 845 |
+
|
| 846 |
+
def _sym_solve(A, B):
|
| 847 |
+
if not isinstance(A, _Dev):
|
| 848 |
+
return _ORIG["sym_solve"](A, B)
|
| 849 |
+
if A.t.shape[0] > FROZEN_DENSE_MAX:
|
| 850 |
+
return _gpu_chol(A, B)
|
| 851 |
+
try:
|
| 852 |
+
return _gpu_chol(A, B)
|
| 853 |
+
except np.linalg.LinAlgError:
|
| 854 |
+
from scipy.linalg import solve
|
| 855 |
+
return solve(A.t.cpu().numpy(), B, lower=True, assume_a="sym", overwrite_a=True, check_finite=False)
|
| 856 |
+
|
| 857 |
+
|
| 858 |
+
# ------------------------------------------------------------------------------------------------ ritz (certified)
|
| 859 |
+
# scripts/varritz/vr.py (templates, Ritz) and scripts/varritz/ritzprior.py (window / local loops, A5 dense dispatch).
|
| 860 |
+
|
| 861 |
+
|
| 862 |
+
def edge_geometry(metals, c, wh, o):
|
| 863 |
+
import shapely
|
| 864 |
+
n, M = len(c), len(metals)
|
| 865 |
+
d, a, g = np.zeros(n), np.zeros(n), np.zeros(n)
|
| 866 |
+
D = np.zeros((n, M))
|
| 867 |
+
pts = shapely.points(c)
|
| 868 |
+
for k in range(M):
|
| 869 |
+
ix = np.nonzero(o == k)[0]
|
| 870 |
+
if not len(ix):
|
| 871 |
+
continue
|
| 872 |
+
bd = metals[k].boundary
|
| 873 |
+
shapely.prepare(bd)
|
| 874 |
+
ln = shapely.shortest_line(pts[ix], bd)
|
| 875 |
+
xy = shapely.get_coordinates(ln).reshape(-1, 2, 2)
|
| 876 |
+
v = xy[:, 0] - xy[:, 1]
|
| 877 |
+
dk = np.sqrt((v**2).sum(1))
|
| 878 |
+
nv = np.where(dk[:, None] > 0, v / np.maximum(dk, 1e-300)[:, None], 0.7071)
|
| 879 |
+
d[ix], a[ix] = dk, np.abs(nv[:, 0]) * wh[ix, 0] + np.abs(nv[:, 1]) * wh[ix, 1]
|
| 880 |
+
others = shapely.union_all([metals[j] for j in range(M) if j != k])
|
| 881 |
+
shapely.prepare(others)
|
| 882 |
+
g[ix] = shapely.distance(shapely.points(xy[:, 1]), others)
|
| 883 |
+
for j in range(M):
|
| 884 |
+
shapely.prepare(metals[j])
|
| 885 |
+
D[:, j] = shapely.distance(pts, metals[j])
|
| 886 |
+
return d, a, np.maximum(g, 1e-9), D
|
| 887 |
+
|
| 888 |
+
|
| 889 |
+
def templates(d, a, g):
|
| 890 |
+
lo, hi = np.maximum(d - a / 2, 0.0), d + a / 2
|
| 891 |
+
w = np.maximum(hi - lo, 1e-12)
|
| 892 |
+
s = 2 * (np.sqrt(hi) - np.sqrt(lo)) / w
|
| 893 |
+
gg = 2 * (np.arcsinh(np.sqrt(hi / g)) - np.arcsinh(np.sqrt(lo / g))) / w
|
| 894 |
+
return {"u": np.ones_like(d), "s": s, "g": gg}
|
| 895 |
+
|
| 896 |
+
|
| 897 |
+
def _orth_blocks(V, o, M):
|
| 898 |
+
out = []
|
| 899 |
+
for k in range(M):
|
| 900 |
+
ix = np.nonzero(o == k)[0]
|
| 901 |
+
cols = np.nonzero(np.abs(V[ix]).sum(0) > 0)[0]
|
| 902 |
+
if not len(cols):
|
| 903 |
+
continue
|
| 904 |
+
Vk = V[np.ix_(ix, cols)]
|
| 905 |
+
Vk = Vk / np.maximum(np.linalg.norm(Vk, axis=0), 1e-300)
|
| 906 |
+
U, s, _ = np.linalg.svd(Vk, full_matrices=False)
|
| 907 |
+
keep = s > 1e-10 * s[0]
|
| 908 |
+
W = np.zeros((len(o), int(keep.sum())))
|
| 909 |
+
W[ix] = U[:, keep]
|
| 910 |
+
out.append(W)
|
| 911 |
+
return np.hstack(out)
|
| 912 |
+
|
| 913 |
+
|
| 914 |
+
def ritz(V, PV, B):
|
| 915 |
+
G = V.T @ PV
|
| 916 |
+
G = 0.5 * (G + G.T)
|
| 917 |
+
Fm = V.T @ B
|
| 918 |
+
try:
|
| 919 |
+
Y = cho_solve(cho_factor(G, lower=True), Fm)
|
| 920 |
+
except np.linalg.LinAlgError:
|
| 921 |
+
lam, U = eigh(G)
|
| 922 |
+
keep = lam > 1e-13 * lam.max()
|
| 923 |
+
Y = U[:, keep] @ ((U[:, keep].T @ Fm) / lam[keep, None])
|
| 924 |
+
CV = Fm.T @ Y
|
| 925 |
+
return 0.5 * (CV + CV.T), Y, B - PV @ Y
|
| 926 |
+
|
| 927 |
+
|
| 928 |
+
def start_basis(metals, c, wh, o, kind, op, B):
|
| 929 |
+
M = len(metals)
|
| 930 |
+
if kind == "pcg":
|
| 931 |
+
Z = op.bj(B)
|
| 932 |
+
return Z / np.maximum(np.linalg.norm(Z, axis=0), 1e-300)
|
| 933 |
+
area = wh[:, 0] * wh[:, 1]
|
| 934 |
+
d, a, g, _ = edge_geometry(metals, c, wh, o)
|
| 935 |
+
T = templates(d, a, g)
|
| 936 |
+
return _orth_blocks(np.hstack([np.where((o == k)[:, None], (T[nm] * area)[:, None], 0.0)
|
| 937 |
+
for k in range(M) for nm in "usg"]), o, M)
|
| 938 |
+
|
| 939 |
+
|
| 940 |
+
def criterion(CV, est, floor=0.1):
|
| 941 |
+
e = np.sqrt(np.maximum(np.diag(est), 0.0))
|
| 942 |
+
return float(np.max(np.outer(e, e) / np.maximum(np.abs(CV), floor)))
|
| 943 |
+
|
| 944 |
+
|
| 945 |
+
def ritz_solve(metals, c, wh, o, eps_r, cols):
|
| 946 |
+
tau, start, maxit = CFG["ritz_tau"], CFG["ritz_start"], CFG["ritz_maxit"]
|
| 947 |
+
t0 = time.perf_counter()
|
| 948 |
+
op = FastOp(c, wh, o, eps_r, len(metals))
|
| 949 |
+
B = op.B
|
| 950 |
+
V = start_basis(metals, c, wh, o, start, op, B)
|
| 951 |
+
PV = op.matvec(V)
|
| 952 |
+
for m in range(maxit + 1):
|
| 953 |
+
CV, Y, R = ritz(V, PV, B)
|
| 954 |
+
est = R.T @ op.bj(R)
|
| 955 |
+
rel = criterion(CV, est)
|
| 956 |
+
if rel <= tau or m == maxit:
|
| 957 |
+
break
|
| 958 |
+
Z = op.bj(R)
|
| 959 |
+
Z /= np.maximum(np.linalg.norm(Z, axis=0), 1e-300)
|
| 960 |
+
V, PV = np.hstack([V, Z]), np.hstack([PV, op.matvec(Z)])
|
| 961 |
+
info = dict(n=len(c), steps=m, converged=rel <= tau, est_rel=rel, est_diag=np.diag(est).tolist(), k=V.shape[1],
|
| 962 |
+
t=time.perf_counter() - t0)
|
| 963 |
+
return (V @ Y)[:, np.asarray(cols)], info
|
| 964 |
+
|
| 965 |
+
|
| 966 |
+
def _frozen_solve(c, wh, o, eps_r, cols, all_rhs, n_metals):
|
| 967 |
+
if len(c) <= P.DENSE_MAX:
|
| 968 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 969 |
+
Bm = (o[:, None] == cols[None, :]).astype(float)
|
| 970 |
+
return P._chol_solve(A, Bm) if all_rhs else F._sym_solve(A, Bm)
|
| 971 |
+
return P.mom_solve_fast(c, wh, o, n_metals, eps_r)[:, cols]
|
| 972 |
+
|
| 973 |
+
|
| 974 |
+
def _ritz_dense_n():
|
| 975 |
+
return max(CFG["ritz_dense_n"], CFG["gpu_dense_max"] if STATE["gpu"] else 0)
|
| 976 |
+
|
| 977 |
+
|
| 978 |
+
def _solve_mesh(metals, c, wh, o, eps_r, cols, all_rhs, kind):
|
| 979 |
+
if len(c) <= _ritz_dense_n(): # A5 (and the GPU): exact solve, estimate 0
|
| 980 |
+
Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals))
|
| 981 |
+
LOG.append(dict(kind=kind, ritz=None, exact=True, steps=0, n=len(c), est_rel=0.0, est_diag=[0.0] * len(metals)))
|
| 982 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 983 |
+
try:
|
| 984 |
+
Q, info = ritz_solve(metals, c, wh, o, eps_r, cols)
|
| 985 |
+
rec = dict(kind=kind, ritz=bool(info["converged"]), exact=False, **info)
|
| 986 |
+
if not info["converged"]:
|
| 987 |
+
raise np.linalg.LinAlgError("ritz not converged")
|
| 988 |
+
except np.linalg.LinAlgError:
|
| 989 |
+
LOG.append(dict(kind=kind, ritz=False, exact=True, fallback=True, steps=0, n=len(c), est_rel=0.0, est_diag=[0.0] * len(metals)))
|
| 990 |
+
Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals))
|
| 991 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 992 |
+
LOG.append(rec)
|
| 993 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 994 |
+
|
| 995 |
+
|
| 996 |
+
def _last_good(R, ok, skipped, metals, eps_r, cols, all_rhs):
|
| 997 |
+
if R is not None and (ok or not np.isnan(R).any()):
|
| 998 |
+
return R
|
| 999 |
+
for c, wh, o in reversed(skipped):
|
| 1000 |
+
try:
|
| 1001 |
+
Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals))
|
| 1002 |
+
LOG.append(dict(kind="last_good", ritz=None, exact=True, steps=0, n=len(c), est_rel=0.0, est_diag=[0.0] * len(metals)))
|
| 1003 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 1004 |
+
except np.linalg.LinAlgError:
|
| 1005 |
+
continue
|
| 1006 |
+
raise np.linalg.LinAlgError("no successful solve in the refinement sequence")
|
| 1007 |
+
|
| 1008 |
+
|
| 1009 |
+
def _final_unpanelled(metals, c, wh, o, eps_r, cols, all_rhs):
|
| 1010 |
+
"""Final majority mesh still missing a metal: the frozen solve (doomed above the frozen DENSE_MAX: it raises)."""
|
| 1011 |
+
if len(c) > FROZEN_DENSE_MAX:
|
| 1012 |
+
raise np.linalg.LinAlgError("final mesh misses a metal above DENSE_MAX (frozen: NaN PCG)")
|
| 1013 |
+
Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals))
|
| 1014 |
+
LOG.append(dict(kind="window", ritz=None, exact=True, steps=0, n=len(c), est_rel=0.0, est_diag=[0.0] * len(metals)))
|
| 1015 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 1016 |
+
|
| 1017 |
+
|
| 1018 |
+
def ritz_window_solve(metals, n_core, h, budget, eps_r, all_rhs, levels=()):
|
| 1019 |
+
cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core)
|
| 1020 |
+
t_mesh = t_sol = 0.0
|
| 1021 |
+
hh, cap, refined, majority = h, budget, 0, False
|
| 1022 |
+
skipped, R = [], None
|
| 1023 |
+
while True:
|
| 1024 |
+
t = time.perf_counter()
|
| 1025 |
+
if not refined:
|
| 1026 |
+
c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels)
|
| 1027 |
+
elif levels:
|
| 1028 |
+
c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority)
|
| 1029 |
+
else:
|
| 1030 |
+
c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority)
|
| 1031 |
+
t_mesh += time.perf_counter() - t
|
| 1032 |
+
t = time.perf_counter()
|
| 1033 |
+
full = bool(np.all(np.bincount(o, minlength=len(metals)) > 0))
|
| 1034 |
+
if not full and not majority:
|
| 1035 |
+
skipped.append((c, wh, o))
|
| 1036 |
+
if refined < F.LITE_REFINE:
|
| 1037 |
+
hh, cap, refined = hh / 2, np.inf, refined + 1
|
| 1038 |
+
else:
|
| 1039 |
+
majority = True
|
| 1040 |
+
continue
|
| 1041 |
+
try:
|
| 1042 |
+
if not full:
|
| 1043 |
+
R = _final_unpanelled(metals, c, wh, o, eps_r, cols, all_rhs)
|
| 1044 |
+
else:
|
| 1045 |
+
R = _solve_mesh(metals, c, wh, o, eps_r, cols, all_rhs, "window")
|
| 1046 |
+
off = np.ones(R.shape, bool)
|
| 1047 |
+
off[np.arange(len(cols)), cols] = False
|
| 1048 |
+
ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0)))
|
| 1049 |
+
except np.linalg.LinAlgError:
|
| 1050 |
+
ok = False
|
| 1051 |
+
t_sol += time.perf_counter() - t
|
| 1052 |
+
if LOG:
|
| 1053 |
+
LOG[-1].update(accepted=ok, refined=refined)
|
| 1054 |
+
if ok or majority:
|
| 1055 |
+
break
|
| 1056 |
+
if refined < F.LITE_REFINE:
|
| 1057 |
+
hh, cap, refined = hh / 2, np.inf, refined + 1
|
| 1058 |
+
else:
|
| 1059 |
+
majority = True
|
| 1060 |
+
R = _last_good(R, ok, skipped, metals, eps_r, cols, all_rhs)
|
| 1061 |
+
return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh,
|
| 1062 |
+
"assemble_s": 0.0, "solve_s": t_sol}
|
| 1063 |
+
|
| 1064 |
+
|
| 1065 |
+
def ritz_loc_solve(metals, H, h_floor, ppg, eps_r):
|
| 1066 |
+
t_mesh = t_sol = 0.0
|
| 1067 |
+
cols = np.arange(len(metals))
|
| 1068 |
+
refined, majority = 0, False
|
| 1069 |
+
skipped, R = [], None
|
| 1070 |
+
while True:
|
| 1071 |
+
t = time.perf_counter()
|
| 1072 |
+
c, wh, o, n_raw = FL.mesh_local(metals, H, h_floor, ppg, majority)
|
| 1073 |
+
t_mesh += time.perf_counter() - t
|
| 1074 |
+
t = time.perf_counter()
|
| 1075 |
+
full = bool(np.all(np.bincount(o, minlength=len(metals)) > 0))
|
| 1076 |
+
if not full and not majority:
|
| 1077 |
+
skipped.append((c, wh, o))
|
| 1078 |
+
if refined < F.LITE_REFINE:
|
| 1079 |
+
H, h_floor, refined = H / 2, h_floor / 2, refined + 1
|
| 1080 |
+
else:
|
| 1081 |
+
majority = True
|
| 1082 |
+
continue
|
| 1083 |
+
try:
|
| 1084 |
+
if not full:
|
| 1085 |
+
R = _final_unpanelled(metals, c, wh, o, eps_r, cols, True)
|
| 1086 |
+
else:
|
| 1087 |
+
R = _solve_mesh(metals, c, wh, o, eps_r, cols, True, "local")
|
| 1088 |
+
off = ~np.eye(len(metals), dtype=bool)
|
| 1089 |
+
ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and np.all(R[off] < 0))
|
| 1090 |
+
except np.linalg.LinAlgError:
|
| 1091 |
+
ok = False
|
| 1092 |
+
t_sol += time.perf_counter() - t
|
| 1093 |
+
if LOG:
|
| 1094 |
+
LOG[-1].update(accepted=ok, refined=refined)
|
| 1095 |
+
if ok or majority:
|
| 1096 |
+
break
|
| 1097 |
+
if refined < F.LITE_REFINE:
|
| 1098 |
+
H, h_floor, refined = H / 2, h_floor / 2, refined + 1
|
| 1099 |
+
else:
|
| 1100 |
+
majority = True
|
| 1101 |
+
R = _last_good(R, ok, skipped, metals, eps_r, cols, True)
|
| 1102 |
+
return R, {"H_um": H, "h_floor_um": h_floor, "h_min_um": float(wh.min()), "panels": len(c), "raw": n_raw,
|
| 1103 |
+
"refined": refined, "majority": majority, "mesh_s": t_mesh, "solve_s": t_sol}
|
| 1104 |
+
|
| 1105 |
+
|
| 1106 |
+
def _micro_mom_loc(G, conds, eps_r=11.45, ppg=3.0, *args, **kw):
|
| 1107 |
+
"""micro_mom_loc + a certificate (certified mode). Window records of forked workers come back through a file."""
|
| 1108 |
+
LOG.clear()
|
| 1109 |
+
fd, path = tempfile.mkstemp(prefix="fp_cert_", suffix=".jsonl")
|
| 1110 |
+
os.close(fd)
|
| 1111 |
+
_CERT_FILE[0] = path
|
| 1112 |
+
try:
|
| 1113 |
+
out = _ORIG["micro_mom_loc"](G, conds, eps_r, ppg, *args, **kw)
|
| 1114 |
+
with open(path) as f:
|
| 1115 |
+
LOG.extend(json.loads(ln) for ln in f if ln.strip())
|
| 1116 |
+
finally:
|
| 1117 |
+
_CERT_FILE[0] = None
|
| 1118 |
+
os.unlink(path)
|
| 1119 |
+
CERTS.append(_certificate(G, conds, out[0], list(LOG), ppg))
|
| 1120 |
+
return out
|
| 1121 |
+
|
| 1122 |
+
|
| 1123 |
+
def _certificate(G, conds, C, recs, ppg):
|
| 1124 |
+
"""Calibrated estimate (c = RITZ_CAL) of the prior's solver error: max relative entry error (0.1 fF floor) over the
|
| 1125 |
+
accepted solves; per-entry fF estimates c sqrt(e_i e_j) for a single-window single-frame layout (input order).
|
| 1126 |
+
Self entries of Ritz solves are lower bounds of the exact ones."""
|
| 1127 |
+
acc = [r for r in recs if r.get("accepted", True) and "est_rel" in r]
|
| 1128 |
+
cert = dict(ppg=ppg, n_solves=len(recs), n_ritz=sum(bool(r.get("ritz")) for r in recs),
|
| 1129 |
+
n_exact=sum(bool(r.get("exact")) for r in recs), n_fallback=sum(bool(r.get("fallback")) for r in recs),
|
| 1130 |
+
max_rel_est=RITZ_CAL * max((r["est_rel"] for r in acc), default=0.0), calibration=RITZ_CAL,
|
| 1131 |
+
windows=[dict(window=r.get("window"), n=r["n"], ritz=bool(r.get("ritz")), max_rel_est=RITZ_CAL * r["est_rel"])
|
| 1132 |
+
for r in acc], entry_est_fF=None)
|
| 1133 |
+
if len(acc) == 1 and len(conds) <= 8:
|
| 1134 |
+
frames = F.canonical_frames(G, conds)
|
| 1135 |
+
e = np.maximum(np.asarray(acc[0]["est_diag"], float), 0.0)
|
| 1136 |
+
if len(frames) == 1 and len(e) == len(conds) + 1:
|
| 1137 |
+
inv = np.r_[0, np.argsort(frames[0][2]) + 1]
|
| 1138 |
+
ei = e[inv]
|
| 1139 |
+
cert["entry_est_fF"] = (RITZ_CAL * np.sqrt(np.outer(ei, ei))).tolist()
|
| 1140 |
+
return cert
|
| 1141 |
+
|
| 1142 |
+
|
| 1143 |
+
def certificates(clear=True) -> list:
|
| 1144 |
+
out = list(CERTS)
|
| 1145 |
+
if clear:
|
| 1146 |
+
CERTS.clear()
|
| 1147 |
+
return out
|
| 1148 |
+
|
| 1149 |
+
|
| 1150 |
+
# ------------------------------------------------------------------------------------------------ install
|
| 1151 |
+
|
| 1152 |
+
|
| 1153 |
+
def _gpu_available() -> bool:
|
| 1154 |
+
try:
|
| 1155 |
+
import torch
|
| 1156 |
+
return bool(torch.cuda.is_available())
|
| 1157 |
+
except Exception: # noqa: BLE001
|
| 1158 |
+
return False
|
| 1159 |
+
|
| 1160 |
+
|
| 1161 |
+
def resolve(mode=None, components=None) -> tuple:
|
| 1162 |
+
if components is not None:
|
| 1163 |
+
bad = set(components) - set(COMPONENTS)
|
| 1164 |
+
if bad:
|
| 1165 |
+
raise ValueError(f"unknown components {sorted(bad)}; known {COMPONENTS}")
|
| 1166 |
+
return tuple(c for c in COMPONENTS if c in components)
|
| 1167 |
+
mode = mode or os.environ.get("TLSQ_PRIOR_MODE", "exact")
|
| 1168 |
+
if mode not in ("exact", "exact_compiled", "certified", "frozen"):
|
| 1169 |
+
raise ValueError(f"mode {mode!r}")
|
| 1170 |
+
if mode == "frozen":
|
| 1171 |
+
return ()
|
| 1172 |
+
comps = ["fv", "parallel", "hmat_vec"]
|
| 1173 |
+
if _gpu_available():
|
| 1174 |
+
comps.append("gpu")
|
| 1175 |
+
if MIXED_GATED:
|
| 1176 |
+
comps.append("mixed")
|
| 1177 |
+
if mode in ("exact_compiled", "certified"):
|
| 1178 |
+
comps.append("compiled")
|
| 1179 |
+
if mode == "certified":
|
| 1180 |
+
comps.append("ritz")
|
| 1181 |
+
return tuple(c for c in COMPONENTS if c in comps)
|
| 1182 |
+
|
| 1183 |
+
|
| 1184 |
+
def install(mode=None, components=None, workers=None, cpus=None, target="auto", blas_threads="auto", value_patch=None,
|
| 1185 |
+
warm=True, **cfg):
|
| 1186 |
+
"""Install the components (mode 'exact' / 'certified' / 'frozen', or an explicit component list) on the frozen
|
| 1187 |
+
vendor modules. workers: window workers (default TLSQ_PRIOR_WORKERS, else CPUs); cpus: default detect_cpus();
|
| 1188 |
+
blas_threads='auto': this process's BLAS / numba / pool threads = CPUs (None leaves them alone)."""
|
| 1189 |
+
uninstall()
|
| 1190 |
+
bind(target)
|
| 1191 |
+
CFG.update(cfg)
|
| 1192 |
+
comps = resolve(mode, components)
|
| 1193 |
+
STATE["mode"] = mode or ("explicit" if components is not None else os.environ.get("TLSQ_PRIOR_MODE", "exact"))
|
| 1194 |
+
STATE["components"] = comps
|
| 1195 |
+
STATE["cpus"] = int(cpus) if cpus else detect_cpus()
|
| 1196 |
+
w = workers if workers is not None else os.environ.get("TLSQ_PRIOR_WORKERS")
|
| 1197 |
+
# default (A5): serial windows with all threads; forked windows only for certified (measured faster there)
|
| 1198 |
+
STATE["workers"] = max(1, int(w)) if w is not None else (STATE["cpus"] if "ritz" in comps else 1)
|
| 1199 |
+
if "parallel" not in comps:
|
| 1200 |
+
STATE["workers"] = 1
|
| 1201 |
+
if sys.platform == "darwin" and STATE["workers"] > 1: # Accelerate (BLAS) crashes in forked children: never fork on macOS
|
| 1202 |
+
STATE["workers"] = 1
|
| 1203 |
+
STATE["parallel_off"] = "darwin: the vendor's window pool forks and Accelerate is not fork-safe"
|
| 1204 |
+
STATE["gpu"] = "gpu" in comps
|
| 1205 |
+
_ORIG.update(window_solve=F._window_solve, loc_solve=FL._solve, micro_mom=F.micro_mom, lite_job=F._lite_job,
|
| 1206 |
+
micro_mom_loc=FL.micro_mom_loc, mom_solve_fast=P.mom_solve_fast, assemble=P._assemble,
|
| 1207 |
+
chol_solve=P._chol_solve, sym_solve=F._sym_solve, dense_max=P.DENSE_MAX, num_threads=P.NUM_THREADS)
|
| 1208 |
+
_ORIG["micro_mom_sig"] = inspect.signature(F.micro_mom)
|
| 1209 |
+
if blas_threads == "auto":
|
| 1210 |
+
_set_threads(STATE["cpus"])
|
| 1211 |
+
elif blas_threads:
|
| 1212 |
+
_set_threads(int(blas_threads))
|
| 1213 |
+
if "fv" in comps:
|
| 1214 |
+
F._window_solve = fv_window_solve
|
| 1215 |
+
F.micro_mom = _micro_mom
|
| 1216 |
+
F._lite_job = _lite_job
|
| 1217 |
+
_PARENT[0] = os.getpid()
|
| 1218 |
+
_CHILD[0] = False
|
| 1219 |
+
if "hmat_vec" in comps:
|
| 1220 |
+
_ORIG.update({"Kernel.__init__": P._Kernel.__init__, "Kernel.row": P._Kernel.row, "Kernel.block": P._Kernel.block, "aca": P._aca})
|
| 1221 |
+
P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = _hv_kernel_init, _hv_row, _hv_block, _hv_aca
|
| 1222 |
+
P.mom_solve_fast = _hv_mom_solve_fast
|
| 1223 |
+
if "compiled" in comps:
|
| 1224 |
+
P.mom_solve_fast = _mom_solve_fast
|
| 1225 |
+
if warm:
|
| 1226 |
+
warmup()
|
| 1227 |
+
if "gpu" in comps:
|
| 1228 |
+
P._assemble = _gpu_assemble
|
| 1229 |
+
P.DENSE_MAX = CFG["gpu_dense_max"]
|
| 1230 |
+
if "gpu" in comps or "mixed" in comps:
|
| 1231 |
+
P._chol_solve = _chol_solve
|
| 1232 |
+
F._sym_solve = _sym_solve
|
| 1233 |
+
if "ritz" in comps:
|
| 1234 |
+
F._window_solve = ritz_window_solve
|
| 1235 |
+
FL._solve = ritz_loc_solve
|
| 1236 |
+
FL.micro_mom_loc = _micro_mom_loc
|
| 1237 |
+
if warm:
|
| 1238 |
+
warmup()
|
| 1239 |
+
if value_patch is not None:
|
| 1240 |
+
_UNDO.append(value_patch(F, P, FL))
|
| 1241 |
+
STATE["value_patch"] = getattr(value_patch, "__name__", str(value_patch))
|
| 1242 |
+
return dict(STATE)
|
| 1243 |
+
|
| 1244 |
+
|
| 1245 |
+
def uninstall() -> None:
|
| 1246 |
+
for u in reversed(_UNDO):
|
| 1247 |
+
if callable(u):
|
| 1248 |
+
u()
|
| 1249 |
+
_UNDO.clear()
|
| 1250 |
+
if _ORIG:
|
| 1251 |
+
F._window_solve, FL._solve, F.micro_mom, F._lite_job = (_ORIG["window_solve"], _ORIG["loc_solve"], _ORIG["micro_mom"],
|
| 1252 |
+
_ORIG["lite_job"])
|
| 1253 |
+
FL.micro_mom_loc, P.mom_solve_fast, P._assemble = _ORIG["micro_mom_loc"], _ORIG["mom_solve_fast"], _ORIG["assemble"]
|
| 1254 |
+
P._chol_solve, F._sym_solve, P.DENSE_MAX = _ORIG["chol_solve"], _ORIG["sym_solve"], _ORIG["dense_max"]
|
| 1255 |
+
P.NUM_THREADS = _ORIG["num_threads"]
|
| 1256 |
+
if "aca" in _ORIG:
|
| 1257 |
+
P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = (_ORIG["Kernel.__init__"], _ORIG["Kernel.row"],
|
| 1258 |
+
_ORIG["Kernel.block"], _ORIG["aca"])
|
| 1259 |
+
_ORIG.clear()
|
| 1260 |
+
for tp in _TP:
|
| 1261 |
+
try:
|
| 1262 |
+
tp.restore_original_limits()
|
| 1263 |
+
except Exception: # noqa: BLE001
|
| 1264 |
+
pass
|
| 1265 |
+
_TP.clear()
|
| 1266 |
+
STATE.update(mode=None, components=(), workers=1, gpu=False, value_patch=None, parallel_off=None)
|
| 1267 |
+
|
| 1268 |
+
|
| 1269 |
+
@contextlib.contextmanager
|
| 1270 |
+
def installed(**kw):
|
| 1271 |
+
install(**kw)
|
| 1272 |
+
try:
|
| 1273 |
+
yield dict(STATE)
|
| 1274 |
+
finally:
|
| 1275 |
+
uninstall()
|
| 1276 |
+
|
| 1277 |
+
|
| 1278 |
+
def warmup() -> float:
|
| 1279 |
+
"""Compile / load the numba kernels (first call per process; cached on disk afterwards). Returns seconds."""
|
| 1280 |
+
t = time.perf_counter()
|
| 1281 |
+
rng = np.random.default_rng(0)
|
| 1282 |
+
c = rng.random((600, 2)) * 100
|
| 1283 |
+
H = HFar(c, leaf=64)
|
| 1284 |
+
H(np.ones((600, 2)))
|
| 1285 |
+
Y = np.empty((600, 1))
|
| 1286 |
+
_numba()["far"](c[:, 0].copy(), c[:, 1].copy(), np.ones((1, 600)), Y)
|
| 1287 |
+
return time.perf_counter() - t
|
| 1288 |
+
|
| 1289 |
+
|
| 1290 |
+
def status() -> dict:
|
| 1291 |
+
return dict(STATE, cfg=dict(CFG), mixed_gated=MIXED_GATED, frozen_dense_max=FROZEN_DENSE_MAX)
|
geomgnn/flagship.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Flagship Space tiers (deploy/registry.yaml: flagship_hub, flagship_tiers; deploy/GNN_SPACE.md "Flagship Space").
|
| 2 |
+
|
| 3 |
+
The tiers run through the paper Space's code path (geomgnn.paper: scene -> embedding -> priors under the tier's prior stack, or the
|
| 4 |
+
learned prior surrogate -> networks), unchanged. `view(reg)` is the registry seen with paper_arms / paper_hub replaced by
|
| 5 |
+
flagship_tiers / flagship_hub, so every geomgnn.paper function serves the tiers as arms.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import copy
|
| 11 |
+
|
| 12 |
+
from . import paper as PA
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def view(reg):
|
| 16 |
+
if reg.data.get("_flagship_view"):
|
| 17 |
+
return reg
|
| 18 |
+
v = copy.copy(reg)
|
| 19 |
+
v.data = dict(reg.data, paper_arms=reg.data["flagship_tiers"], paper_hub=reg.data["flagship_hub"], _flagship_view=True)
|
| 20 |
+
return v
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def tiers(reg) -> dict:
|
| 24 |
+
return view(reg)["paper_arms"]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def served(reg) -> list[str]:
|
| 28 |
+
v = view(reg)
|
| 29 |
+
return [k for k in PA.live(v) if PA.available(k, v)]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def predict(prepared, tier, reg, **kw) -> dict:
|
| 33 |
+
"""Full N x N Maxwell matrix (fF; ground row/column 0) of a prepared layout with a flagship tier (geomgnn.paper.predict)."""
|
| 34 |
+
v = view(reg)
|
| 35 |
+
res = PA.predict(prepared, tier, v, **kw)
|
| 36 |
+
t = v["paper_arms"][tier]
|
| 37 |
+
res.pop("paper", None)
|
| 38 |
+
res.update(tier=tier, tier_title=t["title"], reference=t.get("reference") or {}, b5=t.get("b5"), b5_physics=t.get("b5_physics"))
|
| 39 |
+
return res
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def preload(reg, example=None) -> dict:
|
| 43 |
+
return PA.preload(view(reg), example=example)
|
geomgnn/infer.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Label-free standalone prediction for geomfast checkpoints.
|
| 2 |
+
Only checkpoints, geometry, and optional priors are accepted; never Q3D labels.
|
| 3 |
+
"""
|
| 4 |
+
import os
|
| 5 |
+
os.environ.setdefault('PYTHONDONTWRITEBYTECODE','1')
|
| 6 |
+
import sys,importlib.util,time
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import numpy as np
|
| 9 |
+
import torch
|
| 10 |
+
W=Path(__file__).resolve().parent
|
| 11 |
+
from .pilot_p2 import build_model
|
| 12 |
+
from ._tlsq.uge.scene import scene_from_gds,tlsq_namer
|
| 13 |
+
from ._tlsq.uge.graph import scene_graph
|
| 14 |
+
from ._tlsq.uge.proxies import CapacitanceMoM
|
| 15 |
+
from ._tlsq.uge import localtok as lt
|
| 16 |
+
|
| 17 |
+
class Predictor:
|
| 18 |
+
def __init__(self,paths,threads=2):
|
| 19 |
+
torch.set_num_threads(threads);self.models=[];self.threads=threads
|
| 20 |
+
for path in paths:
|
| 21 |
+
c=torch.load(path,map_location='cpu',weights_only=False)
|
| 22 |
+
model=build_model(len(c['scales']['X']['median']),len(c['scales']['E']['median']),len(c['scales']['G']['median']),c['rounds']);model.load_state_dict(c['state']);model.eval();self.models.append((c,model))
|
| 23 |
+
assert len({c['arm'] for c,m in self.models})==1
|
| 24 |
+
self.arm=self.models[0][0]['arm'];self.direct=self.arm=='direct_gnn';self.fast=self.arm=='fast_gnn'
|
| 25 |
+
def from_features(self,row,priors=None):
|
| 26 |
+
n=len(row['names']);pairs=np.array([(i,j) for i in range(n) for j in range(i+1,n)]);i,j=pairs.T;assert len(row['edges'])==len(pairs),'complete graph required'
|
| 27 |
+
outputs=[]
|
| 28 |
+
for ck,model in self.models:
|
| 29 |
+
schema=ck.get('schema',ck);nk=schema['nodekeys'];ek=schema['edgekeys'];gk=schema.get('globkeys',['log_chip_area','log_pocket_area','log_substrate_t','eps_eff'])
|
| 30 |
+
X=np.array([[(float(not np.isfinite(nd['log_min_halfwidth'])) if k=='missing_log_min_halfwidth' else nd[k]) for k in nk] for nd in row['nodes']],np.float32)[None]
|
| 31 |
+
E=np.array([[row['edges'][tuple(p)][k] for k in ek] for p in pairs],np.float32)[None];G=np.array([[row['global'][k] for k in gk]],np.float32)
|
| 32 |
+
kinds=E[0,:,ek.index('is_ground_pair')].astype(int)
|
| 33 |
+
if self.arm in ['pair_mlp','mesh','lofo_pair']:X=X[:,:,[nk.index('role_ground')]];G=np.zeros((1,1),np.float32)
|
| 34 |
+
if self.direct:base=np.array([[ck['direct_intercepts'][str(k)] for k in kinds]],np.float32)
|
| 35 |
+
else:
|
| 36 |
+
p0,p1=(.75,1.5) if self.fast else (1.5,3.)
|
| 37 |
+
lo=np.log(priors[p0][i,j]);hi=np.log(priors[p1][i,j]);assert np.isfinite(lo).all() and np.isfinite(hi).all()
|
| 38 |
+
base=(2*hi-lo)[None].astype(np.float32);E=np.concatenate([E,(hi-lo)[None,:,None].astype(np.float32)],-1)
|
| 39 |
+
for name,z in [('X',X),('E',E),('G',G)]:
|
| 40 |
+
scale=ck['scales'][name];med=np.array(scale['median']);iqr=np.array(scale['iqr']);z[:]=np.clip((np.where(np.isfinite(z),z,med)-med)/iqr,-10,10)
|
| 41 |
+
assert np.isfinite(z).all()
|
| 42 |
+
closure=ck['closure_intercepts'];bbase=np.array([[closure.get('1',closure.get('True')) if nm!='G' else closure.get('0',closure.get('False')) for nm in row['names']]],np.float32)
|
| 43 |
+
T=lambda a:torch.from_numpy(np.asarray(a));M=torch.ones((1,n),dtype=torch.bool);PM=torch.ones((1,len(pairs)),dtype=torch.bool)
|
| 44 |
+
with torch.inference_mode():C,_=model(T(X),T(E),T(G),M,PM,T(pairs),T(base),T(bbase),self.direct)
|
| 45 |
+
outputs.append(C[0].numpy())
|
| 46 |
+
result=np.mean(outputs,axis=0);assert np.isfinite(result).all();return result
|
| 47 |
+
def from_gds(self,path,names=None,alpha=None,local_gap=None):
|
| 48 |
+
from ._tlsq.embed import proxy,fastprior
|
| 49 |
+
physics=self.models[0][0].get('schema',{}).get('physics',{})
|
| 50 |
+
if alpha is None:alpha=physics.get('alpha')
|
| 51 |
+
if local_gap is None:local_gap=physics.get('prior_method')=='local_gap'
|
| 52 |
+
proxy.NUM_THREADS=self.threads;start=time.perf_counter()
|
| 53 |
+
if names is None:namer=tlsq_namer
|
| 54 |
+
else:
|
| 55 |
+
def namer(S,m):
|
| 56 |
+
sig=[n for n in names if n!='G'];assert len(S)==len(sig),'signal polygons vs supplied file-order names';return sig
|
| 57 |
+
sc=scene_from_gds(path,namer=namer);t1=time.perf_counter();row=lt.add_tokens(sc,scene_graph(sc));t2=time.perf_counter();priors={}
|
| 58 |
+
if not self.direct:
|
| 59 |
+
old=fastprior.ALPHA_GAP_SHARE
|
| 60 |
+
try:
|
| 61 |
+
if alpha is not None:fastprior.ALPHA_GAP_SHARE=alpha
|
| 62 |
+
for p in ([.75,1.5] if self.fast else [1.5,3.]):
|
| 63 |
+
if local_gap:
|
| 64 |
+
from ._tlsq.embed.fastprior_loc import micro_mom_loc
|
| 65 |
+
with proxy.substrate('slab_open',sc.stack['substrate_thickness_um']):
|
| 66 |
+
mat,_,_=micro_mom_loc(sc.conductors[0].geom,[c.geom for c in sc.conductors[1:]],eps_r=sc.stack['substrate_eps_r'],ppg=p)
|
| 67 |
+
priors[p]=-mat
|
| 68 |
+
else:priors[p]=-CapacitanceMoM(max_cells=10**9,mesh='lite',cells_per_gap=p,substrate='slab_open').compute(sc,list(row['edges']))['matrix']
|
| 69 |
+
finally:fastprior.ALPHA_GAP_SHARE=old
|
| 70 |
+
t3=time.perf_counter();C=self.from_features(row,priors);t4=time.perf_counter()
|
| 71 |
+
return dict(C=C,names=row['names'],timing=dict(scene=t1-start,geometry=t2-t1,prior=t3-t2,network_and_pack=t4-t3,total=t4-start))
|
geomgnn/learned_prior.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Fast tier: the learned prior surrogate (agents/learnedprior, t100_norm) in place of the two field solves.
|
| 2 |
+
|
| 3 |
+
priors(model, prep, row) = the statements of scripts/learnedprior/lp_bench.py `Learned.from_gds` between the features and the
|
| 4 |
+
network: pack the scene-graph row with the P2d schema (scripts/learnedprior/lp_label.py `pack`), predict log P at 1.5 and 3
|
| 5 |
+
panels per gap with lp_surrogate.predict_bucket (one layout), and return the symmetric positive mutual matrices that
|
| 6 |
+
infer.Predictor.from_features reads. Self / closure terms are not inputs, so the diagonals stay zero.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
from . import lp_surrogate as S
|
| 14 |
+
|
| 15 |
+
PPG = (1.5, 3.0)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def load(path):
|
| 19 |
+
"""(model, prep, checkpoint) on CPU (lp_surrogate.load_model)."""
|
| 20 |
+
return S.load_model(str(path), "cpu")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def pack(row: dict) -> dict:
|
| 24 |
+
"""lp_label.pack: X (n, nodekeys), E (P, edgekeys), G (globkeys); pairs i < j in scene order."""
|
| 25 |
+
n = len(row["names"])
|
| 26 |
+
pairs = [(i, j) for i in range(n) for j in range(i + 1, n)]
|
| 27 |
+
nk = S.SCHEMA["nodekeys"]
|
| 28 |
+
X = np.array([[(float(not np.isfinite(nd["log_min_halfwidth"])) if k == "missing_log_min_halfwidth" else nd[k]) for k in nk]
|
| 29 |
+
for nd in row["nodes"]], np.float32)
|
| 30 |
+
E = np.array([[row["edges"][p][k] for k in S.SCHEMA["edgekeys"]] for p in pairs], np.float32).reshape(len(pairs), len(S.SCHEMA["edgekeys"]))
|
| 31 |
+
Gv = np.array([row["global"][k] for k in S.SCHEMA["globkeys"]], np.float32)
|
| 32 |
+
return dict(X=X, E=E, G=Gv, names=row["names"])
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def priors(model, prep, row: dict) -> dict:
|
| 36 |
+
z = pack(row)
|
| 37 |
+
n = len(z["names"])
|
| 38 |
+
b = dict(n=n, X=z["X"][None], E=z["E"][None], G=z["G"][None])
|
| 39 |
+
lp = S.predict_bucket(model, prep, b, "cpu")[0]
|
| 40 |
+
iu = np.triu_indices(n, 1)
|
| 41 |
+
out = {}
|
| 42 |
+
for k, p in enumerate(PPG):
|
| 43 |
+
M = np.zeros((n, n))
|
| 44 |
+
M[iu] = np.exp(lp[:, k])
|
| 45 |
+
out[p] = M + M.T
|
| 46 |
+
return out
|
geomgnn/lp_surrogate.py
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""learnedprior surrogate (PROTOCOL.md section 3): tri-GNN on the P2d scene-graph features -> (base, delta) per pair.
|
| 2 |
+
|
| 3 |
+
python lp_surrogate.py train --p2d DIR [--gen DIR] --out DIR [--steps N] [--raw] [--gen-frac F] [--seed S]
|
| 4 |
+
python lp_surrogate.py predict --ckpt FILE --data DIR --kind p2d|gen --out FILE.npz
|
| 5 |
+
|
| 6 |
+
Data buckets are homogeneous in conductor count n (no padding). Pairs i < j in the stored order.
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
os.environ.setdefault("PYTHONDONTWRITEBYTECODE", "1")
|
| 13 |
+
import argparse
|
| 14 |
+
import glob
|
| 15 |
+
import hashlib
|
| 16 |
+
import json
|
| 17 |
+
import platform
|
| 18 |
+
import time
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
import torch
|
| 23 |
+
from torch import nn
|
| 24 |
+
from torch.nn import functional as F
|
| 25 |
+
|
| 26 |
+
HERE = Path(__file__).resolve().parent
|
| 27 |
+
SCHEMA = json.loads((HERE / "schema_p2d.json").read_text())
|
| 28 |
+
NK, EK, GK = SCHEMA["nodekeys"], SCHEMA["edgekeys"], SCHEMA["globkeys"]
|
| 29 |
+
NODE_DIM = {"log_area": 2, "log_perimeter": 1, "log_min_halfwidth": 1, "log_width": 1}
|
| 30 |
+
EDGE_DIM = {"log_dmin": 1, "log_cps": 1, "mom_0.5": 0.5, "mom_1": 0.0, "mom_2": -1.0}
|
| 31 |
+
EDGE_DIM.update({f"lg_{k}": v for k, v in list(EDGE_DIM.items())})
|
| 32 |
+
GLOB_DIM = {"log_chip_area": 2, "log_pocket_area": 2, "log_substrate_t": 1}
|
| 33 |
+
GROUPS = ("geomfast", "base", "large", "scaled", "near")
|
| 34 |
+
T_GROUP = {"t1_near": "near", "t2_large": "large", "t3_scaled": "scaled"}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# ----------------------------------------------------------------------------------------------------------- data
|
| 38 |
+
def load_p2d(root) -> list:
|
| 39 |
+
out = []
|
| 40 |
+
for p in sorted(glob.glob(f"{root}/n*.npz")):
|
| 41 |
+
d = np.load(p, allow_pickle=False)
|
| 42 |
+
out.append(dict(n=d["X"].shape[1], X=d["X"], E=d["E"][:, :, :len(EK)], G=d["G"], pri=d["pri"].astype(np.float64), key=d["key"],
|
| 43 |
+
split=d["split"], family=d["family"], group=np.full(len(d["key"]), "geomfast"), scale=np.ones(len(d["key"]))))
|
| 44 |
+
return out
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def load_gen(roots) -> list:
|
| 48 |
+
"""roots: one packed dir or several separated by "+" (v1 corpus + targeted corpus; no commas: sbatch --export splits on them)."""
|
| 49 |
+
out = []
|
| 50 |
+
for p in sorted(f for root in str(roots).replace(",", "+").split("+") for f in glob.glob(f"{root}/n*.npz")):
|
| 51 |
+
d = np.load(p, allow_pickle=False)
|
| 52 |
+
grp = np.array([T_GROUP.get(str(s), str(s)) for s in d["stratum"]])
|
| 53 |
+
out.append(dict(n=d["X"].shape[1], X=d["X"], E=d["E"], G=d["G"], pri=d["pri"].astype(np.float64), key=d["key"], split=d["split"],
|
| 54 |
+
family=np.char.add("gen_", d["stratum"].astype(str)), group=grp, scale=d["scale"]))
|
| 55 |
+
return out
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def merge_buckets(bs: list) -> list:
|
| 59 |
+
by = {}
|
| 60 |
+
for b in bs:
|
| 61 |
+
by.setdefault(b["n"], []).append(b)
|
| 62 |
+
return [{k: (v[0][k] if k == "n" else np.concatenate([x[k] for x in v])) for k in v[0]} for n, v in sorted(by.items())]
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def subset(b: dict, m: np.ndarray) -> dict:
|
| 66 |
+
return {k: (v if k == "n" else v[m]) for k, v in b.items()}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def log_L(X: np.ndarray) -> np.ndarray:
|
| 70 |
+
"""(B,) log characteristic length: mean over signal nodes of 0.5 log_area."""
|
| 71 |
+
sig = X[:, :, NK.index("role_ground")] < 0.5
|
| 72 |
+
la = X[:, :, NK.index("log_area")]
|
| 73 |
+
return 0.5 * (la * sig).sum(1) / sig.sum(1).clip(1)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class Prep:
|
| 77 |
+
"""Scale shift (optional) + robust scaling; TRAIN statistics only."""
|
| 78 |
+
|
| 79 |
+
def __init__(self, raw: bool = False):
|
| 80 |
+
self.raw = raw
|
| 81 |
+
self.nshift = np.array([NODE_DIM.get(k, 0.0) for k in NK])
|
| 82 |
+
self.eshift = np.array([EDGE_DIM.get(k, 0.0) for k in EK])
|
| 83 |
+
self.gshift = np.array([GLOB_DIM.get(k, 0.0) for k in GK])
|
| 84 |
+
|
| 85 |
+
def features(self, b: dict):
|
| 86 |
+
X, E, G = b["X"].astype(np.float64), b["E"].astype(np.float64), b["G"].astype(np.float64)
|
| 87 |
+
lL = log_L(b["X"]) if not self.raw else np.zeros(len(X))
|
| 88 |
+
if not self.raw:
|
| 89 |
+
X = X - self.nshift * lL[:, None, None]
|
| 90 |
+
E = E - self.eshift * lL[:, None, None]
|
| 91 |
+
G = G - self.gshift * lL[:, None]
|
| 92 |
+
G = np.concatenate([G, lL[:, None]], 1)
|
| 93 |
+
return X, E, G, lL
|
| 94 |
+
|
| 95 |
+
def targets(self, b: dict, lL: np.ndarray):
|
| 96 |
+
y = np.log(b["pri"]) - lL[:, None, None]
|
| 97 |
+
return np.stack([2 * y[..., 1] - y[..., 0], y[..., 1] - y[..., 0]], -1)
|
| 98 |
+
|
| 99 |
+
def fit(self, buckets: list):
|
| 100 |
+
acc = {k: [] for k in "XEGT"}
|
| 101 |
+
rng = np.random.default_rng(0)
|
| 102 |
+
for b in buckets:
|
| 103 |
+
tr = np.flatnonzero(b["split"] == "train")
|
| 104 |
+
if not len(tr):
|
| 105 |
+
continue
|
| 106 |
+
tr = rng.choice(tr, min(len(tr), 3000), replace=False)
|
| 107 |
+
X, E, G, lL = self.features(subset(b, np.isin(np.arange(len(b["key"])), tr)))
|
| 108 |
+
acc["X"].append(X.reshape(-1, X.shape[-1]))
|
| 109 |
+
acc["E"].append(E.reshape(-1, E.shape[-1]))
|
| 110 |
+
acc["G"].append(G)
|
| 111 |
+
acc["T"].append(self.targets(subset(b, np.isin(np.arange(len(b["key"])), tr)), lL).reshape(-1, 2))
|
| 112 |
+
self.stats = {}
|
| 113 |
+
for k in "XEG":
|
| 114 |
+
v = np.concatenate(acc[k])
|
| 115 |
+
med = np.nanmedian(v, 0)
|
| 116 |
+
iq = np.nanpercentile(v, 75, 0) - np.nanpercentile(v, 25, 0)
|
| 117 |
+
self.stats[k] = (med, np.where(iq > 1e-6, iq, 1.0))
|
| 118 |
+
t = np.concatenate(acc["T"])
|
| 119 |
+
self.stats["T"] = (t.mean(0), t.std(0))
|
| 120 |
+
return self
|
| 121 |
+
|
| 122 |
+
def apply(self, b: dict):
|
| 123 |
+
X, E, G, lL = self.features(b)
|
| 124 |
+
out = []
|
| 125 |
+
for k, z in (("X", X), ("E", E), ("G", G)):
|
| 126 |
+
med, iq = self.stats[k]
|
| 127 |
+
out.append(np.clip((np.where(np.isfinite(z), z, med) - med) / iq, -10, 10).astype(np.float32))
|
| 128 |
+
return (*out, lL)
|
| 129 |
+
|
| 130 |
+
def state(self):
|
| 131 |
+
return dict(raw=self.raw, stats={k: [np.asarray(x).tolist() for x in v] for k, v in self.stats.items()})
|
| 132 |
+
|
| 133 |
+
@classmethod
|
| 134 |
+
def from_state(cls, s):
|
| 135 |
+
p = cls(s["raw"])
|
| 136 |
+
p.stats = {k: tuple(np.array(x) for x in v) for k, v in s["stats"].items()}
|
| 137 |
+
return p
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# ---------------------------------------------------------------------------------------------------------- model
|
| 141 |
+
def mlp(i, o, w):
|
| 142 |
+
return nn.Sequential(nn.Linear(i, w), nn.SiLU(), nn.Linear(w, o))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
class Round(nn.Module):
|
| 146 |
+
def __init__(self, w):
|
| 147 |
+
super().__init__()
|
| 148 |
+
self.me, self.mh = nn.Linear(w, w), nn.Linear(w, w)
|
| 149 |
+
self.node = mlp(3 * w, w, w)
|
| 150 |
+
self.edge = mlp(3 * w, w, w)
|
| 151 |
+
self.ln_t = nn.LayerNorm(w)
|
| 152 |
+
self.ta, self.tb, self.tg = nn.Linear(w, w), nn.Linear(w, w), nn.Linear(w, w)
|
| 153 |
+
self.tout = nn.Sequential(nn.LayerNorm(w), nn.Linear(w, w))
|
| 154 |
+
self.ln_h, self.ln_e = nn.LayerNorm(w), nn.LayerNorm(w)
|
| 155 |
+
|
| 156 |
+
def forward(self, h, e, off):
|
| 157 |
+
# h (B,N,w), e (B,N,N,w) symmetric, off (N,N,1) off-diagonal mask
|
| 158 |
+
N = h.shape[1]
|
| 159 |
+
m = self.me(e) * self.mh(h)[:, None, :, :] * off # message j -> i
|
| 160 |
+
mean = m.sum(2) / max(N - 1, 1)
|
| 161 |
+
mx = (m + (off - 1) * 1e4).amax(2) if N > 1 else torch.zeros_like(mean)
|
| 162 |
+
h = h + self.node(torch.cat([self.ln_h(h), mean, mx], -1))
|
| 163 |
+
hs, hd = h[:, :, None] + h[:, None], (h[:, :, None] - h[:, None]).abs()
|
| 164 |
+
e = e + self.edge(torch.cat([self.ln_e(e), hs, hd], -1)) * off
|
| 165 |
+
z = self.ln_t(e)
|
| 166 |
+
a, b = self.ta(z) * off, self.tb(z) * off
|
| 167 |
+
t = torch.einsum("bikc,bjkc->bijc", a, b) / max(N - 2, 1)
|
| 168 |
+
t = 0.5 * (t + t.transpose(1, 2))
|
| 169 |
+
e = e + torch.sigmoid(self.tg(z)) * self.tout(t) * off
|
| 170 |
+
return h, e
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
class TriGNN(nn.Module):
|
| 174 |
+
def __init__(self, dx, de, dg, w=128, rounds=4):
|
| 175 |
+
super().__init__()
|
| 176 |
+
self.node_in, self.edge_in = mlp(dx + dg, w, w), mlp(de, w, w)
|
| 177 |
+
self.rounds = nn.ModuleList([Round(w) for _ in range(rounds)])
|
| 178 |
+
self.head = mlp(3 * w, 2, w)
|
| 179 |
+
|
| 180 |
+
def forward(self, X, Eu, G, iu):
|
| 181 |
+
"""X (B,N,dx), Eu (B,P,de) upper-triangle pairs iu=(i,j), G (B,dg) -> (B,P,2) standardised (b, d)."""
|
| 182 |
+
B, N, _ = X.shape
|
| 183 |
+
i, j = iu
|
| 184 |
+
h = self.node_in(torch.cat([X, G[:, None].expand(-1, N, -1)], -1))
|
| 185 |
+
eu = self.edge_in(Eu)
|
| 186 |
+
e = torch.zeros(B, N, N, eu.shape[-1], device=X.device, dtype=eu.dtype)
|
| 187 |
+
e[:, i, j] = eu
|
| 188 |
+
e[:, j, i] = eu
|
| 189 |
+
off = (1 - torch.eye(N, device=X.device, dtype=eu.dtype))[:, :, None]
|
| 190 |
+
for r in self.rounds:
|
| 191 |
+
h, e = r(h, e, off)
|
| 192 |
+
return self.head(torch.cat([e[:, i, j], h[:, i] + h[:, j], (h[:, i] - h[:, j]).abs()], -1))
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def build(dx, de, dg, w=128, rounds=4):
|
| 196 |
+
return TriGNN(dx, de, dg, w, rounds)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# ------------------------------------------------------------------------------------------------------- training
|
| 200 |
+
def pairs_of(n):
|
| 201 |
+
return np.array([(i, j) for i in range(n) for j in range(i + 1, n)]).T.reshape(2, -1)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def predict_bucket(model, prep, b, device, max_pairs=2**17):
|
| 205 |
+
"""(B,P,2) log P (ppg 1.5, 3) predicted for every layout of bucket b."""
|
| 206 |
+
n = b["n"]
|
| 207 |
+
iu = torch.as_tensor(pairs_of(n), device=device)
|
| 208 |
+
X, E, G, lL = prep.apply(b)
|
| 209 |
+
mu, sd = prep.stats["T"]
|
| 210 |
+
bs = max(1, max_pairs // (n * n))
|
| 211 |
+
out = []
|
| 212 |
+
model.eval()
|
| 213 |
+
with torch.inference_mode():
|
| 214 |
+
for s in range(0, len(X), bs):
|
| 215 |
+
T = lambda a: torch.as_tensor(a[s:s + bs], device=device) # noqa: E731
|
| 216 |
+
z = model(T(X), T(E), T(G), iu).double().cpu().numpy() * sd + mu
|
| 217 |
+
out.append(z)
|
| 218 |
+
z = np.concatenate(out)
|
| 219 |
+
bb, dd = z[..., 0], z[..., 1]
|
| 220 |
+
y3, y1 = bb - dd, bb - 2 * dd
|
| 221 |
+
return np.stack([y1, y3], -1) + lL[:, None, None]
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def fidelity(logp_hat, pri):
|
| 225 |
+
"""Per-entry C0 errors (C0 = exp(2 log P3 - log P1.5)): |rel| with 0.1 fF floor, abs fF; |delta error|."""
|
| 226 |
+
c0 = np.exp(2 * np.log(pri[..., 1]) - np.log(pri[..., 0]))
|
| 227 |
+
c0h = np.exp(2 * logp_hat[..., 1] - logp_hat[..., 0])
|
| 228 |
+
d = np.log(pri[..., 1] / pri[..., 0])
|
| 229 |
+
dh = logp_hat[..., 1] - logp_hat[..., 0]
|
| 230 |
+
err = np.abs(c0h - c0)
|
| 231 |
+
return err / np.maximum(c0, 0.1), err, np.abs(dh - d), c0
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def summarize(rel, err):
|
| 235 |
+
rel, err = np.ravel(rel), np.ravel(err)
|
| 236 |
+
return dict(entries=int(len(rel)), median_percent=float(np.median(rel) * 100), p95_percent=float(np.percentile(rel, 95) * 100),
|
| 237 |
+
mae_fF=float(err.mean()), rmse_fF=float(np.sqrt((err ** 2).mean())))
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def cmd_train(a):
|
| 241 |
+
torch.manual_seed(a.seed)
|
| 242 |
+
rng = np.random.default_rng(a.seed)
|
| 243 |
+
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 244 |
+
out = Path(a.out)
|
| 245 |
+
out.mkdir(parents=True, exist_ok=False)
|
| 246 |
+
buckets = load_p2d(a.p2d)
|
| 247 |
+
if a.p2d_holdout_fold is not None: # out-of-fold surrogate for geomfast TRAIN (PROTOCOL amendment 2026-10-17)
|
| 248 |
+
for b in buckets:
|
| 249 |
+
f = np.array([int(hashlib.sha256(str(k).encode()).hexdigest(), 16) % a.p2d_nfolds for k in b["key"]])
|
| 250 |
+
b.update(subset(b, ~((b["split"] == "train") & (f == a.p2d_holdout_fold))))
|
| 251 |
+
if a.gen:
|
| 252 |
+
g = load_gen(a.gen)
|
| 253 |
+
if a.gen_frac < 1: # nested subsets by parent index hash (learning curve)
|
| 254 |
+
for b in g:
|
| 255 |
+
parent = np.array(["/".join(k.split("/")[:2]) for k in b["key"]])
|
| 256 |
+
u = np.array([int(hashlib.sha256(p.encode()).hexdigest(), 16) % 10**6 / 1e6 for p in parent])
|
| 257 |
+
keep = (b["split"] != "train") | (u < a.gen_frac)
|
| 258 |
+
b.update(subset(b, keep))
|
| 259 |
+
buckets += g
|
| 260 |
+
buckets = [b for b in merge_buckets(buckets) if b["n"] >= 2]
|
| 261 |
+
prep = Prep(a.raw).fit(buckets)
|
| 262 |
+
data = []
|
| 263 |
+
for b in buckets:
|
| 264 |
+
X, E, G, lL = prep.apply(b)
|
| 265 |
+
Y = ((prep.targets(b, lL) - prep.stats["T"][0]) / prep.stats["T"][1]).astype(np.float32)
|
| 266 |
+
data.append(dict(n=b["n"], X=torch.as_tensor(X), E=torch.as_tensor(E), G=torch.as_tensor(G), Y=torch.as_tensor(Y),
|
| 267 |
+
split=b["split"], group=b["group"], iu=torch.as_tensor(pairs_of(b["n"]), device=dev)))
|
| 268 |
+
index = {g: [] for g in GROUPS}
|
| 269 |
+
for k, d in enumerate(data):
|
| 270 |
+
for g in GROUPS:
|
| 271 |
+
idx = np.flatnonzero((d["split"] == "train") & (d["group"] == g))
|
| 272 |
+
if len(idx):
|
| 273 |
+
index[g].append((k, idx))
|
| 274 |
+
groups = [g for g in GROUPS if index[g]]
|
| 275 |
+
counts = {g: int(sum(len(i) for _, i in index[g])) for g in groups}
|
| 276 |
+
val = [(k, np.flatnonzero(d["split"] == "val")) for k, d in enumerate(data)]
|
| 277 |
+
model = build(data[0]["X"].shape[-1], data[0]["E"].shape[-1], data[0]["G"].shape[-1], a.width, a.rounds).to(dev)
|
| 278 |
+
opt = torch.optim.AdamW(model.parameters(), lr=a.lr, weight_decay=1e-4)
|
| 279 |
+
sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=a.lr, total_steps=a.steps, pct_start=0.03, anneal_strategy="cos")
|
| 280 |
+
sd = torch.as_tensor(prep.stats["T"][1], device=dev, dtype=torch.float32)
|
| 281 |
+
meta = dict(args=vars(a), host=platform.node(), device=str(dev), gpu=torch.cuda.get_device_name(0) if dev.type == "cuda" else None,
|
| 282 |
+
train_counts=counts, val_layouts=int(sum(len(i) for _, i in val)), params=sum(p.numel() for p in model.parameters()))
|
| 283 |
+
(out / "run.json").write_text(json.dumps(meta, indent=1))
|
| 284 |
+
print(json.dumps(meta), flush=True)
|
| 285 |
+
|
| 286 |
+
def batch_loss(k, idx):
|
| 287 |
+
d = data[k]
|
| 288 |
+
T = lambda z: z[idx].to(dev, non_blocking=True) # noqa: E731
|
| 289 |
+
z = model(T(d["X"]), T(d["E"]), T(d["G"]), d["iu"]) * sd
|
| 290 |
+
y = T(d["Y"]) * sd
|
| 291 |
+
return (F.huber_loss(z[..., 0], y[..., 0], delta=0.05, reduction="none") + F.huber_loss(z[..., 1], y[..., 1], delta=0.05, reduction="none")).mean(1)
|
| 292 |
+
|
| 293 |
+
def evaluate(maxn=600):
|
| 294 |
+
model.eval()
|
| 295 |
+
res = {}
|
| 296 |
+
with torch.inference_mode():
|
| 297 |
+
for k, idx in val:
|
| 298 |
+
if not len(idx):
|
| 299 |
+
continue
|
| 300 |
+
d = data[k]
|
| 301 |
+
bs = max(1, a.pair_budget // (d["n"] ** 2))
|
| 302 |
+
for s in range(0, min(len(idx), maxn), bs):
|
| 303 |
+
l = batch_loss(k, torch.as_tensor(idx[s:s + bs]))
|
| 304 |
+
for gname in set(d["group"][idx[s:s + bs]]):
|
| 305 |
+
m = d["group"][idx[s:s + bs]] == gname
|
| 306 |
+
res.setdefault(gname, []).extend(l[torch.as_tensor(m, device=dev)].tolist())
|
| 307 |
+
model.train()
|
| 308 |
+
return {g: float(np.mean(v)) for g, v in res.items()}
|
| 309 |
+
|
| 310 |
+
hist, t0, skipped = [], time.perf_counter(), 0
|
| 311 |
+
for step in range(a.steps):
|
| 312 |
+
g = groups[rng.integers(len(groups))]
|
| 313 |
+
w = np.array([len(i) for _, i in index[g]], float)
|
| 314 |
+
k, idx = index[g][rng.choice(len(w), p=w / w.sum())]
|
| 315 |
+
n = data[k]["n"]
|
| 316 |
+
bs = int(max(1, min(a.max_batch, a.pair_budget // (n * n))))
|
| 317 |
+
b = torch.as_tensor(rng.choice(idx, bs))
|
| 318 |
+
loss = batch_loss(k, b).mean()
|
| 319 |
+
if not torch.isfinite(loss): # skipped and counted (PROTOCOL amendment 2026-10-17); abort if frequent
|
| 320 |
+
skipped += 1
|
| 321 |
+
print(json.dumps(dict(step=step, skipped_nonfinite=skipped, n=int(n), batch=bs)), flush=True)
|
| 322 |
+
if skipped > 100:
|
| 323 |
+
raise RuntimeError(f"non-finite loss at step {step} ({skipped} skipped)")
|
| 324 |
+
opt.zero_grad(set_to_none=True)
|
| 325 |
+
sched.step()
|
| 326 |
+
continue
|
| 327 |
+
opt.zero_grad(set_to_none=True)
|
| 328 |
+
loss.backward()
|
| 329 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
|
| 330 |
+
opt.step()
|
| 331 |
+
sched.step()
|
| 332 |
+
if step % a.eval_every == 0 or step == a.steps - 1:
|
| 333 |
+
ev = evaluate()
|
| 334 |
+
hist.append(dict(step=step, loss=float(loss), seconds=time.perf_counter() - t0, val=ev))
|
| 335 |
+
print(json.dumps(hist[-1]), flush=True)
|
| 336 |
+
torch.save(dict(state=model.state_dict(), prep=prep.state(), width=a.width, rounds=a.rounds, dims=[data[0]["X"].shape[-1],
|
| 337 |
+
data[0]["E"].shape[-1], data[0]["G"].shape[-1]], step=step, history=hist), out / "model.pt")
|
| 338 |
+
meta.update(train_seconds=time.perf_counter() - t0, history=hist, skipped_nonfinite=skipped)
|
| 339 |
+
(out / "run.json").write_text(json.dumps(meta, indent=1))
|
| 340 |
+
print("TRAIN_DONE", time.perf_counter() - t0, flush=True)
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def load_model(path, device="cpu"):
|
| 344 |
+
c = torch.load(path, map_location=device, weights_only=False)
|
| 345 |
+
m = build(*c["dims"], c["width"], c["rounds"]).to(device)
|
| 346 |
+
m.load_state_dict(c["state"])
|
| 347 |
+
m.eval()
|
| 348 |
+
return m, Prep.from_state(c["prep"]), c
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def cmd_predict(a):
|
| 352 |
+
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 353 |
+
model, prep, _ = load_model(a.ckpt, dev)
|
| 354 |
+
buckets = load_p2d(a.data) if a.kind == "p2d" else load_gen(a.data)
|
| 355 |
+
keys, logp = [], {}
|
| 356 |
+
for b in merge_buckets(buckets):
|
| 357 |
+
if b["n"] < 2:
|
| 358 |
+
continue
|
| 359 |
+
sel = b["split"] == "val" if a.split == "val" else np.ones(len(b["key"]), bool)
|
| 360 |
+
b = subset(b, sel)
|
| 361 |
+
if not len(b["key"]):
|
| 362 |
+
continue
|
| 363 |
+
lp = predict_bucket(model, prep, b, dev)
|
| 364 |
+
for k, v in zip(b["key"], lp):
|
| 365 |
+
logp[str(k)] = v.astype(np.float64)
|
| 366 |
+
np.savez_compressed(a.out, keys=np.array(list(logp)), **{f"k{i}": v for i, v in enumerate(logp.values())})
|
| 367 |
+
print("PREDICTED", len(logp))
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
if __name__ == "__main__":
|
| 371 |
+
ap = argparse.ArgumentParser()
|
| 372 |
+
sp = ap.add_subparsers(dest="cmd", required=True)
|
| 373 |
+
p = sp.add_parser("train")
|
| 374 |
+
p.add_argument("--p2d", required=True)
|
| 375 |
+
p.add_argument("--gen")
|
| 376 |
+
p.add_argument("--gen-frac", type=float, default=1.0)
|
| 377 |
+
p.add_argument("--out", required=True)
|
| 378 |
+
p.add_argument("--raw", action="store_true")
|
| 379 |
+
p.add_argument("--steps", type=int, default=20000)
|
| 380 |
+
p.add_argument("--lr", type=float, default=1e-3)
|
| 381 |
+
p.add_argument("--width", type=int, default=128)
|
| 382 |
+
p.add_argument("--rounds", type=int, default=4)
|
| 383 |
+
p.add_argument("--pair-budget", type=int, default=2**16)
|
| 384 |
+
p.add_argument("--max-batch", type=int, default=256)
|
| 385 |
+
p.add_argument("--eval-every", type=int, default=1000)
|
| 386 |
+
p.add_argument("--seed", type=int, default=0)
|
| 387 |
+
p.add_argument("--p2d-holdout-fold", type=int)
|
| 388 |
+
p.add_argument("--p2d-nfolds", type=int, default=2)
|
| 389 |
+
p = sp.add_parser("predict")
|
| 390 |
+
p.add_argument("--ckpt", required=True)
|
| 391 |
+
p.add_argument("--data", required=True)
|
| 392 |
+
p.add_argument("--kind", choices=["p2d", "gen"], default="p2d")
|
| 393 |
+
p.add_argument("--split", choices=["val", "all"], default="val")
|
| 394 |
+
p.add_argument("--out", required=True)
|
| 395 |
+
a = ap.parse_args()
|
| 396 |
+
{"train": cmd_train, "predict": cmd_predict}[a.cmd](a)
|
geomgnn/paper.py
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Paper Space arms (deploy/registry.yaml: paper_hub, paper_arms; deploy/GNN_SPACE.md "Paper Space").
|
| 2 |
+
|
| 3 |
+
Every arm of the paper's ladder and routes tables that runs from a layout file on a CPU, through one code path:
|
| 4 |
+
scene (geomgnn.api.scene_from_prepared) -> geometric embedding -> priors at the arm's mesh levels under the arm's prior stack
|
| 5 |
+
-> the arm's combination. Kinds:
|
| 6 |
+
gnn geomfast networks (infer.Predictor): full_gnn / direct_gnn / full_mlp / pair_mlp checkpoints; the two levels are
|
| 7 |
+
fed as the networks' (1.5, 3) inputs (the coarser-pair arm reads R(0.75, 1.5) there, as it was trained);
|
| 8 |
+
learned_prior: the fast model's surrogate replaces the solves
|
| 9 |
+
multifid cheap-physics networks (x1_core.Multifid, scripts/multifid/train_mf.py); learned-exponent arms (curve) use the
|
| 10 |
+
train_mf.fit anchor (finest level) and curve=True
|
| 11 |
+
physics no learned parameter: one level (to_maxwell) or a Richardson pair (x1_core.c0)
|
| 12 |
+
final the prior-generation model and its fast mode (tlsq_infer.final, unchanged)
|
| 13 |
+
Prior stacks (paper_hub.prior_stacks) are process-wide (geomgnn.api.set_prior_stack); each request installs its arm's stack.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import hashlib
|
| 19 |
+
import json
|
| 20 |
+
import os
|
| 21 |
+
import time
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
import numpy as np
|
| 25 |
+
|
| 26 |
+
from . import __version__
|
| 27 |
+
from . import api as A
|
| 28 |
+
from . import infer as I
|
| 29 |
+
from . import x1_core as X
|
| 30 |
+
from ._tlsq.embed import fastprior, proxy
|
| 31 |
+
from ._tlsq.uge.graph import scene_graph
|
| 32 |
+
|
| 33 |
+
lt = I.lt
|
| 34 |
+
ALPHA = 0.49685219816506904 # TRAIN-fitted gap share of every paper prior (P2d physics.alpha)
|
| 35 |
+
_CACHE: dict = {}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def arms(reg) -> dict:
|
| 39 |
+
return reg.data.get("paper_arms") or {}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def live(reg) -> list[str]:
|
| 43 |
+
return [k for k, v in arms(reg).items() if v.get("status", "available") == "available"]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _sha(p) -> str:
|
| 47 |
+
return hashlib.sha256(Path(p).read_bytes()).hexdigest()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _model_dir(model_dir=None):
|
| 51 |
+
return os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def files(reg, name, key="checkpoints", model_dir=None) -> list[Path]:
|
| 55 |
+
"""Bundle copies (<model_dir>/<name>/info.json) or the registry's source paths."""
|
| 56 |
+
a = arms(reg)[name]
|
| 57 |
+
md = _model_dir(model_dir)
|
| 58 |
+
info = Path(md) / name / "info.json" if md else None
|
| 59 |
+
if info is not None and info.exists():
|
| 60 |
+
return [Path(md) / name / f for f in json.loads(info.read_text()).get(key, [])]
|
| 61 |
+
src = a.get(key) or []
|
| 62 |
+
if key == "surrogate":
|
| 63 |
+
src = [a["learned_prior"]["checkpoint"]] if a.get("learned_prior") else []
|
| 64 |
+
return [reg.root / c["path"] for c in src]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def _hashes(a, key):
|
| 68 |
+
if key == "surrogate":
|
| 69 |
+
return [a["learned_prior"]["checkpoint"]["sha256"]] if a.get("learned_prior") else []
|
| 70 |
+
return [c["sha256"] for c in a.get(key) or []]
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def available(name, reg, model_dir=None) -> bool:
|
| 74 |
+
a = arms(reg)[name]
|
| 75 |
+
if a["kind"] == "final":
|
| 76 |
+
from tlsq_infer import final as F
|
| 77 |
+
return F.available(a["final_model"], reg)
|
| 78 |
+
return all(f.exists() for k in ("checkpoints", "surrogate") for f in files(reg, name, k, model_dir))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def stack_of(reg, a) -> dict | None:
|
| 82 |
+
s = a.get("stack")
|
| 83 |
+
return None if s is None else reg["paper_hub"]["prior_stacks"][s]
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def _check(reg, name, model_dir=None):
|
| 87 |
+
a = arms(reg)[name]
|
| 88 |
+
for key in ("checkpoints", "surrogate"):
|
| 89 |
+
fs, hs = files(reg, name, key, model_dir), _hashes(a, key)
|
| 90 |
+
assert len(fs) == len(hs), (name, key)
|
| 91 |
+
for f, h in zip(fs, hs):
|
| 92 |
+
if _sha(f) != h:
|
| 93 |
+
raise RuntimeError(f"{name}: {f.name} SHA256 differs from the registry ({h[:12]})")
|
| 94 |
+
return a
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class _Curve:
|
| 98 |
+
"""Learned-exponent cheap-physics arms (train_mf.fit with curve=True): anchor = log of the finest level, slope = finest -
|
| 99 |
+
next level, the head's exponent p sets m = anchor + slope / (2^p - 1); otherwise x1_core.Multifid.predict_arrays."""
|
| 100 |
+
|
| 101 |
+
def __init__(self, paths, threads=2):
|
| 102 |
+
import torch
|
| 103 |
+
|
| 104 |
+
from .train_mf import build
|
| 105 |
+
torch.set_num_threads(threads)
|
| 106 |
+
self.models = []
|
| 107 |
+
for p in paths:
|
| 108 |
+
c = torch.load(p, map_location="cpu", weights_only=False)
|
| 109 |
+
assert c["curve"]
|
| 110 |
+
m = build(len(c["scales"]["X"]["median"]), len(c["scales"]["E"]["median"]), len(c["scales"]["G"]["median"]), 2)
|
| 111 |
+
m.load_state_dict(c["state"])
|
| 112 |
+
m.eval()
|
| 113 |
+
self.models.append((c, m))
|
| 114 |
+
self.levels = self.models[0][0]["levels"]
|
| 115 |
+
assert all(c["levels"] == self.levels for c, _ in self.models)
|
| 116 |
+
|
| 117 |
+
def predict(self, row, levels):
|
| 118 |
+
nk, ek, gk = X.schema_of(self.models[0][0])
|
| 119 |
+
X0, E0, G0, pairs = X.raw_arrays(row, nk, ek, gk)
|
| 120 |
+
i, j = pairs.T
|
| 121 |
+
sig = np.array([nm != "G" for nm in row["names"]])
|
| 122 |
+
lev = {p: np.asarray(levels[p], float)[i, j] for p in self.levels}
|
| 123 |
+
import torch
|
| 124 |
+
out = []
|
| 125 |
+
for c, m in self.models:
|
| 126 |
+
L = [np.log(np.asarray(lev[p], float)) for p in self.levels]
|
| 127 |
+
assert all(np.isfinite(v).all() for v in L)
|
| 128 |
+
lf, lc = L[-1], L[-2]
|
| 129 |
+
anchor, slope = lf, lf - lc
|
| 130 |
+
E = np.concatenate([E0] + [(L[k + 1] - L[k])[:, None].astype(np.float32) for k in range(len(L) - 1)], -1)
|
| 131 |
+
Xn, En, Gn = X.norm(X0, c["scales"]["X"])[None], X.norm(E, c["scales"]["E"])[None], X.norm(G0[None], c["scales"]["G"])
|
| 132 |
+
bbase = np.where(sig, c["closure"]["1"], c["closure"]["0"]).astype(np.float32)[None]
|
| 133 |
+
T = lambda a: torch.from_numpy(np.ascontiguousarray(a, dtype=np.float32)) # noqa: E731
|
| 134 |
+
n = len(sig)
|
| 135 |
+
with torch.inference_mode():
|
| 136 |
+
C = m(T(Xn), T(En), T(Gn), torch.ones((1, n), dtype=torch.bool), torch.ones((1, len(pairs)), dtype=torch.bool),
|
| 137 |
+
torch.from_numpy(pairs), T(anchor[None].astype(np.float32)), T(slope[None].astype(np.float32)), T(bbase), True)[0]
|
| 138 |
+
out.append(C[0].numpy().astype(np.float64))
|
| 139 |
+
r = np.mean(out, 0)
|
| 140 |
+
assert np.isfinite(r).all()
|
| 141 |
+
return r
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def load(name, reg, model_dir=None, threads=2):
|
| 145 |
+
"""The arm's network object (cached): infer.Predictor (gnn, with .surrogate), x1_core.Multifid / _Curve (multifid), None."""
|
| 146 |
+
key = (str(reg.path), name, str(_model_dir(model_dir)), int(threads))
|
| 147 |
+
if key in _CACHE:
|
| 148 |
+
return _CACHE[key]
|
| 149 |
+
a = _check(reg, name, model_dir)
|
| 150 |
+
obj = None
|
| 151 |
+
if a["kind"] == "gnn":
|
| 152 |
+
obj = I.Predictor(files(reg, name, "checkpoints", model_dir), threads=threads)
|
| 153 |
+
if obj.arm != a["arm"]:
|
| 154 |
+
raise RuntimeError(f"{name}: checkpoints are arm {obj.arm!r}, registry says {a['arm']!r}")
|
| 155 |
+
phys = obj.models[0][0].get("schema", {}).get("physics", {})
|
| 156 |
+
if not obj.direct and float(phys.get("alpha")) != ALPHA:
|
| 157 |
+
raise RuntimeError(f"{name}: checkpoint alpha {phys.get('alpha')} != {ALPHA}")
|
| 158 |
+
obj.surrogate = None
|
| 159 |
+
if a.get("learned_prior"):
|
| 160 |
+
from . import learned_prior
|
| 161 |
+
model, prep, _ = learned_prior.load(files(reg, name, "surrogate", model_dir)[0])
|
| 162 |
+
obj.surrogate = (model, prep)
|
| 163 |
+
if obj.direct != (a.get("stack") is None and not a.get("learned_prior")):
|
| 164 |
+
raise RuntimeError(f"{name}: solver-free checkpoints iff no prior stack and no learned prior")
|
| 165 |
+
elif a["kind"] == "multifid":
|
| 166 |
+
paths = files(reg, name, "checkpoints", model_dir)
|
| 167 |
+
import torch
|
| 168 |
+
curve = torch.load(paths[0], map_location="cpu", weights_only=False)["curve"]
|
| 169 |
+
obj = _Curve(paths, threads) if curve else X.Multifid(paths, threads)
|
| 170 |
+
if [float(p) for p in obj.levels] != [float(p) for p in a["levels"]]:
|
| 171 |
+
raise RuntimeError(f"{name}: checkpoint levels {obj.levels} != registry {a['levels']}")
|
| 172 |
+
_CACHE[key] = obj
|
| 173 |
+
return obj
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def levels_of(a) -> list[float]:
|
| 177 |
+
return [float(p) for p in (a.get("levels") or a.get("ppg") or [])]
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def apply_stack(reg, a, threads=2):
|
| 181 |
+
st = stack_of(reg, a)
|
| 182 |
+
if st is not None:
|
| 183 |
+
A.set_prior_impl("frozen")
|
| 184 |
+
A.set_prior_stack(st, threads)
|
| 185 |
+
A._thread_limits(threads)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def solve_levels(sc, levels, threads=2) -> tuple[dict, dict]:
|
| 189 |
+
"""Positive-mutual prior matrices P = -C at each level under the installed stack (the statements of
|
| 190 |
+
geomgnn.api.predict_scene's prior loop, any level list)."""
|
| 191 |
+
from ._tlsq.embed.fastprior_loc import micro_mom_loc
|
| 192 |
+
proxy.NUM_THREADS = threads
|
| 193 |
+
if set(A.V2_KWARGS) & set(A._STATE["patches"]):
|
| 194 |
+
from . import priorv2
|
| 195 |
+
priorv2.clear()
|
| 196 |
+
out, tm = {}, {}
|
| 197 |
+
old = fastprior.ALPHA_GAP_SHARE
|
| 198 |
+
try:
|
| 199 |
+
fastprior.ALPHA_GAP_SHARE = ALPHA
|
| 200 |
+
for p in levels:
|
| 201 |
+
t = time.perf_counter()
|
| 202 |
+
with proxy.substrate("slab_open", sc.stack["substrate_thickness_um"]), A._quiet_mixed_cast():
|
| 203 |
+
mat, _, _ = micro_mom_loc(sc.conductors[0].geom, [c.geom for c in sc.conductors[1:]], eps_r=sc.stack["substrate_eps_r"], ppg=p)
|
| 204 |
+
out[p] = -mat
|
| 205 |
+
tm[f"{p:g}"] = time.perf_counter() - t
|
| 206 |
+
finally:
|
| 207 |
+
fastprior.ALPHA_GAP_SHARE = old
|
| 208 |
+
return out, tm
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def combine(a, obj, row, P) -> np.ndarray:
|
| 212 |
+
"""The arm's Maxwell matrix from the embedding row and the priors P {ppg: -C} (scene order)."""
|
| 213 |
+
k = a["kind"]
|
| 214 |
+
if k == "physics":
|
| 215 |
+
lv = levels_of(a)
|
| 216 |
+
return X.to_maxwell(P[lv[0]]) if len(lv) == 1 else X.c0(P, lv[0], lv[1])
|
| 217 |
+
if k == "multifid":
|
| 218 |
+
return obj.predict(row, P)
|
| 219 |
+
if obj.direct:
|
| 220 |
+
return obj.from_features(row)
|
| 221 |
+
if obj.surrogate is not None:
|
| 222 |
+
from . import learned_prior
|
| 223 |
+
return obj.from_features(row, learned_prior.priors(*obj.surrogate, row))
|
| 224 |
+
lo, hi = levels_of(a)
|
| 225 |
+
return obj.from_features(row, {1.5: P[lo], 3.0: P[hi]})
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def predict_scene(reg, name, sc, threads=2, model_dir=None) -> dict:
|
| 229 |
+
a = arms(reg)[name]
|
| 230 |
+
obj = load(name, reg, model_dir, threads)
|
| 231 |
+
apply_stack(reg, a, threads)
|
| 232 |
+
t0 = time.perf_counter()
|
| 233 |
+
row = lt.add_tokens(sc, scene_graph(sc))
|
| 234 |
+
t1 = time.perf_counter()
|
| 235 |
+
need = [] if (a["kind"] == "gnn" and (obj.direct or obj.surrogate is not None)) else levels_of(a)
|
| 236 |
+
P, tl = solve_levels(sc, need, threads) if need else ({}, {})
|
| 237 |
+
t2 = time.perf_counter()
|
| 238 |
+
C = np.asarray(combine(a, obj, row, P), float)
|
| 239 |
+
t3 = time.perf_counter()
|
| 240 |
+
return dict(C=C, names=row["names"], row=row, priors=P,
|
| 241 |
+
timing=dict(geometry=t1 - t0, prior=t2 - t1, prior_levels=tl, network=t3 - t2, total=t3 - t0))
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
def predict(prepared, name, reg, substrate="Si (cryogenic, training)", thickness_um=350.0, model_dir=None, threads=None,
|
| 245 |
+
self_from_rows=True, trust_rule=None) -> dict:
|
| 246 |
+
"""Full N x N Maxwell matrix (fF; ground row/column 0) of a prepared layout with a paper_arms entry."""
|
| 247 |
+
a = arms(reg)[name]
|
| 248 |
+
threads = int(threads or os.environ.get("TLSQ_NUM_THREADS") or 2)
|
| 249 |
+
if a["kind"] == "final":
|
| 250 |
+
from tlsq_infer import final as F
|
| 251 |
+
tr = bool(reg["final_defaults"].get("trust_rule", True)) if trust_rule is None else bool(trust_rule)
|
| 252 |
+
res = F.predict_final(prepared, a["final_model"], substrate=substrate, thickness_um=thickness_um, self_from_rows=bool(self_from_rows),
|
| 253 |
+
trust_rule=tr if a.get("trust_rule_applies") else False)
|
| 254 |
+
res.update(arm=name, arm_title=a["title"], paper_name=a["paper_name"], route=a.get("route"))
|
| 255 |
+
return res
|
| 256 |
+
eps = reg.eps_of(substrate)
|
| 257 |
+
if abs(eps - A.EPS_TRAIN) > 1e-9 or abs(float(thickness_um) - A.T_TRAIN_UM) > 1e-9:
|
| 258 |
+
raise ValueError(f"the paper's networks and priors were trained and scored on Si (eps_r {A.EPS_TRAIN:g}), {A.T_TRAIN_UM:g} µm only and "
|
| 259 |
+
f"are served for that stack only (requested eps_r {eps:g}, {float(thickness_um):g} µm); the prior-generation model "
|
| 260 |
+
"accepts other substrates")
|
| 261 |
+
t = time.perf_counter()
|
| 262 |
+
load(name, reg, model_dir, threads)
|
| 263 |
+
load_s = time.perf_counter() - t
|
| 264 |
+
sc = A.scene_from_prepared(prepared)
|
| 265 |
+
try:
|
| 266 |
+
r = predict_scene(reg, name, sc, threads, model_dir)
|
| 267 |
+
except AssertionError as e:
|
| 268 |
+
raise ValueError(f"{name}: the prior or the features of this layout are not finite (no matrix returned): {e}") from e
|
| 269 |
+
C = r["C"]
|
| 270 |
+
chk = A.physical_checks(C)
|
| 271 |
+
warnings = [w for w in prepared.warnings if "beyond the validated range" not in w]
|
| 272 |
+
if a.get("warning"):
|
| 273 |
+
warnings.insert(0, a["warning"])
|
| 274 |
+
if not (chk["symmetric_max_abs"] <= 1e-6 * max(1.0, float(np.abs(C).max())) and chk["offdiag_all_negative"]
|
| 275 |
+
and (chk["signal_block_min_eig"] is None or chk["signal_block_min_eig"] > 0)):
|
| 276 |
+
warnings.append(f"physical check failed: {chk}")
|
| 277 |
+
st = stack_of(reg, a)
|
| 278 |
+
tm = r["timing"]
|
| 279 |
+
return {"names": ["G"] + list(prepared.names), "matrix_fF": C.tolist(), "units": "fF",
|
| 280 |
+
"convention": "Maxwell matrix: diagonal = self, off-diagonal = -mutual; row/column 0 = ground",
|
| 281 |
+
"self_capacitance": a.get("self_note") or ("predicted by the network (learned positive row-sum closure). The ground self term "
|
| 282 |
+
"depends on the chip boundary: indicative only"),
|
| 283 |
+
"model": name, "arm": name, "arm_title": a["title"], "paper_name": a["paper_name"], "route": a.get("route"), "kind": a["kind"],
|
| 284 |
+
"network_arm": a.get("arm"), "levels_ppg": levels_of(a) if a["kind"] != "gnn" or not (a.get("learned_prior") or a.get("stack") is None) else [],
|
| 285 |
+
"weights_sha256": _hashes(a, "checkpoints"), "learned_prior_sha256": (_hashes(a, "surrogate") or [None])[0],
|
| 286 |
+
"prior_stack": (None if st is None else {"name": a["stack"], "patches": A._STATE.get("patches", []), "mode": A._STATE.get("mode") or "frozen",
|
| 287 |
+
"alpha": ALPHA, "method": "local_gap micro-MoM (slab)"}),
|
| 288 |
+
"package_version": f"geomgnn {__version__}", "registry_version": reg["version"], "substrate": "Si (cryogenic, training)",
|
| 289 |
+
"eps_r": A.EPS_TRAIN, "thickness_um": A.T_TRAIN_UM,
|
| 290 |
+
"timing_s": {"load_s": load_s, "geometry_s": tm["geometry"], "prior_s": tm["prior"], "prior_levels_s": tm["prior_levels"],
|
| 291 |
+
"network_s": tm["network"], "total_s": tm["total"], "threads": threads},
|
| 292 |
+
"paper": a.get("paper") or {}, "checks": chk, "warnings": warnings, "stored": False}
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
STARTUP: dict = {}
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def preload(reg, example=None, names=None) -> dict:
|
| 299 |
+
"""Server start: load every live arm (SHA checks) and run the default arm on a small example (numba warmup of the compiled
|
| 300 |
+
solver included), so the first request does not pay the compile."""
|
| 301 |
+
t0 = time.perf_counter()
|
| 302 |
+
for k in names or live(reg):
|
| 303 |
+
if arms(reg)[k]["kind"] != "final" and available(k, reg):
|
| 304 |
+
load(k, reg)
|
| 305 |
+
STARTUP["load_all_s"] = time.perf_counter() - t0
|
| 306 |
+
d = reg["paper_hub"]["default_arm"]
|
| 307 |
+
if example is not None and Path(example).exists():
|
| 308 |
+
from tlsq_infer.layout import prepare
|
| 309 |
+
t = time.perf_counter()
|
| 310 |
+
predict(prepare(str(example)), d, reg)
|
| 311 |
+
STARTUP["example_s"] = time.perf_counter() - t
|
| 312 |
+
STARTUP.update(default=d, numba_warmup_s=A._STATE.get("warmup_s"), total_s=time.perf_counter() - t0)
|
| 313 |
+
print("preload " + json.dumps({k: (round(v, 2) if isinstance(v, float) else v) for k, v in STARTUP.items()}), flush=True)
|
| 314 |
+
return dict(STARTUP)
|
geomgnn/pilot_p2.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Isolated P1 pilot. No external-test loaders or paths are implemented."""
|
| 2 |
+
import os
|
| 3 |
+
os.environ.setdefault('PYTHONDONTWRITEBYTECODE','1')
|
| 4 |
+
import argparse, hashlib, json, pickle, time, platform, sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import numpy as np
|
| 7 |
+
ARMS=['mesh','pair_mlp','full_mlp','full_gnn','fast_gnn','direct_gnn','lofo_pair','lofo_full']
|
| 8 |
+
ROOT=Path(__file__).resolve().parent
|
| 9 |
+
|
| 10 |
+
def digest(p): return hashlib.sha256(Path(p).read_bytes()).hexdigest()
|
| 11 |
+
def qc(x):
|
| 12 |
+
C=x['C']; n=len(x['names']); sig=np.array([n!='G' for n in x['names']])
|
| 13 |
+
assert C.shape==(n,n) and np.isfinite(C).all(), 'shape/finite'
|
| 14 |
+
assert np.max(abs(C-C.T))<1e-5*max(1,np.max(abs(C))), 'symmetry'
|
| 15 |
+
assert np.all(np.diag(C)>0), 'diagonal'
|
| 16 |
+
assert np.max(C[~np.eye(n,dtype=bool)])<=1e-6, 'mutual sign'
|
| 17 |
+
assert np.min(C.sum(1)[sig])>=-1e-6, 'closure'
|
| 18 |
+
assert np.linalg.eigvalsh(C[np.ix_(sig,sig)]).min()>0, 'positive signal block'
|
| 19 |
+
for p in [.75,1.5,3.]:
|
| 20 |
+
v=x['priors'][p][np.triu_indices(n,1)]; assert np.isfinite(v).all() and np.all(v>0), 'prior positive'
|
| 21 |
+
|
| 22 |
+
def feature(x):
|
| 23 |
+
t=time.perf_counter()
|
| 24 |
+
try:
|
| 25 |
+
qc(x)
|
| 26 |
+
if x['nodes'] is None:
|
| 27 |
+
from tlsq.uge.scene import scene_from_gds,tlsq_namer
|
| 28 |
+
from tlsq.uge.blocks import node_features,global_features
|
| 29 |
+
sc=scene_from_gds(x['gds'],namer=tlsq_namer)
|
| 30 |
+
assert len(set(sc.names()))==sc.n and set(sc.names())==set(x['names']), 'net mapping'
|
| 31 |
+
x['nodes']=[node_features(sc,sc.names().index(name)) for name in x['names']]
|
| 32 |
+
x['global_features'].update(global_features(sc))
|
| 33 |
+
x['node_features_s']=time.perf_counter()-t
|
| 34 |
+
else: x['node_features_s']=None
|
| 35 |
+
for n in x['nodes']:
|
| 36 |
+
for k,v in n.items():
|
| 37 |
+
if not k.startswith('proxy_'): assert np.isfinite(v), 'node feature finite'
|
| 38 |
+
return x,None
|
| 39 |
+
except Exception as e:return None,dict(key=x['key'],reason=repr(e))
|
| 40 |
+
|
| 41 |
+
def prepare(a):
|
| 42 |
+
from concurrent.futures import ProcessPoolExecutor
|
| 43 |
+
sys.path.insert(0,str(ROOT/'vendor'))
|
| 44 |
+
items=pickle.load(open(a.input,'rb')); assert all(x['split'] in ['train','val'] for x in items)
|
| 45 |
+
start=time.time()
|
| 46 |
+
with ProcessPoolExecutor(a.workers) as pool: result=list(pool.map(feature,items))
|
| 47 |
+
good=[x for x,e in result if x is not None]; bad=[e for x,e in result if e]
|
| 48 |
+
assert good and len(good)>0.9*len(items),(len(good),bad[:5])
|
| 49 |
+
# Conservative feature-identity audit; all labels remain unused in this decision.
|
| 50 |
+
def geometry_key(x):
|
| 51 |
+
vals=[[{k:round(float(v),9) for k,v in nd.items() if not k.startswith('proxy_')} for nd in x['nodes']],
|
| 52 |
+
[(str(k),{a:round(float(b),9) for a,b in v.items() if not a.startswith(('proxy_','conf_','_'))}) for k,v in sorted(x['edges'].items())]]
|
| 53 |
+
return hashlib.sha256(json.dumps(vals,sort_keys=True).encode()).hexdigest()
|
| 54 |
+
validation_geometry={geometry_key(x) for x in good if x['split']=='val'}
|
| 55 |
+
retained=[]
|
| 56 |
+
for x in good:
|
| 57 |
+
if x['split']=='train' and geometry_key(x) in validation_geometry:
|
| 58 |
+
bad.append(dict(key=x['key'],reason='geometry descriptor duplicate of validation; training copy excluded'))
|
| 59 |
+
else: retained.append(x)
|
| 60 |
+
good=retained
|
| 61 |
+
nodekeys=sorted(set.intersection(*[set(nd) for x in good for nd in x['nodes']]))
|
| 62 |
+
nodekeys=[k for k in nodekeys if not k.startswith('proxy_')]
|
| 63 |
+
edgekeys=sorted(set.intersection(*[set(e) for x in good for e in x['edges'].values()]))
|
| 64 |
+
edgekeys=[k for k in edgekeys if not k.startswith(('proxy_','conf_','_'))]
|
| 65 |
+
globkeys=['log_chip_area','log_pocket_area','log_substrate_t','eps_eff']
|
| 66 |
+
B=len(good); N=max(len(x['names']) for x in good); pairs=np.array([(i,j) for i in range(N) for j in range(i+1,N)])
|
| 67 |
+
F=len(pairs); X=np.zeros((B,N,len(nodekeys)),np.float32); E=np.zeros((B,F,len(edgekeys)),np.float32)
|
| 68 |
+
G=np.zeros((B,len(globkeys)),np.float32); C=np.zeros((B,N,N),np.float32); M=np.zeros((B,N),bool); PM=np.zeros((B,F),bool)
|
| 69 |
+
pri=np.zeros((B,F,3),np.float32); sig=np.zeros((B,N),bool)
|
| 70 |
+
for b,x in enumerate(good):
|
| 71 |
+
n=len(x['names']); M[b,:n]=True; sig[b,:n]=[v!='G' for v in x['names']]; C[b,:n,:n]=x['C']
|
| 72 |
+
X[b,:n]=[[nd[k] for k in nodekeys] for nd in x['nodes']]; G[b]=[x['global_features'][k] for k in globkeys]
|
| 73 |
+
for f,(i,j) in enumerate(pairs):
|
| 74 |
+
if j>=n:continue
|
| 75 |
+
PM[b,f]=True; E[b,f]=[x['edges'][(i,j)][k] for k in edgekeys]
|
| 76 |
+
pri[b,f]=[x['priors'][p][i,j] for p in [.75,1.5,3.]]
|
| 77 |
+
assert np.isfinite(E).all() and np.isfinite(G).all()
|
| 78 |
+
out=Path(a.output); out.mkdir(parents=True,exist_ok=True)
|
| 79 |
+
np.savez_compressed(out/'data.npz',X=X,E=E,G=G,C=C,M=M,PM=PM,pri=pri,sig=sig,pairs=pairs,
|
| 80 |
+
key=np.array([x['key'] for x in good]),split=np.array([x['split'] for x in good]),family=np.array([x['family'] for x in good]),
|
| 81 |
+
nodekeys=np.array(nodekeys),edgekeys=np.array(edgekeys),globkeys=np.array(globkeys))
|
| 82 |
+
report=dict(accepted=len(good),excluded=bad,seconds=time.time()-start,nodekeys=nodekeys,edgekeys=edgekeys,source_hash=digest(a.input),code_hash=digest(__file__),
|
| 83 |
+
source_code_hashes={p:digest(Path(a.repo)/p) for p in ['src/tlsq/uge/blocks.py','src/tlsq/uge/scene.py','src/tlsq/geometry.py']},
|
| 84 |
+
node_feature_seconds=[dict(key=x['key'],seconds=x['node_features_s']) for x in good if x['node_features_s'] is not None],
|
| 85 |
+
twonet_cached_feature_seconds=[dict(key=x['key'],features=x.get('cached_features_s'),tokens=x.get('cached_tokens_s')) for x in good if x['family']=='twonet'])
|
| 86 |
+
(out/'prep.json').write_text(json.dumps(report,indent=2)); print('PREP_DONE',len(good),'excluded',len(bad),flush=True)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def build_model(dx,de,dg,rounds,width=64):
|
| 90 |
+
import torch
|
| 91 |
+
from torch import nn
|
| 92 |
+
def mlp(i,o):return nn.Sequential(nn.Linear(i,width),nn.SiLU(),nn.Linear(width,o))
|
| 93 |
+
class Net(nn.Module):
|
| 94 |
+
def __init__(self):
|
| 95 |
+
super().__init__(); self.node=mlp(dx+dg,width); self.edge=mlp(de,width)
|
| 96 |
+
self.msg=nn.ModuleList([mlp(3*width,width) for _ in range(rounds)])
|
| 97 |
+
self.up=nn.ModuleList([mlp(2*width,width) for _ in range(rounds)])
|
| 98 |
+
self.head=mlp(3*width,1); self.closure=mlp(width,1)
|
| 99 |
+
nn.init.zeros_(self.head[-1].weight); nn.init.zeros_(self.head[-1].bias)
|
| 100 |
+
nn.init.zeros_(self.closure[-1].weight); nn.init.zeros_(self.closure[-1].bias)
|
| 101 |
+
def forward(self,X,E,G,M,PM,pairs,base,bbase,direct=False):
|
| 102 |
+
B,N,_=X.shape; i,j=pairs[:,0],pairs[:,1]
|
| 103 |
+
h=self.node(torch.cat([X,G[:,None].expand(-1,N,-1)],-1))*M[:,:,None]; e=self.edge(E)
|
| 104 |
+
for msg,up in zip(self.msg,self.up):
|
| 105 |
+
v=msg(torch.cat([h[:,i]+h[:,j],abs(h[:,i]-h[:,j]),e],-1))*PM[:,:,None]
|
| 106 |
+
agg=torch.zeros_like(h); agg.index_add_(1,i,v); agg.index_add_(1,j,v)
|
| 107 |
+
degree=torch.zeros_like(M,dtype=X.dtype); degree.index_add_(1,i,PM.float()); degree.index_add_(1,j,PM.float())
|
| 108 |
+
h=(h+up(torch.cat([h,agg/degree.clamp_min(1)[:,:,None]],-1)))*M[:,:,None]
|
| 109 |
+
raw=self.head(torch.cat([h[:,i]+h[:,j],abs(h[:,i]-h[:,j]),e],-1)).squeeze(-1)
|
| 110 |
+
logc=(base+(raw if direct else .2*torch.tanh(raw))).clamp(-25,20)
|
| 111 |
+
mutual=logc.exp()*PM; b=(bbase+self.closure(h).squeeze(-1)).clamp(-20,20).exp()*M
|
| 112 |
+
C=torch.zeros((B,N,N),device=X.device); C[:,i,j]=-mutual; C[:,j,i]=-mutual
|
| 113 |
+
diag=-C.sum(-1)+b; C=C+torch.diag_embed(diag)
|
| 114 |
+
return C,logc
|
| 115 |
+
return Net()
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def fit(a):
|
| 119 |
+
import torch
|
| 120 |
+
from torch.nn import functional as F
|
| 121 |
+
torch.set_num_threads(a.threads); torch.manual_seed(a.seed); rng=np.random.default_rng(a.seed)
|
| 122 |
+
d=dict(np.load(a.data,allow_pickle=False)); arm=a.arm; out=Path(a.output); out.mkdir(parents=True,exist_ok=False)
|
| 123 |
+
(out/'run.json').write_text(json.dumps(dict(arm=arm,seed=a.seed,steps=a.steps,data_sha=digest(a.data),code_sha=digest(__file__),start=time.time(),threads=a.threads,host=platform.node()),indent=2))
|
| 124 |
+
tr=(d['split']=='train') & ((d['family']!='concentric') if arm.startswith('lofo') else True); va=d['split']=='val'
|
| 125 |
+
assert tr.any() and va.any() and not np.any(tr&va)
|
| 126 |
+
X=d['X'].copy(); E=d['E'].copy(); G=d['G'].copy()
|
| 127 |
+
ni=list(d['nodekeys']).index('role_ground'); ei=list(d['edgekeys']).index('is_ground_pair')
|
| 128 |
+
full=arm not in ['mesh','pair_mlp','lofo_pair']; direct=arm=='direct_gnn'; fast=arm=='fast_gnn'
|
| 129 |
+
if not full:X=X[:,:,[ni]]; G=np.zeros((len(X),1),np.float32)
|
| 130 |
+
if arm=='mesh':E=E[:,:,[ei]]
|
| 131 |
+
lo,hi=(0,1) if fast else (1,2); lp=np.log(d['pri'].clip(1e-12)); base=2*lp[:,:,hi]-lp[:,:,lo]
|
| 132 |
+
if not direct:E=np.concatenate([E,(lp[:,:,hi]-lp[:,:,lo])[:,:,None]],-1)
|
| 133 |
+
else:
|
| 134 |
+
for g in [0,1]:
|
| 135 |
+
mask=(d['E'][:,:,ei]==g)&d['PM']&tr[:,None]
|
| 136 |
+
v=(-d['C'][:,d['pairs'][:,0],d['pairs'][:,1]])
|
| 137 |
+
base[d['E'][:,:,ei]==g]=np.median(np.log(v[mask].clip(1e-12)))
|
| 138 |
+
scales={}
|
| 139 |
+
for name,z,mask in [('X',X,d['M']&tr[:,None]),('E',E,d['PM']&tr[:,None]),('G',G,tr)]:
|
| 140 |
+
v=z[mask]; med=np.median(v,0); scale=np.percentile(v,75,0)-np.percentile(v,25,0); scale=np.where(scale>1e-6,scale,1)
|
| 141 |
+
z[:]=np.clip((z-med)/scale,-10,10); scales[name]=dict(median=med.tolist(),iqr=scale.tolist())
|
| 142 |
+
bbase=np.zeros(d['M'].shape,np.float32); closure=d['C'].sum(-1).clip(1e-8)
|
| 143 |
+
for is_signal in [False,True]:
|
| 144 |
+
mask=(d['sig']==is_signal)&d['M']&tr[:,None]; bbase[d['sig']==is_signal]=np.median(np.log(closure[mask]))
|
| 145 |
+
T=lambda x:torch.from_numpy(np.asarray(x))
|
| 146 |
+
X,E,G,base,bbase=map(T,[X,E,G,base.astype(np.float32),bbase]); M,PM,C,sig,pairs=map(T,[d['M'],d['PM'],d['C'],d['sig'],d['pairs']])
|
| 147 |
+
rounds=2 if arm in ['full_gnn','fast_gnn','direct_gnn','lofo_full'] else 0
|
| 148 |
+
model=build_model(X.shape[-1],E.shape[-1],G.shape[-1],rounds); opt=torch.optim.AdamW(model.parameters(),lr=1e-3,weight_decay=1e-4)
|
| 149 |
+
sched=torch.optim.lr_scheduler.CosineAnnealingLR(opt,a.steps,eta_min=1e-5)
|
| 150 |
+
idx=np.flatnonzero(tr); fam=d['family']; groups=[idx[fam[idx]==f] for f in sorted(set(fam[idx]))]
|
| 151 |
+
truth=(-C[:,pairs[:,0],pairs[:,1]]).clamp_min(1e-12).log(); hist=[]; t0=time.perf_counter()
|
| 152 |
+
for step in range(a.steps):
|
| 153 |
+
b=np.array([rng.choice(groups[g]) for g in rng.integers(len(groups),size=64)])
|
| 154 |
+
pred,logc=model(X[b],E[b],G[b],M[b],PM[b],pairs,base[b],bbase[b],direct)
|
| 155 |
+
pairloss=(F.huber_loss(logc,truth[b],delta=.05,reduction='none')*PM[b]).sum(1)/PM[b].sum(1).clamp_min(1)
|
| 156 |
+
selfloss=(F.huber_loss(pred.diagonal(dim1=-2,dim2=-1).clamp_min(1e-12).log(),C[b].diagonal(dim1=-2,dim2=-1).clamp_min(1e-12).log(),delta=.05,reduction='none')*sig[b]).sum(1)/sig[b].sum(1).clamp_min(1)
|
| 157 |
+
groundmask=M[b]&~sig[b]
|
| 158 |
+
groundloss=(F.huber_loss(pred.diagonal(dim1=-2,dim2=-1).clamp_min(1e-12).log(),C[b].diagonal(dim1=-2,dim2=-1).clamp_min(1e-12).log(),delta=.05,reduction='none')*groundmask).sum(1)/groundmask.sum(1).clamp_min(1)
|
| 159 |
+
loss=(pairloss+.1*selfloss+.02*groundloss).mean(); assert torch.isfinite(loss)
|
| 160 |
+
opt.zero_grad(); loss.backward(); torch.nn.utils.clip_grad_norm_(model.parameters(),5); opt.step(); sched.step()
|
| 161 |
+
if step%250==0:hist.append(dict(step=step,loss=float(loss.detach()),seconds=time.perf_counter()-t0)); print(hist[-1],flush=True)
|
| 162 |
+
train_s=time.perf_counter()-t0
|
| 163 |
+
torch.save(dict(state=model.state_dict(),scales=scales,arm=arm,history=hist,train_keys=d['key'][tr].tolist(),data_hash=digest(a.data),base_closure=bbase.numpy(),nodekeys=d['nodekeys'].tolist(),edgekeys=d['edgekeys'].tolist(),rounds=rounds,closure_intercepts={str(flag):float(np.median(bbase.numpy()[(d['sig']==flag)&d['M']&tr[:,None]])) for flag in [False,True]},direct_intercepts={str(flag):float(np.median(base.numpy()[(d['E'][:,:,ei]==flag)&d['PM']&tr[:,None]])) for flag in [0,1]} if direct else None),out/'model.pt')
|
| 164 |
+
ev=np.flatnonzero(va); model.eval(); preds=[]
|
| 165 |
+
with torch.inference_mode():
|
| 166 |
+
for chunk in np.array_split(ev,max(1,int(np.ceil(len(ev)/64)))):
|
| 167 |
+
preds.append(model(X[chunk],E[chunk],G[chunk],M[chunk],PM[chunk],pairs,base[chunk],bbase[chunk],direct)[0].numpy())
|
| 168 |
+
b=ev[:1]
|
| 169 |
+
for _ in range(10):model(X[b],E[b],G[b],M[b],PM[b],pairs,base[b],bbase[b],direct)
|
| 170 |
+
times=[]
|
| 171 |
+
for _ in range(100):
|
| 172 |
+
t=time.perf_counter(); model(X[b],E[b],G[b],M[b],PM[b],pairs,base[b],bbase[b],direct); times.append(time.perf_counter()-t)
|
| 173 |
+
pred=np.concatenate(preds); np.savez_compressed(out/'validation_predictions.npz',key=d['key'][ev],C=pred)
|
| 174 |
+
with (out/'VALIDATION_SCORED.json').open('x') as f:json.dump(dict(time=time.time(),prediction_sha=digest(out/'validation_predictions.npz'),scope='validation only; no external test labels'),f)
|
| 175 |
+
rec=[]
|
| 176 |
+
for family in sorted(set(fam[ev])):
|
| 177 |
+
sel=fam[ev]==family; targets=d['C'][ev][sel]; p=pred[sel]; ss=d['sig'][ev][sel]; pm=d['PM'][ev][sel]; ii,jj=d['pairs'].T
|
| 178 |
+
masks={'mutual':pm,'signal_signal':pm&ss[:,ii]&ss[:,jj],'signal_ground':pm&~(ss[:,ii]&ss[:,jj])}
|
| 179 |
+
for kind,mask in masks.items():
|
| 180 |
+
if mask.any():rec.append(metric(family,kind,-targets[:,ii,jj][mask],-p[:,ii,jj][mask],train_s,times))
|
| 181 |
+
rec.append(metric(family,'signal_self',targets.diagonal(axis1=1,axis2=2)[ss],p.diagonal(axis1=1,axis2=2)[ss],train_s,times))
|
| 182 |
+
prior=np.exp(2*lp[ev][:, :,2]-lp[ev][:,:,1])[sel]
|
| 183 |
+
rec.append(metric(family,'C0_mutual',-targets[:,ii,jj][pm],prior[pm],train_s,times))
|
| 184 |
+
import pandas as pd
|
| 185 |
+
pd.DataFrame(rec).to_csv(out/'metrics.csv',index=False)
|
| 186 |
+
cpu=next((s.split(':',1)[1].strip() for s in Path('/proc/cpuinfo').read_text().splitlines() if s.startswith('model name')),'unknown') if Path('/proc/cpuinfo').exists() else platform.processor()
|
| 187 |
+
(out/'timing.json').write_text(json.dumps(dict(cpu=cpu,host=platform.node(),threads=a.threads,train_seconds=train_s,network_median_seconds=float(np.median(times)),network_p95_seconds=float(np.quantile(times,.95)),timing_scope='cached features; not end-to-end',parameters=sum(p.numel() for p in model.parameters())),indent=2))
|
| 188 |
+
import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot as plt
|
| 189 |
+
fig,ax=plt.subplots(1,2,figsize=(10,4)); ii,jj=d['pairs'].T; mask=d['PM'][ev]; y=-d['C'][ev][:,ii,jj][mask]; yp=-pred[:,ii,jj][mask]
|
| 190 |
+
ax[0].loglog(y,yp,'.',alpha=.3); lo=min(y.min(),yp.min()); hi=max(y.max(),yp.max()); ax[0].plot([lo,hi],[lo,hi],'k--'); ax[0].set(xlabel='Q3D mutual (fF)',ylabel='Prediction (fF)',title=arm)
|
| 191 |
+
ax[1].semilogx(y,yp-y,'.',alpha=.3); ax[1].set(xlabel='Q3D mutual (fF)',ylabel='Signed error (fF)'); fig.tight_layout(); fig.savefig(out/'diagnostics.png',dpi=140); plt.close(fig)
|
| 192 |
+
print('RUN_DONE',arm,train_s,flush=True)
|
| 193 |
+
|
| 194 |
+
def metric(family,kind,y,p,train_s,times):
|
| 195 |
+
err=abs(p-y); return dict(family=family,kind=kind,n=len(y),median_percent=float(np.median(err/np.maximum(abs(y),.1))*100),unfloored_percent=float(np.median(err/np.maximum(abs(y),1e-15))*100),mae_fF=float(err.mean()),rmse_fF=float(np.sqrt(np.mean(err**2))),train_s=train_s,network_median_s=float(np.median(times)))
|
| 196 |
+
|
| 197 |
+
def smoke():
|
| 198 |
+
import torch
|
| 199 |
+
torch.set_num_threads(2); torch.manual_seed(4)
|
| 200 |
+
net=build_model(3,4,2,2); pairs=torch.tensor([[0,1],[0,2],[1,2]])
|
| 201 |
+
X=torch.randn(1,3,3); E=torch.randn(1,3,4); G=torch.randn(1,2); M=torch.ones(1,3,dtype=torch.bool); PM=M.clone(); base=torch.zeros(1,3); bbase=base.clone()
|
| 202 |
+
for p in net.parameters():torch.nn.init.normal_(p,0,.05)
|
| 203 |
+
C,_=net(X,E,G,M,PM,pairs,base,bbase)
|
| 204 |
+
assert torch.allclose(C,C.transpose(1,2)); assert torch.linalg.eigvalsh(C).min()>0
|
| 205 |
+
perm=torch.tensor([2,0,1]); edgeperm=torch.tensor([1,2,0]); C2,_=net(X[:,perm],E[:,edgeperm],G,M,PM,pairs,base,bbase)
|
| 206 |
+
assert torch.allclose(C2,C[:,perm][:,:,perm],atol=1e-6),'permutation equivariance'
|
| 207 |
+
C.sum().backward(); assert all(p.grad is None or torch.isfinite(p.grad).all() for p in net.parameters())
|
| 208 |
+
print('SMOKE_PASS: symmetry, positive definiteness, permutation equivariance, finite backward')
|
| 209 |
+
|
| 210 |
+
if __name__=='__main__':
|
| 211 |
+
p=argparse.ArgumentParser(); sub=p.add_subparsers(dest='cmd',required=True)
|
| 212 |
+
q=sub.add_parser('prepare'); q.add_argument('--repo',required=True); q.add_argument('--input',required=True); q.add_argument('--output',required=True); q.add_argument('--workers',type=int,default=4)
|
| 213 |
+
q=sub.add_parser('fit'); q.add_argument('--data',required=True); q.add_argument('--output',required=True); q.add_argument('--arm',choices=ARMS,required=True); q.add_argument('--seed',type=int,default=0); q.add_argument('--threads',type=int,default=2); q.add_argument('--steps',type=int,default=1500)
|
| 214 |
+
sub.add_parser('smoke'); a=p.parse_args(); {'prepare':prepare,'fit':fit,'smoke':lambda a:smoke()}[a.cmd](a)
|
geomgnn/priorv2.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Prior v2 = frozen geomfast prior + rejtail fix fc (rescue panels), as a monkeypatch of fastprior._window_solve.
|
| 2 |
+
Logic copied from scripts/rejtail/rt.py (policies 'exact' and 'fc' only; no wp dependency). Vendor files untouched.
|
| 3 |
+
Import after the geomfast vendor dir is on sys.path. Usage:
|
| 4 |
+
import priorv2; with priorv2.installed(): ... fastprior.micro_mom / micro_mom_loc ...
|
| 5 |
+
fc acts only on windowed solves (all_rhs False, N > 9) whose FIRST attempt leaves a metal without a panel; everything
|
| 6 |
+
else runs the frozen code path."""
|
| 7 |
+
import time
|
| 8 |
+
import contextlib
|
| 9 |
+
import numpy as np
|
| 10 |
+
import shapely
|
| 11 |
+
from ._tlsq.embed import fastprior as F, proxy as P
|
| 12 |
+
|
| 13 |
+
PRIOR_VERSION = 'v2_fc_dipfill'
|
| 14 |
+
TRACE = []
|
| 15 |
+
POLICY = ['fc']
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _solve(c, wh, o, metals, cols, eps_r, all_rhs):
|
| 19 |
+
if len(c) <= P.DENSE_MAX:
|
| 20 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 21 |
+
Bm = (o[:, None] == cols[None, :]).astype(float)
|
| 22 |
+
Q = P._chol_solve(A, Bm) if all_rhs else F._sym_solve(A, Bm)
|
| 23 |
+
else:
|
| 24 |
+
Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols]
|
| 25 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _mesh(metals, n_core, hh, cap, refined, majority, levels):
|
| 29 |
+
if not refined:
|
| 30 |
+
return F._lite_mesh(metals, hh, cap, n_core, levels)
|
| 31 |
+
if levels:
|
| 32 |
+
return F._graded_mesh(metals, n_core, hh, levels, majority)
|
| 33 |
+
return P.mesh_flagship(metals, hh, cap, majority=majority)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _rescue_panels(metals, miss, h):
|
| 37 |
+
"""Each panel-less metal cut by a lattice of pitch min(h, sqrt(area) / 2) anchored at its bbox; one panel per piece,
|
| 38 |
+
centre = centroid (point on surface if outside), size = piece bbox (the frozen cut-piece form)."""
|
| 39 |
+
cs, whs, os_ = [], [], []
|
| 40 |
+
for a in miss:
|
| 41 |
+
g = metals[a]
|
| 42 |
+
x0, y0, x1, y1 = g.bounds
|
| 43 |
+
p = min(h, np.sqrt(g.area) / 2)
|
| 44 |
+
nx, ny = max(1, int(np.ceil((x1 - x0) / p))), max(1, int(np.ceil((y1 - y0) / p)))
|
| 45 |
+
gx, gy = np.meshgrid(x0 + p * np.arange(nx), y0 + p * np.arange(ny))
|
| 46 |
+
pcs = shapely.intersection(shapely.box(gx.ravel(), gy.ravel(), gx.ravel() + p, gy.ravel() + p), g)
|
| 47 |
+
pcs = pcs[shapely.area(pcs) > 1e-12]
|
| 48 |
+
bb = shapely.bounds(pcs)
|
| 49 |
+
cen = shapely.get_coordinates(shapely.centroid(pcs))
|
| 50 |
+
inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1])
|
| 51 |
+
if not inside.all():
|
| 52 |
+
cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs[~inside]))
|
| 53 |
+
cs.append(cen)
|
| 54 |
+
whs.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6))
|
| 55 |
+
os_.append(np.full(len(pcs), a))
|
| 56 |
+
return np.concatenate(cs), np.concatenate(whs), np.concatenate(os_).astype(int)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def window_solve(metals, n_core, h, budget, eps_r, all_rhs, levels=()):
|
| 60 |
+
pol = POLICY[0] if not all_rhs else 'exact'
|
| 61 |
+
cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core)
|
| 62 |
+
t_mesh = t_sol = 0.0
|
| 63 |
+
hh, cap, refined, majority = h, budget, 0, False
|
| 64 |
+
tr = dict(n_core=n_core, n_metals=len(metals), all_rhs=all_rhs, policy=pol, attempts=[])
|
| 65 |
+
best, hist = None, []
|
| 66 |
+
while True:
|
| 67 |
+
t = time.perf_counter()
|
| 68 |
+
c, wh, o, hh, n_raw = _mesh(metals, n_core, hh, cap, refined, majority, levels)
|
| 69 |
+
miss = np.nonzero(np.bincount(o, minlength=len(metals)) == 0)[0]
|
| 70 |
+
att = dict(refined=refined, majority=majority, panels=len(c), missing=miss.tolist())
|
| 71 |
+
t_mesh += time.perf_counter() - t
|
| 72 |
+
tr['attempts'].append(att)
|
| 73 |
+
cs, whs, os_ = c, wh, o
|
| 74 |
+
if pol != 'exact' and len(miss):
|
| 75 |
+
empty = np.array([metals[a].is_empty for a in miss])
|
| 76 |
+
if pol == 'fc' and not refined and (not empty.any() or len(c) <= P.DENSE_MAX):
|
| 77 |
+
t = time.perf_counter()
|
| 78 |
+
if not empty.all():
|
| 79 |
+
r = _rescue_panels(metals, miss[~empty], hh)
|
| 80 |
+
cs, whs, os_ = np.concatenate([c, r[0]]), np.concatenate([wh, r[1]]), np.concatenate([o, r[2]]).astype(o.dtype)
|
| 81 |
+
att['rescued'] = int(len(r[0]))
|
| 82 |
+
if empty.any():
|
| 83 |
+
att['accept'] = 'empty'
|
| 84 |
+
t_mesh += time.perf_counter() - t
|
| 85 |
+
else:
|
| 86 |
+
att['defer'] = True
|
| 87 |
+
if att.get('defer'):
|
| 88 |
+
doomed = len(c) > P.DENSE_MAX
|
| 89 |
+
hist.append(('doomed',) if doomed else ('mesh', c, wh, o))
|
| 90 |
+
att['solve'] = 'doomed' if doomed else 'deferred'
|
| 91 |
+
ok = False
|
| 92 |
+
else:
|
| 93 |
+
t = time.perf_counter()
|
| 94 |
+
try:
|
| 95 |
+
R = _solve(cs, whs, os_, metals, cols, eps_r, all_rhs)
|
| 96 |
+
off = np.ones(R.shape, bool)
|
| 97 |
+
off[np.arange(len(cols)), cols] = False
|
| 98 |
+
ok = bool(np.all(np.bincount(os_, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0)))
|
| 99 |
+
if att.get('accept') == 'empty':
|
| 100 |
+
ok = True
|
| 101 |
+
att['solve'] = 'ok'
|
| 102 |
+
best = R
|
| 103 |
+
hist.append(('R', R))
|
| 104 |
+
except np.linalg.LinAlgError as e:
|
| 105 |
+
ok = False
|
| 106 |
+
att['solve'], att['err'] = 'error', str(e)[:120]
|
| 107 |
+
hist.append(('err',))
|
| 108 |
+
t_sol += time.perf_counter() - t
|
| 109 |
+
if ok or majority:
|
| 110 |
+
break
|
| 111 |
+
if refined < F.LITE_REFINE:
|
| 112 |
+
hh, cap, refined = hh / 2, np.inf, refined + 1
|
| 113 |
+
else:
|
| 114 |
+
majority = True
|
| 115 |
+
if not ok: # frozen R binding: the latest attempt that solved (deferred dense attempts solved lazily, newest first)
|
| 116 |
+
R = None
|
| 117 |
+
t = time.perf_counter()
|
| 118 |
+
for e in reversed(hist):
|
| 119 |
+
if e[0] == 'R':
|
| 120 |
+
R = e[1]
|
| 121 |
+
break
|
| 122 |
+
if e[0] == 'mesh':
|
| 123 |
+
try:
|
| 124 |
+
R = _solve(e[1], e[2], e[3], metals, cols, eps_r, all_rhs)
|
| 125 |
+
break
|
| 126 |
+
except np.linalg.LinAlgError:
|
| 127 |
+
continue
|
| 128 |
+
t_sol += time.perf_counter() - t
|
| 129 |
+
assert R is not None, 'no attempt solved (the frozen code raises here too)'
|
| 130 |
+
else:
|
| 131 |
+
R = best
|
| 132 |
+
tr['fired'] = bool(pol == 'fc' and tr['attempts'][0]['missing'])
|
| 133 |
+
TRACE.append(tr)
|
| 134 |
+
return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh,
|
| 135 |
+
"assemble_s": 0.0, "solve_s": t_sol}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
FILL = [] # one entry per formula_prior call replaced by the dipole fill: number of pairs
|
| 139 |
+
MARGIN = [] # one entry per window whose margin was widened: (d, margin)
|
| 140 |
+
_FROZEN = dict(formula_prior=F.formula_prior, lite_window=F._lite_window, dipole_far=F.dipole_far)
|
| 141 |
+
K_TOL = 1e-4
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def slab_R(d, t_um=350.0, eps_r=11.45):
|
| 145 |
+
"""src/tlsq/embed/fastprior_farv4.slab_R verbatim: R(d) = 1 + 2 sum_n (-K)^n (1 - 8 n^2 t^2/d^2)/(1 + 4 n^2 t^2/d^2)^(5/2)."""
|
| 146 |
+
d = np.asarray(d, float)
|
| 147 |
+
K = (eps_r - 1) / (eps_r + 1)
|
| 148 |
+
n_max = int(np.ceil(np.log(K_TOL) / np.log(K))) if K > 0 else 0
|
| 149 |
+
n = np.arange(1, n_max + 1)[:, None]
|
| 150 |
+
u = (n * t_um / d[None]) ** 2
|
| 151 |
+
return 1 + 2 * np.sum((-K) ** n * (1 - 8 * u) / (1 + 4 * u) ** 2.5, 0)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def dipole_far_slab(G, conds, pairs, eps_r=11.45, gap=None):
|
| 155 |
+
"""src/tlsq/embed/fastprior_farv4.dipole_far_v4 verbatim: the frozen dipole_far with (1 + eps_r) -> 1 + eps_r R(d)."""
|
| 156 |
+
base = _FROZEN['dipole_far'](G, conds, pairs, eps_r, gap)
|
| 157 |
+
pairs = np.asarray(pairs, int).reshape(-1, 2)
|
| 158 |
+
cen = shapely.get_coordinates(shapely.centroid(np.array([G] + list(conds), dtype=object)))
|
| 159 |
+
d = np.hypot(*(cen[pairs[:, 0]] - cen[pairs[:, 1]]).T)
|
| 160 |
+
return base * (1 + eps_r * slab_R(d, float(P.SUBSTRATE["t_um"]), eps_r)) / (1 + eps_r)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def formula_as_dipole(G, conds, eps_r=11.45, step_um=None, R_gaps=None, pairs=None, gap=None, screen_um=None, workers=1, max_core=8):
|
| 164 |
+
"""Replaces formula_prior inside micro_mom (its only caller there: same-pocket far pairs): the same pairs get
|
| 165 |
+
dipole_far(G, conds, pairs, eps_r, gap), the fill micro_mom uses for cross-pocket far pairs."""
|
| 166 |
+
pairs = np.asarray(pairs, int).reshape(-1, 2)
|
| 167 |
+
N = len(conds) + 1
|
| 168 |
+
C = np.zeros((N, N))
|
| 169 |
+
v = F.dipole_far(G, conds, pairs, eps_r, gap)
|
| 170 |
+
C[pairs[:, 0], pairs[:, 1]] = v
|
| 171 |
+
C[pairs[:, 1], pairs[:, 0]] = v
|
| 172 |
+
FILL.append(len(pairs))
|
| 173 |
+
return C, {"dipole_fill_pairs": len(pairs)}
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def lite_window_margin(G, conds, core, R_g, margin, fill_um, tree, etched):
|
| 177 |
+
"""Frozen _lite_window, except margin' = 2 d when d = max distance of a core conductor to the chip ground > margin."""
|
| 178 |
+
d = max(shapely.distance(conds[i], G) for i in core)
|
| 179 |
+
if d > margin:
|
| 180 |
+
MARGIN.append((float(d), float(margin)))
|
| 181 |
+
margin = 2 * d
|
| 182 |
+
return _FROZEN['lite_window'](G, conds, core, R_g, margin, fill_um, tree, etched)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
@contextlib.contextmanager
|
| 186 |
+
def installed(policy='fc', dip=True, margin=False, slab_alpha=None):
|
| 187 |
+
"""Install prior v2 pieces: window solve policy 'fc' (or 'exact' = traced frozen copy, or None = vendor as is), the
|
| 188 |
+
dipole fill of same-pocket far pairs (dip), the window margin widening (margin) and, for arm v2-slab, the slab dipole
|
| 189 |
+
for every far pair with ALPHA_GAP_SHARE = slab_alpha. Always restored."""
|
| 190 |
+
saved = (F._window_solve, F.formula_prior, F._lite_window, F.dipole_far, F.ALPHA_GAP_SHARE)
|
| 191 |
+
if policy is not None:
|
| 192 |
+
POLICY[0] = policy
|
| 193 |
+
F._window_solve = window_solve
|
| 194 |
+
if dip:
|
| 195 |
+
F.formula_prior = formula_as_dipole
|
| 196 |
+
if margin:
|
| 197 |
+
F._lite_window = lite_window_margin
|
| 198 |
+
if slab_alpha is not None:
|
| 199 |
+
F.dipole_far, F.ALPHA_GAP_SHARE = dipole_far_slab, float(slab_alpha)
|
| 200 |
+
try:
|
| 201 |
+
yield
|
| 202 |
+
finally:
|
| 203 |
+
F._window_solve, F.formula_prior, F._lite_window, F.dipole_far, F.ALPHA_GAP_SHARE = saved
|
| 204 |
+
POLICY[0] = 'fc'
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def clear():
|
| 208 |
+
TRACE.clear()
|
| 209 |
+
FILL.clear()
|
| 210 |
+
MARGIN.clear()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def fired(trace):
|
| 214 |
+
"""True if fc changed the solve of any window of the traced call."""
|
| 215 |
+
return any(t.get('fired') for t in trace)
|
geomgnn/rejtail_fv.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""rejtail fix fv (agents/rejtail/RESULTS.md, scripts/rejtail/rt.py; user approval 2026-10-17): value-preserving removal of
|
| 2 |
+
the rejection tail of the windowed micro-MoM prior (`fastprior._window_solve`, all_rhs=False, layouts with > 8 conductors).
|
| 3 |
+
|
| 4 |
+
A window attempt whose mesh leaves a metal without a panel is always rejected, whatever its solve returns. The frozen code
|
| 5 |
+
still solves it, and above DENSE_MAX panels the H-matrix PCG cannot converge: the panel-less metal has a zero right-hand
|
| 6 |
+
side, so the residual is 0/0 = NaN, and it runs 1,000 iterations before raising. fv meshes such attempts but does not
|
| 7 |
+
solve them. If the loop ends without an accepted attempt, the result the unpatched code returns is rebuilt by solving
|
| 8 |
+
the deferred dense attempts lazily:
|
| 9 |
+
keep_last_solved=True (frozen geomfast stage): the latest attempt whose solve succeeded, newest first;
|
| 10 |
+
keep_last_solved=False (tlsq_infer vendor): the last attempt's own solve, else the same RuntimeError.
|
| 11 |
+
Every accepted solve is the same call on the same mesh, so the result is bit-identical (621 layouts in RESULTS.md;
|
| 12 |
+
deploy/tests/test_gnn_space.py re-checks it). all_rhs=True calls (single window, N <= 8) run the original function.
|
| 13 |
+
The same file is shipped as geomgnn/rejtail_fv.py and tlsq_infer/_vendor/rejtail_fv.py; only the import line differs.
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import time
|
| 18 |
+
|
| 19 |
+
import numpy as np
|
| 20 |
+
|
| 21 |
+
from ._tlsq.embed import fastprior as F
|
| 22 |
+
from ._tlsq.embed import proxy as P
|
| 23 |
+
|
| 24 |
+
KEEP_LAST_SOLVED = True
|
| 25 |
+
_ORIG = {}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _solve(c, wh, o, metals, cols, eps_r):
|
| 29 |
+
"""One attempt solved exactly as the original loop does (windowed: LDL^T dense, H-matrix PCG above DENSE_MAX).
|
| 30 |
+
Returns (R, assemble seconds)."""
|
| 31 |
+
t = time.perf_counter()
|
| 32 |
+
t_asm = 0.0
|
| 33 |
+
if len(c) <= P.DENSE_MAX:
|
| 34 |
+
A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential)
|
| 35 |
+
t_asm = time.perf_counter() - t
|
| 36 |
+
Bm = (o[:, None] == cols[None, :]).astype(float)
|
| 37 |
+
Q = F._sym_solve(A, Bm)
|
| 38 |
+
else:
|
| 39 |
+
Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols]
|
| 40 |
+
return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1), t_asm
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: float, all_rhs: bool, levels: tuple = ()) -> tuple:
|
| 44 |
+
if all_rhs:
|
| 45 |
+
return _ORIG["window_solve"](metals, n_core, h, budget, eps_r, all_rhs, levels)
|
| 46 |
+
cols = np.arange(1, 1 + n_core)
|
| 47 |
+
t_mesh = t_asm = t_sol = 0.0
|
| 48 |
+
hh, cap, refined, majority = h, budget, 0, False
|
| 49 |
+
hist = [] # per attempt, oldest first: ("R", R) | ("err",) | ("mesh", c, wh, o) deferred dense | ("doomed",) deferred H-matrix
|
| 50 |
+
while True:
|
| 51 |
+
t = time.perf_counter()
|
| 52 |
+
if not refined:
|
| 53 |
+
c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels)
|
| 54 |
+
elif levels:
|
| 55 |
+
c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority)
|
| 56 |
+
else:
|
| 57 |
+
c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority)
|
| 58 |
+
t_mesh += time.perf_counter() - t
|
| 59 |
+
if not np.all(np.bincount(o, minlength=len(metals)) > 0):
|
| 60 |
+
hist.append(("doomed",) if len(c) > P.DENSE_MAX else ("mesh", c, wh, o))
|
| 61 |
+
ok = False
|
| 62 |
+
else:
|
| 63 |
+
t = time.perf_counter()
|
| 64 |
+
try:
|
| 65 |
+
R, ta = _solve(c, wh, o, metals, cols, eps_r)
|
| 66 |
+
t_asm += ta
|
| 67 |
+
hist.append(("R", R))
|
| 68 |
+
ok = True
|
| 69 |
+
except np.linalg.LinAlgError:
|
| 70 |
+
hist.append(("err",))
|
| 71 |
+
ok = False
|
| 72 |
+
t_sol += time.perf_counter() - t
|
| 73 |
+
if ok or majority:
|
| 74 |
+
break
|
| 75 |
+
if refined < F.LITE_REFINE:
|
| 76 |
+
hh, cap, refined = hh / 2, np.inf, refined + 1
|
| 77 |
+
else:
|
| 78 |
+
majority = True
|
| 79 |
+
if ok:
|
| 80 |
+
R = hist[-1][1]
|
| 81 |
+
else:
|
| 82 |
+
R = None
|
| 83 |
+
t = time.perf_counter()
|
| 84 |
+
for e in reversed(hist if KEEP_LAST_SOLVED else hist[-1:]):
|
| 85 |
+
if e[0] == "R":
|
| 86 |
+
R = e[1]
|
| 87 |
+
break
|
| 88 |
+
if e[0] == "mesh":
|
| 89 |
+
try:
|
| 90 |
+
R = _solve(e[1], e[2], e[3], metals, cols, eps_r)[0]
|
| 91 |
+
break
|
| 92 |
+
except np.linalg.LinAlgError:
|
| 93 |
+
continue
|
| 94 |
+
t_sol += time.perf_counter() - t
|
| 95 |
+
if R is None:
|
| 96 |
+
raise RuntimeError(f"window solve failed: no positive-definite system after {refined} refinements (h={hh:.4g} um); "
|
| 97 |
+
"the window has no valid solution at this mesh")
|
| 98 |
+
return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh,
|
| 99 |
+
"assemble_s": t_asm, "solve_s": t_sol}
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def install() -> None:
|
| 103 |
+
"""Replace fastprior._window_solve (a module global, looked up at call time, also in the forked window workers)."""
|
| 104 |
+
if not _ORIG:
|
| 105 |
+
_ORIG["window_solve"] = F._window_solve
|
| 106 |
+
F._window_solve = window_solve
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def uninstall() -> None:
|
| 110 |
+
if _ORIG:
|
| 111 |
+
F._window_solve = _ORIG.pop("window_solve")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def installed() -> bool:
|
| 115 |
+
return bool(_ORIG)
|
geomgnn/schema_p2d.json
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nodekeys": [
|
| 3 |
+
"compactness",
|
| 4 |
+
"fourier_00",
|
| 5 |
+
"fourier_01",
|
| 6 |
+
"fourier_02",
|
| 7 |
+
"fourier_03",
|
| 8 |
+
"fourier_04",
|
| 9 |
+
"fourier_05",
|
| 10 |
+
"fourier_06",
|
| 11 |
+
"fourier_07",
|
| 12 |
+
"fourier_08",
|
| 13 |
+
"fourier_09",
|
| 14 |
+
"fourier_10",
|
| 15 |
+
"fourier_11",
|
| 16 |
+
"fourier_12",
|
| 17 |
+
"fourier_13",
|
| 18 |
+
"fourier_14",
|
| 19 |
+
"fourier_15",
|
| 20 |
+
"fourier_16",
|
| 21 |
+
"fourier_17",
|
| 22 |
+
"fourier_18",
|
| 23 |
+
"fourier_19",
|
| 24 |
+
"fourier_20",
|
| 25 |
+
"fourier_21",
|
| 26 |
+
"fourier_22",
|
| 27 |
+
"fourier_23",
|
| 28 |
+
"fourier_24",
|
| 29 |
+
"fourier_25",
|
| 30 |
+
"fourier_26",
|
| 31 |
+
"fourier_27",
|
| 32 |
+
"fourier_28",
|
| 33 |
+
"fourier_29",
|
| 34 |
+
"fourier_30",
|
| 35 |
+
"fourier_31",
|
| 36 |
+
"fourier_32",
|
| 37 |
+
"gran_00",
|
| 38 |
+
"gran_01",
|
| 39 |
+
"gran_02",
|
| 40 |
+
"gran_03",
|
| 41 |
+
"gran_04",
|
| 42 |
+
"gran_05",
|
| 43 |
+
"gran_06",
|
| 44 |
+
"gran_07",
|
| 45 |
+
"gran_08",
|
| 46 |
+
"gran_09",
|
| 47 |
+
"gran_10",
|
| 48 |
+
"gran_11",
|
| 49 |
+
"gran_12",
|
| 50 |
+
"gran_13",
|
| 51 |
+
"gran_14",
|
| 52 |
+
"gran_15",
|
| 53 |
+
"log_Imajor_over_A2",
|
| 54 |
+
"log_area",
|
| 55 |
+
"log_aspect",
|
| 56 |
+
"log_elongation",
|
| 57 |
+
"log_min_halfwidth",
|
| 58 |
+
"log_perimeter",
|
| 59 |
+
"log_width",
|
| 60 |
+
"n_holes",
|
| 61 |
+
"role_ground",
|
| 62 |
+
"solidity",
|
| 63 |
+
"missing_log_min_halfwidth"
|
| 64 |
+
],
|
| 65 |
+
"edgekeys": [
|
| 66 |
+
"is_ground_pair",
|
| 67 |
+
"lg_log_cps",
|
| 68 |
+
"lg_log_dmin",
|
| 69 |
+
"lg_mom_0.5",
|
| 70 |
+
"lg_mom_1",
|
| 71 |
+
"lg_mom_2",
|
| 72 |
+
"lg_sep_00",
|
| 73 |
+
"lg_sep_01",
|
| 74 |
+
"lg_sep_02",
|
| 75 |
+
"lg_sep_03",
|
| 76 |
+
"lg_sep_04",
|
| 77 |
+
"lg_sep_05",
|
| 78 |
+
"lg_sep_06",
|
| 79 |
+
"lg_sep_07",
|
| 80 |
+
"lg_sep_08",
|
| 81 |
+
"lg_sep_09",
|
| 82 |
+
"log_cps",
|
| 83 |
+
"log_dmin",
|
| 84 |
+
"lt_e150",
|
| 85 |
+
"lt_e400",
|
| 86 |
+
"lt_e50",
|
| 87 |
+
"lt_g150",
|
| 88 |
+
"lt_g400",
|
| 89 |
+
"lt_g50",
|
| 90 |
+
"lt_o150",
|
| 91 |
+
"lt_o400",
|
| 92 |
+
"lt_o50",
|
| 93 |
+
"mom_0.5",
|
| 94 |
+
"mom_1",
|
| 95 |
+
"mom_2",
|
| 96 |
+
"sep_00",
|
| 97 |
+
"sep_01",
|
| 98 |
+
"sep_02",
|
| 99 |
+
"sep_03",
|
| 100 |
+
"sep_04",
|
| 101 |
+
"sep_05",
|
| 102 |
+
"sep_06",
|
| 103 |
+
"sep_07",
|
| 104 |
+
"sep_08",
|
| 105 |
+
"sep_09",
|
| 106 |
+
"share_absdiff",
|
| 107 |
+
"share_mean"
|
| 108 |
+
],
|
| 109 |
+
"globkeys": [
|
| 110 |
+
"log_chip_area",
|
| 111 |
+
"log_pocket_area",
|
| 112 |
+
"log_substrate_t",
|
| 113 |
+
"eps_eff"
|
| 114 |
+
],
|
| 115 |
+
"base_sha": "03edcbc72325f1522ad3998aff25b9b0ec86e0af4f479a872c76b144c45605bc",
|
| 116 |
+
"extra_sha": "b72762b01169a9e754265a7fc256c4f8a11930a235da340ebc948e04463007c1",
|
| 117 |
+
"physics": {
|
| 118 |
+
"prior_method": "local_gap",
|
| 119 |
+
"alpha": 0.49685219816506904,
|
| 120 |
+
"ensemble": "arithmetic mean of complete predicted Maxwell matrices",
|
| 121 |
+
"audit": "prior_audit.json"
|
| 122 |
+
}
|
| 123 |
+
}
|
geomgnn/train_mf.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""multifid arms (agents/multifid/PROTOCOL.md): P2d recipe (geomfast ragged.py fit) with a chosen MoM level set, an optional
|
| 2 |
+
learned convergence-curve extrapolation with a MoM-limit anchor, and a detached variance head. TRAIN fits; VAL predicted.
|
| 3 |
+
prepare: prepared_p2d shards + fresh levels -> multifid/prepared/n*.npz (lv: B x F x 4 at ppg 0.375, 0.75, 1.5, 3)
|
| 4 |
+
fit --arm R|L2|L2x|L3|L3x --seed s"""
|
| 5 |
+
import os
|
| 6 |
+
os.environ.setdefault('PYTHONDONTWRITEBYTECODE', '1')
|
| 7 |
+
import argparse, json, pickle, time, hashlib, platform
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
GF = Path(os.environ.get('MF_GF', '/project2/shaas_31/shanto/geomfast'))
|
| 12 |
+
OUT = Path(os.environ.get('MF_OUT', '/project2/shaas_31/shanto/multifid'))
|
| 13 |
+
PPG = [0.375, 0.75, 1.5, 3.0]
|
| 14 |
+
ARMS = {'R': ([1.5, 3.0], False), 'L2': ([0.375, 0.75], False), 'L2x': ([0.375, 0.75], True),
|
| 15 |
+
'L3': ([0.375, 0.75, 1.5], False), 'L3x': ([0.375, 0.75, 1.5], True)}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def digest(p):
|
| 19 |
+
return hashlib.sha256(Path(p).read_bytes()).hexdigest()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def prepare(a):
|
| 23 |
+
recs = {}
|
| 24 |
+
for f in sorted(list((OUT / 'levels_hpc').glob('*.pkl')) + list((OUT / 'levels').glob('*.pkl'))):
|
| 25 |
+
if f.name.endswith('.partial.pkl'):
|
| 26 |
+
continue
|
| 27 |
+
d = pickle.load(f.open('rb'))
|
| 28 |
+
assert not d['meta']['errors'], (f, d['meta']['errors'])
|
| 29 |
+
recs.update(d['records'])
|
| 30 |
+
out = OUT / 'prepared'
|
| 31 |
+
out.mkdir(exist_ok=False)
|
| 32 |
+
for p in sorted((GF / 'prepared_p2d').glob('n*.npz')):
|
| 33 |
+
z = dict(np.load(p, allow_pickle=False))
|
| 34 |
+
B, F = z['pri'].shape[:2]
|
| 35 |
+
lv = np.zeros((B, F, 4), np.float32)
|
| 36 |
+
for b, k in enumerate(z['key']):
|
| 37 |
+
r = recs[str(k)]['levels']
|
| 38 |
+
lv[b, :, 0] = r[0.375]['mutual']
|
| 39 |
+
lv[b, :, 1] = r[0.75]['mutual']
|
| 40 |
+
lv[b, :, 2:] = z['pri'][b] # cached P2d priors at 1.5, 3 (parity-checked on VAL)
|
| 41 |
+
assert np.isfinite(lv).all() and (lv > 0).all()
|
| 42 |
+
z['lv'] = lv
|
| 43 |
+
np.savez_compressed(out / p.name, **z)
|
| 44 |
+
(out / 'schema.json').write_text((GF / 'prepared_p2d/schema.json').read_text())
|
| 45 |
+
print('PREPARED', len(recs), flush=True)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def build(dx, de, dg, rounds, width=64):
|
| 49 |
+
import torch
|
| 50 |
+
from torch import nn
|
| 51 |
+
|
| 52 |
+
def mlp(i, o):
|
| 53 |
+
return nn.Sequential(nn.Linear(i, width), nn.SiLU(), nn.Linear(width, o))
|
| 54 |
+
|
| 55 |
+
class Net(nn.Module):
|
| 56 |
+
def __init__(self):
|
| 57 |
+
super().__init__()
|
| 58 |
+
self.node = mlp(dx + dg, width)
|
| 59 |
+
self.edge = mlp(de, width)
|
| 60 |
+
self.msg = nn.ModuleList([mlp(3 * width, width) for _ in range(rounds)])
|
| 61 |
+
self.up = nn.ModuleList([mlp(2 * width, width) for _ in range(rounds)])
|
| 62 |
+
self.head = mlp(3 * width, 2)
|
| 63 |
+
self.var = mlp(3 * width, 1)
|
| 64 |
+
self.closure = mlp(width, 1)
|
| 65 |
+
for m in [self.head, self.closure]:
|
| 66 |
+
nn.init.zeros_(m[-1].weight); nn.init.zeros_(m[-1].bias)
|
| 67 |
+
|
| 68 |
+
def forward(self, X, E, G, M, PM, pairs, anchor, slope, bbase, curve):
|
| 69 |
+
B, N, _ = X.shape
|
| 70 |
+
i, j = pairs[:, 0], pairs[:, 1]
|
| 71 |
+
h = self.node(torch.cat([X, G[:, None].expand(-1, N, -1)], -1)) * M[:, :, None]
|
| 72 |
+
e = self.edge(E)
|
| 73 |
+
for msg, up in zip(self.msg, self.up):
|
| 74 |
+
v = msg(torch.cat([h[:, i] + h[:, j], abs(h[:, i] - h[:, j]), e], -1)) * PM[:, :, None]
|
| 75 |
+
agg = torch.zeros_like(h); agg.index_add_(1, i, v); agg.index_add_(1, j, v)
|
| 76 |
+
deg = torch.zeros_like(M, dtype=X.dtype); deg.index_add_(1, i, PM.float()); deg.index_add_(1, j, PM.float())
|
| 77 |
+
h = (h + up(torch.cat([h, agg / deg.clamp_min(1)[:, :, None]], -1))) * M[:, :, None]
|
| 78 |
+
feat = torch.cat([h[:, i] + h[:, j], abs(h[:, i] - h[:, j]), e], -1)
|
| 79 |
+
out = self.head(feat)
|
| 80 |
+
r, q = out[..., 0], out[..., 1]
|
| 81 |
+
p = 0.25 + 2.25 * torch.sigmoid(q)
|
| 82 |
+
m = anchor + slope / (2.0 ** p - 1.0) if curve else anchor
|
| 83 |
+
logc = (m + 0.2 * torch.tanh(r)).clamp(-25, 20)
|
| 84 |
+
logvar = self.var(feat.detach()).squeeze(-1).clamp(-16, 4)
|
| 85 |
+
mutual = logc.exp() * PM
|
| 86 |
+
b = (bbase + self.closure(h).squeeze(-1)).clamp(-20, 20).exp() * M
|
| 87 |
+
C = torch.zeros((B, N, N), device=X.device)
|
| 88 |
+
C[:, i, j] = -mutual; C[:, j, i] = -mutual
|
| 89 |
+
C = C + torch.diag_embed(-C.sum(-1) + b)
|
| 90 |
+
return C, logc, m, p, logvar
|
| 91 |
+
return Net()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def fit(a):
|
| 95 |
+
import torch
|
| 96 |
+
from torch.nn import functional as F
|
| 97 |
+
torch.set_num_threads(a.threads); torch.manual_seed(a.seed); rng = np.random.default_rng(a.seed)
|
| 98 |
+
levels, curve = ARMS[a.arm]
|
| 99 |
+
li = [PPG.index(p) for p in levels]
|
| 100 |
+
out = OUT / 'runs' / f'{a.arm}_s{a.seed}'
|
| 101 |
+
out.mkdir(parents=True, exist_ok=False)
|
| 102 |
+
root = OUT / 'prepared'
|
| 103 |
+
schema = json.loads((root / 'schema.json').read_text())
|
| 104 |
+
ds = [dict(np.load(p, allow_pickle=False)) for p in sorted(root.glob('n*.npz'))]
|
| 105 |
+
ei = schema['edgekeys'].index('is_ground_pair')
|
| 106 |
+
closure = {}
|
| 107 |
+
for flag in [0, 1]:
|
| 108 |
+
bs = []
|
| 109 |
+
for d in ds:
|
| 110 |
+
tr = d['split'] == 'train'
|
| 111 |
+
bs.extend(np.log(d['C'].sum(-1).clip(1e-8)[(d['sig'] == bool(flag)) & tr[:, None]]).tolist())
|
| 112 |
+
closure[str(flag)] = float(np.median(bs))
|
| 113 |
+
for d in ds:
|
| 114 |
+
L = np.log(d['lv'].astype(np.float64))
|
| 115 |
+
lf, lc = L[:, :, li[-1]], L[:, :, li[-2]]
|
| 116 |
+
d['slope'] = (lf - lc).astype(np.float32)
|
| 117 |
+
d['anchor'] = (lf if curve else 2 * lf - lc).astype(np.float32)
|
| 118 |
+
d['fine'] = (2 * L[:, :, 3] - L[:, :, 2]).astype(np.float32) # MoM-limit proxy (anchor target, TRAIN only)
|
| 119 |
+
d['E'] = np.concatenate([d['E']] + [(L[:, :, li[k + 1]] - L[:, :, li[k]])[:, :, None].astype(np.float32)
|
| 120 |
+
for k in range(len(li) - 1)], -1)
|
| 121 |
+
d['bbase'] = np.where(d['sig'], closure['1'], closure['0']).astype(np.float32)
|
| 122 |
+
scales = {}
|
| 123 |
+
for name in ['X', 'E', 'G']:
|
| 124 |
+
v = np.concatenate([d[name][d['split'] == 'train'].reshape(-1, d[name].shape[-1]) for d in ds])
|
| 125 |
+
med = np.nanmedian(v, 0); iq = np.nanpercentile(v, 75, 0) - np.nanpercentile(v, 25, 0); iq = np.where(iq > 1e-6, iq, 1)
|
| 126 |
+
scales[name] = dict(median=med.tolist(), iqr=iq.tolist())
|
| 127 |
+
for d in ds:
|
| 128 |
+
d[name] = np.clip((np.where(np.isfinite(d[name]), d[name], med) - med) / iq, -10, 10).astype(np.float32)
|
| 129 |
+
tensors = []; groups = {}
|
| 130 |
+
for k, d in enumerate(ds):
|
| 131 |
+
z = {n: torch.from_numpy(d[n]) for n in ['X', 'E', 'G', 'C', 'sig', 'pairs', 'anchor', 'slope', 'fine', 'bbase']}
|
| 132 |
+
z['M'] = torch.ones(d['sig'].shape, dtype=torch.bool); z['PM'] = torch.ones(d['anchor'].shape, dtype=torch.bool)
|
| 133 |
+
tensors.append(z)
|
| 134 |
+
for fam in set(d['family']):
|
| 135 |
+
idx = np.flatnonzero((d['family'] == fam) & (d['split'] == 'train'))
|
| 136 |
+
if len(idx):
|
| 137 |
+
groups.setdefault(fam, []).append((k, idx))
|
| 138 |
+
families = sorted(groups)
|
| 139 |
+
model = build(ds[0]['X'].shape[-1], ds[0]['E'].shape[-1], ds[0]['G'].shape[-1], 2)
|
| 140 |
+
opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4)
|
| 141 |
+
sched = torch.optim.lr_scheduler.CosineAnnealingLR(opt, a.steps, eta_min=1e-5)
|
| 142 |
+
|
| 143 |
+
def fwd(k, b):
|
| 144 |
+
z = tensors[k]
|
| 145 |
+
return model(z['X'][b], z['E'][b], z['G'][b], z['M'][b], z['PM'][b], z['pairs'], z['anchor'][b], z['slope'][b],
|
| 146 |
+
z['bbase'][b], curve)
|
| 147 |
+
hist = []; start = time.perf_counter()
|
| 148 |
+
for step in range(a.steps):
|
| 149 |
+
fam = families[rng.integers(len(families))]; gs = groups[fam]; w = np.array([len(v) for _, v in gs])
|
| 150 |
+
k, idx = gs[rng.choice(len(gs), p=w / w.sum())]; z = tensors[k]; i, j = z['pairs'].T
|
| 151 |
+
b = rng.choice(idx, max(1, min(64, 8192 // len(z['pairs']))))
|
| 152 |
+
pred, logc, m, p, logvar = fwd(k, b); C = z['C'][b]
|
| 153 |
+
y = (-C[:, i, j]).clamp_min(1e-12).log()
|
| 154 |
+
pl = F.huber_loss(logc, y, delta=.05)
|
| 155 |
+
dl = F.huber_loss(pred.diagonal(dim1=-2, dim2=-1).log(), C.diagonal(dim1=-2, dim2=-1).log(), delta=.05, reduction='none')
|
| 156 |
+
sig = z['sig'][b]
|
| 157 |
+
loss = pl + .1 * dl[sig].mean() + .02 * dl[~sig].mean()
|
| 158 |
+
if curve:
|
| 159 |
+
loss = loss + .5 * F.huber_loss(m, z['fine'][b], delta=.05)
|
| 160 |
+
res = (logc - y).detach()
|
| 161 |
+
loss = loss + .1 * (0.5 * (logvar + res ** 2 / logvar.exp())).mean()
|
| 162 |
+
assert torch.isfinite(loss)
|
| 163 |
+
opt.zero_grad(); loss.backward(); torch.nn.utils.clip_grad_norm_(model.parameters(), 5); opt.step(); sched.step()
|
| 164 |
+
if step % 1000 == 0:
|
| 165 |
+
hist.append(dict(step=step, loss=float(loss.detach()), seconds=time.perf_counter() - start)); print(hist[-1], flush=True)
|
| 166 |
+
train_s = time.perf_counter() - start
|
| 167 |
+
torch.save(dict(state=model.state_dict(), arm=a.arm, levels=levels, curve=curve, scales=scales, closure=closure,
|
| 168 |
+
seed=a.seed, schema=schema, data_sha={p.name: digest(p) for p in root.glob('*.npz')}, code_sha=digest(__file__)),
|
| 169 |
+
out / 'model.pt')
|
| 170 |
+
model.eval(); preds = {}
|
| 171 |
+
with torch.inference_mode():
|
| 172 |
+
for k, d in enumerate(ds):
|
| 173 |
+
for b in np.flatnonzero(d['split'] == 'val'):
|
| 174 |
+
C, logc, m, p, logvar = fwd(k, [b])
|
| 175 |
+
preds[str(d['key'][b])] = dict(C=C[0].numpy(), logc=logc[0].numpy(), m=m[0].numpy(), p=p[0].numpy(),
|
| 176 |
+
logvar=logvar[0].numpy())
|
| 177 |
+
pickle.dump(preds, (out / 'validation_predictions.pkl').open('wb'))
|
| 178 |
+
ci = Path('/proc/cpuinfo')
|
| 179 |
+
cpu = next((s.split(':', 1)[1].strip() for s in ci.read_text().splitlines() if s.startswith('model name')), '?') if ci.exists() else platform.processor()
|
| 180 |
+
(out / 'run.json').write_text(json.dumps(dict(arm=a.arm, seed=a.seed, steps=a.steps, host=platform.node(), cpu=cpu,
|
| 181 |
+
threads=a.threads, train_seconds=train_s, history=hist), indent=2))
|
| 182 |
+
print('DONE', a.arm, a.seed, train_s, flush=True)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
if __name__ == '__main__':
|
| 186 |
+
ap = argparse.ArgumentParser(); sub = ap.add_subparsers(dest='cmd', required=True)
|
| 187 |
+
sub.add_parser('prepare')
|
| 188 |
+
q = sub.add_parser('fit'); q.add_argument('--arm', choices=list(ARMS), required=True); q.add_argument('--seed', type=int, default=0)
|
| 189 |
+
q.add_argument('--steps', type=int, default=18000); q.add_argument('--threads', type=int, default=2)
|
| 190 |
+
a = ap.parse_args(); {'prepare': prepare, 'fit': fit}[a.cmd](a)
|
geomgnn/x1_core.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""X1 shared model code (agents/v4test/PROTOCOL.md): row -> arrays, multifid forward, surrogate priors. No solver, no labels."""
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
PPG = (0.375, 0.75, 1.5, 3.0)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def schema_of(ck):
|
| 8 |
+
s = ck.get('schema', ck)
|
| 9 |
+
return s['nodekeys'], s['edgekeys'], s.get('globkeys', ['log_chip_area', 'log_pocket_area', 'log_substrate_t', 'eps_eff'])
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def raw_arrays(row, nk, ek, gk):
|
| 13 |
+
"""Unnormalised X (n, nk), E (P, ek), G (gk) exactly as geomfast infer.Predictor.from_features builds them."""
|
| 14 |
+
n = len(row['names'])
|
| 15 |
+
pairs = np.array([(i, j) for i in range(n) for j in range(i + 1, n)])
|
| 16 |
+
X = np.array([[(float(not np.isfinite(nd['log_min_halfwidth'])) if k == 'missing_log_min_halfwidth' else nd[k]) for k in nk]
|
| 17 |
+
for nd in row['nodes']], np.float32)
|
| 18 |
+
E = np.array([[row['edges'][tuple(p)][k] for k in ek] for p in pairs], np.float32).reshape(len(pairs), len(ek))
|
| 19 |
+
G = np.array([row['global'][k] for k in gk], np.float32)
|
| 20 |
+
return X, E, G, pairs
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def norm(z, scale):
|
| 24 |
+
med, iqr = np.array(scale['median']), np.array(scale['iqr'])
|
| 25 |
+
return np.clip((np.where(np.isfinite(z), z, med) - med) / iqr, -10, 10).astype(np.float32)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class Multifid:
|
| 29 |
+
"""multifid residual arm (scripts/multifid/train_mf.py Net), seeds averaged as full matrices (analyze.py)."""
|
| 30 |
+
|
| 31 |
+
def __init__(self, paths, threads=2):
|
| 32 |
+
import torch
|
| 33 |
+
from .train_mf import build
|
| 34 |
+
torch.set_num_threads(threads)
|
| 35 |
+
self.models = []
|
| 36 |
+
for p in paths:
|
| 37 |
+
c = torch.load(p, map_location='cpu', weights_only=False)
|
| 38 |
+
assert not c['curve']
|
| 39 |
+
m = build(len(c['scales']['X']['median']), len(c['scales']['E']['median']), len(c['scales']['G']['median']), 2)
|
| 40 |
+
m.load_state_dict(c['state'])
|
| 41 |
+
m.eval()
|
| 42 |
+
self.models.append((c, m))
|
| 43 |
+
self.levels = self.models[0][0]['levels']
|
| 44 |
+
assert all(c['levels'] == self.levels for c, _ in self.models)
|
| 45 |
+
|
| 46 |
+
def predict(self, row, levels):
|
| 47 |
+
"""levels: {ppg: positive mutual matrix (n, n)} in row order."""
|
| 48 |
+
nk, ek, gk = schema_of(self.models[0][0])
|
| 49 |
+
X, E, G, pairs = raw_arrays(row, nk, ek, gk)
|
| 50 |
+
i, j = pairs.T
|
| 51 |
+
sig = np.array([nm != 'G' for nm in row['names']])
|
| 52 |
+
return self.predict_arrays(X, E, G, pairs, sig, {p: np.asarray(levels[p], float)[i, j] for p in self.levels})
|
| 53 |
+
|
| 54 |
+
def predict_arrays(self, X0, E0, G0, pairs, sig, lev):
|
| 55 |
+
"""raw (unnormalised) X (n, f), E (P, f), G (g), pairs (P, 2), sig (n,), lev {ppg: positive mutual (P,)}."""
|
| 56 |
+
import torch
|
| 57 |
+
out = []
|
| 58 |
+
for c, m in self.models:
|
| 59 |
+
assert schema_of(c) == schema_of(self.models[0][0])
|
| 60 |
+
L = [np.log(np.asarray(lev[p], float)) for p in self.levels]
|
| 61 |
+
assert all(np.isfinite(v).all() for v in L)
|
| 62 |
+
X, E, G = X0, E0, G0
|
| 63 |
+
lf, lc = L[-1], L[-2]
|
| 64 |
+
anchor, slope = 2 * lf - lc, lf - lc
|
| 65 |
+
E = np.concatenate([E] + [(L[k + 1] - L[k])[:, None].astype(np.float32) for k in range(len(L) - 1)], -1)
|
| 66 |
+
X, E, G = norm(X, c['scales']['X'])[None], norm(E, c['scales']['E'])[None], norm(G[None], c['scales']['G'])
|
| 67 |
+
bbase = np.where(sig, c['closure']['1'], c['closure']['0']).astype(np.float32)[None]
|
| 68 |
+
T = lambda a: torch.from_numpy(np.ascontiguousarray(a, dtype=np.float32)) # noqa: E731
|
| 69 |
+
n = len(sig)
|
| 70 |
+
with torch.inference_mode():
|
| 71 |
+
C = m(T(X), T(E), T(G), torch.ones((1, n), dtype=torch.bool), torch.ones((1, len(pairs)), dtype=torch.bool),
|
| 72 |
+
torch.from_numpy(pairs), T(anchor[None]), T(slope[None]), T(bbase), False)[0]
|
| 73 |
+
out.append(C[0].numpy().astype(np.float64))
|
| 74 |
+
r = np.mean(out, 0)
|
| 75 |
+
assert np.isfinite(r).all()
|
| 76 |
+
return r
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
class Surrogate:
|
| 80 |
+
"""learnedprior surrogate (lp_surrogate.predict_bucket on lp_label.pack(row)) -> priors at ppg 1.5, 3."""
|
| 81 |
+
|
| 82 |
+
def __init__(self, path):
|
| 83 |
+
from . import lp_surrogate as S
|
| 84 |
+
self.S = S
|
| 85 |
+
self.model, self.prep, _ = S.load_model(path, 'cpu')
|
| 86 |
+
sch = S.SCHEMA
|
| 87 |
+
self.nk, self.ek, self.gk = sch['nodekeys'], sch['edgekeys'], sch['globkeys']
|
| 88 |
+
|
| 89 |
+
def priors(self, row):
|
| 90 |
+
X, E, G, pairs = raw_arrays(row, self.nk, self.ek, self.gk)
|
| 91 |
+
n = len(row['names'])
|
| 92 |
+
lp = self.S.predict_bucket(self.model, self.prep, dict(n=n, X=X[None], E=E[None], G=G[None]), 'cpu')[0]
|
| 93 |
+
iu = np.triu_indices(n, 1)
|
| 94 |
+
pri = {}
|
| 95 |
+
for k, p in enumerate((1.5, 3.0)):
|
| 96 |
+
M = np.zeros((n, n))
|
| 97 |
+
M[iu] = np.exp(lp[:, k])
|
| 98 |
+
pri[p] = M + M.T
|
| 99 |
+
return pri
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def c0(levels, lo=1.5, hi=3.0):
|
| 103 |
+
"""Richardson extrapolation in log of positive mutual / self matrices: exp(2 log c(hi) - log c(lo)); Maxwell form."""
|
| 104 |
+
a, b = np.abs(np.asarray(levels[lo], float)), np.abs(np.asarray(levels[hi], float))
|
| 105 |
+
m = np.exp(2 * np.log(np.maximum(b, 1e-300)) - np.log(np.maximum(a, 1e-300)))
|
| 106 |
+
n = len(m)
|
| 107 |
+
off = ~np.eye(n, dtype=bool)
|
| 108 |
+
C = np.where(off, -m, m)
|
| 109 |
+
return C
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def to_maxwell(P):
|
| 113 |
+
"""stored prior P = -C (positive mutuals) -> Maxwell C."""
|
| 114 |
+
return -np.asarray(P, float)
|
models/{all-data-base/seed0.pt → accurate/acc_v2m_s0.pt}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:56109a61bb264151ca7a650cd79a516d60dcf6c92f257ded38ff9ad97a1421a2
|
| 3 |
+
size 8410286
|
models/{all-data-base/seed1.pt → accurate/acc_v2m_s1.pt}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ae9c4be59caa84eb6a220a91e18fce696e98e3a9c3aab09e16ac727e7fc2d898
|
| 3 |
+
size 8410286
|
models/{all-data-base/seed2.pt → accurate/acc_v2m_s2.pt}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0841acc6a3db32e2d5def72443445361c4ae41ac782b38b7856a78f0547b1e19
|
| 3 |
+
size 8410286
|
models/accurate/info.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "accurate",
|
| 3 |
+
"kind": "gnn",
|
| 4 |
+
"checkpoints": [
|
| 5 |
+
"acc_v2m_s0.pt",
|
| 6 |
+
"acc_v2m_s1.pt",
|
| 7 |
+
"acc_v2m_s2.pt"
|
| 8 |
+
],
|
| 9 |
+
"checkpoints_sha256": [
|
| 10 |
+
"56109a61bb264151ca7a650cd79a516d60dcf6c92f257ded38ff9ad97a1421a2",
|
| 11 |
+
"ae9c4be59caa84eb6a220a91e18fce696e98e3a9c3aab09e16ac727e7fc2d898",
|
| 12 |
+
"0841acc6a3db32e2d5def72443445361c4ae41ac782b38b7856a78f0547b1e19"
|
| 13 |
+
],
|
| 14 |
+
"checkpoints_source": [
|
| 15 |
+
"data_local/flagship_v4/hpc/runs/acc_v2m_s0/model.pt",
|
| 16 |
+
"data_local/flagship_v4/hpc/runs/acc_v2m_s1/model.pt",
|
| 17 |
+
"data_local/flagship_v4/hpc/runs/acc_v2m_s2/model.pt"
|
| 18 |
+
],
|
| 19 |
+
"surrogate": [],
|
| 20 |
+
"surrogate_sha256": [],
|
| 21 |
+
"surrogate_source": []
|
| 22 |
+
}
|
models/all-data-base/config.json
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
{"name": "all-data-base", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]}
|
|
|
|
|
|
models/all-data-flagship/config.json
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
{"name": "all-data-flagship", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]}
|
|
|
|
|
|
models/all-data-flagship/seed1.pt
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:cecfb8d90711dbd0d37446f1dcff7154443fe487d45a3bd9af99ba686e6c4f31
|
| 3 |
-
size 848449
|
|
|
|
|
|
|
|
|
|
|
|
models/all-data-flagship/seed2.pt
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:4bbcfc763a63eeb8dd3f781051a359cd78f2cf8ef7b54818067f9f9d75788269
|
| 3 |
-
size 848449
|
|
|
|
|
|
|
|
|
|
|
|
models/all-data-lite1500/config.json
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
{"name": "all-data-lite1500", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]}
|
|
|
|
|
|
models/all-data-lite1500/seed0.pt
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:62b0e3e4f63a57a5ed467bf97ffa17cd039c69b6e750e606d5e8fd1fe70cc946
|
| 3 |
-
size 848449
|
|
|
|
|
|
|
|
|
|
|
|
models/all-data-lite1500/seed1.pt
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:3ac5907d1999fae825bda3a46654775497474834d943d2dc7c9e6961001a6050
|
| 3 |
-
size 848449
|
|
|
|
|
|
|
|
|
|
|
|