

"""simple docstring"""




import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
    from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor







class      __lowercase			:

  def __init__(  self		:			Optional[int]					,A		:			Union[str, Any]					,A		:			Any=13					,A		:			List[Any]=30					,A		:			Union[str, Any]=2					,A		:			Union[str, Any]=3					,A		:			Optional[int]=True					,A		:			int=True					,A		:			List[str]=32					,A		:			List[str]=5					,A		:			List[str]=4					,A		:			Optional[Any]=37					,A		:			Dict="gelu"					,A		:			List[Any]=0.1					,A		:			int=0.1					,A		:			List[str]=10					,A		:			Union[str, Any]=0.0_2					,A		:			List[Any]=None					,A		:			Optional[Any]=2					,):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					parent
   UpperCAmelCase__					:  List[str]					   =					batch_size
   UpperCAmelCase__					:  Tuple					   =					image_size
   UpperCAmelCase__					:  Optional[Any]					   =					patch_size
   UpperCAmelCase__					:  Union[str, Any]					   =					num_channels
   UpperCAmelCase__					:  List[str]					   =					is_training
   UpperCAmelCase__					:  Optional[int]					   =					use_labels
   UpperCAmelCase__					:  Tuple					   =					hidden_size
   UpperCAmelCase__					:  List[Any]					   =					num_hidden_layers
   UpperCAmelCase__					:  Any					   =					num_attention_heads
   UpperCAmelCase__					:  Any					   =					intermediate_size
   UpperCAmelCase__					:  Union[str, Any]					   =					hidden_act
   UpperCAmelCase__					:  List[str]					   =					hidden_dropout_prob
   UpperCAmelCase__					:  Dict					   =					attention_probs_dropout_prob
   UpperCAmelCase__					:  str					   =					type_sequence_label_size
   UpperCAmelCase__					:  Union[str, Any]					   =					initializer_range
   UpperCAmelCase__					:  Dict					   =					scope
   UpperCAmelCase__					:  Tuple					   =					encoder_stride

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

  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

   UpperCAmelCase__					:  int					   =					None
   if self.use_labels:
    UpperCAmelCase__					:  Union[str, Any]					   =					ids_tensor([self.batch_size]					,self.type_sequence_label_size    )

   UpperCAmelCase__					:  Dict					   =					self.get_config()

   return config, pixel_values, labels

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






   '''simple docstring'''
   return ViTConfig(
       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					,encoder_stride=self.encoder_stride					,)

  def    __lowercase       (  self		:			Optional[Any]					,A		:			Optional[Any]					,A		:			List[str]					,A		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[str]					   =					ViTModel(config=A    )
   model.to(A    )
   model.eval()
   UpperCAmelCase__					:  Optional[Any]					   =					model(A    )
   self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)    )

  def    __lowercase       (  self		:			List[str]					,A		:			Optional[int]					,A		:			List[str]					,A		:			List[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					ViTForMaskedImageModeling(config=A    )
   model.to(A    )
   model.eval()
   UpperCAmelCase__					:  Optional[Any]					   =					model(A    )
   self.parent.assertEqual(
       result.reconstruction.shape					,(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

   # test greyscale images
   UpperCAmelCase__					:  str					   =					1
   UpperCAmelCase__					:  List[str]					   =					ViTForMaskedImageModeling(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Union[str, Any]					   =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
   UpperCAmelCase__					:  Optional[int]					   =					model(A    )
   self.parent.assertEqual(result.reconstruction.shape					,(self.batch_size, 1, self.image_size, self.image_size)    )

  def    __lowercase       (  self		:			Optional[int]					,A		:			List[Any]					,A		:			Union[str, Any]					,A		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					self.type_sequence_label_size
   UpperCAmelCase__					:  Optional[Any]					   =					ViTForImageClassification(A    )
   model.to(A    )
   model.eval()
   UpperCAmelCase__					:  Dict					   =					model(A					,labels=A    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)    )

   # test greyscale images
   UpperCAmelCase__					:  int					   =					1
   UpperCAmelCase__					:  Tuple					   =					ViTForImageClassification(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Optional[int]					   =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
   UpperCAmelCase__					:  str					   =					model(A    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)    )




  def    __lowercase       (  self		:			Tuple    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[str]					   =					self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase__       
   )   ,      (
       UpperCAmelCase__       
   )   ,      (
       UpperCAmelCase__       
   )   ,             
   )					:  List[str]					   =					config_and_inputs
   UpperCAmelCase__					:  Optional[int]					   =					{"""pixel_values""": pixel_values}
   return config, inputs_dict







@require_torch
class      __lowercase			(    UpperCamelCase__	,			UpperCamelCase__	,			unittest.TestCase					):
  snake_case_				       =       (
      (
          ViTModel,
          ViTForImageClassification,
          ViTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  snake_case_				       =       (
      {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
      if is_torch_available()
      else {}
  )
  snake_case_				       =       True

  snake_case_				       =       False
  snake_case_				       =       False
  snake_case_				       =       False

  def    __lowercase       (  self		:			Optional[int]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					ViTModelTester(self    )
   UpperCAmelCase__					:  str					   =					ConfigTester(self					,config_class=A					,has_text_modality=A					,hidden_size=37    )

  def    __lowercase       (  self		:			List[Any]    ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  @unittest.skip(reason="""ViT does not use inputs_embeds"""    )
  def    __lowercase       (  self		:			Dict    ):






   '''simple docstring'''
   pass

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  List[str]					   =					self.model_tester.prepare_config_and_inputs_for_common()

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

  def    __lowercase       (  self		:			Optional[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  Union[str, Any]					   =					self.model_tester.prepare_config_and_inputs_for_common()

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

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

  def    __lowercase       (  self		:			Tuple    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*A    )

  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[str]					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_image_modeling(*A    )

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*A    )




  @slow
  def    __lowercase       (  self		:			Optional[int]    ):






   '''simple docstring'''
   for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__					:  Optional[Any]					   =					ViTModel.from_pretrained(A    )
    self.assertIsNotNone(A    )







def   lowerCAmelCase						(					):
 '''simple docstring'''





 UpperCAmelCase__					:  Optional[Any]					   =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
 return image







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

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






   '''simple docstring'''
   return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""    ) if is_vision_available() else None

  @slow
  def    __lowercase       (  self		:			List[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""    ).to(A    )

   UpperCAmelCase__					:  List[Any]					   =					self.default_image_processor
   UpperCAmelCase__					:  Optional[Any]					   =					prepare_img()
   UpperCAmelCase__					:  str					   =					image_processor(images=A					,return_tensors="""pt"""    ).to(A    )

   # forward pass
   with torch.no_grad():
    UpperCAmelCase__					:  Dict					   =					model(**A    )

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

   UpperCAmelCase__					:  List[Any]					   =					torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]    ).to(A    )

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

  @slow
  def    __lowercase       (  self		:			List[str]    ):






   '''simple docstring'''
   # ViT models have an `interpolate_pos_encoding` argument in their forward method,
   # allowing to interpolate the pre-trained position embeddings in order to use
   # the model on higher resolutions. The DINO model by Facebook AI leverages this
   # to visualize self-attention on higher resolution images.
   UpperCAmelCase__					:  Union[str, Any]					   =					ViTModel.from_pretrained("""facebook/dino-vits8"""    ).to(A    )

   UpperCAmelCase__					:  Dict					   =					ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""					,size=480    )
   UpperCAmelCase__					:  Optional[Any]					   =					prepare_img()
   UpperCAmelCase__					:  List[str]					   =					image_processor(images=A					,return_tensors="""pt"""    )
   UpperCAmelCase__					:  Any					   =					inputs.pixel_values.to(A    )

   # forward pass
   with torch.no_grad():
    UpperCAmelCase__					:  Union[str, Any]					   =					model(A					,interpolate_pos_encoding=A    )

   # verify the logits
   UpperCAmelCase__					:  Any					   =					torch.Size((1, 3_601, 384)    )
   self.assertEqual(outputs.last_hidden_state.shape					,A    )

   UpperCAmelCase__					:  Optional[Any]					   =					torch.tensor(
       [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]]    ).to(A    )

   self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]					,A					,atol=1e-4    )    )




  @slow
  @require_accelerate
  @require_torch_gpu
  def    __lowercase       (  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					ViTModel.from_pretrained("""facebook/dino-vits8"""					,torch_dtype=torch.floataa					,device_map="""auto"""    )
   UpperCAmelCase__					:  Optional[Any]					   =					self.default_image_processor

   UpperCAmelCase__					:  Any					   =					prepare_img()
   UpperCAmelCase__					:  Union[str, Any]					   =					image_processor(images=A					,return_tensors="""pt"""    )
   UpperCAmelCase__					:  Tuple					   =					inputs.pixel_values.to(A    )

   # forward pass to make sure inference works in fp16
   with torch.no_grad():
    UpperCAmelCase__					:  Dict					   =					model(A    )








"""simple docstring"""




from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


__UpperCAmelCase =				logging.get_logger(__name__)


__UpperCAmelCase =				TypeVar('DatasetType', Dataset, IterableDataset)
def   lowerCAmelCase						(					__UpperCamelCase							,    __UpperCamelCase = None							,    __UpperCamelCase = None							,    __UpperCamelCase = None							,    __UpperCamelCase = None							,    __UpperCamelCase = "first_exhausted"							,    ):
 '''simple docstring'''





 from .arrow_dataset import Dataset
 from .iterable_dataset import IterableDataset

 if not datasets:
  raise ValueError("""Unable to interleave an empty list of datasets."""      )
 for i, dataset in enumerate(__UpperCamelCase      ):
  if not isinstance(__UpperCamelCase							,    (Dataset, IterableDataset)      ):
   if isinstance(__UpperCamelCase							,    (DatasetDict, IterableDatasetDict)      ):
    if not dataset:
     raise ValueError(
         F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
         """is an empty dataset dictionary."""      )
    raise ValueError(
        F"Dataset at position {i} has at least one split: {list(__UpperCamelCase      )}\n"
        F"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCamelCase      )      )}']"      )
   raise ValueError(
       F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCamelCase      ).__name__}."      )
  if i == 0:
   UpperCAmelCase__   ,      UpperCAmelCase__					:  List[str]					   =					(
       (Dataset, IterableDataset) if isinstance(__UpperCamelCase							,    __UpperCamelCase      ) else (IterableDataset, Dataset)
   )
  elif not isinstance(__UpperCamelCase							,    __UpperCamelCase      ):
   raise ValueError(
       F"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."      )
 if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
  raise ValueError(F"{stopping_strategy} is not supported. Please enter a valid stopping_strategy."      )
 if dataset_type is Dataset:
  return _interleave_map_style_datasets(
      __UpperCamelCase							,    __UpperCamelCase							,    __UpperCamelCase							,    info=__UpperCamelCase							,    split=__UpperCamelCase							,    stopping_strategy=__UpperCamelCase      )
 else:
  return _interleave_iterable_datasets(
      __UpperCamelCase							,    __UpperCamelCase							,    __UpperCamelCase							,    info=__UpperCamelCase							,    split=__UpperCamelCase							,    stopping_strategy=__UpperCamelCase      )




def   lowerCAmelCase						(					__UpperCamelCase							,    __UpperCamelCase = None							,    __UpperCamelCase = None							,    __UpperCamelCase = 0							,    ):
 '''simple docstring'''





 if not dsets:
  raise ValueError("""Unable to concatenate an empty list of datasets."""      )
 for i, dataset in enumerate(__UpperCamelCase      ):
  if not isinstance(__UpperCamelCase							,    (Dataset, IterableDataset)      ):
   if isinstance(__UpperCamelCase							,    (DatasetDict, IterableDatasetDict)      ):
    if not dataset:
     raise ValueError(
         F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
         """is an empty dataset dictionary."""      )
    raise ValueError(
        F"Dataset at position {i} has at least one split: {list(__UpperCamelCase      )}\n"
        F"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCamelCase      )      )}']"      )
   raise ValueError(
       F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCamelCase      ).__name__}."      )
  if i == 0:
   UpperCAmelCase__   ,      UpperCAmelCase__					:  Optional[int]					   =					(
       (Dataset, IterableDataset) if isinstance(__UpperCamelCase							,    __UpperCamelCase      ) else (IterableDataset, Dataset)
   )
  elif not isinstance(__UpperCamelCase							,    __UpperCamelCase      ):
   raise ValueError(
       F"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."      )
 if dataset_type is Dataset:
  return _concatenate_map_style_datasets(__UpperCamelCase							,    info=__UpperCamelCase							,    split=__UpperCamelCase							,    axis=__UpperCamelCase      )
 else:
  return _concatenate_iterable_datasets(__UpperCamelCase							,    info=__UpperCamelCase							,    split=__UpperCamelCase							,    axis=__UpperCamelCase      )





