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): close iterate ownership gate

JPPhoto80d183762bc9
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index 69e9ca1422..37d5fc15e0 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -570,10 +570,10 @@ Workflow-call note:
- `_PreparedExecRegistry` Owns the relationship between source graph nodes and prepared execution graph nodes, plus
cached metadata such as iteration path and runtime state.
-- `_ExecutionMaterializer` Expands source graph nodes into concrete execution graph nodes when the scheduler runs out of
- ready work. It owns iterator expansion, collector grouping, prepared-parent selection, and creation of execution-graph
- edges. When matching prepared parents for a downstream exec node, skipped prepared exec nodes are ignored and cannot
- be selected as live inputs.
+- `_ExecutionMaterializer` Expands source graph nodes into concrete execution graph nodes when an unsupported or legacy
+ shape runs out of ready work. On those compatibility paths it owns iterator expansion, collector grouping,
+ prepared-parent selection, and creation of execution-graph edges. When matching prepared parents for a downstream
+ exec node, skipped prepared exec nodes are ignored and cannot be selected as live inputs.
The class lives in `graph_materializer.py` and is re-exported by `graph.py`.
- Private `graph_iterate_planner.py` expands the supported direct and body-mediated Iterate/Collect shapes.
Graph-state method wrappers preserve the admission, copy creation, edge attachment, and atomic preparation entry
@@ -625,7 +625,11 @@ frame metadata is retained. Persisted execution references, tokens, and effects
helper objects do not. Queue snapshots carry an additive execution-state version marker and use version-aware loader;
legacy unmarked snapshots are treated as version 0, while unreadable snapshots are quarantined by queue service.
-### 4.4 Preparation (`_prepare()`)
+### 4.4 Compatibility preparation (`_prepare()`)
+
+The steps below describe compatibility materialization for unsupported shapes and legacy snapshots. Supported fresh
+`Iterate`/`Collect` shapes use the bounded planners described above and do not perform this ancestry or collector
+materialization.
- Build a flat DAG from the **source** graph.
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 931a8b8238..f1871009f1 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -2396,7 +2396,8 @@ class GraphExecutionState(BaseModel):
def _prepare_until_node_ready(self) -> Optional[BaseInvocation]:
base_graph = self._get_source_graph_flat()
- self._materializer()._attach_pending_if_inputs()
+ if self._pending_if_exec_nodes:
+ self._materializer()._attach_pending_if_inputs()
self._rehydrate_ready_queues()
next_node = self._get_next_node()
if next_node is not None:
@@ -2773,7 +2774,8 @@ class GraphExecutionState(BaseModel):
self._rehydrate_resolved_if_exec_branches()
self._rehydrate_generic_runtime_state()
self._rehydrate_ready_queues()
- self._materializer()._attach_pending_if_inputs(enqueue=False)
+ if self._pending_if_exec_nodes:
+ self._materializer()._attach_pending_if_inputs(enqueue=False)
def model_post_init(self, __context: Any) -> None:
if isinstance(__context, dict) and "execution_effects_persisted" in __context:
diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.py
index 9191af8fe2..e680559c90 100644
--- a/tests/app/services/shared/test_execution_engine_differential.py
+++ b/tests/app/services/shared/test_execution_engine_differential.py
@@ -3225,6 +3225,79 @@ def test_direct_iterate_body_collect_fresh_execution_preserves_order_and_none()
group_collector_inputs.assert_not_called()
+def test_direct_iterate_body_collect_fresh_construction_and_execution_do_not_instantiate_materializer() -> None:
+ collection = ["first", None, "last"]
+ with patch.object(
+ graph_module,
+ "_ExecutionMaterializer",
+ side_effect=AssertionError("fresh direct Iterate/Collect must not instantiate materializer"),
+ ) as materializer:
+ trace, state = _run_graph(GraphExecutionState(graph=_direct_iterate_body_collect_graph(collection=collection)))
+
+ assert trace == ["source", "iterate", "iterate", "iterate", "body", "body", "body", "collect"]
+ assert _source_output(state, "collect").collection == collection
+ assert state.is_complete()
+ materializer.assert_not_called()
+
+
+def test_direct_iterate_body_collect_checkpoint_rehydration_does_not_instantiate_materializer() -> None:
+ collection = ["first", None, "last"]
+ partial_trace, partial_state = _run_graph(
+ GraphExecutionState(graph=_direct_iterate_body_collect_graph(collection=collection)), stop_after=2
+ )
+
+ with patch.object(
+ graph_module,
+ "_ExecutionMaterializer",
+ side_effect=AssertionError("direct Iterate/Collect checkpoint must not instantiate materializer"),
+ ) as materializer:
+ resumed_trace, resumed_state = _run_graph(load_execution_state(dump_execution_state(partial_state)))
+
+ assert partial_trace + resumed_trace == [
+ "source",
+ "iterate",
+ "iterate",
+ "iterate",
+ "body",
+ "body",
+ "body",
+ "collect",
+ ]
+ assert _source_output(resumed_state, "collect").collection == collection
+ assert resumed_state.is_complete()
+ materializer.assert_not_called()
+
+
+def test_legacy_direct_iterate_body_collect_snapshot_uses_compatibility_materializer() -> None:
+ collection = ["first", None, "last"]
+ partial_trace, partial_state = _run_graph(
+ GraphExecutionState(graph=_direct_iterate_body_collect_graph(collection=collection)), stop_after=1
+ )
+ 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 partial_trace + resumed_trace == [
+ "source",
+ "iterate",
+ "iterate",
+ "iterate",
+ "body",
+ "body",
+ "body",
+ "collect",
+ ]
+ assert _source_output(restored, "collect").collection == collection
+ assert restored.is_complete()
+
+
def test_direct_iterate_fan_in_fresh_execution_owns_two_stream_expansion() -> None:
state = GraphExecutionState(graph=_direct_iterate_fan_in_graph(left=[None, "left-last"], right=["right-first"]))