


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							:


     """simple docstring"""


     def __init__(	self  ,       __A  ,       __A=13  ,       __A=7  ,       __A=True  ,       __A=True  ,       __A=True  ,       __A=True  ,       __A=99  ,       __A=64  ,       __A=32  ,       __A=5  ,       __A=4  ,       __A=37  ,       __A="gelu"  ,       __A=0.1  ,       __A=0.1  ,       __A=512  ,       __A=16  ,       __A=2  ,       __A=0.02  ,       __A=3  ,       __A=4  ,       __A=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 	snake_case_  (	self    ):
        __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 	snake_case_  (	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=__A  ,       initializer_range=self.initializer_range  ,       )


     def 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        __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 	snake_case_  (	self    ):
        __a					=  self.prepare_config_and_inputs()
        (
            (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __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     __UpperCAmelCase							(       __A ,					__A ,					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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A=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 	snake_case_  (	self    ):
        __a					=  MobileBertModelTester(self    )
        __a					=  ConfigTester(self  ,       config_class=__A  ,       hidden_size=37    )


     def 	snake_case_  (	self    ):
        self.config_tester.run_common_tests()


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_model(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_masked_lm(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_pretraining(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_question_answering(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_token_classification(*__A    )







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


SCREAMING_SNAKE_CASE							    =							1E-3


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


     """simple docstring"""


     @slow
     def 	snake_case_  (	self    ):
        __a					=  MobileBertModel.from_pretrained("""google/mobilebert-uncased"""    ).to(__A    )
        __a					=  _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]    )
        with torch.no_grad():
           __a					=  model(__A    )[0]
        __a					=  torch.Size((1, 9, 512)    )
        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    )







import importlib
import shutil
import threading
import warnings
from typing import List

import fsspec
import fsspec.asyn

from . import compression
from .hffilesystem import HfFileSystem


SCREAMING_SNAKE_CASE							    =							importlib.util.find_spec('s3fs') is not None

if _has_safs:
  from .safilesystem import SaFileSystem  # noqa: F401

SCREAMING_SNAKE_CASE							    =							[
    compression.BzaFileSystem,
    compression.GzipFileSystem,
    compression.LzaFileSystem,
    compression.XzFileSystem,
    compression.ZstdFileSystem,
]

# Register custom filesystems
for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]:
  if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class:
    warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''')
  fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True)




def  a   (lowerCAmelCase__   ):
   if "://" in dataset_path:
      __a					=  dataset_path.split("""://"""   )[1]
   return dataset_path




def  a   (lowerCAmelCase__   ):
   if fs is not None and fs.protocol != "file":
      return True
   else:
      return False




def  a   (lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__   ):
   __a					=  not is_remote_filesystem(lowerCAmelCase__   )
   if is_local:
      # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory
      shutil.move(fs._strip_protocol(lowerCAmelCase__   )		,				fs._strip_protocol(lowerCAmelCase__   )   )
   else:
      fs.mv(lowerCAmelCase__		,				lowerCAmelCase__		,				recursive=lowerCAmelCase__   )




def  a   ():
   if hasattr(fsspec.asyn		,				"""reset_lock"""   ):
      # for future fsspec>2022.05.0
      fsspec.asyn.reset_lock()
   else:
      __a					=  None
      __a					=  None
      __a					=  threading.Lock()



