
"""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_							:    Any         =						"""hf-internal-testing/tiny-random-bert"""
lowercase_							:    Optional[int]         =						os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
lowercase_							:    Optional[Any]         =						"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""
class       UpperCamelCase   (				unittest.TestCase	):
     def 					__SCREAMING_SNAKE_CASE				(  self					):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE :					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:
         _SCREAMING_SNAKE_CASE :					List[str]				     =			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.
       _SCREAMING_SNAKE_CASE :					Dict				     =			cached_file(_a	,  _a					)
       self.assertEqual(_a	,  _a					)

       # Using a specific revision to test the full commit hash.
       _SCREAMING_SNAKE_CASE :					Optional[Any]				     =			cached_file(_a	,  _a	,  revision="9b8c223"					)
       self.assertEqual(_a	,  os.path.join(_a	,  "snapshots"	,  _a	,  _a					)					)
     def 					__SCREAMING_SNAKE_CASE				(  self					):




       """simple docstring"""


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

       with self.assertRaisesRegex(_a	,  "is not a valid git identifier"					):
         _SCREAMING_SNAKE_CASE :					Dict				     =			cached_file(_a	,  _a	,  revision="aaaa"					)

       with self.assertRaisesRegex(_a	,  "does not appear to have a file named"					):
         _SCREAMING_SNAKE_CASE :					List[str]				     =			cached_file(_a	,  "conf"					)
     def 					__SCREAMING_SNAKE_CASE				(  self					):




       """simple docstring"""


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

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

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

       _SCREAMING_SNAKE_CASE :					Any				     =			cached_file(_a	,  "conf"	,  local_files_only=_a	,  _raise_exceptions_for_missing_entries=_a					)
       self.assertIsNone(_a					)

       _SCREAMING_SNAKE_CASE :					List[str]				     =			mock.Mock()
       _SCREAMING_SNAKE_CASE :					Any				     =			500
       _SCREAMING_SNAKE_CASE :					str				     =			{}
       _SCREAMING_SNAKE_CASE :					Any				     =			HTTPError
       _SCREAMING_SNAKE_CASE :					Optional[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:
         _SCREAMING_SNAKE_CASE :					List[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 					__SCREAMING_SNAKE_CASE				(  self					):




       """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 					__SCREAMING_SNAKE_CASE				(  self					):




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

       _SCREAMING_SNAKE_CASE :					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.
       _SCREAMING_SNAKE_CASE :					Tuple				     =			json.loads(open(_a	,  "r"					).read()					)
       self.assertEqual(config["hidden_size"]	,  768					)




     def 					__SCREAMING_SNAKE_CASE				(  self					):




       """simple docstring"""


       with tempfile.TemporaryDirectory() as tmp_dir:
         _SCREAMING_SNAKE_CASE :					List[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


lowerCAmelCase				:     Union[str, Any]					=				logging.get_logger(__name__)

lowerCAmelCase				:     Union[str, Any]					=				{
    """speechbrain/m-ctc-t-large""": """https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json""",
    # See all M-CTC-T models at https://huggingface.co/models?filter=mctct
}






class        __magic_name__				(			UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		"mctct"





				def __init__(			self						,					_a=8_065						,					_a=1_536						,					_a=36						,					_a=6_144						,					_a=4						,					_a=384						,					_a=920						,					_a=1e-5						,					_a=0.3						,					_a="relu"						,					_a=0.02						,					_a=0.3						,					_a=0.3						,					_a=1						,					_a=0						,					_a=2						,					_a=1						,					_a=0.3						,					_a=1						,					_a=(7,)						,					_a=(3,)						,					_a=80						,					_a=1						,					_a=None						,					_a="sum"						,					_a=False						,					**_a						,					):





							"""simple docstring"""
							super().__init__(**_a						,					pad_token_id=_a						,					bos_token_id=_a						,					eos_token_id=_a					)
							lowerCamelCase      =   vocab_size
							lowerCamelCase      =   hidden_size
							lowerCamelCase      =   num_hidden_layers
							lowerCamelCase      =   intermediate_size
							lowerCamelCase      =   num_attention_heads
							lowerCamelCase      =   attention_head_dim
							lowerCamelCase      =   max_position_embeddings
							lowerCamelCase      =   layer_norm_eps
							lowerCamelCase      =   layerdrop
							lowerCamelCase      =   hidden_act
							lowerCamelCase      =   initializer_range
							lowerCamelCase      =   hidden_dropout_prob
							lowerCamelCase      =   attention_probs_dropout_prob
							lowerCamelCase      =   pad_token_id
							lowerCamelCase      =   bos_token_id
							lowerCamelCase      =   eos_token_id
							lowerCamelCase      =   conv_glu_dim
							lowerCamelCase      =   conv_dropout
							lowerCamelCase      =   num_conv_layers
							lowerCamelCase      =   input_feat_per_channel
							lowerCamelCase      =   input_channels
							lowerCamelCase      =   conv_channels
							lowerCamelCase      =   ctc_loss_reduction
							lowerCamelCase      =   ctc_zero_infinity

							# prevents config testing fail with exporting to json
							lowerCamelCase      =   list(_a					)
							lowerCamelCase      =   list(_a					)

							if len(self.conv_kernel					) != self.num_conv_layers:
										raise ValueError(
										    """Configuration for convolutional module is incorrect. """
										    """It is required that `len(config.conv_kernel)` == `config.num_conv_layers` """
										    f'but is `len(config.conv_kernel) = {len(self.conv_kernel					)}`, '
										    f'`config.num_conv_layers = {self.num_conv_layers}`.'					)



