

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

				def __init__(	self							:				int     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				Dict=1_3     ,  UpperCamelCase__							:				Dict=6_4     ,  UpperCamelCase__							:				Optional[Any]=2     ,  UpperCamelCase__							:				Dict=3     ,  UpperCamelCase__							:				Optional[Any]=True     ,  UpperCamelCase__							:				List[Any]=True     ,  UpperCamelCase__							:				List[Any]=3_2     ,  UpperCamelCase__							:				int=5     ,  UpperCamelCase__							:				int=4     ,  UpperCamelCase__							:				str=3_7     ,  UpperCamelCase__							:				Optional[int]="gelu"     ,  UpperCamelCase__							:				int=0.1     ,  UpperCamelCase__							:				Optional[int]=0.1     ,  UpperCamelCase__							:				Any=1_0     ,  UpperCamelCase__							:				List[Any]=0.02     ,  UpperCamelCase__							:				Tuple=[1, 1_6, 4, 4]     ,  UpperCamelCase__							:				List[str]=None     ,  )->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			parent
						__lowerCAmelCase:       Optional[Any]          =			batch_size
						__lowerCAmelCase:       Tuple          =			image_size
						__lowerCAmelCase:       Any          =			patch_size
						__lowerCAmelCase:       List[Any]          =			num_channels
						__lowerCAmelCase:       Any          =			is_training
						__lowerCAmelCase:       List[str]          =			use_labels
						__lowerCAmelCase:       str          =			hidden_size
						__lowerCAmelCase:       List[str]          =			num_hidden_layers
						__lowerCAmelCase:       Any          =			num_attention_heads
						__lowerCAmelCase:       str          =			intermediate_size
						__lowerCAmelCase:       Union[str, Any]          =			hidden_act
						__lowerCAmelCase:       Any          =			hidden_dropout_prob
						__lowerCAmelCase:       Any          =			attention_probs_dropout_prob
						__lowerCAmelCase:       List[Any]          =			type_sequence_label_size
						__lowerCAmelCase:       Any          =			initializer_range
						__lowerCAmelCase:       str          =			scope
						__lowerCAmelCase:       Tuple          =			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
						__lowerCAmelCase:       str          =			(self.image_size // 3_2) ** 2
						__lowerCAmelCase:       Optional[Any]          =			num_patches + 1

				def 							lowercase_     (	self							:				Dict)->   Optional[Any]:



						'''simple docstring'''


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

						__lowerCAmelCase:       str          =			None
						if self.use_labels:
								__lowerCAmelCase:       Tuple          =			ids_tensor([self.batch_size]     ,  self.type_sequence_label_size)

						__lowerCAmelCase:       Any          =			self.get_config()

						return config, pixel_values, labels

				def 							lowercase_     (	self							:				int)->   str:



						'''simple docstring'''


						__lowerCAmelCase:       Optional[Any]          =			{
						    "global_padding": "same",
						    "layer_type": "bottleneck",
						    "depths": [3, 4, 9],
						    "out_features": ["stage1", "stage2", "stage3"],
						    "embedding_dynamic_padding": True,
						    "hidden_sizes": [4, 8, 1_6, 3_2],
						    "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=UpperCamelCase__     ,  initializer_range=self.initializer_range     ,  backbone_featmap_shape=self.backbone_featmap_shape     ,  backbone_config=UpperCamelCase__     ,  )

				def 							lowercase_     (	self							:				Optional[Any]     ,  UpperCamelCase__							:				Dict     ,  UpperCamelCase__							:				Any     ,  UpperCamelCase__							:				Tuple)->   Union[str, Any]:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Any     ,  UpperCamelCase__							:				Union[str, Any]     ,  UpperCamelCase__							:				List[Any]     ,  UpperCamelCase__							:				Tuple)->   Any:



						'''simple docstring'''


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




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



						'''simple docstring'''


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





@require_torch
class     snake_case	(				__snake_case,							__snake_case,							unittest.TestCase  ):
				SCREAMING_SNAKE_CASE_   :      Tuple         =  (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_   :      List[str]         =  (
				    {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification}
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_   :      List[Any]         =  False
				SCREAMING_SNAKE_CASE_   :      Optional[int]         =  False
				SCREAMING_SNAKE_CASE_   :      int         =  False

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



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			ViTHybridModelTester(self)
						__lowerCAmelCase:       Any          =			ConfigTester(self     ,  config_class=UpperCamelCase__     ,  has_text_modality=UpperCamelCase__     ,  hidden_size=3_7)

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



						'''simple docstring'''


						self.config_tester.run_common_tests()

				@unittest.skip(reason="ViT does not use inputs_embeds")
				def 							lowercase_     (	self							:				List[Any])->   Union[str, Any]:



						'''simple docstring'''


						pass

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



						'''simple docstring'''


						__lowerCAmelCase      , __lowerCAmelCase:       List[str]          =			self.model_tester.prepare_config_and_inputs_for_common()

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

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



						'''simple docstring'''


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

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

								__lowerCAmelCase:       str          =			["pixel_values"]
								self.assertListEqual(arg_names[:1]     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				List[Any])->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase__)

				def 							lowercase_     (	self							:				Dict)->   List[Any]:



						'''simple docstring'''


						__lowerCAmelCase:       int          =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__)

				def 							lowercase_     (	self							:				int)->   int:



						'''simple docstring'''


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

						__lowerCAmelCase:       List[str]          =			_config_zero_init(UpperCamelCase__)
						for model_class in self.all_model_classes:
								__lowerCAmelCase:       Optional[int]          =			model_class(config=UpperCamelCase__)
								# Skip the check for the backbone
								for name, module in model.named_modules():
										if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
												__lowerCAmelCase:       Dict          =			[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 							lowercase_     (	self							:				str)->   Optional[Any]:



						'''simple docstring'''


						for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:       List[Any]          =			ViTHybridModel.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)






def     a__    (    )	->	List[Any]:
		__lowerCAmelCase:       str          =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
		return image





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

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



						'''simple docstring'''


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

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



						'''simple docstring'''


						__lowerCAmelCase:       Optional[Any]          =			ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
						    UpperCamelCase__)

						__lowerCAmelCase:       Tuple          =			self.default_image_processor
						__lowerCAmelCase:       Any          =			prepare_img()
						__lowerCAmelCase:       Dict          =			image_processor(images=UpperCamelCase__     ,  return_tensors="pt").to(UpperCamelCase__)

						# forward pass
						with torch.no_grad():
								__lowerCAmelCase:       Optional[int]          =			model(**UpperCamelCase__)

						# verify the logits
						__lowerCAmelCase:       Union[str, Any]          =			torch.Size((1, 1_0_0_0))
						self.assertEqual(outputs.logits.shape     ,  UpperCamelCase__)

						__lowerCAmelCase:       Any          =			torch.tensor([-1.9090, -0.4993, -0.2389]).to(UpperCamelCase__)

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




				@slow
				@require_accelerate
				def 							lowercase_     (	self							:				List[Any])->   Dict:



						'''simple docstring'''


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

						__lowerCAmelCase:       List[Any]          =			prepare_img()

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

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





"""simple docstring"""


from __future__ import annotations






def     a__    (    __SCREAMING_SNAKE_CASE )	->	bool:
		__lowerCAmelCase:       Tuple          =			str(__SCREAMING_SNAKE_CASE )
		return len(__SCREAMING_SNAKE_CASE ) == 9 and set(__SCREAMING_SNAKE_CASE ) == set("123456789" )






def     a__    (    )	->	int | None:
		for base_num in range(9_9_9_9					, 4_9_9_9					, -1 ):
				__lowerCAmelCase:       Tuple          =			1_0_0_0_0_2 * base_num
				if is_9_pandigital(__SCREAMING_SNAKE_CASE ):
						return candidate

		for base_num in range(3_3_3					, 9_9					, -1 ):
				__lowerCAmelCase:       int          =			1_0_0_2_0_0_3 * base_num
				if is_9_pandigital(__SCREAMING_SNAKE_CASE ):
						return candidate

		return None


if __name__ == "__main__":
			print(F'''{solution() = }''')


