




"""simple docstring"""


import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
     import torch






class       lowercase(  __a				):







 '''simple docstring'''





 lowercase__				      =      "char"
 lowercase__				      =      "bpe"
 lowercase__				      =      "wp"







A_      			=       (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)






class       lowercase(  __a				):







 '''simple docstring'''





 lowercase__				      =      ["image_processor", "char_tokenizer"]
 lowercase__				      =      "ViTImageProcessor"
 lowercase__				      =      "MgpstrTokenizer"


 def __init__(       self:	Tuple,   a_:	Any=None,   a_:	Tuple=None,   **a_:	Dict	):




   '''simple docstring'''



   _snake_case							:	List[str]			=						None
   if "feature_extractor" in kwargs:
     warnings.warn(
         """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
         """ instead.""",   a_,   )
     _snake_case							:	List[str]			=						kwargs.pop("""feature_extractor"""	)

   _snake_case							:	int			=						image_processor if image_processor is not None else feature_extractor
   if image_processor is None:
     raise ValueError("""You need to specify an `image_processor`."""	)
   if tokenizer is None:
     raise ValueError("""You need to specify a `tokenizer`."""	)

   _snake_case							:	List[str]			=						tokenizer
   _snake_case							:	Tuple			=						AutoTokenizer.from_pretrained("""gpt2"""	)
   _snake_case							:	List[Any]			=						AutoTokenizer.from_pretrained("""bert-base-uncased"""	)

   super().__init__(a_,   a_	)


 def __call__(       self:	Any,   a_:	Any=None,   a_:	List[Any]=None,   a_:	Any=None,   **a_:	Tuple	):




   '''simple docstring'''



   if images is None and text is None:
     raise ValueError("""You need to specify either an `images` or `text` input to process."""	)

   if images is not None:
     _snake_case							:	int			=						self.image_processor(a_,   return_tensors=a_,   **a_	)
   if text is not None:
     _snake_case							:	Union[str, Any]			=						self.char_tokenizer(a_,   return_tensors=a_,   **a_	)

   if text is None:
     return inputs
   elif images is None:
     return encodings
   else:
     _snake_case							:	Optional[int]			=						encodings["""input_ids"""]
     return inputs


 def 				UpperCamelCase_     (       self:	str,   a_:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case	,      _snake_case	,      _snake_case							:	Union[str, Any]			=						sequences
   _snake_case							:	List[Any]			=						char_preds.size(0	)

   _snake_case	,      _snake_case							:	Optional[int]			=						self._decode_helper(a_,   """char"""	)
   _snake_case	,      _snake_case							:	Union[str, Any]			=						self._decode_helper(a_,   """bpe"""	)
   _snake_case	,      _snake_case							:	List[str]			=						self._decode_helper(a_,   """wp"""	)

   _snake_case							:	List[str]			=						[]
   _snake_case							:	List[Any]			=						[]
   for i in range(a_	):
     _snake_case							:	Any			=						[char_scores[i], bpe_scores[i], wp_scores[i]]
     _snake_case							:	str			=						[char_strs[i], bpe_strs[i], wp_strs[i]]
     _snake_case							:	Union[str, Any]			=						scores.index(max(a_	)	)
     final_strs.append(strs[max_score_index]	)
     final_scores.append(scores[max_score_index]	)

   _snake_case							:	Tuple			=						{}
   _snake_case							:	List[str]			=						final_strs
   _snake_case							:	Optional[int]			=						final_scores
   _snake_case							:	Optional[int]			=						char_strs
   _snake_case							:	Optional[Any]			=						bpe_strs
   _snake_case							:	Dict			=						wp_strs
   return out


 def 				UpperCamelCase_     (       self:	Any,   a_:	List[Any],   a_:	Any	):




   '''simple docstring'''



   if format == DecodeType.CHARACTER:
     _snake_case							:	int			=						self.char_decode
     _snake_case							:	Optional[int]			=						1
     _snake_case							:	str			=						"""[s]"""
   elif format == DecodeType.BPE:
     _snake_case							:	Dict			=						self.bpe_decode
     _snake_case							:	Any			=						2
     _snake_case							:	str			=						"""#"""
   elif format == DecodeType.WORDPIECE:
     _snake_case							:	Optional[int]			=						self.wp_decode
     _snake_case							:	Optional[int]			=						102
     _snake_case							:	List[str]			=						"""[SEP]"""
   else:
     raise ValueError(f"Format {format} is not supported."	)

   _snake_case	,      _snake_case							:	List[str]			=						[], []
   _snake_case							:	str			=						pred_logits.size(0	)
   _snake_case							:	List[str]			=						pred_logits.size(1	)
   _snake_case	,      _snake_case							:	List[Any]			=						pred_logits.topk(1,   dim=-1,   largest=a_,   sorted=a_	)
   _snake_case							:	Optional[int]			=						preds_index.view(-1,   a_	)[:, 1:]
   _snake_case							:	Tuple			=						decoder(a_	)
   _snake_case	,      _snake_case							:	Any			=						torch.nn.functional.softmax(a_,   dim=2	).max(dim=2	)
   _snake_case							:	Dict			=						preds_max_prob[:, 1:]

   for index in range(a_	):
     _snake_case							:	Optional[int]			=						preds_str[index].find(a_	)
     _snake_case							:	Dict			=						preds_str[index][:pred_eos]
     _snake_case							:	List[Any]			=						preds_index[index].cpu().tolist()
     _snake_case							:	Tuple			=						pred_index.index(a_	) if eos_token in pred_index else -1
     _snake_case							:	str			=						preds_max_prob[index][: pred_eos_index + 1]
     _snake_case							:	Optional[int]			=						pred_max_prob.cumprod(dim=0	)[-1] if pred_max_prob.nelement() != 0 else 0.0
     dec_strs.append(a_	)
     conf_scores.append(a_	)

   return dec_strs, conf_scores


 def 				UpperCamelCase_     (       self:	Union[str, Any],   a_:	List[Any]	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						[seq.replace(""" """,   """"""	) for seq in self.char_tokenizer.batch_decode(a_	)]
   return decode_strs


 def 				UpperCamelCase_     (       self:	Optional[Any],   a_:	Dict	):




   '''simple docstring'''



   return self.bpe_tokenizer.batch_decode(a_	)


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	str			=						[seq.replace(""" """,   """"""	) for seq in self.wp_tokenizer.batch_decode(a_	)]
   return decode_strs








"""simple docstring"""


import copy
import os
from typing import Union

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


A_      			=       logging.get_logger(__name__)

A_      			=       {
    '''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       lowercase(  __a				):







 '''simple docstring'''





 lowercase__				      =      "bridgetower_vision_model"


 def __init__(       self:	Tuple,   a_:	str=768,   a_:	Union[str, Any]=12,   a_:	List[str]=3,   a_:	Optional[int]=16,   a_:	List[Any]=288,   a_:	Optional[Any]=1,   a_:	Any=1E-05,   a_:	Dict=False,   a_:	Any=True,   a_:	int=False,   **a_:	int,   ):




   '''simple docstring'''



   super().__init__(**a_	)
   _snake_case							:	str			=						hidden_size
   _snake_case							:	int			=						num_hidden_layers
   _snake_case							:	Any			=						num_channels
   _snake_case							:	Union[str, Any]			=						patch_size
   _snake_case							:	Dict			=						image_size
   _snake_case							:	Optional[Any]			=						initializer_factor
   _snake_case							:	Any			=						layer_norm_eps
   _snake_case							:	int			=						stop_gradient
   _snake_case							:	Any			=						share_layernorm
   _snake_case							:	List[Any]			=						remove_last_layer


 @classmethod
 def 				UpperCamelCase_     (       cls:	Union[str, Any],   a_:	Union[str, os.PathLike],   **a_:	Optional[Any]	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	List[Any]			=						cls.get_config_dict(a_,   **a_	)

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







class       lowercase(  __a				):







 '''simple docstring'''





 lowercase__				      =      "bridgetower_text_model"


 def __init__(       self:	str,   a_:	Dict=50_265,   a_:	List[Any]=768,   a_:	Union[str, Any]=12,   a_:	List[str]=12,   a_:	str=1,   a_:	Optional[Any]=3_072,   a_:	int="gelu",   a_:	int=0.1,   a_:	int=0.1,   a_:	Optional[int]=514,   a_:	Tuple=1,   a_:	Tuple=1E-05,   a_:	Optional[int]=1,   a_:	Union[str, Any]=0,   a_:	str=2,   a_:	Any="absolute",   a_:	List[Any]=True,   **a_:	Union[str, Any],   ):




   '''simple docstring'''



   super().__init__(**a_	)

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


 @classmethod
 def 				UpperCamelCase_     (       cls:	str,   a_:	Union[str, os.PathLike],   **a_:	int	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	Optional[int]			=						cls.get_config_dict(a_,   **a_	)

   if config_dict.get("""model_type"""	) == "bridgetower":
     _snake_case							:	Union[str, Any]			=						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       lowercase(  __a				):







 '''simple docstring'''





 lowercase__				      =      "bridgetower"


 def __init__(       self:	int,   a_:	List[str]=True,   a_:	Any="gelu",   a_:	List[Any]=768,   a_:	int=1,   a_:	Optional[int]=1E-05,   a_:	Tuple=False,   a_:	Optional[Any]="add",   a_:	List[str]=12,   a_:	Union[str, Any]=6,   a_:	int=False,   a_:	Any=False,   a_:	Dict=None,   a_:	Any=None,   **a_:	str,   ):




   '''simple docstring'''



   _snake_case							:	str			=						kwargs.pop("""text_config_dict""",   a_	)
   _snake_case							:	Optional[Any]			=						kwargs.pop("""vision_config_dict""",   a_	)

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

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

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

   _snake_case							:	Any			=						BridgeTowerTextConfig(**a_	)
   _snake_case							:	List[Any]			=						BridgeTowerVisionConfig(**a_	)


 @classmethod
 def 				UpperCamelCase_     (       cls:	Union[str, Any],   a_:	BridgeTowerTextConfig,   a_:	BridgeTowerVisionConfig,   **a_:	Optional[Any]	):




   '''simple docstring'''



   return cls(text_config=text_config.to_dict(),   vision_config=vision_config.to_dict(),   **a_	)


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						copy.deepcopy(self.__dict__	)
   _snake_case							:	str			=						self.text_config.to_dict()
   _snake_case							:	List[str]			=						self.vision_config.to_dict()
   _snake_case							:	Tuple			=						self.__class__.model_type
   return output


