






"""simple docstring"""





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 ,      UpperCAmelCase_ ,      UpperCAmelCase_=100 ,      UpperCAmelCase_=13 ,      UpperCAmelCase_=30 ,      UpperCAmelCase_=2 ,      UpperCAmelCase_=3 ,      UpperCAmelCase_=True ,      UpperCAmelCase_=True ,      UpperCAmelCase_=32 ,      UpperCAmelCase_=4 ,      UpperCAmelCase_=4 ,      UpperCAmelCase_=37 ,      UpperCAmelCase_="gelu" ,      UpperCAmelCase_=0.1 ,      UpperCAmelCase_=0.1 ,      UpperCAmelCase_=10 ,      UpperCAmelCase_=0.02 ,      UpperCAmelCase_=3 ,      UpperCAmelCase_=None ,      UpperCAmelCase_=[0, 1, 2, 3] ,      )       ->					Optional[int]:




          '''simple docstring'''




          lowercase__:				Tuple			  =			parent
          lowercase__:				Any			  =			100
          lowercase__:				List[str]			  =			batch_size
          lowercase__:				List[str]			  =			image_size
          lowercase__:				Tuple			  =			patch_size
          lowercase__:				Tuple			  =			num_channels
          lowercase__:				Any			  =			is_training
          lowercase__:				int			  =			use_labels
          lowercase__:				List[Any]			  =			hidden_size
          lowercase__:				List[str]			  =			num_hidden_layers
          lowercase__:				List[str]			  =			num_attention_heads
          lowercase__:				Union[str, Any]			  =			intermediate_size
          lowercase__:				Any			  =			hidden_act
          lowercase__:				List[Any]			  =			hidden_dropout_prob
          lowercase__:				Union[str, Any]			  =			attention_probs_dropout_prob
          lowercase__:				Tuple			  =			type_sequence_label_size
          lowercase__:				List[Any]			  =			initializer_range
          lowercase__:				Any			  =			scope
          lowercase__:				str			  =			out_indices
          lowercase__:				str			  =			num_labels

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





   def __lowercase				(    self)       ->					Tuple:




          '''simple docstring'''




          lowercase__:				Optional[int]			  =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

          lowercase__:				Tuple			  =			None
          lowercase__:				str			  =			None
          if self.use_labels:
                 lowercase__:				List[str]			  =			ids_tensor([self.batch_size] ,      self.type_sequence_label_size)
                 lowercase__:				Dict			  =			ids_tensor([self.batch_size, self.image_size, self.image_size] ,      self.num_labels)

          lowercase__:				Optional[Any]			  =			self.get_config()

          return config, pixel_values, labels, pixel_labels





   def __lowercase				(    self)       ->					str:




          '''simple docstring'''




          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=A_ ,      initializer_range=self.initializer_range ,      out_indices=self.out_indices ,      )





   def __lowercase				(    self ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_)       ->					List[str]:




          '''simple docstring'''




          lowercase__:				Union[str, Any]			  =			DataaVecVisionModel(config=A_)
          model.to(A_)
          model.eval()
          lowercase__:				Optional[int]			  =			model(A_)
          # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
          lowercase__:				List[str]			  =			(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 __lowercase				(    self ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_)       ->					Optional[Any]:




          '''simple docstring'''




          lowercase__:				List[str]			  =			self.type_sequence_label_size
          lowercase__:				Dict			  =			DataaVecVisionForImageClassification(A_)
          model.to(A_)
          model.eval()
          lowercase__:				Dict			  =			model(A_ ,      labels=A_)
          self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.type_sequence_label_size))





   def __lowercase				(    self ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_)       ->					int:




          '''simple docstring'''




          lowercase__:				Dict			  =			self.num_labels
          lowercase__:				int			  =			DataaVecVisionForSemanticSegmentation(A_)
          model.to(A_)
          model.eval()
          lowercase__:				List[Any]			  =			model(A_)
          self.parent.assertEqual(
              result.logits.shape ,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))
          lowercase__:				Optional[int]			  =			model(A_ ,      labels=A_)
          self.parent.assertEqual(
              result.logits.shape ,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))





   def __lowercase				(    self)       ->					List[Any]:




          '''simple docstring'''




          lowercase__:				List[Any]			  =			self.prepare_config_and_inputs()
          lowercase__:				int			  =			config_and_inputs
          lowercase__:				Tuple			  =			{"pixel_values": pixel_values}
          return config, inputs_dict







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





   '''simple docstring'''






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

   UpperCamelCase__			     =				False
   UpperCamelCase__			     =				False
   UpperCamelCase__			     =				False





   def __lowercase				(    self)       ->					Optional[int]:




          '''simple docstring'''




          lowercase__:				List[str]			  =			DataaVecVisionModelTester(self)
          lowercase__:				Union[str, Any]			  =			ConfigTester(
              self ,      config_class=A_ ,      has_text_modality=A_ ,      hidden_size=37)





   def __lowercase				(    self)       ->					Tuple:




          '''simple docstring'''




          self.config_tester.run_common_tests()





   def __lowercase				(    self)       ->					List[str]:




          '''simple docstring'''




          pass





   @require_torch_multi_gpu
   @unittest.skip(
       reason="Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`")
   def __lowercase				(    self)       ->					int:




          '''simple docstring'''




          pass





   def __lowercase				(    self)       ->					str:




          '''simple docstring'''




          lowercase__:				Any			  =			self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 lowercase__:				Union[str, Any]			  =			model_class(A_)
                 self.assertIsInstance(model.get_input_embeddings() ,      (nn.Module))
                 lowercase__:				Tuple			  =			model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(A_ ,      nn.Linear))





   def __lowercase				(    self)       ->					List[str]:




          '''simple docstring'''




          lowercase__:				List[str]			  =			self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowercase__:				List[str]			  =			["pixel_values"]
                 self.assertListEqual(arg_names[:1] ,      A_)





   def __lowercase				(    self)       ->					List[Any]:




          '''simple docstring'''




          lowercase__:				Tuple			  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A_)





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




          '''simple docstring'''




          lowercase__:				Tuple			  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_segmentation(*A_)





   def __lowercase				(    self)       ->					str:




          '''simple docstring'''




          if not self.model_tester.is_training:
                 return

          lowercase__:				List[Any]			  =			self.model_tester.prepare_config_and_inputs_for_common()
          lowercase__:				Any			  =			True

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

                 lowercase__:				str			  =			model_class(A_)
                 model.to(A_)
                 model.train()
                 lowercase__:				int			  =			self._prepare_for_class(A_ ,      A_ ,      return_labels=A_)
                 lowercase__:				str			  =			model(**A_).loss
                 loss.backward()





   def __lowercase				(    self)       ->					List[Any]:




          '''simple docstring'''




          lowercase__:				List[str]			  =			self.model_tester.prepare_config_and_inputs_for_common()
          if not self.model_tester.is_training:
                 return

          lowercase__:				Tuple			  =			False
          lowercase__:				List[Any]			  =			True

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





   def __lowercase				(    self)       ->					str:




          '''simple docstring'''




          lowercase__:				Union[str, Any]			  =			self.model_tester.prepare_config_and_inputs_for_common()

          lowercase__:				List[Any]			  =			_config_zero_init(A_)
          for model_class in self.all_model_classes:
                 lowercase__:				Any			  =			model_class(config=A_)
                 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 __lowercase				(    self ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_=2E-4 ,      UpperCAmelCase_="outputs" ,      UpperCAmelCase_=None)       ->					Any:




          '''simple docstring'''




          super().check_pt_tf_outputs(A_ ,      A_ ,      A_ ,      A_ ,      A_ ,      A_)





   def __lowercase				(    self)       ->					Optional[int]:




          '''simple docstring'''




          lowercase__:				List[str]			  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*A_)





   @slow
   def __lowercase				(    self)       ->					int:




          '''simple docstring'''




          for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowercase__:				Tuple			  =			DataaVecVisionModel.from_pretrained(A_)
                 self.assertIsNotNone(A_)







def 				A(  ):
       """simple docstring"""





       lowercase__:				Dict			  =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
       return image







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





   '''simple docstring'''






   @cached_property
   def __lowercase				(    self)       ->					Optional[Any]:




          '''simple docstring'''




          return (
              BeitImageProcessor.from_pretrained("facebook/data2vec-vision-base-ft1k") if is_vision_available() else None
          )





   @slow
   def __lowercase				(    self)       ->					Tuple:




          '''simple docstring'''




          lowercase__:				List[str]			  =			DataaVecVisionForImageClassification.from_pretrained("facebook/data2vec-vision-base-ft1k").to(
              A_)

          lowercase__:				Optional[int]			  =			self.default_image_processor
          lowercase__:				Any			  =			prepare_img()
          lowercase__:				List[Any]			  =			image_processor(images=A_ ,      return_tensors="pt").to(A_)

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

          # verify the logits
          lowercase__:				Optional[int]			  =			torch.Size((1, 1_000))
          self.assertEqual(logits.shape ,      A_)

          lowercase__:				Any			  =			torch.tensor([0.32_77, -0.13_95, 0.09_11]).to(A_)

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

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









from __future__ import annotations

SCREAMING_SNAKE_CASE_						=					'Alexander Joslin'

def 							__SCREAMING_SNAKE_CASE							( lowerCAmelCase:     int       )      ->				int:

				if number <= 0:
								_UpperCAmelCase				:						Optional[Any]       						=			F'n must be greater than 0. Got n = {number}'
								raise ValueError(lowerCAmelCase       )

				_UpperCAmelCase				:						Dict       						=			[number + 1] * (number + 1)

				# starting position
				_UpperCAmelCase				:						Dict       						=			0
				for i in range(1     ,					lowerCAmelCase       ):
								_UpperCAmelCase				:						int       						=			min(table[i + 1]     ,					table[i] + 1       )
								# check if out of bounds
								if i * 2 <= number:
												_UpperCAmelCase				:						int       						=			min(table[i * 2]     ,					table[i] + 1       )
								# check if out of bounds
								if i * 3 <= number:
												_UpperCAmelCase				:						int       						=			min(table[i * 3]     ,					table[i] + 1       )
				return table[number]


if __name__ == "__main__":
					import doctest

					doctest.testmod()


