
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_  :	Union[str, Any] 				=					"""MobileNetV1Config"""

# Base docstring
lowerCamelCase_  :	Optional[int] 				=					"""google/mobilenet_v1_1.0_224"""
lowerCamelCase_  :	List[str] 				=					[1, 10_24, 7, 7]

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


lowerCamelCase_  :	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     A__   (  lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase=None  )					->		Tuple:
				UpperCamelCase_:							int		=  {}

				if isinstance(lowerCamelCase       ,      lowerCamelCase  ):
								UpperCamelCase_:							Any		=  model.mobilenet_va
				else:
								UpperCamelCase_:							Any		=  model

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

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

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

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

				if isinstance(lowerCamelCase       ,      lowerCamelCase  ):
								UpperCamelCase_:							Union[str, Any]		=  """MobilenetV1/Logits/Conv2d_1c_1x1/"""
								UpperCamelCase_:							Dict		=  model.classifier.weight
								UpperCamelCase_:							Any		=  model.classifier.bias

				return tf_to_pt_map







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

				# Build TF to PyTorch weights loading map
				UpperCamelCase_:							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

								UpperCamelCase_:							str		=  tf_weights[name]

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

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

				if in_width % stride_width == 0:
								UpperCamelCase_:							int		=  max(kernel_width - stride_width       ,      0  )
				else:
								UpperCamelCase_:							Optional[int]		=  max(kernel_width - (in_width % stride_width)       ,      0  )

				UpperCamelCase_:							List[Any]		=  pad_along_width // 2
				UpperCamelCase_:							Dict		=  pad_along_width - pad_left
				UpperCamelCase_:							List[Any]		=  pad_along_height // 2
				UpperCamelCase_:							int		=  pad_along_height - pad_top

				UpperCamelCase_:							str		=  (pad_left, pad_right, pad_top, pad_bottom)
				return nn.functional.pad(lowerCamelCase       ,      lowerCamelCase       ,      """constant"""       ,      0.0  )

class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    List[str]  ,		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  ,		):
							super().__init__()
							UpperCamelCase_:							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.''' )

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

							UpperCamelCase_:							Tuple		=  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:
											UpperCamelCase_:							Any		=  nn.BatchNormad(
											    num_features=snake_case_  ,		eps=config.layer_norm_eps  ,		momentum=0.9997  ,		affine=snake_case_  ,		track_running_stats=snake_case_  ,		)
							else:
											UpperCamelCase_:							Any		=  None

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




			def  lowerCAmelCase__		(    self	:    List[Any]  ,		snake_case_	:    torch.Tensor ):
							if self.config.tf_padding:
											UpperCamelCase_:							Any		=  apply_tf_padding(snake_case_  ,		self.convolution )
							UpperCamelCase_:							Optional[int]		=  self.convolution(snake_case_ )
							if self.normalization is not None:
											UpperCamelCase_:							List[str]		=  self.normalization(snake_case_ )
							if self.activation is not None:
											UpperCamelCase_:							List[Any]		=  self.activation(snake_case_ )
							return features




class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  Optional[int]    =	MobileNetVaConfig
			__UpperCamelCase :  Any    =	load_tf_weights_in_mobilenet_va
			__UpperCamelCase :  Tuple    =	"""mobilenet_v1"""
			__UpperCamelCase :  Union[str, Any]    =	"""pixel_values"""
			__UpperCamelCase :  Optional[int]    =	False

			def  lowerCAmelCase__		(    self	:    Tuple  ,		snake_case_	:    Union[nn.Linear, nn.Convad] ):
							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 )




lowerCamelCase_  :	Optional[int] 				=					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_  :	str 				=					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."""       ,    _A       ,    )
class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			def __init__(    self	:    Union[str, Any]  ,		snake_case_	:    MobileNetVaConfig  ,		snake_case_	:    bool = True ):
							super().__init__(snake_case_ )
							UpperCamelCase_:							Any		=  config

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

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

							UpperCamelCase_:							List[str]		=  [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

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

							UpperCamelCase_:							Any		=  nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None

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

			def  lowerCAmelCase__		(    self	:    Tuple  ,		snake_case_	:    Any ):
							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  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    Optional[torch.Tensor] = None  ,		snake_case_	:    Optional[bool] = None  ,		snake_case_	:    Optional[bool] = None  ,		):
							UpperCamelCase_:							Optional[int]		=  (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCamelCase_:							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""" )

							UpperCamelCase_:							Optional[Any]		=  self.conv_stem(snake_case_ )

							UpperCamelCase_:							List[Any]		=  () if output_hidden_states else None

							for i, layer_module in enumerate(self.layer ):
											UpperCamelCase_:							Dict		=  layer_module(snake_case_ )

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

							UpperCamelCase_:							Tuple		=  hidden_states

							if self.pooler is not None:
											UpperCamelCase_:							Any		=  torch.flatten(self.pooler(snake_case_ )  ,		start_dim=1 )
							else:
											UpperCamelCase_:							Union[str, Any]		=  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.
    """       ,    _A       ,    )
class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			def __init__(    self	:    Union[str, Any]  ,		snake_case_	:    MobileNetVaConfig ):
							super().__init__(snake_case_ )

							UpperCamelCase_:							Optional[Any]		=  config.num_labels
							UpperCamelCase_:							Union[str, Any]		=  MobileNetVaModel(snake_case_ )

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

							# Classifier head
							UpperCamelCase_:							List[Any]		=  nn.Dropout(config.classifier_dropout_prob  ,		inplace=snake_case_ )
							UpperCamelCase_:							Optional[Any]		=  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  lowerCAmelCase__		(    self	:    int  ,		snake_case_	:    Optional[torch.Tensor] = None  ,		snake_case_	:    Optional[bool] = None  ,		snake_case_	:    Optional[torch.Tensor] = None  ,		snake_case_	:    Optional[bool] = None  ,		):
							UpperCamelCase_:							List[Any]		=  return_dict if return_dict is not None else self.config.use_return_dict

							UpperCamelCase_:							List[str]		=  self.mobilenet_va(snake_case_  ,		output_hidden_states=snake_case_  ,		return_dict=snake_case_ )

							UpperCamelCase_:							Any		=  outputs.pooler_output if return_dict else outputs[1]

							UpperCamelCase_:							Optional[int]		=  self.classifier(self.dropout(snake_case_ ) )

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

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

							if not return_dict:
											UpperCamelCase_:							Optional[int]		=  (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  ,		)








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


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

lowerCamelCase_  :	Optional[int] 				=					{"""ctrl""": """https://huggingface.co/ctrl/resolve/main/config.json"""}

class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  int    =	"""ctrl"""
			__UpperCamelCase :  Dict    =	["""past_key_values"""]
			__UpperCamelCase :  List[str]    =	{
			    """max_position_embeddings""": """n_positions""",
			    """hidden_size""": """n_embd""",
			    """num_attention_heads""": """n_head""",
			    """num_hidden_layers""": """n_layer""",
			}

			def __init__(    self	:    Dict  ,		snake_case_	:    Any=24_6534  ,		snake_case_	:    Dict=256  ,		snake_case_	:    str=1280  ,		snake_case_	:    Optional[int]=8192  ,		snake_case_	:    Union[str, Any]=48  ,		snake_case_	:    Any=16  ,		snake_case_	:    Optional[int]=0.1  ,		snake_case_	:    Any=0.1  ,		snake_case_	:    Any=1e-6  ,		snake_case_	:    Optional[Any]=0.02  ,		snake_case_	:    Optional[int]=True  ,		**snake_case_	:    Union[str, Any]  ,		):
							UpperCamelCase_:							Union[str, Any]		=  vocab_size
							UpperCamelCase_:							Union[str, Any]		=  n_positions
							UpperCamelCase_:							Optional[int]		=  n_embd
							UpperCamelCase_:							int		=  n_layer
							UpperCamelCase_:							str		=  n_head
							UpperCamelCase_:							Optional[int]		=  dff
							UpperCamelCase_:							Optional[Any]		=  resid_pdrop
							UpperCamelCase_:							Union[str, Any]		=  embd_pdrop
							UpperCamelCase_:							List[str]		=  layer_norm_epsilon
							UpperCamelCase_:							Optional[Any]		=  initializer_range

							UpperCamelCase_:							Optional[Any]		=  use_cache

							super().__init__(**snake_case_ )






