Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

refactor(execution): compile fresh If dependencies in graph state

JPPhotod35ff0dd9f34
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index a2894b6e0e..baaafef666 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -4514,6 +4514,7 @@ class GraphExecutionState(BaseModel):
indegree: dict[str, int] = Field(default_factory=dict, description="Remaining unmet input count for exec nodes")
_resolved_if_exec_branches: dict[str, str] = PrivateAttr(default_factory=dict)
_pending_if_exec_nodes: set[str] = PrivateAttr(default_factory=set)
+ _if_branch_sources_cache: dict[tuple[str, str], frozenset[str]] = PrivateAttr(default_factory=dict)
_if_activation_dependencies_by_source: dict[tuple[str, tuple[int, ...]], tuple[ActivationDependency, ...]] = (
PrivateAttr(default_factory=dict)
)
@@ -4637,6 +4638,7 @@ class GraphExecutionState(BaseModel):
self._generic_graph_scheduler = None
self._if_activation_controller_instance = None
self._pending_if_exec_nodes = set()
+ self._if_branch_sources_cache = {}
self._if_activation_dependencies_by_source = {}
self._if_activation_dependencies_by_exec = {}
self._execution_runtime = None
@@ -4676,9 +4678,9 @@ class GraphExecutionState(BaseModel):
return self._source_graph_flat
def _can_use_fresh_flat_if_activation(self) -> bool:
- """Admit the bounded fresh flat-If dependency-recording contract."""
+ """Admit fresh single-If dependency compilation for ordinary graph nodes."""
- if self._legacy_snapshot_loaded or len(self.graph.nodes) != 5 or len(self.graph.edges) != 4:
+ if self._legacy_snapshot_loaded:
return False
if_nodes = [node for node in self.graph.nodes.values() if isinstance(node, IfInvocation)]
@@ -4702,26 +4704,34 @@ class GraphExecutionState(BaseModel):
):
return False
- condition_edges = self.graph._get_input_edges(if_node.id, "condition")
- true_edges = self.graph._get_input_edges(if_node.id, "true_input")
- false_edges = self.graph._get_input_edges(if_node.id, "false_input")
- output_edges = self.graph._get_output_edges(if_node.id)
- if len(condition_edges) != 1 or len(true_edges) != 1 or len(false_edges) != 1 or len(output_edges) != 1:
- return False
+ return True
- source_node_ids = {
- condition_edges[0].source.node_id,
- true_edges[0].source.node_id,
- false_edges[0].source.node_id,
- }
- if len(source_node_ids) != 3 or if_node.id in source_node_ids:
- return False
+ def _get_fresh_if_branch_sources(self, if_node_id: str, branch_field: str) -> set[str]:
+ cache_key = (if_node_id, branch_field)
+ cached = self._if_branch_sources_cache.get(cache_key)
+ if cached is not None:
+ return set(cached)
- return set(self.graph.nodes) == {
- *source_node_ids,
- if_node.id,
- output_edges[0].destination.node_id,
- }
+ source_graph = self._get_source_graph_flat()
+ direct_sources = {edge.source.node_id for edge in self.graph._get_input_edges(if_node_id, branch_field)}
+ branch_sources = set(direct_sources)
+ for source_node_id in direct_sources:
+ branch_sources.update(nx.ancestors(source_graph, source_node_id))
+
+ changed = True
+ while changed:
+ changed = False
+ for source_node_id in tuple(branch_sources):
+ if all(
+ edge.destination.node_id in branch_sources
+ or (edge.destination.node_id == if_node_id and edge.destination.field == branch_field)
+ for edge in self.graph._get_output_edges(source_node_id)
+ ):
+ continue
+ branch_sources.remove(source_node_id)
+ changed = True
+ self._if_branch_sources_cache[cache_key] = frozenset(branch_sources)
+ return branch_sources
def _get_source_activation_dependencies(
self, source_node_id: str, iteration_path: tuple[int, ...] = ()
@@ -4737,9 +4747,7 @@ class GraphExecutionState(BaseModel):
dependencies = tuple(
ActivationDependency(owner_id=if_node.id, branch=branch_field, frame=iteration_path)
for branch_field in ("true_input", "false_input")
- if any(
- edge.source.node_id == source_node_id for edge in self.graph._get_input_edges(if_node.id, branch_field)
- )
+ if source_node_id in self._get_fresh_if_branch_sources(if_node.id, branch_field)
)
self._tx_set_mapping(self._if_activation_dependencies_by_source, key, dependencies)
return dependencies
@@ -4843,6 +4851,10 @@ class GraphExecutionState(BaseModel):
def _invalidate_source_graph_cache(self) -> None:
self._source_graph_flat = None
self._for_source_by_return_id = None
+ self._if_branch_sources_cache = {}
+ self._if_activation_dependencies_by_source = {}
+ self._if_activation_dependencies_by_exec = {}
+ self._if_activation_controller_instance = None
def _materializer(self) -> _ExecutionMaterializer:
if self._execution_materializer is None:
@@ -6918,6 +6930,7 @@ class GraphExecutionState(BaseModel):
self._active_class = None
self._resolved_if_exec_branches = {}
self._pending_if_exec_nodes = set()
+ self._if_branch_sources_cache = {}
self._prepared_exec_metadata = {}
self._prepared_exec_registry = None
self._execution_materializer = None
diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.py
index 2ba10a3a58..c2aa9677d7 100644
--- a/tests/app/services/shared/test_execution_engine_differential.py
+++ b/tests/app/services/shared/test_execution_engine_differential.py
@@ -139,6 +139,16 @@ def _flat_if_graph(*, condition: bool = True) -> Graph:
return graph
+def _noncanonical_flat_if_graph(*, condition: bool = True) -> Graph:
+ graph = _flat_if_graph(condition=condition)
+ graph.delete_node("true_branch")
+ graph.add_node(AddInvocation(id="true_source", a=2, b=3))
+ graph.add_node(AddInvocation(id="true_branch", b=4))
+ graph.add_edge(create_edge("true_source", "value", "true_branch", "a"))
+ graph.add_edge(create_edge("true_branch", "value", "if", "true_input"))
+ return graph
+
+
def _flat_for_graph(
*,
with_after: bool = False,
@@ -2493,6 +2503,59 @@ def test_fresh_flat_if_records_dependencies_without_author_graph_branch_analysis
assert state.is_complete()
[email protected]("force_compatibility_scheduler", [False, True])
+def test_fresh_noncanonical_if_compiles_branch_membership_without_controller_analysis(
+ force_compatibility_scheduler: bool,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ def fail_branch_analysis(*_: object, **__: object) -> set[str]:
+ raise AssertionError("fresh noncanonical If used controller-owned branch analysis")
+
+ monkeypatch.setattr(graph_module._IfActivationController, "_branch_sources", fail_branch_analysis)
+
+ trace, state = _run_graph(
+ GraphExecutionState(graph=_noncanonical_flat_if_graph()),
+ force_compatibility_scheduler=force_compatibility_scheduler,
+ )
+
+ assert trace == ["condition", "true_source", "true_branch", "if", "sink"]
+ dependencies_by_source = {
+ source_id: {
+ (dependency.owner_id, dependency.branch, dependency.frame)
+ for execution_id, source_id in state.prepared_source_mapping.items()
+ for dependency in state._if_activation_dependencies_by_exec.get(execution_id, ())
+ }
+ for source_id in {"true_source", "true_branch"}
+ }
+ assert dependencies_by_source == {
+ "true_source": {("if", "true_input", ())},
+ "true_branch": {("if", "true_input", ())},
+ }
+ assert state.is_complete()
+
+
[email protected]("force_compatibility_scheduler", [False, True])
+def test_fresh_if_dependency_cache_refreshes_after_branch_source_graph_edit(
+ force_compatibility_scheduler: bool,
+) -> None:
+ state = GraphExecutionState(graph=_flat_if_graph(condition=False))
+
+ assert state._get_source_activation_dependencies("true_branch") == (
+ graph_module.ActivationDependency(owner_id="if", branch="true_input", frame=()),
+ )
+
+ state.add_node(AddInvocation(id="true_consumer", b=1))
+ state.add_edge(create_edge("true_branch", "value", "true_consumer", "a"))
+
+ assert state._get_source_activation_dependencies("true_branch") == ()
+ trace, state = _run_graph(state, force_compatibility_scheduler=force_compatibility_scheduler)
+
+ assert trace == ["true_branch", "true_consumer", "condition", "false_branch", "if", "sink"]
+ assert state.results[next(iter(state.source_prepared_mapping["true_consumer"]))].value == 6
+ assert state.results[next(iter(state.source_prepared_mapping["sink"]))].value == 31
+ assert state.is_complete()
+
+
def test_fresh_flat_if_with_saved_workflow_uses_controller_fallback(
monkeypatch: pytest.MonkeyPatch,
) -> None: