


'''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 							__UpperCamelCase				(		lowerCAmelCase_					):
	A_				=    "char"
	A_				=    "bpe"
	A_				=    "wp"



__lowercase	:						Dict       =	(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)





class 							__UpperCamelCase				(		lowerCAmelCase_					):
	A_				=    ["image_processor", "char_tokenizer"]
	A_				=    "ViTImageProcessor"
	A_				=    "MgpstrTokenizer"




	def __init__(   self						,				__a=None						,				__a=None						,				**__a							):

						'''simple docstring'''





						__a			:       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						,				)
											__a			:       Any					    =  kwargs.pop('feature_extractor'							)

						__a			:       Union[str, Any]					    =  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`.'							)

						__a			:       Any					    =  tokenizer
						__a			:       int					    =  AutoTokenizer.from_pretrained('gpt2'							)
						__a			:       List[Any]					    =  AutoTokenizer.from_pretrained('bert-base-uncased'							)

						super().__init__(__a						,				__a							)




	def __call__(   self						,				__a=None						,				__a=None						,				__a=None						,				**__a							):

						'''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:
											__a			:       List[str]					    =  self.image_processor(__a						,				return_tensors=__a						,				**__a							)
						if text is not None:
											__a			:       Optional[int]					    =  self.char_tokenizer(__a						,				return_tensors=__a						,				**__a							)

						if text is None:
											return inputs
						elif images is None:
											return encodings
						else:
											__a			:       Dict					    =  encodings['input_ids']
											return inputs




	def       __UpperCAmelCase							(   self						,				__a							):

						'''simple docstring'''





						__a    ,				__a    ,				__a			:       Optional[int]					    =  sequences
						__a			:       List[Any]					    =  char_preds.size(0							)

						__a    ,				__a			:       int					    =  self._decode_helper(__a						,				'char'							)
						__a    ,				__a			:       List[str]					    =  self._decode_helper(__a						,				'bpe'							)
						__a    ,				__a			:       Dict					    =  self._decode_helper(__a						,				'wp'							)

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

						__a			:       Union[str, Any]					    =  {}
						__a			:       Optional[Any]					    =  final_strs
						__a			:       List[Any]					    =  final_scores
						__a			:       Tuple					    =  char_strs
						__a			:       Optional[int]					    =  bpe_strs
						__a			:       Optional[Any]					    =  wp_strs
						return out




	def       __UpperCAmelCase							(   self						,				__a						,				__a							):

						'''simple docstring'''





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

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

						for index in range(__a							):
											__a			:       int					    =  preds_str[index].find(__a							)
											__a			:       Dict					    =  preds_str[index][:pred_eos]
											__a			:       List[str]					    =  preds_index[index].cpu().tolist()
											__a			:       List[Any]					    =  pred_index.index(__a							) if eos_token in pred_index else -1
											__a			:       Optional[int]					    =  preds_max_prob[index][: pred_eos_index + 1]
											__a			:       List[str]					    =  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						,				__a							):

						'''simple docstring'''





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




	def       __UpperCAmelCase							(   self						,				__a							):

						'''simple docstring'''





						return self.bpe_tokenizer.batch_decode(__a							)





	def       __UpperCAmelCase							(   self						,				__a							):

						'''simple docstring'''





						__a			:       str					    =  [seq.replace(' '						,				''							) for seq in self.wp_tokenizer.batch_decode(__a							)]
						return decode_strs







'''simple docstring'''



from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


__lowercase	:						Dict       =	logging.get_logger(__name__)

__lowercase	:						Optional[Any]       =	{
    'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json',
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}





class 							__UpperCamelCase				(		lowerCAmelCase_					):
	A_				=    "umt5"
	A_				=    ["past_key_values"]




	def __init__(   self						,				__a=25_0112						,				__a=512						,				__a=64						,				__a=1024						,				__a=8						,				__a=None						,				__a=6						,				__a=32						,				__a=128						,				__a=0.1						,				__a=1E-6						,				__a=1.0						,				__a="gated-gelu"						,				__a=True						,				__a=True						,				__a="T5Tokenizer"						,				__a=True						,				__a=0						,				__a=1						,				__a=0						,				**__a						,				):

						'''simple docstring'''





						super().__init__(
						    is_encoder_decoder=__a						,				tokenizer_class=__a						,				tie_word_embeddings=__a						,				pad_token_id=__a						,				eos_token_id=__a						,				decoder_start_token_id=__a						,				**__a						,				)
						__a			:       Any					    =  vocab_size
						__a			:       Any					    =  d_model
						__a			:       str					    =  d_kv
						__a			:       Dict					    =  d_ff
						__a			:       Union[str, Any]					    =  num_layers
						__a			:       int					    =  (
						    num_decoder_layers if num_decoder_layers is not None else self.num_layers
						)  # default = symmetry
						__a			:       Optional[int]					    =  num_heads
						__a			:       Tuple					    =  relative_attention_num_buckets
						__a			:       Optional[Any]					    =  relative_attention_max_distance
						__a			:       Optional[int]					    =  dropout_rate
						__a			:       List[Any]					    =  layer_norm_epsilon
						__a			:       int					    =  initializer_factor
						__a			:       Union[str, Any]					    =  feed_forward_proj
						__a			:       Any					    =  use_cache

						__a			:       List[Any]					    =  self.feed_forward_proj.split('-'							)
						__a			:       Dict					    =  act_info[-1]
						__a			:       Dict					    =  act_info[0] == 'gated'

						if len(__a							) > 1 and act_info[0] != "gated" or len(__a							) > 2:
											raise ValueError(
											    f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
											    'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '
											    '\'gated-gelu\' or \'relu\''							)

						if feed_forward_proj == "gated-gelu":
											__a			:       Optional[int]					    =  'gelu_new'




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return self.d_model




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return self.num_heads





	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return self.num_layers





class 							__UpperCamelCase				(		lowerCAmelCase_					):




	@property
	# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  {
						    'input_ids': {0: 'batch', 1: 'encoder_sequence'},
						    'attention_mask': {0: 'batch', 1: 'encoder_sequence'},
						}
						if self.use_past:
											__a			:       Dict					    =  'past_encoder_sequence + sequence'
											__a			:       Tuple					    =  {0: 'batch'}
											__a			:       Tuple					    =  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
						else:
											__a			:       List[Any]					    =  {0: 'batch', 1: 'decoder_sequence'}
											__a			:       int					    =  {0: 'batch', 1: 'decoder_sequence'}

						if self.use_past:
											self.fill_with_past_key_values_(__a						,				direction='inputs'							)

						return common_inputs




	@property
	# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return 13





	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return 5E-4



