




"""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 lowerCAmelCase__	:






			'''simple docstring'''



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

							_lowerCamelCase     :			int    =    None
							if self.use_input_mask:
											_lowerCamelCase     :			Optional[Any]    =    random_attention_mask([self.batch_size, self.seq_length]  )

							_lowerCamelCase     :			Optional[Any]    =    None
							if self.use_token_type_ids:
											_lowerCamelCase     :			Union[str, Any]    =    ids_tensor([self.batch_size, self.seq_length]  ,      self.type_vocab_size  )

							_lowerCamelCase     :			Dict    =    None
							_lowerCamelCase     :			Any    =    None
							_lowerCamelCase     :			Union[str, Any]    =    None
							if self.use_labels:
											_lowerCamelCase     :			List[str]    =    ids_tensor([self.batch_size]  ,      self.type_sequence_label_size  )
											_lowerCamelCase     :			str    =    ids_tensor([self.batch_size, self.seq_length]  ,      self.num_labels  )
											_lowerCamelCase     :			Tuple    =    ids_tensor([self.batch_size]  ,      self.num_choices  )

							_lowerCamelCase     :			Any    =    self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
			def 					A_    (  self  ):
							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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Union[str, Any]    =    MobileBertModel(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[int]    =    model(lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  )
							_lowerCamelCase     :			Union[str, Any]    =    model(lowercase  ,      token_type_ids=lowercase  )
							_lowerCamelCase     :			List[str]    =    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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Union[str, Any]    =    MobileBertForMaskedLM(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[Any]    =    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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			List[Any]    =    MobileBertForNextSentencePrediction(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			List[str]    =    model(
							    lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  ,      )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, 2)  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			str    =    MobileBertForPreTraining(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			int    =    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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Any    =    MobileBertForQuestionAnswering(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			str    =    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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Optional[int]    =    self.num_labels
							_lowerCamelCase     :			List[str]    =    MobileBertForSequenceClassification(lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			str    =    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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Dict    =    self.num_labels
							_lowerCamelCase     :			Optional[int]    =    MobileBertForTokenClassification(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[Any]    =    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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			List[str]    =    self.num_choices
							_lowerCamelCase     :			Optional[int]    =    MobileBertForMultipleChoice(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[int]    =    input_ids.unsqueeze(1  ).expand(-1  ,      self.num_choices  ,      -1  ).contiguous()
							_lowerCamelCase     :			List[Any]    =    token_type_ids.unsqueeze(1  ).expand(-1  ,      self.num_choices  ,      -1  ).contiguous()
							_lowerCamelCase     :			List[str]    =    input_mask.unsqueeze(1  ).expand(-1  ,      self.num_choices  ,      -1  ).contiguous()
							_lowerCamelCase     :			Tuple    =    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  ):
							_lowerCamelCase     :			Tuple    =    self.prepare_config_and_inputs()
							(
							    (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), 
							)     :			Union[str, Any]    =    config_and_inputs
							_lowerCamelCase     :			Tuple    =    {'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'''



			lowerCamelCase__      =			(
			    (
			        MobileBertModel,
			        MobileBertForMaskedLM,
			        MobileBertForMultipleChoice,
			        MobileBertForNextSentencePrediction,
			        MobileBertForPreTraining,
			        MobileBertForQuestionAnswering,
			        MobileBertForSequenceClassification,
			        MobileBertForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__      =			(
			    {
			        """feature-extraction""": MobileBertModel,
			        """fill-mask""": MobileBertForMaskedLM,
			        """question-answering""": MobileBertForQuestionAnswering,
			        """text-classification""": MobileBertForSequenceClassification,
			        """token-classification""": MobileBertForTokenClassification,
			        """zero-shot""": MobileBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase__      =			True
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase=False  ):
							_lowerCamelCase     :			Optional[int]    =    super()._prepare_for_class(lowercase  ,      lowercase  ,      return_labels=lowercase  )

							if return_labels:
											if model_class in get_values(lowercase  ):
															_lowerCamelCase     :			Tuple    =    torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length)  ,      dtype=torch.long  ,      device=lowercase  )
															_lowerCamelCase     :			Dict    =    torch.zeros(
															    self.model_tester.batch_size  ,      dtype=torch.long  ,      device=lowercase  )
							return inputs_dict
			def 					A_    (  self  ):
							_lowerCamelCase     :			int    =    MobileBertModelTester(self  )
							_lowerCamelCase     :			Union[str, Any]    =    ConfigTester(self  ,      config_class=lowercase  ,      hidden_size=37  )
			def 					A_    (  self  ):
							self.config_tester.run_common_tests()
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[str]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			int    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[int]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[Any]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Union[str, Any]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Tuple    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[str]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase  )






			def 					A_    (  self  ):
							_lowerCamelCase     :			Dict    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase  )







def      _snake_case			(				lowercase__		):
				return torch.tensor(
				    lowercase__							,    dtype=torch.long							,    device=lowercase__							,    )


lowercase__     						=						1E-3






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






			'''simple docstring'''



			@slow
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[int]    =    MobileBertModel.from_pretrained('google/mobilebert-uncased'  ).to(lowercase  )
							_lowerCamelCase     :			Tuple    =    _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]  )
							with torch.no_grad():
											_lowerCamelCase     :			Any    =    model(lowercase  )[0]
							_lowerCamelCase     :			Tuple    =    torch.Size((1, 9, 512)  )
							self.assertEqual(output.shape  ,      lowercase  )
							_lowerCamelCase     :			List[Any]    =    torch.tensor(
							    [
							        [
							            [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05],
							            [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00],
							            [2.604_7359E00, 1.567_7652E00, -1.732_4188E-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
							_lowerCamelCase     :			Any    =    torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
							_lowerCamelCase     :			str    =    torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

							self.assertTrue(lower_bound and upper_bound  )





"""simple docstring"""






from __future__ import annotations

from collections.abc import Iterator
from typing import Any






class lowerCAmelCase__	:






			'''simple docstring'''



			def __init__(  self  ,      lowercase  ):
							_lowerCamelCase     :			Any    =    data
							_lowerCamelCase     :			Node | None    =    None






class lowerCAmelCase__	:






			'''simple docstring'''



			def __init__(  self  ):
							_lowerCamelCase     :			str    =    None
							_lowerCamelCase     :			str    =    None
			def __iter__(  self  ):
							_lowerCamelCase     :			List[str]    =    self.head
							while self.head:
											yield node.data
											_lowerCamelCase     :			Optional[int]    =    node.next
											if node == self.head:
															break
			def __len__(  self  ):
							return sum(1 for _ in self  )
			def __repr__(  self  ):
							return "->".join(str(lowercase  ) for item in iter(self  )  )
			def 					A_    (  self  ,      lowercase  ):
							self.insert_nth(len(self  )  ,      lowercase  )
			def 					A_    (  self  ,      lowercase  ):
							self.insert_nth(0  ,      lowercase  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ):
							if index < 0 or index > len(self  ):
											raise IndexError('list index out of range.'  )
							_lowerCamelCase     :			List[Any]    =    Node(lowercase  )
							if self.head is None:
											_lowerCamelCase     :			str    =    new_node  # first node points itself
											_lowerCamelCase     :			Union[str, Any]    =    new_node
							elif index == 0:  # insert at head
											_lowerCamelCase     :			List[str]    =    self.head
											_lowerCamelCase     :			str    =    new_node
							else:
											_lowerCamelCase     :			Union[str, Any]    =    self.head
											for _ in range(index - 1  ):
															_lowerCamelCase     :			List[Any]    =    temp.next
											_lowerCamelCase     :			Union[str, Any]    =    temp.next
											_lowerCamelCase     :			List[str]    =    new_node
											if index == len(self  ) - 1:  # insert at tail
															_lowerCamelCase     :			Any    =    new_node
			def 					A_    (  self  ):
							return self.delete_nth(0  )
			def 					A_    (  self  ):
							return self.delete_nth(len(self  ) - 1  )
			def 					A_    (  self  ,      lowercase = 0  ):
							if not 0 <= index < len(self  ):
											raise IndexError('list index out of range.'  )
							_lowerCamelCase     :			Any    =    self.head
							if self.head == self.tail:  # just one node
											_lowerCamelCase     :			List[str]    =    None
							elif index == 0:  # delete head node
											_lowerCamelCase     :			List[str]    =    self.tail.next.next
											_lowerCamelCase     :			Optional[int]    =    self.head.next
							else:
											_lowerCamelCase     :			Dict    =    self.head
											for _ in range(index - 1  ):
															_lowerCamelCase     :			List[Any]    =    temp.next
											_lowerCamelCase     :			int    =    temp.next
											_lowerCamelCase     :			Optional[int]    =    temp.next.next
											if index == len(self  ) - 1:  # delete at tail
															_lowerCamelCase     :			List[Any]    =    temp
							return delete_node.data






			def 					A_    (  self  ):
							return len(self  ) == 0







def      _snake_case			(				):
				_lowerCamelCase     :			Union[str, Any]    =    CircularLinkedList()
				assert len(lowercase__		) == 0
				assert circular_linked_list.is_empty() is True
				assert str(lowercase__		) == ""

				try:
								circular_linked_list.delete_front()
								raise AssertionError  # This should not happen
				except IndexError:
								assert True  # This should happen

				try:
								circular_linked_list.delete_tail()
								raise AssertionError  # This should not happen
				except IndexError:
								assert True  # This should happen

				try:
								circular_linked_list.delete_nth(-1		)
								raise AssertionError
				except IndexError:
								assert True

				try:
								circular_linked_list.delete_nth(0		)
								raise AssertionError
				except IndexError:
								assert True

				assert circular_linked_list.is_empty() is True
				for i in range(5		):
								assert len(lowercase__		) == i
								circular_linked_list.insert_nth(lowercase__							,    i + 1		)
				assert str(lowercase__		) == "->".join(str(lowercase__		) for i in range(1							,    6		)		)

				circular_linked_list.insert_tail(6		)
				assert str(lowercase__		) == "->".join(str(lowercase__		) for i in range(1							,    7		)		)
				circular_linked_list.insert_head(0		)
				assert str(lowercase__		) == "->".join(str(lowercase__		) for i in range(0							,    7		)		)

				assert circular_linked_list.delete_front() == 0
				assert circular_linked_list.delete_tail() == 6
				assert str(lowercase__		) == "->".join(str(lowercase__		) for i in range(1							,    6		)		)
				assert circular_linked_list.delete_nth(2		) == 3

				circular_linked_list.insert_nth(2							,    3		)
				assert str(lowercase__		) == "->".join(str(lowercase__		) for i in range(1							,    6		)		)

				assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
			import doctest

			doctest.testmod()