



'''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 	a__     :


	def __init__(self : Optional[Any],					__UpperCAmelCase : List[Any],					__UpperCAmelCase : int=13,					__UpperCAmelCase : Optional[int]=30,					__UpperCAmelCase : Tuple=2,					__UpperCAmelCase : List[Any]=3,					__UpperCAmelCase : Any=True,					__UpperCAmelCase : Optional[Any]=True,					__UpperCAmelCase : Optional[Any]=32,					__UpperCAmelCase : Tuple=5,					__UpperCAmelCase : List[Any]=4,					__UpperCAmelCase : Any=37,					__UpperCAmelCase : Dict="gelu",					__UpperCAmelCase : Dict=0.1,					__UpperCAmelCase : Optional[Any]=0.1,					__UpperCAmelCase : int=10,					__UpperCAmelCase : Union[str, Any]=0.02,					__UpperCAmelCase : int=None,					__UpperCAmelCase : Optional[Any]=2,					)     ->			int:

					"""simple docstring"""

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

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


	def 				lowercase__  (self : Any      )     ->			Any:

					"""simple docstring"""

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

					SCREAMING_SNAKE_CASE  :						int      					= None
					if self.use_labels:
									SCREAMING_SNAKE_CASE  :						List[str]      					= ids_tensor([self.batch_size],					self.type_sequence_label_size      )

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= self.get_config()

					return config, pixel_values, labels


	def 				lowercase__  (self : List[str]      )     ->			Dict:

					"""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=__UpperCAmelCase,					initializer_range=self.initializer_range,					encoder_stride=self.encoder_stride,					)


	def 				lowercase__  (self : int,					__UpperCAmelCase : Dict,					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : Any      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= ViTModel(config=__UpperCAmelCase      )
					model.to(__UpperCAmelCase      )
					model.eval()
					SCREAMING_SNAKE_CASE  :						str      					= model(__UpperCAmelCase      )
					self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)      )


	def 				lowercase__  (self : Optional[int],					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : List[str],					__UpperCAmelCase : Dict      )     ->			Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= ViTForMaskedImageModeling(config=__UpperCAmelCase      )
					model.to(__UpperCAmelCase      )
					model.eval()
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= model(__UpperCAmelCase      )
					self.parent.assertEqual(
					    result.reconstruction.shape,					(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

					# test greyscale images
					SCREAMING_SNAKE_CASE  :						int      					= 1
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= ViTForMaskedImageModeling(__UpperCAmelCase      )
					model.to(__UpperCAmelCase      )
					model.eval()

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


	def 				lowercase__  (self : List[str],					__UpperCAmelCase : Dict,					__UpperCAmelCase : Dict,					__UpperCAmelCase : Union[str, Any]      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= self.type_sequence_label_size
					SCREAMING_SNAKE_CASE  :						str      					= ViTForImageClassification(__UpperCAmelCase      )
					model.to(__UpperCAmelCase      )
					model.eval()
					SCREAMING_SNAKE_CASE  :						List[str]      					= model(__UpperCAmelCase,					labels=__UpperCAmelCase      )
					self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.type_sequence_label_size)      )

					# test greyscale images
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= 1
					SCREAMING_SNAKE_CASE  :						Any      					= ViTForImageClassification(__UpperCAmelCase      )
					model.to(__UpperCAmelCase      )
					model.eval()

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
					SCREAMING_SNAKE_CASE  :						List[Any]      					= model(__UpperCAmelCase      )
					self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.type_sequence_label_size)      )






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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						List[Any]      					= config_and_inputs
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= {'''pixel_values''': pixel_values}
					return config, inputs_dict



@require_torch
class 	a__     (				_lowercase,			_lowercase,			unittest.TestCase	):
	__magic_name__		:    List[str]	    =     (
	    (
	        ViTModel,
	        ViTForImageClassification,
	        ViTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	__magic_name__		:    List[str]	    =     (
	    {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
	    if is_torch_available()
	    else {}
	)
	__magic_name__		:    List[str]	    =     True

	__magic_name__		:    Tuple	    =     False
	__magic_name__		:    List[Any]	    =     False
	__magic_name__		:    Optional[int]	    =     False


	def 				lowercase__  (self : List[Any]      )     ->			Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= ViTModelTester(self      )
					SCREAMING_SNAKE_CASE  :						Tuple      					= ConfigTester(self,					config_class=__UpperCAmelCase,					has_text_modality=__UpperCAmelCase,					hidden_size=37      )


	def 				lowercase__  (self : Dict      )     ->			Dict:

					"""simple docstring"""

					self.config_tester.run_common_tests()


	@unittest.skip(reason='''ViT does not use inputs_embeds'''      )
	def 				lowercase__  (self : int      )     ->			List[str]:

					"""simple docstring"""

					pass


	def 				lowercase__  (self : List[str]      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Dict      					= self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE  :						Optional[int]      					= model_class(__UpperCAmelCase      )
									self.assertIsInstance(model.get_input_embeddings(),					(nn.Module)      )
									SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(__UpperCAmelCase,					nn.Linear      )      )


	def 				lowercase__  (self : int      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Tuple      					= self.model_tester.prepare_config_and_inputs_for_common()

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

									SCREAMING_SNAKE_CASE  :						List[Any]      					= ['''pixel_values''']
									self.assertListEqual(arg_names[:1],					__UpperCAmelCase      )


	def 				lowercase__  (self : str      )     ->			Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__UpperCAmelCase      )


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase      )


	def 				lowercase__  (self : List[str]      )     ->			Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase      )






	@slow
	def 				lowercase__  (self : List[str]      )     ->			int:

					"""simple docstring"""

					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= ViTModel.from_pretrained(__UpperCAmelCase      )
									self.assertIsNotNone(__UpperCAmelCase      )






def       __lowercase  ():
				SCREAMING_SNAKE_CASE  :						int      					= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image




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


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

					"""simple docstring"""

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


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Tuple      					= ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''      ).to(__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						List[Any]      					= self.default_image_processor
					SCREAMING_SNAKE_CASE  :						Dict      					= prepare_img()
					SCREAMING_SNAKE_CASE  :						int      					= image_processor(images=__UpperCAmelCase,					return_tensors='''pt'''      ).to(__UpperCAmelCase      )

					# forward pass
					with torch.no_grad():
									SCREAMING_SNAKE_CASE  :						str      					= model(**__UpperCAmelCase      )

					# verify the logits
					SCREAMING_SNAKE_CASE  :						str      					= torch.Size((1, 1000)      )
					self.assertEqual(outputs.logits.shape,					__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						Any      					= torch.tensor([-0.2744, 0.8215, -0.0836]      ).to(__UpperCAmelCase      )

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


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						str      					= ViTModel.from_pretrained('''facebook/dino-vits8'''      ).to(__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= ViTImageProcessor.from_pretrained('''facebook/dino-vits8''',					size=480      )
					SCREAMING_SNAKE_CASE  :						List[Any]      					= prepare_img()
					SCREAMING_SNAKE_CASE  :						Dict      					= image_processor(images=__UpperCAmelCase,					return_tensors='''pt'''      )
					SCREAMING_SNAKE_CASE  :						str      					= inputs.pixel_values.to(__UpperCAmelCase      )

					# forward pass
					with torch.no_grad():
									SCREAMING_SNAKE_CASE  :						int      					= model(__UpperCAmelCase,					interpolate_pos_encoding=__UpperCAmelCase      )

					# verify the logits
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= torch.Size((1, 3601, 384)      )
					self.assertEqual(outputs.last_hidden_state.shape,					__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						Tuple      					= torch.tensor(
					    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]      ).to(__UpperCAmelCase      )

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






	@slow
	@require_accelerate
	@require_torch_gpu
	def 				lowercase__  (self : str      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= ViTModel.from_pretrained('''facebook/dino-vits8''',					torch_dtype=torch.floataa,					device_map='''auto'''      )
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= self.default_image_processor

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= prepare_img()
					SCREAMING_SNAKE_CASE  :						List[str]      					= image_processor(images=__UpperCAmelCase,					return_tensors='''pt'''      )
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= inputs.pixel_values.to(__UpperCAmelCase      )

					# forward pass to make sure inference works in fp16
					with torch.no_grad():
									SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model(__UpperCAmelCase      )









'''simple docstring'''

import inspect
import os
import unittest
from dataclasses import dataclass

import torch

from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs
from accelerate.state import AcceleratorState
from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu
from accelerate.utils import KwargsHandler


@dataclass
class 	a__     (				_lowercase	):
	__magic_name__		:    int	    =     0
	__magic_name__		:    bool	    =     False
	__magic_name__		:    float	    =     3.0



class 	a__     (				unittest.TestCase	):


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

					"""simple docstring"""

					self.assertDictEqual(MockClass().to_kwargs(),					{}      )
					self.assertDictEqual(MockClass(a=2      ).to_kwargs(),					{'''a''': 2}      )
					self.assertDictEqual(MockClass(a=2,					b=__UpperCAmelCase      ).to_kwargs(),					{'''a''': 2, '''b''': True}      )
					self.assertDictEqual(MockClass(a=2,					c=2.25      ).to_kwargs(),					{'''a''': 2, '''c''': 2.25}      )


	@require_cuda
	def 				lowercase__  (self : Dict      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= GradScalerKwargs(init_scale=1024,					growth_factor=2      )
					AcceleratorState._reset_state()
					SCREAMING_SNAKE_CASE  :						Tuple      					= Accelerator(mixed_precision='''fp16''',					kwargs_handlers=[scaler_handler]      )
					print(accelerator.use_fpaa      )
					SCREAMING_SNAKE_CASE  :						str      					= accelerator.scaler

					# Check the kwargs have been applied
					self.assertEqual(scaler._init_scale,					1024.0      )
					self.assertEqual(scaler._growth_factor,					2.0      )

					# Check the other values are at the default
					self.assertEqual(scaler._backoff_factor,					0.5      )
					self.assertEqual(scaler._growth_interval,					2000      )
					self.assertEqual(scaler._enabled,					__UpperCAmelCase      )






	@require_multi_gpu
	def 				lowercase__  (self : Optional[Any]      )     ->			Dict:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						str      					= ['''torchrun''', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__      )]
					execute_subprocess_async(__UpperCAmelCase,					env=os.environ.copy()      )


if __name__ == "__main__":
	snake_case_			=     DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True)
	snake_case_			=     Accelerator(kwargs_handlers=[ddp_scaler])
	snake_case_			=     torch.nn.Linear(100, 200)
	snake_case_			=     accelerator.prepare(model)

	# Check the values changed in kwargs
	snake_case_			=     """"""
	snake_case_			=     model.bucket_bytes_cap // (1024 * 1024)
	if observed_bucket_cap_map != 15:
		error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n"
	if model.find_unused_parameters is not True:
		error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n"

	# Check the values of the defaults
	if model.dim != 0:
		error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n"
	if model.broadcast_buffers is not True:
		error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n"
	if model.gradient_as_bucket_view is not False:
		error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n"

	# Raise error at the end to make sure we don't stop at the first failure.
	if len(error_msg) > 0:
		raise ValueError(error_msg)




