





"""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						: Optional[int]     	=			logging.get_logger(__name__)


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

# Base docstring
a						: str     	=			'''google/mobilenet_v1_1.0_224'''
a						: str     	=			[1, 1024, 7, 7]

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


a						: 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       _SCREAMING_SNAKE_CASE		(	_lowercase						:    List[Any]    ,       _lowercase						:    str    ,       _lowercase						:    int=None					) ->int:






	'''simple docstring'''






	a      :		List[Any]	=				{}

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

	a      :		Dict	=				"MobilenetV1/Conv2d_0/"
	a      :		Tuple	=				backbone.conv_stem.convolution.weight
	a      :		Dict	=				backbone.conv_stem.normalization.bias
	a      :		Optional[Any]	=				backbone.conv_stem.normalization.weight
	a      :		Optional[Any]	=				backbone.conv_stem.normalization.running_mean
	a      :		Tuple	=				backbone.conv_stem.normalization.running_var

	for i in range(13					):
		a      :		List[str]	=				i + 1
		a      :		Dict	=				i * 2

		a      :		int	=				backbone.layer[pt_index]
		a      :		List[str]	=				F"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
		a      :		int	=				pointer.convolution.weight
		a      :		Union[str, Any]	=				pointer.normalization.bias
		a      :		Union[str, Any]	=				pointer.normalization.weight
		a      :		Optional[Any]	=				pointer.normalization.running_mean
		a      :		Dict	=				pointer.normalization.running_var

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

	if isinstance(_lowercase    ,       _lowercase					):
		a      :		Dict	=				"MobilenetV1/Logits/Conv2d_1c_1x1/"
		a      :		Tuple	=				model.classifier.weight
		a      :		Optional[int]	=				model.classifier.bias

	return tf_to_pt_map






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Any    ,       _lowercase						:    List[Any]    ,       _lowercase						:    Tuple					) ->int:






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

	# Build TF to PyTorch weights loading map
	a      :		Tuple	=				_build_tf_to_pytorch_map(_lowercase    ,       _lowercase    ,       _lowercase					)

	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      :		List[str]	=				tf_weights[name]

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

		tf_weights.pop(_lowercase    ,       _lowercase					)
		tf_weights.pop(name + "/RMSProp"    ,       _lowercase					)
		tf_weights.pop(name + "/RMSProp_1"    ,       _lowercase					)
		tf_weights.pop(name + "/ExponentialMovingAverage"    ,       _lowercase					)

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






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    torch.Tensor    ,       _lowercase						:    nn.Convad					) ->torch.Tensor:






	'''simple docstring'''






	a,       a      :		Any	=				features.shape[-2:]
	a,       a      :		Dict	=				conv_layer.stride
	a,       a      :		int	=				conv_layer.kernel_size

	if in_height % stride_height == 0:
		a      :		Tuple	=				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      :		Optional[Any]	=				max(kernel_width - stride_width    ,       0					)
	else:
		a      :		str	=				max(kernel_width - (in_width % stride_width)    ,       0					)

	a      :		Any	=				pad_along_width // 2
	a      :		List[str]	=				pad_along_width - pad_left
	a      :		List[str]	=				pad_along_height // 2
	a      :		List[Any]	=				pad_along_height - pad_top

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


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = 1      ,					lowerCAmelCase__ = 1      ,					lowerCAmelCase__ = False      ,					lowerCAmelCase__ = True      ,					lowerCAmelCase__ = True      ,					)	-> None:
								super().__init__()
								a      :		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."""      )

								a      :		Optional[int]	=				0 if config.tf_padding else int((kernel_size - 1) / 2      )

								a      :		Tuple	=				nn.Convad(
								    in_channels=lowerCAmelCase__      ,					out_channels=lowerCAmelCase__      ,					kernel_size=lowerCAmelCase__      ,					stride=lowerCAmelCase__      ,					padding=lowerCAmelCase__      ,					groups=lowerCAmelCase__      ,					bias=lowerCAmelCase__      ,					padding_mode="zeros"      ,					)

								if use_normalization:
									a      :		Optional[int]	=				nn.BatchNormad(
									    num_features=lowerCAmelCase__      ,					eps=config.layer_norm_eps      ,					momentum=0.9_997      ,					affine=lowerCAmelCase__      ,					track_running_stats=lowerCAmelCase__      ,					)
								else:
									a      :		int	=				None

								if use_activation:
									if isinstance(lowerCAmelCase__      ,					lowerCAmelCase__      ):
										a      :		Optional[int]	=				ACTaFN[use_activation]
									elif isinstance(config.hidden_act      ,					lowerCAmelCase__      ):
										a      :		Dict	=				ACTaFN[config.hidden_act]
									else:
										a      :		Union[str, Any]	=				config.hidden_act
								else:
									a      :		int	=				None






							def     __a   (   self      ,					lowerCAmelCase__      )	-> torch.Tensor:
								if self.config.tf_padding:
									a      :		Union[str, Any]	=				apply_tf_padding(lowerCAmelCase__      ,					self.convolution      )
								a      :		List[str]	=				self.convolution(lowerCAmelCase__      )
								if self.normalization is not None:
									a      :		int	=				self.normalization(lowerCAmelCase__      )
								if self.activation is not None:
									a      :		Dict	=				self.activation(lowerCAmelCase__      )
								return features


class        __UpperCamelCase				(						a__						):
							lowerCamelCase   :					List[str]      					=MobileNetVaConfig
							lowerCamelCase   :					str      					=load_tf_weights_in_mobilenet_va
							lowerCamelCase   :					List[str]      					="""mobilenet_v1"""
							lowerCamelCase   :					Tuple      					="""pixel_values"""
							lowerCamelCase   :					Optional[Any]      					=False






							def     __a   (   self      ,					lowerCAmelCase__      )	-> None:
								if isinstance(lowerCAmelCase__      ,					(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(lowerCAmelCase__      ,					nn.BatchNormad      ):
									module.bias.data.zero_()
									module.weight.data.fill_(1.0      )
a						: Optional[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.
'''

a						: List[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__						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = True      )	-> List[str]:
								super().__init__(lowerCAmelCase__      )
								a      :		Tuple	=				config

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

								a      :		Dict	=				MobileNetVaConvLayer(
								    lowerCAmelCase__      ,					in_channels=config.num_channels      ,					out_channels=lowerCAmelCase__      ,					kernel_size=3      ,					stride=2      ,					)

								a      :		Dict	=				[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

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

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

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

								a      :		Tuple	=				nn.AdaptiveAvgPoolad((1, 1)      ) if add_pooling_layer else None

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






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Optional[Any]:
								raise NotImplementedError






							@add_start_docstrings_to_model_forward(lowerCAmelCase__      )
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC      ,					output_type=lowerCAmelCase__      ,					config_class=_CONFIG_FOR_DOC      ,					modality="vision"      ,					expected_output=_EXPECTED_OUTPUT_SHAPE      ,					)
							def     __a   (   self      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					)	-> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
								a      :		Dict	=				(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								a      :		Optional[int]	=				return_dict if return_dict is not None else self.config.use_return_dict

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

								a      :		List[str]	=				self.conv_stem(lowerCAmelCase__      )

								a      :		Dict	=				() if output_hidden_states else None

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

									if output_hidden_states:
										a      :		Optional[Any]	=				all_hidden_states + (hidden_states,)

								a      :		Any	=				hidden_states

								if self.pooler is not None:
									a      :		Union[str, Any]	=				torch.flatten(self.pooler(lowerCAmelCase__      )      ,					start_dim=1      )
								else:
									a      :		List[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=lowerCAmelCase__      ,					pooler_output=lowerCAmelCase__      ,					hidden_states=lowerCAmelCase__      ,					)


@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__						):






							def __init__(   self      ,					lowerCAmelCase__      )	-> None:
								super().__init__(lowerCAmelCase__      )

								a      :		int	=				config.num_labels
								a      :		List[Any]	=				MobileNetVaModel(lowerCAmelCase__      )

								a      :		List[str]	=				self.mobilenet_va.layer[-1].convolution.out_channels

								# Classifier head
								a      :		Union[str, Any]	=				nn.Dropout(config.classifier_dropout_prob      ,					inplace=lowerCAmelCase__      )
								a      :		str	=				nn.Linear(lowerCAmelCase__      ,					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(lowerCAmelCase__      )
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT      ,					output_type=lowerCAmelCase__      ,					config_class=_CONFIG_FOR_DOC      ,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,					)
							def     __a   (   self      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					)	-> Union[tuple, ImageClassifierOutputWithNoAttention]:
								a      :		Optional[Any]	=				return_dict if return_dict is not None else self.config.use_return_dict

								a      :		Any	=				self.mobilenet_va(lowerCAmelCase__      ,					output_hidden_states=lowerCAmelCase__      ,					return_dict=lowerCAmelCase__      )

								a      :		Optional[int]	=				outputs.pooler_output if return_dict else outputs[1]

								a      :		Tuple	=				self.classifier(self.dropout(lowerCAmelCase__      )      )

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

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

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

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



"""simple docstring"""






def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase )  ->  int:

   if not grid or not grid[0]:
      raise TypeError('''The grid does not contain the appropriate information''' )

   for cell_n in range(1       ,    len(grid[0] ) ):
      grid[0][cell_n] += grid[0][cell_n - 1]
   lowercase__:   Tuple					=      grid[0]

   for row_n in range(1       ,    len(__UpperCAmelCase ) ):
      lowercase__:   Tuple					=      grid[row_n]
      lowercase__:   Dict					=      fill_row(__UpperCAmelCase       ,    __UpperCAmelCase )
      lowercase__:   Union[str, Any]					=      grid[row_n]

   return grid[-1][-1]



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase )  ->  list:
   current_row[0] += row_above[0]
   for cell_n in range(1       ,    len(__UpperCAmelCase ) ):
      current_row[cell_n] += min(current_row[cell_n - 1]       ,    row_above[cell_n] )

   return current_row


if __name__ == "__main__":
       import doctest

       doctest.testmod()
