






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    _lowerCAmelCase			:



				"""simple docstring"""



				def __init__(      self    :    int			,		UpperCamelCase__    :    int			,		UpperCamelCase__    :    Dict=1_3			,		UpperCamelCase__    :    str=7			,		UpperCamelCase__    :    Dict=True			,		UpperCamelCase__    :    Optional[int]=True			,		UpperCamelCase__    :    int=True			,		UpperCamelCase__    :    List[Any]=True			,		UpperCamelCase__    :    Dict=9_9			,		UpperCamelCase__    :    str=6_4			,		UpperCamelCase__    :    Optional[int]=3_2			,		UpperCamelCase__    :    List[Any]=5			,		UpperCamelCase__    :    Optional[Any]=4			,		UpperCamelCase__    :    List[str]=3_7			,		UpperCamelCase__    :    Optional[Any]="gelu"			,		UpperCamelCase__    :    Optional[int]=0.1			,		UpperCamelCase__    :    int=0.1			,		UpperCamelCase__    :    str=5_1_2			,		UpperCamelCase__    :    str=1_6			,		UpperCamelCase__    :    Tuple=2			,		UpperCamelCase__    :    Any=0.02			,		UpperCamelCase__    :    Tuple=3			,		UpperCamelCase__    :    str=4			,		UpperCamelCase__    :    Optional[int]=None			,		):






											'''simple docstring'''







											snake_case__	  =       parent
											snake_case__	  =       batch_size
											snake_case__	  =       seq_length
											snake_case__	  =       is_training
											snake_case__	  =       use_input_mask
											snake_case__	  =       use_token_type_ids
											snake_case__	  =       use_labels
											snake_case__	  =       vocab_size
											snake_case__	  =       hidden_size
											snake_case__	  =       embedding_size
											snake_case__	  =       num_hidden_layers
											snake_case__	  =       num_attention_heads
											snake_case__	  =       intermediate_size
											snake_case__	  =       hidden_act
											snake_case__	  =       hidden_dropout_prob
											snake_case__	  =       attention_probs_dropout_prob
											snake_case__	  =       max_position_embeddings
											snake_case__	  =       type_vocab_size
											snake_case__	  =       type_sequence_label_size
											snake_case__	  =       initializer_range
											snake_case__	  =       num_labels
											snake_case__	  =       num_choices
											snake_case__	  =       scope



				def 					__magic_name__							(      self    :    Optional[Any]):






											'''simple docstring'''







											snake_case__	  =       ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size)

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

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

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

											snake_case__	  =       self.get_config()

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



				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







											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=UpperCamelCase__			,		initializer_range=self.initializer_range			,		)



				def 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    int			,		UpperCamelCase__    :    int			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    Dict):






											'''simple docstring'''







											snake_case__	  =       MobileBertModel(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__)
											snake_case__	  =       model(UpperCamelCase__			,		token_type_ids=UpperCamelCase__)
											snake_case__	  =       model(UpperCamelCase__)

											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 					__magic_name__							(      self    :    List[str]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    int			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    Union[str, Any]):






											'''simple docstring'''







											snake_case__	  =       MobileBertForMaskedLM(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		labels=UpperCamelCase__)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.vocab_size))



				def 					__magic_name__							(      self    :    Any			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    Tuple):






											'''simple docstring'''







											snake_case__	  =       MobileBertForNextSentencePrediction(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(
											    UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		labels=UpperCamelCase__			,		)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, 2))



				def 					__magic_name__							(      self    :    Optional[int]			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    int):






											'''simple docstring'''







											snake_case__	  =       MobileBertForPreTraining(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(
											    UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		labels=UpperCamelCase__			,		next_sentence_label=UpperCamelCase__			,		)
											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 					__magic_name__							(      self    :    Optional[int]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    str):






											'''simple docstring'''







											snake_case__	  =       MobileBertForQuestionAnswering(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(
											    UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		start_positions=UpperCamelCase__			,		end_positions=UpperCamelCase__			,		)
											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 					__magic_name__							(      self    :    Tuple			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    List[Any]):






											'''simple docstring'''







											snake_case__	  =       self.num_labels
											snake_case__	  =       MobileBertForSequenceClassification(UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		labels=UpperCamelCase__)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_labels))



				def 					__magic_name__							(      self    :    Optional[int]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    List[Any]			,		UpperCamelCase__    :    Any):






											'''simple docstring'''







											snake_case__	  =       self.num_labels
											snake_case__	  =       MobileBertForTokenClassification(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       model(UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		labels=UpperCamelCase__)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.num_labels))



				def 					__magic_name__							(      self    :    str			,		UpperCamelCase__    :    Tuple			,		UpperCamelCase__    :    Optional[Any]			,		UpperCamelCase__    :    Optional[Any]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       self.num_choices
											snake_case__	  =       MobileBertForMultipleChoice(config=UpperCamelCase__)
											model.to(UpperCamelCase__)
											model.eval()
											snake_case__	  =       input_ids.unsqueeze(1).expand(-1			,		self.num_choices			,		-1).contiguous()
											snake_case__	  =       token_type_ids.unsqueeze(1).expand(-1			,		self.num_choices			,		-1).contiguous()
											snake_case__	  =       input_mask.unsqueeze(1).expand(-1			,		self.num_choices			,		-1).contiguous()
											snake_case__	  =       model(
											    UpperCamelCase__			,		attention_mask=UpperCamelCase__			,		token_type_ids=UpperCamelCase__			,		labels=UpperCamelCase__			,		)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_choices))



				def 					__magic_name__							(      self    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       self.prepare_config_and_inputs()
											(
											    (
											    snake_case__
											)  , (
											    snake_case__
											)  , (
											    snake_case__
											)  , (
											    snake_case__
											)  , (
											    snake_case__
											)  , (
											    snake_case__
											)  , (
											    snake_case__
											)  , 
											)	  =       config_and_inputs
											snake_case__	  =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
											return config, inputs_dict


@require_torch
class    _lowerCAmelCase			(		lowercase_     ,      lowercase_     ,      unittest.TestCase      ):



				"""simple docstring"""



				_lowercase				:   List[Any]      =  (
				    (
				        MobileBertModel,
				        MobileBertForMaskedLM,
				        MobileBertForMultipleChoice,
				        MobileBertForNextSentencePrediction,
				        MobileBertForPreTraining,
				        MobileBertForQuestionAnswering,
				        MobileBertForSequenceClassification,
				        MobileBertForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				_lowercase				:   int      =  (
				    {
				        '''feature-extraction''': MobileBertModel,
				        '''fill-mask''': MobileBertForMaskedLM,
				        '''question-answering''': MobileBertForQuestionAnswering,
				        '''text-classification''': MobileBertForSequenceClassification,
				        '''token-classification''': MobileBertForTokenClassification,
				        '''zero-shot''': MobileBertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				_lowercase				:   List[str]      =  True



				def 					__magic_name__							(      self    :    Dict			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Any=False):






											'''simple docstring'''







											snake_case__	  =       super()._prepare_for_class(UpperCamelCase__			,		UpperCamelCase__			,		return_labels=UpperCamelCase__)

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



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       MobileBertModelTester(self)
											snake_case__	  =       ConfigTester(self			,		config_class=UpperCamelCase__			,		hidden_size=3_7)



				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







											self.config_tester.run_common_tests()



				def 					__magic_name__							(      self    :    Any):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    Union[str, Any]):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    List[Any]):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase__)



				def 					__magic_name__							(      self    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase__)


def _UpperCAmelCase   (	a						:	str ):
							return torch.tensor(
							    a  ,					dtype=torch.long  ,					device=a  ,					)


a__       =     1e-3



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



				"""simple docstring"""



				@slow
				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											snake_case__	  =       MobileBertModel.from_pretrained("""google/mobilebert-uncased""").to(UpperCamelCase__)
											snake_case__	  =       _long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]])
											with torch.no_grad():
																		snake_case__	  =       model(UpperCamelCase__)[0]
											snake_case__	  =       torch.Size((1, 9, 5_1_2))
											self.assertEqual(output.shape			,		UpperCamelCase__)
											snake_case__	  =       torch.tensor(
											    [
											        [
											            [-2.4_736_526E07, 8.2_691_656E04, 1.6_521_838E05],
											            [-5.7_541_704E-01, 3.9_056_022E00, 4.4_011_507E00],
											            [2.6_047_359E00, 1.5_677_652E00, -1.7_324_188E-01],
											        ]
											    ]			,		device=UpperCamelCase__			,		)

											# 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
											snake_case__	  =       torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE)
											snake_case__	  =       torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE)

											self.assertTrue(lower_bound and upper_bound)








import requests
from bsa import BeautifulSoup

def _UpperCAmelCase   (	a						:	str  ,					a						:	dict ):
							snake_case__	  =       BeautifulSoup(requests.get(a  ,					params=a ).content  ,					"""html.parser""" )
							snake_case__	  =       soup.find("""div"""  ,					attrs={"""class""": """gs_ri"""} )
							snake_case__	  =       div.find("""div"""  ,					attrs={"""class""": """gs_fl"""} ).find_all("""a""" )
							return anchors[2].get_text()


if __name__ == "__main__":
	a__       =     {
	    """title""": (
	        """Precisely geometry controlled microsupercapacitors for ultrahigh areal """
	        """capacitance, volumetric capacitance, and energy density"""
	    ),
	    """journal""": """Chem. Mater.""",
	    """volume""": 3_0,
	    """pages""": """3979-3990""",
	    """year""": 2_0_1_8,
	    """hl""": """en""",
	}
	print(get_citation("""https://scholar.google.com/scholar_lookup""", params=params))
