





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


lowerCAmelCase					:     Optional[int]												=logging.get_logger(__name__)


# General docstring
lowerCAmelCase					:     int												='''MobileNetV1Config'''

# Base docstring
lowerCAmelCase					:     Union[str, Any]												='''google/mobilenet_v1_1.0_224'''
lowerCAmelCase					:     Dict												=[1, 1_024, 7, 7]

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


lowerCAmelCase					:     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 							UpperCAmelCase_  (						__lowerCamelCase					:					Tuple	,__lowerCamelCase					:					List[str]	,__lowerCamelCase					:					Dict=None ):
   lowercase_			:Optional[int]    			= {}

   if isinstance(__lowerCamelCase	,__lowerCamelCase ):
      lowercase_			:Union[str, Any]    			= model.mobilenet_va
   else:
      lowercase_			:List[Any]    			= model

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

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

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

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

   if isinstance(__lowerCamelCase	,__lowerCamelCase ):
      lowercase_			:Tuple    			= "MobilenetV1/Logits/Conv2d_1c_1x1/"
      lowercase_			:Union[str, Any]    			= model.classifier.weight
      lowercase_			:int    			= model.classifier.bias

   return tf_to_pt_map
def 							UpperCAmelCase_  (						__lowerCamelCase					:					Optional[Any]	,__lowerCamelCase					:					Optional[Any]	,__lowerCamelCase					:					int ):
   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_			:List[str]    			= tf.train.list_variables(__lowerCamelCase )
   lowercase_			:Optional[Any]    			= {}
   for name, shape in init_vars:
      logger.info(F'Loading TF weight {name} with shape {shape}' )
      lowercase_			:List[Any]    			= tf.train.load_variable(__lowerCamelCase	,__lowerCamelCase )
      lowercase_			:int    			= array

   # Build TF to PyTorch weights loading map
   lowercase_			:List[str]    			= _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

      lowercase_			:Optional[int]    			= tf_weights[name]

      if "depthwise_weights" in name:
         logger.info("Transposing depthwise" )
         lowercase_			:Any    			= np.transpose(__lowerCamelCase	,(2, 3, 0, 1) )
      elif "weights" in name:
         logger.info("Transposing" )
         if len(pointer.shape ) == 2:  # copying into linear layer
            lowercase_			:int    			= array.squeeze().transpose()
         else:
            lowercase_			:Union[str, 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}' )
      lowercase_			: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 							UpperCAmelCase_  (						__lowerCamelCase					:					torch.Tensor	,__lowerCamelCase					:					nn.Convad ):
   lowercase_			,							lowercase_			:Optional[int]    			= features.shape[-2:]
   lowercase_			,							lowercase_			:Optional[int]    			= conv_layer.stride
   lowercase_			,							lowercase_			:int    			= conv_layer.kernel_size

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

   if in_width % stride_width == 0:
      lowercase_			:Optional[Any]    			= max(kernel_width - stride_width	,0 )
   else:
      lowercase_			:str    			= max(kernel_width - (in_width % stride_width)	,0 )

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

   lowercase_			:List[str]    			= (pad_left, pad_right, pad_top, pad_bottom)
   return nn.functional.pad(__lowerCamelCase	,__lowerCamelCase	,"constant"	,0.0 )
class       a_   (				nn.Module							):





     def __init__( self		:					List[Any]	,      lowercase		:					MobileNetVaConfig	,      lowercase		:					int	,      lowercase		:					int	,      lowercase		:					int	,      lowercase		:					Optional[int] = 1	,      lowercase		:					Optional[int] = 1	,      lowercase		:					bool = False	,      lowercase		:					Optional[bool] = True	,      lowercase		:					Optional[bool or str] = True	,      ):





        """simple docstring"""




        super().__init__()
        lowercase_			:Union[str, Any]    			= 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_			:Tuple    			= 0 if config.tf_padding else int((kernel_size - 1) / 2						)

        lowercase_			:Optional[Any]    			= 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:
           lowercase_			:int    			= nn.BatchNormad(
               num_features=lowercase	,      eps=config.layer_norm_eps	,      momentum=0.99_97	,      affine=lowercase	,      track_running_stats=lowercase	,      )
        else:
           lowercase_			:List[str]    			= None

        if use_activation:
           if isinstance(lowercase	,      lowercase						):
              lowercase_			:List[Any]    			= ACTaFN[use_activation]
           elif isinstance(config.hidden_act	,      lowercase						):
              lowercase_			:List[str]    			= ACTaFN[config.hidden_act]
           else:
              lowercase_			:int    			= config.hidden_act
        else:
           lowercase_			:Dict    			= None







     def 							lowercase__      ( self		:					Union[str, Any]	,      lowercase		:					torch.Tensor						):





        """simple docstring"""




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







class       a_   (				_lowerCAmelCase							):
     __A      =			MobileNetVaConfig
     __A      =			load_tf_weights_in_mobilenet_va
     __A      =			"mobilenet_v1"
     __A      =			"pixel_values"
     __A      =			False





     def 							lowercase__      ( self		:					Optional[int]	,      lowercase		:					Union[nn.Linear, nn.Convad]						):





        """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						)







lowerCAmelCase					:     List[Any]												=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.
'''

lowerCAmelCase					:     Tuple												=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."	,						_lowerCAmelCase	,						)
class       a_   (				_lowerCAmelCase							):





     def __init__( self		:					Optional[Any]	,      lowercase		:					MobileNetVaConfig	,      lowercase		:					bool = True						):





        """simple docstring"""




        super().__init__(lowercase						)
        lowercase_			:str    			= config

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

        lowercase_			:int    			= MobileNetVaConvLayer(
            lowercase	,      in_channels=config.num_channels	,      out_channels=lowercase	,      kernel_size=3	,      stride=2	,      )

        lowercase_			:List[Any]    			= [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

        lowercase_			:Optional[Any]    			= nn.ModuleList()
        for i in range(13						):
           lowercase_			:Tuple    			= out_channels

           if strides[i] == 2 or i == 0:
              depth *= 2
              lowercase_			:List[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	,      )						)

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

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





     def 							lowercase__      ( self		:					List[str]	,      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		:					str	,      lowercase		:					Optional[torch.Tensor] = None	,      lowercase		:					Optional[bool] = None	,      lowercase		:					Optional[bool] = None	,      ):





        """simple docstring"""




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

        lowercase_			:Any    			= () if output_hidden_states else None

        for i, layer_module in enumerate(self.layer						):
           lowercase_			:Any    			= layer_module(lowercase						)

           if output_hidden_states:
              lowercase_			:int    			= all_hidden_states + (hidden_states,)

        lowercase_			:Union[str, Any]    			= hidden_states

        if self.pooler is not None:
           lowercase_			:Tuple    			= torch.flatten(self.pooler(lowercase						)	,      start_dim=1						)
        else:
           lowercase_			:List[str]    			= 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    "	,						_lowerCAmelCase	,						)
class       a_   (				_lowerCAmelCase							):





     def __init__( self		:					Any	,      lowercase		:					MobileNetVaConfig						):





        """simple docstring"""




        super().__init__(lowercase						)

        lowercase_			:str    			= config.num_labels
        lowercase_			:Dict    			= MobileNetVaModel(lowercase						)

        lowercase_			:Optional[int]    			= self.mobilenet_va.layer[-1].convolution.out_channels

        # Classifier head
        lowercase_			:str    			= nn.Dropout(config.classifier_dropout_prob	,      inplace=lowercase						)
        lowercase_			:List[Any]    			= 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		:					Union[str, Any]	,      lowercase		:					Optional[torch.Tensor] = None	,      lowercase		:					Optional[bool] = None	,      lowercase		:					Optional[torch.Tensor] = None	,      lowercase		:					Optional[bool] = None	,      ):





        """simple docstring"""




        lowercase_			:List[str]    			= return_dict if return_dict is not None else self.config.use_return_dict

        lowercase_			:Optional[int]    			= self.mobilenet_va(lowercase	,      output_hidden_states=lowercase	,      return_dict=lowercase						)

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

        lowercase_			:Tuple    			= self.classifier(self.dropout(lowercase						)						)

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

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

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

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









'''simple docstring'''


from typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase					:     Optional[int]												=logging.get_logger(__name__)

lowerCAmelCase					:     Tuple												={
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}
class       a_   (				_lowerCAmelCase							):
     __A      =			"autoformer"
     __A      =			{
         "hidden_size": "d_model",
         "num_attention_heads": "encoder_attention_heads",
         "num_hidden_layers": "encoder_layers",
     }





     def __init__( self		:					Any	,      lowercase		:					Optional[int] = None	,      lowercase		:					Optional[int] = None	,      lowercase		:					str = "student_t"	,      lowercase		:					str = "nll"	,      lowercase		:					int = 1	,      lowercase		:					List[int] = [1, 2, 3, 4, 5, 6, 7]	,      lowercase		:					bool = True	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					Optional[List[int]] = None	,      lowercase		:					Optional[List[int]] = None	,      lowercase		:					int = 64	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 32	,      lowercase		:					int = 32	,      lowercase		:					str = "gelu"	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					int = 100	,      lowercase		:					float = 0.02	,      lowercase		:					bool = True	,      lowercase		:					int=True	,      lowercase		:					int = 10	,      lowercase		:					int = 25	,      lowercase		:					int = 3	,      **lowercase		:					Dict	,      ):





        """simple docstring"""




        lowercase_			:str    			= prediction_length
        lowercase_			:Dict    			= context_length if context_length is not None else prediction_length
        lowercase_			:Any    			= distribution_output
        lowercase_			:Tuple    			= loss
        lowercase_			:Dict    			= input_size
        lowercase_			:Tuple    			= num_time_features
        lowercase_			:int    			= lags_sequence
        lowercase_			:Tuple    			= scaling
        lowercase_			:List[Any]    			= num_dynamic_real_features
        lowercase_			:Union[str, Any]    			= num_static_real_features
        lowercase_			:str    			= num_static_categorical_features
        if cardinality is not None and num_static_categorical_features > 0:
           if len(lowercase						) != num_static_categorical_features:
              raise ValueError(
                  "The cardinality should be a list of the same length as `num_static_categorical_features`"						)
           lowercase_			:Optional[int]    			= cardinality
        else:
           lowercase_			:Optional[int]    			= [0]
        if embedding_dimension is not None and num_static_categorical_features > 0:
           if len(lowercase						) != num_static_categorical_features:
              raise ValueError(
                  "The embedding dimension should be a list of the same length as `num_static_categorical_features`"						)
           lowercase_			:Tuple    			= embedding_dimension
        else:
           lowercase_			:Dict    			= [min(50	,      (cat + 1) // 2						) for cat in self.cardinality]
        lowercase_			:Any    			= num_parallel_samples

        # Transformer architecture configuration
        lowercase_			:Tuple    			= input_size * len(self.lags_sequence						) + self._number_of_features
        lowercase_			:Union[str, Any]    			= d_model
        lowercase_			:Optional[Any]    			= encoder_attention_heads
        lowercase_			:Optional[Any]    			= decoder_attention_heads
        lowercase_			:Optional[int]    			= encoder_ffn_dim
        lowercase_			:int    			= decoder_ffn_dim
        lowercase_			:Any    			= encoder_layers
        lowercase_			:Optional[Any]    			= decoder_layers

        lowercase_			:Dict    			= dropout
        lowercase_			:Dict    			= attention_dropout
        lowercase_			:str    			= activation_dropout
        lowercase_			:int    			= encoder_layerdrop
        lowercase_			:Dict    			= decoder_layerdrop

        lowercase_			:List[str]    			= activation_function
        lowercase_			:int    			= init_std

        lowercase_			:Optional[int]    			= use_cache

        # Autoformer
        lowercase_			:List[str]    			= label_length
        lowercase_			:List[str]    			= moving_average
        lowercase_			:Optional[int]    			= autocorrelation_factor

        super().__init__(is_encoder_decoder=lowercase	,      **lowercase						)







     @property
     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        return (
            sum(self.embedding_dimension						)
            + self.num_dynamic_real_features
            + self.num_time_features
            + self.num_static_real_features
            + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
        )


