





import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


SCREAMING_SNAKE_CASE__			      =						"""hf-internal-testing/tiny-random-bert"""
SCREAMING_SNAKE_CASE__			      =						os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
SCREAMING_SNAKE_CASE__			      =						"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""







class   __lowerCAmelCase			(					unittest.TestCase  ):





   def 	_snake_case ( self      )				->  Optional[Any]:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  cached_file(_lowercase					,			_lowercase      )
      # Should have downloaded the file in here
      self.assertTrue(os.path.isdir(_lowercase      )      )
      # Cache should contain at least those three subfolders:
      for subfolder in ["blobs", "refs", "snapshots"]:
         self.assertTrue(os.path.isdir(os.path.join(_lowercase					,			_lowercase      )      )      )
      with open(os.path.join(_lowercase					,			"refs"					,			"main"      )      ) as f:
         a__	:						Any      		=  f.read()
      self.assertEqual(_lowercase					,			os.path.join(_lowercase					,			"snapshots"					,			_lowercase					,			_lowercase      )      )
      self.assertTrue(os.path.isfile(_lowercase      )      )

      # File is cached at the same place the second time.
      a__	:						List[Any]      		=  cached_file(_lowercase					,			_lowercase      )
      self.assertEqual(_lowercase					,			_lowercase      )

      # Using a specific revision to test the full commit hash.
      a__	:						List[Any]      		=  cached_file(_lowercase					,			_lowercase					,			revision="9b8c223"      )
      self.assertEqual(_lowercase					,			os.path.join(_lowercase					,			"snapshots"					,			_lowercase					,			_lowercase      )      )





   def 	_snake_case ( self      )				->  Union[str, Any]:
      """simple docstring"""





      with self.assertRaisesRegex(_lowercase					,			"is not a valid model identifier"      ):
         a__	:						Union[str, Any]      		=  cached_file("tiny-random-bert"					,			_lowercase      )

      with self.assertRaisesRegex(_lowercase					,			"is not a valid git identifier"      ):
         a__	:						Tuple      		=  cached_file(_lowercase					,			_lowercase					,			revision="aaaa"      )

      with self.assertRaisesRegex(_lowercase					,			"does not appear to have a file named"      ):
         a__	:						str      		=  cached_file(_lowercase					,			"conf"      )





   def 	_snake_case ( self      )				->  Tuple:
      """simple docstring"""





      with self.assertRaisesRegex(_lowercase					,			"does not appear to have a file named"      ):
         a__	:						Any      		=  cached_file(_lowercase					,			"conf"      )

      with open(os.path.join(_lowercase					,			"refs"					,			"main"      )      ) as f:
         a__	:						Any      		=  f.read()
      self.assertTrue(os.path.isfile(os.path.join(_lowercase					,			".no_exist"					,			_lowercase					,			"conf"      )      )      )

      a__	:						List[Any]      		=  cached_file(_lowercase					,			"conf"					,			_raise_exceptions_for_missing_entries=_lowercase      )
      self.assertIsNone(_lowercase      )

      a__	:						str      		=  cached_file(_lowercase					,			"conf"					,			local_files_only=_lowercase					,			_raise_exceptions_for_missing_entries=_lowercase      )
      self.assertIsNone(_lowercase      )

      a__	:						List[Any]      		=  mock.Mock()
      a__	:						List[Any]      		=  500
      a__	:						int      		=  {}
      a__	:						Any      		=  HTTPError
      a__	:						Union[str, Any]      		=  {}

      # Under the mock environment we get a 500 error when trying to reach the tokenizer.
      with mock.patch("requests.Session.request"					,			return_value=_lowercase      ) as mock_head:
         a__	:						Union[str, Any]      		=  cached_file(_lowercase					,			"conf"					,			_raise_exceptions_for_connection_errors=_lowercase      )
         self.assertIsNone(_lowercase      )
         # This check we did call the fake head request
         mock_head.assert_called()





   def 	_snake_case ( self      )				->  Optional[int]:
      """simple docstring"""





      self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"					,			_lowercase      )      )
      self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"					,			_lowercase      )      )
      self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"					,			_lowercase      )      )





   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      self.assertIsNone(get_file_from_repo("bert-base-cased"					,			"ahah.txt"      )      )

      # The function raises if the repository does not exist.
      with self.assertRaisesRegex(_lowercase					,			"is not a valid model identifier"      ):
         get_file_from_repo("bert-base-case"					,			_lowercase      )

      # The function raises if the revision does not exist.
      with self.assertRaisesRegex(_lowercase					,			"is not a valid git identifier"      ):
         get_file_from_repo("bert-base-cased"					,			_lowercase					,			revision="ahaha"      )

      a__	:						str      		=  get_file_from_repo("bert-base-cased"					,			_lowercase      )
      # The name is the cached name which is not very easy to test, so instead we load the content.
      a__	:						Union[str, Any]      		=  json.loads(open(_lowercase					,			"r"      ).read()      )
      self.assertEqual(config["hidden_size"]					,			768      )





   def 	_snake_case ( self      )				->  Tuple:
      """simple docstring"""





      with tempfile.TemporaryDirectory() as tmp_dir:
         a__	:						Any      		=  Path(_lowercase      ) / """a.txt"""
         filename.touch()
         self.assertEqual(get_file_from_repo(_lowercase					,			"a.txt"      )					,			str(_lowercase      )      )

         self.assertIsNone(get_file_from_repo(_lowercase					,			"b.txt"      )      )








from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE__       :			Dict			      =						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       :			Any			      =						{
    """google/mobilenet_v2_1.4_224""": """https://huggingface.co/google/mobilenet_v2_1.4_224/resolve/main/config.json""",
    """google/mobilenet_v2_1.0_224""": """https://huggingface.co/google/mobilenet_v2_1.0_224/resolve/main/config.json""",
    """google/mobilenet_v2_0.75_160""": """https://huggingface.co/google/mobilenet_v2_0.75_160/resolve/main/config.json""",
    """google/mobilenet_v2_0.35_96""": """https://huggingface.co/google/mobilenet_v2_0.35_96/resolve/main/config.json""",
    # See all MobileNetV2 models at https://huggingface.co/models?filter=mobilenet_v2
}







class   __lowerCAmelCase			(					_UpperCamelCase  ):
   _UpperCamelCase :						Optional[int]     		=				"""mobilenet_v2"""





   def __init__( self					,			snake_case=3					,			snake_case=224					,			snake_case=1.0					,			snake_case=8					,			snake_case=8					,			snake_case=6					,			snake_case=32					,			snake_case=True					,			snake_case=True					,			snake_case="relu6"					,			snake_case=True					,			snake_case=0.8					,			snake_case=0.02					,			snake_case=0.001					,			snake_case=255					,			**snake_case					,			)				->  int:
      """simple docstring"""





      super().__init__(**snake_case      )

      if depth_multiplier <= 0:
         raise ValueError("depth_multiplier must be greater than zero."      )

      a__	:						str      		=  num_channels
      a__	:						Dict      		=  image_size
      a__	:						Any      		=  depth_multiplier
      a__	:						str      		=  depth_divisible_by
      a__	:						Optional[int]      		=  min_depth
      a__	:						Dict      		=  expand_ratio
      a__	:						str      		=  output_stride
      a__	:						Optional[int]      		=  first_layer_is_expansion
      a__	:						Union[str, Any]      		=  finegrained_output
      a__	:						Union[str, Any]      		=  hidden_act
      a__	:						str      		=  tf_padding
      a__	:						List[Any]      		=  classifier_dropout_prob
      a__	:						List[Any]      		=  initializer_range
      a__	:						Optional[Any]      		=  layer_norm_eps
      a__	:						str      		=  semantic_loss_ignore_index







class   __lowerCAmelCase			(					_UpperCamelCase  ):
   _UpperCamelCase :						Any     		=				version.parse("""1.11"""  )





   @property
   def 	_snake_case ( self      )				->  Mapping[str, Mapping[int, str]]:
      """simple docstring"""





      return OrderedDict([("pixel_values", {0: "batch"})]      )





   @property
   def 	_snake_case ( self      )				->  Mapping[str, Mapping[int, str]]:
      """simple docstring"""





      if self.task == "image-classification":
         return OrderedDict([("logits", {0: "batch"})]      )
      else:
         return OrderedDict([("last_hidden_state", {0: "batch"}), ("pooler_output", {0: "batch"})]      )





   @property
   def 	_snake_case ( self      )				->  float:
      """simple docstring"""





      return 1E-4

