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.

Support input-driven three-level For scheduling

JPPhoto97de917aa381
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index 7a67120b10..3ccba39508 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -102,6 +102,11 @@ and one ordinary outer-output consumer—is also generic-routed. It owns outer
input hydration, per-outer-frame stream expansion, checkpoint rehydration, and
failure parity; input-driven inner preparation, deeper or multiple nesting,
other mixed control flow, and legacy snapshots remain compatibility-owned.
+The exact nine-node producer-driven extension adds the same inputless,
+statically non-empty `CollectionConcatInvocation` outer producer to the
+three-level serial nested-`For` chain. It is generic-routed with the same
+trace, output, checkpoint, and failure guarantees; other producer classes and
+deeper, multiple, sibling, mixed, and legacy shapes remain compatibility-owned.
The exact serial two-level extension—`For.item -> preparation1 -> Iterate1 ->
preparation2 -> Iterate2 -> ordinary body -> Collect -> linked ForReturn`, plus
one ordinary outer-output consumer—is also generic-routed. Its planner and
@@ -152,7 +157,8 @@ expansion. Other empty/input-driven shapes, five-level-or-deeper or sibling loop
on the compatibility scheduler until their own differential gates are complete. One narrow fresh two-level nested-`For` shape (one non-empty literal outer
collection, one inner `For` sourced from `outer.item`, and no continuation nodes) now uses the generic adapter;
deeper-than-four-level, multiple-child shapes outside the exact two-sibling `CollectionConcat` fan-in contract, other empty/input-driven, mixed, and legacy-loaded shapes remain compatibility-owned.
-The exact fresh three-level serial nested-`For` shape (one literal outer collection,
+The exact fresh three-level serial nested-`For` shape (one literal outer
+collection or one statically non-empty `CollectionConcatInvocation` producer,
one direct child at each level, no continuation nodes) also uses the generic adapter. The exact fresh
four-level serial nested-`For` shape (one literal outer collection, one direct child at each level, no continuation
nodes) is also generic-routed. Its deepest-empty behavior matches compatibility at the trace, output, and completion
@@ -292,7 +298,8 @@ including an empty inner collection, remains isolated by its frame path. `Iterat
for this exact seven-node shape, the private nested planner owns per-outer-frame
copy expansion, stream identity, empty closure, `Collect` readiness/hydration,
checkpoint rehydration, and failure parity. The exact producer-driven outer
-extension described above is also generic-routed. The exact fresh three-level serial nested-`For` shape and the exact
+extension described above is also generic-routed. The exact fresh three-level serial nested-`For` shape, including its
+statically non-empty producer-driven outer variant, and the exact
fresh four-level serial nested-`For` shape are also generic-routed; five-level-or-deeper or sibling nested `For` outside the exact two-sibling `CollectionConcat` fan-in contract, nine-level
or deeper nested iterators, other input-driven outer collections, mixed control
flow, and legacy snapshots retain compatibility materializer ownership. The
@@ -347,7 +354,7 @@ mutation helpers. Those helpers reject changes once the affected nodes have alre
`_active_class: Optional[str]`. Ordinary static DAGs and legacy-shaped `If` graphs derive readiness from the generic
scheduler; the `If` adapter stores frame-local activation dependencies whose private gate state plus persisted token
checks control generic branch readiness. Supported static flat `For` graphs, the narrow canonical two-level nested
- `For` shape, the exact fresh three-level and four-level serial nested-`For` shapes, the exact producer-driven nested extension, the exact bounded empty input-driven flat `For`, the bounded
+ `For` shape, the exact fresh three-level and four-level serial nested-`For` shapes (including the exact three-level producer-driven outer variant), the exact producer-driven nested extension, the exact bounded empty input-driven flat `For`, the bounded
two- through eight-level serial nested-`Iterate` chains, and the bounded per-item `Iterate`/`If`/`Collect` topology use
the generic adapter for readiness and continuation projection; other unsupported input-driven, nine-level or deeper,
mixed graphs outside that topology, and saved-workflow graphs retain the legacy scheduler. Optional
@@ -431,7 +438,7 @@ the generic path completes through the existing synthetic terminal `For` result
The generic scheduler receives only opaque node IDs and dependencies; it never receives a literal next node ID. The
compatibility continuation bridge remains available only for unsupported loop shapes and explicitly legacy-loaded snapshots.
The exact four-node/four-edge empty input-driven shape with one inputless static-list producer is also generic-routed.
-Other input-driven outer collections, five-level-or-deeper or sibling nested loops outside the exact two-sibling `CollectionConcat` fan-in contract, or unsupported mixed loop shapes remain compatibility-owned; the exact fresh two-sibling `For`/`ForReturn` `CollectionConcat` fan-in shape, the narrow canonical two-level, and exact fresh three-level and four-level serial nested-`For` shapes, the canonical bounded
+Other input-driven outer collections, five-level-or-deeper or sibling nested loops outside the exact two-sibling `CollectionConcat` fan-in contract, or unsupported mixed loop shapes remain compatibility-owned; the exact fresh two-sibling `For`/`ForReturn` `CollectionConcat` fan-in shape, the narrow canonical two-level, and exact fresh three-level and four-level serial nested-`For` shapes (including the exact three-level producer-driven outer variant), the canonical bounded
internal `Iterate`/`Collect` shape, the exact producer-driven outer nested extension, and the bounded two- through
six-, seven-, and eight-level serial nested-`Iterate` chains are generic-routed. On
rehydration, the generic adapter restores the active class-drain
@@ -503,7 +510,8 @@ five-level-or-deeper/sibling nested outside the exact two-sibling `CollectionCon
retain their existing materialization and linkage ownership. The supported
fresh static flat `For`/`ForReturn` shape, the exact bounded empty input-driven
flat shape, the narrow canonical two-level and exact fresh three-level and four-level serial nested-`For`
- shapes, and the canonical bounded internal `Iterate`/`Collect`
+ shapes (including the exact three-level producer-driven outer variant),
+ and the canonical bounded internal `Iterate`/`Collect`
shape use the generic adapter. For
`If`, generic readiness consumes opaque frame-local plan dependencies and requires
both matching private `ActivationGate` runtime state and a persisted activation token. Fresh resolution attaches only
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index f9581816fc..b3b172159d 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -906,8 +906,26 @@ class GraphExecutionState(BaseModel):
return False
outer_for = outer_candidates[0]
- if self.graph._get_input_edges(outer_for.id, COLLECTION_FIELD) or not outer_for.collection:
+ outer_collection_edges = self.graph._get_input_edges(outer_for.id, COLLECTION_FIELD)
+ if not outer_collection_edges and not outer_for.collection:
return False
+ if outer_collection_edges:
+ if nested_level != 3 or len(outer_collection_edges) != 1 or outer_for.collection:
+ return False
+ outer_collection_edge = outer_collection_edges[0]
+ if outer_collection_edge.destination.field != COLLECTION_FIELD:
+ return False
+ producer = self.graph.get_node(outer_collection_edge.source.node_id)
+ if not isinstance(producer, CollectionConcatInvocation) or (not producer.first and not producer.second):
+ return False
+ if (
+ outer_collection_edge.source.field != "collection"
+ or self.graph._get_input_edges(producer.id)
+ or self.graph._get_output_edges(producer.id) != outer_collection_edges
+ or len(self.graph.nodes) != 9
+ or len(self.graph.edges) != 11
+ ):
+ return False
chain = [outer_for]
while len(chain) < nested_level:
diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.py
index e6be688dd1..13dabe6e3a 100644
--- a/tests/app/services/shared/test_execution_engine_differential.py
+++ b/tests/app/services/shared/test_execution_engine_differential.py
@@ -656,11 +656,22 @@ def _sibling_nested_for_graph() -> Graph:
def _input_driven_three_level_nested_for_graph() -> Graph:
graph = _three_level_nested_for_graph()
+ graph.get_node("outer_for").collection = []
graph.add_node(CollectionConcatInvocation(id="outer_source", first=[[["a", "b"], ["c"]], [["d"]]]))
graph.add_edge(create_edge("outer_source", "collection", "outer_for", "collection"))
return graph
+def _input_driven_three_level_nested_for_graph_with_collection(
+ outer_collection: list[list[list[str]]],
+) -> Graph:
+ graph = _three_level_nested_for_graph(outer_collection=outer_collection)
+ graph.get_node("outer_for").collection = []
+ graph.add_node(CollectionConcatInvocation(id="outer_source", first=outer_collection))
+ graph.add_edge(create_edge("outer_source", "collection", "outer_for", "collection"))
+ return graph
+
+
def _input_driven_nested_for_graph(*, outer_collection: list[list[str]] | None = None) -> Graph:
graph = _nested_for_graph(outer_collection=outer_collection)
graph.get_node("outer_for").collection = []
@@ -2426,11 +2437,85 @@ def test_three_level_nested_for_generic_failure_matches_compatibility() -> None:
assert _state_projection(generic_state) == _state_projection(compatibility_state)
+def test_input_driven_three_level_nested_for_uses_generic_scheduler() -> None:
+ state = GraphExecutionState(graph=_input_driven_three_level_nested_for_graph())
+
+ assert state._can_use_generic_scheduler()
+
+ compatibility_trace, compatibility_state = _run_graph(
+ GraphExecutionState(graph=_input_driven_three_level_nested_for_graph()),
+ force_compatibility_scheduler=True,
+ )
+ generic_trace, generic_state = _run_graph(state)
+
+ assert generic_trace == compatibility_trace
+ 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)
+ assert _source_output(generic_state, "after").value == [[["a", "b"], ["c"]], [["d"]]]
+ assert generic_trace.count("outer_source") == compatibility_trace.count("outer_source") == 1
+
+
+def test_input_driven_three_level_nested_for_matches_empty_nested_collections() -> None:
+ def graph_factory() -> Graph:
+ return _input_driven_three_level_nested_for_graph_with_collection([[[], ["c"]], [[]]])
+
+ compatibility_trace, compatibility_state = _run_graph(
+ GraphExecutionState(graph=graph_factory()),
+ force_compatibility_scheduler=True,
+ )
+ generic_trace, generic_state = _run_graph(GraphExecutionState(graph=graph_factory()))
+
+ assert generic_trace == compatibility_trace
+ assert generic_state.is_complete()
+ assert compatibility_state.is_complete()
+ assert _source_output(generic_state, "after").value == [[[], ["c"]], [[]]]
+ assert _source_output(compatibility_state, "after").value == [[[], ["c"]], [[]]]
+
+
[email protected]("stop_after", [1, 7], ids=["after-producer", "mid-inner"])
+def test_input_driven_three_level_nested_for_reload_does_not_replay_completed_work(stop_after: int) -> None:
+ def graph_factory() -> Graph:
+ return _input_driven_three_level_nested_for_graph()
+
+ expected_trace, expected_state = _run_graph(GraphExecutionState(graph=graph_factory()))
+ partial_trace, partial_state = _run_graph(GraphExecutionState(graph=graph_factory()), stop_after=stop_after)
+
+ resumed_trace, resumed_state = _run_graph(load_execution_state(dump_execution_state(partial_state)))
+
+ assert partial_trace + resumed_trace == expected_trace
+ assert "outer_source" not in resumed_trace
+ assert resumed_state.is_complete()
+ assert _state_projection(resumed_state) == _state_projection(expected_state)
+
+
+def test_input_driven_three_level_nested_for_failure_matches_compatibility() -> None:
+ generic_trace, generic_state = _run_graph_with_effects(
+ GraphExecutionState(graph=_input_driven_three_level_nested_for_graph()),
+ fail_source_id="inner_body",
+ )
+ compatibility_trace, compatibility_state = _run_graph_with_effects(
+ GraphExecutionState(graph=_input_driven_three_level_nested_for_graph()),
+ force_compatibility_scheduler=True,
+ fail_source_id="inner_body",
+ )
+
+ assert generic_trace == compatibility_trace
+ assert generic_state.has_error() and compatibility_state.has_error()
+ assert generic_state.is_complete() and compatibility_state.is_complete()
+ assert "inner_return" not in generic_trace
+ assert "middle_return" not in generic_trace
+ assert "outer_return" not in generic_trace
+ assert "after" not in generic_trace
+ assert _state_projection(generic_state) == _state_projection(compatibility_state)
+
+
@pytest.mark.parametrize(
"graph_factory",
[
pytest.param(_sibling_nested_for_graph, id="sibling"),
- pytest.param(_input_driven_three_level_nested_for_graph, id="input-driven"),
pytest.param(_malformed_three_level_nested_for_graph, id="malformed-link"),
],
)