
"""simple docstring"""




from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


A__   :					str							=   logging.get_logger(__name__)


# General docstring
A__   :					List[str]							=   'MobileNetV1Config'

# Base docstring
A__   :					Tuple							=   'google/mobilenet_v1_1.0_224'
A__   :					List[str]							=   [1, 1_0_2_4, 7, 7]

# Image classification docstring
A__   :					Union[str, Any]							=   'google/mobilenet_v1_1.0_224'
A__   :					Dict							=   'tabby, tabby cat'


A__   :					Optional[int]							=   [
    'google/mobilenet_v1_1.0_224',
    'google/mobilenet_v1_0.75_192',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]




def  _lowerCAmelCase		(		_UpperCamelCase ,				_UpperCamelCase ,				_UpperCamelCase=None						):






  """simple docstring"""



  _lowercase:			List[Any]       =     {}

  if isinstance(_UpperCamelCase ,				_UpperCamelCase						):
    _lowercase:			Dict       =     model.mobilenet_va
  else:
    _lowercase:			int       =     model

  _lowercase:			int       =     '''MobilenetV1/Conv2d_0/'''
  _lowercase:			Dict       =     backbone.conv_stem.convolution.weight
  _lowercase:			Tuple       =     backbone.conv_stem.normalization.bias
  _lowercase:			Any       =     backbone.conv_stem.normalization.weight
  _lowercase:			Optional[Any]       =     backbone.conv_stem.normalization.running_mean
  _lowercase:			List[str]       =     backbone.conv_stem.normalization.running_var

  for i in range(13						):
    _lowercase:			Tuple       =     i + 1
    _lowercase:			Union[str, Any]       =     i * 2

    _lowercase:			List[str]       =     backbone.layer[pt_index]
    _lowercase:			List[str]       =     f'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
    _lowercase:			Optional[int]       =     pointer.convolution.weight
    _lowercase:			List[str]       =     pointer.normalization.bias
    _lowercase:			Dict       =     pointer.normalization.weight
    _lowercase:			Any       =     pointer.normalization.running_mean
    _lowercase:			Dict       =     pointer.normalization.running_var

    _lowercase:			List[Any]       =     backbone.layer[pt_index + 1]
    _lowercase:			List[Any]       =     f'''MobilenetV1/Conv2d_{tf_index}_pointwise/'''
    _lowercase:			List[Any]       =     pointer.convolution.weight
    _lowercase:			int       =     pointer.normalization.bias
    _lowercase:			Optional[int]       =     pointer.normalization.weight
    _lowercase:			int       =     pointer.normalization.running_mean
    _lowercase:			Tuple       =     pointer.normalization.running_var

  if isinstance(_UpperCamelCase ,				_UpperCamelCase						):
    _lowercase:			Tuple       =     '''MobilenetV1/Logits/Conv2d_1c_1x1/'''
    _lowercase:			int       =     model.classifier.weight
    _lowercase:			List[Any]       =     model.classifier.bias

  return tf_to_pt_map




def  _lowerCAmelCase		(		_UpperCamelCase ,				_UpperCamelCase ,				_UpperCamelCase						):






  """simple docstring"""



  try:
    import numpy as np
    import tensorflow as tf
  except ImportError:
    logger.error(
        '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
        '''https://www.tensorflow.org/install/ for installation instructions.'''						)
    raise

  # Load weights from TF model
  _lowercase:			Optional[Any]       =     tf.train.list_variables(_UpperCamelCase						)
  _lowercase:			Tuple       =     {}
  for name, shape in init_vars:
    logger.info(f'''Loading TF weight {name} with shape {shape}'''						)
    _lowercase:			Dict       =     tf.train.load_variable(_UpperCamelCase ,				_UpperCamelCase						)
    _lowercase:			Union[str, Any]       =     array

  # Build TF to PyTorch weights loading map
  _lowercase:			List[Any]       =     _build_tf_to_pytorch_map(_UpperCamelCase ,				_UpperCamelCase ,				_UpperCamelCase						)

  for name, pointer in tf_to_pt_map.items():
    logger.info(f'''Importing {name}'''						)
    if name not in tf_weights:
      logger.info(f'''{name} not in tf pre-trained weights, skipping'''						)
      continue

    _lowercase:			Optional[int]       =     tf_weights[name]

    if "depthwise_weights" in name:
      logger.info('''Transposing depthwise'''						)
      _lowercase:			Dict       =     np.transpose(_UpperCamelCase ,				(2, 3, 0, 1)						)
    elif "weights" in name:
      logger.info('''Transposing'''						)
      if len(pointer.shape						) == 2:  # copying into linear layer
        _lowercase:			Any       =     array.squeeze().transpose()
      else:
        _lowercase:			str       =     np.transpose(_UpperCamelCase ,				(3, 2, 0, 1)						)

    if pointer.shape != array.shape:
      raise ValueError(f'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched'''						)

    logger.info(f'''Initialize PyTorch weight {name} {array.shape}'''						)
    _lowercase:			Any       =     torch.from_numpy(_UpperCamelCase						)

    tf_weights.pop(_UpperCamelCase ,				_UpperCamelCase						)
    tf_weights.pop(name + '''/RMSProp''' ,				_UpperCamelCase						)
    tf_weights.pop(name + '''/RMSProp_1''' ,				_UpperCamelCase						)
    tf_weights.pop(name + '''/ExponentialMovingAverage''' ,				_UpperCamelCase						)

  logger.info(f'''Weights not copied to PyTorch model: {", ".join(tf_weights.keys()						)}'''						)
  return model




def  _lowerCAmelCase		(		_UpperCamelCase ,				_UpperCamelCase						):






  """simple docstring"""



  _lowercase ,				_lowercase:			List[Any]       =     features.shape[-2:]
  _lowercase ,				_lowercase:			Any       =     conv_layer.stride
  _lowercase ,				_lowercase:			Tuple       =     conv_layer.kernel_size

  if in_height % stride_height == 0:
    _lowercase:			str       =     max(kernel_height - stride_height ,				0						)
  else:
    _lowercase:			List[Any]       =     max(kernel_height - (in_height % stride_height) ,				0						)

  if in_width % stride_width == 0:
    _lowercase:			Any       =     max(kernel_width - stride_width ,				0						)
  else:
    _lowercase:			str       =     max(kernel_width - (in_width % stride_width) ,				0						)

  _lowercase:			Union[str, Any]       =     pad_along_width // 2
  _lowercase:			List[str]       =     pad_along_width - pad_left
  _lowercase:			Optional[int]       =     pad_along_height // 2
  _lowercase:			Optional[Any]       =     pad_along_height - pad_top

  _lowercase:			int       =     (pad_left, pad_right, pad_top, pad_bottom)
  return nn.functional.pad(_UpperCamelCase ,				_UpperCamelCase ,				'''constant''' ,				0.0						)


class 			__magic_name__       (       nn.Module ):
       def __init__(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ = 1 ,				A_ = 1 ,				A_ = False ,				A_ = True ,				A_ = True ,				)     ->	None:





         """simple docstring"""




         super().__init__()
         _lowercase:			List[str]       =     config

         if in_channels % groups != 0:
           raise ValueError(f'''Input channels ({in_channels}) are not divisible by {groups} groups.'''       )
         if out_channels % groups != 0:
           raise ValueError(f'''Output channels ({out_channels}) are not divisible by {groups} groups.'''       )

         _lowercase:			Union[str, Any]       =     0 if config.tf_padding else int((kernel_size - 1) / 2       )

         _lowercase:			Any       =     nn.Convad(
             in_channels=A_ ,				out_channels=A_ ,				kernel_size=A_ ,				stride=A_ ,				padding=A_ ,				groups=A_ ,				bias=A_ ,				padding_mode='''zeros''' ,				)

         if use_normalization:
           _lowercase:			Dict       =     nn.BatchNormad(
               num_features=A_ ,				eps=config.layer_norm_eps ,				momentum=0.99_97 ,				affine=A_ ,				track_running_stats=A_ ,				)
         else:
           _lowercase:			str       =     None

         if use_activation:
           if isinstance(A_ ,				A_       ):
             _lowercase:			Optional[int]       =     ACTaFN[use_activation]
           elif isinstance(config.hidden_act ,				A_       ):
             _lowercase:			Optional[int]       =     ACTaFN[config.hidden_act]
           else:
             _lowercase:			List[Any]       =     config.hidden_act
         else:
           _lowercase:			Any       =     None



       def 							lowercase_							(     self ,				A_       )     ->	torch.Tensor:





         """simple docstring"""




         if self.config.tf_padding:
           _lowercase:			List[str]       =     apply_tf_padding(A_ ,				self.convolution       )
         _lowercase:			Tuple       =     self.convolution(A_       )
         if self.normalization is not None:
           _lowercase:			Any       =     self.normalization(A_       )
         if self.activation is not None:
           _lowercase:			str       =     self.activation(A_       )
         return features


class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       UpperCamelCase_   =		MobileNetVaConfig
       UpperCamelCase_   =		load_tf_weights_in_mobilenet_va
       UpperCamelCase_   =		'''mobilenet_v1'''
       UpperCamelCase_   =		'''pixel_values'''
       UpperCamelCase_   =		False
       def 							lowercase_							(     self ,				A_       )     ->	None:





         """simple docstring"""




         if isinstance(A_ ,				(nn.Linear, nn.Convad)       ):
           module.weight.data.normal_(mean=0.0 ,				std=self.config.initializer_range       )
           if module.bias is not None:
             module.bias.data.zero_()
         elif isinstance(A_ ,				nn.BatchNormad       ):
           module.bias.data.zero_()
           module.weight.data.fill_(1.0       )

A__   :					str							=   r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

A__   :					Tuple							=   r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'


@add_start_docstrings(
    '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.'''   ,					SCREAMING_SNAKE_CASE__   ,					)
class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       def __init__(     self ,				A_ ,				A_ = True       )     ->	List[str]:





         """simple docstring"""




         super().__init__(A_       )
         _lowercase:			Optional[Any]       =     config

         _lowercase:			Union[str, Any]       =     32
         _lowercase:			Any       =     max(int(depth * config.depth_multiplier       ) ,				config.min_depth       )

         _lowercase:			Tuple       =     MobileNetVaConvLayer(
             A_ ,				in_channels=config.num_channels ,				out_channels=A_ ,				kernel_size=3 ,				stride=2 ,				)

         _lowercase:			Tuple       =     [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

         _lowercase:			List[str]       =     nn.ModuleList()
         for i in range(13       ):
           _lowercase:			int       =     out_channels

           if strides[i] == 2 or i == 0:
             depth *= 2
             _lowercase:			List[str]       =     max(int(depth * config.depth_multiplier       ) ,				config.min_depth       )

           self.layer.append(
               MobileNetVaConvLayer(
                   A_ ,				in_channels=A_ ,				out_channels=A_ ,				kernel_size=3 ,				stride=strides[i] ,				groups=A_ ,				)       )

           self.layer.append(
               MobileNetVaConvLayer(
                   A_ ,				in_channels=A_ ,				out_channels=A_ ,				kernel_size=1 ,				)       )

         _lowercase:			str       =     nn.AdaptiveAvgPoolad((1, 1)       ) if add_pooling_layer else None

         # Initialize weights and apply final processing
         self.post_init()
       def 							lowercase_							(     self ,				A_       )     ->	str:





         """simple docstring"""




         raise NotImplementedError



       @add_start_docstrings_to_model_forward(A_       )
       @add_code_sample_docstrings(
           checkpoint=_CHECKPOINT_FOR_DOC ,				output_type=A_ ,				config_class=_CONFIG_FOR_DOC ,				modality='''vision''' ,				expected_output=_EXPECTED_OUTPUT_SHAPE ,				)
       def 							lowercase_							(     self ,				A_ = None ,				A_ = None ,				A_ = None ,				)     ->	Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:





         """simple docstring"""




         _lowercase:			Optional[int]       =     (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         _lowercase:			Optional[int]       =     return_dict if return_dict is not None else self.config.use_return_dict

         if pixel_values is None:
           raise ValueError('''You have to specify pixel_values'''       )

         _lowercase:			Optional[Any]       =     self.conv_stem(A_       )

         _lowercase:			Union[str, Any]       =     () if output_hidden_states else None

         for i, layer_module in enumerate(self.layer       ):
           _lowercase:			List[str]       =     layer_module(A_       )

           if output_hidden_states:
             _lowercase:			List[Any]       =     all_hidden_states + (hidden_states,)

         _lowercase:			Optional[Any]       =     hidden_states

         if self.pooler is not None:
           _lowercase:			Any       =     torch.flatten(self.pooler(A_       ) ,				start_dim=1       )
         else:
           _lowercase:			Dict       =     None

         if not return_dict:
           return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None       )

         return BaseModelOutputWithPoolingAndNoAttention(
             last_hidden_state=A_ ,				pooler_output=A_ ,				hidden_states=A_ ,				)


@add_start_docstrings(
    '''
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''   ,					SCREAMING_SNAKE_CASE__   ,					)
class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       def __init__(     self ,				A_       )     ->	None:





         """simple docstring"""




         super().__init__(A_       )

         _lowercase:			Tuple       =     config.num_labels
         _lowercase:			List[Any]       =     MobileNetVaModel(A_       )

         _lowercase:			List[Any]       =     self.mobilenet_va.layer[-1].convolution.out_channels

         # Classifier head
         _lowercase:			int       =     nn.Dropout(config.classifier_dropout_prob ,				inplace=A_       )
         _lowercase:			List[Any]       =     nn.Linear(A_ ,				config.num_labels       ) if config.num_labels > 0 else nn.Identity()

         # Initialize weights and apply final processing
         self.post_init()



       @add_start_docstrings_to_model_forward(A_       )
       @add_code_sample_docstrings(
           checkpoint=_IMAGE_CLASS_CHECKPOINT ,				output_type=A_ ,				config_class=_CONFIG_FOR_DOC ,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,				)
       def 							lowercase_							(     self ,				A_ = None ,				A_ = None ,				A_ = None ,				A_ = None ,				)     ->	Union[tuple, ImageClassifierOutputWithNoAttention]:





         """simple docstring"""




         _lowercase:			Dict       =     return_dict if return_dict is not None else self.config.use_return_dict

         _lowercase:			Any       =     self.mobilenet_va(A_ ,				output_hidden_states=A_ ,				return_dict=A_       )

         _lowercase:			str       =     outputs.pooler_output if return_dict else outputs[1]

         _lowercase:			List[Any]       =     self.classifier(self.dropout(A_       )       )

         _lowercase:			str       =     None
         if labels is not None:
           if self.config.problem_type is None:
             if self.num_labels == 1:
               _lowercase:			Optional[int]       =     '''regression'''
             elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
               _lowercase:			Dict       =     '''single_label_classification'''
             else:
               _lowercase:			Dict       =     '''multi_label_classification'''

           if self.config.problem_type == "regression":
             _lowercase:			Tuple       =     MSELoss()
             if self.num_labels == 1:
               _lowercase:			Any       =     loss_fct(logits.squeeze() ,				labels.squeeze()       )
             else:
               _lowercase:			Optional[Any]       =     loss_fct(A_ ,				A_       )
           elif self.config.problem_type == "single_label_classification":
             _lowercase:			Dict       =     CrossEntropyLoss()
             _lowercase:			Dict       =     loss_fct(logits.view(-1 ,				self.num_labels       ) ,				labels.view(-1       )       )
           elif self.config.problem_type == "multi_label_classification":
             _lowercase:			Union[str, Any]       =     BCEWithLogitsLoss()
             _lowercase:			Union[str, Any]       =     loss_fct(A_ ,				A_       )

         if not return_dict:
           _lowercase:			List[Any]       =     (logits,) + outputs[2:]
           return ((loss,) + output) if loss is not None else output

         return ImageClassifierOutputWithNoAttention(
             loss=A_ ,				logits=A_ ,				hidden_states=outputs.hidden_states ,				)


"""simple docstring"""




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 			__magic_name__       :
       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 ,				)     ->	Dict:





         """simple docstring"""




         _lowercase:			List[Any]       =     parent
         _lowercase:			str       =     batch_size
         _lowercase:			List[str]       =     seq_length
         _lowercase:			Any       =     is_training
         _lowercase:			Union[str, Any]       =     use_input_mask
         _lowercase:			Any       =     use_token_type_ids
         _lowercase:			int       =     use_labels
         _lowercase:			Any       =     vocab_size
         _lowercase:			Optional[int]       =     hidden_size
         _lowercase:			Optional[int]       =     embedding_size
         _lowercase:			Union[str, Any]       =     num_hidden_layers
         _lowercase:			Optional[int]       =     num_attention_heads
         _lowercase:			Optional[int]       =     intermediate_size
         _lowercase:			List[str]       =     hidden_act
         _lowercase:			Dict       =     hidden_dropout_prob
         _lowercase:			Any       =     attention_probs_dropout_prob
         _lowercase:			Tuple       =     max_position_embeddings
         _lowercase:			List[str]       =     type_vocab_size
         _lowercase:			Any       =     type_sequence_label_size
         _lowercase:			int       =     initializer_range
         _lowercase:			Any       =     num_labels
         _lowercase:			int       =     num_choices
         _lowercase:			List[str]       =     scope
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Tuple       =     ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size       )

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

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

         _lowercase:			Dict       =     None
         _lowercase:			Tuple       =     None
         _lowercase:			List[str]       =     None
         if self.use_labels:
           _lowercase:			str       =     ids_tensor([self.batch_size] ,				self.type_sequence_label_size       )
           _lowercase:			int       =     ids_tensor([self.batch_size, self.seq_length] ,				self.num_labels       )
           _lowercase:			Dict       =     ids_tensor([self.batch_size] ,				self.num_choices       )

         _lowercase:			str       =     self.get_config()

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
       def 							lowercase_							(     self       )     ->	List[Any]:





         """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=A_ ,				initializer_range=self.initializer_range ,				)
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Any:





         """simple docstring"""




         _lowercase:			Tuple       =     MobileBertModel(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Tuple       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_       )
         _lowercase:			Dict       =     model(A_ ,				token_type_ids=A_       )
         _lowercase:			Optional[int]       =     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 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	int:





         """simple docstring"""




         _lowercase:			int       =     MobileBertForMaskedLM(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Optional[int]       =     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 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	int:





         """simple docstring"""




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





         """simple docstring"""




         _lowercase:			str       =     MobileBertForPreTraining(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			List[str]       =     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 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	List[str]:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     MobileBertForQuestionAnswering(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Dict       =     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 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	List[Any]:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     self.num_labels
         _lowercase:			int       =     MobileBertForSequenceClassification(A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			List[Any]       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				labels=A_       )
         self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)       )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Tuple:





         """simple docstring"""




         _lowercase:			Dict       =     self.num_labels
         _lowercase:			Optional[Any]       =     MobileBertForTokenClassification(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Optional[int]       =     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 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	List[str]:





         """simple docstring"""




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



       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			Any       =     self.prepare_config_and_inputs()
         (
             (
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				
         ):			Dict       =     config_and_inputs
         _lowercase:			int       =     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict


@require_torch
class 			__magic_name__       (       SCREAMING_SNAKE_CASE__   ,					SCREAMING_SNAKE_CASE__   ,					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 							lowercase_							(     self ,				A_ ,				A_ ,				A_=False       )     ->	str:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     super()._prepare_for_class(A_ ,				A_ ,				return_labels=A_       )

         if return_labels:
           if model_class in get_values(A_       ):
             _lowercase:			Tuple       =     torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length) ,				dtype=torch.long ,				device=A_       )
             _lowercase:			Tuple       =     torch.zeros(
                 self.model_tester.batch_size ,				dtype=torch.long ,				device=A_       )
         return inputs_dict
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




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





         """simple docstring"""




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





         """simple docstring"""




         _lowercase:			str       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_model(*A_       )
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Union[str, Any]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_masked_lm(*A_       )
       def 							lowercase_							(     self       )     ->	Any:





         """simple docstring"""




         _lowercase:			List[str]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A_       )
       def 							lowercase_							(     self       )     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			Any       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A_       )
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_pretraining(*A_       )
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         _lowercase:			List[Any]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_question_answering(*A_       )
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




         _lowercase:			str       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A_       )



       def 							lowercase_							(     self       )     ->	List[Any]:





         """simple docstring"""




         _lowercase:			Dict       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_token_classification(*A_       )




def  _lowerCAmelCase		(		_UpperCamelCase						):






  """simple docstring"""



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


A__   :					Optional[Any]							=   1e-3







@require_torch
@require_sentencepiece
@require_tokenizers
class 			__magic_name__       (       unittest.TestCase ):
       @slow
       def 							lowercase_							(     self       )     ->	int:





         """simple docstring"""




         _lowercase:			Optional[int]       =     MobileBertModel.from_pretrained('''google/mobilebert-uncased'''       ).to(A_       )
         _lowercase:			str       =     _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]       )
         with torch.no_grad():
           _lowercase:			Dict       =     model(A_       )[0]
         _lowercase:			List[Any]       =     torch.Size((1, 9, 512)       )
         self.assertEqual(output.shape ,				A_       )
         _lowercase:			Tuple       =     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=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
         _lowercase:			Tuple       =     torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE       )
         _lowercase:			Dict       =     torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE       )

         self.assertTrue(lower_bound and upper_bound       )
