





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


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






class        snake_case_    (		unittest.TestCase						):






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]      )->List[Any]:






   '''simple docstring'''

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

   # File is cached at the same place the second time.
   __lowerCAmelCase					:	Dict      =       cached_file(_snake_case       ,  _snake_case      )
   self.assertEqual(_snake_case       ,  _snake_case      )

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






  def 				UpperCAmelCase__						(							self			:					Tuple      )->str:






   '''simple docstring'''

   with self.assertRaisesRegex(_snake_case       ,  """is not a valid model identifier"""      ):
    __lowerCAmelCase					:	Any      =       cached_file("""tiny-random-bert"""       ,  _snake_case      )

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

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






  def 				UpperCAmelCase__						(							self			:					Tuple      )->Optional[int]:






   '''simple docstring'''

   with self.assertRaisesRegex(_snake_case       ,  """does not appear to have a file named"""      ):
    __lowerCAmelCase					:	Optional[Any]      =       cached_file(_snake_case       ,  """conf"""      )

   with open(os.path.join(_snake_case       ,  """refs"""       ,  """main"""      )      ) as f:
    __lowerCAmelCase					:	Union[str, Any]      =       f.read()
   self.assertTrue(os.path.isfile(os.path.join(_snake_case       ,  """.no_exist"""       ,  _snake_case       ,  """conf"""      )      )      )

   __lowerCAmelCase					:	Tuple      =       cached_file(_snake_case       ,  """conf"""       ,  _raise_exceptions_for_missing_entries=_snake_case      )
   self.assertIsNone(_snake_case      )

   __lowerCAmelCase					:	List[Any]      =       cached_file(_snake_case       ,  """conf"""       ,  local_files_only=_snake_case       ,  _raise_exceptions_for_missing_entries=_snake_case      )
   self.assertIsNone(_snake_case      )

   __lowerCAmelCase					:	Dict      =       mock.Mock()
   __lowerCAmelCase					:	List[Any]      =       500
   __lowerCAmelCase					:	int      =       {}
   __lowerCAmelCase					:	int      =       HTTPError
   __lowerCAmelCase					:	Dict      =       {}

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






  def 				UpperCAmelCase__						(							self			:					List[str]      )->int:






   '''simple docstring'''

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






  def 				UpperCAmelCase__						(							self			:					Dict      )->Optional[int]:






   '''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(_snake_case       ,  """is not a valid model identifier"""      ):
    get_file_from_repo("""bert-base-case"""       ,  _snake_case      )

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

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






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






   '''simple docstring'''

   with tempfile.TemporaryDirectory() as tmp_dir:
    __lowerCAmelCase					:	List[Any]      =       Path(_snake_case      ) / """a.txt"""
    filename.touch()
    self.assertEqual(get_file_from_repo(_snake_case       ,  """a.txt"""      )       ,  str(_snake_case      )      )

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






from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase             =							logging.get_logger(__name__)

_UpperCAmelCase             =							{
    'microsoft/biogpt': 'https://huggingface.co/microsoft/biogpt/resolve/main/config.json',
    # See all BioGPT models at https://huggingface.co/models?filter=biogpt
}






class        snake_case_    (		__lowercase						):
  A_      							=   'biogpt'






  def __init__(							self			:					int       ,  _snake_case			:					Any=42384       ,  _snake_case			:					Any=1024       ,  _snake_case			:					List[Any]=24       ,  _snake_case			:					Any=16       ,  _snake_case			:					List[str]=4096       ,  _snake_case			:					Dict="gelu"       ,  _snake_case			:					Tuple=0.1       ,  _snake_case			:					str=0.1       ,  _snake_case			:					Tuple=1024       ,  _snake_case			:					Tuple=0.02       ,  _snake_case			:					Tuple=1E-12       ,  _snake_case			:					Optional[int]=True       ,  _snake_case			:					Optional[int]=True       ,  _snake_case			:					Any=0.0       ,  _snake_case			:					Tuple=0.0       ,  _snake_case			:					str=1       ,  _snake_case			:					Dict=0       ,  _snake_case			:					str=2       ,  **_snake_case			:					Union[str, Any]       ,  )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       vocab_size
   __lowerCAmelCase					:	Dict      =       max_position_embeddings
   __lowerCAmelCase					:	str      =       hidden_size
   __lowerCAmelCase					:	Dict      =       num_hidden_layers
   __lowerCAmelCase					:	List[Any]      =       num_attention_heads
   __lowerCAmelCase					:	Optional[Any]      =       intermediate_size
   __lowerCAmelCase					:	int      =       hidden_act
   __lowerCAmelCase					:	Any      =       hidden_dropout_prob
   __lowerCAmelCase					:	Any      =       attention_probs_dropout_prob
   __lowerCAmelCase					:	Any      =       initializer_range
   __lowerCAmelCase					:	int      =       layer_norm_eps
   __lowerCAmelCase					:	Optional[int]      =       scale_embedding
   __lowerCAmelCase					:	List[Any]      =       use_cache
   __lowerCAmelCase					:	str      =       layerdrop
   __lowerCAmelCase					:	Dict      =       activation_dropout
   super().__init__(pad_token_id=_snake_case       ,  bos_token_id=_snake_case       ,  eos_token_id=_snake_case       ,  **_snake_case      )