





'''simple docstring'''


import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class 				a_       (				lowerCamelCase__     ,						unittest.TestCase							):
     __A								=      DDIMPipeline
     __A								=      UNCONDITIONAL_IMAGE_GENERATION_PARAMS
     __A								=      PipelineTesterMixin.required_optional_params - {
         'num_images_per_prompt',
         'latents',
         'callback',
         'callback_steps',
     }
     __A								=      UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
     __A								=      False





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        torch.manual_seed(0						)
        lowercase_      :str         =			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")     ,				)
        lowercase_      :Union[str, Any]         =			DDIMScheduler()
        lowercase_      :Union[str, Any]         =			{"unet": unet, "scheduler": scheduler}
        return components





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: List[Any]     ,				lowercase		: List[Any]=0						):





        """simple docstring"""




        if str(lowercase						).startswith("mps"						):
           lowercase_      :Tuple         =			torch.manual_seed(lowercase						)
        else:
           lowercase_      :Any         =			torch.Generator(device=lowercase						).manual_seed(lowercase						)
        lowercase_      :Tuple         =			{
            "batch_size": 1,
            "generator": generator,
            "num_inference_steps": 2,
            "output_type": "numpy",
        }
        return inputs





     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        lowercase_      :Dict         =			"cpu"

        lowercase_      :List[str]         =			self.get_dummy_components()
        lowercase_      :Any         =			self.pipeline_class(**lowercase						)
        pipe.to(lowercase						)
        pipe.set_progress_bar_config(disable=lowercase						)

        lowercase_      :Any         =			self.get_dummy_inputs(lowercase						)
        lowercase_      :Dict         =			pipe(**lowercase						).images
        lowercase_      :Any         =			image[0, -3:, -3:, -1]

        self.assertEqual(image.shape     ,				(1, 32, 32, 3)						)
        lowercase_      :int         =			np.array(
            [1.0_0_0e0_0, 5.7_1_7e-0_1, 4.7_1_7e-0_1, 1.0_0_0e0_0, 0.0_0_0e0_0, 1.0_0_0e0_0, 3.0_0_0e-0_4, 0.0_0_0e0_0, 9.0_0_0e-0_4]						)
        lowercase_      :Optional[Any]         =			np.abs(image_slice.flatten() - expected_slice						).max()
        self.assertLessEqual(lowercase     ,				1e-3						)





     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3						)





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        super().test_save_load_local(expected_max_difference=3e-3						)





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        super().test_save_load_optional_components(expected_max_difference=3e-3						)







     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        super().test_inference_batch_single_identical(expected_max_diff=3e-3						)







@slow
@require_torch_gpu
class 				a_       (				unittest.TestCase							):





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :List[str]         =			"google/ddpm-cifar10-32"

        lowercase_      :int         =			UNetaDModel.from_pretrained(lowercase						)
        lowercase_      :Optional[int]         =			DDIMScheduler()

        lowercase_      :Tuple         =			DDIMPipeline(unet=lowercase     ,				scheduler=lowercase						)
        ddim.to(lowercase						)
        ddim.set_progress_bar_config(disable=lowercase						)

        lowercase_      :Any         =			torch.manual_seed(0						)
        lowercase_      :List[str]         =			ddim(generator=lowercase     ,				eta=0.0     ,				output_type="numpy"						).images

        lowercase_      :str         =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 32, 32, 3)
        lowercase_      :Union[str, Any]         =			np.array([0.17_23, 0.16_17, 0.16_00, 0.16_26, 0.14_97, 0.15_13, 0.15_05, 0.14_42, 0.14_53]						)

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







     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        lowercase_      :List[Any]         =			"google/ddpm-ema-bedroom-256"

        lowercase_      :Tuple         =			UNetaDModel.from_pretrained(lowercase						)
        lowercase_      :str         =			DDIMScheduler.from_pretrained(lowercase						)

        lowercase_      :Any         =			DDIMPipeline(unet=lowercase     ,				scheduler=lowercase						)
        ddpm.to(lowercase						)
        ddpm.set_progress_bar_config(disable=lowercase						)

        lowercase_      :Optional[Any]         =			torch.manual_seed(0						)
        lowercase_      :Optional[int]         =			ddpm(generator=lowercase     ,				output_type="numpy"						).images

        lowercase_      :Optional[Any]         =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 256, 256, 3)
        lowercase_      :Tuple         =			np.array([0.00_60, 0.02_01, 0.03_44, 0.00_24, 0.00_18, 0.00_02, 0.00_22, 0.00_00, 0.00_69]						)

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









'''simple docstring'''


def 							UpperCAmelCase_  (						__lowerCamelCase : dict			):
   lowercase_      :set[int]         =			set()
   # To detect a back edge, keep track of vertices currently in the recursion stack
   lowercase_      :set[int]         =			set()
   return any(
       node not in visited and depth_first_search(__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase			)
       for node in graph			)
def 							UpperCAmelCase_  (						__lowerCamelCase : dict     ,__lowerCamelCase : int     ,__lowerCamelCase : set     ,__lowerCamelCase : set			):
   visited.add(__lowerCamelCase			)
   rec_stk.add(__lowerCamelCase			)

   for node in graph[vertex]:
      if node not in visited:
         if depth_first_search(__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase			):
            return True
      elif node in rec_stk:
         return True

    # The node needs to be removed from recursion stack before function ends
   rec_stk.remove(__lowerCamelCase			)
   return False


if __name__ == "__main__":
 from doctest import testmod

 testmod()


