forked from huggingface/diffusers
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix legacy StableDiffusionPipeline init crash (#6969) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srlynch1
wants to merge
1
commit into
main
Choose a base branch
from
e2e/2026-06-21-r2-diffusers-6969
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import unittest | ||
|
|
||
| import torch | ||
| from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer | ||
|
|
||
| from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, StableDiffusionPipeline, UNet2DConditionModel | ||
| from diffusers.pipelines.pipeline_loading_utils import _fetch_class_library_tuple | ||
|
|
||
| from ..testing_utils import require_torch | ||
|
|
||
|
|
||
| def _get_dummy_sd_components(): | ||
| cross_attention_dim = 8 | ||
| torch.manual_seed(0) | ||
| unet = UNet2DConditionModel( | ||
| block_out_channels=(4, 8), | ||
| layers_per_block=1, | ||
| sample_size=32, | ||
| in_channels=4, | ||
| out_channels=4, | ||
| down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"), | ||
| up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"), | ||
| cross_attention_dim=cross_attention_dim, | ||
| norm_num_groups=2, | ||
| ) | ||
| scheduler = DDIMScheduler( | ||
| beta_start=0.00085, | ||
| beta_end=0.012, | ||
| beta_schedule="scaled_linear", | ||
| clip_sample=False, | ||
| set_alpha_to_one=False, | ||
| ) | ||
| torch.manual_seed(0) | ||
| vae = AutoencoderKL( | ||
| block_out_channels=[4, 8], | ||
| in_channels=3, | ||
| out_channels=3, | ||
| down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"], | ||
| up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"], | ||
| latent_channels=4, | ||
| norm_num_groups=2, | ||
| ) | ||
| torch.manual_seed(0) | ||
| text_encoder_config = CLIPTextConfig( | ||
| bos_token_id=0, | ||
| eos_token_id=2, | ||
| hidden_size=cross_attention_dim, | ||
| intermediate_size=16, | ||
| layer_norm_eps=1e-05, | ||
| num_attention_heads=2, | ||
| num_hidden_layers=2, | ||
| pad_token_id=1, | ||
| vocab_size=1000, | ||
| ) | ||
| text_encoder = CLIPTextModel(text_encoder_config) | ||
| tokenizer = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") | ||
| return { | ||
| "unet": unet, | ||
| "scheduler": scheduler, | ||
| "vae": vae, | ||
| "text_encoder": text_encoder, | ||
| "tokenizer": tokenizer, | ||
| "safety_checker": None, | ||
| "feature_extractor": None, | ||
| } | ||
|
|
||
|
|
||
| class LegacyStableDiffusionPipeline(StableDiffusionPipeline): | ||
| """Simulates community pipelines with old positional super().__init__ signature.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| vae, | ||
| text_encoder, | ||
| tokenizer, | ||
| unet, | ||
| scheduler, | ||
| safety_checker, | ||
| feature_extractor, | ||
| requires_safety_checker=True, | ||
| ): | ||
| super().__init__( | ||
| vae, | ||
| text_encoder, | ||
| tokenizer, | ||
| unet, | ||
| scheduler, | ||
| safety_checker, | ||
| feature_extractor, | ||
| requires_safety_checker, | ||
| ) | ||
|
|
||
|
|
||
| @require_torch | ||
| class RegisterModulesLegacyInitTests(unittest.TestCase): | ||
| def test_register_modules_scalar_bool_no_crash(self): | ||
| class DummyPipeline(DiffusionPipeline): | ||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| pipe = DummyPipeline() | ||
| pipe.register_modules(image_encoder=True) | ||
| self.assertIs(pipe.image_encoder, True) | ||
|
|
||
| def test_fetch_class_library_tuple_scalar_raises_type_error(self): | ||
| with self.assertRaises(TypeError): | ||
| _fetch_class_library_tuple(True) | ||
|
|
||
| def test_legacy_sd_pipeline_positional_init(self): | ||
| components = _get_dummy_sd_components() | ||
| # Legacy positional super().__init__ must not crash (#6969) | ||
| pipe = LegacyStableDiffusionPipeline(**components, requires_safety_checker=False) | ||
| self.assertIsNotNone(pipe.unet) | ||
| # bool lands on image_encoder attribute due to signature mismatch, but init completes | ||
| self.assertIs(pipe.image_encoder, False) | ||
|
|
||
| def test_new_signature_image_encoder_none(self): | ||
| components = _get_dummy_sd_components() | ||
| components["image_encoder"] = None | ||
| pipe = StableDiffusionPipeline(**components, requires_safety_checker=False) | ||
| self.assertIsNone(pipe.image_encoder) | ||
| self.assertFalse(pipe.config.requires_safety_checker) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scalar skips config key
Medium Severity
When
register_modulesreceives a scalar component (e.g. a bool mis-bound toimage_encoder), it sets an emptyregister_dictand skipsregister_to_config, unlikeNonewhich records(None, None). The attribute is still set, but the config omits that module name.componentsbuilds keys from config and then requires they match the init signature, so legacy-init pipelines can raiseValueErrorafter init succeeds.Reviewed by Cursor Bugbot for commit 69ee02e. Configure here.