




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							:						str					       =  logging.get_logger(__name__)


# General docstring
lowerCamelCase							:						Dict					       =  "MobileNetV1Config"

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

# Image classification docstring
lowerCamelCase							:						Any					       =  "google/mobilenet_v1_1.0_224"
lowerCamelCase							:						str					       =  "tabby, tabby cat"


lowerCamelCase							:						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      _SCREAMING_SNAKE_CASE     (				lowercase      :			str      ,				lowercase      :			Optional[int]      ,				lowercase      :			Union[str, Any]=None	):


      '''simple docstring'''



      lowerCamelCase_							=       {}

      if isinstance(lowercase      ,				lowercase	):
            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(lowercase      ,				lowercase	):
            lowerCamelCase_							=       'MobilenetV1/Logits/Conv2d_1c_1x1/'
            lowerCamelCase_							=       model.classifier.weight
            lowerCamelCase_							=       model.classifier.bias

      return tf_to_pt_map

def      _SCREAMING_SNAKE_CASE     (				lowercase      :			Dict      ,				lowercase      :			Optional[Any]      ,				lowercase      :			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
      lowerCamelCase_							=       tf.train.list_variables(lowercase	)
      lowerCamelCase_							=       {}
      for name, shape in init_vars:
            logger.info(f"""Loading TF weight {name} with shape {shape}"""	)
            lowerCamelCase_							=       tf.train.load_variable(lowercase      ,				lowercase	)
            lowerCamelCase_							=       array

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

            lowerCamelCase_							=       tf_weights[name]

            if "depthwise_weights" in name:
                  logger.info('Transposing depthwise'	)
                  lowerCamelCase_							=       np.transpose(lowercase      ,				(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(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}"""	)
            lowerCamelCase_							=       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	):


      '''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(lowercase      ,				lowercase      ,				'constant'      ,				0.0	)







class     A(		nn.Module  ):



 '''simple docstring'''


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

       """simple docstring"""






       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=A_		,				out_channels=A_		,				kernel_size=A_		,				stride=A_		,				padding=A_		,				groups=A_		,				bias=A_		,				padding_mode='zeros'		,				)

       if use_normalization:
             lowerCamelCase_							=       nn.BatchNormad(
                 num_features=A_		,				eps=config.layer_norm_eps		,				momentum=0.9997		,				affine=A_		,				track_running_stats=A_		,				)
       else:
             lowerCamelCase_							=       None

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





 def 							a__					(							self   :    Tuple		,				A_   :    torch.Tensor    )  ->      torch.Tensor:

       """simple docstring"""






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







class     A(		UpperCamelCase  ):



 '''simple docstring'''


 UpperCamelCase		  =		MobileNetVaConfig
 UpperCamelCase		  =		load_tf_weights_in_mobilenet_va
 UpperCamelCase		  =		'''mobilenet_v1'''
 UpperCamelCase		  =		'''pixel_values'''
 UpperCamelCase		  =		False

 def 							a__					(							self   :    Dict		,				A_   :    Union[nn.Linear, nn.Convad]    )  ->      None:

       """simple docstring"""






       if isinstance(A_		,				(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(A_		,				nn.BatchNormad    ):
             module.bias.data.zero_()
             module.weight.data.fill_(1.0    )

lowerCamelCase							:						str					       =  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"

lowerCamelCase							:						List[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.'''				, UpperCamelCase				, )
class     A(		UpperCamelCase  ):



 '''simple docstring'''


 def __init__(							self   :    List[Any]		,				A_   :    MobileNetVaConfig		,				A_   :    bool = True    )  ->      Optional[Any]:

       """simple docstring"""






       super().__init__(A_    )
       lowerCamelCase_							=       config

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

       lowerCamelCase_							=       MobileNetVaConvLayer(
           A_		,				in_channels=config.num_channels		,				out_channels=A_		,				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(
                     A_		,				in_channels=A_		,				out_channels=A_		,				kernel_size=3		,				stride=strides[i]		,				groups=A_		,				)    )

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

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

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

 def 							a__					(							self   :    Optional[int]		,				A_   :    Union[str, Any]    )  ->      str:

       """simple docstring"""






       raise NotImplementedError





 @add_start_docstrings_to_model_forward(A_    )
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC		,				output_type=A_		,				config_class=_CONFIG_FOR_DOC		,				modality='vision'		,				expected_output=_EXPECTED_OUTPUT_SHAPE		,				)
 def 							a__					(							self   :    Union[str, Any]		,				A_   :    Optional[torch.Tensor] = None		,				A_   :    Optional[bool] = None		,				A_   :    Optional[bool] = None		,				)  ->      Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:

       """simple docstring"""






       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(A_    )

       lowerCamelCase_							=       () if output_hidden_states else None

       for i, layer_module in enumerate(self.layer    ):
             lowerCamelCase_							=       layer_module(A_    )

             if output_hidden_states:
                   lowerCamelCase_							=       all_hidden_states + (hidden_states,)

       lowerCamelCase_							=       hidden_states

       if self.pooler is not None:
             lowerCamelCase_							=       torch.flatten(self.pooler(A_    )		,				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=A_		,				pooler_output=A_		,				hidden_states=A_		,				)







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



 '''simple docstring'''


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

       """simple docstring"""






       super().__init__(A_    )

       lowerCamelCase_							=       config.num_labels
       lowerCamelCase_							=       MobileNetVaModel(A_    )

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

       # Classifier head
       lowerCamelCase_							=       nn.Dropout(config.classifier_dropout_prob		,				inplace=A_    )
       lowerCamelCase_							=       nn.Linear(A_		,				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(A_    )
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT		,				output_type=A_		,				config_class=_CONFIG_FOR_DOC		,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,				)
 def 							a__					(							self   :    int		,				A_   :    Optional[torch.Tensor] = None		,				A_   :    Optional[bool] = None		,				A_   :    Optional[torch.Tensor] = None		,				A_   :    Optional[bool] = None		,				)  ->      Union[tuple, ImageClassifierOutputWithNoAttention]:

       """simple docstring"""






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

       lowerCamelCase_							=       self.mobilenet_va(A_		,				output_hidden_states=A_		,				return_dict=A_    )

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

       lowerCamelCase_							=       self.classifier(self.dropout(A_    )    )

       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(A_		,				A_    )
             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(A_		,				A_    )

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

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





"""simple docstring"""






from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    get_torch_dist_unique_port,
    require_torch_multi_gpu,
    require_torch_neuroncore,
)
from transformers.training_args import ParallelMode
from transformers.utils import logging


UpperCAmelCase        =				logging.get_logger(__name__)


if is_torch_available():
					import torch
					from torch import nn
					from torch.utils.data import Dataset

					from transformers import Trainer



					class 			lowercase					( lowercase__		):






								def __init__(self  :	int      ,SCREAMING_SNAKE_CASE_  :	int = 101			)       ->	List[str]:





									"""simple docstring"""


									lowerCAmelCase												=  length






								def __len__(self  :	Any			)       ->	List[Any]:





									"""simple docstring"""


									return self.length






								def __getitem__(self  :	str      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]			)       ->	int:





									"""simple docstring"""


									return i





					class 			lowercase					:






								def __call__(self  :	List[Any]      ,SCREAMING_SNAKE_CASE_  :	List[Any]			)       ->	Optional[Any]:





									"""simple docstring"""


									return {"input_ids": torch.tensor(SCREAMING_SNAKE_CASE_			), "labels": torch.tensor(SCREAMING_SNAKE_CASE_			)}





					class 			lowercase					( nn.Module		):






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





									"""simple docstring"""


									super().__init__()
									# Add some (unused) params otherwise DDP will complain.
									lowerCAmelCase												=  nn.Linear(120      ,80			)






								def 					UpperCAmelCase							(self  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	str      ,SCREAMING_SNAKE_CASE_  :	Optional[int]=None			)       ->	Union[str, Any]:





									"""simple docstring"""


									if labels is not None:
										return torch.tensor(0.0      ,device=input_ids.device			), input_ids
									else:
										return input_ids



class 			lowercase					( lowercase__		):






			@require_torch_neuroncore
			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	Union[str, Any]:





				"""simple docstring"""


				lowerCAmelCase												=  F"""--nproc_per_node=2
            --master_port={get_torch_dist_unique_port()}
            {self.test_file_dir}/test_trainer_distributed.py
        """.split()
				lowerCAmelCase												=  self.get_auto_remove_tmp_dir()
				lowerCAmelCase												=  F"""--output_dir {output_dir}""".split()
				lowerCAmelCase												=  ['''torchrun'''] + distributed_args + args
				execute_subprocess_async(SCREAMING_SNAKE_CASE_      ,env=self.get_env()			)
				# successful return here == success - any errors would have caused an error in the sub-call





class 			lowercase					( lowercase__		):






			@require_torch_multi_gpu
			def 					UpperCAmelCase							(self  :	List[str]			)       ->	List[str]:





				"""simple docstring"""


				lowerCAmelCase												=  F"""--nproc_per_node={torch.cuda.device_count()}
            --master_port={get_torch_dist_unique_port()}
            {self.test_file_dir}/test_trainer_distributed.py
        """.split()
				lowerCAmelCase												=  self.get_auto_remove_tmp_dir()
				lowerCAmelCase												=  F"""--output_dir {output_dir}""".split()
				lowerCAmelCase												=  ['''torchrun'''] + distributed_args + args
				execute_subprocess_async(SCREAMING_SNAKE_CASE_      ,env=self.get_env()			)
				# successful return here == success - any errors would have caused an error in the sub-call


if __name__ == "__main__":
					# The script below is meant to be run under torch.distributed, on a machine with multiple GPUs:
					#
					# PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py

					UpperCAmelCase        =				HfArgumentParser((TrainingArguments,))
					UpperCAmelCase        =				parser.parse_args_into_dataclasses()[0]

					logger.warning(
					    f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """
					    f"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}"""
					)

					# Essentially, what we want to verify in the distributed case is that we get all samples back,
					# in the right order. (this is crucial for prediction for instance)
					for dataset_length in [1_0_1, 4_0, 7]:
										UpperCAmelCase        =				DummyDataset(dataset_length)




										def __magic_name__	(				_lowerCamelCase:		EvalPrediction			)					->						Dict:

											'''simple docstring'''




											lowerCAmelCase												=  list(range(len(_lowerCamelCase			)			)			)
											lowerCAmelCase												=  p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
											if not success and training_args.local_rank == 0:
												logger.warning(
												    '''Predictions and/or labels do not match expected results:\n  - predictions: '''
												    F"""{p.predictions.tolist()}\n  - labels: {p.label_ids.tolist()}\n  - expected: {sequential}"""			)
											return {"success": success}

										UpperCAmelCase        =				Trainer(
										    model=DummyModel(),
										    args=training_args,
										    data_collator=DummyDataCollator(),
										    eval_dataset=dataset,
										    compute_metrics=compute_metrics,
										)
										UpperCAmelCase        =				trainer.evaluate()
										logger.info(metrics)
										if metrics["eval_success"] is not True:
															logger.error(metrics)
															exit(1)

										UpperCAmelCase        =				trainer.predict(dataset)
										logger.info(p.metrics)
										if p.metrics["test_success"] is not True:
															logger.error(p.metrics)
															exit(1)

										UpperCAmelCase        =				2

										UpperCAmelCase        =				trainer.evaluate()
										logger.info(metrics)
										if metrics["eval_success"] is not True:
															logger.error(metrics)
															exit(1)

										UpperCAmelCase        =				trainer.predict(dataset)
										logger.info(p.metrics)
										if p.metrics["test_success"] is not True:
															logger.error(p.metrics)
															exit(1)



										UpperCAmelCase        =				None


