import copy
import os
from typing import Union

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


SCREAMING_SNAKE_CASE		:     Dict         =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	{
    "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        UpperCamelCase						(				__a				):
						a__		:Any      =			'''bridgetower_vision_model'''





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





						@classmethod
						def        A_							(cls			,     __UpperCamelCase			,     **__UpperCamelCase						)				->				"PretrainedConfig":
												UpperCamelCase_,UpperCamelCase_		:							List[Any]  =		cls.get_config_dict(__UpperCamelCase			,     **__UpperCamelCase						)

												if config_dict.get("""model_type"""						) == "bridgetower":
																		UpperCamelCase_		:							Tuple  =		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(__UpperCamelCase			,     **__UpperCamelCase						)


class        UpperCamelCase						(				__a				):
						a__		:Dict      =			'''bridgetower_text_model'''





						def __init__(self			,     __UpperCamelCase=50_265			,     __UpperCamelCase=768			,     __UpperCamelCase=12			,     __UpperCamelCase=12			,     __UpperCamelCase=1			,     __UpperCamelCase=3_072			,     __UpperCamelCase="gelu"			,     __UpperCamelCase=0.1			,     __UpperCamelCase=0.1			,     __UpperCamelCase=514			,     __UpperCamelCase=1			,     __UpperCamelCase=1E-0_5			,     __UpperCamelCase=1			,     __UpperCamelCase=0			,     __UpperCamelCase=2			,     __UpperCamelCase="absolute"			,     __UpperCamelCase=True			,     **__UpperCamelCase			,     )				->				Optional[Any]:
												super().__init__(**__UpperCamelCase						)

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





						@classmethod
						def        A_							(cls			,     __UpperCamelCase			,     **__UpperCamelCase						)				->				"PretrainedConfig":
												UpperCamelCase_,UpperCamelCase_		:							List[Any]  =		cls.get_config_dict(__UpperCamelCase			,     **__UpperCamelCase						)

												if config_dict.get("""model_type"""						) == "bridgetower":
																		UpperCamelCase_		:							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(__UpperCamelCase			,     **__UpperCamelCase						)


class        UpperCamelCase						(				__a				):
						a__		:Tuple      =			'''bridgetower'''





						def __init__(self			,     __UpperCamelCase=True			,     __UpperCamelCase="gelu"			,     __UpperCamelCase=768			,     __UpperCamelCase=1			,     __UpperCamelCase=1E-0_5			,     __UpperCamelCase=False			,     __UpperCamelCase="add"			,     __UpperCamelCase=12			,     __UpperCamelCase=6			,     __UpperCamelCase=False			,     __UpperCamelCase=False			,     __UpperCamelCase=None			,     __UpperCamelCase=None			,     **__UpperCamelCase			,     )				->				Tuple:
												# TODO: remove this once the Hub files are updated.
												UpperCamelCase_		:							int  =		kwargs.pop("""text_config_dict"""			,     __UpperCamelCase						)
												UpperCamelCase_		:							Optional[Any]  =		kwargs.pop("""vision_config_dict"""			,     __UpperCamelCase						)

												super().__init__(**__UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		share_cross_modal_transformer_layers
												UpperCamelCase_		:							str  =		hidden_act
												UpperCamelCase_		:							Dict  =		hidden_size
												UpperCamelCase_		:							str  =		initializer_factor
												UpperCamelCase_		:							List[str]  =		layer_norm_eps
												UpperCamelCase_		:							Tuple  =		share_link_tower_layers
												UpperCamelCase_		:							Tuple  =		link_tower_type
												UpperCamelCase_		:							Dict  =		num_attention_heads
												UpperCamelCase_		:							str  =		num_hidden_layers
												UpperCamelCase_		:							Tuple  =		tie_word_embeddings
												UpperCamelCase_		:							Any  =		init_layernorm_from_vision_encoder

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

												if vision_config is None:
																		UpperCamelCase_		:							Tuple  =		{}
																		logger.info("""`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values."""						)

												UpperCamelCase_		:							Dict  =		BridgeTowerTextConfig(**__UpperCamelCase						)
												UpperCamelCase_		:							Dict  =		BridgeTowerVisionConfig(**__UpperCamelCase						)





						@classmethod
						def        A_							(cls			,     __UpperCamelCase			,     __UpperCamelCase			,     **__UpperCamelCase						)				->				List[Any]:
												return cls(text_config=text_config.to_dict()			,     vision_config=vision_config.to_dict()			,     **__UpperCamelCase						)





						def        A_							(self						)				->				List[Any]:
												UpperCamelCase_		:							Dict  =		copy.deepcopy(self.__dict__						)
												UpperCamelCase_		:							int  =		self.text_config.to_dict()
												UpperCamelCase_		:							Union[str, Any]  =		self.vision_config.to_dict()
												UpperCamelCase_		:							Tuple  =		self.__class__.model_type
												return output







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
	from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


SCREAMING_SNAKE_CASE		:     List[str]         =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE		:     str         =	{
    "bigscience/bloom": "https://huggingface.co/bigscience/bloom/resolve/main/config.json",
    "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/config.json",
    "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json",
    "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json",
    "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/config.json",
    "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json",
}

class        UpperCamelCase						(				__a				):
						a__		:Any      =			'''bloom'''
						a__		:Optional[int]      =			['''past_key_values''']
						a__		:Tuple      =			{
						    '''num_hidden_layers''': '''n_layer''',
						    '''num_attention_heads''': '''n_head''',
						}





						def __init__(self			,     __UpperCamelCase=250_880			,     __UpperCamelCase=64			,     __UpperCamelCase=2			,     __UpperCamelCase=8			,     __UpperCamelCase=1E-5			,     __UpperCamelCase=0.02			,     __UpperCamelCase=True			,     __UpperCamelCase=1			,     __UpperCamelCase=2			,     __UpperCamelCase=False			,     __UpperCamelCase=0.0			,     __UpperCamelCase=0.0			,     __UpperCamelCase=1			,     __UpperCamelCase=False			,     **__UpperCamelCase			,     )				->				List[Any]:
												UpperCamelCase_		:							Dict  =		vocab_size
												# Backward compatibility with n_embed kwarg
												UpperCamelCase_		:							Optional[Any]  =		kwargs.pop("""n_embed"""			,     __UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		hidden_size if n_embed is None else n_embed
												UpperCamelCase_		:							List[Any]  =		n_layer
												UpperCamelCase_		:							Optional[Any]  =		n_head
												UpperCamelCase_		:							List[str]  =		layer_norm_epsilon
												UpperCamelCase_		:							int  =		initializer_range
												UpperCamelCase_		:							List[str]  =		use_cache
												UpperCamelCase_		:							List[Any]  =		pretraining_tp
												UpperCamelCase_		:							Optional[int]  =		apply_residual_connection_post_layernorm
												UpperCamelCase_		:							Any  =		hidden_dropout
												UpperCamelCase_		:							Optional[Any]  =		attention_dropout

												UpperCamelCase_		:							List[str]  =		bos_token_id
												UpperCamelCase_		:							Optional[Any]  =		eos_token_id
												UpperCamelCase_		:							str  =		slow_but_exact

												super().__init__(bos_token_id=__UpperCamelCase			,     eos_token_id=__UpperCamelCase			,     **__UpperCamelCase						)


class        UpperCamelCase						(				__a				):
						a__		:Dict      =			version.parse('''1.12'''				)





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase = "default"			,     __UpperCamelCase = None			,     __UpperCamelCase = False			,     )				->				List[str]:
												super().__init__(__UpperCamelCase			,     task=__UpperCamelCase			,     patching_specs=__UpperCamelCase			,     use_past=__UpperCamelCase						)
												if not getattr(self._config			,     """pad_token_id"""			,     __UpperCamelCase						):
																		# TODO: how to do that better?
																		UpperCamelCase_		:							Optional[Any]  =		0





						@property
						def        A_							(self						)				->				Mapping[str, Mapping[int, str]]:
												UpperCamelCase_		:							str  =		OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}						)
												if self.use_past:
																		# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
																		self.fill_with_past_key_values_(__UpperCamelCase			,     direction="""inputs"""			,     inverted_values_shape=__UpperCamelCase						)
																		UpperCamelCase_		:							int  =		{0: """batch""", 1: """past_sequence + sequence"""}
												else:
																		UpperCamelCase_		:							Dict  =		{0: """batch""", 1: """sequence"""}

												return common_inputs





						@property
						def        A_							(self						)				->				int:
												return self._config.n_layer





						@property
						def        A_							(self						)				->				int:
												return self._config.n_head





						@property
						def        A_							(self						)				->				float:
												return 1E-3





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = -1			,     __UpperCamelCase = -1			,     __UpperCamelCase = False			,     __UpperCamelCase = None			,     )				->				Mapping[str, Any]:
												UpperCamelCase_		:							Union[str, Any]  =		super(__UpperCamelCase			,     self						).generate_dummy_inputs(
												    __UpperCamelCase			,     batch_size=__UpperCamelCase			,     seq_length=__UpperCamelCase			,     is_pair=__UpperCamelCase			,     framework=__UpperCamelCase						)

												# We need to order the input in the way they appears in the forward()
												UpperCamelCase_		:							List[Any]  =		OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}						)

												# Need to add the past_keys
												if self.use_past:
																		if not is_torch_available():
																								raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""						)
																		else:
																								import torch

																								UpperCamelCase_,UpperCamelCase_		:							List[str]  =		common_inputs["""input_ids"""].shape
																								# Not using the same length for past_key_values
																								UpperCamelCase_		:							List[str]  =		seqlen + 2
																								UpperCamelCase_		:							Tuple  =		self._config.hidden_size // self.num_attention_heads
																								UpperCamelCase_		:							List[Any]  =		(
																								    batch * self.num_attention_heads,
																								    head_dim,
																								    past_key_values_length,
																								)
																								UpperCamelCase_		:							str  =		(
																								    batch * self.num_attention_heads,
																								    past_key_values_length,
																								    head_dim,
																								)
																								UpperCamelCase_		:							Any  =		[
																								    (torch.zeros(__UpperCamelCase						), torch.zeros(__UpperCamelCase						)) for _ in range(self.num_layers						)
																								]

												UpperCamelCase_		:							int  =		common_inputs["""attention_mask"""]
												if self.use_past:
																		UpperCamelCase_		:							List[str]  =		ordered_inputs["""attention_mask"""].dtype
																		UpperCamelCase_		:							Any  =		torch.cat(
																		    [ordered_inputs["""attention_mask"""], torch.ones(__UpperCamelCase			,     __UpperCamelCase			,     dtype=__UpperCamelCase						)]			,     dim=1						)

												return ordered_inputs





						@property
						def        A_							(self						)				->				int:
												return 13






