






'''simple docstring'''







import copy
import os
from typing import Union

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


__lowerCAmelCase       		=      logging.get_logger(__name__)

__lowerCAmelCase       		=      {
    'BridgeTower/bridgetower-base': 'https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json',
    'BridgeTower/bridgetower-base-itm-mlm': (
        'https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json'
    ),
}





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				"bridgetower_vision_model"






			def __init__( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=768  ,						__SCREAMING_SNAKE_CASE      :			Tuple=12  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=3  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=16  ,						__SCREAMING_SNAKE_CASE      :			List[str]=288  ,						__SCREAMING_SNAKE_CASE      :			Dict=1  ,						__SCREAMING_SNAKE_CASE      :			Tuple=1e-05  ,						__SCREAMING_SNAKE_CASE      :			List[str]=False  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=True  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=False  ,						**__SCREAMING_SNAKE_CASE      :			List[Any]  ,						)			->			int:
							super().__init__(**__SCREAMING_SNAKE_CASE					)
							a_		:  str											=      hidden_size
							a_		:  int											=      num_hidden_layers
							a_		:  Optional[Any]											=      num_channels
							a_		:  List[Any]											=      patch_size
							a_		:  int											=      image_size
							a_		:  Optional[Any]											=      initializer_factor
							a_		:  Optional[int]											=      layer_norm_eps
							a_		:  Optional[int]											=      stop_gradient
							a_		:  Optional[int]											=      share_layernorm
							a_		:  int											=      remove_last_layer







			@classmethod
			def 			SCREAMING_SNAKE_CASE      ( cls      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, os.PathLike]  ,						**__SCREAMING_SNAKE_CASE      :			str					)			->			"PretrainedConfig":
							a_		:  Tuple											=      cls.get_config_dict(__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)

							if config_dict.get('''model_type'''					) == "bridgetower":
											a_		:  str											=      config_dict['''text_config''']

							if "model_type" in config_dict and hasattr(cls  ,						'''model_type'''					) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'					)

							return cls.from_dict(__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				"bridgetower_text_model"






			def __init__( self      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Tuple=5_0265  ,						__SCREAMING_SNAKE_CASE      :			Any=768  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=12  ,						__SCREAMING_SNAKE_CASE      :			int=12  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=1  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=3072  ,						__SCREAMING_SNAKE_CASE      :			List[Any]="gelu"  ,						__SCREAMING_SNAKE_CASE      :			List[str]=0.1  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=0.1  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=514  ,						__SCREAMING_SNAKE_CASE      :			str=1  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=1e-05  ,						__SCREAMING_SNAKE_CASE      :			Tuple=1  ,						__SCREAMING_SNAKE_CASE      :			Dict=0  ,						__SCREAMING_SNAKE_CASE      :			Tuple=2  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]="absolute"  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=True  ,						**__SCREAMING_SNAKE_CASE      :			str  ,						)			->			Optional[Any]:
							super().__init__(**__SCREAMING_SNAKE_CASE					)

							a_		:  Union[str, Any]											=      vocab_size
							a_		:  List[Any]											=      hidden_size
							a_		:  int											=      num_hidden_layers
							a_		:  Dict											=      num_attention_heads
							a_		:  Union[str, Any]											=      hidden_act
							a_		:  Union[str, Any]											=      initializer_factor
							a_		:  Tuple											=      intermediate_size
							a_		:  Tuple											=      hidden_dropout_prob
							a_		:  Optional[int]											=      attention_probs_dropout_prob
							a_		:  int											=      max_position_embeddings
							a_		:  int											=      type_vocab_size
							a_		:  Union[str, Any]											=      layer_norm_eps
							a_		:  Optional[int]											=      position_embedding_type
							a_		:  Union[str, Any]											=      use_cache
							a_		:  Optional[int]											=      pad_token_id
							a_		:  Optional[Any]											=      bos_token_id
							a_		:  int											=      eos_token_id







			@classmethod
			def 			SCREAMING_SNAKE_CASE      ( cls      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, os.PathLike]  ,						**__SCREAMING_SNAKE_CASE      :			List[Any]					)			->			"PretrainedConfig":
							a_		:  Optional[int]											=      cls.get_config_dict(__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)

							if config_dict.get('''model_type'''					) == "bridgetower":
											a_		:  Dict											=      config_dict['''text_config''']

							if "model_type" in config_dict and hasattr(cls  ,						'''model_type'''					) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'					)

							return cls.from_dict(__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				"bridgetower"






			def __init__( self      :			int  ,						__SCREAMING_SNAKE_CASE      :			Tuple=True  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]="gelu"  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=768  ,						__SCREAMING_SNAKE_CASE      :			int=1  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=1e-05  ,						__SCREAMING_SNAKE_CASE      :			int=False  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]="add"  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=12  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=6  ,						__SCREAMING_SNAKE_CASE      :			Tuple=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=None  ,						__SCREAMING_SNAKE_CASE      :			Tuple=None  ,						**__SCREAMING_SNAKE_CASE      :			int  ,						)			->			Dict:
							# TODO: remove this once the Hub files are updated.
							a_		:  List[Any]											=      kwargs.pop('''text_config_dict'''  ,						__SCREAMING_SNAKE_CASE					)
							a_		:  Dict											=      kwargs.pop('''vision_config_dict'''  ,						__SCREAMING_SNAKE_CASE					)

							super().__init__(**__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      share_cross_modal_transformer_layers
							a_		:  Optional[int]											=      hidden_act
							a_		:  Optional[int]											=      hidden_size
							a_		:  Tuple											=      initializer_factor
							a_		:  Dict											=      layer_norm_eps
							a_		:  str											=      share_link_tower_layers
							a_		:  List[Any]											=      link_tower_type
							a_		:  Dict											=      num_attention_heads
							a_		:  List[str]											=      num_hidden_layers
							a_		:  Optional[Any]											=      tie_word_embeddings
							a_		:  Tuple											=      init_layernorm_from_vision_encoder

							if text_config is None:
											a_		:  Any											=      {}
											logger.info('''`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values.'''					)

							if vision_config is None:
											a_		:  Optional[Any]											=      {}
											logger.info('''`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values.'''					)

							a_		:  int											=      BridgeTowerTextConfig(**__SCREAMING_SNAKE_CASE					)
							a_		:  int											=      BridgeTowerVisionConfig(**__SCREAMING_SNAKE_CASE					)






			@classmethod
			def 			SCREAMING_SNAKE_CASE      ( cls      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			BridgeTowerTextConfig  ,						__SCREAMING_SNAKE_CASE      :			BridgeTowerVisionConfig  ,						**__SCREAMING_SNAKE_CASE      :			Optional[Any]					)			->			int:
							return cls(text_config=text_config.to_dict()  ,						vision_config=vision_config.to_dict()  ,						**__SCREAMING_SNAKE_CASE					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			str:
							a_		:  Optional[Any]											=      copy.deepcopy(self.__dict__					)
							a_		:  List[str]											=      self.text_config.to_dict()
							a_		:  Optional[int]											=      self.vision_config.to_dict()
							a_		:  Optional[int]											=      self.__class__.model_type
							return output












'''simple docstring'''







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 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
			snake_case__  =				IFInpaintingSuperResolutionPipeline
			snake_case__  =				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"}
			snake_case__  =				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"}       )
			snake_case__  =				PipelineTesterMixin.required_optional_params - {"latents"}






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			List[str]:
							return self._get_superresolution_dummy_components()






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Dict=0					)			->			List[Any]:
							if str(__SCREAMING_SNAKE_CASE					).startswith('''mps'''					):
											a_		:  Optional[int]											=      torch.manual_seed(__SCREAMING_SNAKE_CASE					)
							else:
											a_		:  str											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(__SCREAMING_SNAKE_CASE					)

							a_		:  Dict											=      floats_tensor((1, 3, 16, 16)  ,						rng=random.Random(__SCREAMING_SNAKE_CASE					)					).to(__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      floats_tensor((1, 3, 32, 32)  ,						rng=random.Random(__SCREAMING_SNAKE_CASE					)					).to(__SCREAMING_SNAKE_CASE					)
							a_		:  Tuple											=      floats_tensor((1, 3, 32, 32)  ,						rng=random.Random(__SCREAMING_SNAKE_CASE					)					).to(__SCREAMING_SNAKE_CASE					)

							a_		:  Union[str, 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 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			List[str]:
							self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			int:
							self._test_save_load_optional_components()






			@unittest.skipIf(torch_device != '''cuda'''  ,						reason='''float16 requires CUDA'''					)
			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Tuple:
							# 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 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Optional[int]:
							self._test_attention_slicing_forward_pass(expected_max_diff=1e-2					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Optional[Any]:
							self._test_save_load_local()







			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Optional[Any]:
							self._test_inference_batch_single_identical(
							    expected_max_diff=1e-2  ,						)




