
import unittest

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

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


if is_torch_available():
      import torch

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



class 		UpperCamelCase__  :
   def __init__(self			: int ,    snake_case_			: Tuple ,    snake_case_			: Union[str, Any]=1_3 ,    snake_case_			: Any=7 ,    snake_case_			: List[str]=True ,    snake_case_			: Optional[int]=True ,    snake_case_			: Any=True ,    snake_case_			: Tuple=True ,    snake_case_			: Optional[Any]=9_9 ,    snake_case_			: Dict=6_4 ,    snake_case_			: str=3_2 ,    snake_case_			: str=5 ,    snake_case_			: List[str]=4 ,    snake_case_			: str=3_7 ,    snake_case_			: List[Any]="gelu" ,    snake_case_			: List[str]=0.1 ,    snake_case_			: List[Any]=0.1 ,    snake_case_			: int=5_1_2 ,    snake_case_			: Any=1_6 ,    snake_case_			: str=2 ,    snake_case_			: Dict=0.02 ,    snake_case_			: List[str]=3 ,    snake_case_			: List[str]=4 ,    snake_case_			: str=None ,    ):
    __a			:  Optional[int]		    =  parent
    __a			:  Optional[Any]		    =  batch_size
    __a			:  Optional[int]		    =  seq_length
    __a			:  Tuple		    =  is_training
    __a			:  Dict		    =  use_input_mask
    __a			:  Union[str, Any]		    =  use_token_type_ids
    __a			:  Union[str, Any]		    =  use_labels
    __a			:  Optional[Any]		    =  vocab_size
    __a			:  List[str]		    =  hidden_size
    __a			:  int		    =  embedding_size
    __a			:  Any		    =  num_hidden_layers
    __a			:  Union[str, Any]		    =  num_attention_heads
    __a			:  Any		    =  intermediate_size
    __a			:  Any		    =  hidden_act
    __a			:  List[str]		    =  hidden_dropout_prob
    __a			:  List[Any]		    =  attention_probs_dropout_prob
    __a			:  List[str]		    =  max_position_embeddings
    __a			:  Tuple		    =  type_vocab_size
    __a			:  Optional[Any]		    =  type_sequence_label_size
    __a			:  int		    =  initializer_range
    __a			:  Optional[int]		    =  num_labels
    __a			:  Optional[Any]		    =  num_choices
    __a			:  int		    =  scope
   def 				lowerCAmelCase      (self			: Union[str, Any]       ):
    __a			:  Optional[Any]		    =  ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size       )

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

    __a			:  List[Any]		    =  None
    if self.use_token_type_ids:
     __a			:  str		    =  ids_tensor([self.batch_size, self.seq_length] ,    self.type_vocab_size       )

    __a			:  str		    =  None
    __a			:  str		    =  None
    __a			:  Optional[Any]		    =  None
    if self.use_labels:
     __a			:  str		    =  ids_tensor([self.batch_size] ,    self.type_sequence_label_size       )
     __a			:  Any		    =  ids_tensor([self.batch_size, self.seq_length] ,    self.num_labels       )
     __a			:  Optional[int]		    =  ids_tensor([self.batch_size] ,    self.num_choices       )

    __a			:  Dict		    =  self.get_config()

    return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def 				lowerCAmelCase      (self			: Union[str, Any]       ):
    return MobileBertConfig(
        vocab_size=self.vocab_size ,    hidden_size=self.hidden_size ,    num_hidden_layers=self.num_hidden_layers ,    num_attention_heads=self.num_attention_heads ,    intermediate_size=self.intermediate_size ,    embedding_size=self.embedding_size ,    hidden_act=self.hidden_act ,    hidden_dropout_prob=self.hidden_dropout_prob ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob ,    max_position_embeddings=self.max_position_embeddings ,    type_vocab_size=self.type_vocab_size ,    is_decoder=snake_case_ ,    initializer_range=self.initializer_range ,    )
   def 				lowerCAmelCase      (self			: int ,    snake_case_			: Tuple ,    snake_case_			: Union[str, Any] ,    snake_case_			: Optional[Any] ,    snake_case_			: List[str] ,    snake_case_			: Optional[int] ,    snake_case_			: Union[str, Any] ,    snake_case_			: Any       ):
    __a			:  List[Any]		    =  MobileBertModel(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Dict		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_       )
    __a			:  Union[str, Any]		    =  model(snake_case_ ,    token_type_ids=snake_case_       )
    __a			:  Tuple		    =  model(snake_case_       )

    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 				lowerCAmelCase      (self			: Dict ,    snake_case_			: Dict ,    snake_case_			: Any ,    snake_case_			: Dict ,    snake_case_			: Any ,    snake_case_			: Any ,    snake_case_			: Dict ,    snake_case_			: List[str]       ):
    __a			:  str		    =  MobileBertForMaskedLM(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Tuple		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_       )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)       )
   def 				lowerCAmelCase      (self			: str ,    snake_case_			: int ,    snake_case_			: int ,    snake_case_			: List[Any] ,    snake_case_			: Any ,    snake_case_			: List[str] ,    snake_case_			: Any ,    snake_case_			: Optional[Any]       ):
    __a			:  List[str]		    =  MobileBertForNextSentencePrediction(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  str		    =  model(
        snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_ ,    )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, 2)       )
   def 				lowerCAmelCase      (self			: Optional[Any] ,    snake_case_			: Union[str, Any] ,    snake_case_			: List[str] ,    snake_case_			: Any ,    snake_case_			: List[str] ,    snake_case_			: Optional[Any] ,    snake_case_			: Any ,    snake_case_			: int       ):
    __a			:  Tuple		    =  MobileBertForPreTraining(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Optional[int]		    =  model(
        snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_ ,    next_sentence_label=snake_case_ ,    )
    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 				lowerCAmelCase      (self			: Union[str, Any] ,    snake_case_			: List[str] ,    snake_case_			: List[str] ,    snake_case_			: List[Any] ,    snake_case_			: Any ,    snake_case_			: List[str] ,    snake_case_			: int ,    snake_case_			: Dict       ):
    __a			:  Optional[int]		    =  MobileBertForQuestionAnswering(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  List[str]		    =  model(
        snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    start_positions=snake_case_ ,    end_positions=snake_case_ ,    )
    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 				lowerCAmelCase      (self			: str ,    snake_case_			: Optional[Any] ,    snake_case_			: str ,    snake_case_			: Optional[int] ,    snake_case_			: Optional[int] ,    snake_case_			: List[str] ,    snake_case_			: str ,    snake_case_			: Union[str, Any]       ):
    __a			:  Tuple		    =  self.num_labels
    __a			:  Tuple		    =  MobileBertForSequenceClassification(snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Optional[Any]		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_       )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_labels)       )
   def 				lowerCAmelCase      (self			: str ,    snake_case_			: int ,    snake_case_			: Tuple ,    snake_case_			: str ,    snake_case_			: Union[str, Any] ,    snake_case_			: List[Any] ,    snake_case_			: int ,    snake_case_			: Optional[Any]       ):
    __a			:  List[Any]		    =  self.num_labels
    __a			:  Tuple		    =  MobileBertForTokenClassification(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Optional[Any]		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_       )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.num_labels)       )
   def 				lowerCAmelCase      (self			: str ,    snake_case_			: Optional[Any] ,    snake_case_			: Optional[Any] ,    snake_case_			: Optional[int] ,    snake_case_			: Union[str, Any] ,    snake_case_			: Dict ,    snake_case_			: Optional[Any] ,    snake_case_			: Any       ):
    __a			:  int		    =  self.num_choices
    __a			:  int		    =  MobileBertForMultipleChoice(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Union[str, Any]		    =  input_ids.unsqueeze(1       ).expand(-1 ,    self.num_choices ,    -1       ).contiguous()
    __a			:  Optional[Any]		    =  token_type_ids.unsqueeze(1       ).expand(-1 ,    self.num_choices ,    -1       ).contiguous()
    __a			:  int		    =  input_mask.unsqueeze(1       ).expand(-1 ,    self.num_choices ,    -1       ).contiguous()
    __a			:  str		    =  model(
        snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_ ,    )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_choices)       )







   def 				lowerCAmelCase      (self			: List[str]       ):
    __a			:  str		    =  self.prepare_config_and_inputs()
    (
        (
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	
    )			:  List[Any]		    =  config_and_inputs
    __a			:  str		    =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict





@require_torch
class 		UpperCamelCase__  (	__lowercase   ,__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							Tuple     	=				(
       (
           MobileBertModel,
           MobileBertForMaskedLM,
           MobileBertForMultipleChoice,
           MobileBertForNextSentencePrediction,
           MobileBertForPreTraining,
           MobileBertForQuestionAnswering,
           MobileBertForSequenceClassification,
           MobileBertForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE				:							Optional[int]     	=				(
       {
           "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				:							List[str]     	=				True
   def 				lowerCAmelCase      (self			: int ,    snake_case_			: str ,    snake_case_			: str ,    snake_case_			: Any=False       ):
    __a			:  List[Any]		    =  super()._prepare_for_class(snake_case_ ,    snake_case_ ,    return_labels=snake_case_       )

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







   def 				lowerCAmelCase      (self			: str       ):
    __a			:  Union[str, Any]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case_       )





def        __UpperCamelCase					(    lowerCAmelCase__							:    Any  ):
 return torch.tensor(
     lowerCAmelCase__  ,       dtype=torch.long  ,       device=lowerCAmelCase__  ,       )


lowercase__          =1e-3



@require_torch
@require_sentencepiece
@require_tokenizers
class 		UpperCamelCase__  (	unittest.TestCase   ):
   @slow
   def 				lowerCAmelCase      (self			: Dict       ):
    __a			:  Tuple		    =  MobileBertModel.from_pretrained('''google/mobilebert-uncased'''       ).to(snake_case_       )
    __a			:  str		    =  _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():
     __a			:  int		    =  model(snake_case_       )[0]
    __a			:  int		    =  torch.Size((1, 9, 5_1_2)       )
    self.assertEqual(output.shape ,    snake_case_       )
    __a			:  Optional[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=snake_case_ ,    )

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

    self.assertTrue(lower_bound and upper_bound       )


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


lowercase__          ={
    'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'],
    'processing_layoutlmv2': ['LayoutLMv2Processor'],
    'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__          =['LayoutLMv2TokenizerFast']

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__          =['LayoutLMv2FeatureExtractor']
      lowercase__          =['LayoutLMv2ImageProcessor']

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__          =[
          'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST',
          'LayoutLMv2ForQuestionAnswering',
          'LayoutLMv2ForSequenceClassification',
          'LayoutLMv2ForTokenClassification',
          'LayoutLMv2Layer',
          'LayoutLMv2Model',
          'LayoutLMv2PreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig
      from .processing_layoutlmva import LayoutLMvaProcessor
      from .tokenization_layoutlmva import LayoutLMvaTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_layoutlmva import (
                LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST,
                LayoutLMvaForQuestionAnswering,
                LayoutLMvaForSequenceClassification,
                LayoutLMvaForTokenClassification,
                LayoutLMvaLayer,
                LayoutLMvaModel,
                LayoutLMvaPreTrainedModel,
            )
else:
      import sys

      lowercase__          =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
