

import math

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


lowerCAmelCase							:  Any            =						logging.get_logger(__name__)

lowerCAmelCase							:  List[str]            =						{
    """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   __lowercase    (			UpperCAmelCase_     ):




			"""simple docstring"""







			_UpperCAmelCase       :							List[str]      							=					'''data2vec-audio'''



			def __init__(			self  :					Any       ,						lowerCAmelCase__  :					Dict=32       ,						lowerCAmelCase__  :					str=768       ,						lowerCAmelCase__  :					Union[str, Any]=12       ,						lowerCAmelCase__  :					Tuple=12       ,						lowerCAmelCase__  :					List[str]=3072       ,						lowerCAmelCase__  :					str="gelu"       ,						lowerCAmelCase__  :					Tuple=0.1       ,						lowerCAmelCase__  :					Tuple=0.1       ,						lowerCAmelCase__  :					Optional[int]=0.1       ,						lowerCAmelCase__  :					Optional[Any]=0.0       ,						lowerCAmelCase__  :					List[str]=0.1       ,						lowerCAmelCase__  :					List[Any]=0.1       ,						lowerCAmelCase__  :					List[Any]=0.02       ,						lowerCAmelCase__  :					Optional[Any]=1E-5       ,						lowerCAmelCase__  :					List[Any]="gelu"       ,						lowerCAmelCase__  :					Union[str, Any]=(512, 512, 512, 512, 512, 512, 512)       ,						lowerCAmelCase__  :					Any=(5, 2, 2, 2, 2, 2, 2)       ,						lowerCAmelCase__  :					List[str]=(10, 3, 3, 3, 3, 2, 2)       ,						lowerCAmelCase__  :					Union[str, Any]=False       ,						lowerCAmelCase__  :					List[Any]=16       ,						lowerCAmelCase__  :					List[str]=19       ,						lowerCAmelCase__  :					Dict=5       ,						lowerCAmelCase__  :					Optional[Any]=0.05       ,						lowerCAmelCase__  :					List[Any]=10       ,						lowerCAmelCase__  :					str=2       ,						lowerCAmelCase__  :					Optional[Any]=0.0       ,						lowerCAmelCase__  :					Optional[int]=10       ,						lowerCAmelCase__  :					Tuple=0       ,						lowerCAmelCase__  :					Tuple="sum"       ,						lowerCAmelCase__  :					List[Any]=False       ,						lowerCAmelCase__  :					Tuple=False       ,						lowerCAmelCase__  :					Tuple=256       ,						lowerCAmelCase__  :					Dict=(512, 512, 512, 512, 1500)       ,						lowerCAmelCase__  :					Any=(5, 3, 3, 1, 1)       ,						lowerCAmelCase__  :					List[str]=(1, 2, 3, 1, 1)       ,						lowerCAmelCase__  :					int=512       ,						lowerCAmelCase__  :					Any=0       ,						lowerCAmelCase__  :					str=1       ,						lowerCAmelCase__  :					List[Any]=2       ,						lowerCAmelCase__  :					Optional[int]=False       ,						lowerCAmelCase__  :					Union[str, Any]=3       ,						lowerCAmelCase__  :					Union[str, Any]=2       ,						lowerCAmelCase__  :					str=3       ,						lowerCAmelCase__  :					Optional[Any]=None       ,						**lowerCAmelCase__  :					Any       ,						):
									super().__init__(**lowerCAmelCase__       ,						pad_token_id=lowerCAmelCase__       ,						bos_token_id=lowerCAmelCase__       ,						eos_token_id=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			List[str]  		=	hidden_size
									SCREAMING_SNAKE_CASE_:			List[str]  		=	feat_extract_activation
									SCREAMING_SNAKE_CASE_:			Tuple  		=	list(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			str  		=	list(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	list(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	conv_bias
									SCREAMING_SNAKE_CASE_:			Dict  		=	num_conv_pos_embeddings
									SCREAMING_SNAKE_CASE_:			Tuple  		=	num_conv_pos_embedding_groups
									SCREAMING_SNAKE_CASE_:			int  		=	conv_pos_kernel_size
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	len(self.conv_dim)
									SCREAMING_SNAKE_CASE_:			int  		=	num_hidden_layers
									SCREAMING_SNAKE_CASE_:			Any  		=	intermediate_size
									SCREAMING_SNAKE_CASE_:			Tuple  		=	hidden_act
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	num_attention_heads
									SCREAMING_SNAKE_CASE_:			str  		=	hidden_dropout
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	attention_dropout
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	activation_dropout
									SCREAMING_SNAKE_CASE_:			Tuple  		=	feat_proj_dropout
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	final_dropout
									SCREAMING_SNAKE_CASE_:			str  		=	layerdrop
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	layer_norm_eps
									SCREAMING_SNAKE_CASE_:			Dict  		=	initializer_range
									SCREAMING_SNAKE_CASE_:			List[str]  		=	vocab_size
									SCREAMING_SNAKE_CASE_:			List[str]  		=	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
									SCREAMING_SNAKE_CASE_:			Any  		=	mask_time_prob
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	mask_time_length
									SCREAMING_SNAKE_CASE_:			List[str]  		=	mask_time_min_masks
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	mask_feature_prob
									SCREAMING_SNAKE_CASE_:			Any  		=	mask_feature_length
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	mask_feature_min_masks

									# ctc loss
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	ctc_loss_reduction
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	ctc_zero_infinity

									# adapter
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	add_adapter
									SCREAMING_SNAKE_CASE_:			Any  		=	adapter_kernel_size
									SCREAMING_SNAKE_CASE_:			Any  		=	adapter_stride
									SCREAMING_SNAKE_CASE_:			List[str]  		=	num_adapter_layers
									SCREAMING_SNAKE_CASE_:			Any  		=	output_hidden_size or hidden_size

									# SequenceClassification-specific parameter. Feel free to ignore for other classes.
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	classifier_proj_size

									# XVector-specific parameters. Feel free to ignore for other classes.
									SCREAMING_SNAKE_CASE_:			List[str]  		=	list(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			Tuple  		=	list(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			str  		=	list(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	xvector_output_dim




			@property
			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									return math.prod(self.conv_stride)









import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402


lowerCAmelCase							:  Union[str, Any]            =						get_tests_dir("""fixtures/dummy-config.json""")



class   __lowercase    (			unittest.TestCase     ):




			"""simple docstring"""







			def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
									SCREAMING_SNAKE_CASE_:			Dict  		=	0



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Any):
									self.assertIsNotNone(transformers.models.auto.__spec__)
									self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"))



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	AutoConfig.from_pretrained("bert-base-uncased")
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					str):
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					int):
									SCREAMING_SNAKE_CASE_:			Tuple  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Union[str, Any]):
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	AutoConfig.for_model("roberta")
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									with tempfile.TemporaryDirectory() as tmp_dir:
															# This model name contains bert and roberta, but roberta ends up being picked.
															SCREAMING_SNAKE_CASE_:			int  		=	os.path.join(lowerCAmelCase__       ,						"fake-roberta")
															os.makedirs(lowerCAmelCase__       ,						exist_ok=lowerCAmelCase__)
															with open(os.path.join(lowerCAmelCase__       ,						"config.json")       ,						"w") as f:
																					f.write(json.dumps({}))
															SCREAMING_SNAKE_CASE_:			Any  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
															self.assertEqual(type(lowerCAmelCase__)       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Optional[int]):
									try:
															AutoConfig.register("custom"       ,						lowerCAmelCase__)
															# Wrong model type will raise an error
															with self.assertRaises(lowerCAmelCase__):
																					AutoConfig.register("model"       ,						lowerCAmelCase__)
															# Trying to register something existing in the Transformers library will raise an error
															with self.assertRaises(lowerCAmelCase__):
																					AutoConfig.register("bert"       ,						lowerCAmelCase__)

															# Now that the config is registered, it can be used as any other config with the auto-API
															SCREAMING_SNAKE_CASE_:			List[Any]  		=	CustomConfig()
															with tempfile.TemporaryDirectory() as tmp_dir:
																					config.save_pretrained(lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_:			Tuple  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
																					self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)

									finally:
															if "custom" in CONFIG_MAPPING._extra_content:
																					del CONFIG_MAPPING._extra_content["custom"]



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]):
									with self.assertRaisesRegex(
									    lowerCAmelCase__       ,						"bert-base is not a local folder and is not a valid model identifier"):
															SCREAMING_SNAKE_CASE_:			List[str]  		=	AutoConfig.from_pretrained("bert-base")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									with self.assertRaisesRegex(
									    lowerCAmelCase__       ,						R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"):
															SCREAMING_SNAKE_CASE_:			str  		=	AutoConfig.from_pretrained(lowerCAmelCase__       ,						revision="aaaaaa")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Optional[int]):
									with self.assertRaisesRegex(
									    lowerCAmelCase__       ,						"hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."       ,						):
															SCREAMING_SNAKE_CASE_:			int  		=	AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]):
									# If remote code is not set, we will time out when asking whether to load the model.
									with self.assertRaises(lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_:			str  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model")
									# If remote code is disabled, we can't load this config.
									with self.assertRaises(lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:			str  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)
									self.assertEqual(config.__class__.__name__       ,						"NewModelConfig")

									# Test config can be reloaded.
									with tempfile.TemporaryDirectory() as tmp_dir:
															config.save_pretrained(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_:			Any  		=	AutoConfig.from_pretrained(lowerCAmelCase__       ,						trust_remote_code=lowerCAmelCase__)
									self.assertEqual(reloaded_config.__class__.__name__       ,						"NewModelConfig")




			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):



									class   __lowercase    (			UpperCAmelCase_     ):




												"""simple docstring"""







												_UpperCAmelCase       :							int      							=					'''new-model'''

									try:
															AutoConfig.register("new-model"       ,						lowerCAmelCase__)
															# If remote code is not set, the default is to use local
															SCREAMING_SNAKE_CASE_:			int  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model")
															self.assertEqual(config.__class__.__name__       ,						"NewModelConfigLocal")

															# If remote code is disabled, we load the local one.
															SCREAMING_SNAKE_CASE_:			Optional[int]  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)
															self.assertEqual(config.__class__.__name__       ,						"NewModelConfigLocal")

															# If remote is enabled, we load from the Hub
															SCREAMING_SNAKE_CASE_:			int  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)
															self.assertEqual(config.__class__.__name__       ,						"NewModelConfig")

									finally:
															if "new-model" in CONFIG_MAPPING._extra_content:
																					del CONFIG_MAPPING._extra_content["new-model"]






