1
0
mirror of https://github.com/huggingface/diffusers.git synced 2026-10-27 21:33:02 +03:00
Files
diffusers/tests/modular_pipelines/test_conditional_pipeline_blocks.py
YiYi Xu 5f9a5583a4 Modular branch specific defaults (#14234)
* init

* Ensure branch-specific input defaults in ConditionalPipelineBlocks

When sibling blocks of a ConditionalPipelineBlocks declare different
defaults for the same input, combine_inputs now merges the default to
None and records the per-block defaults in a new
InputParam.defaults_by_block field, instead of silently promoting the
non-None default. get_block_state falls back to the block's own declared
default when the state value is None, so each branch resolves its own
default when it actually runs and a branch using None as a "user didn't
pass this" sentinel is no longer polluted by a sibling's default.

Docstrings render conflicted defaults as e.g. "defaults to None or 189,
depending on the workflow".

Fixes #14132

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Remove stale note about kwargs_type removal — the feature is staying

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Clarify get_block_state default-resolution comment, drop getattr guards

defaults_by_block is a dataclass field so it always exists; the checks
are already inside the hasattr(param, "required") InputParam guard.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Add example comments to combine_inputs and formatters, revert description block restructure

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Comment the duplicate-input path in combine_inputs with the strength example

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Rework branch-defaults tests to reuse the image block fixtures

strength on img2img (0.3) / inpaint (0.9999), text2img as the None
sentinel branch; drop the separate video fixtures. Also add examples to
the combine_inputs accumulator comments.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Simplify branch-defaults tests: drop t2i sentinel and resolved_strength output

Assert on state.get("strength") directly (write-back records the branch's
resolved value); cover None-vs-non-None merge with a direct combine_inputs
test instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Move combine_inputs tests to module level as test_combine_inputs_*

block-a/block-b names, whole-list assertions, plus new coverage:
first-occurrence-wins, disagreement records every block, direct nested
prefixing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Tighten merge assertions: unique strength param + full InputParam equality

Also group combine_inputs tests under TestConditionalBlocksInputs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Assert workflow in runtime tests, fold doc rendering into merge tests

workflow is now a declared intermediate output on the fixture blocks so
each runtime test shows which branch ran; the merge tests also assert
the rendered docstring (nested case covers distinct-value dedupe).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: declare defaults in InputParam, not inside __call__

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: side-by-side default example in modular.md, test reference in combine_inputs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Fix doc-builder formatting; regenerate cosmos3 use_system_prompt docstring

The combine_inputs docstring example is now a fenced code block so
doc-builder doesn't reflow it.

The cosmos3 use_system_prompt docstring change reflects a real behavior
fix this PR makes: the transfer/action branches declare default=True
while the standard text branch declares default=None as a sentinel that
falls back to the default_use_system_prompt config. Previously the
merged default True leaked into state before branch selection, so the
config fallback never fired for the standard workflow; each branch now
resolves its own default and the config is honored again.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Regenerate cosmos3 auto docstrings

Mechanical output of utils/modular_auto_docstring.py for the cosmos
files this PR touches; regeneration of the remaining stale pipelines
moved to its own PR.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-31 13:53:28 -10:00

375 lines
14 KiB
Python

# Copyright 2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from diffusers.modular_pipelines import (
AutoPipelineBlocks,
ConditionalPipelineBlocks,
InputParam,
ModularPipelineBlocks,
OutputParam,
)
from diffusers.modular_pipelines.modular_pipeline_utils import combine_inputs
class TestConditionalBlocksInputs:
def test_combine_inputs_agreeing_defaults_stay_untouched(self):
combined = combine_inputs(
("block-a", [InputParam(name="x", default=5)]),
("block-b", [InputParam(name="x", default=5)]),
)
assert combined == [InputParam(name="x", default=5)]
def test_combine_inputs_first_occurrence_wins(self):
combined = combine_inputs(
("block-a", [InputParam(name="x", default=5, description="from block-a")]),
("block-b", [InputParam(name="x", default=5, description="from block-b")]),
)
# duplicate inputs keep the first occurrence: block-b's description is dropped
assert combined == [InputParam(name="x", default=5, description="from block-a")]
def test_combine_inputs_none_default_counts_as_disagreement(self):
# a None default is a sentinel ("user didn't pass this"), it must not be overridden by a sibling's default
combined = combine_inputs(
("block-a", [InputParam(name="x", default=None)]),
("block-b", [InputParam(name="x", default=5)]),
)
assert combined == [InputParam(name="x", default=None, defaults_by_block={"block-a": None, "block-b": 5})]
def test_combine_inputs_disagreement_records_every_block(self):
combined = combine_inputs(
("block-a", [InputParam(name="x", default=1)]),
("block-b", [InputParam(name="x", default=2)]),
("block-c", [InputParam(name="x", default=1)]),
)
# any disagreement records every block's default, including the ones that agree with each other
assert combined == [
InputParam(name="x", default=None, defaults_by_block={"block-a": 1, "block-b": 2, "block-c": 1})
]
def test_combine_inputs_nested_defaults_prefixed(self):
# block-b is itself a conditional block whose branches already disagree, so its param carries
# defaults_by_block; merging at the outer level prefixes those entries with the sub-block name
combined = combine_inputs(
("block-a", [InputParam(name="x", default=3)]),
("block-b", [InputParam(name="x", default=None, defaults_by_block={"branch-a": 1, "branch-b": 2})]),
)
assert combined == [
InputParam(
name="x",
default=None,
defaults_by_block={"block-a": 3, "block-b.branch-a": 1, "block-b.branch-b": 2},
)
]
class TextToImageBlock(ModularPipelineBlocks):
model_name = "text2img"
@property
def inputs(self):
return [InputParam(name="prompt")]
@property
def intermediate_outputs(self):
return [OutputParam(name="workflow")]
@property
def description(self):
return "text-to-image workflow"
def __call__(self, components, state):
block_state = self.get_block_state(state)
block_state.workflow = "text2img"
self.set_block_state(state, block_state)
return components, state
class ImageToImageBlock(ModularPipelineBlocks):
model_name = "img2img"
@property
def inputs(self):
return [
InputParam(name="prompt"),
InputParam(name="image"),
InputParam(name="strength", type_hint=float, default=0.3),
]
@property
def intermediate_outputs(self):
return [OutputParam(name="workflow")]
@property
def description(self):
return "image-to-image workflow"
def __call__(self, components, state):
block_state = self.get_block_state(state)
block_state.workflow = "img2img"
self.set_block_state(state, block_state)
return components, state
class InpaintBlock(ModularPipelineBlocks):
model_name = "inpaint"
@property
def inputs(self):
return [
InputParam(name="prompt"),
InputParam(name="image"),
InputParam(name="mask"),
InputParam(name="strength", type_hint=float, default=0.9999),
]
@property
def intermediate_outputs(self):
return [OutputParam(name="workflow")]
@property
def description(self):
return "inpaint workflow"
def __call__(self, components, state):
block_state = self.get_block_state(state)
block_state.workflow = "inpaint"
self.set_block_state(state, block_state)
return components, state
class ConditionalImageBlocks(ConditionalPipelineBlocks):
block_classes = [InpaintBlock, ImageToImageBlock, TextToImageBlock]
block_names = ["inpaint", "img2img", "text2img"]
block_trigger_inputs = ["mask", "image"]
default_block_name = "text2img"
@property
def description(self):
return "Conditional image blocks for testing"
def select_block(self, mask=None, image=None) -> str | None:
if mask is not None:
return "inpaint"
if image is not None:
return "img2img"
return None # falls back to default_block_name
class OptionalConditionalBlocks(ConditionalPipelineBlocks):
block_classes = [InpaintBlock, ImageToImageBlock]
block_names = ["inpaint", "img2img"]
block_trigger_inputs = ["mask", "image"]
default_block_name = None # no default; block can be skipped
@property
def description(self):
return "Optional conditional blocks (skippable)"
def select_block(self, mask=None, image=None) -> str | None:
if mask is not None:
return "inpaint"
if image is not None:
return "img2img"
return None
class AutoImageBlocks(AutoPipelineBlocks):
block_classes = [InpaintBlock, ImageToImageBlock, TextToImageBlock]
block_names = ["inpaint", "img2img", "text2img"]
block_trigger_inputs = ["mask", "image", None]
@property
def description(self):
return "Auto image blocks for testing"
class TestConditionalPipelineBlocksSelectBlock:
def test_select_block_with_mask(self):
blocks = ConditionalImageBlocks()
assert blocks.select_block(mask="something") == "inpaint"
def test_select_block_with_image(self):
blocks = ConditionalImageBlocks()
assert blocks.select_block(image="something") == "img2img"
def test_select_block_with_mask_and_image(self):
blocks = ConditionalImageBlocks()
assert blocks.select_block(mask="m", image="i") == "inpaint"
def test_select_block_no_triggers_returns_none(self):
blocks = ConditionalImageBlocks()
assert blocks.select_block() is None
def test_select_block_explicit_none_values(self):
blocks = ConditionalImageBlocks()
assert blocks.select_block(mask=None, image=None) is None
class TestConditionalPipelineBlocksWorkflowSelection:
def test_default_workflow_when_no_triggers(self):
blocks = ConditionalImageBlocks()
execution = blocks.get_execution_blocks()
assert execution is not None
assert isinstance(execution, TextToImageBlock)
def test_mask_trigger_selects_inpaint(self):
blocks = ConditionalImageBlocks()
execution = blocks.get_execution_blocks(mask=True)
assert isinstance(execution, InpaintBlock)
def test_image_trigger_selects_img2img(self):
blocks = ConditionalImageBlocks()
execution = blocks.get_execution_blocks(image=True)
assert isinstance(execution, ImageToImageBlock)
def test_mask_and_image_selects_inpaint(self):
blocks = ConditionalImageBlocks()
execution = blocks.get_execution_blocks(mask=True, image=True)
assert isinstance(execution, InpaintBlock)
def test_skippable_block_returns_none(self):
blocks = OptionalConditionalBlocks()
execution = blocks.get_execution_blocks()
assert execution is None
def test_skippable_block_still_selects_when_triggered(self):
blocks = OptionalConditionalBlocks()
execution = blocks.get_execution_blocks(image=True)
assert isinstance(execution, ImageToImageBlock)
class TestAutoPipelineBlocksSelectBlock:
def test_auto_select_mask(self):
blocks = AutoImageBlocks()
assert blocks.select_block(mask="m") == "inpaint"
def test_auto_select_image(self):
blocks = AutoImageBlocks()
assert blocks.select_block(image="i") == "img2img"
def test_auto_select_default(self):
blocks = AutoImageBlocks()
# No trigger -> returns None -> falls back to default (text2img)
assert blocks.select_block() is None
def test_auto_select_priority_order(self):
blocks = AutoImageBlocks()
assert blocks.select_block(mask="m", image="i") == "inpaint"
class TestAutoPipelineBlocksWorkflowSelection:
def test_auto_default_workflow(self):
blocks = AutoImageBlocks()
execution = blocks.get_execution_blocks()
assert isinstance(execution, TextToImageBlock)
def test_auto_mask_workflow(self):
blocks = AutoImageBlocks()
execution = blocks.get_execution_blocks(mask=True)
assert isinstance(execution, InpaintBlock)
def test_auto_image_workflow(self):
blocks = AutoImageBlocks()
execution = blocks.get_execution_blocks(image=True)
assert isinstance(execution, ImageToImageBlock)
class TestConditionalPipelineBlocksStructure:
def test_block_names_accessible(self):
blocks = ConditionalImageBlocks()
sub = dict(blocks.sub_blocks)
assert set(sub.keys()) == {"inpaint", "img2img", "text2img"}
def test_sub_block_types(self):
blocks = ConditionalImageBlocks()
sub = dict(blocks.sub_blocks)
assert isinstance(sub["inpaint"], InpaintBlock)
assert isinstance(sub["img2img"], ImageToImageBlock)
assert isinstance(sub["text2img"], TextToImageBlock)
def test_description(self):
blocks = ConditionalImageBlocks()
assert "Conditional" in blocks.description
class NestedImageBlocks(ConditionalPipelineBlocks):
block_classes = [InpaintBlock, AutoImageBlocks]
block_names = ["refine", "image"]
block_trigger_inputs = ["mask"]
default_block_name = "image"
@property
def description(self):
return "Nested conditional blocks: refine when `mask` is provided, auto image blocks otherwise"
def select_block(self, mask=None) -> str | None:
if mask is not None:
return "refine"
return None
class TestConditionalBlocksBranchDefaults:
def test_conflicting_defaults_merge_to_none(self):
# inpaint (0.9999) and img2img (0.3) disagree on strength: the merged input's default becomes None
# and the per-block defaults are recorded in defaults_by_block
strength_params = [p for p in AutoImageBlocks().inputs if p.name == "strength"]
assert len(strength_params) == 1
assert strength_params[0] == InputParam(
name="strength",
type_hint=float,
default=None,
defaults_by_block={"inpaint": 0.9999, "img2img": 0.3},
)
# the docstring renders the distinct per-block defaults instead of a single value
doc = " ".join(AutoImageBlocks().doc.split())
assert "strength (`float`, *optional*, defaults to 0.9999 or 0.3, depending on the workflow):" in doc
def test_branch_resolves_own_default(self):
pipe = AutoImageBlocks().init_pipeline()
state = pipe(prompt="p", image="i")
assert state.get("workflow") == "img2img"
assert state.get("strength") == 0.3
state = pipe(prompt="p", image="i", mask="m")
assert state.get("workflow") == "inpaint"
assert state.get("strength") == 0.9999
def test_explicit_value_overrides_branch_default(self):
pipe = AutoImageBlocks().init_pipeline()
state = pipe(prompt="p", image="i", strength=0.7)
assert state.get("workflow") == "img2img"
assert state.get("strength") == 0.7
def test_standalone_branch_keeps_default(self):
pipe = ImageToImageBlock().init_pipeline()
state = pipe(prompt="p", image="i")
assert state.get("workflow") == "img2img"
assert state.get("strength") == 0.3
def test_nested_defaults_prefixed_with_sub_block_name(self):
strength_params = [p for p in NestedImageBlocks().inputs if p.name == "strength"]
assert len(strength_params) == 1
assert strength_params[0] == InputParam(
name="strength",
type_hint=float,
default=None,
defaults_by_block={"refine": 0.9999, "image.inpaint": 0.9999, "image.img2img": 0.3},
)
# the docstring renders distinct values only: refine and image.inpaint both use 0.9999, shown once
doc = " ".join(NestedImageBlocks().doc.split())
assert "strength (`float`, *optional*, defaults to 0.9999 or 0.3, depending on the workflow):" in doc