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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
refactor(execution): close nested loop ownership gate
JPPhotof666c65b2461
diff --git a/docs/src/content/docs/contributing/loop-nodes.md b/docs/src/content/docs/contributing/loop-nodes.mdindex 12739df293..d62b9a2ec3 100644--- a/docs/src/content/docs/contributing/loop-nodes.md+++ b/docs/src/content/docs/contributing/loop-nodes.md@@ -73,11 +73,12 @@ Iterate.item -> body -> Collect.itemCollect.collection -> ForReturn.output```-The fresh generic scheduler admits this exact shape when the outer collection is a non-empty literal and there is one-final consumer of `For.output_collection`. Each outer frame is isolated, including when the preparation node produces-an empty inner collection. A checkpoint after a nested iterator boundary restores the generic class-drain state and-continues the same frame/stream order. Input-driven outer collections, additional loop or control-flow nodes, escaped-body paths, and other mixed shapes remain on the compatibility scheduler.+The fresh generic scheduler admits this exact shape when the outer collection is a non-empty literal or one of the+supported input-driven outer-collection variants, and there is one final consumer of `For.output_collection`. Each+outer frame is isolated, including when the preparation node produces an empty inner collection. A checkpoint after a+nested iterator boundary restores the generic class-drain state and continues the same frame/stream order. Unsupported+input-driven outer collections, additional loop or control-flow nodes, escaped body paths, and other mixed shapes+remain on the compatibility scheduler.Unsupported shapes, including independent iterator-derived body inputs, mixed nested `For`/`Iterate` bodies, escapingbody paths, ambiguous returns, and arbitrary cyclic graphs, are rejected before execution.@@ -146,10 +147,10 @@ for both streams to close, including empty streams. This is a private boundedplanner case with exactly seven nodes and six ordinary edges: two inputlessordinary sources, two `Iterate` nodes, two ordinary bodies, and one `Collect`.It has no `Collect.collection` input, downstream consumer, or extra topology.-Three or more body-mediated-branches, fan-in with four or more direct branches, nested or input-driven-iterators, and mixed control flow outside the bounded per-item-`Iterate`/`If`/`Collect` topology remain on the compatibility materializer.+Three or more body-mediated branches, fan-in with four or more direct branches, unsupported nested or input-driven+iterator topologies, and mixed control flow outside the bounded per-item `Iterate`/`If`/`Collect` topology remain on+the compatibility materializer. The exact admitted serial nested-`Iterate` chains and producer-driven bounded+`For`/`Iterate`/`Collect` variants use the generic planner described above.Focused compatibility coverage now proves empty, nested, fan-in, partial/rehydrated, failed, canceled, and retriedIterate/Collect sessions. This is evidence for the current adapters; it does not remove materialization or queueownership. Direct `Iterate`/`Collect`-only graphs and the exact bounded nested shape now use the generic schedulerdiff --git a/invokeai/app/services/shared/graph_scheduler.py b/invokeai/app/services/shared/graph_scheduler.pyindex 658b822aa4..172ff7b1d7 100644--- a/invokeai/app/services/shared/graph_scheduler.py+++ b/invokeai/app/services/shared/graph_scheduler.py@@ -34,6 +34,85 @@ from invokeai.app.services.shared.graph_validation import CollectInvocation, Iteif TYPE_CHECKING:from invokeai.app.services.shared.graph import GraphExecutionState+ from invokeai.app.services.shared.graph_for_planner import _GenericForPlanner+ from invokeai.app.services.shared.graph_materializer import _ExecutionMaterializer+++def _prepare_deferred_nested_for_body(+ state: "GraphExecutionState",+ exec_node_id: str,+ planner: "_GenericForPlanner | _ExecutionMaterializer",+) -> None:+ """Prepare a deferred nested For body through the caller's node planner."""++ completed_source_id = state._prepared_registry().get_source_node_id(exec_node_id)+ graph = state._get_source_graph_flat()+ for source_for_id, source_node in state.graph.nodes.items():+ if not isinstance(source_node, ForInvocation):+ continue+ serial_nested_body = state.graph._get_supported_for_serial_nested_iterate_chain(source_for_id, graph)+ if serial_nested_body is not None:+ if completed_source_id not in serial_nested_body.body_path_nodes:+ continue+ prepared_exec_path = state._get_iteration_path(exec_node_id)+ for prepared_for_id in state._prepared_registry().get_prepared_ids(source_for_id):+ prepared_for_path = state._get_iteration_path(prepared_for_id)+ prepared_for_node = state.execution_graph.get_node(prepared_for_id)+ if isinstance(prepared_for_node, ForInvocation) and prepared_for_node.index >= 0:+ prepared_for_path = (+ *state._get_for_parent_iteration_path(prepared_for_id),+ prepared_for_node.index,+ )+ if prepared_exec_path[: len(prepared_for_path)] == prepared_for_path:+ planner.create_for_body_iteration(source_for_id=source_for_id, prepared_for_id=prepared_for_id)+ return+ nested_body = state.graph._get_supported_for_nested_iterate_body(source_for_id, graph)+ nested_for_body = 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 state._prepared_registry().get_prepared_ids(source_for_id):+ prepared_for_node = state.execution_graph.get_node(prepared_for_id)+ prepared_for_path = state._get_iteration_path(prepared_for_id)+ if isinstance(prepared_for_node, ForInvocation) and prepared_for_node.index >= 0:+ prepared_for_path = (+ *state._get_for_parent_iteration_path(prepared_for_id),+ prepared_for_node.index,+ )+ if prepared_for_path != state._get_iteration_path(exec_node_id):+ continue+ if nested_for_body is not None:+ if not all(+ any(+ state._get_for_parent_iteration_path(prepared_child_id) == prepared_for_path+ for prepared_child_id in state._prepared_registry().get_prepared_ids(child_id)+ )+ for child_id in deferred_node_ids+ ):+ planner.create_for_body_iteration(source_for_id=source_for_id, prepared_for_id=prepared_for_id)+ return+ continue+ if any(+ (iterate_path := 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 state._prepared_registry().get_prepared_ids(deferred_node_ids[0])+ ):+ continue+ planner.create_for_body_iteration(source_for_id=source_for_id, prepared_for_id=prepared_for_id)+ returnclass _ExecutionScheduler:@@ -131,78 +210,7 @@ class _ExecutionScheduler: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- serial_nested_body = self._state.graph._get_supported_for_serial_nested_iterate_chain(source_for_id, graph)- if serial_nested_body is not None:- if completed_source_id not in serial_nested_body.body_path_nodes:- continue- prepared_exec_path = self._state._get_iteration_path(exec_node_id)- for prepared_for_id in self._state._prepared_registry().get_prepared_ids(source_for_id):- prepared_for_path = self._state._get_iteration_path(prepared_for_id)- prepared_for_node = self._state.execution_graph.get_node(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_exec_path[: len(prepared_for_path)] == prepared_for_path:- self._state._create_for_body_iteration(- source_for_id=source_for_id, prepared_for_id=prepared_for_id- )- return- 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._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._create_for_body_iteration(source_for_id=source_for_id, prepared_for_id=prepared_for_id)- return+ _prepare_deferred_nested_for_body(self._state, exec_node_id, self._state._materializer())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:@@ -601,11 +609,6 @@ class _GenericGraphSchedulerAdapter:if self._state._count_unexecuted_prepared(source_node_id) == 0 and source_node_id not in self._state.executed:self._state._mark_source_executed(source_node_id)- def _try_materialize_deferred_nested_for_body(self, exec_node_id: str) -> None:- """Keep the compatibility materializer for generic shapes outside the new planner."""-- _ExecutionScheduler._try_materialize_deferred_nested_for_body(self, exec_node_id)-def complete(self, exec_node_id: str, output: BaseInvocationOutput) -> list[tuple[BaseInvocation, BaseInvocationOutput]]:@@ -673,7 +676,7 @@ class _GenericGraphSchedulerAdapter:elif can_use_eight_level_nested_iterate_sequence_planner(self._state):prepare_eight_level_nested_iterate_sequences(self._state)else:- self._try_materialize_deferred_nested_for_body(exec_node_id)+ _prepare_deferred_nested_for_body(self._state, exec_node_id, self._state._for_planner())if finalized_for_exec_node_id is None:return []finalized_for_node = self._state.execution_graph.get_node(finalized_for_exec_node_id)diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.pyindex 7c5707be71..4ba2780d6b 100644--- a/tests/app/services/shared/test_execution_engine_differential.py+++ b/tests/app/services/shared/test_execution_engine_differential.py@@ -2123,12 +2123,14 @@ def test_flat_for_generic_path_does_not_use_compatibility_materializer_helpers("graph_factory",[pytest.param(_flat_for_graph, id="flat"),+ pytest.param(_nested_for_graph, id="nested"),pytest.param(_input_driven_nested_for_graph, id="input-driven-nested"),pytest.param(_two_sibling_nested_for_fan_in_graph, id="two-sibling"),pytest.param(_three_level_nested_for_graph, id="three-level"),pytest.param(_input_driven_three_level_nested_for_graph, id="input-driven-three-level"),pytest.param(_four_level_nested_for_graph, id="four-level"),pytest.param(_nested_for_iterate_collect_graph, id="nested-iterate-collect"),+ pytest.param(_input_driven_nested_for_iterate_collect_graph, id="input-driven-nested-iterate-collect"),pytest.param(_serial_two_level_nested_for_iterate_collect_graph, id="serial-nested-iterate"),],)@@ -2147,6 +2149,69 @@ def test_admitted_generic_for_shapes_do_not_construct_compatibility_materializerassert state.is_complete()+ "graph_factory",+ [+ pytest.param(_nested_for_graph, id="nested"),+ pytest.param(_input_driven_nested_for_graph, id="input-driven-nested"),+ pytest.param(_two_sibling_nested_for_fan_in_graph, id="two-sibling"),+ pytest.param(_input_driven_three_level_nested_for_graph, id="input-driven-three-level"),+ pytest.param(_nested_for_iterate_collect_graph, id="nested-iterate-collect"),+ ],+)+def test_admitted_nested_for_checkpoint_does_not_construct_compatibility_materializer(+ monkeypatch: pytest.MonkeyPatch,+ graph_factory: Callable[[], Graph],+) -> None:+ partial_trace, partial_state = _run_graph_with_effects(+ GraphExecutionState(graph=graph_factory()),+ stop_after=4,+ )++ def fail_compatibility_materializer(*_: object, **__: object) -> None:+ raise AssertionError("generic nested For rehydration instantiated compatibility materializer")++ monkeypatch.setattr(graph_module, "_ExecutionMaterializer", fail_compatibility_materializer)+ resumed_trace, resumed_state = _run_graph_with_effects(load_execution_state(dump_execution_state(partial_state)))++ assert partial_trace+ assert resumed_trace+ assert resumed_state.is_complete()+++def test_legacy_nested_for_snapshot_uses_compatibility_materializer() -> None:+ _partial_trace, partial_state = _run_graph_with_effects(+ GraphExecutionState(graph=_nested_for_graph()),+ stop_after=2,+ )+ snapshot = dump_execution_state(partial_state)+ snapshot.pop("execution_state_version")+ snapshot.pop("execution_effects")+ materializer_type = graph_module._ExecutionMaterializer++ with patch.object(graph_module, "_ExecutionMaterializer", wraps=materializer_type) as materializer:+ restored = load_execution_state(snapshot)+ assert restored._legacy_snapshot_loaded+ _resumed_trace, restored = _run_graph(restored)++ assert materializer.call_count == 1+ assert restored.is_complete()+++def test_generic_nested_for_path_does_not_use_compatibility_deferred_body_helper(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ def fail_compatibility_helper(*_: object, **__: object) -> None:+ raise AssertionError("generic nested For execution used the compatibility deferred-body helper")++ monkeypatch.setattr(_ExecutionScheduler, "_try_materialize_deferred_nested_for_body", fail_compatibility_helper)++ trace, state = _run_graph_with_effects(GraphExecutionState(graph=_nested_for_graph()))++ assert trace+ assert state.is_complete()++def test_compatibility_for_path_does_not_construct_generic_planner(monkeypatch: pytest.MonkeyPatch) -> None:def fail_generic_planner(*_: object, **__: object) -> None:raise AssertionError("compatibility For instantiated generic planner")