



import copy
import os
from typing import Union

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


UpperCAmelCase							 =					logging.get_logger(__name__)

UpperCAmelCase							 =					{
    """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  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Tuple         =					"""bridgetower_vision_model"""


							def __init__(							self  ,  a_=768  ,  a_=12  ,  a_=3  ,  a_=16  ,  a_=288  ,  a_=1  ,  a_=1E-0_5  ,  a_=False  ,  a_=True  ,  a_=False  ,  **a_  ,  ):
											super().__init__(**a_		)
											a__				      =		hidden_size
											a__				      =		num_hidden_layers
											a__				      =		num_channels
											a__				      =		patch_size
											a__				      =		image_size
											a__				      =		initializer_factor
											a__				      =		layer_norm_eps
											a__				      =		stop_gradient
											a__				      =		share_layernorm
											a__				      =		remove_last_layer







							@classmethod
							def _a      (							cls  ,  a_  ,  **a_		):
											a__      ,			a__				      =		cls.get_config_dict(a_  ,  **a_		)

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




class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Dict         =					"""bridgetower_text_model"""


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

											a__				      =		vocab_size
											a__				      =		hidden_size
											a__				      =		num_hidden_layers
											a__				      =		num_attention_heads
											a__				      =		hidden_act
											a__				      =		initializer_factor
											a__				      =		intermediate_size
											a__				      =		hidden_dropout_prob
											a__				      =		attention_probs_dropout_prob
											a__				      =		max_position_embeddings
											a__				      =		type_vocab_size
											a__				      =		layer_norm_eps
											a__				      =		position_embedding_type
											a__				      =		use_cache
											a__				      =		pad_token_id
											a__				      =		bos_token_id
											a__				      =		eos_token_id







							@classmethod
							def _a      (							cls  ,  a_  ,  **a_		):
											a__      ,			a__				      =		cls.get_config_dict(a_  ,  **a_		)

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




class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Any         =					"""bridgetower"""


							def __init__(							self  ,  a_=True  ,  a_="gelu"  ,  a_=768  ,  a_=1  ,  a_=1E-0_5  ,  a_=False  ,  a_="add"  ,  a_=12  ,  a_=6  ,  a_=False  ,  a_=False  ,  a_=None  ,  a_=None  ,  **a_  ,  ):
											# TODO: remove this once the Hub files are updated.
											a__				      =		kwargs.pop("""text_config_dict"""  ,  a_		)
											a__				      =		kwargs.pop("""vision_config_dict"""  ,  a_		)

											super().__init__(**a_		)
											a__				      =		share_cross_modal_transformer_layers
											a__				      =		hidden_act
											a__				      =		hidden_size
											a__				      =		initializer_factor
											a__				      =		layer_norm_eps
											a__				      =		share_link_tower_layers
											a__				      =		link_tower_type
											a__				      =		num_attention_heads
											a__				      =		num_hidden_layers
											a__				      =		tie_word_embeddings
											a__				      =		init_layernorm_from_vision_encoder

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

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

											a__				      =		BridgeTowerTextConfig(**a_		)
											a__				      =		BridgeTowerVisionConfig(**a_		)


							@classmethod
							def _a      (							cls  ,  a_  ,  a_  ,  **a_		):
											return cls(text_config=text_config.to_dict()  ,  vision_config=vision_config.to_dict()  ,  **a_		)







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






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase							 =					logging.get_logger(__name__)

UpperCAmelCase							 =					{"""vocab_file""": """spiece.model"""}

UpperCAmelCase							 =					{
    """vocab_file""": {
        """bert_for_seq_generation""": (
            """https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model"""
        ),
    }
}

UpperCAmelCase							 =					{"""bert_for_seq_generation""": 512}

class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Optional[Any]         =					VOCAB_FILES_NAMES
							UpperCamelCase__				:      Any         =					PRETRAINED_VOCAB_FILES_MAP
							UpperCamelCase__				:      List[str]         =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							UpperCamelCase__				:      List[int]         =					[]
							UpperCamelCase__				:      Optional[int]         =					["""input_ids""", """attention_mask"""]


							def __init__(							self  ,  a_  ,  a_="<s>"  ,  a_="</s>"  ,  a_="<unk>"  ,  a_="<pad>"  ,  a_="<::::>"  ,  a_ = None  ,  **a_  ,  ):
											a__				      =		{} if sp_model_kwargs is None else sp_model_kwargs

											# Add extra_ids to the special token list
											super().__init__(
											    bos_token=a_  ,  eos_token=a_  ,  unk_token=a_  ,  pad_token=a_  ,  sep_token=a_  ,  sp_model_kwargs=self.sp_model_kwargs  ,  **a_  ,  )

											a__				      =		vocab_file

											a__				      =		spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(a_		)


							@property
							def _a      (							self		):
											return self.sp_model.get_piece_size()


							def _a      (							self		):
											a__				      =		{self.convert_ids_to_tokens(a_		): i for i in range(self.vocab_size		)}
											vocab.update(self.added_tokens_encoder		)
											return vocab


							def __getstate__(							self		):
											a__				      =		self.__dict__.copy()
											a__				      =		None
											return state


							def __setstate__(							self  ,  a_		):
											a__				      =		d

											# for backward compatibility
											if not hasattr(self  ,  """sp_model_kwargs"""		):
															a__				      =		{}

											a__				      =		spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(self.vocab_file		)


							def _a      (							self  ,  a_		):
											return self.sp_model.encode(a_  ,  out_type=a_		)


							def _a      (							self  ,  a_		):
											return self.sp_model.piece_to_id(a_		)


							def _a      (							self  ,  a_		):
											a__				      =		self.sp_model.IdToPiece(a_		)
											return token


							def _a      (							self  ,  a_		):
											a__				      =		[]
											a__				      =		""""""
											for token in tokens:
															# make sure that special tokens are not decoded using sentencepiece model
															if token in self.all_special_tokens:
																			out_string += self.sp_model.decode(a_		) + token
																			a__				      =		[]
															else:
																			current_sub_tokens.append(a_		)
											out_string += self.sp_model.decode(a_		)
											return out_string.strip()







							def _a      (							self  ,  a_  ,  a_ = None		):
											if not os.path.isdir(a_		):
															logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''		)
															return
											a__				      =		os.path.join(
											    a_  ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]		)

											if os.path.abspath(self.vocab_file		) != os.path.abspath(a_		) and os.path.isfile(self.vocab_file		):
															copyfile(self.vocab_file  ,  a_		)
											elif not os.path.isfile(self.vocab_file		):
															with open(a_  ,  """wb"""		) as fi:
																			a__				      =		self.sp_model.serialized_model_proto()
																			fi.write(a_		)

											return (out_vocab_file,)

