



'''simple docstring'''
import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
					import jax

					from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel






class 							lowercase__     (							unittest.TestCase       ):





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







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

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





	def      UpperCamelCase_  (			self :    Optional[Any]			):







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

							_UpperCamelCase :		Tuple			    =       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			,)

							return config, pixel_values





	def      UpperCamelCase_  (			self :    str			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Dict			    =       FlaxViTModel(config=lowerCamelCase__			)
							_UpperCamelCase :		List[str]			    =       model(lowerCamelCase__			)
							# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
							_UpperCamelCase :		int			    =       (self.image_size, self.image_size)
							_UpperCamelCase :		str			    =       (self.patch_size, self.patch_size)
							_UpperCamelCase :		str			    =       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
							self.parent.assertEqual(result.last_hidden_state.shape			,(self.batch_size, num_patches + 1, self.hidden_size)			)





	def      UpperCamelCase_  (			self :    Tuple			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       self.type_sequence_label_size
							_UpperCamelCase :		Optional[int]			    =       FlaxViTForImageClassification(config=lowerCamelCase__			)
							_UpperCamelCase :		str			    =       model(lowerCamelCase__			)
							self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.type_sequence_label_size)			)

							# test greyscale images
							_UpperCamelCase :		Optional[int]			    =       1
							_UpperCamelCase :		Union[str, Any]			    =       FlaxViTForImageClassification(lowerCamelCase__			)

							_UpperCamelCase :		Optional[Any]			    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
							_UpperCamelCase :		Optional[Any]			    =       model(lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		Dict			    =       self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      
							) :		Tuple			    =       config_and_inputs
							_UpperCamelCase :		Optional[Any]			    =       {'pixel_values': pixel_values}
							return config, inputs_dict






@require_flax
class 							lowercase__     (							lowercase						, unittest.TestCase       ):
	lowercase__      =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       FlaxViTModelTester(self			)
							_UpperCamelCase :		Any			    =       ConfigTester(self			,config_class=lowerCamelCase__			,has_text_modality=lowerCamelCase__			,hidden_size=37			)





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







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





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Tuple			):







							'''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 :		Optional[int]			    =       model_class(lowerCamelCase__			)
													_UpperCamelCase :		Optional[Any]			    =       inspect.signature(model.__call__			)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													_UpperCamelCase :		Tuple			    =       [*signature.parameters.keys()]

													_UpperCamelCase :		str			    =       ['pixel_values']
													self.assertListEqual(arg_names[:1]			,lowerCamelCase__			)





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







							'''simple docstring'''
							_UpperCamelCase    ,      _UpperCamelCase :		Optional[Any]			    =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													with self.subTest(model_class.__name__			):
																			_UpperCamelCase :		Any			    =       self._prepare_for_class(lowerCamelCase__			,lowerCamelCase__			)
																			_UpperCamelCase :		List[Any]			    =       model_class(lowerCamelCase__			)

																			@jax.jit
																			def model_jitted(lowerCamelCase__ :    Any			,**lowerCamelCase__ :    Optional[int]			):
																									return model(pixel_values=lowerCamelCase__			,**lowerCamelCase__			)

																			with self.subTest('JIT Enabled'			):
																									_UpperCamelCase :		List[Any]			    =       model_jitted(**lowerCamelCase__			).to_tuple()

																			with self.subTest('JIT Disabled'			):
																									with jax.disable_jit():
																															_UpperCamelCase :		int			    =       model_jitted(**lowerCamelCase__			).to_tuple()

																			self.assertEqual(len(lowerCamelCase__			)			,len(lowerCamelCase__			)			)
																			for jitted_output, output in zip(lowerCamelCase__			,lowerCamelCase__			):
																									self.assertEqual(jitted_output.shape			,output.shape			)





	@slow
	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							for model_class_name in self.all_model_classes:
													_UpperCamelCase :		Dict			    =       model_class_name.from_pretrained('google/vit-base-patch16-224'			)
													_UpperCamelCase :		Any			    =       model(np.ones((1, 3, 224, 224)			)			)
													self.assertIsNotNone(lowerCamelCase__			)







'''simple docstring'''
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import MobileViTImageProcessor






class 							lowercase__     (							unittest.TestCase       ):





	def __init__(			self :    List[str]			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    Tuple=7			,lowerCamelCase__ :    Tuple=3			,lowerCamelCase__ :    Any=18			,lowerCamelCase__ :    Tuple=30			,lowerCamelCase__ :    Tuple=400			,lowerCamelCase__ :    int=True			,lowerCamelCase__ :    int=None			,lowerCamelCase__ :    str=True			,lowerCamelCase__ :    str=None			,lowerCamelCase__ :    Any=True			,):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       size if size is not None else {'shortest_edge': 20}
							_UpperCamelCase :		int			    =       crop_size if crop_size is not None else {'height': 18, 'width': 18}
							_UpperCamelCase :		List[str]			    =       parent
							_UpperCamelCase :		List[Any]			    =       batch_size
							_UpperCamelCase :		int			    =       num_channels
							_UpperCamelCase :		Optional[int]			    =       image_size
							_UpperCamelCase :		List[Any]			    =       min_resolution
							_UpperCamelCase :		List[str]			    =       max_resolution
							_UpperCamelCase :		int			    =       do_resize
							_UpperCamelCase :		Optional[int]			    =       size
							_UpperCamelCase :		str			    =       do_center_crop
							_UpperCamelCase :		List[Any]			    =       crop_size
							_UpperCamelCase :		Optional[Any]			    =       do_flip_channel_order





	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							return {
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							    "do_flip_channel_order": self.do_flip_channel_order,
							}






@require_torch
@require_vision
class 							lowercase__     (							lowercase						, unittest.TestCase       ):
	lowercase__      =      MobileViTImageProcessor if is_vision_available() else None





	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       MobileViTImageProcessingTester(self			)





	@property
	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							return self.image_processor_tester.prepare_image_processor_dict()





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.image_processing_class(**self.image_processor_dict			)
							self.assertTrue(hasattr(lowerCamelCase__			,'do_resize'			)			)
							self.assertTrue(hasattr(lowerCamelCase__			,'size'			)			)
							self.assertTrue(hasattr(lowerCamelCase__			,'do_center_crop'			)			)
							self.assertTrue(hasattr(lowerCamelCase__			,'center_crop'			)			)
							self.assertTrue(hasattr(lowerCamelCase__			,'do_flip_channel_order'			)			)





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       self.image_processing_class.from_dict(self.image_processor_dict			)
							self.assertEqual(image_processor.size			,{'shortest_edge': 20}			)
							self.assertEqual(image_processor.crop_size			,{'height': 18, 'width': 18}			)

							_UpperCamelCase :		Union[str, Any]			    =       self.image_processing_class.from_dict(self.image_processor_dict			,size=42			,crop_size=84			)
							self.assertEqual(image_processor.size			,{'shortest_edge': 42}			)
							self.assertEqual(image_processor.crop_size			,{'height': 84, 'width': 84}			)





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							pass





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							# Initialize image_processing
							_UpperCamelCase :		int			    =       self.image_processing_class(**self.image_processor_dict			)
							# create random PIL images
							_UpperCamelCase :		int			    =       prepare_image_inputs(self.image_processor_tester			,equal_resolution=lowerCamelCase__			)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__			,Image.Image			)

							# Test not batched input
							_UpperCamelCase :		Optional[Any]			    =       image_processing(image_inputs[0]			,return_tensors='pt'			).pixel_values
							self.assertEqual(
							    encoded_images.shape			,(
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size['height'],
							        self.image_processor_tester.crop_size['width'],
							    )			,)

							# Test batched
							_UpperCamelCase :		int			    =       image_processing(lowerCamelCase__			,return_tensors='pt'			).pixel_values
							self.assertEqual(
							    encoded_images.shape			,(
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size['height'],
							        self.image_processor_tester.crop_size['width'],
							    )			,)





	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							# Initialize image_processing
							_UpperCamelCase :		int			    =       self.image_processing_class(**self.image_processor_dict			)
							# create random numpy tensors
							_UpperCamelCase :		List[Any]			    =       prepare_image_inputs(self.image_processor_tester			,equal_resolution=lowerCamelCase__			,numpify=lowerCamelCase__			)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__			,np.ndarray			)

							# Test not batched input
							_UpperCamelCase :		Optional[int]			    =       image_processing(image_inputs[0]			,return_tensors='pt'			).pixel_values
							self.assertEqual(
							    encoded_images.shape			,(
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size['height'],
							        self.image_processor_tester.crop_size['width'],
							    )			,)

							# Test batched
							_UpperCamelCase :		List[str]			    =       image_processing(lowerCamelCase__			,return_tensors='pt'			).pixel_values
							self.assertEqual(
							    encoded_images.shape			,(
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size['height'],
							        self.image_processor_tester.crop_size['width'],
							    )			,)





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							# Initialize image_processing
							_UpperCamelCase :		Optional[Any]			    =       self.image_processing_class(**self.image_processor_dict			)
							# create random PyTorch tensors
							_UpperCamelCase :		str			    =       prepare_image_inputs(self.image_processor_tester			,equal_resolution=lowerCamelCase__			,torchify=lowerCamelCase__			)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__			,torch.Tensor			)

							# Test not batched input
							_UpperCamelCase :		str			    =       image_processing(image_inputs[0]			,return_tensors='pt'			).pixel_values
							self.assertEqual(
							    encoded_images.shape			,(
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size['height'],
							        self.image_processor_tester.crop_size['width'],
							    )			,)

							# Test batched
							_UpperCamelCase :		Optional[int]			    =       image_processing(lowerCamelCase__			,return_tensors='pt'			).pixel_values
							self.assertEqual(
							    encoded_images.shape			,(
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size['height'],
							        self.image_processor_tester.crop_size['width'],
							    )			,)


