
"""simple docstring"""


import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
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    __lowerCamelCase     (   lowerCAmelCase	,  lowerCAmelCase	,  unittest.TestCase ):
				a__:			List[str]									=						IFSuperResolutionPipeline
				a__:			Union[str, Any]									=						TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'width', 'height'}
				a__:			List[Any]									=						TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
				a__:			str									=						PipelineTesterMixin.required_optional_params - {'latents'}




				def 					UpperCAmelCase__	(							self      ):
							return self._get_superresolution_dummy_components()




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase=0      ):
							if str(UpperCAmelCase      ).startswith('''mps'''      ):
										lowerCamelCase_ 		=     torch.manual_seed(UpperCAmelCase      )
							else:
										lowerCamelCase_ 		=     torch.Generator(device=UpperCAmelCase      ).manual_seed(UpperCAmelCase      )

							lowerCamelCase_ 		=     floats_tensor((1, 3, 32, 32)       ,	rng=random.Random(UpperCAmelCase      )      ).to(UpperCAmelCase      )

							lowerCamelCase_ 		=     {
							    '''prompt''': '''A painting of a squirrel eating a burger''',
							    '''image''': 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      ):
							self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3      )




				def 					UpperCAmelCase__	(							self      ):
							self._test_save_load_optional_components()




				@unittest.skipIf(torch_device != '''cuda'''       ,	reason='''float16 requires CUDA'''      )
				def 					UpperCAmelCase__	(							self      ):
							# 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 					UpperCAmelCase__	(							self      ):
							self._test_attention_slicing_forward_pass(expected_max_diff=1e-2      )




				def 					UpperCAmelCase__	(							self      ):
							self._test_save_load_local()





				def 					UpperCAmelCase__	(							self      ):
							self._test_inference_batch_single_identical(
							    expected_max_diff=1e-2       ,	)





"""simple docstring"""


import copy

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging


A_						       =				logging.get_logger(__name__)




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			List[str]									=						'hybrid-clip'
				a__:			List[Any]									=						True




				def __init__(							self       ,	UpperCAmelCase=512       ,	**UpperCAmelCase      ):
							super().__init__(**UpperCAmelCase      )

							if "text_config" not in kwargs:
										raise ValueError('''`text_config` can not be `None`.'''      )

							if "vision_config" not in kwargs:
										raise ValueError('''`vision_config` can not be `None`.'''      )

							lowerCamelCase_ 		=     kwargs.pop('''text_config'''      )
							lowerCamelCase_ 		=     kwargs.pop('''vision_config'''      )

							lowerCamelCase_ 		=     text_config.pop('''model_type'''      )
							lowerCamelCase_ 		=     vision_config.pop('''model_type'''      )

							from transformers import AutoConfig

							lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      )

							if vision_model_type == "clip":
										lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      ).vision_config
							elif vision_model_type == "clip_vision_model":
										from transformers import CLIPVisionConfig

										lowerCamelCase_ 		=     CLIPVisionConfig(**UpperCAmelCase      )
							else:
										lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      )

							lowerCamelCase_ 		=     projection_dim
							lowerCamelCase_ 		=     1.0




				@classmethod
				def 					UpperCAmelCase__	(							cls       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	**UpperCAmelCase      ):
							return cls(text_config=text_config.to_dict()       ,	vision_config=vision_config.to_dict()       ,	**UpperCAmelCase      )





				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     copy.deepcopy(self.__dict__      )
							lowerCamelCase_ 		=     self.text_config.to_dict()
							lowerCamelCase_ 		=     self.vision_config.to_dict()
							lowerCamelCase_ 		=     self.__class__.model_type
							return output



