






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


			def __init__(				self					:  Any       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Tuple=13       ,	snake_case__					:  Any=64       ,	snake_case__					:  Optional[Any]=2       ,	snake_case__					:  List[str]=3       ,	snake_case__					:  List[Any]=True       ,	snake_case__					:  Optional[int]=True       ,	snake_case__					:  str=32       ,	snake_case__					:  Dict=5       ,	snake_case__					:  Optional[Any]=4       ,	snake_case__					:  Any=37       ,	snake_case__					:  int="gelu"       ,	snake_case__					:  Optional[Any]=0.1       ,	snake_case__					:  str=0.1       ,	snake_case__					:  List[str]=10       ,	snake_case__					:  List[str]=0.02       ,	snake_case__					:  List[Any]=[1, 16, 4, 4]       ,	snake_case__					:  Tuple=None       ,	):
										lowerCamelCase_	:							str									=parent
										lowerCamelCase_	:							List[str]									=batch_size
										lowerCamelCase_	:							List[Any]									=image_size
										lowerCamelCase_	:							List[Any]									=patch_size
										lowerCamelCase_	:							str									=num_channels
										lowerCamelCase_	:							List[Any]									=is_training
										lowerCamelCase_	:							Tuple									=use_labels
										lowerCamelCase_	:							List[str]									=hidden_size
										lowerCamelCase_	:							Tuple									=num_hidden_layers
										lowerCamelCase_	:							Union[str, Any]									=num_attention_heads
										lowerCamelCase_	:							str									=intermediate_size
										lowerCamelCase_	:							Tuple									=hidden_act
										lowerCamelCase_	:							Optional[int]									=hidden_dropout_prob
										lowerCamelCase_	:							Union[str, Any]									=attention_probs_dropout_prob
										lowerCamelCase_	:							List[Any]									=type_sequence_label_size
										lowerCamelCase_	:							int									=initializer_range
										lowerCamelCase_	:							str									=scope
										lowerCamelCase_	:							List[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
										lowerCamelCase_	:							str									=(self.image_size // 32) ** 2
										lowerCamelCase_	:							Optional[Any]									=num_patches + 1


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							str									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

										lowerCamelCase_	:							Any									=self.get_config()

										return config, pixel_values, labels


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							Any									={
										    "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=snake_case__       ,	initializer_range=self.initializer_range       ,	backbone_featmap_shape=self.backbone_featmap_shape       ,	backbone_config=snake_case__       ,	)


			def      UpperCAmelCase__						(				self					:  Optional[Any]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  int       ,	snake_case__					:  int ):
										lowerCamelCase_	:							List[str]									=ViTHybridModel(config=snake_case__ )
										model.to(snake_case__ )
										model.eval()
										lowerCamelCase_	:							List[str]									=model(snake_case__ )
										self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size) )


			def      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  List[str]       ,	snake_case__					:  Any       ,	snake_case__					:  int ):
										lowerCamelCase_	:							str									=self.type_sequence_label_size
										lowerCamelCase_	:							Tuple									=ViTHybridForImageClassification(snake_case__ )
										model.to(snake_case__ )
										model.eval()
										lowerCamelCase_	:							Optional[int]									=model(snake_case__       ,	labels=snake_case__ )
										self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.type_sequence_label_size) )


			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										lowerCamelCase_	:							str									=self.prepare_config_and_inputs()
										lowerCamelCase_							,						lowerCamelCase_							,						lowerCamelCase_	:							List[Any]									=config_and_inputs
										lowerCamelCase_	:							List[str]									={"pixel_values": pixel_values}
										return config, inputs_dict







@require_torch
class        lowercase__      (							snake_case__,       snake_case__,       unittest.TestCase       ):
			_UpperCAmelCase		:Union[str, Any]             =			(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
			_UpperCAmelCase		:List[str]             =			(
			    {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
			    if is_torch_available()
			    else {}
			)
			_UpperCAmelCase		:str             =			False
			_UpperCAmelCase		:str             =			False
			_UpperCAmelCase		:int             =			False


			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_	:							Any									=ViTHybridModelTester(self )
										lowerCamelCase_	:							Optional[Any]									=ConfigTester(self       ,	config_class=snake_case__       ,	has_text_modality=snake_case__       ,	hidden_size=37 )


			def      UpperCAmelCase__						(				self					:  str ):
										self.config_tester.run_common_tests()


			@unittest.skip(reason="ViT does not use inputs_embeds" )
			def      UpperCAmelCase__						(				self					:  Tuple ):
										pass


			def      UpperCAmelCase__						(				self					:  List[str] ):
										lowerCamelCase_							,						lowerCamelCase_	:							Optional[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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


			def      UpperCAmelCase__						(				self					:  str ):
										lowerCamelCase_							,						lowerCamelCase_	:							str									=self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowerCamelCase_	:							Union[str, Any]									=["pixel_values"]
																	self.assertListEqual(arg_names[:1]       ,	snake_case__ )


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							Optional[int]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*snake_case__ )


			def      UpperCAmelCase__						(				self					:  Optional[int] ):
										lowerCamelCase_	:							int									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*snake_case__ )


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_							,						lowerCamelCase_	:							List[str]									=self.model_tester.prepare_config_and_inputs_for_common()

										lowerCamelCase_	:							Tuple									=_config_zero_init(snake_case__ )
										for model_class in self.all_model_classes:
																	lowerCamelCase_	:							List[Any]									=model_class(config=snake_case__ )
																	# 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      UpperCAmelCase__						(				self					:  Any ):
										for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowerCamelCase_	:							List[str]									=ViTHybridModel.from_pretrained(snake_case__ )
																	self.assertIsNotNone(snake_case__ )


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







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


			@cached_property
			def      UpperCAmelCase__						(				self					:  str ):
										return (
										    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] )
										    if is_vision_available()
										    else None
										)


			@slow
			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							List[Any]									=ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(
										    snake_case__ )

										lowerCamelCase_	:							Dict									=self.default_image_processor
										lowerCamelCase_	:							Optional[int]									=prepare_img()
										lowerCamelCase_	:							List[str]									=image_processor(images=snake_case__       ,	return_tensors="pt" ).to(snake_case__ )

										# forward pass
										with torch.no_grad():
																	lowerCamelCase_	:							Tuple									=model(**snake_case__ )

										# verify the logits
										lowerCamelCase_	:							Dict									=torch.Size((1, 1000) )
										self.assertEqual(outputs.logits.shape       ,	snake_case__ )

										lowerCamelCase_	:							Optional[Any]									=torch.tensor([-1.9_090, -0.4_993, -0.2_389] ).to(snake_case__ )

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


			@slow
			@require_accelerate
			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							int									=ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" )
										lowerCamelCase_	:							Union[str, Any]									=ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384"       ,	device_map="auto" )

										lowerCamelCase_	:							List[str]									=prepare_img()

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

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











"""simple docstring"""



def 			_snake_case    (					lowerCamelCase__     :   Optional[Any]  )    ->     Optional[int]:
							if not head:
														return True
							# split the list to two parts
							lowerCamelCase_							,						lowerCamelCase_	:							Union[str, Any]									=head.next, head
							while fast and fast.next:
														lowerCamelCase_	:							Optional[Any]									=fast.next.next
														lowerCamelCase_	:							str									=slow.next
							lowerCamelCase_	:							Tuple									=slow.next
							lowerCamelCase_	:							Any									=None  # Don't forget here! But forget still works!
							# reverse the second part
							lowerCamelCase_	:							List[str]									=None
							while second:
														lowerCamelCase_	:							Any									=second.next
														lowerCamelCase_	:							Union[str, Any]									=node
														lowerCamelCase_	:							Union[str, Any]									=second
														lowerCamelCase_	:							Optional[Any]									=nxt
							# compare two parts
							# second part has the same or one less node
							while node:
														if node.val != head.val:
																					return False
														lowerCamelCase_	:							List[str]									=node.next
														lowerCamelCase_	:							Optional[Any]									=head.next
							return True

def 			_snake_case    (					lowerCamelCase__     :   str  )    ->     Optional[int]:
							if not head or not head.next:
														return True

							# 1. Get the midpoint (slow)
							lowerCamelCase_	:							List[str]									=head
							while fast and fast.next:
														lowerCamelCase_							,						lowerCamelCase_	:							Union[str, Any]									=fast.next.next, slow.next

							# 2. Push the second half into the stack
							lowerCamelCase_	:							List[Any]									=[slow.val]
							while slow.next:
														lowerCamelCase_	:							List[Any]									=slow.next
														stack.append(slow.val  )

							# 3. Comparison
							while stack:
														if stack.pop() != cur.val:
																					return False
														lowerCamelCase_	:							Union[str, Any]									=cur.next

							return True

def 			_snake_case    (					lowerCamelCase__     :   Dict  )    ->     Optional[Any]:
							if not head or not head.next:
														return True
							lowerCamelCase_	:							Union[str, Any]									={}
							lowerCamelCase_	:							List[Any]									=0
							while head:
														if head.val in d:
																					d[head.val].append(lowerCamelCase__  )
														else:
																					lowerCamelCase_	:							List[str]									=[pos]
														lowerCamelCase_	:							Optional[int]									=head.next
														pos += 1
							lowerCamelCase_	:							Union[str, Any]									=pos - 1
							lowerCamelCase_	:							Optional[int]									=0
							for v in d.values():
														if len(lowerCamelCase__  ) % 2 != 0:
																					middle += 1
														else:
																					lowerCamelCase_	:							Optional[Any]									=0
																					for i in range(0    ,						len(lowerCamelCase__  )  ):
																												if v[i] + v[len(lowerCamelCase__  ) - 1 - step] != checksum:
																																			return False
																												step += 1
														if middle > 1:
																					return False
							return True



