




import math

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


UpperCAmelCase_						=logging.get_logger(__name__)

UpperCAmelCase_						={
    """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""",
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}


class 							__UpperCamelCase			( __UpperCAmelCase  ):





				'''simple docstring'''



				__a							: int										="""data2vec-audio"""



				def __init__( self       , UpperCAmelCase_=32       , UpperCAmelCase_=7_68       , UpperCAmelCase_=12       , UpperCAmelCase_=12       , UpperCAmelCase_=30_72       , UpperCAmelCase_="gelu"       , UpperCAmelCase_=0.1       , UpperCAmelCase_=0.1       , UpperCAmelCase_=0.1       , UpperCAmelCase_=0.0       , UpperCAmelCase_=0.1       , UpperCAmelCase_=0.1       , UpperCAmelCase_=0.02       , UpperCAmelCase_=1E-5       , UpperCAmelCase_="gelu"       , UpperCAmelCase_=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)       , UpperCAmelCase_=(5, 2, 2, 2, 2, 2, 2)       , UpperCAmelCase_=(10, 3, 3, 3, 3, 2, 2)       , UpperCAmelCase_=False       , UpperCAmelCase_=16       , UpperCAmelCase_=19       , UpperCAmelCase_=5       , UpperCAmelCase_=0.05       , UpperCAmelCase_=10       , UpperCAmelCase_=2       , UpperCAmelCase_=0.0       , UpperCAmelCase_=10       , UpperCAmelCase_=0       , UpperCAmelCase_="sum"       , UpperCAmelCase_=False       , UpperCAmelCase_=False       , UpperCAmelCase_=2_56       , UpperCAmelCase_=(5_12, 5_12, 5_12, 5_12, 15_00)       , UpperCAmelCase_=(5, 3, 3, 1, 1)       , UpperCAmelCase_=(1, 2, 3, 1, 1)       , UpperCAmelCase_=5_12       , UpperCAmelCase_=0       , UpperCAmelCase_=1       , UpperCAmelCase_=2       , UpperCAmelCase_=False       , UpperCAmelCase_=3       , UpperCAmelCase_=2       , UpperCAmelCase_=3       , UpperCAmelCase_=None       , **UpperCAmelCase_       , ):
											super().__init__(**UpperCAmelCase_       , pad_token_id=UpperCAmelCase_       , bos_token_id=UpperCAmelCase_       , eos_token_id=UpperCAmelCase_						)
											lowerCAmelCase   =		hidden_size
											lowerCAmelCase   =		feat_extract_activation
											lowerCAmelCase   =		list(UpperCAmelCase_						)
											lowerCAmelCase   =		list(UpperCAmelCase_						)
											lowerCAmelCase   =		list(UpperCAmelCase_						)
											lowerCAmelCase   =		conv_bias
											lowerCAmelCase   =		num_conv_pos_embeddings
											lowerCAmelCase   =		num_conv_pos_embedding_groups
											lowerCAmelCase   =		conv_pos_kernel_size
											lowerCAmelCase   =		len(self.conv_dim						)
											lowerCAmelCase   =		num_hidden_layers
											lowerCAmelCase   =		intermediate_size
											lowerCAmelCase   =		hidden_act
											lowerCAmelCase   =		num_attention_heads
											lowerCAmelCase   =		hidden_dropout
											lowerCAmelCase   =		attention_dropout
											lowerCAmelCase   =		activation_dropout
											lowerCAmelCase   =		feat_proj_dropout
											lowerCAmelCase   =		final_dropout
											lowerCAmelCase   =		layerdrop
											lowerCAmelCase   =		layer_norm_eps
											lowerCAmelCase   =		initializer_range
											lowerCAmelCase   =		vocab_size
											lowerCAmelCase   =		use_weighted_layer_sum

											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
											lowerCAmelCase   =		mask_time_prob
											lowerCAmelCase   =		mask_time_length
											lowerCAmelCase   =		mask_time_min_masks
											lowerCAmelCase   =		mask_feature_prob
											lowerCAmelCase   =		mask_feature_length
											lowerCAmelCase   =		mask_feature_min_masks

											# ctc loss
											lowerCAmelCase   =		ctc_loss_reduction
											lowerCAmelCase   =		ctc_zero_infinity

											# adapter
											lowerCAmelCase   =		add_adapter
											lowerCAmelCase   =		adapter_kernel_size
											lowerCAmelCase   =		adapter_stride
											lowerCAmelCase   =		num_adapter_layers
											lowerCAmelCase   =		output_hidden_size or hidden_size

											# SequenceClassification-specific parameter. Feel free to ignore for other classes.
											lowerCAmelCase   =		classifier_proj_size

											# XVector-specific parameters. Feel free to ignore for other classes.
											lowerCAmelCase   =		list(UpperCAmelCase_						)
											lowerCAmelCase   =		list(UpperCAmelCase_						)
											lowerCAmelCase   =		list(UpperCAmelCase_						)
											lowerCAmelCase   =		xvector_output_dim






				@property
				def       __snake_case		( self						):
											return math.prod(self.conv_stride						)









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


UpperCAmelCase_						="""hf-internal-testing/tiny-random-bert"""
UpperCAmelCase_						=os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
UpperCAmelCase_						="""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""


class 							__UpperCamelCase			( unittest.TestCase  ):





				'''simple docstring'''



				def       __snake_case		( self						):
											lowerCAmelCase   =		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:
																		lowerCAmelCase   =		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.
											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , UpperCAmelCase_						)
											self.assertEqual(UpperCAmelCase_       , UpperCAmelCase_						)

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



				def       __snake_case		( self						):
											with self.assertRaisesRegex(UpperCAmelCase_       , '''is not a valid model identifier'''						):
																		lowerCAmelCase   =		cached_file('''tiny-random-bert'''       , UpperCAmelCase_						)

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

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



				def       __snake_case		( self						):
											with self.assertRaisesRegex(UpperCAmelCase_       , '''does not appear to have a file named'''						):
																		lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''						)

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

											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''       , _raise_exceptions_for_missing_entries=UpperCAmelCase_						)
											self.assertIsNone(UpperCAmelCase_						)

											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''       , local_files_only=UpperCAmelCase_       , _raise_exceptions_for_missing_entries=UpperCAmelCase_						)
											self.assertIsNone(UpperCAmelCase_						)

											lowerCAmelCase   =		mock.Mock()
											lowerCAmelCase   =		5_00
											lowerCAmelCase   =		{}
											lowerCAmelCase   =		HTTPError
											lowerCAmelCase   =		{}

											# 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:
																		lowerCAmelCase   =		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       __snake_case		( self						):
											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       __snake_case		( self						):
											# `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(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'''						)

											lowerCAmelCase   =		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.
											lowerCAmelCase   =		json.loads(open(UpperCAmelCase_       , '''r'''						).read()						)
											self.assertEqual(config['''hidden_size''']       , 7_68						)






				def       __snake_case		( self						):
											with tempfile.TemporaryDirectory() as tmp_dir:
																		lowerCAmelCase   =		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'''						)						)



