





'''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__    :       Union[str, Any]              =logging.get_logger(__name__)


# General docstring
A__    :       Union[str, Any]              ='''MobileNetV1Config'''

# Base docstring
A__    :       Tuple              ='''google/mobilenet_v1_1.0_224'''
A__    :       Dict              =[1, 10_24, 7, 7]

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


A__    :       Any              =[
    '''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        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None  ):





      """simple docstring"""


      _lowerCAmelCase												=							{}

      if isinstance(lowerCAmelCase							,     lowerCAmelCase  ):
            _lowerCAmelCase												=							model.mobilenet_va
      else:
            _lowerCAmelCase												=							model

      _lowerCAmelCase												=							"""MobilenetV1/Conv2d_0/"""
      _lowerCAmelCase												=							backbone.conv_stem.convolution.weight
      _lowerCAmelCase												=							backbone.conv_stem.normalization.bias
      _lowerCAmelCase												=							backbone.conv_stem.normalization.weight
      _lowerCAmelCase												=							backbone.conv_stem.normalization.running_mean
      _lowerCAmelCase												=							backbone.conv_stem.normalization.running_var

      for i in range(13  ):
            _lowerCAmelCase												=							i + 1
            _lowerCAmelCase												=							i * 2

            _lowerCAmelCase												=							backbone.layer[pt_index]
            _lowerCAmelCase												=							f"MobilenetV1/Conv2d_{tf_index}_depthwise/"
            _lowerCAmelCase												=							pointer.convolution.weight
            _lowerCAmelCase												=							pointer.normalization.bias
            _lowerCAmelCase												=							pointer.normalization.weight
            _lowerCAmelCase												=							pointer.normalization.running_mean
            _lowerCAmelCase												=							pointer.normalization.running_var

            _lowerCAmelCase												=							backbone.layer[pt_index + 1]
            _lowerCAmelCase												=							f"MobilenetV1/Conv2d_{tf_index}_pointwise/"
            _lowerCAmelCase												=							pointer.convolution.weight
            _lowerCAmelCase												=							pointer.normalization.bias
            _lowerCAmelCase												=							pointer.normalization.weight
            _lowerCAmelCase												=							pointer.normalization.running_mean
            _lowerCAmelCase												=							pointer.normalization.running_var

      if isinstance(lowerCAmelCase							,     lowerCAmelCase  ):
            _lowerCAmelCase												=							"""MobilenetV1/Logits/Conv2d_1c_1x1/"""
            _lowerCAmelCase												=							model.classifier.weight
            _lowerCAmelCase												=							model.classifier.bias

      return tf_to_pt_map
def        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase  ):





      """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
      _lowerCAmelCase												=							tf.train.list_variables(lowerCAmelCase  )
      _lowerCAmelCase												=							{}
      for name, shape in init_vars:
            logger.info(f"Loading TF weight {name} with shape {shape}"  )
            _lowerCAmelCase												=							tf.train.load_variable(lowerCAmelCase							,     lowerCAmelCase  )
            _lowerCAmelCase												=							array

      # Build TF to PyTorch weights loading map
      _lowerCAmelCase												=							_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

            _lowerCAmelCase												=							tf_weights[name]

            if "depthwise_weights" in name:
                  logger.info("""Transposing depthwise"""  )
                  _lowerCAmelCase												=							np.transpose(lowerCAmelCase							,     (2, 3, 0, 1)  )
            elif "weights" in name:
                  logger.info("""Transposing"""  )
                  if len(pointer.shape  ) == 2:  # copying into linear layer
                        _lowerCAmelCase												=							array.squeeze().transpose()
                  else:
                        _lowerCAmelCase												=							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}"  )
            _lowerCAmelCase												=							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        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase  ):





      """simple docstring"""


      _lowerCAmelCase					,							_lowerCAmelCase												=							features.shape[-2:]
      _lowerCAmelCase					,							_lowerCAmelCase												=							conv_layer.stride
      _lowerCAmelCase					,							_lowerCAmelCase												=							conv_layer.kernel_size

      if in_height % stride_height == 0:
            _lowerCAmelCase												=							max(kernel_height - stride_height							,     0  )
      else:
            _lowerCAmelCase												=							max(kernel_height - (in_height % stride_height)							,     0  )

      if in_width % stride_width == 0:
            _lowerCAmelCase												=							max(kernel_width - stride_width							,     0  )
      else:
            _lowerCAmelCase												=							max(kernel_width - (in_width % stride_width)							,     0  )

      _lowerCAmelCase												=							pad_along_width // 2
      _lowerCAmelCase												=							pad_along_width - pad_left
      _lowerCAmelCase												=							pad_along_height // 2
      _lowerCAmelCase												=							pad_along_height - pad_top

      _lowerCAmelCase												=							(pad_left, pad_right, pad_top, pad_bottom)
      return nn.functional.pad(lowerCAmelCase							,     lowerCAmelCase							,     """constant"""							,     0.0  )




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			Union[str, Any]      ,      __snake_case      :			MobileNetVaConfig      ,      __snake_case      :			int      ,      __snake_case      :			int      ,      __snake_case      :			int      ,      __snake_case      :			Optional[int] = 1      ,      __snake_case      :			Optional[int] = 1      ,      __snake_case      :			bool = False      ,      __snake_case      :			Optional[bool] = True      ,      __snake_case      :			Optional[bool or str] = True      ,      )    ->				None:
            super().__init__()
            _lowerCAmelCase												=							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."  )

            _lowerCAmelCase												=							0 if config.tf_padding else int((kernel_size - 1) / 2  )

            _lowerCAmelCase												=							nn.Convad(
                in_channels=__snake_case      ,      out_channels=__snake_case      ,      kernel_size=__snake_case      ,      stride=__snake_case      ,      padding=__snake_case      ,      groups=__snake_case      ,      bias=__snake_case      ,      padding_mode="""zeros"""      ,      )

            if use_normalization:
                  _lowerCAmelCase												=							nn.BatchNormad(
                      num_features=__snake_case      ,      eps=config.layer_norm_eps      ,      momentum=0.99_97      ,      affine=__snake_case      ,      track_running_stats=__snake_case      ,      )
            else:
                  _lowerCAmelCase												=							None

            if use_activation:
                  if isinstance(__snake_case      ,      __snake_case  ):
                        _lowerCAmelCase												=							ACTaFN[use_activation]
                  elif isinstance(config.hidden_act      ,      __snake_case  ):
                        _lowerCAmelCase												=							ACTaFN[config.hidden_act]
                  else:
                        _lowerCAmelCase												=							config.hidden_act
            else:
                  _lowerCAmelCase												=							None




      def     lowercase__   (      self      :			Dict      ,      __snake_case      :			torch.Tensor  )    ->				torch.Tensor:
            if self.config.tf_padding:
                  _lowerCAmelCase												=							apply_tf_padding(__snake_case      ,      self.convolution  )
            _lowerCAmelCase												=							self.convolution(__snake_case  )
            if self.normalization is not None:
                  _lowerCAmelCase												=							self.normalization(__snake_case  )
            if self.activation is not None:
                  _lowerCAmelCase												=							self.activation(__snake_case  )
            return features




class 			UpperCAmelCase					(      snake_case_       ):
      _lowercase:      Any											=       MobileNetVaConfig
      _lowercase:      Union[str, Any]											=       load_tf_weights_in_mobilenet_va
      _lowercase:      Optional[int]											=       '''mobilenet_v1'''
      _lowercase:      str											=       '''pixel_values'''
      _lowercase:      List[str]											=       False




      def     lowercase__   (      self      :			Union[str, Any]      ,      __snake_case      :			Union[nn.Linear, nn.Convad]  )    ->				None:
            if isinstance(__snake_case      ,      (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(__snake_case      ,      nn.BatchNormad  ):
                  module.bias.data.zero_()
                  module.weight.data.fill_(1.0  )




A__    :       List[str]              =r'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

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




@add_start_docstrings(
    '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.'''							,		snake_case_							,		)
class 			UpperCAmelCase					(      snake_case_       ):




      def __init__(      self      :			Any      ,      __snake_case      :			MobileNetVaConfig      ,      __snake_case      :			bool = True  )    ->				str:
            super().__init__(__snake_case  )
            _lowerCAmelCase												=							config

            _lowerCAmelCase												=							32
            _lowerCAmelCase												=							max(int(depth * config.depth_multiplier  )      ,      config.min_depth  )

            _lowerCAmelCase												=							MobileNetVaConvLayer(
                __snake_case      ,      in_channels=config.num_channels      ,      out_channels=__snake_case      ,      kernel_size=3      ,      stride=2      ,      )

            _lowerCAmelCase												=							[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

            _lowerCAmelCase												=							nn.ModuleList()
            for i in range(13  ):
                  _lowerCAmelCase												=							out_channels

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

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

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

            _lowerCAmelCase												=							nn.AdaptiveAvgPoolad((1, 1)  ) if add_pooling_layer else None

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




      def     lowercase__   (      self      :			Dict      ,      __snake_case      :			Optional[int]  )    ->				List[str]:
            raise NotImplementedError




      @add_start_docstrings_to_model_forward(__snake_case  )
      @add_code_sample_docstrings(
          checkpoint=_CHECKPOINT_FOR_DOC      ,      output_type=__snake_case      ,      config_class=_CONFIG_FOR_DOC      ,      modality="""vision"""      ,      expected_output=_EXPECTED_OUTPUT_SHAPE      ,      )
      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Optional[torch.Tensor] = None      ,      __snake_case      :			Optional[bool] = None      ,      __snake_case      :			Optional[bool] = None      ,      )    ->				Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
            _lowerCAmelCase												=							(
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            _lowerCAmelCase												=							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"""  )

            _lowerCAmelCase												=							self.conv_stem(__snake_case  )

            _lowerCAmelCase												=							() if output_hidden_states else None

            for i, layer_module in enumerate(self.layer  ):
                  _lowerCAmelCase												=							layer_module(__snake_case  )

                  if output_hidden_states:
                        _lowerCAmelCase												=							all_hidden_states + (hidden_states,)

            _lowerCAmelCase												=							hidden_states

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




@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.
    '''							,		snake_case_							,		)
class 			UpperCAmelCase					(      snake_case_       ):




      def __init__(      self      :			List[Any]      ,      __snake_case      :			MobileNetVaConfig  )    ->				None:
            super().__init__(__snake_case  )

            _lowerCAmelCase												=							config.num_labels
            _lowerCAmelCase												=							MobileNetVaModel(__snake_case  )

            _lowerCAmelCase												=							self.mobilenet_va.layer[-1].convolution.out_channels

            # Classifier head
            _lowerCAmelCase												=							nn.Dropout(config.classifier_dropout_prob      ,      inplace=__snake_case  )
            _lowerCAmelCase												=							nn.Linear(__snake_case      ,      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(__snake_case  )
      @add_code_sample_docstrings(
          checkpoint=_IMAGE_CLASS_CHECKPOINT      ,      output_type=__snake_case      ,      config_class=_CONFIG_FOR_DOC      ,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,      )
      def     lowercase__   (      self      :			int      ,      __snake_case      :			Optional[torch.Tensor] = None      ,      __snake_case      :			Optional[bool] = None      ,      __snake_case      :			Optional[torch.Tensor] = None      ,      __snake_case      :			Optional[bool] = None      ,      )    ->				Union[tuple, ImageClassifierOutputWithNoAttention]:
            _lowerCAmelCase												=							return_dict if return_dict is not None else self.config.use_return_dict

            _lowerCAmelCase												=							self.mobilenet_va(__snake_case      ,      output_hidden_states=__snake_case      ,      return_dict=__snake_case  )

            _lowerCAmelCase												=							outputs.pooler_output if return_dict else outputs[1]

            _lowerCAmelCase												=							self.classifier(self.dropout(__snake_case  )  )

            _lowerCAmelCase												=							None
            if labels is not None:
                  if self.config.problem_type is None:
                        if self.num_labels == 1:
                              _lowerCAmelCase												=							"""regression"""
                        elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                              _lowerCAmelCase												=							"""single_label_classification"""
                        else:
                              _lowerCAmelCase												=							"""multi_label_classification"""

                  if self.config.problem_type == "regression":
                        _lowerCAmelCase												=							MSELoss()
                        if self.num_labels == 1:
                              _lowerCAmelCase												=							loss_fct(logits.squeeze()      ,      labels.squeeze()  )
                        else:
                              _lowerCAmelCase												=							loss_fct(__snake_case      ,      __snake_case  )
                  elif self.config.problem_type == "single_label_classification":
                        _lowerCAmelCase												=							CrossEntropyLoss()
                        _lowerCAmelCase												=							loss_fct(logits.view(-1      ,      self.num_labels  )      ,      labels.view(-1  )  )
                  elif self.config.problem_type == "multi_label_classification":
                        _lowerCAmelCase												=							BCEWithLogitsLoss()
                        _lowerCAmelCase												=							loss_fct(__snake_case      ,      __snake_case  )

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

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













'''simple docstring'''






def        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase  ):





      """simple docstring"""


      if not (isinstance(lowerCAmelCase							,     lowerCAmelCase  ) and isinstance(lowerCAmelCase							,     lowerCAmelCase  )):
            raise ValueError("""longest_common_substring() takes two strings for inputs"""  )

      _lowerCAmelCase												=							len(lowerCAmelCase  )
      _lowerCAmelCase												=							len(lowerCAmelCase  )

      _lowerCAmelCase												=							[[0] * (texta_length + 1) for _ in range(texta_length + 1  )]
      _lowerCAmelCase												=							0
      _lowerCAmelCase												=							0

      for i in range(1							,     texta_length + 1  ):
            for j in range(1							,     texta_length + 1  ):
                  if texta[i - 1] == texta[j - 1]:
                        _lowerCAmelCase												=							1 + dp[i - 1][j - 1]
                        if dp[i][j] > ans_length:
                              _lowerCAmelCase												=							i
                              _lowerCAmelCase												=							dp[i][j]

      return texta[ans_index - ans_length : ans_index]


if __name__ == "__main__":
      import doctest

      doctest.testmod()






