

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import EfficientNetImageProcessor



class 					__snake_case     (			unittest.TestCase				):

  def __init__(    self  ,						_A  ,						_A=13  ,						_A=3  ,						_A=18  ,						_A=30  ,						_A=400  ,						_A=True  ,						_A=None  ,						_A=True  ,						_A=[0.5, 0.5, 0.5]  ,						_A=[0.5, 0.5, 0.5]  ,						):
      SCREAMING_SNAKE_CASE_													=  size if size is not None else {'height': 18, 'width': 18}
      SCREAMING_SNAKE_CASE_													=  parent
      SCREAMING_SNAKE_CASE_													=  batch_size
      SCREAMING_SNAKE_CASE_													=  num_channels
      SCREAMING_SNAKE_CASE_													=  image_size
      SCREAMING_SNAKE_CASE_													=  min_resolution
      SCREAMING_SNAKE_CASE_													=  max_resolution
      SCREAMING_SNAKE_CASE_													=  do_resize
      SCREAMING_SNAKE_CASE_													=  size
      SCREAMING_SNAKE_CASE_													=  do_normalize
      SCREAMING_SNAKE_CASE_													=  image_mean
      SCREAMING_SNAKE_CASE_													=  image_std


  def   lowerCAmelCase__   (    self):
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
      }






@require_torch
@require_vision
class 					__snake_case     (			lowerCAmelCase__   ,       unittest.TestCase				):
  __lowerCAmelCase   :		Tuple         =     EfficientNetImageProcessor if is_vision_available() else None

  def   lowerCAmelCase__   (    self):
      SCREAMING_SNAKE_CASE_													=  EfficientNetImageProcessorTester(self)

  @property
  def   lowerCAmelCase__   (    self):
      return self.image_processor_tester.prepare_image_processor_dict()

  def   lowerCAmelCase__   (    self):
      SCREAMING_SNAKE_CASE_													=  self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(_A  ,						'image_mean'))
      self.assertTrue(hasattr(_A  ,						'image_std'))
      self.assertTrue(hasattr(_A  ,						'do_normalize'))
      self.assertTrue(hasattr(_A  ,						'do_resize'))
      self.assertTrue(hasattr(_A  ,						'size'))

  def   lowerCAmelCase__   (    self):
      SCREAMING_SNAKE_CASE_													=  self.image_processing_class.from_dict(self.image_processor_dict)
      self.assertEqual(image_processor.size  ,						{'height': 18, 'width': 18})

      SCREAMING_SNAKE_CASE_													=  self.image_processing_class.from_dict(self.image_processor_dict  ,						size=42)
      self.assertEqual(image_processor.size  ,						{'height': 42, 'width': 42})

  def   lowerCAmelCase__   (    self):
      # Initialize image_processing
      SCREAMING_SNAKE_CASE_													=  self.image_processing_class(**self.image_processor_dict)
      # create random PIL images
      SCREAMING_SNAKE_CASE_													=  prepare_image_inputs(self.image_processor_tester  ,						equal_resolution=_A)
      for image in image_inputs:
          self.assertIsInstance(_A  ,						Image.Image)

      # Test not batched input
      SCREAMING_SNAKE_CASE_													=  image_processing(image_inputs[0]  ,						return_tensors='pt').pixel_values
      self.assertEqual(
          encoded_images.shape  ,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )  ,						)

      # Test batched
      SCREAMING_SNAKE_CASE_													=  image_processing(_A  ,						return_tensors='pt').pixel_values
      self.assertEqual(
          encoded_images.shape  ,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )  ,						)

  def   lowerCAmelCase__   (    self):
      # Initialize image_processing
      SCREAMING_SNAKE_CASE_													=  self.image_processing_class(**self.image_processor_dict)
      # create random numpy tensors
      SCREAMING_SNAKE_CASE_													=  prepare_image_inputs(self.image_processor_tester  ,						equal_resolution=_A  ,						numpify=_A)
      for image in image_inputs:
          self.assertIsInstance(_A  ,						np.ndarray)

      # Test not batched input
      SCREAMING_SNAKE_CASE_													=  image_processing(image_inputs[0]  ,						return_tensors='pt').pixel_values
      self.assertEqual(
          encoded_images.shape  ,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )  ,						)

      # Test batched
      SCREAMING_SNAKE_CASE_													=  image_processing(_A  ,						return_tensors='pt').pixel_values
      self.assertEqual(
          encoded_images.shape  ,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )  ,						)


  def   lowerCAmelCase__   (    self):
      # Initialize image_processing
      SCREAMING_SNAKE_CASE_													=  self.image_processing_class(**self.image_processor_dict)
      # create random PyTorch tensors
      SCREAMING_SNAKE_CASE_													=  prepare_image_inputs(self.image_processor_tester  ,						equal_resolution=_A  ,						torchify=_A)
      for image in image_inputs:
          self.assertIsInstance(_A  ,						torch.Tensor)

      # Test not batched input
      SCREAMING_SNAKE_CASE_													=  image_processing(image_inputs[0]  ,						return_tensors='pt').pixel_values
      self.assertEqual(
          encoded_images.shape  ,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )  ,						)

      # Test batched
      SCREAMING_SNAKE_CASE_													=  image_processing(_A  ,						return_tensors='pt').pixel_values
      self.assertEqual(
          encoded_images.shape  ,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )  ,						)





import copy

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


UpperCamelCase__     :					Optional[Any]								=			logging.get_logger(__name__)

UpperCamelCase__     :					List[Any]								=			{
    "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 					__snake_case     (			lowerCAmelCase__				):
  __lowerCAmelCase   :		Any         =     'musicgen_decoder'
  __lowerCAmelCase   :		Optional[Any]         =     ['past_key_values']

  def __init__(    self  ,						_A=2048  ,						_A=2048  ,						_A=24  ,						_A=4096  ,						_A=16  ,						_A=0.0  ,						_A=True  ,						_A="gelu"  ,						_A=1024  ,						_A=0.1  ,						_A=0.0  ,						_A=0.0  ,						_A=0.0_2  ,						_A=False  ,						_A=4  ,						_A=2048  ,						_A=2048  ,						_A=None  ,						_A=False  ,						**_A  ,						):
      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=_A  ,						bos_token_id=_A  ,						eos_token_id=_A  ,						tie_word_embeddings=_A  ,						**_A  ,						)






class 					__snake_case     (			lowerCAmelCase__				):
  __lowerCAmelCase   :		int         =     'musicgen'
  __lowerCAmelCase   :		Optional[Any]         =     True

  def __init__(    self  ,						**_A):
      super().__init__(**_A)
      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(_A  ,						**_A)
      SCREAMING_SNAKE_CASE_													=  AutoConfig.for_model(_A  ,						**_A)
      SCREAMING_SNAKE_CASE_													=  MusicgenDecoderConfig(**_A)
      SCREAMING_SNAKE_CASE_													=  True

  @classmethod
  def   lowerCAmelCase__   (    cls  ,						_A  ,						_A  ,						_A  ,						**_A  ,						):
      return cls(
          text_encoder=text_encoder_config.to_dict()  ,						audio_encoder=audio_encoder_config.to_dict()  ,						decoder=decoder_config.to_dict()  ,						**_A  ,						)


  def   lowerCAmelCase__   (    self):
      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


