Improve routing quality with bounded cost refinement

This commit is contained in:
Jan Petykiewicz 2026-12-05 12:25:58 -08:00
commit db685794e5

View file

@ -19,14 +19,17 @@ from inire import NetSpec, ObjectiveWeights, Port, RefinementOptions, RoutingOpt
from inire._catalog import (
HEADING_STEPS, BendVariant, Catalog, FixedVariant, Grid, Move, MoveEvaluator, Orientation, Pose, Profile, StraightVariant,
)
from inire._catalog_timing import DelayModel
from inire._catalog_timing import DelayModel, evaluate_delay
from inire.router._catalog_guidance import AutomaticGrid, CoarseGuidance
from inire.router._catalog_layout import CatalogNet, LayoutSettings, route_layout
from inire.router._catalog_matching import MatchGroup
from inire.router._catalog_matching import DelayChoice, MatchGroup, choose_match
from inire.router._catalog_verify import replay_moves, verify_path
from scripts.catalog_floorplan import BLOCKS, COMPONENTS, NET_BANKS, STEPPED_BLOCKS
PROFILE_HOLDOUTS = ("peer-cost-holdout", "matched-cost-holdout")
@dataclass(frozen=True, slots=True)
class ScaleCase:
grid: Grid
@ -38,6 +41,45 @@ class ScaleCase:
groups: tuple[MatchGroup, ...] = ()
windows: dict[str, Polygon] = field(default_factory=dict)
clearance: float = 1000.0
cheaper_references: dict[str, tuple[Move, ...]] | None = None
def profile_holdout(matched: bool) -> ScaleCase:
"""Two nearby lanes compete for a cheaper profile beside a long keepout."""
grid = Grid((0, 0), 100, 1e-9)
lanes = {"bypass": 975_000, "through": 1_000_000}
if matched:
lanes["reference"] = 1_150_000
nets = {name: CatalogNet(Pose(200_000, y, 0, "4"), Pose(4_800_000, y, 0, "4")) for name, y in lanes.items()}
enter_wide = Move("centered", traversal=("a", "b"))
exit_wide = Move("centered", traversal=("b", "a"), orientation=Orientation(180))
widen = (enter_wide, Move("5-straight", steps=45_800), exit_wide)
left_up = Move("left-90", displacement=(10_000, 10_000))
left_down = Move("left-90", displacement=(10_000, -10_000))
right_up = Move("right-90", displacement=(10_000, 10_000))
right_down = Move("right-90", displacement=(10_000, -10_000))
references = {
"bypass": (left_up, right_up, Move("4-straight", steps=45_600), right_down, left_down),
"through": (Move("4-straight", steps=46_000),),
}
cheaper = {
"bypass": (
right_down, Move("4-straight", steps=600), left_down,
enter_wide, Move("5-straight", steps=45_400), exit_wide,
left_up, Move("4-straight", steps=600), right_up,
),
"through": widen,
}
timing = None
groups = ()
if matched:
references["reference"] = cheaper["reference"] = widen
timing = DelayModel(1.55e-6, group_indices={"4": 4, "5": 4}, variant_delays={"centered": 40_000})
groups = (MatchGroup("delay", {"through": 0, "reference": 0}, timing.wavelength, 0),)
return ScaleCase(
grid, box(0, 0, 5_000_000, 1_200_000), nets, (box(400_000, 900_000, 4_600_000, 990_000),),
references, timing, groups, cheaper_references=cheaper,
)
def timing_case(name: str) -> ScaleCase:
@ -133,6 +175,8 @@ def scale_case(name: str) -> ScaleCase:
return floorplan_case()
if name in ("tuning", "retry"):
return timing_case(name)
if name in PROFILE_HOLDOUTS:
return profile_holdout(matched=name == "matched-cost-holdout")
if name in ("corpus-lanes", "corpus-open-lanes"):
return corpus_lanes(name)
grid = Grid((0, 0), 100, 1e-9)
@ -187,7 +231,7 @@ def catalog(mode: str, case: str) -> Catalog:
(Polygon(((0, -2000), (10_000, -2500), (10_000, 2500), (0, 2000))),),
(("a", "b"), ("b", "a")), (Orientation(), Orientation(180)), 0.01, 10_000,
)
if case in ("floorplan", "constrained", "diagonal-savings", "tuning"):
if case in ("floorplan", "constrained", "diagonal-savings", "tuning", *PROFILE_HOLDOUTS):
variants["left-90"] = BendVariant("4", 90, 10_000, 0.01, sagitta=30)
variants["right-90"] = BendVariant("4", -90, 10_000, 0.01, sagitta=30)
if mode == "octagonal":
@ -203,10 +247,12 @@ def catalog(mode: str, case: str) -> Catalog:
return Catalog("scale", profiles, variants)
def verify_references(case: ScaleCase, technology: Catalog) -> None:
def _reference_cost(case: ScaleCase, technology: Catalog, recipes: dict[str, tuple[Move, ...]]) -> float:
if recipes.keys() != case.nets.keys():
raise AssertionError("Reference layout must route every net")
evaluator = MoveEvaluator(case.grid, technology)
placed = {}
for name, recipe in case.references.items():
for name, recipe in recipes.items():
net = case.nets[name]
path = replay_moves(evaluator, net.start, recipe)
if path is None:
@ -220,6 +266,28 @@ def verify_references(case: ScaleCase, technology: Catalog) -> None:
)
if error is not None:
raise AssertionError(f"Invalid reference for {name}: {error}")
if case.cheaper_references is not None and case.timing is not None:
for group in case.groups:
timing = replace(case.timing, wavelength=group.wavelength)
choices = {}
for name in group.offsets:
delay = evaluate_delay(evaluator, placed[name], timing, net_id=name)
if delay is None:
raise AssertionError(f"Missing reference delay for {name}")
choices[name] = (DelayChoice(placed[name], delay),)
if not choose_match(group, choices)[2]:
raise AssertionError(f"Unmatched reference group {group.group_id}")
return fsum(piece.cost for path in placed.values() for piece in path)
def verify_references(case: ScaleCase, technology: Catalog) -> dict[str, float]:
cost = _reference_cost(case, technology, case.references)
if case.cheaper_references is None:
return {}
cheaper = _reference_cost(case, technology, case.cheaper_references)
if cheaper >= cost:
raise AssertionError("Cheaper reference must lower the total cost")
return {"original": cost, "cheaper": cheaper}
def bank_order_key(net: CatalogNet) -> tuple[int, int, bool, int, int]:
@ -338,7 +406,7 @@ def main() -> None:
"--case", default="floorplan", help="routing fixture (default: floorplan)",
choices=(
"floorplan", "aligned", "constrained", "diagonal-savings", "diagonal", "off-coarse", "tuning", "retry",
"corpus-lanes", "corpus-open-lanes",
"corpus-lanes", "corpus-open-lanes", *PROFILE_HOLDOUTS,
),
)
parser.add_argument("--guide-pitch", type=int, help="coarse pitch in nanometers")
@ -358,6 +426,8 @@ def main() -> None:
parser.error("--native-jog-limit requires native mode and a nonnegative limit")
if args.case == "diagonal" and args.mode != "octagonal":
parser.error("The native-diagonal terminal case requires octagonal mode")
if args.case in PROFILE_HOLDOUTS and args.mode not in ("typed", "octagonal"):
parser.error("The profile holdouts require typed or octagonal mode")
if args.mode == "native" and (
args.guide_pitch is not None or args.auto_pitch is not None
or args.case not in ("floorplan", "constrained", "aligned", "diagonal-savings", "corpus-lanes", "corpus-open-lanes")
@ -365,7 +435,7 @@ def main() -> None:
parser.error("Native mode needs a case with cardinal terminals on the explicit grid")
case = scale_case(args.case)
technology = catalog(args.mode, args.case)
verify_references(case, technology)
reference_costs = verify_references(case, technology)
if args.net_order is None:
args.net_order = "bank" if args.case == "floorplan" else "input"
if args.net_order == "bank":
@ -374,7 +444,7 @@ def main() -> None:
if args.mode == "native" and args.native_jog_limit is None:
args.native_jog_limit = 48 if args.case == "floorplan" else 0
if args.constructive is None:
args.constructive = args.case == "floorplan" and args.mode != "native"
args.constructive = args.case in ("floorplan", *PROFILE_HOLDOUTS) and args.mode != "native"
samples = []
measurements = {}
for _ in range(args.repeat):
@ -386,6 +456,7 @@ def main() -> None:
"options": vars(args), "machine": platform.machine(), "python": platform.python_version(), "requested": len(case.nets),
"wall_seconds": samples, "median_seconds": elapsed, "peak_rss": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
"catalog_cost": total_cost, "time_cost_squared": elapsed * total_cost**2 if measurements["completed"] else None,
**({"reference_costs": reference_costs} if reference_costs else {}),
**measurements,
}, indent=2))