






import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch
       from torch import nn

       from transformers import (
           MODEL_MAPPING,
           DataaVecVisionForImageClassification,
           DataaVecVisionForSemanticSegmentation,
           DataaVecVisionModel,
       )
       from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import BeitImageProcessor
class    a   :



      """simple docstring"""


      def __init__(				self :      List[str]		,				__lowercase :      Tuple		,				__lowercase :      Tuple=100		,				__lowercase :      Any=13		,				__lowercase :      Union[str, Any]=30		,				__lowercase :      Any=2		,				__lowercase :      int=3		,				__lowercase :      List[str]=True		,				__lowercase :      str=True		,				__lowercase :      List[str]=32		,				__lowercase :      List[Any]=4		,				__lowercase :      Dict=4		,				__lowercase :      Dict=37		,				__lowercase :      Optional[int]="gelu"		,				__lowercase :      Optional[Any]=0.1		,				__lowercase :      int=0.1		,				__lowercase :      Dict=10		,				__lowercase :      Tuple=0.02		,				__lowercase :      str=3		,				__lowercase :      Optional[int]=None		,				__lowercase :      List[str]=[0, 1, 2, 3]		,				)							->				int:
       __UpperCAmelCase							:     str									=  parent
       __UpperCAmelCase							:     List[str]									=  100
       __UpperCAmelCase							:     int									=  batch_size
       __UpperCAmelCase							:     Optional[Any]									=  image_size
       __UpperCAmelCase							:     int									=  patch_size
       __UpperCAmelCase							:     int									=  num_channels
       __UpperCAmelCase							:     List[Any]									=  is_training
       __UpperCAmelCase							:     List[Any]									=  use_labels
       __UpperCAmelCase							:     Tuple									=  hidden_size
       __UpperCAmelCase							:     str									=  num_hidden_layers
       __UpperCAmelCase							:     Union[str, Any]									=  num_attention_heads
       __UpperCAmelCase							:     List[Any]									=  intermediate_size
       __UpperCAmelCase							:     Union[str, Any]									=  hidden_act
       __UpperCAmelCase							:     Union[str, Any]									=  hidden_dropout_prob
       __UpperCAmelCase							:     List[Any]									=  attention_probs_dropout_prob
       __UpperCAmelCase							:     int									=  type_sequence_label_size
       __UpperCAmelCase							:     int									=  initializer_range
       __UpperCAmelCase							:     str									=  scope
       __UpperCAmelCase							:     Tuple									=  out_indices
       __UpperCAmelCase							:     str									=  num_labels

       # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
       __UpperCAmelCase							:     Tuple									=  (image_size // patch_size) ** 2
       __UpperCAmelCase							:     Union[str, Any]									=  num_patches + 1
      def      UpperCAmelCase				(				self :      int )							->				Tuple:
       __UpperCAmelCase							:     Optional[int]									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

       __UpperCAmelCase							:     str									=  None
       __UpperCAmelCase							:     int									=  None
       if self.use_labels:
        __UpperCAmelCase							:     str									=  ids_tensor([self.batch_size]		,				self.type_sequence_label_size )
        __UpperCAmelCase							:     str									=  ids_tensor([self.batch_size, self.image_size, self.image_size]		,				self.num_labels )

       __UpperCAmelCase							:     Any									=  self.get_config()

       return config, pixel_values, labels, pixel_labels
      def      UpperCAmelCase				(				self :      int )							->				Tuple:
       return DataaVecVisionConfig(
           vocab_size=self.vocab_size		,				image_size=self.image_size		,				patch_size=self.patch_size		,				num_channels=self.num_channels		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				intermediate_size=self.intermediate_size		,				hidden_act=self.hidden_act		,				hidden_dropout_prob=self.hidden_dropout_prob		,				attention_probs_dropout_prob=self.attention_probs_dropout_prob		,				is_decoder=__lowerCAmelCase		,				initializer_range=self.initializer_range		,				out_indices=self.out_indices		,				)
      def      UpperCAmelCase				(				self :      str		,				__lowercase :      Tuple		,				__lowercase :      Optional[int]		,				__lowercase :      Dict		,				__lowercase :      Any )							->				Union[str, Any]:
       __UpperCAmelCase							:     Any									=  DataaVecVisionModel(config=__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       __UpperCAmelCase							:     Tuple									=  model(__lowerCAmelCase )
       # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
       __UpperCAmelCase							:     int									=  (self.image_size // self.patch_size) ** 2
       self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, num_patches + 1, self.hidden_size) )
      def      UpperCAmelCase				(				self :      List[Any]		,				__lowercase :      Tuple		,				__lowercase :      str		,				__lowercase :      List[str]		,				__lowercase :      Optional[Any] )							->				str:
       __UpperCAmelCase							:     Dict									=  self.type_sequence_label_size
       __UpperCAmelCase							:     Optional[int]									=  DataaVecVisionForImageClassification(__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       __UpperCAmelCase							:     str									=  model(__lowerCAmelCase		,				labels=__lowerCAmelCase )
       self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size) )
      def      UpperCAmelCase				(				self :      List[str]		,				__lowercase :      Any		,				__lowercase :      Union[str, Any]		,				__lowercase :      Union[str, Any]		,				__lowercase :      str )							->				Any:
       __UpperCAmelCase							:     List[str]									=  self.num_labels
       __UpperCAmelCase							:     Union[str, Any]									=  DataaVecVisionForSemanticSegmentation(__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       __UpperCAmelCase							:     int									=  model(__lowerCAmelCase )
       self.parent.assertEqual(
           result.logits.shape		,				(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) )
       __UpperCAmelCase							:     int									=  model(__lowerCAmelCase		,				labels=__lowerCAmelCase )
       self.parent.assertEqual(
           result.logits.shape		,				(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) )




      def      UpperCAmelCase				(				self :      Any )							->				List[str]:
       __UpperCAmelCase							:     str									=  self.prepare_config_and_inputs()
       __UpperCAmelCase				,			__UpperCAmelCase				,			__UpperCAmelCase				,			__UpperCAmelCase							:     Union[str, Any]									=  config_and_inputs
       __UpperCAmelCase							:     Tuple									=  {"""pixel_values""": pixel_values}
       return config, inputs_dict


@require_torch
class    a   (			lowerCAmelCase__							,	lowerCAmelCase__							,	unittest.TestCase       ):



      """simple docstring"""


      a  :			Optional[int]								=     (
          (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      a  :			Dict								=     (
          {
              '''feature-extraction''': DataaVecVisionModel,
              '''image-classification''': DataaVecVisionForImageClassification,
              '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      a  :			List[Any]								=     False
      a  :			Optional[int]								=     False
      a  :			str								=     False
      def      UpperCAmelCase				(				self :      Optional[int] )							->				int:
       __UpperCAmelCase							:     str									=  DataaVecVisionModelTester(self )
       __UpperCAmelCase							:     List[Any]									=  ConfigTester(
           self		,				config_class=__lowerCAmelCase		,				has_text_modality=__lowerCAmelCase		,				hidden_size=37 )
      def      UpperCAmelCase				(				self :      int )							->				List[str]:
       self.config_tester.run_common_tests()
      def      UpperCAmelCase				(				self :      str )							->				Optional[int]:
       pass
      @require_torch_multi_gpu
      @unittest.skip(
          reason="""Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" )
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Tuple:
       pass
      def      UpperCAmelCase				(				self :      List[str] )							->				Union[str, Any]:
       __UpperCAmelCase				,			__UpperCAmelCase							:     List[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __UpperCAmelCase							:     int									=  model_class(__lowerCAmelCase )
        self.assertIsInstance(model.get_input_embeddings()		,				(nn.Module) )
        __UpperCAmelCase							:     List[Any]									=  model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__lowerCAmelCase		,				nn.Linear ) )
      def      UpperCAmelCase				(				self :      Tuple )							->				List[Any]:
       __UpperCAmelCase				,			__UpperCAmelCase							:     Any									=  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __UpperCAmelCase							:     Optional[int]									=  model_class(__lowerCAmelCase )
        __UpperCAmelCase							:     List[str]									=  inspect.signature(model.forward )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        __UpperCAmelCase							:     Optional[Any]									=  [*signature.parameters.keys()]

        __UpperCAmelCase							:     List[Any]									=  ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]		,				__lowerCAmelCase )
      def      UpperCAmelCase				(				self :      Optional[Any] )							->				Optional[int]:
       __UpperCAmelCase							:     Any									=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCAmelCase )
      def      UpperCAmelCase				(				self :      Tuple )							->				Any:
       __UpperCAmelCase							:     Optional[int]									=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_segmentation(*__lowerCAmelCase )
      def      UpperCAmelCase				(				self :      Optional[Any] )							->				Dict:


       if not self.model_tester.is_training:
        return

       __UpperCAmelCase				,			__UpperCAmelCase							:     Tuple									=  self.model_tester.prepare_config_and_inputs_for_common()
       __UpperCAmelCase							:     Any									=  True

       for model_class in self.all_model_classes:
        if model_class in [*get_values(__lowerCAmelCase )]:
         continue

        __UpperCAmelCase							:     Optional[int]									=  model_class(__lowerCAmelCase )
        model.to(__lowerCAmelCase )
        model.train()
        __UpperCAmelCase							:     Union[str, Any]									=  self._prepare_for_class(__lowerCAmelCase		,				__lowerCAmelCase		,				return_labels=__lowerCAmelCase )
        __UpperCAmelCase							:     List[Any]									=  model(**__lowerCAmelCase ).loss
        loss.backward()
      def      UpperCAmelCase				(				self :      List[Any] )							->				int:
       __UpperCAmelCase				,			__UpperCAmelCase							:     Tuple									=  self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
        return

       __UpperCAmelCase							:     Union[str, Any]									=  False
       __UpperCAmelCase							:     Dict									=  True

       for model_class in self.all_model_classes:
        if model_class in [*get_values(__lowerCAmelCase )] or not model_class.supports_gradient_checkpointing:
         continue
        # TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
        # this can then be incorporated into _prepare_for_class in test_modeling_common.py
        elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
         __UpperCAmelCase				,			__UpperCAmelCase				,			__UpperCAmelCase				,			__UpperCAmelCase							:     str									=  inputs_dict["""pixel_values"""].shape
         __UpperCAmelCase							:     Optional[int]									=  torch.zeros(
             [self.model_tester.batch_size, height, width]		,				device=__lowerCAmelCase ).long()
        __UpperCAmelCase							:     Optional[int]									=  model_class(__lowerCAmelCase )
        model.gradient_checkpointing_enable()
        model.to(__lowerCAmelCase )
        model.train()
        __UpperCAmelCase							:     List[str]									=  self._prepare_for_class(__lowerCAmelCase		,				__lowerCAmelCase		,				return_labels=__lowerCAmelCase )
        __UpperCAmelCase							:     Dict									=  model(**__lowerCAmelCase ).loss
        loss.backward()
      def      UpperCAmelCase				(				self :      Dict )							->				Any:
       __UpperCAmelCase				,			__UpperCAmelCase							:     Dict									=  self.model_tester.prepare_config_and_inputs_for_common()

       __UpperCAmelCase							:     Union[str, Any]									=  _config_zero_init(__lowerCAmelCase )
       for model_class in self.all_model_classes:
        __UpperCAmelCase							:     Tuple									=  model_class(config=__lowerCAmelCase )
        for name, param in model.named_parameters():
         # we skip lambda parameters as these require special initial values
         # determined by config.layer_scale_init_value
         if "lambda" in name:
          continue
         if param.requires_grad:
          self.assertIn(
              ((param.data.mean() * 1e9).round() / 1e9).item()		,				[0.0, 1.0]		,				msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""		,				)
      def      UpperCAmelCase				(				self :      str		,				__lowercase :      Optional[Any]		,				__lowercase :      str		,				__lowercase :      List[str]		,				__lowercase :      List[str]=2e-4		,				__lowercase :      int="outputs"		,				__lowercase :      Any=None )							->				Tuple:
       super().check_pt_tf_outputs(__lowerCAmelCase		,				__lowerCAmelCase		,				__lowerCAmelCase		,				__lowerCAmelCase		,				__lowerCAmelCase		,				__lowerCAmelCase )
      def      UpperCAmelCase				(				self :      str )							->				Optional[Any]:
       __UpperCAmelCase							:     Union[str, Any]									=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase )




      @slow
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Any:


       for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __UpperCAmelCase							:     int									=  DataaVecVisionModel.from_pretrained(__lowerCAmelCase )
        self.assertIsNotNone(__lowerCAmelCase )






def     lowerCamelCase__				(   ):
 __UpperCAmelCase							:     List[str]									=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
 return image







@require_torch
@require_vision
class    a   (			unittest.TestCase       ):



      """simple docstring"""


      @cached_property
      def      UpperCAmelCase				(				self :      str )							->				List[str]:
       return (
           BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k""" ) if is_vision_available() else None
       )




      @slow
      def      UpperCAmelCase				(				self :      Tuple )							->				Tuple:
       __UpperCAmelCase							:     int									=  DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k""" ).to(
           __lowerCAmelCase )

       __UpperCAmelCase							:     Any									=  self.default_image_processor
       __UpperCAmelCase							:     Tuple									=  prepare_img()
       __UpperCAmelCase							:     Any									=  image_processor(images=__lowerCAmelCase		,				return_tensors="""pt""" ).to(__lowerCAmelCase )

       # forward pass
       with torch.no_grad():
        __UpperCAmelCase							:     List[Any]									=  model(**__lowerCAmelCase )
       __UpperCAmelCase							:     Tuple									=  outputs.logits

       # verify the logits
       __UpperCAmelCase							:     Optional[Any]									=  torch.Size((1, 1000) )
       self.assertEqual(logits.shape		,				__lowerCAmelCase )

       __UpperCAmelCase							:     Dict									=  torch.tensor([0.3_277, -0.1_395, 0.0_911] ).to(__lowerCAmelCase )

       self.assertTrue(torch.allclose(logits[0, :3]		,				__lowerCAmelCase		,				atol=1e-4 ) )

       __UpperCAmelCase							:     int									=  [model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
       self.assertEqual(logits[0].topk(2 ).indices.cpu().tolist()		,				__lowerCAmelCase )










import copy

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


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

a		:	int      					=			{
    "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    a   (			lowercase__       ):



      """simple docstring"""


      a  :			Optional[int]								=     'musicgen_decoder'
      a  :			Optional[int]								=     ['past_key_values']
      def __init__(				self :      Optional[Any]		,				__lowercase :      str=2048		,				__lowercase :      Optional[int]=2048		,				__lowercase :      List[Any]=24		,				__lowercase :      Optional[Any]=4096		,				__lowercase :      Dict=16		,				__lowercase :      List[Any]=0.0		,				__lowercase :      Tuple=True		,				__lowercase :      List[Any]="gelu"		,				__lowercase :      Any=1024		,				__lowercase :      Optional[Any]=0.1		,				__lowercase :      List[Any]=0.0		,				__lowercase :      int=0.0		,				__lowercase :      List[str]=0.02		,				__lowercase :      Dict=False		,				__lowercase :      List[str]=4		,				__lowercase :      List[str]=2048		,				__lowercase :      List[Any]=2048		,				__lowercase :      Union[str, Any]=None		,				__lowercase :      List[str]=False		,				**__lowercase :      int		,				)							->				str:
        __UpperCAmelCase							:     Union[str, Any]									=  vocab_size
        __UpperCAmelCase							:     Dict									=  max_position_embeddings
        __UpperCAmelCase							:     str									=  hidden_size
        __UpperCAmelCase							:     Optional[Any]									=  ffn_dim
        __UpperCAmelCase							:     List[Any]									=  num_hidden_layers
        __UpperCAmelCase							:     Any									=  num_attention_heads
        __UpperCAmelCase							:     List[Any]									=  dropout
        __UpperCAmelCase							:     List[str]									=  attention_dropout
        __UpperCAmelCase							:     List[Any]									=  activation_dropout
        __UpperCAmelCase							:     int									=  activation_function
        __UpperCAmelCase							:     Optional[int]									=  initializer_factor
        __UpperCAmelCase							:     List[Any]									=  layerdrop
        __UpperCAmelCase							:     Tuple									=  use_cache
        __UpperCAmelCase							:     Tuple									=  scale_embedding  # scale factor will be sqrt(d_model) if True
        __UpperCAmelCase							:     int									=  num_codebooks
        super().__init__(
            pad_token_id=__lowercase		,				bos_token_id=__lowercase		,				eos_token_id=__lowercase		,				tie_word_embeddings=__lowercase		,				**__lowercase		,				)


class    a   (			lowercase__       ):



      """simple docstring"""


      a  :			Tuple								=     'musicgen'
      a  :			int								=     True
      def __init__(				self :      Any		,				**__lowercase :      Tuple )							->				Union[str, Any]:
        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""" )

        __UpperCAmelCase							:     int									=  kwargs.pop("""text_encoder""" )
        __UpperCAmelCase							:     str									=  text_encoder_config.pop("""model_type""" )

        __UpperCAmelCase							:     Tuple									=  kwargs.pop("""audio_encoder""" )
        __UpperCAmelCase							:     Optional[Any]									=  audio_encoder_config.pop("""model_type""" )

        __UpperCAmelCase							:     Optional[Any]									=  kwargs.pop("""decoder""" )

        __UpperCAmelCase							:     Any									=  AutoConfig.for_model(__lowercase		,				**__lowercase )
        __UpperCAmelCase							:     Dict									=  AutoConfig.for_model(__lowercase		,				**__lowercase )
        __UpperCAmelCase							:     Any									=  MusicgenDecoderConfig(**__lowercase )
        __UpperCAmelCase							:     int									=  True
      @classmethod
      def      UpperCAmelCase				(				cls :      Optional[Any]		,				__lowercase :      PretrainedConfig		,				__lowercase :      PretrainedConfig		,				__lowercase :      MusicgenDecoderConfig		,				**__lowercase :      List[Any]		,				)							->				List[str]:
        return cls(
            text_encoder=text_encoder_config.to_dict()		,				audio_encoder=audio_encoder_config.to_dict()		,				decoder=decoder_config.to_dict()		,				**__lowercase		,				)




      def      UpperCAmelCase				(				self :      Any )							->				int:
        __UpperCAmelCase							:     Optional[Any]									=  copy.deepcopy(self.__dict__ )
        __UpperCAmelCase							:     List[Any]									=  self.text_encoder.to_dict()
        __UpperCAmelCase							:     str									=  self.audio_encoder.to_dict()
        __UpperCAmelCase							:     List[str]									=  self.decoder.to_dict()
        __UpperCAmelCase							:     List[str]									=  self.__class__.model_type
        return output


