code: 
  
  
  
  
  import unittest
  
  import numpy as np
  import torch
  from torch import nn
  from transformers import (
      CLIPImageProcessor,
      CLIPTextConfig,
      CLIPTextModelWithProjection,
      CLIPTokenizer,
      CLIPVisionConfig,
      CLIPVisionModelWithProjection,
  )
  
  from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
  from diffusers.utils import torch_device
  from diffusers.utils.testing_utils import enable_full_determinism, skip_mps
  
  from ..test_pipelines_common import PipelineTesterMixin
  
  
  enable_full_determinism()
  
  
  
  
  
  class 						lowerCamelCase			(					__lowerCamelCase   ,		unittest.TestCase				):
  	UpperCamelCase_     :				int								=  KandinskyPriorPipeline
  	UpperCamelCase_     :				Any								=  ['prompt']
  	UpperCamelCase_     :				List[Any]								=  ['prompt', 'negative_prompt']
  	UpperCamelCase_     :				List[str]								=  [
  	    'num_images_per_prompt',
  	    'generator',
  	    'num_inference_steps',
  	    'latents',
  	    'negative_prompt',
  	    'guidance_scale',
  	    'output_type',
  	    'return_dict',
  	]
  	UpperCamelCase_     :				Any								=  False
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:Tuple )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return 3_2
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:Optional[int] )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return 3_2
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:List[str] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return self.time_input_dim
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:Optional[Any] )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return self.time_input_dim * 4
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:Optional[int] )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return 1_0_0
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:Tuple )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )
  				return tokenizer
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:str )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				torch.manual_seed(0 )
  				SCREAMING_SNAKE_CASE    	=   CLIPTextConfig(
  				    bos_token_id=0      ,    eos_token_id=2      ,    hidden_size=self.text_embedder_hidden_size      ,    projection_dim=self.text_embedder_hidden_size      ,    intermediate_size=3_7      ,    layer_norm_eps=1e-05      ,    num_attention_heads=4      ,    num_hidden_layers=5      ,    pad_token_id=1      ,    vocab_size=1_0_0_0      ,    )
  				return CLIPTextModelWithProjection(lowercase )
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:str )		->			List[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				torch.manual_seed(0 )
  
  				SCREAMING_SNAKE_CASE    	=   {
  				    '''num_attention_heads''': 2,
  				    '''attention_head_dim''': 1_2,
  				    '''embedding_dim''': self.text_embedder_hidden_size,
  				    '''num_layers''': 1,
  				}
  
  				SCREAMING_SNAKE_CASE    	=   PriorTransformer(**lowercase )
  				# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
  				SCREAMING_SNAKE_CASE    	=   nn.Parameter(torch.ones(model.clip_std.shape ) )
  				return model
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:List[Any] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				torch.manual_seed(0 )
  				SCREAMING_SNAKE_CASE    	=   CLIPVisionConfig(
  				    hidden_size=self.text_embedder_hidden_size      ,    image_size=2_2_4      ,    projection_dim=self.text_embedder_hidden_size      ,    intermediate_size=3_7      ,    num_attention_heads=4      ,    num_channels=3      ,    num_hidden_layers=5      ,    patch_size=1_4      ,    )
  
  				SCREAMING_SNAKE_CASE    	=   CLIPVisionModelWithProjection(lowercase )
  				return model
  
  
  
  
  
  
  
  	@property
  	def    snake_case__    (    self			:int )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   CLIPImageProcessor(
  				    crop_size=2_2_4      ,    do_center_crop=lowercase      ,    do_normalize=lowercase      ,    do_resize=lowercase      ,    image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]      ,    image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]      ,    resample=3      ,    size=2_2_4      ,    )
  
  				return image_processor
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[str] )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.dummy_prior
  				SCREAMING_SNAKE_CASE    	=   self.dummy_image_encoder
  				SCREAMING_SNAKE_CASE    	=   self.dummy_text_encoder
  				SCREAMING_SNAKE_CASE    	=   self.dummy_tokenizer
  				SCREAMING_SNAKE_CASE    	=   self.dummy_image_processor
  
  				SCREAMING_SNAKE_CASE    	=   UnCLIPScheduler(
  				    variance_type='''fixed_small_log'''      ,    prediction_type='''sample'''      ,    num_train_timesteps=1_0_0_0      ,    clip_sample=lowercase      ,    clip_sample_range=10.0      ,    )
  
  				SCREAMING_SNAKE_CASE    	=   {
  				    '''prior''': prior,
  				    '''image_encoder''': image_encoder,
  				    '''text_encoder''': text_encoder,
  				    '''tokenizer''': tokenizer,
  				    '''scheduler''': scheduler,
  				    '''image_processor''': image_processor,
  				}
  
  				return components
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:str      ,    lowercase			:int      ,    lowercase			:Optional[int]=0 )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				if str(lowercase ).startswith('''mps''' ):
  							SCREAMING_SNAKE_CASE    	=   torch.manual_seed(lowercase )
  				else:
  							SCREAMING_SNAKE_CASE    	=   torch.Generator(device=lowercase ).manual_seed(lowercase )
  				SCREAMING_SNAKE_CASE    	=   {
  				    '''prompt''': '''horse''',
  				    '''generator''': generator,
  				    '''guidance_scale''': 4.0,
  				    '''num_inference_steps''': 2,
  				    '''output_type''': '''np''',
  				}
  				return inputs
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Union[str, Any] )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   '''cpu'''
  
  				SCREAMING_SNAKE_CASE    	=   self.get_dummy_components()
  
  				SCREAMING_SNAKE_CASE    	=   self.pipeline_class(**lowercase )
  				SCREAMING_SNAKE_CASE    	=   pipe.to(lowercase )
  
  				pipe.set_progress_bar_config(disable=lowercase )
  
  				SCREAMING_SNAKE_CASE    	=   pipe(**self.get_dummy_inputs(lowercase ) )
  				SCREAMING_SNAKE_CASE    	=   output.image_embeds
  
  				SCREAMING_SNAKE_CASE    	=   pipe(
  				    **self.get_dummy_inputs(lowercase )      ,    return_dict=lowercase      ,    )[0]
  
  				SCREAMING_SNAKE_CASE    	=   image[0, -1_0:]
  				SCREAMING_SNAKE_CASE    	=   image_from_tuple[0, -1_0:]
  
  				assert image.shape == (1, 3_2)
  
  				SCREAMING_SNAKE_CASE    	=   np.array(
  				    [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56] )
  
  				assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
  				assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2
  
  
  
  
  
  
  
  	@skip_mps
  	def    snake_case__    (    self			:Tuple )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   torch_device == '''cpu'''
  				SCREAMING_SNAKE_CASE    	=   True
  				SCREAMING_SNAKE_CASE    	=   False
  
  				self._test_inference_batch_single_identical(
  				    test_max_difference=lowercase      ,    relax_max_difference=lowercase      ,    test_mean_pixel_difference=lowercase      ,    )
  
  
  
  
  
  
  
  	@skip_mps
  	def    snake_case__    (    self			:Optional[Any] )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   torch_device == '''cpu'''
  				SCREAMING_SNAKE_CASE    	=   False
  
  				self._test_attention_slicing_forward_pass(
  				    test_max_difference=lowercase      ,    test_mean_pixel_difference=lowercase      ,    )
style_context: 
  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			(					__lowerCamelCase				):
  	UpperCamelCase_     :				int								=  'musicgen_decoder'
  	UpperCamelCase_     :				Optional[int]								=  ['past_key_values']
  
  
  
  
  
  
  
  	def __init__(    self			:List[str]      ,    lowercase			:Tuple=2_0_4_8      ,    lowercase			:Dict=2_0_4_8      ,    lowercase			:Tuple=2_4      ,    lowercase			:Any=4_0_9_6      ,    lowercase			:Optional[Any]=1_6      ,    lowercase			:List[Any]=0.0      ,    lowercase			:Union[str, Any]=True      ,    lowercase			:Optional[Any]="gelu"      ,    lowercase			:Any=1_0_2_4      ,    lowercase			:str=0.1      ,    lowercase			:List[Any]=0.0      ,    lowercase			:List[Any]=0.0      ,    lowercase			:List[Any]=0.02      ,    lowercase			:Optional[int]=False      ,    lowercase			:List[Any]=4      ,    lowercase			:Any=2_0_4_8      ,    lowercase			:Any=2_0_4_8      ,    lowercase			:Optional[int]=None      ,    lowercase			:Tuple=False      ,    **lowercase			:List[Any]      ,    )		->			Tuple:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   vocab_size
  				SCREAMING_SNAKE_CASE    	=   max_position_embeddings
  				SCREAMING_SNAKE_CASE    	=   hidden_size
  				SCREAMING_SNAKE_CASE    	=   ffn_dim
  				SCREAMING_SNAKE_CASE    	=   num_hidden_layers
  				SCREAMING_SNAKE_CASE    	=   num_attention_heads
  				SCREAMING_SNAKE_CASE    	=   dropout
  				SCREAMING_SNAKE_CASE    	=   attention_dropout
  				SCREAMING_SNAKE_CASE    	=   activation_dropout
  				SCREAMING_SNAKE_CASE    	=   activation_function
  				SCREAMING_SNAKE_CASE    	=   initializer_factor
  				SCREAMING_SNAKE_CASE    	=   layerdrop
  				SCREAMING_SNAKE_CASE    	=   use_cache
  				SCREAMING_SNAKE_CASE    	=   scale_embedding  # scale factor will be sqrt(d_model) if True
  				SCREAMING_SNAKE_CASE    	=   num_codebooks
  				super().__init__(
  				    pad_token_id=lowercase      ,    bos_token_id=lowercase      ,    eos_token_id=lowercase      ,    tie_word_embeddings=lowercase      ,    **lowercase      ,    )
  
  
  
  
  
  class 						lowerCamelCase			(					__lowerCamelCase				):
  	UpperCamelCase_     :				str								=  'musicgen'
  	UpperCamelCase_     :				Union[str, Any]								=  True
  
  
  
  
  
  
  
  	def __init__(    self			:str      ,    **lowercase			:int )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__(**lowercase )
  				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''' )
  
  				SCREAMING_SNAKE_CASE    	=   kwargs.pop('''text_encoder''' )
  				SCREAMING_SNAKE_CASE    	=   text_encoder_config.pop('''model_type''' )
  
  				SCREAMING_SNAKE_CASE    	=   kwargs.pop('''audio_encoder''' )
  				SCREAMING_SNAKE_CASE    	=   audio_encoder_config.pop('''model_type''' )
  
  				SCREAMING_SNAKE_CASE    	=   kwargs.pop('''decoder''' )
  
  				SCREAMING_SNAKE_CASE    	=   AutoConfig.for_model(lowercase      ,    **lowercase )
  				SCREAMING_SNAKE_CASE    	=   AutoConfig.for_model(lowercase      ,    **lowercase )
  				SCREAMING_SNAKE_CASE    	=   MusicgenDecoderConfig(**lowercase )
  				SCREAMING_SNAKE_CASE    	=   True
  
  
  
  
  
  
  
  	@classmethod
  	def    snake_case__    (    cls			:Tuple      ,    lowercase			:PretrainedConfig      ,    lowercase			:PretrainedConfig      ,    lowercase			:MusicgenDecoderConfig      ,    **lowercase			:Tuple      ,    )		->			Tuple:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return cls(
  				    text_encoder=text_encoder_config.to_dict()      ,    audio_encoder=audio_encoder_config.to_dict()      ,    decoder=decoder_config.to_dict()      ,    **lowercase      ,    )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Dict )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   copy.deepcopy(self.__dict__ )
  				SCREAMING_SNAKE_CASE    	=   self.text_encoder.to_dict()
  				SCREAMING_SNAKE_CASE    	=   self.audio_encoder.to_dict()
  				SCREAMING_SNAKE_CASE    	=   self.decoder.to_dict()
  				SCREAMING_SNAKE_CASE    	=   self.__class__.model_type
  				return output