




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  lowerCamelCase_							:


 '''simple docstring'''







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

       # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
       SCREAMING_SNAKE_CASE_					=    (image_size // patch_size) ** 2
       SCREAMING_SNAKE_CASE_					=    num_patches + 1



 def    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       SCREAMING_SNAKE_CASE_					=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

       SCREAMING_SNAKE_CASE_					=    self.get_config()

       return config, pixel_values, labels, pixel_labels



 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       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    lowerCAmelCase_						(							self						:	int       ,       _lowerCAmelCase						:	Any       ,       _lowerCAmelCase						:	Dict       ,       _lowerCAmelCase						:	Optional[int]       ,       _lowerCAmelCase						:	Union[str, Any]		):
       SCREAMING_SNAKE_CASE_					=    DataaVecVisionModel(config=_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()
       SCREAMING_SNAKE_CASE_					=    model(_lowerCAmelCase		)
       # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
       SCREAMING_SNAKE_CASE_					=    (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    lowerCAmelCase_						(							self						:	Dict       ,       _lowerCAmelCase						:	Dict       ,       _lowerCAmelCase						:	Tuple       ,       _lowerCAmelCase						:	Any       ,       _lowerCAmelCase						:	Dict		):
       SCREAMING_SNAKE_CASE_					=    self.type_sequence_label_size
       SCREAMING_SNAKE_CASE_					=    DataaVecVisionForImageClassification(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()
       SCREAMING_SNAKE_CASE_					=    model(_lowerCAmelCase       ,       labels=_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.type_sequence_label_size)		)



 def    lowerCAmelCase_						(							self						:	List[Any]       ,       _lowerCAmelCase						:	Dict       ,       _lowerCAmelCase						:	int       ,       _lowerCAmelCase						:	Optional[Any]       ,       _lowerCAmelCase						:	Dict		):
       SCREAMING_SNAKE_CASE_					=    self.num_labels
       SCREAMING_SNAKE_CASE_					=    DataaVecVisionForSemanticSegmentation(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()
       SCREAMING_SNAKE_CASE_					=    model(_lowerCAmelCase		)
       self.parent.assertEqual(
           result.logits.shape       ,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
       SCREAMING_SNAKE_CASE_					=    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    lowerCAmelCase_						(							self						:	Optional[Any]		):
       SCREAMING_SNAKE_CASE_					=    self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    config_and_inputs
       SCREAMING_SNAKE_CASE_					=    {'pixel_values': pixel_values}
       return config, inputs_dict





@require_torch
class  lowerCamelCase_							(   _SCREAMING_SNAKE_CASE ,     _SCREAMING_SNAKE_CASE ,     unittest.TestCase      ):


 '''simple docstring'''







 lowercase_       					=					(
     (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 lowercase_       					=					(
     {
         "feature-extraction": DataaVecVisionModel,
         "image-classification": DataaVecVisionForImageClassification,
         "image-segmentation": DataaVecVisionForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 lowercase_       					=					False
 lowercase_       					=					False
 lowercase_       					=					False



 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       SCREAMING_SNAKE_CASE_					=    DataaVecVisionModelTester(self		)
       SCREAMING_SNAKE_CASE_					=    ConfigTester(
           self       ,       config_class=_lowerCAmelCase       ,       has_text_modality=_lowerCAmelCase       ,       hidden_size=37		)



 def    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       self.config_tester.run_common_tests()



 def    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       # Data2VecVision does not use inputs_embeds
       pass



 @require_torch_multi_gpu
 @unittest.skip(
     reason='Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'		)
 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       pass



 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE_					=    model_class(_lowerCAmelCase		)
             self.assertIsInstance(model.get_input_embeddings()       ,       (nn.Module)		)
             SCREAMING_SNAKE_CASE_					=    model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_lowerCAmelCase       ,       nn.Linear		)		)



 def    lowerCAmelCase_						(							self						:	Dict		):
       SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE_					=    model_class(_lowerCAmelCase		)
             SCREAMING_SNAKE_CASE_					=    inspect.signature(model.forward		)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             SCREAMING_SNAKE_CASE_					=    [*signature.parameters.keys()]

             SCREAMING_SNAKE_CASE_					=    ['pixel_values']
             self.assertListEqual(arg_names[:1]       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	int		):
       SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_segmentation(*_lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	List[str]		):
       if not self.model_tester.is_training:
             return

       SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE_					=    True

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

             SCREAMING_SNAKE_CASE_					=    model_class(_lowerCAmelCase		)
             model.to(_lowerCAmelCase		)
             model.train()
             SCREAMING_SNAKE_CASE_					=    self._prepare_for_class(_lowerCAmelCase       ,       _lowerCAmelCase       ,       return_labels=_lowerCAmelCase		)
             SCREAMING_SNAKE_CASE_					=    model(**_lowerCAmelCase		).loss
             loss.backward()



 def    lowerCAmelCase_						(							self						:	List[Any]		):
       SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
             return

       SCREAMING_SNAKE_CASE_					=    False
       SCREAMING_SNAKE_CASE_					=    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":
                   SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    inputs_dict['pixel_values'].shape
                   SCREAMING_SNAKE_CASE_					=    torch.zeros(
                       [self.model_tester.batch_size, height, width]       ,       device=_lowerCAmelCase		).long()
             SCREAMING_SNAKE_CASE_					=    model_class(_lowerCAmelCase		)
             model.gradient_checkpointing_enable()
             model.to(_lowerCAmelCase		)
             model.train()
             SCREAMING_SNAKE_CASE_					=    self._prepare_for_class(_lowerCAmelCase       ,       _lowerCAmelCase       ,       return_labels=_lowerCAmelCase		)
             SCREAMING_SNAKE_CASE_					=    model(**_lowerCAmelCase		).loss
             loss.backward()



 def    lowerCAmelCase_						(							self						:	Any		):
       SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs_for_common()

       SCREAMING_SNAKE_CASE_					=    _config_zero_init(_lowerCAmelCase		)
       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE_					=    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    lowerCAmelCase_						(							self						:	Dict       ,       _lowerCAmelCase						:	Tuple       ,       _lowerCAmelCase						:	Optional[int]       ,       _lowerCAmelCase						:	Optional[Any]       ,       _lowerCAmelCase						:	Optional[Any]=2E-4       ,       _lowerCAmelCase						:	Tuple="outputs"       ,       _lowerCAmelCase						:	Any=None		):
       # We override with a slightly higher tol value, as semseg models tend to diverge a bit more
       super().check_pt_tf_outputs(_lowerCAmelCase       ,       _lowerCAmelCase       ,       _lowerCAmelCase       ,       _lowerCAmelCase       ,       _lowerCAmelCase       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Any		):
       SCREAMING_SNAKE_CASE_					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase		)




 @slow
 def    lowerCAmelCase_						(							self						:	List[Any]		):
       for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_					=    DataaVecVisionModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)


def       UpperCAmelCase_							(   )      ->				str:
      SCREAMING_SNAKE_CASE_					=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
      return image





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


 '''simple docstring'''







 @cached_property
 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       return (
           BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'		) if is_vision_available() else None
       )




 @slow
 def    lowerCAmelCase_						(							self						:	List[Any]		):
       SCREAMING_SNAKE_CASE_					=    DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'		).to(
           _lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    self.default_image_processor
       SCREAMING_SNAKE_CASE_					=    prepare_img()
       SCREAMING_SNAKE_CASE_					=    image_processor(images=_lowerCAmelCase       ,       return_tensors='pt'		).to(_lowerCAmelCase		)

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE_					=    model(**_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    outputs.logits

       # verify the logits
       SCREAMING_SNAKE_CASE_					=    torch.Size((1, 1_000)		)
       self.assertEqual(logits.shape       ,       _lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    torch.tensor([0.3277, -0.1395, 0.0911]		).to(_lowerCAmelCase		)

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

       SCREAMING_SNAKE_CASE_					=    [model.config.labelaid[i] for i in ['remote control, remote', 'tabby, tabby cat']]
       self.assertEqual(logits[0].topk(2		).indices.cpu().tolist()       ,       _lowerCAmelCase		)





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


lowerCamelCase__				:     int			       =							False





@nightly
@require_torch_gpu
class  lowerCamelCase_							(   unittest.TestCase      ):


 '''simple docstring'''







 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()



 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       SCREAMING_SNAKE_CASE_					=    VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'		)
       # remove text_unet
       pipe.remove_unused_weights()
       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'		)

       SCREAMING_SNAKE_CASE_					=    torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    pipe(
           prompt='first prompt'       ,       image=_lowerCAmelCase       ,       text_to_image_strength=0.75       ,       generator=_lowerCAmelCase       ,       guidance_scale=7.5       ,       num_inference_steps=2       ,       output_type='numpy'       ,       ).images

       with tempfile.TemporaryDirectory() as tmpdirname:
             pipe.save_pretrained(_lowerCAmelCase		)
             SCREAMING_SNAKE_CASE_					=    VersatileDiffusionDualGuidedPipeline.from_pretrained(_lowerCAmelCase		)

       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    generator.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    pipe(
           prompt='first prompt'       ,       image=_lowerCAmelCase       ,       text_to_image_strength=0.75       ,       generator=_lowerCAmelCase       ,       guidance_scale=7.5       ,       num_inference_steps=2       ,       output_type='numpy'       ,       ).images

       assert np.abs(image - new_image		).sum() < 1E-5, "Models don't have the same forward pass"




 def    lowerCAmelCase_						(							self						:	Any		):
       SCREAMING_SNAKE_CASE_					=    VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'		)
       pipe.remove_unused_weights()
       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    'cyberpunk 2077'
       SCREAMING_SNAKE_CASE_					=    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'		)
       SCREAMING_SNAKE_CASE_					=    torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    pipe(
           prompt=_lowerCAmelCase       ,       image=_lowerCAmelCase       ,       text_to_image_strength=0.75       ,       generator=_lowerCAmelCase       ,       guidance_scale=7.5       ,       num_inference_steps=50       ,       output_type='numpy'       ,       ).images

       SCREAMING_SNAKE_CASE_					=    image[0, 253:256, 253:256, -1]

       assert image.shape == (1, 512, 512, 3)
       SCREAMING_SNAKE_CASE_					=    np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]		)

       assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2