

import inspect
import tempfile
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack, contextmanager
from dataclasses import fields
from enum import Enum
from typing import Any, ContextManager, List, Tuple

import numpy as np

from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy


if is_flax_available():
       import jax.numpy as jnp






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  def __get__(							self		:	str			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	str=None      )	->							List[str]:
       # See docs.python.org/3/howto/descriptor.html#properties
       if obj is None:
            return self
       if self.fget is None:
            raise AttributeError('''unreadable attribute'''      )
       SCREAMING_SNAKE_CASE__					=					'''__cached_''' + self.fget.__name__
       SCREAMING_SNAKE_CASE__					=					getattr(__lowerCamelCase			,     __lowerCamelCase			,     __lowerCamelCase      )
       if cached is None:
            SCREAMING_SNAKE_CASE__					=					self.fget(__lowerCamelCase      )
            setattr(__lowerCamelCase			,     __lowerCamelCase			,     __lowerCamelCase      )
       return cached



def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					val.lower()
     if val in {"y", "yes", "t", "true", "on", "1"}:
          return 1
     if val in {"n", "no", "f", "false", "off", "0"}:
          return 0
     raise ValueError(F'''invalid truth value {val!r}'''       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if is_torch_fx_proxy(_A       ):
          return True
     if is_torch_available():
          import torch

          if isinstance(_A   ,	torch.Tensor       ):
               return True
     if is_tf_available():
          import tensorflow as tf

          if isinstance(_A   ,	tf.Tensor       ):
               return True

     if is_flax_available():
          import jax.numpy as jnp
          from jax.core import Tracer

          if isinstance(_A   ,	(jnp.ndarray, Tracer)       ):
               return True

     return isinstance(_A   ,	np.ndarray       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return isinstance(_A   ,	np.ndarray       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return _is_numpy(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     import torch

     return isinstance(_A   ,	torch.Tensor       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return False if not is_torch_available() else _is_torch(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     import torch

     return isinstance(_A   ,	torch.device       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return False if not is_torch_available() else _is_torch_device(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     import torch

     if isinstance(_A   ,	_A       ):
          if hasattr(_A   ,	_A       ):
               SCREAMING_SNAKE_CASE__					=					getattr(_A   ,	_A       )
          else:
               return False
     return isinstance(_A   ,	torch.dtype       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return False if not is_torch_available() else _is_torch_dtype(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     import tensorflow as tf

     return isinstance(_A   ,	tf.Tensor       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return False if not is_tf_available() else _is_tensorflow(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     import tensorflow as tf

     # the `is_symbolic_tensor` predicate is only available starting with TF 2.14
     if hasattr(_A   ,	'''is_symbolic_tensor'''       ):
          return tf.is_symbolic_tensor(_A       )
     return type(_A       ) == tf.Tensor
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return False if not is_tf_available() else _is_tf_symbolic_tensor(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     import jax.numpy as jnp  # noqa: F811

     return isinstance(_A   ,	jnp.ndarray       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     return False if not is_flax_available() else _is_jax(_A       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if isinstance(_A   ,	(dict, UserDict)       ):
          return {k: to_py_obj(_A       ) for k, v in obj.items()}
     elif isinstance(_A   ,	(list, tuple)       ):
          return [to_py_obj(_A       ) for o in obj]
     elif is_tf_tensor(_A       ):
          return obj.numpy().tolist()
     elif is_torch_tensor(_A       ):
          return obj.detach().cpu().tolist()
     elif is_jax_tensor(_A       ):
          return np.asarray(_A       ).tolist()
     elif isinstance(_A   ,	(np.ndarray, np.number)       ):  # tolist also works on 0d np arrays
          return obj.tolist()
     else:
          return obj
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if isinstance(_A   ,	(dict, UserDict)       ):
          return {k: to_numpy(_A       ) for k, v in obj.items()}
     elif isinstance(_A   ,	(list, tuple)       ):
          return np.array(_A       )
     elif is_tf_tensor(_A       ):
          return obj.numpy()
     elif is_torch_tensor(_A       ):
          return obj.detach().cpu().numpy()
     elif is_jax_tensor(_A       ):
          return np.asarray(_A       )
     else:
          return obj






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  def    lowercase_	(							self		:	List[Any]      )	->							Dict:
       SCREAMING_SNAKE_CASE__					=					fields(self      )

       # Safety and consistency checks
       if not len(__lowerCamelCase      ):
            raise ValueError(f'''{self.__class__.__name__} has no fields.'''      )
       if not all(field.default is None for field in class_fields[1:]      ):
            raise ValueError(f'''{self.__class__.__name__} should not have more than one required field.'''      )

       SCREAMING_SNAKE_CASE__					=					getattr(self			,     class_fields[0].name      )
       SCREAMING_SNAKE_CASE__					=					all(getattr(self			,     field.name      ) is None for field in class_fields[1:]      )

       if other_fields_are_none and not is_tensor(__lowerCamelCase      ):
            if isinstance(__lowerCamelCase			,     __lowerCamelCase      ):
                 SCREAMING_SNAKE_CASE__					=					first_field.items()
                 SCREAMING_SNAKE_CASE__					=					True
            else:
                 try:
                      SCREAMING_SNAKE_CASE__					=					iter(__lowerCamelCase      )
                      SCREAMING_SNAKE_CASE__					=					True
                 except TypeError:
                      SCREAMING_SNAKE_CASE__					=					False

            # if we provided an iterator as first field and the iterator is a (key, value) iterator
            # set the associated fields
            if first_field_iterator:
                 for idx, element in enumerate(__lowerCamelCase      ):
                      if (
                          not isinstance(__lowerCamelCase			,     (list, tuple)      )
                          or not len(__lowerCamelCase      ) == 2
                          or not isinstance(element[0]			,     __lowerCamelCase      )
                      ):
                           if idx == 0:
                                # If we do not have an iterator of key/values, set it as attribute
                                SCREAMING_SNAKE_CASE__					=					first_field
                           else:
                                # If we have a mixed iterator, raise an error
                                raise ValueError(
                                    f'''Cannot set key/value for {element}. It needs to be a tuple (key, value).'''      )
                           break
                      setattr(self			,     element[0]			,     element[1]      )
                      if element[1] is not None:
                           SCREAMING_SNAKE_CASE__					=					element[1]
            elif first_field is not None:
                 SCREAMING_SNAKE_CASE__					=					first_field
       else:
            for field in class_fields:
                 SCREAMING_SNAKE_CASE__					=					getattr(self			,     field.name      )
                 if v is not None:
                      SCREAMING_SNAKE_CASE__					=					v






  def __delitem__(							self		:	Union[str, Any]			,     *__lowerCamelCase		:	Tuple			,     **__lowerCamelCase		:	Optional[Any]      )	->							Any:
       raise Exception(f'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''      )






  def    lowercase_	(							self		:	List[Any]			,     *__lowerCamelCase		:	List[Any]			,     **__lowerCamelCase		:	str      )	->							str:
       raise Exception(f'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''      )






  def    lowercase_	(							self		:	Optional[int]			,     *__lowerCamelCase		:	int			,     **__lowerCamelCase		:	Dict      )	->							List[str]:
       raise Exception(f'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''      )






  def    lowercase_	(							self		:	Optional[int]			,     *__lowerCamelCase		:	Any			,     **__lowerCamelCase		:	List[Any]      )	->							Union[str, Any]:
       raise Exception(f'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''      )






  def __getitem__(							self		:	Tuple			,     __lowerCamelCase		:	Tuple      )	->							List[Any]:
       if isinstance(__lowerCamelCase			,     __lowerCamelCase      ):
            SCREAMING_SNAKE_CASE__					=					dict(self.items()      )
            return inner_dict[k]
       else:
            return self.to_tuple()[k]






  def __setattr__(							self		:	Optional[Any]			,     __lowerCamelCase		:	Optional[int]			,     __lowerCamelCase		:	Dict      )	->							Dict:
       if name in self.keys() and value is not None:
            # Don't call self.__setitem__ to avoid recursion errors
            super().__setitem__(__lowerCamelCase			,     __lowerCamelCase      )
       super().__setattr__(__lowerCamelCase			,     __lowerCamelCase      )






  def __setitem__(							self		:	str			,     __lowerCamelCase		:	Union[str, Any]			,     __lowerCamelCase		:	Dict      )	->							int:
       # Will raise a KeyException if needed
       super().__setitem__(__lowerCamelCase			,     __lowerCamelCase      )
       # Don't call self.__setattr__ to avoid recursion errors
       super().__setattr__(__lowerCamelCase			,     __lowerCamelCase      )







  def    lowercase_	(							self		:	Any      )	->							Tuple[Any]:
       return tuple(self[k] for k in self.keys()      )






class  UpperCAmelCase__	( A__		,  A__ ):






  """simple docstring"""


  @classmethod
  def    lowercase_	(							cls		:	Tuple			,     __lowerCamelCase		:	int      )	->							List[str]:
       raise ValueError(
           f'''{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys()      )}'''      )






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  a     =      "longest"
  a     =      "max_length"
  a     =      "do_not_pad"






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  a     =      "pt"
  a     =      "tf"
  a     =      "np"
  a     =      "jax"






class  UpperCAmelCase__	:






  """simple docstring"""


  def __init__(							self		:	Optional[int]			,     __lowerCamelCase		:	List[ContextManager]      )	->							List[str]:
       SCREAMING_SNAKE_CASE__					=					context_managers
       SCREAMING_SNAKE_CASE__					=					ExitStack()






  def __enter__(							self		:	int      )	->							List[Any]:
       for context_manager in self.context_managers:
            self.stack.enter_context(__lowerCamelCase      )







  def __exit__(							self		:	int			,     *__lowerCamelCase		:	Union[str, Any]			,     **__lowerCamelCase		:	Optional[int]      )	->							int:
       self.stack.__exit__(*__lowerCamelCase			,     **__lowerCamelCase      )



def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					infer_framework(_A       )
     if framework == "tf":
          SCREAMING_SNAKE_CASE__					=					inspect.signature(model_class.call       )  # TensorFlow models
     elif framework == "pt":
          SCREAMING_SNAKE_CASE__					=					inspect.signature(model_class.forward       )  # PyTorch models
     else:
          SCREAMING_SNAKE_CASE__					=					inspect.signature(model_class.__call__       )  # Flax models

     for p in signature.parameters:
          if p == "return_loss" and signature.parameters[p].default is True:
               return True

     return False
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					model_class.__name__
     SCREAMING_SNAKE_CASE__					=					infer_framework(_A       )
     if framework == "tf":
          SCREAMING_SNAKE_CASE__					=					inspect.signature(model_class.call       )  # TensorFlow models
     elif framework == "pt":
          SCREAMING_SNAKE_CASE__					=					inspect.signature(model_class.forward       )  # PyTorch models
     else:
          SCREAMING_SNAKE_CASE__					=					inspect.signature(model_class.__call__       )  # Flax models

     if "QuestionAnswering" in model_name:
          return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")]
     else:
          return [p for p in signature.parameters if "label" in p]
def        UpperCAmelCase_     (		_A   ,	_A = ""   ,	_A = "."       ):





     '''simple docstring'''




     def _flatten_dict(_A   ,	_A=""   ,	_A="."       ):
          for k, v in d.items():
               SCREAMING_SNAKE_CASE__					=					str(_A       ) + delimiter + str(_A       ) if parent_key else k
               if v and isinstance(_A   ,	_A       ):
                    yield from flatten_dict(_A   ,	_A   ,	delimiter=_A       ).items()
               else:
                    yield key, v

     return dict(_flatten_dict(_A   ,	_A   ,	_A       )       )
@contextmanager
def        UpperCAmelCase_     (		_A   ,	_A = False       ):





     '''simple docstring'''




     if use_temp_dir:
          with tempfile.TemporaryDirectory() as tmp_dir:
               yield tmp_dir
     else:
          yield working_dir
def        UpperCAmelCase_     (		_A   ,	_A=None       ):





     '''simple docstring'''




     if is_numpy_array(_A       ):
          return np.transpose(_A   ,	axes=_A       )
     elif is_torch_tensor(_A       ):
          return array.T if axes is None else array.permute(*_A       )
     elif is_tf_tensor(_A       ):
          import tensorflow as tf

          return tf.transpose(_A   ,	perm=_A       )
     elif is_jax_tensor(_A       ):
          return jnp.transpose(_A   ,	axes=_A       )
     else:
          raise ValueError(F'''Type not supported for transpose: {type(_A       )}.'''       )
def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     if is_numpy_array(_A       ):
          return np.reshape(_A   ,	_A       )
     elif is_torch_tensor(_A       ):
          return array.reshape(*_A       )
     elif is_tf_tensor(_A       ):
          import tensorflow as tf

          return tf.reshape(_A   ,	_A       )
     elif is_jax_tensor(_A       ):
          return jnp.reshape(_A   ,	_A       )
     else:
          raise ValueError(F'''Type not supported for reshape: {type(_A       )}.'''       )
def        UpperCAmelCase_     (		_A   ,	_A=None       ):





     '''simple docstring'''




     if is_numpy_array(_A       ):
          return np.squeeze(_A   ,	axis=_A       )
     elif is_torch_tensor(_A       ):
          return array.squeeze() if axis is None else array.squeeze(dim=_A       )
     elif is_tf_tensor(_A       ):
          import tensorflow as tf

          return tf.squeeze(_A   ,	axis=_A       )
     elif is_jax_tensor(_A       ):
          return jnp.squeeze(_A   ,	axis=_A       )
     else:
          raise ValueError(F'''Type not supported for squeeze: {type(_A       )}.'''       )
def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     if is_numpy_array(_A       ):
          return np.expand_dims(_A   ,	_A       )
     elif is_torch_tensor(_A       ):
          return array.unsqueeze(dim=_A       )
     elif is_tf_tensor(_A       ):
          import tensorflow as tf

          return tf.expand_dims(_A   ,	axis=_A       )
     elif is_jax_tensor(_A       ):
          return jnp.expand_dims(_A   ,	axis=_A       )
     else:
          raise ValueError(F'''Type not supported for expand_dims: {type(_A       )}.'''       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if is_numpy_array(_A       ):
          return np.size(_A       )
     elif is_torch_tensor(_A       ):
          return array.numel()
     elif is_tf_tensor(_A       ):
          import tensorflow as tf

          return tf.size(_A       )
     elif is_jax_tensor(_A       ):
          return array.size
     else:
          raise ValueError(F'''Type not supported for expand_dims: {type(_A       )}.'''       )
def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     for key, value in auto_map.items():
          if isinstance(_A   ,	(tuple, list)       ):
               SCREAMING_SNAKE_CASE__					=					[F'''{repo_id}--{v}''' if (v is not None and '''--''' not in v) else v for v in value]
          elif value is not None and "--" not in value:
               SCREAMING_SNAKE_CASE__					=					F'''{repo_id}--{value}'''

     return auto_map







def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     for base_class in inspect.getmro(_A       ):
          SCREAMING_SNAKE_CASE__					=					base_class.__module__
          SCREAMING_SNAKE_CASE__					=					base_class.__name__
          if module.startswith('''tensorflow'''       ) or module.startswith('''keras'''       ) or name == "TFPreTrainedModel":
               return "tf"
          elif module.startswith('''torch'''       ) or name == "PreTrainedModel":
               return "pt"
          elif module.startswith('''flax'''       ) or module.startswith('''jax'''       ) or name == "FlaxPreTrainedModel":
               return "flax"
     else:
          raise TypeError(F'''Could not infer framework from class {model_class}.'''       )





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		:	Optional[int]			,     __lowerCamelCase		:	Tuple			,     __lowerCamelCase		:	List[str]=13			,     __lowerCamelCase		:	Any=7			,     __lowerCamelCase		:	str=True			,     __lowerCamelCase		:	Tuple=True			,     __lowerCamelCase		:	Tuple=True			,     __lowerCamelCase		:	str=True			,     __lowerCamelCase		:	int=99			,     __lowerCamelCase		:	Optional[int]=64			,     __lowerCamelCase		:	Dict=32			,     __lowerCamelCase		:	Dict=5			,     __lowerCamelCase		:	Optional[Any]=4			,     __lowerCamelCase		:	Optional[Any]=37			,     __lowerCamelCase		:	Dict="gelu"			,     __lowerCamelCase		:	Any=0.1			,     __lowerCamelCase		:	str=0.1			,     __lowerCamelCase		:	Optional[Any]=512			,     __lowerCamelCase		:	List[Any]=16			,     __lowerCamelCase		:	Optional[int]=2			,     __lowerCamelCase		:	Union[str, Any]=0.02			,     __lowerCamelCase		:	List[str]=3			,     __lowerCamelCase		:	Any=4			,     __lowerCamelCase		:	Dict=None			,     )	->							int:
       SCREAMING_SNAKE_CASE__					=					parent
       SCREAMING_SNAKE_CASE__					=					batch_size
       SCREAMING_SNAKE_CASE__					=					seq_length
       SCREAMING_SNAKE_CASE__					=					is_training
       SCREAMING_SNAKE_CASE__					=					use_input_mask
       SCREAMING_SNAKE_CASE__					=					use_token_type_ids
       SCREAMING_SNAKE_CASE__					=					use_labels
       SCREAMING_SNAKE_CASE__					=					vocab_size
       SCREAMING_SNAKE_CASE__					=					hidden_size
       SCREAMING_SNAKE_CASE__					=					embedding_size
       SCREAMING_SNAKE_CASE__					=					num_hidden_layers
       SCREAMING_SNAKE_CASE__					=					num_attention_heads
       SCREAMING_SNAKE_CASE__					=					intermediate_size
       SCREAMING_SNAKE_CASE__					=					hidden_act
       SCREAMING_SNAKE_CASE__					=					hidden_dropout_prob
       SCREAMING_SNAKE_CASE__					=					attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__					=					max_position_embeddings
       SCREAMING_SNAKE_CASE__					=					type_vocab_size
       SCREAMING_SNAKE_CASE__					=					type_sequence_label_size
       SCREAMING_SNAKE_CASE__					=					initializer_range
       SCREAMING_SNAKE_CASE__					=					num_labels
       SCREAMING_SNAKE_CASE__					=					num_choices
       SCREAMING_SNAKE_CASE__					=					scope






  def    lowercase_	(							self		:	List[str]      )	->							Optional[int]:
       SCREAMING_SNAKE_CASE__					=					ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size      )

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

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

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

       SCREAMING_SNAKE_CASE__					=					self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






  def    lowercase_	(							self		:	Dict      )	->							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=__lowerCamelCase			,     initializer_range=self.initializer_range			,     )






  def    lowercase_	(							self		:	List[str]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	Tuple			,     __lowerCamelCase		:	Union[str, Any]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	List[Any]      )	->							Any:
       SCREAMING_SNAKE_CASE__					=					MobileBertModel(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase			,     token_type_ids=__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase      )

       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    lowercase_	(							self		:	Dict			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	List[Any]			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	Optional[int]			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	Any      )	->							Optional[Any]:
       SCREAMING_SNAKE_CASE__					=					MobileBertForMaskedLM(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     labels=__lowerCamelCase      )
       self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)      )






  def    lowercase_	(							self		:	int			,     __lowerCamelCase		:	Any			,     __lowerCamelCase		:	Optional[int]			,     __lowerCamelCase		:	Optional[int]			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	List[Any]			,     __lowerCamelCase		:	List[Any]      )	->							int:
       SCREAMING_SNAKE_CASE__					=					MobileBertForNextSentencePrediction(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(
           __lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     labels=__lowerCamelCase			,     )
       self.parent.assertEqual(result.logits.shape			,     (self.batch_size, 2)      )






  def    lowercase_	(							self		:	str			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	List[Any]			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	Any      )	->							int:
       SCREAMING_SNAKE_CASE__					=					MobileBertForPreTraining(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(
           __lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     labels=__lowerCamelCase			,     next_sentence_label=__lowerCamelCase			,     )
       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    lowercase_	(							self		:	List[Any]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	Tuple			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	Any      )	->							Dict:
       SCREAMING_SNAKE_CASE__					=					MobileBertForQuestionAnswering(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(
           __lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     start_positions=__lowerCamelCase			,     end_positions=__lowerCamelCase			,     )
       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    lowercase_	(							self		:	Optional[Any]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	List[Any]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	Union[str, Any]      )	->							List[Any]:
       SCREAMING_SNAKE_CASE__					=					self.num_labels
       SCREAMING_SNAKE_CASE__					=					MobileBertForSequenceClassification(__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     labels=__lowerCamelCase      )
       self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)      )






  def    lowercase_	(							self		:	int			,     __lowerCamelCase		:	Optional[int]			,     __lowerCamelCase		:	Any			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	Any			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	Union[str, Any]      )	->							int:
       SCREAMING_SNAKE_CASE__					=					self.num_labels
       SCREAMING_SNAKE_CASE__					=					MobileBertForTokenClassification(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     labels=__lowerCamelCase      )
       self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.num_labels)      )






  def    lowercase_	(							self		:	Union[str, Any]			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	Any			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	Any			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	Tuple      )	->							List[Any]:
       SCREAMING_SNAKE_CASE__					=					self.num_choices
       SCREAMING_SNAKE_CASE__					=					MobileBertForMultipleChoice(config=__lowerCamelCase      )
       model.to(__lowerCamelCase      )
       model.eval()
       SCREAMING_SNAKE_CASE__					=					input_ids.unsqueeze(1      ).expand(-1			,     self.num_choices			,     -1      ).contiguous()
       SCREAMING_SNAKE_CASE__					=					token_type_ids.unsqueeze(1      ).expand(-1			,     self.num_choices			,     -1      ).contiguous()
       SCREAMING_SNAKE_CASE__					=					input_mask.unsqueeze(1      ).expand(-1			,     self.num_choices			,     -1      ).contiguous()
       SCREAMING_SNAKE_CASE__					=					model(
           __lowerCamelCase			,     attention_mask=__lowerCamelCase			,     token_type_ids=__lowerCamelCase			,     labels=__lowerCamelCase			,     )
       self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_choices)      )







  def    lowercase_	(							self		:	List[str]      )	->							Tuple:
       SCREAMING_SNAKE_CASE__					=					self.prepare_config_and_inputs()
       (
           (
           SCREAMING_SNAKE_CASE__
       ),(
           SCREAMING_SNAKE_CASE__
       ),(
           SCREAMING_SNAKE_CASE__
       ),(
           SCREAMING_SNAKE_CASE__
       ),(
           SCREAMING_SNAKE_CASE__
       ),(
           SCREAMING_SNAKE_CASE__
       ),(
           SCREAMING_SNAKE_CASE__
       ),
       )					=					config_and_inputs
       SCREAMING_SNAKE_CASE__					=					{'''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"""


  a     =      (
      (
          MobileBertModel,
          MobileBertForMaskedLM,
          MobileBertForMultipleChoice,
          MobileBertForNextSentencePrediction,
          MobileBertForPreTraining,
          MobileBertForQuestionAnswering,
          MobileBertForSequenceClassification,
          MobileBertForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  a     =      (
      {
          "feature-extraction": MobileBertModel,
          "fill-mask": MobileBertForMaskedLM,
          "question-answering": MobileBertForQuestionAnswering,
          "text-classification": MobileBertForSequenceClassification,
          "token-classification": MobileBertForTokenClassification,
          "zero-shot": MobileBertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  a     =      True






  def    lowercase_	(							self		:	int			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	Optional[int]=False      )	->							str:
       SCREAMING_SNAKE_CASE__					=					super()._prepare_for_class(__lowerCamelCase			,     __lowerCamelCase			,     return_labels=__lowerCamelCase      )

       if return_labels:
            if model_class in get_values(__lowerCamelCase      ):
                 SCREAMING_SNAKE_CASE__					=					torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.seq_length)			,     dtype=torch.long			,     device=__lowerCamelCase      )
                 SCREAMING_SNAKE_CASE__					=					torch.zeros(
                     self.model_tester.batch_size			,     dtype=torch.long			,     device=__lowerCamelCase      )
       return inputs_dict






  def    lowercase_	(							self		:	Optional[Any]      )	->							List[str]:
       SCREAMING_SNAKE_CASE__					=					MobileBertModelTester(self      )
       SCREAMING_SNAKE_CASE__					=					ConfigTester(self			,     config_class=__lowerCamelCase			,     hidden_size=37      )






  def    lowercase_	(							self		:	Any      )	->							Union[str, Any]:
       self.config_tester.run_common_tests()






  def    lowercase_	(							self		:	Optional[int]      )	->							Union[str, Any]:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*__lowerCamelCase      )






  def    lowercase_	(							self		:	Tuple      )	->							Any:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*__lowerCamelCase      )






  def    lowercase_	(							self		:	Dict      )	->							Optional[Any]:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCamelCase      )






  def    lowercase_	(							self		:	Union[str, Any]      )	->							Optional[Any]:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCamelCase      )






  def    lowercase_	(							self		:	Any      )	->							str:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCamelCase      )






  def    lowercase_	(							self		:	Dict      )	->							str:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*__lowerCamelCase      )






  def    lowercase_	(							self		:	List[str]      )	->							str:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCamelCase      )







  def    lowercase_	(							self		:	Tuple      )	->							List[Any]:
       SCREAMING_SNAKE_CASE__					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCamelCase      )



def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




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


_SCREAMING_SNAKE_CASE  :						Optional[Any]         =    1e-3







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






  """simple docstring"""


  @slow
  def    lowercase_	(							self		:	List[str]      )	->							List[Any]:
       SCREAMING_SNAKE_CASE__					=					MobileBertModel.from_pretrained('''google/mobilebert-uncased'''      ).to(__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					_long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]      )
       with torch.no_grad():
            SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase      )[0]
       SCREAMING_SNAKE_CASE__					=					torch.Size((1, 9, 512)      )
       self.assertEqual(output.shape			,     __lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					torch.tensor(
           [
               [
                   [-2.4_73_65_26e07, 8.2_69_16_56e04, 1.6_52_18_38e05],
                   [-5.7_54_17_04e-01, 3.9_05_60_22e00, 4.4_01_15_07e00],
                   [2.6_04_73_59e00, 1.5_67_76_52e00, -1.7_32_41_88e-01],
               ]
           ]			,     device=__lowerCamelCase			,     )

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

       self.assertTrue(lower_bound and upper_bound      )


