Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def settle_assignments(
available,
backend,
preferred_boundaries=preferred_boundaries,
).settle(target)
).settle((target,))


def settle_assignments_many(
Expand All @@ -84,25 +84,17 @@ def settle_assignments_many(
Keep established intermediate values and child routes within one settlement
rather than starting a fresh dependency search for each target.
"""
settlement = _TargetSettlement(assignments, available, backend)
for target in targets:
settlement.settle(target)
return (
tuple(settlement.steps[name] for name in sorted(settlement.steps)),
frozenset((settlement.requested & assignments.keys()) - settlement.steps.keys()),
)
return _TargetSettlement(assignments, available, backend).settle(targets)


def _assignment_step(
operation: BiltModelConstituent,
def _lookup_child_step(
operation: SubSystemConstituent,
output: str,
available: collections.abc.Set[str],
boundary: collections.abc.Set[str],
backend: Backend,
) -> PlanStep | None:
if not isinstance(operation, SubSystemConstituent):
return PlanStep(operation=operation, output=output)

candidate_ports = (set(operation.__bilt_parameters__) & available) - {output}
"""Find a child step within the offered boundary, excluding the target."""
candidate_ports = (set(operation.__bilt_parameters__) & boundary) - {output}
child_inputs = operation.required_inputs_for(output, backend, within=candidate_ports)
if child_inputs is None:
return None
Expand Down Expand Up @@ -133,7 +125,16 @@ def __init__(
self.stalled_at: dict[str, int] = {}
self.cycle_visits = 0

def settle(self, output: str) -> tuple[tuple[PlanStep, ...], frozenset[str]]:
def settle(self, targets: tuple[str, ...]) -> tuple[tuple[PlanStep, ...], frozenset[str]]:
"""Settle targets in order and report the accumulated steps and rejected outputs."""
for target in targets:
self._settle_target(target)
return (
tuple(self.steps[name] for name in sorted(self.steps)),
frozenset((self.requested & self.assignments.keys()) - self.steps.keys()),
)

def _settle_target(self, output: str) -> None:
"""Revisit stalled dependencies whenever another value was established."""
while output not in self.established:
before = len(self.established)
Expand All @@ -147,10 +148,6 @@ def settle(self, output: str) -> tuple[tuple[PlanStep, ...], frozenset[str]]:
pending.append(self._resolve(dependency))
if len(self.established) == before:
break
return (
tuple(self.steps[name] for name in sorted(self.steps)),
frozenset((self.requested & self.assignments.keys()) - self.steps.keys()),
)

def _resolve(self, attribute: str) -> collections.abc.Iterator[str]:
if attribute in self.active:
Expand Down Expand Up @@ -189,35 +186,37 @@ def _resolve(self, attribute: str) -> collections.abc.Iterator[str]:
def _child_step(
self, operation: SubSystemConstituent, output: str
) -> collections.abc.Generator[str, None, PlanStep | None]:
# These directions come from the expanded model. The relevant values
# can still be pending on a parent-local coupled block.
"""Try preferred, established and finally speculative child boundaries."""
# Preferred directions can depend on a pending parent-local coupled block.
preferred = None if self.preferred_boundaries is None else self.preferred_boundaries.get(output)
if preferred is not None and preferred <= self.candidates:
yield from sorted(preferred)
step = _assignment_step(operation, output, preferred | self.fixed, self.backend)
step = _lookup_child_step(operation, output, preferred | self.fixed, self.backend)
if step is not None:
return step
ports = sorted((set(operation.__bilt_parameters__) & self.candidates) - {output})
# Establish parent-produced boundary inputs together before asking the
# child to search with an unnecessarily incomplete set of supplied ports.
yield from self._settle_parent_inputs(ports)
step = self._child_route(operation, output)
step = self._established_child_step(operation, output)
if step is not None:
return step
# Establish additional child-produced ports, retrying only on progress.
for port in ports:
if not isinstance(self.assignments.get(port), SubSystemConstituent):
continue
before = len(self.established)
yield port
if len(self.established) != before:
step = _assignment_step(operation, output, self.established, self.backend)
step = _lookup_child_step(operation, output, self.established, self.backend)
if step is not None:
return step
# The initial probe must defer speculative boundaries to the full search.
if self.defer_child_boundary and not self.established.issuperset(ports):
raise DeferredChildBoundaryError
# Preserve cyclic/unresolved candidates for explicit checks, without
# Retain cyclic/unresolved candidates for explicit checks without
# claiming their dependencies have been established.
return _assignment_step(operation, output, self.candidates, self.backend)
return _lookup_child_step(operation, output, self.candidates, self.backend)

def _settle_parent_inputs(self, ports: list[str]) -> collections.abc.Iterator[str]:
for port in ports:
Expand All @@ -227,14 +226,14 @@ def _settle_parent_inputs(self, ports: list[str]) -> collections.abc.Iterator[st
if self.defer_child_boundary and port not in self.established:
raise DeferredChildBoundaryError

def _child_route(self, operation: SubSystemConstituent, output: str) -> PlanStep | None:
def _established_child_step(self, operation: SubSystemConstituent, output: str) -> PlanStep | None:
"""Avoid constraining a child with its own already-computed sibling outputs.

Prefer the ordinary route. Only if the child's requested route is
structurally overconstrained do we retry without outputs produced by
this same child in the parent. Explicitly supplied ports stay fixed.
"""
step = _assignment_step(operation, output, self.established, self.backend)
step = _lookup_child_step(operation, output, self.established, self.backend)
if step is None or step.child_inputs is None:
return step

Expand All @@ -254,7 +253,7 @@ def _child_route(self, operation: SubSystemConstituent, output: str) -> PlanStep
operation.child_port(output),
)
except BiltOverconstrainedError:
reduced = _assignment_step(operation, output, self.established - sibling_outputs, self.backend)
reduced = _lookup_child_step(operation, output, self.established - sibling_outputs, self.backend)
if reduced is not None and reduced.child_inputs is not None:
try:
validate_request_structure(
Expand Down
4 changes: 2 additions & 2 deletions tests/core/structural/scc/test_settle_assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,12 @@ def expose(answer: float, child: Child) -> bool:
PYTHON_BACKEND,
preferred_boundaries={"child.c": frozenset({"child.a"})},
)
steps, rejected = trial.settle("answer")
steps, rejected = trial.settle(("answer",))
assert not rejected
assert {step.output for step in steps} == {"answer", "child.a", "child.c"}
assert not trial.stalled_at
cycle_visits = trial.cycle_visits
trial.settle("child.c")
trial.settle(("child.c",))
assert trial.cycle_visits > cycle_visits


Expand Down
Loading