


"""simple docstring"""







import warnings

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


a__				:							Tuple						=     logging.get_logger(__name__)

a__				:							Tuple						=     {
    '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/config.json''',
    '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/config.json''',
}




class  UpperCamelCase_	( UpperCamelCase):

	"""simple docstring"""





	snake_case__				:  List[Any]    					=			"xlnet"
	snake_case__				:  Tuple    					=			["mems"]
	snake_case__				:  Optional[Any]    					=			{
	    "n_token": "vocab_size",  # Backward compatibility
	    "hidden_size": "d_model",
	    "num_attention_heads": "n_head",
	    "num_hidden_layers": "n_layer",
	}




	def __init__( self			:				str			,			UpperCAmelCase__			:				int=3_2_0_0_0			,			UpperCAmelCase__			:				List[str]=1_0_2_4			,			UpperCAmelCase__			:				Union[str, Any]=2_4			,			UpperCAmelCase__			:				str=1_6			,			UpperCAmelCase__			:				int=4_0_9_6			,			UpperCAmelCase__			:				Dict="gelu"			,			UpperCAmelCase__			:				Optional[int]=True			,			UpperCAmelCase__			:				Dict="bi"			,			UpperCAmelCase__			:				Tuple=0.02			,			UpperCAmelCase__			:				Optional[Any]=1E-12			,			UpperCAmelCase__			:				Any=0.1			,			UpperCAmelCase__			:				Optional[Any]=5_1_2			,			UpperCAmelCase__			:				Optional[int]=None			,			UpperCAmelCase__			:				Dict=True			,			UpperCAmelCase__			:				List[str]=False			,			UpperCAmelCase__			:				Optional[Any]=False			,			UpperCAmelCase__			:				Optional[Any]=-1			,			UpperCAmelCase__			:				Optional[Any]=False			,			UpperCAmelCase__			:				str="last"			,			UpperCAmelCase__			:				List[Any]=True			,			UpperCAmelCase__			:				Dict="tanh"			,			UpperCAmelCase__			:				Optional[Any]=0.1			,			UpperCAmelCase__			:				Dict=5			,			UpperCAmelCase__			:				List[Any]=5			,			UpperCAmelCase__			:				Optional[Any]=5			,			UpperCAmelCase__			:				Optional[int]=1			,			UpperCAmelCase__			:				Dict=2			,			**UpperCAmelCase__			:				str			,			)       ->	Dict:
						__SCREAMING_SNAKE_CASE						   =				vocab_size
						__SCREAMING_SNAKE_CASE						   =				d_model
						__SCREAMING_SNAKE_CASE						   =				n_layer
						__SCREAMING_SNAKE_CASE						   =				n_head
						if d_model % n_head != 0:
											raise ValueError(F"""'d_model % n_head' ({d_model % n_head}) should be equal to 0"""   )
						if "d_head" in kwargs:
											if kwargs["d_head"] != d_model // n_head:
																raise ValueError(
																    F"""`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})"""   )
						__SCREAMING_SNAKE_CASE						   =				d_model // n_head
						__SCREAMING_SNAKE_CASE						   =				ff_activation
						__SCREAMING_SNAKE_CASE						   =				d_inner
						__SCREAMING_SNAKE_CASE						   =				untie_r
						__SCREAMING_SNAKE_CASE						   =				attn_type

						__SCREAMING_SNAKE_CASE						   =				initializer_range
						__SCREAMING_SNAKE_CASE						   =				layer_norm_eps

						__SCREAMING_SNAKE_CASE						   =				dropout
						__SCREAMING_SNAKE_CASE						   =				mem_len
						__SCREAMING_SNAKE_CASE						   =				reuse_len
						__SCREAMING_SNAKE_CASE						   =				bi_data
						__SCREAMING_SNAKE_CASE						   =				clamp_len
						__SCREAMING_SNAKE_CASE						   =				same_length

						__SCREAMING_SNAKE_CASE						   =				summary_type
						__SCREAMING_SNAKE_CASE						   =				summary_use_proj
						__SCREAMING_SNAKE_CASE						   =				summary_activation
						__SCREAMING_SNAKE_CASE						   =				summary_last_dropout
						__SCREAMING_SNAKE_CASE						   =				start_n_top
						__SCREAMING_SNAKE_CASE						   =				end_n_top

						__SCREAMING_SNAKE_CASE						   =				bos_token_id
						__SCREAMING_SNAKE_CASE						   =				pad_token_id
						__SCREAMING_SNAKE_CASE						   =				eos_token_id

						if "use_cache" in kwargs:
											warnings.warn(
											    "The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"
											    " instead."			,			UpperCAmelCase__			,			)
											__SCREAMING_SNAKE_CASE						   =				kwargs["use_cache"]

						__SCREAMING_SNAKE_CASE						   =				use_mems_eval
						__SCREAMING_SNAKE_CASE						   =				use_mems_train
						super().__init__(pad_token_id=UpperCAmelCase__			,			bos_token_id=UpperCAmelCase__			,			eos_token_id=UpperCAmelCase__			,			**UpperCAmelCase__   )




	@property
	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	str:
						logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit."""   )
						return -1




	@max_position_embeddings.setter
	def   UpperCAmelCase_      ( self			:				Union[str, Any]			,			UpperCAmelCase__			:				str   )       ->	str:
						# Message copied from Transformer-XL documentation
						raise NotImplementedError(
						    F"""The model {self.model_type} is one of the few models that has no sequence length limit."""   )







'''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	(       UpperCamelCase__				):
 __lowercase   		=				"""char"""
 __lowercase   		=				"""bpe"""
 __lowercase   		=				"""wp"""

__lowerCAmelCase							:	Union[str, Any]			=(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)






class    UpperCAmelCase	(       UpperCamelCase__				):
 __lowercase   		=				["""image_processor""", """char_tokenizer"""]
 __lowercase   		=				"""ViTImageProcessor"""
 __lowercase   		=				"""MgpstrTokenizer"""
 def __init__(				self      :int						,       lowercase_      :int=None						,       lowercase_      :List[str]=None						,       **lowercase_      :List[Any]							)->							Optional[Any]:
     A__				=			None
     if "feature_extractor" in kwargs:
         warnings.warn(
             "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
             " instead."						,       lowercase_						,       )
         A__				=			kwargs.pop("feature_extractor"							)

     A__				=			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__				=			tokenizer
     A__				=			AutoTokenizer.from_pretrained("gpt2"							)
     A__				=			AutoTokenizer.from_pretrained("bert-base-uncased"							)

     super().__init__(lowercase_						,       lowercase_							)
 def __call__(				self      :Optional[Any]						,       lowercase_      :Any=None						,       lowercase_      :Tuple=None						,       lowercase_      :List[str]=None						,       **lowercase_      :Union[str, Any]							)->							Optional[Any]:
     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__				=			self.image_processor(lowercase_						,       return_tensors=lowercase_						,       **lowercase_							)
     if text is not None:
         A__				=			self.char_tokenizer(lowercase_						,       return_tensors=lowercase_						,       **lowercase_							)

     if text is None:
         return inputs
     elif images is None:
         return encodings
     else:
         A__				=			encodings["input_ids"]
         return inputs
 def      UpperCAmelCase_							(				self      :List[str]						,       lowercase_      :int							)->							int:
     A__,	A__,	A__				=			sequences
     A__				=			char_preds.size(0							)

     A__,	A__				=			self._decode_helper(lowercase_						,       "char"							)
     A__,	A__				=			self._decode_helper(lowercase_						,       "bpe"							)
     A__,	A__				=			self._decode_helper(lowercase_						,       "wp"							)

     A__				=			[]
     A__				=			[]
     for i in range(lowercase_							):
         A__				=			[char_scores[i], bpe_scores[i], wp_scores[i]]
         A__				=			[char_strs[i], bpe_strs[i], wp_strs[i]]
         A__				=			scores.index(max(lowercase_							)							)
         final_strs.append(strs[max_score_index]							)
         final_scores.append(scores[max_score_index]							)

     A__				=			{}
     A__				=			final_strs
     A__				=			final_scores
     A__				=			char_strs
     A__				=			bpe_strs
     A__				=			wp_strs
     return out
 def      UpperCAmelCase_							(				self      :Union[str, Any]						,       lowercase_      :List[str]						,       lowercase_      :str							)->							Optional[Any]:
     if format == DecodeType.CHARACTER:
         A__				=			self.char_decode
         A__				=			1
         A__				=			"[s]"
     elif format == DecodeType.BPE:
         A__				=			self.bpe_decode
         A__				=			2
         A__				=			"#"
     elif format == DecodeType.WORDPIECE:
         A__				=			self.wp_decode
         A__				=			1_02
         A__				=			"[SEP]"
     else:
         raise ValueError(F"Format {format} is not supported."							)

     A__,	A__				=			[], []
     A__				=			pred_logits.size(0							)
     A__				=			pred_logits.size(1							)
     A__,	A__				=			pred_logits.topk(1						,       dim=-1						,       largest=lowercase_						,       sorted=lowercase_							)
     A__				=			preds_index.view(-1						,       lowercase_							)[:, 1:]
     A__				=			decoder(lowercase_							)
     A__,	A__				=			torch.nn.functional.softmax(lowercase_						,       dim=2							).max(dim=2							)
     A__				=			preds_max_prob[:, 1:]

     for index in range(lowercase_							):
         A__				=			preds_str[index].find(lowercase_							)
         A__				=			preds_str[index][:pred_eos]
         A__				=			preds_index[index].cpu().tolist()
         A__				=			pred_index.index(lowercase_							) if eos_token in pred_index else -1
         A__				=			preds_max_prob[index][: pred_eos_index + 1]
         A__				=			pred_max_prob.cumprod(dim=0							)[-1] if pred_max_prob.nelement() != 0 else 0.0
         dec_strs.append(lowercase_							)
         conf_scores.append(lowercase_							)

     return dec_strs, conf_scores
 def      UpperCAmelCase_							(				self      :Dict						,       lowercase_      :Optional[Any]							)->							int:
     A__				=			[seq.replace(" "						,       ""							) for seq in self.char_tokenizer.batch_decode(lowercase_							)]
     return decode_strs
 def      UpperCAmelCase_							(				self      :List[Any]						,       lowercase_      :Optional[Any]							)->							List[str]:
     return self.bpe_tokenizer.batch_decode(lowercase_							)



 def      UpperCAmelCase_							(				self      :Optional[int]						,       lowercase_      :List[str]							)->							Union[str, Any]:
     A__				=			[seq.replace(" "						,       ""							) for seq in self.wp_tokenizer.batch_decode(lowercase_							)]
     return decode_strs



