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.

fix(graph): honor supplied graph in If path fallback

JPPhoto51864718030a
diff --git a/invokeai/app/services/shared/graph_if_runtime.py b/invokeai/app/services/shared/graph_if_runtime.py
index 0496f0d4bf..f44f880841 100644
--- a/invokeai/app/services/shared/graph_if_runtime.py
+++ b/invokeai/app/services/shared/graph_if_runtime.py
@@ -6,7 +6,7 @@ from invokeai.app.invocations.logic import IfInvocation
from invokeai.app.services.shared.execution_engine.primitives import ActivationGate
from invokeai.app.services.shared.execution_engine.scheduler import ActivationDependency
from invokeai.app.services.shared.graph_models import Edge, ExecutionToken
-from invokeai.app.services.shared.graph_validation import ForInvocation, IterateInvocation, nx
+from invokeai.app.services.shared.graph_validation import IterateInvocation, nx
if TYPE_CHECKING:
from invokeai.app.services.shared.graph import GraphExecutionState
@@ -18,6 +18,7 @@ def _get_effective_iteration_paths_for_edge(
visited_source_ids: set[str],
get_prepared_nodes_for_source: Callable[[str], set[str]],
get_prepared_edge_iteration_path: Callable[[Edge, str], tuple[int, ...]],
+ source_graph: Optional["nx.DiGraph"] = None,
) -> set[tuple[int, ...]]:
source_node_id = edge.source.node_id
prepared_nodes: set[str] = set()
@@ -39,6 +40,7 @@ def _get_effective_iteration_paths_for_edge(
visited_source_ids,
get_prepared_nodes_for_source,
get_prepared_edge_iteration_path,
+ source_graph,
)
)
@@ -54,6 +56,7 @@ def _get_effective_iteration_paths_for_edge(
next_visited_source_ids,
get_prepared_nodes_for_source,
get_prepared_edge_iteration_path,
+ source_graph,
)
for input_edge in input_edges
)
@@ -66,6 +69,7 @@ def _get_if_iteration_paths(
visited_source_ids: set[str],
get_prepared_nodes_for_source: Callable[[str], set[str]],
get_prepared_edge_iteration_path: Callable[[Edge, str], tuple[int, ...]],
+ source_graph: Optional["nx.DiGraph"] = None,
) -> list[tuple[int, ...]]:
"""Infer the selected branch's effective iteration paths for an If output."""
node = state.graph.get_node(node_id)
@@ -108,6 +112,7 @@ def _get_if_iteration_paths(
next_visited_source_ids,
get_prepared_nodes_for_source,
get_prepared_edge_iteration_path,
+ source_graph,
)
for edge in selected_edges
)
@@ -127,11 +132,10 @@ def _get_if_iteration_paths(
if not has_selected_edges:
return [()]
if not condition_edges and has_unresolved_selected_source:
- iterator_graph = state._iterator_graph(state._get_source_graph_flat())
- has_iterator_ancestor = any(
- isinstance(state.graph.get_node(ancestor_id), (ForInvocation, IterateInvocation))
- for ancestor_id in nx.ancestors(iterator_graph, node_id)
+ iterator_graph = state._iterator_graph(
+ source_graph if source_graph is not None else state._get_source_graph_flat()
)
+ has_iterator_ancestor = bool(state._materializer().get_node_iterators(node_id, iterator_graph))
if not has_iterator_ancestor:
return [()]
return []
diff --git a/invokeai/app/services/shared/graph_materializer.py b/invokeai/app/services/shared/graph_materializer.py
index 33f5f84074..26a6c34c91 100644
--- a/invokeai/app/services/shared/graph_materializer.py
+++ b/invokeai/app/services/shared/graph_materializer.py
@@ -1425,6 +1425,7 @@ class _ExecutionNodeBuilder:
set(),
self._get_prepared_nodes_for_source,
self._get_prepared_edge_iteration_path,
+ graph,
)
)
mappings = self._get_if_condition_iteration_mappings(node_id, graph)
@@ -1767,6 +1768,7 @@ class _ExecutionNodeBuilder:
set(),
self._get_prepared_nodes_for_source,
self._get_prepared_edge_iteration_path,
+ g,
)
condition_edges = self._state.graph._get_input_edges(next_node_id, "condition")
if condition_edges:
diff --git a/tests/app/services/shared/test_graph_if_runtime.py b/tests/app/services/shared/test_graph_if_runtime.py
index 2ff72717bf..74f6731aac 100644
--- a/tests/app/services/shared/test_graph_if_runtime.py
+++ b/tests/app/services/shared/test_graph_if_runtime.py
@@ -2,9 +2,11 @@ import pytest
from invokeai.app.invocations.logic import IfInvocation
from invokeai.app.invocations.math import AddInvocation
+from invokeai.app.services.shared import graph_if_runtime
from invokeai.app.services.shared.execution_engine.scheduler import ActivationDependency
from invokeai.app.services.shared.graph import Edge, EdgeConnection, Graph, GraphExecutionState
from invokeai.app.services.shared.graph_runtime_records import _ApplyTransaction
+from invokeai.app.services.shared.graph_validation import IterateInvocation
from tests.app.services.shared.import_test_utils import assert_module_imports_without_graph
@@ -103,3 +105,54 @@ def test_apply_rollback_reconstructs_pending_if_nodes(state: GraphExecutionState
state._reset_apply_derived_caches()
assert state._pending_if_exec_nodes == {"if-exec"}
+
+
+def test_prepare_uses_supplied_source_graph_for_if_iteration_paths() -> None:
+ state = GraphExecutionState(graph=Graph(nodes={"if": IfInvocation(id="if", condition=True)}))
+ state.graph.add_node(IterateInvocation(id="iterator"))
+ state.graph.add_node(IfInvocation(id="inner"))
+ state.graph.edges.extend(
+ [
+ Edge(
+ source=EdgeConnection(node_id="iterator", field="item"),
+ destination=EdgeConnection(node_id="inner", field="condition"),
+ ),
+ Edge(
+ source=EdgeConnection(node_id="inner", field="value"),
+ destination=EdgeConnection(node_id="if", field="true_input"),
+ ),
+ Edge(
+ source=EdgeConnection(node_id="iterator", field="item"),
+ destination=EdgeConnection(node_id="if", field="false_input"),
+ ),
+ ]
+ )
+ state.graph._invalidate_edge_indexes()
+ source_graph = state._get_source_graph_flat()
+ constrained_graph = source_graph.copy()
+ constrained_graph.remove_node("iterator")
+
+ constrained_graph.remove_edge("inner", "if")
+
+ assert (
+ graph_if_runtime._get_if_iteration_paths(
+ state,
+ "if",
+ set(),
+ lambda _source_id: set(),
+ lambda _edge, _prepared_id: (),
+ source_graph,
+ )
+ == []
+ )
+ assert graph_if_runtime._get_if_iteration_paths(
+ state,
+ "if",
+ set(),
+ lambda _source_id: set(),
+ lambda _edge, _prepared_id: (),
+ constrained_graph,
+ ) == [()]
+ prepared_id = state._materializer().prepare(constrained_graph)
+ assert prepared_id is not None and prepared_id != "if"
+ assert state.prepared_source_mapping[prepared_id] == "if"