"""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        snake_case :




					"""simple docstring"""

					def __init__(       self     :			Any					,lowerCamelCase__     :			int					,lowerCamelCase__     :			List[str]=13					,lowerCamelCase__     :			List[Any]=30					,lowerCamelCase__     :			int=2					,lowerCamelCase__     :			Any=3					,lowerCamelCase__     :			str=True					,lowerCamelCase__     :			Union[str, Any]=True					,lowerCamelCase__     :			Dict=32					,lowerCamelCase__     :			int=5					,lowerCamelCase__     :			List[Any]=4					,lowerCamelCase__     :			List[Any]=37					,lowerCamelCase__     :			Dict="gelu"					,lowerCamelCase__     :			str=0.1					,lowerCamelCase__     :			List[str]=0.1					,lowerCamelCase__     :			Optional[int]=10					,lowerCamelCase__     :			Optional[Any]=0.0_2					,lowerCamelCase__     :			int=None					,lowerCamelCase__     :			Tuple=2					,):
									UpperCAmelCase__    =							parent
									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__    =							encoder_stride

									# in ViT, 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     :			List[str]    ):
									UpperCAmelCase__    =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

									UpperCAmelCase__    =							self.get_config()

									return config, pixel_values, labels







					def 	__lowerCAmelCase		(       self     :			Any    ):
									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=lowerCamelCase__					,initializer_range=self.initializer_range					,encoder_stride=self.encoder_stride					,)







					def 	__lowerCAmelCase		(       self     :			Dict					,lowerCamelCase__     :			Optional[int]					,lowerCamelCase__     :			List[str]					,lowerCamelCase__     :			Tuple    ):
									UpperCAmelCase__    =							ViTModel(config=lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()
									UpperCAmelCase__    =							model(lowerCamelCase__    )
									self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)    )







					def 	__lowerCAmelCase		(       self     :			Union[str, Any]					,lowerCamelCase__     :			Dict					,lowerCamelCase__     :			List[Any]					,lowerCamelCase__     :			Union[str, Any]    ):
									UpperCAmelCase__    =							ViTForMaskedImageModeling(config=lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()
									UpperCAmelCase__    =							model(lowerCamelCase__    )
									self.parent.assertEqual(
									    result.reconstruction.shape					,(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

									# test greyscale images
									UpperCAmelCase__    =							1
									UpperCAmelCase__    =							ViTForMaskedImageModeling(lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()

									UpperCAmelCase__    =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
									UpperCAmelCase__    =							model(lowerCamelCase__    )
									self.parent.assertEqual(result.reconstruction.shape					,(self.batch_size, 1, self.image_size, self.image_size)    )







					def 	__lowerCAmelCase		(       self     :			Any					,lowerCamelCase__     :			str					,lowerCamelCase__     :			str					,lowerCamelCase__     :			Optional[Any]    ):
									UpperCAmelCase__    =							self.type_sequence_label_size
									UpperCAmelCase__    =							ViTForImageClassification(lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()
									UpperCAmelCase__    =							model(lowerCamelCase__					,labels=lowerCamelCase__    )
									self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)    )

									# test greyscale images
									UpperCAmelCase__    =							1
									UpperCAmelCase__    =							ViTForImageClassification(lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()

									UpperCAmelCase__    =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
									UpperCAmelCase__    =							model(lowerCamelCase__    )
									self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)    )







					def 	__lowerCAmelCase		(       self     :			List[str]    ):
									UpperCAmelCase__    =							self.prepare_config_and_inputs()
									(
									    (
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			
									)    =							config_and_inputs
									UpperCAmelCase__    =							{'pixel_values': pixel_values}
									return config, inputs_dict






@require_torch
class        snake_case (				__UpperCAmelCase   ,					__UpperCAmelCase   ,					unittest.TestCase       ):




					"""simple docstring"""

					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 	__lowerCAmelCase		(       self     :			str    ):
									UpperCAmelCase__    =							ViTModelTester(self    )
									UpperCAmelCase__    =							ConfigTester(self					,config_class=lowerCamelCase__					,has_text_modality=lowerCamelCase__					,hidden_size=37    )







					def 	__lowerCAmelCase		(       self     :			int    ):
									self.config_tester.run_common_tests()







					@unittest.skip(reason='ViT does not use inputs_embeds'    )
					def 	__lowerCAmelCase		(       self     :			Dict    ):
									pass







					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(lowerCamelCase__    )
													self.assertIsInstance(model.get_input_embeddings()					,(nn.Module)    )
													UpperCAmelCase__    =							model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__					,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(lowerCamelCase__    )
													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]					,lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			str    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			List[Any]    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			int    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__    )







					@slow
					def 	__lowerCAmelCase		(       self     :			Any    ):
									for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCAmelCase__    =							ViTModel.from_pretrained(lowerCamelCase__    )
													self.assertIsNotNone(lowerCamelCase__    )






def 						a_		(      ):
				UpperCAmelCase__    =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
				return image







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




					"""simple docstring"""

					@cached_property
					def 	__lowerCAmelCase		(       self     :			Union[str, Any]    ):
									return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'    ) if is_vision_available() else None







					@slow
					def 	__lowerCAmelCase		(       self     :			Optional[Any]    ):
									UpperCAmelCase__    =							ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'    ).to(lowerCamelCase__    )

									UpperCAmelCase__    =							self.default_image_processor
									UpperCAmelCase__    =							prepare_img()
									UpperCAmelCase__    =							image_processor(images=lowerCamelCase__					,return_tensors='pt'    ).to(lowerCamelCase__    )

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

									# verify the logits
									UpperCAmelCase__    =							torch.Size((1, 1_000)    )
									self.assertEqual(outputs.logits.shape					,lowerCamelCase__    )

									UpperCAmelCase__    =							torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]    ).to(lowerCamelCase__    )

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







					@slow
					def 	__lowerCAmelCase		(       self     :			List[str]    ):
									# 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__    =							ViTModel.from_pretrained('facebook/dino-vits8'    ).to(lowerCamelCase__    )

									UpperCAmelCase__    =							ViTImageProcessor.from_pretrained('facebook/dino-vits8'					,size=480    )
									UpperCAmelCase__    =							prepare_img()
									UpperCAmelCase__    =							image_processor(images=lowerCamelCase__					,return_tensors='pt'    )
									UpperCAmelCase__    =							inputs.pixel_values.to(lowerCamelCase__    )

									# forward pass
									with torch.no_grad():
													UpperCAmelCase__    =							model(lowerCamelCase__					,interpolate_pos_encoding=lowerCamelCase__    )

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

									UpperCAmelCase__    =							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(lowerCamelCase__    )

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







					@slow
					@require_accelerate
					@require_torch_gpu
					def 	__lowerCAmelCase		(       self     :			Tuple    ):
									UpperCAmelCase__    =							ViTModel.from_pretrained('facebook/dino-vits8'					,torch_dtype=torch.floataa					,device_map='auto'    )
									UpperCAmelCase__    =							self.default_image_processor

									UpperCAmelCase__    =							prepare_img()
									UpperCAmelCase__    =							image_processor(images=lowerCamelCase__					,return_tensors='pt'    )
									UpperCAmelCase__    =							inputs.pixel_values.to(lowerCamelCase__    )

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




"""simple docstring"""






import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


lowerCAmelCase__						:						Optional[int]		  =			False
class        snake_case (				unittest.TestCase       ):




					"""simple docstring"""

					pass






@slow
@require_torch_gpu
class        snake_case (				unittest.TestCase       ):




					"""simple docstring"""

					def 	__lowerCAmelCase		(       self     :			Tuple    ):
									UpperCAmelCase__    =							VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion'    )
									pipe.to(lowerCamelCase__    )
									pipe.set_progress_bar_config(disable=lowerCamelCase__    )

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

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

									assert image.shape == (1, 512, 512, 3)
									UpperCAmelCase__    =							np.array([0.0_4_4_1, 0.0_4_6_9, 0.0_5_0_7, 0.0_5_7_5, 0.0_6_3_2, 0.0_6_5_0, 0.0_8_6_5, 0.0_9_0_9, 0.0_9_4_5]    )

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



