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: deduplicate projected If branch sources

JPPhotof5f61c0c8e0b
diff --git a/invokeai/app/services/shared/graph_validation.py b/invokeai/app/services/shared/graph_validation.py
index 9ee119752d..521b1b53c8 100644
--- a/invokeai/app/services/shared/graph_validation.py
+++ b/invokeai/app/services/shared/graph_validation.py
@@ -1928,33 +1928,41 @@ class Graph(BaseModel):
return "Iterator collection type must match all iterator output types"
return None
- def _get_effective_output_connections(
- self, node_id: str, field: str, visited: Optional[set[tuple[str, str]]] = None
- ) -> list[EdgeConnection] | None:
- """Resolve an If output to its connected branch sources, or None when a branch is unresolved."""
- if visited is None:
- visited = set()
- source = (node_id, field)
- node = self.get_node(node_id)
- if not isinstance(node, IfInvocation) or field != "value":
- return [EdgeConnection(node_id=node_id, field=field)]
- if source in visited:
- return None
- visited.add(source)
-
+ def _get_effective_output_connections(self, node_id: str, field: str) -> list[EdgeConnection] | None:
+ """Resolve an If output to its unique branch sources, or None when a branch is unresolved or cyclic."""
+ pending: list[tuple[str, str, bool]] = [(node_id, field, False)]
+ active: set[tuple[str, str]] = set()
+ visited: set[tuple[str, str]] = set()
sources: list[EdgeConnection] = []
- for branch_field in ("true_input", "false_input"):
- branch_edges = self._get_input_edges(node_id, branch_field)
- if len(branch_edges) != 1:
+ while pending:
+ source_node_id, source_field, exiting = pending.pop()
+ source = (source_node_id, source_field)
+ if exiting:
+ active.remove(source)
+ visited.add(source)
+ continue
+ if source in active:
return None
+ if source in visited:
+ continue
- branch_edge = branch_edges[0]
- branch_sources = self._get_effective_output_connections(
- branch_edge.source.node_id, branch_edge.source.field, visited.copy()
- )
- if branch_sources is None:
- return None
- sources.extend(branch_sources)
+ node = self.get_node(source_node_id)
+ if not isinstance(node, IfInvocation) or source_field != "value":
+ visited.add(source)
+ sources.append(EdgeConnection(node_id=source_node_id, field=source_field))
+ continue
+
+ branch_sources: list[tuple[str, str]] = []
+ for branch_field in ("true_input", "false_input"):
+ branch_edges = self._get_input_edges(source_node_id, branch_field)
+ if len(branch_edges) != 1:
+ return None
+ branch_edge = branch_edges[0]
+ branch_sources.append((branch_edge.source.node_id, branch_edge.source.field))
+
+ active.add(source)
+ pending.append((source_node_id, source_field, True))
+ pending.extend((*branch_source, False) for branch_source in reversed(branch_sources))
return sources
diff --git a/tests/test_node_graph.py b/tests/test_node_graph.py
index d1e98def52..7735ffcdf8 100644
--- a/tests/test_node_graph.py
+++ b/tests/test_node_graph.py
@@ -1188,6 +1188,49 @@ def test_graph_rejects_mixed_if_branch_types_for_collector_items():
graph.add_edge(create_edge(if_node.id, "value", collector.id, "item"))
+def test_graph_validation_does_not_repeat_shared_if_branch_compatibility_checks(monkeypatch: pytest.MonkeyPatch):
+ from invokeai.app.services.shared import graph as graph_facade
+
+ true_value = StringInvocation(id="true", value="true")
+ false_value = StringInvocation(id="false", value="false")
+ nodes = {true_value.id: true_value, false_value.id: false_value}
+ edges = []
+
+ previous_if_id = "if_0"
+ nodes[previous_if_id] = IfInvocation(id=previous_if_id)
+ edges.extend(
+ [
+ create_edge(true_value.id, "value", previous_if_id, "true_input"),
+ create_edge(false_value.id, "value", previous_if_id, "false_input"),
+ ]
+ )
+
+ for index in range(1, 12):
+ if_node_id = f"if_{index}"
+ nodes[if_node_id] = IfInvocation(id=if_node_id)
+ edges.extend(
+ [
+ create_edge(previous_if_id, "value", if_node_id, "true_input"),
+ create_edge(previous_if_id, "value", if_node_id, "false_input"),
+ ]
+ )
+ previous_if_id = if_node_id
+
+ graph = Graph(nodes=nodes, edges=edges)
+ compatibility_calls = 0
+ original_are_connections_compatible = graph_facade.are_connections_compatible
+
+ def count_compatibility_calls(*args, **kwargs):
+ nonlocal compatibility_calls
+ compatibility_calls += 1
+ return original_are_connections_compatible(*args, **kwargs)
+
+ monkeypatch.setattr(graph_facade, "are_connections_compatible", count_compatibility_calls)
+ graph.validate_self()
+
+ assert compatibility_calls <= len(edges) * 2
+
+
def test_graph_rejects_collector_output_edge_before_input_edge():
graph = Graph()
graph.add_node(CollectInvocation(id="collect"))