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(execution): rehydrate If discarded branch state

JPPhotofa6417d296f2
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index 81a3803fdb..9f999df65c 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -303,7 +303,7 @@ are treated as version 0, while unreadable snapshots are quarantined by the queu
- Cache the preserved iteration path when the materializer has one, such as for grouped collectors.
- Wire edges from chosen prepared parents.
- Set `indegree = number of unmet inputs` (i.e., parents not yet executed). The generic scheduler mirrors this into
- its opaque plan for ordinary static DAGs.
+ its opaque plan for ordinary static DAGs and legacy-shaped `If` graphs.
- Try to resolve any `If`-specific scheduling state.
- If the node is ready and not deferred by an unresolved `If`, enqueue it into its class queue.
@@ -311,9 +311,9 @@ are treated as version 0, while unreadable snapshots are quarantined by the queu
- `_enqueue_if_ready(nid)` applies the same readiness predicate to both adapters. The generic scheduler additionally
tracks claimed (returned-but-not-completed) work so re-registration cannot duplicate a node.
-- `_get_next_node()` uses the generic scheduler for ordinary static DAGs and projects its deterministic class/frame
- order into the compatibility queues. Control-flow graphs use `_active_class` and the legacy class queues. No
- batch-size or fairness cap is currently implemented.
+- `_get_next_node()` uses the generic scheduler for ordinary static DAGs and legacy-shaped `If` graphs, projecting its
+ deterministic class/frame order into the compatibility queues. Loop and saved-workflow control-flow graphs use
+ `_active_class` and the legacy class queues. No batch-size or fairness cap is currently implemented.
#### 4.5.1 Indegree (what it is and how it's used)
diff --git a/invokeai/app/services/shared/execution_engine/scheduler.py b/invokeai/app/services/shared/execution_engine/scheduler.py
index 7b044a543e..5f98fd6d68 100644
--- a/invokeai/app/services/shared/execution_engine/scheduler.py
+++ b/invokeai/app/services/shared/execution_engine/scheduler.py
@@ -358,8 +358,7 @@ class ExecutionScheduler:
raise ValueError(f"executed node {node_id} is missing prerequisite: {missing[0]}")
self.indegree = {
node_id: sum(
- dependency not in self.executed and dependency not in self.discarded
- for dependency in node.dependencies
+ dependency not in self.executed and dependency not in self.discarded for dependency in node.dependencies
)
for node_id, node in self.plan.nodes.items()
}
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 7e927bfd81..d7d6b055ad 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -2179,13 +2179,13 @@ class _GenericGraphSchedulerAdapter:
)
self._register_existing_nodes()
prepared_ids = set(state.prepared_source_mapping).intersection(state.results)
- skipped_ids = {
+ discarded_ids = {
exec_node_id
for exec_node_id in state.prepared_source_mapping
if state._get_prepared_exec_metadata(exec_node_id).state == "skipped"
}
self._scheduler.executed = prepared_ids
- self._scheduler.skipped = skipped_ids
+ self._scheduler.discarded = discarded_ids
self._scheduler.rebuild_ready()
self._initializing = False
self._sync_indegree()
@@ -2288,7 +2288,7 @@ class _GenericGraphSchedulerAdapter:
def mark_skipped(self, exec_node_id: str) -> None:
"""Mirror a compatibility skip in the opaque scheduler projection."""
- self._scheduler.skip(exec_node_id)
+ self._scheduler.discard(exec_node_id)
self._sync_indegree()
self._project_ready_nodes()
diff --git a/tests/app/services/shared/test_execution_engine_scheduler.py b/tests/app/services/shared/test_execution_engine_scheduler.py
index e77da8c137..0b079c6f27 100644
--- a/tests/app/services/shared/test_execution_engine_scheduler.py
+++ b/tests/app/services/shared/test_execution_engine_scheduler.py
@@ -4,8 +4,8 @@ from unittest.mock import Mock
import pytest
-from invokeai.app.invocations.math import AddInvocation, MultiplyInvocation
from invokeai.app.invocations.logic import IfInvocation
+from invokeai.app.invocations.math import AddInvocation, MultiplyInvocation
from invokeai.app.invocations.primitives import BooleanInvocation
from invokeai.app.services.shared.execution_engine.scheduler import (
ExecutionPlan,
@@ -410,7 +410,9 @@ def test_graph_state_if_generic_and_legacy_paths_have_matching_completion() -> N
trace.append(state.prepared_source_mapping[node.id])
state.complete(node.id, node.invoke(Mock()))
completed_sources = {
- source_id for exec_node_id, source_id in state.prepared_source_mapping.items() if exec_node_id in state.results
+ source_id
+ for exec_node_id, source_id in state.prepared_source_mapping.items()
+ if exec_node_id in state.results
}
return trace, completed_sources
@@ -421,6 +423,44 @@ def test_graph_state_if_generic_and_legacy_paths_have_matching_completion() -> N
assert generic_completed == legacy_completed == {"condition", "false_value", "if"}
+def test_graph_state_if_rehydrates_discarded_unselected_branch() -> None:
+ graph = Graph()
+ graph.add_node(BooleanInvocation(id="condition", value=False))
+ graph.add_node(AddInvocation(id="true_value", a=2, b=2))
+ graph.add_node(AddInvocation(id="false_value", a=3, b=3))
+ graph.add_node(IfInvocation(id="if"))
+ graph.add_edge(
+ Edge(
+ source=EdgeConnection(node_id="condition", field="value"),
+ destination=EdgeConnection(node_id="if", field="condition"),
+ )
+ )
+ graph.add_edge(
+ Edge(
+ source=EdgeConnection(node_id="true_value", field="value"),
+ destination=EdgeConnection(node_id="if", field="true_input"),
+ )
+ )
+ graph.add_edge(
+ Edge(
+ source=EdgeConnection(node_id="false_value", field="value"),
+ destination=EdgeConnection(node_id="if", field="false_input"),
+ )
+ )
+
+ state = GraphExecutionState(graph=graph)
+ condition = state.next()
+ assert condition is not None
+ assert state.prepared_source_mapping[condition.id] == "condition"
+ state.complete(condition.id, condition.invoke(Mock()))
+
+ restored = GraphExecutionState.model_validate(state.model_dump(mode="python"), strict=False)
+ next_node = restored.next()
+
+ assert next_node is not None
+ assert restored.prepared_source_mapping[next_node.id] == "false_value"
+
+
def test_graph_state_static_dag_rehydrates_generic_scheduler_after_partial_run() -> None:
graph = Graph()
graph.add_node(AddInvocation(id="first", a=1, b=2))