


'''simple docstring'''


import inspect
import unittest

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


if is_torch_available():
						import torch
						from torch import nn

						from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
						from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image






class 		lowerCAmelCase__	:




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



					'''simple docstring'''







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

					# in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
					# the number of patches is based on the feature map of the backbone, which by default uses an output stride
					# of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
					_UpperCAmelCase						:				List[str]	    =					(self.image_size // 32) ** 2
					_UpperCAmelCase						:				List[Any]	    =					num_patches + 1




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



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[Any]	    =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

					_UpperCAmelCase						:				List[Any]	    =					None
					if self.use_labels:
							_UpperCAmelCase						:				Tuple	    =					ids_tensor([self.batch_size]		,     self.type_sequence_label_size       )

					_UpperCAmelCase						:				Optional[Any]	    =					self.get_config()

					return config, pixel_values, labels




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



					'''simple docstring'''







					_UpperCAmelCase						:				List[str]	    =					{
					    "global_padding": "same",
					    "layer_type": "bottleneck",
					    "depths": [3, 4, 9],
					    "out_features": ["stage1", "stage2", "stage3"],
					    "embedding_dynamic_padding": True,
					    "hidden_sizes": [4, 8, 16, 32],
					    "num_groups": 2,
					}

					return ViTHybridConfig(
					    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		,     backbone_featmap_shape=self.backbone_featmap_shape		,     backbone_config=lowerCamelCase__		,     )




			def 							lowerCAmelCase__			(						self     :   Optional[int]		,     lowerCamelCase__     :   Union[str, Any]		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   int       )      ->Dict:



					'''simple docstring'''







					_UpperCAmelCase						:				Tuple	    =					ViTHybridModel(config=lowerCamelCase__       )
					model.to(lowerCamelCase__       )
					model.eval()
					_UpperCAmelCase						:				List[str]	    =					model(lowerCamelCase__       )
					self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)       )




			def 							lowerCAmelCase__			(						self     :   List[Any]		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   Dict		,     lowerCamelCase__     :   str       )      ->Dict:



					'''simple docstring'''







					_UpperCAmelCase						:				List[str]	    =					self.type_sequence_label_size
					_UpperCAmelCase						:				Any	    =					ViTHybridForImageClassification(lowerCamelCase__       )
					model.to(lowerCamelCase__       )
					model.eval()
					_UpperCAmelCase						:				Dict	    =					model(lowerCamelCase__		,     labels=lowerCamelCase__       )
					self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)       )






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



					'''simple docstring'''







					_UpperCAmelCase						:				Dict	    =					self.prepare_config_and_inputs()
					_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase						:				Union[str, Any]	    =					config_and_inputs
					_UpperCAmelCase						:				Any	    =					{"pixel_values": pixel_values}
					return config, inputs_dict






@require_torch
class 		lowerCAmelCase__	(	UpperCAmelCase__			,				UpperCAmelCase__			,				unittest.TestCase		):
			lowerCAmelCase       :		str				      =   (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
			lowerCAmelCase       :		Optional[int]				      =   (
			    {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
			    if is_torch_available()
			    else {}
			)
			lowerCAmelCase       :		Optional[int]				      =   False
			lowerCAmelCase       :		str				      =   False
			lowerCAmelCase       :		Tuple				      =   False




			def 							lowerCAmelCase__			(						self     :   Dict       )      ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				Dict	    =					ViTHybridModelTester(self       )
					_UpperCAmelCase						:				int	    =					ConfigTester(self		,     config_class=lowerCamelCase__		,     has_text_modality=lowerCamelCase__		,     hidden_size=37       )




			def 							lowerCAmelCase__			(						self     :   Any       )      ->int:



					'''simple docstring'''







					self.config_tester.run_common_tests()




			@unittest.skip(reason="ViT does not use inputs_embeds"       )
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->List[Any]:



					'''simple docstring'''







					pass




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



					'''simple docstring'''







					_UpperCAmelCase    ,			_UpperCAmelCase						:				Optional[int]	    =					self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							_UpperCAmelCase						:				Tuple	    =					model_class(lowerCamelCase__       )
							self.assertIsInstance(model.get_input_embeddings()		,     (nn.Module)       )
							_UpperCAmelCase						:				List[str]	    =					model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowerCamelCase__		,     nn.Linear       )       )




			def 							lowerCAmelCase__			(						self     :   Any       )      ->Dict:



					'''simple docstring'''







					_UpperCAmelCase    ,			_UpperCAmelCase						:				str	    =					self.model_tester.prepare_config_and_inputs_for_common()

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

							_UpperCAmelCase						:				List[Any]	    =					["pixel_values"]
							self.assertListEqual(arg_names[:1]		,     lowerCamelCase__       )




			def 							lowerCAmelCase__			(						self     :   int       )      ->int:



					'''simple docstring'''







					_UpperCAmelCase						:				Dict	    =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCamelCase__       )




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



					'''simple docstring'''







					_UpperCAmelCase						:				List[str]	    =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__       )




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



					'''simple docstring'''







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

					_UpperCAmelCase						:				List[Any]	    =					_config_zero_init(lowerCamelCase__       )
					for model_class in self.all_model_classes:
							_UpperCAmelCase						:				Union[str, Any]	    =					model_class(config=lowerCamelCase__       )
							# Skip the check for the backbone
							for name, module in model.named_modules():
									if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
											_UpperCAmelCase						:				Any	    =					[F"""{name}.{key}""" for key in module.state_dict().keys()]
											break

							for name, param in model.named_parameters():
									if param.requires_grad:
											if name in backbone_params:
													continue
											self.assertIn(
											    ((param.data.mean() * 1E9).round() / 1E9).item()		,     [0.0, 1.0]		,     msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""		,     )






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



					'''simple docstring'''







					for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_UpperCAmelCase						:				int	    =					ViTHybridModel.from_pretrained(lowerCamelCase__       )
							self.assertIsNotNone(lowerCamelCase__       )






def        __lowerCAmelCase       ():
		_UpperCAmelCase						:				int	    =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
		return image






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




			@cached_property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->Optional[int]:



					'''simple docstring'''







					return (
					    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )
					    if is_vision_available()
					    else None
					)




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



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[Any]	    =					ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]       ).to(
					    lowerCamelCase__       )

					_UpperCAmelCase						:				Union[str, Any]	    =					self.default_image_processor
					_UpperCAmelCase						:				str	    =					prepare_img()
					_UpperCAmelCase						:				Optional[int]	    =					image_processor(images=lowerCamelCase__		,     return_tensors="pt"       ).to(lowerCamelCase__       )

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

					# verify the logits
					_UpperCAmelCase						:				str	    =					torch.Size((1, 10_00)       )
					self.assertEqual(outputs.logits.shape		,     lowerCamelCase__       )

					_UpperCAmelCase						:				Optional[int]	    =					torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]       ).to(lowerCamelCase__       )

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






			@slow
			@require_accelerate
			def 							lowerCAmelCase__			(						self     :   str       )      ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[Any]	    =					ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384"       )
					_UpperCAmelCase						:				Any	    =					ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384"		,     device_map="auto"       )

					_UpperCAmelCase						:				Union[str, Any]	    =					prepare_img()

					_UpperCAmelCase						:				int	    =					image_processor(images=lowerCamelCase__		,     return_tensors="pt"       )
					_UpperCAmelCase						:				Dict	    =					model(**lowerCamelCase__       )
					_UpperCAmelCase						:				int	    =					outputs.logits
					# model predicts one of the 1000 ImageNet classes
					_UpperCAmelCase						:				Union[str, Any]	    =					logits.argmax(-1       ).item()

					self.assertTrue(model.config.idalabel[predicted_class_idx]		,     "tabby, tabby cat"       )







'''simple docstring'''


import math






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		return math.sqrt(__lowerCAmelCase      ) * math.sqrt(__lowerCAmelCase      ) == num






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				int	    =					0
		_UpperCAmelCase						:				Tuple	    =					n
		while left <= right:
				_UpperCAmelCase						:				int	    =					(left + right) // 2
				if mid**2 == n:
						return True
				elif mid**2 > n:
						_UpperCAmelCase						:				str	    =					mid - 1
				else:
						_UpperCAmelCase						:				List[str]	    =					mid + 1
		return False


if __name__ == "__main__":
						import doctest

						doctest.testmod()



