


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







   def __init__(			self		,			UpperCamelCase__		,			UpperCamelCase__=13		,			UpperCamelCase__=30		,			UpperCamelCase__=2		,			UpperCamelCase__=3		,			UpperCamelCase__=True		,			UpperCamelCase__=True		,			UpperCamelCase__=32		,			UpperCamelCase__=5		,			UpperCamelCase__=4		,			UpperCamelCase__=37		,			UpperCamelCase__="gelu"		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.1		,			UpperCamelCase__=10		,			UpperCamelCase__=0.02		,			UpperCamelCase__=None		,			UpperCamelCase__=2		,			)			->	Optional[int]:

       '''simple docstring'''




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

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







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

       '''simple docstring'''




       snake_case					:   str         =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

       snake_case					:   List[str]         =					self.get_config()

       return config, pixel_values, labels







   def 				lowerCamelCase	(			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=UpperCamelCase__		,			initializer_range=self.initializer_range		,			encoder_stride=self.encoder_stride		,			)







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

       '''simple docstring'''




       snake_case					:   str         =					ViTModel(config=UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       snake_case					:   Any         =					model(UpperCamelCase__		)
       self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size)		)







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		)			->	int:

       '''simple docstring'''




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

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

       snake_case					:   Dict         =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
       snake_case					:   int         =					model(UpperCamelCase__		)
       self.parent.assertEqual(result.reconstruction.shape		,			(self.batch_size, 1, self.image_size, self.image_size)		)







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		)			->	Dict:

       '''simple docstring'''




       snake_case					:   List[Any]         =					self.type_sequence_label_size
       snake_case					:   int         =					ViTForImageClassification(UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       snake_case					:   Tuple         =					model(UpperCamelCase__		,			labels=UpperCamelCase__		)
       self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.type_sequence_label_size)		)

       # test greyscale images
       snake_case					:   Any         =					1
       snake_case					:   str         =					ViTForImageClassification(UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()

       snake_case					:   Union[str, Any]         =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
       snake_case					:   int         =					model(UpperCamelCase__		)
       self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.type_sequence_label_size)		)







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

       '''simple docstring'''




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







@require_torch
class _lowerCAmelCase       (   snake_case_					,							snake_case_					,							unittest.TestCase			):
   __UpperCAmelCase				: Tuple													=						(
       (
           ViTModel,
           ViTForImageClassification,
           ViTForMaskedImageModeling,
       )
       if is_torch_available()
       else ()
   )
   __UpperCAmelCase				: Any													=						(
       {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
       if is_torch_available()
       else {}
   )
   __UpperCAmelCase				: Optional[int]													=						True

   __UpperCAmelCase				: Any													=						False
   __UpperCAmelCase				: str													=						False
   __UpperCAmelCase				: Tuple													=						False







   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




       snake_case					:   Dict         =					ViTModelTester(self		)
       snake_case					:   str         =					ConfigTester(self		,			config_class=UpperCamelCase__		,			has_text_modality=UpperCamelCase__		,			hidden_size=37		)







   def 				lowerCamelCase	(			self		)			->	Optional[Any]:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   @unittest.skip(reason="ViT does not use inputs_embeds"		)
   def 				lowerCamelCase	(			self		)			->	str:

       '''simple docstring'''




       pass







   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




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

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







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

       '''simple docstring'''




       snake_case      ,snake_case					:   List[Any]         =					self.model_tester.prepare_config_and_inputs_for_common()

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

           snake_case					:   Dict         =					["pixel_values"]
           self.assertListEqual(arg_names[:1]		,			UpperCamelCase__		)







   def 				lowerCamelCase	(			self		)			->	Optional[Any]:

       '''simple docstring'''




       snake_case					:   List[str]         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__		)







   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   List[Any]         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__		)







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

       '''simple docstring'''




       snake_case					:   Any         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)







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

       '''simple docstring'''




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







def 						__lowerCAmelCase		(		)			->		Optional[Any]:


    """simple docstring"""





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







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







   @cached_property
   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




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







   @slow
   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   Union[str, Any]         =					ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"		).to(UpperCamelCase__		)

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

       # forward pass
       with torch.no_grad():
           snake_case					:   List[Any]         =					model(**UpperCamelCase__		)

       # verify the logits
       snake_case					:   List[Any]         =					torch.Size((1, 1000)		)
       self.assertEqual(outputs.logits.shape		,			UpperCamelCase__		)

       snake_case					:   List[str]         =					torch.tensor([-0.2744, 0.8215, -0.0836]		).to(UpperCamelCase__		)

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







   @slow
   def 				lowerCamelCase	(			self		)			->	Optional[Any]:

       '''simple docstring'''




       snake_case					:   Dict         =					ViTModel.from_pretrained("facebook/dino-vits8"		).to(UpperCamelCase__		)

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

       # forward pass
       with torch.no_grad():
           snake_case					:   str         =					model(UpperCamelCase__		,			interpolate_pos_encoding=UpperCamelCase__		)

       # verify the logits
       snake_case					:   Tuple         =					torch.Size((1, 3601, 384)		)
       self.assertEqual(outputs.last_hidden_state.shape		,			UpperCamelCase__		)

       snake_case					:   List[str]         =					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 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   int         =					ViTModel.from_pretrained("facebook/dino-vits8"		,			torch_dtype=torch.floataa		,			device_map="auto"		)
       snake_case					:   Optional[int]         =					self.default_image_processor

       snake_case					:   str         =					prepare_img()
       snake_case					:   List[Any]         =					image_processor(images=UpperCamelCase__		,			return_tensors="pt"		)
       snake_case					:   str         =					inputs.pixel_values.to(UpperCamelCase__		)

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
           snake_case					:   Any         =					model(UpperCamelCase__		)









"""simple docstring"""



import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class _lowerCAmelCase       (   unittest.TestCase			):







   @property
   def 				lowerCamelCase	(			self		)			->	Any:

       '''simple docstring'''




       torch.manual_seed(0		)
       snake_case					:   Tuple         =					UNetaDModel(
           block_out_channels=(32, 64)		,			layers_per_block=2		,			sample_size=32		,			in_channels=3		,			out_channels=3		,			down_block_types=("DownBlock2D", "AttnDownBlock2D")		,			up_block_types=("AttnUpBlock2D", "UpBlock2D")		,			)
       return model







   def 				lowerCamelCase	(			self		)			->	Optional[Any]:

       '''simple docstring'''




       snake_case					:   Optional[Any]         =					self.dummy_uncond_unet
       snake_case					:   str         =					PNDMScheduler()

       snake_case					:   List[Any]         =					PNDMPipeline(unet=UpperCamelCase__		,			scheduler=UpperCamelCase__		)
       pndm.to(UpperCamelCase__		)
       pndm.set_progress_bar_config(disable=UpperCamelCase__		)

       snake_case					:   str         =					torch.manual_seed(0		)
       snake_case					:   Union[str, Any]         =					pndm(generator=UpperCamelCase__		,			num_inference_steps=20		,			output_type="numpy"		).images

       snake_case					:   Optional[int]         =					torch.manual_seed(0		)
       snake_case					:   Any         =					pndm(generator=UpperCamelCase__		,			num_inference_steps=20		,			output_type="numpy"		,			return_dict=UpperCamelCase__		)[0]

       snake_case					:   Optional[Any]         =					image[0, -3:, -3:, -1]
       snake_case					:   List[str]         =					image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case					:   int         =					np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]		)

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







@slow
@require_torch
class _lowerCAmelCase       (   unittest.TestCase			):







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

       '''simple docstring'''




       snake_case					:   Dict         =					"google/ddpm-cifar10-32"

       snake_case					:   Optional[int]         =					UNetaDModel.from_pretrained(UpperCamelCase__		)
       snake_case					:   List[str]         =					PNDMScheduler()

       snake_case					:   Union[str, Any]         =					PNDMPipeline(unet=UpperCamelCase__		,			scheduler=UpperCamelCase__		)
       pndm.to(UpperCamelCase__		)
       pndm.set_progress_bar_config(disable=UpperCamelCase__		)
       snake_case					:   int         =					torch.manual_seed(0		)
       snake_case					:   str         =					pndm(generator=UpperCamelCase__		,			output_type="numpy"		).images

       snake_case					:   Union[str, Any]         =					image[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case					:   Optional[Any]         =					np.array([0.1564, 0.14645, 0.1406, 0.14715, 0.12425, 0.14045, 0.13115, 0.12175, 0.125]		)

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





