"""simple docstring"""




import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
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_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin







@skip_mps
class __snake_case    (						lowerCamelCase__       ,		lowerCamelCase__       ,		unittest.TestCase		):
   lowerCAmelCase_														=							IFImgaImgSuperResolutionPipeline
   lowerCAmelCase_														=							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'width', 'height'}
   lowerCAmelCase_														=							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"}		)
   lowerCAmelCase_														=							PipelineTesterMixin.required_optional_params - {'latents'}




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     return self._get_superresolution_dummy_components()




   def      __a						(   self      :    Optional[int]	,     _lowercase      :    Tuple	,     _lowercase      :    List[Any]=0				):



     """simple docstring"""






     if str(lowercase__				).startswith("""mps"""				):
       SCREAMING_SNAKE_CASE__      							=				torch.manual_seed(lowercase__				)
     else:
       SCREAMING_SNAKE_CASE__      							=				torch.Generator(device=lowercase__				).manual_seed(lowercase__				)

     SCREAMING_SNAKE_CASE__      							=				floats_tensor((1, 3, 32, 32)	,     rng=random.Random(lowercase__				)				).to(lowercase__				)
     SCREAMING_SNAKE_CASE__      							=				floats_tensor((1, 3, 16, 16)	,     rng=random.Random(lowercase__				)				).to(lowercase__				)

     SCREAMING_SNAKE_CASE__      							=				{
         """prompt""": """A painting of a squirrel eating a burger""",
         """image""": image,
         """original_image""": original_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      __a						(   self      :    int				):



     """simple docstring"""






     self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3				)




   def      __a						(   self      :    int				):



     """simple docstring"""






     self._test_save_load_optional_components()




   @unittest.skipIf(torch_device != """cuda"""	,     reason="""float16 requires CUDA"""				)
   def      __a						(   self      :    Tuple				):



     """simple docstring"""






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




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     self._test_attention_slicing_forward_pass(expected_max_diff=1E-2				)




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



     """simple docstring"""






     self._test_save_load_local()




   def      __a						(   self      :    Any				):



     """simple docstring"""






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

import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('''9.1.0'''):
   __lowerCamelCase      :			Optional[Any]							       =		{
       '''linear''': PIL.Image.Resampling.BILINEAR,
       '''bilinear''': PIL.Image.Resampling.BILINEAR,
       '''bicubic''': PIL.Image.Resampling.BICUBIC,
       '''lanczos''': PIL.Image.Resampling.LANCZOS,
       '''nearest''': PIL.Image.Resampling.NEAREST,
   }
else:
   __lowerCamelCase      :			int							       =		{
       '''linear''': PIL.Image.LINEAR,
       '''bilinear''': PIL.Image.BILINEAR,
       '''bicubic''': PIL.Image.BICUBIC,
       '''lanczos''': PIL.Image.LANCZOS,
       '''nearest''': PIL.Image.NEAREST,
   }



def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	int     )							-> Optional[Any]:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__      							=				(images / 2 + 0.5).clamp(0			,					1     )
  SCREAMING_SNAKE_CASE__      							=				images.cpu().permute(0			,					2			,					3			,					1     ).float().numpy()
  SCREAMING_SNAKE_CASE__      							=				numpy_to_pil(__UpperCamelCase     )
  return images






def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	int     )							-> int:







  """simple docstring"""





  if images.ndim == 3:
    SCREAMING_SNAKE_CASE__      							=				images[None, ...]
  SCREAMING_SNAKE_CASE__      							=				(images * 2_55).round().astype("""uint8"""     )
  if images.shape[-1] == 1:
    # special case for grayscale (single channel) images
    SCREAMING_SNAKE_CASE__      							=				[Image.fromarray(image.squeeze()			,					mode="""L"""     ) for image in images]
  else:
    SCREAMING_SNAKE_CASE__      							=				[Image.fromarray(__UpperCamelCase     ) for image in images]

  return pil_images
