





import random
import unittest

import torch

from diffusers import IFInpaintingPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin







@skip_mps
class      lowerCamelCase(						__snake_case			, __snake_case			, unittest.TestCase	):
     '''simple docstring'''


     __magic_name__   =			IFInpaintingPipeline
     __magic_name__   =			TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'}
     __magic_name__   =			TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     __magic_name__   =			PipelineTesterMixin.required_optional_params - {'latents'}




     def        lowerCAmelCase__		(    self							):
         return self._get_dummy_components()




     def        lowerCAmelCase__		(    self				,     snake_case_				,     snake_case_=0							):
         if str(snake_case_							).startswith('mps'							):
             _A     =							torch.manual_seed(snake_case_							)
         else:
             _A     =							torch.Generator(device=snake_case_							).manual_seed(snake_case_							)

         _A     =							floats_tensor((1, 3, 32, 32)				,     rng=random.Random(snake_case_							)							).to(snake_case_							)
         _A     =							floats_tensor((1, 3, 32, 32)				,     rng=random.Random(snake_case_							)							).to(snake_case_							)

         _A     =							{
             'prompt': 'A painting of a squirrel eating a burger',
             'image': image,
             'mask_image': mask_image,
             'generator': generator,
             'num_inference_steps': 2,
             'output_type': 'numpy',
         }

         return inputs




     @unittest.skipIf(
         torch_device != 'cuda' or not is_xformers_available()				,     reason='XFormers attention is only available with CUDA and `xformers` installed'				,     )
     def        lowerCAmelCase__		(    self							):
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3							)




     def        lowerCAmelCase__		(    self							):
         self._test_save_load_optional_components()




     @unittest.skipIf(torch_device != 'cuda'				,     reason='float16 requires CUDA'							)
     def        lowerCAmelCase__		(    self							):
         # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
         super().test_save_load_floataa(expected_max_diff=1E-1							)




     def        lowerCAmelCase__		(    self							):
         self._test_attention_slicing_forward_pass(expected_max_diff=1E-2							)




     def        lowerCAmelCase__		(    self							):
         self._test_save_load_local()




     def        lowerCAmelCase__		(    self							):
         self._test_inference_batch_single_identical(
             expected_max_diff=1E-2				,     )












import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class      lowerCamelCase(						unittest.TestCase	):
     '''simple docstring'''


     @property
     def        lowerCAmelCase__		(    self							):
         torch.manual_seed(0							)
         _A     =							UNetaDModel(
             block_out_channels=(32, 64)				,     layers_per_block=2				,     sample_size=32				,     in_channels=3				,     out_channels=3				,     down_block_types=('DownBlock2D', 'AttnDownBlock2D')				,     up_block_types=('AttnUpBlock2D', 'UpBlock2D')				,     )
         return model




     def        lowerCAmelCase__		(    self							):
         _A     =							self.dummy_uncond_unet
         _A     =							KarrasVeScheduler()

         _A     =							KarrasVePipeline(unet=snake_case_				,     scheduler=snake_case_							)
         pipe.to(snake_case_							)
         pipe.set_progress_bar_config(disable=snake_case_							)

         _A     =							torch.manual_seed(0							)
         _A     =							pipe(num_inference_steps=2				,     generator=snake_case_				,     output_type='numpy'							).images

         _A     =							torch.manual_seed(0							)
         _A     =							pipe(num_inference_steps=2				,     generator=snake_case_				,     output_type='numpy'				,     return_dict=snake_case_							)[0]

         _A     =							image[0, -3:, -3:, -1]
         _A     =							image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 32, 32, 3)
         _A     =							np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]							)

         assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2
         assert np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1E-2







@slow
@require_torch
class      lowerCamelCase(						unittest.TestCase	):
     '''simple docstring'''


     def        lowerCAmelCase__		(    self							):
         _A     =							'google/ncsnpp-celebahq-256'
         _A     =							UNetaDModel.from_pretrained(snake_case_							)
         _A     =							KarrasVeScheduler()

         _A     =							KarrasVePipeline(unet=snake_case_				,     scheduler=snake_case_							)
         pipe.to(snake_case_							)
         pipe.set_progress_bar_config(disable=snake_case_							)

         _A     =							torch.manual_seed(0							)
         _A     =							pipe(num_inference_steps=20				,     generator=snake_case_				,     output_type='numpy'							).images

         _A     =							image[0, -3:, -3:, -1]
         assert image.shape == (1, 256, 256, 3)
         _A     =							np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]							)

         assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2





