






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      UpperCamelCase (				snake_case__			,       snake_case__			,       unittest.TestCase ):







		"""simple docstring"""



		snake_case    =   IFImgaImgSuperResolutionPipeline
		snake_case    =   TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
		snake_case    =   TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"} )
		snake_case    =   PipelineTesterMixin.required_optional_params - {"latents"}
		def       A(  self		:       List[str]		)    ->   Optional[Any]:


						'''simple docstring'''
						return self._get_superresolution_dummy_components()
		def       A(  self		:       Optional[int]						,_SCREAMING_SNAKE_CASE		:       Optional[int]						,_SCREAMING_SNAKE_CASE		:       Any=0		)    ->   int:


						'''simple docstring'''
						if str(_SCREAMING_SNAKE_CASE		).startswith('mps'		):
										A	 =      torch.manual_seed(_SCREAMING_SNAKE_CASE		)
						else:
										A	 =      torch.Generator(device=_SCREAMING_SNAKE_CASE		).manual_seed(_SCREAMING_SNAKE_CASE		)

						A	 =      floats_tensor((1, 3, 3_2, 3_2)						,rng=random.Random(_SCREAMING_SNAKE_CASE		)		).to(_SCREAMING_SNAKE_CASE		)
						A	 =      floats_tensor((1, 3, 1_6, 1_6)						,rng=random.Random(_SCREAMING_SNAKE_CASE		)		).to(_SCREAMING_SNAKE_CASE		)

						A	 =      {
						    '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		:       List[Any]		)    ->   Optional[int]:


						'''simple docstring'''
						self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3		)
		def       A(  self		:       List[Any]		)    ->   List[Any]:


						'''simple docstring'''
						self._test_save_load_optional_components()
		@unittest.skipIf(torch_device != 'cuda'						,reason='float16 requires CUDA'		)
		def       A(  self		:       List[Any]		)    ->   Tuple:


						'''simple docstring'''
						# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
						super().test_save_load_floataa(expected_max_diff=1E-1		)
		def       A(  self		:       Dict		)    ->   str:


						'''simple docstring'''
						self._test_attention_slicing_forward_pass(expected_max_diff=1E-2		)
		def       A(  self		:       Optional[Any]		)    ->   List[Any]:


						'''simple docstring'''
						self._test_save_load_local()







		def       A(  self		:       str		)    ->   List[str]:


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













import copy
import re
class      UpperCamelCase :







		"""simple docstring"""



		snake_case    =   "hp"
		snake_case    =   {}
		snake_case    =   None
		@classmethod
		def       A(  cls		:       Union[str, Any]						,_SCREAMING_SNAKE_CASE		:       int						,_SCREAMING_SNAKE_CASE		:       List[Any]		)    ->   int:


						'''simple docstring'''
						A	 =      prefix
						A	 =      defaults
						cls.build_naming_info()
		@staticmethod
		def       A(  _SCREAMING_SNAKE_CASE		:       List[str]						,_SCREAMING_SNAKE_CASE		:       List[Any]		)    ->   Optional[Any]:


						'''simple docstring'''
						if len(_SCREAMING_SNAKE_CASE		) == 0:
										return ""
						A	 =      None
						if any(char.isdigit() for char in word		):
										raise Exception(f'Parameters should not contain numbers: \'{word}\' contains a number'		)
						if word in info["short_word"]:
										return info["short_word"][word]
						for prefix_len in range(1						,len(_SCREAMING_SNAKE_CASE		) + 1		):
										A	 =      word[:prefix_len]
										if prefix in info["reverse_short_word"]:
														continue
										else:
														A	 =      prefix
														break

						if short_word is None:
										# Paranoid fallback
										def int_to_alphabetic(_SCREAMING_SNAKE_CASE		:       Optional[int]		):
														A	 =      ''
														while integer != 0:
																		A	 =      chr(ord('A'		) + integer % 1_0		) + s
																		integer //= 1_0
														return s

										A	 =      0
										while True:
														A	 =      word + '#' + int_to_alphabetic(_SCREAMING_SNAKE_CASE		)
														if sword in info["reverse_short_word"]:
																		continue
														else:
																		A	 =      sword
																		break

						A	 =      short_word
						A	 =      word
						return short_word
		@staticmethod
		def       A(  _SCREAMING_SNAKE_CASE		:       Optional[int]						,_SCREAMING_SNAKE_CASE		:       Optional[int]		)    ->   int:


						'''simple docstring'''
						A	 =      param_name.split('_'		)

						A	 =      [TrialShortNamer.shortname_for_word(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		) for word in words]

						# We try to create a separatorless short name, but if there is a collision we have to fallback
						# to a separated short name
						A	 =      ['', '_']

						for separator in separators:
										A	 =      separator.join(_SCREAMING_SNAKE_CASE		)
										if shortname not in info["reverse_short_param"]:
														A	 =      shortname
														A	 =      param_name
														return shortname

						return param_name
		@staticmethod
		def       A(  _SCREAMING_SNAKE_CASE		:       Optional[Any]						,_SCREAMING_SNAKE_CASE		:       Optional[int]		)    ->   List[str]:


						'''simple docstring'''
						A	 =      TrialShortNamer.shortname_for_key(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		)
						A	 =      short_name
						A	 =      param_name
		@classmethod
		def       A(  cls		:       Optional[int]		)    ->   str:


						'''simple docstring'''
						if cls.NAMING_INFO is not None:
										return

						A	 =      {
						    'short_word': {},
						    'reverse_short_word': {},
						    'short_param': {},
						    'reverse_short_param': {},
						}

						A	 =      list(cls.DEFAULTS.keys()		)

						for k in field_keys:
										cls.add_new_param_name(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		)

						A	 =      info
		@classmethod
		def       A(  cls		:       List[str]						,_SCREAMING_SNAKE_CASE		:       str		)    ->   List[Any]:


						'''simple docstring'''
						cls.build_naming_info()
						assert cls.PREFIX is not None
						A	 =      [copy.copy(cls.PREFIX		)]

						for k, v in params.items():
										if k not in cls.DEFAULTS:
														raise Exception(f'You should provide a default value for the param name {k} with value {v}'		)
										if v == cls.DEFAULTS[k]:
														# The default value is not added to the name
														continue

										A	 =      cls.NAMING_INFO['short_param'][k]

										if isinstance(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		):
														A	 =      1 if v else 0

										A	 =      '' if isinstance(_SCREAMING_SNAKE_CASE						,(int, float)		) else '-'
										A	 =      f'{key}{sep}{v}'
										name.append(_SCREAMING_SNAKE_CASE		)

						return "_".join(_SCREAMING_SNAKE_CASE		)







		@classmethod
		def       A(  cls		:       int						,_SCREAMING_SNAKE_CASE		:       Optional[int]		)    ->   str:


						'''simple docstring'''
						A	 =      repr[len(cls.PREFIX		) + 1 :]
						if repr == "":
										A	 =      []
						else:
										A	 =      repr.split('_'		)

						A	 =      {}

						for value in values:
										if "-" in value:
														A ,		A	 =      value.split('-'		)
										else:
														A	 =      re.sub('[0-9.]'						,''						,_SCREAMING_SNAKE_CASE		)
														A	 =      float(re.sub('[^0-9.]'						,''						,_SCREAMING_SNAKE_CASE		)		)

										A	 =      cls.NAMING_INFO['reverse_short_param'][p_k]

										A	 =      p_v

						for k in cls.DEFAULTS:
										if k not in parameters:
														A	 =      cls.DEFAULTS[k]

						return parameters





