Skip to content
Closed
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 @@ -27,6 +27,7 @@
)
from bilt.core._structural_analysis._causalization_block_solver._structural_cache import structural_cache_scope
from bilt.core._structural_analysis._causalization_block_solver._structural_validation import (
shares_equation_scope,
validate_request_structure,
)
from bilt.core._structural_analysis._linear_block import BiltLinearBlockError
Expand Down Expand Up @@ -61,6 +62,7 @@ def __init__(
self.backend = backend
self.full_incidence_graph = FullIncidenceGraph(constituents)
self._failures: dict[PlanStep, RealizationFailure] = {}
self._prepared_scopes: list[tuple[frozenset[str], str, ExpansionSnapshot]] = []

def analyse(self, request: EvaluationRequest) -> BackendCase:
"""Find and realise a plan, sharing request caches across nested analysis."""
Expand All @@ -70,11 +72,7 @@ def analyse(self, request: EvaluationRequest) -> BackendCase:
def _analyse(self, request: EvaluationRequest) -> BackendCase:
"""Search within the current request's cache scopes."""
available = set(request.input_domains)
structure = validate_request_structure(
self.full_incidence_graph,
available,
request.output,
)
structure = self._validated_structure(available, request.output)
expanded = self._preflight(request, structure)
if expanded is not None:
return expanded
Expand Down Expand Up @@ -129,6 +127,24 @@ def _analyse(self, request: EvaluationRequest) -> BackendCase:

raise BiltExecutionPlanError(msg)

def _validated_structure(self, available: set[str], output: str) -> ValidatedStructure | None:
supplied = frozenset(available)
prepared = next(
(
snapshot
for inputs, original, snapshot in reversed(self._prepared_scopes)
if inputs == supplied and shares_equation_scope(snapshot, original, output)
),
None,
)
structure = validate_request_structure(self.full_incidence_graph, available, output, prepared=prepared)
if prepared is None and structure is not None:
self._prepared_scopes.append((supplied, output, structure.snapshot))
max_prepared_scopes = 8
if len(self._prepared_scopes) > max_prepared_scopes:
self._prepared_scopes.pop(0)
return structure

def _preflight(self, request: EvaluationRequest, structure: ValidatedStructure | None) -> BackendCase | None:
"""Keep local requests on their existing path; expand execution only for cross-boundary coupling."""
if structure is None or not (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import typing

from bilt.core._equation import BasicEquation
from bilt.core._structural_analysis._causalization_block_solver._errors import (
BiltOverconstrainedError,
BiltUnderconstrainedError,
Expand Down Expand Up @@ -67,10 +68,40 @@ def validate_structure(
return partition


def shares_equation_scope(snapshot: ExpansionSnapshot, original: str, requested: str) -> bool:
"""Test conservative target equivalence without following directed transforms."""
if original == requested:
return True
if requested in snapshot.available or requested not in snapshot.attributes:
return False

adjacency: dict[str, set[str]] = {}
for constraint in snapshot.constraints:
if not isinstance(constraint.operation, BasicEquation):
continue
attributes = sorted(constraint.attributes - snapshot.available)
for attribute in attributes[1:]:
adjacency.setdefault(attributes[0], set()).add(attribute)
adjacency.setdefault(attribute, set()).add(attributes[0])

reached = {original}
pending = [original]
while pending:
for neighbor in adjacency.get(pending.pop(), ()):
if neighbor == requested:
return True
if neighbor not in reached:
reached.add(neighbor)
pending.append(neighbor)
return False


def validate_request_structure(
incidence: FullIncidenceGraph,
available: collections.abc.Set[str],
output: str,
*,
prepared: ExpansionSnapshot | None = None,
) -> ValidatedStructure | None:
"""Expand relevant children structurally, then validate the combined scope.

Expand All @@ -83,8 +114,13 @@ def validate_request_structure(
if output in available:
return None

expansion = StructuralExpansion(incidence, available, output)
graph, attributes = expansion.collect()
if prepared is None:
expansion = StructuralExpansion(incidence, available, output)
graph, attributes = expansion.collect()
snapshot = expansion.snapshot()
else:
graph, attributes = prepared.to_incidence()
snapshot = prepared
try:
partition = validate_structure(
graph,
Expand All @@ -96,7 +132,6 @@ def validate_request_structure(
message = f"Cannot compute {output} from available inputs {sorted(available)}. {error}"
raise type(error)(message) from error

snapshot = expansion.snapshot()
attribute_nodes = frozenset(attributes.values())
well_attributes = frozenset(name for name, node in attributes.items() if node in partition.well_determined)
# to_incidence inserts primitive rows in snapshot order after the attributes.
Expand Down
134 changes: 134 additions & 0 deletions tests/core/structural/scc/test_shared_structural_preparation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# ruff: noqa: PLR2004

"""Check structural preparation reuse without sharing execution decisions."""

import pytest
lazy import rustworkx as rx

from bilt import Implementation, Interface, equation, field, system, transform
from bilt._backend._python._backend import PYTHON_BACKEND
from bilt.core._evaluation_context import EvaluationContext
from bilt.core._evaluation_request import EvaluationRequest
from bilt.core._structural_analysis._causalization_block_solver import _structural_validation
from bilt.core._structural_analysis._causalization_block_solver._backend import CausalizationStructuralAnalyzer
from bilt.core._structural_analysis._causalization_block_solver._expand_structure import StructuralExpansion
from bilt.core._structural_analysis._causalization_block_solver._full_incidence_graph import FullIncidenceGraph
from bilt.core._structural_analysis._causalization_block_solver._structural_validation import (
shares_equation_scope,
validate_request_structure,
)
from bilt.core._sub_system import SubSystemConstituent
from bilt.symbolic._domains import Domain


@system
class Ports(metaclass=Interface):
x: float = field()
y: float = field()
z: float = field()
w: float = field()


@equation
def relation_y(x: float, y: float) -> bool:
return y == x + 2.0


@equation
def relation_z(y: float, z: float) -> bool:
return z == 3.0 * y


@transform
def w(z: float) -> float:
return z + 1.0


def test_equation_equivalence_does_not_reverse_transforms() -> None:
graph = FullIncidenceGraph([relation_y, relation_z, w])
structure = validate_request_structure(graph, {"x"}, "z")
assert structure is not None
assert shares_equation_scope(structure.snapshot, "z", "y")
assert not shares_equation_scope(structure.snapshot, "z", "w")

reverse = validate_request_structure(graph, {"x"}, "w")
assert reverse is not None
assert not shares_equation_scope(reverse.snapshot, "w", "z")

direct = validate_request_structure(graph, {"x"}, "y")
reused = validate_request_structure(graph, {"x"}, "y", prepared=structure.snapshot)
assert direct is not None
assert reused is not None
assert reused.well_determined == direct.well_determined
assert {row.operation for row in reused.snapshot.constraints} == {
row.operation for row in direct.snapshot.constraints
}


def test_equivalent_outputs_reuse_expansion_but_validate_each_output(monkeypatch: pytest.MonkeyPatch) -> None:
analyser = CausalizationStructuralAnalyzer([relation_y, relation_z, w], PYTHON_BACKEND)
original = StructuralExpansion.collect
calls = []

def collect(self: StructuralExpansion) -> tuple[rx.PyGraph, dict[str, int]]:
calls.append(self)
return original(self)

monkeypatch.setattr(StructuralExpansion, "collect", collect)
with EvaluationContext():
for output, expected in (("z", 15.0), ("y", 5.0)):
request = EvaluationRequest(output=output, input_domains={"x": Domain.scalar()})
case = analyser.analyse(request)
assert PYTHON_BACKEND.chain_case(case).callable(x=3.0) == expected
assert len(calls) == 1

with EvaluationContext():
request = EvaluationRequest(output="w", input_domains={"x": Domain.scalar()})
case = analyser.analyse(request)
assert PYTHON_BACKEND.chain_case(case).callable(x=3.0) == 16.0
assert len(calls) == 2


def test_different_supplied_inputs_remain_independent(monkeypatch: pytest.MonkeyPatch) -> None:
analyser = CausalizationStructuralAnalyzer([relation_y, relation_z], PYTHON_BACKEND)
original = _structural_validation.StructuralExpansion.collect
calls = []

def collect(self: StructuralExpansion) -> tuple[rx.PyGraph, dict[str, int]]:
calls.append(self)
return original(self)

monkeypatch.setattr(StructuralExpansion, "collect", collect)
with EvaluationContext():
for available in ({"x"}, {"y"}):
request = EvaluationRequest(output="z", input_domains={key: Domain.scalar() for key in available})
analyser.analyse(request)
assert len(calls) == 2


def test_shared_nested_children_retain_instance_rows() -> None:
child = Implementation(Ports, [relation_y, relation_z])
left = SubSystemConstituent("left", child)
right = SubSystemConstituent("right", child)

@equation
def combine(answer: float, left: Ports, right: Ports) -> bool:
return answer == left.z + right.z

graph = FullIncidenceGraph([left, right, combine])
structure = validate_request_structure(graph, {"left.x", "right.x"}, "answer")
assert structure is not None
paths = {row.path for row in structure.snapshot.constraints if row.operation is relation_y}
assert paths == {("left",), ("right",)}
assert not shares_equation_scope(structure.snapshot, "answer", "left.x")


@pytest.mark.parametrize("output", ("y", "z", "w"))
def test_cached_and_uncached_validation_agree_for_output(output: str) -> None:
graph = FullIncidenceGraph([relation_y, relation_z, w])
cached = validate_request_structure(graph, {"x"}, "z")
assert cached is not None
original = validate_request_structure(graph, {"x"}, output)
prepared = cached.snapshot if shares_equation_scope(cached.snapshot, "z", output) else None
obtained = validate_request_structure(graph, {"x"}, output, prepared=prepared)
assert obtained == original
Loading