


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


_snake_case					:		Any      =    "hf-internal-testing/tiny-random-bert"
_snake_case					:		Dict      =    os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
_snake_case					:		Tuple      =    "9b8c223d42b2188cb49d29af482996f9d0f3e5a6"

class     a		(unittest.TestCase				):




							"""simple docstring"""


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

														# File is cached at the same place the second time.
														__snake_case		:      Any										=       cached_file(lowerCamelCase   ,     lowerCamelCase    )
														self.assertEqual(lowerCamelCase   ,     lowerCamelCase    )

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

							def    __snake_case (							self	:  Tuple    )     ->   Optional[Any]:
														with self.assertRaisesRegex(lowerCamelCase   ,     "is not a valid model identifier"    ):
																					__snake_case		:      str										=       cached_file("tiny-random-bert"   ,     lowerCamelCase    )

														with self.assertRaisesRegex(lowerCamelCase   ,     "is not a valid git identifier"    ):
																					__snake_case		:      Optional[int]										=       cached_file(lowerCamelCase   ,     lowerCamelCase   ,     revision="aaaa"    )

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

							def    __snake_case (							self	:  Any    )     ->   Union[str, Any]:
														with self.assertRaisesRegex(lowerCamelCase   ,     "does not appear to have a file named"    ):
																					__snake_case		:      Tuple										=       cached_file(lowerCamelCase   ,     "conf"    )

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

														__snake_case		:      List[str]										=       cached_file(lowerCamelCase   ,     "conf"   ,     _raise_exceptions_for_missing_entries=lowerCamelCase    )
														self.assertIsNone(lowerCamelCase    )

														__snake_case		:      Dict										=       cached_file(lowerCamelCase   ,     "conf"   ,     local_files_only=lowerCamelCase   ,     _raise_exceptions_for_missing_entries=lowerCamelCase    )
														self.assertIsNone(lowerCamelCase    )

														__snake_case		:      Any										=       mock.Mock()
														__snake_case		:      Any										=       500
														__snake_case		:      Dict										=       {}
														__snake_case		:      Any										=       HTTPError
														__snake_case		:      Tuple										=       {}

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

							def    __snake_case (							self	:  Dict    )     ->   List[str]:
														self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"   ,     lowerCamelCase    )    )
														self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"   ,     lowerCamelCase    )    )
														self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"   ,     lowerCamelCase    )    )

							def    __snake_case (							self	:  List[str]    )     ->   List[str]:
														# `get_file_from_repo` returns None if the file does not exist
														self.assertIsNone(get_file_from_repo("bert-base-cased"   ,     "ahah.txt"    )    )

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

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

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






							def    __snake_case (							self	:  int    )     ->   str:
														with tempfile.TemporaryDirectory() as tmp_dir:
																					__snake_case		:      List[Any]										=       Path(lowerCamelCase    ) / "a.txt"
																					filename.touch()
																					self.assertEqual(get_file_from_repo(lowerCamelCase   ,     "a.txt"    )   ,     str(lowerCamelCase    )    )

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





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


_snake_case					:		Any      =    logging.get_logger(__name__)

_snake_case					:		Dict      =    {
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Union[str, Any]					  =  "speech_to_text_2"
							__UpperCAmelCase   :							Dict					  =  ["past_key_values"]
							__UpperCAmelCase   :							str					  =  {"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"}

							def __init__(							self	:  str   ,     lowerCamelCase	:  List[Any]=10000   ,     lowerCamelCase	:  Tuple=6   ,     lowerCamelCase	:  Optional[Any]=2048   ,     lowerCamelCase	:  Optional[int]=4   ,     lowerCamelCase	:  Optional[int]=0.0   ,     lowerCamelCase	:  str=True   ,     lowerCamelCase	:  str="relu"   ,     lowerCamelCase	:  Tuple=256   ,     lowerCamelCase	:  Union[str, Any]=0.1   ,     lowerCamelCase	:  Optional[int]=0.0   ,     lowerCamelCase	:  Any=0.0   ,     lowerCamelCase	:  Optional[int]=0.02   ,     lowerCamelCase	:  Dict=2   ,     lowerCamelCase	:  List[str]=True   ,     lowerCamelCase	:  str=1   ,     lowerCamelCase	:  Any=0   ,     lowerCamelCase	:  List[Any]=2   ,     lowerCamelCase	:  List[Any]=1024   ,     **lowerCamelCase	:  List[str]   ,     )     ->   Any:
														__snake_case		:      Tuple										=       vocab_size
														__snake_case		:      int										=       d_model
														__snake_case		:      List[str]										=       decoder_ffn_dim
														__snake_case		:      Union[str, Any]										=       decoder_layers
														__snake_case		:      Dict										=       decoder_attention_heads
														__snake_case		:      Any										=       dropout
														__snake_case		:      List[str]										=       attention_dropout
														__snake_case		:      Optional[Any]										=       activation_dropout
														__snake_case		:      Union[str, Any]										=       activation_function
														__snake_case		:      Union[str, Any]										=       init_std
														__snake_case		:      Union[str, Any]										=       decoder_layerdrop
														__snake_case		:      Optional[int]										=       use_cache
														__snake_case		:      Optional[int]										=       decoder_layers
														__snake_case		:      str										=       scale_embedding  # scale factor will be sqrt(d_model) if True
														__snake_case		:      Dict										=       max_target_positions

														super().__init__(
														    pad_token_id=lowerCamelCase   ,     bos_token_id=lowerCamelCase   ,     eos_token_id=lowerCamelCase   ,     decoder_start_token_id=lowerCamelCase   ,     **lowerCamelCase   ,     )

