Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
Fix deterministic issue when getting pipeline dtype and device#10696
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,7 +19,7 @@ | ||
| UNet2DConditionModel, | ||
| ) | ||
| from diffusers.pipelines.pipeline_loading_utils import is_safetensors_compatible, variant_compatible_siblings | ||
| from diffusers.utils.testing_utils import torch_device | ||
| from diffusers.utils.testing_utils import require_torch_gpu, torch_device | ||
| class IsSafetensorsCompatibleTests(unittest.TestCase): | ||
| @@ -585,3 +585,104 @@ def test_video_to_video(self): | ||
| with io.StringIO() as stderr, contextlib.redirect_stderr(stderr): | ||
| _ = pipe(**inputs) | ||
| self.assertTrue(stderr.getvalue() == "", "Progress bar should be disabled") | ||
| @require_torch_gpu | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need this to test multiple devices across pipeline components, so that testing with distinct pipeline devices fails without the code changes. Otherwise, I guess only CPU can be available? | ||
| class PipelineDeviceAndDtypeStabilityTests(unittest.TestCase): | ||
| expected_pipe_device = torch.device("cuda:0") | ||
| expected_pipe_dtype = torch.float64 | ||
| def get_dummy_components_image_generation(self): | ||
| 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") | ||
| components = { | ||
| "unet": unet, | ||
| "scheduler": scheduler, | ||
| "vae": vae, | ||
| "text_encoder": text_encoder, | ||
| "tokenizer": tokenizer, | ||
| "safety_checker": None, | ||
| "feature_extractor": None, | ||
| "image_encoder": None, | ||
| } | ||
| return components | ||
| def test_deterministic_device(self): | ||
| components = self.get_dummy_components_image_generation() | ||
| pipe = StableDiffusionPipeline(**components) | ||
| pipe.to(device=torch_device, dtype=torch.float32) | ||
| pipe.unet.to(device="cpu") | ||
| pipe.vae.to(device="cuda") | ||
| pipe.text_encoder.to(device="cuda:0") | ||
| pipe_device = pipe.device | ||
| self.assertEqual( | ||
| self.expected_pipe_device, | ||
| pipe_device, | ||
| f"Wrong expected device. Expected {self.expected_pipe_device}. Got {pipe_device}.", | ||
| ) | ||
| def test_deterministic_dtype(self): | ||
| components = self.get_dummy_components_image_generation() | ||
| pipe = StableDiffusionPipeline(**components) | ||
| pipe.to(device=torch_device, dtype=torch.float32) | ||
| pipe.unet.to(dtype=torch.float16) | ||
| pipe.vae.to(dtype=torch.float32) | ||
| pipe.text_encoder.to(dtype=torch.float64) | ||
| pipe_dtype = pipe.dtype | ||
| self.assertEqual( | ||
| self.expected_pipe_dtype, | ||
| pipe_dtype, | ||
| f"Wrong expected dtype. Expected {self.expected_pipe_dtype}. Got {pipe_dtype}.", | ||
| ) | ||
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.
I'm not sure if using
sortedis necessary here, sinceexpected_modulescomes from the_get_signature_keyscall which already callssortedinternally.However, I leave it that way because it allows to be completely decoupled from the
_get_signature_keysfunction. And I don't think it will add a big performance overhead (sorting of n elements, n=~10, elements already sorted).