
"""simple docstring"""


import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer
from ...utils import logging


SCREAMING_SNAKE_CASE			  =  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE			  =  """▁"""

SCREAMING_SNAKE_CASE			  =  {"""vocab_file""": """sentencepiece.bpe.model"""}

SCREAMING_SNAKE_CASE			  =  {
    """vocab_file""": {
        """facebook/nllb-200-distilled-600M""": (
            """https://huggingface.co/facebook/nllb-200-distilled-600M/blob/main/sentencepiece.bpe.model"""
        ),
    }
}

SCREAMING_SNAKE_CASE			  =  {
    """facebook/nllb-200-distilled-600M""": 1_024,
}

# fmt: off
SCREAMING_SNAKE_CASE			  =  ["""ace_Arab""", """ace_Latn""", """acm_Arab""", """acq_Arab""", """aeb_Arab""", """afr_Latn""", """ajp_Arab""", """aka_Latn""", """amh_Ethi""", """apc_Arab""", """arb_Arab""", """ars_Arab""", """ary_Arab""", """arz_Arab""", """asm_Beng""", """ast_Latn""", """awa_Deva""", """ayr_Latn""", """azb_Arab""", """azj_Latn""", """bak_Cyrl""", """bam_Latn""", """ban_Latn""", """bel_Cyrl""", """bem_Latn""", """ben_Beng""", """bho_Deva""", """bjn_Arab""", """bjn_Latn""", """bod_Tibt""", """bos_Latn""", """bug_Latn""", """bul_Cyrl""", """cat_Latn""", """ceb_Latn""", """ces_Latn""", """cjk_Latn""", """ckb_Arab""", """crh_Latn""", """cym_Latn""", """dan_Latn""", """deu_Latn""", """dik_Latn""", """dyu_Latn""", """dzo_Tibt""", """ell_Grek""", """eng_Latn""", """epo_Latn""", """est_Latn""", """eus_Latn""", """ewe_Latn""", """fao_Latn""", """pes_Arab""", """fij_Latn""", """fin_Latn""", """fon_Latn""", """fra_Latn""", """fur_Latn""", """fuv_Latn""", """gla_Latn""", """gle_Latn""", """glg_Latn""", """grn_Latn""", """guj_Gujr""", """hat_Latn""", """hau_Latn""", """heb_Hebr""", """hin_Deva""", """hne_Deva""", """hrv_Latn""", """hun_Latn""", """hye_Armn""", """ibo_Latn""", """ilo_Latn""", """ind_Latn""", """isl_Latn""", """ita_Latn""", """jav_Latn""", """jpn_Jpan""", """kab_Latn""", """kac_Latn""", """kam_Latn""", """kan_Knda""", """kas_Arab""", """kas_Deva""", """kat_Geor""", """knc_Arab""", """knc_Latn""", """kaz_Cyrl""", """kbp_Latn""", """kea_Latn""", """khm_Khmr""", """kik_Latn""", """kin_Latn""", """kir_Cyrl""", """kmb_Latn""", """kon_Latn""", """kor_Hang""", """kmr_Latn""", """lao_Laoo""", """lvs_Latn""", """lij_Latn""", """lim_Latn""", """lin_Latn""", """lit_Latn""", """lmo_Latn""", """ltg_Latn""", """ltz_Latn""", """lua_Latn""", """lug_Latn""", """luo_Latn""", """lus_Latn""", """mag_Deva""", """mai_Deva""", """mal_Mlym""", """mar_Deva""", """min_Latn""", """mkd_Cyrl""", """plt_Latn""", """mlt_Latn""", """mni_Beng""", """khk_Cyrl""", """mos_Latn""", """mri_Latn""", """zsm_Latn""", """mya_Mymr""", """nld_Latn""", """nno_Latn""", """nob_Latn""", """npi_Deva""", """nso_Latn""", """nus_Latn""", """nya_Latn""", """oci_Latn""", """gaz_Latn""", """ory_Orya""", """pag_Latn""", """pan_Guru""", """pap_Latn""", """pol_Latn""", """por_Latn""", """prs_Arab""", """pbt_Arab""", """quy_Latn""", """ron_Latn""", """run_Latn""", """rus_Cyrl""", """sag_Latn""", """san_Deva""", """sat_Beng""", """scn_Latn""", """shn_Mymr""", """sin_Sinh""", """slk_Latn""", """slv_Latn""", """smo_Latn""", """sna_Latn""", """snd_Arab""", """som_Latn""", """sot_Latn""", """spa_Latn""", """als_Latn""", """srd_Latn""", """srp_Cyrl""", """ssw_Latn""", """sun_Latn""", """swe_Latn""", """swh_Latn""", """szl_Latn""", """tam_Taml""", """tat_Cyrl""", """tel_Telu""", """tgk_Cyrl""", """tgl_Latn""", """tha_Thai""", """tir_Ethi""", """taq_Latn""", """taq_Tfng""", """tpi_Latn""", """tsn_Latn""", """tso_Latn""", """tuk_Latn""", """tum_Latn""", """tur_Latn""", """twi_Latn""", """tzm_Tfng""", """uig_Arab""", """ukr_Cyrl""", """umb_Latn""", """urd_Arab""", """uzn_Latn""", """vec_Latn""", """vie_Latn""", """war_Latn""", """wol_Latn""", """xho_Latn""", """ydd_Hebr""", """yor_Latn""", """yue_Hant""", """zho_Hans""", """zho_Hant""", """zul_Latn"""]



class 	__a	(   _lowerCAmelCase						):
       UpperCamelCase_		:       List[Any]				=      VOCAB_FILES_NAMES
       UpperCamelCase_		:       Any				=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       UpperCamelCase_		:       Any				=      PRETRAINED_VOCAB_FILES_MAP
       UpperCamelCase_		:       str				=      ['''input_ids''', '''attention_mask''']

       UpperCamelCase_		:       List[int]				=      []
       UpperCamelCase_		:       List[int]				=      []






       def __init__(	self			:				str							,   UpperCAmelCase_			:				List[Any]							,   UpperCAmelCase_			:				List[str]="<s>"							,   UpperCAmelCase_			:				List[str]="</s>"							,   UpperCAmelCase_			:				List[str]="</s>"							,   UpperCAmelCase_			:				Union[str, Any]="<s>"							,   UpperCAmelCase_			:				Dict="<unk>"							,   UpperCAmelCase_			:				Optional[Any]="<pad>"							,   UpperCAmelCase_			:				List[Any]="<mask>"							,   UpperCAmelCase_			:				Union[str, Any]=None							,   UpperCAmelCase_			:				Tuple=None							,   UpperCAmelCase_			:				List[str]=None							,   UpperCAmelCase_			:				Optional[Dict[str, Any]] = None							,   UpperCAmelCase_			:				Optional[int]=None							,   UpperCAmelCase_			:				List[str]=False							,   **UpperCAmelCase_			:				Optional[int]							,   )->	Optional[int]:







              """simple docstring"""
              # Mask token behave like a normal word, i.e. include the space before it
              UpperCamelCase       			=		AddedToken(UpperCAmelCase_							,   lstrip=UpperCAmelCase_							,   rstrip=UpperCAmelCase_      ) if isinstance(UpperCAmelCase_							,   UpperCAmelCase_      ) else mask_token

              UpperCamelCase       			=		{} if sp_model_kwargs is None else sp_model_kwargs
              UpperCamelCase       			=		legacy_behaviour

              super().__init__(
                  bos_token=UpperCAmelCase_							,   eos_token=UpperCAmelCase_							,   unk_token=UpperCAmelCase_							,   sep_token=UpperCAmelCase_							,   cls_token=UpperCAmelCase_							,   pad_token=UpperCAmelCase_							,   mask_token=UpperCAmelCase_							,   tokenizer_file=UpperCAmelCase_							,   src_lang=UpperCAmelCase_							,   tgt_lang=UpperCAmelCase_							,   additional_special_tokens=UpperCAmelCase_							,   sp_model_kwargs=self.sp_model_kwargs							,   legacy_behaviour=UpperCAmelCase_							,   **UpperCAmelCase_							,   )

              UpperCamelCase       			=		spm.SentencePieceProcessor(**self.sp_model_kwargs      )
              self.sp_model.Load(str(UpperCAmelCase_      )      )
              UpperCamelCase       			=		vocab_file
              # Original fairseq vocab and spm vocab must be "aligned":
              # Vocab    |    0    |    1    |   2    |    3    |  4   |  5   |  6   |   7  |   8  |  9
              # -------- | ------- | ------- | ------ | ------- | ---- | ---- | ---- | ---- | ---- | ----
              # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | 'an' | '▁n' | '▁m' | '▁t' | '▁k' | '▁a'
              # spm      | '<unk>' | '<s>'   | '</s>' | 'an'    | '▁n' | '▁m' | '▁t' | '▁k' | '▁a' | '▁s'

              # Mimic fairseq token-to-id alignment for the first 4 token
              UpperCamelCase       			=		{"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3}

              # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
              UpperCamelCase       			=		1

              UpperCamelCase       			=		len(self.sp_model      )
              UpperCamelCase       			=		{
                  code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(UpperCAmelCase_      )
              }
              UpperCamelCase       			=		{v: k for k, v in self.lang_code_to_id.items()}
              UpperCamelCase       			=		len(self.sp_model      ) + len(self.lang_code_to_id      ) + self.fairseq_offset

              self.fairseq_tokens_to_ids.update(self.lang_code_to_id      )
              UpperCamelCase       			=		{v: k for k, v in self.fairseq_tokens_to_ids.items()}
              UpperCamelCase       			=		list(self.lang_code_to_id.keys()      )

              if additional_special_tokens is not None:
                     # Only add those special tokens if they are not already there.
                     self._additional_special_tokens.extend(
                         [t for t in additional_special_tokens if t not in self._additional_special_tokens]      )

              UpperCamelCase       			=		src_lang if src_lang is not None else "eng_Latn"
              UpperCamelCase       			=		self.lang_code_to_id[self._src_lang]
              UpperCamelCase       			=		tgt_lang
              self.set_src_lang_special_tokens(self._src_lang      )






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







              """simple docstring"""
              UpperCamelCase       			=		self.__dict__.copy()
              UpperCamelCase       			=		None
              UpperCamelCase       			=		self.sp_model.serialized_model_proto()
              return state






       def __setstate__(	self			:				int							,   UpperCAmelCase_			:				Tuple      )->	Optional[Any]:







              """simple docstring"""
              UpperCamelCase       			=		d

              # for backward compatibility
              if not hasattr(self							,   "sp_model_kwargs"      ):
                     UpperCamelCase       			=		{}

              UpperCamelCase       			=		spm.SentencePieceProcessor(**self.sp_model_kwargs      )
              self.sp_model.LoadFromSerializedProto(self.sp_model_proto      )






       @property
       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict      )->	int:







              """simple docstring"""
              return len(self.sp_model      ) + len(self.lang_code_to_id      ) + self.fairseq_offset + 1  # Plus 1 for the mask token






       @property
       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]      )->	str:







              """simple docstring"""
              return self._src_lang






       @src_lang.setter
       def 						_SCREAMING_SNAKE_CASE							(	self			:				int							,   UpperCAmelCase_			:				str      )->	None:







              """simple docstring"""
              UpperCamelCase       			=		new_src_lang
              self.set_src_lang_special_tokens(self._src_lang      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Tuple							,   UpperCAmelCase_			:				List[int]							,   UpperCAmelCase_			:				Optional[List[int]] = None							,   UpperCAmelCase_			:				bool = False      )->	List[int]:







              """simple docstring"""

              if already_has_special_tokens:
                     return super().get_special_tokens_mask(
                         token_ids_a=UpperCAmelCase_							,   token_ids_a=UpperCAmelCase_							,   already_has_special_tokens=UpperCAmelCase_      )

              UpperCamelCase       			=		[1] * len(self.prefix_tokens      )
              UpperCamelCase       			=		[1] * len(self.suffix_tokens      )
              if token_ids_a is None:
                     return prefix_ones + ([0] * len(UpperCAmelCase_      )) + suffix_ones
              return prefix_ones + ([0] * len(UpperCAmelCase_      )) + ([0] * len(UpperCAmelCase_      )) + suffix_ones






       def 						_SCREAMING_SNAKE_CASE							(	self			:				List[str]							,   UpperCAmelCase_			:				List[int]							,   UpperCAmelCase_			:				Optional[List[int]] = None      )->	List[int]:







              """simple docstring"""
              if token_ids_a is None:
                     return self.prefix_tokens + token_ids_a + self.suffix_tokens
              # We don't expect to process pairs, but leave the pair logic for API consistency
              return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict							,   UpperCAmelCase_			:				List[int]							,   UpperCAmelCase_			:				Optional[List[int]] = None      )->	List[int]:







              """simple docstring"""
              UpperCamelCase       			=		[self.sep_token_id]
              UpperCamelCase       			=		[self.cls_token_id]

              if token_ids_a is None:
                     return len(cls + token_ids_a + sep      ) * [0]
              return len(cls + token_ids_a + sep + sep + token_ids_a + sep      ) * [0]






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[Any]							,   UpperCAmelCase_			:				Any							,   UpperCAmelCase_			:				str							,   UpperCAmelCase_			:				Optional[str]							,   UpperCAmelCase_			:				Optional[str]							,   **UpperCAmelCase_			:				Tuple      )->	List[str]:







              """simple docstring"""
              if src_lang is None or tgt_lang is None:
                     raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model"      )
              UpperCamelCase       			=		src_lang
              UpperCamelCase       			=		self(UpperCAmelCase_							,   add_special_tokens=UpperCAmelCase_							,   return_tensors=UpperCAmelCase_							,   **UpperCAmelCase_      )
              UpperCamelCase       			=		self.convert_tokens_to_ids(UpperCAmelCase_      )
              UpperCamelCase       			=		tgt_lang_id
              return inputs






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict      )->	Optional[Any]:







              """simple docstring"""
              UpperCamelCase       			=		{self.convert_ids_to_tokens(UpperCAmelCase_      ): i for i in range(self.vocab_size      )}
              vocab.update(self.added_tokens_encoder      )
              return vocab






       def 						_SCREAMING_SNAKE_CASE							(	self			:				str							,   UpperCAmelCase_			:				str      )->	List[str]:







              """simple docstring"""
              return self.sp_model.encode(UpperCAmelCase_							,   out_type=UpperCAmelCase_      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[Any]							,   UpperCAmelCase_			:				Union[str, Any]      )->	Tuple:







              """simple docstring"""
              if token in self.fairseq_tokens_to_ids:
                     return self.fairseq_tokens_to_ids[token]
              UpperCamelCase       			=		self.sp_model.PieceToId(UpperCAmelCase_      )

              # Need to return unknown token if the SP model returned 0
              return spm_id + self.fairseq_offset if spm_id else self.unk_token_id






       def 						_SCREAMING_SNAKE_CASE							(	self			:				int							,   UpperCAmelCase_			:				str      )->	List[str]:







              """simple docstring"""
              if index in self.fairseq_ids_to_tokens:
                     return self.fairseq_ids_to_tokens[index]
              return self.sp_model.IdToPiece(index - self.fairseq_offset      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any							,   UpperCAmelCase_			:				List[Any]      )->	Dict:







              """simple docstring"""
              UpperCamelCase       			=		"".join(UpperCAmelCase_      ).replace(UpperCAmelCase_							,   " "      ).strip()
              return out_string






       def 						_SCREAMING_SNAKE_CASE							(	self			:				str							,   UpperCAmelCase_			:				str							,   UpperCAmelCase_			:				Optional[str] = None      )->	Tuple[str]:







              """simple docstring"""
              if not os.path.isdir(UpperCAmelCase_      ):
                     logger.error(f"Vocabulary path ({save_directory}) should be a directory"      )
                     return
              UpperCamelCase       			=		os.path.join(
                  UpperCAmelCase_							,   (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]      )

              if os.path.abspath(self.vocab_file      ) != os.path.abspath(UpperCAmelCase_      ) and os.path.isfile(self.vocab_file      ):
                     copyfile(self.vocab_file							,   UpperCAmelCase_      )
              elif not os.path.isfile(self.vocab_file      ):
                     with open(UpperCAmelCase_							,   "wb"      ) as fi:
                            UpperCamelCase       			=		self.sp_model.serialized_model_proto()
                            fi.write(UpperCAmelCase_      )

              return (out_vocab_file,)






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]							,   UpperCAmelCase_			:				List[str]							,   UpperCAmelCase_			:				str = "eng_Latn"							,   UpperCAmelCase_			:				Optional[List[str]] = None							,   UpperCAmelCase_			:				str = "fra_Latn"							,   **UpperCAmelCase_			:				Tuple							,   )->	BatchEncoding:







              """simple docstring"""
              UpperCamelCase       			=		src_lang
              UpperCamelCase       			=		tgt_lang
              return super().prepare_seqaseq_batch(UpperCAmelCase_							,   UpperCAmelCase_							,   **UpperCAmelCase_      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				str      )->	List[str]:







              """simple docstring"""
              return self.set_src_lang_special_tokens(self.src_lang      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Union[str, Any]      )->	Tuple:







              """simple docstring"""
              return self.set_tgt_lang_special_tokens(self.tgt_lang      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any							,   UpperCAmelCase_			:				int      )->	None:







              """simple docstring"""
              UpperCamelCase       			=		self.lang_code_to_id[src_lang]
              if self.legacy_behaviour:
                     UpperCamelCase       			=		[]
                     UpperCamelCase       			=		[self.eos_token_id, self.cur_lang_code]
              else:
                     UpperCamelCase       			=		[self.cur_lang_code]
                     UpperCamelCase       			=		[self.eos_token_id]






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]							,   UpperCAmelCase_			:				str      )->	None:







              """simple docstring"""
              UpperCamelCase       			=		self.lang_code_to_id[lang]
              if self.legacy_behaviour:
                     UpperCamelCase       			=		[]
                     UpperCamelCase       			=		[self.eos_token_id, self.cur_lang_code]
              else:
                     UpperCamelCase       			=		[self.cur_lang_code]
                     UpperCamelCase       			=		[self.eos_token_id]







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


SCREAMING_SNAKE_CASE			  =  logging.get_logger(__name__)


# General docstring
SCREAMING_SNAKE_CASE			  =  """MobileNetV1Config"""

# Base docstring
SCREAMING_SNAKE_CASE			  =  """google/mobilenet_v1_1.0_224"""
SCREAMING_SNAKE_CASE			  =  [1, 1_024, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE			  =  """google/mobilenet_v1_1.0_224"""
SCREAMING_SNAKE_CASE			  =  """tabby, tabby cat"""


SCREAMING_SNAKE_CASE			  =  [
    """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 						lowerCamelCase__      (				UpperCAmelCase_		,   UpperCAmelCase_		,   UpperCAmelCase_=None					)->				int:







       """simple docstring"""
       UpperCamelCase       			=		{}

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

       UpperCamelCase       			=		"MobilenetV1/Conv2d_0/"
       UpperCamelCase       			=		backbone.conv_stem.convolution.weight
       UpperCamelCase       			=		backbone.conv_stem.normalization.bias
       UpperCamelCase       			=		backbone.conv_stem.normalization.weight
       UpperCamelCase       			=		backbone.conv_stem.normalization.running_mean
       UpperCamelCase       			=		backbone.conv_stem.normalization.running_var

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

              UpperCamelCase       			=		backbone.layer[pt_index]
              UpperCamelCase       			=		F"MobilenetV1/Conv2d_{tf_index}_depthwise/"
              UpperCamelCase       			=		pointer.convolution.weight
              UpperCamelCase       			=		pointer.normalization.bias
              UpperCamelCase       			=		pointer.normalization.weight
              UpperCamelCase       			=		pointer.normalization.running_mean
              UpperCamelCase       			=		pointer.normalization.running_var

              UpperCamelCase       			=		backbone.layer[pt_index + 1]
              UpperCamelCase       			=		F"MobilenetV1/Conv2d_{tf_index}_pointwise/"
              UpperCamelCase       			=		pointer.convolution.weight
              UpperCamelCase       			=		pointer.normalization.bias
              UpperCamelCase       			=		pointer.normalization.weight
              UpperCamelCase       			=		pointer.normalization.running_mean
              UpperCamelCase       			=		pointer.normalization.running_var

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

       return tf_to_pt_map



def 						lowerCamelCase__      (				UpperCAmelCase_		,   UpperCAmelCase_		,   UpperCAmelCase_					)->				List[str]:







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

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

              UpperCamelCase       			=		tf_weights[name]

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







       """simple docstring"""
       UpperCamelCase			,							UpperCamelCase       			=		features.shape[-2:]
       UpperCamelCase			,							UpperCamelCase       			=		conv_layer.stride
       UpperCamelCase			,							UpperCamelCase       			=		conv_layer.kernel_size

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

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

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

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



class 	__a	(   nn.Module						):






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







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

              UpperCamelCase       			=		nn.Convad(
                  in_channels=UpperCAmelCase_							,   out_channels=UpperCAmelCase_							,   kernel_size=UpperCAmelCase_							,   stride=UpperCAmelCase_							,   padding=UpperCAmelCase_							,   groups=UpperCAmelCase_							,   bias=UpperCAmelCase_							,   padding_mode="zeros"							,   )

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

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






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict							,   UpperCAmelCase_			:				torch.Tensor      )->	torch.Tensor:







              """simple docstring"""
              if self.config.tf_padding:
                     UpperCamelCase       			=		apply_tf_padding(UpperCAmelCase_							,   self.convolution      )
              UpperCamelCase       			=		self.convolution(UpperCAmelCase_      )
              if self.normalization is not None:
                     UpperCamelCase       			=		self.normalization(UpperCAmelCase_      )
              if self.activation is not None:
                     UpperCamelCase       			=		self.activation(UpperCAmelCase_      )
              return features





class 	__a	(   _lowerCAmelCase						):
       UpperCamelCase_		:       List[str]				=      MobileNetVaConfig
       UpperCamelCase_		:       Dict				=      load_tf_weights_in_mobilenet_va
       UpperCamelCase_		:       List[str]				=      '''mobilenet_v1'''
       UpperCamelCase_		:       Optional[int]				=      '''pixel_values'''
       UpperCamelCase_		:       List[Any]				=      False






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any							,   UpperCAmelCase_			:				Union[nn.Linear, nn.Convad]      )->	None:







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





SCREAMING_SNAKE_CASE			  =  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.
"""

SCREAMING_SNAKE_CASE			  =  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			:				str							,   UpperCAmelCase_			:				MobileNetVaConfig							,   UpperCAmelCase_			:				bool = True      )->	Optional[int]:







              """simple docstring"""
              super().__init__(UpperCAmelCase_      )
              UpperCamelCase       			=		config

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

              UpperCamelCase       			=		MobileNetVaConvLayer(
                  UpperCAmelCase_							,   in_channels=config.num_channels							,   out_channels=UpperCAmelCase_							,   kernel_size=3							,   stride=2							,   )

              UpperCamelCase       			=		[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

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

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

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

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

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






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]							,   UpperCAmelCase_			:				Dict      )->	List[str]:







              """simple docstring"""
              raise NotImplementedError






       @add_start_docstrings_to_model_forward(UpperCAmelCase_      )
       @add_code_sample_docstrings(
           checkpoint=_CHECKPOINT_FOR_DOC							,   output_type=UpperCAmelCase_							,   config_class=_CONFIG_FOR_DOC							,   modality="vision"							,   expected_output=_EXPECTED_OUTPUT_SHAPE							,   )
       def 						_SCREAMING_SNAKE_CASE							(	self			:				Tuple							,   UpperCAmelCase_			:				Optional[torch.Tensor] = None							,   UpperCAmelCase_			:				Optional[bool] = None							,   UpperCAmelCase_			:				Optional[bool] = None							,   )->	Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:







              """simple docstring"""
              UpperCamelCase       			=		(
                  output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
              )
              UpperCamelCase       			=		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       			=		self.conv_stem(UpperCAmelCase_      )

              UpperCamelCase       			=		() if output_hidden_states else None

              for i, layer_module in enumerate(self.layer      ):
                     UpperCamelCase       			=		layer_module(UpperCAmelCase_      )

                     if output_hidden_states:
                            UpperCamelCase       			=		all_hidden_states + (hidden_states,)

              UpperCamelCase       			=		hidden_states

              if self.pooler is not None:
                     UpperCamelCase       			=		torch.flatten(self.pooler(UpperCAmelCase_      )							,   start_dim=1      )
              else:
                     UpperCamelCase       			=		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=UpperCAmelCase_							,   pooler_output=UpperCAmelCase_							,   hidden_states=UpperCAmelCase_							,   )





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






       def __init__(	self			:				str							,   UpperCAmelCase_			:				MobileNetVaConfig      )->	None:







              """simple docstring"""
              super().__init__(UpperCAmelCase_      )

              UpperCamelCase       			=		config.num_labels
              UpperCamelCase       			=		MobileNetVaModel(UpperCAmelCase_      )

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

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







              """simple docstring"""
              UpperCamelCase       			=		return_dict if return_dict is not None else self.config.use_return_dict

              UpperCamelCase       			=		self.mobilenet_va(UpperCAmelCase_							,   output_hidden_states=UpperCAmelCase_							,   return_dict=UpperCAmelCase_      )

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

              UpperCamelCase       			=		self.classifier(self.dropout(UpperCAmelCase_      )      )

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

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

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

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





