





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


lowercase__				:str				   =       'hf-internal-testing/tiny-random-bert'
lowercase__				:List[Any]				   =       os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
lowercase__				:str				   =       '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'




class 							snake_case							(			unittest.TestCase ):

   '''simple docstring'''



   def       A_					(							self       :      Optional[int] ):


     '''simple docstring'''




     __UpperCAmelCase    :						Tuple       =						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:
       __UpperCAmelCase    :						Tuple       =						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.
     __UpperCAmelCase    :						Tuple       =						cached_file(__A						,				__A )
     self.assertEqual(__A						,				__A )

     # Using a specific revision to test the full commit hash.
     __UpperCAmelCase    :						List[str]       =						cached_file(__A						,				__A						,				revision='''9b8c223''' )
     self.assertEqual(__A						,				os.path.join(__A						,				'''snapshots'''						,				__A						,				__A ) )

   def       A_					(							self       :      List[str] ):


     '''simple docstring'''




     with self.assertRaisesRegex(__A						,				'''is not a valid model identifier''' ):
       __UpperCAmelCase    :						Optional[int]       =						cached_file('''tiny-random-bert'''						,				__A )

     with self.assertRaisesRegex(__A						,				'''is not a valid git identifier''' ):
       __UpperCAmelCase    :						Union[str, Any]       =						cached_file(__A						,				__A						,				revision='''aaaa''' )

     with self.assertRaisesRegex(__A						,				'''does not appear to have a file named''' ):
       __UpperCAmelCase    :						Tuple       =						cached_file(__A						,				'''conf''' )

   def       A_					(							self       :      Dict ):


     '''simple docstring'''




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

     with open(os.path.join(__A						,				'''refs'''						,				'''main''' ) ) as f:
       __UpperCAmelCase    :						Dict       =						f.read()
     self.assertTrue(os.path.isfile(os.path.join(__A						,				'''.no_exist'''						,				__A						,				'''conf''' ) ) )

     __UpperCAmelCase    :						Dict       =						cached_file(__A						,				'''conf'''						,				_raise_exceptions_for_missing_entries=__A )
     self.assertIsNone(__A )

     __UpperCAmelCase    :						List[str]       =						cached_file(__A						,				'''conf'''						,				local_files_only=__A						,				_raise_exceptions_for_missing_entries=__A )
     self.assertIsNone(__A )

     __UpperCAmelCase    :						Optional[Any]       =						mock.Mock()
     __UpperCAmelCase    :						List[Any]       =						500
     __UpperCAmelCase    :						List[Any]       =						{}
     __UpperCAmelCase    :						List[Any]       =						HTTPError
     __UpperCAmelCase    :						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=__A ) as mock_head:
       __UpperCAmelCase    :						int       =						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       A_					(							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       A_					(							self       :      Tuple ):


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

     __UpperCAmelCase    :						Tuple       =						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.
     __UpperCAmelCase    :						Any       =						json.loads(open(__A						,				'''r''' ).read() )
     self.assertEqual(config['''hidden_size''']						,				768 )







   def       A_					(							self       :      List[Any] ):


     '''simple docstring'''




     with tempfile.TemporaryDirectory() as tmp_dir:
       __UpperCAmelCase    :						str       =						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


lowercase__				:int				   =       logging.get_logger(__name__)

lowercase__				:List[str]				   =       {
    'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json',
    # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox
}




class 							snake_case							(			__UpperCAmelCase ):

      '''simple docstring'''



      _A     :       List[Any]         =       'gpt_neox'

      def __init__(							self       :      Union[str, Any]						,				__lowercase       :      Any=50_432						,				__lowercase       :      int=6_144						,				__lowercase       :      List[str]=44						,				__lowercase       :      List[Any]=64						,				__lowercase       :      int=24_576						,				__lowercase       :      List[str]="gelu"						,				__lowercase       :      Optional[int]=0.2_5						,				__lowercase       :      Any=10_000						,				__lowercase       :      Union[str, Any]=0.0						,				__lowercase       :      List[str]=0.0						,				__lowercase       :      Union[str, Any]=0.1						,				__lowercase       :      Optional[int]=2_048						,				__lowercase       :      Union[str, Any]=0.0_2						,				__lowercase       :      Optional[int]=1e-5						,				__lowercase       :      str=True						,				__lowercase       :      str=0						,				__lowercase       :      Tuple=2						,				__lowercase       :      Tuple=False						,				__lowercase       :      int=True						,				__lowercase       :      Any=None						,				**__lowercase       :      List[str]						,				):


        '''simple docstring'''




        super().__init__(bos_token_id=__lowercase						,				eos_token_id=__lowercase						,				**__lowercase )
        __UpperCAmelCase    :						int       =						vocab_size
        __UpperCAmelCase    :						Union[str, Any]       =						max_position_embeddings
        __UpperCAmelCase    :						List[str]       =						hidden_size
        __UpperCAmelCase    :						Tuple       =						num_hidden_layers
        __UpperCAmelCase    :						Any       =						num_attention_heads
        __UpperCAmelCase    :						Union[str, Any]       =						intermediate_size
        __UpperCAmelCase    :						str       =						hidden_act
        __UpperCAmelCase    :						str       =						rotary_pct
        __UpperCAmelCase    :						str       =						rotary_emb_base
        __UpperCAmelCase    :						Dict       =						attention_dropout
        __UpperCAmelCase    :						str       =						hidden_dropout
        __UpperCAmelCase    :						Any       =						classifier_dropout
        __UpperCAmelCase    :						Tuple       =						initializer_range
        __UpperCAmelCase    :						Any       =						layer_norm_eps
        __UpperCAmelCase    :						Optional[int]       =						use_cache
        __UpperCAmelCase    :						List[Any]       =						tie_word_embeddings
        __UpperCAmelCase    :						Any       =						use_parallel_residual
        __UpperCAmelCase    :						List[str]       =						rope_scaling
        self._rope_scaling_validation()

        if self.hidden_size % self.num_attention_heads != 0:
          raise ValueError(
              '''The hidden size is not divisble by the number of attention heads! Make sure to update them!''' )







      def       A_					(							self       :      int ):


        '''simple docstring'''




        if self.rope_scaling is None:
          return

        if not isinstance(self.rope_scaling						,				__lowercase ) or len(self.rope_scaling ) != 2:
          raise ValueError(
              '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
              f'''got {self.rope_scaling}''' )
        __UpperCAmelCase    :						Union[str, Any]       =						self.rope_scaling.get('''type'''						,				__lowercase )
        __UpperCAmelCase    :						List[Any]       =						self.rope_scaling.get('''factor'''						,				__lowercase )
        if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
          raise ValueError(
              f'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' )
        if rope_scaling_factor is None or not isinstance(__lowercase						,				__lowercase ) or rope_scaling_factor <= 1.0:
          raise ValueError(f'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )