'''simple docstring'''
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        _UpperCAmelCase						(		unittest.TestCase			):




    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






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

       # File is cached at the same place the second time.
       lowercase_	:    List[str]           =     cached_file(a  ,     a			)
       self.assertEqual(a  ,     a			)

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




    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






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

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

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




    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






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

       with open(os.path.join(a  ,     "refs"  ,     "main"			)			) as f:
          lowercase_	:    Optional[int]           =     f.read()
       self.assertTrue(os.path.isfile(os.path.join(a  ,     ".no_exist"  ,     a  ,     "conf"			)			)			)

       lowercase_	:    int           =     cached_file(a  ,     "conf"  ,     _raise_exceptions_for_missing_entries=a			)
       self.assertIsNone(a			)

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

       lowercase_	:    int           =     mock.Mock()
       lowercase_	:    Tuple           =     5_0_0
       lowercase_	:    Tuple           =     {}
       lowercase_	:    List[Any]           =     HTTPError
       lowercase_	:    Optional[int]           =     {}

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




    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






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




    def 				lowerCAmelCase__				(			self  :       str			):






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

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

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





    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






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

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












'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__ =			logging.get_logger(__name__)

UpperCamelCase__ =			{
    'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json',
}


class        _UpperCAmelCase						(		snake_case			):
    __lowerCamelCase:   List[Any]		       =    'gpt_neox_japanese'




    def __init__(			self  :       List[str]  ,     a  :       List[Any]=3_2_0_0_0  ,     a  :       Union[str, Any]=2_5_6_0  ,     a  :       Optional[Any]=3_2  ,     a  :       Any=3_2  ,     a  :       str=4  ,     a  :       Optional[int]="gelu"  ,     a  :       Optional[Any]=1.00  ,     a  :       Dict=1_0_0_0_0  ,     a  :       List[Any]=2_0_4_8  ,     a  :       Dict=0.02  ,     a  :       int=1e-5  ,     a  :       Optional[int]=True  ,     a  :       Union[str, Any]=3_1_9_9_6  ,     a  :       List[Any]=3_1_9_9_9  ,     a  :       List[str]=0.1  ,     a  :       Dict=0.0  ,     **a  :       Union[str, Any]  ,     ):






       '''simple docstring'''






       super().__init__(bos_token_id=a  ,     eos_token_id=a  ,     **a			)
       lowercase_	:    int           =     vocab_size
       lowercase_	:    int           =     max_position_embeddings
       lowercase_	:    List[str]           =     hidden_size
       lowercase_	:    Any           =     num_hidden_layers
       lowercase_	:    Any           =     num_attention_heads
       lowercase_	:    List[Any]           =     intermediate_multiple_size
       lowercase_	:    List[str]           =     hidden_act
       lowercase_	:    Optional[int]           =     rotary_pct
       lowercase_	:    Tuple           =     rotary_emb_base
       lowercase_	:    Optional[Any]           =     initializer_range
       lowercase_	:    List[str]           =     layer_norm_eps
       lowercase_	:    List[str]           =     use_cache
       lowercase_	:    Any           =     attention_dropout
       lowercase_	:    List[Any]           =     hidden_dropout




