






import functools
import operator

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


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

lowerCamelCase      :       Union[str, Any]    =						{
    '''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_      ):
      snake_case_	=				"""wav2vec2"""


      def __init__(						self       ,    __UpperCamelCase=3_2       ,    __UpperCamelCase=7_6_8       ,    __UpperCamelCase=1_2       ,    __UpperCamelCase=1_2       ,    __UpperCamelCase=3_0_7_2       ,    __UpperCamelCase="gelu"       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.0       ,    __UpperCamelCase=0.0       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.0_2       ,    __UpperCamelCase=1e-5       ,    __UpperCamelCase="group"       ,    __UpperCamelCase="gelu"       ,    __UpperCamelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)       ,    __UpperCamelCase=(5, 2, 2, 2, 2, 2, 2)       ,    __UpperCamelCase=(1_0, 3, 3, 3, 3, 2, 2)       ,    __UpperCamelCase=False       ,    __UpperCamelCase=1_2_8       ,    __UpperCamelCase=1_6       ,    __UpperCamelCase=False       ,    __UpperCamelCase=True       ,    __UpperCamelCase=0.0_5       ,    __UpperCamelCase=1_0       ,    __UpperCamelCase=2       ,    __UpperCamelCase=0.0       ,    __UpperCamelCase=1_0       ,    __UpperCamelCase=0       ,    __UpperCamelCase=3_2_0       ,    __UpperCamelCase=2       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=1_0_0       ,    __UpperCamelCase=2_5_6       ,    __UpperCamelCase=2_5_6       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase="sum"       ,    __UpperCamelCase=False       ,    __UpperCamelCase=False       ,    __UpperCamelCase=2_5_6       ,    __UpperCamelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)       ,    __UpperCamelCase=(5, 3, 3, 1, 1)       ,    __UpperCamelCase=(1, 2, 3, 1, 1)       ,    __UpperCamelCase=5_1_2       ,    __UpperCamelCase=0       ,    __UpperCamelCase=1       ,    __UpperCamelCase=2       ,    __UpperCamelCase=False       ,    __UpperCamelCase=3       ,    __UpperCamelCase=2       ,    __UpperCamelCase=3       ,    __UpperCamelCase=None       ,    __UpperCamelCase=None       ,    **__UpperCamelCase       ,    )->						List[str]:
           super().__init__(**__UpperCamelCase       ,    pad_token_id=__UpperCamelCase       ,    bos_token_id=__UpperCamelCase       ,    eos_token_id=__UpperCamelCase						)
           __lowerCAmelCase             =       hidden_size
           __lowerCAmelCase             =       feat_extract_norm
           __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             =       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             =       do_stable_layer_norm
           __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             =       apply_spec_augment
           __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

           # parameters for pretraining with codevector quantized representations
           __lowerCAmelCase             =       num_codevectors_per_group
           __lowerCAmelCase             =       num_codevector_groups
           __lowerCAmelCase             =       contrastive_logits_temperature
           __lowerCAmelCase             =       feat_quantizer_dropout
           __lowerCAmelCase             =       num_negatives
           __lowerCAmelCase             =       codevector_dim
           __lowerCAmelCase             =       proj_codevector_dim
           __lowerCAmelCase             =       diversity_loss_weight

           # 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
           __lowerCAmelCase             =       adapter_attn_dim

           # 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 			__UpperCAmelCase		(						self						)->						Optional[Any]:
           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"       ,    )






