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[Any]  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]=13  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]=7  ,				__SCREAMING_SNAKE_CASE			:   Any=True  ,				__SCREAMING_SNAKE_CASE			:   List[str]=True  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]=True  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]=True  ,				__SCREAMING_SNAKE_CASE			:   int=99  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]=64  ,				__SCREAMING_SNAKE_CASE			:   str=32  ,				__SCREAMING_SNAKE_CASE			:   Dict=5  ,				__SCREAMING_SNAKE_CASE			:   str=4  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]=37  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]="gelu"  ,				__SCREAMING_SNAKE_CASE			:   Dict=0.1  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]=0.1  ,				__SCREAMING_SNAKE_CASE			:   List[str]=5_12  ,				__SCREAMING_SNAKE_CASE			:   List[Any]=16  ,				__SCREAMING_SNAKE_CASE			:   str=2  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]=0.02  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]=3  ,				__SCREAMING_SNAKE_CASE			:   Tuple=4  ,				__SCREAMING_SNAKE_CASE			:   List[Any]=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			:   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			:   str							):
   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			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   Dict							):
   __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			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Any  ,				__SCREAMING_SNAKE_CASE			:   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			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   str							):
   __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			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   int  ,				__SCREAMING_SNAKE_CASE			:   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			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Tuple							):
   __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			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   Any  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]							):
   __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			:   Any  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]  ,				__SCREAMING_SNAKE_CASE			:   Tuple							):
   __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			:   str  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Any  ,				__SCREAMING_SNAKE_CASE			:   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			:   str							):
   __a							=	self.prepare_config_and_inputs()
   (
       __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  A_			(							__snake_case      ,							__snake_case      ,							unittest.TestCase			):
  _SCREAMING_SNAKE_CASE     					=      (
      (
          MobileBertModel,
          MobileBertForMaskedLM,
          MobileBertForMultipleChoice,
          MobileBertForNextSentencePrediction,
          MobileBertForPreTraining,
          MobileBertForQuestionAnswering,
          MobileBertForSequenceClassification,
          MobileBertForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  _SCREAMING_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 {}
  )
  _SCREAMING_SNAKE_CASE     					=      True

  def 		_UpperCAmelCase (		self			:   Union[str, Any]  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   Tuple=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			:   Tuple							):
   __a							=	MobileBertModelTester(self							)
   __a							=	ConfigTester(self  ,				config_class=A_  ,				hidden_size=37							)

  def 		_UpperCAmelCase (		self			:   str							):
   self.config_tester.run_common_tests()

  def 		_UpperCAmelCase (		self			:   str							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_model(*A_							)

  def 		_UpperCAmelCase (		self			:   int							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_masked_lm(*A_							)

  def 		_UpperCAmelCase (		self			:   str							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A_							)

  def 		_UpperCAmelCase (		self			:   Optional[int]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A_							)

  def 		_UpperCAmelCase (		self			:   int							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_pretraining(*A_							)

  def 		_UpperCAmelCase (		self			:   Union[str, Any]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_question_answering(*A_							)

  def 		_UpperCAmelCase (		self			:   Optional[int]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A_							)

  def 		_UpperCAmelCase (		self			:   Optional[Any]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_token_classification(*A_							)



def 	__A		(      _A       ):



 """simple docstring"""


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


SCREAMING_SNAKE_CASE			:						List[Any]				  =		1e-3






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

  @slow
  def 		_UpperCAmelCase (		self			:   str							):
   __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.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=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							)









# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

__lowerCamelCase			:	str					    =	TypeVar("""T""")



class    A__  (			Generic[T]     ):





      def __init__(					self			,				A_ = True  ):
       '''simple docstring'''




       UpperCamelCase      :   dict[T, list[T]]			      =  {}  # dictionary of lists
       UpperCamelCase      :   Union[str, Any]			      =  directed





      def 			__UpperCamelCase(					self			,				A_			,				A_  ):
       '''simple docstring'''




       if not self.directed:  # For undirected graphs
        # if both source vertex and destination vertex are both present in the
        # adjacency list, add destination vertex to source vertex list of adjacent
        # vertices and add source vertex to destination vertex list of adjacent
        # vertices.
        if source_vertex in self.adj_list and destination_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_  )
         self.adj_list[destination_vertex].append(A_  )
        # if only source vertex is present in adjacency list, add destination vertex
        # to source vertex list of adjacent vertices, then create a new vertex with
        # destination vertex as key and assign a list containing the source vertex
        # as it's first adjacent vertex.
        elif source_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_  )
         UpperCamelCase      :   Any			      =  [source_vertex]
        # if only destination vertex is present in adjacency list, add source vertex
        # to destination vertex list of adjacent vertices, then create a new vertex
        # with source vertex as key and assign a list containing the source vertex
        # as it's first adjacent vertex.
        elif destination_vertex in self.adj_list:
         self.adj_list[destination_vertex].append(A_  )
         UpperCamelCase      :   Any			      =  [destination_vertex]
        # if both source vertex and destination vertex are not present in adjacency
        # list, create a new vertex with source vertex as key and assign a list
        # containing the destination vertex as it's first adjacent vertex also
        # create a new vertex with destination vertex as key and assign a list
        # containing the source vertex as it's first adjacent vertex.
        else:
         UpperCamelCase      :   Union[str, Any]			      =  [destination_vertex]
         UpperCamelCase      :   List[str]			      =  [source_vertex]
       else:  # For directed graphs
        # if both source vertex and destination vertex are present in adjacency
        # list, add destination vertex to source vertex list of adjacent vertices.
        if source_vertex in self.adj_list and destination_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_  )
        # if only source vertex is present in adjacency list, add destination
        # vertex to source vertex list of adjacent vertices and create a new vertex
        # with destination vertex as key, which has no adjacent vertex
        elif source_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_  )
         UpperCamelCase      :   List[str]			      =  []
        # if only destination vertex is present in adjacency list, create a new
        # vertex with source vertex as key and assign a list containing destination
        # vertex as first adjacent vertex
        elif destination_vertex in self.adj_list:
         UpperCamelCase      :   Union[str, Any]			      =  [destination_vertex]
        # if both source vertex and destination vertex are not present in adjacency
        # list, create a new vertex with source vertex as key and a list containing
        # destination vertex as it's first adjacent vertex. Then create a new vertex
        # with destination vertex as key, which has no adjacent vertex
        else:
         UpperCamelCase      :   Union[str, Any]			      =  [destination_vertex]
         UpperCamelCase      :   Dict			      =  []

       return self





      def __repr__(					self  ):
       '''simple docstring'''




       return pformat(self.adj_list  )
