





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


lowercase :			List[str]     		=    logging.get_logger(__name__)


# General docstring
lowercase :			int     		=    'MobileNetV1Config'

# Base docstring
lowercase :			List[Any]     		=    'google/mobilenet_v1_1.0_224'
lowercase :			Any     		=    [1, 1_0_2_4, 7, 7]

# Image classification docstring
lowercase :			Optional[Any]     		=    'google/mobilenet_v1_1.0_224'
lowercase :			Tuple     		=    'tabby, tabby cat'


lowercase :			List[str]     		=    [
    '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 A_      (						A__						, A__						, A__=None							)    ->						Dict:
       a__		:      List[str]					=							{}

       if isinstance(_lowerCamelCase						, _lowerCamelCase							):
              a__		:      Any					=							model.mobilenet_va
       else:
              a__		:      List[Any]					=							model

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

       for i in range(13							):
              a__		:      Optional[int]					=							i + 1
              a__		:      Any					=							i * 2

              a__		:      List[str]					=							backbone.layer[pt_index]
              a__		:      Dict					=							F'MobilenetV1/Conv2d_{tf_index}_depthwise/'
              a__		:      Tuple					=							pointer.convolution.weight
              a__		:      Any					=							pointer.normalization.bias
              a__		:      str					=							pointer.normalization.weight
              a__		:      Tuple					=							pointer.normalization.running_mean
              a__		:      List[str]					=							pointer.normalization.running_var

              a__		:      Tuple					=							backbone.layer[pt_index + 1]
              a__		:      str					=							F'MobilenetV1/Conv2d_{tf_index}_pointwise/'
              a__		:      List[str]					=							pointer.convolution.weight
              a__		:      Dict					=							pointer.normalization.bias
              a__		:      Optional[int]					=							pointer.normalization.weight
              a__		:      Union[str, Any]					=							pointer.normalization.running_mean
              a__		:      Optional[Any]					=							pointer.normalization.running_var

       if isinstance(_lowerCamelCase						, _lowerCamelCase							):
              a__		:      Dict					=							"MobilenetV1/Logits/Conv2d_1c_1x1/"
              a__		:      Tuple					=							model.classifier.weight
              a__		:      List[Any]					=							model.classifier.bias

       return tf_to_pt_map



def A_      (						A__						, A__						, A__							)    ->						Dict:





       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
       a__		:      int					=							tf.train.list_variables(_lowerCamelCase							)
       a__		:      Optional[int]					=							{}
       for name, shape in init_vars:
              logger.info(F'Loading TF weight {name} with shape {shape}'							)
              a__		:      Tuple					=							tf.train.load_variable(_lowerCamelCase						, _lowerCamelCase							)
              a__		:      int					=							array

       # Build TF to PyTorch weights loading map
       a__		:      Optional[Any]					=							_build_tf_to_pytorch_map(_lowerCamelCase						, _lowerCamelCase						, _lowerCamelCase							)

       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

              a__		:      Optional[Any]					=							tf_weights[name]

              if "depthwise_weights" in name:
                     logger.info('Transposing depthwise'							)
                     a__		:      List[str]					=							np.transpose(_lowerCamelCase						, (2, 3, 0, 1)							)
              elif "weights" in name:
                     logger.info('Transposing'							)
                     if len(pointer.shape							) == 2:  # copying into linear layer
                            a__		:      List[Any]					=							array.squeeze().transpose()
                     else:
                            a__		:      List[Any]					=							np.transpose(_lowerCamelCase						, (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}'							)
              a__		:      Optional[int]					=							torch.from_numpy(_lowerCamelCase							)

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

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



def A_      (						A__						, A__							)    ->						torch.Tensor:
       a__		:      Tuple					=							features.shape[-2:]
       a__		:      Optional[Any]					=							conv_layer.stride
       a__		:      Optional[int]					=							conv_layer.kernel_size

       if in_height % stride_height == 0:
              a__		:      Optional[int]					=							max(kernel_height - stride_height						, 0							)
       else:
              a__		:      Optional[Any]					=							max(kernel_height - (in_height % stride_height)						, 0							)

       if in_width % stride_width == 0:
              a__		:      Dict					=							max(kernel_width - stride_width						, 0							)
       else:
              a__		:      str					=							max(kernel_width - (in_width % stride_width)						, 0							)

       a__		:      Tuple					=							pad_along_width // 2
       a__		:      Optional[Any]					=							pad_along_width - pad_left
       a__		:      List[Any]					=							pad_along_height // 2
       a__		:      Optional[int]					=							pad_along_height - pad_top

       a__		:      Optional[int]					=							(pad_left, pad_right, pad_top, pad_bottom)
       return nn.functional.pad(_lowerCamelCase						, _lowerCamelCase						, 'constant'						, 0.0							)





class 					A__       (				nn.Module      ):




   """simple docstring"""



   def __init__(   self   ,    lowercase   ,    lowercase   ,    lowercase   ,    lowercase   ,    lowercase = 1   ,    lowercase = 1   ,    lowercase = False   ,    lowercase = True   ,    lowercase = True   ,    )     ->		None:







          '''simple docstring'''

          super().__init__()
          a__		:      Tuple					=							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.')

          a__		:      List[Any]					=							0 if config.tf_padding else int((kernel_size - 1) / 2)

          a__		:      List[str]					=							nn.Convad(
              in_channels=lowercase   ,    out_channels=lowercase   ,    kernel_size=lowercase   ,    stride=lowercase   ,    padding=lowercase   ,    groups=lowercase   ,    bias=lowercase   ,    padding_mode='zeros'   ,    )

          if use_normalization:
                 a__		:      Optional[int]					=							nn.BatchNormad(
                     num_features=lowercase   ,    eps=config.layer_norm_eps   ,    momentum=0.99_97   ,    affine=lowercase   ,    track_running_stats=lowercase   ,    )
          else:
                 a__		:      List[Any]					=							None

          if use_activation:
                 if isinstance(lowercase   ,    lowercase):
                        a__		:      Any					=							ACTaFN[use_activation]
                 elif isinstance(config.hidden_act   ,    lowercase):
                        a__		:      Any					=							ACTaFN[config.hidden_act]
                 else:
                        a__		:      str					=							config.hidden_act
          else:
                 a__		:      Tuple					=							None

   def 				__lowercase						(   self   ,    lowercase)     ->		torch.Tensor:







          '''simple docstring'''

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





class 					A__       (				__lowercase      ):




   """simple docstring"""



   __A  :							Any			    =    MobileNetVaConfig
   __A  :							Optional[int]			    =    load_tf_weights_in_mobilenet_va
   __A  :							int			    =    '''mobilenet_v1'''
   __A  :							int			    =    '''pixel_values'''
   __A  :							str			    =    False

   def 				__lowercase						(   self   ,    lowercase)     ->		None:







          '''simple docstring'''

          if isinstance(lowercase   ,    (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(lowercase   ,    nn.BatchNormad):
                 module.bias.data.zero_()
                 module.weight.data.fill_(1.0)

lowercase :			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'

lowercase :			str     		=    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.'''					,    __lowercase					,    )
class 					A__       (				__lowercase      ):




   """simple docstring"""



   def __init__(   self   ,    lowercase   ,    lowercase = True)     ->		Union[str, Any]:







          '''simple docstring'''

          super().__init__(lowercase)
          a__		:      Dict					=							config

          a__		:      List[str]					=							32
          a__		:      Optional[int]					=							max(int(depth * config.depth_multiplier)   ,    config.min_depth)

          a__		:      Optional[int]					=							MobileNetVaConvLayer(
              lowercase   ,    in_channels=config.num_channels   ,    out_channels=lowercase   ,    kernel_size=3   ,    stride=2   ,    )

          a__		:      Optional[Any]					=							[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

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

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

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

          a__		:      Optional[int]					=							nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None

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

   def 				__lowercase						(   self   ,    lowercase)     ->		Tuple:







          '''simple docstring'''

          raise NotImplementedError

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







          '''simple docstring'''

          a__		:      List[Any]					=							(
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          a__		:      List[str]					=							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')

          a__		:      Dict					=							self.conv_stem(lowercase)

          a__		:      List[str]					=							() if output_hidden_states else None

          for i, layer_module in enumerate(self.layer):
                 a__		:      List[Any]					=							layer_module(lowercase)

                 if output_hidden_states:
                        a__		:      Dict					=							all_hidden_states + (hidden_states,)

          a__		:      Any					=							hidden_states

          if self.pooler is not None:
                 a__		:      str					=							torch.flatten(self.pooler(lowercase)   ,    start_dim=1)
          else:
                 a__		:      int					=							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=lowercase   ,    pooler_output=lowercase   ,    hidden_states=lowercase   ,    )





@add_start_docstrings(
    '''\n    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '''					,    __lowercase					,    )
class 					A__       (				__lowercase      ):




   """simple docstring"""



   def __init__(   self   ,    lowercase)     ->		None:







          '''simple docstring'''

          super().__init__(lowercase)

          a__		:      List[Any]					=							config.num_labels
          a__		:      Any					=							MobileNetVaModel(lowercase)

          a__		:      Tuple					=							self.mobilenet_va.layer[-1].convolution.out_channels

          # Classifier head
          a__		:      List[str]					=							nn.Dropout(config.classifier_dropout_prob   ,    inplace=lowercase)
          a__		:      Optional[int]					=							nn.Linear(lowercase   ,    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(lowercase)
   @add_code_sample_docstrings(
       checkpoint=_IMAGE_CLASS_CHECKPOINT   ,    output_type=lowercase   ,    config_class=_CONFIG_FOR_DOC   ,    expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,    )
   def 				__lowercase						(   self   ,    lowercase = None   ,    lowercase = None   ,    lowercase = None   ,    lowercase = None   ,    )     ->		Union[tuple, ImageClassifierOutputWithNoAttention]:







          '''simple docstring'''

          a__		:      Union[str, Any]					=							return_dict if return_dict is not None else self.config.use_return_dict

          a__		:      int					=							self.mobilenet_va(lowercase   ,    output_hidden_states=lowercase   ,    return_dict=lowercase)

          a__		:      Union[str, Any]					=							outputs.pooler_output if return_dict else outputs[1]

          a__		:      Dict					=							self.classifier(self.dropout(lowercase))

          a__		:      Any					=							None
          if labels is not None:
                 if self.config.problem_type is None:
                        if self.num_labels == 1:
                               a__		:      Union[str, Any]					=							"regression"
                        elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                               a__		:      List[Any]					=							"single_label_classification"
                        else:
                               a__		:      List[str]					=							"multi_label_classification"

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

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

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




import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
		import tensorflow as tf

if is_tensorflow_text_available():
		from transformers.models.bert import TFBertTokenizer


lowercase    :   Union[str, Any]						=			['bert-base-uncased', 'bert-base-cased']
lowercase    :   Any						=			'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():




		class 					lowerCamelCase__				(							tf.keras.Model):




			'''simple docstring'''




			def __init__(   self						:List[Any]	,  a						:int		)						->					int:
										super().__init__()
										__UpperCamelCase      :  Any       =   tokenizer
										__UpperCamelCase      :  Union[str, Any]       =   AutoConfig.from_pretrained(a		)
										__UpperCamelCase      :  Dict       =   TFAutoModel.from_config(a		)







			def   _lowerCamelCase							(   self						:List[Any]	,  a						:Dict		)						->					Any:
										__UpperCamelCase      :  int       =   self.tokenizer(a		)
										__UpperCamelCase      :  int       =   self.bert(**a		)
										return out["pooler_output"]




@require_tf
@require_tensorflow_text
class 					lowerCamelCase__				(							unittest.TestCase):




	'''simple docstring'''




	def   _lowerCamelCase							(   self						:str		)						->					str:
								super().setUp()

								__UpperCamelCase      :  Dict       =   [
								    BertTokenizer.from_pretrained(a		) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
								]  # repeat for when fast_bert_tokenizer=false
								__UpperCamelCase      :  List[Any]       =   [TFBertTokenizer.from_pretrained(a		) for checkpoint in TOKENIZER_CHECKPOINTS] + [
								    TFBertTokenizer.from_pretrained(a	,  use_fast_bert_tokenizer=a		)
								    for checkpoint in TOKENIZER_CHECKPOINTS
								]
								assert len(self.tokenizers		) == len(self.tf_tokenizers		)

								__UpperCamelCase      :  Dict       =   [
								    "This is a straightforward English test sentence.",
								    "This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.",
								    "Now we're going to add some Chinese: 一 二 三 一二三",
								    "And some much more rare Chinese: 齉 堃 齉堃",
								    "Je vais aussi écrire en français pour tester les accents",
								    "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ",
								]
								__UpperCamelCase      :  Dict       =   list(zip(self.test_sentences	,  self.test_sentences[::-1]		)		)







	def   _lowerCamelCase							(   self						:List[Any]		)						->					int:
								for tokenizer, tf_tokenizer in zip(self.tokenizers	,  self.tf_tokenizers		):
															for test_inputs in (self.test_sentences, self.paired_sentences):
																						__UpperCamelCase      :  str       =   tokenizer(a	,  return_tensors="tf"	,  padding="longest"		)
																						__UpperCamelCase      :  Any       =   tf_tokenizer(a		)

																						for key in python_outputs.keys():
																													self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape		)		)
																													self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]	,  tf.intaa		) == tf_outputs[key]		)		)







	@slow
	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					Any:
								for tf_tokenizer in self.tf_tokenizers:
															__UpperCamelCase      :  List[str]       =   tf_tokenizer(self.paired_sentences		)
															__UpperCamelCase      :  Union[str, Any]       =   tf_tokenizer(
															    text=[sentence[0] for sentence in self.paired_sentences]	,  text_pair=[sentence[1] for sentence in self.paired_sentences]	,  )
															for key in merged_outputs.keys():
																						self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]	,  tf.intaa		) == separated_outputs[key]		)		)







	@slow
	def   _lowerCamelCase							(   self						:Any		)						->					Optional[int]:
								for tf_tokenizer in self.tf_tokenizers:
															__UpperCamelCase      :  Optional[Any]       =   tf.function(a		)
															for test_inputs in (self.test_sentences, self.paired_sentences):
																						__UpperCamelCase      :  Tuple       =   tf.constant(a		)
																						__UpperCamelCase      :  List[str]       =   compiled_tokenizer(a		)
																						__UpperCamelCase      :  Any       =   tf_tokenizer(a		)

																						for key in eager_outputs.keys():
																													self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]		)		)







	@slow
	def   _lowerCamelCase							(   self						:List[Any]		)						->					Tuple:
								for tf_tokenizer in self.tf_tokenizers:
															__UpperCamelCase      :  str       =   ModelToSave(tokenizer=a		)
															__UpperCamelCase      :  List[Any]       =   tf.convert_to_tensor(self.test_sentences		)
															__UpperCamelCase      :  Optional[int]       =   model(a		)  # Build model with some sample inputs
															with TemporaryDirectory() as tempdir:
																						__UpperCamelCase      :  Optional[Any]       =   Path(a		) / "saved.model"
																						model.save(a		)
																						__UpperCamelCase      :  List[Any]       =   tf.keras.models.load_model(a		)
															__UpperCamelCase      :  Optional[Any]       =   loaded_model(a		)
															# We may see small differences because the loaded model is compiled, so we need an epsilon for the test
															self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output		)		)	,  1E-5		)