
import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_PRETRAINING_MAPPING,
					    MobileBertForMaskedLM,
					    MobileBertForMultipleChoice,
					    MobileBertForNextSentencePrediction,
					    MobileBertForPreTraining,
					    MobileBertForQuestionAnswering,
					    MobileBertForSequenceClassification,
					    MobileBertForTokenClassification,
					    MobileBertModel,
					)





class 							UpperCAmelCase      :




	'''simple docstring'''





	def __init__(       self					: int							,A					: Tuple							,A					: List[str]=13							,A					: List[str]=7							,A					: str=True							,A					: int=True							,A					: Union[str, Any]=True							,A					: str=True							,A					: Dict=99							,A					: Optional[Any]=64							,A					: Union[str, Any]=32							,A					: Dict=5							,A					: Union[str, Any]=4							,A					: str=37							,A					: int="gelu"							,A					: str=0.1							,A					: List[Any]=0.1							,A					: Union[str, Any]=5_12							,A					: Tuple=16							,A					: List[Any]=2							,A					: Any=0.02							,A					: List[Any]=3							,A					: Union[str, Any]=4							,A					: List[str]=None							,):
					__A   			= parent
					__A   			= batch_size
					__A   			= seq_length
					__A   			= is_training
					__A   			= use_input_mask
					__A   			= use_token_type_ids
					__A   			= use_labels
					__A   			= vocab_size
					__A   			= hidden_size
					__A   			= embedding_size
					__A   			= num_hidden_layers
					__A   			= num_attention_heads
					__A   			= intermediate_size
					__A   			= hidden_act
					__A   			= hidden_dropout_prob
					__A   			= attention_probs_dropout_prob
					__A   			= max_position_embeddings
					__A   			= type_vocab_size
					__A   			= type_sequence_label_size
					__A   			= initializer_range
					__A   			= num_labels
					__A   			= num_choices
					__A   			= scope






	def 				UpperCamelCase_  (       self					: List[Any] ):
					__A   			= ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size )

					__A   			= None
					if self.use_input_mask:
									__A   			= random_attention_mask([self.batch_size, self.seq_length] )

					__A   			= None
					if self.use_token_type_ids:
									__A   			= ids_tensor([self.batch_size, self.seq_length]							,self.type_vocab_size )

					__A   			= None
					__A   			= None
					__A   			= None
					if self.use_labels:
									__A   			= ids_tensor([self.batch_size]							,self.type_sequence_label_size )
									__A   			= ids_tensor([self.batch_size, self.seq_length]							,self.num_labels )
									__A   			= ids_tensor([self.batch_size]							,self.num_choices )

					__A   			= self.get_config()

					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






	def 				UpperCamelCase_  (       self					: Any ):
					return MobileBertConfig(
					    vocab_size=self.vocab_size							,hidden_size=self.hidden_size							,num_hidden_layers=self.num_hidden_layers							,num_attention_heads=self.num_attention_heads							,intermediate_size=self.intermediate_size							,embedding_size=self.embedding_size							,hidden_act=self.hidden_act							,hidden_dropout_prob=self.hidden_dropout_prob							,attention_probs_dropout_prob=self.attention_probs_dropout_prob							,max_position_embeddings=self.max_position_embeddings							,type_vocab_size=self.type_vocab_size							,is_decoder=A							,initializer_range=self.initializer_range							,)






	def 				UpperCamelCase_  (       self					: Dict							,A					: Tuple							,A					: int							,A					: List[Any]							,A					: Optional[Any]							,A					: Optional[int]							,A					: int							,A					: int ):
					__A   			= MobileBertModel(config=A )
					model.to(A )
					model.eval()
					__A   			= model(A							,attention_mask=A							,token_type_ids=A )
					__A   			= model(A							,token_type_ids=A )
					__A   			= model(A )

					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size) )
					self.parent.assertEqual(result.pooler_output.shape							,(self.batch_size, self.hidden_size) )






	def 				UpperCamelCase_  (       self					: int							,A					: Dict							,A					: int							,A					: List[Any]							,A					: Optional[Any]							,A					: Tuple							,A					: str							,A					: str ):
					__A   			= MobileBertForMaskedLM(config=A )
					model.to(A )
					model.eval()
					__A   			= model(A							,attention_mask=A							,token_type_ids=A							,labels=A )
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size) )






	def 				UpperCamelCase_  (       self					: Any							,A					: Optional[Any]							,A					: Optional[Any]							,A					: Dict							,A					: Optional[int]							,A					: Any							,A					: Optional[Any]							,A					: Any ):
					__A   			= MobileBertForNextSentencePrediction(config=A )
					model.to(A )
					model.eval()
					__A   			= model(
					    A							,attention_mask=A							,token_type_ids=A							,labels=A							,)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, 2) )






	def 				UpperCamelCase_  (       self					: Dict							,A					: int							,A					: str							,A					: Optional[Any]							,A					: Optional[int]							,A					: Dict							,A					: Tuple							,A					: Optional[int] ):
					__A   			= MobileBertForPreTraining(config=A )
					model.to(A )
					model.eval()
					__A   			= model(
					    A							,attention_mask=A							,token_type_ids=A							,labels=A							,next_sentence_label=A							,)
					self.parent.assertEqual(result.prediction_logits.shape							,(self.batch_size, self.seq_length, self.vocab_size) )
					self.parent.assertEqual(result.seq_relationship_logits.shape							,(self.batch_size, 2) )






	def 				UpperCamelCase_  (       self					: int							,A					: Tuple							,A					: Any							,A					: Dict							,A					: List[Any]							,A					: Union[str, Any]							,A					: Optional[int]							,A					: Optional[int] ):
					__A   			= MobileBertForQuestionAnswering(config=A )
					model.to(A )
					model.eval()
					__A   			= model(
					    A							,attention_mask=A							,token_type_ids=A							,start_positions=A							,end_positions=A							,)
					self.parent.assertEqual(result.start_logits.shape							,(self.batch_size, self.seq_length) )
					self.parent.assertEqual(result.end_logits.shape							,(self.batch_size, self.seq_length) )






	def 				UpperCamelCase_  (       self					: Tuple							,A					: List[str]							,A					: List[str]							,A					: Optional[Any]							,A					: List[Any]							,A					: List[str]							,A					: Dict							,A					: Tuple ):
					__A   			= self.num_labels
					__A   			= MobileBertForSequenceClassification(A )
					model.to(A )
					model.eval()
					__A   			= model(A							,attention_mask=A							,token_type_ids=A							,labels=A )
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels) )






	def 				UpperCamelCase_  (       self					: Optional[int]							,A					: Dict							,A					: Optional[int]							,A					: str							,A					: Dict							,A					: Union[str, Any]							,A					: int							,A					: Union[str, Any] ):
					__A   			= self.num_labels
					__A   			= MobileBertForTokenClassification(config=A )
					model.to(A )
					model.eval()
					__A   			= model(A							,attention_mask=A							,token_type_ids=A							,labels=A )
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels) )






	def 				UpperCamelCase_  (       self					: Optional[int]							,A					: int							,A					: str							,A					: Optional[int]							,A					: List[Any]							,A					: Optional[Any]							,A					: Optional[Any]							,A					: Optional[Any] ):
					__A   			= self.num_choices
					__A   			= MobileBertForMultipleChoice(config=A )
					model.to(A )
					model.eval()
					__A   			= input_ids.unsqueeze(1 ).expand(-1							,self.num_choices							,-1 ).contiguous()
					__A   			= token_type_ids.unsqueeze(1 ).expand(-1							,self.num_choices							,-1 ).contiguous()
					__A   			= input_mask.unsqueeze(1 ).expand(-1							,self.num_choices							,-1 ).contiguous()
					__A   			= model(
					    A							,attention_mask=A							,token_type_ids=A							,labels=A							,)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices) )







	def 				UpperCamelCase_  (       self					: Tuple ):
					__A   			= self.prepare_config_and_inputs()
					(
					    (
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							
					)   			= config_and_inputs
					__A   			= {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict





@require_torch
class 							UpperCAmelCase      (  __SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			unittest.TestCase  ):




	'''simple docstring'''





	snake_case_					    =     (
	    (
	        MobileBertModel,
	        MobileBertForMaskedLM,
	        MobileBertForMultipleChoice,
	        MobileBertForNextSentencePrediction,
	        MobileBertForPreTraining,
	        MobileBertForQuestionAnswering,
	        MobileBertForSequenceClassification,
	        MobileBertForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case_					    =     (
	    {
	        "feature-extraction": MobileBertModel,
	        "fill-mask": MobileBertForMaskedLM,
	        "question-answering": MobileBertForQuestionAnswering,
	        "text-classification": MobileBertForSequenceClassification,
	        "token-classification": MobileBertForTokenClassification,
	        "zero-shot": MobileBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	snake_case_					    =     True






	def 				UpperCamelCase_  (       self					: List[str]							,A					: Tuple							,A					: List[Any]							,A					: Union[str, Any]=False ):
					__A   			= super()._prepare_for_class(A							,A							,return_labels=A )

					if return_labels:
									if model_class in get_values(A ):
													__A   			= torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.seq_length)							,dtype=torch.long							,device=A )
													__A   			= torch.zeros(
													    self.model_tester.batch_size							,dtype=torch.long							,device=A )
					return inputs_dict






	def 				UpperCamelCase_  (       self					: int ):
					__A   			= MobileBertModelTester(self )
					__A   			= ConfigTester(self							,config_class=A							,hidden_size=37 )






	def 				UpperCamelCase_  (       self					: Any ):
					self.config_tester.run_common_tests()






	def 				UpperCamelCase_  (       self					: Any ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_model(*A )






	def 				UpperCamelCase_  (       self					: Union[str, Any] ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_masked_lm(*A )






	def 				UpperCamelCase_  (       self					: Optional[Any] ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A )






	def 				UpperCamelCase_  (       self					: Optional[Any] ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A )






	def 				UpperCamelCase_  (       self					: Optional[int] ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_pretraining(*A )






	def 				UpperCamelCase_  (       self					: Any ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_question_answering(*A )






	def 				UpperCamelCase_  (       self					: str ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A )







	def 				UpperCamelCase_  (       self					: int ):
					__A   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_token_classification(*A )





def 						UpperCAmelCase			(	a_						)		->	str:







				"""simple docstring"""



				return torch.tensor(
				    a_		,   dtype=torch.long		,   device=a_		,   )


SCREAMING_SNAKE_CASE							:List[str]							 =				1E-3





@require_torch
@require_sentencepiece
@require_tokenizers
class 							UpperCAmelCase      (  unittest.TestCase  ):




	'''simple docstring'''





	@slow
	def 				UpperCamelCase_  (       self					: Tuple ):
					__A   			= MobileBertModel.from_pretrained("google/mobilebert-uncased" ).to(A )
					__A   			= _long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]] )
					with torch.no_grad():
									__A   			= model(A )[0]
					__A   			= torch.Size((1, 9, 5_12) )
					self.assertEqual(output.shape							,A )
					__A   			= torch.tensor(
					    [
					        [
					            [-2.4736526E07, 8.2691656E04, 1.6521838E05],
					            [-5.7541704E-01, 3.9056022E00, 4.4011507E00],
					            [2.6047359E00, 1.5677652E00, -1.7324188E-01],
					        ]
					    ]							,device=A							,)

					# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
					# ~1 difference, it's therefore not a good idea to measure using addition.
					# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
					# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
					__A   			= torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE )
					__A   			= torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE )

					self.assertTrue(lower_bound and upper_bound )


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

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


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

SCREAMING_SNAKE_CASE							:int							 =				{'vocab_file': 'spiece.model'}

SCREAMING_SNAKE_CASE							:Union[str, Any]							 =				{
    'vocab_file': {
        'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model',
        'google/bigbird-roberta-large': (
            'https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model'
        ),
        'google/bigbird-base-trivia-itc': (
            'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model'
        ),
    }
}

SCREAMING_SNAKE_CASE							:int							 =				{
    'google/bigbird-roberta-base': 4096,
    'google/bigbird-roberta-large': 4096,
    'google/bigbird-base-trivia-itc': 4096,
}





class 							UpperCAmelCase      (  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	snake_case_					    =     VOCAB_FILES_NAMES
	snake_case_					    =     PRETRAINED_VOCAB_FILES_MAP
	snake_case_					    =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	snake_case_					    =     ["input_ids", "attention_mask"]
	snake_case_					    =     []






	def __init__(       self					: Any							,A					: List[str]							,A					: str="<unk>"							,A					: int="<s>"							,A					: Union[str, Any]="</s>"							,A					: List[str]="<pad>"							,A					: int="[SEP]"							,A					: Optional[Any]="[MASK]"							,A					: Tuple="[CLS]"							,A					: Optional[Dict[str, Any]] = None							,**A					: Any							,):
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else bos_token
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else eos_token
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else unk_token
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else pad_token
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else cls_token
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else sep_token

					# Mask token behave like a normal word, i.e. include the space before it
					__A   			= AddedToken(A							,lstrip=A							,rstrip=A ) if isinstance(A							,A ) else mask_token

					__A   			= {} if sp_model_kwargs is None else sp_model_kwargs

					super().__init__(
					    bos_token=A							,eos_token=A							,unk_token=A							,pad_token=A							,sep_token=A							,mask_token=A							,cls_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 				UpperCamelCase_  (       self					: List[str] ):
					return self.sp_model.get_piece_size()






	def 				UpperCamelCase_  (       self					: Optional[Any] ):
					__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					: Optional[int] ):
					__A   			= self.__dict__.copy()
					__A   			= None
					return state






	def __setstate__(       self					: str							,A					: Optional[Any] ):
					__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 				UpperCamelCase_  (       self					: Any							,A					: str ):
					return self.sp_model.encode(A							,out_type=A )






	def 				UpperCamelCase_  (       self					: List[str]							,A					: Tuple ):
					return self.sp_model.piece_to_id(A )






	def 				UpperCamelCase_  (       self					: List[Any]							,A					: Tuple ):
					__A   			= self.sp_model.IdToPiece(A )
					return token






	def 				UpperCamelCase_  (       self					: List[Any]							,A					: int ):
					__A   			= []
					__A   			= ""
					__A   			= False
					for token in tokens:
									# make sure that special tokens are not decoded using sentencepiece model
									if token in self.all_special_tokens:
													if not prev_is_special:
																	out_string += " "
													out_string += self.sp_model.decode(A ) + token
													__A   			= True
													__A   			= []
									else:
													current_sub_tokens.append(A )
													__A   			= False
					out_string += self.sp_model.decode(A )
					return out_string.strip()






	def 				UpperCamelCase_  (       self					: Tuple							,A					: List[int]							,A					: bool = False							,A					: bool = None							,A					: bool = True							,**A					: Union[str, Any]							,):
					__A   			= kwargs.pop("use_source_tokenizer"							,A )

					__A   			= self.convert_ids_to_tokens(A							,skip_special_tokens=A )

					# To avoid mixing byte-level and unicode for byte-level BPT
					# we need to build string separately for added tokens and byte-level tokens
					# cf. https://github.com/huggingface/transformers/issues/1133
					__A   			= []
					__A   			= []
					for token in filtered_tokens:
									if skip_special_tokens and token in self.all_special_ids:
													continue
									if token in self.added_tokens_encoder:
													if current_sub_text:
																	sub_texts.append(self.convert_tokens_to_string(A ) )
																	__A   			= []
													sub_texts.append(A )
									else:
													current_sub_text.append(A )
					if current_sub_text:
									sub_texts.append(self.convert_tokens_to_string(A ) )

					# Mimic the behavior of the Rust tokenizer:
					# No space before [MASK] and [SEP]
					if spaces_between_special_tokens:
									__A   			= re.sub(R" (\[(MASK|SEP)\])"							,R"\1"							," ".join(A ) )
					else:
									__A   			= "".join(A )

					__A   			= (
					    clean_up_tokenization_spaces
					    if clean_up_tokenization_spaces is not None
					    else self.clean_up_tokenization_spaces
					)
					if clean_up_tokenization_spaces:
									__A   			= self.clean_up_tokenization(A )
									return clean_text
					else:
									return text






	def 				UpperCamelCase_  (       self					: str							,A					: str							,A					: Optional[str] = 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,)






	def 				UpperCamelCase_  (       self					: Dict							,A					: List[int]							,A					: Optional[List[int]] = None ):
					if token_ids_a is None:
									return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
					__A   			= [self.cls_token_id]
					__A   			= [self.sep_token_id]
					return cls + token_ids_a + sep + token_ids_a + sep






	def 				UpperCamelCase_  (       self					: Optional[int]							,A					: List[int]							,A					: Optional[List[int]] = None							,A					: bool = False ):
					if already_has_special_tokens:
									return super().get_special_tokens_mask(
									    token_ids_a=A							,token_ids_a=A							,already_has_special_tokens=A )

					if token_ids_a is None:
									return [1] + ([0] * len(A )) + [1]
					return [1] + ([0] * len(A )) + [1] + ([0] * len(A )) + [1]







	def 				UpperCamelCase_  (       self					: Any							,A					: List[int]							,A					: Optional[List[int]] = None ):
					__A   			= [self.sep_token_id]
					__A   			= [self.cls_token_id]
					if token_ids_a is None:
									return len(cls + token_ids_a + sep ) * [0]
					return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
