





import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
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        snake_case_    (		__lowercase					,__lowercase					,unittest.TestCase						):
  A_      							=   IFInpaintingSuperResolutionPipeline
  A_      							=   TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'}
  A_      							=   TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'}						)
  A_      							=   PipelineTesterMixin.required_optional_params - {'latents'}






  def 				UpperCAmelCase__						(							self			:					str      )->List[Any]:






   '''simple docstring'''

   return self._get_superresolution_dummy_components()






  def 				UpperCAmelCase__						(							self			:					int       ,  _snake_case			:					Tuple       ,  _snake_case			:					List[str]=0      )->Tuple:






   '''simple docstring'''

   if str(_snake_case      ).startswith("""mps"""      ):
    __lowerCAmelCase					:	List[str]      =       torch.manual_seed(_snake_case      )
   else:
    __lowerCAmelCase					:	int      =       torch.Generator(device=_snake_case      ).manual_seed(_snake_case      )

   __lowerCAmelCase					:	Tuple      =       floats_tensor((1, 3, 16, 16)       ,  rng=random.Random(_snake_case      )      ).to(_snake_case      )
   __lowerCAmelCase					:	str      =       floats_tensor((1, 3, 32, 32)       ,  rng=random.Random(_snake_case      )      ).to(_snake_case      )
   __lowerCAmelCase					:	Dict      =       floats_tensor((1, 3, 32, 32)       ,  rng=random.Random(_snake_case      )      ).to(_snake_case      )

   __lowerCAmelCase					:	List[Any]      =       {
       """prompt""": """A painting of a squirrel eating a burger""",
       """image""": image,
       """original_image""": original_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 				UpperCAmelCase__						(							self			:					int      )->Dict:






   '''simple docstring'''

   self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3      )






  def 				UpperCAmelCase__						(							self			:					Any      )->Dict:






   '''simple docstring'''

   self._test_save_load_optional_components()






  @unittest.skipIf(torch_device != """cuda"""       ,  reason="""float16 requires CUDA"""      )
  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->str:






   '''simple docstring'''

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






  def 				UpperCAmelCase__						(							self			:					Any      )->Optional[Any]:






   '''simple docstring'''

   self._test_attention_slicing_forward_pass(expected_max_diff=1E-2      )






  def 				UpperCAmelCase__						(							self			:					Dict      )->List[str]:






   '''simple docstring'''

   self._test_save_load_local()






  def 				UpperCAmelCase__						(							self			:					str      )->List[Any]:






   '''simple docstring'''

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






_UpperCAmelCase             =							{0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

_UpperCAmelCase             =							{0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:dict[int, list[int]]   ,   SCREAMING_SNAKE_CASE		:int   ,   SCREAMING_SNAKE_CASE		:list[bool]   )      ->   list[int]:
 __lowerCAmelCase					:	str      =       True
 __lowerCAmelCase					:	str      =       []

 for neighbour in graph[vert]:
  if not visited[neighbour]:
   order += topology_sort(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )

 order.append(SCREAMING_SNAKE_CASE   )

 return order






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:dict[int, list[int]]   ,   SCREAMING_SNAKE_CASE		:int   ,   SCREAMING_SNAKE_CASE		:list[bool]   )      ->   list[int]:
 __lowerCAmelCase					:	Optional[Any]      =       True
 __lowerCAmelCase					:	Union[str, Any]      =       [vert]

 for neighbour in reversed_graph[vert]:
  if not visited[neighbour]:
   component += find_components(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )

 return component







def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:dict[int, list[int]]   )      ->   list[list[int]]:
 __lowerCAmelCase					:	Optional[Any]      =       len(SCREAMING_SNAKE_CASE   ) * [False]
 __lowerCAmelCase					:	dict[int, list[int]]      =       {vert: [] for vert in range(len(SCREAMING_SNAKE_CASE   )   )}

 for vert, neighbours in graph.items():
  for neighbour in neighbours:
   reversed_graph[neighbour].append(SCREAMING_SNAKE_CASE   )

 __lowerCAmelCase					:	Tuple      =       []
 for i, was_visited in enumerate(SCREAMING_SNAKE_CASE   ):
  if not was_visited:
   order += topology_sort(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )

 __lowerCAmelCase					:	Optional[Any]      =       []
 __lowerCAmelCase					:	Any      =       len(SCREAMING_SNAKE_CASE   ) * [False]

 for i in range(len(SCREAMING_SNAKE_CASE   )   ):
  __lowerCAmelCase					:	Optional[int]      =       order[len(SCREAMING_SNAKE_CASE   ) - i - 1]
  if not visited[vert]:
   __lowerCAmelCase					:	Any      =       find_components(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )
   components_list.append(SCREAMING_SNAKE_CASE   )

 return components_list