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): extract If dependency compilation

JPPhoto704f2d0cf5ef
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 50591216d7..8fcb6f374d 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -62,6 +62,7 @@ from invokeai.app.invocations.loops import (
ForReturnInvocationOutput,
LoopState,
)
+from invokeai.app.services.shared import graph_if_dependencies
from invokeai.app.services.shared.execution_effects import ContinuationEffect
from invokeai.app.services.shared.execution_effects import ExecutionRef as EffectExecutionRef
from invokeai.app.services.shared.execution_engine.child import (
@@ -2693,127 +2694,18 @@ class GraphExecutionState(BaseModel):
return self._source_graph_flat
def _get_fresh_if_nodes(self) -> tuple[IfInvocation, ...]:
- source_graph = self._get_source_graph_flat()
- source_order = {node_id: index for index, node_id in enumerate(nx.topological_sort(source_graph))}
- return tuple(
- sorted(
- (node for node in self.graph.nodes.values() if isinstance(node, IfInvocation)),
- key=lambda node: (source_order.get(node.id, len(source_order)), node.id),
- )
- )
+ return graph_if_dependencies._get_fresh_if_nodes(self)
def _can_use_fresh_flat_if_activation(self) -> bool:
- """Admit fresh single-If or bounded nested-If dependency compilation."""
-
- if self._legacy_snapshot_loaded:
- return False
-
- forbidden_nodes = (
- CallSavedWorkflowInvocation,
- ForInvocation,
- ForReturnInvocation,
- IterateInvocation,
- CollectInvocation,
- )
- if any(isinstance(node, forbidden_nodes) for node in self.graph.nodes.values()):
- return False
-
- try:
- source_graph = self._get_source_graph_flat()
- if not nx.is_directed_acyclic_graph(source_graph):
- return False
- if_nodes = self._get_fresh_if_nodes()
- except nx.NetworkXUnfeasible:
- return False
-
- if len(if_nodes) != 1:
- if len(if_nodes) != 2:
- return False
-
- if any(edge.type != "default" for edge in self.graph.edges):
- return False
-
- if_node_ids = {node.id for node in if_nodes}
- nested_edges = [
- edge
- for edge in self.graph.edges
- if edge.source.node_id in if_node_ids and edge.destination.node_id in if_node_ids
- ]
- if len(nested_edges) != 1:
- return False
-
- nested_edge = nested_edges[0]
- if nested_edge.source.field != "value" or nested_edge.destination.field not in {
- "true_input",
- "false_input",
- }:
- return False
-
- inner_if_id = nested_edge.source.node_id
- if any(edge.source.node_id == inner_if_id and edge != nested_edge for edge in self.graph.edges):
- return False
-
- outer_if_id = nested_edge.destination.node_id
- for if_node in if_nodes:
- for field in ("condition", "true_input", "false_input"):
- input_edges = self.graph._get_input_edges(if_node.id, field)
- if len(input_edges) != 1:
- return False
- for edge in input_edges:
- if edge.source.node_id in if_node_ids and edge != nested_edge:
- return False
- if edge == nested_edge and (
- if_node.id != outer_if_id or field != nested_edge.destination.field
- ):
- return False
-
- return True
+ return graph_if_dependencies._can_use_fresh_flat_if_activation(self)
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)
-
- 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
+ return graph_if_dependencies._get_fresh_if_branch_sources(self, if_node_id, branch_field)
def _get_source_activation_dependencies(
self, source_node_id: str, iteration_path: tuple[int, ...] = ()
) -> tuple[ActivationDependency, ...]:
- key = (source_node_id, iteration_path)
- if key in self._if_activation_dependencies_by_source:
- return self._if_activation_dependencies_by_source[key]
-
- if not self._can_use_fresh_flat_if_activation():
- return self._if_activation_controller().get_source_dependencies(source_node_id, iteration_path)
-
- dependencies = tuple(
- ActivationDependency(owner_id=if_node.id, branch=branch_field, frame=iteration_path)
- for if_node in self._get_fresh_if_nodes()
- for branch_field in ("true_input", "false_input")
- 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
+ return graph_if_dependencies._get_source_activation_dependencies(self, source_node_id, iteration_path)
def _record_activation_dependencies(self, exec_node_id: str) -> tuple[ActivationDependency, ...]:
dependencies = self._if_activation_dependencies_by_exec.get(exec_node_id)
diff --git a/invokeai/app/services/shared/graph_if_dependencies.py b/invokeai/app/services/shared/graph_if_dependencies.py
new file mode 100644
index 0000000000..920389b9b3
--- /dev/null
+++ b/invokeai/app/services/shared/graph_if_dependencies.py
@@ -0,0 +1,137 @@
+# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
+
+from typing import TYPE_CHECKING
+
+from invokeai.app.invocations.call_saved_workflow import CallSavedWorkflowInvocation
+from invokeai.app.invocations.logic import IfInvocation
+from invokeai.app.invocations.loops import ForInvocation, ForReturnInvocation
+from invokeai.app.services.shared.execution_engine.scheduler import ActivationDependency
+from invokeai.app.services.shared.graph_validation import CollectInvocation, IterateInvocation, nx
+
+if TYPE_CHECKING:
+ from invokeai.app.services.shared.graph import GraphExecutionState
+
+
+def _get_fresh_if_nodes(state: "GraphExecutionState") -> tuple[IfInvocation, ...]:
+ source_graph = state._get_source_graph_flat()
+ source_order = {node_id: index for index, node_id in enumerate(nx.topological_sort(source_graph))}
+ return tuple(
+ sorted(
+ (node for node in state.graph.nodes.values() if isinstance(node, IfInvocation)),
+ key=lambda node: (source_order.get(node.id, len(source_order)), node.id),
+ )
+ )
+
+
+def _can_use_fresh_flat_if_activation(state: "GraphExecutionState") -> bool:
+ """Admit fresh single-If or bounded nested-If dependency compilation."""
+
+ if state._legacy_snapshot_loaded:
+ return False
+
+ forbidden_nodes = (
+ CallSavedWorkflowInvocation,
+ ForInvocation,
+ ForReturnInvocation,
+ IterateInvocation,
+ CollectInvocation,
+ )
+ if any(isinstance(node, forbidden_nodes) for node in state.graph.nodes.values()):
+ return False
+
+ try:
+ source_graph = state._get_source_graph_flat()
+ if not nx.is_directed_acyclic_graph(source_graph):
+ return False
+ if_nodes = _get_fresh_if_nodes(state)
+ except nx.NetworkXUnfeasible:
+ return False
+
+ if len(if_nodes) != 1:
+ if len(if_nodes) != 2:
+ return False
+
+ if any(edge.type != "default" for edge in state.graph.edges):
+ return False
+
+ if_node_ids = {node.id for node in if_nodes}
+ nested_edges = [
+ edge
+ for edge in state.graph.edges
+ if edge.source.node_id in if_node_ids and edge.destination.node_id in if_node_ids
+ ]
+ if len(nested_edges) != 1:
+ return False
+
+ nested_edge = nested_edges[0]
+ if nested_edge.source.field != "value" or nested_edge.destination.field not in {
+ "true_input",
+ "false_input",
+ }:
+ return False
+
+ inner_if_id = nested_edge.source.node_id
+ if any(edge.source.node_id == inner_if_id and edge != nested_edge for edge in state.graph.edges):
+ return False
+
+ outer_if_id = nested_edge.destination.node_id
+ for if_node in if_nodes:
+ for field in ("condition", "true_input", "false_input"):
+ input_edges = state.graph._get_input_edges(if_node.id, field)
+ if len(input_edges) != 1:
+ return False
+ for edge in input_edges:
+ if edge.source.node_id in if_node_ids and edge != nested_edge:
+ return False
+ if edge == nested_edge and (if_node.id != outer_if_id or field != nested_edge.destination.field):
+ return False
+
+ return True
+
+
+def _get_fresh_if_branch_sources(state: "GraphExecutionState", if_node_id: str, branch_field: str) -> set[str]:
+ cache_key = (if_node_id, branch_field)
+ cached = state._if_branch_sources_cache.get(cache_key)
+ if cached is not None:
+ return set(cached)
+
+ source_graph = state._get_source_graph_flat()
+ direct_sources = {edge.source.node_id for edge in state.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 state.graph._get_output_edges(source_node_id)
+ ):
+ continue
+ branch_sources.remove(source_node_id)
+ changed = True
+ state._if_branch_sources_cache[cache_key] = frozenset(branch_sources)
+ return branch_sources
+
+
+def _get_source_activation_dependencies(
+ state: "GraphExecutionState", source_node_id: str, iteration_path: tuple[int, ...] = ()
+) -> tuple[ActivationDependency, ...]:
+ key = (source_node_id, iteration_path)
+ if key in state._if_activation_dependencies_by_source:
+ return state._if_activation_dependencies_by_source[key]
+
+ if not _can_use_fresh_flat_if_activation(state):
+ return state._if_activation_controller().get_source_dependencies(source_node_id, iteration_path)
+
+ dependencies = tuple(
+ ActivationDependency(owner_id=if_node.id, branch=branch_field, frame=iteration_path)
+ for if_node in _get_fresh_if_nodes(state)
+ for branch_field in ("true_input", "false_input")
+ if source_node_id in _get_fresh_if_branch_sources(state, if_node.id, branch_field)
+ )
+ state._tx_set_mapping(state._if_activation_dependencies_by_source, key, dependencies)
+ return dependencies
diff --git a/tests/app/services/shared/test_graph_if_dependencies.py b/tests/app/services/shared/test_graph_if_dependencies.py
new file mode 100644
index 0000000000..9dd8cfddc4
--- /dev/null
+++ b/tests/app/services/shared/test_graph_if_dependencies.py
@@ -0,0 +1,199 @@
+from __future__ import annotations
+
+import subprocess
+import sys
+import textwrap
+from typing import Any
+
+import pytest
+
+from invokeai.app.invocations.call_saved_workflow import CallSavedWorkflowInvocation
+from invokeai.app.invocations.logic import IfInvocation
+from invokeai.app.invocations.math import AddInvocation
+from invokeai.app.invocations.primitives import BooleanInvocation
+from invokeai.app.services.shared import graph as graph_module
+from invokeai.app.services.shared import graph_if_dependencies
+from invokeai.app.services.shared.execution_engine import ActivationDependency
+from invokeai.app.services.shared.graph import Edge, EdgeConnection, Graph, GraphExecutionState
+
+
+def _edge(source: str, source_field: str, destination: str, destination_field: str) -> Edge:
+ return Edge(
+ source=EdgeConnection(node_id=source, field=source_field),
+ destination=EdgeConnection(node_id=destination, field=destination_field),
+ )
+
+
+def _flat_if_graph() -> Graph:
+ graph = Graph()
+ graph.add_node(BooleanInvocation(id="condition", value=True))
+ graph.add_node(AddInvocation(id="true_branch", a=2, b=3))
+ graph.add_node(AddInvocation(id="false_branch", a=10, b=20))
+ graph.add_node(IfInvocation(id="if"))
+ graph.add_node(AddInvocation(id="sink", b=1))
+ graph.add_edge(_edge("condition", "value", "if", "condition"))
+ graph.add_edge(_edge("true_branch", "value", "if", "true_input"))
+ graph.add_edge(_edge("false_branch", "value", "if", "false_input"))
+ graph.add_edge(_edge("if", "value", "sink", "a"))
+ return graph
+
+
+def _nested_if_graph() -> Graph:
+ graph = Graph()
+ graph.add_node(BooleanInvocation(id="outer_condition", value=True))
+ graph.add_node(BooleanInvocation(id="inner_condition", value=False))
+ graph.add_node(AddInvocation(id="inner_true", a=2, b=2))
+ graph.add_node(AddInvocation(id="inner_false", a=3, b=3))
+ graph.add_node(AddInvocation(id="outer_false", a=10, b=0))
+ graph.add_node(IfInvocation(id="inner_if"))
+ graph.add_node(IfInvocation(id="outer_if"))
+ graph.add_node(AddInvocation(id="sink", b=1))
+ graph.add_edge(_edge("outer_condition", "value", "outer_if", "condition"))
+ graph.add_edge(_edge("inner_condition", "value", "inner_if", "condition"))
+ graph.add_edge(_edge("inner_true", "value", "inner_if", "true_input"))
+ graph.add_edge(_edge("inner_false", "value", "inner_if", "false_input"))
+ graph.add_edge(_edge("inner_if", "value", "outer_if", "true_input"))
+ graph.add_edge(_edge("outer_false", "value", "outer_if", "false_input"))
+ graph.add_edge(_edge("outer_if", "value", "sink", "a"))
+ return graph
+
+
+def test_leaf_import_does_not_import_graph() -> None:
+ script = """
+ import builtins
+ import sys
+
+ real_import = builtins.__import__
+
+ def blocked_import(name, globals=None, locals=None, fromlist=(), level=0):
+ if name == "invokeai.app.services.shared.graph" or name.startswith("invokeai.app.services.shared.graph."):
+ raise ModuleNotFoundError("graph import blocked")
+ return real_import(name, globals, locals, fromlist, level)
+
+ builtins.__import__ = blocked_import
+ import invokeai.app.services.shared.graph_if_dependencies
+
+ assert "invokeai.app.services.shared.graph" not in sys.modules
+ """
+ result = subprocess.run(
+ [sys.executable, "-c", textwrap.dedent(script)],
+ capture_output=True,
+ text=True,
+ timeout=300,
+ check=False,
+ )
+ assert result.returncode == 0, result.stderr
+
+
+def test_graph_methods_delegate_to_leaf_functions(monkeypatch: pytest.MonkeyPatch) -> None:
+ state = object()
+ marker = object()
+ monkeypatch.setattr(graph_if_dependencies, "_get_fresh_if_nodes", lambda actual_state: (actual_state, marker))
+ monkeypatch.setattr(graph_if_dependencies, "_can_use_fresh_flat_if_activation", lambda actual_state: marker)
+ monkeypatch.setattr(
+ graph_if_dependencies,
+ "_get_fresh_if_branch_sources",
+ lambda actual_state, if_node_id, branch_field: {actual_state, if_node_id, branch_field},
+ )
+ monkeypatch.setattr(
+ graph_if_dependencies,
+ "_get_source_activation_dependencies",
+ lambda actual_state, source_node_id, iteration_path=(): (actual_state, source_node_id, iteration_path),
+ )
+
+ assert GraphExecutionState._get_fresh_if_nodes(state) == (state, marker)
+ assert GraphExecutionState._can_use_fresh_flat_if_activation(state) is marker
+ assert GraphExecutionState._get_fresh_if_branch_sources(state, "if", "true_input") == {
+ state,
+ "if",
+ "true_input",
+ }
+ assert GraphExecutionState._get_source_activation_dependencies(state, "source", (2,)) == (
+ state,
+ "source",
+ (2,),
+ )
+
+
+def test_flat_and_nested_dependencies_preserve_values_and_order() -> None:
+ flat_state = GraphExecutionState(graph=_flat_if_graph())
+ assert flat_state._get_source_activation_dependencies("true_branch") == (
+ ActivationDependency(owner_id="if", branch="true_input", frame=()),
+ )
+ assert flat_state._get_source_activation_dependencies("false_branch", (3,)) == (
+ ActivationDependency(owner_id="if", branch="false_input", frame=(3,)),
+ )
+
+ nested_state = GraphExecutionState(graph=_nested_if_graph())
+ assert nested_state._get_source_activation_dependencies("inner_true") == (
+ ActivationDependency(owner_id="inner_if", branch="true_input", frame=()),
+ ActivationDependency(owner_id="outer_if", branch="true_input", frame=()),
+ )
+ assert nested_state._get_source_activation_dependencies("inner_false", (4,)) == (
+ ActivationDependency(owner_id="inner_if", branch="false_input", frame=(4,)),
+ ActivationDependency(owner_id="outer_if", branch="true_input", frame=(4,)),
+ )
+
+
+def test_ineligible_shape_uses_controller_fallback(monkeypatch: pytest.MonkeyPatch) -> None:
+ graph = _flat_if_graph()
+ graph.add_node(CallSavedWorkflowInvocation(id="call", workflow_id="saved-workflow"))
+ state = GraphExecutionState(graph=graph)
+ expected = (ActivationDependency(owner_id="fallback", branch="true_input", frame=(7,)),)
+ calls: list[tuple[str, tuple[int, ...]]] = []
+
+ def get_source_dependencies(
+ _controller: Any, source_node_id: str, iteration_path: tuple[int, ...]
+ ) -> tuple[ActivationDependency, ...]:
+ calls.append((source_node_id, iteration_path))
+ return expected
+
+ monkeypatch.setattr(graph_module._IfActivationController, "get_source_dependencies", get_source_dependencies)
+ assert not state._can_use_fresh_flat_if_activation()
+ assert state._get_source_activation_dependencies("true_branch", (7,)) == expected
+ assert calls == [("true_branch", (7,))]
+
+
+def test_branch_cache_uses_graph_facade_nx_seam(monkeypatch: pytest.MonkeyPatch) -> None:
+ state = GraphExecutionState(graph=_flat_if_graph())
+ source_graph = state._get_source_graph_flat()
+ calls: list[tuple[str, str]] = []
+
+ class _NetworkX:
+ @staticmethod
+ def topological_sort(graph: Any) -> list[str]:
+ calls.append(("topological_sort", ""))
+ return list(graph.nodes)
+
+ @staticmethod
+ def ancestors(_graph: Any, node_id: str) -> set[str]:
+ calls.append(("ancestors", node_id))
+ return set()
+
+ @staticmethod
+ def is_directed_acyclic_graph(_graph: Any) -> bool:
+ calls.append(("is_directed_acyclic_graph", ""))
+ return True
+
+ monkeypatch.setattr(graph_module, "nx", _NetworkX())
+ assert graph_if_dependencies._get_fresh_if_nodes(state)[0].id == "if"
+ assert graph_if_dependencies._get_fresh_if_branch_sources(state, "if", "true_input") == {"true_branch"}
+ assert graph_if_dependencies._get_fresh_if_branch_sources(state, "if", "true_input") == {"true_branch"}
+ assert source_graph is state._source_graph_flat
+ assert calls == [("topological_sort", ""), ("ancestors", "true_branch")]
+
+
+def test_branch_dependency_cache_is_frame_local_and_graph_edits_invalidate() -> None:
+ state = GraphExecutionState(graph=_flat_if_graph())
+ first = state._get_source_activation_dependencies("true_branch", (1,))
+ second = state._get_source_activation_dependencies("true_branch", (2,))
+ assert first[0].frame == (1,)
+ assert second[0].frame == (2,)
+ assert set(state._if_activation_dependencies_by_source) == {
+ ("true_branch", (1,)),
+ ("true_branch", (2,)),
+ }
+
+ state.add_node(AddInvocation(id="true_consumer", b=1))
+ state.add_edge(_edge("true_branch", "value", "true_consumer", "a"))
+ assert state._get_source_activation_dependencies("true_branch") == ()