

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     ,      lowercase__     ,      lowercase__=100     ,      lowercase__=13     ,      lowercase__=30     ,      lowercase__=2     ,      lowercase__=3     ,      lowercase__=True     ,      lowercase__=True     ,      lowercase__=32     ,      lowercase__=4     ,      lowercase__=4     ,      lowercase__=37     ,      lowercase__="gelu"     ,      lowercase__=0.1     ,      lowercase__=0.1     ,      lowercase__=10     ,      lowercase__=0.02     ,      lowercase__=3     ,      lowercase__=None     ,      lowercase__=[0, 1, 2, 3]     ,      )     ->      Optional[int]:
        __UpperCAmelCase									=			parent
        __UpperCAmelCase									=			100
        __UpperCAmelCase									=			batch_size
        __UpperCAmelCase									=			image_size
        __UpperCAmelCase									=			patch_size
        __UpperCAmelCase									=			num_channels
        __UpperCAmelCase									=			is_training
        __UpperCAmelCase									=			use_labels
        __UpperCAmelCase									=			hidden_size
        __UpperCAmelCase									=			num_hidden_layers
        __UpperCAmelCase									=			num_attention_heads
        __UpperCAmelCase									=			intermediate_size
        __UpperCAmelCase									=			hidden_act
        __UpperCAmelCase									=			hidden_dropout_prob
        __UpperCAmelCase									=			attention_probs_dropout_prob
        __UpperCAmelCase									=			type_sequence_label_size
        __UpperCAmelCase									=			initializer_range
        __UpperCAmelCase									=			scope
        __UpperCAmelCase									=			out_indices
        __UpperCAmelCase									=			num_labels

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


    def      lowerCAmelCase_						(self  )     ->      Optional[int]:
        __UpperCAmelCase									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

        __UpperCAmelCase									=			self.get_config()

        return config, pixel_values, labels, pixel_labels


    def      lowerCAmelCase_						(self  )     ->      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=lowercase__     ,      initializer_range=self.initializer_range     ,      out_indices=self.out_indices     ,      )


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


    def      lowerCAmelCase_						(self     ,      lowercase__     ,      lowercase__     ,      lowercase__     ,      lowercase__  )     ->      Optional[Any]:
        __UpperCAmelCase									=			self.num_labels
        __UpperCAmelCase									=			DataaVecVisionForSemanticSegmentation(lowercase__  )
        model.to(lowercase__  )
        model.eval()
        __UpperCAmelCase									=			model(lowercase__  )
        self.parent.assertEqual(
            result.logits.shape     ,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)  )
        __UpperCAmelCase									=			model(lowercase__     ,      labels=lowercase__  )
        self.parent.assertEqual(
            result.logits.shape     ,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)  )






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






@require_torch
class 					A_   (      _a ,     _a ,     unittest.TestCase						):


    '''simple docstring'''






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

    a__     							=       False
    a__     							=       False
    a__     							=       False


    def      lowerCAmelCase_						(self  )     ->      int:
        __UpperCAmelCase									=			DataaVecVisionModelTester(self  )
        __UpperCAmelCase									=			ConfigTester(
            self     ,      config_class=lowercase__     ,      has_text_modality=lowercase__     ,      hidden_size=37  )


    def      lowerCAmelCase_						(self  )     ->      List[str]:
        self.config_tester.run_common_tests()


    def      lowerCAmelCase_						(self  )     ->      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]:
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs_for_common()

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


    def      lowerCAmelCase_						(self  )     ->      Dict:
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs_for_common()

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

            __UpperCAmelCase									=			['''pixel_values''']
            self.assertListEqual(arg_names[:1]     ,      lowercase__  )


    def      lowerCAmelCase_						(self  )     ->      int:
        __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase__  )


    def      lowerCAmelCase_						(self  )     ->      Union[str, Any]:
        __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*lowercase__  )


    def      lowerCAmelCase_						(self  )     ->      Dict:
        if not self.model_tester.is_training:
            return

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

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

            __UpperCAmelCase									=			model_class(lowercase__  )
            model.to(lowercase__  )
            model.train()
            __UpperCAmelCase									=			self._prepare_for_class(lowercase__     ,      lowercase__     ,      return_labels=lowercase__  )
            __UpperCAmelCase									=			model(**lowercase__  ).loss
            loss.backward()


    def      lowerCAmelCase_						(self  )     ->      Any:
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
            return

        __UpperCAmelCase									=			False
        __UpperCAmelCase									=			True

        for model_class in self.all_model_classes:
            if model_class in [*get_values(lowercase__  )] 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									=			inputs_dict['''pixel_values'''].shape
                __UpperCAmelCase									=			torch.zeros(
                    [self.model_tester.batch_size, height, width]     ,      device=lowercase__  ).long()
            __UpperCAmelCase									=			model_class(lowercase__  )
            model.gradient_checkpointing_enable()
            model.to(lowercase__  )
            model.train()
            __UpperCAmelCase									=			self._prepare_for_class(lowercase__     ,      lowercase__     ,      return_labels=lowercase__  )
            __UpperCAmelCase									=			model(**lowercase__  ).loss
            loss.backward()


    def      lowerCAmelCase_						(self  )     ->      List[Any]:
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs_for_common()

        __UpperCAmelCase									=			_config_zero_init(lowercase__  )
        for model_class in self.all_model_classes:
            __UpperCAmelCase									=			model_class(config=lowercase__  )
            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     ,      lowercase__     ,      lowercase__     ,      lowercase__     ,      lowercase__=2E-4     ,      lowercase__="outputs"     ,      lowercase__=None  )     ->      List[str]:
        # We override with a slightly higher tol value, as semseg models tend to diverge a bit more
        super().check_pt_tf_outputs(lowercase__     ,      lowercase__     ,      lowercase__     ,      lowercase__     ,      lowercase__     ,      lowercase__  )


    def      lowerCAmelCase_						(self  )     ->      List[Any]:
        __UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase__  )






    @slow
    def      lowerCAmelCase_						(self  )     ->      Optional[Any]:
        for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __UpperCAmelCase									=			DataaVecVisionModel.from_pretrained(lowercase__  )
            self.assertIsNotNone(lowercase__  )






def       __a       (   )      ->     Union[str, Any]:






    '''simple docstring'''
    __UpperCAmelCase									=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    return image
@require_torch
@require_vision
class 					A_   (      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  )     ->      Tuple:
        __UpperCAmelCase									=			DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''  ).to(
            lowercase__  )

        __UpperCAmelCase									=			self.default_image_processor
        __UpperCAmelCase									=			prepare_img()
        __UpperCAmelCase									=			image_processor(images=lowercase__     ,      return_tensors='''pt'''  ).to(lowercase__  )

        # forward pass
        with torch.no_grad():
            __UpperCAmelCase									=			model(**lowercase__  )
        __UpperCAmelCase									=			outputs.logits

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

        __UpperCAmelCase									=			torch.tensor([0.3277, -0.1395, 0.0911]  ).to(lowercase__  )

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

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







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


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import SwinaSRImageProcessor
       from transformers.image_transforms import get_image_size
class 					A_   (      unittest.TestCase						):


    '''simple docstring'''






    def __init__(self     ,      lowercase__     ,      lowercase__=7     ,      lowercase__=3     ,      lowercase__=18     ,      lowercase__=30     ,      lowercase__=400     ,      lowercase__=True     ,      lowercase__=1 / 255     ,      lowercase__=True     ,      lowercase__=8     ,      )     ->      List[str]:
        __UpperCAmelCase									=			parent
        __UpperCAmelCase									=			batch_size
        __UpperCAmelCase									=			num_channels
        __UpperCAmelCase									=			image_size
        __UpperCAmelCase									=			min_resolution
        __UpperCAmelCase									=			max_resolution
        __UpperCAmelCase									=			do_rescale
        __UpperCAmelCase									=			rescale_factor
        __UpperCAmelCase									=			do_pad
        __UpperCAmelCase									=			pad_size


    def      lowerCAmelCase_						(self  )     ->      int:
        return {
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_pad": self.do_pad,
            "pad_size": self.pad_size,
        }






    def      lowerCAmelCase_						(self     ,      lowercase__=False     ,      lowercase__=False     ,      lowercase__=False  )     ->      Tuple:
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
            __UpperCAmelCase									=			[]
            for i in range(self.batch_size  ):
                image_inputs.append(
                    np.random.randint(
                        255     ,      size=(self.num_channels, self.max_resolution, self.max_resolution)     ,      dtype=np.uinta  )  )
        else:
            __UpperCAmelCase									=			[]
            for i in range(self.batch_size  ):
                __UpperCAmelCase					,     __UpperCAmelCase									=			np.random.choice(np.arange(self.min_resolution     ,      self.max_resolution  )     ,      2  )
                image_inputs.append(np.random.randint(255     ,      size=(self.num_channels, width, height)     ,      dtype=np.uinta  )  )

        if not numpify and not torchify:
            # PIL expects the channel dimension as last dimension
            __UpperCAmelCase									=			[Image.fromarray(np.moveaxis(lowercase__     ,      0     ,      -1  )  ) for x in image_inputs]

        if torchify:
            __UpperCAmelCase									=			[torch.from_numpy(lowercase__  ) for x in image_inputs]

        return image_inputs






@require_torch
@require_vision
class 					A_   (      _a ,     unittest.TestCase						):


    '''simple docstring'''






    a__     							=       SwinaSRImageProcessor if is_vision_available() else None


    def      lowerCAmelCase_						(self  )     ->      str:
        __UpperCAmelCase									=			SwinaSRImageProcessingTester(self  )


    @property
    def      lowerCAmelCase_						(self  )     ->      str:
        return self.image_processor_tester.prepare_image_processor_dict()


    def      lowerCAmelCase_						(self  )     ->      List[Any]:
        __UpperCAmelCase									=			self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(lowercase__     ,      '''do_rescale'''  )  )
        self.assertTrue(hasattr(lowercase__     ,      '''rescale_factor'''  )  )
        self.assertTrue(hasattr(lowercase__     ,      '''do_pad'''  )  )
        self.assertTrue(hasattr(lowercase__     ,      '''pad_size'''  )  )


    def      lowerCAmelCase_						(self  )     ->      Optional[Any]:
        pass


    def      lowerCAmelCase_						(self     ,      lowercase__  )     ->      int:
        __UpperCAmelCase					,     __UpperCAmelCase									=			get_image_size(lowercase__  )
        __UpperCAmelCase									=			self.image_processor_tester.pad_size

        __UpperCAmelCase									=			(old_height // size + 1) * size - old_height
        __UpperCAmelCase									=			(old_width // size + 1) * size - old_width
        return old_height + pad_height, old_width + pad_width


    def      lowerCAmelCase_						(self  )     ->      Optional[int]:
        # Initialize image_processor
        __UpperCAmelCase									=			self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        __UpperCAmelCase									=			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__  )
        for image in image_inputs:
            self.assertIsInstance(lowercase__     ,      Image.Image  )

        # Test not batched input
        __UpperCAmelCase									=			image_processor(image_inputs[0]     ,      return_tensors='''pt'''  ).pixel_values
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.calculate_expected_size(np.array(image_inputs[0]  )  )
        self.assertEqual(
            encoded_images.shape     ,      (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )     ,      )


    def      lowerCAmelCase_						(self  )     ->      str:
        # Initialize image_processor
        __UpperCAmelCase									=			self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        __UpperCAmelCase									=			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__     ,      numpify=lowercase__  )
        for image in image_inputs:
            self.assertIsInstance(lowercase__     ,      np.ndarray  )

        # Test not batched input
        __UpperCAmelCase									=			image_processor(image_inputs[0]     ,      return_tensors='''pt'''  ).pixel_values
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.calculate_expected_size(image_inputs[0]  )
        self.assertEqual(
            encoded_images.shape     ,      (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )     ,      )






    def      lowerCAmelCase_						(self  )     ->      int:
        # Initialize image_processor
        __UpperCAmelCase									=			self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        __UpperCAmelCase									=			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__     ,      torchify=lowercase__  )
        for image in image_inputs:
            self.assertIsInstance(lowercase__     ,      torch.Tensor  )

        # Test not batched input
        __UpperCAmelCase									=			image_processor(image_inputs[0]     ,      return_tensors='''pt'''  ).pixel_values
        __UpperCAmelCase					,     __UpperCAmelCase									=			self.calculate_expected_size(image_inputs[0]  )
        self.assertEqual(
            encoded_images.shape     ,      (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )     ,      )




