



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


lowerCamelCase       	=						"""hf-internal-testing/tiny-random-bert"""
lowerCamelCase       	=						os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
lowerCamelCase       	=						"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       def 					lowercase__    (							self      :  Any   )			->						List[Any]:


             '''simple docstring'''




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

             # File is cached at the same place the second time.
             UpperCAmelCase_      			=							cached_file(_UpperCAmelCase		,    _UpperCAmelCase   )
             self.assertEqual(_UpperCAmelCase		,    _UpperCAmelCase   )

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



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


             '''simple docstring'''




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

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

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



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


             '''simple docstring'''




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

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

             UpperCAmelCase_      			=							cached_file(_UpperCAmelCase		,    "conf"		,    _raise_exceptions_for_missing_entries=_UpperCAmelCase   )
             self.assertIsNone(_UpperCAmelCase   )

             UpperCAmelCase_      			=							cached_file(_UpperCAmelCase		,    "conf"		,    local_files_only=_UpperCAmelCase		,    _raise_exceptions_for_missing_entries=_UpperCAmelCase   )
             self.assertIsNone(_UpperCAmelCase   )

             UpperCAmelCase_      			=							mock.Mock()
             UpperCAmelCase_      			=							500
             UpperCAmelCase_      			=							{}
             UpperCAmelCase_      			=							HTTPError
             UpperCAmelCase_      			=							{}

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



       def 					lowercase__    (							self      :  List[Any]   )			->						List[str]:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  Optional[int]   )			->						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(_UpperCAmelCase		,    "is not a valid model identifier"   ):
                   get_file_from_repo("bert-base-case"		,    _UpperCAmelCase   )

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

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







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


             '''simple docstring'''




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

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









"""simple docstring"""





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


lowerCamelCase       	=						logging.get_logger(__name__)

lowerCamelCase       	=						{
    """RWKV/rwkv-4-169m-pile""": """https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-430m-pile""": """https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-1b5-pile""": """https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-3b-pile""": """https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-7b-pile""": """https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-14b-pile""": """https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json""",
    """RWKV/rwkv-raven-1b5""": """https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json""",
    """RWKV/rwkv-raven-3b""": """https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json""",
    """RWKV/rwkv-raven-7b""": """https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json""",
    """RWKV/rwkv-raven-14b""": """https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json""",
}
class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					'''rwkv'''
       UpperCamelCase						=					{'''max_position_embeddings''': '''context_length'''}



       def __init__(							self      :  str		,    _UpperCAmelCase      :  int=50277		,    _UpperCAmelCase      :  Optional[Any]=1024		,    _UpperCAmelCase      :  str=4096		,    _UpperCAmelCase      :  Any=32		,    _UpperCAmelCase      :  Any=None		,    _UpperCAmelCase      :  Optional[int]=None		,    _UpperCAmelCase      :  Tuple=1e-5		,    _UpperCAmelCase      :  str=0		,    _UpperCAmelCase      :  Any=0		,    _UpperCAmelCase      :  Union[str, Any]=6		,    _UpperCAmelCase      :  Dict=False		,    _UpperCAmelCase      :  Any=True		,    **_UpperCAmelCase      :  Optional[Any]		,    )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							vocab_size
             UpperCAmelCase_      			=							context_length
             UpperCAmelCase_      			=							hidden_size
             UpperCAmelCase_      			=							num_hidden_layers
             UpperCAmelCase_      			=							attention_hidden_size if attention_hidden_size is not None else hidden_size
             UpperCAmelCase_      			=							intermediate_size if intermediate_size is not None else 4 * hidden_size
             UpperCAmelCase_      			=							layer_norm_epsilon
             UpperCAmelCase_      			=							rescale_every
             UpperCAmelCase_      			=							use_cache

             UpperCAmelCase_      			=							bos_token_id
             UpperCAmelCase_      			=							eos_token_id

             super().__init__(
                 tie_word_embeddings=_UpperCAmelCase		,    bos_token_id=_UpperCAmelCase		,    eos_token_id=_UpperCAmelCase		,    **_UpperCAmelCase   )




