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 graph schedulers

JPPhoto7043b12fad0e
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index c911eea855..e9643305c6 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -455,6 +455,9 @@ Workflow-call note:
`graph_if_runtime.py` records dependencies and evaluates admission against gates and tokens.
`GraphExecutionState` retains the method entry points, durable token/reference/effect storage,
derived caches, transaction journal, and fallback controller selection.
+- Private `graph_scheduler.py` contains `_ExecutionScheduler` and `_GenericGraphSchedulerAdapter`. `graph.py`
+ deliberately re-exports both classes, preserving their existing import paths and monkeypatch seams while keeping
+ scheduler implementation details out of the graph-state façade.
- `_GenericGraphSchedulerAdapter` Projects the generic `ExecutionPlan`/`ExecutionScheduler` into the existing state
fields for ordinary static DAGs and legacy-shaped `If` graphs; the generic scheduler owns opaque readiness,
intentional discards, indegree transitions, deterministic ordering, claimed work, and completion. The adapter registers
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index dfb93c9b66..16cd8639cd 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -3,7 +3,6 @@
import copy # noqa: F401
import json
import weakref # noqa: F401
-from collections import deque
from functools import wraps # noqa: F401
from typing import (
TYPE_CHECKING, # noqa: F401
@@ -77,8 +76,6 @@ from invokeai.app.services.shared.execution_engine.primitives import (
from invokeai.app.services.shared.execution_engine.runtime import ExecutionEngineRuntime
from invokeai.app.services.shared.execution_engine.scheduler import (
ActivationDependency,
- ExecutionPlan,
- ExecutionScheduler,
)
from invokeai.app.services.shared.graph_execution_runtime import (
_BodyIterateCollectFanIn,
@@ -120,6 +117,7 @@ from invokeai.app.services.shared.graph_runtime_records import (
_PreparedExecNodeMetadata,
_PreparedExecRegistry,
)
+from invokeai.app.services.shared.graph_scheduler import _ExecutionScheduler, _GenericGraphSchedulerAdapter
from invokeai.app.services.shared.graph_validation import (
_RESERVED_EFFECT_PORTS,
COLLECTION_FIELD,
@@ -169,598 +167,6 @@ from invokeai.app.util.misc import uuid_string
_JSON_SERIALIZER = TypeAdapter(Any)
-class _ExecutionScheduler:
- """Owns ready-queue ordering and indegree-driven execution transitions."""
-
- def __init__(self, state: "GraphExecutionState") -> None:
- self._state = state
-
- def _validate_exec_node_ready_state(self, exec_node_id: str) -> None:
- if exec_node_id not in self._state.execution_graph.nodes:
- raise KeyError(f"exec node {exec_node_id} missing from execution_graph")
- if exec_node_id not in self._state.indegree:
- raise KeyError(f"indegree missing for exec node {exec_node_id}")
-
- def _should_skip_ready_enqueue(self, exec_node_id: str) -> bool:
- if (
- self._state.indegree[exec_node_id] != 0
- or exec_node_id in self._state.executed
- or self._state._is_pending_if(exec_node_id)
- or self._state._is_deferred_by_unresolved_if(exec_node_id)
- ):
- return True
- node = self._state.execution_graph.nodes[exec_node_id]
- return isinstance(node, CollectInvocation) and not self._state._collect_streams_ready(exec_node_id)
-
- def _discard_rejected_activation_node(self, exec_node_id: str) -> bool:
- if exec_node_id not in self._state.indegree:
- return False
- metadata = self._state._get_prepared_exec_metadata(exec_node_id)
- if metadata.state in ("executed", "skipped"):
- return False
- if not self._state._has_rejected_activation_dependency(exec_node_id):
- return False
-
- self.remove_from_ready_queues(exec_node_id)
- self._state._set_prepared_exec_state(exec_node_id, "skipped")
- self._state._tx_add_set(self._state.executed, exec_node_id)
-
- registry = self._state._prepared_registry()
- source_node_id = registry.get_source_node_id(exec_node_id)
- prepared_nodes = registry.get_prepared_ids(source_node_id)
- if all(node_id in self._state.executed for node_id in prepared_nodes):
- if source_node_id not in self._state.executed:
- self._state._mark_source_executed(source_node_id)
- self.mark_skipped(exec_node_id)
- return True
-
- def _discard_rejected_activation_nodes(self) -> None:
- for exec_node_id in tuple(nx.topological_sort(self._state._get_execution_graph_flat())):
- self._discard_rejected_activation_node(exec_node_id)
-
- def _enqueue_activation_ready_nodes(self) -> None:
- for exec_node_id in tuple(self._state.prepared_source_mapping):
- if exec_node_id not in self._state.indegree or exec_node_id in self._state.executed:
- continue
- self.enqueue_if_ready(exec_node_id)
-
- def _get_ready_queue(self, exec_node_id: str) -> Deque[str]:
- node_obj = self._state.execution_graph.nodes[exec_node_id]
- return self.queue_for(self._state._type_key(node_obj))
-
- def _insert_ready_node(self, queue: Deque[str], exec_node_id: str) -> None:
- exec_node_path = self._state._get_iteration_path(exec_node_id)
- if not queue or self._state._get_iteration_path(queue[-1]) <= exec_node_path:
- self._state._tx_queue_append(queue, exec_node_id)
- return
- for i, existing in enumerate(queue):
- if self._state._get_iteration_path(existing) > exec_node_path:
- self._state._tx_queue_insert(queue, i, exec_node_id)
- return
- self._state._tx_queue_append(queue, exec_node_id)
-
- def _record_completed_node(self, exec_node_id: str, output: BaseInvocationOutput) -> None:
- self._state._set_prepared_exec_state(exec_node_id, "executed")
- self._state._tx_add_set(self._state.executed, exec_node_id)
- self._state._tx_set_mapping(self._state.results, exec_node_id, output)
- # Keep the generic stream ledger in sync while the materializer remains
- # the compatibility authority for runtime node creation.
- if isinstance(self._state.execution_graph.nodes.get(exec_node_id), IterateInvocation):
- self._state._record_iterate_stream(exec_node_id, output)
- if isinstance(self._state.execution_graph.nodes.get(exec_node_id), ForInvocation):
- for_node = self._state.execution_graph.nodes[exec_node_id]
- assert isinstance(for_node, ForInvocation)
- if for_node.index >= 0:
- self._state._for_continuation(exec_node_id)
- node = self._state.execution_graph.nodes[exec_node_id]
- if isinstance(node, (IterateInvocation, CollectInvocation)):
- self._state._tx_set_attr(node, "collection", [])
-
- def _mark_source_node_complete(self, exec_node_id: str) -> None:
- registry = self._state._prepared_registry()
- source_node_id = registry.get_source_node_id(exec_node_id)
- prepared_nodes = registry.get_prepared_ids(source_node_id)
- if (
- all(node_id in self._state.executed for node_id in prepared_nodes)
- and source_node_id not in self._state.executed
- ):
- self._state._mark_source_executed(source_node_id)
-
- def _try_schedule_next_for_iteration(self, exec_node_id: str, output: BaseInvocationOutput) -> Optional[str]:
- return self._state._try_schedule_next_for_iteration(exec_node_id, output)
-
- def _try_materialize_deferred_nested_for_body(self, exec_node_id: str) -> None:
- completed_source_id = self._state._prepared_registry().get_source_node_id(exec_node_id)
- graph = self._state._get_source_graph_flat()
- for source_for_id, source_node in self._state.graph.nodes.items():
- if not isinstance(source_node, ForInvocation):
- continue
- nested_body = self._state.graph._get_supported_for_nested_iterate_body(source_for_id, graph)
- nested_for_body = self._state.graph._get_supported_for_nested_for_body(source_for_id, graph)
- if nested_body is not None:
- body_path_nodes = nested_body.body_path_nodes
- deferred_node_ids = (nested_body.iterate_node_id,)
- elif nested_for_body is None:
- continue
- else:
- body_path_nodes = nested_for_body.body_path_nodes
- deferred_node_ids = nested_for_body.inner_for_ids
- if completed_source_id != source_for_id and (
- completed_source_id not in body_path_nodes
- or not any(
- nx.has_path(graph, completed_source_id, deferred_node_id) for deferred_node_id in deferred_node_ids
- )
- ):
- continue
- for prepared_for_id in self._state._prepared_registry().get_prepared_ids(source_for_id):
- prepared_for_node = self._state.execution_graph.get_node(prepared_for_id)
- prepared_for_path = self._state._get_iteration_path(prepared_for_id)
- if isinstance(prepared_for_node, ForInvocation) and prepared_for_node.index >= 0:
- prepared_for_path = (
- *self._state._get_for_parent_iteration_path(prepared_for_id),
- prepared_for_node.index,
- )
- if prepared_for_path != self._state._get_iteration_path(exec_node_id):
- continue
- if nested_for_body is not None:
- if not all(
- any(
- self._state._get_for_parent_iteration_path(prepared_child_id) == prepared_for_path
- for prepared_child_id in self._state._prepared_registry().get_prepared_ids(child_id)
- )
- for child_id in deferred_node_ids
- ):
- self._state._materializer().create_for_body_iteration(
- source_for_id=source_for_id, prepared_for_id=prepared_for_id
- )
- return
- continue
- if any(
- (iterate_path := self._state._get_iteration_path(prepared_iterate_id))[: len(prepared_for_path)]
- == prepared_for_path
- and len(iterate_path) > len(prepared_for_path)
- for prepared_iterate_id in self._state._prepared_registry().get_prepared_ids(deferred_node_ids[0])
- ):
- continue
- self._state._materializer().create_for_body_iteration(
- source_for_id=source_for_id, prepared_for_id=prepared_for_id
- )
- return
-
- def _decrement_child_indegree(self, child_exec_node_id: str, parent_exec_node_id: str) -> None:
- if child_exec_node_id not in self._state.indegree:
- raise KeyError(f"indegree missing for exec node {child_exec_node_id}")
- if self._state.indegree[child_exec_node_id] == 0:
- raise RuntimeError(f"indegree underflow for {child_exec_node_id} from parent {parent_exec_node_id}")
- self._state._tx_set_mapping(
- self._state.indegree, child_exec_node_id, self._state.indegree[child_exec_node_id] - 1
- )
-
- def _release_downstream_nodes(self, exec_node_id: str) -> None:
- for edge in self._state.execution_graph._get_output_edges(exec_node_id):
- child = edge.destination.node_id
- self._decrement_child_indegree(child, exec_node_id)
- self._state._try_resolve_if_node(child)
- if self._state.indegree[child] == 0:
- self.enqueue_if_ready(child)
-
- def queue_for(self, cls_name: str) -> Deque[str]:
- q = self._state._ready_queues.get(cls_name)
- if q is None:
- q = deque()
- self._state._tx_set_mapping(self._state._ready_queues, cls_name, q)
- return q
-
- def remove_from_ready_queues(self, exec_node_id: str) -> None:
- for q in self._state._ready_queues.values():
- try:
- self._state._tx_queue_remove(q, exec_node_id)
- except ValueError:
- continue
- self._state._tx_discard_set(self._state._ready_node_ids, exec_node_id)
-
- def mark_skipped(self, exec_node_id: str) -> None:
- """Satisfy downstream indegrees without resolving inputs from a skipped node."""
- self._validate_exec_node_ready_state(exec_node_id)
- for edge in self._state.execution_graph._get_output_edges(exec_node_id):
- child = edge.destination.node_id
- self._decrement_child_indegree(child, exec_node_id)
- if self._state.indegree[child] == 0:
- self.enqueue_if_ready(child)
-
- def enqueue_if_ready(self, exec_node_id: str) -> None:
- """Push exec_node_id to its class queue if unmet inputs == 0."""
- self._validate_exec_node_ready_state(exec_node_id)
- if self._discard_rejected_activation_node(exec_node_id):
- return
- if self._should_skip_ready_enqueue(exec_node_id):
- return
- queue = self._get_ready_queue(exec_node_id)
- if exec_node_id in self._state._ready_node_ids:
- return
- self._state._set_prepared_exec_state(exec_node_id, "ready")
- self._insert_ready_node(queue, exec_node_id)
- self._state._tx_add_set(self._state._ready_node_ids, exec_node_id)
-
- def get_next_node(self) -> Optional[BaseInvocation]:
- """Gets the next ready node: FIFO within class, drain class before switching."""
- while True:
- if not self._state._active_class and self._state.results:
- last_exec_node_id = next(reversed(self._state.results))
- last_node = self._state.execution_graph.nodes.get(last_exec_node_id)
- if last_node is not None:
- last_class = self._state._type_key(last_node)
- if self._state._ready_queues.get(last_class):
- self._state._active_class = last_class
- if self._state._active_class:
- q = self._state._ready_queues.get(self._state._active_class)
- while q:
- exec_node_id = q.popleft()
- self._state._ready_node_ids.discard(exec_node_id)
- if exec_node_id not in self._state.executed:
- return self._state.execution_graph.nodes[exec_node_id]
- self._state._active_class = None
- continue
-
- seen = set(self._state.ready_order)
- next_class = next(
- (cls_name for cls_name in self._state.ready_order if self._state._ready_queues.get(cls_name)),
- None,
- )
- if next_class is None:
- next_class = next(
- (
- cls_name
- for cls_name in sorted(k for k in self._state._ready_queues.keys() if k not in seen)
- if self._state._ready_queues[cls_name]
- ),
- None,
- )
- if next_class is None:
- return None
-
- self._state._active_class = next_class
-
- def complete(
- self, exec_node_id: str, output: BaseInvocationOutput
- ) -> list[tuple[BaseInvocation, BaseInvocationOutput]]:
- if exec_node_id not in self._state.execution_graph.nodes:
- return []
-
- self._record_completed_node(exec_node_id, output)
- finalized_for_exec_node_id = self._try_schedule_next_for_iteration(exec_node_id, output)
- self._mark_source_node_complete(exec_node_id)
- self._release_downstream_nodes(exec_node_id)
- completed_node = self._state.execution_graph.get_node(exec_node_id)
- if isinstance(completed_node, ForInvocation) and completed_node.index >= 0:
- source_for_id = self._state._prepared_registry().get_source_node_id(exec_node_id)
- nested_body = self._state.graph._get_supported_for_nested_iterate_body(
- source_for_id, self._state._get_source_graph_flat()
- )
- prepared_for_node = self._state.execution_graph.get_node(exec_node_id)
- prepared_for_path = self._state._get_iteration_path(exec_node_id)
- if isinstance(prepared_for_node, ForInvocation) and prepared_for_node.index >= 0:
- prepared_for_path = (
- *self._state._get_for_parent_iteration_path(exec_node_id),
- prepared_for_node.index,
- )
- if nested_body is not None and not any(
- (iterate_path := self._state._get_iteration_path(prepared_iterate_id))[: len(prepared_for_path)]
- == prepared_for_path
- and len(iterate_path) > len(prepared_for_path)
- for prepared_iterate_id in self._state._prepared_registry().get_prepared_ids(
- nested_body.iterate_node_id
- )
- ):
- self._state._materializer().create_for_body_iteration(
- source_for_id=source_for_id, prepared_for_id=exec_node_id
- )
- elif nested_body is None:
- self._try_materialize_deferred_nested_for_body(exec_node_id)
- else:
- self._try_materialize_deferred_nested_for_body(exec_node_id)
- if finalized_for_exec_node_id is None:
- return []
- finalized_for_node = self._state.execution_graph.get_node(finalized_for_exec_node_id)
- finalized_for_output = self._state.results.get(finalized_for_exec_node_id)
- if not isinstance(finalized_for_node, ForInvocation) or not isinstance(
- finalized_for_output, ForInvocationOutput
- ):
- return []
- return [(finalized_for_node, finalized_for_output)]
-
-
-class _GenericGraphSchedulerAdapter:
- """Projects the generic scheduler into GraphExecutionState's legacy queues."""
-
- def __init__(self, state: "GraphExecutionState") -> None:
- self._state = state
- self._initializing = True
- self._scheduler = ExecutionScheduler(
- ExecutionPlan(),
- state.ready_order,
- ready_predicate=self._is_node_activation_ready,
- )
- self._register_existing_nodes()
- prepared_ids = set(state.prepared_source_mapping).intersection(state.results)
- 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.discarded = discarded_ids
- self._scheduler.rebuild_ready()
- self._initializing = False
- self._sync_indegree()
- self._project_ready_nodes()
- self._restore_active_class()
-
- def _restore_active_class(self) -> None:
- """Continue draining the class that produced the latest durable result."""
-
- if not self._state.results:
- return
- last_exec_node_id = next(reversed(self._state.results))
- last_plan_node = self._scheduler.plan.nodes.get(last_exec_node_id)
- if last_plan_node is None:
- return
- self._scheduler._active_class = last_plan_node.class_name
-
- def _is_node_activation_ready(self, exec_node_id: str) -> bool:
- """Require every frame-local activation dependency to be satisfied."""
-
- if self._state._is_pending_if(exec_node_id):
- return False
- if not all(
- self._state._is_activation_dependency_satisfied(dependency)
- for dependency in self._scheduler.plan.nodes[exec_node_id].activation_dependencies
- ):
- return False
- node = self._state.execution_graph.nodes[exec_node_id]
- return not isinstance(node, CollectInvocation) or self._state._collect_streams_ready(exec_node_id)
-
- def _retire_unselected_node(self, exec_node_id: str) -> None:
- """Retire one unselected prepared node through generic scheduler state."""
-
- if exec_node_id not in self._scheduler.plan.nodes or exec_node_id in self._state.executed:
- return
- if exec_node_id in self._scheduler.discarded:
- return
- self._remove_projected(exec_node_id)
- self._state._set_prepared_exec_state(exec_node_id, "skipped")
- self._state._tx_add_set(self._state.executed, exec_node_id)
- self._scheduler.discard(exec_node_id)
- source_node_id = self._state._prepared_registry().get_source_node_id(exec_node_id)
- prepared_nodes = self._state._prepared_registry().get_prepared_ids(source_node_id)
- if all(node_id in self._state.executed for node_id in prepared_nodes):
- if source_node_id not in self._state.executed:
- self._state._mark_source_executed(source_node_id)
-
- def resolve_if_node(self, exec_node_id: str) -> None:
- """Resolve legacy If inputs without pruning graph edges or using type-specific skips."""
-
- if exec_node_id in self._state._resolved_if_exec_branches:
- return
- node = self._state.execution_graph.get_node(exec_node_id)
- if not isinstance(node, IfInvocation):
- return
- if not self._state._apply_if_condition_inputs(exec_node_id, node):
- return
-
- selected_field = "true_input" if node.condition else "false_input"
- self._state._resolve_activation_gate(exec_node_id, selected_field)
- self._state._tx_set_mapping(self._state._resolved_if_exec_branches, exec_node_id, selected_field)
- self._state._record_compatibility_activation_token(exec_node_id, selected_field)
- self._discard_rejected_nodes()
- self._sync_indegree()
- self._project_ready_nodes()
-
- def _discard_rejected_node(self, exec_node_id: str) -> None:
- node = self._scheduler.plan.nodes.get(exec_node_id)
- if node is None or not node.activation_dependencies:
- return
- if any(
- self._state._is_activation_dependency_rejected(dependency) for dependency in node.activation_dependencies
- ):
- self._retire_unselected_node(exec_node_id)
-
- def _discard_rejected_nodes(self) -> None:
- """Discard plan nodes whose opaque activation requirements are rejected."""
-
- for exec_node_id in tuple(self._scheduler.plan.nodes):
- self._discard_rejected_node(exec_node_id)
-
- def _sync_executed_state(self, excluded: Iterable[str] = ()) -> None:
- excluded_ids = set(excluded)
- self._scheduler.executed.update(
- exec_node_id
- for exec_node_id in self._state.results
- if exec_node_id not in excluded_ids and exec_node_id in self._scheduler.plan.nodes
- )
- self._scheduler.rebuild_ready()
- self._sync_indegree()
-
- def _register_existing_nodes(self) -> None:
- execution_graph = self._state._get_execution_graph_flat()
- for exec_node_id in nx.topological_sort(execution_graph):
- self.register_node(exec_node_id)
-
- def register_node(self, exec_node_id: str) -> None:
- if exec_node_id in self._scheduler.plan.nodes:
- return
- node = self._state.execution_graph.nodes.get(exec_node_id)
- if node is None:
- raise KeyError(f"exec node {exec_node_id} missing from execution_graph")
- dependencies = tuple(edge.source.node_id for edge in self._state.execution_graph._get_input_edges(exec_node_id))
- activation_dependencies = self._state._get_activation_dependencies(exec_node_id)
- for dependency in dependencies:
- if dependency not in self._scheduler.plan.nodes:
- self.register_node(dependency)
- if exec_node_id in self._state.executed:
- self._scheduler.executed.add(exec_node_id)
- self._scheduler.add_node(
- self._scheduler.plan.add_node(
- exec_node_id,
- type(node).__name__,
- self._state._get_iteration_path(exec_node_id),
- dependencies,
- activation_dependencies,
- )
- )
- self._state._tx_set_mapping(self._state.indegree, exec_node_id, self._scheduler.indegree[exec_node_id])
- self._discard_rejected_node(exec_node_id)
- if not self._initializing:
- self._project_ready_node(exec_node_id)
-
- def _sync_indegree(self) -> None:
- for exec_node_id, degree in self._scheduler.indegree.items():
- self._state._tx_set_mapping(self._state.indegree, exec_node_id, degree)
-
- def _project_ready_node(self, exec_node_id: str) -> None:
- if exec_node_id not in self._scheduler.ready_ids or exec_node_id in self._state._ready_node_ids:
- return
- node = self._state.execution_graph.nodes[exec_node_id]
- cls_name = self._state._type_key(node)
- queue = self._state._ready_queues.get(cls_name)
- if queue is None:
- queue = deque()
- self._state._tx_set_mapping(self._state._ready_queues, cls_name, queue)
- iteration_path = self._state._get_iteration_path(exec_node_id)
- insert_at = next(
- (
- index
- for index, queued_id in enumerate(queue)
- if self._state._get_iteration_path(queued_id) > iteration_path
- ),
- len(queue),
- )
- if insert_at == len(queue):
- self._state._tx_queue_append(queue, exec_node_id)
- else:
- self._state._tx_queue_insert(queue, insert_at, exec_node_id)
- self._state._set_prepared_exec_state(exec_node_id, "ready")
- self._state._tx_add_set(self._state._ready_node_ids, exec_node_id)
-
- def _project_ready_nodes(self) -> None:
- for exec_node_id in self._scheduler.ready_ids:
- self._project_ready_node(exec_node_id)
-
- def _remove_projected(self, exec_node_id: str) -> None:
- for queue in self._state._ready_queues.values():
- try:
- self._state._tx_queue_remove(queue, exec_node_id)
- except ValueError:
- continue
- self._state._tx_discard_set(self._state._ready_node_ids, exec_node_id)
-
- def queue_for(self, cls_name: str) -> Deque[str]:
- queue = self._state._ready_queues.get(cls_name)
- if queue is None:
- queue = deque()
- self._state._tx_set_mapping(self._state._ready_queues, cls_name, queue)
- return queue
-
- def remove_from_ready_queues(self, exec_node_id: str) -> None:
- self._scheduler.discard(exec_node_id)
- self._remove_projected(exec_node_id)
-
- def mark_skipped(self, exec_node_id: str) -> None:
- """Mirror a compatibility skip in the opaque scheduler projection."""
-
- self._scheduler.discard(exec_node_id)
- self._sync_indegree()
- self._project_ready_nodes()
-
- def enqueue_if_ready(self, exec_node_id: str) -> None:
- self.register_node(exec_node_id)
- if self._state.indegree.get(exec_node_id) != 0 or exec_node_id in self._state.executed:
- return
- self._scheduler.enqueue(exec_node_id)
- self._project_ready_node(exec_node_id)
-
- def get_next_node(self) -> Optional[BaseInvocation]:
- exec_node_id = self._scheduler.pop_next()
- if exec_node_id is None:
- return None
- self._remove_projected(exec_node_id)
- if exec_node_id in self._state.executed:
- return self.get_next_node()
- return self._state.execution_graph.nodes[exec_node_id]
-
- def _record_completed_node(self, exec_node_id: str, output: BaseInvocationOutput) -> None:
- node = self._state.execution_graph.nodes.get(exec_node_id)
- self._state._set_prepared_exec_state(exec_node_id, "executed")
- self._state._tx_add_set(self._state.executed, exec_node_id)
- self._state._tx_set_mapping(self._state.results, exec_node_id, output)
- if isinstance(node, ForInvocation) and node.index >= 0:
- self._state._for_continuation(exec_node_id)
- if isinstance(node, IterateInvocation):
- self._state._record_iterate_stream(exec_node_id, output)
- if isinstance(node, (IterateInvocation, CollectInvocation)):
- self._state._tx_set_attr(node, "collection", [])
-
- def _mark_source_node_complete(self, exec_node_id: str) -> None:
- registry = self._state._prepared_registry()
- source_node_id = registry.get_source_node_id(exec_node_id)
- prepared_nodes = registry.get_prepared_ids(source_node_id)
- if (
- all(node_id in self._state.executed for node_id in prepared_nodes)
- and source_node_id not in self._state.executed
- ):
- self._state._mark_source_executed(source_node_id)
-
- def complete(
- self, exec_node_id: str, output: BaseInvocationOutput
- ) -> list[tuple[BaseInvocation, BaseInvocationOutput]]:
- if exec_node_id not in self._state.execution_graph.nodes:
- return []
- # A few compatibility callers register prepared execution nodes directly on the
- # execution graph. Ensure those nodes are represented in the opaque plan before
- # applying their completion, just as materialized nodes are.
- if exec_node_id not in self._scheduler.plan.nodes:
- self.register_node(exec_node_id)
- if exec_node_id not in self._state.indegree:
- raise KeyError(f"indegree missing for exec node {exec_node_id}")
- if exec_node_id in self._scheduler.executed:
- # Compatibility callers may advance with the invocation result and then replace that result explicitly.
- self._state._tx_set_mapping(self._state.results, exec_node_id, output)
- return []
- dependents = self._scheduler.plan.dependents(exec_node_id)
- for dependent in dependents:
- if dependent not in self._state.indegree:
- raise KeyError(f"indegree missing for exec node {dependent}")
- self._remove_projected(exec_node_id)
- self._record_completed_node(exec_node_id, output)
- finalized_for_exec_node_id = self._state._apply_generic_for_continuation(exec_node_id, output)
- self._mark_source_node_complete(exec_node_id)
- # A condition may become resolvable when this node completes. Resolve it
- # while the state has the completed result, before recalculating generic
- # indegrees and readiness.
- for dependent in dependents:
- self._state._try_resolve_if_node(dependent)
- self._sync_executed_state(excluded=(exec_node_id,))
- newly_ready = self._scheduler.complete(exec_node_id)
- for dependent in set(dependents):
- self._state._tx_set_mapping(self._state.indegree, dependent, self._scheduler.indegree[dependent])
- for ready_node_id in newly_ready:
- self._project_ready_node(ready_node_id)
- _ExecutionScheduler._try_materialize_deferred_nested_for_body(self, exec_node_id)
- if finalized_for_exec_node_id is None:
- return []
- finalized_for_node = self._state.execution_graph.get_node(finalized_for_exec_node_id)
- finalized_for_output = self._state.results.get(finalized_for_exec_node_id)
- if not isinstance(finalized_for_node, ForInvocation) or not isinstance(
- finalized_for_output, ForInvocationOutput
- ):
- return []
- return [(finalized_for_node, finalized_for_output)]
-
- def set_ready_order(self, ready_order: Iterable[str]) -> None:
- self._scheduler.set_ready_order(ready_order)
- self._project_ready_nodes()
-
-
class GraphExecutionState(BaseModel):
"""Tracks source-graph expansion, execution progress, and runtime results."""
diff --git a/invokeai/app/services/shared/graph_scheduler.py b/invokeai/app/services/shared/graph_scheduler.py
new file mode 100644
index 0000000000..b6ace14a74
--- /dev/null
+++ b/invokeai/app/services/shared/graph_scheduler.py
@@ -0,0 +1,610 @@
+# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
+
+"""Scheduler implementations used by the graph execution-state façade."""
+
+from collections import deque
+from typing import TYPE_CHECKING, Deque, Iterable, Optional
+
+from invokeai.app.invocations.baseinvocation import BaseInvocation, BaseInvocationOutput
+from invokeai.app.invocations.logic import IfInvocation
+from invokeai.app.invocations.loops import ForInvocation, ForInvocationOutput
+from invokeai.app.services.shared.execution_engine.scheduler import (
+ ExecutionPlan,
+ ExecutionScheduler,
+)
+from invokeai.app.services.shared.graph_validation import CollectInvocation, IterateInvocation, nx
+
+if TYPE_CHECKING:
+ from invokeai.app.services.shared.graph import GraphExecutionState
+
+
+class _ExecutionScheduler:
+ """Owns ready-queue ordering and indegree-driven execution transitions."""
+
+ def __init__(self, state: "GraphExecutionState") -> None:
+ self._state = state
+
+ def _validate_exec_node_ready_state(self, exec_node_id: str) -> None:
+ if exec_node_id not in self._state.execution_graph.nodes:
+ raise KeyError(f"exec node {exec_node_id} missing from execution_graph")
+ if exec_node_id not in self._state.indegree:
+ raise KeyError(f"indegree missing for exec node {exec_node_id}")
+
+ def _should_skip_ready_enqueue(self, exec_node_id: str) -> bool:
+ if (
+ self._state.indegree[exec_node_id] != 0
+ or exec_node_id in self._state.executed
+ or self._state._is_pending_if(exec_node_id)
+ or self._state._is_deferred_by_unresolved_if(exec_node_id)
+ ):
+ return True
+ node = self._state.execution_graph.nodes[exec_node_id]
+ return isinstance(node, CollectInvocation) and not self._state._collect_streams_ready(exec_node_id)
+
+ def _discard_rejected_activation_node(self, exec_node_id: str) -> bool:
+ if exec_node_id not in self._state.indegree:
+ return False
+ metadata = self._state._get_prepared_exec_metadata(exec_node_id)
+ if metadata.state in ("executed", "skipped"):
+ return False
+ if not self._state._has_rejected_activation_dependency(exec_node_id):
+ return False
+
+ self.remove_from_ready_queues(exec_node_id)
+ self._state._set_prepared_exec_state(exec_node_id, "skipped")
+ self._state._tx_add_set(self._state.executed, exec_node_id)
+
+ registry = self._state._prepared_registry()
+ source_node_id = registry.get_source_node_id(exec_node_id)
+ prepared_nodes = registry.get_prepared_ids(source_node_id)
+ if all(node_id in self._state.executed for node_id in prepared_nodes):
+ if source_node_id not in self._state.executed:
+ self._state._mark_source_executed(source_node_id)
+ self.mark_skipped(exec_node_id)
+ return True
+
+ def _discard_rejected_activation_nodes(self) -> None:
+ for exec_node_id in tuple(nx.topological_sort(self._state._get_execution_graph_flat())):
+ self._discard_rejected_activation_node(exec_node_id)
+
+ def _enqueue_activation_ready_nodes(self) -> None:
+ for exec_node_id in tuple(self._state.prepared_source_mapping):
+ if exec_node_id not in self._state.indegree or exec_node_id in self._state.executed:
+ continue
+ self.enqueue_if_ready(exec_node_id)
+
+ def _get_ready_queue(self, exec_node_id: str) -> Deque[str]:
+ node_obj = self._state.execution_graph.nodes[exec_node_id]
+ return self.queue_for(self._state._type_key(node_obj))
+
+ def _insert_ready_node(self, queue: Deque[str], exec_node_id: str) -> None:
+ exec_node_path = self._state._get_iteration_path(exec_node_id)
+ if not queue or self._state._get_iteration_path(queue[-1]) <= exec_node_path:
+ self._state._tx_queue_append(queue, exec_node_id)
+ return
+ for i, existing in enumerate(queue):
+ if self._state._get_iteration_path(existing) > exec_node_path:
+ self._state._tx_queue_insert(queue, i, exec_node_id)
+ return
+ self._state._tx_queue_append(queue, exec_node_id)
+
+ def _record_completed_node(self, exec_node_id: str, output: BaseInvocationOutput) -> None:
+ self._state._set_prepared_exec_state(exec_node_id, "executed")
+ self._state._tx_add_set(self._state.executed, exec_node_id)
+ self._state._tx_set_mapping(self._state.results, exec_node_id, output)
+ # Keep the generic stream ledger in sync while the materializer remains
+ # the compatibility authority for runtime node creation.
+ if isinstance(self._state.execution_graph.nodes.get(exec_node_id), IterateInvocation):
+ self._state._record_iterate_stream(exec_node_id, output)
+ if isinstance(self._state.execution_graph.nodes.get(exec_node_id), ForInvocation):
+ for_node = self._state.execution_graph.nodes[exec_node_id]
+ assert isinstance(for_node, ForInvocation)
+ if for_node.index >= 0:
+ self._state._for_continuation(exec_node_id)
+ node = self._state.execution_graph.nodes[exec_node_id]
+ if isinstance(node, (IterateInvocation, CollectInvocation)):
+ self._state._tx_set_attr(node, "collection", [])
+
+ def _mark_source_node_complete(self, exec_node_id: str) -> None:
+ registry = self._state._prepared_registry()
+ source_node_id = registry.get_source_node_id(exec_node_id)
+ prepared_nodes = registry.get_prepared_ids(source_node_id)
+ if (
+ all(node_id in self._state.executed for node_id in prepared_nodes)
+ and source_node_id not in self._state.executed
+ ):
+ self._state._mark_source_executed(source_node_id)
+
+ def _try_schedule_next_for_iteration(self, exec_node_id: str, output: BaseInvocationOutput) -> Optional[str]:
+ return self._state._try_schedule_next_for_iteration(exec_node_id, output)
+
+ def _try_materialize_deferred_nested_for_body(self, exec_node_id: str) -> None:
+ completed_source_id = self._state._prepared_registry().get_source_node_id(exec_node_id)
+ graph = self._state._get_source_graph_flat()
+ for source_for_id, source_node in self._state.graph.nodes.items():
+ if not isinstance(source_node, ForInvocation):
+ continue
+ nested_body = self._state.graph._get_supported_for_nested_iterate_body(source_for_id, graph)
+ nested_for_body = self._state.graph._get_supported_for_nested_for_body(source_for_id, graph)
+ if nested_body is not None:
+ body_path_nodes = nested_body.body_path_nodes
+ deferred_node_ids = (nested_body.iterate_node_id,)
+ elif nested_for_body is None:
+ continue
+ else:
+ body_path_nodes = nested_for_body.body_path_nodes
+ deferred_node_ids = nested_for_body.inner_for_ids
+ if completed_source_id != source_for_id and (
+ completed_source_id not in body_path_nodes
+ or not any(
+ nx.has_path(graph, completed_source_id, deferred_node_id) for deferred_node_id in deferred_node_ids
+ )
+ ):
+ continue
+ for prepared_for_id in self._state._prepared_registry().get_prepared_ids(source_for_id):
+ prepared_for_node = self._state.execution_graph.get_node(prepared_for_id)
+ prepared_for_path = self._state._get_iteration_path(prepared_for_id)
+ if isinstance(prepared_for_node, ForInvocation) and prepared_for_node.index >= 0:
+ prepared_for_path = (
+ *self._state._get_for_parent_iteration_path(prepared_for_id),
+ prepared_for_node.index,
+ )
+ if prepared_for_path != self._state._get_iteration_path(exec_node_id):
+ continue
+ if nested_for_body is not None:
+ if not all(
+ any(
+ self._state._get_for_parent_iteration_path(prepared_child_id) == prepared_for_path
+ for prepared_child_id in self._state._prepared_registry().get_prepared_ids(child_id)
+ )
+ for child_id in deferred_node_ids
+ ):
+ self._state._materializer().create_for_body_iteration(
+ source_for_id=source_for_id, prepared_for_id=prepared_for_id
+ )
+ return
+ continue
+ if any(
+ (iterate_path := self._state._get_iteration_path(prepared_iterate_id))[: len(prepared_for_path)]
+ == prepared_for_path
+ and len(iterate_path) > len(prepared_for_path)
+ for prepared_iterate_id in self._state._prepared_registry().get_prepared_ids(deferred_node_ids[0])
+ ):
+ continue
+ self._state._materializer().create_for_body_iteration(
+ source_for_id=source_for_id, prepared_for_id=prepared_for_id
+ )
+ return
+
+ def _decrement_child_indegree(self, child_exec_node_id: str, parent_exec_node_id: str) -> None:
+ if child_exec_node_id not in self._state.indegree:
+ raise KeyError(f"indegree missing for exec node {child_exec_node_id}")
+ if self._state.indegree[child_exec_node_id] == 0:
+ raise RuntimeError(f"indegree underflow for {child_exec_node_id} from parent {parent_exec_node_id}")
+ self._state._tx_set_mapping(
+ self._state.indegree, child_exec_node_id, self._state.indegree[child_exec_node_id] - 1
+ )
+
+ def _release_downstream_nodes(self, exec_node_id: str) -> None:
+ for edge in self._state.execution_graph._get_output_edges(exec_node_id):
+ child = edge.destination.node_id
+ self._decrement_child_indegree(child, exec_node_id)
+ self._state._try_resolve_if_node(child)
+ if self._state.indegree[child] == 0:
+ self.enqueue_if_ready(child)
+
+ def queue_for(self, cls_name: str) -> Deque[str]:
+ q = self._state._ready_queues.get(cls_name)
+ if q is None:
+ q = deque()
+ self._state._tx_set_mapping(self._state._ready_queues, cls_name, q)
+ return q
+
+ def remove_from_ready_queues(self, exec_node_id: str) -> None:
+ for q in self._state._ready_queues.values():
+ try:
+ self._state._tx_queue_remove(q, exec_node_id)
+ except ValueError:
+ continue
+ self._state._tx_discard_set(self._state._ready_node_ids, exec_node_id)
+
+ def mark_skipped(self, exec_node_id: str) -> None:
+ """Satisfy downstream indegrees without resolving inputs from a skipped node."""
+ self._validate_exec_node_ready_state(exec_node_id)
+ for edge in self._state.execution_graph._get_output_edges(exec_node_id):
+ child = edge.destination.node_id
+ self._decrement_child_indegree(child, exec_node_id)
+ if self._state.indegree[child] == 0:
+ self.enqueue_if_ready(child)
+
+ def enqueue_if_ready(self, exec_node_id: str) -> None:
+ """Push exec_node_id to its class queue if unmet inputs == 0."""
+ self._validate_exec_node_ready_state(exec_node_id)
+ if self._discard_rejected_activation_node(exec_node_id):
+ return
+ if self._should_skip_ready_enqueue(exec_node_id):
+ return
+ queue = self._get_ready_queue(exec_node_id)
+ if exec_node_id in self._state._ready_node_ids:
+ return
+ self._state._set_prepared_exec_state(exec_node_id, "ready")
+ self._insert_ready_node(queue, exec_node_id)
+ self._state._tx_add_set(self._state._ready_node_ids, exec_node_id)
+
+ def get_next_node(self) -> Optional[BaseInvocation]:
+ """Gets the next ready node: FIFO within class, drain class before switching."""
+ while True:
+ if not self._state._active_class and self._state.results:
+ last_exec_node_id = next(reversed(self._state.results))
+ last_node = self._state.execution_graph.nodes.get(last_exec_node_id)
+ if last_node is not None:
+ last_class = self._state._type_key(last_node)
+ if self._state._ready_queues.get(last_class):
+ self._state._active_class = last_class
+ if self._state._active_class:
+ q = self._state._ready_queues.get(self._state._active_class)
+ while q:
+ exec_node_id = q.popleft()
+ self._state._ready_node_ids.discard(exec_node_id)
+ if exec_node_id not in self._state.executed:
+ return self._state.execution_graph.nodes[exec_node_id]
+ self._state._active_class = None
+ continue
+
+ seen = set(self._state.ready_order)
+ next_class = next(
+ (cls_name for cls_name in self._state.ready_order if self._state._ready_queues.get(cls_name)),
+ None,
+ )
+ if next_class is None:
+ next_class = next(
+ (
+ cls_name
+ for cls_name in sorted(k for k in self._state._ready_queues.keys() if k not in seen)
+ if self._state._ready_queues[cls_name]
+ ),
+ None,
+ )
+ if next_class is None:
+ return None
+
+ self._state._active_class = next_class
+
+ def complete(
+ self, exec_node_id: str, output: BaseInvocationOutput
+ ) -> list[tuple[BaseInvocation, BaseInvocationOutput]]:
+ if exec_node_id not in self._state.execution_graph.nodes:
+ return []
+
+ self._record_completed_node(exec_node_id, output)
+ finalized_for_exec_node_id = self._try_schedule_next_for_iteration(exec_node_id, output)
+ self._mark_source_node_complete(exec_node_id)
+ self._release_downstream_nodes(exec_node_id)
+ completed_node = self._state.execution_graph.get_node(exec_node_id)
+ if isinstance(completed_node, ForInvocation) and completed_node.index >= 0:
+ source_for_id = self._state._prepared_registry().get_source_node_id(exec_node_id)
+ nested_body = self._state.graph._get_supported_for_nested_iterate_body(
+ source_for_id, self._state._get_source_graph_flat()
+ )
+ prepared_for_node = self._state.execution_graph.get_node(exec_node_id)
+ prepared_for_path = self._state._get_iteration_path(exec_node_id)
+ if isinstance(prepared_for_node, ForInvocation) and prepared_for_node.index >= 0:
+ prepared_for_path = (
+ *self._state._get_for_parent_iteration_path(exec_node_id),
+ prepared_for_node.index,
+ )
+ if nested_body is not None and not any(
+ (iterate_path := self._state._get_iteration_path(prepared_iterate_id))[: len(prepared_for_path)]
+ == prepared_for_path
+ and len(iterate_path) > len(prepared_for_path)
+ for prepared_iterate_id in self._state._prepared_registry().get_prepared_ids(
+ nested_body.iterate_node_id
+ )
+ ):
+ self._state._materializer().create_for_body_iteration(
+ source_for_id=source_for_id, prepared_for_id=exec_node_id
+ )
+ elif nested_body is None:
+ self._try_materialize_deferred_nested_for_body(exec_node_id)
+ else:
+ self._try_materialize_deferred_nested_for_body(exec_node_id)
+ if finalized_for_exec_node_id is None:
+ return []
+ finalized_for_node = self._state.execution_graph.get_node(finalized_for_exec_node_id)
+ finalized_for_output = self._state.results.get(finalized_for_exec_node_id)
+ if not isinstance(finalized_for_node, ForInvocation) or not isinstance(
+ finalized_for_output, ForInvocationOutput
+ ):
+ return []
+ return [(finalized_for_node, finalized_for_output)]
+
+
+class _GenericGraphSchedulerAdapter:
+ """Projects the generic scheduler into GraphExecutionState's legacy queues."""
+
+ def __init__(self, state: "GraphExecutionState") -> None:
+ self._state = state
+ self._initializing = True
+ self._scheduler = ExecutionScheduler(
+ ExecutionPlan(),
+ state.ready_order,
+ ready_predicate=self._is_node_activation_ready,
+ )
+ self._register_existing_nodes()
+ prepared_ids = set(state.prepared_source_mapping).intersection(state.results)
+ 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.discarded = discarded_ids
+ self._scheduler.rebuild_ready()
+ self._initializing = False
+ self._sync_indegree()
+ self._project_ready_nodes()
+ self._restore_active_class()
+
+ def _restore_active_class(self) -> None:
+ """Continue draining the class that produced the latest durable result."""
+
+ if not self._state.results:
+ return
+ last_exec_node_id = next(reversed(self._state.results))
+ last_plan_node = self._scheduler.plan.nodes.get(last_exec_node_id)
+ if last_plan_node is None:
+ return
+ self._scheduler._active_class = last_plan_node.class_name
+
+ def _is_node_activation_ready(self, exec_node_id: str) -> bool:
+ """Require every frame-local activation dependency to be satisfied."""
+
+ if self._state._is_pending_if(exec_node_id):
+ return False
+ if not all(
+ self._state._is_activation_dependency_satisfied(dependency)
+ for dependency in self._scheduler.plan.nodes[exec_node_id].activation_dependencies
+ ):
+ return False
+ node = self._state.execution_graph.nodes[exec_node_id]
+ return not isinstance(node, CollectInvocation) or self._state._collect_streams_ready(exec_node_id)
+
+ def _retire_unselected_node(self, exec_node_id: str) -> None:
+ """Retire one unselected prepared node through generic scheduler state."""
+
+ if exec_node_id not in self._scheduler.plan.nodes or exec_node_id in self._state.executed:
+ return
+ if exec_node_id in self._scheduler.discarded:
+ return
+ self._remove_projected(exec_node_id)
+ self._state._set_prepared_exec_state(exec_node_id, "skipped")
+ self._state._tx_add_set(self._state.executed, exec_node_id)
+ self._scheduler.discard(exec_node_id)
+ source_node_id = self._state._prepared_registry().get_source_node_id(exec_node_id)
+ prepared_nodes = self._state._prepared_registry().get_prepared_ids(source_node_id)
+ if all(node_id in self._state.executed for node_id in prepared_nodes):
+ if source_node_id not in self._state.executed:
+ self._state._mark_source_executed(source_node_id)
+
+ def resolve_if_node(self, exec_node_id: str) -> None:
+ """Resolve legacy If inputs without pruning graph edges or using type-specific skips."""
+
+ if exec_node_id in self._state._resolved_if_exec_branches:
+ return
+ node = self._state.execution_graph.get_node(exec_node_id)
+ if not isinstance(node, IfInvocation):
+ return
+ if not self._state._apply_if_condition_inputs(exec_node_id, node):
+ return
+
+ selected_field = "true_input" if node.condition else "false_input"
+ self._state._resolve_activation_gate(exec_node_id, selected_field)
+ self._state._tx_set_mapping(self._state._resolved_if_exec_branches, exec_node_id, selected_field)
+ self._state._record_compatibility_activation_token(exec_node_id, selected_field)
+ self._discard_rejected_nodes()
+ self._sync_indegree()
+ self._project_ready_nodes()
+
+ def _discard_rejected_node(self, exec_node_id: str) -> None:
+ node = self._scheduler.plan.nodes.get(exec_node_id)
+ if node is None or not node.activation_dependencies:
+ return
+ if any(
+ self._state._is_activation_dependency_rejected(dependency) for dependency in node.activation_dependencies
+ ):
+ self._retire_unselected_node(exec_node_id)
+
+ def _discard_rejected_nodes(self) -> None:
+ """Discard plan nodes whose opaque activation requirements are rejected."""
+
+ for exec_node_id in tuple(self._scheduler.plan.nodes):
+ self._discard_rejected_node(exec_node_id)
+
+ def _sync_executed_state(self, excluded: Iterable[str] = ()) -> None:
+ excluded_ids = set(excluded)
+ self._scheduler.executed.update(
+ exec_node_id
+ for exec_node_id in self._state.results
+ if exec_node_id not in excluded_ids and exec_node_id in self._scheduler.plan.nodes
+ )
+ self._scheduler.rebuild_ready()
+ self._sync_indegree()
+
+ def _register_existing_nodes(self) -> None:
+ execution_graph = self._state._get_execution_graph_flat()
+ for exec_node_id in nx.topological_sort(execution_graph):
+ self.register_node(exec_node_id)
+
+ def register_node(self, exec_node_id: str) -> None:
+ if exec_node_id in self._scheduler.plan.nodes:
+ return
+ node = self._state.execution_graph.nodes.get(exec_node_id)
+ if node is None:
+ raise KeyError(f"exec node {exec_node_id} missing from execution_graph")
+ dependencies = tuple(edge.source.node_id for edge in self._state.execution_graph._get_input_edges(exec_node_id))
+ activation_dependencies = self._state._get_activation_dependencies(exec_node_id)
+ for dependency in dependencies:
+ if dependency not in self._scheduler.plan.nodes:
+ self.register_node(dependency)
+ if exec_node_id in self._state.executed:
+ self._scheduler.executed.add(exec_node_id)
+ self._scheduler.add_node(
+ self._scheduler.plan.add_node(
+ exec_node_id,
+ type(node).__name__,
+ self._state._get_iteration_path(exec_node_id),
+ dependencies,
+ activation_dependencies,
+ )
+ )
+ self._state._tx_set_mapping(self._state.indegree, exec_node_id, self._scheduler.indegree[exec_node_id])
+ self._discard_rejected_node(exec_node_id)
+ if not self._initializing:
+ self._project_ready_node(exec_node_id)
+
+ def _sync_indegree(self) -> None:
+ for exec_node_id, degree in self._scheduler.indegree.items():
+ self._state._tx_set_mapping(self._state.indegree, exec_node_id, degree)
+
+ def _project_ready_node(self, exec_node_id: str) -> None:
+ if exec_node_id not in self._scheduler.ready_ids or exec_node_id in self._state._ready_node_ids:
+ return
+ node = self._state.execution_graph.nodes[exec_node_id]
+ cls_name = self._state._type_key(node)
+ queue = self._state._ready_queues.get(cls_name)
+ if queue is None:
+ queue = deque()
+ self._state._tx_set_mapping(self._state._ready_queues, cls_name, queue)
+ iteration_path = self._state._get_iteration_path(exec_node_id)
+ insert_at = next(
+ (
+ index
+ for index, queued_id in enumerate(queue)
+ if self._state._get_iteration_path(queued_id) > iteration_path
+ ),
+ len(queue),
+ )
+ if insert_at == len(queue):
+ self._state._tx_queue_append(queue, exec_node_id)
+ else:
+ self._state._tx_queue_insert(queue, insert_at, exec_node_id)
+ self._state._set_prepared_exec_state(exec_node_id, "ready")
+ self._state._tx_add_set(self._state._ready_node_ids, exec_node_id)
+
+ def _project_ready_nodes(self) -> None:
+ for exec_node_id in self._scheduler.ready_ids:
+ self._project_ready_node(exec_node_id)
+
+ def _remove_projected(self, exec_node_id: str) -> None:
+ for queue in self._state._ready_queues.values():
+ try:
+ self._state._tx_queue_remove(queue, exec_node_id)
+ except ValueError:
+ continue
+ self._state._tx_discard_set(self._state._ready_node_ids, exec_node_id)
+
+ def queue_for(self, cls_name: str) -> Deque[str]:
+ queue = self._state._ready_queues.get(cls_name)
+ if queue is None:
+ queue = deque()
+ self._state._tx_set_mapping(self._state._ready_queues, cls_name, queue)
+ return queue
+
+ def remove_from_ready_queues(self, exec_node_id: str) -> None:
+ self._scheduler.discard(exec_node_id)
+ self._remove_projected(exec_node_id)
+
+ def mark_skipped(self, exec_node_id: str) -> None:
+ """Mirror a compatibility skip in the opaque scheduler projection."""
+
+ self._scheduler.discard(exec_node_id)
+ self._sync_indegree()
+ self._project_ready_nodes()
+
+ def enqueue_if_ready(self, exec_node_id: str) -> None:
+ self.register_node(exec_node_id)
+ if self._state.indegree.get(exec_node_id) != 0 or exec_node_id in self._state.executed:
+ return
+ self._scheduler.enqueue(exec_node_id)
+ self._project_ready_node(exec_node_id)
+
+ def get_next_node(self) -> Optional[BaseInvocation]:
+ exec_node_id = self._scheduler.pop_next()
+ if exec_node_id is None:
+ return None
+ self._remove_projected(exec_node_id)
+ if exec_node_id in self._state.executed:
+ return self.get_next_node()
+ return self._state.execution_graph.nodes[exec_node_id]
+
+ def _record_completed_node(self, exec_node_id: str, output: BaseInvocationOutput) -> None:
+ node = self._state.execution_graph.nodes.get(exec_node_id)
+ self._state._set_prepared_exec_state(exec_node_id, "executed")
+ self._state._tx_add_set(self._state.executed, exec_node_id)
+ self._state._tx_set_mapping(self._state.results, exec_node_id, output)
+ if isinstance(node, ForInvocation) and node.index >= 0:
+ self._state._for_continuation(exec_node_id)
+ if isinstance(node, IterateInvocation):
+ self._state._record_iterate_stream(exec_node_id, output)
+ if isinstance(node, (IterateInvocation, CollectInvocation)):
+ self._state._tx_set_attr(node, "collection", [])
+
+ def _mark_source_node_complete(self, exec_node_id: str) -> None:
+ registry = self._state._prepared_registry()
+ source_node_id = registry.get_source_node_id(exec_node_id)
+ prepared_nodes = registry.get_prepared_ids(source_node_id)
+ if (
+ all(node_id in self._state.executed for node_id in prepared_nodes)
+ and source_node_id not in self._state.executed
+ ):
+ self._state._mark_source_executed(source_node_id)
+
+ def complete(
+ self, exec_node_id: str, output: BaseInvocationOutput
+ ) -> list[tuple[BaseInvocation, BaseInvocationOutput]]:
+ if exec_node_id not in self._state.execution_graph.nodes:
+ return []
+ # A few compatibility callers register prepared execution nodes directly on the
+ # execution graph. Ensure those nodes are represented in the opaque plan before
+ # applying their completion, just as materialized nodes are.
+ if exec_node_id not in self._scheduler.plan.nodes:
+ self.register_node(exec_node_id)
+ if exec_node_id not in self._state.indegree:
+ raise KeyError(f"indegree missing for exec node {exec_node_id}")
+ if exec_node_id in self._scheduler.executed:
+ # Compatibility callers may advance with the invocation result and then replace that result explicitly.
+ self._state._tx_set_mapping(self._state.results, exec_node_id, output)
+ return []
+ dependents = self._scheduler.plan.dependents(exec_node_id)
+ for dependent in dependents:
+ if dependent not in self._state.indegree:
+ raise KeyError(f"indegree missing for exec node {dependent}")
+ self._remove_projected(exec_node_id)
+ self._record_completed_node(exec_node_id, output)
+ finalized_for_exec_node_id = self._state._apply_generic_for_continuation(exec_node_id, output)
+ self._mark_source_node_complete(exec_node_id)
+ # A condition may become resolvable when this node completes. Resolve it
+ # while the state has the completed result, before recalculating generic
+ # indegrees and readiness.
+ for dependent in dependents:
+ self._state._try_resolve_if_node(dependent)
+ self._sync_executed_state(excluded=(exec_node_id,))
+ newly_ready = self._scheduler.complete(exec_node_id)
+ for dependent in set(dependents):
+ self._state._tx_set_mapping(self._state.indegree, dependent, self._scheduler.indegree[dependent])
+ for ready_node_id in newly_ready:
+ self._project_ready_node(ready_node_id)
+ _ExecutionScheduler._try_materialize_deferred_nested_for_body(self, exec_node_id)
+ if finalized_for_exec_node_id is None:
+ return []
+ finalized_for_node = self._state.execution_graph.get_node(finalized_for_exec_node_id)
+ finalized_for_output = self._state.results.get(finalized_for_exec_node_id)
+ if not isinstance(finalized_for_node, ForInvocation) or not isinstance(
+ finalized_for_output, ForInvocationOutput
+ ):
+ return []
+ return [(finalized_for_node, finalized_for_output)]
+
+ def set_ready_order(self, ready_order: Iterable[str]) -> None:
+ self._scheduler.set_ready_order(ready_order)
+ self._project_ready_nodes()
diff --git a/tests/app/services/shared/test_graph_scheduler.py b/tests/app/services/shared/test_graph_scheduler.py
new file mode 100644
index 0000000000..da40f6b90d
--- /dev/null
+++ b/tests/app/services/shared/test_graph_scheduler.py
@@ -0,0 +1,26 @@
+import subprocess
+import sys
+
+from invokeai.app.services.shared import graph as graph_module
+from invokeai.app.services.shared import graph_scheduler
+
+
+def test_graph_facade_reexports_scheduler_classes() -> None:
+ assert graph_module._ExecutionScheduler is graph_scheduler._ExecutionScheduler
+ assert graph_module._GenericGraphSchedulerAdapter is graph_scheduler._GenericGraphSchedulerAdapter
+
+
+def test_graph_scheduler_import_does_not_import_graph_facade() -> None:
+ result = subprocess.run(
+ [
+ sys.executable,
+ "-c",
+ "import sys; import invokeai.app.services.shared.graph_scheduler; "
+ "print('invokeai.app.services.shared.graph' in sys.modules)",
+ ],
+ check=True,
+ capture_output=True,
+ text=True,
+ )
+
+ assert result.stdout.strip() == "False"