diff --git a/.tlsq_deploy b/.tlsq_deploy index 76a163a85f7f4e836d2e71d43e54b983910073ef..a21598a22268c935a8b87eb7e7d461b275b0bf71 100644 --- a/.tlsq_deploy +++ b/.tlsq_deploy @@ -1 +1 @@ -managed by deploy/sync.py (registry v1) +managed by deploy/sync.py --flagship (registry v1) diff --git a/LIMITS.md b/LIMITS.md deleted file mode 100644 index bced85fdbe162c3bd447c89607620a3fdb270713..0000000000000000000000000000000000000000 --- a/LIMITS.md +++ /dev/null @@ -1,201 +0,0 @@ -# Deployment limits (tlsq_infer 0.1.0) - -Measured, not estimated. Sources: `deploy/tests/results/stress.json` (local, 2-thread budget), -`deploy/tests/results/reproduction_*.json` (accuracy vs crop), `deploy/tests/results/robustness.json`. - -## 1. Local, 2-thread budget (HF free-tier proxy) - -Apple M-series host, `torch.set_num_threads(2)`, OMP/BLAS = 2 threads, one fresh process per case (peak RSS of that -process). Model pair-local unless stated. Wall = prepare + MoM prior + features + network. - -| sweep | value | wall (s) | MoM prior (s) | features (s) | MoM cells | peak RSS (GB) | -|---|---|---|---|---|---|---| -| two pads, gap 5 µm, size (µm) | 1,020 | 0.8 | 0.8 | 0.03 | 6,658 | 1.04 | -| | 1,620 | 0.9 | 0.8 | 0.09 | 4,964 | 0.70 | -| | 4,820 | 3.8 | 3.3 | 0.53 | 7,244 | 1.24 | -| | 8,820 | 5.6 | 4.0 | 1.6 | 5,580 | 0.88 | -| | 20,820 | 11.8 | 4.1 | 7.7 | 4,116 | 0.65 | -| interdigital comb, 5 µm fingers/gaps, fingers | 10 | 1.4 | 1.2 | 0.19 | 5,900 | 0.87 | -| | 25 | 3.5 | 3.0 | 0.41 | 7,473 | 1.24 | -| | 50 | 6.2 | 5.5 | 0.62 | 3,680 | 0.58 | -| | 100 | 14.4 | 13.0 | 1.4 | 1,916 | 0.43 | -| | 200 | 38.9 | 35.5 | 3.4 | 6,421 | 1.23 | -| | 400 | 85.8 | 76.1 | 9.7 | 2,690 | 0.73 | -| conductor count (pads, 30 µm gaps) | 1 / 3 / 5 / 7 | 0.07 / 0.25 / 0.21 / 0.32 | | | | <= 0.38 | -| | 8 | rejected (cap: 7 conductors + ground) | | | | | -| 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 | -| model at comb 25 / 200 | pair-local | 3.4 / 39.2 | | | | 1.24 | -| | blend-pair-local | 4.3 / 39.8 (+0.08 s charge correction, +0.7 s first GBM load) | | | | 1.37 | -| | message-passing | 3.5 / 40.3 | | | | 1.25 | -| | fine-mom-prior (16k cells) | 5.7 / 52.0 | | | | 1.26 | - -Findings -* **No failure point was reached** within a 300 s timeout or the 16 GB budget: the largest case (400-finger comb, - 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 - default (`max_cells` in `tlsq_infer.registry.PRIORS`, a cost setting), so memory is bounded (~1.4 GB per standard - request, ~4-5 GB worst case for the fine prior); a larger cap raises both accuracy on long combs and memory. -* **Latency is driven by boundary complexity, not cell count**: the MoM mesh builder (quadtree + shapely coverage, - repeated with coarser h_min until <= 8,000 cells) dominates; long, finely gapped boundaries (combs, meanders) cost - most. Feature extraction samples every boundary at 0.25 µm and grows linearly with total perimeter (7.7 s at 21 mm). -* The network itself is < 3 ms; charge correction < 0.1 s (it reuses the prior's solve). -* GDS parsing: ~55 µs per polygon (300k polygons: 16 s, 1.75 GB). -* Paper-family designs (1 thread, all 926 never-seen designs): median 5.8 s, p90 34 s per design for blend + message - passing (two MoM solves); a single model is about half. - -Recommended caps (implemented in `tlsq_infer/app_ui.py`, overridable by env): -* 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); -* queue concurrency 2 per Space (`TLSQ_CONCURRENCY`; each request uses ~2 threads and <= 1.5 GB); -* expect ~1-10 s for single components, up to ~90 s for very long interdigitated or meandered boundaries. - -## 2. Ground crop and accuracy - -The training chips extend 150-330 µm (15-220 conductor-ground gaps) beyond the conductors. Cropping the ground tighter -changes both the physics and the network input. 150 random never-seen designs, all mutual entries, median % / MAE fF / -RMSE fF (`reproduction_n150_crop_margins.json`, `reproduction_n150_auto.json`): - -| crop | pair-local | blend-pair-local | -|---|---|---| -| none (paper setting) | 1.78 / 0.98 / 1.82 | 1.70 / 0.84 / 1.53 | -| **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** | -| 100 gaps | 1.96 / 1.01 / 1.84 | 1.93 / 0.92 / 1.60 | -| 50 gaps | 2.61 / 1.32 / 2.27 | 2.56 / 1.31 / 2.30 | -| 20 gaps | 3.76 / 2.13 / 3.81 | 3.71 / 2.20 / 4.08 | -| 10 gaps | 4.77 / 2.76 / 4.95 | 4.90 / 2.90 / 5.36 | - -(Errors against the labels of the uncropped chips, so tight crops also measure the physical change of removing ground.) -Default is therefore `auto`; tight windows are available but flagged in the docs. - -## 3. Robustness (219 files, `robustness.json`) - -| source | files | result | rejected (clean error) | crashes | -|---|---|---|---|---| -| Qiskit Metal singletons (all) | 36 | 36 physical matrices | 0 | 0 | -| Qiskit Metal parametric sweeps (3 per class, 29 classes) | 87 | 87 | 0 | 0 | -| our families (exp6, exp13-15) | 15 | 15 | 0 | 0 | -| OOD-60 shapes | 60 | 60 | 0 | 0 | -| synthetic adversarial | 21 | 15 | 6 (as expected) | 0 | - -Synthetic cases: non-default layers (clean error with defaults, works with explicit roles), 10 mm full-chip ground with -48 conductors (clean error; works with `select` / `select_box`, unselected bodies flagged), conductor with holes and an -island inside, 50 nm gap (resolution-floor warning), 7 conductors (ok) / 8 (clean error), overlapping and edge-sharing -polygons (merged into one net), conductor touching ground (merged into ground, warning), references + FlexPath -(flattened), ground split by the window, no ground / empty / non-GDS (clean errors), 8 mm pocket. Invariance: -translation by 1e5 µm, 90° rotation and nm database units give identical matrices (max rel. diff 0). -Physical checks: all mutuals negative and self terms positive in every returned matrix. The network self term is -below the row sum of mutuals (C_ii < sum |C_ij|) in 87/213 matrices (mostly Qiskit Metal parts far from the training -families): use `self_from_rows=True` when a strictly consistent Maxwell matrix is needed. - -## 4. Deployed Spaces (HF free tier, cpu-basic) - -Measured 2026-10-10 after the fine-tier switch to `all-data-flagship` (private Spaces, `examples/client.py` path -`/predict_gds`, one request at a time; server time = prepare + predict inside the Space; wall adds upload / queue). Every -returned matrix equals the local prediction of the same bundle to <= 3e-5 relative (max over entries). - -| layout | coarse tier (`all-data-v3s`), server s | fine tier (`all-data-flagship`), server s | note | -|---|---|---|---| -| Xmon example | 1.2 | 1.5 | | -| interdigital example | 2.4 | 0.8 | | -| TransmonPocket example | 19.7 (routed to the fine tier) | 19.4 | capped coarse mesh < 0.38 panels/gap: large-layout routing | -| shielded concentric example | 4.8 | 79.6 | flagship H-matrix solve, Mac 1 thread 14.6 s | -| two pads, size 20x (stress) | 61.7 (routed) | 60.2 | | -| comb, 200 fingers (stress) | 224.4 (routed) | 220.0 | | - -Local, same bundle through the Space API function (`app_ui.api_predict_gds`), Mac M5 Pro, 2 threads, one persistent -process, fine tier, s: Xmon 0.5, interdigital 0.3, TransmonPocket 5.2, shielded concentric 21.2, never-seen shielded -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; -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 -(e.g. TransmonPocket 18.0 s fine). Gradio has no request timeout; the queue runs 2 requests at once (`TLSQ_CONCURRENCY`) -but at most one flagship-prior solve (`TLSQ_HEAVY_CONCURRENCY`, default 1; fine tier and coarse requests routed to it), -so two large solves (up to 8.3 GB each, grid_qubit_9) cannot exceed the 16 GB of cpu-basic. - -## 5. Known biases and reporting (review fixes, 2026-10-08) - -* **Self-capacitance from row sums.** `self_from_rows=True` uses C_ii = sum_j |C_ij| / (1 - eps) with eps = 0.0143, the - training-family median of the row-sum fraction. On the never-seen components the median is 0.0099 (0.986 % over - 2,477 signal conductors of the 926-design evaluation set), so the default overestimates C_Sigma (and underestimates - E_C) by about 0.45 % there. eps is a parameter (`predict(..., row_sum_eps=...)`); the default is kept at the training - value and is not tuned on held-out data. -* **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, - 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 - across it, `cap_hit`; for v3-rich also `extrapolated`), a warning in `warnings` and `expected_error["mesh_warning"]`. -* **Conductor order.** Networks and the MoM prior are invariant to the polygon order of the file (shuffling test: - MoM exact to 4e-16, network outputs to 5e-5 float32 round-off). The published blend assigns entry kinds by file order; - deployment blends use order-invariant kinds (conductor-conductor / conductor-ground) when `blend.kinds: invariant` - is set in the registry (`deploy/tests/test_review_fixes.py`). - -## 6. Flagship tiers: `all-data-v3s` (coarse prior, default) and `all-data-v3-rich` (fine prior), 2-thread latency - -**Update 2026-10-10:** the fine tier is now `all-data-flagship` (same network as `all-data-v3-rich`, flagship prior; -deploy/MODEL_SELECTION.md Results 3, PRIOR_SCHEME.md section 8.1). Mac M5 Pro, 2 threads, whole `predict`: median / -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 -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 -one returns a matrix (the resolution floor L/8192 limits comb_f400 / pads_s20 / pads_s50 to 4.65 / 4.64 / 1.97 panels -per gap). The table below is the former fine tier (`all-data-v3-rich`) and stays as its record. - -Naming (2026-10-09): **coarse prior** = one gap/3 solve on the conductor-anchored v3 mesh (was "fast"), **fine prior** = -gap/3 + gap/6 with Richardson extrapolation (was "accurate"); `predict(model="coarse" | "fine")`, the old words are kept as -aliases and in the model ids. The chip-anchored baselines are the earlier standard (8k) and earlier 16k priors (the latter -was "earlier fine"). "Flagship prior" is reserved for a future large-scale scheme and is not released. - -Measured on HPC main-partition nodes (`torch.set_num_threads(2)`, OMP/BLAS 2 threads, `OPENBLAS_CORETYPE=Haswell`; -fastest of the runs per case). These nodes are ~2.5x slower than the Apple M host of section 1 (pair-local comb_f400: -277 s here, 86 s there). End to end from the GDS, s, after the v3 non-positive-definite fix (deploy/MODEL_SELECTION.md, -amendment 5): - -| layout | all-data-v3s (coarse) | all-data-v3-rich (fine) | all-data-base (fallback) | pair-local (previous flagship) | -|---|---|---|---|---| -| 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 | -| comb, 200 / 400 fingers | 91.0 / 161.4 | 257.3 / 293.2 | 98.3 / 276.7 | 98.3 / 276.7 | -| 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 | -| 7 conductors | 2.0 | 8.6 | 0.7 | 0.7 | -| 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 | - -Findings -* **v3 non-positive-definite systems (fixed).** On long fine combs the cell cap coarsens the v3 lattice beyond the finger - 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 - over-estimates the coupling of panels closer than their size (smallest eigenvalue -0.15). Only when the Cholesky fails, - the v3 solve is repeated with the exact potential of a uniformly charged rectangle for near pairs (solves the 50- and - 200-finger combs), and if that still fails, with whole finest cells owned by the majority metal (as the v2 mesh; solves - the 400-finger comb, where cut panels of different conductors overlap). Priors that solved before are bit-identical. - `all-data-v3s` no longer needs its fallback model on any stress, held-out or robustness layout; the fallback stays as a - safety net. -* **Resolution floor.** At 200-400 fingers every prior (v2 included) is far from converged (0.15-0.75 cells across the - 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 - carries the mesh-coarsening warning; treat such layouts as indicative. -* 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 - hardware measure it with `deploy/tests/test_app.py` after deployment. -* OpenBLAS's SkylakeX Cholesky kernel segfaulted on one v3 system (Qiskit Metal TransmonInterdigitated sample, 15.5k - panels); the Space apps set `OPENBLAS_CORETYPE=Haswell` on x86 before numpy loads. -* Robustness (`deploy/tests/results/robustness_all-data-v3s.json`, `robustness_all-data-v3-rich.json`): 219/219 files - pass for both (matrices or clean rejections, invariance to translation, rotation, units); C_ii < sum |C_ij| in 98 - (coarse) / 80 (fine) of 213 matrices (use `self_from_rows=True` for a consistent Maxwell matrix). -* Bundle: flagship Space 10.4 MB (both tiers + `all-data-base` + `all-data`, the fine tier's fallback), paper-models - Space 25.7 MB (`python deploy/sync.py`, dry run). - -## 7. Per-entry error estimates (`result["per_entry"]`, ERROR_MODEL.md, 2026-10-08) - -**Not enabled in the release** (user decision 2026-10-08): `artifacts.error_model` in the registry is empty, so -`per_entry` is `None` and `expected_error` is the global estimate, until the pre-declared v2 has been evaluated on the -fresh Qiskit Metal atlas and >7-conductor labels. The code, the v1 artifacts and the tests stay in place; enabling is -one registry line. When enabled, the coarse and fine tiers and `pair-local-fast` return, per mutual entry, `interval_80_fF`, `interval_95_fF`, -`p_large_error` (P(|err| > 5 %)) and `predicted_median_err_pct`; the Spaces show the matrix with ± the 80 % interval -(toggle), a per-entry table and a CSV of the intervals. `expected_error` is now derived from the per-entry estimates -(`source`; the old global estimate is kept under `global_estimate`); the material / thickness terms are added in -quadrature. Other models keep the global estimate. The matrix itself is unchanged (tests: `test_error_model.py`). -Cost: < 35 ms per 8-conductor matrix (features + two 300-tree models); artifacts 0.4 MB each. - -**Measured on held-out designs (never used in fitting or calibration), coarse tier / fine tier:** - -| set | entries (> 5 %) | network median % / MAE fF / RMSE fF | AUROC P(>5 %) | 80 % coverage | 95 % coverage | -|---|---|---|---|---|---| -| 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 | -| 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 | -| 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 | - -* **Treat the intervals as indicative.** The pre-declared targets (AUROC >= 0.75, coverage within 5 points) were not - met: the intervals under-cover by up to 10 points (80 %) / 9 points (95 %) on the Qiskit Metal components. -* **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 - 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 - relative error flag on a sub-0.3 fF coupling is usually an aF-scale absolute error. -* `p_large_error` ranks entries only moderately; the predicted |C_ij| alone, the seed spread and the embedding distance - rank them as well or better on these sets (ERROR_MODEL.md, Results). Verify critical couplings with a field solver. diff --git a/README.md b/README.md index 916fb8c3a5c6d37779e4805ffd87ed5af7895c60..c9408193d73a648b1e8c6732876ed07fc4a877f3 100644 --- a/README.md +++ b/README.md @@ -8,35 +8,81 @@ sdk_version: 6.29.1 python_version: '3.12' app_file: app.py pinned: false -short_description: Zero-shot capacitance matrices of planar layouts -models: -- SQuADDS/2026PaperModels-pair-local -- SQuADDS/2026PaperModels-message-passing -- SQuADDS/2026PaperModels-charge-corrected-prior -- SQuADDS/2026PaperModels-blend-pair-local -- SQuADDS/2026PaperModels-blend-message-passing -- SQuADDS/2026PaperModels-geometry-only -- SQuADDS/2026PaperModels-pair-local-fine -- SQuADDS/2026PaperModels-message-passing-fine -- SQuADDS/2026PaperModels-all-data -- SQuADDS/2026PaperModels-all-data-base -- SQuADDS/2026PaperModels-all-data-v3s -- SQuADDS/2026PaperModels-all-data-v3-rich -- SQuADDS/2026PaperModels-all-data-lite1500 -- SQuADDS/2026PaperModels-all-data-flagship -- SQuADDS/2026PaperModels-pair-local-fast -- SQuADDS/2026PaperModels-message-passing-fast -- SQuADDS/2026PaperModels-pair-local-accurate -- SQuADDS/2026PaperModels-message-passing-accurate -- SQuADDS/2026PaperModels-charge-corrected-prior-fast -- SQuADDS/2026PaperModels-blend-pair-local-fast -- SQuADDS/2026PaperModels-blend-pair-local-accurate +short_description: 'Maxwell capacitance of planar layouts: 3 tiers' --- # Planar Capacitance Model -Upload a GDS of a planar superconducting layout (ground 1/0, conductors 1/10, or assign your layers) and get the full Maxwell capacitance matrix in fF, zero-shot. See also [shanto268/2026PaperModels](https://huggingface.co/spaces/shanto268/2026PaperModels). +Upload a GDS of a planar superconducting layout (ground 1/0, conductors 1/10, or assign your layers) and get its full Maxwell +capacitance matrix in fF, self terms included. Three tiers in one selector, 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: training, validation and every test +layout that passed label QC). Private preview while the paper is in preparation. To reproduce the paper's numbers with the paper's own +models, use the paper Space [shanto268/2026PaperModels](https://huggingface.co/spaces/shanto268/2026PaperModels). -Programmatic use: `examples/client.py` (gradio_client, endpoint `/predict_gds`; model = `fast` (default: flagship-fast, also multi-qubit chips), `coarse` or `fine` (most accurate; alias `accurate`)). +## Accuracy: what can honestly be said +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 +**same recipe** trained on the 35,354-layout TRAIN split (validation = 4,608 layouts of 13 families; test = the paper's test benchmark, +read once: in family 4,009 layouts, zero-shot macro over nine component cohorts 1,878 layouts; held-out family = leave one family out, +4,071 layouts) and (b) **one fresh check, B5**: 39 new layouts of one generated family (campaign450 B5, 3-4 conductors) that no +component of any tier ever saw, against its physics reference. Layout-mean median relative error % (MAE / RMSE fF) over mutual +entries against Q3D, 0.1 fF floor. "–" = not measured. -Hardware: free CPU tier (2 vCPU, 16 GB); see LIMITS.md for measured latency and caps. +| 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) | +|---|---|---|---|---|---|---|---|---|---| +| `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 | +| `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 | +| `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 | + +* **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 + of MAE and 0.187 fF of RMSE. Use it for anything that matters. +* **Balanced** is about 2x faster than Accurate at 1.5-1.7x its error (paper VAL 0.343 % (0.123 / 0.138 fF) vs + 0.229 % (0.0674 / 0.0792 fF); B5 1.753 % (0.236 / 0.308 fF) vs 1.034 % (0.131 / 0.189 fF)). +* **Fast** is for quick screening of the component families it was trained on (interdigital, paddle, concentric, star, multipad star, + Xmon, TransmonPocket, shielded concentric, two-net, random, procedurally generated and synthetic campaign layouts). **On B5 (new designs + 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 + 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) + against 1.554 % (0.166 / 0.205 fF). Its error comes from the learned prior. Paper validation of this surrogate recipe with + the networks: 0.722 % (0.387 / 0.436 fF). + +Sources: `agents/flagship_v4/RESULTS.md` (training, B5, timings), paper_v4 (tab:ladder-test, tab:routes) for the reference rows. + +### `accurate`: Accurate (~1 s): extrapolated physics R(1.5, 3) on the corrected prior + residual networks, compiled solver + +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. + +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)). + +### `balanced`: Balanced (~0.5 s): the same recipe on the coarser pair R(0.75, 1.5) of the corrected prior, networks retrained + +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. + +Reference recipe: the paper's coarser pair + the model's recipe (trained on 35,354 TRAIN layouts; validation only, never test-scored). + +### `fast`: Fast (~0.1 s): learned prior surrogate + residual networks, no field solve (trained component families only) + +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. + +Reference recipe: the paper's fast model (learned prior surrogate + the networks on its prior, trained on 35,354 TRAIN layouts). + +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 +two thin-film solves; Fast by the geometric embedding. Ground self term: indicative only (it depends on the chip boundary). + +## API (gradio_client; the same endpoints over REST at `/gradio_api/call/`) +* `/models` () -> served tiers with their reference numbers and times. +* `/inspect` (gds) -> layer table; `/prepare` (gds, layers, crop, margin_gaps, margin_um, select) -> preview and summary. +* `/predict` (gds, layers, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model). +* `/predict_gds` (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model) -> result dict + (`matrix_fF`, `names`, `timing_s`, `prior_stack`, `reference`, `b5`). model = `accurate`, `balanced`, `fast` (default `accurate`). + Up to 70 conductors + ground per layout. + +```python +import os +from gradio_client import Client, handle_file +c = Client("shanto268/PlanarCapacitanceModel", token=os.environ["HF_TOKEN"]) # token while the Space is private +r = c.predict(handle_file("layout.gds"), "1/0", "1/10", 0, "Si (cryogenic, training)", 11.45, 350, "accurate", + api_name="/predict_gds") +print(r["names"], r["matrix_fF"], r["timing_s"]["total_s"]) +``` + +Privacy: uploads and result files are deleted within 10 minutes; no persistent storage, no analytics, no file names or +geometry in the logs. Hugging Face keeps ordinary web access logs under its own policy. diff --git a/app.py b/app.py index e685cabc33cb7972df6303fa0fba456e6adc23d6..5e68b959d341e6461ce2c14c8df11765c141cdc6 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,6 @@ -"""Flagship Space: GDS -> zero-shot Maxwell capacitance matrix. Names and links come from registry.yaml.""" +"""Flagship Space (deploy/GNN_SPACE.md "Flagship Space"): GDS -> full Maxwell capacitance matrix with three tiers (Accurate default, +Balanced, Fast) retrained on all on-stack data, through geomgnn.flagship. Names, tiers and numbers come from registry.yaml +(flagship_hub, flagship_tiers).""" import os import platform @@ -6,37 +8,37 @@ import sys from pathlib import Path if platform.machine() in ("x86_64", "AMD64"): # OpenBLAS SkylakeX Cholesky kernel segfaulted on a v3 MoM system (LIMITS.md) - os.environ.setdefault("OPENBLAS_CORETYPE", "Haswell") # must be set before numpy loads OpenBLAS + os.environ.setdefault("OPENBLAS_CORETYPE", "Haswell") +os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False") +os.environ.setdefault("NUMBA_CACHE_DIR", os.path.join(os.environ.get("TMPDIR", "/tmp"), "numba_cache")) # writable on the Space +os.environ.pop("TLSQ_GNN_PRIOR_MODE", None) # every tier names its own solver mode (registry flagship_hub.prior_stacks) HERE = Path(__file__).resolve().parent -if (HERE / "tlsq_infer").exists(): # built Space bundle (deploy/sync.py): vendored package, registry and weights +if (HERE / "geomgnn").exists(): # built bundle (deploy/sync.py --flagship): vendored packages, registry and weights sys.path.insert(0, str(HERE)) os.environ.setdefault("TLSQ_REGISTRY", str(HERE / "registry.yaml")) os.environ.setdefault("TLSQ_MODEL_DIR", str(HERE / "models")) + os.environ.setdefault("TLSQ_OFFLINE", "1") else: # repository checkout sys.path.insert(0, str(HERE.parents[1] / "tlsq_infer" / "src")) + sys.path.insert(0, str(HERE.parents[1] / "geomgnn" / "src")) + os.environ.setdefault("TLSQ_REGISTRY", str(HERE.parents[1] / "registry.yaml")) from tlsq_infer import set_num_threads # noqa: E402 -from tlsq_infer.app_ui import build_app # noqa: E402 from tlsq_infer.registry import load_registry # noqa: E402 -set_num_threads() # TLSQ_NUM_THREADS, else the CPUs of the container (cgroup quota) +os.environ.setdefault("TLSQ_NUM_THREADS", str(set_num_threads())) + +from geomgnn.app_flagship import build_app # noqa: E402 + reg = load_registry() -hub = reg["hub"] -space = os.environ.get("SPACE_ID", hub["flagship_space"]) -default = reg["defaults"]["flagship_model"] -models = [default] + [reg["defaults"][k] for k in ("coarse_model", "flagship_accurate_model") if reg["defaults"].get(k)] -t = reg["defaults"].get("tiers", {}).get("fast") or {} -d = t if t.get("median_pct") is not None else reg.model(default)["paper"]["never_seen"] -where = t.get("set", "926 never-seen designs") if t else "926 never-seen designs" -header = (f"Zero-shot Maxwell capacitance of planar superconducting layouts, from one capacitor to a multi-qubit chip. Default " - f"(fast tier) model: **{reg.model(default)['title']}** " - f"({d['median_pct']} % median error, MAE {d['mae_ff']} fF, RMSE {d['rmse_ff']} fF on {where}). " - f"All paper models: [{hub['paper_models_space']}](https://huggingface.co/spaces/{hub['paper_models_space']}). " - f"Weights: [{reg.model(default)['repo_id']}](https://huggingface.co/{reg.model(default)['repo_id']}). " - f"Programmatic use: `gradio_client.Client(\"{space}\")` (see the API page and examples/client.py).") -demo = build_app(hub["flagship_title"], models, default, header, examples_dir=HERE / "examples", tiers=True) -demo.queue(default_concurrency_limit=int(os.environ.get("TLSQ_CONCURRENCY", 2)), max_size=32) +ex = HERE / "examples" if (HERE / "examples").exists() else HERE.parent / "PlanarCapacitanceModel" / "examples" +if os.environ.get("TLSQ_PRELOAD", "1") != "0": # load every tier (SHA checks) + numba warmup + one small prediction before the queue opens + from geomgnn import flagship as FS # noqa: E402 + + FS.preload(reg, example=ex / "interdigital.gds") +demo = build_app(reg["flagship_hub"]["title"], examples_dir=ex) +demo.queue(default_concurrency_limit=int(os.environ.get("TLSQ_CONCURRENCY", 1)), max_size=32) if __name__ == "__main__": demo.launch(server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"), server_port=int(os.environ.get("PORT", 7860))) diff --git a/examples/__pycache__/client.cpython-312.pyc b/examples/__pycache__/client.cpython-312.pyc deleted file mode 100644 index f6431d46378d8deb1fc1606a2b6494cc4cd49cc5..0000000000000000000000000000000000000000 Binary files a/examples/__pycache__/client.cpython-312.pyc and /dev/null differ diff --git a/examples/client.py b/examples/client.py deleted file mode 100644 index 2054f9ebc757d804f44124c7dd066d7968d66be9..0000000000000000000000000000000000000000 --- a/examples/client.py +++ /dev/null @@ -1,42 +0,0 @@ -"""Call the Space from Python (pip install gradio_client). - - python client.py my_layout.gds [--space shanto268/PlanarCapacitanceModel] [--substrate sapphire...] [--thickness 430] - -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/ -""" - -import argparse -import json -import os - -from gradio_client import Client, handle_file - -ap = argparse.ArgumentParser() -ap.add_argument("gds") -ap.add_argument("--space", default=os.environ.get("TLSQ_SPACE", "shanto268/PlanarCapacitanceModel")) -ap.add_argument("--ground", default="1/0", help="ground layer/datatype list, e.g. '1/0' or '1/0,5/0'") -ap.add_argument("--conductors", default="1/10") -ap.add_argument("--margin-gaps", type=float, default=0.0, help="ground crop margin in conductor-ground gaps (0: auto, < 0: no crop)") -ap.add_argument("--substrate", default="Si (cryogenic, training)", help="a substrate name of the Space, or 'custom eps_r'") -ap.add_argument("--eps", type=float, default=11.45, help="eps_r when --substrate 'custom eps_r'") -ap.add_argument("--thickness", type=float, default=350.0, help="substrate thickness (µm)") -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") -ap.add_argument("--self-from-rows", action="store_true") -a = ap.parse_args() - -client = Client(a.space, token=os.environ.get("HF_TOKEN")) if not a.space.startswith("http") else Client(a.space) -res = client.predict(handle_file(a.gds), a.ground, a.conductors, a.margin_gaps, a.substrate, a.eps, a.thickness, a.model, - a.self_from_rows, api_name="/predict_gds") -names = res["names"] -print("Maxwell capacitance matrix (fF):") -print(" " + "".join(f"{n:>12s}" for n in names)) -for n, row in zip(names, res["matrix_fF"]): - print(f"{n:>6s}" + "".join(f"{'--':>12s}" if v is None else f"{v:12.4f}" for v in row)) -e = res["expected_error"] -print(f"\nmodel {res['model']} (package {res['package_version']}); eps factor {res['eps_correction_factor']:.4f}") -if "expected_median_pct" in e: - print(f"expected error: median {e['expected_median_pct']:.2f} %, MAE {e['expected_mae_ff']:.3f} fF, RMSE {e['expected_rmse_ff']:.3f} fF") -for w in res["warnings"]: - print("warning:", w) -with open("capacitance.json", "w") as f: - json.dump(res, f, indent=1) diff --git a/examples/lattice_1q.gds b/examples/lattice_1q.gds new file mode 100644 index 0000000000000000000000000000000000000000..103809f3fdb05398f0c0b1e1f4148fc5a99d3774 Binary files /dev/null and b/examples/lattice_1q.gds differ diff --git a/examples/lattice_5q.gds b/examples/lattice_5q.gds new file mode 100644 index 0000000000000000000000000000000000000000..0ab3871511b3a7f7e39d2511100b1f0b7a092533 Binary files /dev/null and b/examples/lattice_5q.gds differ diff --git a/geomgnn/SOURCES.json b/geomgnn/SOURCES.json new file mode 100644 index 0000000000000000000000000000000000000000..0062d5002e6eb04fd74c40a79ecc33042b156e2b --- /dev/null +++ b/geomgnn/SOURCES.json @@ -0,0 +1,191 @@ +{ + "stage": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage", + "note": "frozen geomfast inference stack (FINALIZATION_V1), namespaced under geomgnn; only import lines rewritten", + "files": { + "_tlsq/__init__.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/__init__.py", + "source_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "rewrites": [] + }, + "_tlsq/geometry.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/geometry.py", + "source_sha256": "0bb8d3ead358870a026eebf8e26a212bdf1a838565bd30251a684fab7c97c99b", + "sha256": "0bb8d3ead358870a026eebf8e26a212bdf1a838565bd30251a684fab7c97c99b", + "rewrites": [] + }, + "_tlsq/embed/__init__.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/__init__.py", + "source_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "rewrites": [] + }, + "_tlsq/embed/proxy.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/proxy.py", + "source_sha256": "78d8dffd2fac75632a91ebee151247afe4157716ffb11e624e6b2932c618c86e", + "sha256": "78d8dffd2fac75632a91ebee151247afe4157716ffb11e624e6b2932c618c86e", + "rewrites": [] + }, + "_tlsq/embed/fastprior.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/fastprior.py", + "source_sha256": "1755668ca197bc56bcc4ef1d23a01464df75ba27e79eb2f447c8f147152cbbf1", + "sha256": "1755668ca197bc56bcc4ef1d23a01464df75ba27e79eb2f447c8f147152cbbf1", + "rewrites": [] + }, + "_tlsq/embed/fastprior_loc.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/embed/fastprior_loc.py", + "source_sha256": "3642ebac602a0767cad2cdd51e04dc602055c0045022b71f14d93298af943a2b", + "sha256": "3642ebac602a0767cad2cdd51e04dc602055c0045022b71f14d93298af943a2b", + "rewrites": [] + }, + "_tlsq/uge/__init__.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/__init__.py", + "source_sha256": "2c9ab4995b49e228e84ff19f44c79f944a3f5d0489db0a688ccaf76546995c22", + "sha256": "2c9ab4995b49e228e84ff19f44c79f944a3f5d0489db0a688ccaf76546995c22", + "rewrites": [] + }, + "_tlsq/uge/blocks.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/blocks.py", + "source_sha256": "c58313f167f7df0ef0de6b321ab6b7214a661aef10b461e2bf90a60999fb4632", + "sha256": "c58313f167f7df0ef0de6b321ab6b7214a661aef10b461e2bf90a60999fb4632", + "rewrites": [] + }, + "_tlsq/uge/graph.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/graph.py", + "source_sha256": "aeb8c58cf6c1d2169eab8a58fba362634d7489f3414f260a9fc55a02048217ee", + "sha256": "aeb8c58cf6c1d2169eab8a58fba362634d7489f3414f260a9fc55a02048217ee", + "rewrites": [] + }, + "_tlsq/uge/localtok.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/localtok.py", + "source_sha256": "8b47bd454353709a29ad996243d0f8b4b0d94991ee43715145af41a912dd9319", + "sha256": "8b47bd454353709a29ad996243d0f8b4b0d94991ee43715145af41a912dd9319", + "rewrites": [], + "note": "byte-identical to the stage's top-level localtok.py, which infer.py loads as tlsq.uge.localtok" + }, + "_tlsq/uge/proxies.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/proxies.py", + "source_sha256": "1726d068105f88f2654fd84a07919b7ecd9852cc5f826f6dd812109e6ce55b38", + "sha256": "1726d068105f88f2654fd84a07919b7ecd9852cc5f826f6dd812109e6ce55b38", + "rewrites": [] + }, + "_tlsq/uge/scene.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/vendor/tlsq/uge/scene.py", + "source_sha256": "f990d3028092ca59e4bbb274bcc181eb52cc7d0c73c158cbc1b23003d0368285", + "sha256": "f990d3028092ca59e4bbb274bcc181eb52cc7d0c73c158cbc1b23003d0368285", + "rewrites": [] + }, + "pilot_p2.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/pilot_p2.py", + "source_sha256": "c38264c43ab342ba64338892edf23c8abd4b88503b99bce322431efb7db77c77", + "sha256": "c38264c43ab342ba64338892edf23c8abd4b88503b99bce322431efb7db77c77", + "rewrites": [] + }, + "infer.py": { + "source": "/Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage/infer.py", + "source_sha256": "f1829b165b1c1abac5e9e8b6e9ccda8bcf91f977c331ee4b408615455e902d09", + "sha256": "8aa26ec0da20be8158096448390911c380ee0e8b8faeecf21546e2e4f67b344f", + "rewrites": [ + [ + "W=Path(__file__).resolve().parent;sys.path.insert(0,str(W/'vendor'))", + "W=Path(__file__).resolve().parent" + ], + [ + "from pilot_p2 import build_model", + "from .pilot_p2 import build_model" + ], + [ + "from tlsq.uge.scene import scene_from_gds,tlsq_namer", + "from ._tlsq.uge.scene import scene_from_gds,tlsq_namer" + ], + [ + "from tlsq.uge.graph import scene_graph", + "from ._tlsq.uge.graph import scene_graph" + ], + [ + "from tlsq.uge.proxies import CapacitanceMoM", + "from ._tlsq.uge.proxies import CapacitanceMoM" + ], + [ + "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)", + "from ._tlsq.uge import localtok as lt" + ], + [ + " from tlsq.embed import proxy,fastprior", + " from ._tlsq.embed import proxy,fastprior" + ], + [ + " from tlsq.embed.fastprior_loc import micro_mom_loc", + " from ._tlsq.embed.fastprior_loc import micro_mom_loc" + ] + ] + }, + "fastmom.py": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/geomfast/fastmom.py", + "source_sha256": "8f3642b22bd81325a782ee2a07d0341761d017351d838f484ee375cda025e85c", + "sha256": "7447474366ddae654100467125c84ddb7fb503a1c9d4fd1264825bde164de951", + "rewrites": [ + [ + "from tlsq.embed import proxy as P", + "from ._tlsq.embed import proxy as P" + ] + ] + }, + "priorv2.py": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/priorv2/priorv2.py", + "source_sha256": "1a0bdcb71500c7f11e63238afe531c18caabd5c9f6f7aaa73d9b7dcd0ad61f8c", + "sha256": "64057e92e37a04a0231dfdc34df52a20542f88c2b2a3e8c2cc00a32d9ec1d21b", + "rewrites": [ + [ + "from tlsq.embed import fastprior as F, proxy as P", + "from ._tlsq.embed import fastprior as F, proxy as P" + ] + ], + "note": "prior v2m (agents/priorv2/FINALIZATION_V2.json module_sha256); vendored 2026-10-17" + }, + "fastprior_pkg.py": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/fastprior_pkg/fastprior_pkg.py", + "source_sha256": "956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde", + "sha256": "956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde", + "rewrites": [], + "note": "gated fast-solve package (agents/fastprior_pkg/RESULTS.md), verbatim; bound with bind('geomgnn'); vendored 2026-10-17" + }, + "lp_surrogate.py": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/learnedprior/lp_surrogate.py", + "source_sha256": "55804c9349a2ca4a74f3ae821a19c4637dbaf16d25b65c8ee70c746b767d2707", + "sha256": "55804c9349a2ca4a74f3ae821a19c4637dbaf16d25b65c8ee70c746b767d2707", + "rewrites": [], + "note": "learned prior surrogate (agents/learnedprior; HASHES.json lp_surrogate.py), verbatim; fast tier; vendored 2026-10-18" + }, + "schema_p2d.json": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/learnedprior/schema_p2d.json", + "source_sha256": "17eba5f7b5d01668019df9301507133dfe4e38689acbde5256f3ade2107df8e9", + "sha256": "17eba5f7b5d01668019df9301507133dfe4e38689acbde5256f3ade2107df8e9", + "rewrites": [], + "note": "P2d feature schema read by lp_surrogate.py (HASHES.json schema_p2d.json), verbatim; vendored 2026-10-18" + }, + "train_mf.py": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/multifid/train_mf.py", + "source_sha256": "d4df1310cf8758d6e5165c514fc0bac497768b7e37da91dd2bede717fd746676", + "sha256": "d4df1310cf8758d6e5165c514fc0bac497768b7e37da91dd2bede717fd746676", + "rewrites": [], + "note": "multifid network (agents/multifid; HASHES.json train_mf.py), verbatim; paper Space cheap-physics arms; vendored 2026-10-19" + }, + "x1_core.py": { + "source": "/Users/shanto/LFL/tl_squadds/scripts/v4test/x1_core.py", + "source_sha256": "f4ee1cb4074eb7a3dc019072722f36d58a320d6bf711af3c66c7a5cbc96499cc", + "sha256": "a6702ffcbf8b1b03b99340654f89c707825e10f67a7921e52c21a8702edbeaca", + "rewrites": [ + [ + " from train_mf import build", + " from .train_mf import build" + ], + [ + " import lp_surrogate as S", + " from . import lp_surrogate as S" + ] + ], + "note": "X1 shared model code (multifid forward, Richardson c0, to_maxwell); paper Space arms; vendored 2026-10-19, import lines rewritten" + } + } +} diff --git a/geomgnn/__init__.py b/geomgnn/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..0a2acf47a067aafdc79963ee6955840ed3f066c9 --- /dev/null +++ b/geomgnn/__init__.py @@ -0,0 +1,8 @@ +"""geomgnn: the frozen geomfast GNN (paper_v4; agents/geomfast/FINALIZATION_V1.json) for the GNN Space. + +The inference code is the frozen geomfast stage, namespaced (`geomgnn.infer`, `geomgnn.pilot_p2`, `geomgnn._tlsq`; only import +lines rewritten, see SOURCES.json) so it cannot collide with `tlsq_infer` or the repository's `tlsq`. `geomgnn.api` adapts it +to `tlsq_infer.layout.PreparedLayout` and to the registry entries `gnn_models` (deploy/registry.yaml). +""" + +__version__ = "0.1.0" diff --git a/geomgnn/_tlsq/__init__.py b/geomgnn/_tlsq/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/geomgnn/_tlsq/embed/__init__.py b/geomgnn/_tlsq/embed/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/geomgnn/_tlsq/embed/fastprior.py b/geomgnn/_tlsq/embed/fastprior.py new file mode 100644 index 0000000000000000000000000000000000000000..bdc69d0e7732eba10adcfd261f33583c1b358082 --- /dev/null +++ b/geomgnn/_tlsq/embed/fastprior.py @@ -0,0 +1,925 @@ +"""Local (windowed) MoM prior: PRIOR_SCHEME.md Part III prototype. + +The Maxwell entry C_ij is defined with every other conductor at 0 V, and a ground plane screens the field, so the +entries of a conductor are set by its neighbourhood. The layout is partitioned into windows (recursive bisection of +the conductor sites until the metal edge length inside each window fits a panel budget); a window models its core +conductors (whose matrix rows it provides), the halo conductors within R_g of the core as separate nets at 0 V, the +ground plane cropped by a margin around core + halo, and every farther conductor filled into the ground. Each window +is a strip-merged conductor-anchored mesh (the flagship mesh family, PRIOR_SCHEME.md Part II) solved densely with the +exact near field and one right-hand side per core conductor, optionally Richardson-extrapolated (h and 2 h). Windows +that are translation copies of one another are solved once (geometric hash). Cost: O(number of distinct windows) x +(a bounded dense solve), independent of the chip size; a layout that fits one window is one global solve. +Pairs never co-windowed (farther than R_g apart) are not predicted: NaN in C and True in far_mask. +""" + +from __future__ import annotations + +import hashlib +import time +from concurrent.futures import ProcessPoolExecutor +from multiprocessing import get_context + +import numpy as np +import shapely +from scipy.spatial import cKDTree + +from ..geometry import sample_boundary +from ..uge.blocks import _kratio +from . import proxy as P + +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) + + +def _valid(g): + g = shapely.make_valid(g) + parts = [p for p in getattr(g, "geoms", [g]) if p.geom_type in ("Polygon", "MultiPolygon")] + return shapely.union_all(parts) if parts else shapely.Polygon() + + +def gap_of(metals: list) -> float: + tree = shapely.STRtree(metals) + d = np.inf + for i, g in enumerate(metals): + j, dist = tree.query_nearest(g, return_distance=True, exclusive=True, all_matches=False) + if len(dist): + d = min(d, float(dist[0])) + return d + + +def within(geoms: list, targets: list, R: float) -> np.ndarray: + """Boolean (len(geoms),): geometry within distance R of any target (STRtree, O(n log n)).""" + out = np.zeros(len(geoms), bool) + if not targets or not geoms: + return out + tree = shapely.STRtree(geoms) + for t in targets: + out[tree.query(t, predicate="dwithin", distance=R)] = True + return out + + +def plan_windows(G, conds: list, h: float, R_g: float, margin: float, panel_budget: int, global_budget: int | None = None, + split_gain: float = 0.75, level_rings: tuple = ()) -> list: + """Windows (dicts: core, halo, box). One global window (every conductor in the core, no halo, no crop) when the + metal edge length of the whole layout fits global_budget panels; otherwise recursive bisection of the conductor + sites (bbox centres) until the estimated merged panel count of the window fits panel_budget. The estimate weights + the metal edge length by the grading level it will be meshed at (1, 1/2, 1/4, ... beyond each of level_rings um + from the core; ground edges beyond R_g of the members are filled and count 0), so the budget measures the core + content, not the fixed halo + margin overhead. Each split is snapped to the widest coordinate gap between sites in + the central half of the sorted order (cores follow the natural cell structure); a split whose child is not below + split_gain x the parent (a dense cluster) is not made and the window is solved whole (H-matrix above dense_max).""" + sites = np.array([[(b[0] + b[2]) / 2, (b[1] + b[3]) / 2] for b in shapely.bounds(np.array(conds, dtype=object))]) + per_panel = h / PANELS_PER_EDGE_H + all_idx = np.arange(len(conds)) + L_all = G.length + sum(c.length for c in conds) + if L_all <= (global_budget if global_budget is not None else panel_budget) * per_panel: + return [{"core": all_idx, "halo": np.zeros(0, int), "box": None, "n_core": len(conds), "n_halo": 0}] + out = [] + Gb = G.boundary + gtree = shapely.STRtree(list(getattr(Gb, "geoms", [Gb]))) + rings = list(level_rings) + [np.inf] + + def window_of(core): + halo = within(conds, [conds[i] for i in core], R_g) + halo[core] = False + halo = np.nonzero(halo)[0] + x0, y0, x1, y1 = shapely.union_all([conds[i] for i in np.concatenate([core, halo])]).bounds + box = (x0 - margin, y0 - margin, x1 + margin, y1 + margin) + return halo, box + + def est_panels(core, halo, box): + cg = shapely.union_all([conds[i] for i in core]) + members = shapely.union_all([conds[i] for i in np.concatenate([core, halo])]) + b = shapely.box(*box) + gl = shapely.union_all([gtree.geometries[k] for k in gtree.query(b)]) if len(gtree.geometries) else Gb + edges = shapely.union_all([shapely.intersection(gl, b).intersection(members.buffer(R_g)), members.boundary]) + L, prev, w = 0.0, None, 1.0 + for r in rings: + ring = cg.buffer(r) if np.isfinite(r) else b + if prev is not None: + ring = ring.difference(prev) + L += w * shapely.intersection(edges, ring).length + prev, w = (cg.buffer(r) if np.isfinite(r) else b), w / 2 + return L / per_panel + + def rec(idx, n_parent=None): + halo, box = window_of(idx) + n = est_panels(idx, halo, box) + if len(idx) == 1 or n <= panel_budget or (n_parent is not None and n > split_gain * n_parent): + out.append({"core": idx, "halo": halo, "box": box, "est_panels": n}) + return + s = sites[idx] + ax = int(np.argmax(s.max(0) - s.min(0))) + order = idx[np.argsort(s[:, ax], kind="stable")] + x = sites[order, ax] + lo = max(1, len(order) // 4) + hi = max(lo + 1, min(len(order), (3 * len(order)) // 4 + 1)) + gaps = x[lo:hi] - x[lo - 1:hi - 1] + m = lo + int(np.argmax(gaps)) + rec(order[:m], n), rec(order[m:], n) + + rec(all_idx) + for w in out: + w["n_core"], w["n_halo"] = len(w["core"]), len(w["halo"]) + return out + + +def window_metals(G, conds: list, w: dict, fill_um: float = 250.0, keep_um: float = 0.0) -> list: + """[ground, core..., halo...] of window w: ground = chip ground cropped to the box plus every farther conductor + inside the box as ground metal (0 V), whose etched surroundings within fill_um are filled in where they are more + than keep_um from any member (their edges leave the mesh; nothing changes within keep_um of a member).""" + members = np.concatenate([w["core"], w["halo"]]).astype(int) + if w["box"] is None: + return [G] + [conds[i] for i in members] + box = shapely.box(*w["box"]) + far = np.setdiff1d(np.nonzero(within(conds, [box], 0.0))[0], members) + Gw = shapely.intersection(G, box) + if len(far): + chip = shapely.box(*G.bounds) + farg = shapely.union_all([conds[i] for i in far]) + fill = shapely.intersection(shapely.intersection(chip.difference(G), farg.buffer(fill_um)), box) + fill = fill.difference(shapely.union_all([conds[i] for i in members]).buffer(keep_um)) + Gw = shapely.union_all([Gw, shapely.intersection(farg, box), fill]) + return [_valid(Gw)] + [conds[i] for i in members] + + +def anchored_cells_graded(metals: list, h_f: float, core, levels: tuple, grade: float = 2.0, majority: bool = False): + """`proxy.anchored_cells` with the finest panel graded by the distance to the core conductors: h_f within levels[0] + of `core`, 2 h_f up to levels[1], 4 h_f up to levels[2], ... (same lattice; with levels=() identical to the plain + mesh). Returns centres (n, 2), sizes (n, 2), owner (n,).""" + edges = shapely.union_all([g.boundary for g in metals]) + sig = metals[1:] if len(metals) > 1 else metals + ax, ay = shapely.union_all(sig).bounds[:2] + X0, Y0, X1, Y1 = shapely.union_all(metals).bounds + span = max(X1 - X0, Y1 - Y0) + K = max(0, int(np.ceil(np.log2(max(span / 16, h_f) / h_f)))) + h0 = h_f * 2**K + ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0)) + ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0)) + gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1)) + xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0) + gq = P._Geoms(list(metals) + [edges, core]) + lv = np.asarray(levels, float) + tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool) + out_c, out_h, out_o = [], [], [] + while len(xs): + boxes = shapely.box(xs, ys, xs + h, ys + h) + cov, hit, inn, pcs = P._cover(gq, boxes, h, tst, inn) + keep = cov.sum(0) > 1e-3 + xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep] + pcs = [p[keep] for p in pcs] + cx, cy = xs + h / 2, ys + h / 2 + full = cov.max(0) > 0.999 + if len(lv): + maybe = h <= h_f * 2 ** len(lv) * 1.0001 # cells that can be at their last level + hl = np.zeros(len(xs)) + if maybe.any(): + dc = P._distance(gq, len(metals) + 1, boxes[maybe]) + hl[maybe] = h_f * 2.0 ** np.searchsorted(lv, dc, side="right") + last = h <= hl * 1.0001 + else: + last = h <= h_f * 1.0001 + d = np.full(len(xs), np.inf) + far = full & ~last + d[far] = P._distance(gq, len(metals), boxes[far]) + leaf = full & (last | (h <= grade * d + 1e-12)) + out_c.append(np.stack([cx[leaf], cy[leaf]], 1)) + out_h.append(np.stack([h[leaf], h[leaf]], 1)) + out_o.append(cov[:, leaf].argmax(0)) + cut = last & ~leaf + if majority and cut.any(): + take = cut & (cov.max(0) >= 0.5) + out_c.append(np.stack([cx[take], cy[take]], 1)) + out_h.append(np.stack([h[take], h[take]], 1)) + out_o.append(cov[:, take].argmax(0)) + cut = cut & False + if cut.any(): + gs = gq.get() + for k, g in enumerate(gs[:len(metals)]): + shapely.prepare(g) + sel = np.nonzero(cut & (cov[k] > 1e-3))[0] + if not len(sel): + continue + pcs_k = pcs[k][sel] + bb = shapely.bounds(pcs_k) + cen = shapely.get_coordinates(shapely.centroid(pcs_k)) + inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1]) + if not inside.all(): + cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside])) + out_c.append(cen) + out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6)) + out_o.append(np.full(len(sel), k)) + gq.put(gs) + split = ~leaf & ~last + hs = h[split] / 2 + xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs]) + ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs]) + h = np.concatenate([hs] * 4) + tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4) + return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o) + + +def strip_merge(c, wh, o, metals, h_f: float, lmax_mult: float = 16.0, grade: float = 1.0): + """`proxy.strip_merge` with the strip length bounded by lmax_mult x the panel's own lattice level (max(width across + the edge, h_f), so coarser regions get proportionally longer strips) and by grade x the distance to the nearest + polygon vertex (coarse square cells therefore merge into longer strips than in proxy.strip_merge).""" + lines, S, verts = P._segments(metals) + near = shapely.STRtree(lines).nearest(shapely.points(c)) + d = S[near, 1] - S[near, 0] + horiz = np.abs(d[:, 0]) >= np.abs(d[:, 1]) + dv, _ = P.cKDTree(verts).query(c, workers=-1) + allow = np.minimum(lmax_mult * np.maximum(wh.min(1), h_f), grade * dv) + out_c, out_wh, out_o = [], [], [] + for axis, mask in ((0, horiz), (1, ~horiz)): + if not mask.any(): + continue + idx = np.nonzero(mask)[0] + other = 1 - axis + key = np.stack([o[idx], np.round(c[idx, other], 4), np.round(wh[idx, other], 4)], 1) + order = np.lexsort([c[idx, axis], key[:, 2], key[:, 1], key[:, 0]]) + idx, key = idx[order], key[order] + same = np.all(key[1:] == key[:-1], 1) + lo, hi = c[idx, axis] - wh[idx, axis] / 2, c[idx, axis] + wh[idx, axis] / 2 + adjacent = same & (np.abs(hi[:-1] - lo[1:]) < 1e-6) + start = 0 + while start < len(idx): + end, length, cap = start, wh[idx[start], axis], allow[idx[start]] + while end + 1 < len(idx) and adjacent[end] and length + wh[idx[end + 1], axis] <= min(cap, allow[idx[end + 1]]): + end += 1 + length += wh[idx[end], axis] + cap = min(cap, allow[idx[end]]) + cc, ww = np.zeros(2), np.zeros(2) + cc[axis], cc[other] = 0.5 * (lo[start] + hi[end]), c[idx[start], other] + ww[axis], ww[other] = hi[end] - lo[start], wh[idx[start], other] + out_c.append(cc) + out_wh.append(ww) + out_o.append(o[idx[start]]) + start = end + 1 + return np.array(out_c), np.array(out_wh), np.array(out_o) + + +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, + grade: float = 2.0, rich: bool = True, dense_max: int = 12000, levels: tuple = ()) -> tuple: + """Rows of the Maxwell matrix for the core conductors (metals[1:1+n_core]): (rows (n_core, len(metals)), info). + levels: distances (um) from the core beyond which the finest panel doubles (core-graded mesh); () = uniform.""" + info = {} + core = shapely.union_all(metals[1:1 + n_core]) + + def one(hh, tag, majority=False): + t = time.perf_counter() + raw = anchored_cells_graded(metals, hh, core, levels, grade, majority=majority) + c, wh, o = strip_merge(*raw, metals, hh, lmax_mult, merge_grade) + info[f"mesh_s_{tag}"], info[f"raw_{tag}"], info[f"panels_{tag}"] = time.perf_counter() - t, len(raw[0]), len(c) + info[f"share_{tag}"] = [float(np.mean(o == 0)), float(np.mean((o >= 1) & (o <= n_core))), float(np.mean(o > n_core))] + t = time.perf_counter() + B = (o[:, None] == np.arange(1, 1 + n_core)[None, :]).astype(float) + if len(c) <= dense_max: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + info[f"assemble_s_{tag}"] = time.perf_counter() - t + t = time.perf_counter() + try: + Q = P._chol_solve(A, B) + except np.linalg.LinAlgError: # cut panels of different nets overlap: whole finest cells by majority (as the flagship) + if majority: + raise + del A + info[f"majority_{tag}"] = True + return one(hh, tag, majority=True) + info[f"solver_{tag}"] = "dense" + else: + H = P._HMatrix(c, wh, eps_r, P.HMAT["leaf"], P.HMAT["eta"], P.HMAT["eps"]) + info[f"assemble_s_{tag}"] = time.perf_counter() - t + t = time.perf_counter() + Xp, _, _ = H.pcg(B[H.perm], P.HMAT["tol"], P.HMAT["maxit"]) + Q = np.empty_like(Xp) + Q[H.perm] = Xp + info[f"solver_{tag}"] = "H-matrix PCG" + info[f"solve_s_{tag}"] = time.perf_counter() - t + 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 + + Rf = one(h, "f") + if not rich: + return Rf, info + Rc = one(2 * h, "c") + return 2 * Rf - Rc, info + + +def canonical_hash(metals: list, n_core: int, h: float) -> str: + """Translation-invariant key of a window (lattice anchored to the signal bbox, coordinates rounded to 0.1 nm).""" + x0, y0 = shapely.union_all(metals[1:]).bounds[:2] + tr = [shapely.transform(g, lambda xy: np.round(xy - [x0, y0], 4)) for g in metals] + hsh = hashlib.sha1(f"{n_core}:{h:.9g}:".encode()) + for g in tr: + hsh.update(shapely.to_wkb(g)) + return hsh.hexdigest() + + +def _job(args): + wi, metals, n_core, h, kw = args + rows, info = solve_window_rows(metals, n_core, h, **kw) + return wi, rows, info + + +def local_prior(G, conds: list, eps_r: float = 11.45, cells_per_gap: float = 3.0, rich: bool = True, panel_budget: int = 12000, + halo_gaps: float = 100.0, margin_gaps: float = 100.0, margin_min_um: float = 150.0, fill_um: float = 250.0, + lmax_mult: float = 16.0, merge_grade: float = 1.0, workers: int = 1, cache: bool = True, dense_max: int = 12000, + global_budget: int | None = None, level_gaps: tuple = ()) -> tuple: + """Local prior of a layout (ground G, conductors conds, never merged): (C (N+1, N+1) fF with NaN for far pairs, + far_mask, info). Finest panel h = gap / cells_per_gap; with rich=True the Richardson pair is (h, 2 h). Windows + are solved in parallel on `workers` processes.""" + t_all = time.perf_counter() + metals_all = [G] + list(conds) + gap = gap_of(metals_all) + h = gap / cells_per_gap + R_g, margin = halo_gaps * gap, max(margin_gaps * gap, margin_min_um) + t = time.perf_counter() + wins = plan_windows(G, conds, h, R_g, margin, panel_budget, global_budget, level_rings=tuple(float(g) * gap for g in level_gaps)) + t_plan = time.perf_counter() - t + t = time.perf_counter() + mats = [window_metals(G, conds, w, fill_um, keep_um=R_g) for w in wins] + keys = [canonical_hash(m, w["n_core"], h) if cache else str(i) for i, (m, w) in enumerate(zip(mats, wins))] + t_build = time.perf_counter() - t + uniq = {} + for i, k in enumerate(keys): + uniq.setdefault(k, i) + kw = dict(eps_r=eps_r, lmax_mult=lmax_mult, merge_grade=merge_grade, rich=rich, dense_max=dense_max, + levels=tuple(float(g) * gap for g in level_gaps)) + jobs = [(i, mats[i], wins[i]["n_core"], h, kw) for i in uniq.values()] + t = time.perf_counter() + if workers > 1 and len(jobs) > 1: + with ProcessPoolExecutor(workers, mp_context=get_context("fork")) as ex: + res = {wi: (rows, info) for wi, rows, info in ex.map(_job, jobs)} + else: + res = {wi: (rows, info) for wi, rows, info in map(_job, jobs)} + t_solve = time.perf_counter() - t + N = len(conds) + 1 + acc, cnt = np.zeros((N, N)), np.zeros((N, N)) + for i, w in enumerate(wins): + rows, _ = res[uniq[keys[i]]] + idx = np.concatenate([[0], w["core"] + 1, w["halo"] + 1]).astype(int) + for k, ci in enumerate(w["core"] + 1): + acc[ci, idx] += rows[k] + cnt[ci, idx] += 1 + C = np.full((N, N), np.nan) + have = cnt > 0 + C[have] = acc[have] / cnt[have] + # symmetrise: mean of the two estimates when both exist, the one available otherwise; ground row from the columns + 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)) + S[0, 0] = np.nan + far = ~(have | have.T) + np.fill_diagonal(far, False) + infos = [res[j][1] for j in uniq.values()] + info = {"n_windows": len(wins), "n_unique": len(uniq), "h_um": h, "min_gap_um": gap, "R_g_um": R_g, "margin_um": margin, + "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], + "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], + "solvers": sorted({i["solver_f"] for i in infos}), "n_far_pairs": int(far[np.triu_indices(N, 1)].sum()), + "mesh_s": sum(i["mesh_s_f"] + i.get("mesh_s_c", 0.0) for i in infos), + "assemble_s": sum(i["assemble_s_f"] + i.get("assemble_s_c", 0.0) for i in infos), + "solve_s": sum(i["solve_s_f"] + i.get("solve_s_c", 0.0) for i in infos), + "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} + return S, far, info + + +# ---------------------------------------------------------------- flagship-fast: the cheap prior (PRIOR_SCHEME.md Part III, sections 12 and 16) +# Hybrid H = micro-MoM (`micro_mom`: the flagship mesh family at a fixed panel budget per window, dense Cholesky) for self, +# ground and co-windowed pairs + the closed-form edge-coupling prior (`formula_prior`) for pairs never co-windowed. +# Mirrored verbatim (function sources) in deploy/tlsq_infer/src/tlsq_infer/_vendor/fastprior.py. + +# formula constants: frozen 2026-10-09, not tuned on labels +FORMULA_STEP_UM = 0.25 # boundary sample step (as the pair tokens) +FORMULA_R_GAPS = 150.0 # pairs farther apart than this (in smallest gaps) use the bounding-box form +FORMULA_SCREEN_UM = 1.0 # the screening factor is evaluated every FORMULA_SCREEN_UM of boundary and held in between +LITE_REFINE = 3 # rejected window solves: refine h by 2 up to this many times before majority-owned cells +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 +LITE_WINDOW_BUDGET = 2.0 # windows of large layouts: panel budget = this x the budget of the one-window (component) prior +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) +LITE_MAX_CORE = [None] # windows of large layouts: conductors per core (None = the max_core argument; PRIOR_SCHEME.md 14.4.2) +LITE_GROWTH = 2.0 # merged panels per halving of h (measured 1.6-2.2 on the training families) +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) +DIPOLE_K = 8 # nearest boundary samples searched for the nearest other conductor (gbar) +ROW_SUM_EPS = 0.0143 # training median of 1 - sum_j |C_ij| / C_ii (self = row sum / (1 - eps)) +EPS0_FF_UM = 8.8541878128e-3 + + +def plan_cores(sites: np.ndarray, max_core: int = 8) -> list: + """Cores (index arrays) by recursive bisection of the conductor sites (n, 2) along the longer extent until each core + has <= max_core conductors; each split is snapped to the widest coordinate gap between sites in the central half + of the sorted order (ties broken by the other coordinate, so the cores do not depend on the conductor order).""" + out = [] + + def rec(idx): + if len(idx) <= max_core: + out.append(idx) + return + s = sites[idx] + ax = int(np.argmax(s.max(0) - s.min(0))) + order = idx[np.lexsort((s[:, 1 - ax], s[:, ax]))] + x = sites[order, ax] + lo = max(1, len(order) // 4) + hi = max(lo + 1, min(len(order), (3 * len(order)) // 4 + 1)) + m = lo + int(np.argmax(x[lo:hi] - x[lo - 1:hi - 1])) + rec(order[:m]), rec(order[m:]) + + rec(np.arange(len(sites))) + return out + + +def _rot90(k: int): + """Exact rotation of coordinates by k x 90 degrees (swaps and sign flips only).""" + def f(xy): + x, y = xy[:, 0], xy[:, 1] + return np.stack([(x, y), (-y, x), (-x, -y), (y, -x)][k % 4], 1) + return f + + +def canonical_frames(G, conds: list) -> list: + """The layout in canonical frames, so that the lattice-anchored mesh (and hence the prior) does not depend on the + position, the 90-degree orientation or the conductor order of the input: rotations by k x 90 degrees whose + rotation-invariant key (signal bounding box, sorted conductor centroids and convex-hull vertices about the box + centre, ground bounds, rounded to 1 nm) is smallest (several when the layout is symmetric), translation of the signal + bounding box corner to the origin, coordinates rounded to 1e-6 um, collinear vertices removed (`shapely.simplify(0)`), + rings in normal form (`shapely.normalize`), conductors sorted by (centroid, area). Returns [(G, conds, order)]: + conds[k] of a frame is input conductor order[k].""" + keys = [] + for k in range(4): + f = _rot90(k) + sig = [shapely.transform(c, f) for c in conds] + b = shapely.bounds(np.array(sig, dtype=object)) + x0, y0, x1, y1 = b[:, 0].min(), b[:, 1].min(), b[:, 2].max(), b[:, 3].max() + cen = shapely.get_coordinates(shapely.centroid(np.array(sig, dtype=object))) - [(x0 + x1) / 2, (y0 + y1) / 2] + hull = shapely.get_coordinates(shapely.convex_hull(shapely.union_all(sig)))[:-1] - [(x0 + x1) / 2, (y0 + y1) / 2] + gb = np.round(np.array(shapely.transform(G, f).bounds) - [(x0 + x1) / 2, (y0 + y1) / 2] * 2, 3) + key = ((round(x1 - x0, 3), round(y1 - y0, 3)) + tuple(sorted(map(tuple, np.round(cen, 3).tolist()))) + + tuple(sorted(map(tuple, np.round(hull, 3).tolist()))) + tuple(gb.tolist())) + keys.append((key, k, x0, y0)) + best = min(kk[0] for kk in keys) + out = [] + for key, k, x0, y0 in keys: + if key != best: + continue + f = _rot90(k) + + def to(g, f=f, x0=x0, y0=y0): + return shapely.normalize(shapely.simplify(shapely.transform(g, lambda xy: np.round(f(xy) - [x0, y0], 6)), 0.0)) + + cs = [to(c) for c in conds] + cen = shapely.get_coordinates(shapely.centroid(np.array(cs, dtype=object))) + order = np.lexsort((np.round([c.area for c in cs], 6), np.round(cen[:, 1], 6), np.round(cen[:, 0], 6))) + out.append((to(G), [cs[i] for i in order], order)) + return out + + +def _gap_size(metals: list) -> tuple: + """(smallest gap, layout size) exactly as mom_capacitance_gds_flagship computes them (all pairs; small layouts).""" + bbox = shapely.union_all(metals).bounds + size = max(bbox[2] - bbox[0], bbox[3] - bbox[1]) + gap = min(shapely.distance(metals[i], metals[j]) for i in range(len(metals)) for j in range(i + 1, len(metals))) + return gap, size + + +def _graded_mesh(metals: list, n_core: int, h: float, levels: tuple, majority: bool = False) -> tuple: + """Core-graded flagship-family mesh (`anchored_cells_graded` + `strip_merge`): finest panel h within levels[0] of the + core conductors (metals[1:1 + n_core]), doubling beyond each level. Returns (centres, sizes, owners, h, raw count).""" + raw = anchored_cells_graded(metals, h, shapely.union_all(metals[1:1 + n_core]), levels, majority=majority) + c, wh, o = strip_merge(*raw, metals, h) + return c, wh, o, h, len(raw[0]) + + +def _lite_mesh(metals: list, h: float, budget: float, n_core: int = 0, levels: tuple = ()) -> tuple: + """Flagship-family mesh at the finest lattice level h 2^k predicted to fit the budget: a probe mesh at the level + estimated (2 merged panels per h of metal edge) to hold ~budget / 4 panels, then the level LITE_GROWTH^j finer + predicted to hold <= budget panels (each halving of h multiplies the panels by ~LITE_GROWTH), doubled while it does + not fit. levels (um from the n_core core conductors): core-graded mesh (`_graded_mesh`); () = uniform + (`proxy.mesh_flagship`). With an unbounded budget: the mesh at h. Returns (centres, sizes, owners, h, raw count).""" + def mesh(hh): + if not levels: + return P.mesh_flagship(metals, hh, budget) + while True: + r = _graded_mesh(metals, n_core, hh, levels) + if len(r[0]) <= budget: + return r + hh *= 2.0 + + if not np.isfinite(budget): + return mesh(h) + L = sum(g.length for g in metals) + k = max(0, int(np.ceil(np.log2(max(2.0 * L / (h * budget / 4), 1.0))))) + probe = mesh(h * 2.0**k) + j = min(int(np.floor(np.log(budget / len(probe[0])) / np.log(LITE_GROWTH))), int(round(np.log2(probe[3] / h)))) + return probe if j <= 0 else mesh(probe[3] / 2.0**j) + + +def _sym_solve(A, B): + """Solve with the lower triangle of the Fortran-ordered A from `proxy._assemble`: Cholesky, or the symmetric + indefinite (Bunch-Kaufman) factorisation when A is not positive definite.""" + try: + return P._chol_solve(A.copy(order="F"), B) + except np.linalg.LinAlgError: + from scipy.linalg import solve + return solve(A, B, lower=True, assume_a="sym", overwrite_a=True, check_finite=False) + + +def _window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: float, all_rhs: bool, levels: tuple = ()) -> tuple: + """Flagship-family mesh of one window at the lattice level of h that fits the budget (`_lite_mesh`), exact near + field, dense Cholesky (H-matrix PCG above DENSE_MAX panels); right-hand sides: every metal (all_rhs, as + `proxy._cap_flagship`) or the n_core core conductors; levels: core-graded mesh (`_lite_mesh`). A solve is rejected + when the system is not positive definite, a metal has no panel or (all_rhs) a mutual entry is not negative (cut + 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 + meshed with majority-owned finest cells (as the flagship). Returns (R (n_rhs, n_metals) charges per metal, info).""" + cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core) + t_mesh = t_asm = t_sol = 0.0 + hh, cap, refined, majority = h, budget, 0, False + while True: + t = time.perf_counter() + if not refined: + c, wh, o, hh, n_raw = _lite_mesh(metals, hh, cap, n_core, levels) + elif levels: + c, wh, o, hh, n_raw = _graded_mesh(metals, n_core, hh, levels, majority) + else: + c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority) + t_mesh += time.perf_counter() - t + t = time.perf_counter() + try: + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + t_asm += time.perf_counter() - t + t = time.perf_counter() + Bm = (o[:, None] == cols[None, :]).astype(float) + if all_rhs: + Q = P._chol_solve(A, Bm) + else: # windows: an indefinite system (overlapping cut panels on the coarse graded levels) is solved by LDL^T + Q = _sym_solve(A, Bm) + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols] + R = np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + off = np.ones(R.shape, bool) + off[np.arange(len(cols)), cols] = False + ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0))) + except np.linalg.LinAlgError: + ok = False + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, + "assemble_s": t_asm, "solve_s": t_sol} + + +def _lite_window(G, conds: list, core, R_g: float, margin: float, fill_um: float, tree, etched) -> tuple: + """Window of a core: ([ground, core..., halo...], halo). Halo = conductors within R_g of a core conductor (separate + nets at 0 V); ground = chip ground clipped to the core + halo box widened by margin, plus every farther conductor in + the box as ground metal with its etched surroundings within fill_um filled where farther than R_g from a member.""" + hit = tree.query([conds[i] for i in core], predicate="dwithin", distance=R_g)[1] + halo = np.setdiff1d(np.unique(hit), core) + members = np.concatenate([core, halo]).astype(int) + b = shapely.bounds(np.array([conds[i] for i in members], dtype=object)) + box = (b[:, 0].min() - margin, b[:, 1].min() - margin, b[:, 2].max() + margin, b[:, 3].max() + margin) + far = np.setdiff1d(tree.query(shapely.box(*box), predicate="intersects"), members) + Gw = shapely.clip_by_rect(G, *box) + if len(far): + farg = shapely.clip_by_rect(shapely.union_all([conds[i] for i in far]), *box) + fill = shapely.intersection(shapely.clip_by_rect(etched, *box), farg.buffer(fill_um, quad_segs=4)) + fill = fill.difference(shapely.union_all([conds[i] for i in members]).buffer(R_g, quad_segs=4)) + Gw = shapely.union_all([Gw, farg, fill]) + return [_valid(Gw)] + [conds[i] for i in members], halo + + +_LITE_BUDGET_USED: list = [0.0] +_LITE: dict = {} # layout of the running micro_mom call, inherited by forked window workers + + +def _lite_job(k: int) -> tuple: + L = _LITE + t = time.perf_counter() + metals, halo = _lite_window(L["G"], L["conds"], L["cores"][k], L["R_g"], L["margin"], L["fill_um"], L["tree"], L["etched"]) + t_build = time.perf_counter() - t + bx = shapely.bounds(metals[0]) + h = max(L["h"], max(bx[2] - bx[0], bx[3] - bx[1]) / 4096) + budget = L["budget"] + if LITE_WINDOW_PPG[0]: # resolution-controlled windows (PRIOR_SCHEME.md 14.4): finest panel gap / ppg, no budget + h, budget = L["gap"] / LITE_WINDOW_PPG[0], np.inf + R, info = _window_solve(metals, len(L["cores"][k]), h, budget, L["eps_r"], False, L["levels"]) + info.update(build_s=t_build, n_halo=len(halo), n_core=len(L["cores"][k])) + return k, halo, R, info + + +def micro_mom(G, conds: list, eps_r: float = 11.45, budget: float = 1500, cells_per_gap: float = 3.0, max_core: int = 8, + halo_gaps: float = 100.0, margin_gaps: float = 100.0, margin_min_um: float = 150.0, fill_um: float = 250.0, + workers: int = 1, level_gaps: tuple = LITE_LEVEL_GAPS, window_budget: float | None = None, canonical: bool = True) -> tuple: + """Cheap micro-MoM prior (PRIOR_SCHEME.md section 12, M / hybrid H): (C (N+1, N+1) fF Maxwell, far_mask, info). + <= max_core conductors: one global window = the layout as given, h = max(gap / cells_per_gap, size / 4096) doubled + on the flagship lattice until the merged mesh has <= budget panels, every metal excited (with budget = inf this is + `proxy.mom_capacitance_gds_flagship(rich=False)` bit for bit; `_lite_mesh` picks the level). More conductors: cores of <= max_core by bisection, + one window per core (halo within halo_gaps, ground clipped at margin_gaps), entry (i, j) = mean of the two window + estimates when both exist (a non-negative window estimate is not used), ground row from the columns, C_00 = row sum / + (1 - ROW_SUM_EPS); pairs never co-windowed (far_mask, > halo_gaps apart) in different ground pockets take the screened + dipole value (`dipole_far`), in the same pocket the closed form (`formula_prior`, quadrature step one smallest gap). level_gaps: core-graded window + meshes (finest panel within level_gaps[0] smallest gaps of the core, doubling beyond each level); window systems that + are not positive definite are solved by LDL^T; window budget = window_budget or LITE_WINDOW_BUDGET x budget. Windows + run on `workers` forked processes. canonical: solved in the `canonical_frames` (mean over them when the layout is + symmetric; invariant to translation, 90-degree rotation and conductor order), returned in the input order.""" + t_all = time.perf_counter() + if canonical: # mean over the canonical frames (one unless the layout is symmetric), in the input order + runs = [] + for Gc, cc, order in canonical_frames(G, conds): + C, far, info = micro_mom(Gc, cc, eps_r, budget, cells_per_gap, max_core, halo_gaps, margin_gaps, margin_min_um, fill_um, + workers, level_gaps, window_budget, canonical=False) + inv = np.r_[0, np.argsort(order) + 1] + runs.append((C[np.ix_(inv, inv)], far[np.ix_(inv, inv)], info)) + C = runs[0][0] if len(runs) == 1 else sum(r[0] for r in runs) / len(runs) + far = np.logical_or.reduce([r[1] for r in runs]) + info = runs[0][2] + for k in ("mesh_s", "assemble_s", "solve_s", "build_s", "formula_s"): + info[k] = sum(r[2][k] for r in runs) + info["n_frames"] = len(runs) + info["total_s"] = time.perf_counter() - t_all + return C, far, info + metals = [G] + list(conds) + N = len(metals) + far = np.zeros((N, N), bool) + if len(conds) <= max_core: + gap, size = _gap_size(metals) + h = max(gap / cells_per_gap, size / 4096) + R, wi = _window_solve(metals, len(conds), h, budget, eps_r, True) + C = 0.5 * (R + R.T) + info = {"n_windows": 1, "min_gap_um": gap, "h_um": [wi["h_um"]], "panels": [wi["panels"]], "panels_per_gap": [gap / wi["h_um"]], + "majority": [wi["majority"]], "refined": [wi["refined"]], "mesh_s": wi["mesh_s"], "assemble_s": wi["assemble_s"], "solve_s": wi["solve_s"], + "build_s": 0.0, "plan_s": 0.0, "formula_s": 0.0, "n_far_pairs": 0, "budget": budget} + info["total_s"] = time.perf_counter() - t_all + return C, far, info + t = time.perf_counter() + gap = gap_of(metals) + R_g, margin = halo_gaps * gap, max(margin_gaps * gap, margin_min_um) + b = shapely.bounds(np.array(conds, dtype=object)) + cores = plan_cores(np.stack([(b[:, 0] + b[:, 2]) / 2, (b[:, 1] + b[:, 3]) / 2], 1), LITE_MAX_CORE[0] or max_core) + tree = shapely.STRtree(conds) + etched = shapely.box(*G.bounds).difference(G) + _LITE.clear() + _LITE.update(G=G, conds=conds, cores=cores, R_g=R_g, margin=margin, fill_um=fill_um, tree=tree, etched=etched, + h=gap / cells_per_gap, gap=gap, budget=LITE_WINDOW_BUDGET * budget if window_budget is None else window_budget, eps_r=eps_r, + levels=tuple(float(x) * gap for x in level_gaps)) + _LITE_BUDGET_USED[:] = [_LITE["budget"]] + t_plan = time.perf_counter() - t + t = time.perf_counter() + if workers > 1 and len(cores) > 1: + with ProcessPoolExecutor(min(workers, len(cores)), mp_context=get_context("fork")) as ex: + res = list(ex.map(_lite_job, range(len(cores)))) + else: + res = [_lite_job(k) for k in range(len(cores))] + t_win = time.perf_counter() - t + _LITE.clear() + acc, cnt = np.zeros((N, N)), np.zeros((N, N)) + for k, halo, R, _ in res: + idx = np.concatenate([[0], cores[k] + 1, halo + 1]).astype(int) + for r, ci in enumerate(cores[k] + 1): + use = (R[r] < 0) | (idx == ci) # a non-negative mutual estimate of a window (tiny far entries) is not used + acc[ci, idx[use]] += R[r][use] + cnt[ci, idx[use]] += 1 + have = cnt > 0 + Cw = np.where(have, acc / np.where(have, cnt, 1), 0.0) + C = np.where(have & have.T, 0.5 * (Cw + Cw.T), np.where(have, Cw, Cw.T)) + far = ~(have | have.T) + np.fill_diagonal(far, False) + t = time.perf_counter() + iu = np.nonzero(np.triu(far, 1)) + t_dip, n_dip = 0.0, 0 + if len(iu[0]): + pid = np.r_[-10**9, pocket_ids(G, conds)] + cross = pid[iu[0]] != pid[iu[1]] # different ground pockets: screened dipole; same pocket: closed form + if (~cross).any(): + F, _ = formula_prior(G, conds, eps_r, step_um=max(FORMULA_STEP_UM, gap), pairs=np.stack(iu, 1)[~cross], gap=gap, workers=workers) + C[iu[0][~cross], iu[1][~cross]] = F[iu[0][~cross], iu[1][~cross]] + if cross.any(): + t_dip = time.perf_counter() + C[iu[0][cross], iu[1][cross]] = dipole_far(G, conds, np.stack(iu, 1)[cross], eps_r, gap) + t_dip, n_dip = time.perf_counter() - t_dip, int(cross.sum()) + C[iu[1], iu[0]] = C[iu] + t_form = time.perf_counter() - t + C[0, 0] = np.abs(C[0, 1:]).sum() / (1 - ROW_SUM_EPS) + infos = [r[3] for r in res] + 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], + "panels": [i["panels"] for i in infos], "panels_per_gap": [gap / i["h_um"] for i in infos], + "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], + "mesh_s": sum(i["mesh_s"] for i in infos), "assemble_s": sum(i["assemble_s"] for i in infos), + "solve_s": sum(i["solve_s"] for i in infos), "build_s": sum(i["build_s"] for i in infos), "plan_s": t_plan, + "windows_wall_s": t_win, "formula_s": t_form, "n_far_pairs": int(len(iu[0])), "dipole_s": t_dip, + "n_dipole_pairs": n_dip, "budget": _LITE_BUDGET_USED[0], + "workers": workers, "level_gaps": list(level_gaps)} + info["total_s"] = time.perf_counter() - t_all + return C, far, info + + +def pocket_ids(G, conds: list) -> np.ndarray: + """Ground pocket of every conductor: index of the interior ring of the ground metal that contains it (STRtree of the + rings' polygons, `within`); a conductor in no ring gets its own id (-1 - k).""" + rings = [shapely.Polygon(r) for p in getattr(G, "geoms", [G]) for r in p.interiors] + out = -1 - np.arange(len(conds)) + if rings: + c, r = shapely.STRtree(rings).query(list(conds), predicate="within") + out[c] = r + return out + + +def _edge_samples(geoms: list, step: float) -> tuple: + """Points every <= step along every polygon edge of geoms (vertices included), with the index of their geometry: + (points (n, 2), owner (n,)). Vectorised over all edges (no per-ring interpolation).""" + segs, sown = [], [] + for k, g in enumerate(geoms): + for p in getattr(g, "geoms", [g]): + for ring in [p.exterior, *p.interiors]: + xy = np.asarray(ring.coords) + segs.append(np.stack([xy[:-1], xy[1:]], 1)) + sown.append(np.full(len(xy) - 1, k)) + S, sown = np.concatenate(segs), np.concatenate(sown) + L = np.sqrt(((S[:, 1] - S[:, 0]) ** 2).sum(1)) + n = np.maximum(1, np.ceil(L / step).astype(int)) + seg = np.repeat(np.arange(len(S)), n) + t = (np.arange(n.sum()) - np.repeat(np.cumsum(n) - n, n)) / np.repeat(n, n) + return S[seg, 0] + t[:, None] * (S[seg, 1] - S[seg, 0]), sown[seg] + + +def dipole_far(G, conds: list, pairs, eps_r: float = 11.45, gap: float | None = None) -> np.ndarray: + """Screened-dipole far couplings (PRIOR_SCHEME.md 14.2): for metal-index pairs (k, 2) (1 = first conductor), + 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 + 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 + nearest other metal: ground edge samples (step gap / 4) by one k-d tree, other conductors by one k-d tree of all + conductor samples with owner labels (DIPOLE_K neighbours). Returns the values (k,).""" + metals = [G] + list(conds) + gap = gap_of(metals) if gap is None else gap + pairs = np.asarray(pairs, int).reshape(-1, 2) + pts, own = _edge_samples(list(conds), gap) + own = own + 1 + sel = np.isin(own, np.unique(pairs)) + q = pts[sel] + dg, _ = cKDTree(_edge_samples([G], gap / 4)[0]).query(q) + d, idx = cKDTree(pts).query(q, k=min(DIPOLE_K, len(own))) + if d.ndim == 1: + d, idx = d[:, None], idx[:, None] + d = np.where(own[np.minimum(idx, len(own) - 1)] != own[sel][:, None], d, np.inf).min(1) + gbar = np.bincount(own[sel], np.minimum(dg, d), len(metals)) / np.maximum(np.bincount(own[sel], minlength=len(metals)), 1) + area = np.array([g.area for g in metals]) + perim = np.array([g.length for g in metals]) + At = area + ALPHA_GAP_SHARE * perim * gbar + cen = shapely.get_coordinates(shapely.centroid(np.array(metals, dtype=object))) + a, b = pairs[:, 0], pairs[:, 1] + d2 = ((cen[a] - cen[b]) ** 2).sum(1) + (At[a] + At[b]) / np.pi + return -(1 + eps_r) * EPS0_FF_UM * At[a] * At[b] / (2 * np.pi * d2 ** 1.5) + + +def _ground_segments(G): + """Edge segments (m, 2, 2) of the ground metal and an STRtree over them.""" + _, S, _ = P._segments([G]) + return S, shapely.STRtree(shapely.linestrings(S)) + + +def _metal_length(A: np.ndarray, B: np.ndarray, S: np.ndarray, tree) -> np.ndarray: + """Length of ground metal crossed by each segment A[k] -> B[k] (endpoints off the metal): crossings with the ground + edges (STRtree) sorted along the segment, alternate inside / outside intervals summed.""" + out = np.zeros(len(A)) + if not len(A): + return out + li, gi = tree.query(shapely.linestrings(np.stack([A, B], 1)), predicate="intersects") + if not len(li): + return out + p, r = A[li], B[li] - A[li] + q, s = S[gi, 0], S[gi, 1] - S[gi, 0] + den = r[:, 0] * s[:, 1] - r[:, 1] * s[:, 0] + ok = np.abs(den) > 1e-12 + 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) + order = np.lexsort((t, li)) + li, t = li[order], t[order] + start = np.r_[0, np.nonzero(np.diff(li))[0] + 1] + n = np.diff(np.r_[start, len(li)]) + rank = np.arange(len(li)) - np.repeat(start, n) + keep = rank < np.repeat(n // 2 * 2, n) + sgn = np.where(rank % 2 == 1, 1.0, -1.0) + out += np.bincount(li[keep], weights=(sgn * t)[keep], minlength=len(A)) + return out * np.sqrt(((B - A) ** 2).sum(1)) + + +def _cps_m(d, w1, w2): + """Coplanar-strip modulus (as `blocks.edge_features`; w = inf for the ground).""" + if np.isinf(w1) or np.isinf(w2): + wf = w2 if np.isinf(w1) else w1 + return d / (d + wf) + return d * (d + w1 + w2) / ((d + w1) * (d + w2)) + + +_FORM: dict = {} # samples and trees of the running formula_prior call, inherited by forked row workers + + +def _formula_row(i: int) -> tuple: + """Row i of the directed sums I[i, j] of `formula_prior` (j = the near metals of i; ground last term included).""" + F = _FORM + 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"] + I = np.zeros(near.shape[0]) + dmin_other, dG = np.full(len(pts[i]), np.inf), None + for j in np.nonzero(near[i])[0]: + d, idx = kd[j].query(pts[i], distance_upper_bound=R if j > 0 else np.inf) + ok = np.isfinite(d) + if j > 0: + dmin_other = np.minimum(dmin_other, d) + else: + dG = d + if not ok.any(): + continue + dd = np.maximum(d[ok], 0.05) + if j == 0: + S = np.ones(ok.sum()) + else: + sel = np.arange(0, ok.sum(), every) + A = pts[i][ok][sel] + lg = _metal_length(A, pts[j][idx[ok][sel]], F["SG"], F["gtree"]) + S = np.exp(-lg / (dd[sel] + min(width[i], width[j])))[np.arange(ok.sum()) // every] + I[j] = k * np.sum(step_um * _kratio(_cps_m(dd, width[i], width[j])) * S) + if dG is not None: + share = float(np.mean(dG <= dmin_other)) + I[0] += 8 * EPS0_FF_UM * (1 + F["eps_r"]) / 2 * np.sqrt(F["area"][i] / np.pi) * share + return i, I + + +def formula_prior(G, conds: list, eps_r: float = 11.45, step_um: float = FORMULA_STEP_UM, R_gaps: float = FORMULA_R_GAPS, + pairs=None, gap: float | None = None, screen_um: float = FORMULA_SCREEN_UM, workers: int = 1, + max_core: int = 8) -> tuple: + """Closed-form edge-coupling prior (PRIOR_SCHEME.md section 12, F): (C (N+1, N+1) fF Maxwell, info). No mesh, no solve. + 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 + samples p of i (modulus of the local widths 2 A / P and the distance to the nearest sample of j, within R_gaps + 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 + 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 + ground). Pairs farther than R_gaps: the same form on the bounding boxes (facing length (P_i + P_j) / 2, distance of + the boxes, l_G = ground area fraction x distance). Self terms: row sums / (1 - ROW_SUM_EPS). pairs (k, 2) of metal + indices (0 = ground): only those mutual entries (the rest 0, no self terms). More than max_core conductors: the + quadrature step is at least gap / 4 and the screening spacing at least one gap (numerically equivalent: median + 0.02 %, max 1.2 % on the grids vs 0.25 um), rows on `workers` forked processes.""" + t0 = time.perf_counter() + metals = [G] + list(conds) + N = len(metals) + gap = gap_of(metals) if gap is None else gap + if len(conds) > max_core: + step_um, screen_um = max(step_um, gap / 4), max(screen_um, gap) + R = R_gaps * gap + k = EPS0_FF_UM * (1 + eps_r) / 2 + want = np.zeros((N, N), bool) + if pairs is None: + want[np.triu_indices(N, 1)] = True + else: + pairs = np.sort(np.asarray(pairs, int).reshape(-1, 2), 1) + want[pairs[:, 0], pairs[:, 1]] = True + area = np.array([g.area for g in metals]) + perim = np.array([g.length for g in metals]) + width = np.r_[np.inf, 2 * area[1:] / perim[1:]] + tree = shapely.STRtree(list(conds)) + near = np.zeros((N, N), bool) + a, b = tree.query(list(conds), predicate="dwithin", distance=R) + near[a + 1, b + 1] = True + near[0, 1:] = near[1:, 0] = True + np.fill_diagonal(near, False) + near &= want | want.T + gx0, gy0, gx1, gy1 = G.bounds + need = np.unique(np.nonzero(near)[0]) + pts, kd = {}, {} + for i in np.unique(np.r_[need, np.nonzero(near.any(0))[0]]): + p = sample_boundary(metals[i], step_um) + if i == 0: # ground edge = boundary of the etched region (not the chip outline) + 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) + p = p[~on] + pts[i] = p + kd[i] = cKDTree(p) + SG, gtree = _ground_segments(G) + _FORM.clear() + _FORM.update(pts=pts, kd=kd, near=near, width=width, area=area, R=R, k=k, step=step_um, eps_r=eps_r, + every=max(1, int(round(screen_um / step_um))), SG=SG, gtree=gtree) + rows = [i for i in need if i > 0] + if workers > 1 and len(rows) > 1: + with ProcessPoolExecutor(min(workers, len(rows)), mp_context=get_context("fork")) as ex: + res = list(ex.map(_formula_row, rows, chunksize=max(1, len(rows) // (4 * workers)))) + else: + res = [_formula_row(i) for i in rows] + _FORM.clear() + I = np.zeros((N, N)) + for i, row in res: + I[i] = row + C = np.zeros((N, N)) + iu = np.nonzero(np.triu(near, 1)) + gnd = iu[0] == 0 + C[iu] = np.where(gnd, I[iu[1], iu[0]], 0.5 * (I[iu] + I[iu[1], iu[0]])) + fa, fb = np.nonzero(np.triu(want & ~near & ~near.T, 1)) + if len(fa): + bb = shapely.bounds(np.array(metals, dtype=object)) + dx = np.maximum(0.0, np.maximum(bb[fa, 0] - bb[fb, 2], bb[fb, 0] - bb[fa, 2])) + dy = np.maximum(0.0, np.maximum(bb[fa, 1] - bb[fb, 3], bb[fb, 1] - bb[fa, 3])) + dd = np.maximum(np.hypot(dx, dy), 0.05) + fG = G.area / max(shapely.box(*G.bounds).area, 1e-12) + w1, w2 = width[fa], width[fb] + m = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2)) + S = np.exp(-fG * dd / (dd + np.minimum(w1, w2))) + C[fa, fb] = k * 0.5 * (perim[fa] + perim[fb]) * _kratio(m) * S + C = -(C + C.T) + info = {"n_sampled_pairs": int(len(iu[0])), "n_bbox_pairs": int(len(fa)), "min_gap_um": gap, "step_um": step_um, + "samples": int(sum(len(p) for p in pts.values()))} + if pairs is None: + np.fill_diagonal(C, np.abs(C).sum(1) / (1 - ROW_SUM_EPS)) + info["total_s"] = time.perf_counter() - t0 + return C, info diff --git a/geomgnn/_tlsq/embed/fastprior_loc.py b/geomgnn/_tlsq/embed/fastprior_loc.py new file mode 100644 index 0000000000000000000000000000000000000000..192c71d44cbb0044b22f60215cfbe9100bde288c --- /dev/null +++ b/geomgnn/_tlsq/embed/fastprior_loc.py @@ -0,0 +1,210 @@ +"""Local-gap (graded) lite micro-MoM prior: PRIOR_SCHEME.md Amendment MESH (2026-10-11). + +The lite prior meshes every metal edge at h = gap / ppg with gap the layout's global smallest conductor gap. A sliver +(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 +floor at every ppg (the resolution knob is dead). Here the panel size is set by the LOCAL gap of each quadtree cell: + + h(cell) = clamp(local_gap(cell) / ppg, h_floor, H), H = G_ref / ppg, h_floor = size / FLOOR_DIV + +local_gap(cell) = min over the metals k the cell touches and the other metals j of distance(cell & metal_k, metal_j) +(the gap of the edge stretch inside the cell, never smaller than the global smallest gap); G_ref = the smallest pair +gap among conductor pairs whose gap is >= SLIVER_UM ("sliver" pairs only refine their own neighbourhood). Same lattice +(H 2^-k, anchored to the signal bounding box), exact cut panels, strip merge bounded by each panel's own size, slab +kernel and exact near field as `fastprior.micro_mom`. A layout without a sliver pair is passed to `fastprior.micro_mom` +unchanged (bit-identical prior). Resolution descriptor: proxy_min_gap_um = G_ref, proxy_h_min_um = H (log ppg). +""" + +from __future__ import annotations + +import time + +import numpy as np +import shapely + +from . import fastprior as F +from . import proxy as P + +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) +FLOOR_DIV = 4096 # h_floor = layout size / FLOOR_DIV (the existing floor) + + +def ref_gap(metals: list, sliver: float = SLIVER_UM) -> tuple: + """(G_ref, smallest gap, layout size, number of sliver pairs). G_ref = smallest pair gap >= sliver; when every pair + is a sliver, the edge-length-weighted 5th percentile of the local gap of the boundary (0.25 um samples).""" + gmin, size = F._gap_size(metals) + n = len(metals) + d = np.array([shapely.distance(metals[i], metals[j]) for i in range(n) for j in range(i + 1, n)]) + ok = d >= sliver + if ok.any(): + return float(d[ok].min()), float(gmin), float(size), int((~ok).sum()) + lg = [] + for i, g in enumerate(metals): + others = shapely.union_all([metals[k] for k in range(n) if k != i]) + for ln in getattr(g.boundary, "geoms", [g.boundary]): + pts = shapely.line_interpolate_point(ln, np.arange(0, ln.length, 0.25)) + lg.append(shapely.distance(pts, others)) + return float(max(np.percentile(np.concatenate(lg), 5), gmin)), float(gmin), float(size), int((~ok).sum()) + + +def _local_gap(metals: list, boxes, hit, inn, pcs, sel) -> np.ndarray: + """local_gap of the cells sel (indices): min over touched metals k and other metals j of distance(cell & k, j).""" + lg = np.full(len(sel), np.inf) + m = len(metals) + for k in range(m): + t = hit[k, sel] + if not t.any(): + continue + ix = sel[t] + geo = np.where(inn[k, ix], boxes[ix], pcs[k][ix]) + bad = np.array([g is None for g in geo]) + geo[bad] = boxes[ix][bad] + dk = np.full(len(ix), np.inf) + for j in range(m): + if j != k: + dk = np.minimum(dk, shapely.distance(geo, metals[j])) + lg[t] = np.minimum(lg[t], dk) + return lg + + +def anchored_cells_local(metals: list, H: float, h_floor: float, ppg: float, grade: float = 2.0, majority: bool = False): + """`proxy.anchored_cells` on the lattice of H (anchored to the signal bounding box) with the finest level of every + cell set by its local gap: a cell (cut or whole) is final once h <= clamp(local_gap / ppg, h_floor, H). Returns + centres (n, 2), sizes (n, 2), owner (n,).""" + edges = shapely.union_all([g.boundary for g in metals]) + sig = metals[1:] if len(metals) > 1 else metals + ax, ay = shapely.union_all(sig).bounds[:2] + X0, Y0, X1, Y1 = shapely.union_all(metals).bounds + span = max(X1 - X0, Y1 - Y0) + K = max(0, int(np.ceil(np.log2(max(span / 16, H) / H)))) + h0 = H * 2**K + ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0)) + ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0)) + gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1)) + xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0) + gq = P._Geoms(list(metals) + [edges]) + tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool) + out_c, out_h, out_o = [], [], [] + while len(xs): + boxes = shapely.box(xs, ys, xs + h, ys + h) + cov, hit, inn, pcs = P._cover(gq, boxes, h, tst, inn) + keep = cov.sum(0) > 1e-3 + xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep] + pcs = [p[keep] for p in pcs] + cx, cy = xs + h / 2, ys + h / 2 + full = cov.max(0) > 0.999 + cand = h <= H * 1.0001 + last = cand & (h <= h_floor * 1.0001) + need = np.nonzero(cand & ~last)[0] + if len(need): + tgt = np.clip(_local_gap(metals, boxes, hit, inn, pcs, need) / ppg, h_floor, H) + last[need] = h[need] <= tgt * 1.0001 + d = np.full(len(xs), np.inf) + far = full & ~last + d[far] = P._distance(gq, len(metals), boxes[far]) + leaf = full & (last | (h <= grade * d + 1e-12)) + out_c.append(np.stack([cx[leaf], cy[leaf]], 1)) + out_h.append(np.stack([h[leaf], h[leaf]], 1)) + out_o.append(cov[:, leaf].argmax(0)) + cut = last & ~leaf + if majority and cut.any(): + take = cut & (cov.max(0) >= 0.5) + out_c.append(np.stack([cx[take], cy[take]], 1)) + out_h.append(np.stack([h[take], h[take]], 1)) + out_o.append(cov[:, take].argmax(0)) + cut = cut & False + if cut.any(): + gs = gq.get() + for k, g in enumerate(gs[:len(metals)]): + shapely.prepare(g) + sel = np.nonzero(cut & (cov[k] > 1e-3))[0] + if not len(sel): + continue + pcs_k = pcs[k][sel] + bb = shapely.bounds(pcs_k) + cen = shapely.get_coordinates(shapely.centroid(pcs_k)) + inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1]) + if not inside.all(): + cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside])) + out_c.append(cen) + out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6)) + out_o.append(np.full(len(sel), k)) + gq.put(gs) + split = ~leaf & ~last + hs = h[split] / 2 + xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs]) + ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs]) + h = np.concatenate([hs] * 4) + tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4) + return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o) + + +def mesh_local(metals: list, H: float, h_floor: float, ppg: float, majority: bool = False) -> tuple: + """Strip-merged local mesh (strip length <= 16 x the panel's own size and <= the distance to the nearest vertex). + Returns (centres, sizes, owners, raw count).""" + raw = anchored_cells_local(metals, H, h_floor, ppg, majority=majority) + c, wh, o = F.strip_merge(*raw, metals, h_floor) + return c, wh, o, len(raw[0]) + + +def _solve(metals: list, H: float, h_floor: float, ppg: float, eps_r: float) -> tuple: + """`fastprior._window_solve(all_rhs=True)` on the local mesh: rejected solves (not PD, a metal without a panel, a + non-negative mutual entry) refine (H, h_floor) / 2 up to LITE_REFINE times, then majority-owned cut cells.""" + t_mesh = t_sol = 0.0 + cols = np.arange(len(metals)) + refined, majority = 0, False + while True: + t = time.perf_counter() + c, wh, o, n_raw = mesh_local(metals, H, h_floor, ppg, majority) + t_mesh += time.perf_counter() - t + t = time.perf_counter() + try: + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + Q = P._chol_solve(A, (o[:, None] == cols[None, :]).astype(float)) + del A + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r) + R = np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + off = ~np.eye(len(metals), dtype=bool) + ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and np.all(R[off] < 0)) + except np.linalg.LinAlgError: + ok = False + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < F.LITE_REFINE: + H, h_floor, refined = H / 2, h_floor / 2, refined + 1 + else: + majority = True + return R, {"H_um": H, "h_floor_um": h_floor, "h_min_um": float(wh.min()), "panels": len(c), "raw": n_raw, + "refined": refined, "majority": majority, "mesh_s": t_mesh, "solve_s": t_sol} + + +def micro_mom_loc(G, conds: list, eps_r: float = 11.45, ppg: float = 3.0, sliver: float = SLIVER_UM, + floor_div: float = FLOOR_DIV, canonical: bool = True) -> tuple: + """Local-gap lite prior of a <= 8-conductor layout: (C (N+1, N+1) fF, far_mask (all False), info). Layouts without a + sliver pair: `fastprior.micro_mom(budget=inf, cells_per_gap=ppg)` (the existing liteppg prior, bit-identical). + Mean over the canonical frames of symmetric layouts, returned in the input order (as micro_mom).""" + t_all = time.perf_counter() + metals = [G] + list(conds) + gref, gmin, size, n_sl = ref_gap(metals, sliver) + if n_sl == 0 or len(conds) > 8: # no sliver (or windows: out of scope) -> the existing prior + C, far, info = F.micro_mom(G, conds, eps_r, budget=np.inf, cells_per_gap=ppg) + info.update(local=False, G_ref_um=float(info["min_gap_um"]), H_um=float(min(info["h_um"])), n_sliver_pairs=n_sl) + return C, far, info + runs = [] + frames = F.canonical_frames(G, conds) if canonical else [(G, list(conds), np.arange(len(conds)))] + for Gc, cc, order in frames: + R, wi = _solve([Gc] + list(cc), gref / ppg, size / floor_div, ppg, eps_r) + C = 0.5 * (R + R.T) + inv = np.r_[0, np.argsort(order) + 1] + runs.append((C[np.ix_(inv, inv)], wi)) + C = sum(r[0] for r in runs) / len(runs) + wi = runs[0][1] + info = {"local": True, "n_windows": 1, "n_frames": len(runs), "min_gap_um": gref, "G_ref_um": gref, "gmin_um": gmin, + "n_sliver_pairs": n_sl, "H_um": wi["H_um"], "h_floor_um": wi["h_floor_um"], "h_um": [wi["H_um"]], + "h_min_um": wi["h_min_um"], "panels": [wi["panels"]], "panels_per_gap": [gref / wi["H_um"]], + "majority": [wi["majority"]], "refined": [wi["refined"]], + "mesh_s": sum(r[1]["mesh_s"] for r in runs), "solve_s": sum(r[1]["solve_s"] for r in runs)} + info["total_s"] = time.perf_counter() - t_all + return C, np.zeros(C.shape, bool), info diff --git a/geomgnn/_tlsq/embed/proxy.py b/geomgnn/_tlsq/embed/proxy.py new file mode 100644 index 0000000000000000000000000000000000000000..27f8c4401fead85b91d59edf40669bde07d69e59 --- /dev/null +++ b/geomgnn/_tlsq/embed/proxy.py @@ -0,0 +1,944 @@ +"""Cheap physics proxy: thin-film method-of-moments capacitance on the metal masks. + +Planar conductors of negligible thickness on a dielectric half-space: the quasi-static Green's function is +1 / (4 pi eps0 eps_eff r) with eps_eff = (eps_r + 1) / 2. The metal is discretised into square cells (pulse basis, +point matching); a uniformly charged square of side a has self-potential q / (4 pi eps0 eps_eff) * 4 ln(1 + sqrt 2) / a. +Solving P q = v for each conductor at 1 V (others at 0 V) gives the full Maxwell capacitance matrix, including +shielding and other non-local effects that local descriptors cannot represent. Truncation: the ground plane is +represented only inside the design window (a proxy, not a replacement for Q3D). + +v2 (`mom_capacitance_gds`) works on the exact GDS polygons with the whole ground plane and an adaptive quadtree mesh +(cells no larger than `grade` x their distance to a metal edge, edge cells down to ~1/3 of the smallest gap). On the +gallery designs it reaches 5-6 % (NCAP, star) and 12 % (concentric) median error with no training, vs 14-32 % for v1. +""" + +from __future__ import annotations + +import os +import queue +import threading +from concurrent.futures import ThreadPoolExecutor +from functools import partial + +import numpy as np +import shapely +from scipy.linalg import cho_factor, cho_solve +from scipy.spatial import cKDTree + +EPS0_FF_PER_UM = 8.8541878128e-3 +SELF = 4 * np.log(1 + np.sqrt(2)) +NUM_THREADS = 0 # threads of the mesh / assembly pool and of the concurrent v3-rich solves; 0 = torch.get_num_threads() +_LOCK = threading.Lock() +_POOL: list = [] # [(threads, executor)] +if hasattr(os, "register_at_fork"): + os.register_at_fork(after_in_child=_POOL.clear) # a forked worker must not reuse the parent's pool threads + + +def _nthreads() -> int: + if NUM_THREADS: + return NUM_THREADS + try: + import torch + except ImportError: + return 1 + return torch.get_num_threads() + + +def _run(tasks: list) -> list: + """Results of the zero-argument callables in order, on the thread pool when more than one thread is allowed (numpy + ufuncs and shapely's vectorised functions release the GIL). Tasks never wait on other pool tasks.""" + n = _nthreads() + if n < 2 or len(tasks) < 2: + return [t() for t in tasks] + with _LOCK: + if not _POOL or _POOL[0][0] != n: + _POOL[:] = [(n, ThreadPoolExecutor(n, thread_name_prefix="tlsq-mom"))] + pool = _POOL[0][1] + return list(pool.map(lambda t: t(), tasks)) + + +def _spans(n: int, size: int) -> list: + return [(a, min(a + size, n)) for a in range(0, n, size)] + + +class _Geoms: + """Private copies of geometries (exact WKB round trip) handed out to one pool task at a time and made on demand: GEOS + prepared geometries keep mutable search state and must not be shared between threads (nor with a concurrent solve).""" + + def __init__(self, geoms: list): + self.wkb, self.free = shapely.to_wkb(geoms), queue.SimpleQueue() + + def get(self) -> list: + try: + return self.free.get_nowait() + except queue.Empty: + return list(shapely.from_wkb(self.wkb)) + + def put(self, gs: list) -> None: + self.free.put(gs) + + +def _cover(gq, boxes, h, tst, inn): + """Coverage area(box & metal) / h^2 of every box by every metal (m, n), evaluated exactly only where a box straddles a + metal: a box properly inside a metal covers 1 and a box disjoint from it 0 (same decisions as the exact areas). tst + (m, n): metals the box can touch (its parent did); inn (m, n): metals the box lies properly inside (from its parent). + Returns cov, hit (box intersects the metal), inn, and per metal the pieces box & metal where they were computed.""" + m, n = tst.shape + cov, hit, inn = inn.astype(float), inn.copy(), inn.copy() + pcs = [np.empty(n, dtype=object) for _ in range(m)] + 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)] + + def task(k, ix): + gs = gq.get() + try: + g = gs[k] + shapely.prepare(g) + b = boxes[ix] + ht = shapely.intersects(g, b) + it = shapely.contains_properly(g, b) + p = shapely.intersection(b[ht & ~it], g) + return ht, it, p, shapely.area(p) + finally: + gq.put(gs) + + for (k, ix), (ht, it, p, a) in zip(work, _run([partial(task, k, ix) for k, ix in work])): + hit[k, ix], inn[k, ix] = ht, it + cov[k, ix[it]] = 1.0 + s = ix[ht & ~it] + cov[k, s] = a / h[s] ** 2 + pcs[k][s] = p + return cov, hit, inn, pcs + + +def _distance(gq, j: int, geoms) -> np.ndarray: + """shapely.distance(geoms, geometry j of the queue), in chunks on the pool.""" + def task(a, b): + gs = gq.get() + try: + return shapely.distance(geoms[a:b], gs[j]) + finally: + gq.put(gs) + return np.concatenate([np.zeros(0)] + _run([partial(task, a, b) for a, b in _spans(len(geoms), 256)])) + + + + +def mom_capacitance(masks: np.ndarray, window_um: float, eps_r: float = 11.45, cells: int = 64) -> np.ndarray: + """masks: (n_cond, R, R) coverage in [0, 1] (channel 0 = ground). Returns the (n_cond, n_cond) Maxwell C in fF.""" + nc, R, _ = masks.shape + f = R // cells + cov = masks.reshape(nc, cells, f, cells, f).mean((2, 4)) # coverage per coarse cell + owner = np.where(cov.max(0) >= 0.5, cov.argmax(0), -1) + ii, jj = np.nonzero(owner >= 0) + lab = owner[ii, jj] + a = window_um / cells + xy = np.stack([(jj + 0.5) * a, (ii + 0.5) * a], 1) + d = np.sqrt(((xy[:, None, :] - xy[None, :, :]) ** 2).sum(-1)) + np.fill_diagonal(d, 1.0) + P = 1.0 / d + np.fill_diagonal(P, SELF / a) + P /= 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2 + B = (lab[:, None] == np.arange(nc)[None, :]).astype(float) # conductor b at 1 V + Q = cho_solve(cho_factor(P, lower=True), B) + C = np.stack([Q[lab == a_].sum(0) for a_ in range(nc)]) # C[a, b] = charge on a with b at 1 V + return 0.5 * (C + C.T) + + +def quadtree_cells(metals: list, bbox, h0: float, h_min: float, grade: float = 2.0, limit: int | None = None): + """Square cells covering the metal polygons: a cell is kept whole when it lies inside one conductor and is no larger + than `grade` x its distance to the nearest metal edge; edge cells are split down to h_min, where they go to the + conductor covering >= 50 % of them. Returns centres (n, 2), sides (n,), owner (n,); None once more than `limit` cells.""" + edges = shapely.union_all([g.boundary for g in metals]) + x0, y0, x1, y1 = bbox + nx, ny = int(np.ceil((x1 - x0) / h0)), int(np.ceil((y1 - y0) / h0)) + gx, gy = np.meshgrid(x0 + h0 * np.arange(nx), y0 + h0 * np.arange(ny)) + xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0) + gq = _Geoms(list(metals) + [edges]) + tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool) + out_c, out_h, out_o, count = [], [], [], 0 + while len(xs): + boxes = shapely.box(xs, ys, xs + h, ys + h) + cov, hit, inn, _ = _cover(gq, boxes, h, tst, inn) + keep = cov.sum(0) > 1e-3 + xs, ys, h, cov, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], hit[:, keep], inn[:, keep] + cx, cy = xs + h / 2, ys + h / 2 + full = cov.max(0) > 0.999 + d = np.full(len(xs), np.inf) # the edge distance only decides for cells inside one conductor + d[full] = _distance(gq, len(metals), shapely.points(cx[full], cy[full])) + leaf = full & (h <= grade * d + 1e-12) + last = h <= h_min * 1.0001 + take_last = last & ~leaf & (cov.max(0) >= 0.5) + acc = leaf | take_last + out_c.append(np.stack([cx[acc], cy[acc]], 1)) + out_h.append(h[acc]) + out_o.append(cov[:, acc].argmax(0)) + count += int(acc.sum()) + if limit is not None and count > limit: + return None + split = ~acc & ~last + hs = h[split] / 2 + xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs]) + ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs]) + h = np.concatenate([hs] * 4) + tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4) + return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o) + + +def anchored_cells(metals: list, h_f: float, grade: float = 2.0, majority: bool = False, limit: int | None = None): + """v3 mesh: quadtree on a lattice anchored to the signal conductors, independent of the chip size and position. + + Finest cells have side h_f; coarse cells are h_f * 2^k on the same lattice (origin = lower-left corner of the + signal conductors' bounding box), so the cells near the conductors do not change when the ground plane grows, + shrinks or the layout is translated. A cell is kept whole when it lies inside one conductor and is no larger than + `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 + polygon boundaries into one panel per metal (no rounding of gaps to the grid). + Returns centres (n, 2), sizes (n, 2) = (width, height) of each panel, owner (n,); None once the mesh has more + than `limit` cells.""" + edges = shapely.union_all([g.boundary for g in metals]) + sig = metals[1:] if len(metals) > 1 else metals + ax, ay = shapely.union_all(sig).bounds[:2] + X0, Y0, X1, Y1 = shapely.union_all(metals).bounds + span = max(X1 - X0, Y1 - Y0) + K = max(0, int(np.ceil(np.log2(max(span / 16, h_f) / h_f)))) + h0 = h_f * 2**K + ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0)) + ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0)) + gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1)) + xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0) + gq = _Geoms(list(metals) + [edges]) + tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool) + out_c, out_h, out_o, count = [], [], [], 0 + while len(xs): + boxes = shapely.box(xs, ys, xs + h, ys + h) + cov, hit, inn, pcs = _cover(gq, boxes, h, tst, inn) + keep = cov.sum(0) > 1e-3 + xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep] + pcs = [p[keep] for p in pcs] + cx, cy = xs + h / 2, ys + h / 2 + full = cov.max(0) > 0.999 + last = h <= h_f * 1.0001 + d = np.full(len(xs), np.inf) # the edge distance only decides for whole cells above the finest level + far = full & ~last + d[far] = _distance(gq, len(metals), boxes[far]) + leaf = full & (last | (h <= grade * d + 1e-12)) + out_c.append(np.stack([cx[leaf], cy[leaf]], 1)) + out_h.append(np.stack([h[leaf], h[leaf]], 1)) + out_o.append(cov[:, leaf].argmax(0)) + count += int(leaf.sum()) + cut = last & ~leaf + if majority and cut.any(): + take = cut & (cov.max(0) >= 0.5) + out_c.append(np.stack([cx[take], cy[take]], 1)) + out_h.append(np.stack([h[take], h[take]], 1)) + out_o.append(cov[:, take].argmax(0)) + count += int(take.sum()) + cut = cut & False + if cut.any(): + gs = gq.get() + for k, g in enumerate(gs[:-1]): + shapely.prepare(g) + sel = np.nonzero(cut & (cov[k] > 1e-3))[0] + if not len(sel): + continue + pcs_k = pcs[k][sel] # box & metal, computed by _cover (straddling cells only reach this level uncut) + bb = shapely.bounds(pcs_k) + cen = shapely.get_coordinates(shapely.centroid(pcs_k)) + inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1]) + if not inside.all(): + rp = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside])) + cen[~inside] = rp + out_c.append(cen) + out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6)) + out_o.append(np.full(len(sel), k)) + count += len(sel) + gq.put(gs) + if limit is not None and count > limit: + return None + split = ~leaf & ~last + hs = h[split] / 2 + xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs]) + ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs]) + h = np.concatenate([hs] * 4) + tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4) + return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o) + + +def mom_mesh(metals: list, max_cells: int = 8000, h_min: float | None = None, grade: float = 2.0, cells_per_gap: float = 3.0, + mesh: str = "v2", offset: tuple = (0.0, 0.0)): + """Mesh for the thin-film MoM. mesh="v3": conductor-anchored lattice with exact edge panels + (`anchored_cells`); mesh="v2" (default, published priors): the original chip-anchored quadtree (reproduces the published v2 priors). + Returns (centres, sides, owner, h_min).""" + bbox = shapely.union_all(metals).bounds + size = max(bbox[2] - bbox[0], bbox[3] - bbox[1]) + if h_min is None: + gap = min(shapely.distance(metals[i], metals[j]) for i in range(len(metals)) for j in range(i + 1, len(metals))) + h_min = max(gap / cells_per_gap, size / 4096) + if mesh == "v2" and any(offset): + h0 = size / 16 + bbox = (bbox[0] - offset[0] * h0, bbox[1] - offset[1] * h0, bbox[2] + h0, bbox[3] + h0) + while True: + if mesh == "v3": + r = anchored_cells(metals, h_min, grade, limit=max_cells) + else: + r = quadtree_cells(metals, bbox, size / 16, h_min, grade, limit=max_cells) + if r is not None: + return (*r, h_min) + h_min *= 1.5 if mesh == "v2" else 2.0 # v3: stay on the same lattice + + +def rect_self(a, b): + """Potential at the centre of a uniformly charged a x b rectangle per unit charge, times 4 pi eps (= SELF / a for a + square).""" + r = np.sqrt(a**2 + b**2) + return (2 * a * np.log((b + r) / a) + 2 * b * np.log((a + r) / b)) / (a * b) + + +def _mom_solve_torch(c, h, o, n_metals, eps_r, dev): + """Same assembly and Cholesky solve as `mom_solve`, in float64 torch on `dev` (opt-in via TLSQ_MOM_DEVICE).""" + import torch + + t = lambda x: torch.as_tensor(np.ascontiguousarray(x), dtype=torch.float64, device=dev) # noqa: E731 + wh = np.stack([h, h], 1) if h.ndim == 1 else h + C, WH = t(c), t(wh) + hm = WH.max(1).values + d = torch.cdist(C, C) + d.fill_diagonal_(1.0) + P = 1.0 / d + near = d < 1.5 * torch.maximum(hm[:, None], hm[None, :]) + near.fill_diagonal_(False) + del d + ii, jj = torch.nonzero(near, as_tuple=True) + del near + if len(ii): + off = (torch.arange(3, dtype=torch.float64, device=dev) - 1) / 3 + ox, oy = torch.meshgrid(off, off, indexing="xy") + sx = C[jj, 0, None] + ox.reshape(-1)[None] * WH[jj, 0, None] + sy = C[jj, 1, None] + oy.reshape(-1)[None] * WH[jj, 1, None] + r = torch.sqrt((C[ii, 0, None] - sx) ** 2 + (C[ii, 1, None] - sy) ** 2) + P[ii, jj] = (1.0 / torch.clamp(r, min=1e-9)).mean(1) + P = 0.5 * (P + P.T) + diag = t(rect_self(wh[:, 0], wh[:, 1]) if h.ndim == 2 else SELF / h) + P.diagonal().copy_(diag) + P /= 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2 + B = t((o[:, None] == np.arange(n_metals)[None, :]).astype(float)) + L, info = torch.linalg.cholesky_ex(P) + del P + if int(info): + raise np.linalg.LinAlgError(f"{int(info)}-th leading minor not positive definite") + return torch.cholesky_solve(B, L).cpu().numpy() + + +def _mom_solve_gpu_slot(c, h, o, n_metals, eps_r): + """GPU solve if one of TLSQ_MOM_GPU_SLOTS slots (file locks shared by the worker processes) is free and the solve + fits; None means: solve on the CPU instead.""" + import fcntl + import os + import tempfile + + import torch + + slots = int(os.environ.get("TLSQ_MOM_GPU_SLOTS", "3")) + tag = os.environ.get("SLURM_JOB_ID", "local") + for k in range(slots): + f = open(os.path.join(tempfile.gettempdir(), f"tlsq_gpu_{tag}_{k}.lock"), "w") + try: + fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + f.close() + continue + try: + return _mom_solve_torch(c, h, o, n_metals, eps_r, os.environ["TLSQ_MOM_DEVICE"]) + except RuntimeError as e: # includes torch.OutOfMemoryError + if "out of memory" not in str(e).lower(): + raise + return None + finally: + torch.cuda.empty_cache() + fcntl.flock(f, fcntl.LOCK_UN) + f.close() + return None + + +def _G(x, y): + """Antiderivative of 1 / sqrt(x^2 + y^2) over x and y: x asinh(y / |x|) + y asinh(x / |y|) (0 on the axes).""" + ax, ay = np.abs(x), np.abs(y) + with np.errstate(divide="ignore", invalid="ignore"): + g1 = np.where(ax > 0, x * np.arcsinh(y / np.where(ax > 0, ax, 1.0)), 0.0) + g2 = np.where(ay > 0, y * np.arcsinh(x / np.where(ay > 0, ay, 1.0)), 0.0) + return g1 + g2 + + +def rect_potential(p, cq, wh): + """Exact potential (x 4 pi eps) at points p (n, 2) of uniformly charged rectangles (unit total charge) centred at cq + with sizes wh (n, 2): (1 / (w h)) * integral of 1 / r over the rectangle. Equals rect_self at the centre.""" + x1, x2 = cq[:, 0] - wh[:, 0] / 2 - p[:, 0], cq[:, 0] + wh[:, 0] / 2 - p[:, 0] + y1, y2 = cq[:, 1] - wh[:, 1] / 2 - p[:, 1], cq[:, 1] + wh[:, 1] / 2 - p[:, 1] + return (_G(x2, y2) - _G(x1, y2) - _G(x2, y1) + _G(x1, y1)) / (wh[:, 0] * wh[:, 1]) + + +def _mom_solve_exact_near(c, wh, o, n_metals, eps_r): + """v3 panels: exact rectangle potential for near pairs (d < 1.5 max side) instead of the 3x3 point average. Used only + when the 3x3 system is not positive definite (sliver edge panels on long fine combs), so priors that solved before + are unchanged.""" + P = _assemble(c, wh, rect_self(wh[:, 0], wh[:, 1]), eps_r, rect_potential) + B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float) + return _chol_solve(P, B) + + +def mom_solve(c, h, o, n_metals: int, eps_r: float = 11.45): + """mom_solve_quad; for v3 panels (h of shape (n, 2)) a non-positive-definite system is re-solved with the exact + near field (`_mom_solve_exact_near`).""" + try: + return mom_solve_quad(c, h, o, n_metals, eps_r) + except np.linalg.LinAlgError: + if h.ndim != 2: + raise + print("v3: 3x3 near-field system not positive definite, exact near field", flush=True) + return _mom_solve_exact_near(c, h, o, n_metals, eps_r) + + +def mom_solve_quad(c, h, o, n_metals: int, eps_r: float = 11.45): + """Point-matching solve on a mesh from `mom_mesh`: cell charges Q (n_cells, n_metals) for each conductor at 1 V. + h: square sides (n,) or panel (width, height) (n, 2). Set TLSQ_MOM_DEVICE=cuda to assemble and solve on a GPU.""" + import os + + if os.environ.get("TLSQ_MOM_DEVICE") and SUBSTRATE["model"] == "halfspace": # the torch path has no image correction + Q = _mom_solve_gpu_slot(c, h, o, n_metals, eps_r) + if Q is not None: + return Q + wh = np.stack([h, h], 1) if h.ndim == 1 else h + P = _assemble(c, wh, rect_self(wh[:, 0], wh[:, 1]) if h.ndim == 2 else SELF / h, eps_r, _near_quad) + B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float) + return _chol_solve(P, B) + + +def _near_quad(p, cq, wh): + """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 + panel (point matching is poor for close, unequal cells).""" + off = (np.arange(3) - 1) / 3 + ox, oy = np.meshgrid(off, off) + sx = cq[:, 0, None] + ox.ravel()[None] * wh[:, 0, None] + sy = cq[:, 1, None] + oy.ravel()[None] * wh[:, 1, None] + r = np.sqrt((p[:, 0, None] - sx) ** 2 + (p[:, 1, None] - sy) ** 2) + return (1.0 / np.maximum(r, 1e-9)).mean(1) + + +def _near_pairs(c, hm): + """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 + floating-point test as the dense mask.""" + n = len(c) + tree, lev, key = cKDTree(c), np.floor(np.log2(hm)), [np.zeros(0, np.int64)] + for L in np.unique(lev): + sel = np.nonzero(lev == L)[0] + pr = cKDTree(c[sel]).sparse_distance_matrix(tree, 1.5 * hm[sel].max() * (1 + 1e-9), output_type="ndarray") + a, b = sel[pr["i"]], pr["j"] + key.append(np.minimum(a, b) * n + np.maximum(a, b)) + key = np.unique(np.concatenate(key)) + ii, jj = key // n, key % n + 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])) + return ii[ok], jj[ok] + + +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" +_IMG_CACHE: dict = {} +_IMG_GRID: dict = {} + + +class substrate: + """Context manager: the MoM Green's function inside the block (`SUBSTRATE`): model "slab_open" (default since + 2026-10-10, PRIOR_SCHEME.md 14.6), "halfspace" (the kernel of the priors the released models were trained on) or + "slab_pec"; t_um the substrate thickness (None: unchanged).""" + + def __init__(self, model: str | None, t_um: float | None = None): + self.new = {k: v for k, v in (("model", model), ("t_um", t_um)) if v is not None} + + def __enter__(self): + self.old = dict(SUBSTRATE) + SUBSTRATE.update(self.new) + return self + + def __exit__(self, *exc): + SUBSTRATE.clear() + SUBSTRATE.update(self.old) + + +def _image_correction(d, eps_r: float, exact: bool = False): + """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 + 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, + 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), + "slab_pec" = metal backside (w_n = -(1 + K) (-K)^(n-1); weights sum to -1). Images up to K^2n < 1e-4, the remaining + weight as one more image; tabulated on a uniform grid of t / 1000 up to 100 mm and interpolated linearly (dense + assembly), or summed exactly (exact=True: the H-matrix kernel, whose ACA blocks need a smooth kernel). None for the + half-space (the published kernel).""" + m = SUBSTRATE["model"] + if m == "halfspace": + return None + key = (m, float(SUBSTRATE["t_um"]), float(eps_r)) + if key not in _IMG_CACHE: + K, t = (eps_r - 1) / (eps_r + 1), float(SUBSTRATE["t_um"]) + n = np.arange(1, 400) + nmax = int(np.argmax(K ** (2 * n) < 1e-4)) + 1 + n = n[:nmax] + if m == "slab_open": + w, total = K ** (2 * n) + K ** (2 * n - 1), K / (1 - K) + else: + w, total = -(1 + K) * (-K) ** (n - 1), -1.0 + w, D = np.r_[w, total - w.sum()], np.r_[2 * n * t, 2 * (nmax + 1) * t] + du = t / 1000 + u = np.arange(0.0, 1e5 + 2 * du, du) + T = (w[None, :] / np.sqrt(u[:, None] ** 2 + D[None, :] ** 2)).sum(1) + _IMG_CACHE[key] = (du, T, w, D) + du, T = _IMG_CACHE[key][:2] + if exact: + w, D = _IMG_CACHE[key][2:] + d2 = np.asarray(d, float) ** 2 + return sum(wn / np.sqrt(d2 + Dn * Dn) for wn, Dn in zip(w, D)) + if key not in _IMG_GRID: + _IMG_GRID[key] = du * np.arange(len(T)) + return np.interp(d, _IMG_GRID[key], T) + + +def _assemble(c, wh, diag, eps_r: float, near): + """Interaction matrix / (4 pi eps0 eps_eff) for the lower Cholesky factorisation: far pairs 1 / d, near pairs (d < 1.5 + max side) the symmetrised near(p, cq, wh) potential, diagonal `diag`; the same numbers as the dense construction. + Built in row blocks on the pool threads without n x n temporaries, and only the half the factorisation reads: returns + a Fortran-ordered matrix whose lower triangle holds the entries (its upper triangle is not initialised).""" + n = len(c) + k = 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2 + A = np.empty((n, n)) + x, y = c[:, 0], c[:, 1] + + def rows(r0, r1): + d = x[r0:r1, None] - x[None, r0:] + np.multiply(d, d, out=d) + t = y[r0:r1, None] - y[None, r0:] + np.multiply(t, t, out=t) + d += t + np.sqrt(d, out=d) + corr = _image_correction(d, eps_r) + d[np.arange(r1 - r0), np.arange(r1 - r0)] = 1.0 + out = A[r0:r1, r0:] + np.divide(1.0, d, out=out) + if corr is not None: + out += corr + out /= k + + _run([partial(rows, a, b) for a, b in _spans(n, 64)]) + ii, jj = _near_pairs(c, wh.max(1)) + img = _image_correction(np.zeros(1), eps_r) + if len(ii): + A[ii, jj] = 0.5 * (near(c[ii], c[jj], wh[jj]) + near(c[jj], c[ii], wh[ii])) / k + if img is not None: + A[ii, jj] += _image_correction(np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)), eps_r) / k + A[np.arange(n), np.arange(n)] = diag / k if img is None else (diag + img[0]) / k + return A.T + + +def _chol_solve(P, B): + """Cholesky solve with the lower triangle of the Fortran-ordered P from `_assemble`, factorised in place (no copy).""" + return cho_solve(cho_factor(P, lower=True, overwrite_a=True, check_finite=False), B, check_finite=False) + + +def mom_capacitance_gds(metals: list, eps_r: float = 11.45, max_cells: int = 8000, h_min: float | None = None, grade: float = 2.0, + cells_per_gap: float = 3.0, offset: tuple = (0.0, 0.0), mesh: str = "v2"): + """Thin-film MoM on the exact GDS polygons (ground to the chip edge), adaptive quadtree mesh (see `mom_mesh`). + + metals: shapely geometries, one per conductor (index 0 = ground). `offset` (v2 mesh only) shifts the quadtree origin + by that fraction of the coarsest cell. Returns (C fF in the same order, n_cells, h_min).""" + c, h, o, h_min = mom_mesh(metals, max_cells, h_min, grade, cells_per_gap, mesh, offset) + try: + Q = mom_solve(c, h, o, len(metals), eps_r) # v3: exact near field if the 3x3 system is not positive definite + except np.linalg.LinAlgError: + if mesh != "v3": + raise + # cell cap coarser than the finger pitch: cut-panel boxes of different conductors overlap; finest cells as whole + # squares owned by the metal covering >= 50 % (as the v2 mesh), exact near field + print("v3: still not positive definite, majority finest cells", flush=True) + c, h, o = anchored_cells(metals, h_min, grade, majority=True) + Q = _mom_solve_exact_near(c, h, o, len(metals), eps_r) + C = np.stack([Q[o == a].sum(0) for a in range(len(metals))]) + return 0.5 * (C + C.T), len(c), h_min + + +def mom_capacitance_gds_avg(metals: list, n_offsets: int = 4, **kw): + """MoM averaged over quadtree origins (offsets on a diagonal grid of the coarsest cell): removes the part of the + discretisation error that jumps with mesh alignment as the geometry is swept.""" + offs = [(i / n_offsets, ((i * 3) % n_offsets) / n_offsets) for i in range(n_offsets)] + runs = [mom_capacitance_gds(metals, offset=o, mesh="v2", **kw) for o in offs] + return np.mean([r[0] for r in runs], 0), int(np.mean([r[1] for r in runs])), runs[0][2] + + +def _both(f, g): + """(f(), g()), a LinAlgError in place of a result; concurrently (g on a helper thread) when more than one thread is + allowed: the gap/6 and gap/3 solves of v3-rich are independent.""" + def call(fn): + try: + return fn() + except np.linalg.LinAlgError as e: + return e + if _nthreads() < 2: + return call(f), call(g) + with ThreadPoolExecutor(1, thread_name_prefix="tlsq-rich") as ex: + fut = ex.submit(call, g) + a = call(f) + return a, fut.result() + + +def mom_capacitance_gds_rich(metals: list, eps_r: float = 11.45, cells_per_gap: float = 3.0, max_cells: int = 8000, + max_cells_fine: int = 16000, grade: float = 2.0): + """v3 MoM with Richardson extrapolation: solves at gap/cells_per_gap and at half that cell size on the same + conductor-anchored lattice and extrapolates the first-order discretisation error to zero cell size. If the cell + cap forces both meshes to the same resolution, returns the finer solve. If the coarse system is not positive + definite (sliver edge panels badly resolved by the 3x3 near-field rule; ~0.2 % of designs), returns the other + solve alone (fine if the coarse one fails, coarse if the fine one fails). The two solves run concurrently when more + than one thread is allowed. Returns (C, total cells, h of the fine mesh).""" + rf, rc = _both(partial(mom_capacitance_gds, metals, eps_r, max_cells_fine, None, grade, 2 * cells_per_gap, mesh="v3"), + partial(mom_capacitance_gds, metals, eps_r, max_cells, None, grade, cells_per_gap, mesh="v3")) + if isinstance(rf, np.linalg.LinAlgError): + print("v3-rich: fine mesh not positive definite, coarse solve only", flush=True) + if isinstance(rc, np.linalg.LinAlgError): + raise rc + return rc + Cf, nf, hf = rf + if isinstance(rc, np.linalg.LinAlgError): + print("v3-rich: coarse mesh not positive definite, fine solve only", flush=True) + return Cf, nf, hf + Cc, nc, hc = rc + r = hc / hf + C = Cf if r < 1.01 else Cf + (Cf - Cc) / (r - 1) + return C, nc + nf, hf + + +# ---------------------------------------------------------------- flagship prior: strip-merged mesh, H-matrix solve, Richardson +# Resolution-controlled (panels per gap, no small cap), the same conductor-anchored panels as v3 merged along straight +# edges, the exact rectangle potential for near pairs, and for large systems an H-matrix (ACA far field) with +# preconditioned CG instead of the dense Cholesky: O(n log n) time and memory. + +DENSE_MAX = int(os.environ.get("TLSQ_MOM_DENSE_MAX", 12000)) # panels up to which the dense Cholesky is used (faster below this size) +HMAT = dict(leaf=128, eta=2.0, eps=1e-9, tol=1e-12, maxit=1000) # ACA block tolerance (relative Frobenius) and PCG residual + + +def _segments(metals): + segs, verts = [], [] + for g in metals: + for p in getattr(g, "geoms", [g]): + for ring in [p.exterior, *p.interiors]: + xy = np.asarray(ring.coords) + verts.append(xy[:-1]) + segs.append(np.stack([xy[:-1], xy[1:]], 1)) + S = np.concatenate(segs) + return shapely.linestrings(S), S, np.concatenate(verts) + + +def strip_merge(c, wh, o, metals, h_f, lmax_mult: float = 16.0, grade: float = 1.0): + """Merge runs of equal-width panels along the direction of their nearest polygon edge (where the charge density is + translation-invariant) into panels of length <= lmax_mult * h_f and <= grade * distance to the nearest vertex; + the size across the edge is kept. Returns merged (centres, sizes, owners).""" + lines, S, verts = _segments(metals) + near = shapely.STRtree(lines).nearest(shapely.points(c)) + d = S[near, 1] - S[near, 0] + horiz = np.abs(d[:, 0]) >= np.abs(d[:, 1]) + dv, _ = cKDTree(verts).query(c, workers=-1) + allow = np.minimum(lmax_mult * h_f, grade * dv) + out_c, out_wh, out_o = [], [], [] + for axis, mask in ((0, horiz), (1, ~horiz)): + if not mask.any(): + continue + idx = np.nonzero(mask)[0] + other = 1 - axis + key = np.stack([o[idx], np.round(c[idx, other], 4), np.round(wh[idx, other], 4)], 1) + order = np.lexsort([c[idx, axis], key[:, 2], key[:, 1], key[:, 0]]) + idx, key = idx[order], key[order] + same = np.all(key[1:] == key[:-1], 1) + lo, hi = c[idx, axis] - wh[idx, axis] / 2, c[idx, axis] + wh[idx, axis] / 2 + adjacent = same & (np.abs(hi[:-1] - lo[1:]) < 1e-6) + start = 0 + while start < len(idx): + end, length, cap = start, wh[idx[start], axis], allow[idx[start]] + while end + 1 < len(idx) and adjacent[end] and length + wh[idx[end + 1], axis] <= min(cap, allow[idx[end + 1]]): + end += 1 + length += wh[idx[end], axis] + cap = min(cap, allow[idx[end]]) + cc, ww = np.zeros(2), np.zeros(2) + cc[axis], cc[other] = 0.5 * (lo[start] + hi[end]), c[idx[start], other] + ww[axis], ww[other] = hi[end] - lo[start], wh[idx[start], other] + out_c.append(cc) + out_wh.append(ww) + out_o.append(o[idx[start]]) + start = end + 1 + return np.array(out_c), np.array(out_wh), np.array(out_o) + + +def mesh_flagship(metals: list, h: float, max_cells: int, grade: float = 2.0, lmax_mult: float = 16.0, merge_grade: float = 1.0, + majority: bool = False): + """Strip-merged v3 mesh at finest panel h, doubled (same lattice) until the merged mesh has at most max_cells panels. + Returns (centres, sizes, owners, h, raw panel count).""" + while True: + raw = anchored_cells(metals, h, grade, majority=majority, limit=8 * max_cells) + if raw is not None: + c, wh, o = strip_merge(*raw, metals, h, lmax_mult, merge_grade) + if len(c) <= max_cells: + return c, wh, o, h, len(raw[0]) + h *= 2.0 + + +class _Kernel: + """Entries of P / (4 pi eps0 eps_eff) in a permuted panel order: 1 / d for far pairs, the exact rectangle potential + for near pairs (d < 1.5 max side) and rect_self on the diagonal; near and diagonal corrections to 1 / d are held in + a CSR matrix so that any block is 1 / d + N[I, J] (the same numbers as `_assemble` with `rect_potential`).""" + + def __init__(self, c, wh, eps_r, perm): + from scipy.sparse import csr_matrix + + self.x, self.y = c[perm, 0].copy(), c[perm, 1].copy() + self.k = 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2 + self.eps_r = eps_r + self.img = _image_correction(np.zeros(1), eps_r) is not None # finite-substrate image term (exact series in blocks / rows) + n = len(c) + ii, jj = _near_pairs(c, wh.max(1)) + d = np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)) + near = 0.5 * (rect_potential(c[ii], c[jj], wh[jj]) + rect_potential(c[jj], c[ii], wh[ii])) - 1.0 / d + inv = np.empty(n, np.int64) + inv[perm] = np.arange(n) + rows = inv[np.concatenate([ii, jj, np.arange(n)])] + cols = inv[np.concatenate([jj, ii, np.arange(n)])] + vals = np.concatenate([near, near, rect_self(wh[:, 0], wh[:, 1]) - 1.0]) + self.N = csr_matrix((vals, (rows, cols)), shape=(n, n)) + + def block(self, a0, a1, b0, b1): + 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) + corr = _image_correction(d, self.eps_r, exact=True) + if a0 == b0: + d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0 + A = 1.0 / d + if corr is not None: + A += corr + Nb = self.N[a0:a1, b0:b1] + if Nb.nnz: + A += Nb.toarray() + return A / self.k + + def row(self, i, b0, b1): + d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2) + r = 1.0 / d + if self.img: + r += _image_correction(d, self.eps_r, exact=True) + Nr = self.N[i, b0:b1] + if Nr.nnz: + r += Nr.toarray()[0] + return r / self.k + + +def _cluster_tree(c, leaf: int): + """Recursive bisection of the panel centres along the longer side of their bounding box at the median: a permutation + making every cluster a contiguous range, and nodes (a, b, lo, hi, left, right).""" + perm, nodes = np.arange(len(c)), [] + + def rec(a, b): + idx = perm[a:b] + lo, hi = c[idx].min(0), c[idx].max(0) + k = len(nodes) + nodes.append([a, b, lo, hi, -1, -1]) + if b - a > leaf: + ax = int(np.argmax(hi - lo)) + perm[a:b] = idx[np.argsort(c[idx, ax], kind="stable")] + m = a + (b - a) // 2 + nodes[k][4], nodes[k][5] = rec(a, m), rec(m, b) + return k + + rec(0, len(c)) + return perm, nodes + + +def _aca(K, a0, a1, b0, b1, eps, max_rank): + """Partial-pivoting adaptive cross approximation of the block (a0:a1, b0:b1) to relative Frobenius accuracy eps: + (U, V) with block ~ U V^T, or None when the rank would exceed max_rank.""" + m, n = a1 - a0, b1 - b0 + U, V, used_r, used_c = [], [], np.zeros(m, bool), np.zeros(n, bool) + i, norm2 = 0, 0.0 + for _ in range(min(m, n, max_rank)): + r = K.row(a0 + i, b0, b1) + for u, v in zip(U, V): + r -= u[i] * v + rm = np.where(used_c, 0.0, np.abs(r)) + j = int(np.argmax(rm)) + if rm[j] == 0.0: + break + v = r / r[j] + u = K.row(b0 + j, a0, a1) # column j of the block = row j of the symmetric kernel + for uu, vv in zip(U, V): + u -= vv[j] * uu + used_r[i], used_c[j] = True, True + nu, nv = float(u @ u), float(v @ v) + norm2 += nu * nv + 2 * sum(float(u @ uu) * float(v @ vv) for uu, vv in zip(U, V)) + U.append(u) + V.append(v) + if nu * nv <= eps**2 * norm2: + break + um = np.where(used_r, 0.0, np.abs(u)) + i = int(np.argmax(um)) + if um[i] == 0.0: + break + else: + return None + return np.stack(U, 1), np.stack(V, 1) + + +class _HMatrix: + """Hierarchical matrix of the panel system: dense blocks for leaf pairs that are not well separated, ACA low-rank + blocks for admissible pairs (min diameter <= eta x distance); only the upper block triangle is stored (symmetric).""" + + def __init__(self, c, wh, eps_r, leaf=128, eta=2.0, eps=1e-9): + self.n = len(c) + self.perm, nodes = _cluster_tree(c, leaf) + self.K = _Kernel(c, wh, eps_r, self.perm) + self.eps = eps + plan = [] + + def leafq(k): + return nodes[k][4] < 0 + + def rec(s, t): + a0, a1, lo_s, hi_s, sl, sr = nodes[s] + b0, b1, lo_t, hi_t, tl, tr = nodes[t] + if s == t: + if leafq(s): + plan.append(("d", a0, a1, a0, a1)) + else: + rec(sl, sl), rec(sr, sr), rec(sl, sr) + return + gap = np.maximum(0.0, np.maximum(lo_s - hi_t, lo_t - hi_s)) + diam = min(np.sqrt(((hi_s - lo_s) ** 2).sum()), np.sqrt(((hi_t - lo_t) ** 2).sum())) + if diam <= eta * np.sqrt((gap**2).sum()): + plan.append(("l", a0, a1, b0, b1)) + elif leafq(s) and leafq(t): + plan.append(("d", a0, a1, b0, b1)) + elif leafq(s): + rec(s, tl), rec(s, tr) + elif leafq(t): + rec(sl, t), rec(sr, t) + else: + for u in (sl, sr): + for v in (tl, tr): + rec(u, v) + + rec(0, 0) + self.dense, self.low = [], [] + + def build(chunk): + out = [] + for kind, a0, a1, b0, b1 in chunk: + if kind == "l": + r = _aca(self.K, a0, a1, b0, b1, eps, max(2, min(a1 - a0, b1 - b0) // 2)) + if r is not None: + out.append(("l", a0, a1, b0, b1, r)) + continue + out.append(("d", a0, a1, b0, b1, self.K.block(a0, a1, b0, b1))) + return out + + for res in _run([partial(build, plan[a:b]) for a, b in _spans(len(plan), 64)]): + for kind, a0, a1, b0, b1, M in res: + (self.low if kind == "l" else self.dense).append((a0, a1, b0, b1, M)) + self.pre = [(a0, a1, cho_factor(D, lower=True)) for a0, a1, b0, _, D in self.dense if a0 == b0] # block Jacobi + self.n_threads = max(1, _nthreads()) + + + def nbytes(self): + return sum(D.nbytes for *_, D in self.dense) + sum(U.nbytes + V.nbytes for *_, (U, V) in self.low) + + def matvec(self, X): + def part(dense, low): + Y = np.zeros_like(X) + for a0, a1, b0, b1, D in dense: + Y[a0:a1] += D @ X[b0:b1] + if a0 != b0: + Y[b0:b1] += D.T @ X[a0:a1] + for a0, a1, b0, b1, (U, V) in low: + Y[a0:a1] += U @ (V.T @ X[b0:b1]) + Y[b0:b1] += V @ (U.T @ X[a0:a1]) + return Y + + T = self.n_threads + if T < 2: + return part(self.dense, self.low) + return sum(_run([partial(part, self.dense[k::T], self.low[k::T]) for k in range(T)])) + + def precondition(self, R): + Z = np.empty_like(R) + for a0, a1, cf in self.pre: + Z[a0:a1] = cho_solve(cf, R[a0:a1], check_finite=False) + return Z + + def pcg(self, B, tol, maxit): + """Conjugate gradients on every right-hand side column at once (independent columns), block-Jacobi preconditioned, + until every column's relative residual is below tol. B and the result are in the permuted order.""" + X, R = np.zeros_like(B), B.copy() + Z = self.precondition(R) + P, rz = Z.copy(), (R * Z).sum(0) + bn = np.sqrt((B * B).sum(0)) + for it in range(maxit): + AP = self.matvec(P) + alpha = rz / (P * AP).sum(0) + X += alpha * P + R -= alpha * AP + res = (np.sqrt((R * R).sum(0)) / bn).max() + if res < tol: + return X, it + 1, res + Z = self.precondition(R) + rz_new = (R * Z).sum(0) + P = Z + (rz_new / rz) * P + rz = rz_new + raise np.linalg.LinAlgError(f"H-matrix PCG did not converge in {maxit} iterations (residual {res:.1e})") + + +def mom_solve_fast(c, wh, o, n_metals: int, eps_r: float = 11.45, dense_max: int | None = None): + """Panel charges Q (n, n_metals) for unit excitation of every conductor, exact near field: dense Cholesky for + n <= dense_max panels (`_mom_solve_exact_near`), otherwise the H-matrix / PCG solve of the same system.""" + n = len(c) + if n <= (DENSE_MAX if dense_max is None else dense_max): + return _mom_solve_exact_near(c, wh, o, n_metals, eps_r) + H = _HMatrix(c, wh, eps_r, HMAT["leaf"], HMAT["eta"], HMAT["eps"]) + B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float)[H.perm] + Xp, _, _ = H.pcg(B, HMAT["tol"], HMAT["maxit"]) + Q = np.empty_like(Xp) + Q[H.perm] = Xp + return Q + + +def _cap_flagship(metals, eps_r, h, max_cells, grade, lmax_mult, merge_grade): + c, wh, o, h, n_raw = mesh_flagship(metals, h, max_cells, grade, lmax_mult, merge_grade) + try: + Q = mom_solve_fast(c, wh, o, len(metals), eps_r) + except np.linalg.LinAlgError: # cut panels of different conductors overlap (lattice coarser than the finger pitch) + print("flagship: system not positive definite, majority finest cells", flush=True) + c, wh, o, h, n_raw = mesh_flagship(metals, h, max_cells, grade, lmax_mult, merge_grade, majority=True) + Q = mom_solve_fast(c, wh, o, len(metals), eps_r) + C = np.stack([Q[o == a].sum(0) for a in range(len(metals))]) + return 0.5 * (C + C.T), len(c), h, n_raw + + +def mom_capacitance_gds_flagship(metals: list, eps_r: float = 11.45, cells_per_gap: float = 3.0, max_cells: int = 250000, + grade: float = 2.0, rich: bool = True, lmax_mult: float = 16.0, merge_grade: float = 1.0): + """Flagship prior: strip-merged conductor-anchored mesh at h_f = max(gap / (2 cells_per_gap), size / 8192) and, with + rich=True, Richardson extrapolation with the mesh at 2 h_f (same lattice), both solved with `mom_solve_fast`. The + resolution is set by cells_per_gap (the fine mesh has 2 cells_per_gap panels across the smallest gap; the coarse + one cells_per_gap); max_cells is only a safety cap on the merged panel count (h doubles until it fits). The two solves + run concurrently when more than one thread is allowed. Returns (C, total panels, h of the fine mesh, info dict).""" + bbox = shapely.union_all(metals).bounds + size = max(bbox[2] - bbox[0], bbox[3] - bbox[1]) + gap = min(shapely.distance(metals[i], metals[j]) for i in range(len(metals)) for j in range(i + 1, len(metals))) + hf = max(gap / (2 * cells_per_gap), size / 8192) + if not rich: + C, n, h, n_raw = _cap_flagship(metals, eps_r, 2 * hf, max_cells, grade, lmax_mult, merge_grade) + 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} + rf, rc = _both(partial(_cap_flagship, metals, eps_r, hf, max_cells, grade, lmax_mult, merge_grade), + partial(_cap_flagship, metals, eps_r, 2 * hf, max_cells, grade, lmax_mult, merge_grade)) + for r in (rf, rc): + if isinstance(r, np.linalg.LinAlgError): + raise r + (Cf, nf, h_f, raw_f), (Cc, nc, h_c, raw_c) = rf, rc + r = h_c / h_f + C = Cf if r < 1.01 else Cf + (Cf - Cc) / (r - 1) + info = {"h_target_um": hf, "min_gap_um": gap, "cap_hit": h_f > hf * 1.0001, "raw_cells": raw_f, "coarse_cells": nc, + "extrapolated": bool(r >= 1.01)} + return C, nf + nc, h_f, info diff --git a/geomgnn/_tlsq/geometry.py b/geomgnn/_tlsq/geometry.py new file mode 100644 index 0000000000000000000000000000000000000000..357d691247147e8087420fecf6bc55ab9463d367 --- /dev/null +++ b/geomgnn/_tlsq/geometry.py @@ -0,0 +1,256 @@ +"""GDS conductor geometry: exact polygon metrics, separation spectra and spectral shape descriptors. + +Only metal layers are used: (1,0) ground and (1,10) signal conductors. Port stubs (2,0)/(3,0) are ignored. +""" + +from __future__ import annotations + +from dataclasses import dataclass, field + +import gdstk +import numpy as np +import shapely +from scipy import sparse +from scipy.sparse.linalg import eigsh +from scipy.spatial import cKDTree +from shapely.geometry import MultiPolygon, Polygon + +GROUND_LAYER = (1, 0) +SIGNAL_LAYER = (1, 10) +BOUNDARY_STEP_UM = 0.25 +SEP_BINS_UM = np.geomspace(0.5, 2000.0, 41) +N_FOURIER = 16 +MIC_TOL = [0.01] # inscribed-circle tolerance (um); 0.1 for the approximate token variants (PRIOR_SCHEME.md 14.3.1) +RASTER_PX = 512 +N_EIG = 16 +K_BINS = 48 + + +@dataclass +class Conductors: + N: Polygon + S: Polygon + G: Polygon | MultiPolygon + pocket: Polygon | MultiPolygon # etched region inside the chip (contains N and S) + gap: Polygon | MultiPolygon # pocket minus signal metal (dielectric surface) + chip: Polygon + pads: list = field(default_factory=list) # extra 0 V bodies (merged into G) + + def metals(self) -> dict: + return {"N": self.N, "S": self.S, "G": self.G} + + +def _valid(geom): + g = shapely.make_valid(geom) + polys = [p for p in getattr(g, "geoms", [g]) if isinstance(p, (Polygon, MultiPolygon))] + return shapely.union_all(polys) + + +def load_conductors(gds_path) -> Conductors: + lib = gdstk.read_gds(str(gds_path)) + cell = lib.top_level()[0] + to_um = lib.unit / 1e-6 + sig = [p for p in cell.polygons if (p.layer, p.datatype) == SIGNAL_LAYER] + gnd = [p for p in cell.polygons if (p.layer, p.datatype) == GROUND_LAYER] + if len(sig) < 2 or len(gnd) != 1: + raise ValueError(f"expected >=2 signal + 1 ground polygons, got {len(sig)} + {len(gnd)}") + polys = [_valid(Polygon(p.points * to_um)) for p in sig] + if len(polys) == 2: + N, S = polys + pads = [] + else: + # multi-terminal (exp12): GDS order is not the net order. S = star at the origin; N = body fed by the + # single port stub; the remaining pads are held at 0 V in the solve and are merged into ground. + 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]]) + 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]))) + n = min((i for i in range(len(polys)) if i != s), key=lambda i: polys[i].distance(port)) + N, S = polys[n], polys[s] + pads = [g for i, g in enumerate(polys) if i not in (n, s)] + G0 = _valid(Polygon(gnd[0].points * to_um)) + if abs(G0.area - gnd[0].area() * to_um**2) > 1e-6 * G0.area: + raise ValueError("ground polygon area changed during repair") + chip = shapely.box(*G0.bounds) + pocket = chip.difference(G0) + G = shapely.union_all([G0, *pads]) if pads else G0 + gap = pocket.difference(N).difference(S) + for g in pads: + gap = gap.difference(g) + return Conductors(N, S, G, pocket, gap, chip, pads) + + +def sample_boundary(geom, step: float = BOUNDARY_STEP_UM) -> np.ndarray: + rings = [] + for poly in getattr(geom, "geoms", [geom]): + rings += [poly.exterior, *poly.interiors] + pts = [] + for r in rings: + n = max(int(np.ceil(r.length / step)), 4) + pts.append(shapely.get_coordinates(shapely.line_interpolate_point(r, np.linspace(0, r.length, n, endpoint=False)))) + return np.concatenate(pts) + + +def boundaries(c: Conductors) -> dict[str, np.ndarray]: + # ground edge = boundary of the etched pocket + g = [sample_boundary(c.pocket)] + [sample_boundary(p) for p in c.pads] + return {"N": sample_boundary(c.N), "S": sample_boundary(c.S), "G": np.concatenate(g)} + + +def shape_metrics(poly) -> dict: + hull = poly.convex_hull + x0, y0, x1, y1 = poly.bounds + cx, cy = poly.centroid.x, poly.centroid.y + ixx, iyy, ixy = _second_moments(poly, cx, cy) + evals = np.linalg.eigvalsh([[ixx, ixy], [ixy, iyy]]) + return { + "area": poly.area, + "perimeter": poly.length, + "convex_area": hull.area, + "solidity": poly.area / hull.area, + "compactness": 4 * np.pi * poly.area / poly.length**2, + "bbox_w": x1 - x0, + "bbox_h": y1 - y0, + "cx": cx, + "cy": cy, + "I_major": evals[1], + "I_minor": evals[0], + "elongation": np.sqrt(evals[1] / max(evals[0], 1e-30)), + "inscribed_radius": shapely.maximum_inscribed_circle(poly, MIC_TOL[0]).length if isinstance(poly, Polygon) else np.nan, + "n_vertices": int(shapely.get_num_coordinates(poly)), + "n_holes": int(sum(len(p.interiors) for p in getattr(poly, "geoms", [poly]))), + } + + +def _second_moments(geom, cx, cy) -> tuple[float, float, float]: + ixx = iyy = ixy = 0.0 + for poly in getattr(geom, "geoms", [geom]): + for k, ring in enumerate([poly.exterior, *poly.interiors]): + xy = np.asarray(ring.coords) - (cx, cy) + x, y = xy[:-1, 0], xy[:-1, 1] + xn, yn = xy[1:, 0], xy[1:, 1] + a = x * yn - xn * y + sgn = 1.0 if ring.is_ccw == (k == 0) else -1.0 + ixx += sgn * np.sum(a * (y**2 + y * yn + yn**2)) / 12 + iyy += sgn * np.sum(a * (x**2 + x * xn + xn**2)) / 12 + ixy += sgn * np.sum(a * (x * yn + 2 * x * y + 2 * xn * yn + xn * y)) / 24 + return abs(ixx), abs(iyy), ixy + + +def fourier_descriptors(poly, n_samples: int = 512) -> np.ndarray: + """|c_k|/|c_1| for k=-N..N of the outer contour (translation/rotation/scale invariant).""" + ring = max(getattr(poly, "geoms", [poly]), key=lambda p: p.area).exterior + if not ring.is_ccw: + ring = shapely.reverse(ring) + pts = shapely.get_coordinates(shapely.line_interpolate_point(ring, np.linspace(0, 1, n_samples, endpoint=False), normalized=True)) + c = np.fft.fft(pts[:, 0] + 1j * pts[:, 1]) / n_samples + k = np.r_[np.arange(-N_FOURIER, 0), np.arange(0, N_FOURIER + 1)] + mags = np.abs(c[k]) + mags[k == 0] = 0.0 + return mags / mags[k == 1][0] + + +def separation_spectrum(a_pts: np.ndarray, b_pts: np.ndarray) -> tuple[np.ndarray, dict]: + d, _ = cKDTree(b_pts).query(a_pts) + hist, _ = np.histogram(d, bins=SEP_BINS_UM, weights=np.full(len(d), BOUNDARY_STEP_UM)) + dmin = float(d.min()) + s = {"dmin": dmin, "P_inv": float(np.sum(BOUNDARY_STEP_UM / np.maximum(d, 0.05)))} + for f in (1.05, 1.5, 2.0, 3.0): + s[f"L_{f:g}dmin"] = float(np.sum(d <= f * dmin) * BOUNDARY_STEP_UM) + for d0 in (5.0, 10.0, 20.0): + s[f"L_le{d0:g}um"] = float(np.sum(d <= d0) * BOUNDARY_STEP_UM) + return hist, s + + +def window(c: Conductors) -> tuple[float, float, float]: + """Square raster window around the pocket and signal metal: (x0, y0, side) in µm.""" + x0, y0, x1, y1 = shapely.union(c.N, c.S).buffer(0).bounds + gx0, gy0, gx1, gy1 = c.pocket.bounds + cx, cy = (gx0 + gx1) / 2, (gy0 + gy1) / 2 + side = 1.05 * max(gx1 - gx0, gy1 - gy0, x1 - x0, y1 - y0) + return cx - side / 2, cy - side / 2, side + + +def raster(c: Conductors, npx: int = RASTER_PX) -> tuple[dict[str, np.ndarray], float, tuple[float, float]]: + wx0, wy0, side = window(c) + cx, cy = wx0 + side / 2, wy0 + side / 2 + px = side / npx + xs = cx - side / 2 + (np.arange(npx) + 0.5) * px + ys = cy - side / 2 + (np.arange(npx) + 0.5) * px + X, Y = np.meshgrid(xs, ys) + masks = {} + for k, g in c.metals().items(): + shapely.prepare(g) + masks[k] = shapely.contains_xy(g, X, Y) + return masks, px, (cx - side / 2, cy - side / 2) + + +def radial_power_spectrum(img: np.ndarray, px: float) -> tuple[np.ndarray, np.ndarray]: + n = img.shape[0] + F = np.fft.fftshift(np.fft.fft2(img - img.mean())) + P = np.abs(F) ** 2 / img.size + k = np.fft.fftshift(np.fft.fftfreq(n, d=px)) + KX, KY = np.meshgrid(k, k) + kr = np.hypot(KX, KY).ravel() + edges = np.geomspace(1.0 / (n * px), 0.5 / px, K_BINS + 1) + idx = np.digitize(kr, edges) - 1 + ok = (idx >= 0) & (idx < K_BINS) + s = np.bincount(idx[ok], weights=P.ravel()[ok], minlength=K_BINS) + cnt = np.bincount(idx[ok], minlength=K_BINS) + return np.sqrt(edges[1:] * edges[:-1]), np.where(cnt > 0, s / np.maximum(cnt, 1), np.nan) + + +def dirichlet_eigenvalues(mask: np.ndarray, px: float, k: int = N_EIG) -> np.ndarray: + """Lowest Dirichlet Laplacian eigenvalues of a pixel mask (Shape-DNA), in um^-2.""" + idx = -np.ones(mask.shape, dtype=np.int64) + ii, jj = np.nonzero(mask) + n = len(ii) + if n < 4 * k: + return np.full(k, np.nan) + idx[ii, jj] = np.arange(n) + rows, cols = [np.arange(n)], [np.arange(n)] + vals = [np.full(n, 4.0)] + for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)): + ni, nj = ii + di, jj + dj + inside = (ni >= 0) & (ni < mask.shape[0]) & (nj >= 0) & (nj < mask.shape[1]) + nb = np.full(n, -1) + nb[inside] = idx[ni[inside], nj[inside]] + ok = nb >= 0 + rows.append(np.arange(n)[ok]) + cols.append(nb[ok]) + vals.append(-np.ones(ok.sum())) + L = sparse.csc_matrix((np.concatenate(vals), (np.concatenate(rows), np.concatenate(cols))), shape=(n, n)) + ev = eigsh(L, k=k, sigma=0, which="LM", return_eigenvectors=False) + return np.sort(ev) / px**2 + + +def geometry_record(gds_path) -> tuple[dict, dict]: + return geometry_record_conductors(load_conductors(gds_path)) + + +def geometry_record_conductors(c: Conductors) -> tuple[dict, dict]: + """Descriptors of a (N, S, G) conductor triple; for multi-conductor queries N/S are the queried pair, G the rest.""" + scalars, arrays = {}, {} + for name, g in (("N", c.N), ("S", c.S), ("pocket", c.pocket)): + for k, v in shape_metrics(g).items(): + scalars[f"{name}_{k}"] = float(v) + arrays[f"fourier_{name}"] = fourier_descriptors(g) + scalars["gap_area"] = c.gap.area + scalars["chip_w"], scalars["chip_h"] = c.chip.bounds[2] - c.chip.bounds[0], c.chip.bounds[3] - c.chip.bounds[1] + scalars["G_area"] = c.G.area + + b = boundaries(c) + for a, t in (("N", "S"), ("S", "N"), ("N", "G"), ("G", "N"), ("S", "G"), ("G", "S")): + hist, s = separation_spectrum(b[a], b[t]) + arrays[f"sep_{a}{t}"] = hist + scalars.update({f"sep_{a}{t}_{k}": v for k, v in s.items()}) + + masks, px, origin = raster(c) + scalars["raster_px_um"] = px + metal = (masks["N"] | masks["S"] | masks["G"]).astype(np.float64) + k, P = radial_power_spectrum(metal, px) + arrays["psd_k"], arrays["psd_metal"] = k, P + arrays["psd_NS"] = radial_power_spectrum(masks["N"].astype(float) - masks["S"].astype(float), px)[1] + for name in ("N", "S"): + arrays[f"eig_{name}"] = dirichlet_eigenvalues(masks[name], px) + scalars[f"{name}_raster_area_err"] = float(masks[name].sum() * px**2 / getattr(c, name).area - 1) + arrays["sep_bins_um"] = SEP_BINS_UM + return scalars, arrays diff --git a/geomgnn/_tlsq/uge/__init__.py b/geomgnn/_tlsq/uge/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..b6689b8e89b04c8f16ebc3e1102af4190fb8d841 --- /dev/null +++ b/geomgnn/_tlsq/uge/__init__.py @@ -0,0 +1,5 @@ +"""Universal GDS Embedding framework: GDS -> scene -> scene graph (+ physics-proxy plugins) -> universal vector / GNN. + +The recipe is dataset-agnostic; what a user adapts is (1) the layer map / net naming of their GDS, (2) the physics proxy +plugin that fits their observable (capacitance, resonance frequency, kappa, ...), and (3) the target adapter. +""" diff --git a/geomgnn/_tlsq/uge/blocks.py b/geomgnn/_tlsq/uge/blocks.py new file mode 100644 index 0000000000000000000000000000000000000000..6af5b2b7fea2512ce9fe003f3bb5d33eb6e3a492 --- /dev/null +++ b/geomgnn/_tlsq/uge/blocks.py @@ -0,0 +1,149 @@ +"""Physics-agnostic geometry blocks: node (per conductor), edge (per conductor pair) and global features, all in +physical units (log µm, ratios) so that nothing is fitted to a dataset.""" + +from __future__ import annotations + +import numpy as np +import shapely +from scipy.special import ellipk +from scipy.spatial import cKDTree + +from ..embed.proxy import _nthreads +from ..geometry import fourier_descriptors, sample_boundary, shape_metrics +from .scene import Scene + +GRAN_R_UM = np.geomspace(0.25, 100.0, 16) +SEP_EDGES_UM = np.geomspace(0.5, 2000.0, 11) +MOMENTS = (0.5, 1.0, 2.0) +STEP_UM = 0.25 + + +def _log(x): + return float(np.log(max(float(x), 1e-9))) + + +def region(scene: Scene, i: int): + """The shape that carries conductor i's edge: the etched pocket for the ground net, the metal otherwise.""" + return scene.pocket if scene.conductors[i].role == "ground" else scene.conductors[i].geom + + +def granulometry(g) -> np.ndarray: + a0 = g.area + return np.array([g.buffer(-r, quad_segs=4).area / a0 if a0 > 0 else 0.0 for r in GRAN_R_UM]) + + +def node_features(scene: Scene, i: int) -> dict: + g = region(scene, i) + m = shape_metrics(g) + f = {"role_ground": float(scene.conductors[i].role == "ground"), + "log_area": _log(m["area"]), "log_perimeter": _log(m["perimeter"]), "compactness": m["compactness"], + "solidity": m["solidity"], "log_min_halfwidth": _log(m["inscribed_radius"]), + "log_aspect": abs(_log(m["bbox_w"]) - _log(m["bbox_h"])), "log_elongation": _log(m["elongation"]), + "log_Imajor_over_A2": _log(m["I_major"] / max(m["area"], 1e-9) ** 2), "n_holes": float(m["n_holes"]), + "log_width": _log(2 * m["area"] / m["perimeter"])} + for j, v in enumerate(fourier_descriptors(g)): + f[f"fourier_{j:02d}"] = float(np.log1p(50 * v)) + for j, v in enumerate(granulometry(g)): + f[f"gran_{j:02d}"] = float(v) + return f + + +def _kratio(m): + m = np.clip(m, 1e-12, 1 - 1e-12) + return ellipk(1 - m) / ellipk(m) + + +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)} +TOKEN_VARIANT: dict = {"name": "exact"} # PRIOR_SCHEME.md 14.3; "exact" = the published tokens + + +def set_token_variant(name: str) -> None: + from ..geometry import MIC_TOL + + assert name in TOKEN_VARIANTS, name + TOKEN_VARIANT["name"] = name + MIC_TOL[0] = 0.01 if name == "exact" else 0.1 + + +def _block_distances(pts: np.ndarray, tree) -> tuple: + """Distances from the samples of a to b (k-d tree) for the active token variant, with weights (multiples of the + sample step): the samples are grouped into consecutive blocks of k; a block whose first sample is farther than d_far + from b is represented by that sample with weight = block size, the other blocks sample by sample. crop: blocks whose + first sample is farther than the crop distance are dropped (their representatives kept if every block is dropped).""" + v = TOKEN_VARIANTS[TOKEN_VARIANT["name"]] + k = v.get("k", 64) + r = v.get("crop", v.get("d_far")) + rep = np.arange(0, len(pts), k) + d_rep, _ = tree.query(pts[rep], workers=_nthreads()) + size = np.diff(np.r_[rep, len(pts)]) + far = d_rep > r + near_pts = np.repeat(~far, size) + d_near, _ = tree.query(pts[near_pts], workers=_nthreads()) + if "crop" in v: + if not near_pts.any(): + return d_rep, size.astype(float) + return d_near, np.ones(len(d_near)) + return np.r_[d_rep[far], d_near], np.r_[size[far].astype(float), np.ones(len(d_near))] + + +def edge_features(scene: Scene, i: int, j: int, bpts: dict, widths: dict) -> dict: + """Symmetric pair features: separation spectrum (both directions averaged), minimum gap, coupling moments and the + coplanar-strip conformal-map coupling (Gevorgian modulus; the ground net is treated as infinitely wide).""" + f = {"is_ground_pair": float("ground" in (scene.conductors[i].role, scene.conductors[j].role))} + hist = np.zeros(len(SEP_EDGES_UM) - 1) + mom = np.zeros(len(MOMENTS)) + cps = 0.0 + dmin = np.inf + for a, b in ((i, j), (j, i)): + if TOKEN_VARIANT["name"] != "exact": # approximate tokens (PRIOR_SCHEME.md 14.3) + d, wq = _block_distances(bpts[a], cKDTree(bpts[b])) + dmin = min(dmin, d.min()) + hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=STEP_UM * wq)[0] + dd = np.maximum(d, 0.05) + mom += 0.5 * np.array([np.sum(STEP_UM * wq * dd ** (-p)) for p in MOMENTS]) + w1, w2 = widths[a], widths[b] + if np.isinf(w1) or np.isinf(w2): + wf = w2 if np.isinf(w1) else w1 + mm = dd / (dd + wf) + else: + mm = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2)) + cps += 0.5 * np.sum(STEP_UM * wq * _kratio(mm)) + continue + d, _ = cKDTree(bpts[b]).query(bpts[a], workers=_nthreads()) + dmin = min(dmin, d.min()) + hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=np.full(len(d), STEP_UM))[0] + dd = np.maximum(d, 0.05) + mom += 0.5 * np.array([np.sum(STEP_UM * dd ** (-p)) for p in MOMENTS]) + w1, w2 = widths[a], widths[b] + if np.isinf(w1) or np.isinf(w2): + wf = w2 if np.isinf(w1) else w1 + mm = dd / (dd + wf) + else: + mm = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2)) + cps += 0.5 * np.sum(STEP_UM * _kratio(mm)) + f["log_dmin"] = _log(dmin) + for k, v in enumerate(hist): + f[f"sep_{k:02d}"] = float(np.log1p(v)) + for p, v in zip(MOMENTS, mom): + f[f"mom_{p:g}"] = _log(v) + f["log_cps"] = _log(cps) + f["_cps"] = cps # raw, used for shielding shares + return f + + +def global_features(scene: Scene) -> dict: + st = scene.stack + n_sig = sum(c.role == "signal" for c in scene.conductors) + return {"n_metal_layers": float(st["n_metal_layers"]), "log_metal_t": _log(st["metal_thickness_um"]), + "log_substrate_t": _log(st["substrate_thickness_um"]), "eps_eff": (st["substrate_eps_r"] + 1) / 2, + "n_signal": float(n_sig), "log_chip_area": _log(scene.chip.area), + "log_pocket_area": _log(scene.pocket.area)} + + +def boundary_points(scene: Scene) -> tuple[dict, dict]: + pts, widths = {}, {} + for i, c in enumerate(scene.conductors): + g = region(scene, i) + pts[i] = sample_boundary(g, STEP_UM) + widths[i] = np.inf if c.role == "ground" else 2 * g.area / g.length + return pts, widths diff --git a/geomgnn/_tlsq/uge/graph.py b/geomgnn/_tlsq/uge/graph.py new file mode 100644 index 0000000000000000000000000000000000000000..5e7be861abf5666f04dd1dd12b73dc19ec03063a --- /dev/null +++ b/geomgnn/_tlsq/uge/graph.py @@ -0,0 +1,59 @@ +"""Scene graph (nodes = conductors, edges = all conductor pairs) and its permutation-invariant pooled vector.""" + +from __future__ import annotations + +from itertools import combinations + +import numpy as np + +from .blocks import boundary_points, edge_features, global_features, node_features +from .proxies import Proxy +from .scene import Scene + + +def scene_graph(scene: Scene, proxy: Proxy | None = None) -> dict: + bpts, widths = boundary_points(scene) + nodes = [node_features(scene, i) for i in range(scene.n)] + edges = {(i, j): edge_features(scene, i, j, bpts, widths) for i, j in combinations(range(scene.n), 2)} + # shielding: share of each endpoint's total geometric coupling carried by this edge + tot = np.zeros(scene.n) + for (i, j), f in edges.items(): + tot[i] += f["_cps"] + tot[j] += f["_cps"] + for (i, j), f in edges.items(): + si, sj = f["_cps"] / max(tot[i], 1e-12), f["_cps"] / max(tot[j], 1e-12) + f["share_mean"], f["share_absdiff"] = 0.5 * (si + sj), abs(si - sj) + g = {"names": scene.names(), "global": global_features(scene), "nodes": nodes, "edges": edges} + if proxy is not None: + px = proxy.compute(scene, edges) + for i, v in px["node"].items(): + nodes[i].update({f"proxy_{k}": x for k, x in v.items()}) + for e, v in px["edge"].items(): + edges[e].update({f"proxy_{k}": x for k, x in v.items()}) + g["global"].update({f"proxy_{k}": x for k, x in px["global"].items()}) + if "matrix" in px: + g["proxy_matrix"] = px["matrix"] + for f in edges.values(): + f.pop("_cps") + return g + + +def pooled_vector(g: dict) -> dict: + """Fixed-length universal vector for any number of conductors: global block, the ground node, sum/mean/max over + signal nodes, and sum/mean/max over signal-signal and signal-ground edges.""" + out = {f"global.{k}": v for k, v in g["global"].items()} + roles = ["ground" if n["role_ground"] else "signal" for n in g["nodes"]] + keys = [k for k in g["nodes"][0] if k != "role_ground"] + gi = roles.index("ground") + out.update({f"ground.{k}": g["nodes"][gi][k] for k in keys}) + S = np.array([[n[k] for k in keys] for n, r in zip(g["nodes"], roles) if r == "signal"]) + for agg, fn in (("mean", np.mean), ("max", np.max), ("min", np.min)): + out.update({f"signal.{agg}.{k}": v for k, v in zip(keys, fn(S, 0))}) + ekeys = [k for k in next(iter(g["edges"].values())) if k != "is_ground_pair"] + for typ, flag in (("ss", 0.0), ("sg", 1.0)): + E = np.array([[f[k] for k in ekeys] for f in g["edges"].values() if f["is_ground_pair"] == flag]) + if len(E) == 0: + E = np.full((1, len(ekeys)), np.nan) + for agg, fn in (("mean", np.nanmean), ("max", np.nanmax), ("min", np.nanmin)): + out.update({f"edge_{typ}.{agg}.{k}": v for k, v in zip(ekeys, fn(E, 0))}) + return out diff --git a/geomgnn/_tlsq/uge/localtok.py b/geomgnn/_tlsq/uge/localtok.py new file mode 100644 index 0000000000000000000000000000000000000000..b24ae43dfc904cd4fc12b7d6e75e30e8b82d0e9f --- /dev/null +++ b/geomgnn/_tlsq/uge/localtok.py @@ -0,0 +1,99 @@ +"""Local, window-invariant context tokens (PRIOR_SCHEME.md 14.5 E4; replace the chip / pocket areas): for every signal +conductor and radius R, the fractions of its surrounding annulus (conductor buffered by R, minus the conductor) that are +ground metal, other-conductor metal and etched chip; the rest of the annulus lies outside the chip (no metal). Pair +tokens = mean over the pair's signal ends (a ground pair: its signal end). Identical copy: tlsq_infer/localtok.py.""" + +from __future__ import annotations + +import numpy as np +import shapely +from scipy.spatial import cKDTree + +from ..geometry import sample_boundary +from .blocks import MOMENTS, SEP_EDGES_UM, STEP_UM, _kratio + +RADII_UM = (50.0, 150.0, 400.0) +KEYS = [f"lt_{k}{int(r)}" for r in RADII_UM for k in ("g", "o", "e")] + + +def node_tokens(scene) -> np.ndarray: + """(n, len(KEYS)) per conductor (ground row = NaN).""" + conds = scene.conductors + n = len(conds) + G = next(c.geom for c in conds if c.role == "ground") + sig = [k for k, c in enumerate(conds) if c.role != "ground"] + tree = shapely.STRtree([conds[k].geom for k in sig]) + out = np.full((n, len(KEYS)), np.nan) + for k in sig: + g = conds[k].geom + vals = [] + for R in RADII_UM: + ring = g.buffer(R, quad_segs=4).difference(g) + A = ring.area + box = ring.bounds + gm = shapely.intersection(shapely.clip_by_rect(G, *box), ring).area + near = [sig[i] for i in tree.query(ring, predicate="intersects") if sig[i] != k] + om = shapely.intersection(shapely.union_all([conds[i].geom for i in near]), ring).area if near else 0.0 + inchip = shapely.intersection(scene.chip, ring).area + vals += [gm / A, om / A, max(inchip - gm - om, 0.0) / A] + out[k] = vals + return out + + +LG_R_UM = 100.0 +TOK = ["log_dmin"] + [f"sep_{k:02d}" for k in range(10)] + ["mom_0.5", "mom_1", "mom_2", "log_cps"] +LG_KEYS = [f"lg_{k}" for k in TOK] + + +def ground_tokens(scene) -> np.ndarray: + """(n, len(LG_KEYS)) per signal conductor: the ground-pair tokens of blocks.edge_features (separation spectrum, minimum + gap, coupling moments, coplanar-strip coupling) from the ground-edge samples within LG_R_UM of the conductor only + (fixed radius: independent of the ground window).""" + conds = scene.conductors + out = np.full((len(conds), len(LG_KEYS)), np.nan) + for k, c in enumerate(conds): + if c.role == "ground": + continue + g = c.geom + b = g.bounds + 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) + pj = sample_boundary(g, STEP_UM) + pg = sample_boundary(P, STEP_UM) if not P.is_empty else np.zeros((0, 2)) + if len(pg): + pg = pg[cKDTree(pj).query(pg)[0] < LG_R_UM] # drops the clip-box edges (>= LG_R_UM away) and the far ground + if not len(pg): + continue + wj = 2 * g.area / g.length + hist = np.zeros(len(SEP_EDGES_UM) - 1) + mom = np.zeros(len(MOMENTS)) + cps, dmin = 0.0, np.inf + for a_, b_ in ((pj, pg), (pg, pj)): + d = cKDTree(b_).query(a_)[0] + dmin = min(dmin, d.min()) + hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=np.full(len(d), STEP_UM))[0] + dd = np.maximum(d, 0.05) + mom += 0.5 * np.array([np.sum(STEP_UM * dd ** (-p)) for p in MOMENTS]) + cps += 0.5 * np.sum(STEP_UM * _kratio(dd / (dd + wj))) + 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))] + return out + + +def add_tokens(scene, row: dict) -> dict: + """Row (scene graph dict) with the KEYS added to every edge (shallow copy).""" + return add_from_tokens(row, node_tokens(scene), ground_tokens(scene), [c.role != "ground" for c in scene.conductors]) + + +def add_from_tokens(row: dict, T: np.ndarray, Lg: np.ndarray, sig) -> dict: + """Edges + KEYS (mean over the signal ends) + LG_KEYS (ground pairs: the fixed-radius tokens of the signal end; other + pairs: their own tokens, unchanged).""" + sig = np.asarray(sig) + edges = {} + for (i, j), f in row["edges"].items(): + ends = [e for e in (i, j) if sig[e]] + new = {k: float(x) for k, x in zip(KEYS, T[ends].mean(0))} + if len(ends) == 1 and np.all(np.isfinite(Lg[ends[0]])): + new.update({k: float(x) for k, x in zip(LG_KEYS, Lg[ends[0]])}) + else: + new.update({f"lg_{k}": float(f[k]) for k in TOK}) + edges[(i, j)] = {**f, **new} + return {**row, "edges": edges} diff --git a/geomgnn/_tlsq/uge/proxies.py b/geomgnn/_tlsq/uge/proxies.py new file mode 100644 index 0000000000000000000000000000000000000000..257da3ed0b735f127e702b3c1f0cdf776366d45b --- /dev/null +++ b/geomgnn/_tlsq/uge/proxies.py @@ -0,0 +1,102 @@ +"""Physics-proxy plugins. A proxy maps a Scene to extra node / edge / global quantities that are cheap to compute and +physically aligned with the target. Swap the proxy to move the recipe to another observable (e.g. a resonator dataset +would plug in a quasi-TEM transmission-line proxy for f0 and kappa); the rest of the pipeline is unchanged.""" + +from __future__ import annotations + +import numpy as np + +from ..embed.proxy import mom_capacitance_gds, mom_capacitance_gds_avg, mom_capacitance_gds_flagship, mom_capacitance_gds_rich +from .scene import Scene + +EPS0 = 8.8541878128e-3 # fF/µm + + +class Proxy: + name = "none" + + def compute(self, scene: Scene, edges: dict) -> dict: + """Return {"node": {i: {...}}, "edge": {(i, j): {...}}, "global": {...}}; `edges` holds the geometry edge features.""" + return {"node": {}, "edge": {}, "global": {}} + + +class NoProxy(Proxy): + name = "none" + + +class ConformalCoupling(Proxy): + """Solver-free: per-pair coplanar-strip conformal-map coupling (already a geometry edge feature) turned into a + capacitance estimate C_ij ~ eps0 eps_eff * sum L K(k')/K(k), and node totals.""" + + name = "conformal" + + def compute(self, scene, edges): + e_eff = (scene.stack["substrate_eps_r"] + 1) / 2 + out = {"node": {i: {"log_Cself": -np.inf} for i in range(scene.n)}, "edge": {}, "global": {}} + tot = np.zeros(scene.n) + for (i, j), f in edges.items(): + c = EPS0 * e_eff * f["_cps"] + out["edge"][(i, j)] = {"log_C": float(np.log(max(c, 1e-9)))} + tot[i] += c + tot[j] += c + for i in range(scene.n): + out["node"][i] = {"log_Cself": float(np.log(max(tot[i], 1e-9)))} + return out + + +class CapacitanceMoM(Proxy): + """Thin-film method of moments on the exact polygons (quadtree mesh, whole ground plane): full Maxwell matrix.""" + + name = "mom" + + def __init__(self, max_cells: int = 8000, cells_per_gap: float = 3.0, n_offsets: int = 1, mesh: str = "v2", + substrate: str = "slab_open"): + self.max_cells, self.cells_per_gap, self.n_offsets, self.mesh = max_cells, cells_per_gap, n_offsets, mesh + self.substrate = substrate # MoM Green's function (embed.proxy.SUBSTRATE; PRIOR_SCHEME.md 14.6) + + def compute(self, scene, edges): + from ..embed.proxy import substrate + + with substrate(self.substrate, scene.stack.get("substrate_thickness_um")): + return self._compute(scene, edges) + + def _compute(self, scene, edges): + metals, kw = [c.geom for c in scene.conductors], dict(eps_r=scene.stack["substrate_eps_r"], max_cells=self.max_cells, + cells_per_gap=self.cells_per_gap) + if self.mesh == "v3-rich": # conductor-anchored mesh, Richardson-extrapolated (gap/3 and gap/6) + C, n, hm = mom_capacitance_gds_rich(metals, kw["eps_r"], self.cells_per_gap, self.max_cells, 2 * self.max_cells) + elif self.mesh in ("lite", "formula"): # flagship-fast cheap priors (PRIOR_SCHEME.md Part III): micro-MoM at a panel budget / closed form + from ..embed.fastprior import formula_prior, micro_mom + + if self.mesh == "lite": + C, _, info = micro_mom(metals[0], metals[1:], kw["eps_r"], budget=self.max_cells, cells_per_gap=self.cells_per_gap) + n, hm = int(sum(info["panels"])), float(min(info["h_um"])) + mg = float(info["min_gap_um"]) # the mesh's smallest conductor gap (h = gap / ppg off the floor) + else: + C, info = formula_prior(metals[0], metals[1:], kw["eps_r"]) + n, hm = 0, float(info["min_gap_um"]) + elif self.mesh == "flagship": # strip-merged, resolution-controlled Richardson pair, H-matrix solve (PRIOR_SCHEME.md Part II) + C, n, hm, _ = mom_capacitance_gds_flagship(metals, kw["eps_r"], self.cells_per_gap, self.max_cells) + else: + if self.mesh != "v2": + kw["mesh"] = self.mesh + try: + C, n, hm = mom_capacitance_gds_avg(metals, self.n_offsets, **kw) if self.n_offsets > 1 else mom_capacitance_gds(metals, **kw) + except np.linalg.LinAlgError: + if self.mesh != "v3": + raise + # v3 single solve: sliver edge panels can make the gap/3 system indefinite (~0.2 % of designs); use gap/6 + kw.update(cells_per_gap=2 * self.cells_per_gap, max_cells=2 * self.max_cells) + C, n, hm = mom_capacitance_gds(metals, **kw) + out = {"node": {i: {"log_Cself": float(np.log(max(C[i, i], 1e-9)))} for i in range(scene.n)}, "edge": {}, + "global": {"cells": float(n), "h_min_um": float(hm)}, "matrix": C} + if self.mesh != "lite": # every variant records the layout's smallest conductor gap (review W1); lite: the mesh's own + from ..embed.fastprior import gap_of + mg = float(info["min_gap_um"]) if self.mesh == "formula" else float(gap_of(metals)) + out["global"]["min_gap_um"] = mg + for (i, j) in edges: + out["edge"][(i, j)] = {"log_C": float(np.log(max(-C[i, j], 1e-9)))} + return out + + +PROXIES = {p.name: p for p in (NoProxy, ConformalCoupling, CapacitanceMoM)} diff --git a/geomgnn/_tlsq/uge/scene.py b/geomgnn/_tlsq/uge/scene.py new file mode 100644 index 0000000000000000000000000000000000000000..fd065001f444949153aed2c227c3eda7c321d02f --- /dev/null +++ b/geomgnn/_tlsq/uge/scene.py @@ -0,0 +1,90 @@ +"""Scene: the metal of a layout as a list of conductors (one ground net + any number of signal bodies), plus the stack.""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Callable + +import gdstk +import numpy as np +import shapely +from shapely.geometry import Polygon + +DEFAULT_LAYERS = {"ground": [(1, 0)], "signal": [(1, 10)]} +DEFAULT_STACK = {"n_metal_layers": 1, "metal_thickness_um": 0.25, "substrate_thickness_um": 350.0, "substrate_eps_r": 11.45} + + +@dataclass +class Conductor: + name: str + role: str # "ground" | "signal" + geom: object # shapely (Multi)Polygon, µm + + +@dataclass +class Scene: + conductors: list[Conductor] # index 0 is the ground net + chip: Polygon + pocket: object # chip minus ground metal: the etched region (its edge is the ground's active edge) + stack: dict = field(default_factory=lambda: dict(DEFAULT_STACK)) + markers: dict = field(default_factory=dict) # non-metal helper shapes by (layer, datatype), e.g. port stubs; naming only + + @property + def n(self) -> int: + return len(self.conductors) + + def names(self) -> list[str]: + return [c.name for c in self.conductors] + + +def _valid(g): + g = shapely.make_valid(g) + parts = [p for p in getattr(g, "geoms", [g]) if p.geom_type in ("Polygon", "MultiPolygon")] + return shapely.union_all(parts) + + +def scene_from_gds(path, layers: dict = DEFAULT_LAYERS, stack: dict | None = None, + namer: Callable[[list, object], list[str]] | None = None) -> Scene: + """Read only metal layers. Each signal polygon is one conductor; all ground polygons form one net. + `namer(signal_geoms, markers)` returns a name per signal body (dataset convention); default S0, S1, ...""" + lib = gdstk.read_gds(str(path)) + cell = lib.top_level()[0] + u = lib.unit / 1e-6 + lay = {k: {tuple(x) for x in v} for k, v in layers.items()} + sig, gnd, other = [], [], {} + for p in cell.polygons: + key = (p.layer, p.datatype) + g = Polygon(p.points * u) + if key in lay["signal"]: + sig.append(g) + elif key in lay["ground"]: + gnd.append(g) + else: + other.setdefault(key, []).append(g) + G = _valid(shapely.union_all([_valid(g) for g in gnd])) + S = [_valid(g) for g in sig] + chip = shapely.box(*G.bounds) + markers = {k: shapely.union_all(v) for k, v in other.items()} + names = namer(S, markers) if namer else [f"S{i}" for i in range(len(S))] + conds = [Conductor("G", "ground", G)] + [Conductor(n, "signal", g) for n, g in zip(names, S)] + return Scene(conds, chip, chip.difference(G), {**DEFAULT_STACK, **(stack or {})}, markers) + + +def tlsq_namer(signals: list, markers: dict, n_slots: int = 5) -> list[str]: + """This dataset's convention: N = body fed by the north port stub (2,0); S = body fed by the south stub (3,0) if + present, else the body containing the origin (star, inner pad); remaining bodies P1, P2, ... ordered by angle.""" + north, south = markers.get((2, 0)), markers.get((3, 0)) + n = min(range(len(signals)), key=lambda i: signals[i].distance(north)) + rest = [i for i in range(len(signals)) if i != n] + if south is not None: + s = min(rest, key=lambda i: signals[i].distance(south)) + else: + s = next((i for i in rest if signals[i].contains(shapely.Point(0, 0))), max(rest, key=lambda i: signals[i].area)) + names = [""] * len(signals) + names[s], names[n] = "S", "N" + a0 = np.degrees(np.arctan2(signals[n].centroid.y, signals[n].centroid.x)) + for i in rest: + if i != s: + a = np.degrees(np.arctan2(signals[i].centroid.y, signals[i].centroid.x)) + names[i] = f"P{int(round(((a - a0) % 360) / (360 / n_slots))) % n_slots}" + return names diff --git a/geomgnn/api.py b/geomgnn/api.py new file mode 100644 index 0000000000000000000000000000000000000000..ec934095eacf3d50bd4b971fd9513f8a5274d686 --- /dev/null +++ b/geomgnn/api.py @@ -0,0 +1,380 @@ +"""Registry-driven GNN prediction for the Space (deploy/registry.yaml: gnn_hub, gnn_models). + +predict_gnn(prepared, name) runs the frozen Predictor (geomgnn.infer, FINALIZATION_V1 checkpoints) on the scene built from a +tlsq_infer PreparedLayout. The steps after the scene are those of infer.Predictor.from_gds, statement for statement; +deploy/tests/test_gnn_space.py checks the result against the frozen stage's from_gds on the raw TRAIN GDS files. +Entries with `learned_prior` (the fast tier) replace the two field solves by the learned prior surrogate +(geomgnn.learned_prior = scripts/learnedprior/lp_bench.py Learned.from_gds); the networks are the same frozen weights. +""" + +from __future__ import annotations + +import contextlib +import hashlib +import json +import os +import threading +import time +import warnings +from pathlib import Path + +import numpy as np +import shapely + +from . import __version__ +from . import infer as I +from ._tlsq.embed import fastprior, proxy +from ._tlsq.uge.graph import scene_graph +from ._tlsq.uge.scene import DEFAULT_STACK, Conductor, Scene + +lt = I.lt +EPS_TRAIN, T_TRAIN_UM = 11.45, 350.0 +PRIOR_IMPLS = ("frozen", "cpu_fast") +_HMAT0 = dict(proxy.HMAT) +_STATE = {"prior_impl": "frozen", "patches": [], "mode": None, "stack": None} +_CACHE: dict = {} + + +def gnn_names(reg) -> list[str]: + return [k for k, v in (reg.data.get("gnn_models") or {}).items() if v.get("status", "available") == "available"] + + +def set_prior_impl(impl: str) -> None: + """frozen: the solver as frozen. cpu_fast: scripts/geomfast/fastmom.py (vectorised H-matrix kernel) with ACA eps 1e-6 and + PCG tol 1e-8 (agents/geomfast/FASTPRIOR_STATUS.md); process-wide.""" + if impl not in PRIOR_IMPLS: + raise ValueError(f"prior_impl {impl!r} not in {PRIOR_IMPLS}") + if impl == _STATE["prior_impl"]: + return + from . import fastmom + if impl == "cpu_fast": + fastmom.install() + proxy.HMAT.update(eps=1e-6, tol=1e-8) + else: + fastmom.uninstall() + proxy.HMAT.clear() + proxy.HMAT.update(_HMAT0) + _STATE["prior_impl"] = impl + + +VALUE_PRESERVING = ("rejtail_fv",) # bit-identical priors +VALUE_CHANGING = ("priorv2m", "priorv2") # change the prior: need prior.value_changing_approved in the registry entry +# priorv2.installed() arguments of each value-changing patch: v2m = the corrected prior of the model; v2 = version 2 (no margin +# widening), the prior of the paper's "retrained, version 2" ablation (paper Space only) +V2_KWARGS = {"priorv2m": dict(policy="fc", dip=True, margin=True), "priorv2": dict(policy="fc", dip=True, margin=False)} +PRIOR_PATCHES = VALUE_PRESERVING + VALUE_CHANGING + + +SPEED_MODES = ("exact", "exact_compiled", "certified") # geomgnn.fastprior_pkg modes (agents/fastprior_pkg/RESULTS.md) + + +def speed_mode(pr: dict): + """Solver mode of the prior: TLSQ_GNN_PRIOR_MODE (opt-in override, e.g. 'certified' or 'frozen') else registry prior.mode; + None / 'frozen' = the vendor solver as frozen (no fastprior_pkg).""" + m = os.environ.get("TLSQ_GNN_PRIOR_MODE") or pr.get("mode") + if m in (None, "", "frozen"): + return None + if m not in SPEED_MODES: + raise ValueError(f"prior mode {m!r} not in {SPEED_MODES} (or 'frozen')") + return m + + +def _v2m_value_patch(F, P, FL, patch="priorv2m"): + """fastprior_pkg value hook: prior v2m (geomgnn.priorv2 installed(policy='fc', dip=True, margin=True)) on top of the + solver components (patch="priorv2": version 2, margin=False); returns the undo callable.""" + from . import priorv2 + assert priorv2.F is F and priorv2.P is P, "priorv2 and fastprior_pkg must patch the same vendor modules" + ctx = priorv2.installed(**V2_KWARGS[patch]) + ctx.__enter__() + return lambda: ctx.__exit__(None, None, None) + + +def _teardown() -> None: + from . import rejtail_fv + if _STATE.get("mode"): + from . import fastprior_pkg + fastprior_pkg.uninstall() # also undoes its value patch + elif set(V2_KWARGS) & set(_STATE["patches"]): + _STATE.pop("v2ctx").__exit__(None, None, None) + rejtail_fv.uninstall() + _STATE.update(patches=[], mode=None, stack=None) + + +def set_prior_stack(pr: dict, threads: int = 2) -> None: + """Process-wide prior stack of a registry prior: patches (rejtail_fv | priorv2m) and the solver mode (fastprior_pkg). + rejtail_fv: geomgnn.rejtail_fv (value-preserving rejection-tail fix; fastprior_pkg's fv replaces it, so not with a mode). + priorv2m: prior v2m (agents/priorv2/FINALIZATION_V2.json): priorv2.installed(policy='fc', dip=True, margin=True), as + fastprior_pkg's value_patch when a mode is set; it replaces _window_solve itself, so never with rejtail_fv. + mode: fastprior_pkg.install(mode, cpus=threads, target='geomgnn', value_patch=v2m) + warmup() (numba compile).""" + patches = sorted(pr.get("patches") or []) + mode = speed_mode(pr) + bad = set(patches) - set(PRIOR_PATCHES) + if bad: + raise ValueError(f"unknown prior patches {bad}; known: {PRIOR_PATCHES}") + v2 = [p for p in patches if p in V2_KWARGS] + if v2 and "rejtail_fv" in patches: + raise ValueError("priorv2m already contains the rejection-tail handling; do not combine it with rejtail_fv") + if len(v2) > 1: + raise ValueError(f"one prior version per stack, got {v2}") + if mode and "rejtail_fv" in patches: + raise ValueError("fastprior_pkg contains fv; list rejtail_fv only without a prior mode") + unapproved = [p for p in patches if p in VALUE_CHANGING and not pr.get("value_changing_approved")] + if unapproved: + raise ValueError(f"value-changing prior patches {unapproved} need prior.value_changing_approved in the registry") + key = (tuple(patches), mode, int(threads)) + if key == _STATE.get("stack"): + return + _teardown() + if mode: + from . import fastprior_pkg as fp + fp.install(mode=mode, cpus=int(threads), workers=os.environ.get("TLSQ_PRIOR_WORKERS"), target="geomgnn", blas_threads=int(threads), + value_patch=(lambda F, P, FL: _v2m_value_patch(F, P, FL, v2[0])) if v2 else None, warm=False) + if mode in ("exact_compiled", "certified"): + _STATE["warmup_s"] = fp.warmup() + else: + from . import priorv2, rejtail_fv + if "rejtail_fv" in patches: + rejtail_fv.install() + if v2: + ctx = priorv2.installed(**V2_KWARGS[v2[0]]) + ctx.__enter__() + _STATE["v2ctx"] = ctx + _STATE.update(patches=patches, mode=mode, stack=key) + + +def _sha(p) -> str: + return hashlib.sha256(Path(p).read_bytes()).hexdigest() + + +def surrogate_file(reg, name: str, model_dir=None) -> Path | None: + """The learned prior checkpoint of an entry (bundle copy named in info.json "surrogate", else the registry path).""" + lp = reg["gnn_models"][name].get("learned_prior") + if not lp: + return None + md = os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir + if md and (Path(md) / name / "info.json").exists(): + info = json.loads((Path(md) / name / "info.json").read_text()) + if info.get("surrogate"): + return Path(md) / name / info["surrogate"] + return reg.root / lp["checkpoint"]["path"] + + +def checkpoint_files(reg, name: str, model_dir=None) -> list[Path]: + """Bundle copies (//info.json) or the registry's source paths; SHA256 checked against the registry.""" + m = reg["gnn_models"][name] + md = os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir + if md and (Path(md) / name / "info.json").exists(): + info = json.loads((Path(md) / name / "info.json").read_text()) + files = [Path(md) / name / f for f in info["files"]] + else: + files = [reg.root / c["path"] for c in m["checkpoints"]] + return files + + +def available(name: str, reg, model_dir=None) -> bool: + s = surrogate_file(reg, name, model_dir) + return all(f.exists() for f in checkpoint_files(reg, name, model_dir)) and (s is None or s.exists()) + + +def load(name: str, reg, model_dir=None, threads: int | None = None) -> I.Predictor: + m = reg["gnn_models"][name] + threads = int(threads or os.environ.get("TLSQ_NUM_THREADS") or 2) + key = (str(reg.path), name, str(os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir), threads) + if key in _CACHE: + _apply_prior(m, threads) + return _CACHE[key] + files = checkpoint_files(reg, name, model_dir) + for f, c in zip(files, m["checkpoints"], strict=True): + if _sha(f) != c["sha256"]: + raise RuntimeError(f"{name}: {f.name} SHA256 differs from the registry (frozen checkpoint {c['sha256'][:12]})") + P = I.Predictor(files, threads=threads) + if P.arm != m["arm"]: + raise RuntimeError(f"{name}: checkpoints are arm {P.arm!r}, registry says {m['arm']!r}") + pr = m.get("prior") + lpr = m.get("learned_prior") + phys = P.models[0][0].get("schema", {}).get("physics", {}) + P.surrogate = None + if P.direct: + if pr or lpr: + raise RuntimeError(f"{name}: direct arm has no prior, registry lists one") + elif lpr: + if pr or P.fast or [float(x) for x in lpr["ppg"]] != [1.5, 3.0]: + raise RuntimeError(f"{name}: a learned prior replaces the 1.5 / 3 ppg field solves of a full_gnn arm only ({P.arm}, {lpr})") + sf = surrogate_file(reg, name, model_dir) + if _sha(sf) != lpr["checkpoint"]["sha256"]: + raise RuntimeError(f"{name}: {sf.name} SHA256 differs from the registry (learned prior {lpr['checkpoint']['sha256'][:12]})") + from . import learned_prior + model, prep, _ = learned_prior.load(sf) + P.surrogate = (model, prep) + else: + if not pr or abs(float(pr["alpha"]) - float(phys.get("alpha"))) > 0 or (pr["method"] == "local_gap") != (phys.get("prior_method") == "local_gap") \ + or [float(x) for x in pr["ppg"]] != ([0.75, 1.5] if P.fast else [1.5, 3.0]): + raise RuntimeError(f"{name}: registry prior {pr} does not match the checkpoint physics {phys}") + _apply_prior(m, threads) + _CACHE[key] = P + return P + + +def _apply_prior(m: dict, threads: int = 2) -> None: + """Process-wide prior settings of a gnn_models entry (on every load: entries may differ, the patches are global).""" + pr = m.get("prior") + if pr: + set_prior_impl(pr.get("prior_impl", "frozen")) + set_prior_stack(pr, threads) + + +STARTUP: dict = {} # preload record (reported by /models) +_TLS = threading.local() + + +def _thread_limits(threads: int) -> None: + """numba and OpenMP thread counts are per thread: apply fastprior_pkg's limits once in every thread that predicts (the + preload runs in the main thread, requests in the server's worker threads).""" + if not _STATE.get("mode") or getattr(_TLS, "key", None) == (_STATE["stack"], threads): + return + from . import fastprior_pkg + fastprior_pkg._set_threads(int(threads)) + _TLS.key = (_STATE["stack"], threads) + + +def preload(name: str, reg, example=None) -> dict: + """Server start: load the default model (SHA check, prior stack, numba warmup) and, if given, predict one small example so + the first user request runs at normal speed. Synchronous (about 20-30 s on the 2-vCPU Space), before the queue opens. The + learned-prior entries (no prior stack) are loaded and run on the example too.""" + t0 = time.perf_counter() + load(name, reg) + STARTUP.update(model=name, load_and_warmup_s=time.perf_counter() - t0, numba_warmup_s=_STATE.get("warmup_s"), mode=_STATE.get("mode")) + also = [k for k in gnn_names(reg) if k != name and reg["gnn_models"][k].get("learned_prior") and available(k, reg)] + if example is not None and Path(example).exists(): + from tlsq_infer.layout import prepare + t = time.perf_counter() + predict_gnn(prepare(str(example)), name, reg) + STARTUP["example_s"] = time.perf_counter() - t + for k in also: + t = time.perf_counter() + predict_gnn(prepare(str(example)), k, reg) + STARTUP.setdefault("also_s", {})[k] = time.perf_counter() - t + else: + for k in also: + load(k, reg) + STARTUP["total_s"] = time.perf_counter() - t0 + print("preload " + json.dumps({k: (round(v, 2) if isinstance(v, float) else v) for k, v in STARTUP.items()}), flush=True) + return dict(STARTUP) + + +def scene_from_prepared(lay) -> Scene: + """The Scene scene_from_gds builds (ground = net 0, signal bodies in file order, chip = ground bounding box, training stack), + from the prepared ground and conductor bodies.""" + G = lay.ground + chip = shapely.box(*G.bounds) + conds = [Conductor("G", "ground", G)] + [Conductor(n, "signal", g) for n, g in zip(lay.names, lay.conductors)] + return Scene(conds, chip, chip.difference(G), dict(DEFAULT_STACK), {}) + + +def predict_scene(P: I.Predictor, sc: Scene, alpha=None, local_gap=None) -> dict: + """infer.Predictor.from_gds after the scene (same statements).""" + physics = P.models[0][0].get("schema", {}).get("physics", {}) + if alpha is None: + alpha = physics.get("alpha") + if local_gap is None: + local_gap = physics.get("prior_method") == "local_gap" + proxy.NUM_THREADS = P.threads + if set(V2_KWARGS) & set(_STATE["patches"]): + from . import priorv2 + priorv2.clear() # its per-window traces would otherwise grow for the life of the server + start = time.perf_counter() + t1 = time.perf_counter() + row = lt.add_tokens(sc, scene_graph(sc)) + t2 = time.perf_counter() + priors = {} + if getattr(P, "surrogate", None) is not None: + from . import learned_prior + priors = learned_prior.priors(*P.surrogate, row) + elif not P.direct: + old = fastprior.ALPHA_GAP_SHARE + try: + if alpha is not None: + fastprior.ALPHA_GAP_SHARE = alpha + for p in ([.75, 1.5] if P.fast else [1.5, 3.]): + if local_gap: + from ._tlsq.embed.fastprior_loc import micro_mom_loc + with proxy.substrate("slab_open", sc.stack["substrate_thickness_um"]), _quiet_mixed_cast(): + 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) + priors[p] = -mat + else: + priors[p] = -I.CapacitanceMoM(max_cells=10**9, mesh="lite", cells_per_gap=p, + substrate="slab_open").compute(sc, list(row["edges"]))["matrix"] + finally: + fastprior.ALPHA_GAP_SHARE = old + t3 = time.perf_counter() + C = P.from_features(row, priors) + t4 = time.perf_counter() + 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)) + + +@contextlib.contextmanager +def _quiet_mixed_cast(): + """fastprior_pkg's mixed Cholesky casts the system to float32 first; on some windows the cast overflows and numpy warns + 'invalid value / overflow encountered in cast' before the package falls back to the float64 solve (expected; the result is checked + by iterative refinement). Silence exactly that warning, from that module, during the prior call only.""" + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message="(invalid value|overflow) encountered in cast", category=RuntimeWarning, + module=r"geomgnn\.fastprior_pkg") + yield + + +def physical_checks(C: np.ndarray) -> dict: + n = len(C) + off = C[~np.eye(n, dtype=bool)] + sig = C[1:, 1:] + return {"symmetric_max_abs": float(np.max(np.abs(C - C.T))), "offdiag_all_negative": bool(np.all(off < 0)), + "signal_block_min_eig": float(np.linalg.eigvalsh(sig).min()) if n > 1 else None} + + +def predict_gnn(prepared, name: str, reg, substrate="Si (cryogenic, training)", thickness_um: float = 350.0, model_dir=None, + threads: int | None = None) -> dict: + """Full N x N Maxwell matrix (fF; ground row/column 0, self terms learned) of a prepared layout with a gnn_models entry.""" + m = reg["gnn_models"][name] + eps = reg.eps_of(substrate) + if abs(eps - EPS_TRAIN) > 1e-9 or abs(float(thickness_um) - T_TRAIN_UM) > 1e-9: + 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 " + f"only (requested eps_r {eps:g}, {float(thickness_um):g} µm); use E3 or C0 for other substrates") + t = time.perf_counter() + P = load(name, reg, model_dir, threads) + _thread_limits(P.threads) + load_s = time.perf_counter() - t + sc = scene_from_prepared(prepared) + try: + r = predict_scene(P, sc) + except AssertionError as e: + raise ValueError(f"{name}: the prior or the features of this layout are not finite (no matrix returned): {e}") from e + C = np.asarray(r["C"], float) + chk = physical_checks(C) + labels = ["G"] + list(prepared.names) + pr = m.get("prior") + lpr = m.get("learned_prior") + warnings = [w for w in prepared.warnings if "beyond the validated range" not in w] + if m.get("warning"): + warnings.insert(0, m["warning"]) + if not (chk["symmetric_max_abs"] <= 1e-6 * max(1.0, float(np.abs(C).max())) and chk["offdiag_all_negative"] + and (chk["signal_block_min_eig"] is None or chk["signal_block_min_eig"] > 0)): + warnings.append(f"physical check failed: {chk}") + tm = r["timing"] + return {"names": labels, "matrix_fF": C.tolist(), "units": "fF", + "convention": "Maxwell matrix: diagonal = self (learned), off-diagonal = -mutual; row/column 0 = ground", + "self_capacitance": "predicted by the network (learned positive row-sum closure; trained). The ground self term is " + "trained at low weight and depends on the chip boundary: indicative only", + "model": name, "model_title": m.get("title"), "recipe": "GNN", "arm": m["arm"], "role": m.get("role"), + "ensemble": m.get("ensemble"), "weights_sha256": [c["sha256"] for c in m["checkpoints"]], + "prior": ({**pr, "prior_impl": _STATE["prior_impl"], "patches": _STATE.get("patches", []), "mode": _STATE.get("mode") or "frozen", + "numba_warmup_s": _STATE.get("warmup_s")} if pr else None), + "learned_prior": ({"name": lpr["name"], "sha256": lpr["checkpoint"]["sha256"], "ppg": lpr["ppg"]} if lpr else None), + "package_version": f"geomgnn {__version__}", + "registry_version": reg["version"], "substrate": "Si (cryogenic, training)", "eps_r": EPS_TRAIN, "eps_correction_factor": 1.0, + "thickness_um": T_TRAIN_UM, "training_stack": {"eps_r": EPS_TRAIN, "thickness_um": T_TRAIN_UM}, + "timing_s": {"load_s": load_s, "geometry_s": tm["geometry"], "prior_s": tm["prior"], "network_s": tm["network_and_pack"], + "total_s": tm["total"], "threads": P.threads}, + "accuracy": m.get("accuracy") or {}, "checks": chk, "trust": None, "expected_error": {}, "warnings": warnings, + "stored": False} diff --git a/geomgnn/app_flagship.py b/geomgnn/app_flagship.py new file mode 100644 index 0000000000000000000000000000000000000000..76790d4aa6872ed708730004fbb5addfecb84a6b --- /dev/null +++ b/geomgnn/app_flagship.py @@ -0,0 +1,230 @@ +"""Gradio front end of the flagship Space (deploy/GNN_SPACE.md "Flagship Space"; registry flagship_hub, flagship_tiers): three tiers in +one selector (Accurate default, Balanced, Fast), served through geomgnn.flagship (= the paper Space's code path). Endpoints and argument +order as the GNN Space: + + /models () -> served tiers with their reference numbers + /inspect (gds file) -> layer table, layer plot + /prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS + /predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model) -> matrix, CSV, JSON, report + /predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model) -> result dict; + model = accurate (default) | balanced | fast +""" + +from __future__ import annotations + +import json +import time +from pathlib import Path + +from tlsq_infer import __version__ as TLSQ_VERSION +from tlsq_infer.app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _path, _safe, inspect_fn, matrix_table +from tlsq_infer.app_v2 import PURGE_AGE_S, _margin_api, _outdir, _prepare, prepare_fn +from tlsq_infer.layout import LayoutError, prepare +from tlsq_infer.predict import matrix_csv +from tlsq_infer.registry import load_registry + +from . import __version__ +from . import flagship as FS + +ORDER = ["accurate", "balanced", "fast"] + + +def served(reg=None) -> list[str]: + ks = FS.served(reg or load_registry()) + return sorted(ks, key=lambda k: ORDER.index(k) if k in ORDER else len(ORDER)) + + +def resolve(reg, model) -> str: + m = str(model or reg["flagship_hub"]["default_arm"]).strip().lower() + if m not in served(reg): + raise ValueError(f"model {model!r} is not served here; choose one of {served(reg)}") + return m + + +def pct(e) -> str: + """'0.177 % (0.0203 / 0.0250 fF)': every % with its MAE / RMSE.""" + return "n/a" if not e else f"{e['median_pct']:g} % ({e['mae_ff']:g} / {e['rmse_ff']:g} fF)" + + +def numbers(t) -> list[str]: + r = t.get("reference") or {} + rows = [("paper, validation (4,608 layouts)", r.get("val")), ("paper, test in family (4,009)", r.get("test_infamily")), + ("paper, test zero-shot macro (1,878)", r.get("test_zeroshot")), ("paper, held-out family (4,071)", r.get("heldout_family")), + ("paper, held-out family: physics alone", r.get("heldout_family_physics")), + ("paper, validation: " + (r.get("val_same_surrogate") or {}).get("note", ""), r.get("val_same_surrogate")), + ("B5 fresh check (39 new layouts, never seen)", t.get("b5")), + (f"B5: {(t.get('b5_physics') or {}).get('name', 'physics alone')}", t.get("b5_physics"))] + out = [f"| {k} | {pct(v)} |" for k, v in rows if v] + if t.get("time_space"): + out.append(f"| time on this Space (2 vCPU) | {t['time_space']} |") + if t.get("time_x86"): + out.append(f"| time, x86 cluster node | {t['time_x86']} |") + return ["| reference | layout-mean median % (MAE / RMSE fF) |", "|---|---|"] + out + + +def tier_md(reg, k) -> str: + t = FS.tiers(reg)[k] + lines = [f"**{t['title']}**", "", t.get("summary", "")] + if t.get("warning"): + lines += ["", f"> **Warning.** {t['warning']}"] + lines += ["", f"Reference recipe: {(t.get('reference') or {}).get('recipe', '')}.", "", *numbers(t), "", + "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 " + "the same recipe trained on the TRAIN split; B5 is one fresh check (one generated family, 3-4 conductors). Layout-mean median " + "relative error over mutual entries against Q3D, 0.1 fF floor."] + return "\n".join(lines) + + +def _predict(lay, model, substrate, thickness_um): + reg = load_registry() + name = resolve(reg, model) + cap = int(reg["flagship_hub"]["max_conductors"]) + if lay.n_conductors > cap: + raise LayoutError(f"{lay.n_conductors} conductors: this server is capped at {cap}; run the package locally for larger layouts") + fn = lambda: FS.predict(lay, name, reg, substrate=substrate, thickness_um=thickness_um) # noqa: E731 + if lay.n_conductors <= 8: + return fn() + with HEAVY: + return fn() + + +def report_md(res: dict, reg) -> str: + t = res["timing_s"] + st = res.get("prior_stack") + lv = res.get("levels_ppg") or [] + lines = [f"**Tier** `{res['tier']}`: {res['tier_title']}, {res['package_version']}, registry v{res['registry_version']}", + "**Physics** " + (f"thin-film solves at {', '.join(f'{p:g}' for p in lv)} panels per gap, prior `{st['name']}` " + f"(patches {st['patches'] or 'none'}, solver `{st['mode']}`)" if st else "learned prior surrogate (no field solve)"), + "**Substrate** training stack only: Si, eps_r 11.45, 350 µm", + "**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float)) + f" ({t.get('threads')} threads)", + f"**Physical checks** symmetric (max |C - Cᵀ| {res['checks']['symmetric_max_abs']:.2g} fF), off-diagonals negative: " + f"{res['checks']['offdiag_all_negative']}, smallest eigenvalue of the signal block {res['checks']['signal_block_min_eig']:.4g} fF", + "", tier_md(reg, res["tier"])] + if res["warnings"]: + lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]] + lines += ["", f"Self capacitance: {res['self_capacitance']}.", "Planar single-layer layouts only (zero-thickness metal). Nothing you " + f"upload is stored: result files are deleted within {PURGE_AGE_S // 60} minutes."] + return "\n".join(lines) + + +def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, model): + reg = load_registry() + lay = _prepare(f, table, crop, margin_gaps, margin_um, select) + res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness)) + d = _outdir() + (d / "capacitance_fF.csv").write_text(matrix_csv(res)) + (d / "capacitance.json").write_text(json.dumps(res, indent=1, default=float)) + return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res + + +def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)", + custom_eps=11.45, thickness_um=350.0, model="accurate"): + t = time.perf_counter() + lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers), + margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"]) + res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness_um)) + res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t + return res + + +def models_fn(): + reg = load_registry() + h = reg["flagship_hub"] + out = [] + for k in served(reg): + t = FS.tiers(reg)[k] + out.append({"id": k, "title": t["title"], "summary": t.get("summary"), "warning": t.get("warning"), "reference": t.get("reference") or {}, + "b5": t.get("b5"), "b5_physics": t.get("b5_physics"), "time_space": t.get("time_space"), "time_x86": t.get("time_x86"), + "stack": t.get("stack"), "levels_ppg": [float(p) for p in t.get("ppg") or []], "default": k == h["default_arm"], + "max_conductors": int(h["max_conductors"])}) + from . import paper as PA + return out + [{"geomgnn_version": __version__, "tlsq_infer_version": TLSQ_VERSION, "registry_version": reg["version"], + "training_layouts": h.get("training_layouts"), "paper_space": h.get("paper_space"), "startup": dict(PA.STARTUP)}] + + +def header(reg) -> str: + h = reg["flagship_hub"] + ps = h.get("paper_space") + return ("Full **Maxwell capacitance matrix** (fF, self terms included) of a planar superconducting layout from its GDS. Three tiers, " + "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): " + "**Accurate** (default, ~1 s: two thin-film solves + networks), **Balanced** (~0.5 s: a coarser solve pair + networks; about 2x " + "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 " + "families only, worse than the physics alone on new designs). Use **Accurate** for anything that matters. Trained on everything, " + "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 " + f"check on 39 never-seen layouts. To reproduce the paper, use [{ps}](https://huggingface.co/spaces/{ps}). **Private preview**.") + + +def build_app(title: str, examples_dir=None): + import gradio as gr + + reg = load_registry() + subs = list(reg.substrates()) + [CUSTOM] + ms = served(reg) + default = reg["flagship_hub"]["default_arm"] + ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else [] + label = {"accurate": "Accurate (~1 s, default)", "balanced": "Balanced (~0.5 s)", "fast": "Fast (~0.1 s; trained families only)"} + with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo: + gr.Markdown(f"# {title}\n{header(reg)}") + with gr.Accordion("Planar circuits and the layer convention", open=False): + 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.", + "See the tier descriptions below.")) + gr.Markdown("## Tier") + model = gr.Radio([(label.get(k, k), k) for k in ms], value=default, label="model", interactive=True) + model_info = gr.Markdown(tier_md(reg, default)) + model.change(lambda k: tier_md(reg, k), model, model_info, api_visibility="private") + gr.Markdown("## 1. Upload a GDS and assign layer roles") + with gr.Row(): + with gr.Column(scale=1): + gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"]) + if ex: + gr.Examples([[str(p)] for p in ex], inputs=[gds], label="or start from an example") + layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"], + label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True, + value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]]) + layer_plot = gr.Plot(label="layers") + gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)") + with gr.Row(): + with gr.Column(scale=1): + select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="") + with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False): + crop = gr.Checkbox(False, label="crop the ground plane around the conductors") + margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)") + margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider") + prep_btn = gr.Button("Prepare", variant="secondary") + warn_md = gr.Markdown() + summary = gr.JSON(label="prepared layout") + prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)") + prep_plot = gr.Plot(label="prepared layout") + gr.Markdown("## 3. Substrate\nThe models were trained on Si (eps_r 11.45), 350 µm only and are served for that stack only.") + with gr.Row(): + substrate = gr.Dropdown(subs, value=subs[0], label="substrate") + custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')") + thickness = gr.Number(value=350, label="substrate thickness (µm)") + pred_btn = gr.Button("Predict", variant="primary") + gr.Markdown("## 4. Maxwell capacitance matrix (fF)") + with gr.Row(): + with gr.Column(scale=2): + mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False) + with gr.Column(): + csv_f = gr.File(label="CSV") + json_f = gr.File(label="JSON (matrix + metadata)") + report = gr.Markdown() + res_state = gr.State(None) + with gr.Row(visible=False): + a_g = gr.Textbox("1/0") + a_c = gr.Textbox("1/10") + a_m = gr.Number(0) + a_out = gr.JSON() + a_btn = gr.Button("api") + m_btn = gr.Button("models") + m_out = gr.JSON() + gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect") + prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds], + api_name="prepare") + pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model], + [mat, csv_f, json_f, report, res_state], api_name="predict") + 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") + m_btn.click(models_fn, None, m_out, api_name="models") + gr.Markdown(f"geomgnn {__version__}, tlsq_infer {TLSQ_VERSION}. Demo-server caps (not limits of the models): " + f"{LIMITS['max_file_mb']:g} MB per file, {LIMITS['max_polygons']:,} polygons, {reg['flagship_hub']['max_conductors']} conductors " + "+ ground. One request runs at a time; a 66-qubit lattice takes minutes on Accurate.") + return demo diff --git a/geomgnn/app_gnn.py b/geomgnn/app_gnn.py new file mode 100644 index 0000000000000000000000000000000000000000..252b6d3c1bf06a63a247e4868fce76d58816d6c5 --- /dev/null +++ b/geomgnn/app_gnn.py @@ -0,0 +1,311 @@ +"""Gradio front end of the GNN Space (deploy/GNN_SPACE.md): the paper-models Space (tlsq_infer.app_v2) with the geomfast GNN +(geomgnn.api, registry gnn_models) in two tiers, accurate (default, field-solve prior) and fast (learned prior), and E3 / C0 +(tlsq_infer.final, registry final_models) kept for comparison. +Same endpoints and argument order as app_v2: + + /models () -> served models: id, title, recipe, role, accuracy + /inspect (gds file) -> layer table, layer plot + /prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS + /predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model, self_from_rows, + trust_rule) -> matrix table, CSV, JSON, report + /predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model, self_from_rows, trust_rule) + -> result dict. model: "GNN" (accurate, default), "GNN-LP" (fast), "E3", "C0" (the server's radio accepts these ids + only; in process also "accurate" / "gnn" / "fast" / "model" / "physics"). + GNN models: training stack only (Si 11.45, 350 um); self_from_rows and trust_rule apply to E3 / C0 only. +""" + +from __future__ import annotations + +import json +import time +from pathlib import Path + +from tlsq_infer import __version__ as TLSQ_VERSION +from tlsq_infer import final as F +from tlsq_infer.app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _path, _safe, inspect_fn, matrix_table +from tlsq_infer.app_v2 import NETWORK_CAP, PURGE_AGE_S, _margin_api, _outdir, _prepare, prepare_fn +from tlsq_infer.app_v2 import report_md as report_md_v2 +from tlsq_infer.layout import LayoutError, prepare +from tlsq_infer.predict import matrix_csv +from tlsq_infer.registry import load_registry + +from . import __version__ +from . import api as A + +ALIASES = {"gnn": "GNN", "accurate": "GNN", "fast": "GNN-LP", "gnn-lp": "GNN-LP", "learned prior": "GNN-LP", "model": "E3", + "physics": "C0", "physics only": "C0"} + + +def compare_names(reg) -> list[str]: + return [k for k in (reg["gnn_hub"].get("compare_models") or []) if k in (reg.data.get("final_models") or {})] + + +def served(reg=None) -> list[str]: + reg = reg or load_registry() + 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)] + + +def resolve(reg, model) -> str: + m = str(model or reg["gnn_hub"]["default_model"]).strip() + m = ALIASES.get(m.lower(), m) + if m not in served(reg): + old = (reg.data.get("gnn_models") or {}).get(m) or {} + why = f" ({m} is retired: {old['retired']})" if old.get("retired") else "" + raise ValueError(f"model {model!r} is not served here{why}; choose one of {served(reg)} (or accurate / fast / model / physics)") + return m + + +def is_gnn(reg, name) -> bool: + return name in (reg.data.get("gnn_models") or {}) + + +def _predict(lay, model, substrate, thickness_um, self_from_rows=True, trust_rule=None): + reg = load_registry() + name = resolve(reg, model) + if lay.n_conductors > NETWORK_CAP and (is_gnn(reg, name) or F.load_model(name, reg).recipe == "E3"): + raise LayoutError(f"{lay.n_conductors} conductors: the network models are capped at {NETWORK_CAP} on this server; use the " + "physics-only C0 recipe, or run the package locally") + if is_gnn(reg, name): + fn = lambda: A.predict_gnn(lay, name, reg, substrate=substrate, thickness_um=thickness_um) # noqa: E731 + else: + tr = bool(reg["final_defaults"].get("trust_rule", True)) if trust_rule is None else bool(trust_rule) + fn = lambda: F.predict_final(lay, name, substrate=substrate, thickness_um=thickness_um, # noqa: E731 + self_from_rows=bool(self_from_rows), trust_rule=tr) + if lay.n_conductors <= 8: + return fn() + with HEAVY: + return fn() + + +def _fmt(x, nd=3) -> str: + return "n/a" if x is None else (f"{x:.{nd}f}" if abs(x) < 100 else f"{x:.1f}") + + +DEFAULT_ACC_NOTE = ("paper_v4 Table 2: layout-mean median relative error, MAE and RMSE over mutual entries, 0.1 fF floor; frozen " + "before any test set was read and scored once.") + + +def acc_note(m: dict) -> str: + return m.get("accuracy_note") or DEFAULT_ACC_NOTE + + +def extra_acc(m: dict) -> list[str]: + """Signal-self rows, test cohorts, held-out-family rows and the prior-v1 test cohorts of an entry, when the registry has them.""" + out = [] + if m.get("accuracy_signal_self"): + out += ["", "Signal self terms:", ""] + acc_lines({"accuracy": m["accuracy_signal_self"]}) + if m.get("accuracy_test"): + out += ["", m.get("accuracy_test_note", ""), ""] + acc_lines({"accuracy": m["accuracy_test"]}) + if m.get("accuracy_lofo"): + out += ["", m.get("accuracy_lofo_note", ""), ""] + acc_lines({"accuracy": m["accuracy_lofo"]}) + if m.get("accuracy_test_prior_v1") and not m.get("accuracy_test"): + out += ["", m.get("accuracy_test_prior_v1_note", ""), ""] + acc_lines({"accuracy": m["accuracy_test_prior_v1"]}) + return out + + +def acc_lines(m: dict) -> list[str]: + acc = m.get("accuracy") or {} + if not acc: + return ["No test number applies to this model."] + rows = ["| test cohort | layouts | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|---|"] + rows += [f"| {v['set']} | {v.get('layouts', '')} | {_fmt(v['median_pct'])} | {_fmt(v['mae_ff'], 4)} | {_fmt(v['rmse_ff'], 4)} |" + for v in acc.values()] + return rows + + +def lat_test_lines(m: dict) -> list[str]: + lat = m.get("latency_test") or {} + if not lat: + return [] + rows = ["| test latency | layouts | median (s) | p90 (s) | sum (s) |", "|---|---|---|---|---|"] + 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()] + return rows + ["", m.get("latency_test_note", "")] + + +def lat_lines(m: dict) -> list[str]: + lat = m.get("latency") or {} + if not lat: + return [] + rows = ["| latency set | layouts | total median (s) | total p95 (s) | geometry (s) | prior (s) | network (s) |", "|---|---|---|---|---|---|---|"] + rows += [f"| {v['set']} | {v['layouts']} | {v['total_median_s']:.3f} | {v['total_p95_s']:.2f} | {v['geometry_median_s']:.3f} | " + f"{v['prior_median_s']:.3f} | {v['network_median_s']:.4f} |" for v in lat.values()] + machine = next(iter(lat.values())).get("machine", "") + return rows + ["", f"Wall time from the layout file to the matrix, median over layouts; {machine}."] + + +def report_md(res: dict, reg) -> str: + if not is_gnn(reg, res["model"]): + return report_md_v2(res, reg) + m = reg["gnn_models"][res["model"]] + t = res["timing_s"] + pr = res.get("prior") + lp = res.get("learned_prior") + lines = [f"**Model** `{res['model']}`: {res['model_title']} ({res['role']}), {res['package_version']}, registry v{res['registry_version']}", + "**Prior** " + (f"{pr['method']} micro-MoM at {pr['ppg'][0]:g} and {pr['ppg'][1]:g} panels per gap, alpha {pr['alpha']}, " + f"implementation `{pr['prior_impl']}`, patches {pr.get('patches') or 'none'}, solver mode `{pr.get('mode')}`" + if pr else f"learned prior `{lp['name']}` (no field solve; predicts the 1.5 and 3 panels-per-gap priors)" + if lp else "none (solver-free)"), + f"**Substrate** training stack only: Si, eps_r {res['eps_r']:g}, {res['thickness_um']:g} µm", + "**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float)) + + f" ({t.get('threads')} threads)", + f"**Physical checks** symmetric (max |C - Cᵀ| {res['checks']['symmetric_max_abs']:.2g} fF), off-diagonals negative: " + f"{res['checks']['offdiag_all_negative']}, smallest eigenvalue of the signal block {res['checks']['signal_block_min_eig']:.4g} fF", + "", f"**Accuracy of this model.** {acc_note(m)}", ""] + acc_lines(m) + extra_acc(m) + if res["warnings"]: + lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]] + lines += ["", f"Self capacitance: {res['self_capacitance']}.", + "Planar single-layer layouts only (zero-thickness metal). Nothing you upload is stored: result files are deleted " + f"within {PURGE_AGE_S // 60} minutes."] + return "\n".join(lines) + + +def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_rule=True): + reg = load_registry() + lay = _prepare(f, table, crop, margin_gaps, margin_um, select) + res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness), self_from_rows=bool(self_rows), trust_rule=trust_rule) + d = _outdir() + (d / "capacitance_fF.csv").write_text(matrix_csv(res)) + (d / "capacitance.json").write_text(json.dumps(res, indent=1)) + return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res + + +def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)", + custom_eps=11.45, thickness_um=350.0, model="GNN", self_from_rows=True, trust_rule=True): + t = time.perf_counter() + lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers), + margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"]) + res = _predict(lay, model, _eps(substrate, custom_eps), float(thickness_um), self_from_rows=bool(self_from_rows), trust_rule=trust_rule) + res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t + return res + + +def _entry(reg, k) -> dict: + return reg["gnn_models"][k] if is_gnn(reg, k) else reg["final_models"][k] + + +def _lp_public(lp): + """The learned prior without its file path (a {"path": ...} dict would be read as a file by gradio clients).""" + return None if not lp else {"name": lp["name"], "sha256": lp["checkpoint"]["sha256"], "ppg": lp.get("ppg"), "training": lp.get("training")} + + +def models_fn(): + reg = load_registry() + out = [] + for k in served(reg): + m = _entry(reg, k) + d = {"id": k, "title": m["title"], "recipe": "GNN" if is_gnn(reg, k) else m["recipe"], "role": m.get("role"), + "accuracy": m.get("accuracy") or {}, "warning": m.get("warning"), "default": k == reg["gnn_hub"]["default_model"]} + if is_gnn(reg, k): + d |= {"arm": m["arm"], "prior": m.get("prior"), "learned_prior": _lp_public(m.get("learned_prior")), "accuracy_test": m.get("accuracy_test") or {}, + "latency": m.get("latency") or {}, "ensemble": m.get("ensemble"), + "substrate": "training stack only (Si 11.45, 350 um)"} + else: + d["ledger_id"] = m.get("ledger_id") + out.append(d) + return out + [{"geomgnn_version": __version__, "tlsq_infer_version": TLSQ_VERSION, "registry_version": reg["version"], "startup": dict(A.STARTUP)}] + + +def _model_md(reg, k) -> str: + m = _entry(reg, k) + if not is_gnn(reg, k): + acc = (m.get("accuracy") or {}).values() + rows = ["| test set | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|"] + rows += [f"| {v['set']} | {v['median_pct']:.3f} | {v['mae_ff']:.3f} | {v['rmse_ff']:.3f} |" for v in acc] + return "\n".join([f"**{m['title']}** ({m.get('role')}; for comparison). {m.get('summary', '')}", "", + "Test results as reported in paper_v3 (pooled over entries; not the paired paper_v4 cohorts of the GNN rows):", ""] + + (rows if acc else ["n/a"])) + return "\n".join([f"**{m['title']}** ({m.get('role')}). {m.get('summary', '')}"] + ([f"\n> **Warning.** {m['warning']}"] if m.get("warning") else []) + + ["", acc_note(m), "", *acc_lines(m), *extra_acc(m), "", *lat_lines(m)] + ([m["latency_note"]] if m.get("latency_note") else []) + + ["", *lat_test_lines(m)]) + + +def header(reg) -> str: + h = reg["gnn_hub"] + return (f"Zero-shot **Maxwell capacitance matrix** of a planar superconducting layout from its GDS. The geomfast GNN, a " + f"message-passing network on the full geometric embedding that corrects a two-resolution thin-film MoM prior and predicts " + f"the complete N x N matrix (self terms included), in two tiers: **Accurate** (default) solves the prior, **Fast** predicts " + f"it with a learned surrogate (no field solve: 6-10x faster, 3.6x the median error in family). **Use Fast only for the " + f"component families it was trained on** (listed under the model); on a component family it never saw it is worse than " + f"the physics prior alone, so **for a new component type use Accurate** (numbers under each model). " + f"{h.get('comparison_note', '')} **Private preview** (paper in preparation).") + + +def build_app(title: str, examples_dir=None): + import gradio as gr + + reg = load_registry() + subs = list(reg.substrates()) + [CUSTOM] + ms = served(reg) + default = reg["gnn_hub"]["default_model"] + ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else [] + with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo: + gr.Markdown(f"# {title}\n{header(reg)}") + with gr.Accordion("Planar circuits and the layer convention", open=False): + 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.", + "See the model descriptions below.")) + gr.Markdown("## Model") + model = gr.Radio([(_entry(reg, k)["title"], k) for k in ms], value=default, label="model") + model_info = gr.Markdown(_model_md(reg, default)) + model.change(lambda k: _model_md(reg, k), model, model_info, api_visibility="private") + gr.Markdown("## 1. Upload a GDS and assign layer roles") + with gr.Row(): + with gr.Column(scale=1): + gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"]) + if ex: + gr.Examples([[str(p)] for p in ex], inputs=[gds], label="or start from an example") + layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"], + label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True, + value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]]) + layer_plot = gr.Plot(label="layers") + gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)") + with gr.Row(): + with gr.Column(scale=1): + select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="") + with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False): + crop = gr.Checkbox(False, label="crop the ground plane around the conductors") + margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)") + margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider") + prep_btn = gr.Button("Prepare", variant="secondary") + warn_md = gr.Markdown() + summary = gr.JSON(label="prepared layout") + prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)") + prep_plot = gr.Plot(label="prepared layout") + gr.Markdown("## 3. Substrate\nThe GNN models are served for their training stack only (Si, eps_r 11.45, 350 µm); other substrates " + "are available with E3 / C0 (half-space factor (1+eps)/(1+11.45)).") + with gr.Row(): + substrate = gr.Dropdown(subs, value=subs[0], label="substrate") + custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')") + thickness = gr.Number(value=350, label="substrate thickness (µm)") + self_rows = gr.Checkbox(True, label="E3 / C0: self-capacitance from row sums (the GNN predicts its self terms)") + trust_cb = gr.Checkbox(bool(reg["final_defaults"].get("trust_rule", True)), + label="E3: apply the trust rule (default on: fall back to physics-only C0 when the networks disagree)") + pred_btn = gr.Button("Predict", variant="primary") + gr.Markdown("## 4. Maxwell capacitance matrix (fF)") + with gr.Row(): + with gr.Column(scale=2): + mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False) + with gr.Column(): + csv_f = gr.File(label="CSV") + json_f = gr.File(label="JSON (matrix + metadata)") + report = gr.Markdown() + res_state = gr.State(None) + with gr.Row(visible=False): + a_g = gr.Textbox("1/0") + a_c = gr.Textbox("1/10") + a_m = gr.Number(0) + a_out = gr.JSON() + a_btn = gr.Button("api") + m_btn = gr.Button("models") + m_out = gr.JSON() + gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect") + prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds], + api_name="prepare") + pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_cb], + [mat, csv_f, json_f, report, res_state], api_name="predict") + 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, + api_name="predict_gds") + m_btn.click(models_fn, None, m_out, api_name="models") + gr.Markdown(f"geomgnn {__version__}, tlsq_infer {TLSQ_VERSION}. Demo-server caps (not limits of the models): " + f"{LIMITS['max_file_mb']:g} MB per file, {LIMITS['max_polygons']:,} polygons, {LIMITS['max_conductors']} conductors + " + f"ground, {NETWORK_CAP} for the network models (GNN, E3).") + return demo diff --git a/geomgnn/app_paper.py b/geomgnn/app_paper.py new file mode 100644 index 0000000000000000000000000000000000000000..a00aa38b8239c35da8ab287f2a4137178bf0d8ef --- /dev/null +++ b/geomgnn/app_paper.py @@ -0,0 +1,250 @@ +"""Gradio front end of the paper Space (deploy/GNN_SPACE.md "Paper Space"; registry paper_hub, paper_arms): one selector over every +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: + + /models () -> served arms: id, paper name, route, numbers + /inspect (gds file) -> layer table, layer plot + /prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS + /predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, arm, self_from_rows, + trust_rule) -> matrix table, CSV, JSON, report + /predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, arm, self_from_rows, trust_rule) + -> result dict; arm = an id of /models (default "model") +""" + +from __future__ import annotations + +import json +import time +from pathlib import Path + +from tlsq_infer import __version__ as TLSQ_VERSION +from tlsq_infer.app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _path, _safe, inspect_fn, matrix_table +from tlsq_infer.app_v2 import NETWORK_CAP, PURGE_AGE_S, _margin_api, _outdir, _prepare, prepare_fn +from tlsq_infer.app_v2 import report_md as report_md_v2 +from tlsq_infer.layout import LayoutError, prepare +from tlsq_infer.predict import matrix_csv +from tlsq_infer.registry import load_registry + +from . import __version__ +from . import paper as PA + +ROUTES = ["(d) good physics", "(c) cheap physics", "(b) learned physics", "(a) no physics", "baseline"] + + +def served(reg=None) -> list[str]: + reg = reg or load_registry() + ks = [k for k in PA.live(reg) if PA.available(k, reg)] + return sorted(ks, key=lambda k: (ROUTES.index(PA.arms(reg)[k].get("route", "baseline")), list(PA.arms(reg)).index(k))) + + +def resolve(reg, arm) -> str: + a = str(arm or reg["paper_hub"]["default_arm"]).strip() + if a not in served(reg): + raise ValueError(f"arm {arm!r} is not served here; choose one of {served(reg)}") + return a + + +def cap(reg, a) -> int: + """Signal conductors per layout on this Space: the prior-generation network keeps the GNN Space's network cap (dense pair + tensors); every other arm runs up to paper_hub.max_conductors (the nine-qubit lattice of the paper).""" + return NETWORK_CAP if (a.get("kind") == "final" and a.get("final_model") == "E3") else int(reg["paper_hub"]["max_conductors"]) + + +def _predict(lay, arm, substrate, thickness_um, self_from_rows=True, trust_rule=None): + reg = load_registry() + name = resolve(reg, arm) + a = PA.arms(reg)[name] + if lay.n_conductors > cap(reg, a): + raise LayoutError(f"{lay.n_conductors} conductors: the arm '{name}' is capped at {cap(reg, a)} on this server; run the package " + "locally for larger layouts") + fn = lambda: PA.predict(lay, name, reg, substrate=substrate, thickness_um=thickness_um, self_from_rows=self_from_rows, # noqa: E731 + trust_rule=trust_rule) + if lay.n_conductors <= 8: + return fn() + with HEAVY: + return fn() + + +def pct(e) -> str: + """'0.177 % (0.0203 / 0.0250 fF)': every % with its MAE / RMSE.""" + return "n/a" if not e else f"{e['median_pct']:g} % ({e['mae_ff']:g} / {e['rmse_ff']:g} fF)" + + +def numbers(a) -> list[str]: + p = a.get("paper") or {} + rows = [("validation (4,608 layouts)", p.get("val")), ("test, in family (4,009)", p.get("test_infamily")), + ("test, zero-shot macro (1,878)", p.get("test_zeroshot")), ("held-out family (4,071 validation layouts)", p.get("heldout_family"))] + out = [f"| {k} | {pct(v)}{' — ' + v['note'] if v and v.get('note') else ''} |" for k, v in rows if v] + if p.get("time"): + out.append(f"| time per layout (median / 90th percentile) | {p['time']} |") + if p.get("note"): + out.append(f"| note | {p['note']} |") + return (["| paper | layout-mean median % (MAE / RMSE fF) |", "|---|---|"] + out) if out else [] + + +def arm_md(reg, k) -> str: + a = PA.arms(reg)[k] + tabs = ", ".join((a.get("paper") or {}).get("tables") or []) + lines = [f"**{a['paper_name']}** ({a.get('route')}; paper: {tabs}). {a.get('summary', '')}"] + if a.get("warning"): + lines.append(f"\n> **Warning.** {a['warning']}") + lines += ["", *numbers(a), "", "Layout-mean median relative error over mutual entries against Q3D, 0.1 fF floor; MAE / RMSE in fF. " + "Test = the paper's test benchmark, read once; validation = development data."] + if a["kind"] == "final": + m = reg["final_models"][a["final_model"]] + acc = (m.get("accuracy") or {}).values() + if acc: + lines += ["", "Prior-generation evaluation (pooled over entries, not the layout-mean of the table above):", "", + "| set | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|"] + lines += [f"| {v['set']} | {v['median_pct']:.3f} | {v['mae_ff']:.3f} | {v['rmse_ff']:.3f} |" for v in acc] + return "\n".join(lines) + + +def report_md(res: dict, reg) -> str: + a = PA.arms(reg)[res["arm"]] + if a["kind"] == "final": + return report_md_v2(res, reg) + t = res["timing_s"] + st = res.get("prior_stack") + lv = res.get("levels_ppg") or [] + lines = [f"**Arm** `{res['arm']}`: {res['paper_name']} ({res['route']}), {res['package_version']}, registry v{res['registry_version']}", + "**Physics** " + (f"thin-film solves at {', '.join(f'{p:g}' for p in lv)} panels per gap, prior `{st['name']}` " + f"(patches {st['patches'] or 'none'}, solver `{st['mode']}`)" if st else + "learned prior surrogate (no field solve)" if res.get("learned_prior_sha256") else "none (solver-free)"), + "**Substrate** training stack only: Si, eps_r 11.45, 350 µm", + "**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float)) + + f" ({t.get('threads')} threads)", + f"**Physical checks** symmetric (max |C - Cᵀ| {res['checks']['symmetric_max_abs']:.2g} fF), off-diagonals negative: " + f"{res['checks']['offdiag_all_negative']}, smallest eigenvalue of the signal block {res['checks']['signal_block_min_eig']:.4g} fF", + "", arm_md(reg, res["arm"])] + if res["warnings"]: + lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]] + lines += ["", f"Self capacitance: {res['self_capacitance']}.", "Planar single-layer layouts only (zero-thickness metal). Nothing you " + f"upload is stored: result files are deleted within {PURGE_AGE_S // 60} minutes."] + return "\n".join(lines) + + +def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, arm, self_rows, trust_rule=True): + reg = load_registry() + lay = _prepare(f, table, crop, margin_gaps, margin_um, select) + res = _predict(lay, arm, _eps(substrate, custom_eps), float(thickness), self_from_rows=bool(self_rows), trust_rule=trust_rule) + d = _outdir() + (d / "capacitance_fF.csv").write_text(matrix_csv(res)) + (d / "capacitance.json").write_text(json.dumps(res, indent=1, default=float)) + return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res + + +def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)", + custom_eps=11.45, thickness_um=350.0, arm="model", self_from_rows=True, trust_rule=True): + t = time.perf_counter() + lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers), + margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"]) + res = _predict(lay, arm, _eps(substrate, custom_eps), float(thickness_um), self_from_rows=bool(self_from_rows), trust_rule=trust_rule) + res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t + return res + + +def models_fn(): + reg = load_registry() + out = [] + for k in served(reg): + a = PA.arms(reg)[k] + out.append({"id": k, "paper_name": a["paper_name"], "title": a["title"], "route": a.get("route"), "kind": a["kind"], + "summary": a.get("summary"), "warning": a.get("warning"), "paper": a.get("paper") or {}, + "stack": a.get("stack"), "levels_ppg": PA.levels_of(a), "default": k == reg["paper_hub"]["default_arm"], + "max_conductors": cap(reg, a)}) + return out + [{"geomgnn_version": __version__, "tlsq_infer_version": TLSQ_VERSION, "registry_version": reg["version"], + "checkpoints_repo": reg["paper_hub"].get("checkpoints_repo"), "startup": dict(PA.STARTUP)}] + + +def header(reg) -> str: + h = reg["paper_hub"] + ck = h.get("checkpoints_repo") + return ("Full **Maxwell capacitance matrix** of a planar superconducting layout from its GDS, with **every route of the paper that " + "runs on a CPU**: the model, the fast model, the solver-free network, the cheap-physics networks, the mesh ladder of single " + "solves and Richardson pairs, the extrapolated physics alone, the ablations and controls, the solver modes and the " + "prior-generation baseline. Each arm shows its paper numbers (layout-mean median %, MAE / RMSE fF). Every other checkpoint the " + f"paper reports (k-curves, learning curves, leave-one-family-out folds, the solver-free series, ...) with its training and " + f"scoring scripts is in the model repository [{ck}](https://huggingface.co/{ck}). **Private preview** (paper in preparation).") + + +def build_app(title: str, examples_dir=None): + import gradio as gr + + reg = load_registry() + subs = list(reg.substrates()) + [CUSTOM] + ms = served(reg) + default = reg["paper_hub"]["default_arm"] + ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else [] + choices = [(f"{PA.arms(reg)[k].get('route')}: {PA.arms(reg)[k]['paper_name']}", k) for k in ms] + with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo: + gr.Markdown(f"# {title}\n{header(reg)}") + with gr.Accordion("Planar circuits and the layer convention", open=False): + 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.", + "See the arm descriptions below.")) + gr.Markdown("## Arm (route of the paper)") + model = gr.Dropdown(choices, value=default, label="arm", interactive=True) + model_info = gr.Markdown(arm_md(reg, default)) + model.change(lambda k: arm_md(reg, k), model, model_info, api_visibility="private") + gr.Markdown("## 1. Upload a GDS and assign layer roles") + with gr.Row(): + with gr.Column(scale=1): + gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"]) + if ex: + 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)") + layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"], + label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True, + value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]]) + layer_plot = gr.Plot(label="layers") + gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)") + with gr.Row(): + with gr.Column(scale=1): + select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="") + with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False): + crop = gr.Checkbox(False, label="crop the ground plane around the conductors") + margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)") + margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider") + prep_btn = gr.Button("Prepare", variant="secondary") + warn_md = gr.Markdown() + summary = gr.JSON(label="prepared layout") + prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)") + prep_plot = gr.Plot(label="prepared layout") + gr.Markdown("## 3. Substrate\nThe paper's networks and priors are served for their training stack only (Si, eps_r 11.45, 350 µm); " + "the prior-generation baseline also takes other substrates (half-space factor (1+eps)/(1+11.45)).") + with gr.Row(): + substrate = gr.Dropdown(subs, value=subs[0], label="substrate") + custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')") + thickness = gr.Number(value=350, label="substrate thickness (µm)") + self_rows = gr.Checkbox(True, label="prior-generation baseline: self-capacitance from row sums") + trust_cb = gr.Checkbox(bool(reg["final_defaults"].get("trust_rule", True)), + label="prior-generation model: apply its trust rule (default on)") + pred_btn = gr.Button("Predict", variant="primary") + gr.Markdown("## 4. Maxwell capacitance matrix (fF)") + with gr.Row(): + with gr.Column(scale=2): + mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False) + with gr.Column(): + csv_f = gr.File(label="CSV") + json_f = gr.File(label="JSON (matrix + metadata)") + report = gr.Markdown() + res_state = gr.State(None) + with gr.Row(visible=False): + a_g = gr.Textbox("1/0") + a_c = gr.Textbox("1/10") + a_m = gr.Number(0) + a_out = gr.JSON() + a_btn = gr.Button("api") + m_btn = gr.Button("models") + m_out = gr.JSON() + gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect") + prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds], + api_name="prepare") + pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_cb], + [mat, csv_f, json_f, report, res_state], api_name="predict") + 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, + api_name="predict_gds") + m_btn.click(models_fn, None, m_out, api_name="models") + gr.Markdown(f"geomgnn {__version__}, tlsq_infer {TLSQ_VERSION}. Demo-server caps (not limits of the models): " + f"{LIMITS['max_file_mb']:g} MB per file, {LIMITS['max_polygons']:,} polygons, {reg['paper_hub']['max_conductors']} conductors " + f"+ ground ({NETWORK_CAP} for the prior-generation network). One request runs at a time; the nine-qubit lattice takes minutes " + "on the accurate arms.") + return demo diff --git a/geomgnn/fastmom.py b/geomgnn/fastmom.py new file mode 100644 index 0000000000000000000000000000000000000000..6500ae97672071def8cd0ccf599369d544f30636 --- /dev/null +++ b/geomgnn/fastmom.py @@ -0,0 +1,147 @@ +"""Vectorised drop-in replacements for the H-matrix kernel of tlsq.embed.proxy (same mathematics, no per-row scipy +sparse slicing, no Python loop over image charges or ACA ranks). install() patches the loaded module in place.""" +import numpy as np + +from ._tlsq.embed import proxy as P + +_orig = {} + + +def _images(eps_r): + P._image_correction(np.zeros(1), eps_r) + key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r)) + w, D = P._IMG_CACHE[key][2:] + return np.asarray(w, float), np.asarray(D, float) ** 2 + + +def _kernel_init(self, c, wh, eps_r, perm): + _orig["Kernel.__init__"](self, c, wh, eps_r, perm) + N = self.N.tocsr() + N.sort_indices() + self.indptr, self.indices, self.data = N.indptr, N.indices, N.data + self.w, self.D2 = _images(eps_r) if self.img else (None, None) + + +def _corr(self, d): + return np.sqrt((d * d)[..., None] + self.D2) ** -1 @ self.w + + +def _row(self, i, b0, b1): + d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2) + r = 1.0 / d + if self.img: + r += _corr(self, d) + s, e = self.indptr[i], self.indptr[i + 1] + if e > s: + ix = self.indices[s:e] + lo, hi = np.searchsorted(ix, (b0, b1)) + if hi > lo: + r[ix[lo:hi] - b0] += self.data[s + lo:s + hi] + return r / self.k + + +def _block(self, a0, a1, b0, b1): + 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) + corr = _corr(self, d) if self.img else None + if a0 == b0: + d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0 + A = 1.0 / d + if corr is not None: + A += corr + s, e = self.indptr[a0], self.indptr[a1] + if e > s: + ix = self.indices[s:e] + rr = np.repeat(np.arange(a1 - a0), np.diff(self.indptr[a0:a1 + 1])) + m = (ix >= b0) & (ix < b1) + np.add.at(A, (rr[m], ix[m] - b0), self.data[s:e][m]) + return A / self.k + + +def _aca(K, a0, a1, b0, b1, eps, max_rank): + m, n = a1 - a0, b1 - b0 + R = min(m, n, max_rank) + U, V = np.empty((m, R)), np.empty((n, R)) + used_r, used_c = np.zeros(m, bool), np.zeros(n, bool) + i, norm2, k = 0, 0.0, 0 + for _ in range(R): + r = K.row(a0 + i, b0, b1) + if k: + r -= V[:, :k] @ U[i, :k] + rm = np.where(used_c, 0.0, np.abs(r)) + j = int(np.argmax(rm)) + if rm[j] == 0.0: + break + v = r / r[j] + u = K.row(b0 + j, a0, a1) + if k: + u -= U[:, :k] @ V[j, :k] + used_r[i], used_c[j] = True, True + nu, nv = float(u @ u), float(v @ v) + norm2 += nu * nv + (2 * float((U[:, :k].T @ u) @ (V[:, :k].T @ v)) if k else 0.0) + U[:, k], V[:, k] = u, v + k += 1 + if nu * nv <= eps**2 * norm2: + break + um = np.where(used_r, 0.0, np.abs(u)) + i = int(np.argmax(um)) + if um[i] == 0.0: + break + else: + return None + return U[:, :k].copy(), V[:, :k].copy() + + +def _image_correction(d, eps_r, exact=False): + if exact or P.SUBSTRATE["model"] == "halfspace": + return _orig["image"](d, eps_r, exact) + _orig["image"](np.zeros(1), eps_r) + key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r)) + du, T = P._IMG_CACHE[key][:2] + u = np.asarray(d, float) / du + j = np.minimum(u.astype(np.int64), len(T) - 2) + f = u - j + t0 = T[j] + return t0 + f * (T[j + 1] - t0) + + +REFINE = dict(steps=6, tol=1e-13) + + +def _chol_solve(A, B): + from scipy.linalg import cho_factor, cho_solve + from scipy.linalg.blas import dsymm + + try: + cf = cho_factor(A.astype(np.float32, order="F"), lower=True, overwrite_a=True, check_finite=False) + except np.linalg.LinAlgError: + return _orig["chol_solve"](A, B) + B = np.asfortranarray(B, dtype=float) + X = cho_solve(cf, B.astype(np.float32), check_finite=False).astype(float) + bn = np.linalg.norm(B, axis=0) + for _ in range(REFINE["steps"]): + R = B - dsymm(1.0, A, X, side=0, lower=1) + if (np.linalg.norm(R, axis=0) <= REFINE["tol"] * bn).all(): + return X + X += cho_solve(cf, R.astype(np.float32), check_finite=False) + return _orig["chol_solve"](A, B) + + +def install(dense_max=None, mixed=False): + if not _orig: + _orig["Kernel.__init__"] = P._Kernel.__init__ + _orig.update(row=P._Kernel.row, block=P._Kernel.block, aca=P._aca, dense_max=P.DENSE_MAX, image=P._image_correction, + chol_solve=P._chol_solve) + P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = _kernel_init, _row, _block, _aca + P._image_correction = _image_correction + P._chol_solve = _chol_solve if mixed else _orig["chol_solve"] + if dense_max is not None: + P.DENSE_MAX = dense_max + + +def uninstall(): + if _orig: + P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = (_orig["Kernel.__init__"], _orig["row"], _orig["block"], + _orig["aca"]) + P._image_correction, P._chol_solve = _orig["image"], _orig["chol_solve"] + P.DENSE_MAX = _orig["dense_max"] + _orig.clear() diff --git a/geomgnn/fastprior_pkg.py b/geomgnn/fastprior_pkg.py new file mode 100644 index 0000000000000000000000000000000000000000..ef4071d2fb072d85ad9d03530f2bb028e7707b33 --- /dev/null +++ b/geomgnn/fastprior_pkg.py @@ -0,0 +1,1291 @@ +"""fastprior_pkg: switchable speed components for the FROZEN physical prior of the P2d GNN (micro_mom_loc at ppg 1.5 +and 3, alpha 0.49685219816506904), installed by monkeypatching the loaded frozen vendor modules (never edited). + + import fastprior_pkg as fp + fp.install(mode="exact") # fv + parallel + hmat_vec (+ gpu if CUDA) (+ mixed if gated) + fp.install(mode="exact_compiled") # exact + numba H-matrix operator (closer to the dense solve; VAL-tested, A5) + fp.install(mode="certified") # exact + compiled + Rayleigh-Ritz solves with an error estimate (fp.certificates()) + fp.install(components=["fv", "compiled"]) # explicit set + fp.uninstall() + +Components (protocol and gates: agents/fastprior_pkg/PROTOCOL.md; sources cited per section, code reused verbatim +where possible, only module bindings changed): + fv value-preserving rejection-tail fix of the windowed solve (rejtail; bit-identical) + parallel forked window workers (the vendor's micro_mom(workers=k)); threads per worker limited (bit-identical) + hmat_vec the frozen H-matrix PCG (> 12,000 panels) with a vectorised kernel, frozen tolerances (<= 1e-6) + compiled numba H-matrix operator for the H-matrix PCG path: closer to the exact dense solve than the frozen + H-matrix, but up to 1.4e-5 from it (fails strict parity; opt-in, used by certified) + gpu dense assembly + Cholesky on CUDA (only when torch.cuda is available) + mixed float32 Cholesky + float64 iterative refinement (only if gated, see MIXED_GATED) + ritz Rayleigh-Ritz window / local solves (template start, tau 1e-3, dense <= 3,000 panels), certified +Parallel windows fork (the vendor's pool); on macOS they are always off (Accelerate is not fork-safe) and the prior +runs serially. Environment: TLSQ_PRIOR_CPUS (CPUs; default cgroup / affinity aware), TLSQ_PRIOR_WORKERS (window workers; default = +CPUs), TLSQ_PRIOR_MODE (default mode of install() without arguments). +Value hook: install(..., value_patch=fn) with fn(F, P, FL) -> undo callable (e.g. a prior-v2 patch once it passes).""" +from __future__ import annotations + +import contextlib +import inspect +import json +import os +import sys +import tempfile +import time + +import numpy as np +from scipy.linalg import cho_factor, cho_solve, eigh + +FROZEN_DENSE_MAX = 12000 # the frozen proxy.DENSE_MAX: decides which attempts the frozen code could not solve (fv) +RITZ_CAL = 1.825367354114339 # varritz S0 calibration of the block-Jacobi estimate (s0_choice.json) +MIXED_GATED = True # PROTOCOL.md G3 passed on Discovery x86 (A5): prior <= 4.4e-10, final matrix <= 4.5e-7 +COMPONENTS = ("fv", "parallel", "hmat_vec", "compiled", "gpu", "mixed", "ritz") +CFG = dict(device="cuda", gpu_min=2000, gpu_dense_max=60000, rows=4096, ritz_tau=1e-3, ritz_maxit=12, ritz_dense_n=3000, + ritz_start="tmpl", refine_steps=6, refine_tol=1e-13) +STATE = dict(mode=None, components=(), cpus=1, workers=1, gpu=False, value_patch=None, target=None, parallel_off=None) +F = P = FL = None # bound vendor modules: fastprior, proxy, fastprior_loc +_ORIG: dict = {} +_UNDO: list = [] +_PARENT = [0] +_CHILD = [False] +_TP: list = [] +STATS = dict(compiled_calls=0) # per-process counters +LOG: list = [] # Ritz / exact solve records of the running prior call (certified mode) +CERTS: list = [] # one certificate per micro_mom_loc call (certified mode) +_CERT_FILE = [None] + + +# ------------------------------------------------------------------------------------------------ binding / CPUs + + +def bind(target="auto"): + """Bind the frozen vendor modules: 'stage' (tlsq.embed of the geomfast stage), 'geomgnn' (the GNN Space bundle, + geomgnn._tlsq.embed), or a tuple (fastprior, proxy, fastprior_loc). 'auto': whichever is loaded, else stage.""" + global F, P, FL + if isinstance(target, tuple): + F, P, FL = target + STATE["target"] = "modules" + return F, P, FL + if target == "auto": + target = "geomgnn" if "geomgnn._tlsq.embed.fastprior" in sys.modules else "stage" + if target == "stage" and "tlsq.embed.fastprior" not in sys.modules: + try: + import tlsq.embed.fastprior # noqa: F401 + except ImportError: + target = "geomgnn" + if target == "geomgnn": + from geomgnn._tlsq.embed import fastprior as F_, fastprior_loc as FL_, proxy as P_ + else: + from tlsq.embed import fastprior as F_, fastprior_loc as FL_, proxy as P_ + F, P, FL = F_, P_, FL_ + STATE["target"] = target + return F, P, FL + + +def _cgroup_cpus(): + """CPU quota of this process's cgroup (v2 cpu.max along the cgroup path, or v1 cfs quota / period); None if none.""" + best = None + try: + lines = open("/proc/self/cgroup").read().splitlines() + except OSError: + lines = [] + paths = [] + for ln in lines: + parts = ln.split(":", 2) + if len(parts) == 3 and parts[0] == "0" and parts[1] == "": + rel = parts[2].strip("/") + segs = rel.split("/") if rel else [] + for k in range(len(segs), -1, -1): + paths.append(os.path.join("/sys/fs/cgroup", *segs[:k], "cpu.max")) + paths.append("/sys/fs/cgroup/cpu.max") + for p in paths: + try: + q, per = open(p).read().split()[:2] + except (OSError, ValueError): + continue + if q != "max" and float(per) > 0: + v = float(q) / float(per) + best = v if best is None else min(best, v) + for d in ("/sys/fs/cgroup/cpu", "/sys/fs/cgroup/cpu,cpuacct"): + try: + q = int(open(f"{d}/cpu.cfs_quota_us").read()) + per = int(open(f"{d}/cpu.cfs_period_us").read()) + except (OSError, ValueError): + continue + if q > 0 and per > 0: + best = q / per if best is None else min(best, q / per) + return best + + +def detect_cpus() -> int: + """CPUs this process may use: TLSQ_PRIOR_CPUS, else min(cgroup quota, affinity, os.cpu_count()).""" + env = os.environ.get("TLSQ_PRIOR_CPUS") + if env: + return max(1, int(env)) + cands = [os.cpu_count() or 1] + q = _cgroup_cpus() + if q: + cands.append(max(1, int(q))) + if hasattr(os, "sched_getaffinity"): + cands.append(len(os.sched_getaffinity(0))) + return max(1, min(cands)) + + +def _set_threads(n: int) -> None: + """Limit BLAS / OpenMP (threadpoolctl), numba and the vendor's thread pool to n threads in this process.""" + try: + from threadpoolctl import threadpool_limits + _TP[:] = [threadpool_limits(n)] + except Exception: # noqa: BLE001 (no threadpoolctl / Accelerate: env vars at process start decide) + pass + if P is not None: + P.NUM_THREADS = n + try: + import numba + numba.set_num_threads(max(1, min(n, numba.config.NUMBA_NUM_THREADS))) + except Exception: # noqa: BLE001 + pass + + +# ------------------------------------------------------------------------------------------------ fv +# deploy/geomgnn/src/geomgnn/rejtail_fv.py (= scripts/rejtail/rt.py policy fv), keep_last_solved=True (frozen stage), +# with the "doomed" test on the frozen DENSE_MAX (a GPU-raised DENSE_MAX must not change which attempts solve). + + +def _fv_solve(c, wh, o, metals, cols, eps_r): + t = time.perf_counter() + t_asm = 0.0 + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + t_asm = time.perf_counter() - t + Bm = (o[:, None] == cols[None, :]).astype(float) + Q = F._sym_solve(A, Bm) + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols] + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1), t_asm + + +def fv_window_solve(metals, n_core, h, budget, eps_r, all_rhs, levels=()): + if all_rhs: + return _ORIG["window_solve"](metals, n_core, h, budget, eps_r, all_rhs, levels) + cols = np.arange(1, 1 + n_core) + t_mesh = t_asm = t_sol = 0.0 + hh, cap, refined, majority = h, budget, 0, False + hist = [] + while True: + t = time.perf_counter() + if not refined: + c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels) + elif levels: + c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority) + else: + c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority) + t_mesh += time.perf_counter() - t + if not np.all(np.bincount(o, minlength=len(metals)) > 0): + hist.append(("doomed",) if len(c) > FROZEN_DENSE_MAX else ("mesh", c, wh, o)) + ok = False + else: + t = time.perf_counter() + try: + R, ta = _fv_solve(c, wh, o, metals, cols, eps_r) + t_asm += ta + hist.append(("R", R)) + ok = True + except np.linalg.LinAlgError: + hist.append(("err",)) + ok = False + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < F.LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + if ok: + R = hist[-1][1] + else: + R = None + t = time.perf_counter() + for e in reversed(hist): + if e[0] == "R": + R = e[1] + break + if e[0] == "mesh": + try: + R = _fv_solve(e[1], e[2], e[3], metals, cols, eps_r)[0] + break + except np.linalg.LinAlgError: + continue + t_sol += time.perf_counter() - t + assert R is not None, "no attempt solved (the frozen code raises here too)" + return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, + "assemble_s": t_asm, "solve_s": t_sol} + + +# ------------------------------------------------------------------------------------------------ parallel windows +# The vendor's micro_mom(workers=k) (ProcessPoolExecutor, fork); windows inherit the layout through fastprior._LITE. + + +def _micro_mom(*args, **kw): + ba = _ORIG["micro_mom_sig"].bind(*args, **kw) + if sys.platform == "darwin": # never fork on macOS, whatever the caller asked for + ba.arguments["workers"] = 1 + if STATE["workers"] > 1 and ba.arguments.get("workers", 1) <= 1: + ba.arguments["workers"] = STATE["workers"] + _PARENT[0] = os.getpid() + return _ORIG["micro_mom"](*ba.args, **ba.kwargs) + + +def _child_setup(): + """First window job in a forked worker: threads per worker = CPUs // workers; CUDA is never used after fork.""" + if _CHILD[0] or os.getpid() == _PARENT[0]: + return + _CHILD[0] = True + nw = max(1, min(STATE["workers"], len(F._LITE.get("cores", [0])))) + _set_threads(max(1, STATE["cpus"] // nw)) + if STATE["gpu"]: + STATE["gpu"] = False + P.DENSE_MAX = FROZEN_DENSE_MAX + + +def _lite_job(k): + _child_setup() + if _CHILD[0] and _CERT_FILE[0]: + i0 = len(LOG) + out = _ORIG["lite_job"](k) + recs = LOG[i0:] + for r in recs: + r["window"] = int(k) + with open(_CERT_FILE[0], "a") as f: + f.write("".join(json.dumps(_jsonable(r)) + "\n" for r in recs)) + return out + i0 = len(LOG) + out = _ORIG["lite_job"](k) + for r in LOG[i0:]: + r["window"] = int(k) + return out + + +def _jsonable(r): + 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()} + + +# ------------------------------------------------------------------------------------------------ compiled operator +# scripts/varritz/hmat.py and scripts/varritz/fastop.py (numba H-matrix of the far kernel, exact near field, FFT image +# term, block-Jacobi leaf factors on the frozen cluster tree); bodies verbatim, `P` bound late. + +os.environ.setdefault("NUMBA_THREADING_LAYER", "workqueue") # fork-safe; GNU OpenMP aborts in forked window workers +try: + import numba as nb + nb.config.THREADING_LAYER = os.environ["NUMBA_THREADING_LAYER"] +except ImportError: # compiled / ritz unavailable without numba + nb = None + +if nb is not None: + @nb.njit(fastmath=True, cache=True) + def _krow(x, y, i, b0, b1, out): + for j in range(b0, b1): + dx, dy = x[i] - x[j], y[i] - y[j] + r2 = dx * dx + dy * dy + out[j - b0] = 1.0 / np.sqrt(r2) if r2 > 0 else 0.0 + + @nb.njit(fastmath=True, cache=True) + def _aca(x, y, a0, a1, b0, b1, eps, kmax, U, V): + m, n = a1 - a0, b1 - b0 + used_r = np.zeros(m, np.bool_) + used_c = np.zeros(n, np.bool_) + r = np.empty(n) + u = np.empty(m) + norm2 = 0.0 + i = 0 + k = 0 + while k < min(m, n, kmax): + _krow(x, y, a0 + i, b0, b1, r) + for q in range(k): + ui = U[i, q] + for j in range(n): + r[j] -= ui * V[j, q] + used_r[i] = True + jbest, vbest = -1, 0.0 + for j in range(n): + if not used_c[j] and abs(r[j]) > vbest: + vbest, jbest = abs(r[j]), j + if jbest < 0 or vbest == 0.0: + return k + piv = r[jbest] + for j in range(n): + V[j, k] = r[j] / piv + _krow(x, y, b0 + jbest, a0, a1, u) + for q in range(k): + vj = V[jbest, q] + for ii in range(m): + u[ii] -= vj * U[ii, q] + for ii in range(m): + U[ii, k] = u[ii] + used_c[jbest] = True + nu, nv = 0.0, 0.0 + for ii in range(m): + nu += u[ii] * u[ii] + for j in range(n): + nv += V[j, k] * V[j, k] + cross = 0.0 + for q in range(k): + su, sv = 0.0, 0.0 + for ii in range(m): + su += u[ii] * U[ii, q] + for j in range(n): + sv += V[j, k] * V[j, q] + cross += su * sv + norm2 += nu * nv + 2.0 * cross + k += 1 + if nu * nv <= eps * eps * norm2: + return k + ibest, ub = -1, 0.0 + for ii in range(m): + if not used_r[ii] and abs(u[ii]) > ub: + ub, ibest = abs(u[ii]), ii + if ibest < 0: + return k + i = ibest + return -1 if k >= kmax and k < min(m, n) else k + + @nb.njit(parallel=True, cache=True) + def _build(x, y, lows, eps, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata): + nl = lows.shape[0] + for b in nb.prange(nl): + a0, a1, b0, b1 = lows[b, 0], lows[b, 1], lows[b, 2], lows[b, 3] + m, n = a1 - a0, b1 - b0 + U = Ubuf[uoff[b]:uoff[b] + m * kmax].reshape(m, kmax) + V = Vbuf[voff[b]:voff[b] + n * kmax].reshape(n, kmax) + ranks[b] = _aca(x, y, a0, a1, b0, b1, eps, kmax, U, V) + nd = dense.shape[0] + for b in nb.prange(nd): + a0, a1, b0, b1 = dense[b, 0], dense[b, 1], dense[b, 2], dense[b, 3] + n = b1 - b0 + row = np.empty(n) + for i in range(a0, a1): + _krow(x, y, i, b0, b1, row) + ddata[doff[b] + (i - a0) * n:doff[b] + (i - a0 + 1) * n] = row + + @nb.njit(parallel=True, cache=True) + def _apply(X, lows, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata, nthr, Y): + n, mc = X.shape + buf = np.zeros((nthr, n, mc)) + nl = lows.shape[0] + nd = dense.shape[0] + tot = nl + nd + for t in nb.prange(nthr): + B = buf[t] + for b in range(t, tot, nthr): + if b < nl: + a0, a1, b0, b1 = lows[b, 0], lows[b, 1], lows[b, 2], lows[b, 3] + m, nn = a1 - a0, b1 - b0 + k = ranks[b] + U = Ubuf[uoff[b]:uoff[b] + m * kmax].reshape(m, kmax)[:, :k] + V = Vbuf[voff[b]:voff[b] + nn * kmax].reshape(nn, kmax)[:, :k] + B[a0:a1] += U @ (V.T @ X[b0:b1]) + B[b0:b1] += V @ (U.T @ X[a0:a1]) + else: + q = b - nl + a0, a1, b0, b1 = dense[q, 0], dense[q, 1], dense[q, 2], dense[q, 3] + D = ddata[doff[q]:doff[q] + (a1 - a0) * (b1 - b0)].reshape(a1 - a0, b1 - b0) + B[a0:a1] += D @ X[b0:b1] + if a0 != b0: + B[b0:b1] += D.T @ X[a0:a1] + for i in nb.prange(n): + for c in range(mc): + s = 0.0 + for t in range(nthr): + s += buf[t, i, c] + Y[i, c] = s + + @nb.njit(parallel=True, fastmath=True, cache=True) + def _far(x, y, Xt, Y, T=64): + m, n = Xt.shape + nt = (n + T - 1) // T + for it in nb.prange(nt): + r = np.empty(n) + for i in range(it * T, min(n, it * T + T)): + xi, yi = x[i], y[i] + for j in range(n): + dx, dy = xi - x[j], yi - y[j] + r[j] = dx * dx + dy * dy + r[i] = 1.0 + for j in range(n): + r[j] = 1.0 / np.sqrt(r[j]) + r[i] = 0.0 + for k in range(m): + s = 0.0 + for j in range(n): + s += r[j] * Xt[k, j] + Y[i, k] = s + + +def _numba(): + if nb is None: + raise ImportError("numba is required for the compiled / ritz components") + return dict(nb=nb, build=_build, apply=_apply, far=_far) + + +class HFar: + """Far kernel sum_{j != i} x_j / d_ij as an H-matrix (frozen block plan: leaf 128, eta 2). (varritz/hmat.py)""" + + def __init__(self, c, leaf=128, eta=2.0, eps=1e-7, kmax=48, nthr=None): + K = _numba() + self.perm, nodes = P._cluster_tree(c, leaf) + self.x, self.y = np.ascontiguousarray(c[self.perm, 0]), np.ascontiguousarray(c[self.perm, 1]) + plan_l, plan_d = [], [] + + def leafq(k): + return nodes[k][4] < 0 + + def rec(s, t): + a0, a1, lo_s, hi_s, sl, sr = nodes[s] + b0, b1, lo_t, hi_t, tl, tr = nodes[t] + if s == t: + if leafq(s): + plan_d.append((a0, a1, a0, a1)) + else: + rec(sl, sl), rec(sr, sr), rec(sl, sr) + return + gap = np.maximum(0.0, np.maximum(lo_s - hi_t, lo_t - hi_s)) + diam = min(np.sqrt(((hi_s - lo_s) ** 2).sum()), np.sqrt(((hi_t - lo_t) ** 2).sum())) + if diam <= eta * np.sqrt((gap**2).sum()): + plan_l.append((a0, a1, b0, b1)) + elif leafq(s) and leafq(t): + plan_d.append((a0, a1, b0, b1)) + elif leafq(s): + rec(s, tl), rec(s, tr) + elif leafq(t): + rec(sl, t), rec(sr, t) + else: + for u in (sl, sr): + for v in (tl, tr): + rec(u, v) + + rec(0, 0) + lows = np.array(plan_l, np.int64).reshape(-1, 4) + dense = np.array(plan_d, np.int64).reshape(-1, 4) + m = lows[:, 1] - lows[:, 0] + nn = lows[:, 3] - lows[:, 2] + uoff = np.r_[0, np.cumsum(m * kmax)][:-1] + voff = np.r_[0, np.cumsum(nn * kmax)][:-1] + Ubuf, Vbuf = np.zeros(int((m * kmax).sum())), np.zeros(int((nn * kmax).sum())) + dsz = (dense[:, 1] - dense[:, 0]) * (dense[:, 3] - dense[:, 2]) + doff = np.r_[0, np.cumsum(dsz)][:-1] + ddata = np.zeros(int(dsz.sum())) + ranks = np.zeros(len(lows), np.int64) + K["build"](self.x, self.y, lows, eps, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata) + bad = ranks < 0 + if bad.any(): # rank above kmax: these blocks become dense + extra = lows[bad] + lows = lows[~bad] + keep_u, keep_v = uoff[~bad], voff[~bad] + ranks = ranks[~bad] + uoff, voff = keep_u, keep_v + esz = (extra[:, 1] - extra[:, 0]) * (extra[:, 3] - extra[:, 2]) + eoff = np.r_[0, np.cumsum(esz)][:-1] + edata = np.zeros(int(esz.sum())) + 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) + doff = np.r_[doff, eoff + len(ddata)] + ddata = np.r_[ddata, edata] + dense = np.r_[dense, extra] + self.args = (lows, kmax, Ubuf, Vbuf, uoff, voff, ranks, dense, doff, ddata) + self.nthr = nthr or K["nb"].get_num_threads() + self.n_low, self.n_dense, self.mean_rank = len(lows), len(dense), float(ranks.mean()) if len(ranks) else 0.0 + self.n_converted = int(bad.sum()) + + def __call__(self, X): + Xp = np.ascontiguousarray(X[self.perm]) + Yp = np.empty_like(Xp) + _numba()["apply"](Xp, *self.args, self.nthr, Yp) + Y = np.empty_like(Yp) + Y[self.perm] = Yp + return Y + + +def _lagrange(u, q): + i0 = np.floor(u).astype(np.int64) - (q // 2 - 1) + t = u - i0 + w = np.ones((len(u), q)) + for a in range(q): + for b in range(q): + if a != b: + w[:, a] *= (t - b) / (a - b) + return i0, w + + +class ImageConv: + """Smooth finite-substrate image kernel (proxy._image_correction, exact series) by grid convolution. (fastop.py)""" + + def __init__(self, c, eps_r, spacing_frac=1 / 16, q=6): + from scipy.sparse import csr_matrix + P._image_correction(np.zeros(1), eps_r) + key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r)) + _, _, w, D = P._IMG_CACHE[key] + self.w, self.D = w, D + h = spacing_frac * D.min() + lo = c.min(0) - (q // 2 + 1) * h + u = (c - lo) / h + ix0, wx = _lagrange(u[:, 0], q) + iy0, wy = _lagrange(u[:, 1], q) + nx, ny = int(ix0.max()) + q + 1, int(iy0.max()) + q + 1 + rows = np.repeat(np.arange(len(c)), q * q) + gx = (ix0[:, None, None] + np.arange(q)[None, :, None]) * np.ones((1, 1, q), np.int64) + gy = (iy0[:, None, None] + np.arange(q)[None, None, :]) * np.ones((1, q, 1), np.int64) + vals = (wx[:, :, None] * wy[:, None, :]).ravel() + self.W = csr_matrix((vals, (rows, (gx * ny + gy).ravel())), shape=(len(c), nx * ny)) + self.nx, self.ny = nx, ny + Lx, Ly = 2 * nx, 2 * ny + ax = np.minimum(np.arange(Lx), Lx - np.arange(Lx)) * h + ay = np.minimum(np.arange(Ly), Ly - np.arange(Ly)) * h + rho = np.sqrt(ax[:, None] ** 2 + ay[None, :] ** 2) + Kk = sum(wn / np.sqrt(rho**2 + Dn * Dn) for wn, Dn in zip(w, D)) + self.Kf = np.fft.rfft2(Kk) + self.L = (Lx, Ly) + + def __call__(self, X): + G = (self.W.T @ X).reshape(self.nx, self.ny, -1) + out = np.empty((self.nx * self.ny, X.shape[1])) + for k in range(X.shape[1]): + f = np.fft.irfft2(np.fft.rfft2(G[:, :, k], s=self.L) * self.Kf, s=self.L)[: self.nx, : self.ny] + out[:, k] = f.ravel() + return self.W @ out + + +class FastOp: + """The frozen panel operator, matrix-free; bj(): block-Jacobi on the frozen cluster tree's leaves. (fastop.py)""" + + def __init__(self, c, wh, o, eps_r, M, leaf=128, h_min_n=4000, h_eps=1e-8): + from scipy.sparse import csr_matrix + t = time.perf_counter() + self.H = None + if len(c) >= h_min_n: + self.H = HFar(c, eps=h_eps) + self.t_hmat = time.perf_counter() - t + t = time.perf_counter() + self.c, self.wh, self.o, self.n, self.M = c, wh, o, len(c), M + self.B = (o[:, None] == np.arange(M)[None]).astype(float) + self.k = 4 * np.pi * P.EPS0_FF_PER_UM * (eps_r + 1) / 2 + self.x, self.y = np.ascontiguousarray(c[:, 0]), np.ascontiguousarray(c[:, 1]) + ii, jj = P._near_pairs(c, wh.max(1)) + d = np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)) + near = 0.5 * (P.rect_potential(c[ii], c[jj], wh[jj]) + P.rect_potential(c[jj], c[ii], wh[ii])) - 1.0 / d + dg = P.rect_self(wh[:, 0], wh[:, 1]) + n = self.n + 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)) + self.t_near = time.perf_counter() - t + t = time.perf_counter() + self.img = ImageConv(c, eps_r) if P._image_correction(np.zeros(1), eps_r) is not None else None + self.t_img = time.perf_counter() - t + t = time.perf_counter() + perm, nodes = P._cluster_tree(c, leaf) + self.leaves = [] + img0 = P._image_correction + for a0, a1, lo, hi, l, r in nodes: + if l >= 0: + continue + ix = np.sort(perm[a0:a1]) + cc = c[ix] + dd = np.sqrt(((cc[:, None] - cc[None]) ** 2).sum(-1)) + np.fill_diagonal(dd, 1.0) + A = 1.0 / dd + np.fill_diagonal(A, 0.0) + A += self.N[ix][:, ix].toarray() + if self.img is not None: + np.fill_diagonal(dd, 0.0) + A += img0(dd, eps_r, exact=True) + self.leaves.append((ix, cho_factor(A / self.k, lower=True))) + self.t_blocks = time.perf_counter() - t + + def matvec(self, X): + X = np.ascontiguousarray(X, dtype=np.float64) + if self.H is not None: + Y = self.H(X) + else: + Y = np.empty_like(X) + _numba()["far"](self.x, self.y, np.ascontiguousarray(X.T), Y) + Y += self.N @ X + if self.img is not None: + Y += self.img(X) + return Y / self.k + + def bj(self, R): + Z = np.zeros_like(R) + for ix, cf in self.leaves: + Z[ix] = cho_solve(cf, R[ix], check_finite=False) + return Z + + +def compiled_pcg(c, wh, o, n_metals, eps_r): + """Panel charges (n, n_metals) by the frozen block-Jacobi PCG (proxy._HMatrix.pcg: all columns, stop when every + column's relative residual < HMAT tol, LinAlgError after maxit) on the compiled operator. A zero right-hand-side + column (a metal without panels) gives residual 0/0 = NaN forever, so it raises at once instead of after maxit.""" + STATS["compiled_calls"] += 1 + B = (o[:, None] == np.arange(n_metals)[None, :]).astype(float) + bn = np.sqrt((B * B).sum(0)) + if not np.all(bn > 0): + raise np.linalg.LinAlgError("H-matrix PCG: a metal without panels (zero right-hand side; frozen: NaN residual)") + op = FastOp(c, wh, o, eps_r, n_metals, h_eps=P.HMAT["eps"]) + tol, maxit = P.HMAT["tol"], P.HMAT["maxit"] + X, R = np.zeros_like(B), B.copy() + Z = op.bj(R) + Pd, rz = Z.copy(), (R * Z).sum(0) + res = np.inf + for it in range(maxit): + AP = op.matvec(Pd) + al = rz / (Pd * AP).sum(0) + X += al * Pd + R -= al * AP + res = (np.sqrt((R * R).sum(0)) / bn).max() + if res < tol: + return X + if not np.isfinite(res): + break + Z = op.bj(R) + rzn = (R * Z).sum(0) + Pd = Z + (rzn / rz) * Pd + rz = rzn + raise np.linalg.LinAlgError(f"compiled H-matrix PCG did not converge (residual {res:.1e})") + + +def _mom_solve_fast(c, wh, o, n_metals, eps_r=11.45, dense_max=None): + n = len(c) + if n <= (P.DENSE_MAX if dense_max is None else dense_max) or "compiled" not in STATE["components"]: + return _ORIG["mom_solve_fast"](c, wh, o, n_metals, eps_r, dense_max) + return compiled_pcg(c, wh, o, n_metals, eps_r) + + +# ------------------------------------------------------------------------------------------------ hmat_vec (fastmom.py) +# scripts/geomfast/fastmom.py: the frozen H-matrix (same cluster tree, ACA, block-Jacobi PCG, FROZEN tolerances) with a +# vectorised kernel (no per-row scipy sparse slicing, no Python loop over image charges or ACA ranks). Only the H-matrix +# kernel is patched (proxy._image_correction and the dense path are untouched). + + +def _hv_images(eps_r): + P._image_correction(np.zeros(1), eps_r) + key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r)) + w, D = P._IMG_CACHE[key][2:] + return np.asarray(w, float), np.asarray(D, float) ** 2 + + +def _hv_kernel_init(self, c, wh, eps_r, perm): + _ORIG["Kernel.__init__"](self, c, wh, eps_r, perm) + N = self.N.tocsr() + N.sort_indices() + self.indptr, self.indices, self.data = N.indptr, N.indices, N.data + self.w, self.D2 = _hv_images(eps_r) if self.img else (None, None) + + +def _hv_corr(self, d): + return np.sqrt((d * d)[..., None] + self.D2) ** -1 @ self.w + + +def _hv_row(self, i, b0, b1): + d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2) + r = 1.0 / d + if self.img: + r += _hv_corr(self, d) + s, e = self.indptr[i], self.indptr[i + 1] + if e > s: + ix = self.indices[s:e] + lo, hi = np.searchsorted(ix, (b0, b1)) + if hi > lo: + r[ix[lo:hi] - b0] += self.data[s + lo:s + hi] + return r / self.k + + +def _hv_block(self, a0, a1, b0, b1): + 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) + corr = _hv_corr(self, d) if self.img else None + if a0 == b0: + d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0 + A = 1.0 / d + if corr is not None: + A += corr + s, e = self.indptr[a0], self.indptr[a1] + if e > s: + ix = self.indices[s:e] + rr = np.repeat(np.arange(a1 - a0), np.diff(self.indptr[a0:a1 + 1])) + m = (ix >= b0) & (ix < b1) + np.add.at(A, (rr[m], ix[m] - b0), self.data[s:e][m]) + return A / self.k + + +def _hv_aca(K, a0, a1, b0, b1, eps, max_rank): + m, n = a1 - a0, b1 - b0 + R = min(m, n, max_rank) + U, V = np.empty((m, R)), np.empty((n, R)) + used_r, used_c = np.zeros(m, bool), np.zeros(n, bool) + i, norm2, k = 0, 0.0, 0 + for _ in range(R): + r = K.row(a0 + i, b0, b1) + if k: + r -= V[:, :k] @ U[i, :k] + rm = np.where(used_c, 0.0, np.abs(r)) + j = int(np.argmax(rm)) + if rm[j] == 0.0: + break + v = r / r[j] + u = K.row(b0 + j, a0, a1) + if k: + u -= U[:, :k] @ V[j, :k] + used_r[i], used_c[j] = True, True + nu, nv = float(u @ u), float(v @ v) + norm2 += nu * nv + (2 * float((U[:, :k].T @ u) @ (V[:, :k].T @ v)) if k else 0.0) + U[:, k], V[:, k] = u, v + k += 1 + if nu * nv <= eps**2 * norm2: + break + um = np.where(used_r, 0.0, np.abs(u)) + i = int(np.argmax(um)) + if um[i] == 0.0: + break + else: + return None + return U[:, :k].copy(), V[:, :k].copy() + + +def _hv_mom_solve_fast(c, wh, o, n_metals, eps_r=11.45, dense_max=None): + """The frozen mom_solve_fast (vectorised kernel); a zero right-hand-side column raises at once (frozen: after maxit).""" + 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): + raise np.linalg.LinAlgError("H-matrix PCG: a metal without panels (zero right-hand side; frozen: NaN residual)") + return _ORIG["mom_solve_fast"](c, wh, o, n_metals, eps_r, dense_max) + + +# ------------------------------------------------------------------------------------------------ gpu (fastgpu.py) + + +class _Dev: + def __init__(self, t): + self.t = t + + +def _gpu_assemble(c, wh, diag, eps_r, near): + n = len(c) + if not STATE["gpu"] or n < CFG["gpu_min"]: + return _ORIG["assemble"](c, wh, diag, eps_r, near) + import torch + dev = CFG["device"] + k = 4 * np.pi * P.EPS0_FF_PER_UM * (eps_r + 1) / 2 + img = P._image_correction(np.zeros(1), eps_r) is not None + if img: + key = (P.SUBSTRATE["model"], float(P.SUBSTRATE["t_um"]), float(eps_r)) + du, T = P._IMG_CACHE[key][:2] + T = torch.as_tensor(T, dtype=torch.float64, device=dev) + x = torch.as_tensor(c[:, 0], dtype=torch.float64, device=dev) + y = torch.as_tensor(c[:, 1], dtype=torch.float64, device=dev) + A = torch.empty((n, n), dtype=torch.float64, device=dev) + for r0 in range(0, n, CFG["rows"]): + r1 = min(r0 + CFG["rows"], n) + d = torch.sqrt((x[r0:r1, None] - x[None, :]) ** 2 + (y[r0:r1, None] - y[None, :]) ** 2) + if img: + u = d / du + j = torch.clamp(u.floor().long(), 0, len(T) - 2) + f = u - j + corr = T[j] + f * (T[j + 1] - T[j]) + d[torch.arange(r1 - r0, device=dev), torch.arange(r0, r1, device=dev)] = 1.0 + blk = 1.0 / d + if img: + blk += corr + A[r0:r1] = blk / k + ii, jj = P._near_pairs(c, wh.max(1)) + if len(ii): + v = 0.5 * (near(c[ii], c[jj], wh[jj]) + near(c[jj], c[ii], wh[ii])) / k + if img: + v = v + P._image_correction(np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)), eps_r) / k + vi = torch.as_tensor(v, dtype=torch.float64, device=dev) + ti, tj = torch.as_tensor(ii, device=dev), torch.as_tensor(jj, device=dev) + A[ti, tj] = vi + A[tj, ti] = vi + dg = diag / k if not img else (diag + P._image_correction(np.zeros(1), eps_r)[0]) / k + A[torch.arange(n, device=dev), torch.arange(n, device=dev)] = torch.as_tensor(dg, dtype=torch.float64, device=dev) + return _Dev(A) + + +def _gpu_chol(A, B): + import torch + L, info = torch.linalg.cholesky_ex(A.t) + if int(info) > 0: + raise np.linalg.LinAlgError("not positive definite (device Cholesky)") + Bt = torch.as_tensor(np.ascontiguousarray(B), dtype=torch.float64, device=A.t.device) + X = torch.cholesky_solve(Bt, L).cpu().numpy() + A.t = None # the matrix is not used after a successful solve: release it and the allocator cache (shared GPU) + del L, Bt + torch.cuda.empty_cache() + return X + + +# ------------------------------------------------------------------------------------------------ mixed (fastmom.py) + + +def _mixed_chol(A, B): + from scipy.linalg.blas import dsymm + try: + cf = cho_factor(A.astype(np.float32, order="F"), lower=True, overwrite_a=True, check_finite=False) + except np.linalg.LinAlgError: + return _ORIG["chol_solve"](A, B) + B = np.asfortranarray(B, dtype=float) + X = cho_solve(cf, B.astype(np.float32), check_finite=False).astype(float) + bn = np.linalg.norm(B, axis=0) + for _ in range(CFG["refine_steps"]): + R = B - dsymm(1.0, A, X, side=0, lower=1) + if (np.linalg.norm(R, axis=0) <= CFG["refine_tol"] * bn).all(): + return X + X += cho_solve(cf, R.astype(np.float32), check_finite=False) + return _ORIG["chol_solve"](A, B) + + +def _chol_solve(A, B): + if isinstance(A, _Dev): + return _gpu_chol(A, B) + if "mixed" in STATE["components"]: + return _mixed_chol(A, B) + return _ORIG["chol_solve"](A, B) + + +def _sym_solve(A, B): + if not isinstance(A, _Dev): + return _ORIG["sym_solve"](A, B) + if A.t.shape[0] > FROZEN_DENSE_MAX: + return _gpu_chol(A, B) + try: + return _gpu_chol(A, B) + except np.linalg.LinAlgError: + from scipy.linalg import solve + return solve(A.t.cpu().numpy(), B, lower=True, assume_a="sym", overwrite_a=True, check_finite=False) + + +# ------------------------------------------------------------------------------------------------ ritz (certified) +# scripts/varritz/vr.py (templates, Ritz) and scripts/varritz/ritzprior.py (window / local loops, A5 dense dispatch). + + +def edge_geometry(metals, c, wh, o): + import shapely + n, M = len(c), len(metals) + d, a, g = np.zeros(n), np.zeros(n), np.zeros(n) + D = np.zeros((n, M)) + pts = shapely.points(c) + for k in range(M): + ix = np.nonzero(o == k)[0] + if not len(ix): + continue + bd = metals[k].boundary + shapely.prepare(bd) + ln = shapely.shortest_line(pts[ix], bd) + xy = shapely.get_coordinates(ln).reshape(-1, 2, 2) + v = xy[:, 0] - xy[:, 1] + dk = np.sqrt((v**2).sum(1)) + nv = np.where(dk[:, None] > 0, v / np.maximum(dk, 1e-300)[:, None], 0.7071) + d[ix], a[ix] = dk, np.abs(nv[:, 0]) * wh[ix, 0] + np.abs(nv[:, 1]) * wh[ix, 1] + others = shapely.union_all([metals[j] for j in range(M) if j != k]) + shapely.prepare(others) + g[ix] = shapely.distance(shapely.points(xy[:, 1]), others) + for j in range(M): + shapely.prepare(metals[j]) + D[:, j] = shapely.distance(pts, metals[j]) + return d, a, np.maximum(g, 1e-9), D + + +def templates(d, a, g): + lo, hi = np.maximum(d - a / 2, 0.0), d + a / 2 + w = np.maximum(hi - lo, 1e-12) + s = 2 * (np.sqrt(hi) - np.sqrt(lo)) / w + gg = 2 * (np.arcsinh(np.sqrt(hi / g)) - np.arcsinh(np.sqrt(lo / g))) / w + return {"u": np.ones_like(d), "s": s, "g": gg} + + +def _orth_blocks(V, o, M): + out = [] + for k in range(M): + ix = np.nonzero(o == k)[0] + cols = np.nonzero(np.abs(V[ix]).sum(0) > 0)[0] + if not len(cols): + continue + Vk = V[np.ix_(ix, cols)] + Vk = Vk / np.maximum(np.linalg.norm(Vk, axis=0), 1e-300) + U, s, _ = np.linalg.svd(Vk, full_matrices=False) + keep = s > 1e-10 * s[0] + W = np.zeros((len(o), int(keep.sum()))) + W[ix] = U[:, keep] + out.append(W) + return np.hstack(out) + + +def ritz(V, PV, B): + G = V.T @ PV + G = 0.5 * (G + G.T) + Fm = V.T @ B + try: + Y = cho_solve(cho_factor(G, lower=True), Fm) + except np.linalg.LinAlgError: + lam, U = eigh(G) + keep = lam > 1e-13 * lam.max() + Y = U[:, keep] @ ((U[:, keep].T @ Fm) / lam[keep, None]) + CV = Fm.T @ Y + return 0.5 * (CV + CV.T), Y, B - PV @ Y + + +def start_basis(metals, c, wh, o, kind, op, B): + M = len(metals) + if kind == "pcg": + Z = op.bj(B) + return Z / np.maximum(np.linalg.norm(Z, axis=0), 1e-300) + area = wh[:, 0] * wh[:, 1] + d, a, g, _ = edge_geometry(metals, c, wh, o) + T = templates(d, a, g) + return _orth_blocks(np.hstack([np.where((o == k)[:, None], (T[nm] * area)[:, None], 0.0) + for k in range(M) for nm in "usg"]), o, M) + + +def criterion(CV, est, floor=0.1): + e = np.sqrt(np.maximum(np.diag(est), 0.0)) + return float(np.max(np.outer(e, e) / np.maximum(np.abs(CV), floor))) + + +def ritz_solve(metals, c, wh, o, eps_r, cols): + tau, start, maxit = CFG["ritz_tau"], CFG["ritz_start"], CFG["ritz_maxit"] + t0 = time.perf_counter() + op = FastOp(c, wh, o, eps_r, len(metals)) + B = op.B + V = start_basis(metals, c, wh, o, start, op, B) + PV = op.matvec(V) + for m in range(maxit + 1): + CV, Y, R = ritz(V, PV, B) + est = R.T @ op.bj(R) + rel = criterion(CV, est) + if rel <= tau or m == maxit: + break + Z = op.bj(R) + Z /= np.maximum(np.linalg.norm(Z, axis=0), 1e-300) + V, PV = np.hstack([V, Z]), np.hstack([PV, op.matvec(Z)]) + info = dict(n=len(c), steps=m, converged=rel <= tau, est_rel=rel, est_diag=np.diag(est).tolist(), k=V.shape[1], + t=time.perf_counter() - t0) + return (V @ Y)[:, np.asarray(cols)], info + + +def _frozen_solve(c, wh, o, eps_r, cols, all_rhs, n_metals): + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + Bm = (o[:, None] == cols[None, :]).astype(float) + return P._chol_solve(A, Bm) if all_rhs else F._sym_solve(A, Bm) + return P.mom_solve_fast(c, wh, o, n_metals, eps_r)[:, cols] + + +def _ritz_dense_n(): + return max(CFG["ritz_dense_n"], CFG["gpu_dense_max"] if STATE["gpu"] else 0) + + +def _solve_mesh(metals, c, wh, o, eps_r, cols, all_rhs, kind): + if len(c) <= _ritz_dense_n(): # A5 (and the GPU): exact solve, estimate 0 + Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals)) + LOG.append(dict(kind=kind, ritz=None, exact=True, steps=0, n=len(c), est_rel=0.0, est_diag=[0.0] * len(metals))) + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + try: + Q, info = ritz_solve(metals, c, wh, o, eps_r, cols) + rec = dict(kind=kind, ritz=bool(info["converged"]), exact=False, **info) + if not info["converged"]: + raise np.linalg.LinAlgError("ritz not converged") + except np.linalg.LinAlgError: + 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))) + Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals)) + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + LOG.append(rec) + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + + +def _last_good(R, ok, skipped, metals, eps_r, cols, all_rhs): + if R is not None and (ok or not np.isnan(R).any()): + return R + for c, wh, o in reversed(skipped): + try: + Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals)) + 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))) + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + except np.linalg.LinAlgError: + continue + raise np.linalg.LinAlgError("no successful solve in the refinement sequence") + + +def _final_unpanelled(metals, c, wh, o, eps_r, cols, all_rhs): + """Final majority mesh still missing a metal: the frozen solve (doomed above the frozen DENSE_MAX: it raises).""" + if len(c) > FROZEN_DENSE_MAX: + raise np.linalg.LinAlgError("final mesh misses a metal above DENSE_MAX (frozen: NaN PCG)") + Q = _frozen_solve(c, wh, o, eps_r, cols, all_rhs, len(metals)) + LOG.append(dict(kind="window", ritz=None, exact=True, steps=0, n=len(c), est_rel=0.0, est_diag=[0.0] * len(metals))) + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + + +def ritz_window_solve(metals, n_core, h, budget, eps_r, all_rhs, levels=()): + cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core) + t_mesh = t_sol = 0.0 + hh, cap, refined, majority = h, budget, 0, False + skipped, R = [], None + while True: + t = time.perf_counter() + if not refined: + c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels) + elif levels: + c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority) + else: + c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority) + t_mesh += time.perf_counter() - t + t = time.perf_counter() + full = bool(np.all(np.bincount(o, minlength=len(metals)) > 0)) + if not full and not majority: + skipped.append((c, wh, o)) + if refined < F.LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + continue + try: + if not full: + R = _final_unpanelled(metals, c, wh, o, eps_r, cols, all_rhs) + else: + R = _solve_mesh(metals, c, wh, o, eps_r, cols, all_rhs, "window") + off = np.ones(R.shape, bool) + off[np.arange(len(cols)), cols] = False + ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0))) + except np.linalg.LinAlgError: + ok = False + t_sol += time.perf_counter() - t + if LOG: + LOG[-1].update(accepted=ok, refined=refined) + if ok or majority: + break + if refined < F.LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + R = _last_good(R, ok, skipped, metals, eps_r, cols, all_rhs) + return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, + "assemble_s": 0.0, "solve_s": t_sol} + + +def ritz_loc_solve(metals, H, h_floor, ppg, eps_r): + t_mesh = t_sol = 0.0 + cols = np.arange(len(metals)) + refined, majority = 0, False + skipped, R = [], None + while True: + t = time.perf_counter() + c, wh, o, n_raw = FL.mesh_local(metals, H, h_floor, ppg, majority) + t_mesh += time.perf_counter() - t + t = time.perf_counter() + full = bool(np.all(np.bincount(o, minlength=len(metals)) > 0)) + if not full and not majority: + skipped.append((c, wh, o)) + if refined < F.LITE_REFINE: + H, h_floor, refined = H / 2, h_floor / 2, refined + 1 + else: + majority = True + continue + try: + if not full: + R = _final_unpanelled(metals, c, wh, o, eps_r, cols, True) + else: + R = _solve_mesh(metals, c, wh, o, eps_r, cols, True, "local") + off = ~np.eye(len(metals), dtype=bool) + ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and np.all(R[off] < 0)) + except np.linalg.LinAlgError: + ok = False + t_sol += time.perf_counter() - t + if LOG: + LOG[-1].update(accepted=ok, refined=refined) + if ok or majority: + break + if refined < F.LITE_REFINE: + H, h_floor, refined = H / 2, h_floor / 2, refined + 1 + else: + majority = True + R = _last_good(R, ok, skipped, metals, eps_r, cols, True) + return R, {"H_um": H, "h_floor_um": h_floor, "h_min_um": float(wh.min()), "panels": len(c), "raw": n_raw, + "refined": refined, "majority": majority, "mesh_s": t_mesh, "solve_s": t_sol} + + +def _micro_mom_loc(G, conds, eps_r=11.45, ppg=3.0, *args, **kw): + """micro_mom_loc + a certificate (certified mode). Window records of forked workers come back through a file.""" + LOG.clear() + fd, path = tempfile.mkstemp(prefix="fp_cert_", suffix=".jsonl") + os.close(fd) + _CERT_FILE[0] = path + try: + out = _ORIG["micro_mom_loc"](G, conds, eps_r, ppg, *args, **kw) + with open(path) as f: + LOG.extend(json.loads(ln) for ln in f if ln.strip()) + finally: + _CERT_FILE[0] = None + os.unlink(path) + CERTS.append(_certificate(G, conds, out[0], list(LOG), ppg)) + return out + + +def _certificate(G, conds, C, recs, ppg): + """Calibrated estimate (c = RITZ_CAL) of the prior's solver error: max relative entry error (0.1 fF floor) over the + accepted solves; per-entry fF estimates c sqrt(e_i e_j) for a single-window single-frame layout (input order). + Self entries of Ritz solves are lower bounds of the exact ones.""" + acc = [r for r in recs if r.get("accepted", True) and "est_rel" in r] + cert = dict(ppg=ppg, n_solves=len(recs), n_ritz=sum(bool(r.get("ritz")) for r in recs), + n_exact=sum(bool(r.get("exact")) for r in recs), n_fallback=sum(bool(r.get("fallback")) for r in recs), + max_rel_est=RITZ_CAL * max((r["est_rel"] for r in acc), default=0.0), calibration=RITZ_CAL, + windows=[dict(window=r.get("window"), n=r["n"], ritz=bool(r.get("ritz")), max_rel_est=RITZ_CAL * r["est_rel"]) + for r in acc], entry_est_fF=None) + if len(acc) == 1 and len(conds) <= 8: + frames = F.canonical_frames(G, conds) + e = np.maximum(np.asarray(acc[0]["est_diag"], float), 0.0) + if len(frames) == 1 and len(e) == len(conds) + 1: + inv = np.r_[0, np.argsort(frames[0][2]) + 1] + ei = e[inv] + cert["entry_est_fF"] = (RITZ_CAL * np.sqrt(np.outer(ei, ei))).tolist() + return cert + + +def certificates(clear=True) -> list: + out = list(CERTS) + if clear: + CERTS.clear() + return out + + +# ------------------------------------------------------------------------------------------------ install + + +def _gpu_available() -> bool: + try: + import torch + return bool(torch.cuda.is_available()) + except Exception: # noqa: BLE001 + return False + + +def resolve(mode=None, components=None) -> tuple: + if components is not None: + bad = set(components) - set(COMPONENTS) + if bad: + raise ValueError(f"unknown components {sorted(bad)}; known {COMPONENTS}") + return tuple(c for c in COMPONENTS if c in components) + mode = mode or os.environ.get("TLSQ_PRIOR_MODE", "exact") + if mode not in ("exact", "exact_compiled", "certified", "frozen"): + raise ValueError(f"mode {mode!r}") + if mode == "frozen": + return () + comps = ["fv", "parallel", "hmat_vec"] + if _gpu_available(): + comps.append("gpu") + if MIXED_GATED: + comps.append("mixed") + if mode in ("exact_compiled", "certified"): + comps.append("compiled") + if mode == "certified": + comps.append("ritz") + return tuple(c for c in COMPONENTS if c in comps) + + +def install(mode=None, components=None, workers=None, cpus=None, target="auto", blas_threads="auto", value_patch=None, + warm=True, **cfg): + """Install the components (mode 'exact' / 'certified' / 'frozen', or an explicit component list) on the frozen + vendor modules. workers: window workers (default TLSQ_PRIOR_WORKERS, else CPUs); cpus: default detect_cpus(); + blas_threads='auto': this process's BLAS / numba / pool threads = CPUs (None leaves them alone).""" + uninstall() + bind(target) + CFG.update(cfg) + comps = resolve(mode, components) + STATE["mode"] = mode or ("explicit" if components is not None else os.environ.get("TLSQ_PRIOR_MODE", "exact")) + STATE["components"] = comps + STATE["cpus"] = int(cpus) if cpus else detect_cpus() + w = workers if workers is not None else os.environ.get("TLSQ_PRIOR_WORKERS") + # default (A5): serial windows with all threads; forked windows only for certified (measured faster there) + STATE["workers"] = max(1, int(w)) if w is not None else (STATE["cpus"] if "ritz" in comps else 1) + if "parallel" not in comps: + STATE["workers"] = 1 + if sys.platform == "darwin" and STATE["workers"] > 1: # Accelerate (BLAS) crashes in forked children: never fork on macOS + STATE["workers"] = 1 + STATE["parallel_off"] = "darwin: the vendor's window pool forks and Accelerate is not fork-safe" + STATE["gpu"] = "gpu" in comps + _ORIG.update(window_solve=F._window_solve, loc_solve=FL._solve, micro_mom=F.micro_mom, lite_job=F._lite_job, + micro_mom_loc=FL.micro_mom_loc, mom_solve_fast=P.mom_solve_fast, assemble=P._assemble, + chol_solve=P._chol_solve, sym_solve=F._sym_solve, dense_max=P.DENSE_MAX, num_threads=P.NUM_THREADS) + _ORIG["micro_mom_sig"] = inspect.signature(F.micro_mom) + if blas_threads == "auto": + _set_threads(STATE["cpus"]) + elif blas_threads: + _set_threads(int(blas_threads)) + if "fv" in comps: + F._window_solve = fv_window_solve + F.micro_mom = _micro_mom + F._lite_job = _lite_job + _PARENT[0] = os.getpid() + _CHILD[0] = False + if "hmat_vec" in comps: + _ORIG.update({"Kernel.__init__": P._Kernel.__init__, "Kernel.row": P._Kernel.row, "Kernel.block": P._Kernel.block, "aca": P._aca}) + P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = _hv_kernel_init, _hv_row, _hv_block, _hv_aca + P.mom_solve_fast = _hv_mom_solve_fast + if "compiled" in comps: + P.mom_solve_fast = _mom_solve_fast + if warm: + warmup() + if "gpu" in comps: + P._assemble = _gpu_assemble + P.DENSE_MAX = CFG["gpu_dense_max"] + if "gpu" in comps or "mixed" in comps: + P._chol_solve = _chol_solve + F._sym_solve = _sym_solve + if "ritz" in comps: + F._window_solve = ritz_window_solve + FL._solve = ritz_loc_solve + FL.micro_mom_loc = _micro_mom_loc + if warm: + warmup() + if value_patch is not None: + _UNDO.append(value_patch(F, P, FL)) + STATE["value_patch"] = getattr(value_patch, "__name__", str(value_patch)) + return dict(STATE) + + +def uninstall() -> None: + for u in reversed(_UNDO): + if callable(u): + u() + _UNDO.clear() + if _ORIG: + F._window_solve, FL._solve, F.micro_mom, F._lite_job = (_ORIG["window_solve"], _ORIG["loc_solve"], _ORIG["micro_mom"], + _ORIG["lite_job"]) + FL.micro_mom_loc, P.mom_solve_fast, P._assemble = _ORIG["micro_mom_loc"], _ORIG["mom_solve_fast"], _ORIG["assemble"] + P._chol_solve, F._sym_solve, P.DENSE_MAX = _ORIG["chol_solve"], _ORIG["sym_solve"], _ORIG["dense_max"] + P.NUM_THREADS = _ORIG["num_threads"] + if "aca" in _ORIG: + P._Kernel.__init__, P._Kernel.row, P._Kernel.block, P._aca = (_ORIG["Kernel.__init__"], _ORIG["Kernel.row"], + _ORIG["Kernel.block"], _ORIG["aca"]) + _ORIG.clear() + for tp in _TP: + try: + tp.restore_original_limits() + except Exception: # noqa: BLE001 + pass + _TP.clear() + STATE.update(mode=None, components=(), workers=1, gpu=False, value_patch=None, parallel_off=None) + + +@contextlib.contextmanager +def installed(**kw): + install(**kw) + try: + yield dict(STATE) + finally: + uninstall() + + +def warmup() -> float: + """Compile / load the numba kernels (first call per process; cached on disk afterwards). Returns seconds.""" + t = time.perf_counter() + rng = np.random.default_rng(0) + c = rng.random((600, 2)) * 100 + H = HFar(c, leaf=64) + H(np.ones((600, 2))) + Y = np.empty((600, 1)) + _numba()["far"](c[:, 0].copy(), c[:, 1].copy(), np.ones((1, 600)), Y) + return time.perf_counter() - t + + +def status() -> dict: + return dict(STATE, cfg=dict(CFG), mixed_gated=MIXED_GATED, frozen_dense_max=FROZEN_DENSE_MAX) diff --git a/geomgnn/flagship.py b/geomgnn/flagship.py new file mode 100644 index 0000000000000000000000000000000000000000..6ea709ca344552f2d25ede46e1654d5b0b20bed3 --- /dev/null +++ b/geomgnn/flagship.py @@ -0,0 +1,43 @@ +"""Flagship Space tiers (deploy/registry.yaml: flagship_hub, flagship_tiers; deploy/GNN_SPACE.md "Flagship Space"). + +The tiers run through the paper Space's code path (geomgnn.paper: scene -> embedding -> priors under the tier's prior stack, or the +learned prior surrogate -> networks), unchanged. `view(reg)` is the registry seen with paper_arms / paper_hub replaced by +flagship_tiers / flagship_hub, so every geomgnn.paper function serves the tiers as arms. +""" + +from __future__ import annotations + +import copy + +from . import paper as PA + + +def view(reg): + if reg.data.get("_flagship_view"): + return reg + v = copy.copy(reg) + v.data = dict(reg.data, paper_arms=reg.data["flagship_tiers"], paper_hub=reg.data["flagship_hub"], _flagship_view=True) + return v + + +def tiers(reg) -> dict: + return view(reg)["paper_arms"] + + +def served(reg) -> list[str]: + v = view(reg) + return [k for k in PA.live(v) if PA.available(k, v)] + + +def predict(prepared, tier, reg, **kw) -> dict: + """Full N x N Maxwell matrix (fF; ground row/column 0) of a prepared layout with a flagship tier (geomgnn.paper.predict).""" + v = view(reg) + res = PA.predict(prepared, tier, v, **kw) + t = v["paper_arms"][tier] + res.pop("paper", None) + res.update(tier=tier, tier_title=t["title"], reference=t.get("reference") or {}, b5=t.get("b5"), b5_physics=t.get("b5_physics")) + return res + + +def preload(reg, example=None) -> dict: + return PA.preload(view(reg), example=example) diff --git a/geomgnn/infer.py b/geomgnn/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..31df041b31ecde2d2585729b578802c88b5bcb30 --- /dev/null +++ b/geomgnn/infer.py @@ -0,0 +1,71 @@ +"""Label-free standalone prediction for geomfast checkpoints. +Only checkpoints, geometry, and optional priors are accepted; never Q3D labels. +""" +import os +os.environ.setdefault('PYTHONDONTWRITEBYTECODE','1') +import sys,importlib.util,time +from pathlib import Path +import numpy as np +import torch +W=Path(__file__).resolve().parent +from .pilot_p2 import build_model +from ._tlsq.uge.scene import scene_from_gds,tlsq_namer +from ._tlsq.uge.graph import scene_graph +from ._tlsq.uge.proxies import CapacitanceMoM +from ._tlsq.uge import localtok as lt + +class Predictor: + def __init__(self,paths,threads=2): + torch.set_num_threads(threads);self.models=[];self.threads=threads + for path in paths: + c=torch.load(path,map_location='cpu',weights_only=False) + 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)) + assert len({c['arm'] for c,m in self.models})==1 + self.arm=self.models[0][0]['arm'];self.direct=self.arm=='direct_gnn';self.fast=self.arm=='fast_gnn' + def from_features(self,row,priors=None): + 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' + outputs=[] + for ck,model in self.models: + 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']) + 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] + 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) + kinds=E[0,:,ek.index('is_ground_pair')].astype(int) + if self.arm in ['pair_mlp','mesh','lofo_pair']:X=X[:,:,[nk.index('role_ground')]];G=np.zeros((1,1),np.float32) + if self.direct:base=np.array([[ck['direct_intercepts'][str(k)] for k in kinds]],np.float32) + else: + p0,p1=(.75,1.5) if self.fast else (1.5,3.) + lo=np.log(priors[p0][i,j]);hi=np.log(priors[p1][i,j]);assert np.isfinite(lo).all() and np.isfinite(hi).all() + base=(2*hi-lo)[None].astype(np.float32);E=np.concatenate([E,(hi-lo)[None,:,None].astype(np.float32)],-1) + for name,z in [('X',X),('E',E),('G',G)]: + 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) + assert np.isfinite(z).all() + 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) + 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) + with torch.inference_mode():C,_=model(T(X),T(E),T(G),M,PM,T(pairs),T(base),T(bbase),self.direct) + outputs.append(C[0].numpy()) + result=np.mean(outputs,axis=0);assert np.isfinite(result).all();return result + def from_gds(self,path,names=None,alpha=None,local_gap=None): + from ._tlsq.embed import proxy,fastprior + physics=self.models[0][0].get('schema',{}).get('physics',{}) + if alpha is None:alpha=physics.get('alpha') + if local_gap is None:local_gap=physics.get('prior_method')=='local_gap' + proxy.NUM_THREADS=self.threads;start=time.perf_counter() + if names is None:namer=tlsq_namer + else: + def namer(S,m): + sig=[n for n in names if n!='G'];assert len(S)==len(sig),'signal polygons vs supplied file-order names';return sig + sc=scene_from_gds(path,namer=namer);t1=time.perf_counter();row=lt.add_tokens(sc,scene_graph(sc));t2=time.perf_counter();priors={} + if not self.direct: + old=fastprior.ALPHA_GAP_SHARE + try: + if alpha is not None:fastprior.ALPHA_GAP_SHARE=alpha + for p in ([.75,1.5] if self.fast else [1.5,3.]): + if local_gap: + from ._tlsq.embed.fastprior_loc import micro_mom_loc + with proxy.substrate('slab_open',sc.stack['substrate_thickness_um']): + 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) + priors[p]=-mat + else:priors[p]=-CapacitanceMoM(max_cells=10**9,mesh='lite',cells_per_gap=p,substrate='slab_open').compute(sc,list(row['edges']))['matrix'] + finally:fastprior.ALPHA_GAP_SHARE=old + t3=time.perf_counter();C=self.from_features(row,priors);t4=time.perf_counter() + 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)) diff --git a/geomgnn/learned_prior.py b/geomgnn/learned_prior.py new file mode 100644 index 0000000000000000000000000000000000000000..f963683d19c53aaa36c51d32d2616305e548e08d --- /dev/null +++ b/geomgnn/learned_prior.py @@ -0,0 +1,46 @@ +"""Fast tier: the learned prior surrogate (agents/learnedprior, t100_norm) in place of the two field solves. + +priors(model, prep, row) = the statements of scripts/learnedprior/lp_bench.py `Learned.from_gds` between the features and the +network: pack the scene-graph row with the P2d schema (scripts/learnedprior/lp_label.py `pack`), predict log P at 1.5 and 3 +panels per gap with lp_surrogate.predict_bucket (one layout), and return the symmetric positive mutual matrices that +infer.Predictor.from_features reads. Self / closure terms are not inputs, so the diagonals stay zero. +""" + +from __future__ import annotations + +import numpy as np + +from . import lp_surrogate as S + +PPG = (1.5, 3.0) + + +def load(path): + """(model, prep, checkpoint) on CPU (lp_surrogate.load_model).""" + return S.load_model(str(path), "cpu") + + +def pack(row: dict) -> dict: + """lp_label.pack: X (n, nodekeys), E (P, edgekeys), G (globkeys); pairs i < j in scene order.""" + n = len(row["names"]) + pairs = [(i, j) for i in range(n) for j in range(i + 1, n)] + nk = S.SCHEMA["nodekeys"] + 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) + 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"])) + Gv = np.array([row["global"][k] for k in S.SCHEMA["globkeys"]], np.float32) + return dict(X=X, E=E, G=Gv, names=row["names"]) + + +def priors(model, prep, row: dict) -> dict: + z = pack(row) + n = len(z["names"]) + b = dict(n=n, X=z["X"][None], E=z["E"][None], G=z["G"][None]) + lp = S.predict_bucket(model, prep, b, "cpu")[0] + iu = np.triu_indices(n, 1) + out = {} + for k, p in enumerate(PPG): + M = np.zeros((n, n)) + M[iu] = np.exp(lp[:, k]) + out[p] = M + M.T + return out diff --git a/geomgnn/lp_surrogate.py b/geomgnn/lp_surrogate.py new file mode 100644 index 0000000000000000000000000000000000000000..94c1294f5ca74de155a3dbb65fbd6a1f7daa3f62 --- /dev/null +++ b/geomgnn/lp_surrogate.py @@ -0,0 +1,396 @@ +"""learnedprior surrogate (PROTOCOL.md section 3): tri-GNN on the P2d scene-graph features -> (base, delta) per pair. + + python lp_surrogate.py train --p2d DIR [--gen DIR] --out DIR [--steps N] [--raw] [--gen-frac F] [--seed S] + python lp_surrogate.py predict --ckpt FILE --data DIR --kind p2d|gen --out FILE.npz + +Data buckets are homogeneous in conductor count n (no padding). Pairs i < j in the stored order. +""" +from __future__ import annotations + +import os + +os.environ.setdefault("PYTHONDONTWRITEBYTECODE", "1") +import argparse +import glob +import hashlib +import json +import platform +import time +from pathlib import Path + +import numpy as np +import torch +from torch import nn +from torch.nn import functional as F + +HERE = Path(__file__).resolve().parent +SCHEMA = json.loads((HERE / "schema_p2d.json").read_text()) +NK, EK, GK = SCHEMA["nodekeys"], SCHEMA["edgekeys"], SCHEMA["globkeys"] +NODE_DIM = {"log_area": 2, "log_perimeter": 1, "log_min_halfwidth": 1, "log_width": 1} +EDGE_DIM = {"log_dmin": 1, "log_cps": 1, "mom_0.5": 0.5, "mom_1": 0.0, "mom_2": -1.0} +EDGE_DIM.update({f"lg_{k}": v for k, v in list(EDGE_DIM.items())}) +GLOB_DIM = {"log_chip_area": 2, "log_pocket_area": 2, "log_substrate_t": 1} +GROUPS = ("geomfast", "base", "large", "scaled", "near") +T_GROUP = {"t1_near": "near", "t2_large": "large", "t3_scaled": "scaled"} + + +# ----------------------------------------------------------------------------------------------------------- data +def load_p2d(root) -> list: + out = [] + for p in sorted(glob.glob(f"{root}/n*.npz")): + d = np.load(p, allow_pickle=False) + 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"], + split=d["split"], family=d["family"], group=np.full(len(d["key"]), "geomfast"), scale=np.ones(len(d["key"])))) + return out + + +def load_gen(roots) -> list: + """roots: one packed dir or several separated by "+" (v1 corpus + targeted corpus; no commas: sbatch --export splits on them).""" + out = [] + for p in sorted(f for root in str(roots).replace(",", "+").split("+") for f in glob.glob(f"{root}/n*.npz")): + d = np.load(p, allow_pickle=False) + grp = np.array([T_GROUP.get(str(s), str(s)) for s in d["stratum"]]) + 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"], + family=np.char.add("gen_", d["stratum"].astype(str)), group=grp, scale=d["scale"])) + return out + + +def merge_buckets(bs: list) -> list: + by = {} + for b in bs: + by.setdefault(b["n"], []).append(b) + 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())] + + +def subset(b: dict, m: np.ndarray) -> dict: + return {k: (v if k == "n" else v[m]) for k, v in b.items()} + + +def log_L(X: np.ndarray) -> np.ndarray: + """(B,) log characteristic length: mean over signal nodes of 0.5 log_area.""" + sig = X[:, :, NK.index("role_ground")] < 0.5 + la = X[:, :, NK.index("log_area")] + return 0.5 * (la * sig).sum(1) / sig.sum(1).clip(1) + + +class Prep: + """Scale shift (optional) + robust scaling; TRAIN statistics only.""" + + def __init__(self, raw: bool = False): + self.raw = raw + self.nshift = np.array([NODE_DIM.get(k, 0.0) for k in NK]) + self.eshift = np.array([EDGE_DIM.get(k, 0.0) for k in EK]) + self.gshift = np.array([GLOB_DIM.get(k, 0.0) for k in GK]) + + def features(self, b: dict): + X, E, G = b["X"].astype(np.float64), b["E"].astype(np.float64), b["G"].astype(np.float64) + lL = log_L(b["X"]) if not self.raw else np.zeros(len(X)) + if not self.raw: + X = X - self.nshift * lL[:, None, None] + E = E - self.eshift * lL[:, None, None] + G = G - self.gshift * lL[:, None] + G = np.concatenate([G, lL[:, None]], 1) + return X, E, G, lL + + def targets(self, b: dict, lL: np.ndarray): + y = np.log(b["pri"]) - lL[:, None, None] + return np.stack([2 * y[..., 1] - y[..., 0], y[..., 1] - y[..., 0]], -1) + + def fit(self, buckets: list): + acc = {k: [] for k in "XEGT"} + rng = np.random.default_rng(0) + for b in buckets: + tr = np.flatnonzero(b["split"] == "train") + if not len(tr): + continue + tr = rng.choice(tr, min(len(tr), 3000), replace=False) + X, E, G, lL = self.features(subset(b, np.isin(np.arange(len(b["key"])), tr))) + acc["X"].append(X.reshape(-1, X.shape[-1])) + acc["E"].append(E.reshape(-1, E.shape[-1])) + acc["G"].append(G) + acc["T"].append(self.targets(subset(b, np.isin(np.arange(len(b["key"])), tr)), lL).reshape(-1, 2)) + self.stats = {} + for k in "XEG": + v = np.concatenate(acc[k]) + med = np.nanmedian(v, 0) + iq = np.nanpercentile(v, 75, 0) - np.nanpercentile(v, 25, 0) + self.stats[k] = (med, np.where(iq > 1e-6, iq, 1.0)) + t = np.concatenate(acc["T"]) + self.stats["T"] = (t.mean(0), t.std(0)) + return self + + def apply(self, b: dict): + X, E, G, lL = self.features(b) + out = [] + for k, z in (("X", X), ("E", E), ("G", G)): + med, iq = self.stats[k] + out.append(np.clip((np.where(np.isfinite(z), z, med) - med) / iq, -10, 10).astype(np.float32)) + return (*out, lL) + + def state(self): + return dict(raw=self.raw, stats={k: [np.asarray(x).tolist() for x in v] for k, v in self.stats.items()}) + + @classmethod + def from_state(cls, s): + p = cls(s["raw"]) + p.stats = {k: tuple(np.array(x) for x in v) for k, v in s["stats"].items()} + return p + + +# ---------------------------------------------------------------------------------------------------------- model +def mlp(i, o, w): + return nn.Sequential(nn.Linear(i, w), nn.SiLU(), nn.Linear(w, o)) + + +class Round(nn.Module): + def __init__(self, w): + super().__init__() + self.me, self.mh = nn.Linear(w, w), nn.Linear(w, w) + self.node = mlp(3 * w, w, w) + self.edge = mlp(3 * w, w, w) + self.ln_t = nn.LayerNorm(w) + self.ta, self.tb, self.tg = nn.Linear(w, w), nn.Linear(w, w), nn.Linear(w, w) + self.tout = nn.Sequential(nn.LayerNorm(w), nn.Linear(w, w)) + self.ln_h, self.ln_e = nn.LayerNorm(w), nn.LayerNorm(w) + + def forward(self, h, e, off): + # h (B,N,w), e (B,N,N,w) symmetric, off (N,N,1) off-diagonal mask + N = h.shape[1] + m = self.me(e) * self.mh(h)[:, None, :, :] * off # message j -> i + mean = m.sum(2) / max(N - 1, 1) + mx = (m + (off - 1) * 1e4).amax(2) if N > 1 else torch.zeros_like(mean) + h = h + self.node(torch.cat([self.ln_h(h), mean, mx], -1)) + hs, hd = h[:, :, None] + h[:, None], (h[:, :, None] - h[:, None]).abs() + e = e + self.edge(torch.cat([self.ln_e(e), hs, hd], -1)) * off + z = self.ln_t(e) + a, b = self.ta(z) * off, self.tb(z) * off + t = torch.einsum("bikc,bjkc->bijc", a, b) / max(N - 2, 1) + t = 0.5 * (t + t.transpose(1, 2)) + e = e + torch.sigmoid(self.tg(z)) * self.tout(t) * off + return h, e + + +class TriGNN(nn.Module): + def __init__(self, dx, de, dg, w=128, rounds=4): + super().__init__() + self.node_in, self.edge_in = mlp(dx + dg, w, w), mlp(de, w, w) + self.rounds = nn.ModuleList([Round(w) for _ in range(rounds)]) + self.head = mlp(3 * w, 2, w) + + def forward(self, X, Eu, G, iu): + """X (B,N,dx), Eu (B,P,de) upper-triangle pairs iu=(i,j), G (B,dg) -> (B,P,2) standardised (b, d).""" + B, N, _ = X.shape + i, j = iu + h = self.node_in(torch.cat([X, G[:, None].expand(-1, N, -1)], -1)) + eu = self.edge_in(Eu) + e = torch.zeros(B, N, N, eu.shape[-1], device=X.device, dtype=eu.dtype) + e[:, i, j] = eu + e[:, j, i] = eu + off = (1 - torch.eye(N, device=X.device, dtype=eu.dtype))[:, :, None] + for r in self.rounds: + h, e = r(h, e, off) + return self.head(torch.cat([e[:, i, j], h[:, i] + h[:, j], (h[:, i] - h[:, j]).abs()], -1)) + + +def build(dx, de, dg, w=128, rounds=4): + return TriGNN(dx, de, dg, w, rounds) + + +# ------------------------------------------------------------------------------------------------------- training +def pairs_of(n): + return np.array([(i, j) for i in range(n) for j in range(i + 1, n)]).T.reshape(2, -1) + + +def predict_bucket(model, prep, b, device, max_pairs=2**17): + """(B,P,2) log P (ppg 1.5, 3) predicted for every layout of bucket b.""" + n = b["n"] + iu = torch.as_tensor(pairs_of(n), device=device) + X, E, G, lL = prep.apply(b) + mu, sd = prep.stats["T"] + bs = max(1, max_pairs // (n * n)) + out = [] + model.eval() + with torch.inference_mode(): + for s in range(0, len(X), bs): + T = lambda a: torch.as_tensor(a[s:s + bs], device=device) # noqa: E731 + z = model(T(X), T(E), T(G), iu).double().cpu().numpy() * sd + mu + out.append(z) + z = np.concatenate(out) + bb, dd = z[..., 0], z[..., 1] + y3, y1 = bb - dd, bb - 2 * dd + return np.stack([y1, y3], -1) + lL[:, None, None] + + +def fidelity(logp_hat, pri): + """Per-entry C0 errors (C0 = exp(2 log P3 - log P1.5)): |rel| with 0.1 fF floor, abs fF; |delta error|.""" + c0 = np.exp(2 * np.log(pri[..., 1]) - np.log(pri[..., 0])) + c0h = np.exp(2 * logp_hat[..., 1] - logp_hat[..., 0]) + d = np.log(pri[..., 1] / pri[..., 0]) + dh = logp_hat[..., 1] - logp_hat[..., 0] + err = np.abs(c0h - c0) + return err / np.maximum(c0, 0.1), err, np.abs(dh - d), c0 + + +def summarize(rel, err): + rel, err = np.ravel(rel), np.ravel(err) + return dict(entries=int(len(rel)), median_percent=float(np.median(rel) * 100), p95_percent=float(np.percentile(rel, 95) * 100), + mae_fF=float(err.mean()), rmse_fF=float(np.sqrt((err ** 2).mean()))) + + +def cmd_train(a): + torch.manual_seed(a.seed) + rng = np.random.default_rng(a.seed) + dev = torch.device("cuda" if torch.cuda.is_available() else "cpu") + out = Path(a.out) + out.mkdir(parents=True, exist_ok=False) + buckets = load_p2d(a.p2d) + if a.p2d_holdout_fold is not None: # out-of-fold surrogate for geomfast TRAIN (PROTOCOL amendment 2026-10-17) + for b in buckets: + f = np.array([int(hashlib.sha256(str(k).encode()).hexdigest(), 16) % a.p2d_nfolds for k in b["key"]]) + b.update(subset(b, ~((b["split"] == "train") & (f == a.p2d_holdout_fold)))) + if a.gen: + g = load_gen(a.gen) + if a.gen_frac < 1: # nested subsets by parent index hash (learning curve) + for b in g: + parent = np.array(["/".join(k.split("/")[:2]) for k in b["key"]]) + u = np.array([int(hashlib.sha256(p.encode()).hexdigest(), 16) % 10**6 / 1e6 for p in parent]) + keep = (b["split"] != "train") | (u < a.gen_frac) + b.update(subset(b, keep)) + buckets += g + buckets = [b for b in merge_buckets(buckets) if b["n"] >= 2] + prep = Prep(a.raw).fit(buckets) + data = [] + for b in buckets: + X, E, G, lL = prep.apply(b) + Y = ((prep.targets(b, lL) - prep.stats["T"][0]) / prep.stats["T"][1]).astype(np.float32) + 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), + split=b["split"], group=b["group"], iu=torch.as_tensor(pairs_of(b["n"]), device=dev))) + index = {g: [] for g in GROUPS} + for k, d in enumerate(data): + for g in GROUPS: + idx = np.flatnonzero((d["split"] == "train") & (d["group"] == g)) + if len(idx): + index[g].append((k, idx)) + groups = [g for g in GROUPS if index[g]] + counts = {g: int(sum(len(i) for _, i in index[g])) for g in groups} + val = [(k, np.flatnonzero(d["split"] == "val")) for k, d in enumerate(data)] + model = build(data[0]["X"].shape[-1], data[0]["E"].shape[-1], data[0]["G"].shape[-1], a.width, a.rounds).to(dev) + opt = torch.optim.AdamW(model.parameters(), lr=a.lr, weight_decay=1e-4) + sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=a.lr, total_steps=a.steps, pct_start=0.03, anneal_strategy="cos") + sd = torch.as_tensor(prep.stats["T"][1], device=dev, dtype=torch.float32) + meta = dict(args=vars(a), host=platform.node(), device=str(dev), gpu=torch.cuda.get_device_name(0) if dev.type == "cuda" else None, + train_counts=counts, val_layouts=int(sum(len(i) for _, i in val)), params=sum(p.numel() for p in model.parameters())) + (out / "run.json").write_text(json.dumps(meta, indent=1)) + print(json.dumps(meta), flush=True) + + def batch_loss(k, idx): + d = data[k] + T = lambda z: z[idx].to(dev, non_blocking=True) # noqa: E731 + z = model(T(d["X"]), T(d["E"]), T(d["G"]), d["iu"]) * sd + y = T(d["Y"]) * sd + 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) + + def evaluate(maxn=600): + model.eval() + res = {} + with torch.inference_mode(): + for k, idx in val: + if not len(idx): + continue + d = data[k] + bs = max(1, a.pair_budget // (d["n"] ** 2)) + for s in range(0, min(len(idx), maxn), bs): + l = batch_loss(k, torch.as_tensor(idx[s:s + bs])) + for gname in set(d["group"][idx[s:s + bs]]): + m = d["group"][idx[s:s + bs]] == gname + res.setdefault(gname, []).extend(l[torch.as_tensor(m, device=dev)].tolist()) + model.train() + return {g: float(np.mean(v)) for g, v in res.items()} + + hist, t0, skipped = [], time.perf_counter(), 0 + for step in range(a.steps): + g = groups[rng.integers(len(groups))] + w = np.array([len(i) for _, i in index[g]], float) + k, idx = index[g][rng.choice(len(w), p=w / w.sum())] + n = data[k]["n"] + bs = int(max(1, min(a.max_batch, a.pair_budget // (n * n)))) + b = torch.as_tensor(rng.choice(idx, bs)) + loss = batch_loss(k, b).mean() + if not torch.isfinite(loss): # skipped and counted (PROTOCOL amendment 2026-10-17); abort if frequent + skipped += 1 + print(json.dumps(dict(step=step, skipped_nonfinite=skipped, n=int(n), batch=bs)), flush=True) + if skipped > 100: + raise RuntimeError(f"non-finite loss at step {step} ({skipped} skipped)") + opt.zero_grad(set_to_none=True) + sched.step() + continue + opt.zero_grad(set_to_none=True) + loss.backward() + torch.nn.utils.clip_grad_norm_(model.parameters(), 5) + opt.step() + sched.step() + if step % a.eval_every == 0 or step == a.steps - 1: + ev = evaluate() + hist.append(dict(step=step, loss=float(loss), seconds=time.perf_counter() - t0, val=ev)) + print(json.dumps(hist[-1]), flush=True) + torch.save(dict(state=model.state_dict(), prep=prep.state(), width=a.width, rounds=a.rounds, dims=[data[0]["X"].shape[-1], + data[0]["E"].shape[-1], data[0]["G"].shape[-1]], step=step, history=hist), out / "model.pt") + meta.update(train_seconds=time.perf_counter() - t0, history=hist, skipped_nonfinite=skipped) + (out / "run.json").write_text(json.dumps(meta, indent=1)) + print("TRAIN_DONE", time.perf_counter() - t0, flush=True) + + +def load_model(path, device="cpu"): + c = torch.load(path, map_location=device, weights_only=False) + m = build(*c["dims"], c["width"], c["rounds"]).to(device) + m.load_state_dict(c["state"]) + m.eval() + return m, Prep.from_state(c["prep"]), c + + +def cmd_predict(a): + dev = torch.device("cuda" if torch.cuda.is_available() else "cpu") + model, prep, _ = load_model(a.ckpt, dev) + buckets = load_p2d(a.data) if a.kind == "p2d" else load_gen(a.data) + keys, logp = [], {} + for b in merge_buckets(buckets): + if b["n"] < 2: + continue + sel = b["split"] == "val" if a.split == "val" else np.ones(len(b["key"]), bool) + b = subset(b, sel) + if not len(b["key"]): + continue + lp = predict_bucket(model, prep, b, dev) + for k, v in zip(b["key"], lp): + logp[str(k)] = v.astype(np.float64) + np.savez_compressed(a.out, keys=np.array(list(logp)), **{f"k{i}": v for i, v in enumerate(logp.values())}) + print("PREDICTED", len(logp)) + + +if __name__ == "__main__": + ap = argparse.ArgumentParser() + sp = ap.add_subparsers(dest="cmd", required=True) + p = sp.add_parser("train") + p.add_argument("--p2d", required=True) + p.add_argument("--gen") + p.add_argument("--gen-frac", type=float, default=1.0) + p.add_argument("--out", required=True) + p.add_argument("--raw", action="store_true") + p.add_argument("--steps", type=int, default=20000) + p.add_argument("--lr", type=float, default=1e-3) + p.add_argument("--width", type=int, default=128) + p.add_argument("--rounds", type=int, default=4) + p.add_argument("--pair-budget", type=int, default=2**16) + p.add_argument("--max-batch", type=int, default=256) + p.add_argument("--eval-every", type=int, default=1000) + p.add_argument("--seed", type=int, default=0) + p.add_argument("--p2d-holdout-fold", type=int) + p.add_argument("--p2d-nfolds", type=int, default=2) + p = sp.add_parser("predict") + p.add_argument("--ckpt", required=True) + p.add_argument("--data", required=True) + p.add_argument("--kind", choices=["p2d", "gen"], default="p2d") + p.add_argument("--split", choices=["val", "all"], default="val") + p.add_argument("--out", required=True) + a = ap.parse_args() + {"train": cmd_train, "predict": cmd_predict}[a.cmd](a) diff --git a/geomgnn/paper.py b/geomgnn/paper.py new file mode 100644 index 0000000000000000000000000000000000000000..1cc91ce045a9c3c282bbbffaed0df8f3e3fc37c1 --- /dev/null +++ b/geomgnn/paper.py @@ -0,0 +1,314 @@ +"""Paper Space arms (deploy/registry.yaml: paper_hub, paper_arms; deploy/GNN_SPACE.md "Paper Space"). + +Every arm of the paper's ladder and routes tables that runs from a layout file on a CPU, through one code path: +scene (geomgnn.api.scene_from_prepared) -> geometric embedding -> priors at the arm's mesh levels under the arm's prior stack +-> the arm's combination. Kinds: + gnn geomfast networks (infer.Predictor): full_gnn / direct_gnn / full_mlp / pair_mlp checkpoints; the two levels are + fed as the networks' (1.5, 3) inputs (the coarser-pair arm reads R(0.75, 1.5) there, as it was trained); + learned_prior: the fast model's surrogate replaces the solves + multifid cheap-physics networks (x1_core.Multifid, scripts/multifid/train_mf.py); learned-exponent arms (curve) use the + train_mf.fit anchor (finest level) and curve=True + physics no learned parameter: one level (to_maxwell) or a Richardson pair (x1_core.c0) + final the prior-generation model and its fast mode (tlsq_infer.final, unchanged) +Prior stacks (paper_hub.prior_stacks) are process-wide (geomgnn.api.set_prior_stack); each request installs its arm's stack. +""" + +from __future__ import annotations + +import hashlib +import json +import os +import time +from pathlib import Path + +import numpy as np + +from . import __version__ +from . import api as A +from . import infer as I +from . import x1_core as X +from ._tlsq.embed import fastprior, proxy +from ._tlsq.uge.graph import scene_graph + +lt = I.lt +ALPHA = 0.49685219816506904 # TRAIN-fitted gap share of every paper prior (P2d physics.alpha) +_CACHE: dict = {} + + +def arms(reg) -> dict: + return reg.data.get("paper_arms") or {} + + +def live(reg) -> list[str]: + return [k for k, v in arms(reg).items() if v.get("status", "available") == "available"] + + +def _sha(p) -> str: + return hashlib.sha256(Path(p).read_bytes()).hexdigest() + + +def _model_dir(model_dir=None): + return os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir + + +def files(reg, name, key="checkpoints", model_dir=None) -> list[Path]: + """Bundle copies (//info.json) or the registry's source paths.""" + a = arms(reg)[name] + md = _model_dir(model_dir) + info = Path(md) / name / "info.json" if md else None + if info is not None and info.exists(): + return [Path(md) / name / f for f in json.loads(info.read_text()).get(key, [])] + src = a.get(key) or [] + if key == "surrogate": + src = [a["learned_prior"]["checkpoint"]] if a.get("learned_prior") else [] + return [reg.root / c["path"] for c in src] + + +def _hashes(a, key): + if key == "surrogate": + return [a["learned_prior"]["checkpoint"]["sha256"]] if a.get("learned_prior") else [] + return [c["sha256"] for c in a.get(key) or []] + + +def available(name, reg, model_dir=None) -> bool: + a = arms(reg)[name] + if a["kind"] == "final": + from tlsq_infer import final as F + return F.available(a["final_model"], reg) + return all(f.exists() for k in ("checkpoints", "surrogate") for f in files(reg, name, k, model_dir)) + + +def stack_of(reg, a) -> dict | None: + s = a.get("stack") + return None if s is None else reg["paper_hub"]["prior_stacks"][s] + + +def _check(reg, name, model_dir=None): + a = arms(reg)[name] + for key in ("checkpoints", "surrogate"): + fs, hs = files(reg, name, key, model_dir), _hashes(a, key) + assert len(fs) == len(hs), (name, key) + for f, h in zip(fs, hs): + if _sha(f) != h: + raise RuntimeError(f"{name}: {f.name} SHA256 differs from the registry ({h[:12]})") + return a + + +class _Curve: + """Learned-exponent cheap-physics arms (train_mf.fit with curve=True): anchor = log of the finest level, slope = finest - + next level, the head's exponent p sets m = anchor + slope / (2^p - 1); otherwise x1_core.Multifid.predict_arrays.""" + + def __init__(self, paths, threads=2): + import torch + + from .train_mf import build + torch.set_num_threads(threads) + self.models = [] + for p in paths: + c = torch.load(p, map_location="cpu", weights_only=False) + assert c["curve"] + m = build(len(c["scales"]["X"]["median"]), len(c["scales"]["E"]["median"]), len(c["scales"]["G"]["median"]), 2) + m.load_state_dict(c["state"]) + m.eval() + self.models.append((c, m)) + self.levels = self.models[0][0]["levels"] + assert all(c["levels"] == self.levels for c, _ in self.models) + + def predict(self, row, levels): + nk, ek, gk = X.schema_of(self.models[0][0]) + X0, E0, G0, pairs = X.raw_arrays(row, nk, ek, gk) + i, j = pairs.T + sig = np.array([nm != "G" for nm in row["names"]]) + lev = {p: np.asarray(levels[p], float)[i, j] for p in self.levels} + import torch + out = [] + for c, m in self.models: + L = [np.log(np.asarray(lev[p], float)) for p in self.levels] + assert all(np.isfinite(v).all() for v in L) + lf, lc = L[-1], L[-2] + anchor, slope = lf, lf - lc + E = np.concatenate([E0] + [(L[k + 1] - L[k])[:, None].astype(np.float32) for k in range(len(L) - 1)], -1) + Xn, En, Gn = X.norm(X0, c["scales"]["X"])[None], X.norm(E, c["scales"]["E"])[None], X.norm(G0[None], c["scales"]["G"]) + bbase = np.where(sig, c["closure"]["1"], c["closure"]["0"]).astype(np.float32)[None] + T = lambda a: torch.from_numpy(np.ascontiguousarray(a, dtype=np.float32)) # noqa: E731 + n = len(sig) + with torch.inference_mode(): + C = m(T(Xn), T(En), T(Gn), torch.ones((1, n), dtype=torch.bool), torch.ones((1, len(pairs)), dtype=torch.bool), + torch.from_numpy(pairs), T(anchor[None].astype(np.float32)), T(slope[None].astype(np.float32)), T(bbase), True)[0] + out.append(C[0].numpy().astype(np.float64)) + r = np.mean(out, 0) + assert np.isfinite(r).all() + return r + + +def load(name, reg, model_dir=None, threads=2): + """The arm's network object (cached): infer.Predictor (gnn, with .surrogate), x1_core.Multifid / _Curve (multifid), None.""" + key = (str(reg.path), name, str(_model_dir(model_dir)), int(threads)) + if key in _CACHE: + return _CACHE[key] + a = _check(reg, name, model_dir) + obj = None + if a["kind"] == "gnn": + obj = I.Predictor(files(reg, name, "checkpoints", model_dir), threads=threads) + if obj.arm != a["arm"]: + raise RuntimeError(f"{name}: checkpoints are arm {obj.arm!r}, registry says {a['arm']!r}") + phys = obj.models[0][0].get("schema", {}).get("physics", {}) + if not obj.direct and float(phys.get("alpha")) != ALPHA: + raise RuntimeError(f"{name}: checkpoint alpha {phys.get('alpha')} != {ALPHA}") + obj.surrogate = None + if a.get("learned_prior"): + from . import learned_prior + model, prep, _ = learned_prior.load(files(reg, name, "surrogate", model_dir)[0]) + obj.surrogate = (model, prep) + if obj.direct != (a.get("stack") is None and not a.get("learned_prior")): + raise RuntimeError(f"{name}: solver-free checkpoints iff no prior stack and no learned prior") + elif a["kind"] == "multifid": + paths = files(reg, name, "checkpoints", model_dir) + import torch + curve = torch.load(paths[0], map_location="cpu", weights_only=False)["curve"] + obj = _Curve(paths, threads) if curve else X.Multifid(paths, threads) + if [float(p) for p in obj.levels] != [float(p) for p in a["levels"]]: + raise RuntimeError(f"{name}: checkpoint levels {obj.levels} != registry {a['levels']}") + _CACHE[key] = obj + return obj + + +def levels_of(a) -> list[float]: + return [float(p) for p in (a.get("levels") or a.get("ppg") or [])] + + +def apply_stack(reg, a, threads=2): + st = stack_of(reg, a) + if st is not None: + A.set_prior_impl("frozen") + A.set_prior_stack(st, threads) + A._thread_limits(threads) + + +def solve_levels(sc, levels, threads=2) -> tuple[dict, dict]: + """Positive-mutual prior matrices P = -C at each level under the installed stack (the statements of + geomgnn.api.predict_scene's prior loop, any level list).""" + from ._tlsq.embed.fastprior_loc import micro_mom_loc + proxy.NUM_THREADS = threads + if set(A.V2_KWARGS) & set(A._STATE["patches"]): + from . import priorv2 + priorv2.clear() + out, tm = {}, {} + old = fastprior.ALPHA_GAP_SHARE + try: + fastprior.ALPHA_GAP_SHARE = ALPHA + for p in levels: + t = time.perf_counter() + with proxy.substrate("slab_open", sc.stack["substrate_thickness_um"]), A._quiet_mixed_cast(): + 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) + out[p] = -mat + tm[f"{p:g}"] = time.perf_counter() - t + finally: + fastprior.ALPHA_GAP_SHARE = old + return out, tm + + +def combine(a, obj, row, P) -> np.ndarray: + """The arm's Maxwell matrix from the embedding row and the priors P {ppg: -C} (scene order).""" + k = a["kind"] + if k == "physics": + lv = levels_of(a) + return X.to_maxwell(P[lv[0]]) if len(lv) == 1 else X.c0(P, lv[0], lv[1]) + if k == "multifid": + return obj.predict(row, P) + if obj.direct: + return obj.from_features(row) + if obj.surrogate is not None: + from . import learned_prior + return obj.from_features(row, learned_prior.priors(*obj.surrogate, row)) + lo, hi = levels_of(a) + return obj.from_features(row, {1.5: P[lo], 3.0: P[hi]}) + + +def predict_scene(reg, name, sc, threads=2, model_dir=None) -> dict: + a = arms(reg)[name] + obj = load(name, reg, model_dir, threads) + apply_stack(reg, a, threads) + t0 = time.perf_counter() + row = lt.add_tokens(sc, scene_graph(sc)) + t1 = time.perf_counter() + need = [] if (a["kind"] == "gnn" and (obj.direct or obj.surrogate is not None)) else levels_of(a) + P, tl = solve_levels(sc, need, threads) if need else ({}, {}) + t2 = time.perf_counter() + C = np.asarray(combine(a, obj, row, P), float) + t3 = time.perf_counter() + return dict(C=C, names=row["names"], row=row, priors=P, + timing=dict(geometry=t1 - t0, prior=t2 - t1, prior_levels=tl, network=t3 - t2, total=t3 - t0)) + + +def predict(prepared, name, reg, substrate="Si (cryogenic, training)", thickness_um=350.0, model_dir=None, threads=None, + self_from_rows=True, trust_rule=None) -> dict: + """Full N x N Maxwell matrix (fF; ground row/column 0) of a prepared layout with a paper_arms entry.""" + a = arms(reg)[name] + threads = int(threads or os.environ.get("TLSQ_NUM_THREADS") or 2) + if a["kind"] == "final": + from tlsq_infer import final as F + tr = bool(reg["final_defaults"].get("trust_rule", True)) if trust_rule is None else bool(trust_rule) + res = F.predict_final(prepared, a["final_model"], substrate=substrate, thickness_um=thickness_um, self_from_rows=bool(self_from_rows), + trust_rule=tr if a.get("trust_rule_applies") else False) + res.update(arm=name, arm_title=a["title"], paper_name=a["paper_name"], route=a.get("route")) + return res + eps = reg.eps_of(substrate) + if abs(eps - A.EPS_TRAIN) > 1e-9 or abs(float(thickness_um) - A.T_TRAIN_UM) > 1e-9: + 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 " + f"are served for that stack only (requested eps_r {eps:g}, {float(thickness_um):g} µm); the prior-generation model " + "accepts other substrates") + t = time.perf_counter() + load(name, reg, model_dir, threads) + load_s = time.perf_counter() - t + sc = A.scene_from_prepared(prepared) + try: + r = predict_scene(reg, name, sc, threads, model_dir) + except AssertionError as e: + raise ValueError(f"{name}: the prior or the features of this layout are not finite (no matrix returned): {e}") from e + C = r["C"] + chk = A.physical_checks(C) + warnings = [w for w in prepared.warnings if "beyond the validated range" not in w] + if a.get("warning"): + warnings.insert(0, a["warning"]) + if not (chk["symmetric_max_abs"] <= 1e-6 * max(1.0, float(np.abs(C).max())) and chk["offdiag_all_negative"] + and (chk["signal_block_min_eig"] is None or chk["signal_block_min_eig"] > 0)): + warnings.append(f"physical check failed: {chk}") + st = stack_of(reg, a) + tm = r["timing"] + return {"names": ["G"] + list(prepared.names), "matrix_fF": C.tolist(), "units": "fF", + "convention": "Maxwell matrix: diagonal = self, off-diagonal = -mutual; row/column 0 = ground", + "self_capacitance": a.get("self_note") or ("predicted by the network (learned positive row-sum closure). The ground self term " + "depends on the chip boundary: indicative only"), + "model": name, "arm": name, "arm_title": a["title"], "paper_name": a["paper_name"], "route": a.get("route"), "kind": a["kind"], + "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 [], + "weights_sha256": _hashes(a, "checkpoints"), "learned_prior_sha256": (_hashes(a, "surrogate") or [None])[0], + "prior_stack": (None if st is None else {"name": a["stack"], "patches": A._STATE.get("patches", []), "mode": A._STATE.get("mode") or "frozen", + "alpha": ALPHA, "method": "local_gap micro-MoM (slab)"}), + "package_version": f"geomgnn {__version__}", "registry_version": reg["version"], "substrate": "Si (cryogenic, training)", + "eps_r": A.EPS_TRAIN, "thickness_um": A.T_TRAIN_UM, + "timing_s": {"load_s": load_s, "geometry_s": tm["geometry"], "prior_s": tm["prior"], "prior_levels_s": tm["prior_levels"], + "network_s": tm["network"], "total_s": tm["total"], "threads": threads}, + "paper": a.get("paper") or {}, "checks": chk, "warnings": warnings, "stored": False} + + +STARTUP: dict = {} + + +def preload(reg, example=None, names=None) -> dict: + """Server start: load every live arm (SHA checks) and run the default arm on a small example (numba warmup of the compiled + solver included), so the first request does not pay the compile.""" + t0 = time.perf_counter() + for k in names or live(reg): + if arms(reg)[k]["kind"] != "final" and available(k, reg): + load(k, reg) + STARTUP["load_all_s"] = time.perf_counter() - t0 + d = reg["paper_hub"]["default_arm"] + if example is not None and Path(example).exists(): + from tlsq_infer.layout import prepare + t = time.perf_counter() + predict(prepare(str(example)), d, reg) + STARTUP["example_s"] = time.perf_counter() - t + STARTUP.update(default=d, numba_warmup_s=A._STATE.get("warmup_s"), total_s=time.perf_counter() - t0) + print("preload " + json.dumps({k: (round(v, 2) if isinstance(v, float) else v) for k, v in STARTUP.items()}), flush=True) + return dict(STARTUP) diff --git a/geomgnn/pilot_p2.py b/geomgnn/pilot_p2.py new file mode 100644 index 0000000000000000000000000000000000000000..5cd7702720fcc5bb9e1985c95aa4430ce0089031 --- /dev/null +++ b/geomgnn/pilot_p2.py @@ -0,0 +1,214 @@ +"""Isolated P1 pilot. No external-test loaders or paths are implemented.""" +import os +os.environ.setdefault('PYTHONDONTWRITEBYTECODE','1') +import argparse, hashlib, json, pickle, time, platform, sys +from pathlib import Path +import numpy as np +ARMS=['mesh','pair_mlp','full_mlp','full_gnn','fast_gnn','direct_gnn','lofo_pair','lofo_full'] +ROOT=Path(__file__).resolve().parent + +def digest(p): return hashlib.sha256(Path(p).read_bytes()).hexdigest() +def qc(x): + C=x['C']; n=len(x['names']); sig=np.array([n!='G' for n in x['names']]) + assert C.shape==(n,n) and np.isfinite(C).all(), 'shape/finite' + assert np.max(abs(C-C.T))<1e-5*max(1,np.max(abs(C))), 'symmetry' + assert np.all(np.diag(C)>0), 'diagonal' + assert np.max(C[~np.eye(n,dtype=bool)])<=1e-6, 'mutual sign' + assert np.min(C.sum(1)[sig])>=-1e-6, 'closure' + assert np.linalg.eigvalsh(C[np.ix_(sig,sig)]).min()>0, 'positive signal block' + for p in [.75,1.5,3.]: + v=x['priors'][p][np.triu_indices(n,1)]; assert np.isfinite(v).all() and np.all(v>0), 'prior positive' + +def feature(x): + t=time.perf_counter() + try: + qc(x) + if x['nodes'] is None: + from tlsq.uge.scene import scene_from_gds,tlsq_namer + from tlsq.uge.blocks import node_features,global_features + sc=scene_from_gds(x['gds'],namer=tlsq_namer) + assert len(set(sc.names()))==sc.n and set(sc.names())==set(x['names']), 'net mapping' + x['nodes']=[node_features(sc,sc.names().index(name)) for name in x['names']] + x['global_features'].update(global_features(sc)) + x['node_features_s']=time.perf_counter()-t + else: x['node_features_s']=None + for n in x['nodes']: + for k,v in n.items(): + if not k.startswith('proxy_'): assert np.isfinite(v), 'node feature finite' + return x,None + except Exception as e:return None,dict(key=x['key'],reason=repr(e)) + +def prepare(a): + from concurrent.futures import ProcessPoolExecutor + sys.path.insert(0,str(ROOT/'vendor')) + items=pickle.load(open(a.input,'rb')); assert all(x['split'] in ['train','val'] for x in items) + start=time.time() + with ProcessPoolExecutor(a.workers) as pool: result=list(pool.map(feature,items)) + good=[x for x,e in result if x is not None]; bad=[e for x,e in result if e] + assert good and len(good)>0.9*len(items),(len(good),bad[:5]) + # Conservative feature-identity audit; all labels remain unused in this decision. + def geometry_key(x): + vals=[[{k:round(float(v),9) for k,v in nd.items() if not k.startswith('proxy_')} for nd in x['nodes']], + [(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())]] + return hashlib.sha256(json.dumps(vals,sort_keys=True).encode()).hexdigest() + validation_geometry={geometry_key(x) for x in good if x['split']=='val'} + retained=[] + for x in good: + if x['split']=='train' and geometry_key(x) in validation_geometry: + bad.append(dict(key=x['key'],reason='geometry descriptor duplicate of validation; training copy excluded')) + else: retained.append(x) + good=retained + nodekeys=sorted(set.intersection(*[set(nd) for x in good for nd in x['nodes']])) + nodekeys=[k for k in nodekeys if not k.startswith('proxy_')] + edgekeys=sorted(set.intersection(*[set(e) for x in good for e in x['edges'].values()])) + edgekeys=[k for k in edgekeys if not k.startswith(('proxy_','conf_','_'))] + globkeys=['log_chip_area','log_pocket_area','log_substrate_t','eps_eff'] + 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)]) + F=len(pairs); X=np.zeros((B,N,len(nodekeys)),np.float32); E=np.zeros((B,F,len(edgekeys)),np.float32) + 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) + pri=np.zeros((B,F,3),np.float32); sig=np.zeros((B,N),bool) + for b,x in enumerate(good): + n=len(x['names']); M[b,:n]=True; sig[b,:n]=[v!='G' for v in x['names']]; C[b,:n,:n]=x['C'] + X[b,:n]=[[nd[k] for k in nodekeys] for nd in x['nodes']]; G[b]=[x['global_features'][k] for k in globkeys] + for f,(i,j) in enumerate(pairs): + if j>=n:continue + PM[b,f]=True; E[b,f]=[x['edges'][(i,j)][k] for k in edgekeys] + pri[b,f]=[x['priors'][p][i,j] for p in [.75,1.5,3.]] + assert np.isfinite(E).all() and np.isfinite(G).all() + out=Path(a.output); out.mkdir(parents=True,exist_ok=True) + np.savez_compressed(out/'data.npz',X=X,E=E,G=G,C=C,M=M,PM=PM,pri=pri,sig=sig,pairs=pairs, + 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]), + nodekeys=np.array(nodekeys),edgekeys=np.array(edgekeys),globkeys=np.array(globkeys)) + report=dict(accepted=len(good),excluded=bad,seconds=time.time()-start,nodekeys=nodekeys,edgekeys=edgekeys,source_hash=digest(a.input),code_hash=digest(__file__), + 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']}, + node_feature_seconds=[dict(key=x['key'],seconds=x['node_features_s']) for x in good if x['node_features_s'] is not None], + 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']) + (out/'prep.json').write_text(json.dumps(report,indent=2)); print('PREP_DONE',len(good),'excluded',len(bad),flush=True) + + +def build_model(dx,de,dg,rounds,width=64): + import torch + from torch import nn + def mlp(i,o):return nn.Sequential(nn.Linear(i,width),nn.SiLU(),nn.Linear(width,o)) + class Net(nn.Module): + def __init__(self): + super().__init__(); self.node=mlp(dx+dg,width); self.edge=mlp(de,width) + self.msg=nn.ModuleList([mlp(3*width,width) for _ in range(rounds)]) + self.up=nn.ModuleList([mlp(2*width,width) for _ in range(rounds)]) + self.head=mlp(3*width,1); self.closure=mlp(width,1) + nn.init.zeros_(self.head[-1].weight); nn.init.zeros_(self.head[-1].bias) + nn.init.zeros_(self.closure[-1].weight); nn.init.zeros_(self.closure[-1].bias) + def forward(self,X,E,G,M,PM,pairs,base,bbase,direct=False): + B,N,_=X.shape; i,j=pairs[:,0],pairs[:,1] + h=self.node(torch.cat([X,G[:,None].expand(-1,N,-1)],-1))*M[:,:,None]; e=self.edge(E) + for msg,up in zip(self.msg,self.up): + v=msg(torch.cat([h[:,i]+h[:,j],abs(h[:,i]-h[:,j]),e],-1))*PM[:,:,None] + agg=torch.zeros_like(h); agg.index_add_(1,i,v); agg.index_add_(1,j,v) + degree=torch.zeros_like(M,dtype=X.dtype); degree.index_add_(1,i,PM.float()); degree.index_add_(1,j,PM.float()) + h=(h+up(torch.cat([h,agg/degree.clamp_min(1)[:,:,None]],-1)))*M[:,:,None] + raw=self.head(torch.cat([h[:,i]+h[:,j],abs(h[:,i]-h[:,j]),e],-1)).squeeze(-1) + logc=(base+(raw if direct else .2*torch.tanh(raw))).clamp(-25,20) + mutual=logc.exp()*PM; b=(bbase+self.closure(h).squeeze(-1)).clamp(-20,20).exp()*M + C=torch.zeros((B,N,N),device=X.device); C[:,i,j]=-mutual; C[:,j,i]=-mutual + diag=-C.sum(-1)+b; C=C+torch.diag_embed(diag) + return C,logc + return Net() + + +def fit(a): + import torch + from torch.nn import functional as F + torch.set_num_threads(a.threads); torch.manual_seed(a.seed); rng=np.random.default_rng(a.seed) + d=dict(np.load(a.data,allow_pickle=False)); arm=a.arm; out=Path(a.output); out.mkdir(parents=True,exist_ok=False) + (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)) + tr=(d['split']=='train') & ((d['family']!='concentric') if arm.startswith('lofo') else True); va=d['split']=='val' + assert tr.any() and va.any() and not np.any(tr&va) + X=d['X'].copy(); E=d['E'].copy(); G=d['G'].copy() + ni=list(d['nodekeys']).index('role_ground'); ei=list(d['edgekeys']).index('is_ground_pair') + full=arm not in ['mesh','pair_mlp','lofo_pair']; direct=arm=='direct_gnn'; fast=arm=='fast_gnn' + if not full:X=X[:,:,[ni]]; G=np.zeros((len(X),1),np.float32) + if arm=='mesh':E=E[:,:,[ei]] + lo,hi=(0,1) if fast else (1,2); lp=np.log(d['pri'].clip(1e-12)); base=2*lp[:,:,hi]-lp[:,:,lo] + if not direct:E=np.concatenate([E,(lp[:,:,hi]-lp[:,:,lo])[:,:,None]],-1) + else: + for g in [0,1]: + mask=(d['E'][:,:,ei]==g)&d['PM']&tr[:,None] + v=(-d['C'][:,d['pairs'][:,0],d['pairs'][:,1]]) + base[d['E'][:,:,ei]==g]=np.median(np.log(v[mask].clip(1e-12))) + scales={} + for name,z,mask in [('X',X,d['M']&tr[:,None]),('E',E,d['PM']&tr[:,None]),('G',G,tr)]: + 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) + z[:]=np.clip((z-med)/scale,-10,10); scales[name]=dict(median=med.tolist(),iqr=scale.tolist()) + bbase=np.zeros(d['M'].shape,np.float32); closure=d['C'].sum(-1).clip(1e-8) + for is_signal in [False,True]: + mask=(d['sig']==is_signal)&d['M']&tr[:,None]; bbase[d['sig']==is_signal]=np.median(np.log(closure[mask])) + T=lambda x:torch.from_numpy(np.asarray(x)) + 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']]) + rounds=2 if arm in ['full_gnn','fast_gnn','direct_gnn','lofo_full'] else 0 + 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) + sched=torch.optim.lr_scheduler.CosineAnnealingLR(opt,a.steps,eta_min=1e-5) + idx=np.flatnonzero(tr); fam=d['family']; groups=[idx[fam[idx]==f] for f in sorted(set(fam[idx]))] + truth=(-C[:,pairs[:,0],pairs[:,1]]).clamp_min(1e-12).log(); hist=[]; t0=time.perf_counter() + for step in range(a.steps): + b=np.array([rng.choice(groups[g]) for g in rng.integers(len(groups),size=64)]) + pred,logc=model(X[b],E[b],G[b],M[b],PM[b],pairs,base[b],bbase[b],direct) + pairloss=(F.huber_loss(logc,truth[b],delta=.05,reduction='none')*PM[b]).sum(1)/PM[b].sum(1).clamp_min(1) + 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) + groundmask=M[b]&~sig[b] + 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) + loss=(pairloss+.1*selfloss+.02*groundloss).mean(); assert torch.isfinite(loss) + opt.zero_grad(); loss.backward(); torch.nn.utils.clip_grad_norm_(model.parameters(),5); opt.step(); sched.step() + if step%250==0:hist.append(dict(step=step,loss=float(loss.detach()),seconds=time.perf_counter()-t0)); print(hist[-1],flush=True) + train_s=time.perf_counter()-t0 + 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') + ev=np.flatnonzero(va); model.eval(); preds=[] + with torch.inference_mode(): + for chunk in np.array_split(ev,max(1,int(np.ceil(len(ev)/64)))): + preds.append(model(X[chunk],E[chunk],G[chunk],M[chunk],PM[chunk],pairs,base[chunk],bbase[chunk],direct)[0].numpy()) + b=ev[:1] + for _ in range(10):model(X[b],E[b],G[b],M[b],PM[b],pairs,base[b],bbase[b],direct) + times=[] + for _ in range(100): + 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) + pred=np.concatenate(preds); np.savez_compressed(out/'validation_predictions.npz',key=d['key'][ev],C=pred) + 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) + rec=[] + for family in sorted(set(fam[ev])): + 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 + masks={'mutual':pm,'signal_signal':pm&ss[:,ii]&ss[:,jj],'signal_ground':pm&~(ss[:,ii]&ss[:,jj])} + for kind,mask in masks.items(): + if mask.any():rec.append(metric(family,kind,-targets[:,ii,jj][mask],-p[:,ii,jj][mask],train_s,times)) + rec.append(metric(family,'signal_self',targets.diagonal(axis1=1,axis2=2)[ss],p.diagonal(axis1=1,axis2=2)[ss],train_s,times)) + prior=np.exp(2*lp[ev][:, :,2]-lp[ev][:,:,1])[sel] + rec.append(metric(family,'C0_mutual',-targets[:,ii,jj][pm],prior[pm],train_s,times)) + import pandas as pd + pd.DataFrame(rec).to_csv(out/'metrics.csv',index=False) + 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() + (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)) + import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot as plt + 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] + 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) + 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) + print('RUN_DONE',arm,train_s,flush=True) + +def metric(family,kind,y,p,train_s,times): + 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))) + +def smoke(): + import torch + torch.set_num_threads(2); torch.manual_seed(4) + net=build_model(3,4,2,2); pairs=torch.tensor([[0,1],[0,2],[1,2]]) + 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() + for p in net.parameters():torch.nn.init.normal_(p,0,.05) + C,_=net(X,E,G,M,PM,pairs,base,bbase) + assert torch.allclose(C,C.transpose(1,2)); assert torch.linalg.eigvalsh(C).min()>0 + perm=torch.tensor([2,0,1]); edgeperm=torch.tensor([1,2,0]); C2,_=net(X[:,perm],E[:,edgeperm],G,M,PM,pairs,base,bbase) + assert torch.allclose(C2,C[:,perm][:,:,perm],atol=1e-6),'permutation equivariance' + C.sum().backward(); assert all(p.grad is None or torch.isfinite(p.grad).all() for p in net.parameters()) + print('SMOKE_PASS: symmetry, positive definiteness, permutation equivariance, finite backward') + +if __name__=='__main__': + p=argparse.ArgumentParser(); sub=p.add_subparsers(dest='cmd',required=True) + 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) + 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) + sub.add_parser('smoke'); a=p.parse_args(); {'prepare':prepare,'fit':fit,'smoke':lambda a:smoke()}[a.cmd](a) diff --git a/geomgnn/priorv2.py b/geomgnn/priorv2.py new file mode 100644 index 0000000000000000000000000000000000000000..e216c68d79ecf4709ea3e6c167bbf2000646ae7a --- /dev/null +++ b/geomgnn/priorv2.py @@ -0,0 +1,215 @@ +"""Prior v2 = frozen geomfast prior + rejtail fix fc (rescue panels), as a monkeypatch of fastprior._window_solve. +Logic copied from scripts/rejtail/rt.py (policies 'exact' and 'fc' only; no wp dependency). Vendor files untouched. +Import after the geomfast vendor dir is on sys.path. Usage: + import priorv2; with priorv2.installed(): ... fastprior.micro_mom / micro_mom_loc ... +fc acts only on windowed solves (all_rhs False, N > 9) whose FIRST attempt leaves a metal without a panel; everything +else runs the frozen code path.""" +import time +import contextlib +import numpy as np +import shapely +from ._tlsq.embed import fastprior as F, proxy as P + +PRIOR_VERSION = 'v2_fc_dipfill' +TRACE = [] +POLICY = ['fc'] + + +def _solve(c, wh, o, metals, cols, eps_r, all_rhs): + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + Bm = (o[:, None] == cols[None, :]).astype(float) + Q = P._chol_solve(A, Bm) if all_rhs else F._sym_solve(A, Bm) + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols] + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + + +def _mesh(metals, n_core, hh, cap, refined, majority, levels): + if not refined: + return F._lite_mesh(metals, hh, cap, n_core, levels) + if levels: + return F._graded_mesh(metals, n_core, hh, levels, majority) + return P.mesh_flagship(metals, hh, cap, majority=majority) + + +def _rescue_panels(metals, miss, h): + """Each panel-less metal cut by a lattice of pitch min(h, sqrt(area) / 2) anchored at its bbox; one panel per piece, + centre = centroid (point on surface if outside), size = piece bbox (the frozen cut-piece form).""" + cs, whs, os_ = [], [], [] + for a in miss: + g = metals[a] + x0, y0, x1, y1 = g.bounds + p = min(h, np.sqrt(g.area) / 2) + nx, ny = max(1, int(np.ceil((x1 - x0) / p))), max(1, int(np.ceil((y1 - y0) / p))) + gx, gy = np.meshgrid(x0 + p * np.arange(nx), y0 + p * np.arange(ny)) + pcs = shapely.intersection(shapely.box(gx.ravel(), gy.ravel(), gx.ravel() + p, gy.ravel() + p), g) + pcs = pcs[shapely.area(pcs) > 1e-12] + bb = shapely.bounds(pcs) + cen = shapely.get_coordinates(shapely.centroid(pcs)) + inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1]) + if not inside.all(): + cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs[~inside])) + cs.append(cen) + whs.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6)) + os_.append(np.full(len(pcs), a)) + return np.concatenate(cs), np.concatenate(whs), np.concatenate(os_).astype(int) + + +def window_solve(metals, n_core, h, budget, eps_r, all_rhs, levels=()): + pol = POLICY[0] if not all_rhs else 'exact' + cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core) + t_mesh = t_sol = 0.0 + hh, cap, refined, majority = h, budget, 0, False + tr = dict(n_core=n_core, n_metals=len(metals), all_rhs=all_rhs, policy=pol, attempts=[]) + best, hist = None, [] + while True: + t = time.perf_counter() + c, wh, o, hh, n_raw = _mesh(metals, n_core, hh, cap, refined, majority, levels) + miss = np.nonzero(np.bincount(o, minlength=len(metals)) == 0)[0] + att = dict(refined=refined, majority=majority, panels=len(c), missing=miss.tolist()) + t_mesh += time.perf_counter() - t + tr['attempts'].append(att) + cs, whs, os_ = c, wh, o + if pol != 'exact' and len(miss): + empty = np.array([metals[a].is_empty for a in miss]) + if pol == 'fc' and not refined and (not empty.any() or len(c) <= P.DENSE_MAX): + t = time.perf_counter() + if not empty.all(): + r = _rescue_panels(metals, miss[~empty], hh) + cs, whs, os_ = np.concatenate([c, r[0]]), np.concatenate([wh, r[1]]), np.concatenate([o, r[2]]).astype(o.dtype) + att['rescued'] = int(len(r[0])) + if empty.any(): + att['accept'] = 'empty' + t_mesh += time.perf_counter() - t + else: + att['defer'] = True + if att.get('defer'): + doomed = len(c) > P.DENSE_MAX + hist.append(('doomed',) if doomed else ('mesh', c, wh, o)) + att['solve'] = 'doomed' if doomed else 'deferred' + ok = False + else: + t = time.perf_counter() + try: + R = _solve(cs, whs, os_, metals, cols, eps_r, all_rhs) + off = np.ones(R.shape, bool) + off[np.arange(len(cols)), cols] = False + ok = bool(np.all(np.bincount(os_, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0))) + if att.get('accept') == 'empty': + ok = True + att['solve'] = 'ok' + best = R + hist.append(('R', R)) + except np.linalg.LinAlgError as e: + ok = False + att['solve'], att['err'] = 'error', str(e)[:120] + hist.append(('err',)) + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < F.LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + if not ok: # frozen R binding: the latest attempt that solved (deferred dense attempts solved lazily, newest first) + R = None + t = time.perf_counter() + for e in reversed(hist): + if e[0] == 'R': + R = e[1] + break + if e[0] == 'mesh': + try: + R = _solve(e[1], e[2], e[3], metals, cols, eps_r, all_rhs) + break + except np.linalg.LinAlgError: + continue + t_sol += time.perf_counter() - t + assert R is not None, 'no attempt solved (the frozen code raises here too)' + else: + R = best + tr['fired'] = bool(pol == 'fc' and tr['attempts'][0]['missing']) + TRACE.append(tr) + return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, + "assemble_s": 0.0, "solve_s": t_sol} + + +FILL = [] # one entry per formula_prior call replaced by the dipole fill: number of pairs +MARGIN = [] # one entry per window whose margin was widened: (d, margin) +_FROZEN = dict(formula_prior=F.formula_prior, lite_window=F._lite_window, dipole_far=F.dipole_far) +K_TOL = 1e-4 + + +def slab_R(d, t_um=350.0, eps_r=11.45): + """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).""" + d = np.asarray(d, float) + K = (eps_r - 1) / (eps_r + 1) + n_max = int(np.ceil(np.log(K_TOL) / np.log(K))) if K > 0 else 0 + n = np.arange(1, n_max + 1)[:, None] + u = (n * t_um / d[None]) ** 2 + return 1 + 2 * np.sum((-K) ** n * (1 - 8 * u) / (1 + 4 * u) ** 2.5, 0) + + +def dipole_far_slab(G, conds, pairs, eps_r=11.45, gap=None): + """src/tlsq/embed/fastprior_farv4.dipole_far_v4 verbatim: the frozen dipole_far with (1 + eps_r) -> 1 + eps_r R(d).""" + base = _FROZEN['dipole_far'](G, conds, pairs, eps_r, gap) + pairs = np.asarray(pairs, int).reshape(-1, 2) + cen = shapely.get_coordinates(shapely.centroid(np.array([G] + list(conds), dtype=object))) + d = np.hypot(*(cen[pairs[:, 0]] - cen[pairs[:, 1]]).T) + return base * (1 + eps_r * slab_R(d, float(P.SUBSTRATE["t_um"]), eps_r)) / (1 + eps_r) + + +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): + """Replaces formula_prior inside micro_mom (its only caller there: same-pocket far pairs): the same pairs get + dipole_far(G, conds, pairs, eps_r, gap), the fill micro_mom uses for cross-pocket far pairs.""" + pairs = np.asarray(pairs, int).reshape(-1, 2) + N = len(conds) + 1 + C = np.zeros((N, N)) + v = F.dipole_far(G, conds, pairs, eps_r, gap) + C[pairs[:, 0], pairs[:, 1]] = v + C[pairs[:, 1], pairs[:, 0]] = v + FILL.append(len(pairs)) + return C, {"dipole_fill_pairs": len(pairs)} + + +def lite_window_margin(G, conds, core, R_g, margin, fill_um, tree, etched): + """Frozen _lite_window, except margin' = 2 d when d = max distance of a core conductor to the chip ground > margin.""" + d = max(shapely.distance(conds[i], G) for i in core) + if d > margin: + MARGIN.append((float(d), float(margin))) + margin = 2 * d + return _FROZEN['lite_window'](G, conds, core, R_g, margin, fill_um, tree, etched) + + +@contextlib.contextmanager +def installed(policy='fc', dip=True, margin=False, slab_alpha=None): + """Install prior v2 pieces: window solve policy 'fc' (or 'exact' = traced frozen copy, or None = vendor as is), the + dipole fill of same-pocket far pairs (dip), the window margin widening (margin) and, for arm v2-slab, the slab dipole + for every far pair with ALPHA_GAP_SHARE = slab_alpha. Always restored.""" + saved = (F._window_solve, F.formula_prior, F._lite_window, F.dipole_far, F.ALPHA_GAP_SHARE) + if policy is not None: + POLICY[0] = policy + F._window_solve = window_solve + if dip: + F.formula_prior = formula_as_dipole + if margin: + F._lite_window = lite_window_margin + if slab_alpha is not None: + F.dipole_far, F.ALPHA_GAP_SHARE = dipole_far_slab, float(slab_alpha) + try: + yield + finally: + F._window_solve, F.formula_prior, F._lite_window, F.dipole_far, F.ALPHA_GAP_SHARE = saved + POLICY[0] = 'fc' + + +def clear(): + TRACE.clear() + FILL.clear() + MARGIN.clear() + + +def fired(trace): + """True if fc changed the solve of any window of the traced call.""" + return any(t.get('fired') for t in trace) diff --git a/geomgnn/rejtail_fv.py b/geomgnn/rejtail_fv.py new file mode 100644 index 0000000000000000000000000000000000000000..e1f3e9a1c4d168780cd664c493e7e4c87cd11a75 --- /dev/null +++ b/geomgnn/rejtail_fv.py @@ -0,0 +1,115 @@ +"""rejtail fix fv (agents/rejtail/RESULTS.md, scripts/rejtail/rt.py; user approval 2026-10-17): value-preserving removal of +the rejection tail of the windowed micro-MoM prior (`fastprior._window_solve`, all_rhs=False, layouts with > 8 conductors). + +A window attempt whose mesh leaves a metal without a panel is always rejected, whatever its solve returns. The frozen code +still solves it, and above DENSE_MAX panels the H-matrix PCG cannot converge: the panel-less metal has a zero right-hand +side, so the residual is 0/0 = NaN, and it runs 1,000 iterations before raising. fv meshes such attempts but does not +solve them. If the loop ends without an accepted attempt, the result the unpatched code returns is rebuilt by solving +the deferred dense attempts lazily: + keep_last_solved=True (frozen geomfast stage): the latest attempt whose solve succeeded, newest first; + keep_last_solved=False (tlsq_infer vendor): the last attempt's own solve, else the same RuntimeError. +Every accepted solve is the same call on the same mesh, so the result is bit-identical (621 layouts in RESULTS.md; +deploy/tests/test_gnn_space.py re-checks it). all_rhs=True calls (single window, N <= 8) run the original function. +The same file is shipped as geomgnn/rejtail_fv.py and tlsq_infer/_vendor/rejtail_fv.py; only the import line differs. +""" +from __future__ import annotations + +import time + +import numpy as np + +from ._tlsq.embed import fastprior as F +from ._tlsq.embed import proxy as P + +KEEP_LAST_SOLVED = True +_ORIG = {} + + +def _solve(c, wh, o, metals, cols, eps_r): + """One attempt solved exactly as the original loop does (windowed: LDL^T dense, H-matrix PCG above DENSE_MAX). + Returns (R, assemble seconds).""" + t = time.perf_counter() + t_asm = 0.0 + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + t_asm = time.perf_counter() - t + Bm = (o[:, None] == cols[None, :]).astype(float) + Q = F._sym_solve(A, Bm) + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols] + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1), t_asm + + +def window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: float, all_rhs: bool, levels: tuple = ()) -> tuple: + if all_rhs: + return _ORIG["window_solve"](metals, n_core, h, budget, eps_r, all_rhs, levels) + cols = np.arange(1, 1 + n_core) + t_mesh = t_asm = t_sol = 0.0 + hh, cap, refined, majority = h, budget, 0, False + hist = [] # per attempt, oldest first: ("R", R) | ("err",) | ("mesh", c, wh, o) deferred dense | ("doomed",) deferred H-matrix + while True: + t = time.perf_counter() + if not refined: + c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels) + elif levels: + c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority) + else: + c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority) + t_mesh += time.perf_counter() - t + if not np.all(np.bincount(o, minlength=len(metals)) > 0): + hist.append(("doomed",) if len(c) > P.DENSE_MAX else ("mesh", c, wh, o)) + ok = False + else: + t = time.perf_counter() + try: + R, ta = _solve(c, wh, o, metals, cols, eps_r) + t_asm += ta + hist.append(("R", R)) + ok = True + except np.linalg.LinAlgError: + hist.append(("err",)) + ok = False + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < F.LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + if ok: + R = hist[-1][1] + else: + R = None + t = time.perf_counter() + for e in reversed(hist if KEEP_LAST_SOLVED else hist[-1:]): + if e[0] == "R": + R = e[1] + break + if e[0] == "mesh": + try: + R = _solve(e[1], e[2], e[3], metals, cols, eps_r)[0] + break + except np.linalg.LinAlgError: + continue + t_sol += time.perf_counter() - t + if R is None: + raise RuntimeError(f"window solve failed: no positive-definite system after {refined} refinements (h={hh:.4g} um); " + "the window has no valid solution at this mesh") + return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, + "assemble_s": t_asm, "solve_s": t_sol} + + +def install() -> None: + """Replace fastprior._window_solve (a module global, looked up at call time, also in the forked window workers).""" + if not _ORIG: + _ORIG["window_solve"] = F._window_solve + F._window_solve = window_solve + + +def uninstall() -> None: + if _ORIG: + F._window_solve = _ORIG.pop("window_solve") + + +def installed() -> bool: + return bool(_ORIG) diff --git a/geomgnn/schema_p2d.json b/geomgnn/schema_p2d.json new file mode 100644 index 0000000000000000000000000000000000000000..13943022ba85c7b66321f18b38e8bd08303d022c --- /dev/null +++ b/geomgnn/schema_p2d.json @@ -0,0 +1,123 @@ +{ + "nodekeys": [ + "compactness", + "fourier_00", + "fourier_01", + "fourier_02", + "fourier_03", + "fourier_04", + "fourier_05", + "fourier_06", + "fourier_07", + "fourier_08", + "fourier_09", + "fourier_10", + "fourier_11", + "fourier_12", + "fourier_13", + "fourier_14", + "fourier_15", + "fourier_16", + "fourier_17", + "fourier_18", + "fourier_19", + "fourier_20", + "fourier_21", + "fourier_22", + "fourier_23", + "fourier_24", + "fourier_25", + "fourier_26", + "fourier_27", + "fourier_28", + "fourier_29", + "fourier_30", + "fourier_31", + "fourier_32", + "gran_00", + "gran_01", + "gran_02", + "gran_03", + "gran_04", + "gran_05", + "gran_06", + "gran_07", + "gran_08", + "gran_09", + "gran_10", + "gran_11", + "gran_12", + "gran_13", + "gran_14", + "gran_15", + "log_Imajor_over_A2", + "log_area", + "log_aspect", + "log_elongation", + "log_min_halfwidth", + "log_perimeter", + "log_width", + "n_holes", + "role_ground", + "solidity", + "missing_log_min_halfwidth" + ], + "edgekeys": [ + "is_ground_pair", + "lg_log_cps", + "lg_log_dmin", + "lg_mom_0.5", + "lg_mom_1", + "lg_mom_2", + "lg_sep_00", + "lg_sep_01", + "lg_sep_02", + "lg_sep_03", + "lg_sep_04", + "lg_sep_05", + "lg_sep_06", + "lg_sep_07", + "lg_sep_08", + "lg_sep_09", + "log_cps", + "log_dmin", + "lt_e150", + "lt_e400", + "lt_e50", + "lt_g150", + "lt_g400", + "lt_g50", + "lt_o150", + "lt_o400", + "lt_o50", + "mom_0.5", + "mom_1", + "mom_2", + "sep_00", + "sep_01", + "sep_02", + "sep_03", + "sep_04", + "sep_05", + "sep_06", + "sep_07", + "sep_08", + "sep_09", + "share_absdiff", + "share_mean" + ], + "globkeys": [ + "log_chip_area", + "log_pocket_area", + "log_substrate_t", + "eps_eff" + ], + "base_sha": "03edcbc72325f1522ad3998aff25b9b0ec86e0af4f479a872c76b144c45605bc", + "extra_sha": "b72762b01169a9e754265a7fc256c4f8a11930a235da340ebc948e04463007c1", + "physics": { + "prior_method": "local_gap", + "alpha": 0.49685219816506904, + "ensemble": "arithmetic mean of complete predicted Maxwell matrices", + "audit": "prior_audit.json" + } +} \ No newline at end of file diff --git a/geomgnn/train_mf.py b/geomgnn/train_mf.py new file mode 100644 index 0000000000000000000000000000000000000000..96ac63251e7dd46abb5432f7c22bd278c173868f --- /dev/null +++ b/geomgnn/train_mf.py @@ -0,0 +1,190 @@ +"""multifid arms (agents/multifid/PROTOCOL.md): P2d recipe (geomfast ragged.py fit) with a chosen MoM level set, an optional +learned convergence-curve extrapolation with a MoM-limit anchor, and a detached variance head. TRAIN fits; VAL predicted. + 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) + fit --arm R|L2|L2x|L3|L3x --seed s""" +import os +os.environ.setdefault('PYTHONDONTWRITEBYTECODE', '1') +import argparse, json, pickle, time, hashlib, platform +from pathlib import Path +import numpy as np + +GF = Path(os.environ.get('MF_GF', '/project2/shaas_31/shanto/geomfast')) +OUT = Path(os.environ.get('MF_OUT', '/project2/shaas_31/shanto/multifid')) +PPG = [0.375, 0.75, 1.5, 3.0] +ARMS = {'R': ([1.5, 3.0], False), 'L2': ([0.375, 0.75], False), 'L2x': ([0.375, 0.75], True), + 'L3': ([0.375, 0.75, 1.5], False), 'L3x': ([0.375, 0.75, 1.5], True)} + + +def digest(p): + return hashlib.sha256(Path(p).read_bytes()).hexdigest() + + +def prepare(a): + recs = {} + for f in sorted(list((OUT / 'levels_hpc').glob('*.pkl')) + list((OUT / 'levels').glob('*.pkl'))): + if f.name.endswith('.partial.pkl'): + continue + d = pickle.load(f.open('rb')) + assert not d['meta']['errors'], (f, d['meta']['errors']) + recs.update(d['records']) + out = OUT / 'prepared' + out.mkdir(exist_ok=False) + for p in sorted((GF / 'prepared_p2d').glob('n*.npz')): + z = dict(np.load(p, allow_pickle=False)) + B, F = z['pri'].shape[:2] + lv = np.zeros((B, F, 4), np.float32) + for b, k in enumerate(z['key']): + r = recs[str(k)]['levels'] + lv[b, :, 0] = r[0.375]['mutual'] + lv[b, :, 1] = r[0.75]['mutual'] + lv[b, :, 2:] = z['pri'][b] # cached P2d priors at 1.5, 3 (parity-checked on VAL) + assert np.isfinite(lv).all() and (lv > 0).all() + z['lv'] = lv + np.savez_compressed(out / p.name, **z) + (out / 'schema.json').write_text((GF / 'prepared_p2d/schema.json').read_text()) + print('PREPARED', len(recs), flush=True) + + +def build(dx, de, dg, rounds, width=64): + import torch + from torch import nn + + def mlp(i, o): + return nn.Sequential(nn.Linear(i, width), nn.SiLU(), nn.Linear(width, o)) + + class Net(nn.Module): + def __init__(self): + super().__init__() + self.node = mlp(dx + dg, width) + self.edge = mlp(de, width) + self.msg = nn.ModuleList([mlp(3 * width, width) for _ in range(rounds)]) + self.up = nn.ModuleList([mlp(2 * width, width) for _ in range(rounds)]) + self.head = mlp(3 * width, 2) + self.var = mlp(3 * width, 1) + self.closure = mlp(width, 1) + for m in [self.head, self.closure]: + nn.init.zeros_(m[-1].weight); nn.init.zeros_(m[-1].bias) + + def forward(self, X, E, G, M, PM, pairs, anchor, slope, bbase, curve): + B, N, _ = X.shape + i, j = pairs[:, 0], pairs[:, 1] + h = self.node(torch.cat([X, G[:, None].expand(-1, N, -1)], -1)) * M[:, :, None] + e = self.edge(E) + for msg, up in zip(self.msg, self.up): + v = msg(torch.cat([h[:, i] + h[:, j], abs(h[:, i] - h[:, j]), e], -1)) * PM[:, :, None] + agg = torch.zeros_like(h); agg.index_add_(1, i, v); agg.index_add_(1, j, v) + deg = torch.zeros_like(M, dtype=X.dtype); deg.index_add_(1, i, PM.float()); deg.index_add_(1, j, PM.float()) + h = (h + up(torch.cat([h, agg / deg.clamp_min(1)[:, :, None]], -1))) * M[:, :, None] + feat = torch.cat([h[:, i] + h[:, j], abs(h[:, i] - h[:, j]), e], -1) + out = self.head(feat) + r, q = out[..., 0], out[..., 1] + p = 0.25 + 2.25 * torch.sigmoid(q) + m = anchor + slope / (2.0 ** p - 1.0) if curve else anchor + logc = (m + 0.2 * torch.tanh(r)).clamp(-25, 20) + logvar = self.var(feat.detach()).squeeze(-1).clamp(-16, 4) + mutual = logc.exp() * PM + b = (bbase + self.closure(h).squeeze(-1)).clamp(-20, 20).exp() * M + C = torch.zeros((B, N, N), device=X.device) + C[:, i, j] = -mutual; C[:, j, i] = -mutual + C = C + torch.diag_embed(-C.sum(-1) + b) + return C, logc, m, p, logvar + return Net() + + +def fit(a): + import torch + from torch.nn import functional as F + torch.set_num_threads(a.threads); torch.manual_seed(a.seed); rng = np.random.default_rng(a.seed) + levels, curve = ARMS[a.arm] + li = [PPG.index(p) for p in levels] + out = OUT / 'runs' / f'{a.arm}_s{a.seed}' + out.mkdir(parents=True, exist_ok=False) + root = OUT / 'prepared' + schema = json.loads((root / 'schema.json').read_text()) + ds = [dict(np.load(p, allow_pickle=False)) for p in sorted(root.glob('n*.npz'))] + ei = schema['edgekeys'].index('is_ground_pair') + closure = {} + for flag in [0, 1]: + bs = [] + for d in ds: + tr = d['split'] == 'train' + bs.extend(np.log(d['C'].sum(-1).clip(1e-8)[(d['sig'] == bool(flag)) & tr[:, None]]).tolist()) + closure[str(flag)] = float(np.median(bs)) + for d in ds: + L = np.log(d['lv'].astype(np.float64)) + lf, lc = L[:, :, li[-1]], L[:, :, li[-2]] + d['slope'] = (lf - lc).astype(np.float32) + d['anchor'] = (lf if curve else 2 * lf - lc).astype(np.float32) + d['fine'] = (2 * L[:, :, 3] - L[:, :, 2]).astype(np.float32) # MoM-limit proxy (anchor target, TRAIN only) + d['E'] = np.concatenate([d['E']] + [(L[:, :, li[k + 1]] - L[:, :, li[k]])[:, :, None].astype(np.float32) + for k in range(len(li) - 1)], -1) + d['bbase'] = np.where(d['sig'], closure['1'], closure['0']).astype(np.float32) + scales = {} + for name in ['X', 'E', 'G']: + v = np.concatenate([d[name][d['split'] == 'train'].reshape(-1, d[name].shape[-1]) for d in ds]) + med = np.nanmedian(v, 0); iq = np.nanpercentile(v, 75, 0) - np.nanpercentile(v, 25, 0); iq = np.where(iq > 1e-6, iq, 1) + scales[name] = dict(median=med.tolist(), iqr=iq.tolist()) + for d in ds: + d[name] = np.clip((np.where(np.isfinite(d[name]), d[name], med) - med) / iq, -10, 10).astype(np.float32) + tensors = []; groups = {} + for k, d in enumerate(ds): + z = {n: torch.from_numpy(d[n]) for n in ['X', 'E', 'G', 'C', 'sig', 'pairs', 'anchor', 'slope', 'fine', 'bbase']} + z['M'] = torch.ones(d['sig'].shape, dtype=torch.bool); z['PM'] = torch.ones(d['anchor'].shape, dtype=torch.bool) + tensors.append(z) + for fam in set(d['family']): + idx = np.flatnonzero((d['family'] == fam) & (d['split'] == 'train')) + if len(idx): + groups.setdefault(fam, []).append((k, idx)) + families = sorted(groups) + model = build(ds[0]['X'].shape[-1], ds[0]['E'].shape[-1], ds[0]['G'].shape[-1], 2) + opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4) + sched = torch.optim.lr_scheduler.CosineAnnealingLR(opt, a.steps, eta_min=1e-5) + + def fwd(k, b): + z = tensors[k] + 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], + z['bbase'][b], curve) + hist = []; start = time.perf_counter() + for step in range(a.steps): + fam = families[rng.integers(len(families))]; gs = groups[fam]; w = np.array([len(v) for _, v in gs]) + k, idx = gs[rng.choice(len(gs), p=w / w.sum())]; z = tensors[k]; i, j = z['pairs'].T + b = rng.choice(idx, max(1, min(64, 8192 // len(z['pairs'])))) + pred, logc, m, p, logvar = fwd(k, b); C = z['C'][b] + y = (-C[:, i, j]).clamp_min(1e-12).log() + pl = F.huber_loss(logc, y, delta=.05) + dl = F.huber_loss(pred.diagonal(dim1=-2, dim2=-1).log(), C.diagonal(dim1=-2, dim2=-1).log(), delta=.05, reduction='none') + sig = z['sig'][b] + loss = pl + .1 * dl[sig].mean() + .02 * dl[~sig].mean() + if curve: + loss = loss + .5 * F.huber_loss(m, z['fine'][b], delta=.05) + res = (logc - y).detach() + loss = loss + .1 * (0.5 * (logvar + res ** 2 / logvar.exp())).mean() + assert torch.isfinite(loss) + opt.zero_grad(); loss.backward(); torch.nn.utils.clip_grad_norm_(model.parameters(), 5); opt.step(); sched.step() + if step % 1000 == 0: + hist.append(dict(step=step, loss=float(loss.detach()), seconds=time.perf_counter() - start)); print(hist[-1], flush=True) + train_s = time.perf_counter() - start + torch.save(dict(state=model.state_dict(), arm=a.arm, levels=levels, curve=curve, scales=scales, closure=closure, + seed=a.seed, schema=schema, data_sha={p.name: digest(p) for p in root.glob('*.npz')}, code_sha=digest(__file__)), + out / 'model.pt') + model.eval(); preds = {} + with torch.inference_mode(): + for k, d in enumerate(ds): + for b in np.flatnonzero(d['split'] == 'val'): + C, logc, m, p, logvar = fwd(k, [b]) + preds[str(d['key'][b])] = dict(C=C[0].numpy(), logc=logc[0].numpy(), m=m[0].numpy(), p=p[0].numpy(), + logvar=logvar[0].numpy()) + pickle.dump(preds, (out / 'validation_predictions.pkl').open('wb')) + ci = Path('/proc/cpuinfo') + 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() + (out / 'run.json').write_text(json.dumps(dict(arm=a.arm, seed=a.seed, steps=a.steps, host=platform.node(), cpu=cpu, + threads=a.threads, train_seconds=train_s, history=hist), indent=2)) + print('DONE', a.arm, a.seed, train_s, flush=True) + + +if __name__ == '__main__': + ap = argparse.ArgumentParser(); sub = ap.add_subparsers(dest='cmd', required=True) + sub.add_parser('prepare') + q = sub.add_parser('fit'); q.add_argument('--arm', choices=list(ARMS), required=True); q.add_argument('--seed', type=int, default=0) + q.add_argument('--steps', type=int, default=18000); q.add_argument('--threads', type=int, default=2) + a = ap.parse_args(); {'prepare': prepare, 'fit': fit}[a.cmd](a) diff --git a/geomgnn/x1_core.py b/geomgnn/x1_core.py new file mode 100644 index 0000000000000000000000000000000000000000..6a8c6c810ea9a6f8ef77e2c794d6b326ca3aae75 --- /dev/null +++ b/geomgnn/x1_core.py @@ -0,0 +1,114 @@ +"""X1 shared model code (agents/v4test/PROTOCOL.md): row -> arrays, multifid forward, surrogate priors. No solver, no labels.""" +import numpy as np + +PPG = (0.375, 0.75, 1.5, 3.0) + + +def schema_of(ck): + s = ck.get('schema', ck) + return s['nodekeys'], s['edgekeys'], s.get('globkeys', ['log_chip_area', 'log_pocket_area', 'log_substrate_t', 'eps_eff']) + + +def raw_arrays(row, nk, ek, gk): + """Unnormalised X (n, nk), E (P, ek), G (gk) exactly as geomfast infer.Predictor.from_features builds them.""" + n = len(row['names']) + pairs = np.array([(i, j) for i in range(n) for j in range(i + 1, n)]) + 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) + E = np.array([[row['edges'][tuple(p)][k] for k in ek] for p in pairs], np.float32).reshape(len(pairs), len(ek)) + G = np.array([row['global'][k] for k in gk], np.float32) + return X, E, G, pairs + + +def norm(z, scale): + med, iqr = np.array(scale['median']), np.array(scale['iqr']) + return np.clip((np.where(np.isfinite(z), z, med) - med) / iqr, -10, 10).astype(np.float32) + + +class Multifid: + """multifid residual arm (scripts/multifid/train_mf.py Net), seeds averaged as full matrices (analyze.py).""" + + def __init__(self, paths, threads=2): + import torch + from .train_mf import build + torch.set_num_threads(threads) + self.models = [] + for p in paths: + c = torch.load(p, map_location='cpu', weights_only=False) + assert not c['curve'] + m = build(len(c['scales']['X']['median']), len(c['scales']['E']['median']), len(c['scales']['G']['median']), 2) + m.load_state_dict(c['state']) + m.eval() + self.models.append((c, m)) + self.levels = self.models[0][0]['levels'] + assert all(c['levels'] == self.levels for c, _ in self.models) + + def predict(self, row, levels): + """levels: {ppg: positive mutual matrix (n, n)} in row order.""" + nk, ek, gk = schema_of(self.models[0][0]) + X, E, G, pairs = raw_arrays(row, nk, ek, gk) + i, j = pairs.T + sig = np.array([nm != 'G' for nm in row['names']]) + return self.predict_arrays(X, E, G, pairs, sig, {p: np.asarray(levels[p], float)[i, j] for p in self.levels}) + + def predict_arrays(self, X0, E0, G0, pairs, sig, lev): + """raw (unnormalised) X (n, f), E (P, f), G (g), pairs (P, 2), sig (n,), lev {ppg: positive mutual (P,)}.""" + import torch + out = [] + for c, m in self.models: + assert schema_of(c) == schema_of(self.models[0][0]) + L = [np.log(np.asarray(lev[p], float)) for p in self.levels] + assert all(np.isfinite(v).all() for v in L) + X, E, G = X0, E0, G0 + lf, lc = L[-1], L[-2] + anchor, slope = 2 * lf - lc, lf - lc + E = np.concatenate([E] + [(L[k + 1] - L[k])[:, None].astype(np.float32) for k in range(len(L) - 1)], -1) + X, E, G = norm(X, c['scales']['X'])[None], norm(E, c['scales']['E'])[None], norm(G[None], c['scales']['G']) + bbase = np.where(sig, c['closure']['1'], c['closure']['0']).astype(np.float32)[None] + T = lambda a: torch.from_numpy(np.ascontiguousarray(a, dtype=np.float32)) # noqa: E731 + n = len(sig) + with torch.inference_mode(): + C = m(T(X), T(E), T(G), torch.ones((1, n), dtype=torch.bool), torch.ones((1, len(pairs)), dtype=torch.bool), + torch.from_numpy(pairs), T(anchor[None]), T(slope[None]), T(bbase), False)[0] + out.append(C[0].numpy().astype(np.float64)) + r = np.mean(out, 0) + assert np.isfinite(r).all() + return r + + +class Surrogate: + """learnedprior surrogate (lp_surrogate.predict_bucket on lp_label.pack(row)) -> priors at ppg 1.5, 3.""" + + def __init__(self, path): + from . import lp_surrogate as S + self.S = S + self.model, self.prep, _ = S.load_model(path, 'cpu') + sch = S.SCHEMA + self.nk, self.ek, self.gk = sch['nodekeys'], sch['edgekeys'], sch['globkeys'] + + def priors(self, row): + X, E, G, pairs = raw_arrays(row, self.nk, self.ek, self.gk) + n = len(row['names']) + lp = self.S.predict_bucket(self.model, self.prep, dict(n=n, X=X[None], E=E[None], G=G[None]), 'cpu')[0] + iu = np.triu_indices(n, 1) + pri = {} + for k, p in enumerate((1.5, 3.0)): + M = np.zeros((n, n)) + M[iu] = np.exp(lp[:, k]) + pri[p] = M + M.T + return pri + + +def c0(levels, lo=1.5, hi=3.0): + """Richardson extrapolation in log of positive mutual / self matrices: exp(2 log c(hi) - log c(lo)); Maxwell form.""" + a, b = np.abs(np.asarray(levels[lo], float)), np.abs(np.asarray(levels[hi], float)) + m = np.exp(2 * np.log(np.maximum(b, 1e-300)) - np.log(np.maximum(a, 1e-300))) + n = len(m) + off = ~np.eye(n, dtype=bool) + C = np.where(off, -m, m) + return C + + +def to_maxwell(P): + """stored prior P = -C (positive mutuals) -> Maxwell C.""" + return -np.asarray(P, float) diff --git a/models/accurate/acc_v2m_s0.pt b/models/accurate/acc_v2m_s0.pt new file mode 100644 index 0000000000000000000000000000000000000000..e1cb7eb5acd9f41831eb50631d3c2fcf096120b9 --- /dev/null +++ b/models/accurate/acc_v2m_s0.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:56109a61bb264151ca7a650cd79a516d60dcf6c92f257ded38ff9ad97a1421a2 +size 8410286 diff --git a/models/accurate/acc_v2m_s1.pt b/models/accurate/acc_v2m_s1.pt new file mode 100644 index 0000000000000000000000000000000000000000..3aa360174817b879e215b0187a80c45f569dc7df --- /dev/null +++ b/models/accurate/acc_v2m_s1.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae9c4be59caa84eb6a220a91e18fce696e98e3a9c3aab09e16ac727e7fc2d898 +size 8410286 diff --git a/models/accurate/acc_v2m_s2.pt b/models/accurate/acc_v2m_s2.pt new file mode 100644 index 0000000000000000000000000000000000000000..a5a7878ab039646ffc049073465b2b854a71f85b --- /dev/null +++ b/models/accurate/acc_v2m_s2.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0841acc6a3db32e2d5def72443445361c4ae41ac782b38b7856a78f0547b1e19 +size 8410286 diff --git a/models/accurate/info.json b/models/accurate/info.json new file mode 100644 index 0000000000000000000000000000000000000000..b47424199d09b2541a3e3bdb971b7f17e2e55bfa --- /dev/null +++ b/models/accurate/info.json @@ -0,0 +1,22 @@ +{ + "name": "accurate", + "kind": "gnn", + "checkpoints": [ + "acc_v2m_s0.pt", + "acc_v2m_s1.pt", + "acc_v2m_s2.pt" + ], + "checkpoints_sha256": [ + "56109a61bb264151ca7a650cd79a516d60dcf6c92f257ded38ff9ad97a1421a2", + "ae9c4be59caa84eb6a220a91e18fce696e98e3a9c3aab09e16ac727e7fc2d898", + "0841acc6a3db32e2d5def72443445361c4ae41ac782b38b7856a78f0547b1e19" + ], + "checkpoints_source": [ + "data_local/flagship_v4/hpc/runs/acc_v2m_s0/model.pt", + "data_local/flagship_v4/hpc/runs/acc_v2m_s1/model.pt", + "data_local/flagship_v4/hpc/runs/acc_v2m_s2/model.pt" + ], + "surrogate": [], + "surrogate_sha256": [], + "surrogate_source": [] +} \ No newline at end of file diff --git a/models/all-data-base/config.json b/models/all-data-base/config.json deleted file mode 100644 index a4876b3e496e276dffc64d11707a0943db7cbf22..0000000000000000000000000000000000000000 --- a/models/all-data-base/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "all-data-base", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/all-data-base/seed0.pt b/models/all-data-base/seed0.pt deleted file mode 100644 index 86dffb1066ed3bbb4f81dd072b0df39ba77af2bd..0000000000000000000000000000000000000000 --- a/models/all-data-base/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:60f29f22b4ee0fe2b10a29131f0d1c56b7af4b9537f1c17208a1f7ade567044b -size 848449 diff --git a/models/all-data-base/seed1.pt b/models/all-data-base/seed1.pt deleted file mode 100644 index 31c8dc6ac189d42c3dc9d3c70edee8bef4e5757d..0000000000000000000000000000000000000000 --- a/models/all-data-base/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:b6f9f861c4a65f711b081e3f90e396bf5540178335d8588375c71ec590e7fc85 -size 848449 diff --git a/models/all-data-base/seed2.pt b/models/all-data-base/seed2.pt deleted file mode 100644 index b07b68f6748a4122f4e760f454d1f1516aad2204..0000000000000000000000000000000000000000 --- a/models/all-data-base/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:1b4a2a15a1d9f6e2b19281e213f56a22278ef93e753ec61f656bed99c66391ae -size 848449 diff --git a/models/all-data-flagship/config.json b/models/all-data-flagship/config.json deleted file mode 100644 index 72105b3a78f5fd65ac782401a96344b8e44fa528..0000000000000000000000000000000000000000 --- a/models/all-data-flagship/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "all-data-flagship", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/all-data-flagship/seed0.pt b/models/all-data-flagship/seed0.pt deleted file mode 100644 index 2f7e723c22f99fdbb5cf5153d90c26f3b49de4ec..0000000000000000000000000000000000000000 --- a/models/all-data-flagship/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:e3305b5d522abccc52c31b46f13cf424be197aa7fcfbd1d9a70bb9ea3d7c8ce9 -size 848449 diff --git a/models/all-data-flagship/seed1.pt b/models/all-data-flagship/seed1.pt deleted file mode 100644 index cab490783d79d7d74a023c558c29a6d96c5eda5a..0000000000000000000000000000000000000000 --- a/models/all-data-flagship/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cecfb8d90711dbd0d37446f1dcff7154443fe487d45a3bd9af99ba686e6c4f31 -size 848449 diff --git a/models/all-data-flagship/seed2.pt b/models/all-data-flagship/seed2.pt deleted file mode 100644 index 7f5789383e378899a1453e32e37df042a395eec9..0000000000000000000000000000000000000000 --- a/models/all-data-flagship/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:4bbcfc763a63eeb8dd3f781051a359cd78f2cf8ef7b54818067f9f9d75788269 -size 848449 diff --git a/models/all-data-lite1500/config.json b/models/all-data-lite1500/config.json deleted file mode 100644 index e2519a8d3c949843286210d7d274d034d600a309..0000000000000000000000000000000000000000 --- a/models/all-data-lite1500/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "all-data-lite1500", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/all-data-lite1500/seed0.pt b/models/all-data-lite1500/seed0.pt deleted file mode 100644 index f5c7dd810fb88d7c5500422e80bd4ba8d91d3982..0000000000000000000000000000000000000000 --- a/models/all-data-lite1500/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:62b0e3e4f63a57a5ed467bf97ffa17cd039c69b6e750e606d5e8fd1fe70cc946 -size 848449 diff --git a/models/all-data-lite1500/seed1.pt b/models/all-data-lite1500/seed1.pt deleted file mode 100644 index c7133c80e4aac4e05c2cfcfcf0a2389978411bcf..0000000000000000000000000000000000000000 --- a/models/all-data-lite1500/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3ac5907d1999fae825bda3a46654775497474834d943d2dc7c9e6961001a6050 -size 848449 diff --git a/models/all-data-lite1500/seed2.pt b/models/all-data-lite1500/seed2.pt deleted file mode 100644 index b05d7589b7891dcc3ae65a3e499f5825f261be30..0000000000000000000000000000000000000000 --- a/models/all-data-lite1500/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:d2a5f0fc3b5accbae0c543f1b8d8bda9a4859fc636d4ac1c9e915febce10fbf4 -size 848449 diff --git a/models/all-data-v3-rich/config.json b/models/all-data-v3-rich/config.json deleted file mode 100644 index 7f7c7430682a4e744850a725469ad06c2021c449..0000000000000000000000000000000000000000 --- a/models/all-data-v3-rich/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "all-data-v3-rich", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/all-data-v3-rich/seed0.pt b/models/all-data-v3-rich/seed0.pt deleted file mode 100644 index 2f7e723c22f99fdbb5cf5153d90c26f3b49de4ec..0000000000000000000000000000000000000000 --- a/models/all-data-v3-rich/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:e3305b5d522abccc52c31b46f13cf424be197aa7fcfbd1d9a70bb9ea3d7c8ce9 -size 848449 diff --git a/models/all-data-v3-rich/seed1.pt b/models/all-data-v3-rich/seed1.pt deleted file mode 100644 index cab490783d79d7d74a023c558c29a6d96c5eda5a..0000000000000000000000000000000000000000 --- a/models/all-data-v3-rich/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cecfb8d90711dbd0d37446f1dcff7154443fe487d45a3bd9af99ba686e6c4f31 -size 848449 diff --git a/models/all-data-v3-rich/seed2.pt b/models/all-data-v3-rich/seed2.pt deleted file mode 100644 index 7f5789383e378899a1453e32e37df042a395eec9..0000000000000000000000000000000000000000 --- a/models/all-data-v3-rich/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:4bbcfc763a63eeb8dd3f781051a359cd78f2cf8ef7b54818067f9f9d75788269 -size 848449 diff --git a/models/all-data-v3s/config.json b/models/all-data-v3s/config.json deleted file mode 100644 index afe290b875cd529f03e4588fb2624479c4fec7fd..0000000000000000000000000000000000000000 --- a/models/all-data-v3s/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "all-data-v3s", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/all-data-v3s/seed0.pt b/models/all-data-v3s/seed0.pt deleted file mode 100644 index de8c97732fa9fdbf973ace49314bb76498e7ac8a..0000000000000000000000000000000000000000 --- a/models/all-data-v3s/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:a445b7f10bb9fa82f4d6c7a1d95c02ddc0d5979180ab50da39e9e7bc27f0017d -size 848449 diff --git a/models/all-data-v3s/seed1.pt b/models/all-data-v3s/seed1.pt deleted file mode 100644 index 66d6db8563afc29dc902fd12e0c022a65687c08b..0000000000000000000000000000000000000000 --- a/models/all-data-v3s/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:634dbf3699ec8f4355ada5c51ccb08c881d0ea74801e70c328a9fcae4d0b0102 -size 848449 diff --git a/models/all-data-v3s/seed2.pt b/models/all-data-v3s/seed2.pt deleted file mode 100644 index 976735747432b1fef0a30033dc2d3409a35f4f7a..0000000000000000000000000000000000000000 --- a/models/all-data-v3s/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:d186d67cb5a4b4ccfbd52e4e288d56fc3b98c45850e994e66d18ae49ef09a3ba -size 848449 diff --git a/models/all-data/config.json b/models/all-data/config.json deleted file mode 100644 index 8c0442544cd5b84934501486951bde1a416c398c..0000000000000000000000000000000000000000 --- a/models/all-data/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "all-data", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/all-data/seed0.pt b/models/all-data/seed0.pt deleted file mode 100644 index b878bca3d5c13d2e58a8a6a2f7a378450fc4eaac..0000000000000000000000000000000000000000 --- a/models/all-data/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:803ac1db3b05f41fc8d6368dc6f666eb88406c0c52d7e087a49668d57c6a9e3c -size 848449 diff --git a/models/all-data/seed1.pt b/models/all-data/seed1.pt deleted file mode 100644 index 34a64425ac2295274ba062d94a96fd7166f4bd49..0000000000000000000000000000000000000000 --- a/models/all-data/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5a579faeee0a2aa2fdcf89c216e313eb4683ee8111f6b5962a9f6d11faca21df -size 848449 diff --git a/models/all-data/seed2.pt b/models/all-data/seed2.pt deleted file mode 100644 index e6dca6e19de7cae55886e712a57eec7c0cfbc059..0000000000000000000000000000000000000000 --- a/models/all-data/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:2b97ae3d84ef2c8a36a93c16051d81472caf55ddf27a230ceda842e29750807d -size 848449 diff --git a/models/balanced/bal_v2m_s0.pt b/models/balanced/bal_v2m_s0.pt new file mode 100644 index 0000000000000000000000000000000000000000..41465fa42650a225869006359d85f79841b3d672 --- /dev/null +++ b/models/balanced/bal_v2m_s0.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5016672366dead959a425890bfb23df0ea4fdab050aaf03e1e428ac2597bfe0c +size 8410286 diff --git a/models/balanced/bal_v2m_s1.pt b/models/balanced/bal_v2m_s1.pt new file mode 100644 index 0000000000000000000000000000000000000000..1f1f86ebae63160860fd33dcd55ad0add5ca2c33 --- /dev/null +++ b/models/balanced/bal_v2m_s1.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d1f0ffbac76bc2742ef26adfcbe44be99dc795ba92fd2b0439c541dcfbbb14e +size 8410286 diff --git a/models/balanced/bal_v2m_s2.pt b/models/balanced/bal_v2m_s2.pt new file mode 100644 index 0000000000000000000000000000000000000000..f6114069945813551e97aead25cd05c5243defe8 --- /dev/null +++ b/models/balanced/bal_v2m_s2.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dcd14c610e251b23d77b34a114e5a2db33fade289306270057151f2750890d35 +size 8410286 diff --git a/models/balanced/info.json b/models/balanced/info.json new file mode 100644 index 0000000000000000000000000000000000000000..310efccb3206eb5f92270c380cff45683f28a143 --- /dev/null +++ b/models/balanced/info.json @@ -0,0 +1,22 @@ +{ + "name": "balanced", + "kind": "gnn", + "checkpoints": [ + "bal_v2m_s0.pt", + "bal_v2m_s1.pt", + "bal_v2m_s2.pt" + ], + "checkpoints_sha256": [ + "5016672366dead959a425890bfb23df0ea4fdab050aaf03e1e428ac2597bfe0c", + "5d1f0ffbac76bc2742ef26adfcbe44be99dc795ba92fd2b0439c541dcfbbb14e", + "dcd14c610e251b23d77b34a114e5a2db33fade289306270057151f2750890d35" + ], + "checkpoints_source": [ + "data_local/flagship_v4/hpc/runs/bal_v2m_s0/model.pt", + "data_local/flagship_v4/hpc/runs/bal_v2m_s1/model.pt", + "data_local/flagship_v4/hpc/runs/bal_v2m_s2/model.pt" + ], + "surrogate": [], + "surrogate_sha256": [], + "surrogate_source": [] +} \ No newline at end of file diff --git a/models/blend-pair-local/config.json b/models/blend-pair-local/config.json deleted file mode 100644 index e766bb4fd3ec730093535aaf0cfbd00d5d8f0b05..0000000000000000000000000000000000000000 --- a/models/blend-pair-local/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "blend-pair-local", "files": []} \ No newline at end of file diff --git a/models/charge-corrected-prior/charge_gbm.joblib b/models/charge-corrected-prior/charge_gbm.joblib deleted file mode 100644 index 36848271d3d9eae3b27ddc75c215575b4914a3e3..0000000000000000000000000000000000000000 --- a/models/charge-corrected-prior/charge_gbm.joblib +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3dcca168d8d732c19b200eba17999774e71fc760a874f451147e9b07df8d8b47 -size 677172 diff --git a/models/charge-corrected-prior/config.json b/models/charge-corrected-prior/config.json deleted file mode 100644 index fbe92a301ab3f8bf934ba1ca68553758d7bde977..0000000000000000000000000000000000000000 --- a/models/charge-corrected-prior/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "charge-corrected-prior", "files": ["charge_gbm.joblib"]} \ No newline at end of file diff --git a/models/fast/acc_v1_s0.pt b/models/fast/acc_v1_s0.pt new file mode 100644 index 0000000000000000000000000000000000000000..1d9a7f8c89ab4d7ae7ae1d0a003d17fbd3b2b289 --- /dev/null +++ b/models/fast/acc_v1_s0.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9dff20ade26619e7713c2d145dada65e4554a623780290673cfe9452cf9b64ba +size 8410094 diff --git a/models/fast/acc_v1_s1.pt b/models/fast/acc_v1_s1.pt new file mode 100644 index 0000000000000000000000000000000000000000..aba50ebbf99f9ca5cfefbf8850c901cf4a158399 --- /dev/null +++ b/models/fast/acc_v1_s1.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1ad2ca065dd650ccabcad73a3c6ad2ac1042f4f68bb828893bbdecc47411ffe +size 8410094 diff --git a/models/fast/acc_v1_s2.pt b/models/fast/acc_v1_s2.pt new file mode 100644 index 0000000000000000000000000000000000000000..0fc18bf6ec847f87e2f59b19b226f05226067ae4 --- /dev/null +++ b/models/fast/acc_v1_s2.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8cbada211724df5cc05a7b4fdd232607de047b248a43993d3844bd68ff8060c4 +size 8410094 diff --git a/models/fast/info.json b/models/fast/info.json new file mode 100644 index 0000000000000000000000000000000000000000..be3ee3a71b10345d212b15225069bb52e324c2fb --- /dev/null +++ b/models/fast/info.json @@ -0,0 +1,28 @@ +{ + "name": "fast", + "kind": "gnn", + "checkpoints": [ + "acc_v1_s0.pt", + "acc_v1_s1.pt", + "acc_v1_s2.pt" + ], + "checkpoints_sha256": [ + "9dff20ade26619e7713c2d145dada65e4554a623780290673cfe9452cf9b64ba", + "f1ad2ca065dd650ccabcad73a3c6ad2ac1042f4f68bb828893bbdecc47411ffe", + "8cbada211724df5cc05a7b4fdd232607de047b248a43993d3844bd68ff8060c4" + ], + "checkpoints_source": [ + "data_local/flagship_v4/hpc/runs/acc_v1_s0/model.pt", + "data_local/flagship_v4/hpc/runs/acc_v1_s1/model.pt", + "data_local/flagship_v4/hpc/runs/acc_v1_s2/model.pt" + ], + "surrogate": [ + "sall_all_s0.pt" + ], + "surrogate_sha256": [ + "132d6f389eda798d31bd5654a96e24cce18fa750134a9fb2a9725eafed339dac" + ], + "surrogate_source": [ + "data_local/flagship_v4/hpc/sur/sall_all_s0/model.pt" + ] +} \ No newline at end of file diff --git a/models/fast/sall_all_s0.pt b/models/fast/sall_all_s0.pt new file mode 100644 index 0000000000000000000000000000000000000000..fa4fa120dd9577ccebca5b29e551e4ca7a5e4e88 --- /dev/null +++ b/models/fast/sall_all_s0.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:132d6f389eda798d31bd5654a96e24cce18fa750134a9fb2a9725eafed339dac +size 4172218 diff --git a/models/message-passing/config.json b/models/message-passing/config.json deleted file mode 100644 index eaf6c27e86b2e371e4b6142ca76a62f6f94906a5..0000000000000000000000000000000000000000 --- a/models/message-passing/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "message-passing", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/message-passing/seed0.pt b/models/message-passing/seed0.pt deleted file mode 100644 index fd70f38172239195a844a5f3b4b60f4b2cc2026a..0000000000000000000000000000000000000000 --- a/models/message-passing/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:a3de2e4ea45dd70e7d9fefd563f42d5c38ac8e9dd990568383e38958d3e7d087 -size 3028237 diff --git a/models/message-passing/seed1.pt b/models/message-passing/seed1.pt deleted file mode 100644 index 5dd210a81bf752e376baf0ef9c3d990f530c2846..0000000000000000000000000000000000000000 --- a/models/message-passing/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:2a924d03e0d4ceecd2e29030c8dfb5d718240e2de97514d15eaece7ad8086b08 -size 3028237 diff --git a/models/message-passing/seed2.pt b/models/message-passing/seed2.pt deleted file mode 100644 index 9b5837de2033efff792f029270eafcbc2dbb7ef0..0000000000000000000000000000000000000000 --- a/models/message-passing/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:37551cbdb21ca9a896eb599f0285718f2fa50102665002c1b8340ca69c9e09b5 -size 3028237 diff --git a/models/pair-local/config.json b/models/pair-local/config.json deleted file mode 100644 index f93bdea931b478a81feb05e58723d55d66164940..0000000000000000000000000000000000000000 --- a/models/pair-local/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name": "pair-local", "files": ["seed0.pt", "seed1.pt", "seed2.pt"]} \ No newline at end of file diff --git a/models/pair-local/seed0.pt b/models/pair-local/seed0.pt deleted file mode 100644 index b1aae6ed2234909d089d49f02b9d861a24d46d2f..0000000000000000000000000000000000000000 --- a/models/pair-local/seed0.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:8a8df1507d65f2b3decdbf70e66a59e5ddfd18861d973abe093b07507848d0eb -size 848449 diff --git a/models/pair-local/seed1.pt b/models/pair-local/seed1.pt deleted file mode 100644 index b840ee54bc81f836db3c6ca8468f12dc743fd435..0000000000000000000000000000000000000000 --- a/models/pair-local/seed1.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:58be037a06e4771d40ed5312a31805290e68ad433009e183864715eb93b55a70 -size 848449 diff --git a/models/pair-local/seed2.pt b/models/pair-local/seed2.pt deleted file mode 100644 index 3d74dffd2890639457811def2e2dc451dfc6cbee..0000000000000000000000000000000000000000 --- a/models/pair-local/seed2.pt +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:58a2973c7bbbec8d3d385c63fd5f8f47c5088b4b0b7df0c110030512a57dbc3a -size 848449 diff --git a/registry.yaml b/registry.yaml index 41b83c775d35d5cc9d6f94df9a18ab9cebdd15de..ab1d60ffba7a10f2e92a80e40ee8cca4f029524d 100644 --- a/registry.yaml +++ b/registry.yaml @@ -340,6 +340,36 @@ models: pocket3_biased20: {median_pct: 5.75, mae_ff: 0.641, rmse_ff: 1.076} cost_s_per_design: {median: 0.23, p90: 0.68, note: "end to end from the GDS, 2 threads, Mac M5 Pro, 212 held-out designs; 780-conductor grid ~15 s on 18 Mac cores"} + all-data-lite1500td16: + title: "flagship-fast candidate: all-data pair-local network on the lite-1500 prior with approximate pair tokens td16 (R=0)" + kind: gnn + prior: lite1500 + tokens: td16 + rounds: 0 + status: candidate + checkpoints: [data_local/mega/alldata/lite1500td16_r0_s0.pt, data_local/mega/alldata/lite1500td16_r0_s1.pt, data_local/mega/alldata/lite1500td16_r0_s2.pt] + summary: "Token-variant candidate (PRIOR_SCHEME.md 14.3): far boundary samples block-decimated (td16); network retrained on these tokens. Not deployable until the user decides." + + all-data-lite1500td64: + title: "flagship-fast candidate: all-data pair-local network on the lite-1500 prior with approximate pair tokens td64 (R=0)" + kind: gnn + prior: lite1500 + tokens: td64 + rounds: 0 + status: candidate + checkpoints: [data_local/mega/alldata/lite1500td64_r0_s0.pt, data_local/mega/alldata/lite1500td64_r0_s1.pt, data_local/mega/alldata/lite1500td64_r0_s2.pt] + summary: "Token-variant candidate (PRIOR_SCHEME.md 14.3): far boundary samples block-decimated (td64); network retrained on these tokens. Not deployable until the user decides." + + all-data-lite1500tc600: + title: "flagship-fast candidate: all-data pair-local network on the lite-1500 prior with approximate pair tokens tc600 (R=0)" + kind: gnn + prior: lite1500 + tokens: tc600 + rounds: 0 + status: candidate + checkpoints: [data_local/mega/alldata/lite1500tc600_r0_s0.pt, data_local/mega/alldata/lite1500tc600_r0_s1.pt, data_local/mega/alldata/lite1500tc600_r0_s2.pt] + summary: "Token-variant candidate (PRIOR_SCHEME.md 14.3): far boundary samples block-decimated (tc600); network retrained on these tokens. Not deployable until the user decides." + all-data-formula: title: "All-data pair-local network retrained on the flagship-fast formula prior (R=0)" kind: gnn @@ -569,3 +599,854 @@ models: appears_in: [tab:candidates, tab:lofo] never_seen: {median_pct: 4.02, mae_ff: 1.5, rmse_ff: 2.4} ood60: {median_pct: "3.1-6.0", mae_ff: "1.2-1.6", rmse_ff: "1.5-2.2", note: range of per-class medians} + +# --------------------------------------------------------------------------------------------------------------------------- +# paper_v3 final recipes (tlsq_infer.final; parity with scripts/tm_final.py: deploy/tests/test_final_parity.py) and the new +# Space (PLAN_HF_SPACE_V2.md; user decisions 2026-10-14: one PRIVATE Space with a model toggle, built as a new copy, the +# 2026-10-10 Spaces above never overwritten; geometry = training geometry, no ground crop by default). Separate from +# `models` (the 2026-10-10 tiers) so the existing apps and tests are unchanged. Accuracy = the paper's test-once numbers of +# the REPORTED model (data_local/train_matrix/eval/final/summary.parquet; median % / MAE fF / RMSE fF over mutual entries). +v2_hub: + space: shanto268/PlanarCapModel # NEW private repo (user, 2026-10-14); never the 2026-10-10 Spaces above + title: Planar Capacitance (paper models) + private: true # stays private until the paper is out (user, 2026-10-14) + +final_defaults: + model: E3 + trust_rule: true # APPLIED by default to E3 and E3-D (user decision 2026-10-14); trust_rule=False switches it off + trust_rule_note: "Trust rule (applied by default to E3 and E3-D). The E3 model is three independently trained networks. When they agree, their answer is used for pairs of conductors closer than 100 um, and the physics-only C0 value for all other pairs. When they disagree (median spread of log C over the close pairs above tau = 1.254e-3, a threshold fixed in the paper before its blind test), the whole matrix falls back to the physics-only C0 recipe. Every E3 result shows the spread, the threshold and whether the fallback happened. On the paper's blind 9-qubit grid the networks disagreed (spread 2.28e-3) and the rule chose C0: 0.453 % (MAE 0.247 / RMSE 0.473 fF) on the 135 close pairs, against 1.153 % (0.235 / 0.319 fF) for E3 alone. The test results listed for E3 are those of E3 without the rule. Switch the rule off with the 'apply the trust rule' box (UI), trust_rule=False (/predict_gds) or predict_final(..., trust_rule=False) in the package." + +final_models: + E3: + title: "Model (E3): extrapolated physics + per-kind offsets + bounded learned correction" + recipe: E3 + pair: [1.5, 3.0] + role: reported model (paper_v3) + ledger_id: em_1_p1.5 + source: data_local/e13/em/runs/1 # info_p1.5.json + s{0,1,2}_p1.5.pt (training checkpoints); bundles carry an export + summary: "log-Richardson R(1.5, 3) of the slab lite micro-MoM, per-entry-kind offsets and a bounded (+-3 %) SceneGNN correction; mean log C of 3 seeds. Trained on exp6-12 train + exp13-15 + random 200." + accuracy: + in_family_test_exp6_11: {set: "in-family test exp6-11 (3,819 designs; test-once)", median_pct: 0.110, mae_ff: 0.017, rmse_ff: 0.093} + in_family_test_exp12: {set: "in-family test exp12 (190)", median_pct: 0.089, mae_ff: 0.009, rmse_ff: 0.016} + ood60: {set: "OOD-60 freeform (60)", median_pct: 0.393, mae_ff: 0.195, rmse_ff: 0.437} + qm116: {set: "qm116 unseen qubits / couplers (116)", median_pct: 0.761, mae_ff: 0.227, rmse_ff: 0.591} + p3rand36: {set: "p3rand36 (36)", median_pct: 0.690, mae_ff: 0.099, rmse_ff: 0.180} + largen: {set: "large-N 8-12 conductors (40)", median_pct: 0.972, mae_ff: 0.058, rmse_ff: 0.158} + atlas: {set: "quantum-metal atlas (348)", median_pct: 1.240, mae_ff: 7.102, rmse_ff: 11.734} + grid_qubit_5_near: {set: "grid_qubit_5, pairs within 100 um (67 entries)", median_pct: 1.135, mae_ff: 0.247, rmse_ff: 0.319} + E1: + title: "Fast (E1): extrapolated physics + per-kind offsets (no network)" + recipe: E1 + pair: [0.75, 1.5] + role: reported model (paper_v3), fast mode + ledger_id: em_1_E1_p0.75 + source: data_local/e13/em/1/info_E1_p0.75.json + summary: "log-Richardson R(0.75, 1.5) of the slab lite micro-MoM plus the model's per-entry-kind offsets c_kind; no network, about 3x cheaper solves." + accuracy: + in_family_test_exp6_11: {set: "in-family test exp6-11 (3,819 designs; test-once)", median_pct: 0.656, mae_ff: 0.105, rmse_ff: 0.366} + in_family_test_exp12: {set: "in-family test exp12 (190)", median_pct: 0.964, mae_ff: 0.058, rmse_ff: 0.110} + ood60: {set: "OOD-60 freeform (60)", median_pct: 1.175, mae_ff: 0.503, rmse_ff: 1.159} + qm116: {set: "qm116 unseen qubits / couplers (116)", median_pct: 1.788, mae_ff: 0.568, rmse_ff: 1.135} + p3rand36: {set: "p3rand36 (36)", median_pct: 1.681, mae_ff: 0.380, rmse_ff: 0.652} + largen: {set: "large-N 8-12 conductors (40)", median_pct: 1.894, mae_ff: 0.154, rmse_ff: 0.326} + atlas: {set: "quantum-metal atlas (348)", median_pct: 1.386, mae_ff: 9.423, rmse_ff: 16.172} + grid_qubit_5_near: {set: "grid_qubit_5, pairs within 100 um (67 entries)", median_pct: 0.544, mae_ff: 0.360, rmse_ff: 0.822} + C0: + title: "Physics only (C0): extrapolated micro-MoM, no fitted parameter" + recipe: C0 + pair: [1.5, 3.0] + role: reported model (paper_v3), physics only + ledger_id: C0_p1.5 + summary: "log-Richardson R(1.5, 3) = 2 log MoM(3) - log MoM(1.5) of the slab lite micro-MoM; zero fitted parameters." + accuracy: + in_family_test_exp6_11: {set: "in-family test exp6-11 (3,819 designs; test-once)", median_pct: 1.259, mae_ff: 0.172, rmse_ff: 0.312} + in_family_test_exp12: {set: "in-family test exp12 (190)", median_pct: 1.374, mae_ff: 0.114, rmse_ff: 0.226} + ood60: {set: "OOD-60 freeform (60)", median_pct: 1.065, mae_ff: 0.303, rmse_ff: 0.572} + qm116: {set: "qm116 unseen qubits / couplers (116)", median_pct: 1.260, mae_ff: 0.255, rmse_ff: 0.591} + p3rand36: {set: "p3rand36 (36)", median_pct: 0.836, mae_ff: 0.111, rmse_ff: 0.181} + largen: {set: "large-N 8-12 conductors (40)", median_pct: 0.955, mae_ff: 0.081, rmse_ff: 0.215} + atlas: {set: "quantum-metal atlas (348)", median_pct: 0.609, mae_ff: 3.262, rmse_ff: 5.882} + newfam926: {set: "never-seen 926", median_pct: 0.597, mae_ff: 0.231, rmse_ff: 0.390} + grid_qubit_5_near: {set: "grid_qubit_5, pairs within 100 um (67 entries)", median_pct: 0.735, mae_ff: 0.238, rmse_ff: 0.460} + E3-D: + title: "Deployment model (E3 recipe, D weights): trained on every labelled design" + recipe: E3 + pair: [1.5, 3.0] + role: deployment model (never a paper claim, never test-scored) + ledger_id: null + source: data_local/e13/em/runs/D + summary: "E3 recipe retrained on 42,967 designs (exp6-12 train + val + test, exp13-15 (all 1,754), random 1,000, OOD-60). Its scores on those sets would be in-sample; held-out scoring (atlas, qm116, p3rand36, large-N, grids) not yet run." + warning: "Deployment model D: trained on the in-family test split, exp13-15 and OOD-60, so no test number of the paper applies to it; its held-out scoring is pending. The paper's independently tested model is 'E3'." + scoring_note: &d_scoring "Scoring note for D (2026-10-14, documentation only, no set scored): IN-SAMPLE for D (trained on them, any score would not be a test): in-family test exp6-11 and exp12 (D trained on train + val + test), never-seen 926 (exp13-15, all 1,754 trained), OOD-60 (unusual), random 1,000. HELD OUT from D (no rows of these in its training data): quantum-metal atlas (348), qm116 (116), p3rand36 (36), large-N (40), grid_qubit_1 / 5 / 9, multi-stack, margin. D has not been scored on any of them; numbers shown are the paper's reported model, never D's." + accuracy: {} + E1-D: + title: "Deployment fast (E1 recipe, D constants): per-kind offsets fitted on every labelled design" + recipe: E1 + pair: [0.75, 1.5] + role: deployment model, fast mode (never a paper claim, never test-scored) + ledger_id: null + source: data_local/e13/em/D/info_E1_p0.75.json # scripts/em_matrix.py e1 --models D --pair 0.75 (2026-10-14; train_keys_md5 = D's E3 rows) + summary: "log-Richardson R(0.75, 1.5) of the slab lite micro-MoM plus per-entry-kind offsets c_kind fitted (median log residual, the E1 rule of scripts/em_matrix.py) on D's 42,967 training designs; no network." + warning: "Deployment fast model (D constants): fitted on the in-family test split, exp13-15 and OOD-60, so no test number of the paper applies to it; its held-out scoring is pending. The paper's independently tested fast model is 'E1'." + scoring_note: *d_scoring + accuracy: {} + +# ---------------------------------------------------------------- GNN Space (deploy/GNN_SPACE.md; user 2026-10-16) +# Built by `python deploy/sync.py --gnn` (dry run) / `--gnn --push`; gated by deploy/tests/test_gnn_space.py. New models: add an +# entry below, keep the old one with status: retired (never delete), point gnn_hub.default_model at the new id. +gnn_hub: + space: shanto268/PlanarCapGNN # NEW private repo (user, 2026-10-16); never the Spaces above + title: Planar Capacitance (GNN preview) + private: true # Spaces stay private (CLAUDE.md rule 11) + space_hardware: cpu-basic # free tier: 2 vCPU, 16 GB RAM + default_model: GNN + compare_models: [E3, C0] # final_models entries served next to the GNN (tlsq_infer.final, as in the v2 Space) + finalization: agents/geomfast/FINALIZATION_V1.json # frozen checkpoint SHA256s; sync.py refuses checkpoints not listed there + parity_reference: data_local/geomfast/raw_parity.json # the 14 TRAIN layouts of the raw-GDS parity gate + frozen_stage: /Users/shanto/Documents/ChatGPT/transfer_learning/geomfast_stage # frozen inference code (parity reference; local) + fastprior_status: agents/geomfast/FASTPRIOR_STATUS.md # cpu_fast allowed only with a "PASSED for ... cpu_fast" line there (2026-10-16: cpu_fast FAILED, 9.6e-6) + comparison_note: "Paired against E3 on twelve held-out cohorts (paper_v4; equivalence margin 0.25 percentage points of layout-mean median error or 0.05 fF of MAE, set after the results were seen): equivalent on eight, materially better on four (two-net test, component atlas, the three added families against the five-family E3 that never saw them while the GNN trained on 828 other designs of them, sealed synthetic layouts on the median only), materially worse on none; equal end-to-end latency (the two prior solves dominate both)." + +gnn_models: + GNN: + title: "Accurate (physics + network, ~1 s): corrected prior v2m + 3-seed GNN ensemble" + status: available + tier: accurate + arm: full_gnn + role: "accurate tier, default (since 2026-10-17; arm Bm of agents/priorv2): the frozen P2d full_gnn weights (FINALIZATION_V1, unchanged) + prior v2m solved by the compiled exact solver" + ensemble: arithmetic mean of the three full Maxwell matrices + checkpoints: + - {path: data_local/geomfast/runs/p2d_full_gnn_s0/model.pt, sha256: fcb5db5d8237436d546aca0e7463a2f1d4f2e1b18757b2a35eff646c03f3bc5c} + - {path: data_local/geomfast/runs/p2d_full_gnn_s1/model.pt, sha256: 9aef64adfaf99cad35da891a2329d43eac46b2768cd133b5313085891a4fed92} + - {path: data_local/geomfast/runs/p2d_full_gnn_s2/model.pt, sha256: 357c16539967404b0a7eea514ef3ff3dc763c764da192b219d919cb01f697669} + prior: + method: local_gap # micro_mom_loc (slab, local-gap mesh); base = 2 log C(3) - log C(1.5) + ppg: [1.5, 3.0] + alpha: 0.49685219816506904 # unchanged (TRAIN-fitted) + prior_impl: frozen + version: v2m_fc_dipfill_margin # agents/priorv2/FINALIZATION_V2.json "prior" + patches: [priorv2m] # VALUE-CHANGING: geomgnn/priorv2.py = scripts/priorv2/priorv2.py, installed(policy='fc', dip=True, margin=True) + module_sha256: 1a0bdcb71500c7f11e63238afe531c18caabd5c9f6f7aaa73d9b7dcd0ad61f8c + mode: exact_compiled # geomgnn.fastprior_pkg solver mode (fv + hmat_vec + mixed + numba compiled H-matrix); TLSQ_GNN_PRIOR_MODE overrides + speed_package_sha256: 956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde # scripts/fastprior_pkg/fastprior_pkg.py, vendored verbatim + speed_approved: "user 2026-10-17 ('faster at equal accuracy is a win'): fastprior_pkg 'Ready to deploy' (agents/fastprior_pkg/RESULTS.md); exact_compiled VAL tie on all 13 families; deploy-path parity gate in deploy/tests/test_gnn_space.py (<= 1e-6 vs the v2m exact path)" + certified_opt_in: "mode certified (+ Rayleigh-Ritz windows with an error certificate) is gated on VAL (tie) but not enabled here; set TLSQ_GNN_PRIOR_MODE=certified (Space variable) to opt in" + value_changing_approved: "user 2026-10-17 ('do whatever to make the models faster and better'); prior v2 PASSED its pre-declared protocol, arm Bm (frozen nets + v2m) recommended for deployment (agents/priorv2/RESULTS.md verdict 4, FINALIZATION_V2.json)" + summary: "The paper_v4 GNN (same frozen weights) on prior v2m: the frozen prior with three fixes of its windowed path (layouts with more than 8 conductors): rescue panels for metals the window mesh leaves without a panel (fc; also removes the rejection tail), the dipole fill for same-pocket far pairs (a physics bug of the frozen fill) and a wider window ground margin. Predictions for N <= 9 are bit-identical to the v1-prior model." + accuracy_note: "Validation split (VAL, development; NOT a test set), arm Bm of agents/priorv2 (data_local/priorv2/val_summary.csv): layout-mean median relative error, MAE and RMSE over mutual entries, 0.1 fF floor; priors computed on the GH200 CPU. v1 prior on the same VAL: all 0.337 % (0.0750 / 0.0997 fF), N > 9 3.468 % (0.2360 / 0.7004 fF). Test numbers (paper_v4, v2m): accuracy_test; the prior-v1 test scoring is kept as accuracy_test_prior_v1." + accuracy: + all_val: {set: "VAL, all layouts", layouts: 4608, median_pct: 0.2287, mae_ff: 0.06737, rmse_ff: 0.0792} + val_n_le_9: {set: "VAL, N <= 9 (bit-identical to the v1-prior model)", layouts: 4428, median_pct: 0.2096, mae_ff: 0.06846, rmse_ff: 0.07529} + val_n_gt_9: {set: "VAL, N > 9", layouts: 180, median_pct: 0.6979, mae_ff: 0.04063, rmse_ff: 0.1752} + val_b3_n_gt_9: {set: "VAL, campaign B3, N > 9", layouts: 115, median_pct: 0.8698, mae_ff: 0.04342, rmse_ff: 0.1724} + val_synth_n_gt_9: {set: "VAL, synthetic, N > 9", layouts: 65, median_pct: 0.3938, mae_ff: 0.0357, rmse_ff: 0.1801} + val_fc_tail: {set: "VAL, rejection-tail layouts (fc fires)", layouts: 16, median_pct: 0.6802, mae_ff: 0.03806, rmse_ff: 0.1842} + val_fill: {set: "VAL, same-pocket dipole fill fires", layouts: 54, median_pct: 1.092, mae_ff: 0.06035, rmse_ff: 0.2571} + val_margin: {set: "VAL, window margin widening fires", layouts: 40, median_pct: 1.207, mae_ff: 0.07865, rmse_ff: 0.3569} + val_twonet: {set: "VAL, two-net", layouts: 98, median_pct: 0.9075, mae_ff: 2.105, rmse_ff: 2.105} + accuracy_signal_self: + all_val: {set: "VAL, all layouts, signal self terms", layouts: 4608, median_pct: 0.2873, mae_ff: 0.1205, rmse_ff: 0.141} + val_n_gt_9: {set: "VAL, N > 9, signal self terms", layouts: 180, median_pct: 1.108, mae_ff: 0.2903, rmse_ff: 0.5737} + accuracy_test_note: "TEST (paper_v4 Table tab:ladder-test and SM Table tab:ladder-cohort, column 'model' = corrected prior v2m, compiled solver; read once): layout-mean median relative error %, MAE and RMSE fF over mutual entries, 0.1 fF floor. Zero-shot macro = mean of the nine component cohorts' layout means (1,878 layouts)." + accuracy_test: + infamily_test: {set: "in-family test (exp6-12 test split)", layouts: 4009, median_pct: 0.177, mae_ff: 0.0203, rmse_ff: 0.0250} + zeroshot_macro: {set: "zero-shot macro (9 component cohorts below)", layouts: 1878, median_pct: 0.587, mae_ff: 1.092, rmse_ff: 1.152} + newfam926: {set: "added families: Xmon, TransmonPocket, shielded concentric (926)", layouts: 926, median_pct: 0.264, mae_ff: 0.1164, rmse_ff: 0.1657} + twonet_test: {set: "two-net test", layouts: 147, median_pct: 0.594, mae_ff: 2.387, rmse_ff: 2.387} + random_sealed: {set: "random layouts, sealed", layouts: 98, median_pct: 0.559, mae_ff: 0.0903, rmse_ff: 0.1216} + synth_sealed: {set: "procedurally generated layouts, sealed", layouts: 107, median_pct: 0.651, mae_ff: 0.0462, rmse_ff: 0.1583} + p3rand36: {set: "held-out TransmonPocket + 3 couplers", layouts: 36, median_pct: 0.326, mae_ff: 0.0686, rmse_ff: 0.1152} + unusual: {set: "freeform geometries", layouts: 60, median_pct: 0.570, mae_ff: 0.2197, rmse_ff: 0.2873} + qm116: {set: "unseen qubits and couplers", layouts: 116, median_pct: 0.672, mae_ff: 0.2103, rmse_ff: 0.3522} + atlas: {set: "quantum-metal component atlas", layouts: 348, median_pct: 0.993, mae_ff: 6.621, rmse_ff: 6.631} + largen: {set: "large N, 8-12 conductors", layouts: 40, median_pct: 0.657, mae_ff: 0.0662, rmse_ff: 0.1522} + grid_qubit_1: {set: "one-qubit lattice, N = 15", layouts: 1, median_pct: 0.370, mae_ff: 0.0538, rmse_ff: 0.1769} + grid_qubit_5: {set: "five-qubit lattice, N = 35", layouts: 1, median_pct: 1.009, mae_ff: 0.0356, rmse_ff: 0.1449} + grid_qubit_9: {set: "nine-qubit lattice, N = 67", layouts: 1, median_pct: 0.960, mae_ff: 0.0221, rmse_ff: 0.1042} + accuracy_lofo_note: "Held-out component family (leave-one-family-out over the five original families; VAL only, no test cohort read): the shipped recipe (P2d full_gnn on prior v2m, 3-seed mean) retrained without the family, scored on that family's held-out VAL layouts (agents/shipped_lofo/RESULTS_P1.md, row d, protocol PROTOCOL_P1.md frozen 2026-10-18). Layout-mean median relative error %, MAE and RMSE fF over mutual entries, 0.1 fF floor. Better than the physics prior alone C^R = R(1.5, 3) on every family, median, MAE and RMSE (pooled 1.554 %, 0.166 / 0.205 fF). The served weights saw these families; this measures a new component type." + accuracy_lofo: + lofo_pooled: {set: "held-out family, pooled VAL", layouts: 4071, median_pct: 0.408, mae_ff: 0.0494, rmse_ff: 0.0591} + lofo_macro: {set: "held-out family, macro of 5", layouts: 4071, median_pct: 0.390, mae_ff: 0.1065, rmse_ff: 0.1383} + lofo_interdigital: {set: "interdigital held out", layouts: 1931, median_pct: 0.413, mae_ff: 0.0397, rmse_ff: 0.0435} + lofo_paddle: {set: "paddle held out", layouts: 1592, median_pct: 0.417, mae_ff: 0.0274, rmse_ff: 0.0312} + lofo_concentric: {set: "concentric held out", layouts: 170, median_pct: 0.544, mae_ff: 0.3824, rmse_ff: 0.4906} + lofo_star: {set: "star held out", layouts: 202, median_pct: 0.200, mae_ff: 0.0395, rmse_ff: 0.0492} + lofo_multipad: {set: "multipad star held out", layouts: 176, median_pct: 0.376, mae_ff: 0.0435, rmse_ff: 0.0771} + latency_test_note: "paper_v4 Table tab:ladder-test: end to end per layout from the layout file, median / 90th percentile, every arm in the same pass on two CPUs of an x86 cluster node (AMD EPYC 7513 / 7542, Xeon Silver 4116)." + latency_test: + infamily_test: {set: "in-family test", layouts: 4009, median_s: 0.845, p90_s: 5.96} + zeroshot: {set: "zero-shot cohorts, pooled", layouts: 1878, median_s: 1.76, p90_s: 18.1} + all_test: {set: "sum over all test layouts", layouts: 5902, sum_s: 20385} + accuracy_test_prior_v1_note: "paper_v4 Table 2 test cohorts, scored once with the SAME weights on prior v1 (the retired default; kept for the record). Layouts with N <= 9 give bit-identical predictions under v2m; the v2m test numbers are accuracy_test." + accuracy_test_prior_v1: + infamily_test: {set: "in-family test (exp6-12 test split)", layouts: 4009, median_pct: 0.1775, mae_ff: 0.02032, rmse_ff: 0.02502} + unusual: {set: "freeform shapes (OOD-60)", layouts: 60, median_pct: 0.5702, mae_ff: 0.2197, rmse_ff: 0.2873} + newfam926: {set: "added families: Xmon, TransmonPocket, shielded concentric (926 test designs; the GNN trained on 828 others of these families)", layouts: 926, median_pct: 0.2642, mae_ff: 0.1164, rmse_ff: 0.1657} + qm116: {set: "unseen qubits and couplers (qm116)", layouts: 116, median_pct: 0.6722, mae_ff: 0.2103, rmse_ff: 0.3522} + p3rand36: {set: "held-out TransmonPockets (p3rand36)", layouts: 36, median_pct: 0.3262, mae_ff: 0.06864, rmse_ff: 0.1152} + largen: {set: "large N, 8-12 conductors", layouts: 40, median_pct: 0.6575, mae_ff: 0.06625, rmse_ff: 0.1522} + atlas: {set: "quantum-metal component atlas", layouts: 348, median_pct: 0.9929, mae_ff: 6.621, rmse_ff: 6.631} + twonet_test: {set: "two-net test", layouts: 147, median_pct: 0.5945, mae_ff: 2.391, rmse_ff: 2.391} + random_sealed: {set: "random layouts, sealed", layouts: 110, median_pct: 0.5672, mae_ff: 0.09022, rmse_ff: 0.1225} + synth_sealed: {set: "synthetic layouts, sealed (2-68 conductors)", layouts: 107, median_pct: 0.6995, mae_ff: 0.1108, rmse_ff: 0.4076} + grid_qubit_1: {set: "grid unit, N = 15 (one layout)", layouts: 1, median_pct: 0.3703, mae_ff: 0.05383, rmse_ff: 0.1769} + grid_qubit_5: {set: "grid cluster, N = 35 (one layout)", layouts: 1, median_pct: 1.009, mae_ff: 0.0356, rmse_ff: 0.1449} + latency_note: "prior v1 numbers (paper_v4 Table 6). prior v2m: same cost on N <= 9; on the rejection tail the prior is 18-68x faster (agents/priorv2/RESULTS.md: VAL tail 3,609 -> 52.6 s on the GH200 CPU), the margin widening adds ~2 %." + latency: + development: {set: "development: 14 TRAIN layouts, one per source, N <= 18 (job 6492904)", layouts: 14, total_median_s: 1.258, total_p95_s: 53.25, geometry_median_s: 0.1202, prior_median_s: 0.95, network_median_s: 0.0116, paired_speedup_vs_E3: 1.14, speedup_ci95: [1.04, 1.26], machine: "GH200 node c02-13 (aarch64), 2 threads, 3 interleaved invocations per layout, median over layouts"} + external: {set: "external: 9 label-free layouts, N = 2-68 (job 6493337)", layouts: 9, total_median_s: 13.08, total_p95_s: 67.88, geometry_median_s: 1.796, prior_median_s: 10.6, network_median_s: 0.0428, paired_speedup_vs_E3: 0.955, speedup_ci95: [0.924, 0.981], machine: "GH200 node c02-13 (aarch64), 2 threads, 3 interleaved invocations per layout, median over layouts"} + GNN-prior-v1: + title: "GNN, prior v1 (default until 2026-10-17): full geometric embedding + two-resolution MoM prior, 3-seed ensemble" + status: retired # 2026-10-17: replaced as default by GNN (same weights, prior v2m); kept, never deleted + arm: full_gnn + role: retired default (paper_v4 primary; frozen 2026-10-16, FINALIZATION_V1); prior v1 + rejtail_fv + ensemble: arithmetic mean of the three full Maxwell matrices + checkpoints: + - {path: data_local/geomfast/runs/p2d_full_gnn_s0/model.pt, sha256: fcb5db5d8237436d546aca0e7463a2f1d4f2e1b18757b2a35eff646c03f3bc5c} + - {path: data_local/geomfast/runs/p2d_full_gnn_s1/model.pt, sha256: 9aef64adfaf99cad35da891a2329d43eac46b2768cd133b5313085891a4fed92} + - {path: data_local/geomfast/runs/p2d_full_gnn_s2/model.pt, sha256: 357c16539967404b0a7eea514ef3ff3dc763c764da192b219d919cb01f697669} + prior: + method: local_gap # micro_mom_loc (slab, local-gap mesh); base = 2 log C(3) - log C(1.5) + ppg: [1.5, 3.0] + alpha: 0.49685219816506904 # gap-share alpha fitted on TRAIN only + prior_impl: frozen # frozen | cpu_fast (geomgnn.fastmom; cpu_fast FAILED parity 2026-10-16: keep frozen) + patches: [rejtail_fv] # value-preserving rejection-tail fix (geomgnn/rejtail_fv.py; agents/rejtail/RESULTS.md; user 2026-10-17); bit-identical matrices + summary: "Message-passing network (2 rounds, 89,858 parameters per seed) over the full conductor vectors (shape scalars, Fourier contour, width spectrum), pair vectors with local context tokens and the global vector; corrects the log-Richardson extrapolation of the slab micro-MoM at 1.5 and 3 panels per gap by at most +-0.2 in log and predicts every self term through a learned positive row-sum closure, so the output is the complete Maxwell matrix (symmetric, Maxwell-signed, positive-definite signal block by construction). Trained on 35,354 Q3D layouts (all training sources, 2-68 conductors), Si 11.45 / 350 um." + accuracy: # paper_v4 Table 2 (data_local/geomfast/consolidated/layout_macro_summary.csv): layout-mean, 0.1 fF floor + infamily_test: {set: "in-family test (exp6-12 test split)", layouts: 4009, median_pct: 0.1775, mae_ff: 0.02032, rmse_ff: 0.02502} + unusual: {set: "freeform shapes (OOD-60)", layouts: 60, median_pct: 0.5702, mae_ff: 0.2197, rmse_ff: 0.2873} + newfam926: {set: "added families: Xmon, TransmonPocket, shielded concentric (926 test designs; the GNN trained on 828 others of these families)", layouts: 926, median_pct: 0.2642, mae_ff: 0.1164, rmse_ff: 0.1657} + qm116: {set: "unseen qubits and couplers (qm116)", layouts: 116, median_pct: 0.6722, mae_ff: 0.2103, rmse_ff: 0.3522} + p3rand36: {set: "held-out TransmonPockets (p3rand36)", layouts: 36, median_pct: 0.3262, mae_ff: 0.06864, rmse_ff: 0.1152} + largen: {set: "large N, 8-12 conductors", layouts: 40, median_pct: 0.6575, mae_ff: 0.06625, rmse_ff: 0.1522} + atlas: {set: "quantum-metal component atlas", layouts: 348, median_pct: 0.9929, mae_ff: 6.621, rmse_ff: 6.631} + twonet_test: {set: "two-net test", layouts: 147, median_pct: 0.5945, mae_ff: 2.391, rmse_ff: 2.391} + random_sealed: {set: "random layouts, sealed", layouts: 110, median_pct: 0.5672, mae_ff: 0.09022, rmse_ff: 0.1225} + synth_sealed: {set: "synthetic layouts, sealed (2-68 conductors)", layouts: 107, median_pct: 0.6995, mae_ff: 0.1108, rmse_ff: 0.4076} + grid_qubit_1: {set: "grid unit, N = 15 (one layout)", layouts: 1, median_pct: 0.3703, mae_ff: 0.05383, rmse_ff: 0.1769} + grid_qubit_5: {set: "grid cluster, N = 35 (one layout)", layouts: 1, median_pct: 1.009, mae_ff: 0.0356, rmse_ff: 0.1449} + latency: # paper_v4 Table 6 (data_local/geomfast/consolidated/timing_summary.csv) + development: {set: "development: 14 TRAIN layouts, one per source, N <= 18 (job 6492904)", layouts: 14, total_median_s: 1.258, total_p95_s: 53.25, geometry_median_s: 0.1202, prior_median_s: 0.95, network_median_s: 0.0116, paired_speedup_vs_E3: 1.14, speedup_ci95: [1.04, 1.26], machine: "GH200 node c02-13 (aarch64), 2 threads, 3 interleaved invocations per layout, median over layouts"} + external: {set: "external: 9 label-free layouts, N = 2-68 (job 6493337)", layouts: 9, total_median_s: 13.08, total_p95_s: 67.88, geometry_median_s: 1.796, prior_median_s: 10.6, network_median_s: 0.0428, paired_speedup_vs_E3: 0.955, speedup_ci95: [0.924, 0.981], machine: "GH200 node c02-13 (aarch64), 2 threads, 3 interleaved invocations per layout, median over layouts"} + GNN-LP: + title: "Fast (learned prior, ~0.1 s; trained component families only): learned prior surrogate + the same 3-seed GNN ensemble" + status: available + tier: fast + arm: full_gnn + role: "fast tier (2026-10-18; agents/learnedprior final arm): the learned prior surrogate t100_norm in place of the two field solves, feeding the frozen P2d full_gnn weights (FINALIZATION_V1, unchanged); no field solve" + ensemble: arithmetic mean of the three full Maxwell matrices + checkpoints: + - {path: data_local/geomfast/runs/p2d_full_gnn_s0/model.pt, sha256: fcb5db5d8237436d546aca0e7463a2f1d4f2e1b18757b2a35eff646c03f3bc5c} + - {path: data_local/geomfast/runs/p2d_full_gnn_s1/model.pt, sha256: 9aef64adfaf99cad35da891a2329d43eac46b2768cd133b5313085891a4fed92} + - {path: data_local/geomfast/runs/p2d_full_gnn_s2/model.pt, sha256: 357c16539967404b0a7eea514ef3ff3dc763c764da192b219d919cb01f697669} + prior: null # no field solve; the learned prior below predicts both priors + learned_prior: + name: surrogate_t100_norm + checkpoint: {path: data_local/learnedprior/runs/t100_norm/model.pt, sha256: 622fc374cc989bcc5569373787996169b8cde1254efdd118baa7252da0d788ce} # = agents/v4test/HASHES.json surrogate_t100_norm + code: "geomgnn/lp_surrogate.py = scripts/learnedprior/lp_surrogate.py (sha256 55804c93..., verbatim) + schema_p2d.json (17eba5f7...); geomgnn/learned_prior.py = lp_bench.py Learned.from_gds after the features" + ppg: [1.5, 3.0] # predicts log P at 1.5 and 3 panels per gap (the priors the networks read) + training: "tri-GNN (1.02M parameters, width 128, 4 rounds) on the exact frozen prior of 35,354 geomfast TRAIN layouts + 384k generated layouts (v1 235k + targeted 149k), 60k steps, H200 job 6497375 (agents/learnedprior/STATUS.md, PROTOCOL.md)" + summary: "The accurate tier's networks (same frozen weights) fed by a learned surrogate of the two field-solve priors instead of the solves: about 0.1 s per layout, no field solve. Its accuracy holds only for the 13 component families its learned prior and networks were trained on: interdigital capacitors, paddle capacitors, concentric, star, multipad star, Xmon, TransmonPocket, shielded concentric, two-net layouts, random layouts, procedurally generated layouts (2-7 and 8 or more conductors) and the synthetic multi-conductor campaign layouts (up to 18 conductors). On a component family it never saw, the surrogate's prior error rises 3-17x and the fast tier is worse than the physics prior alone (accuracy_lofo). For a new component type use the accurate tier (GNN)." + warning: "Use Fast only for the 13 component families it was trained on: interdigital capacitors, paddle capacitors, concentric, star, multipad star, Xmon, TransmonPocket, shielded concentric, two-net layouts, random layouts, procedurally generated layouts (2-7 and 8 or more conductors) and synthetic multi-conductor campaign layouts (up to 18 conductors). For any other component type use Accurate (GNN). On a component family held out of its training (learned prior and networks both retrained without it; five families, 4,071 held-out validation layouts) Fast is at 3.11 % (MAE 0.533 / RMSE 0.633 fF), worse than the physics prior alone, 1.55 % (0.166 / 0.205 fF), while Accurate held out the same way is at 0.41 % (0.049 / 0.059 fF); worst on concentric, 11.7 % (6.43 / 7.36 fF). In family (test) Fast is at 0.630 % (0.113 / 0.141 fF) vs Accurate 0.177 % (0.0203 / 0.0250 fF); on the never-seen test components (zero-shot macro) 3.58 % (4.61 / 5.06 fF) vs 0.587 % (1.09 / 1.15 fF). Use Accurate for any number you rely on." + accuracy_note: "Validation split (VAL, development; NOT a test set; agents/learnedprior ladder_val4608, data_local/learnedprior/e2e_phase2_t100/ladder_val4608.json): layout-mean median relative error, MAE and RMSE over mutual entries, 0.1 fF floor. Same VAL, exact prior v1 + the same networks: 0.337 % (0.0750 / 0.0997 fF); accurate tier (v2m): 0.229 % (0.0674 / 0.0792 fF); solver-free network: 1.486 % (0.630 / 0.728 fF)." + accuracy: + all_val: {set: "VAL, all layouts", layouts: 4608, median_pct: 0.8457, mae_ff: 0.4910, rmse_ff: 0.5486} + val_interdigital: {set: "VAL, interdigital", layouts: 1931, median_pct: 0.5782, mae_ff: 0.06004, rmse_ff: 0.06874} + val_paddle: {set: "VAL, paddle", layouts: 1592, median_pct: 0.4224, mae_ff: 0.03180, rmse_ff: 0.03893} + val_star: {set: "VAL, star", layouts: 202, median_pct: 0.7109, mae_ff: 0.1384, rmse_ff: 0.1741} + val_multipad: {set: "VAL, multipad", layouts: 176, median_pct: 1.314, mae_ff: 0.1447, rmse_ff: 0.2437} + val_concentric: {set: "VAL, concentric", layouts: 170, median_pct: 1.778, mae_ff: 1.160, rmse_ff: 1.449} + val_campaign_b3: {set: "VAL, campaign B3 (N up to 18)", layouts: 149, median_pct: 3.926, mae_ff: 0.2707, rmse_ff: 0.8017} + val_twonet: {set: "VAL, two-net", layouts: 98, median_pct: 2.811, mae_ff: 17.14, rmse_ff: 17.14} + val_random: {set: "VAL, random", layouts: 94, median_pct: 2.292, mae_ff: 0.3121, rmse_ff: 0.3924} + val_synth_n_ge_8: {set: "VAL, synthetic N >= 8", layouts: 74, median_pct: 1.417, mae_ff: 0.1628, rmse_ff: 0.7144} + val_synth_n_le_7: {set: "VAL, synthetic N <= 7", layouts: 29, median_pct: 2.422, mae_ff: 0.4819, rmse_ff: 0.8089} + val_transmon_pocket: {set: "VAL, TransmonPocket", layouts: 48, median_pct: 1.290, mae_ff: 0.2917, rmse_ff: 0.4039} + val_shielded_concentric: {set: "VAL, shielded concentric", layouts: 27, median_pct: 3.267, mae_ff: 1.655, rmse_ff: 2.298} + val_xmon: {set: "VAL, Xmon", layouts: 18, median_pct: 1.647, mae_ff: 0.6089, rmse_ff: 0.8233} + accuracy_lofo_note: "Held-out component family (leave-one-family-out over the five original families; VAL only, no test cohort read): the learned prior surrogate AND the networks retrained without the family, scored on that family's held-out VAL layouts (agents/fastlofo_p5/RESULTS_P5.md, row a, protocol PROTOCOL_P5.md frozen 2026-10-18; data_local/fastlofo_p5/metrics/family_table.csv). Layout-mean median relative error %, MAE and RMSE fF over mutual entries, 0.1 fF floor. Same layouts: physics prior alone C^R = R(1.5, 3) 1.554 % (0.166 / 0.205 fF); accurate recipe held out the same way 0.408 % (0.0494 / 0.0591 fF). Mac M5 Pro, 2 threads: fast 0.0835 s median per layout (p90 0.163 s), exact prior + networks 1.891 s (p90 9.53 s)." + accuracy_lofo: + lofo_pooled: {set: "held-out family, pooled VAL", layouts: 4071, median_pct: 3.113, mae_ff: 0.533, rmse_ff: 0.633} + lofo_macro: {set: "held-out family, macro of 5", layouts: 4071, median_pct: 5.413, mae_ff: 1.622, rmse_ff: 1.974} + lofo_interdigital: {set: "interdigital held out", layouts: 1931, median_pct: 2.550, mae_ff: 0.291, rmse_ff: 0.331} + lofo_paddle: {set: "paddle held out", layouts: 1592, median_pct: 2.444, mae_ff: 0.184, rmse_ff: 0.206} + lofo_concentric: {set: "concentric held out", layouts: 170, median_pct: 11.693, mae_ff: 6.431, rmse_ff: 7.358} + lofo_star: {set: "star held out", layouts: 202, median_pct: 1.761, mae_ff: 0.327, rmse_ff: 0.404} + lofo_multipad: {set: "multipad star held out", layouts: 176, median_pct: 8.619, mae_ff: 0.877, rmse_ff: 1.573} + accuracy_test_note: "TEST (paper_v4 Table tab:ladder-test and SM Table tab:ladder-cohort-b, column 'surrogate + networks'; read once): layout-mean median relative error %, MAE and RMSE fF over mutual entries, 0.1 fF floor. Zero-shot macro = mean of the nine component cohorts' layout means (1,878 layouts)." + accuracy_test: + infamily_test: {set: "in-family test (exp6-12 test split)", layouts: 4009, median_pct: 0.630, mae_ff: 0.1130, rmse_ff: 0.1414} + zeroshot_macro: {set: "zero-shot macro (9 component cohorts below)", layouts: 1878, median_pct: 3.576, mae_ff: 4.608, rmse_ff: 5.058} + newfam926: {set: "added families: Xmon, TransmonPocket, shielded concentric (926)", layouts: 926, median_pct: 1.879, mae_ff: 0.795, rmse_ff: 1.103} + twonet_test: {set: "two-net test", layouts: 147, median_pct: 2.453, mae_ff: 15.90, rmse_ff: 15.90} + random_sealed: {set: "random layouts, sealed", layouts: 98, median_pct: 2.136, mae_ff: 0.328, rmse_ff: 0.417} + synth_sealed: {set: "procedurally generated layouts, sealed", layouts: 107, median_pct: 1.449, mae_ff: 0.139, rmse_ff: 0.465} + p3rand36: {set: "held-out TransmonPocket + 3 couplers", layouts: 36, median_pct: 4.014, mae_ff: 1.045, rmse_ff: 2.169} + unusual: {set: "freeform geometries", layouts: 60, median_pct: 5.961, mae_ff: 2.288, rmse_ff: 2.952} + qm116: {set: "unseen qubits and couplers", layouts: 116, median_pct: 4.767, mae_ff: 1.358, rmse_ff: 2.230} + atlas: {set: "quantum-metal component atlas", layouts: 348, median_pct: 5.477, mae_ff: 19.23, rmse_ff: 19.37} + largen: {set: "large N, 8-12 conductors", layouts: 40, median_pct: 4.049, mae_ff: 0.384, rmse_ff: 0.920} + grid_qubit_1: {set: "one-qubit lattice, N = 15", layouts: 1, median_pct: 1.549, mae_ff: 0.548, rmse_ff: 1.649} + grid_qubit_5: {set: "five-qubit lattice, N = 35", layouts: 1, median_pct: 2.471, mae_ff: 0.206, rmse_ff: 0.751} + grid_qubit_9: {set: "nine-qubit lattice, N = 67", layouts: 1, median_pct: 1.408, mae_ff: 0.151, rmse_ff: 0.940} + latency_test_note: "paper_v4 Table tab:ladder-test: end to end per layout from the layout file, median / 90th percentile, every arm in the same pass on two CPUs of an x86 cluster node (AMD EPYC 7513 / 7542, Xeon Silver 4116)." + latency_test: + infamily_test: {set: "in-family test", layouts: 4009, median_s: 0.092, p90_s: 0.26} + zeroshot: {set: "zero-shot cohorts, pooled", layouts: 1878, median_s: 0.30, p90_s: 2.47} + all_test: {set: "sum over all test layouts", layouts: 5902, sum_s: 2635} + latency_note: "agents/learnedprior/STATUS.md (job 6501886, data_local/learnedprior/bench/final_t100): same run, exact prior v1 + the same networks 1.120 s median (p95 31.6 s, max 56.2 s); the surrogate step itself 5.6 ms median." + latency: + development: {set: "development: 14 TRAIN layouts, one per source, N <= 18 (job 6501886)", layouts: 14, total_median_s: 0.1224, total_p95_s: 0.5945, geometry_median_s: 0.1116, prior_median_s: 0.00556, network_median_s: 0.00317, machine: "GH200 node c02-13 CPU (aarch64, shared node), 2 threads, 3 interleaved invocations per layout, median over layouts"} + GNN-fast: + title: "GNN-fast: same network, no physics solve (much less accurate on unseen geometry)" + status: retired # 2026-10-18: superseded by GNN-LP (learned prior) at the same speed; kept, never deleted (restore: status available) + retired: "superseded 2026-10-18 by the fast tier GNN-LP (learned prior + the accurate tier's networks): same speed (test sum 2,635 vs 2,619 s), lower MAE and RMSE on every test cohort and lower median on all but the five- and nine-qubit lattices (2.47 vs 1.95 %, 1.41 vs 1.30 %)" + arm: direct_gnn + role: speed trade-off (paper_v4 solver-free arm; frozen 2026-10-16, FINALIZATION_V1) + ensemble: arithmetic mean of the three full Maxwell matrices + checkpoints: + - {path: data_local/geomfast/runs/p2d_direct_gnn_s0/model.pt, sha256: c3c0b8850d74984ebd6d1d42b63d9f8124bf9029627dda60e7aa0a1bdd9065c9} + - {path: data_local/geomfast/runs/p2d_direct_gnn_s1/model.pt, sha256: 3cc2e71de05e29a18212ad467c664d3219ff853c7522d45c1582b7af4185db25} + - {path: data_local/geomfast/runs/p2d_direct_gnn_s2/model.pt, sha256: 57bc28024fc6c5f0c5db424e313474cd64177b13fb9e3bc3d2044b1a01b8f691} + prior: null # no solve: base = training-median log mutual per pair kind, unbounded head + summary: "The GNN without the physics prior: reads the layout and the geometric embedding only. 5-12x faster, but far less accurate outside the training distribution." + warning: "GNN-fast runs no physics solve and is much less accurate on geometry unlike its training layouts: component atlas median 114.5 % (MAE 53.6 fF, RMSE 56.6 fF), unseen qubits and couplers 9.6 % (MAE 2.02 fF, RMSE 3.28 fF). Use GNN for any number you rely on." + accuracy: + infamily_test: {set: "in-family test (exp6-12 test split)", layouts: 4009, median_pct: 1.219, mae_ff: 0.1872, rmse_ff: 0.229} + unusual: {set: "freeform shapes (OOD-60)", layouts: 60, median_pct: 7.007, mae_ff: 3.358, rmse_ff: 4.731} + newfam926: {set: "added families: Xmon, TransmonPocket, shielded concentric (926 test designs; the GNN trained on 828 others of these families)", layouts: 926, median_pct: 2.381, mae_ff: 0.8933, rmse_ff: 1.194} + qm116: {set: "unseen qubits and couplers (qm116)", layouts: 116, median_pct: 9.572, mae_ff: 2.016, rmse_ff: 3.28} + p3rand36: {set: "held-out TransmonPockets (p3rand36)", layouts: 36, median_pct: 6.208, mae_ff: 1.515, rmse_ff: 3.127} + largen: {set: "large N, 8-12 conductors", layouts: 40, median_pct: 6.089, mae_ff: 0.5381, rmse_ff: 1.324} + atlas: {set: "quantum-metal component atlas", layouts: 348, median_pct: 114.5, mae_ff: 53.57, rmse_ff: 56.6} + twonet_test: {set: "two-net test", layouts: 147, median_pct: 2.724, mae_ff: 17.68, rmse_ff: 17.68} + random_sealed: {set: "random layouts, sealed", layouts: 110, median_pct: 3.282, mae_ff: 0.4278, rmse_ff: 0.5397} + synth_sealed: {set: "synthetic layouts, sealed (2-68 conductors)", layouts: 107, median_pct: 3.862, mae_ff: 0.413, rmse_ff: 1.271} + grid_qubit_1: {set: "grid unit, N = 15 (one layout)", layouts: 1, median_pct: 2.271, mae_ff: 1.163, rmse_ff: 3.212} + grid_qubit_5: {set: "grid cluster, N = 35 (one layout)", layouts: 1, median_pct: 1.948, mae_ff: 0.5229, rmse_ff: 2.736} + latency: + development: {set: "development: 14 TRAIN layouts, one per source, N <= 18 (job 6492904)", layouts: 14, total_median_s: 0.1246, total_p95_s: 0.6876, geometry_median_s: 0.1162, prior_median_s: 0.0, network_median_s: 0.00455, paired_speedup_vs_E3: 12.0, speedup_ci95: [6.58, 21.4], machine: "GH200 node c02-13 (aarch64), 2 threads, 3 interleaved invocations per layout, median over layouts"} + external: {set: "external: 9 label-free layouts, N = 2-68 (job 6493337)", layouts: 9, total_median_s: 1.817, total_p95_s: 6.641, geometry_median_s: 1.799, prior_median_s: 0.0, network_median_s: 0.0188, paired_speedup_vs_E3: 5.28, speedup_ci95: [4.0, 7.08], machine: "GH200 node c02-13 (aarch64), 2 threads, 3 interleaved invocations per layout, median over layouts"} + +# ---------------------------------------------------------------- paper Space (paper_v4 PRX; user decision 2026-10-19; deploy/GNN_SPACE.md "Paper Space") +# Built by `python deploy/sync.py --paper` (dry run) / `--paper --push`; gated by deploy/tests/test_paper_space.py. One live selector per arm +# of the paper's ladder (tab:ladder-test) and routes (tab:routes) tables that runs from a layout file on a CPU; every other study +# checkpoint is in the private model repo paper_hub.checkpoints_repo. Numbers: layout-mean median relative error % (MAE / RMSE fF) +# over mutual entries, 0.1 fF floor; VAL = 4,608 validation layouts (development); test = the paper's test benchmark, read once +# (in family 4,009 layouts; zero-shot macro = mean of the nine component cohorts' layout means, 1,878 layouts); time = end to end per +# layout, median / 90th percentile, two CPUs of an x86 cluster node (tab:ladder-test) unless stated. +paper_hub: + space: shanto268/2026PaperModels # reused (same gradio SDK): its earlier contents are archived (archive below) + title: Planar Capacitance (paper models) + private: true # Spaces stay private (CLAUDE.md rule 11) + space_hardware: cpu-basic # free tier: 2 vCPU, 16 GB RAM + default_arm: model + checkpoints_repo: shanto268/PlanarCap2026PaperCheckpoints # private model repo: every other checkpoint the paper reports + scripts + companion_space: shanto268/PlanarCapGNN # the GNN Space (accurate + fast tiers), unchanged + max_conductors: 70 # signal conductors per layout on this Space (the nine-qubit lattice has 66 + ground) + archive: + space: shanto268/2026PaperModels + commit: 6dc21890fa2d8569225ce8be97cb091b87465d0d # last commit of the earlier draft's 21-model Space (2026-10-10); restore = push this revision + local_copy: data_local/hf_archive/2026PaperModels_6dc21890 # snapshot_download of that revision (100 files, 64 MB) + note: "the earlier draft's model repos (SQuADDS/2026PaperModels-*) are untouched" + deployed: {commit: 53ba4502b9697bd65d69f249d71df65c475c863b, date: 2026-10-20, gate: deploy/tests/results/paper_space_gate_20261020.json, + live: deploy/tests/results/paper_space_live_20261020_examples.json} # 24 arms, 156 MB bundle, RUNNING on cpu-basic, private + prior_stacks: # process-wide prior settings (geomgnn.api.set_prior_stack), installed per request + corrected: &stack_corrected # prior v2m + compiled exact solver = the model's prior (gnn_models.GNN); also flagship_hub + patches: [priorv2m] + mode: exact_compiled + module_sha256: 1a0bdcb71500c7f11e63238afe531c18caabd5c9f6f7aaa73d9b7dcd0ad61f8c + speed_package_sha256: 956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde + value_changing_approved: "user 2026-10-17 (gnn_models.GNN)" + speed_approved: "user 2026-10-17 (gnn_models.GNN)" + uncorrected: # prior v1, frozen solver (+ the value-preserving rejection-tail fix, bit-identical) + patches: [rejtail_fv] + version2: # prior version 2 (no window-margin widening), frozen solver: the prior of the 'retrained, version 2' ablation + patches: [priorv2] + module_sha256: 1a0bdcb71500c7f11e63238afe531c18caabd5c9f6f7aaa73d9b7dcd0ad61f8c + value_changing_approved: "paper ablation (SM tab:priorv2 'retrained, version 2'), served for reproduction only (paper Space, 2026-10-19)" + uncorrected_certified: # prior v1, certified solve (Rayleigh-Ritz windows with an error estimate) + patches: [] + mode: certified + speed_package_sha256: 956f2b62c308ec1561bbc033716b52c24dc63a916b9373cc44090eb6514e5dde + speed_approved: "VAL tie on all 13 families (agents/fastprior_pkg/RESULTS.md); paper tab:routes (d)" + +_paper_ckpt: + full_gnn: &ck_full_gnn + - {path: data_local/geomfast/runs/p2d_full_gnn_s0/model.pt, sha256: fcb5db5d8237436d546aca0e7463a2f1d4f2e1b18757b2a35eff646c03f3bc5c} + - {path: data_local/geomfast/runs/p2d_full_gnn_s1/model.pt, sha256: 9aef64adfaf99cad35da891a2329d43eac46b2768cd133b5313085891a4fed92} + - {path: data_local/geomfast/runs/p2d_full_gnn_s2/model.pt, sha256: 357c16539967404b0a7eea514ef3ff3dc763c764da192b219d919cb01f697669} + +paper_arms: + # ---------------- (d) good physics + model: + title: "The model: extrapolated physics on the corrected prior + residual networks + closure, compiled solver" + paper_name: "model (corrected prior, compiled solver)" + route: "(d) good physics" + kind: gnn + arm: full_gnn + stack: corrected + ppg: [1.5, 3.0] + checkpoints: *ck_full_gnn + summary: "C^R = R(1.5, 3) of the corrected prior (two thin-film solves) corrected by the 3-seed network ensemble (89,858 parameters per seed, bounded residual +-0.2 in log) with a learned row-sum closure: the complete Maxwell matrix. Trained on 35,354 Q3D layouts of 13 families." + paper: + tables: [tab:ladder-test, tab:routes (d), tab:sota, tab:cohorts, tab:grid, tab:circuit] + val: {median_pct: 0.229, mae_ff: 0.0674, rmse_ff: 0.0792} + test_infamily: {median_pct: 0.177, mae_ff: 0.0203, rmse_ff: 0.0250} + test_zeroshot: {median_pct: 0.587, mae_ff: 1.092, rmse_ff: 1.152} + heldout_family: {median_pct: 0.408, mae_ff: 0.049, rmse_ff: 0.059} + time: "0.845 / 5.96 s in family, 1.76 / 18.1 s zero-shot (test, x86, 2 CPUs); 1.16 / 6.3 s (GH200)" + certified: + title: "The same networks, certified variational solve (uncorrected prior)" + paper_name: "the same, engineered solves: certified variational solve" + route: "(d) good physics" + kind: gnn + arm: full_gnn + stack: uncorrected_certified + ppg: [1.5, 3.0] + checkpoints: *ck_full_gnn + summary: "The model's networks on the uncorrected prior solved with Rayleigh-Ritz windows that carry an error estimate; the paper's speed comparison of the solver modes, scored on the validation set against the exact solve of the same prior." + paper: + tables: [tab:routes (d), S-tab:package] + val: {median_pct: 0.3369, mae_ff: 0.0772, rmse_ff: 0.1018, note: "against 0.3369 % (0.0750 / 0.0997 fF) with the exact solve of the same prior: a tie on every family"} + time: "0.83 / 3.4 s (GH200, 2 CPU threads, 14 timing layouts) against 1.16 / 6.3 s compiled" + networks-uncorrected: + title: "Networks on the uncorrected prior" + paper_name: "networks on the uncorrected prior" + route: "(d) good physics" + kind: gnn + arm: full_gnn + stack: uncorrected + ppg: [1.5, 3.0] + checkpoints: *ck_full_gnn + summary: "The model's networks (same weights) on the uncorrected prior (frozen solver); identical to the model for N <= 9 conductors." + paper: + tables: [tab:ladder-test, S-tab:priorv2 (frozen, original)] + val: {median_pct: 0.337, mae_ff: 0.0750, rmse_ff: 0.0997} + test_infamily: {median_pct: 0.177, mae_ff: 0.0203, rmse_ff: 0.0250} + test_zeroshot: {median_pct: 0.593, mae_ff: 1.099, rmse_ff: 1.180} + time: "0.857 / 7.91 s in family, 1.89 / 41.5 s zero-shot (test)" + physics: + title: "Extrapolated physics C^R = R(1.5, 3), corrected prior (no learned parameter)" + paper_name: "extrapolated physics R(1.5,3), corrected prior" + route: "(d) good physics" + kind: physics + stack: corrected + levels: [1.5, 3.0] + self_note: "Richardson-extrapolated self terms of the thin-film solve (no network); the ground self term depends on the chip boundary" + summary: "2 log C(3) - log C(1.5) of the thin-film method of moments on the corrected prior, every entry; zero fitted parameters (the gap share alpha is TRAIN-fitted once)." + paper: + tables: [tab:ladder-test, tab:routes (d)] + val: {median_pct: 1.508, mae_ff: 0.291, rmse_ff: 0.337} + test_infamily: {median_pct: 1.559, mae_ff: 0.1703, rmse_ff: 0.2114} + test_zeroshot: {median_pct: 0.982, mae_ff: 1.050, rmse_ff: 1.128} + heldout_family: {median_pct: 1.554, mae_ff: 0.166, rmse_ff: 0.205} + time: "0.752 / 5.70 s in family, 1.39 / 15.0 s zero-shot (test)" + physics-uncorrected: + title: "Extrapolated physics R(1.5, 3), uncorrected prior" + paper_name: "extrapolated physics R(1.5,3), uncorrected prior" + route: "(d) good physics" + kind: physics + stack: uncorrected + levels: [1.5, 3.0] + self_note: "Richardson-extrapolated self terms of the thin-film solve (no network)" + summary: "The same extrapolation on the uncorrected prior (frozen solver)." + paper: + tables: [tab:ladder-test, S-tab:multifid] + val: {median_pct: 1.615, mae_ff: 0.2993, rmse_ff: 0.3574} + test_infamily: {median_pct: 1.559, mae_ff: 0.1703, rmse_ff: 0.2114} + test_zeroshot: {median_pct: 0.991, mae_ff: 1.057, rmse_ff: 1.157} + time: "0.767 / 7.71 s in family, 1.48 / 39.5 s zero-shot (test)" + single-3: + title: "Single solve, 3 panels per gap (corrected prior)" + paper_name: "single solve, 3 ppg" + route: "(d) good physics" + kind: physics + stack: corrected + levels: [3.0] + self_note: "self terms of the thin-film solve (no network)" + summary: "One thin-film solve at 3 panels per gap, no extrapolation." + paper: + tables: [tab:ladder-test] + test_infamily: {median_pct: 2.577, mae_ff: 0.2547, rmse_ff: 0.2991} + test_zeroshot: {median_pct: 1.755, mae_ff: 3.736, rmse_ff: 3.847} + time: "0.491 / 4.01 s in family, 0.94 / 10.4 s zero-shot (test)" + retrained-corrected: + title: "Networks retrained on the corrected prior" + paper_name: "retrained, corrected" + route: "(d) good physics" + kind: gnn + arm: full_gnn + stack: corrected + ppg: [1.5, 3.0] + checkpoints: + - {path: data_local/priorv2/hpc/runs/p2d_v2m_full_gnn_s0/model.pt, sha256: 04721d7e7f5c6faa7aea352fe1f9bb2261c5cc6835d169957bd3a1a8e61164dd} + - {path: data_local/priorv2/hpc/runs/p2d_v2m_full_gnn_s1/model.pt, sha256: f2352aa25536776e85daecd6681da697497ce9474c3a2391baeb66ec27ea89a5} + - {path: data_local/priorv2/hpc/runs/p2d_v2m_full_gnn_s2/model.pt, sha256: 4257ff6b4a16f69151d04514b71e922dd2b3a998f4ef550693365fd5415fcd8f} + summary: "The model's recipe retrained from scratch on the corrected prior (ablation; the paper keeps the frozen networks, which take the whole gain)." + paper: + tables: [S-tab:priorv2 (retrained, corrected)] + val: {median_pct: 0.218, mae_ff: 0.0774, rmse_ff: 0.0888} + retrained-version2: + title: "Networks retrained on prior version 2" + paper_name: "retrained, version 2" + route: "(d) good physics" + kind: gnn + arm: full_gnn + stack: version2 + ppg: [1.5, 3.0] + checkpoints: + - {path: data_local/priorv2/hpc/runs/p2d_v2_full_gnn_s0/model.pt, sha256: 0d436573e10a8d102ece6b58fb2a20d6b50e50f1feb653bc435229725f91c2b3} + - {path: data_local/priorv2/hpc/runs/p2d_v2_full_gnn_s1/model.pt, sha256: a22effd6b33a099bd4e633c188e1f428b626f4b51cbf660d68ab1c63d165a3f6} + - {path: data_local/priorv2/hpc/runs/p2d_v2_full_gnn_s2/model.pt, sha256: e6e93fd00d35836f5c18a960fef59e1cb450c9e52042bcd0fd531b892db9a518} + summary: "The model's recipe retrained on prior version 2 (the corrected prior without the window-margin widening; ablation)." + paper: + tables: [S-tab:priorv2 (retrained, version 2)] + val: {median_pct: 0.230, mae_ff: 0.0853, rmse_ff: 0.0997} + control-no-message-passing: + title: "Control: no message passing" + paper_name: "no-message-passing control" + route: "(d) good physics" + kind: gnn + arm: full_mlp + stack: uncorrected + ppg: [1.5, 3.0] + checkpoints: + - {path: data_local/geomfast/runs/p2d_full_mlp_s0/model.pt, sha256: 38be84e67ca58fe60d797f533e0fe7de355c7bf8e707d73268e19638677f94b6} + - {path: data_local/geomfast/runs/p2d_full_mlp_s1/model.pt, sha256: 37b2fefbac1c8191084464d56c3461687e945f8c76078cbbe3a690553a5ba757} + - {path: data_local/geomfast/runs/p2d_full_mlp_s2/model.pt, sha256: 7efe03578f314ae6516b3f6bd210e61caf25f77d57b05d06d3aef65e56747e9f} + summary: "The model's encoders and heads with no message-passing round (same data, loss, optimiser, seeds), on the uncorrected prior it was trained and scored on." + paper: + tables: [S-app:controls, S-fig:ladder-val] + val: {median_pct: 0.367, mae_ff: 0.084, rmse_ff: 0.109} + control-pair-only: + title: "Control: pair vector only" + paper_name: "pair-only control" + route: "(d) good physics" + kind: gnn + arm: pair_mlp + stack: uncorrected + ppg: [1.5, 3.0] + checkpoints: + - {path: data_local/geomfast/runs/p2d_pair_mlp_s0/model.pt, sha256: 67ee4de766ebdc20295534462929835437e3de437d08b54eb1d705d83d97915a} + - {path: data_local/geomfast/runs/p2d_pair_mlp_s1/model.pt, sha256: 2c84421056598c2d551e570f1e2e20ba55144b3c69cdbb9ee40d3170ab9ae4db} + - {path: data_local/geomfast/runs/p2d_pair_mlp_s2/model.pt, sha256: ddcd72c06be3b3cf6c5860cb4d57cba2e1c38888b6a497086381452382da46dc} + summary: "The no-message-passing control with the conductor vector reduced to the ground flag and the global vector removed: the matched ablation of the geometric embedding (uncorrected prior)." + paper: + tables: [S-app:controls, S-fig:ladder-val] + val: {median_pct: 0.417, mae_ff: 0.126, rmse_ff: 0.157} + prior-generation: + title: "Prior-generation model: extrapolated physics + per-kind offsets + bounded learned correction" + paper_name: "prior-generation model" + route: "baseline" + kind: final + final_model: E3 + trust_rule_applies: true + summary: "The baseline of every results table: R(1.5, 3) of the uncorrected prior (alpha 0.52), per-entry-kind offsets and a +-3 % network correction (747,266 parameters per seed), mutual entries only (self terms from row sums). Accepts other substrates (half-space factor). Its trust rule is on by default (box below)." + paper: + tables: [tab:ladder-test, S-tab:earlier-read, tab:sota] + test_infamily: {median_pct: 0.124, mae_ff: 0.0170, rmse_ff: 0.0211} + test_zeroshot: {median_pct: 0.959, mae_ff: 1.914, rmse_ff: 2.014} + time: "not timed in the test pass" + prior-generation-fast: + title: "Prior-generation model, fast mode: R(0.75, 1.5) + per-kind offsets (no network)" + paper_name: "prior-generation model, fast mode" + route: "baseline" + kind: final + final_model: E1 + summary: "The coarser Richardson pair of the uncorrected prior (alpha 0.52) plus the prior-generation model's per-kind offsets; no network." + paper: + tables: [Fig. sota (fast mode)] + note: "the paper shows it only in the k-curve of Fig. sota; the numbers under the model panel are the prior-generation evaluation (pooled over entries)" + # ---------------- (c) cheap physics + single-0.375: + title: "Single solve, 0.375 panels per gap (corrected prior)" + paper_name: "single solve, 0.375 ppg" + route: "(c) cheap physics" + kind: physics + stack: corrected + levels: [0.375] + self_note: "self terms of the thin-film solve (no network)" + summary: "One thin-film solve at 0.375 panels per gap." + paper: + tables: [tab:ladder-test] + test_infamily: {median_pct: 8.532, mae_ff: 0.7994, rmse_ff: 0.8871} + test_zeroshot: {median_pct: 7.578, mae_ff: 17.19, rmse_ff: 17.84} + time: "0.081 / 0.30 s in family, 0.11 / 0.73 s zero-shot (test)" + single-0.75: + title: "Single solve, 0.75 panels per gap (corrected prior)" + paper_name: "single solve, 0.75 ppg" + route: "(c) cheap physics" + kind: physics + stack: corrected + levels: [0.75] + self_note: "self terms of the thin-film solve (no network)" + summary: "One thin-film solve at 0.75 panels per gap." + paper: + tables: [tab:ladder-test] + test_infamily: {median_pct: 5.609, mae_ff: 0.5284, rmse_ff: 0.5918} + test_zeroshot: {median_pct: 4.777, mae_ff: 12.00, rmse_ff: 12.33} + time: "0.140 / 0.56 s in family, 0.21 / 1.93 s zero-shot (test)" + single-1.5: + title: "Single solve, 1.5 panels per gap (corrected prior)" + paper_name: "single solve, 1.5 ppg" + route: "(c) cheap physics" + kind: physics + stack: corrected + levels: [1.5] + self_note: "self terms of the thin-film solve (no network)" + summary: "One thin-film solve at 1.5 panels per gap." + paper: + tables: [tab:ladder-test] + test_infamily: {median_pct: 3.575, mae_ff: 0.3441, rmse_ff: 0.3928} + test_zeroshot: {median_pct: 2.871, mae_ff: 7.041, rmse_ff: 7.236} + time: "0.261 / 1.45 s in family, 0.42 / 4.81 s zero-shot (test)" + pair-0.375-0.75: + title: "Richardson pair R(0.375, 0.75) (corrected prior)" + paper_name: "R(0.375,0.75)" + route: "(c) cheap physics" + kind: physics + stack: corrected + levels: [0.375, 0.75] + self_note: "Richardson-extrapolated self terms of the thin-film solve (no network)" + summary: "2 log C(0.75) - log C(0.375): the cheapest Richardson pair." + paper: + tables: [tab:ladder-test, S-tab:multifid] + val: {median_pct: 2.773, mae_ff: 0.8157, rmse_ff: 0.8980, note: "validation on the uncorrected prior"} + test_infamily: {median_pct: 2.558, mae_ff: 0.2601, rmse_ff: 0.3125} + test_zeroshot: {median_pct: 3.740, mae_ff: 9.021, rmse_ff: 9.327} + time: "0.223 / 0.91 s in family, 0.32 / 2.79 s zero-shot (test)" + pair-0.75-1.5: + title: "Richardson pair R(0.75, 1.5) (corrected prior)" + paper_name: "R(0.75,1.5)" + route: "(c) cheap physics" + kind: physics + stack: corrected + levels: [0.75, 1.5] + self_note: "Richardson-extrapolated self terms of the thin-film solve (no network)" + summary: "2 log C(1.5) - log C(0.75): the coarser pair." + paper: + tables: [tab:ladder-test, S-tab:multifid] + val: {median_pct: 1.661, mae_ff: 0.4495, rmse_ff: 0.5077, note: "validation on the uncorrected prior"} + test_infamily: {median_pct: 1.508, mae_ff: 0.1663, rmse_ff: 0.2031} + test_zeroshot: {median_pct: 1.660, mae_ff: 2.450, rmse_ff: 2.575} + time: "0.403 / 2.05 s in family, 0.63 / 6.84 s zero-shot (test)" + cheap-2: + title: "Cheap-physics network, 2 levels (0.375, 0.75)" + paper_name: "cheap-physics network, 2 levels (0.375, 0.75)" + route: "(c) cheap physics" + kind: multifid + stack: corrected + levels: [0.375, 0.75] + checkpoints: + - {path: data_local/multifid/runs/L2_s0/model.pt, sha256: af43c30ac6fc2c34b12310b2dc1ccd91c6daf0342f1849f3601ff2659c6a8d1f} + - {path: data_local/multifid/runs/L2_s1/model.pt, sha256: 58d5815b8c8f3d170b04c89c26e86ca4586eae1b881c16fc8873c5bc8c01a45f} + - {path: data_local/multifid/runs/L2_s2/model.pt, sha256: a82145cb2ecc628db6c6c0328004a2c6a62d24c7aeb1d540ba20bdc23092e1fc} + summary: "Solves at 0.375 and 0.75 panels per gap and a residual network on their extrapolation (3 seeds; the variance head is not served)." + paper: + tables: [tab:ladder-test, tab:routes (c), S-tab:multifid] + val: {median_pct: 0.695, mae_ff: 0.1996, rmse_ff: 0.2397, note: "validation on the uncorrected prior"} + test_infamily: {median_pct: 0.480, mae_ff: 0.0510, rmse_ff: 0.0622} + test_zeroshot: {median_pct: 1.695, mae_ff: 2.591, rmse_ff: 2.779} + time: "0.308 / 1.13 s in family, 0.66 / 5.83 s zero-shot (test)" + cheap-3: + title: "Cheap-physics network, 3 levels (0.375, 0.75, 1.5)" + paper_name: "cheap-physics network, 3 levels (0.375, 0.75, 1.5)" + route: "(c) cheap physics" + kind: multifid + stack: corrected + levels: [0.375, 0.75, 1.5] + checkpoints: + - {path: data_local/multifid/runs/L3_s0/model.pt, sha256: 5aeb0c03f180ea3365ceadf9d30ea0eee559087e684cfc07d237cc2842084877} + - {path: data_local/multifid/runs/L3_s1/model.pt, sha256: e1bb3f6c09477bf434d1319d420ad0aa323f6239494bd29d740e650a3fb4fad0} + - {path: data_local/multifid/runs/L3_s2/model.pt, sha256: 100d0363aab3567324d0f42e6040161b132700148a56473e5d0de0446c83b9da} + summary: "Solves at 0.375, 0.75 and 1.5 panels per gap and a residual network (3 seeds; the variance head is not served)." + paper: + tables: [tab:ladder-test, tab:routes (c), S-tab:multifid] + val: {median_pct: 0.451, mae_ff: 0.1157, rmse_ff: 0.1445, note: "validation on the uncorrected prior"} + test_infamily: {median_pct: 0.275, mae_ff: 0.0307, rmse_ff: 0.0375} + test_zeroshot: {median_pct: 0.914, mae_ff: 1.711, rmse_ff: 1.795} + time: "0.575 / 2.63 s in family, 1.16 / 10.7 s zero-shot (test)" + cheap-2-exponent: + title: "Cheap-physics network, 2 levels, learned exponent" + paper_name: "2 levels, learned exponent" + route: "(c) cheap physics" + kind: multifid + stack: corrected + levels: [0.375, 0.75] + checkpoints: + - {path: data_local/multifid/runs/L2x_s0/model.pt, sha256: 48d83cdf96614f4bc3b2c9ae6d2765c1c985222ab7689e9d848737bc6200fe88} + - {path: data_local/multifid/runs/L2x_s1/model.pt, sha256: 718d4387fc42e79ef1c318c482496fa112b3985adf5be7546976dbf09cec6f5a} + - {path: data_local/multifid/runs/L2x_s2/model.pt, sha256: df0fe917bb6493ebdf41652006023375e75452a7bb18dff56409ca9b5bbc2319} + summary: "The 2-level network predicting a per-entry convergence exponent of the extrapolation instead of the fixed Richardson step." + paper: + tables: [S-tab:multifid] + val: {median_pct: 0.698, mae_ff: 0.2312, rmse_ff: 0.2683, note: "validation on the uncorrected prior; not test-scored"} + cheap-3-exponent: + title: "Cheap-physics network, 3 levels, learned exponent" + paper_name: "3 levels, learned exponent" + route: "(c) cheap physics" + kind: multifid + stack: corrected + levels: [0.375, 0.75, 1.5] + checkpoints: + - {path: data_local/multifid/runs/L3x_s0/model.pt, sha256: 9fa1d9814e7823eff1953337db9ee9a2752caf722fdfee817e28308056c705c9} + - {path: data_local/multifid/runs/L3x_s1/model.pt, sha256: 6a2485022c29af6494c69f344f7e49acf8cc422bd63d1e5d19de611a90aba6a1} + - {path: data_local/multifid/runs/L3x_s2/model.pt, sha256: d88cfaf5e988d85ed5e4408321c676eaf8bd39c8a50c4bcfa6240c3aebf34c4d} + summary: "The 3-level network predicting a per-entry convergence exponent." + paper: + tables: [S-tab:multifid] + val: {median_pct: 0.456, mae_ff: 0.1286, rmse_ff: 0.1573, note: "validation on the uncorrected prior; not test-scored"} + coarser-pair: + title: "The model's recipe on the coarser pair R(0.75, 1.5), retrained" + paper_name: "coarser pair + the model's recipe" + route: "(c) cheap physics" + kind: gnn + arm: full_gnn + stack: corrected + ppg: [0.75, 1.5] + checkpoints: + - {path: data_local/fastprior_p2/runs/p2_s0/model.pt, sha256: d9c01b20823a1c0b473949ad2b3f0977fc8c9f288b1d7737fb9e42f0ef2ee4ab} + - {path: data_local/fastprior_p2/runs/p2_s1/model.pt, sha256: 6dec390290f9d94af700bfd5e4e7e907a56c413d303d777b13eb9d2ce73e650c} + - {path: data_local/fastprior_p2/runs/p2_s2/model.pt, sha256: 0d43b0b5388e06fd49bf5c1b350e0aaa2a8a627d7b5d95972c0062affb1a24b0} + summary: "The model's networks retrained from scratch on R(0.75, 1.5) of the corrected prior: half the prior time; a trade-off point, never test-scored." + paper: + tables: [tab:routes (c), S-fig:ladder-val] + val: {median_pct: 0.343, mae_ff: 0.123, rmse_ff: 0.138, note: "against 0.229 % (0.067 / 0.079 fF) for the model; not test-scored (the physics pair alone: 1.508 % in family, 1.660 % zero-shot)"} + time: "prior 0.430 / 2.29 s against 0.809 / 6.61 s for the model's pair (x86, 2 CPUs, validation)" + # ---------------- (b) learned physics, (a) no physics + fast-model: + title: "The fast model: learned prior surrogate + the model's networks (families of its training data only)" + paper_name: "the fast model (learned prior surrogate + networks)" + route: "(b) learned physics" + kind: gnn + arm: full_gnn + stack: null + checkpoints: *ck_full_gnn + learned_prior: + name: surrogate_t100_norm + checkpoint: {path: data_local/learnedprior/runs/t100_norm/model.pt, sha256: 622fc374cc989bcc5569373787996169b8cde1254efdd118baa7252da0d788ce} + summary: "A learned surrogate (1.02M parameters, 385,593 generated layouts labelled by the prior alone + every family's training geometry) predicts the two priors; the model's networks correct them. No field solve." + warning: "Use the fast model only for the 13 component families of its training data (interdigital, paddle, concentric, star, multipad star, Xmon, TransmonPocket, shielded concentric, two-net, random, procedurally generated, synthetic campaign layouts). On a family held out of it, it is behind the physics it distils: 3.113 % (MAE 0.533 / RMSE 0.633 fF) against 1.554 % (0.166 / 0.205 fF). Use the model for a new component type." + paper: + tables: [tab:ladder-test, tab:routes (b), S-tab:sf-fast] + val: {median_pct: 0.846, mae_ff: 0.491, rmse_ff: 0.549} + test_infamily: {median_pct: 0.630, mae_ff: 0.1130, rmse_ff: 0.1414} + test_zeroshot: {median_pct: 3.576, mae_ff: 4.608, rmse_ff: 5.058, note: "families of its surrogate's training data"} + heldout_family: {median_pct: 3.113, mae_ff: 0.533, rmse_ff: 0.633} + time: "0.092 / 0.26 s in family, 0.30 / 2.47 s zero-shot (test); 0.122 / 0.471 s (GH200)" + solver-free: + title: "Solver-free network: the model's network with no prior, geometry only" + paper_name: "solver-free network" + route: "(a) no physics" + kind: gnn + arm: direct_gnn + stack: null + checkpoints: + - {path: data_local/geomfast/runs/p2d_direct_gnn_s0/model.pt, sha256: c3c0b8850d74984ebd6d1d42b63d9f8124bf9029627dda60e7aa0a1bdd9065c9} + - {path: data_local/geomfast/runs/p2d_direct_gnn_s1/model.pt, sha256: 3cc2e71de05e29a18212ad467c664d3219ff853c7522d45c1582b7af4185db25} + - {path: data_local/geomfast/runs/p2d_direct_gnn_s2/model.pt, sha256: 57bc28024fc6c5f0c5db424e313474cd64177b13fb9e3bc3d2044b1a01b8f691} + summary: "The model's network reading the geometric embedding only: base = training-median log mutual per pair kind, unbounded head." + warning: "No field solve: it interpolates in family and loses an order of magnitude outside it (zero-shot macro 17.29 %, MAE 8.934 / RMSE 9.972 fF). Use the model for any number you rely on." + paper: + tables: [tab:ladder-test, tab:routes (a), S-tab:multifid] + val: {median_pct: 1.486, mae_ff: 0.630, rmse_ff: 0.728} + test_infamily: {median_pct: 1.219, mae_ff: 0.1872, rmse_ff: 0.2290} + test_zeroshot: {median_pct: 17.29, mae_ff: 8.934, rmse_ff: 9.972} + time: "0.089 / 0.27 s in family, 0.30 / 2.48 s zero-shot (test); 0.124 s (GH200)" + +# ---------------------------------------------------------------- flagship Space (v4; user decisions 2026-10-19/20; deploy/GNN_SPACE.md "Flagship Space") +# Built by `python deploy/sync.py --flagship` (dry run) / `--flagship --push`; gated by deploy/tests/test_flagship_space.py. Three tiers, one +# selector, each the best recipe per speed level RETRAINED ON ALL ON-STACK DATA (45,771 Q3D layouts: TRAIN + VAL + every read test layout +# passing label QC; agents/flagship_v4/{PROTOCOL,RESULTS}.md). These models therefore have NO held-out score of their own: `reference` = the +# paper's held-out numbers for the same recipe (paper_v4; trained on 35,354 TRAIN layouts), `b5` = the one fresh check (campaign450 B5, 39 +# layouts of one generated family, N 3-4, never seen by any component; RESULTS.md section 5). Numbers: layout-mean median relative error % +# (MAE / RMSE fF) over mutual entries, 0.1 fF floor. Code path = the paper Space's (geomgnn.paper) through geomgnn.flagship.view. +flagship_hub: + space: shanto268/PlanarCapacitanceModel # the URL is kept; its earlier v3 tiers are archived (archive below) + title: Planar Capacitance Model + private: true # Spaces stay private (CLAUDE.md rule 11) + space_hardware: cpu-basic # free tier: 2 vCPU, 16 GB RAM + default_arm: accurate + max_conductors: 70 # signal conductors per layout (the nine-qubit lattice has 66 + ground) + paper_space: shanto268/2026PaperModels # every route of the paper, for reproducing it + checkpoints: data_local/flagship_v4/checkpoints.json # sha256 of every flagship checkpoint; sync.py refuses any other file + results: agents/flagship_v4/RESULTS.md + training_layouts: 45771 + archive: + space: shanto268/PlanarCapacitanceModel + commit: 906bc45ae05ce9a1dac796a22c9aef481f079d40 # last commit of the earlier draft's v3 tiers (fast / coarse / fine, 2026-10-10); restore = push this revision + local_copy: data_local/hf_archive/PlanarCapacitanceModel_906bc45a # snapshot_download of that revision (79 files, 27 MB) + note: "the earlier draft's model repos (SQuADDS/2026PaperModels-*) are untouched; the old builder (sync.py without flags) skips this Space" + prior_stacks: + corrected: *stack_corrected # prior v2m + compiled exact solver (the same stack as the paper Space's model) + +flagship_tiers: + accurate: + title: "Accurate (~1 s): extrapolated physics R(1.5, 3) on the corrected prior + residual networks, compiled solver" + paper_name: "Accurate" + route: "Accurate" + kind: gnn + arm: full_gnn + stack: corrected + ppg: [1.5, 3.0] + checkpoints: + - {name: acc_v2m_s0, path: data_local/flagship_v4/hpc/runs/acc_v2m_s0/model.pt, sha256: 56109a61bb264151ca7a650cd79a516d60dcf6c92f257ded38ff9ad97a1421a2} + - {name: acc_v2m_s1, path: data_local/flagship_v4/hpc/runs/acc_v2m_s1/model.pt, sha256: ae9c4be59caa84eb6a220a91e18fce696e98e3a9c3aab09e16ac727e7fc2d898} + - {name: acc_v2m_s2, path: data_local/flagship_v4/hpc/runs/acc_v2m_s2/model.pt, sha256: 0841acc6a3db32e2d5def72443445361c4ae41ac782b38b7856a78f0547b1e19} + summary: "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." + 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))" + val: {median_pct: 0.229, mae_ff: 0.0674, rmse_ff: 0.0792} + test_infamily: {median_pct: 0.177, mae_ff: 0.0203, rmse_ff: 0.0250} + test_zeroshot: {median_pct: 0.587, mae_ff: 1.092, rmse_ff: 1.152} + heldout_family: {median_pct: 0.408, mae_ff: 0.049, rmse_ff: 0.059} + b5: {median_pct: 1.034, mae_ff: 0.131, rmse_ff: 0.189} + b5_physics: {name: "physics alone C^R = R(1.5, 3)", median_pct: 1.765, mae_ff: 0.275, rmse_ff: 0.376} + time_x86: "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" + balanced: + title: "Balanced (~0.5 s): the same recipe on the coarser pair R(0.75, 1.5) of the corrected prior, networks retrained" + paper_name: "Balanced" + route: "Balanced" + kind: gnn + arm: full_gnn + stack: corrected + ppg: [0.75, 1.5] + checkpoints: + - {name: bal_v2m_s0, path: data_local/flagship_v4/hpc/runs/bal_v2m_s0/model.pt, sha256: 5016672366dead959a425890bfb23df0ea4fdab050aaf03e1e428ac2597bfe0c} + - {name: bal_v2m_s1, path: data_local/flagship_v4/hpc/runs/bal_v2m_s1/model.pt, sha256: 5d1f0ffbac76bc2742ef26adfcbe44be99dc795ba92fd2b0439c541dcfbbb14e} + - {name: bal_v2m_s2, path: data_local/flagship_v4/hpc/runs/bal_v2m_s2/model.pt, sha256: dcd14c610e251b23d77b34a114e5a2db33fade289306270057151f2750890d35} + summary: "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." + reference: + recipe: "the paper's coarser pair + the model's recipe (trained on 35,354 TRAIN layouts; validation only, never test-scored)" + val: {median_pct: 0.343, mae_ff: 0.123, rmse_ff: 0.138} + b5: {median_pct: 1.753, mae_ff: 0.236, rmse_ff: 0.308} + b5_physics: {name: "physics alone R(0.75, 1.5)", median_pct: 2.681, mae_ff: 0.396, rmse_ff: 0.583} + time_x86: "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" + fast: + title: "Fast (~0.1 s): learned prior surrogate + residual networks, no field solve (trained component families only)" + paper_name: "Fast" + route: "Fast" + kind: gnn + arm: full_gnn + stack: null + checkpoints: + - {name: acc_v1_s0, path: data_local/flagship_v4/hpc/runs/acc_v1_s0/model.pt, sha256: 9dff20ade26619e7713c2d145dada65e4554a623780290673cfe9452cf9b64ba} + - {name: acc_v1_s1, path: data_local/flagship_v4/hpc/runs/acc_v1_s1/model.pt, sha256: f1ad2ca065dd650ccabcad73a3c6ad2ac1042f4f68bb828893bbdecc47411ffe} + - {name: acc_v1_s2, path: data_local/flagship_v4/hpc/runs/acc_v1_s2/model.pt, sha256: 8cbada211724df5cc05a7b4fdd232607de047b248a43993d3844bd68ff8060c4} + learned_prior: + name: sall_all_s0 # surrogate seed 0 by declaration (PROTOCOL.md section 3, amendment A1) + checkpoint: {name: sall_all_s0, path: data_local/flagship_v4/hpc/sur/sall_all_s0/model.pt, sha256: 132d6f389eda798d31bd5654a96e24cce18fa750134a9fb2a9725eafed339dac} + summary: "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." + warning: "Use Fast only for quick screening of the component families it was trained on (interdigital, paddle, concentric, star, multipad star, Xmon, TransmonPocket, shielded concentric, two-net, random, procedurally generated and synthetic campaign layouts). On new designs of a trained family (B5) it was worse than the physics alone, 3.175 % (MAE 0.583 / RMSE 0.824 fF) against 1.765 % (0.275 / 0.376 fF), and in the paper it was worse than the physics alone on a held-out family too, 3.11 % (0.533 / 0.633 fF) against 1.55 % (0.166 / 0.205 fF). Use Accurate for anything that matters." + reference: + recipe: "the paper's fast model (learned prior surrogate + the networks on its prior, trained on 35,354 TRAIN layouts)" + val: {median_pct: 0.846, mae_ff: 0.491, rmse_ff: 0.549} + test_infamily: {median_pct: 0.630, mae_ff: 0.1130, rmse_ff: 0.1414} + test_zeroshot: {median_pct: 3.576, mae_ff: 4.608, rmse_ff: 5.058} + heldout_family: {median_pct: 3.113, mae_ff: 0.533, rmse_ff: 0.633} + heldout_family_physics: {median_pct: 1.554, mae_ff: 0.166, rmse_ff: 0.205} + val_same_surrogate: {median_pct: 0.722, mae_ff: 0.387, rmse_ff: 0.436, note: "this surrogate recipe (SUR-ALL) + the networks, validation"} + b5: {median_pct: 3.175, mae_ff: 0.583, rmse_ff: 0.824} + b5_physics: {name: "physics alone C^R = R(1.5, 3) (worse than the physics: Fast is behind it here)", median_pct: 1.765, mae_ff: 0.275, rmse_ff: 0.376} + time_x86: "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" diff --git a/requirements.txt b/requirements.txt index 476bf9391a90a8334f9e31a493c1eb2be852c817..d266007bb511c3554212db580ce41ab525f124a5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,6 @@ pandas pyyaml matplotlib huggingface_hub +numba==0.68.0 +llvmlite==0.50.0 +threadpoolctl diff --git a/tlsq_infer/__init__.py b/tlsq_infer/__init__.py index 70526f531d09ef66bd76550d77c803db12af6bb5..179a3e3e0e8c6f96934bab0f466e5e1599ae09a9 100644 --- a/tlsq_infer/__init__.py +++ b/tlsq_infer/__init__.py @@ -2,10 +2,13 @@ from tlsq_infer import load_gds, prepare, predict print(load_gds("chip.gds")) # layers / datatypes present - lay = prepare("chip.gds") # ground (1, 0), conductors (1, 10), cropped window + lay = prepare("chip.gds") # ground (1, 0), conductors (1, 10), full ground (training geometry) res = predict(lay, model="pair-local", substrate="sapphire", thickness_um=430) res["names"], res["matrix_fF"], res["expected_error"] + from tlsq_infer.final import predict_final + res = predict_final(lay, "E3") # final recipes of the paper: "E3" (default), "E1", "C0", "E3-D" + Threads: tlsq_infer.set_num_threads(n) (or the environment variable TLSQ_NUM_THREADS at import) sets torch, the BLAS pools that threadpoolctl can reach (OpenBLAS / MKL; Apple Accelerate ignores it) and the mesh / assembly pool of the MoM prior; the default is torch's thread count. @@ -53,5 +56,10 @@ def set_num_threads(n: int | None = None) -> int: if os.environ.get("TLSQ_NUM_THREADS"): set_num_threads() +if os.environ.get("TLSQ_REJTAIL_FV", "1") != "0": # value-preserving rejection-tail fix of the windowed prior (_vendor/rejtail_fv.py) + from ._vendor import rejtail_fv as _rejtail_fv + + _rejtail_fv.install() + __all__ = ["LayoutError", "PreparedLayout", "PriorError", "available_cpus", "load_gds", "load_registry", "matrix_csv", "predict", "prepare", "set_num_threads", "__version__"] diff --git a/tlsq_infer/__pycache__/__init__.cpython-312.pyc b/tlsq_infer/__pycache__/__init__.cpython-312.pyc deleted file mode 100644 index d5a7b5115a03da7efc3abbdef4c7e3b8ec03a835..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/__init__.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/__pycache__/charge.cpython-312.pyc b/tlsq_infer/__pycache__/charge.cpython-312.pyc deleted file mode 100644 index 32a9864e556bd2c56d9a879310ba4e56e7f83385..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/charge.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/__pycache__/errmodel.cpython-312.pyc b/tlsq_infer/__pycache__/errmodel.cpython-312.pyc deleted file mode 100644 index 440c4deca93136e7cdf4950e76cc96ee60f3c352..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/errmodel.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/__pycache__/layout.cpython-312.pyc b/tlsq_infer/__pycache__/layout.cpython-312.pyc deleted file mode 100644 index f1922b5814359f576d6aac0737a4063e3e140f7f..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/layout.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/__pycache__/predict.cpython-312.pyc b/tlsq_infer/__pycache__/predict.cpython-312.pyc deleted file mode 100644 index 7f8c7b3798dbc1ee7125838697c36b7ca7f86cda..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/predict.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/__pycache__/registry.cpython-312.pyc b/tlsq_infer/__pycache__/registry.cpython-312.pyc deleted file mode 100644 index 2732c7d193b579dd8d57618deb5af1f6362f91f1..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/registry.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/__pycache__/stack_error.cpython-312.pyc b/tlsq_infer/__pycache__/stack_error.cpython-312.pyc deleted file mode 100644 index 7e6d12c55d2b05defb51b86b9ad9279299117695..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/__pycache__/stack_error.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/__init__.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/__init__.cpython-312.pyc deleted file mode 100644 index c085ee85933750471f6b6eb2b1fc4f30fce78e30..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/__init__.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/blocks.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/blocks.cpython-312.pyc deleted file mode 100644 index 6df9e0f52fa44a9d0bbfb34b6ec61ff378409aad..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/blocks.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/geometry.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/geometry.cpython-312.pyc deleted file mode 100644 index f4815c755ba6029dd1ca9eb898960f8beadddd7e..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/geometry.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/gnn.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/gnn.cpython-312.pyc deleted file mode 100644 index ec335734ea8decf22a1b9753ceedb7763aa76266..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/gnn.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/graph.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/graph.cpython-312.pyc deleted file mode 100644 index 97aaedea7fdb01b95f46f1eec82b684bcdcf0d74..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/graph.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/mom.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/mom.cpython-312.pyc deleted file mode 100644 index 3473d334833fb3fd49a4297a431d06ed5c5b1afb..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/mom.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/__pycache__/scene.cpython-312.pyc b/tlsq_infer/_vendor/__pycache__/scene.cpython-312.pyc deleted file mode 100644 index 5d9830d0870de2155538154c609daa1915d7fd3c..0000000000000000000000000000000000000000 Binary files a/tlsq_infer/_vendor/__pycache__/scene.cpython-312.pyc and /dev/null differ diff --git a/tlsq_infer/_vendor/blocks.py b/tlsq_infer/_vendor/blocks.py index e11c1e474293d0180e583dfead94b8e25fb6a193..cd82542abec9ebbee01c46bcd926c23355126ae7 100644 --- a/tlsq_infer/_vendor/blocks.py +++ b/tlsq_infer/_vendor/blocks.py @@ -53,6 +53,39 @@ def _kratio(m): return ellipk(1 - m) / ellipk(m) +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)} +TOKEN_VARIANT: dict = {"name": "exact"} # PRIOR_SCHEME.md 14.3; "exact" = the published tokens + + +def set_token_variant(name: str) -> None: + from .geometry import MIC_TOL + + assert name in TOKEN_VARIANTS, name + TOKEN_VARIANT["name"] = name + MIC_TOL[0] = 0.01 if name == "exact" else 0.1 + + +def _block_distances(pts: np.ndarray, tree) -> tuple: + """Distances from the samples of a to b (k-d tree) for the active token variant, with weights (multiples of the + sample step): the samples are grouped into consecutive blocks of k; a block whose first sample is farther than d_far + from b is represented by that sample with weight = block size, the other blocks sample by sample. crop: blocks whose + first sample is farther than the crop distance are dropped (their representatives kept if every block is dropped).""" + v = TOKEN_VARIANTS[TOKEN_VARIANT["name"]] + k = v.get("k", 64) + r = v.get("crop", v.get("d_far")) + rep = np.arange(0, len(pts), k) + d_rep, _ = tree.query(pts[rep], workers=_nthreads()) + size = np.diff(np.r_[rep, len(pts)]) + far = d_rep > r + near_pts = np.repeat(~far, size) + d_near, _ = tree.query(pts[near_pts], workers=_nthreads()) + if "crop" in v: + if not near_pts.any(): + return d_rep, size.astype(float) + return d_near, np.ones(len(d_near)) + return np.r_[d_rep[far], d_near], np.r_[size[far].astype(float), np.ones(len(d_near))] + + def edge_features(scene: Scene, i: int, j: int, bpts: dict, widths: dict, trees: dict | None = None) -> dict: """Symmetric pair features: separation spectrum (both directions averaged), minimum gap, coupling moments and the coplanar-strip conformal-map coupling (Gevorgian modulus; the ground net is treated as infinitely wide). trees: k-d @@ -63,6 +96,20 @@ def edge_features(scene: Scene, i: int, j: int, bpts: dict, widths: dict, trees: cps = 0.0 dmin = np.inf for a, b in ((i, j), (j, i)): + if TOKEN_VARIANT["name"] != "exact": # approximate tokens (PRIOR_SCHEME.md 14.3) + d, wq = _block_distances(bpts[a], trees[b] if trees is not None else cKDTree(bpts[b])) + dmin = min(dmin, d.min()) + hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=STEP_UM * wq)[0] + dd = np.maximum(d, 0.05) + mom += 0.5 * np.array([np.sum(STEP_UM * wq * dd ** (-p)) for p in MOMENTS]) + w1, w2 = widths[a], widths[b] + if np.isinf(w1) or np.isinf(w2): + wf = w2 if np.isinf(w1) else w1 + mm = dd / (dd + wf) + else: + mm = dd * (dd + w1 + w2) / ((dd + w1) * (dd + w2)) + cps += 0.5 * np.sum(STEP_UM * wq * _kratio(mm)) + continue d, _ = (trees[b] if trees is not None else cKDTree(bpts[b])).query(bpts[a], workers=_nthreads()) dmin = min(dmin, d.min()) hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=np.full(len(d), STEP_UM))[0] diff --git a/tlsq_infer/_vendor/em.py b/tlsq_infer/_vendor/em.py new file mode 100644 index 0000000000000000000000000000000000000000..d980e90c960d07ae9d8ab1f7e714ce0dab28d81b --- /dev/null +++ b/tlsq_infer/_vendor/em.py @@ -0,0 +1,120 @@ +"""Vendored logic of the final recipes (paper_v3: C0, E1, E3), copied from the training / scoring code without changes to +the arithmetic: scripts/e3_corr.py (KIND_ID, EDGE_KEYS, BOUND, N_PAD, _kind, _pack, make_model), scripts/em_matrix.py (item, +pack, apply_scale, predict), scripts/tm_final.py (logR) and scripts/metrics.py (entry_kind, KIND_XSET). The SceneGNN is the +vendored tlsq.uge.gnn class (same source). Parity: deploy/tests/test_final_parity.py.""" + +from __future__ import annotations + +import numpy as np + +KIND_ID = {"signal-signal": 1, "signal-ground": 2, "pad-ground": 3, "pad-other": 4} +EDGE_KEYS = (["is_ground_pair", "log_dmin_g"] + [f"sep_{k:02d}" for k in range(10)] + ["mom_0.5", "mom_1", "mom_2", "log_cps", + "share_mean", "share_absdiff"] + [f"lt_{k}{r}" for r in (50, 150, 400) for k in ("g", "o", "e")] + + ["lg_log_dmin_g"] + [f"lg_sep_{k:02d}" for k in range(10)] + ["lg_mom_0.5", "lg_mom_1", "lg_mom_2", "lg_log_cps", "mesh_step"]) +BOUND = 0.03 +N_PAD = 8 +KIND_XSET = {"signal-signal": "conductor-conductor", "pad-other": "conductor-conductor", "signal-ground": "conductor-ground", + "pad-ground": "conductor-ground"} + + +def entry_kind(a: str, b: str) -> str: + """Kind of the mutual entry between conductors named a and b ('P*' = pad, 'G' = ground) (metrics.entry_kind).""" + pad, gnd = a.startswith("P") or b.startswith("P"), "G" in (a, b) + return ("pad-ground" if gnd else "pad-other") if pad else ("signal-ground" if gnd else "signal-signal") + + +_kind = entry_kind # e3_corr._kind: the same rule + + +def logR(rlo, rhi): + """(log R, log MoM hi) of the mutual magnitudes: R = 2 log MoM(hi) - log MoM(lo) (tm_final.logR).""" + ml, mh = -np.asarray(rlo["proxy_matrix"], float), -np.asarray(rhi["proxy_matrix"], float) + with np.errstate(divide="ignore", invalid="ignore"): + return 2 * np.log(np.where(mh > 0, mh, np.nan)) - np.log(np.where(ml > 0, ml, np.nan)), np.log(np.where(mh > 0, mh, np.nan)) + + +def item(rlo, rhi, target=None): + """em_matrix.item (non-light): names, the two prior magnitudes, roles, pair tokens and the mesh gap of the finer row.""" + n = len(rhi["names"]) + tgt = np.ones((n, n)) if target is None else target + return {"names": list(rhi["names"]), "mlo": -np.asarray(rlo["proxy_matrix"], float), "mhi": -np.asarray(rhi["proxy_matrix"], float), + "target": -np.asarray(tgt, float), "roles": [nd["role_ground"] for nd in rhi["nodes"]], "edges": rhi["edges"], + "gap": rhi["global"]["proxy_min_gap_um"]} + + +def _pack(items, N): + """items: dicts with names, roles, edges {(i,j): token dict}, mlo, mhi (n x n magnitudes), target (n x n or None).""" + B, de = len(items), len(EDGE_KEYS) + X = np.zeros((B, N, 1), np.float32) + E = np.zeros((B, N, N, de), np.float32) + M = np.zeros((B, N), np.float32) + PR = np.zeros((B, N, N), np.float32) + Y = np.full((B, N, N), np.nan, np.float32) + K = np.zeros((B, N, N), np.int8) + A = np.zeros((B, N, N), np.float32) + for b, it in enumerate(items): + nm, n = it["names"], len(it["names"]) + M[b, :n] = 1 + X[b, :n, 0] = it["roles"] + g = np.log(max(it["gap"], 1e-6)) + for (i, j), f in it["edges"].items(): + v = [f["log_dmin"] - g if k == "log_dmin_g" else f["lg_log_dmin"] - g if k == "lg_log_dmin_g" else + np.log(it["mhi"][i, j]) - np.log(it["mlo"][i, j]) if k == "mesh_step" else f[k] for k in EDGE_KEYS] + E[b, i, j] = E[b, j, i] = np.nan_to_num(np.asarray(v, float)) + A[b, i, j] = A[b, j, i] = 1 + for i in range(n): + for j in range(n): + if i == j: + continue + PR[b, i, j] = 2 * np.log(it["mhi"][i, j]) - np.log(it["mlo"][i, j]) + K[b, i, j] = KIND_ID[_kind(nm[i], nm[j])] + if it["target"] is not None and it["target"][i, j] > 0: + Y[b, i, j] = np.log(it["target"][i, j]) + return X, E, M, PR, Y, K, A + + +def pack(items): + N = max(N_PAD, max(len(it["names"]) for it in items)) + X, E, M, PR, Y, K, A = _pack(items, N) + L = np.zeros((len(items), N, N), np.float32) + for b, it in enumerate(items): + n = len(it["names"]) + with np.errstate(divide="ignore", invalid="ignore"): + L[b, :n, :n] = np.log(np.where(it["mhi"] > 0, it["mhi"], np.nan)) + return dict(X=X, E=E, M=M, PR=PR, Y=Y, K=K, A=A, LMH=L) + + +def apply_scale(E, A, s): + return (np.clip((E - s["med"]) / s["iqr"], (s["lo"] - s["med"]) / s["iqr"], (s["hi"] - s["med"]) / s["iqr"]) * (A[..., None] > 0)).astype(np.float32) + + +def make_model(de): + """Bounded (+-BOUND) local correction on the base log B = log R + c_kind (e3_corr.make_model).""" + import torch.nn as nn + + from .gnn import SceneGNN + + class Bounded(nn.Module): + def __init__(self): + super().__init__() + self.net = SceneGNN(1, de, 1, 128, rounds=3) + + def forward(self, X, E, U, M, P, A=None): + import torch + raw = self.net(X, E, U, M, torch.zeros_like(P), A) # = edge head (off-diagonal) + node head (diagonal) + eye = torch.eye(M.shape[1], device=M.device)[None] + return P + BOUND * torch.tanh(raw) * (1 - eye) + + return Bounded() + + +def predict(model, d, idx, cvec, pre=""): + import torch + T = lambda x: torch.tensor(x, dtype=torch.float32) # noqa: E731 + P = d[pre + "PR"] + cvec[d[pre + "K"].astype(int)] * (d[pre + "K"] > 0) + out = [] + with torch.no_grad(): + for s0 in range(0, len(idx), 1024): + j = idx[s0:s0 + 1024] + out.append(model(T(d[pre + "X"][j]), T(d[pre + "E"][j]), torch.zeros((len(j), 1)), T(d[pre + "M"][j]), T(P[j]), T(d[pre + "A"][j])).numpy()) + return np.concatenate(out) diff --git a/tlsq_infer/_vendor/fastprior.py b/tlsq_infer/_vendor/fastprior.py index e56cc48b73acf956c221dcfafc06f6a0cc8c8f48..9dadd84fcbaa92659d0b6c98b4f87309220313fd 100644 --- a/tlsq_infer/_vendor/fastprior.py +++ b/tlsq_infer/_vendor/fastprior.py @@ -157,6 +157,8 @@ FORMULA_SCREEN_UM = 1.0 # the screening factor is evaluated every FORMULA_SCREE LITE_REFINE = 3 # rejected window solves: refine h by 2 up to this many times before majority-owned cells 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 LITE_WINDOW_BUDGET = 2.0 # windows of large layouts: panel budget = this x the budget of the one-window (component) prior +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) +LITE_MAX_CORE = [None] # windows of large layouts: conductors per core (None = the max_core argument; PRIOR_SCHEME.md 14.4.2) LITE_GROWTH = 2.0 # merged panels per halving of h (measured 1.6-2.2 on the training families) 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) DIPOLE_K = 8 # nearest boundary samples searched for the nearest other conductor (gbar) @@ -292,6 +294,7 @@ def _window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: flo cols = np.arange(len(metals)) if all_rhs else np.arange(1, 1 + n_core) t_mesh = t_asm = t_sol = 0.0 hh, cap, refined, majority = h, budget, 0, False + R = None while True: t = time.perf_counter() if not refined: @@ -319,7 +322,7 @@ def _window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: flo off[np.arange(len(cols)), cols] = False ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and (not all_rhs or np.all(R[off] < 0))) except np.linalg.LinAlgError: - ok = False + ok, R = False, None t_sol += time.perf_counter() - t if ok or majority: break @@ -327,6 +330,9 @@ def _window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: flo hh, cap, refined = hh / 2, np.inf, refined + 1 else: majority = True + if R is None: + raise RuntimeError(f"window solve failed: no positive-definite system after {refined} refinements (h={hh:.4g} um); " + "the window has no valid solution at this mesh") return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, "assemble_s": t_asm, "solve_s": t_sol} @@ -361,7 +367,10 @@ def _lite_job(k: int) -> tuple: t_build = time.perf_counter() - t bx = shapely.bounds(metals[0]) h = max(L["h"], max(bx[2] - bx[0], bx[3] - bx[1]) / 4096) - R, info = _window_solve(metals, len(L["cores"][k]), h, L["budget"], L["eps_r"], False, L["levels"]) + budget = L["budget"] + if LITE_WINDOW_PPG[0]: # resolution-controlled windows (PRIOR_SCHEME.md 14.4): finest panel gap / ppg, no budget + h, budget = L["gap"] / LITE_WINDOW_PPG[0], np.inf + R, info = _window_solve(metals, len(L["cores"][k]), h, budget, L["eps_r"], False, L["levels"]) info.update(build_s=t_build, n_halo=len(halo), n_core=len(L["cores"][k])) return k, halo, R, info @@ -414,12 +423,12 @@ def micro_mom(G, conds: list, eps_r: float = 11.45, budget: float = 1500, cells_ gap = gap_of(metals) R_g, margin = halo_gaps * gap, max(margin_gaps * gap, margin_min_um) b = shapely.bounds(np.array(conds, dtype=object)) - cores = plan_cores(np.stack([(b[:, 0] + b[:, 2]) / 2, (b[:, 1] + b[:, 3]) / 2], 1), max_core) + cores = plan_cores(np.stack([(b[:, 0] + b[:, 2]) / 2, (b[:, 1] + b[:, 3]) / 2], 1), LITE_MAX_CORE[0] or max_core) tree = shapely.STRtree(conds) etched = shapely.box(*G.bounds).difference(G) _LITE.clear() _LITE.update(G=G, conds=conds, cores=cores, R_g=R_g, margin=margin, fill_um=fill_um, tree=tree, etched=etched, - h=gap / cells_per_gap, budget=LITE_WINDOW_BUDGET * budget if window_budget is None else window_budget, eps_r=eps_r, + h=gap / cells_per_gap, gap=gap, budget=LITE_WINDOW_BUDGET * budget if window_budget is None else window_budget, eps_r=eps_r, levels=tuple(float(x) * gap for x in level_gaps)) _LITE_BUDGET_USED[:] = [_LITE["budget"]] t_plan = time.perf_counter() - t diff --git a/tlsq_infer/_vendor/fastprior_loc.py b/tlsq_infer/_vendor/fastprior_loc.py new file mode 100644 index 0000000000000000000000000000000000000000..72246beafb3078f9cb9f6d6f3d56a83372b3ea42 --- /dev/null +++ b/tlsq_infer/_vendor/fastprior_loc.py @@ -0,0 +1,199 @@ +"""Vendored from tlsq.embed.fastprior_loc (copied verbatim except this header and `from . import mom as P`; checked by +deploy/tests/test_fastprior_loc.py). PRIOR_SCHEME.md section 19 Amendment MESH: local-gap graded lite prior.""" +from __future__ import annotations + +import time + +import numpy as np +import shapely + +from . import fastprior as F +from . import mom as P + +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) +FLOOR_DIV = 4096 # h_floor = layout size / FLOOR_DIV (the existing floor) + + +def ref_gap(metals: list, sliver: float = SLIVER_UM) -> tuple: + """(G_ref, smallest gap, layout size, number of sliver pairs). G_ref = smallest pair gap >= sliver; when every pair + is a sliver, the edge-length-weighted 5th percentile of the local gap of the boundary (0.25 um samples).""" + gmin, size = F._gap_size(metals) + n = len(metals) + d = np.array([shapely.distance(metals[i], metals[j]) for i in range(n) for j in range(i + 1, n)]) + ok = d >= sliver + if ok.any(): + return float(d[ok].min()), float(gmin), float(size), int((~ok).sum()) + lg = [] + for i, g in enumerate(metals): + others = shapely.union_all([metals[k] for k in range(n) if k != i]) + for ln in getattr(g.boundary, "geoms", [g.boundary]): + pts = shapely.line_interpolate_point(ln, np.arange(0, ln.length, 0.25)) + lg.append(shapely.distance(pts, others)) + return float(max(np.percentile(np.concatenate(lg), 5), gmin)), float(gmin), float(size), int((~ok).sum()) + + +def _local_gap(metals: list, boxes, hit, inn, pcs, sel) -> np.ndarray: + """local_gap of the cells sel (indices): min over touched metals k and other metals j of distance(cell & k, j).""" + lg = np.full(len(sel), np.inf) + m = len(metals) + for k in range(m): + t = hit[k, sel] + if not t.any(): + continue + ix = sel[t] + geo = np.where(inn[k, ix], boxes[ix], pcs[k][ix]) + bad = np.array([g is None for g in geo]) + geo[bad] = boxes[ix][bad] + dk = np.full(len(ix), np.inf) + for j in range(m): + if j != k: + dk = np.minimum(dk, shapely.distance(geo, metals[j])) + lg[t] = np.minimum(lg[t], dk) + return lg + + +def anchored_cells_local(metals: list, H: float, h_floor: float, ppg: float, grade: float = 2.0, majority: bool = False): + """`proxy.anchored_cells` on the lattice of H (anchored to the signal bounding box) with the finest level of every + cell set by its local gap: a cell (cut or whole) is final once h <= clamp(local_gap / ppg, h_floor, H). Returns + centres (n, 2), sizes (n, 2), owner (n,).""" + edges = shapely.union_all([g.boundary for g in metals]) + sig = metals[1:] if len(metals) > 1 else metals + ax, ay = shapely.union_all(sig).bounds[:2] + X0, Y0, X1, Y1 = shapely.union_all(metals).bounds + span = max(X1 - X0, Y1 - Y0) + K = max(0, int(np.ceil(np.log2(max(span / 16, H) / H)))) + h0 = H * 2**K + ix0, iy0 = int(np.floor((X0 - ax) / h0)), int(np.floor((Y0 - ay) / h0)) + ix1, iy1 = int(np.ceil((X1 - ax) / h0)), int(np.ceil((Y1 - ay) / h0)) + gx, gy = np.meshgrid(ax + h0 * np.arange(ix0, ix1), ay + h0 * np.arange(iy0, iy1)) + xs, ys, h = gx.ravel(), gy.ravel(), np.full(gx.size, h0) + gq = P._Geoms(list(metals) + [edges]) + tst, inn = np.ones((len(metals), len(xs)), bool), np.zeros((len(metals), len(xs)), bool) + out_c, out_h, out_o = [], [], [] + while len(xs): + boxes = shapely.box(xs, ys, xs + h, ys + h) + cov, hit, inn, pcs = P._cover(gq, boxes, h, tst, inn) + keep = cov.sum(0) > 1e-3 + xs, ys, h, cov, boxes, hit, inn = xs[keep], ys[keep], h[keep], cov[:, keep], boxes[keep], hit[:, keep], inn[:, keep] + pcs = [p[keep] for p in pcs] + cx, cy = xs + h / 2, ys + h / 2 + full = cov.max(0) > 0.999 + cand = h <= H * 1.0001 + last = cand & (h <= h_floor * 1.0001) + need = np.nonzero(cand & ~last)[0] + if len(need): + tgt = np.clip(_local_gap(metals, boxes, hit, inn, pcs, need) / ppg, h_floor, H) + last[need] = h[need] <= tgt * 1.0001 + d = np.full(len(xs), np.inf) + far = full & ~last + d[far] = P._distance(gq, len(metals), boxes[far]) + leaf = full & (last | (h <= grade * d + 1e-12)) + out_c.append(np.stack([cx[leaf], cy[leaf]], 1)) + out_h.append(np.stack([h[leaf], h[leaf]], 1)) + out_o.append(cov[:, leaf].argmax(0)) + cut = last & ~leaf + if majority and cut.any(): + take = cut & (cov.max(0) >= 0.5) + out_c.append(np.stack([cx[take], cy[take]], 1)) + out_h.append(np.stack([h[take], h[take]], 1)) + out_o.append(cov[:, take].argmax(0)) + cut = cut & False + if cut.any(): + gs = gq.get() + for k, g in enumerate(gs[:len(metals)]): + shapely.prepare(g) + sel = np.nonzero(cut & (cov[k] > 1e-3))[0] + if not len(sel): + continue + pcs_k = pcs[k][sel] + bb = shapely.bounds(pcs_k) + cen = shapely.get_coordinates(shapely.centroid(pcs_k)) + inside = shapely.contains_xy(g, cen[:, 0], cen[:, 1]) + if not inside.all(): + cen[~inside] = shapely.get_coordinates(shapely.point_on_surface(pcs_k[~inside])) + out_c.append(cen) + out_h.append(np.maximum(np.stack([bb[:, 2] - bb[:, 0], bb[:, 3] - bb[:, 1]], 1), 1e-6)) + out_o.append(np.full(len(sel), k)) + gq.put(gs) + split = ~leaf & ~last + hs = h[split] / 2 + xs = np.concatenate([xs[split], xs[split] + hs, xs[split], xs[split] + hs]) + ys = np.concatenate([ys[split], ys[split], ys[split] + hs, ys[split] + hs]) + h = np.concatenate([hs] * 4) + tst, inn = np.tile((hit & ~inn)[:, split], 4), np.tile(inn[:, split], 4) + return np.concatenate(out_c), np.concatenate(out_h), np.concatenate(out_o) + + +def mesh_local(metals: list, H: float, h_floor: float, ppg: float, majority: bool = False) -> tuple: + """Strip-merged local mesh (strip length <= 16 x the panel's own size and <= the distance to the nearest vertex). + Returns (centres, sizes, owners, raw count).""" + raw = anchored_cells_local(metals, H, h_floor, ppg, majority=majority) + c, wh, o = F.strip_merge(*raw, metals, h_floor) + return c, wh, o, len(raw[0]) + + +def _solve(metals: list, H: float, h_floor: float, ppg: float, eps_r: float) -> tuple: + """`fastprior._window_solve(all_rhs=True)` on the local mesh: rejected solves (not PD, a metal without a panel, a + non-negative mutual entry) refine (H, h_floor) / 2 up to LITE_REFINE times, then majority-owned cut cells.""" + t_mesh = t_sol = 0.0 + cols = np.arange(len(metals)) + refined, majority = 0, False + R = None + while True: + t = time.perf_counter() + c, wh, o, n_raw = mesh_local(metals, H, h_floor, ppg, majority) + t_mesh += time.perf_counter() - t + t = time.perf_counter() + try: + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + Q = P._chol_solve(A, (o[:, None] == cols[None, :]).astype(float)) + del A + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r) + R = np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1) + off = ~np.eye(len(metals), dtype=bool) + ok = bool(np.all(np.bincount(o, minlength=len(metals)) > 0) and np.all(R[off] < 0)) + except np.linalg.LinAlgError: + ok, R = False, None + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < F.LITE_REFINE: + H, h_floor, refined = H / 2, h_floor / 2, refined + 1 + else: + majority = True + if R is None: + raise RuntimeError(f"window solve failed: no positive-definite system after {refined} refinements (H={H:.4g} um)") + return R, {"H_um": H, "h_floor_um": h_floor, "h_min_um": float(wh.min()), "panels": len(c), "raw": n_raw, + "refined": refined, "majority": majority, "mesh_s": t_mesh, "solve_s": t_sol} + + +def micro_mom_loc(G, conds: list, eps_r: float = 11.45, ppg: float = 3.0, sliver: float = SLIVER_UM, + floor_div: float = FLOOR_DIV, canonical: bool = True) -> tuple: + """Local-gap lite prior of a <= 8-conductor layout: (C (N+1, N+1) fF, far_mask (all False), info). Layouts without a + sliver pair: `fastprior.micro_mom(budget=inf, cells_per_gap=ppg)` (the existing liteppg prior, bit-identical). + Mean over the canonical frames of symmetric layouts, returned in the input order (as micro_mom).""" + t_all = time.perf_counter() + metals = [G] + list(conds) + gref, gmin, size, n_sl = ref_gap(metals, sliver) + if n_sl == 0 or len(conds) > 8: # no sliver (or windows: out of scope) -> the existing prior + C, far, info = F.micro_mom(G, conds, eps_r, budget=np.inf, cells_per_gap=ppg) + info.update(local=False, G_ref_um=float(info["min_gap_um"]), H_um=float(min(info["h_um"])), n_sliver_pairs=n_sl) + return C, far, info + runs = [] + frames = F.canonical_frames(G, conds) if canonical else [(G, list(conds), np.arange(len(conds)))] + for Gc, cc, order in frames: + R, wi = _solve([Gc] + list(cc), gref / ppg, size / floor_div, ppg, eps_r) + C = 0.5 * (R + R.T) + inv = np.r_[0, np.argsort(order) + 1] + runs.append((C[np.ix_(inv, inv)], wi)) + C = sum(r[0] for r in runs) / len(runs) + wi = runs[0][1] + info = {"local": True, "n_windows": 1, "n_frames": len(runs), "min_gap_um": gref, "G_ref_um": gref, "gmin_um": gmin, + "n_sliver_pairs": n_sl, "H_um": wi["H_um"], "h_floor_um": wi["h_floor_um"], "h_um": [wi["H_um"]], + "h_min_um": wi["h_min_um"], "panels": [wi["panels"]], "panels_per_gap": [gref / wi["H_um"]], + "majority": [wi["majority"]], "refined": [wi["refined"]], + "mesh_s": sum(r[1]["mesh_s"] for r in runs), "solve_s": sum(r[1]["solve_s"] for r in runs)} + info["total_s"] = time.perf_counter() - t_all + return C, np.zeros(C.shape, bool), info diff --git a/tlsq_infer/_vendor/geometry.py b/tlsq_infer/_vendor/geometry.py index 513e9ddc9a2430cd780b0edf7798adc032831a03..65444375772ceea17bfaf97cf8469add2627bf81 100644 --- a/tlsq_infer/_vendor/geometry.py +++ b/tlsq_infer/_vendor/geometry.py @@ -9,6 +9,7 @@ from shapely.geometry import Polygon BOUNDARY_STEP_UM = 0.25 N_FOURIER = 16 +MIC_TOL = [0.01] # inscribed-circle tolerance (um); 0.1 for the approximate token variants (PRIOR_SCHEME.md 14.3.1) def sample_boundary(geom, step: float = BOUNDARY_STEP_UM) -> np.ndarray: @@ -61,7 +62,7 @@ def shape_metrics(poly) -> dict: "I_major": evals[1], "I_minor": evals[0], "elongation": np.sqrt(evals[1] / max(evals[0], 1e-30)), - "inscribed_radius": shapely.maximum_inscribed_circle(poly, 0.01).length if isinstance(poly, Polygon) else np.nan, + "inscribed_radius": shapely.maximum_inscribed_circle(poly, MIC_TOL[0]).length if isinstance(poly, Polygon) else np.nan, "n_vertices": int(shapely.get_num_coordinates(poly)), "n_holes": int(sum(len(p.interiors) for p in getattr(poly, "geoms", [poly]))), } diff --git a/tlsq_infer/_vendor/gnn.py b/tlsq_infer/_vendor/gnn.py index e16656680ed9c9da019ff40353aa1d2fd69ba816..40341394b2e6457938af77c8fbde094e8f14dd68 100644 --- a/tlsq_infer/_vendor/gnn.py +++ b/tlsq_infer/_vendor/gnn.py @@ -9,6 +9,61 @@ import torch import torch.nn as nn N_MAX = 8 +CLIP_MODES = ("iqr20", "iqr3", "envelope") +FEATURE_CLIP = {"mode": None} # evaluation override of the per-checkpoint clip (GRID_SCALING.md amendment 4); None = checkpoint's + + +def set_feature_clip(mode) -> None: + """None (each checkpoint's own: 'iqr20' for checkpoints without a 'features' spec), 'iqr20' (+-20 IQR, the released + models), 'iqr3' (+-3 IQR) or 'envelope' (the training range q0.1-q99.9 of each feature).""" + assert mode is None or mode in CLIP_MODES, mode + FEATURE_CLIP["mode"] = mode + + +N_LOCAL_UM = 200.0 +DERIVED_SCALE = {"prior_log_ppg": (float(np.log(3.0)), float(np.log(2.0))), "n_local": (1.0, 2.0)} # fixed (median, scale): one +# training resolution would give a near-zero IQR (single-ppg rows: IQR 3e-4, z down to -16,666) + + +def derive_features(row: dict) -> dict: + """Size- and window-invariant tokens (GRID_SCALING.md amendment 6): global prior_log_ppg = log(smallest gap / prior + finest panel), the prior's resolution; edge n_local = signal conductors (other than the pair) within N_LOCAL_UM of a + signal end of the pair (pair tokens' log_dmin). Returns a shallow copy of the row with the extra keys.""" + n = len(row["names"]) + dm = np.full((n, n), np.inf) + for (i, j), f in row["edges"].items(): + dm[i, j] = dm[j, i] = np.exp(f["log_dmin"]) + sig = np.array([nd["role_ground"] == 0 for nd in row["nodes"]]) + g = dict(row["global"]) + h = g.get("proxy_h_min_um") + gap = float(np.min(dm[np.isfinite(dm)])) if np.isfinite(dm).any() else 1.0 + if h and not g.get("proxy_min_gap_um"): # review W1: no sampled-dmin fallback (it was the B3 bug) + raise ValueError("row without proxy_min_gap_um: rebuild or backfill it (fix_hmin.py) before deriving the descriptor") + gap = g.get("proxy_min_gap_um") or gap + g["prior_log_ppg"] = float(np.log(max(gap, 1e-6) / h)) if h else 0.0 + near = (dm <= N_LOCAL_UM) & sig[None, :] + edges = {} + for (i, j), f in row["edges"].items(): + m = np.zeros(n, bool) + for e in (i, j): + if sig[e]: + m |= near[e] + m[[i, j]] = False + edges[(i, j)] = {**f, "n_local": float(m.sum())} + return {**row, "global": g, "edges": edges} + + +def scale_features(A, s, part: str, scaling: dict): + """(A - median) / IQR, clipped per FEATURE_CLIP or the checkpoint's scaling['features']['clip'].""" + z = np.nan_to_num((A - s[0]) / s[1]) + feats = scaling.get("features") or {} + mode = FEATURE_CLIP["mode"] or feats.get("clip", "iqr20") + if mode == "iqr3": + return np.clip(z, -3.0, 3.0) + if mode == "envelope": + lo, hi = (np.nan_to_num((np.asarray(b, float) - s[0]) / s[1]) for b in feats["envelope"][part]) + return np.clip(z, lo, hi) + return np.clip(z, -20.0, 20.0) def _mlp(i, o, w): @@ -24,20 +79,31 @@ class GraphSet: P: np.ndarray # (B, N_MAX, N_MAX) proxy log|C| (off-diagonal: log -C_ij, diagonal: log C_ii) Y: np.ndarray # (B, N_MAX, N_MAX) target log|C| (NaN where absent) ground: np.ndarray # (B, N_MAX) 1 for the ground node + A: np.ndarray | None = None # (B, N_MAX, N_MAX) 1 where the pair is a graph edge; None = every pair (dense graphs) def subset(self, idx): - return GraphSet(*(a[idx] for a in (self.X, self.E, self.U, self.M, self.P, self.Y, self.ground))) + return GraphSet(*(a[idx] for a in (self.X, self.E, self.U, self.M, self.P, self.Y, self.ground)), + None if self.A is None else self.A[idx]) def tensors(self, dev="cpu"): return [torch.tensor(a, dtype=torch.float32, device=dev) for a in (self.X, self.E, self.U, self.M, self.P, self.Y, self.ground)] -def build_graphset(rows: list[dict], scaling: dict | None = None, proxy_key: str = "proxy_log_C") -> tuple[GraphSet, dict]: +def build_graphset(rows: list[dict], scaling: dict | None = None, proxy_key: str = "proxy_log_C", drop=(), + clip: str = "iqr20", derive: bool = False, local: bool = False) -> tuple[GraphSet, dict]: """rows: scene-pass records. scaling (median/IQR per feature) is computed from rows when not given and returned, so it - can be frozen and shipped. proxy_key selects the edge proxy ('proxy_log_C' = MoM, 'conf_log_C' = conformal, None = none).""" - nk = [k for k in rows[0]["nodes"][0] if not k.startswith("proxy_")] - ek = [k for k in next(iter(rows[0]["edges"].values())) if not (k.startswith("proxy_") or k.startswith("conf_"))] - gk = [k for k in rows[0]["global"] if not k.startswith("proxy_")] + can be frozen and shipped. proxy_key selects the edge proxy ('proxy_log_C' = MoM, 'conf_log_C' = conformal, None = none). + New scaling only: drop = feature names left out, clip = clip mode stored in scaling['features'] (with the training + envelope q0.1-q99.9 per feature). A given scaling fixes the features (its 'keys').""" + if derive or (scaling is not None and (scaling.get("features") or {}).get("derive")): + rows = [derive_features(r) for r in rows] + if scaling is not None: + nk, ek, gk = (list(k) for k in scaling["keys"]) + else: + nk = [k for k in rows[0]["nodes"][0] if not k.startswith("proxy_") and k not in drop] + ek = [k for k in next(iter(rows[0]["edges"].values())) if not (k.startswith("proxy_") or k.startswith("conf_")) and k not in drop + and (local or not k.startswith(("lt_", "lg_")))] # lt_*: local context tokens (localtok), only when asked for + gk = [k for k in rows[0]["global"] if not k.startswith("proxy_") and k not in drop] B = len(rows) N = max(N_MAX, max(len(r["names"]) for r in rows)) # pad to the largest layout; N_MAX is only a minimum X = np.zeros((B, N, len(nk))) @@ -47,6 +113,8 @@ def build_graphset(rows: list[dict], scaling: dict | None = None, proxy_key: str G = np.zeros((B, N)) P = np.zeros((B, N, N)) Y = np.full((B, N, N), np.nan) + sparse = any(len(r["edges"]) < len(r["names"]) * (len(r["names"]) - 1) // 2 for r in rows) + A = np.zeros((B, N, N)) if sparse else None for b, r in enumerate(rows): n = len(r["names"]) M[b, :n] = 1 @@ -56,6 +124,8 @@ def build_graphset(rows: list[dict], scaling: dict | None = None, proxy_key: str P[b, i, i] = nd.get("proxy_log_Cself", 0.0) if proxy_key == "proxy_log_C" else 0.0 for (i, j), f in r["edges"].items(): E[b, i, j] = E[b, j, i] = [f[k] for k in ek] + if A is not None: + A[b, i, j] = A[b, j, i] = 1.0 P[b, i, j] = P[b, j, i] = f.get(proxy_key, 0.0) if proxy_key else 0.0 if proxy_key == "conf_log_C": # solver-free self term: sum of the conformal mutuals for i in range(n): @@ -73,10 +143,24 @@ def build_graphset(rows: list[dict], scaling: dict | None = None, proxy_key: str med = np.median(v, 0) iqr = np.percentile(v, 75, 0) - np.percentile(v, 25, 0) return med, np.where(iqr > 1e-9, iqr, 1.0) + def envelope(V, mask): + v = V[mask] + return np.percentile(v, 0.1, 0), np.percentile(v, 99.9, 0) em = M[:, :, None] * M[:, None, :] * (1 - np.eye(N))[None] - scaling = {"node": stats(X, M > 0), "edge": stats(E, em > 0), "global": stats(U, np.ones(B, bool)), "keys": (nk, ek, gk)} - sc = lambda A, s: np.clip(np.nan_to_num((A - s[0]) / s[1]), -20, 20) # noqa: E731 - return GraphSet(sc(X, scaling["node"]) * M[..., None], sc(E, scaling["edge"]), sc(U, scaling["global"]), M, P, Y, G), scaling + if A is not None: + em = em * A + + def fixed(st, keys): + med, iqr = (np.array(x, float) for x in st) + for c, k in enumerate(keys): + if k in DERIVED_SCALE: + med[c], iqr[c] = DERIVED_SCALE[k] + return med, iqr + scaling = {"node": stats(X, M > 0), "edge": fixed(stats(E, em > 0), ek), "global": fixed(stats(U, np.ones(B, bool)), gk), "keys": (nk, ek, gk), + "features": {"drop": list(drop), "clip": clip, "derive": bool(derive), "local": bool(local), "envelope": {"node": envelope(X, M > 0), "edge": envelope(E, em > 0), + "global": envelope(U, np.ones(B, bool))}}} + sc = lambda V, p: scale_features(V, scaling[p], p, scaling) # noqa: E731 + return GraphSet(sc(X, "node") * M[..., None], sc(E, "edge"), sc(U, "global"), M, P, Y, G, A), scaling class SceneGNN(nn.Module): @@ -96,8 +180,10 @@ class SceneGNN(nn.Module): nn.init.zeros_(g[-1].weight) nn.init.constant_(g[-1].bias, 3.0) - def forward(self, X, E, U, M, P): + def forward(self, X, E, U, M, P, A=None): mm = M[:, :, None] * M[:, None, :] * (1 - torch.eye(M.shape[1], device=M.device))[None] + if A is not None: # sparse graphs: pairs without an edge carry no message (their raw features are absent, not 0) + mm = mm * A h, e, u = self.enc_n(X), self.enc_e(E), self.enc_u(U) for msg, upd in zip(self.msg, self.upd): hi, hj = h[:, :, None].expand_as(e), h[:, None, :].expand_as(e) @@ -121,8 +207,11 @@ class SceneGNN(nn.Module): @torch.no_grad() def predict(model, gs: GraphSet, idx, dev="cpu"): - X, E, U, M, P, Y, G = gs.subset(idx).tensors(dev) + sub = gs.subset(idx) + X, E, U, M, P, Y, G = sub.tensors(dev) + A = None if sub.A is None else torch.tensor(sub.A, dtype=torch.float32, device=dev) out = [] for s in range(0, len(X), 2048): - out.append(model(X[s:s + 2048], E[s:s + 2048], U[s:s + 2048], M[s:s + 2048], P[s:s + 2048]).cpu().numpy()) + out.append(model(X[s:s + 2048], E[s:s + 2048], U[s:s + 2048], M[s:s + 2048], P[s:s + 2048], + None if A is None else A[s:s + 2048]).cpu().numpy()) return np.concatenate(out) diff --git a/tlsq_infer/_vendor/graph.py b/tlsq_infer/_vendor/graph.py index 7494b92d371cb7df0a966efaf54d18682c9bd4bf..df2706ea22a30439aa78712f4b47737409f6304d 100644 --- a/tlsq_infer/_vendor/graph.py +++ b/tlsq_infer/_vendor/graph.py @@ -11,11 +11,14 @@ from .blocks import boundary_points, edge_features, global_features, node_featur from .scene import Scene -def prior_dict(C: np.ndarray, n_cells: int, h_min: float, edges) -> dict: - """Same output as tlsq.uge.proxies.CapacitanceMoM.compute for a given Maxwell matrix C.""" +def prior_dict(C: np.ndarray, n_cells: int, h_min: float, edges, min_gap: float | None = None) -> dict: + """Same output as tlsq.uge.proxies.CapacitanceMoM.compute for a given Maxwell matrix C (min_gap: the prior mesh's + smallest conductor gap, recorded for the lite priors; the resolution descriptor = log(min_gap / h_min)).""" n = C.shape[0] out = {"node": {i: {"log_Cself": float(np.log(max(C[i, i], 1e-9)))} for i in range(n)}, "edge": {}, "global": {"cells": float(n_cells), "h_min_um": float(h_min)}, "matrix": C} + if min_gap is not None: + out["global"]["min_gap_um"] = float(min_gap) for (i, j) in edges: out["edge"][(i, j)] = {"log_C": float(np.log(max(-C[i, j], 1e-9)))} return out diff --git a/tlsq_infer/_vendor/mom.py b/tlsq_infer/_vendor/mom.py index 9ea2d0b56b14a26710628033c3ec9350a96c48cc..4a757df7553b6c080afe98b0fde237d74f3bfab5 100644 --- a/tlsq_infer/_vendor/mom.py +++ b/tlsq_infer/_vendor/mom.py @@ -217,6 +217,65 @@ def _near_pairs(c, hm): return ii[ok], jj[ok] +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" +_IMG_CACHE: dict = {} +_IMG_GRID: dict = {} + + +class substrate: + """Context manager: the MoM Green's function inside the block (`SUBSTRATE`): model "slab_open" (default since + 2026-10-10, PRIOR_SCHEME.md 14.6), "halfspace" (the kernel of the priors the released models were trained on) or + "slab_pec"; t_um the substrate thickness (None: unchanged).""" + + def __init__(self, model: str | None, t_um: float | None = None): + self.new = {k: v for k, v in (("model", model), ("t_um", t_um)) if v is not None} + + def __enter__(self): + self.old = dict(SUBSTRATE) + SUBSTRATE.update(self.new) + return self + + def __exit__(self, *exc): + SUBSTRATE.clear() + SUBSTRATE.update(self.old) + + +def _image_correction(d, eps_r: float, exact: bool = False): + """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 + 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, + 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), + "slab_pec" = metal backside (w_n = -(1 + K) (-K)^(n-1); weights sum to -1). Images up to K^2n < 1e-4, the remaining + weight as one more image; tabulated on a uniform grid of t / 1000 up to 100 mm and interpolated linearly (dense + assembly), or summed exactly (exact=True: the H-matrix kernel, whose ACA blocks need a smooth kernel). None for the + half-space (the published kernel).""" + m = SUBSTRATE["model"] + if m == "halfspace": + return None + key = (m, float(SUBSTRATE["t_um"]), float(eps_r)) + if key not in _IMG_CACHE: + K, t = (eps_r - 1) / (eps_r + 1), float(SUBSTRATE["t_um"]) + n = np.arange(1, 400) + nmax = int(np.argmax(K ** (2 * n) < 1e-4)) + 1 + n = n[:nmax] + if m == "slab_open": + w, total = K ** (2 * n) + K ** (2 * n - 1), K / (1 - K) + else: + w, total = -(1 + K) * (-K) ** (n - 1), -1.0 + w, D = np.r_[w, total - w.sum()], np.r_[2 * n * t, 2 * (nmax + 1) * t] + du = t / 1000 + u = np.arange(0.0, 1e5 + 2 * du, du) + T = (w[None, :] / np.sqrt(u[:, None] ** 2 + D[None, :] ** 2)).sum(1) + _IMG_CACHE[key] = (du, T, w, D) + du, T = _IMG_CACHE[key][:2] + if exact: + w, D = _IMG_CACHE[key][2:] + d2 = np.asarray(d, float) ** 2 + return sum(wn / np.sqrt(d2 + Dn * Dn) for wn, Dn in zip(w, D)) + if key not in _IMG_GRID: + _IMG_GRID[key] = du * np.arange(len(T)) + return np.interp(d, _IMG_GRID[key], T) + + def _assemble(c, wh, diag, eps_r: float, near): """Interaction matrix / (4 pi eps0 eps_eff) for the lower Cholesky factorisation: far pairs 1 / d, near pairs (d < 1.5 max side) the symmetrised near(p, cq, wh) potential, diagonal `diag`; the same numbers as the dense construction. @@ -234,16 +293,22 @@ def _assemble(c, wh, diag, eps_r: float, near): np.multiply(t, t, out=t) d += t np.sqrt(d, out=d) + corr = _image_correction(d, eps_r) d[np.arange(r1 - r0), np.arange(r1 - r0)] = 1.0 out = A[r0:r1, r0:] np.divide(1.0, d, out=out) + if corr is not None: + out += corr out /= k _run([partial(rows, a, b) for a, b in _spans(n, 64)]) ii, jj = _near_pairs(c, wh.max(1)) + img = _image_correction(np.zeros(1), eps_r) if len(ii): A[ii, jj] = 0.5 * (near(c[ii], c[jj], wh[jj]) + near(c[jj], c[ii], wh[ii])) / k - A[np.arange(n), np.arange(n)] = diag / k + if img is not None: + A[ii, jj] += _image_correction(np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)), eps_r) / k + A[np.arange(n), np.arange(n)] = diag / k if img is None else (diag + img[0]) / k return A.T @@ -470,7 +535,7 @@ def _solve_panels_gpu(c, wh, o, n_metals: int, eps_r: float): def _solve_panels_quad(c, wh, o, n_metals: int, eps_r: float): """tlsq.embed.proxy.mom_solve (CPU path) for (width, height) panels: 3x3 point average for near pairs. With TLSQ_MOM_DEVICE set (e.g. cuda) the same float64 assembly and Cholesky run in torch on that device (opt-in).""" - if os.environ.get("TLSQ_MOM_DEVICE"): + if os.environ.get("TLSQ_MOM_DEVICE") and SUBSTRATE["model"] == "halfspace": # the torch path has no image correction Q = _solve_panels_gpu(c, wh, o, n_metals, eps_r) if Q is not None: return Q @@ -642,6 +707,8 @@ class _Kernel: self.x, self.y = c[perm, 0].copy(), c[perm, 1].copy() self.k = 4 * np.pi * EPS0_FF_PER_UM * (eps_r + 1) / 2 + self.eps_r = eps_r + self.img = _image_correction(np.zeros(1), eps_r) is not None # finite-substrate image term (exact series in blocks / rows) n = len(c) ii, jj = _near_pairs(c, wh.max(1)) d = np.sqrt(((c[ii] - c[jj]) ** 2).sum(1)) @@ -655,9 +722,12 @@ class _Kernel: def block(self, a0, a1, b0, b1): 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) + corr = _image_correction(d, self.eps_r, exact=True) if a0 == b0: d[np.arange(a1 - a0), np.arange(a1 - a0)] = 1.0 A = 1.0 / d + if corr is not None: + A += corr Nb = self.N[a0:a1, b0:b1] if Nb.nnz: A += Nb.toarray() @@ -666,6 +736,8 @@ class _Kernel: def row(self, i, b0, b1): d = np.sqrt((self.x[i] - self.x[b0:b1]) ** 2 + (self.y[i] - self.y[b0:b1]) ** 2) r = 1.0 / d + if self.img: + r += _image_correction(d, self.eps_r, exact=True) Nr = self.N[i, b0:b1] if Nr.nnz: r += Nr.toarray()[0] @@ -785,6 +857,7 @@ class _HMatrix: self.pre = [(a0, a1, cho_factor(D, lower=True)) for a0, a1, b0, _, D in self.dense if a0 == b0] # block Jacobi self.n_threads = max(1, _nthreads()) + def nbytes(self): return sum(D.nbytes for *_, D in self.dense) + sum(U.nbytes + V.nbytes for *_, (U, V) in self.low) @@ -941,10 +1014,16 @@ def mom_solve_formula(metals: list, eps_r: float = 11.45, workers: int | None = return {"C": C, "n_cells": 0, "h_min": float(info["min_gap_um"]), "mesh": rep, "far_mask": np.zeros(C.shape, bool)} -def solve_prior(metals: list, mesh: str = "v2", **kw): +def solve_prior(metals: list, mesh: str = "v2", substrate: str = "slab_open", **kw): """Dispatch: mesh "v2" -> mom_solve (published priors, unchanged), "v3" / "v3-rich" -> mom_solve_v3, "flagship" -> mom_solve_flagship (strip-merged Richardson, resolution-controlled), "lite" / "formula" -> the flagship-fast cheap - priors (mom_solve_lite / mom_solve_formula).""" + priors (mom_solve_lite / mom_solve_formula). substrate: the MoM Green's function (`SUBSTRATE`; the registry gives + "halfspace" for the priors the released models were trained on).""" + with globals()["substrate"](substrate): + return _solve_prior(metals, mesh, **kw) + + +def _solve_prior(metals: list, mesh: str = "v2", **kw): if mesh == "v2": return mom_solve(metals, **kw) if mesh == "flagship": diff --git a/tlsq_infer/_vendor/mom_loc.py b/tlsq_infer/_vendor/mom_loc.py new file mode 100644 index 0000000000000000000000000000000000000000..856b42a26d049830cfbebc454689ebe45753b020 --- /dev/null +++ b/tlsq_infer/_vendor/mom_loc.py @@ -0,0 +1,46 @@ +"""Local-gap lite prior for inference (PRIOR_SCHEME.md section 19, Amendment MESH; training variants liteppg

_slab_loc). +Off the training path: nothing imports this module until the final-matrix code freeze hooks it into `mom.solve_prior` +(mesh "lite_loc") and `registry.PRIORS` (PRIORS_LOC below). Parity: deploy/tests/test_fastprior_loc.py.""" + +from __future__ import annotations + +import time + +import numpy as np + +from . import mom as M + +PRIORS_LOC = {f"liteppg{x:g}-slab-loc": dict(mesh="lite_loc", max_cells=10**9, cells_per_gap=x, substrate="slab_open") + for x in (0.375, 0.75, 1.5, 3.0)} + + +def mom_solve_lite_loc(metals: list, eps_r: float = 11.45, max_cells: int = 10**9, cells_per_gap: float = 3.0, workers: int | None = None): + """Layouts with a sliver pair (< 0.25 um) and <= 8 conductors: each cell meshed at clamp(local gap / ppg, size / 4096, + G_ref / ppg) (`fastprior_loc.micro_mom_loc`); every other layout: `mom.mom_solve_lite` unchanged. Same return dict + as mom_solve_lite; h_min = G_ref / ppg and mesh min_gap_um = G_ref (descriptor log ppg).""" + from .fastprior_loc import micro_mom_loc, ref_gap + + if len(metals) - 1 > 8 or ref_gap(metals)[3] == 0: + out = M.mom_solve_lite(metals, eps_r, max_cells, cells_per_gap, workers) + out["mesh"]["local"] = False + return out + t0 = time.perf_counter() + C, far, info = micro_mom_loc(metals[0], list(metals[1:]), eps_r, ppg=cells_per_gap) + M._tick("lite", t0) + for k in ("mesh_s", "solve_s"): + with M._LOCK: + M.STAGE_TIMES[f"lite_{k}"] = M.STAGE_TIMES.get(f"lite_{k}", 0.0) + float(info[k]) + rep = {"prior": "lite_loc", "local": True, "h_min_um": float(info["H_um"]), "min_gap_um": float(info["G_ref_um"]), + "gmin_um": float(info["gmin_um"]), "h_floor_um": float(info["h_floor_um"]), "max_cells": int(max_cells), + "panels_per_gap_min": float(min(info["panels_per_gap"])), "panels_per_gap_median": float(np.median(info["panels_per_gap"])), + "n_windows": 1, "panels": [int(x) for x in info["panels"]], "n_far_pairs": 0, "workers": 1, "cap_hit": False, + "merged": True, "solver": "dense (lite, local mesh)"} + return {"C": C, "n_cells": int(sum(info["panels"])), "h_min": float(info["H_um"]), "mesh": rep, "far_mask": far} + + +def solve_prior_loc(metals: list, mesh: str = "lite_loc", substrate: str = "slab_open", **kw): + """`mom.solve_prior` for mesh "lite_loc" (until the hook-up); any other mesh is passed to mom.solve_prior.""" + if mesh != "lite_loc": + return M.solve_prior(metals, mesh, substrate, **kw) + with M.substrate(substrate): + return mom_solve_lite_loc(metals, **kw) diff --git a/tlsq_infer/_vendor/rejtail_fv.py b/tlsq_infer/_vendor/rejtail_fv.py new file mode 100644 index 0000000000000000000000000000000000000000..a3f90bc45a8f46600fb8c3675505909c7d5b7b32 --- /dev/null +++ b/tlsq_infer/_vendor/rejtail_fv.py @@ -0,0 +1,115 @@ +"""rejtail fix fv (agents/rejtail/RESULTS.md, scripts/rejtail/rt.py; user approval 2026-10-17): value-preserving removal of +the rejection tail of the windowed micro-MoM prior (`fastprior._window_solve`, all_rhs=False, layouts with > 8 conductors). + +A window attempt whose mesh leaves a metal without a panel is always rejected, whatever its solve returns. The frozen code +still solves it, and above DENSE_MAX panels the H-matrix PCG cannot converge: the panel-less metal has a zero right-hand +side, so the residual is 0/0 = NaN, and it runs 1,000 iterations before raising. fv meshes such attempts but does not +solve them. If the loop ends without an accepted attempt, the result the unpatched code returns is rebuilt by solving +the deferred dense attempts lazily: + keep_last_solved=True (frozen geomfast stage): the latest attempt whose solve succeeded, newest first; + keep_last_solved=False (tlsq_infer vendor): the last attempt's own solve, else the same RuntimeError. +Every accepted solve is the same call on the same mesh, so the result is bit-identical (621 layouts in RESULTS.md; +deploy/tests/test_gnn_space.py re-checks it). all_rhs=True calls (single window, N <= 8) run the original function. +The same file is shipped as geomgnn/rejtail_fv.py and tlsq_infer/_vendor/rejtail_fv.py; only the import line differs. +""" +from __future__ import annotations + +import time + +import numpy as np + +from . import fastprior as F +from . import mom as P + +KEEP_LAST_SOLVED = False +_ORIG = {} + + +def _solve(c, wh, o, metals, cols, eps_r): + """One attempt solved exactly as the original loop does (windowed: LDL^T dense, H-matrix PCG above DENSE_MAX). + Returns (R, assemble seconds).""" + t = time.perf_counter() + t_asm = 0.0 + if len(c) <= P.DENSE_MAX: + A = P._assemble(c, wh, P.rect_self(wh[:, 0], wh[:, 1]), eps_r, P.rect_potential) + t_asm = time.perf_counter() - t + Bm = (o[:, None] == cols[None, :]).astype(float) + Q = F._sym_solve(A, Bm) + else: + Q = P.mom_solve_fast(c, wh, o, len(metals), eps_r)[:, cols] + return np.stack([Q[o == a].sum(0) for a in range(len(metals))], 1), t_asm + + +def window_solve(metals: list, n_core: int, h: float, budget: float, eps_r: float, all_rhs: bool, levels: tuple = ()) -> tuple: + if all_rhs: + return _ORIG["window_solve"](metals, n_core, h, budget, eps_r, all_rhs, levels) + cols = np.arange(1, 1 + n_core) + t_mesh = t_asm = t_sol = 0.0 + hh, cap, refined, majority = h, budget, 0, False + hist = [] # per attempt, oldest first: ("R", R) | ("err",) | ("mesh", c, wh, o) deferred dense | ("doomed",) deferred H-matrix + while True: + t = time.perf_counter() + if not refined: + c, wh, o, hh, n_raw = F._lite_mesh(metals, hh, cap, n_core, levels) + elif levels: + c, wh, o, hh, n_raw = F._graded_mesh(metals, n_core, hh, levels, majority) + else: + c, wh, o, hh, n_raw = P.mesh_flagship(metals, hh, cap, majority=majority) + t_mesh += time.perf_counter() - t + if not np.all(np.bincount(o, minlength=len(metals)) > 0): + hist.append(("doomed",) if len(c) > P.DENSE_MAX else ("mesh", c, wh, o)) + ok = False + else: + t = time.perf_counter() + try: + R, ta = _solve(c, wh, o, metals, cols, eps_r) + t_asm += ta + hist.append(("R", R)) + ok = True + except np.linalg.LinAlgError: + hist.append(("err",)) + ok = False + t_sol += time.perf_counter() - t + if ok or majority: + break + if refined < F.LITE_REFINE: + hh, cap, refined = hh / 2, np.inf, refined + 1 + else: + majority = True + if ok: + R = hist[-1][1] + else: + R = None + t = time.perf_counter() + for e in reversed(hist if KEEP_LAST_SOLVED else hist[-1:]): + if e[0] == "R": + R = e[1] + break + if e[0] == "mesh": + try: + R = _solve(e[1], e[2], e[3], metals, cols, eps_r)[0] + break + except np.linalg.LinAlgError: + continue + t_sol += time.perf_counter() - t + if R is None: + raise RuntimeError(f"window solve failed: no positive-definite system after {refined} refinements (h={hh:.4g} um); " + "the window has no valid solution at this mesh") + return R, {"h_um": hh, "panels": len(c), "raw": n_raw, "refined": refined, "majority": majority, "mesh_s": t_mesh, + "assemble_s": t_asm, "solve_s": t_sol} + + +def install() -> None: + """Replace fastprior._window_solve (a module global, looked up at call time, also in the forked window workers).""" + if not _ORIG: + _ORIG["window_solve"] = F._window_solve + F._window_solve = window_solve + + +def uninstall() -> None: + if _ORIG: + F._window_solve = _ORIG.pop("window_solve") + + +def installed() -> bool: + return bool(_ORIG) diff --git a/tlsq_infer/app_v2.py b/tlsq_infer/app_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..13458371ed775d38706ecf48a38ac156a85da80e --- /dev/null +++ b/tlsq_infer/app_v2.py @@ -0,0 +1,290 @@ +"""Gradio front end of the paper-models Space (v2, PLAN_HF_SPACE_V2.md): one app, a model toggle over the final recipes of +the paper (tlsq_infer.final: E3 model, E1 fast, C0 physics only, plus the deployment model E3-D when its weights are +bundled), training geometry by default (no ground crop; the crop is an advanced option). Reuses app_ui (layer table, +previews, error handling). Needs `gradio`. + +API endpoints (gradio_client, api_name; the same over REST at /gradio_api/call/): + /models () -> served models: id, title, recipe, role, accuracy + /inspect (gds file) -> layer table, layer plot + /prepare (gds, layers table, crop, margin_gaps, margin_um, select) -> preview, warnings, summary, simplified GDS + /predict (gds, layers table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness_um, model, self_from_rows, + trust_rule) -> matrix table, CSV, JSON, report + /predict_gds (gds, ground "1/0", conductor "1/10", margin_gaps, substrate, custom_eps, thickness_um, model, self_from_rows, trust_rule) + -> result dict (one call, for scripts). margin_gaps: 0 or < 0 = no crop (training geometry, default), + > 0 = crop the ground to that many conductor-ground gaps around the conductors, "auto" = the old auto window. + model: "E3" (default) / "E1" / "C0" / "E3-D" / "E1-D", or the words "model" / "fast" / "physics" / "deployment" / + "deployment fast". trust_rule (default True = final_defaults.trust_rule): the paper's seed-spread rule for the E3 + recipes (E3, E3-D); False returns the raw E3 matrix. The trust decision and the seed spread are in "trust". +""" + +from __future__ import annotations + +import json +import os +import shutil +import tempfile +import time +from pathlib import Path + +from . import __version__ +from . import final as F +from .app_ui import CUSTOM, HEAVY, INTRO, LAYER_COLS, LIMITS, _eps, _parse_layers, _parse_select, _path, _roles, _safe, inspect_fn, matrix_table +from .layout import LayoutError, prepare +from .predict import matrix_csv +from .registry import load_registry + +ALIASES = {"model": "E3", "fast": "E1", "physics": "C0", "physics only": "C0", "deployment": "E3-D", "deployment fast": "E1-D"} +NETWORK_CAP = int(os.environ.get("TLSQ_MAX_CONDUCTORS_NETWORK", 30)) # E3 recipes: dense pair tensors; E1 / C0 up to LIMITS["max_conductors"] +PURGE_AGE_S = int(os.environ.get("TLSQ_PURGE_AGE_S", 600)) +_TMP = Path(tempfile.gettempdir()) / "tlsq_results" + + +def served(reg=None) -> list[str]: + """Final models with weights present (C0 always), in registry order.""" + reg = reg or load_registry() + return [k for k in F.model_names(reg) if F.available(k, reg)] + + +def resolve(reg, model) -> str: + m = str(model or reg["final_defaults"]["model"]).strip() + m = ALIASES.get(m.lower(), m) + if m not in served(reg): + raise ValueError(f"model {model!r} is not served here; choose one of {served(reg)} (or model / fast / physics / deployment / " + "deployment fast)") + return m + + +def _crop(crop, margin_gaps, margin_um): + """(margin_gaps, margin_um) for prepare: no crop unless asked (training geometry).""" + if not crop: + return None, None + mu = float(margin_um) if margin_um not in (None, "", 0, 0.0) else None + return (float(margin_gaps) if margin_gaps not in (None, "", 0, 0.0) else "auto"), mu + + +def _prepare(f, table, crop, margin_gaps, margin_um, select): + g, c, i = _roles(table) + mg, mu = _crop(crop, margin_gaps, margin_um) + return prepare(_path(f), ground_layers=g, conductor_layers=c, ignore_layers=i, margin_gaps=mg, margin_um=mu, + select=_parse_select(select), max_conductors=LIMITS["max_conductors"]) + + +def prepare_fn(f, table, crop, margin_gaps, margin_um, select): + import matplotlib + matplotlib.use("Agg") + lay = _prepare(f, table, crop, margin_gaps, margin_um, select) + fig = lay.render() + fig.tight_layout() + out = _outdir() / "prepared_1-0_ground_1-10_conductors.gds" + lay.to_gds(out) + summ = {"conductors": lay.names, "min_gap_um": round(lay.min_gap_um, 4), "geometry": "cropped" if lay.window else "training (no crop)", + "window_um": [round(x, 2) for x in lay.window] if lay.window else None, "lateral_extent_um": round(lay.extent_um(), 1), + **{k: v for k, v in lay.info.items() if k != "cell"}} + warn = "\n".join(f"- {w}" for w in lay.warnings) or "- none" + return fig, f"**Warnings**\n\n{warn}", summ, str(out) + + +def _outdir() -> Path: + """Per-request result folder; folders older than PURGE_AGE_S are deleted on every request (nothing is kept).""" + _TMP.mkdir(parents=True, exist_ok=True) + now = time.time() + for d in _TMP.iterdir(): + try: + if now - d.stat().st_mtime > PURGE_AGE_S: + shutil.rmtree(d, ignore_errors=True) + except OSError: + pass + return Path(tempfile.mkdtemp(dir=_TMP)) + + +def _predict(lay, model, **kw): + reg = load_registry() + name = resolve(reg, model) + if F.load_model(name, reg).recipe == "E3" and lay.n_conductors > NETWORK_CAP: + raise LayoutError(f"{lay.n_conductors} conductors: the network recipes (E3) are capped at {NETWORK_CAP} on this server; " + "use the fast (E1) or physics-only (C0) recipe, or run the package locally") + kw["trust_rule"] = bool(reg["final_defaults"].get("trust_rule", True)) if kw.get("trust_rule") is None else bool(kw["trust_rule"]) + if lay.n_conductors <= 8: + return F.predict_final(lay, name, **kw) + with HEAVY: + return F.predict_final(lay, name, **kw) + + +def trust_note(reg) -> str: + return reg["final_defaults"].get("trust_rule_note") or "" + + +def _acc_lines(m: dict) -> list[str]: + acc = m.get("accuracy") or {} + if not acc: + return ["No test number applies to this model (see its warning)."] + ([f"\n{m['scoring_note']}"] if m.get("scoring_note") else []) + rows = ["| test set | median % | MAE (fF) | RMSE (fF) |", "|---|---|---|---|"] + rows += [f"| {v['set']} | {v['median_pct']:.3f} | {v['mae_ff']:.3f} | {v['rmse_ff']:.3f} |" for v in acc.values()] + return rows + + +def report_md(res: dict, reg) -> str: + m = reg["final_models"][res["model"]] + t = res["timing_s"] + tr = res.get("trust") or {} + lines = [f"**Model** `{res['model']}`: {res['model_title']} ({res['role']}; ledger id `{res['ledger_id']}`), package " + f"{res['package_version']}, registry v{res['registry_version']}", + f"**Substrate** {res['substrate']} (eps_r {res['eps_r']:g}), {res['thickness_um']:g} µm; the recipes see the training " + f"stack (Si 11.45, 350 µm) and every capacitance is scaled by (1+eps)/(1+11.45) = {res['eps_correction_factor']:.4f}", + "**Timing** " + ", ".join(f"{k.replace('_s', '')} {v:.2f} s" for k, v in t.items() if isinstance(v, float)), + ""] + if tr: + sp = f"seed spread of the three networks {tr['seed_spread']:.3g} vs threshold {tr['tau']:g}" + if tr.get("applied") and tr.get("fallback_to_C0"): + lines.append(f"**Trust rule applied (default): FALLBACK to physics-only C0** for this whole layout ({sp}: the networks disagree).") + elif tr.get("applied"): + lines.append(f"**Trust rule applied (default): E3 kept** on the {tr['n_E3_entries']} conductor pairs closer than " + f"{tr['near_um']:g} µm, physics-only C0 on the other pairs ({sp}: the networks agree).") + else: + lines.append(f"**Trust rule switched off**: raw E3 on every pair ({sp}" + + ("; the rule would have fallen back to C0)." if tr["exceeds_tau"] else "; the rule would have kept E3 on the close pairs).")) + if m.get("accuracy"): + lines += ["", "**Test results of this recipe in the paper** (reported model, scored once on each test set; median relative " + "error / MAE / RMSE over mutual entries):", ""] + _acc_lines(m) + else: + lines += ["", "**Test results**:", ""] + _acc_lines(m) + e = res.get("expected_error") or {} + if "material_residual_pct" in e: + lines.append(f"\nStack residuals for this request: material {e['material_residual_pct']:.2f} %, finite thickness " + f"{e['thickness_residual_pct']:.2f} % (15-design Q3D DoE, indicative).") + if res["warnings"]: + lines += ["", "**Warnings**", ""] + [f"- {w}" for w in res["warnings"]] + lines += ["", f"Self capacitance: {res['self_capacitance']}. Ground self-capacitance is not predicted.", + "Planar single-layer layouts only (zero-thickness metal). Nothing you upload is stored: result files are deleted " + f"within {PURGE_AGE_S // 60} minutes."] + return "\n".join(lines) + + +def predict_fn(f, table, crop, margin_gaps, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_rule=True): + reg = load_registry() + lay = _prepare(f, table, crop, margin_gaps, margin_um, select) + res = _predict(lay, model, substrate=_eps(substrate, custom_eps), thickness_um=float(thickness), self_from_rows=bool(self_rows), + trust_rule=trust_rule) + d = _outdir() + (d / "capacitance_fF.csv").write_text(matrix_csv(res)) + (d / "capacitance.json").write_text(json.dumps(res, indent=1)) + return matrix_table(res), str(d / "capacitance_fF.csv"), str(d / "capacitance.json"), report_md(res, reg), res + + +def _margin_api(v): + if isinstance(v, str) and v.strip().lower() == "auto": + return "auto" + v = float(v or 0) + return None if v <= 0 else v + + +def api_predict_gds(f, ground_layers="1/0", conductor_layers="1/10", margin_gaps=0.0, substrate="Si (cryogenic, training)", + custom_eps=11.45, thickness_um=350.0, model="E3", self_from_rows=True, trust_rule=True): + t = time.perf_counter() + lay = prepare(_path(f), ground_layers=_parse_layers(ground_layers), conductor_layers=_parse_layers(conductor_layers), + margin_gaps=_margin_api(margin_gaps), max_conductors=LIMITS["max_conductors"]) + res = _predict(lay, model, substrate=_eps(substrate, custom_eps), thickness_um=float(thickness_um), self_from_rows=bool(self_from_rows), + trust_rule=trust_rule) + res["timing_s"]["prepare_and_total_s"] = time.perf_counter() - t + return res + + +def models_fn(): + reg = load_registry() + return [{"id": k, "title": reg["final_models"][k]["title"], "recipe": reg["final_models"][k]["recipe"], "role": reg["final_models"][k].get("role"), + "ledger_id": reg["final_models"][k].get("ledger_id"), "accuracy": reg["final_models"][k].get("accuracy") or {}, + "warning": reg["final_models"][k].get("warning")} for k in served(reg)] + [{"package_version": __version__, "registry_version": reg["version"]}] + + +def _model_md(reg, k) -> str: + m = reg["final_models"][k] + return "\n".join([f"**{m['title']}** ({m.get('role')}). {m.get('summary', '')}"] + ([f"\n> {m['warning']}"] if m.get("warning") else []) + + ["", *_acc_lines(m)]) + + +HEADER = """Zero-shot **Maxwell capacitance matrix** of a planar superconducting layout from its GDS: a slab-kernel micro-MoM solve at +two panel densities, log-Richardson extrapolation, per-kind offsets and (model E3) a bounded learned correction. Choose the +recipe below; the default is the paper's reported model, with the paper's trust rule applied (note below). **Private preview** +(paper in preparation).""" + + +def build_app(title: str, examples_dir=None): + import gradio as gr + + reg = load_registry() + subs = list(reg.substrates()) + [CUSTOM] + ms = served(reg) + default = reg["final_defaults"]["model"] + ex = sorted(Path(examples_dir).glob("*.gds")) if examples_dir and Path(examples_dir).exists() else [] + with gr.Blocks(title=title, delete_cache=(60, PURGE_AGE_S)) as demo: + gr.Markdown(f"# {title}\n{HEADER}") + if trust_note(reg): + gr.Markdown(f"> {trust_note(reg)}") + with gr.Accordion("Planar circuits and the layer convention", open=False): + 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.", + "See the model descriptions below.")) + gr.Markdown("## Model") + model = gr.Radio([(reg["final_models"][k]["title"], k) for k in ms], value=default, label="recipe") + model_info = gr.Markdown(_model_md(reg, default)) + model.change(lambda k: _model_md(reg, k), model, model_info, api_visibility="private") + gr.Markdown("## 1. Upload a GDS and assign layer roles") + with gr.Row(): + with gr.Column(scale=1): + gds = gr.File(label="GDS file", file_types=[".gds", ".gds2", ".gdsii"]) + if ex: + gr.Examples([[str(p)] for p in ex], inputs=[gds], label="or start from an example") + layers = gr.Dataframe(headers=LAYER_COLS, datatype=["number", "number", "str", "number", "number", "str"], + label="detected layers: edit 'role' (ground / conductor / ignore)", interactive=True, + value=[[1, 0, "ground", 0, 0, ""], [1, 10, "conductor", 0, 0, ""]]) + layer_plot = gr.Plot(label="layers") + gr.Markdown("## 2. Prepare: extract the nets (full ground, as in training)") + with gr.Row(): + with gr.Column(scale=1): + select = gr.Textbox(label="conductors to model (e.g. 1,2,3; empty = all)", value="") + with gr.Accordion("Advanced: crop the ground (not the training geometry)", open=False): + crop = gr.Checkbox(False, label="crop the ground plane around the conductors") + margin = gr.Slider(0, 300, value=0, step=1, label="margin in conductor-ground gaps (0 = auto window)") + margin_um = gr.Number(value=None, label="or absolute margin (µm); overrides the slider") + prep_btn = gr.Button("Prepare", variant="secondary") + warn_md = gr.Markdown() + summary = gr.JSON(label="prepared layout") + prepared_gds = gr.File(label="simplified GDS (1/0 ground, 1/10 conductors)") + prep_plot = gr.Plot(label="prepared layout") + gr.Markdown("## 3. Substrate") + with gr.Row(): + substrate = gr.Dropdown(subs, value=subs[0], label="substrate") + custom_eps = gr.Number(value=11.45, label="custom eps_r (when 'custom')") + thickness = gr.Number(value=350, label="substrate thickness (µm)") + self_rows = gr.Checkbox(True, label="self-capacitance from row sums (not trained, not scored)") + trust_cb = gr.Checkbox(bool(reg["final_defaults"].get("trust_rule", True)), + label="apply the trust rule (E3 models; default on: fall back to physics-only C0 when the networks disagree)") + pred_btn = gr.Button("Predict", variant="primary") + gr.Markdown("## 4. Maxwell capacitance matrix (fF)") + with gr.Row(): + with gr.Column(scale=2): + mat = gr.Dataframe(label="Maxwell matrix (fF): diagonal = self, off-diagonal = -mutual", interactive=False) + with gr.Column(): + csv_f = gr.File(label="CSV") + json_f = gr.File(label="JSON (matrix + metadata)") + report = gr.Markdown() + res_state = gr.State(None) + with gr.Row(visible=False): + a_g = gr.Textbox("1/0") + a_c = gr.Textbox("1/10") + a_m = gr.Number(0) + a_out = gr.JSON() + a_btn = gr.Button("api") + m_btn = gr.Button("models") + m_out = gr.JSON() + gds.upload(_safe(inspect_fn, 2), gds, [layers, layer_plot], api_name="inspect") + prep_btn.click(_safe(prepare_fn, 4), [gds, layers, crop, margin, margin_um, select], [prep_plot, warn_md, summary, prepared_gds], + api_name="prepare") + pred_btn.click(_safe(predict_fn, 5), [gds, layers, crop, margin, margin_um, select, substrate, custom_eps, thickness, model, self_rows, trust_cb], + [mat, csv_f, json_f, report, res_state], api_name="predict") + 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, + api_name="predict_gds") + m_btn.click(models_fn, None, m_out, api_name="models") + gr.Markdown(f"tlsq_infer {__version__}. Demo-server caps (not limits of the models): {LIMITS['max_file_mb']:g} MB per file, " + f"{LIMITS['max_polygons']:,} polygons, {LIMITS['max_conductors']} conductors + ground (E1 / C0), {NETWORK_CAP} for the " + "network recipes (E3).") + return demo diff --git a/tlsq_infer/final.py b/tlsq_infer/final.py new file mode 100644 index 0000000000000000000000000000000000000000..6a9c871f26471fc479b8e66a202f1ff215ec3714 --- /dev/null +++ b/tlsq_infer/final.py @@ -0,0 +1,423 @@ +"""Final recipes of the paper (paper_v3; TRAINING_PLAN Amendment E-matrix, scored by scripts/tm_final.py): + + C0 = R(1.5, 3) log-Richardson of the slab lite micro-MoM at 1.5 and 3 panels per gap (no fitted parameter) + E1 = R(0.75, 1.5) + c_kind per-entry-kind log offsets of the model (no network) + E3 = R(1.5, 3) + c_kind + correction bounded (+-3 %) local correction (SceneGNN on pair tokens), mean log C of 3 seeds + +The rows are built as the evaluation rows of scripts/tm_eval.py (build_one): training-convention scene (uncropped ground), +pair tokens (tlsq.uge.blocks) on every pair (pairs within 150 smallest gaps on layouts with > 8 conductors), local tokens +(localtok), and per resolution the slab lite prior (local-gap mesh on sliver layouts, PRIOR_SCHEME.md section 19; identical to +the plain lite prior elsewhere). Prediction on rows reproduces tm_final.score_em exactly (deploy/tests/test_final_parity.py). +The networks see the training stack (Si 11.45, 350 um); other substrates use the half-space factor of predict.py. +""" + +from __future__ import annotations + +import copy +import hashlib +import json +import os +import time +from dataclasses import dataclass, field +from itertools import combinations +from pathlib import Path + +import numpy as np + +from . import __version__ +from ._vendor import em as EM +from ._vendor import mom as _mom +from ._vendor.graph import prior_dict +from ._vendor.scene import make_scene +from .layout import PreparedLayout +from .registry import PRIORS, load_registry +from .stack_error import (EPS_TRAIN, breakdown_thickness_um, eps_factor, lateral_extent, load_params, material_residual_pct, + thickness_residual_pct) + +RECIPES = {"C0": (1.5, 3.0), "E1": (0.75, 1.5), "E3": (1.5, 3.0)} +TAU = 1.254e-3 # trust rule (tm_final.TAU): seed spread above this -> C0 on the near graph entries +NEAR_UM = 100.0 +SPARSE_MIN, SPARSE_R_GAPS = 8, 150.0 # tm_eval: > 8 signal conductors -> pair tokens only within 150 smallest gaps + + +class PriorError(ValueError): + """The physics prior produced no usable mutual entry.""" + + +def internal_names(n_signal: int) -> list[str]: + """Entry-kind names of the deployment convention (as predict.py and the qm116 / atlas / large-N rows): G, N, S, P1, ...""" + return ["G"] + (["N", "S"] + [f"P{k}" for k in range(1, n_signal - 1)])[:n_signal] + + +# ------------------------------------------------------------------ rows (tm_eval.build_one) +def far_mask(metals: list) -> np.ndarray: + import shapely + + from ._vendor.fastprior import gap_of + n = len(metals) + far = np.zeros((n, n), bool) + if n - 1 <= SPARSE_MIN: + return far + conds = list(metals[1:]) + a, b = shapely.STRtree(conds).query(conds, predicate="dwithin", distance=SPARSE_R_GAPS * gap_of(list(metals))) + far[:] = True + far[a + 1, b + 1] = False + far[0, :] = far[:, 0] = False + np.fill_diagonal(far, False) + return far + + +def graph(scene, far: np.ndarray) -> dict: + """tm_eval.graph: tlsq.uge.graph.scene_graph without the prior, pairs restricted to the non-far ones.""" + from ._vendor.blocks import boundary_points, edge_features, global_features, node_features + bpts, widths = boundary_points(scene) + nodes = [node_features(scene, i) for i in range(scene.n)] + edges = {(i, j): edge_features(scene, i, j, bpts, widths) for i, j in combinations(range(scene.n), 2) if not far[i, j]} + tot = np.zeros(scene.n) + for (i, j), f in edges.items(): + tot[i] += f["_cps"] + tot[j] += f["_cps"] + for (i, j), f in edges.items(): + si, sj = f["_cps"] / max(tot[i], 1e-12), f["_cps"] / max(tot[j], 1e-12) + f["share_mean"], f["share_absdiff"] = 0.5 * (si + sj), abs(si - sj) + f.pop("_cps") + return {"names": scene.names(), "global": global_features(scene), "nodes": nodes, "edges": edges} + + +def solve(metals: list, ppg: float, mesh: str = "loc") -> dict: + """Slab lite micro-MoM at ppg panels per smallest gap: mesh "loc" (training rows: local-gap mesh on sliver layouts, + otherwise the plain lite mesh) or "plain" (liteppg

_slab everywhere).""" + if mesh == "loc": + from ._vendor.mom_loc import PRIORS_LOC, solve_prior_loc + return solve_prior_loc(metals, **PRIORS_LOC[f"liteppg{ppg:g}-slab-loc"]) + return _mom.solve_prior(metals, **PRIORS[f"liteppg{ppg:g}-slab"]) + + +def build_rows(metals: list, names: list[str], ppgs, features: bool = True, mesh: str = "loc", timing: dict | None = None) -> dict: + """{ppg: row} for one layout (metals[0] = ground; names[0] = 'G'). features=False: prior matrices only (C0 / E1).""" + timing = {} if timing is None else timing + t = time.perf_counter() + far = far_mask(metals) + g = {"names": list(names), "nodes": [], "edges": {}, "global": {}} + if features: + from .localtok import add_tokens + sc = make_scene(metals[0], list(metals[1:]), list(names[1:])) + timing["scene_s"] = time.perf_counter() - t + t = time.perf_counter() + g = graph(sc, far) + timing["features_s"] = time.perf_counter() - t + t = time.perf_counter() + g = add_tokens(sc, g) + timing["tokens_s"] = time.perf_counter() - t + out = {} + for p in ppgs: + t = time.perf_counter() + px = solve(metals, p, mesh) + timing[f"prior_ppg{p:g}_s"] = time.perf_counter() - t + r = copy.deepcopy(g) + d = prior_dict(px["C"], px["n_cells"], px["h_min"], list(g["edges"]), (px.get("mesh") or {}).get("min_gap_um")) + for i, v in d["node"].items(): + if features: + r["nodes"][i].update({f"proxy_{k}": x for k, x in v.items()}) + for e, v in d["edge"].items(): + r["edges"][e].update({f"proxy_{k}": x for k, x in v.items()}) + r["global"].update({f"proxy_{k}": x for k, x in d["global"].items()}) + r["proxy_matrix"] = px["C"] + r["_mesh"] = px.get("mesh") or {} + out[p] = r + out["far"] = far + return out + + +# ------------------------------------------------------------------ models +@dataclass +class FinalModel: + name: str + recipe: str + pair: tuple + c_kind: dict = field(default_factory=dict) + scaling: dict | None = None + nets: list = field(default_factory=list) + files_sha256: list = field(default_factory=list) + entry: dict = field(default_factory=dict) + + +def _sha(f) -> str: + return hashlib.sha256(Path(f).read_bytes()).hexdigest() + + +def _paths(reg, name: str, model_dir=None) -> tuple[str, Path | None]: + """("export", dir) for an exported model (//info.json: Space bundle) or ("source", path) for the + training artefacts named in the registry (repository checkout).""" + m = reg["final_models"][name] + md = os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir + if md and (Path(md) / name / "info.json").exists(): + return "export", Path(md) / name + if m.get("source"): + return "source", reg.root / m["source"] + return "none", None + + +_CACHE: dict = {} + + +def load_model(name: str, registry=None, model_dir=None) -> FinalModel: + reg = registry or load_registry() + key = (str(reg.path), name, str(os.environ.get("TLSQ_MODEL_DIR") if model_dir is None else model_dir)) + if key in _CACHE: + return _CACHE[key] + m = reg["final_models"][name] + recipe, pair = m["recipe"], tuple(float(x) for x in m["pair"]) + fm = FinalModel(name, recipe, pair, entry={"name": name, **m}) + kind, p = _paths(reg, name, model_dir) + if recipe == "E1": + f = p / "info.json" if kind == "export" else p + fm.c_kind = json.loads(Path(f).read_text())["c_kind"] + fm.files_sha256 = [_sha(f)] + elif recipe == "E3": + import torch + lo = pair[0] + if kind == "export": + info = json.loads((p / "info.json").read_text()) + files = [p / f for f in info["files"]] + states = [torch.load(f, map_location="cpu", weights_only=True) for f in files] + else: + info = json.loads((p / f"info_p{lo:g}.json").read_text()) + files = [p / f"s{s}_p{lo:g}.pt" for s in (0, 1, 2)] + states = [torch.load(f, map_location="cpu", weights_only=False)["state"] for f in files] + fm.c_kind = info["c_kind"] + fm.scaling = {k: np.asarray(v) for k, v in info["scaling"].items()} + assert list(info.get("edge_keys", EM.EDGE_KEYS)) == list(EM.EDGE_KEYS), f"{name}: edge keys differ from the package" + for st in states: + net = EM.make_model(len(EM.EDGE_KEYS)) + net.load_state_dict(st) + net.eval() + fm.nets.append(net) + fm.files_sha256 = [_sha(f) for f in files] + elif recipe != "C0": + raise ValueError(f"{name}: unknown recipe {recipe!r}") + _CACHE[key] = fm + return fm + + +def export_model(name: str, dst, registry=None) -> list[str]: + """Weights-only export of a final model into dst (Space bundle / model repo): info.json (+ seed.pt state dicts).""" + import torch + reg = registry or load_registry() + fm = load_model(name, reg, model_dir="") + dst = Path(dst) + dst.mkdir(parents=True, exist_ok=True) + m = reg["final_models"][name] + info = {"name": name, "recipe": fm.recipe, "pair": list(fm.pair), "c_kind": fm.c_kind, "ledger_id": m.get("ledger_id"), + "role": m.get("role"), "package_version": __version__, "source_sha256": fm.files_sha256, "files": []} + if fm.recipe == "E3": + info["scaling"] = {k: np.asarray(v).tolist() for k, v in fm.scaling.items()} + info["edge_keys"] = list(EM.EDGE_KEYS) + for k, net in enumerate(fm.nets): + torch.save({k_: v.clone() for k_, v in net.state_dict().items()}, dst / f"seed{k}.pt") + info["files"].append(f"seed{k}.pt") + (dst / "info.json").write_text(json.dumps(info, indent=1)) + return ["info.json"] + info["files"] + + +# ------------------------------------------------------------------ prediction on rows (tm_final.score_em) +def predict_rows(model: FinalModel, pairs: list, seeds: bool = False): + """pairs: [(row_lo, row_hi)] at model.pair. Returns a list of (n, n) log|C_ij| matrices (diagonal NaN), in the order of + the rows; seeds=True also returns the per-seed E3 matrices (S, n, n) per design (None for C0 / E1). One batch, as + tm_final (E3: one pack of all rows; the padding is max(8, max n)).""" + out, per_seed = [], [] + if model.recipe == "E3": + items = [EM.item(rl, rh) for rl, rh in pairs] + d = EM.pack(items) + d["E"] = EM.apply_scale(d["E"], d["A"], model.scaling) + cvec = np.array([0.0] + [model.c_kind[k] for k in EM.KIND_ID], np.float32) + P = np.asarray([EM.predict(net, d, np.arange(len(items)), cvec) for net in model.nets]) + Pm = np.mean(P, 0) + for b, (rl, rh) in enumerate(pairs): + nm = list(rh["names"]) + n = len(nm) + LR, _ = EM.logR(rl, rh) + L = np.full((n, n), np.nan) + for i in range(n): + for j in range(i + 1, n): + if model.recipe == "E3": + v = float(Pm[b, i, j]) + elif model.recipe == "E1": + v = float(LR[i, j] + model.c_kind[EM.entry_kind(nm[i], nm[j])]) + else: + v = float(LR[i, j]) + L[i, j] = L[j, i] = v + out.append(L) + per_seed.append(P[:, b, :n, :n] if model.recipe == "E3" else None) + return (out, per_seed) if seeds else out + + +def pair_distance_um(metals: list, pairs) -> dict: + """{(i, j): exact polygon distance (um)} for the given pairs: the paper's pair distance (scripts/grid_scaling.pair_distances, + the near / far slices of the test tables; ground pairs included).""" + import shapely + return {(i, j): float(shapely.distance(metals[i], metals[j])) for i, j in pairs} + + +def seed_spread(seeds: np.ndarray, row: dict, near_um: float | None = NEAR_UM, dist: dict | None = None) -> tuple[float, np.ndarray]: + """Trust statistic of tm_final (TRUST): median seed std over the near graph entries (pairs with an edge and a pair distance + below near_um: dist = pair_distance_um, else the edge feature's sampled minimum distance; near_um=None: every graph edge, + the convention of rows without a pair distance); returns (s, near mask).""" + n = len(row["names"]) + near = np.zeros((n, n), bool) + for (a, b), f in row["edges"].items(): + d = dist[(a, b)] if dist is not None else np.exp(f["log_dmin"]) + if near_um is None or d < near_um: + near[a, b] = near[b, a] = True + sd = seeds.std(0)[:n, :n] + return (float(np.median(sd[near])) if near.any() else float("inf")), near + + +def apply_trust(L: np.ndarray, seeds: np.ndarray, LR: np.ndarray, row: dict, near_um: float | None = NEAR_UM, + dist: dict | None = None) -> tuple[np.ndarray, dict]: + """Trust rule (TRAINING_PLAN Amendment Trust rule; tm_final TRUST, tau fixed before unblinding): s = seed spread over the near + graph entries. s <= TAU: E3 (L) on the near entries, C0 (LR) on every other entry; s > TAU: C0 on every entry. Returns the + log|C| matrix and the trust record.""" + s, near = seed_spread(seeds, row, near_um, dist) + use = s <= TAU + out = np.where(use & near, L, LR) + np.fill_diagonal(out, np.nan) + rec = {"seed_spread": s, "tau": TAU, "exceeds_tau": bool(s > TAU), "applied": True, "fallback_to_C0": not use, + "used": "E3 on near entries, C0 elsewhere" if use else "C0 (seed spread above tau)", + "near_um": near_um, "n_near_entries": int(np.triu(near, 1).sum()), + "n_E3_entries": int(np.triu(near, 1).sum()) if use else 0} + return out, rec + + +# ------------------------------------------------------------------ layout -> Maxwell matrix +def stack_residuals(eps: float, t_um: float, conductors: list, model_median_pct: float | None) -> dict: + """Substrate residuals of the 15-design Q3D stack DoE (stack_error): material (after the eps factor) and finite thickness, + and the breakdown thickness for this layout's lateral extent (needs the model's median error).""" + P = load_params() + if P is None: + return {"warning": "stack-error parameters not available"} + L = lateral_extent(conductors, P) + out = {"material_residual_pct": material_residual_pct(P, eps), "thickness_residual_pct": thickness_residual_pct(P, t_um, L), + "lateral_extent_um": float(L), "source": "15-design Q3D stack DoE (indicative)"} + if model_median_pct is not None: + tb = breakdown_thickness_um(P, L, float(model_median_pct)) + out.update(breakdown_thickness_um=tb, breakdown=bool(t_um < tb)) + if out["breakdown"]: + out["warning"] = (f"substrate {t_um:g} um is below the breakdown thickness {tb:.0f} um for a layout of lateral extent " + f"{L:.0f} um: the prediction (training stack 350 um) is not reliable here") + if not (2.0 <= eps <= 30.0): + out["warning"] = (out.get("warning", "") + f" eps={eps:g} is outside the validated range 3.8-24.").strip() + return out + + +def model_names(registry=None) -> list[str]: + reg = registry or load_registry() + return [k for k, v in (reg.data.get("final_models") or {}).items() if v.get("status", "available") == "available"] + + +def available(name: str, registry=None, model_dir=None) -> bool: + reg = registry or load_registry() + m = reg["final_models"][name] + if m["recipe"] == "C0": + return True + kind, p = _paths(reg, name, model_dir) + if kind == "export": + return True + if kind == "none": + return False + return p.exists() and (m["recipe"] == "E1" or all((p / f"s{s}_p{float(m['pair'][0]):g}.pt").exists() for s in (0, 1, 2))) + + +def predict_final(prepared: PreparedLayout, model: str | None = None, substrate="Si (cryogenic, training)", thickness_um: float = 350.0, + self_from_rows: bool = True, names: list[str] | None = None, registry=None, model_dir=None, mesh: str = "loc", + trust_rule: bool | None = None, trust_near_um: float | None = NEAR_UM, return_debug: bool = False) -> dict: + """Maxwell capacitance matrix (fF) of a prepared layout with a final recipe (registry final_models; default + final_defaults.model). names: entry-kind names (default G, N, S, P1, ...: the deployment convention). Self terms: row + sums / (1 - row_sum_eps) (not trained, not scored) when self_from_rows, else not returned (null). trust_rule (E3 recipes): + the paper's seed-spread rule (apply_trust); None = registry final_defaults.trust_rule (on), False = raw E3 (spread still + reported). trust_near_um: near = graph pairs whose polygon distance (the paper's pair distance) is below this; None = every + graph pair.""" + reg = registry or load_registry() + if trust_rule is None: + trust_rule = bool(reg["final_defaults"].get("trust_rule", True)) + model = model or reg["final_defaults"]["model"] + if model not in (reg.data.get("final_models") or {}): + raise ValueError(f"unknown final model {model!r}; available: {model_names(reg)}") + t_all = time.perf_counter() + timing: dict = {} + t = time.perf_counter() + fm = load_model(model, reg, model_dir) + timing["load_s"] = time.perf_counter() - t + eps = reg.eps_of(substrate) + metals = [prepared.ground] + list(prepared.conductors) + k = len(metals) + nm = list(names) if names is not None else internal_names(k - 1) + lo, hi = fm.pair + need = sorted({lo, hi} | ({1.5, 3.0} if trust_rule and fm.recipe == "E3" else set())) + rows = build_rows(metals, nm, need, features=(fm.recipe == "E3"), mesh=mesh, timing=timing) + for p in need: + mut = -np.asarray(rows[p]["proxy_matrix"])[np.triu_indices(k, 1)] + if not np.all(np.isfinite(mut)) or np.any(mut <= 0): + raise PriorError(f"the slab lite prior at {p:g} panels per gap returned {int((~np.isfinite(mut)).sum())} non-finite and " + f"{int((mut <= 0).sum())} non-positive mutual entries on this layout: no matrix is returned") + t = time.perf_counter() + L, S = predict_rows(fm, [(rows[lo], rows[hi])], seeds=True) + L, S = L[0], S[0] + timing["network_s" if fm.recipe == "E3" else "recipe_s"] = time.perf_counter() - t + debug = {"rows": rows, "logC": L.copy()} + trust = None + if fm.recipe == "E3": + dist = pair_distance_um(metals, rows[hi]["edges"]) if trust_near_um is not None else None + if trust_rule: + L, trust = apply_trust(L, S, EM.logR(rows[1.5], rows[3.0])[0], rows[hi], trust_near_um, dist) + else: + s, near = seed_spread(S, rows[hi], trust_near_um, dist) + trust = {"seed_spread": s, "tau": TAU, "exceeds_tau": bool(s > TAU), "applied": False, "fallback_to_C0": False, + "used": "E3 everywhere (trust rule switched off)", "near_um": trust_near_um, "n_near_entries": int(np.triu(near, 1).sum())} + f = eps_factor(eps) + Cabs = np.exp(L) + M = -Cabs * f + e_rs = float(reg["self_capacitance"]["row_sum_eps"]) + for i in range(k): + M[i, i] = np.nan + if self_from_rows: + for i in range(1, k): + M[i, i] = f * sum(Cabs[i, j] for j in range(k) if j != i) / (1 - e_rs) + timing["total_s"] = time.perf_counter() - t_all + timing["threads"] = int(_mom.NUM_THREADS or 0) or None + labels = ["G"] + list(prepared.names) + iu = np.triu_indices(k, 1) + mut = np.abs(M[iu]) + m = fm.entry + acc = m.get("accuracy") or {} + base = next(iter(acc.values()), {}) if acc else {} + exp_err = stack_residuals(eps, float(thickness_um), prepared.conductors, base.get("median_pct")) + warnings = list(prepared.warnings) + far = rows["far"] + if far.any(): + warnings.append(f"{int(np.triu(far, 1).sum())} conductor pairs farther apart than {SPARSE_R_GAPS:g} smallest gaps have no pair " + "tokens (large layout); their entries carry the prior's long-range value (aF scale, weakest entries)") + if trust and trust["exceeds_tau"]: + warnings.append(f"trust rule: seed spread {trust['seed_spread']:.3g} > tau {TAU:g}: " + + ("fell back to the physics-only C0 recipe for this whole layout" if trust["applied"] else + "the rule (switched off here) would use the physics-only C0 recipe for this layout")) + if exp_err.get("warning"): + warnings.append(exp_err["warning"]) + mesh_info = {p: rows[p]["_mesh"] for p in need} + out = {"names": labels, "matrix_fF": [[None if np.isnan(v) else float(v) for v in row] for row in M], "units": "fF", + "convention": "Maxwell matrix: diagonal = self, off-diagonal = -mutual; row/column 0 = ground (self not predicted)", + "self_capacitance": (f"row sums / (1 - {e_rs}); not trained, not scored" if self_from_rows else "not returned"), + "model": model, "model_title": m.get("title"), "recipe": fm.recipe, "pair": list(fm.pair), "role": m.get("role"), + "ledger_id": m.get("ledger_id"), "weights_sha256": fm.files_sha256, "package_version": __version__, + "registry_version": reg["version"], "substrate": substrate if not isinstance(substrate, (int, float)) else f"custom eps={eps:g}", + "eps_r": eps, "eps_correction_factor": f, "thickness_um": float(thickness_um), + "training_stack": {"eps_r": EPS_TRAIN, "thickness_um": 350.0}, "kind_names": nm, "timing_s": timing, + "prior_mesh": {f"{p:g}": {kk: v for kk, v in mi.items() if kk in ("prior", "local", "min_gap_um", "h_min_um", "panels", "n_windows")} + for p, mi in mesh_info.items()}, + "accuracy": acc, "expected_error": exp_err, "trust": trust, "far_mask": far.tolist() if far.any() else None, + "warnings": warnings, "stored": False} + if m.get("warning"): + out["warnings"].insert(0, m["warning"]) + if return_debug: + out["debug"] = debug + return out diff --git a/tlsq_infer/layout.py b/tlsq_infer/layout.py index 653de58306d20fea2237ef3478f93501e64853b2..da59c281a0ef79686632f89f0733b0dc3c423ce8 100644 --- a/tlsq_infer/layout.py +++ b/tlsq_infer/layout.py @@ -1,4 +1,4 @@ -"""GDS reading and layout preparation: layers -> ground net + conductor bodies, cropped to a window around the conductors.""" +"""GDS reading and layout preparation: layers -> ground net + conductor bodies (full ground by default; optional crop window).""" from __future__ import annotations @@ -213,17 +213,17 @@ class PreparedLayout: return fig -def prepare(path, ground_layers=(GROUND,), conductor_layers=(CONDUCTOR,), margin_gaps: float | str | None = "auto", +def prepare(path, ground_layers=(GROUND,), conductor_layers=(CONDUCTOR,), margin_gaps: float | str | None = None, margin_um: float | None = None, ignore_layers=(), cell: str | None = None, select=None, select_box=None, max_conductors: int = N_MAX_SIGNALS) -> PreparedLayout: """Extract the capacitance problem from a GDS file. - conductors: connected bodies of the conductor-layer polygons (touching/overlapping polygons are one net), in file order; bodies that touch the ground metal are shorted to it and merged into the ground net (warning). - - ground: union of the ground-layer polygons, cropped to the window = bbox(conductors + the etched pocket that - contains them) expanded by `margin_um` if given, else `margin_gaps` x (smallest conductor-to-ground gap), else - ("auto", default) by max(100 gaps, half the conductor extent, 150 µm), which keeps a training-like ground - neighbourhood (tighter crops cost accuracy: 20 gaps doubles the error, see deploy/LIMITS.md). None: no crop. + - ground: union of the ground-layer polygons. Default (margin_gaps=None, margin_um=None): no crop, the training + geometry (tlsq.uge.scene.scene_from_gds; TRAINING_PLAN Addendum R5; default since 2026-10-14). Optional crop to the + window = bbox(conductors + the etched pocket that contains them) expanded by `margin_um` if given, else `margin_gaps` + x (smallest conductor-to-ground gap), or ("auto") by max(100 gaps, half the conductor extent, 150 µm) (deploy/LIMITS.md). - select: optional list of conductor-body indices (0-based, file order), or select_box = (x0, y0, x1, y1) µm (bodies fully inside); the other bodies are not modelled and are flagged as extra metal when they fall inside the window. - metal on layers that are neither ground, conductor nor in `ignore_layers` is flagged when inside the window. diff --git a/tlsq_infer/localtok.py b/tlsq_infer/localtok.py new file mode 100644 index 0000000000000000000000000000000000000000..63eda9be8af482cc8c6eed25ab34b5076963a057 --- /dev/null +++ b/tlsq_infer/localtok.py @@ -0,0 +1,99 @@ +"""Local, window-invariant context tokens (PRIOR_SCHEME.md 14.5 E4; replace the chip / pocket areas): for every signal +conductor and radius R, the fractions of its surrounding annulus (conductor buffered by R, minus the conductor) that are +ground metal, other-conductor metal and etched chip; the rest of the annulus lies outside the chip (no metal). Pair +tokens = mean over the pair's signal ends (a ground pair: its signal end). Identical copy: tlsq.uge.localtok (training).""" + +from __future__ import annotations + +import numpy as np +import shapely +from scipy.spatial import cKDTree + +from ._vendor.blocks import MOMENTS, SEP_EDGES_UM, STEP_UM, _kratio +from ._vendor.geometry import sample_boundary + +RADII_UM = (50.0, 150.0, 400.0) +KEYS = [f"lt_{k}{int(r)}" for r in RADII_UM for k in ("g", "o", "e")] + + +def node_tokens(scene) -> np.ndarray: + """(n, len(KEYS)) per conductor (ground row = NaN).""" + conds = scene.conductors + n = len(conds) + G = next(c.geom for c in conds if c.role == "ground") + sig = [k for k, c in enumerate(conds) if c.role != "ground"] + tree = shapely.STRtree([conds[k].geom for k in sig]) + out = np.full((n, len(KEYS)), np.nan) + for k in sig: + g = conds[k].geom + vals = [] + for R in RADII_UM: + ring = g.buffer(R, quad_segs=4).difference(g) + A = ring.area + box = ring.bounds + gm = shapely.intersection(shapely.clip_by_rect(G, *box), ring).area + near = [sig[i] for i in tree.query(ring, predicate="intersects") if sig[i] != k] + om = shapely.intersection(shapely.union_all([conds[i].geom for i in near]), ring).area if near else 0.0 + inchip = shapely.intersection(scene.chip, ring).area + vals += [gm / A, om / A, max(inchip - gm - om, 0.0) / A] + out[k] = vals + return out + + +LG_R_UM = 100.0 +TOK = ["log_dmin"] + [f"sep_{k:02d}" for k in range(10)] + ["mom_0.5", "mom_1", "mom_2", "log_cps"] +LG_KEYS = [f"lg_{k}" for k in TOK] + + +def ground_tokens(scene) -> np.ndarray: + """(n, len(LG_KEYS)) per signal conductor: the ground-pair tokens of blocks.edge_features (separation spectrum, minimum + gap, coupling moments, coplanar-strip coupling) from the ground-edge samples within LG_R_UM of the conductor only + (fixed radius: independent of the ground window).""" + conds = scene.conductors + out = np.full((len(conds), len(LG_KEYS)), np.nan) + for k, c in enumerate(conds): + if c.role == "ground": + continue + g = c.geom + b = g.bounds + 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) + pj = sample_boundary(g, STEP_UM) + pg = sample_boundary(P, STEP_UM) if not P.is_empty else np.zeros((0, 2)) + if len(pg): + pg = pg[cKDTree(pj).query(pg)[0] < LG_R_UM] # drops the clip-box edges (>= LG_R_UM away) and the far ground + if not len(pg): + continue + wj = 2 * g.area / g.length + hist = np.zeros(len(SEP_EDGES_UM) - 1) + mom = np.zeros(len(MOMENTS)) + cps, dmin = 0.0, np.inf + for a_, b_ in ((pj, pg), (pg, pj)): + d = cKDTree(b_).query(a_)[0] + dmin = min(dmin, d.min()) + hist += 0.5 * np.histogram(d, bins=SEP_EDGES_UM, weights=np.full(len(d), STEP_UM))[0] + dd = np.maximum(d, 0.05) + mom += 0.5 * np.array([np.sum(STEP_UM * dd ** (-p)) for p in MOMENTS]) + cps += 0.5 * np.sum(STEP_UM * _kratio(dd / (dd + wj))) + 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))] + return out + + +def add_tokens(scene, row: dict) -> dict: + """Row (scene graph dict) with the KEYS added to every edge (shallow copy).""" + return add_from_tokens(row, node_tokens(scene), ground_tokens(scene), [c.role != "ground" for c in scene.conductors]) + + +def add_from_tokens(row: dict, T: np.ndarray, Lg: np.ndarray, sig) -> dict: + """Edges + KEYS (mean over the signal ends) + LG_KEYS (ground pairs: the fixed-radius tokens of the signal end; other + pairs: their own tokens, unchanged).""" + sig = np.asarray(sig) + edges = {} + for (i, j), f in row["edges"].items(): + ends = [e for e in (i, j) if sig[e]] + new = {k: float(x) for k, x in zip(KEYS, T[ends].mean(0))} + if len(ends) == 1 and np.all(np.isfinite(Lg[ends[0]])): + new.update({k: float(x) for k, x in zip(LG_KEYS, Lg[ends[0]])}) + else: + new.update({f"lg_{k}": float(f[k]) for k in TOK}) + edges[(i, j)] = {**f, **new} + return {**row, "edges": edges} diff --git a/tlsq_infer/ood.py b/tlsq_infer/ood.py new file mode 100644 index 0000000000000000000000000000000000000000..9252e42fad6153956a478060fc4a7aaea6db0cae --- /dev/null +++ b/tlsq_infer/ood.py @@ -0,0 +1,81 @@ +"""Out-of-distribution trust gate (GRID_SCALING.md amendment 4, PRIOR_SCHEME.md section 16-OOD). + +Per entry, z = the largest excess of a used feature beyond the training envelope (q0.1-q99.9), in IQR units (global +features; the pair's edge features for a mutual entry, the conductor's node features for a self entry), or the excess of +the seed spread of the network term beyond its training reference. trust a = exp(-(z / 3)^2) is 1 inside the training +envelope; the gated prediction is log P + a * f (f = network log-correction, P = physics prior). +""" + +from __future__ import annotations + +import json +from pathlib import Path + +import numpy as np + +TRUST_Z = 3.0 +WARN_TRUST = 0.5 + + +def find_stats(name: str, reg=None) -> Path | None: + """artifacts/ood/.json (training envelope + seed-spread reference) next to the package or in deploy/, or None.""" + here = Path(__file__).resolve() + for q in [here.parent] + list(here.parents): + for p in (q / "artifacts" / "ood" / f"{name}.json", q / "deploy" / "artifacts" / "ood" / f"{name}.json"): + if p.exists(): + return p + return None + + +def load_stats(name: str, reg=None) -> dict | None: + p = find_stats(name, reg) + return json.loads(p.read_text()) if p else None + + +def _excess(V: np.ndarray, iqr, lo, hi) -> np.ndarray: + """max over features of max(0, x - hi, lo - x) / IQR (NaN features ignored).""" + V, lo, hi, iqr = (np.asarray(a, float) for a in (V, lo, hi, iqr)) + ex = np.maximum(np.maximum(V - hi, lo - V), 0.0) / iqr + return np.nanmax(np.nan_to_num(ex, nan=0.0), axis=-1) + + +def trust(graph: dict, scaling: dict, stats: dict | None, f_seeds: np.ndarray | None) -> dict: + """graph: scene-graph row (nodes / edges / global); scaling: a member's scaling (keys, median / IQR); stats: the model's + OOD artifact (envelope when the checkpoint has none, seed_std_ref); f_seeds: (S, n, n) network term per seed. + Returns {"trust": (n, n), "z": (n, n), "z_global": float}; pairs without an edge get trust 1 (the prior is used).""" + nk, ek, gk = scaling["keys"] + env = (scaling.get("features") or {}).get("envelope") or (stats or {}).get("envelope") + n = len(graph["names"]) + z = np.zeros((n, n)) + if env is not None: + zu = float(_excess(np.array([graph["global"][k] for k in gk]), scaling["global"][1], *env["global"])) + Xn = np.array([[nd[k] for k in nk] for nd in graph["nodes"]], float) + zn = _excess(Xn, scaling["node"][1], *env["node"]) + z[np.arange(n), np.arange(n)] = zn + if graph["edges"]: + pairs = list(graph["edges"]) + En = np.array([[graph["edges"][e][k] for k in ek] for e in pairs], float) + ze = _excess(En, scaling["edge"][1], *env["edge"]) + ii, jj = np.array(pairs).T + z[ii, jj] = z[jj, ii] = ze + z = np.maximum(z, zu) + else: + zu = float("nan") + ref = (stats or {}).get("seed_std_ref") + if ref and f_seeds is not None and len(f_seeds) > 1: + sd = np.std(np.asarray(f_seeds)[:, :n, :n], 0) + r = np.full((n, n), float(ref["edge"])) + np.fill_diagonal(r, float(ref["node"])) + z = np.maximum(z, TRUST_Z * np.maximum(sd - r, 0.0) / r) + if graph["edges"]: + mask = np.zeros((n, n), bool) + ii, jj = np.array(list(graph["edges"])).T + mask[ii, jj] = mask[jj, ii] = True + np.fill_diagonal(mask, True) + z = np.where(mask, z, 0.0) + return {"trust": np.exp(-(z / TRUST_Z) ** 2), "z": z, "z_global": zu} + + +def gate(logP: np.ndarray, log_net: np.ndarray, a: np.ndarray) -> np.ndarray: + """log P + a * (log_net - log P).""" + return logP + a * (log_net - logP) diff --git a/tlsq_infer/predict.py b/tlsq_infer/predict.py index 39e83babc9e16581168eec3d77c40f5792d18583..31b519e33589dd2abf009c92f05a5de54602093f 100644 --- a/tlsq_infer/predict.py +++ b/tlsq_infer/predict.py @@ -18,6 +18,7 @@ from .layout import PreparedLayout from .registry import PRIORS, get_component, load_registry from .stack_error import EPS_TRAIN, eps_factor, expected_error, lateral_extent, load_params from . import errmodel +from .ood import WARN_TRUST def _internal_names(n_signal: int) -> list[str]: @@ -59,7 +60,7 @@ TIERS = ("fast", "coarse", "fine") # released tiers (2026-10-10): fast = flagsh # gap/3 conductor-anchored solve, fine = flagship prior (Richardson gap/6 + gap/3) TIER_ALIASES = {"fast": "fast", "coarse": "coarse", "fine": "fine", "accurate": "fine", "flagship-fast": "fast", "default": "fast"} TIER_KEYS = {"fast": "flagship_model", "coarse": "coarse_model", "fine": "flagship_accurate_model"} -CHEAP_PRIORS = ("lite500", "lite1500", "formula") # flagship-fast priors: windowed, cost bounded per conductor, never re-routed +CHEAP_PRIORS = ("lite500", "lite1500", "formula", "liteppg0.375", "liteppg0.75", "liteppg1.5", "liteppg3") # flagship-fast priors: windowed, cost bounded per conductor, never re-routed REGIME_MIN_CELLS_PER_GAP = 0.75 # training-family priors sit at 0.75-3 panels across the smallest gap (GRID_SCALING.md section 2) LARGE_LAYOUT_MODEL_KEY = "large_layout_model" # registry defaults: model (flagship prior) that large layouts are routed to @@ -143,7 +144,7 @@ def far_pairs(metals: list) -> np.ndarray: def run_pipeline(metals: list, names: list[str], model: str, reg=None, model_dir=None, timing=None, - blend_kinds: str | None = None) -> tuple[np.ndarray, dict]: + blend_kinds: str | None = None, ood_gate: bool = False) -> tuple[np.ndarray, dict]: """Core: log|C| matrix (diag: log C_ii, off-diag: log -C_ij) on the training stack, in `metals` order (0 = ground).""" reg = reg or load_registry() m = reg.model(model) @@ -171,14 +172,26 @@ def run_pipeline(metals: list, names: list[str], model: str, reg=None, model_dir gname = model if kind == "gnn" else gnn_name t = time.perf_counter() sc = make_scene(metals[0], metals[1:], names[1:]) - prior_fn = (lambda edges: prior_dict(mom["C"], mom["n_cells"], mom["h_min"], edges)) if mom is not None else None + prior_fn = (lambda edges: prior_dict(mom["C"], mom["n_cells"], mom["h_min"], edges, + (mom.get("mesh") or {}).get("min_gap_um"))) if mom is not None else None far = far_pairs(metals) if mom is not None and len(metals) - 1 > SPARSE_MIN_CONDUCTORS else None - g = scene_graph(sc, prior_fn, None if far is None else (lambda i, j: not far[i, j])) + from ._vendor import blocks as _blocks + + _blocks.set_token_variant(reg.model(gname).get("tokens", "exact")) # approximate token variants (PRIOR_SCHEME.md 14.3) + try: + g = scene_graph(sc, prior_fn, None if far is None else (lambda i, j: not far[i, j])) + finally: + _blocks.set_token_variant("exact") g["target"] = np.ones((len(metals), len(metals))) timing["features_s"] = time.perf_counter() - t t = time.perf_counter() ens = get_component(reg, gname, model_dir) timing["load_s"] = timing.get("load_s", 0.0) + time.perf_counter() - t + if any((m_sc.get("features") or {}).get("local") for _, m_sc in ens.members): # local context tokens (localtok, E4) + from .localtok import add_tokens + t = time.perf_counter() + g = add_tokens(sc, g) + timing["features_s"] = timing.get("features_s", 0.0) + time.perf_counter() - t t = time.perf_counter() seeds = ens.member_outputs([g], proxy_key="proxy_log_C" if mom is not None else None)[:, 0, : len(metals), : len(metals)] out_gnn = np.mean(seeds, 0) @@ -187,6 +200,8 @@ def run_pipeline(metals: list, names: list[str], model: str, reg=None, model_dir debug["far_mask"] = far timing["network_s"] = time.perf_counter() - t debug["seed_logC"] = seeds + if mom is not None: + out_gnn = ood_trust_gate(debug, out_gnn, seeds, _logC(mom["C"]), g, ens, gname, ood_gate) debug["graph"] = g if kind in ("charge_prior", "blend"): cname = model if kind == "charge_prior" else ch_name @@ -217,9 +232,32 @@ def run_pipeline(metals: list, names: list[str], model: str, reg=None, model_dir return out, debug +def ood_trust_gate(debug: dict, out_gnn: np.ndarray, seeds: np.ndarray, logP: np.ndarray, g: dict, ens, name: str, + apply: bool) -> np.ndarray: + """Per-entry network trust (GRID_SCALING.md amendment 4; tlsq_infer.ood): recorded in debug["ood_trust"] when the model + has a training envelope; apply=True returns log P + trust * (network - log P), else out_gnn unchanged.""" + from . import ood + + stats = ood.load_stats(name) + sc = ens.members[0][1] + if stats is None and not (sc.get("features") or {}).get("envelope"): + return out_gnn + k = out_gnn.shape[0] + if (sc.get("features") or {}).get("derive"): + from ._vendor.gnn import derive_features + g = derive_features(g) + t = ood.trust(g, sc, stats, np.asarray(seeds)[:, :k, :k] - logP[None]) + debug["ood_trust"], debug["ood_z"] = t["trust"], t["z"] + if not apply: + return out_gnn + debug["ood_gate_applied"] = True + return ood.gate(logP, out_gnn, t["trust"]) + + def predict(prepared: PreparedLayout, model: str | None = None, substrate="Si (cryogenic, training)", thickness_um: float = 350.0, self_from_rows: bool = False, registry=None, model_dir=None, return_debug: bool = False, - row_sum_eps: float | None = None, blend_kinds: str | None = None, large_layouts: str = "route") -> dict: + row_sum_eps: float | None = None, blend_kinds: str | None = None, large_layouts: str = "route", + ood_gate: bool = False) -> dict: """Maxwell capacitance matrix (fF) of a prepared layout. model: a deployable registry name, or a tier: "fast" (default: defaults.flagship_model, flagship-fast), "coarse" @@ -262,7 +300,7 @@ def predict(prepared: PreparedLayout, model: str | None = None, substrate="Si (c f"({reg.model(fb).get('title')}) instead") model, m = fb, reg.model(fb) try: - logC, debug = run_pipeline(metals, names, model, reg, model_dir, timing, blend_kinds) + logC, debug = run_pipeline(metals, names, model, reg, model_dir, timing, blend_kinds, ood_gate) except np.linalg.LinAlgError as e: # v3 MoM system not positive definite at gap/3 and gap/6 (e.g. long fine combs) fb = m.get("fallback") if not fb: @@ -270,7 +308,7 @@ def predict(prepared: PreparedLayout, model: str | None = None, substrate="Si (c fallback_note = (f"the {m.get('prior')} MoM prior of {model!r} failed on this layout ({e}); predicted with the fallback " f"model {fb!r} ({reg.model(fb).get('title')}) instead") timing.clear() - logC, debug = run_pipeline(metals, names, fb, reg, model_dir, timing, blend_kinds) + logC, debug = run_pipeline(metals, names, fb, reg, model_dir, timing, blend_kinds, ood_gate) k = len(metals) Cabs = np.exp(logC[:k, :k]) mesh = debug.get("prior_mesh") or {} @@ -347,6 +385,16 @@ def predict(prepared: PreparedLayout, model: str | None = None, substrate="Si (c "tier (`all-data-flagship`: resolution-controlled prior) or a smaller selection of conductors.") out["warnings"] += msg exp_err["mesh_warning"] = "; ".join(msg) if msg else None + tr = debug.get("ood_trust") + if tr is not None: + out["ood_trust"] = tr.tolist() + out["ood_gate"] = bool(debug.get("ood_gate_applied")) + lo = float(tr[np.triu_indices(k, 1)].min()) if k > 1 else 1.0 + if lo < WARN_TRUST: + out["warnings"].append(f"OUT OF THE TRAINING DISTRIBUTION: {int((tr[np.triu_indices(k, 1)] < WARN_TRUST).sum())} entries have " + f"network trust < {WARN_TRUST} (min {lo:.2f}; features beyond the training envelope, see ood_trust); " + + ("their network corrections are shrunk towards the physics prior (ood_gate)." if debug.get("ood_gate_applied") + else "predict(..., ood_gate=True) shrinks them towards the physics prior.")) fm = debug.get("far_mask") if fm is not None: out["far_mask"] = fm.tolist() diff --git a/tlsq_infer/registry.py b/tlsq_infer/registry.py index bfa07bdead17ca49631524095c3f0b4118ef4f17..c6212894be3fc35149401c7c157e9f15551a1a3c 100644 --- a/tlsq_infer/registry.py +++ b/tlsq_infer/registry.py @@ -21,7 +21,12 @@ PRIORS = {"standard": dict(max_cells=8000, cells_per_gap=3.0), "fine": dict(max_ # flagship-fast cheap priors (PRIOR_SCHEME.md Part III): micro-MoM at a merged-panel budget (one window up to 8 # conductors, core-graded windows beyond) and the closed-form edge-coupling prior "lite500": dict(mesh="lite", max_cells=500, cells_per_gap=3.0), "lite1500": dict(mesh="lite", max_cells=1500, cells_per_gap=3.0), - "formula": dict(mesh="formula")} + "formula": dict(mesh="formula"), + # resolution-controlled lite priors (PRIOR_SCHEME.md 14.5 E1): no panel budget, finest panel = smallest gap / ppg + **{f"liteppg{x:g}": dict(mesh="lite", max_cells=10**9, cells_per_gap=x) for x in (0.375, 0.75, 1.5, 3.0)}} +for _k in list(PRIORS): # 2026-10-10: the slab kernel is the physics of every prior (PRIOR_SCHEME.md 14.6); the existing names keep + PRIORS[_k] = {**PRIORS[_k], "substrate": "halfspace"} # the half-space kernel their networks were trained on + PRIORS[f"{_k}-slab"] = {**PRIORS[_k], "substrate": "slab_open"} def find_registry(path=None) -> Path: @@ -93,6 +98,10 @@ def export_checkpoint(src, dst) -> dict: | {"keys": [list(x) for x in sc["keys"]]}, "dims": list(st["dims"]), "w": int(st["w"]), "rounds": int(st["rounds"]), "variant": st.get("variant", ""), "prior": st.get("prior", ""), "n_train": int(st.get("n_train", 0))} + if sc.get("features"): # dropped columns, clip mode and training envelope (GRID_SCALING.md amendment 4) + f = sc["features"] + out["scaling"]["features"] = {"drop": list(f.get("drop", [])), "clip": f.get("clip", "iqr20"), + "envelope": {p: [np.asarray(v, float).tolist() for v in f["envelope"][p]] for p in ("node", "edge", "global")}} torch.save(out, dst) return {k: out[k] for k in ("dims", "w", "rounds", "variant", "prior", "n_train")} @@ -106,6 +115,8 @@ def _load_ckpt(path): st = torch.load(path, map_location="cpu", weights_only=False) sc = st["scaling"] st["scaling"] = {p: (np.asarray(sc[p][0], float), np.asarray(sc[p][1], float)) for p in ("node", "edge", "global")} | {"keys": sc["keys"]} + if sc.get("features"): + st["scaling"]["features"] = sc["features"] return st @@ -133,6 +144,43 @@ def resolve_files(reg: Registry, name: str, model_dir=None) -> list[str]: raise FileNotFoundError(f"no weights for {name!r}: looked in {model_dir}, {local}, hub repo {m.get('repo_id')}") +def _pair_local_sparse(net, scaling: dict, row: dict, proxy_key: str) -> np.ndarray: + """Pair-local (R = 0) network on one graph evaluated on its edges only (large layouts with sparse graphs): the same + MLPs and scaling as SceneGNN.forward on the dense padded batch, without the N x N edge tensor. Returns (1, N, N) log|C|; + pairs without an edge get the prior's value (predict replaces them by the prior anyway).""" + import torch + + from ._vendor.gnn import derive_features, scale_features + + if (scaling.get("features") or {}).get("derive"): + row = derive_features(row) + nk, ek, gk = scaling["keys"] + sc = lambda A, p: scale_features(A, scaling[p], p, scaling) # noqa: E731 + n = len(row["names"]) + pairs = list(row["edges"]) + X = sc(np.array([[nd[k] for k in nk] for nd in row["nodes"]], float), "node") + E = sc(np.array([[row["edges"][e][k] for k in ek] for e in pairs], float), "edge") + U = sc(np.array([[row["global"][k] for k in gk]], float), "global") + P = np.zeros((n, n)) + for i, nd in enumerate(row["nodes"]): + P[i, i] = nd.get("proxy_log_Cself", 0.0) if proxy_key == "proxy_log_C" else 0.0 + for (i, j), f in row["edges"].items(): + P[i, j] = P[j, i] = f.get(proxy_key, 0.0) if proxy_key else 0.0 + t = lambda a: torch.tensor(a, dtype=torch.float32) # noqa: E731 + with torch.no_grad(): + u = net.enc_u(t(U)) + oe = net.head_e(torch.cat([net.enc_e(t(E)), u.expand(len(pairs), -1)], -1))[:, 0].numpy() + on = net.head_n(torch.cat([net.enc_n(t(X)), u.expand(n, -1)], -1))[:, 0].numpy() + N = max(8, n) + out = np.zeros((1, N, N)) + out[0, :n, :n] = P + ii, jj = np.array(pairs).T + out[0, ii, jj] += oe + out[0, jj, ii] += oe + out[0, np.arange(n), np.arange(n)] += on + return out + + class Ensemble: """Mean of log C over the seeds of one GNN model.""" @@ -152,6 +200,8 @@ class Ensemble: """Per-seed predictions (S, B, N_MAX, N_MAX); the error model uses their spread.""" from ._vendor.gnn import build_graphset, predict + if self.rounds == 0 and len(rows) == 1 and len(rows[0]["names"]) > 8 and not any(getattr(n, "gate", False) for n, _ in self.members): + return np.asarray([_pair_local_sparse(net, sc, rows[0], proxy_key) for net, sc in self.members]) out = [] for net, sc in self.members: gs, _ = build_graphset(rows, scaling=sc, proxy_key=proxy_key)