




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


_snake_case   :   Dict     							=	logging.get_logger(__name__)


# General docstring
_snake_case   :   List[str]     							=	'MobileNetV1Config'

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

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


_snake_case   :   List[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 	snake_case_					(UpperCamelCase :						Optional[int]				,		UpperCamelCase :						Union[str, Any]				,		UpperCamelCase :						int=None   ):





							'''simple docstring'''


							_a			  =       {}

							if isinstance(UpperCamelCase				,		UpperCamelCase   ):
														_a			  =       model.mobilenet_va
							else:
														_a			  =       model

							_a			  =       '''MobilenetV1/Conv2d_0/'''
							_a			  =       backbone.conv_stem.convolution.weight
							_a			  =       backbone.conv_stem.normalization.bias
							_a			  =       backbone.conv_stem.normalization.weight
							_a			  =       backbone.conv_stem.normalization.running_mean
							_a			  =       backbone.conv_stem.normalization.running_var

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

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

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

							if isinstance(UpperCamelCase				,		UpperCamelCase   ):
														_a			  =       '''MobilenetV1/Logits/Conv2d_1c_1x1/'''
														_a			  =       model.classifier.weight
														_a			  =       model.classifier.bias

							return tf_to_pt_map
def 	snake_case_					(UpperCamelCase :						Any				,		UpperCamelCase :						Dict				,		UpperCamelCase :						List[Any]   ):





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

							# Build TF to PyTorch weights loading map
							_a			  =       _build_tf_to_pytorch_map(UpperCamelCase				,		UpperCamelCase				,		UpperCamelCase   )

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

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

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

							logger.info(f'Weights not copied to PyTorch model: {", ".join(tf_weights.keys()   )}'   )
							return model
def 	snake_case_					(UpperCamelCase :						torch.Tensor				,		UpperCamelCase :						nn.Convad   ):





							'''simple docstring'''


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

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

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

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

							_a			  =       (pad_left, pad_right, pad_top, pad_bottom)
							return nn.functional.pad(UpperCamelCase				,		UpperCamelCase				,		'''constant'''				,		0.0   )

class     A					(	nn.Module					):







							def __init__(						self :						int							,    lowerCAmelCase_ :						MobileNetVaConfig							,    lowerCAmelCase_ :						int							,    lowerCAmelCase_ :						int							,    lowerCAmelCase_ :						int							,    lowerCAmelCase_ :						Optional[int] = 1							,    lowerCAmelCase_ :						Optional[int] = 1							,    lowerCAmelCase_ :						bool = False							,    lowerCAmelCase_ :						Optional[bool] = True							,    lowerCAmelCase_ :						Optional[bool or str] = True							,    )    ->							None:


														"""simple docstring"""


														super().__init__()
														_a			  =       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			  =       0 if config.tf_padding else int((kernel_size - 1) / 2      )

														_a			  =       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			  =       nn.BatchNormad(
																					    num_features=lowerCAmelCase_							,    eps=config.layer_norm_eps							,    momentum=0.9_9_9_7							,    affine=lowerCAmelCase_							,    track_running_stats=lowerCAmelCase_							,    )
														else:
																					_a			  =       None

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







							def      __lowerCAmelCase    (						self :						List[Any]							,    lowerCAmelCase_ :						torch.Tensor      )    ->							torch.Tensor:


														"""simple docstring"""


														if self.config.tf_padding:
																					_a			  =       apply_tf_padding(lowerCAmelCase_							,    self.convolution      )
														_a			  =       self.convolution(lowerCAmelCase_      )
														if self.normalization is not None:
																					_a			  =       self.normalization(lowerCAmelCase_      )
														if self.activation is not None:
																					_a			  =       self.activation(lowerCAmelCase_      )
														return features



class     A					(	_a					):
							lowercase_           =       MobileNetVaConfig
							lowercase_           =       load_tf_weights_in_mobilenet_va
							lowercase_           =       'mobilenet_v1'
							lowercase_           =       'pixel_values'
							lowercase_           =       False







							def      __lowerCAmelCase    (						self :						Optional[int]							,    lowerCAmelCase_ :						Union[nn.Linear, nn.Convad]      )    ->							None:


														"""simple docstring"""


														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      )



_snake_case   :   Dict     							=	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'

_snake_case   :   Any     							=	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.'  ,_a  ,)
class     A					(	_a					):







							def __init__(						self :						Tuple							,    lowerCAmelCase_ :						MobileNetVaConfig							,    lowerCAmelCase_ :						bool = True      )    ->							Dict:


														"""simple docstring"""


														super().__init__(lowerCAmelCase_      )
														_a			  =       config

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

														_a			  =       MobileNetVaConvLayer(
														    lowerCAmelCase_							,    in_channels=config.num_channels							,    out_channels=lowerCAmelCase_							,    kernel_size=3							,    stride=2							,    )

														_a			  =       [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

																					if strides[i] == 2 or i == 0:
																												depth *= 2
																												_a			  =       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			  =       nn.AdaptiveAvgPoolad((1, 1)      ) if add_pooling_layer else None

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







							def      __lowerCAmelCase    (						self :						List[Any]							,    lowerCAmelCase_ :						Optional[Any]      )    ->							Optional[int]:


														"""simple docstring"""


														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      __lowerCAmelCase    (						self :						Optional[Any]							,    lowerCAmelCase_ :						Optional[torch.Tensor] = None							,    lowerCAmelCase_ :						Optional[bool] = None							,    lowerCAmelCase_ :						Optional[bool] = None							,    )    ->							Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:


														"""simple docstring"""


														_a			  =       (
														    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
														)
														_a			  =       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			  =       self.conv_stem(lowerCAmelCase_      )

														_a			  =       () if output_hidden_states else None

														for i, layer_module in enumerate(self.layer      ):
																					_a			  =       layer_module(lowerCAmelCase_      )

																					if output_hidden_states:
																												_a			  =       all_hidden_states + (hidden_states,)

														_a			  =       hidden_states

														if self.pooler is not None:
																					_a			  =       torch.flatten(self.pooler(lowerCAmelCase_      )							,    start_dim=1      )
														else:
																					_a			  =       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(
    '\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    '  ,_a  ,)
class     A					(	_a					):







							def __init__(						self :						Tuple							,    lowerCAmelCase_ :						MobileNetVaConfig      )    ->							None:


														"""simple docstring"""


														super().__init__(lowerCAmelCase_      )

														_a			  =       config.num_labels
														_a			  =       MobileNetVaModel(lowerCAmelCase_      )

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

														# Classifier head
														_a			  =       nn.Dropout(config.classifier_dropout_prob							,    inplace=lowerCAmelCase_      )
														_a			  =       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      __lowerCAmelCase    (						self :						Union[str, Any]							,    lowerCAmelCase_ :						Optional[torch.Tensor] = None							,    lowerCAmelCase_ :						Optional[bool] = None							,    lowerCAmelCase_ :						Optional[torch.Tensor] = None							,    lowerCAmelCase_ :						Optional[bool] = None							,    )    ->							Union[tuple, ImageClassifierOutputWithNoAttention]:


														"""simple docstring"""


														_a			  =       return_dict if return_dict is not None else self.config.use_return_dict

														_a			  =       self.mobilenet_va(lowerCAmelCase_							,    output_hidden_states=lowerCAmelCase_							,    return_dict=lowerCAmelCase_      )

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

														_a			  =       self.classifier(self.dropout(lowerCAmelCase_      )      )

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

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

														if not return_dict:
																					_a			  =       (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'''


import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


_snake_case   :   str     							=	'\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n'

class     A					(	unittest.TestCase  ,_a					):







							def      __lowerCAmelCase    (						self :						Any      )    ->							Dict:


														"""simple docstring"""


														_a			  =       load_tool('''text-question-answering'''      )
														self.tool.setup()
														_a			  =       load_tool('''text-question-answering'''							,    remote=lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						List[Any]      )    ->							Optional[int]:


														"""simple docstring"""


														_a			  =       self.tool(lowerCAmelCase_							,    '''What did Hugging Face do in April 2021?'''      )
														self.assertEqual(lowerCAmelCase_							,    '''launched the BigScience Research Workshop'''      )







							def      __lowerCAmelCase    (						self :						Union[str, Any]      )    ->							Dict:


														"""simple docstring"""


														_a			  =       self.remote_tool(lowerCAmelCase_							,    '''What did Hugging Face do in April 2021?'''      )
														self.assertEqual(lowerCAmelCase_							,    '''launched the BigScience Research Workshop'''      )







							def      __lowerCAmelCase    (						self :						List[Any]      )    ->							List[str]:


														"""simple docstring"""


														_a			  =       self.tool(text=lowerCAmelCase_							,    question='''What did Hugging Face do in April 2021?'''      )
														self.assertEqual(lowerCAmelCase_							,    '''launched the BigScience Research Workshop'''      )







							def      __lowerCAmelCase    (						self :						int      )    ->							List[Any]:


														"""simple docstring"""


														_a			  =       self.remote_tool(text=lowerCAmelCase_							,    question='''What did Hugging Face do in April 2021?'''      )
														self.assertEqual(lowerCAmelCase_							,    '''launched the BigScience Research Workshop'''      )

