



import functools
import operator

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


_snake_case						=    logging.get_logger(__name__)

_snake_case						=    {
    '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''',
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}



class     UpperCAmelCase_       (			a_):
							lowerCamelCase__       =						'wav2vec2'







							def __init__(  self,     __a=32,     __a=768,     __a=12,     __a=12,     __a=3072,     __a="gelu",     __a=0.1,     __a=0.1,     __a=0.1,     __a=0.0,     __a=0.0,     __a=0.1,     __a=0.1,     __a=0.02,     __a=1E-5,     __a="group",     __a="gelu",     __a=(512, 512, 512, 512, 512, 512, 512),     __a=(5, 2, 2, 2, 2, 2, 2),     __a=(10, 3, 3, 3, 3, 2, 2),     __a=False,     __a=128,     __a=16,     __a=False,     __a=True,     __a=0.05,     __a=10,     __a=2,     __a=0.0,     __a=10,     __a=0,     __a=320,     __a=2,     __a=0.1,     __a=100,     __a=256,     __a=256,     __a=0.1,     __a="sum",     __a=False,     __a=False,     __a=256,     __a=(512, 512, 512, 512, 1500),     __a=(5, 3, 3, 1, 1),     __a=(1, 2, 3, 1, 1),     __a=512,     __a=0,     __a=1,     __a=2,     __a=False,     __a=3,     __a=2,     __a=3,     __a=None,     __a=None,     **__a,     ):

														'''simple docstring'''

														super().__init__(**__UpperCamelCase,     pad_token_id=__UpperCamelCase,     bos_token_id=__UpperCamelCase,     eos_token_id=__UpperCamelCase)
														_lowerCAmelCase	:				List[str]					  =       hidden_size
														_lowerCAmelCase	:				List[str]					  =       feat_extract_norm
														_lowerCAmelCase	:				Optional[Any]					  =       feat_extract_activation
														_lowerCAmelCase	:				Any					  =       list(__UpperCamelCase)
														_lowerCAmelCase	:				Tuple					  =       list(__UpperCamelCase)
														_lowerCAmelCase	:				Union[str, Any]					  =       list(__UpperCamelCase)
														_lowerCAmelCase	:				Optional[int]					  =       conv_bias
														_lowerCAmelCase	:				Tuple					  =       num_conv_pos_embeddings
														_lowerCAmelCase	:				Dict					  =       num_conv_pos_embedding_groups
														_lowerCAmelCase	:				Any					  =       len(self.conv_dim)
														_lowerCAmelCase	:				int					  =       num_hidden_layers
														_lowerCAmelCase	:				Optional[int]					  =       intermediate_size
														_lowerCAmelCase	:				Optional[Any]					  =       hidden_act
														_lowerCAmelCase	:				int					  =       num_attention_heads
														_lowerCAmelCase	:				List[str]					  =       hidden_dropout
														_lowerCAmelCase	:				str					  =       attention_dropout
														_lowerCAmelCase	:				List[str]					  =       activation_dropout
														_lowerCAmelCase	:				Optional[Any]					  =       feat_proj_dropout
														_lowerCAmelCase	:				int					  =       final_dropout
														_lowerCAmelCase	:				str					  =       layerdrop
														_lowerCAmelCase	:				Dict					  =       layer_norm_eps
														_lowerCAmelCase	:				Tuple					  =       initializer_range
														_lowerCAmelCase	:				List[Any]					  =       vocab_size
														_lowerCAmelCase	:				Union[str, Any]					  =       do_stable_layer_norm
														_lowerCAmelCase	:				Optional[int]					  =       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	:				str					  =       apply_spec_augment
														_lowerCAmelCase	:				Dict					  =       mask_time_prob
														_lowerCAmelCase	:				Any					  =       mask_time_length
														_lowerCAmelCase	:				Tuple					  =       mask_time_min_masks
														_lowerCAmelCase	:				List[str]					  =       mask_feature_prob
														_lowerCAmelCase	:				List[Any]					  =       mask_feature_length
														_lowerCAmelCase	:				Dict					  =       mask_feature_min_masks

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

														# ctc loss
														_lowerCAmelCase	:				Union[str, Any]					  =       ctc_loss_reduction
														_lowerCAmelCase	:				str					  =       ctc_zero_infinity

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

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

														# XVector-specific parameters. Feel free to ignore for other classes.
														_lowerCAmelCase	:				Union[str, Any]					  =       list(__UpperCamelCase)
														_lowerCAmelCase	:				Tuple					  =       list(__UpperCamelCase)
														_lowerCAmelCase	:				List[Any]					  =       list(__UpperCamelCase)
														_lowerCAmelCase	:				Optional[int]					  =       xvector_output_dim







							@property
							def   snake_case__ (  self):

														'''simple docstring'''

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








import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list



@require_torchaudio
@require_sentencepiece
class 				_UpperCamelCase      (unittest.TestCase      ):


      def 			__UpperCAmelCase		(						self						)->						Optional[int]:
           __lowerCAmelCase             =       "laion/clap-htsat-unfused"
           __lowerCAmelCase             =       tempfile.mkdtemp()


      def 			__UpperCAmelCase		(						self       ,    **__UpperCamelCase						)->						Optional[int]:
           return RobertaTokenizer.from_pretrained(self.checkpoint       ,    **__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self       ,    **__UpperCamelCase						)->						List[Any]:
           return ClapFeatureExtractor.from_pretrained(self.checkpoint       ,    **__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Union[str, Any]:
           shutil.rmtree(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.get_tokenizer()
           __lowerCAmelCase             =       self.get_feature_extractor()

           __lowerCAmelCase             =       ClapProcessor(tokenizer=__UpperCamelCase       ,    feature_extractor=__UpperCamelCase						)

           processor.save_pretrained(self.tmpdirname						)
           __lowerCAmelCase             =       ClapProcessor.from_pretrained(self.tmpdirname						)

           self.assertEqual(processor.tokenizer.get_vocab()       ,    tokenizer.get_vocab()						)
           self.assertIsInstance(processor.tokenizer       ,    __UpperCamelCase						)

           self.assertEqual(processor.feature_extractor.to_json_string()       ,    feature_extractor.to_json_string()						)
           self.assertIsInstance(processor.feature_extractor       ,    __UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						List[str]:
           __lowerCAmelCase             =       ClapProcessor(tokenizer=self.get_tokenizer()       ,    feature_extractor=self.get_feature_extractor()						)
           processor.save_pretrained(self.tmpdirname						)

           __lowerCAmelCase             =       self.get_tokenizer(bos_token="(BOS)"       ,    eos_token="(EOS)"						)
           __lowerCAmelCase             =       self.get_feature_extractor(do_normalize=__UpperCamelCase       ,    padding_value=1.0						)

           __lowerCAmelCase             =       ClapProcessor.from_pretrained(
               self.tmpdirname       ,    bos_token="(BOS)"       ,    eos_token="(EOS)"       ,    do_normalize=__UpperCamelCase       ,    padding_value=1.0						)

           self.assertEqual(processor.tokenizer.get_vocab()       ,    tokenizer_add_kwargs.get_vocab()						)
           self.assertIsInstance(processor.tokenizer       ,    __UpperCamelCase						)

           self.assertEqual(processor.feature_extractor.to_json_string()       ,    feature_extractor_add_kwargs.to_json_string()						)
           self.assertIsInstance(processor.feature_extractor       ,    __UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						List[str]:
           __lowerCAmelCase             =       self.get_feature_extractor()
           __lowerCAmelCase             =       self.get_tokenizer()

           __lowerCAmelCase             =       ClapProcessor(tokenizer=__UpperCamelCase       ,    feature_extractor=__UpperCamelCase						)

           __lowerCAmelCase             =       floats_list((3, 1_0_0_0)						)

           __lowerCAmelCase             =       feature_extractor(__UpperCamelCase       ,    return_tensors="np"						)
           __lowerCAmelCase             =       processor(audios=__UpperCamelCase       ,    return_tensors="np"						)

           for key in input_feat_extract.keys():
                self.assertAlmostEqual(input_feat_extract[key].sum()       ,    input_processor[key].sum()       ,    delta=1e-2						)


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.get_feature_extractor()
           __lowerCAmelCase             =       self.get_tokenizer()

           __lowerCAmelCase             =       ClapProcessor(tokenizer=__UpperCamelCase       ,    feature_extractor=__UpperCamelCase						)

           __lowerCAmelCase             =       "This is a test string"

           __lowerCAmelCase             =       processor(text=__UpperCamelCase						)

           __lowerCAmelCase             =       tokenizer(__UpperCamelCase						)

           for key in encoded_tok.keys():
                self.assertListEqual(encoded_tok[key]       ,    encoded_processor[key]						)


      def 			__UpperCAmelCase		(						self						)->						int:
           __lowerCAmelCase             =       self.get_feature_extractor()
           __lowerCAmelCase             =       self.get_tokenizer()

           __lowerCAmelCase             =       ClapProcessor(tokenizer=__UpperCamelCase       ,    feature_extractor=__UpperCamelCase						)

           __lowerCAmelCase             =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

           __lowerCAmelCase             =       processor.batch_decode(__UpperCamelCase						)
           __lowerCAmelCase             =       tokenizer.batch_decode(__UpperCamelCase						)

           self.assertListEqual(__UpperCamelCase       ,    __UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.get_feature_extractor()
           __lowerCAmelCase             =       self.get_tokenizer()

           __lowerCAmelCase             =       ClapProcessor(tokenizer=__UpperCamelCase       ,    feature_extractor=__UpperCamelCase						)

           self.assertListEqual(
               processor.model_input_names[2:]       ,    feature_extractor.model_input_names       ,    msg="`processor` and `feature_extractor` model input names do not match"       ,    )






