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): route input-driven flat for generically

JPPhoto618ae48a2fab
diff --git a/docs/src/content/docs/contributing/loop-nodes.md b/docs/src/content/docs/contributing/loop-nodes.md
index 048faad34e..7a5f96c673 100644
--- a/docs/src/content/docs/contributing/loop-nodes.md
+++ b/docs/src/content/docs/contributing/loop-nodes.md
@@ -98,14 +98,16 @@ iteration and state; the `ForReturn` effect completes it with output, state, and
are persisted under the invocation reference. Session-built effect references carry graph-state, durable-frame,
iteration-path, and workflow-call-depth identity, and stale or cross-scope effects are rejected before mutation. They
never encode `loop_linkage` as a data token. A fresh graph with exactly one static flat `For`/`ForReturn` pair,
-including an empty literal collection, now uses the generic scheduler adapter for readiness and continuation transitions.
+including an empty literal collection or a supported non-empty input-driven collection producer, now uses the generic
+scheduler adapter for readiness and continuation transitions.
Graph state owns the
generic continuation boundary: it carries returned state, honors `continue_condition`, materializes the next body
iteration, and finalizes the aggregate. An empty literal collection completes through the existing synthetic terminal
`For` result without running the body or `ForReturn`. The generic scheduler remains opaque and never receives a literal
successor node ID. The compatibility continuation bridge is retained only for unsupported loop shapes and explicitly
-legacy-loaded snapshots. Input-driven outer collections, deeper or multiple nested loops, and unsupported mixed control
-flow remain on the compatibility scheduler; the narrow canonical two-level nested-`For` shape and the exact
+legacy-loaded snapshots. Empty input-driven collections, unsupported input-driven outer collections, deeper or multiple
+nested loops, and unsupported mixed control flow remain on the compatibility scheduler; the narrow canonical two-level
+nested-`For` shape and the exact
bounded outer-`For`/`Iterate`/`Collect` shape are now generic-routed. This additive effect seam does not claim generic
scheduling for the remaining shapes. `Iterate` records ordered item tokens in
a closed `StreamBuffer`; an empty `Iterate` records an
@@ -192,7 +194,7 @@ ports and literal successor IDs remain absent, while unsupported loop shapes and
their compatibility paths until differential coverage proves their generic replacements.
The engine still owns runtime node materialization and queue readiness for loop-containing graphs that require empty or
-input-driven `For`, deeper nested/mixed control flow, and existing snapshots. Ordinary static DAGs, legacy-shaped `If` graphs,
+unsupported input-driven `For`, deeper nested/mixed control flow, and existing snapshots. Ordinary static DAGs, legacy-shaped `If` graphs,
direct `Iterate`/`Collect`-only graphs, and supported fresh static flat `For` graphs now use the generic opaque
plan/scheduler through a compatibility projection. This is intentional:
the generic records preserve tested loop semantics first, while the old execution graph remains the fallback for control
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index afa681f26a..7adab4c369 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -260,10 +260,10 @@ Holds the state for a single run. Keeps the source graph intact and materializes
set of internal helper classes. For ordinary static DAGs and legacy-shaped `If` graphs, readiness and completion are
projected through the generic `ExecutionPlan`/`ExecutionScheduler` adapter. Direct `Iterate`/`Collect` graphs without
other control-flow nodes also use that adapter: its readiness predicate waits for canonical streams to close and its
-completion mirrors Iterate outputs into the stream ledger. A fresh graph with one static, non-empty flat
-`For`/`ForReturn` pair also uses the adapter for readiness and continuation transitions; graph state owns the
-invocation-specific continuation boundary while the generic scheduler remains opaque. Empty or input-driven `For`
-collections, deeper or multiple loops, saved-workflow calls, and unsupported mixed control-flow shapes outside the
+completion mirrors Iterate outputs into the stream ledger. A fresh graph with one static flat `For`/`ForReturn` pair,
+including a supported non-empty input-driven collection producer, also uses the adapter for readiness and continuation
+transitions; graph state owns the invocation-specific continuation boundary while the generic scheduler remains opaque.
+Empty input-driven collections, unsupported input-driven shapes, deeper or multiple loops, saved-workflow calls, and unsupported mixed control-flow shapes outside the
bounded per-item topology continue to use
the legacy compatibility scheduler until their differential coverage is complete. The narrow canonical two-level
nested-`For` shape, the canonical outer-`For`/bounded-`Iterate`/`Collect` shape,
@@ -332,7 +332,7 @@ mutation helpers. Those helpers reject changes once the affected nodes have alre
checks control generic branch readiness. Supported static flat `For` graphs and the narrow canonical two-level nested
`For` shape, the exact producer-driven nested extension, and the bounded two- through six-level serial nested-
`Iterate` chains and the bounded per-item `Iterate`/`If`/`Collect` topology use the generic adapter for readiness and
- continuation projection; empty/input-driven, seven-level or deeper, mixed graphs outside that topology, and saved-workflow
+ continuation projection; empty input-driven, unsupported input-driven, seven-level or deeper, mixed graphs outside that topology, and saved-workflow
graphs retain the legacy scheduler. Optional
`ready_order: list[str]` prioritizes classes. Queues are rebuilt from persisted execution state when a session is
deserialized.
@@ -448,7 +448,8 @@ and terminal effects. The generic scheduler selects nodes from required
effects for the current frame; it never receives a literal successor-node ID.
Current implementation is narrower: `IfInvocation` and non-empty
`IterateInvocation` use the effect recorder for activation and stream effects;
-the supported static, non-empty flat `For`/`ForReturn` shape uses it for
+the supported static flat `For`/`ForReturn` shape, including the non-empty
+input-driven producer case, uses it for
frame-scoped continuation effects and generic readiness/continuation
projection.
Direct `Iterate`/`Collect`-only graphs use the generic scheduler adapter for
@@ -479,7 +480,8 @@ after finalization, their authoritative returned collection and final state. Unv
compatibility loader. `For` start payloads must match the prepared index, collection total, and state before mutation. This is the invocation/effect ownership
seam: the generic adapter calls the graph-state generic continuation boundary,
which creates the next prepared iteration, aggregates outputs, and finalizes the
-supported flat loop. The compatibility bridge remains for empty/input-driven,
+supported flat loop. The compatibility bridge remains for empty input-driven,
+unsupported input-driven,
deeper/multiple nested, mixed-loop, and legacy-snapshot execution; those paths
retain their existing materialization and linkage ownership. The supported
fresh static flat `For`/`ForReturn` shape, the narrow canonical two-level
@@ -513,7 +515,7 @@ compatibility skip propagation must not leave stale downstream indegrees. Both
scheduler adapters expose the same skip transition; the legacy path releases
downstream indegrees without trying to hydrate inputs from the skipped node. It
does not claim durable persistence of the generic scheduler's private claim set,
-nor does it claim ownership migration for empty/input-driven, deeper/multiple,
+nor does it claim ownership migration for empty input-driven, unsupported input-driven, deeper/multiple,
mixed, or workflow-call loop shapes. Real queue/processor
coverage in `tests/app/services/session_processor/test_if_processor_sqlite.py`
also exercises true and false `If` selection, cancellation before and after
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index e6948b656c..cf9f592f5d 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -802,8 +802,16 @@ class GraphExecutionState(BaseModel):
return True
if len(for_nodes) != 1 or len(return_nodes) != 1:
return False
- if self.graph._get_input_edges(for_nodes[0].id, COLLECTION_FIELD):
- return False
+ collection_edges = self.graph._get_input_edges(for_nodes[0].id, COLLECTION_FIELD)
+ if collection_edges:
+ collection_source = self.graph.get_node(collection_edges[0].source.node_id)
+ collection_value = getattr(collection_source, "value", None)
+ if (
+ collection_edges[0].source.field == "value"
+ and isinstance(collection_value, list)
+ and not collection_value
+ ):
+ return False
body_path = self.graph._get_for_body_path_to_return(for_nodes[0].id, self._get_source_graph_flat())
if body_path is None:
return False
@@ -1301,10 +1309,6 @@ class GraphExecutionState(BaseModel):
source_node_id = self.prepared_source_mapping.get(node.id)
if source_node_id is None:
return node.collection
- source_node = self.graph.get_node(source_node_id)
- if isinstance(source_node, ForInvocation) and source_node.collection:
- return source_node.collection
-
collection_edges = self.graph._get_input_edges(source_node_id, COLLECTION_FIELD)
if collection_edges:
source_output = self.results.get(collection_edges[0].source.node_id)
@@ -1326,6 +1330,9 @@ class GraphExecutionState(BaseModel):
collection = self._value_from_object(source_output, collection_edges[0].source.field)
if isinstance(collection, list):
return collection
+ source_node = self.graph.get_node(source_node_id)
+ if isinstance(source_node, ForInvocation) and source_node.collection:
+ return source_node.collection
return node.collection
def _prepared_for_continuation_payload(self, node: ForInvocation | ForReturnInvocation) -> dict[str, Any] | None:
diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.py
index 89a5763c8f..b5d753e82e 100644
--- a/tests/app/services/shared/test_execution_engine_differential.py
+++ b/tests/app/services/shared/test_execution_engine_differential.py
@@ -4916,40 +4916,6 @@ def test_empty_literal_flat_for_matches_compatibility_scheduler() -> None:
assert _state_projection(generic_state) == _state_projection(compatibility_state)
- ("graph_kwargs", "expected_trace", "expected_collection"),
- [
- ({"input_collection": [1, 2]}, ["collection", "for", "body", "return", "for", "body", "return"], [11, 12]),
- ],
-)
-def test_flat_for_ineligible_collections_use_compatibility_scheduler(
- graph_kwargs: dict[str, list[Any]], expected_trace: list[str], expected_collection: list[Any]
-) -> None:
- state = GraphExecutionState(graph=_flat_for_graph(**graph_kwargs))
-
- assert state._can_use_generic_scheduler() is False
- trace, state = _run_graph_with_effects(state)
-
- assert trace == expected_trace
- assert isinstance(state._execution_scheduler, _ExecutionScheduler)
- assert _final_for_output(state).output_collection == expected_collection
- assert state.is_complete()
- continuations = list(state._generic_runtime().continuations.values())
- assert len(continuations) == len(expected_collection)
- assert all(state.prepared_source_mapping[item.owner_id] == "for" for item in continuations)
- assert all(item.status == "completed" for item in continuations)
- assert sum(len(effects) for effects in state.execution_effects.values()) == 2 * len(expected_collection)
- _assert_execution_identity_consistent(state)
-
- restored = load_execution_state(dump_execution_state(state))
- _restore_compatibility_scheduler(restored)
- assert isinstance(restored._execution_scheduler, _ExecutionScheduler)
- assert _state_projection(restored) == _state_projection(state)
- assert _effect_ledger_projection(restored) == _effect_ledger_projection(state)
- assert _continuation_projection(restored) == _continuation_projection(state)
- _assert_execution_identity_consistent(restored)
-
-
def test_input_driven_empty_flat_for_uses_compatibility_scheduler() -> None:
state = GraphExecutionState(graph=_flat_for_graph(input_collection=[]))
@@ -4964,6 +4930,54 @@ def test_input_driven_empty_flat_for_uses_compatibility_scheduler() -> None:
_assert_execution_identity_consistent(state)
+def test_input_driven_flat_for_uses_generic_scheduler() -> None:
+ state = GraphExecutionState(graph=_flat_for_graph(input_collection=[3, 7]))
+
+ assert state._can_use_generic_scheduler()
+ assert isinstance(state._scheduler(), _GenericGraphSchedulerAdapter)
+
+
+def test_input_driven_flat_for_matches_compatibility_scheduler() -> None:
+ generic_trace, generic_state = _run_graph_with_effects(
+ GraphExecutionState(graph=_flat_for_graph(input_collection=[3, 7]))
+ )
+ compatibility_trace, compatibility_state = _run_graph_with_effects(
+ GraphExecutionState(graph=_flat_for_graph(input_collection=[3, 7])),
+ force_compatibility_scheduler=True,
+ )
+
+ assert (
+ generic_trace
+ == compatibility_trace
+ == [
+ "collection",
+ "for",
+ "body",
+ "return",
+ "for",
+ "body",
+ "return",
+ ]
+ )
+ assert _final_for_output(generic_state).output_collection == [13, 17]
+ assert _final_for_output(compatibility_state).output_collection == [13, 17]
+ assert generic_state.is_complete()
+ assert compatibility_state.is_complete()
+ assert isinstance(generic_state._execution_scheduler, _GenericGraphSchedulerAdapter)
+ assert isinstance(compatibility_state._execution_scheduler, _ExecutionScheduler)
+ assert _state_projection(generic_state) == _state_projection(compatibility_state)
+
+ restored_generic = load_execution_state(dump_execution_state(generic_state))
+ restored_compatibility = load_execution_state(dump_execution_state(compatibility_state))
+ _restore_compatibility_scheduler(restored_compatibility)
+ assert isinstance(restored_generic._scheduler(), _GenericGraphSchedulerAdapter)
+ assert isinstance(restored_compatibility._execution_scheduler, _ExecutionScheduler)
+ assert _state_projection(restored_generic) == _state_projection(generic_state)
+ assert _state_projection(restored_compatibility) == _state_projection(compatibility_state)
+ _assert_execution_identity_consistent(restored_generic)
+ _assert_execution_identity_consistent(restored_compatibility)
+
+
@pytest.mark.parametrize(
("continue_condition", "expected_trace", "expected_collection", "expected_state"),
[