






import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





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





   def 	__lowerCAmelCase							(	self			):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''			)
         _lowerCAmelCase 			=  sd_pipe.to(_lowerCAmelCase			)
         sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase			)

         sd_pipe.set_scheduler('''sample_euler'''			)

         _lowerCAmelCase 			=  '''A painting of a squirrel eating a burger'''
         _lowerCAmelCase 			=  torch.manual_seed(0			)
         _lowerCAmelCase 			=  sd_pipe([prompt]       ,     generator=_lowerCAmelCase       ,     guidance_scale=9.0       ,     num_inference_steps=20       ,     output_type='''np'''			)

         _lowerCAmelCase 			=  output.images

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

         assert image.shape == (1, 512, 512, 3)
         _lowerCAmelCase 			=  np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]			)

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





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''			)
         _lowerCAmelCase 			=  sd_pipe.to(_lowerCAmelCase			)
         sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase			)

         sd_pipe.set_scheduler('''sample_euler'''			)

         _lowerCAmelCase 			=  '''A painting of a squirrel eating a burger'''
         _lowerCAmelCase 			=  torch.manual_seed(0			)
         _lowerCAmelCase 			=  sd_pipe([prompt]       ,     generator=_lowerCAmelCase       ,     guidance_scale=9.0       ,     num_inference_steps=20       ,     output_type='''np'''			)

         _lowerCAmelCase 			=  output.images

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

         assert image.shape == (1, 512, 512, 3)
         _lowerCAmelCase 			=  np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]			)

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





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''			)
         _lowerCAmelCase 			=  sd_pipe.to(_lowerCAmelCase			)
         sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase			)

         sd_pipe.set_scheduler('''sample_dpmpp_2m'''			)

         _lowerCAmelCase 			=  '''A painting of a squirrel eating a burger'''
         _lowerCAmelCase 			=  torch.manual_seed(0			)
         _lowerCAmelCase 			=  sd_pipe(
             [prompt]       ,     generator=_lowerCAmelCase       ,     guidance_scale=7.5       ,     num_inference_steps=15       ,     output_type='''np'''       ,     use_karras_sigmas=_lowerCAmelCase       ,     )

         _lowerCAmelCase 			=  output.images

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

         assert image.shape == (1, 512, 512, 3)
         _lowerCAmelCase 			=  np.array(
             [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]			)

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




def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Optional[Any]				)->Any:  # noqa: E741
      _lowerCAmelCase 			=  len(_SCREAMING_SNAKE_CASE				)
      _lowerCAmelCase 			=  0
      _lowerCAmelCase 			=  [0] * n
      _lowerCAmelCase 			=  [False] * n
      _lowerCAmelCase 			=  [False] * n

      def dfs(_SCREAMING_SNAKE_CASE   :	List[str]     ,				_SCREAMING_SNAKE_CASE   :	List[Any]     ,				_SCREAMING_SNAKE_CASE   :	Optional[int]     ,				_SCREAMING_SNAKE_CASE   :	int				):
            if parent == root:
                  out_edge_count += 1
            _lowerCAmelCase 			=  True
            _lowerCAmelCase 			=  at

            for to in l[at]:
                  if to == parent:
                        pass
                  elif not visited[to]:
                        _lowerCAmelCase 			=  dfs(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)
                        _lowerCAmelCase 			=  min(low[at]     ,				low[to]				)

                        # AP found via bridge
                        if at < low[to]:
                              _lowerCAmelCase 			=  True
                        # AP found via cycle
                        if at == low[to]:
                              _lowerCAmelCase 			=  True
                  else:
                        _lowerCAmelCase 			=  min(low[at]     ,				_SCREAMING_SNAKE_CASE				)
            return out_edge_count

      for i in range(_SCREAMING_SNAKE_CASE				):
            if not visited[i]:
                  _lowerCAmelCase 			=  0
                  _lowerCAmelCase 			=  dfs(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				-1     ,				_SCREAMING_SNAKE_CASE				)
                  _lowerCAmelCase 			=  out_edge_count > 1

      for x in range(len(_SCREAMING_SNAKE_CASE				)				):
            if is_art[x] is True:
                  print(_SCREAMING_SNAKE_CASE				)


# Adjacency list of graph
UpperCAmelCase_									=							{
    0: [1, 2],
    1: [0, 2],
    2: [0, 1, 3, 5],
    3: [2, 4],
    4: [3],
    5: [2, 6, 8],
    6: [5, 7],
    7: [6, 8],
    8: [5, 7],
}


compute_ap(data)