




'''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       A   :







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


														"""simple docstring"""


														_a     					=    parent
														_a     					=    batch_size
														_a     					=    image_size
														_a     					=    patch_size
														_a     					=    num_channels
														_a     					=    is_training
														_a     					=    use_labels
														_a     					=    hidden_size
														_a     					=    num_hidden_layers
														_a     					=    num_attention_heads
														_a     					=    intermediate_size
														_a     					=    hidden_act
														_a     					=    hidden_dropout_prob
														_a     					=    attention_probs_dropout_prob
														_a     					=    type_sequence_label_size
														_a     					=    initializer_range
														_a     					=    scope
														_a     					=    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     					=    (self.image_size // 32) ** 2
														_a     					=    num_patches + 1







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

														_a     					=    None
														if self.use_labels:
																					_a     					=    ids_tensor([self.batch_size]							,      self.type_sequence_label_size )

														_a     					=    self.get_config()

														return config, pixel_values, labels







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							str:


														"""simple docstring"""


														_a     					=    {
														    '''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	:  Dict							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  str )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    ViTHybridModel(config=lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.last_hidden_state.shape							,      (self.batch_size, self.seq_length, self.hidden_size) )







							def        __lowerCAmelCase      (  self	:  Dict							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Union[str, Any] )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.type_sequence_label_size
														_a     					=    ViTHybridForImageClassification(lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()
														_a     					=    model(lowerCAmelCase_							,      labels=lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.type_sequence_label_size) )







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


														"""simple docstring"""


														_a     					=    self.prepare_config_and_inputs()
														_a					,							_a					,							_a     					=    config_and_inputs
														_a     					=    {'''pixel_values''': pixel_values}
														return config, inputs_dict



@require_torch
class       A   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
							lowercase_          =    (
							    {'feature-extraction': ViTHybridModel, 'image-classification': ViTHybridForImageClassification}
							    if is_torch_available()
							    else {}
							)
							lowercase_          =    False
							lowercase_          =    False
							lowercase_          =    False







							def        __lowerCAmelCase      (  self	:  List[str] )      ->							Dict:


														"""simple docstring"""


														_a     					=    ViTHybridModelTester(self )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      has_text_modality=lowerCAmelCase_							,      hidden_size=37 )







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







							@unittest.skip(reason='''ViT does not use inputs_embeds''' )
							def        __lowerCAmelCase      (  self	:  Tuple )      ->							int:


														"""simple docstring"""


														pass







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


														"""simple docstring"""


														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					_a     					=    model_class(lowerCAmelCase_ )
																					self.assertIsInstance(model.get_input_embeddings()							,      (nn.Module) )
																					_a     					=    model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(lowerCAmelCase_							,      nn.Linear ) )







							def        __lowerCAmelCase      (  self	:  int )      ->							str:


														"""simple docstring"""


														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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

																					_a     					=    ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]							,      lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Dict )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							str:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Optional[int] )      ->							Union[str, Any]:


														"""simple docstring"""


														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

														_a     					=    _config_zero_init(lowerCAmelCase_ )
														for model_class in self.all_model_classes:
																					_a     					=    model_class(config=lowerCAmelCase_ )
																					# Skip the check for the backbone
																					for name, module in model.named_modules():
																												if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																																			_a     					=    [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	:  Tuple )      ->							Dict:


														"""simple docstring"""


														for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_a     					=    ViTHybridModel.from_pretrained(lowerCAmelCase_ )
																					self.assertIsNotNone(lowerCAmelCase_ )



def 		snake_case_   ():





							'''simple docstring'''


							_a     					=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
							return image

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







							@cached_property
							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Dict:


														"""simple docstring"""


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







							@slow
							def        __lowerCAmelCase      (  self	:  str )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(
														    lowerCAmelCase_ )

														_a     					=    self.default_image_processor
														_a     					=    prepare_img()
														_a     					=    image_processor(images=lowerCAmelCase_							,      return_tensors='''pt''' ).to(lowerCAmelCase_ )

														# forward pass
														with torch.no_grad():
																					_a     					=    model(**lowerCAmelCase_ )

														# verify the logits
														_a     					=    torch.Size((1, 10_00) )
														self.assertEqual(outputs.logits.shape							,      lowerCAmelCase_ )

														_a     					=    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	:  Any )      ->							Optional[int]:


														"""simple docstring"""


														_a     					=    ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384''' )
														_a     					=    ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''							,      device_map='''auto''' )

														_a     					=    prepare_img()

														_a     					=    image_processor(images=lowerCAmelCase_							,      return_tensors='''pt''' )
														_a     					=    model(**lowerCAmelCase_ )
														_a     					=    outputs.logits
														# model predicts one of the 1000 ImageNet classes
														_a     					=    logits.argmax(-1 ).item()

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







'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_snake_case				:				Dict       							=		logging.get_logger(__name__)

_snake_case				:				Optional[Any]       							=		{
    'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json',
    # See all Nat models at https://huggingface.co/models?filter=nat
}

class       A   (		_a					,_a   ):
							lowercase_          =    'nat'

							lowercase_          =    {
							    'num_attention_heads': 'num_heads',
							    'num_hidden_layers': 'num_layers',
							}







							def __init__(  self	:  List[str]							,      lowerCAmelCase_	:  str=4							,      lowerCAmelCase_	:  Any=3							,      lowerCAmelCase_	:  List[Any]=64							,      lowerCAmelCase_	:  Dict=[3, 4, 6, 5]							,      lowerCAmelCase_	:  Dict=[2, 4, 8, 16]							,      lowerCAmelCase_	:  str=7							,      lowerCAmelCase_	:  Dict=3.0							,      lowerCAmelCase_	:  int=True							,      lowerCAmelCase_	:  Tuple=0.0							,      lowerCAmelCase_	:  str=0.0							,      lowerCAmelCase_	:  List[str]=0.1							,      lowerCAmelCase_	:  int="gelu"							,      lowerCAmelCase_	:  List[str]=0.0_2							,      lowerCAmelCase_	:  str=1e-5							,      lowerCAmelCase_	:  Tuple=0.0							,      lowerCAmelCase_	:  str=None							,      lowerCAmelCase_	:  int=None							,      **lowerCAmelCase_	:  List[Any]							,      )      ->							Any:


														"""simple docstring"""


														super().__init__(**lowerCAmelCase_ )

														_a     					=    patch_size
														_a     					=    num_channels
														_a     					=    embed_dim
														_a     					=    depths
														_a     					=    len(lowerCAmelCase_ )
														_a     					=    num_heads
														_a     					=    kernel_size
														_a     					=    mlp_ratio
														_a     					=    qkv_bias
														_a     					=    hidden_dropout_prob
														_a     					=    attention_probs_dropout_prob
														_a     					=    drop_path_rate
														_a     					=    hidden_act
														_a     					=    layer_norm_eps
														_a     					=    initializer_range
														# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
														# this indicates the channel dimension after the last stage of the model
														_a     					=    int(embed_dim * 2 ** (len(lowerCAmelCase_ ) - 1) )
														_a     					=    layer_scale_init_value
														_a     					=    ['''stem'''] + [F'stage{idx}' for idx in range(1							,      len(lowerCAmelCase_ ) + 1 )]
														_a					,							_a     					=    get_aligned_output_features_output_indices(
														    out_features=lowerCAmelCase_							,      out_indices=lowerCAmelCase_							,      stage_names=self.stage_names )

