Loading files
#!/usr/bin/env python3.12
"""Measure spatial decomposition against the relay, on one ruler.
Four arms, same benchmark, same budget, same seed. The arms exist to separate
two effects that a single "spatial vs relay" number would confound:
``relay``
The number to beat: ``routerlib.portfolio`` in relay mode. Re-measured
here rather than quoted, because the published 98.0% was taken against
ruler ``b3c77d55b171`` — the inscribed-stadium pad model — and a score is
only comparable to a score taken with the same ruler.
``spatial``
Crossings first, then each region to the expert its character names.
``spatial-flat``
The identical partition and the identical order, every stage routed by the
global router. The difference between this and ``spatial`` is what the
expert *table* is worth; the difference between this and ``relay`` is what
the crossing-first *ordering* is worth.
``spatial-residue``
``spatial`` plus the relay's follower chain on whatever is left, so the
comparison with ``relay`` is four routers against four routers.
python3.12 packages/router/scripts/spatial_suite.py --arm spatial \
--out work/spatial/spatial.json
"""
from __future__ import annotations
import argparse
import json
import subprocess
import sys
import time
from pathlib import Path
HERE = Path(__file__).resolve().parent
PACKAGE = HERE.parent
for extra in (PACKAGE / "src", PACKAGE.parent / "circuitpy" / "src"):
if str(extra) not in sys.path:
sys.path.insert(0, str(extra))
ARMS = (
# The floor: one router on the whole board, chosen by the selector's rules.
# Without it, "crossing-first ordering helps" cannot be told apart from
# "four routers help", because spatial-flat is one router in stages.
"single",
"relay",
"spatial",
"spatial-flat",
"spatial-residue",
# Two more, each changing exactly one thing against ``spatial``:
# ``spatial-tight`` cuts harder (a split must isolate twice what it cuts),
# ``spatial-chain`` retries a failed crossing net before the regions take
# the space rather than after.
"spatial-tight",
"spatial-chain",
# Deliberately contradicts the crossings-first rule, for the one region
# class where both routers fail: a 0.4mm escape has one or two channels
# out, and a crossing net can detour round the whole package.
"spatial-escape-first",
# Everything the single-variable arms argued for, at once: the constant
# expert map, escapes before crossings, and the relay's chain on the
# residue. Four routers, so it is the like-for-like against ``relay``.
"spatial-best",
# The control that decides whether the *geometry* of the seam matters at
# all: the same number of groups, the same group sizes, the same staging,
# nets assigned to groups by a deterministic shuffle instead of by
# position. If this scores like ``spatial-flat``, the partition is doing
# nothing and only the staging is visible in the number.
"spatial-shuffled",
)
#: ``spatial-tight``'s partition. Measured on the fixtures with no routing:
#: interior share over the 16 instances is 61.8% at the default and 76.8%
#: here, at 40 regions against 65.
TIGHT = {"max_depth": 2, "max_cut_ratio": 0.5}
#: The relay's chain, so ``spatial-residue`` and ``relay`` run four routers each.
FOLLOWERS = ("maze-astar", "plane-and-classes", "exact-and-structured")
#: The region-expert table this module was built to test, kept as an arm after
#: it was refuted. Every arm names its expert map explicitly rather than
#: inheriting ``spatial.EXPERTS``, so a change to the module default cannot
#: silently redefine what an arm measured.
HYPOTHESIS_EXPERTS = {
"lattice": "exact-and-structured",
"fine-pitch": "exact-and-structured",
"open": "pathfinder-negotiated",
}
def _git(*args: str) -> str:
try:
return subprocess.run(
["git", *args], cwd=PACKAGE, capture_output=True, text=True, check=True
).stdout.strip()
except Exception: # noqa: BLE001 - provenance is best effort, never a gate
return "unknown"
def ruler_hash() -> str:
from routerlib.model import DesignRules
from routerlib.scoring import ruler_for
return ruler_for(DesignRules.jlcpcb()).hash
def uniquified(solution):
"""The same copper with every id made unique.
Geometry untouched, so this is the *honest* score of the same board.
:mod:`routerlib.connectivity` unions ``(copper id, layer)`` nodes and
:mod:`routerlib.drc` skips a pair when the two ids match, so two stages
that both mint ``v0`` are one node carrying two nets — a connection that
does not exist and a short nobody checked. Scoring a solution twice, once
as returned and once like this, prices exactly that.
"""
import dataclasses
return dataclasses.replace(
solution,
traces=tuple(dataclasses.replace(t, id=f"{i}!{t.id}")
for i, t in enumerate(solution.traces)),
vias=tuple(dataclasses.replace(v, id=f"{i}!{v.id}")
for i, v in enumerate(solution.vias)),
)
def shuffled(part, seed: int = 0):
"""The same partition shape with the nets dealt out by hash, not position.
Region ids, region count, interior-net counts and the crossing count are
all preserved exactly; only *which* net lands in which group changes. It is
the null hypothesis for the whole module — a spatial decomposition whose
seams carry no information scores the same as this.
"""
import dataclasses
import hashlib
every = sorted(
[n for r in part.regions for n in r.interior_nets] + list(part.crossing_nets)
)
order = sorted(
every,
key=lambda n: hashlib.sha256(f"{seed}:{n}".encode()).hexdigest(),
)
at = 0
crossing = tuple(sorted(order[: len(part.crossing_nets)]))
at = len(part.crossing_nets)
regions = []
for region in part.regions:
take = len(region.interior_nets)
regions.append(
dataclasses.replace(region, interior_nets=tuple(sorted(order[at: at + take])))
)
at += take
return dataclasses.replace(
part,
regions=tuple(regions),
crossing_nets=crossing,
why=part.why + f" [nets shuffled, seed {seed} — control]",
)
def run_one(arm: str, problem, budget, registry):
"""One instance, one arm. Returns ``(solution, detail_dict)``."""
from routerlib import portfolio
from routerlib.compositions import spatial
if arm in ("relay", "single"):
result = portfolio.route(
problem, budget, registry,
budget_class="thorough" if arm == "relay" else "cheap",
mode=arm if arm == "single" else "relay",
)
return result.solution, {"portfolio": result.as_dict()}
# Every spatial arm is explicit about its expert map. ``spatial`` is the
# refuted hypothesis; everything else routes every region with the global
# router, which is what the A/B said to do.
# Every spatial arm names both knobs the module has a measured opinion
# about, so a change to a module default cannot silently redefine what an
# arm measured. ``spatial`` carries the refuted hypothesis in both.
options: dict = {
"experts": HYPOTHESIS_EXPERTS if arm == "spatial" else {},
"escape_first": arm in ("spatial-escape-first", "spatial-best"),
}
if arm == "spatial-residue":
options["residue"] = FOLLOWERS
if arm == "spatial-tight":
options["partition_kwargs"] = TIGHT
if arm == "spatial-chain":
options["crossing_chain"] = FOLLOWERS
if arm == "spatial-best":
options["residue"] = FOLLOWERS
if arm == "spatial-shuffled":
options["given"] = shuffled(
spatial.partition(problem, experts={}), seed=budget.seed
)
result = spatial.route(problem, budget, registry, **options)
return result.solution, {"spatial": result.as_dict()}
def main(argv: list[str] | None = None) -> int:
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--arm", required=True, choices=ARMS)
ap.add_argument("--only", default=None, help="comma-separated instance ids")
ap.add_argument("--out", default=None)
ap.add_argument("--runs", type=int, default=1,
help="repeat each instance and compare fingerprints")
ap.add_argument("--max-iterations", type=int, default=2_000_000)
ap.add_argument("--max-nodes", type=int, default=20_000_000)
ap.add_argument("--seed", type=int, default=0)
args = ap.parse_args(argv)
from routerlib.bench import INSTANCE_DIR, load_instance
from routerlib.compositions.registry import load_algorithms
from routerlib.drc import copper_items
from routerlib.model import Budget
from routerlib.scoring import score
paths = sorted(Path(INSTANCE_DIR).glob("*.json"))
if args.only:
wanted = set(args.only.split(","))
paths = [p for p in paths if p.stem in wanted]
registry = load_algorithms()
budget = Budget(max_iterations=args.max_iterations,
max_nodes=args.max_nodes, seed=args.seed)
ruler_before = ruler_hash()
rows = []
for path in paths:
problem = load_instance(path)
started = time.perf_counter()
solution, detail = run_one(args.arm, problem, budget, registry)
seconds = time.perf_counter() - started
scored = score(problem, solution)
fingerprints = [solution.fingerprint()]
for _ in range(args.runs - 1):
again, _ = run_one(args.arm, problem, budget, registry)
fingerprints.append(again.fingerprint())
ids = [item.id for item in copper_items(problem, solution)]
honest = score(problem, uniquified(solution))
row = {
"instance": problem.id,
"arm": args.arm,
"wallClockS": round(seconds, 3),
# ``null``, not ``true``, when the instance was routed once. A
# determinism verdict from a single run is not a measurement, and
# a column of vacuous ``true`` reads exactly like a checked one.
"deterministic": (len(set(fingerprints)) == 1) if args.runs > 1 else None,
"runs": args.runs,
"fingerprints": fingerprints,
# A colliding copper id is a false connection and an unchecked
# short at once, so it is counted beside the score rather than
# assumed away, and the same copper is scored again with unique
# ids to price what the collisions were worth.
"collidingCopperIds": len(ids) - len(set(ids)),
"score": scored.as_dict(),
"scoreWithUniqueIds": {
"completeness": round(honest.completeness, 6),
"connectedNets": honest.connected_nets,
"errors": honest.errors,
"warnings": honest.warnings,
"clean": honest.clean,
},
**detail,
}
rows.append(row)
extra = ""
if row["collidingCopperIds"]:
extra += (
f" {row['collidingCopperIds']} COLLIDING IDS "
f"(honest: {honest.completeness * 100:.1f}% / {honest.errors} err)"
)
if row["deterministic"] is False:
extra += " NONDETERMINISTIC"
print(scored.line() + extra, flush=True)
if args.arm.startswith("spatial"):
part = detail["spatial"]["partition"]
print(f" partition: {len(part['regions'])} regions, "
f"{part['interiorNets']}/{part['routableNets']} interior, "
f"seam={part['seam']}", flush=True)
for stage in detail.get("spatial", detail.get("portfolio", {})).get("stages", []):
label = stage.get("stage", stage.get("router"))
print(f" {label:<16} {stage['router']:<24} "
f"asked={stage['asked_nets']:>3} +{stage['added_nets']:>3} "
f"-> {stage['completeness'] * 100:5.1f}% ({stage['seconds']:.1f}s)",
flush=True)
ruler_after = ruler_hash()
mean = sum(r["score"]["completeness"] for r in rows) / max(1, len(rows))
honest_mean = sum(
r["scoreWithUniqueIds"]["completeness"] for r in rows
) / max(1, len(rows))
summary = {
"arm": args.arm,
"instances": len(rows),
"meanCompleteness": round(mean, 4),
"meanCompletenessWithUniqueIds": round(honest_mean, 4),
"cleanInstances": sum(1 for r in rows if r["score"]["clean"]),
"cleanInstancesWithUniqueIds": sum(
1 for r in rows if r["scoreWithUniqueIds"]["clean"]
),
"harnessErrors": sum(r["score"]["errors"] for r in rows),
"harnessErrorsWithUniqueIds": sum(
r["scoreWithUniqueIds"]["errors"] for r in rows
),
"deterministic": (sum(1 for r in rows if r["deterministic"])
if args.runs > 1 else None),
"runs": args.runs,
"collidingCopperIds": sum(r["collidingCopperIds"] for r in rows),
"totalSeconds": round(sum(r["wallClockS"] for r in rows), 1),
}
print("\n" + json.dumps(summary))
payload = {
"schema": "routerlib/spatial-suite@1",
"measuredAgainst": {
"rulerHashBefore": ruler_before,
"rulerHashAfter": ruler_after,
"rulerStable": ruler_before == ruler_after,
"gitHead": _git("rev-parse", "--short", "HEAD"),
"gitDirty": bool(_git("status", "--porcelain")),
},
"budget": {"maxIterations": args.max_iterations,
"maxNodes": args.max_nodes, "seed": args.seed},
"summary": summary,
"rows": rows,
}
if ruler_before != ruler_after:
print(f"WARNING: the ruler moved mid-run, {ruler_before} -> {ruler_after}; "
f"this run is not a measurement", file=sys.stderr)
if args.out:
out = Path(args.out)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(payload, indent=1) + "\n", encoding="utf-8")
print(f"wrote {out}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
11b8da784This file is pinned to the selected release. Download the original to open it in your design tool.