



"""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__			   =  "hf-internal-testing/tiny-random-bert"
lowercase__			   =  os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
lowercase__			   =  "9b8c223d42b2188cb49d29af482996f9d0f3e5a6"




class SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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

													# File is cached at the same place the second time.
													__a		:		List[Any]									=       cached_file(_lowercase						,     _lowercase							)
													self.assertEqual(_lowercase						,     _lowercase							)

													# Using a specific revision to test the full commit hash.
													__a		:		List[Any]									=       cached_file(_lowercase						,     _lowercase						,     revision="""9b8c223"""							)
													self.assertEqual(_lowercase						,     os.path.join(_lowercase						,     """snapshots"""						,     _lowercase						,     _lowercase							)							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													with self.assertRaisesRegex(_lowercase						,     """is not a valid model identifier"""							):
																				__a		:		Union[str, Any]									=       cached_file("""tiny-random-bert"""						,     _lowercase							)

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

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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

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

													__a		:		List[Any]									=       cached_file(_lowercase						,     """conf"""						,     _raise_exceptions_for_missing_entries=_lowercase							)
													self.assertIsNone(_lowercase							)

													__a		:		str									=       cached_file(_lowercase						,     """conf"""						,     local_files_only=_lowercase						,     _raise_exceptions_for_missing_entries=_lowercase							)
													self.assertIsNone(_lowercase							)

													__a		:		List[Any]									=       mock.Mock()
													__a		:		List[Any]									=       500
													__a		:		int									=       {}
													__a		:		Any									=       HTTPError
													__a		:		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=_lowercase							) as mock_head:
																				__a		:		Union[str, Any]									=       cached_file(_lowercase						,     """conf"""						,     _raise_exceptions_for_connection_errors=_lowercase							)
																				self.assertIsNone(_lowercase							)
																				# This check we did call the fake head request
																				mock_head.assert_called()



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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



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

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

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





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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

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






"""simple docstring"""

import functools
import operator

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


lowercase__			   =  logging.get_logger(__name__)

lowercase__			   =  {
    "microsoft/unispeech-large-1500h-cv": (
        "https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}




class SCREAMING_SNAKE_CASE__					(		__snake_case      ):
						_lowerCAmelCase							=					"unispeech"



						def __init__(self						,     _lowercase=32						,     _lowercase=768						,     _lowercase=12						,     _lowercase=12						,     _lowercase=3072						,     _lowercase="gelu"						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=0.0						,     _lowercase=0.0						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=0.02						,     _lowercase=1e-5						,     _lowercase="group"						,     _lowercase="gelu"						,     _lowercase=(512, 512, 512, 512, 512, 512, 512)						,     _lowercase=(5, 2, 2, 2, 2, 2, 2)						,     _lowercase=(10, 3, 3, 3, 3, 2, 2)						,     _lowercase=False						,     _lowercase=128						,     _lowercase=16						,     _lowercase=False						,     _lowercase=True						,     _lowercase=0.05						,     _lowercase=10						,     _lowercase=2						,     _lowercase=0.0						,     _lowercase=10						,     _lowercase=0						,     _lowercase=320						,     _lowercase=2						,     _lowercase=0.1						,     _lowercase=100						,     _lowercase=256						,     _lowercase=256						,     _lowercase=0.1						,     _lowercase="mean"						,     _lowercase=False						,     _lowercase=False						,     _lowercase=256						,     _lowercase=80						,     _lowercase=0						,     _lowercase=1						,     _lowercase=2						,     _lowercase=0.5						,     **_lowercase						,     ):





													'''simple docstring'''

													super().__init__(**_lowercase						,     pad_token_id=_lowercase						,     bos_token_id=_lowercase						,     eos_token_id=_lowercase							)
													__a		:		Union[str, Any]									=       hidden_size
													__a		:		Any									=       feat_extract_norm
													__a		:		Union[str, Any]									=       feat_extract_activation
													__a		:		Tuple									=       list(_lowercase							)
													__a		:		Dict									=       list(_lowercase							)
													__a		:		List[Any]									=       list(_lowercase							)
													__a		:		List[Any]									=       conv_bias
													__a		:		Optional[Any]									=       num_conv_pos_embeddings
													__a		:		Union[str, Any]									=       num_conv_pos_embedding_groups
													__a		:		Dict									=       len(self.conv_dim							)
													__a		:		Dict									=       num_hidden_layers
													__a		:		Union[str, Any]									=       intermediate_size
													__a		:		List[str]									=       hidden_act
													__a		:		int									=       num_attention_heads
													__a		:		int									=       hidden_dropout
													__a		:		Any									=       attention_dropout
													__a		:		List[Any]									=       activation_dropout
													__a		:		List[Any]									=       feat_proj_dropout
													__a		:		Union[str, Any]									=       final_dropout
													__a		:		str									=       layerdrop
													__a		:		Dict									=       layer_norm_eps
													__a		:		Dict									=       initializer_range
													__a		:		Union[str, Any]									=       num_ctc_classes
													__a		:		List[Any]									=       vocab_size
													__a		:		Any									=       do_stable_layer_norm
													__a		:		List[str]									=       use_weighted_layer_sum
													__a		:		List[str]									=       classifier_proj_size

													if (
													    (len(self.conv_stride							) != self.num_feat_extract_layers)
													    or (len(self.conv_kernel							) != self.num_feat_extract_layers)
													    or (len(self.conv_dim							) != self.num_feat_extract_layers)
													):
																				raise ValueError(
																				    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
																				    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
																				    F''' {len(self.conv_dim							)}`, `len(config.conv_stride) = {len(self.conv_stride							)}`,'''
																				    F''' `len(config.conv_kernel) = {len(self.conv_kernel							)}`.'''							)

													# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
													__a		:		Dict									=       apply_spec_augment
													__a		:		Union[str, Any]									=       mask_time_prob
													__a		:		List[str]									=       mask_time_length
													__a		:		Dict									=       mask_time_min_masks
													__a		:		List[Any]									=       mask_feature_prob
													__a		:		Tuple									=       mask_feature_length
													__a		:		int									=       mask_feature_min_masks

													# parameters for pretraining with codevector quantized representations
													__a		:		List[Any]									=       num_codevectors_per_group
													__a		:		Union[str, Any]									=       num_codevector_groups
													__a		:		List[Any]									=       contrastive_logits_temperature
													__a		:		Any									=       feat_quantizer_dropout
													__a		:		Optional[int]									=       num_negatives
													__a		:		List[str]									=       codevector_dim
													__a		:		List[Any]									=       proj_codevector_dim
													__a		:		Tuple									=       diversity_loss_weight

													# ctc loss
													__a		:		Any									=       ctc_loss_reduction
													__a		:		List[str]									=       ctc_zero_infinity

													# pretraining loss
													__a		:		Tuple									=       replace_prob





						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return functools.reduce(operator.mul						,     self.conv_stride						,     1							)

