


'''simple docstring'''

import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


lowerCamelCase  						=     logging.get_logger(__name__)

lowerCamelCase  						=     {
    """facebook/musicgen-small""": """https://huggingface.co/facebook/musicgen-small/resolve/main/config.json""",
    # See all Musicgen models at https://huggingface.co/models?filter=musicgen
}
class   __lowerCAmelCase						(snake_case__					):





      '''simple docstring'''






      lowerCAmelCase__ :					Optional[int]   =							"musicgen_decoder"
      lowerCAmelCase__ :					Optional[Any]   =							["past_key_values"]



      def __init__(self   :   int       ,						UpperCamelCase   :   Union[str, Any]=2048       ,						UpperCamelCase   :   Optional[Any]=2048       ,						UpperCamelCase   :   Any=24       ,						UpperCamelCase   :   List[str]=4096       ,						UpperCamelCase   :   Optional[int]=16       ,						UpperCamelCase   :   Optional[Any]=0.0       ,						UpperCamelCase   :   List[str]=True       ,						UpperCamelCase   :   Any="gelu"       ,						UpperCamelCase   :   Optional[Any]=1024       ,						UpperCamelCase   :   Optional[Any]=0.1       ,						UpperCamelCase   :   Tuple=0.0       ,						UpperCamelCase   :   List[Any]=0.0       ,						UpperCamelCase   :   Dict=0.02       ,						UpperCamelCase   :   int=False       ,						UpperCamelCase   :   Tuple=4       ,						UpperCamelCase   :   Tuple=2048       ,						UpperCamelCase   :   Optional[int]=2048       ,						UpperCamelCase   :   List[str]=None       ,						UpperCamelCase   :   List[Any]=False       ,						**UpperCamelCase   :   Dict       ,						):

             '''simple docstring'''







             lowercase__				      =			vocab_size
             lowercase__				      =			max_position_embeddings
             lowercase__				      =			hidden_size
             lowercase__				      =			ffn_dim
             lowercase__				      =			num_hidden_layers
             lowercase__				      =			num_attention_heads
             lowercase__				      =			dropout
             lowercase__				      =			attention_dropout
             lowercase__				      =			activation_dropout
             lowercase__				      =			activation_function
             lowercase__				      =			initializer_factor
             lowercase__				      =			layerdrop
             lowercase__				      =			use_cache
             lowercase__				      =			scale_embedding  # scale factor will be sqrt(d_model) if True
             lowercase__				      =			num_codebooks
             super().__init__(
                 pad_token_id=UpperCamelCase       ,						bos_token_id=UpperCamelCase       ,						eos_token_id=UpperCamelCase       ,						tie_word_embeddings=UpperCamelCase       ,						**UpperCamelCase       ,						)






class   __lowerCAmelCase						(snake_case__					):





      '''simple docstring'''






      lowerCAmelCase__ :					Tuple   =							"musicgen"
      lowerCAmelCase__ :					List[str]   =							True



      def __init__(self   :   Tuple       ,						**UpperCamelCase   :   str			):

             '''simple docstring'''







             super().__init__(**UpperCamelCase			)
             if "text_encoder" not in kwargs or "audio_encoder" not in kwargs or "decoder" not in kwargs:
                    raise ValueError('''Config has to be initialized with text_encoder, audio_encoder and decoder config'''			)

             lowercase__				      =			kwargs.pop('''text_encoder'''			)
             lowercase__				      =			text_encoder_config.pop('''model_type'''			)

             lowercase__				      =			kwargs.pop('''audio_encoder'''			)
             lowercase__				      =			audio_encoder_config.pop('''model_type'''			)

             lowercase__				      =			kwargs.pop('''decoder'''			)

             lowercase__				      =			AutoConfig.for_model(UpperCamelCase       ,						**UpperCamelCase			)
             lowercase__				      =			AutoConfig.for_model(UpperCamelCase       ,						**UpperCamelCase			)
             lowercase__				      =			MusicgenDecoderConfig(**UpperCamelCase			)
             lowercase__				      =			True



      @classmethod
      def 				UpperCamelCase__		(cls   :   int       ,						UpperCamelCase   :   PretrainedConfig       ,						UpperCamelCase   :   PretrainedConfig       ,						UpperCamelCase   :   MusicgenDecoderConfig       ,						**UpperCamelCase   :   List[str]       ,						):

             '''simple docstring'''







             return cls(
                 text_encoder=text_encoder_config.to_dict()       ,						audio_encoder=audio_encoder_config.to_dict()       ,						decoder=decoder_config.to_dict()       ,						**UpperCamelCase       ,						)






      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             lowercase__				      =			copy.deepcopy(self.__dict__			)
             lowercase__				      =			self.text_encoder.to_dict()
             lowercase__				      =			self.audio_encoder.to_dict()
             lowercase__				      =			self.decoder.to_dict()
             lowercase__				      =			self.__class__.model_type
             return output








'''simple docstring'''

import random
import unittest

import torch

from diffusers import IFImgaImgPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class   __lowerCAmelCase						(lowercase_  ,					lowercase_  ,					unittest.TestCase					):





      '''simple docstring'''






      lowerCAmelCase__ :					Dict   =							IFImgaImgPipeline
      lowerCAmelCase__ :					int   =							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
      lowerCAmelCase__ :					Optional[int]   =							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
      lowerCAmelCase__ :					str   =							PipelineTesterMixin.required_optional_params - {"""latents"""}



      def 				UpperCamelCase__		(self   :   Dict			):

             '''simple docstring'''







             return self._get_dummy_components()



      def 				UpperCamelCase__		(self   :   Optional[Any]       ,						UpperCamelCase   :   List[Any]       ,						UpperCamelCase   :   Optional[Any]=0			):

             '''simple docstring'''







             if str(UpperCamelCase			).startswith('''mps'''			):
                    lowercase__				      =			torch.manual_seed(UpperCamelCase			)
             else:
                    lowercase__				      =			torch.Generator(device=UpperCamelCase			).manual_seed(UpperCamelCase			)

             lowercase__				      =			floats_tensor((1, 3, 32, 32)       ,						rng=random.Random(UpperCamelCase			)			).to(UpperCamelCase			)

             lowercase__				      =			{
                 '''prompt''': '''A painting of a squirrel eating a burger''',
                 '''image''': image,
                 '''generator''': generator,
                 '''num_inference_steps''': 2,
                 '''output_type''': '''numpy''',
             }

             return inputs



      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             self._test_save_load_optional_components()



      @unittest.skipIf(
          torch_device != '''cuda''' or not is_xformers_available()       ,						reason='''XFormers attention is only available with CUDA and `xformers` installed'''       ,						)
      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3			)



      @unittest.skipIf(torch_device != '''cuda'''       ,						reason='''float16 requires CUDA'''			)
      def 				UpperCamelCase__		(self   :   Any			):

             '''simple docstring'''







             super().test_save_load_floataa(expected_max_diff=1E-1			)



      @unittest.skipIf(torch_device != '''cuda'''       ,						reason='''float16 requires CUDA'''			)
      def 				UpperCamelCase__		(self   :   Dict			):

             '''simple docstring'''







             super().test_floataa_inference(expected_max_diff=1E-1			)



      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             self._test_attention_slicing_forward_pass(expected_max_diff=1E-2			)



      def 				UpperCamelCase__		(self   :   str			):

             '''simple docstring'''







             self._test_save_load_local()






      def 				UpperCamelCase__		(self   :   List[Any]			):

             '''simple docstring'''







             self._test_inference_batch_single_identical(
                 expected_max_diff=1E-2       ,						)




