




'''simple docstring'''







import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory

from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline
from transformers.convert_graph_to_onnx import (
    convert,
    ensure_valid_input,
    generate_identified_filename,
    infer_shapes,
    quantize,
)
from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow


class     _a						:
       def 					A   (			self		:			int     ,       lowercase		:			List[Any]     ,       lowercase		:			List[Any]     ,       lowercase		:			Optional[Any]       ):



           '''simple docstring'''

           return None






class     _a						:
       def 					A   (			self		:			List[Any]     ,       lowercase		:			List[Any]     ,       lowercase		:			List[str]     ,       lowercase		:			Tuple     ,       lowercase		:			Optional[int]       ):



           '''simple docstring'''

           return None






class     _a						(			unittest.TestCase				):
       __a       :  str            =      [
           # (model_name, model_kwargs)
           ("""bert-base-cased""", {}),
           ("""gpt2""", {"""use_cache""": False}),  # We don't support exporting GPT2 past keys anymore
       ]
       @require_tf
       @slow
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               self._test_export(lowercase     ,       '''tf'''     ,       12     ,       **lowercase       )
       @require_torch
       @slow
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               self._test_export(lowercase     ,       '''pt'''     ,       12     ,       **lowercase       )
       @require_torch
       @slow
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           from transformers import BertModel

           UpperCAmelCase     =	['''[UNK]''', '''[SEP]''', '''[CLS]''', '''[PAD]''', '''[MASK]''', '''some''', '''other''', '''words''']
           with NamedTemporaryFile(mode='''w+t'''       ) as vocab_file:
               vocab_file.write('''\n'''.join(lowercase       )       )
               vocab_file.flush()
               UpperCAmelCase     =	BertTokenizerFast(vocab_file.name       )

           with TemporaryDirectory() as bert_save_dir:
               UpperCAmelCase     =	BertModel(BertConfig(vocab_size=len(lowercase       )       )       )
               model.save_pretrained(lowercase       )
               self._test_export(lowercase     ,       '''pt'''     ,       12     ,       lowercase       )
       @require_tf
       @slow
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               UpperCAmelCase     =	self._test_export(lowercase     ,       '''tf'''     ,       12     ,       **lowercase       )
               UpperCAmelCase     =	quantize(Path(lowercase       )       )

               # Ensure the actual quantized model is not bigger than the original one
               if quantized_path.stat().st_size >= Path(lowercase       ).stat().st_size:
                   self.fail('''Quantized model is bigger than initial ONNX model'''       )
       @require_torch
       @slow
       def 					A   (			self		:			Tuple       ):



           '''simple docstring'''

           for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               UpperCAmelCase     =	self._test_export(lowercase     ,       '''pt'''     ,       12     ,       **lowercase       )
               UpperCAmelCase     =	quantize(lowercase       )

               # Ensure the actual quantized model is not bigger than the original one
               if quantized_path.stat().st_size >= Path(lowercase       ).stat().st_size:
                   self.fail('''Quantized model is bigger than initial ONNX model'''       )
       def 					A   (			self		:			int     ,       lowercase		:			List[str]     ,       lowercase		:			List[str]     ,       lowercase		:			Optional[Any]     ,       lowercase		:			Optional[int]=None     ,       **lowercase		:			str       ):



           '''simple docstring'''

           try:
               # Compute path
               with TemporaryDirectory() as tempdir:
                   UpperCAmelCase     =	Path(lowercase       ).joinpath('''model.onnx'''       )

               # Remove folder if exists
               if path.parent.exists():
                   path.parent.rmdir()

               # Export
               convert(lowercase     ,       lowercase     ,       lowercase     ,       lowercase     ,       lowercase     ,       **lowercase       )

               return path
           except Exception as e:
               self.fail(lowercase       )
       @require_torch
       @require_tokenizers
       @slow
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           from transformers import BertModel

           UpperCAmelCase     =	BertModel(BertConfig.from_pretrained('''lysandre/tiny-bert-random'''       )       )
           UpperCAmelCase     =	BertTokenizerFast.from_pretrained('''lysandre/tiny-bert-random'''       )
           self._test_infer_dynamic_axis(lowercase     ,       lowercase     ,       '''pt'''       )
       @require_tf
       @require_tokenizers
       @slow
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           from transformers import TFBertModel

           UpperCAmelCase     =	TFBertModel(BertConfig.from_pretrained('''lysandre/tiny-bert-random'''       )       )
           UpperCAmelCase     =	BertTokenizerFast.from_pretrained('''lysandre/tiny-bert-random'''       )
           self._test_infer_dynamic_axis(lowercase     ,       lowercase     ,       '''tf'''       )
       def 					A   (			self		:			List[str]     ,       lowercase		:			List[Any]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	FeatureExtractionPipeline(lowercase     ,       lowercase       )

           UpperCAmelCase     =	['''input_ids''', '''token_type_ids''', '''attention_mask''', '''output_0''', '''output_1''']
           UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase     =	infer_shapes(lowercase     ,       lowercase       )

           # Assert all variables are present
           self.assertEqual(len(lowercase       )     ,       len(lowercase       )       )
           self.assertTrue(all(var_name in shapes for var_name in variable_names       )       )
           self.assertSequenceEqual(variable_names[:3]     ,       lowercase       )
           self.assertSequenceEqual(variable_names[3:]     ,       lowercase       )

           # Assert inputs are {0: batch, 1: sequence}
           for var_name in ["input_ids", "token_type_ids", "attention_mask"]:
               self.assertDictEqual(shapes[var_name]     ,       {0: '''batch''', 1: '''sequence'''}       )

           # Assert outputs are {0: batch, 1: sequence} and {0: batch}
           self.assertDictEqual(shapes['''output_0''']     ,       {0: '''batch''', 1: '''sequence'''}       )
           self.assertDictEqual(shapes['''output_1''']     ,       {0: '''batch'''}       )
       def 					A   (			self		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	['''input_ids''', '''attention_mask''', '''token_type_ids''']
           UpperCAmelCase     =	{'''input_ids''': [1, 2, 3, 4], '''attention_mask''': [0, 0, 0, 0], '''token_type_ids''': [1, 1, 1, 1]}
           UpperCAmelCase ,  UpperCAmelCase     =	ensure_valid_input(FuncContiguousArgs()     ,       lowercase     ,       lowercase       )

           # Should have exactly the same number of args (all are valid)
           self.assertEqual(len(lowercase       )     ,       3       )

           # Should have exactly the same input names
           self.assertEqual(set(lowercase       )     ,       set(lowercase       )       )

           # Parameter should be reordered according to their respective place in the function:
           # (input_ids, token_type_ids, attention_mask)
           self.assertEqual(lowercase     ,       (tokens['''input_ids'''], tokens['''token_type_ids'''], tokens['''attention_mask'''])       )

           # Generated args are interleaved with another args (for instance parameter "past" in GPT2)
           UpperCAmelCase ,  UpperCAmelCase     =	ensure_valid_input(FuncNonContiguousArgs()     ,       lowercase     ,       lowercase       )

           # Should have exactly the one arg (all before the one not provided "some_other_args")
           self.assertEqual(len(lowercase       )     ,       1       )
           self.assertEqual(len(lowercase       )     ,       1       )

           # Should have only "input_ids"
           self.assertEqual(inputs_args[0]     ,       tokens['''input_ids''']       )
           self.assertEqual(ordered_input_names[0]     ,       '''input_ids'''       )







       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           UpperCAmelCase     =	generate_identified_filename(Path('''/home/something/my_fake_model.onnx'''       )     ,       '''-test'''       )
           self.assertEqual('''/home/something/my_fake_model-test.onnx'''     ,       generated.as_posix()       )










'''simple docstring'''







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     _a						:
       def __init__(			self		:			List[str]     ,       lowercase		:			Dict     ,       lowercase		:			List[Any]=13     ,       lowercase		:			Optional[Any]=7     ,       lowercase		:			Any=True     ,       lowercase		:			str=True     ,       lowercase		:			List[Any]=True     ,       lowercase		:			str=True     ,       lowercase		:			List[str]=99     ,       lowercase		:			int=64     ,       lowercase		:			List[Any]=32     ,       lowercase		:			str=5     ,       lowercase		:			Optional[int]=4     ,       lowercase		:			int=37     ,       lowercase		:			str="gelu"     ,       lowercase		:			Any=0.1     ,       lowercase		:			Optional[Any]=0.1     ,       lowercase		:			Optional[int]=512     ,       lowercase		:			Union[str, Any]=16     ,       lowercase		:			List[str]=2     ,       lowercase		:			Tuple=0.02     ,       lowercase		:			List[Any]=3     ,       lowercase		:			int=4     ,       lowercase		:			Optional[Any]=None     ,       ):



           '''simple docstring'''

           UpperCAmelCase     =	parent
           UpperCAmelCase     =	batch_size
           UpperCAmelCase     =	seq_length
           UpperCAmelCase     =	is_training
           UpperCAmelCase     =	use_input_mask
           UpperCAmelCase     =	use_token_type_ids
           UpperCAmelCase     =	use_labels
           UpperCAmelCase     =	vocab_size
           UpperCAmelCase     =	hidden_size
           UpperCAmelCase     =	embedding_size
           UpperCAmelCase     =	num_hidden_layers
           UpperCAmelCase     =	num_attention_heads
           UpperCAmelCase     =	intermediate_size
           UpperCAmelCase     =	hidden_act
           UpperCAmelCase     =	hidden_dropout_prob
           UpperCAmelCase     =	attention_probs_dropout_prob
           UpperCAmelCase     =	max_position_embeddings
           UpperCAmelCase     =	type_vocab_size
           UpperCAmelCase     =	type_sequence_label_size
           UpperCAmelCase     =	initializer_range
           UpperCAmelCase     =	num_labels
           UpperCAmelCase     =	num_choices
           UpperCAmelCase     =	scope
       def 					A   (			self		:			Union[str, Any]       ):



           '''simple docstring'''

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

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

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

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

           UpperCAmelCase     =	self.get_config()

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
       def 					A   (			self		:			Optional[Any]       ):



           '''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=lowercase     ,       initializer_range=self.initializer_range     ,       )
       def 					A   (			self		:			Tuple     ,       lowercase		:			Optional[Any]     ,       lowercase		:			List[Any]     ,       lowercase		:			Optional[int]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			List[Any]     ,       lowercase		:			str     ,       lowercase		:			Dict       ):



           '''simple docstring'''

           UpperCAmelCase     =	MobileBertModel(config=lowercase       )
           model.to(lowercase       )
           model.eval()
           UpperCAmelCase     =	model(lowercase     ,       attention_mask=lowercase     ,       token_type_ids=lowercase       )
           UpperCAmelCase     =	model(lowercase     ,       token_type_ids=lowercase       )
           UpperCAmelCase     =	model(lowercase       )

           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 					A   (			self		:			Dict     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			List[str]     ,       lowercase		:			List[str]     ,       lowercase		:			Dict     ,       lowercase		:			List[str]     ,       lowercase		:			Optional[int]     ,       lowercase		:			List[str]       ):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase     =	MobileBertForNextSentencePrediction(config=lowercase       )
           model.to(lowercase       )
           model.eval()
           UpperCAmelCase     =	model(
               lowercase     ,       attention_mask=lowercase     ,       token_type_ids=lowercase     ,       labels=lowercase     ,       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, 2)       )
       def 					A   (			self		:			Optional[int]     ,       lowercase		:			Tuple     ,       lowercase		:			Optional[Any]     ,       lowercase		:			int     ,       lowercase		:			Optional[Any]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			Tuple       ):



           '''simple docstring'''

           UpperCAmelCase     =	MobileBertForPreTraining(config=lowercase       )
           model.to(lowercase       )
           model.eval()
           UpperCAmelCase     =	model(
               lowercase     ,       attention_mask=lowercase     ,       token_type_ids=lowercase     ,       labels=lowercase     ,       next_sentence_label=lowercase     ,       )
           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 					A   (			self		:			Optional[int]     ,       lowercase		:			Any     ,       lowercase		:			Dict     ,       lowercase		:			str     ,       lowercase		:			Optional[Any]     ,       lowercase		:			List[str]     ,       lowercase		:			int     ,       lowercase		:			Any       ):



           '''simple docstring'''

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



           '''simple docstring'''

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



           '''simple docstring'''

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



           '''simple docstring'''

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







       def 					A   (			self		:			int       ):



           '''simple docstring'''

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






@require_torch
class     _a						(			__a							,							__a							,							unittest.TestCase				):
       __a       :  Dict            =      (
           (
               MobileBertModel,
               MobileBertForMaskedLM,
               MobileBertForMultipleChoice,
               MobileBertForNextSentencePrediction,
               MobileBertForPreTraining,
               MobileBertForQuestionAnswering,
               MobileBertForSequenceClassification,
               MobileBertForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       __a       :  List[str]            =      (
           {
               """feature-extraction""": MobileBertModel,
               """fill-mask""": MobileBertForMaskedLM,
               """question-answering""": MobileBertForQuestionAnswering,
               """text-classification""": MobileBertForSequenceClassification,
               """token-classification""": MobileBertForTokenClassification,
               """zero-shot""": MobileBertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       __a       :  Any            =      True
       def 					A   (			self		:			List[Any]     ,       lowercase		:			int     ,       lowercase		:			Any     ,       lowercase		:			Optional[Any]=False       ):



           '''simple docstring'''

           UpperCAmelCase     =	super()._prepare_for_class(lowercase     ,       lowercase     ,       return_labels=lowercase       )

           if return_labels:
               if model_class in get_values(lowercase       ):
                   UpperCAmelCase     =	torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.seq_length)     ,       dtype=torch.long     ,       device=lowercase       )
                   UpperCAmelCase     =	torch.zeros(
                       self.model_tester.batch_size     ,       dtype=torch.long     ,       device=lowercase       )
           return inputs_dict
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	MobileBertModelTester(self       )
           UpperCAmelCase     =	ConfigTester(self     ,       config_class=lowercase     ,       hidden_size=37       )
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_model(*lowercase       )
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase       )
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase       )
       def 					A   (			self		:			int       ):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase       )
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase       )







       def 					A   (			self		:			Tuple       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase       )






def 		snake_case_							(_a			:							List[Any]  ):
    return torch.tensor(
        _a					,     dtype=torch.long					,     device=_a					,     )


A									=1E-3







@require_torch
@require_sentencepiece
@require_tokenizers
class     _a						(			unittest.TestCase				):
       @slow
       def 					A   (			self		:			Union[str, Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	MobileBertModel.from_pretrained('''google/mobilebert-uncased'''       ).to(lowercase       )
           UpperCAmelCase     =	_long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]       )
           with torch.no_grad():
               UpperCAmelCase     =	model(lowercase       )[0]
           UpperCAmelCase     =	torch.Size((1, 9, 512)       )
           self.assertEqual(output.shape     ,       lowercase       )
           UpperCAmelCase     =	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=lowercase     ,       )

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

           self.assertTrue(lower_bound and upper_bound       )




