





import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
      pass

if is_speech_available():
      from transformers import EncodecFeatureExtractor, MusicgenProcessor


UpperCAmelCase_       : Tuple							=    random.Random()






def 				_lowerCAmelCase			(	_a :       Union[str, Any]       , _a :       Union[str, Any]=1.0       , _a :       Any=None       , _a :       Union[str, Any]=None      )						->      int:
     if rng is None:
          lowerCAmelCase_       :							List[str]			 =		global_rng

     lowerCAmelCase_       :							Any			 =		[]
     for batch_idx in range(shape[0]      ):
          values.append([]      )
          for _ in range(shape[1]      ):
               values[-1].append(rng.random() * scale      )

     return values







@require_torch
@require_sentencepiece
class   lowercase__     (				unittest.TestCase	):







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							int			 =		"""facebook/musicgen-small"""
       lowerCAmelCase_       :							str			 =		tempfile.mkdtemp()







  def 						UpperCAmelCase__		(						self     ,    **_lowercase    ):
       return TaTokenizer.from_pretrained(self.checkpoint     ,    **_lowercase    )







  def 						UpperCAmelCase__		(						self     ,    **_lowercase    ):
       return EncodecFeatureExtractor.from_pretrained(self.checkpoint     ,    **_lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       shutil.rmtree(self.tmpdirname    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.get_tokenizer()
       lowerCAmelCase_       :							Optional[int]			 =		self.get_feature_extractor()

       lowerCAmelCase_       :							str			 =		MusicgenProcessor(tokenizer=_lowercase     ,    feature_extractor=_lowercase    )

       processor.save_pretrained(self.tmpdirname    )
       lowerCAmelCase_       :							Optional[int]			 =		MusicgenProcessor.from_pretrained(self.tmpdirname    )

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

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[Any]			 =		MusicgenProcessor(tokenizer=self.get_tokenizer()     ,    feature_extractor=self.get_feature_extractor()    )
       processor.save_pretrained(self.tmpdirname    )

       lowerCAmelCase_       :							Union[str, Any]			 =		self.get_tokenizer(bos_token="""(BOS)"""     ,    eos_token="""(EOS)"""    )
       lowerCAmelCase_       :							Union[str, Any]			 =		self.get_feature_extractor(do_normalize=_lowercase     ,    padding_value=1.0    )

       lowerCAmelCase_       :							Optional[Any]			 =		MusicgenProcessor.from_pretrained(
           self.tmpdirname     ,    bos_token="""(BOS)"""     ,    eos_token="""(EOS)"""     ,    do_normalize=_lowercase     ,    padding_value=1.0    )

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

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							List[str]			 =		self.get_feature_extractor()
       lowerCAmelCase_       :							str			 =		self.get_tokenizer()

       lowerCAmelCase_       :							Any			 =		MusicgenProcessor(tokenizer=_lowercase     ,    feature_extractor=_lowercase    )

       lowerCAmelCase_       :							Optional[Any]			 =		floats_list((3, 1_000)    )

       lowerCAmelCase_       :							int			 =		feature_extractor(_lowercase     ,    return_tensors="""np"""    )
       lowerCAmelCase_       :							Optional[int]			 =		processor(_lowercase     ,    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    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.get_feature_extractor()
       lowerCAmelCase_       :							str			 =		self.get_tokenizer()

       lowerCAmelCase_       :							List[str]			 =		MusicgenProcessor(tokenizer=_lowercase     ,    feature_extractor=_lowercase    )

       lowerCAmelCase_       :							List[Any]			 =		"""This is a test string"""

       lowerCAmelCase_       :							Tuple			 =		processor(text=_lowercase    )

       lowerCAmelCase_       :							List[str]			 =		tokenizer(_lowercase    )

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Any			 =		self.get_feature_extractor()
       lowerCAmelCase_       :							List[Any]			 =		self.get_tokenizer()

       lowerCAmelCase_       :							Optional[int]			 =		MusicgenProcessor(tokenizer=_lowercase     ,    feature_extractor=_lowercase    )

       lowerCAmelCase_       :							Union[str, Any]			 =		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       lowerCAmelCase_       :							Union[str, Any]			 =		processor.batch_decode(sequences=_lowercase    )
       lowerCAmelCase_       :							List[Any]			 =		tokenizer.batch_decode(_lowercase    )

       self.assertListEqual(_lowercase     ,    _lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							int			 =		self.get_feature_extractor()
       lowerCAmelCase_       :							int			 =		self.get_tokenizer()

       lowerCAmelCase_       :							int			 =		MusicgenProcessor(tokenizer=_lowercase     ,    feature_extractor=_lowercase    )

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.get_feature_extractor(padding_side="""left"""    )
       lowerCAmelCase_       :							Optional[Any]			 =		self.get_tokenizer()

       lowerCAmelCase_       :							List[Any]			 =		MusicgenProcessor(tokenizer=_lowercase     ,    feature_extractor=_lowercase    )

       lowerCAmelCase_       :							List[Any]			 =		[floats_list((1, x)    )[0] for x in range(5     ,    20     ,    5    )]
       lowerCAmelCase_       :							List[str]			 =		processor(_lowercase    ).padding_mask

       lowerCAmelCase_       :							List[str]			 =		np.asarray(floats_list((3, 20)    )    )[:, None, :]
       lowerCAmelCase_       :							Dict			 =		processor.batch_decode(_lowercase     ,    padding_mask=_lowercase    )

       self.assertIsInstance(_lowercase     ,    _lowercase    )

       for audio in decoded_audios:
            self.assertIsInstance(_lowercase     ,    np.ndarray    )

       self.assertTrue(decoded_audios[0].shape == (1, 10)    )
       self.assertTrue(decoded_audios[1].shape == (1, 15)    )
       self.assertTrue(decoded_audios[2].shape == (1, 20)    )







import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig
from ..clip.configuration_clip import CLIPVisionConfig


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





class   lowercase__     (				__A	):
  __UpperCamelCase						  =	"""vision-text-dual-encoder"""
  __UpperCamelCase						  =	True







  def __init__(						self     ,    _lowercase=512     ,    _lowercase=2.6592     ,    **_lowercase    ):
       super().__init__(**_lowercase    )

       if "vision_config" not in kwargs:
            raise ValueError("""`vision_config` can not be `None`."""    )

       if "text_config" not in kwargs:
            raise ValueError("""`text_config` can not be `None`."""    )

       lowerCAmelCase_       :							Dict			 =		kwargs.pop("""vision_config"""    )
       lowerCAmelCase_       :							Optional[int]			 =		kwargs.pop("""text_config"""    )

       lowerCAmelCase_       :							Tuple			 =		vision_config.pop("""model_type"""    )
       lowerCAmelCase_       :							str			 =		text_config.pop("""model_type"""    )

       if vision_model_type == "clip":
            lowerCAmelCase_       :							Dict			 =		AutoConfig.for_model(_lowercase     ,    **_lowercase    ).vision_config
       elif vision_model_type == "clip_vision_model":
            lowerCAmelCase_       :							Any			 =		CLIPVisionConfig(**_lowercase    )
       else:
            lowerCAmelCase_       :							Tuple			 =		AutoConfig.for_model(_lowercase     ,    **_lowercase    )

       lowerCAmelCase_       :							List[Any]			 =		AutoConfig.for_model(_lowercase     ,    **_lowercase    )

       lowerCAmelCase_       :							Union[str, Any]			 =		projection_dim
       lowerCAmelCase_       :							Optional[int]			 =		logit_scale_init_value







  @classmethod
  def 						UpperCAmelCase__		(						cls     ,    _lowercase     ,    _lowercase     ,    **_lowercase    ):
       return cls(vision_config=vision_config.to_dict()     ,    text_config=text_config.to_dict()     ,    **_lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							int			 =		copy.deepcopy(self.__dict__    )
       lowerCAmelCase_       :							List[str]			 =		self.vision_config.to_dict()
       lowerCAmelCase_       :							List[str]			 =		self.text_config.to_dict()
       lowerCAmelCase_       :							str			 =		self.__class__.model_type
       return output
