



'''simple docstring'''





import os
import sys
import warnings
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen
from ..table import array_cast
from ..utils.file_utils import is_local_path
from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
     import PIL.Image

     from .features import FeatureType


A_		:     Optional[List[str]]        =     None
A_		:     Tuple        =     "<" if sys.byteorder == "little" else ">"
# Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image
A_		:     List[Any]        =     [
    np.dtype('''|b1'''),
    np.dtype('''|u1'''),
    np.dtype('''<u2'''),
    np.dtype('''>u2'''),
    np.dtype('''<i2'''),
    np.dtype('''>i2'''),
    np.dtype('''<u4'''),
    np.dtype('''>u4'''),
    np.dtype('''<i4'''),
    np.dtype('''>i4'''),
    np.dtype('''<f4'''),
    np.dtype('''>f4'''),
    np.dtype('''<f8'''),
    np.dtype('''>f8'''),
]



@dataclass
class 		_lowercase		:
   _UpperCAmelCase     	=			True
   _UpperCAmelCase     	=			None
   # Automatically constructed
   _UpperCAmelCase     	=			'''PIL.Image.Image'''
   _UpperCAmelCase     	=			pa.struct({'''bytes''': pa.binary(), '''path''': pa.string()}	)
   _UpperCAmelCase     	=			field(default='''Image''',   init=_UpperCAmelCase,   repr=_UpperCAmelCase	)

   def __call__(      self			:       Optional[int]							)       ->				str:


    """simple docstring"""






    return self.pa_type

   def 		A							(      self			:       int							,   __lowerCAmelCase			:       Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"]							)       ->				Union[str, Any]:


    """simple docstring"""






    if config.PIL_AVAILABLE:
     import PIL.Image
    else:
     raise ImportError("To support encoding images, please install 'Pillow'."							)

    if isinstance(lowercase_							,   lowercase_							):
     a						=		np.array(lowercase_							)

    if isinstance(lowercase_							,   lowercase_							):
     return {"path": value, "bytes": None}
    elif isinstance(lowercase_							,   lowercase_							):
     return {"path": None, "bytes": value}
    elif isinstance(lowercase_							,   np.ndarray							):
     # convert the image array to PNG/TIFF bytes
     return encode_np_array(lowercase_							)
    elif isinstance(lowercase_							,   PIL.Image.Image							):
     # convert the PIL image to bytes (default format is PNG/TIFF)
     return encode_pil_image(lowercase_							)
    elif value.get("path"							) is not None and os.path.isfile(value["path"]							):
     # we set "bytes": None to not duplicate the data if they're already available locally
     return {"bytes": None, "path": value.get("path"							)}
    elif value.get("bytes"							) is not None or value.get("path"							) is not None:
     # store the image bytes, and path is used to infer the image format using the file extension
     return {"bytes": value.get("bytes"							), "path": value.get("path"							)}
    else:
     raise ValueError(
         f"""An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}."""							)

   def 		A							(      self			:       Any							,   __lowerCAmelCase			:       dict							,   __lowerCAmelCase			:       Any=None							)       ->				Optional[int]:


    """simple docstring"""






    if not self.decode:
     raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead."							)

    if config.PIL_AVAILABLE:
     import PIL.Image
    else:
     raise ImportError("To support decoding images, please install 'Pillow'."							)

    if token_per_repo_id is None:
     a						=		{}

    a						=		value["""path"""], value["""bytes"""]
    if bytes_ is None:
     if path is None:
      raise ValueError(f"""An image should have one of \'path\' or \'bytes\' but both are None in {value}."""							)
     else:
      if is_local_path(lowercase_							):
       a						=		PIL.Image.open(lowercase_							)
      else:
       a						=		path.split("::"							)[-1]
       try:
        a						=		string_to_dict(lowercase_							,   config.HUB_DATASETS_URL							)["""repo_id"""]
        a						=		token_per_repo_id.get(lowercase_							)
       except ValueError:
        a						=		None
       with xopen(lowercase_							,   "rb"							,   use_auth_token=lowercase_							) as f:
        a						=		BytesIO(f.read()							)
       a						=		PIL.Image.open(bytes_							)
    else:
     a						=		PIL.Image.open(BytesIO(bytes_							)							)
    image.load()  # to avoid "Too many open files" errors
    return image

   def 		A							(      self			:       Optional[Any]							)       ->				Optional[Any]:


    """simple docstring"""






    from .features import Value

    return (
        self
        if self.decode
        else {
            "bytes": Value("binary"							),
            "path": Value("string"							),
        }
    )

   def 		A							(      self			:       Any							,   __lowerCAmelCase			:       Union[pa.StringArray, pa.StructArray, pa.ListArray]							)       ->				Any:


    """simple docstring"""






    if pa.types.is_string(storage.type							):
     a						=		pa.array([None] * len(lowercase_							)							,   type=pa.binary()							)
     a						=		pa.StructArray.from_arrays([bytes_array, storage]							,   ["bytes", "path"]							,   mask=storage.is_null()							)
    elif pa.types.is_binary(storage.type							):
     a						=		pa.array([None] * len(lowercase_							)							,   type=pa.string()							)
     a						=		pa.StructArray.from_arrays([storage, path_array]							,   ["bytes", "path"]							,   mask=storage.is_null()							)
    elif pa.types.is_struct(storage.type							):
     if storage.type.get_field_index("bytes"							) >= 0:
      a						=		storage.field("bytes"							)
     else:
      a						=		pa.array([None] * len(lowercase_							)							,   type=pa.binary()							)
     if storage.type.get_field_index("path"							) >= 0:
      a						=		storage.field("path"							)
     else:
      a						=		pa.array([None] * len(lowercase_							)							,   type=pa.string()							)
     a						=		pa.StructArray.from_arrays([bytes_array, path_array]							,   ["bytes", "path"]							,   mask=storage.is_null()							)
    elif pa.types.is_list(storage.type							):
     a						=		pa.array(
         [encode_np_array(np.array(lowercase_							)							)["bytes"] if arr is not None else None for arr in storage.to_pylist()]							,   type=pa.binary()							,   )
     a						=		pa.array([None] * len(lowercase_							)							,   type=pa.string()							)
     a						=		pa.StructArray.from_arrays(
         [bytes_array, path_array]							,   ["bytes", "path"]							,   mask=bytes_array.is_null()							)
    return array_cast(lowercase_							,   self.pa_type							)




   def 		A							(      self			:       List[Any]							,   __lowerCAmelCase			:       pa.StructArray							)       ->				str:


    """simple docstring"""






    @no_op_if_value_is_null
    def path_to_bytes(__lowerCAmelCase			:       Any							):
     with xopen(lowercase_							,   "rb"							) as f:
      a						=		f.read()
     return bytes_

    a						=		pa.array(
        [
            (path_to_bytes(x["path"]							) if x["bytes"] is None else x["bytes"]) if x is not None else None
            for x in storage.to_pylist()
        ]							,   type=pa.binary()							,   )
    a						=		pa.array(
        [os.path.basename(lowercase_							) if path is not None else None for path in storage.field("path"							).to_pylist()]							,   type=pa.string()							,   )
    a						=		pa.StructArray.from_arrays([bytes_array, path_array]							,   ["bytes", "path"]							,   mask=bytes_array.is_null()							)
    return array_cast(lowercase_							,   self.pa_type							)


def 	UpperCAmelCase__      (      ):






 '''simple docstring'''

 if config.PIL_AVAILABLE:
  import PIL.Image
 else:
  raise ImportError("To support encoding images, please install 'Pillow'."					)

 global _IMAGE_COMPRESSION_FORMATS
 if _IMAGE_COMPRESSION_FORMATS is None:
  PIL.Image.init()
  a						=		list(set(PIL.Image.OPEN.keys()					) & set(PIL.Image.SAVE.keys()					)					)
 return _IMAGE_COMPRESSION_FORMATS


def 	UpperCAmelCase__      (      UpperCAmelCase__ :"PIL.Image.Image"					):






 '''simple docstring'''

 a						=		BytesIO()
 if image.format in list_image_compression_formats():
  a						=		image.format
 else:
  a						=		"""PNG""" if image.mode in ["""1""", """L""", """LA""", """RGB""", """RGBA"""] else """TIFF"""
 image.save(UpperCAmelCase__ ,					format=UpperCAmelCase__					)
 return buffer.getvalue()


def 	UpperCAmelCase__      (      UpperCAmelCase__ :"PIL.Image.Image"					):






 '''simple docstring'''

 if hasattr(UpperCAmelCase__ ,					"filename"					) and image.filename != "":
  return {"path": image.filename, "bytes": None}
 else:
  return {"path": None, "bytes": image_to_bytes(UpperCAmelCase__					)}


def 	UpperCAmelCase__      (      UpperCAmelCase__ :np.ndarray					):






 '''simple docstring'''

 if config.PIL_AVAILABLE:
  import PIL.Image
 else:
  raise ImportError("To support encoding images, please install 'Pillow'."					)

 a						=		array.dtype
 a						=		dtype.byteorder if dtype.byteorder != """=""" else _NATIVE_BYTEORDER
 a						=		dtype.kind
 a						=		dtype.itemsize

 a						=		None

 # Multi-channel array case (only np.dtype("|u1") is allowed)
 if array.shape[2:]:
  a						=		np.dtype("|u1"					)
  if dtype_kind not in ["u", "i"]:
   raise TypeError(
       F"""Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays."""					)
  if dtype is not dest_dtype:
   warnings.warn(F"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'"""					)
    # Exact match
 elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
  a						=		dtype
 else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
  while dtype_itemsize >= 1:
   a						=		dtype_byteorder + dtype_kind + str(UpperCAmelCase__					)
   a						=		np.dtype(UpperCAmelCase__					)
   if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES:
    warnings.warn(F"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'"""					)
    break
   else:
    dtype_itemsize //= 2

 if dest_dtype is None:
  raise TypeError(
      F"""Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}"""					)
 a						=		PIL.Image.fromarray(array.astype(UpperCAmelCase__					)					)
 return {"path": None, "bytes": image_to_bytes(UpperCAmelCase__					)}




def 	UpperCAmelCase__      (      UpperCAmelCase__ :Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]]					):






 '''simple docstring'''

 if config.PIL_AVAILABLE:
  import PIL.Image
 else:
  raise ImportError("To support encoding images, please install 'Pillow'."					)

 if objs:
  a						=		first_non_null_value(UpperCAmelCase__					)
  if isinstance(UpperCAmelCase__ ,					UpperCAmelCase__					):
   return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs]
  if isinstance(UpperCAmelCase__ ,					np.ndarray					):
   a						=		no_op_if_value_is_null(UpperCAmelCase__					)
   return [obj_to_image_dict_func(UpperCAmelCase__					) for obj in objs]
  elif isinstance(UpperCAmelCase__ ,					PIL.Image.Image					):
   a						=		no_op_if_value_is_null(UpperCAmelCase__					)
   return [obj_to_image_dict_func(UpperCAmelCase__					) for obj in objs]
  else:
   return objs
 else:
  return objs






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 		_lowercase		:

   def __init__(      self			:       List[Any]							,   __lowerCAmelCase			:       Union[str, Any]							,   __lowerCAmelCase			:       Any=13							,   __lowerCAmelCase			:       Any=7							,   __lowerCAmelCase			:       int=True							,   __lowerCAmelCase			:       Union[str, Any]=True							,   __lowerCAmelCase			:       Any=True							,   __lowerCAmelCase			:       Union[str, Any]=True							,   __lowerCAmelCase			:       str=99							,   __lowerCAmelCase			:       List[str]=64							,   __lowerCAmelCase			:       Optional[Any]=32							,   __lowerCAmelCase			:       Dict=5							,   __lowerCAmelCase			:       int=4							,   __lowerCAmelCase			:       Optional[Any]=37							,   __lowerCAmelCase			:       Union[str, Any]="gelu"							,   __lowerCAmelCase			:       Union[str, Any]=0.1							,   __lowerCAmelCase			:       List[str]=0.1							,   __lowerCAmelCase			:       List[str]=512							,   __lowerCAmelCase			:       List[Any]=16							,   __lowerCAmelCase			:       Union[str, Any]=2							,   __lowerCAmelCase			:       Optional[Any]=0.0_2							,   __lowerCAmelCase			:       Dict=3							,   __lowerCAmelCase			:       Optional[int]=4							,   __lowerCAmelCase			:       Union[str, Any]=None							,   )       ->				List[str]:


    """simple docstring"""






    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 		A							(      self			:       Optional[int]							)       ->				Optional[int]:


    """simple docstring"""






    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 		A							(      self			:       int							)       ->				List[str]:


    """simple docstring"""






    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 		A							(      self			:       Optional[int]							,   __lowerCAmelCase			:       Dict							,   __lowerCAmelCase			:       int							,   __lowerCAmelCase			:       Dict							,   __lowerCAmelCase			:       List[str]							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       Union[str, Any]							,   __lowerCAmelCase			:       Dict							)       ->				Union[str, Any]:


    """simple docstring"""






    a						=		MobileBertModel(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		model(__lowerCAmelCase							,   attention_mask=__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							)
    a						=		model(__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							)
    a						=		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 		A							(      self			:       List[Any]							,   __lowerCAmelCase			:       Optional[int]							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       Union[str, Any]							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Optional[int]							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Any							)       ->				str:


    """simple docstring"""






    a						=		MobileBertForMaskedLM(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		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 		A							(      self			:       List[Any]							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Dict							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       int							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       List[str]							)       ->				List[str]:


    """simple docstring"""






    a						=		MobileBertForNextSentencePrediction(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		model(
        __lowerCAmelCase							,   attention_mask=__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							,   labels=__lowerCAmelCase							,   )
    self.parent.assertEqual(result.logits.shape							,   (self.batch_size, 2)							)

   def 		A							(      self			:       List[str]							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       List[str]							,   __lowerCAmelCase			:       Dict							,   __lowerCAmelCase			:       List[str]							)       ->				List[Any]:


    """simple docstring"""






    a						=		MobileBertForPreTraining(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		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 		A							(      self			:       Union[str, Any]							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       Optional[int]							,   __lowerCAmelCase			:       List[str]							)       ->				Any:


    """simple docstring"""






    a						=		MobileBertForQuestionAnswering(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		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 		A							(      self			:       List[Any]							,   __lowerCAmelCase			:       Dict							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       Optional[int]							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       int							,   __lowerCAmelCase			:       Optional[Any]							)       ->				Optional[int]:


    """simple docstring"""






    a						=		self.num_labels
    a						=		MobileBertForSequenceClassification(__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		model(__lowerCAmelCase							,   attention_mask=__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							,   labels=__lowerCAmelCase							)
    self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.num_labels)							)

   def 		A							(      self			:       Tuple							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       List[str]							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       Any							)       ->				Optional[Any]:


    """simple docstring"""






    a						=		self.num_labels
    a						=		MobileBertForTokenClassification(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()
    a						=		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 		A							(      self			:       Union[str, Any]							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       int							,   __lowerCAmelCase			:       List[str]							,   __lowerCAmelCase			:       Union[str, Any]							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Optional[int]							)       ->				List[str]:


    """simple docstring"""






    a						=		self.num_choices
    a						=		MobileBertForMultipleChoice(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    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(
        __lowerCAmelCase							,   attention_mask=__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							,   labels=__lowerCAmelCase							,   )
    self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.num_choices)							)




   def 		A							(      self			:       List[Any]							)       ->				Dict:


    """simple docstring"""






    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 		_lowercase		(       UpperCAmelCase__,   UpperCAmelCase__,   unittest.TestCase	):
   _UpperCAmelCase     	=			(
       (
           MobileBertModel,
           MobileBertForMaskedLM,
           MobileBertForMultipleChoice,
           MobileBertForNextSentencePrediction,
           MobileBertForPreTraining,
           MobileBertForQuestionAnswering,
           MobileBertForSequenceClassification,
           MobileBertForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   _UpperCAmelCase     	=			(
       {
           '''feature-extraction''': MobileBertModel,
           '''fill-mask''': MobileBertForMaskedLM,
           '''question-answering''': MobileBertForQuestionAnswering,
           '''text-classification''': MobileBertForSequenceClassification,
           '''token-classification''': MobileBertForTokenClassification,
           '''zero-shot''': MobileBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _UpperCAmelCase     	=			True

   def 		A							(      self			:       Optional[int]							,   __lowerCAmelCase			:       Dict							,   __lowerCAmelCase			:       Union[str, Any]							,   __lowerCAmelCase			:       Any=False							)       ->				Any:


    """simple docstring"""






    a						=		super()._prepare_for_class(__lowerCAmelCase							,   __lowerCAmelCase							,   return_labels=__lowerCAmelCase							)

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

   def 		A							(      self			:       Optional[int]							)       ->				List[Any]:


    """simple docstring"""






    a						=		MobileBertModelTester(self							)
    a						=		ConfigTester(self							,   config_class=__lowerCAmelCase							,   hidden_size=37							)

   def 		A							(      self			:       int							)       ->				Tuple:


    """simple docstring"""






    self.config_tester.run_common_tests()

   def 		A							(      self			:       str							)       ->				Dict:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_model(*__lowerCAmelCase							)

   def 		A							(      self			:       str							)       ->				str:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_masked_lm(*__lowerCAmelCase							)

   def 		A							(      self			:       List[str]							)       ->				Dict:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCAmelCase							)

   def 		A							(      self			:       int							)       ->				Dict:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCAmelCase							)

   def 		A							(      self			:       List[Any]							)       ->				int:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCAmelCase							)

   def 		A							(      self			:       List[Any]							)       ->				Dict:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_question_answering(*__lowerCAmelCase							)

   def 		A							(      self			:       List[Any]							)       ->				Optional[int]:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCAmelCase							)




   def 		A							(      self			:       int							)       ->				Tuple:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCAmelCase							)


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Dict					):






 '''simple docstring'''

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


A_		:     Dict        =     1E-3




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

   @slow
   def 		A							(      self			:       Optional[Any]							)       ->				Optional[Any]:


    """simple docstring"""






    a						=		MobileBertModel.from_pretrained("google/mobilebert-uncased"							).to(__lowerCAmelCase							)
    a						=		_long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]							)
    with torch.no_grad():
     a						=		model(__lowerCAmelCase							)[0]
    a						=		torch.Size((1, 9, 512)							)
    self.assertEqual(output.shape							,   __lowerCAmelCase							)
    a						=		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
    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							)

