

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     A__:
						"""simple docstring"""






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





						def        UpperCamelCase__ (  self  )			->      List[Any]:
									a_		:		Optional[int]      				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

									a_		:		Union[str, Any]      				=      None
									if self.use_labels:
												a_		:		List[Any]      				=      ids_tensor([self.batch_size]   ,	self.type_sequence_label_size  )

									a_		:		Dict      				=      self.get_config()

									return config, pixel_values, labels





						def        UpperCamelCase__ (  self  )			->      List[str]:
									a_		:		Dict      				=      {
									    """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=UpperCamelCase__   ,	initializer_range=self.initializer_range   ,	backbone_featmap_shape=self.backbone_featmap_shape   ,	backbone_config=UpperCamelCase__   ,	)





						def        UpperCamelCase__ (  self   ,	_lowercase   ,	_lowercase   ,	_lowercase  )			->      str:
									a_		:		Tuple      				=      ViTHybridModel(config=UpperCamelCase__  )
									model.to(UpperCamelCase__  )
									model.eval()
									a_		:		Optional[int]      				=      model(UpperCamelCase__  )
									self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)  )





						def        UpperCamelCase__ (  self   ,	_lowercase   ,	_lowercase   ,	_lowercase  )			->      Tuple:
									a_		:		List[Any]      				=      self.type_sequence_label_size
									a_		:		List[Any]      				=      ViTHybridForImageClassification(UpperCamelCase__  )
									model.to(UpperCamelCase__  )
									model.eval()
									a_		:		str      				=      model(UpperCamelCase__   ,	labels=UpperCamelCase__  )
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.type_sequence_label_size)  )





						def        UpperCamelCase__ (  self  )			->      int:
									a_		:		List[str]      				=      self.prepare_config_and_inputs()
									a_				, a_				, a_		:		Optional[Any]      				=      config_and_inputs
									a_		:		Optional[Any]      				=      {"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_torch
class     A__(_a,   _a,   unittest.TestCase				):
						"""simple docstring"""






						_A  :							List[Any]     =       (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
						_A  :							Tuple     =       (
						    {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification}
						    if is_torch_available()
						    else {}
						)
						_A  :							Optional[Any]     =       False
						_A  :							int     =       False
						_A  :							List[str]     =       False





						def        UpperCamelCase__ (  self  )			->      List[str]:
									a_		:		List[str]      				=      ViTHybridModelTester(self  )
									a_		:		Optional[int]      				=      ConfigTester(self   ,	config_class=UpperCamelCase__   ,	has_text_modality=UpperCamelCase__   ,	hidden_size=37  )





						def        UpperCamelCase__ (  self  )			->      Union[str, Any]:
									self.config_tester.run_common_tests()





						@unittest.skip(reason="""ViT does not use inputs_embeds"""  )
						def        UpperCamelCase__ (  self  )			->      Dict:
									pass





						def        UpperCamelCase__ (  self  )			->      Optional[Any]:
									a_				, a_		:		Any      				=      self.model_tester.prepare_config_and_inputs_for_common()

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





						def        UpperCamelCase__ (  self  )			->      Tuple:
									a_				, a_		:		List[str]      				=      self.model_tester.prepare_config_and_inputs_for_common()

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

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





						def        UpperCamelCase__ (  self  )			->      Union[str, Any]:
									a_		:		str      				=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCamelCase__  )





						def        UpperCamelCase__ (  self  )			->      List[Any]:
									a_		:		int      				=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__  )





						def        UpperCamelCase__ (  self  )			->      int:
									a_				, a_		:		List[str]      				=      self.model_tester.prepare_config_and_inputs_for_common()

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







									for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_		:		str      				=      ViTHybridModel.from_pretrained(UpperCamelCase__  )
												self.assertIsNotNone(UpperCamelCase__  )







def  _UpperCAmelCase							(		):

			'''simple docstring'''



			a_		:		Union[str, Any]      				=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
			return image



@require_torch
@require_vision
class     A__(unittest.TestCase				):
						"""simple docstring"""






						@cached_property
						def        UpperCamelCase__ (  self  )			->      Optional[int]:
									return (
									    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )
									    if is_vision_available()
									    else None
									)





						@slow
						def        UpperCamelCase__ (  self  )			->      Tuple:
									a_		:		Any      				=      ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]  ).to(
									    UpperCamelCase__  )

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

									# forward pass
									with torch.no_grad():
												a_		:		Dict      				=      model(**UpperCamelCase__  )

									# verify the logits
									a_		:		List[str]      				=      torch.Size((1, 1_000)  )
									self.assertEqual(outputs.logits.shape   ,	UpperCamelCase__  )

									a_		:		Optional[Any]      				=      torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]  ).to(UpperCamelCase__  )

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





						@slow
						@require_accelerate
						def        UpperCamelCase__ (  self  )			->      str:
									a_		:		Tuple      				=      ViTHybridImageProcessor.from_pretrained("""google/vit-hybrid-base-bit-384"""  )
									a_		:		Optional[Any]      				=      ViTHybridForImageClassification.from_pretrained("""google/vit-hybrid-base-bit-384"""   ,	device_map="""auto"""  )

									a_		:		Dict      				=      prepare_img()

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

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







'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCamelCase	:       Tuple      	=	{"configuration_ibert": ["IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "IBertConfig", "IBertOnnxConfig"]}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _lowerCamelCase	:       Dict      	=	[
       "IBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
       "IBertForMaskedLM",
       "IBertForMultipleChoice",
       "IBertForQuestionAnswering",
       "IBertForSequenceClassification",
       "IBertForTokenClassification",
       "IBertModel",
       "IBertPreTrainedModel",
   ]

if TYPE_CHECKING:
   from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_ibert import (
          IBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          IBertForMaskedLM,
          IBertForMultipleChoice,
          IBertForQuestionAnswering,
          IBertForSequenceClassification,
          IBertForTokenClassification,
          IBertModel,
          IBertPreTrainedModel,
      )

else:
   import sys

   _lowerCamelCase	:       Union[str, Any]      	=	_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

