

'''simple docstring'''

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  (   A ,      A ,      unittest.TestCase ):
       lowerCAmelCase_												=			IFInpaintingPipeline
       lowerCAmelCase_												=			TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"}
       lowerCAmelCase_												=			TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
       lowerCAmelCase_												=			PipelineTesterMixin.required_optional_params - {"latents"}







       def       snake_case (				self      :			List[Any]   ):







            """simple docstring"""







            return self._get_dummy_components()







       def       snake_case (				self      :			Optional[Any]    ,   __lowercase      :			List[Any]    ,   __lowercase      :			Dict=0   ):







            """simple docstring"""







            if str(__lowercase   ).startswith('mps'   ):
                 __lowercase      		=torch.manual_seed(__lowercase   )
            else:
                 __lowercase      		=torch.Generator(device=__lowercase   ).manual_seed(__lowercase   )

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

            __lowercase      		={
                '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       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            self._test_save_load_optional_components()







       @unittest.skipIf(torch_device != 'cuda'    ,   reason='float16 requires CUDA'   )
       def       snake_case (				self      :			List[Any]   ):







            """simple docstring"""







            super().test_save_load_floataa(expected_max_diff=1E-1   )







       def       snake_case (				self      :			List[Any]   ):







            """simple docstring"""







            self._test_attention_slicing_forward_pass(expected_max_diff=1E-2   )







       def       snake_case (				self      :			Union[str, Any]   ):







            """simple docstring"""







            self._test_save_load_local()







       def       snake_case (				self      :			Any   ):







            """simple docstring"""







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





'''simple docstring'''

import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class 	lowerCAmelCase  (   unittest.TestCase ):







       @property
       def       snake_case (				self      :			str   ):







            """simple docstring"""







            torch.manual_seed(0   )
            __lowercase      		=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       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=self.dummy_uncond_unet
            __lowercase      		=PNDMScheduler()

            __lowercase      		=PNDMPipeline(unet=__lowercase    ,   scheduler=__lowercase   )
            pndm.to(__lowercase   )
            pndm.set_progress_bar_config(disable=__lowercase   )

            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   num_inference_steps=20    ,   output_type='numpy'   ).images

            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   num_inference_steps=20    ,   output_type='numpy'    ,   return_dict=__lowercase   )[0]

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

            assert image.shape == (1, 32, 32, 3)
            __lowercase      		=np.array([1.0, 1.0, 0.0, 1.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 ):







       def       snake_case (				self      :			Union[str, Any]   ):







            """simple docstring"""







            __lowercase      		='google/ddpm-cifar10-32'

            __lowercase      		=UNetaDModel.from_pretrained(__lowercase   )
            __lowercase      		=PNDMScheduler()

            __lowercase      		=PNDMPipeline(unet=__lowercase    ,   scheduler=__lowercase   )
            pndm.to(__lowercase   )
            pndm.set_progress_bar_config(disable=__lowercase   )
            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   output_type='numpy'   ).images

            __lowercase      		=image[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            __lowercase      		=np.array([0.1_5_6_4, 0.1_4_6_4_5, 0.1_4_0_6, 0.1_4_7_1_5, 0.1_2_4_2_5, 0.1_4_0_4_5, 0.1_3_1_1_5, 0.1_2_1_7_5, 0.1_2_5]   )

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


