




'''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 argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand
def 		snake_case_   (UpperCamelCase	:  Namespace				):





							'''simple docstring'''


							return ConvertCommand(
							    args.model_type			,						args.tf_checkpoint			,						args.pytorch_dump_output			,						args.config			,						args.finetuning_task_name				)


_snake_case				:				List[Any]       							=		'\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n'

class       A   (		_a   ):







							@staticmethod
							def        __lowerCAmelCase      (  lowerCAmelCase_	:  ArgumentParser )      ->							Any:


														"""simple docstring"""


														_a     					=    parser.add_parser(
														    '''convert'''							,      help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'''							,      )
														train_parser.add_argument('''--model_type'''							,      type=lowerCAmelCase_							,      required=lowerCAmelCase_							,      help='''Model\'s type.''' )
														train_parser.add_argument(
														    '''--tf_checkpoint'''							,      type=lowerCAmelCase_							,      required=lowerCAmelCase_							,      help='''TensorFlow checkpoint path or folder.''' )
														train_parser.add_argument(
														    '''--pytorch_dump_output'''							,      type=lowerCAmelCase_							,      required=lowerCAmelCase_							,      help='''Path to the PyTorch saved model output.''' )
														train_parser.add_argument('''--config'''							,      type=lowerCAmelCase_							,      default=''''''							,      help='''Configuration file path or folder.''' )
														train_parser.add_argument(
														    '''--finetuning_task_name'''							,      type=lowerCAmelCase_							,      default=lowerCAmelCase_							,      help='''Optional fine-tuning task name if the TF model was a finetuned model.'''							,      )
														train_parser.set_defaults(func=lowerCAmelCase_ )







							def __init__(  self	:  int							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      *lowerCAmelCase_	:  Optional[Any]							,      )      ->							List[str]:


														"""simple docstring"""


														_a     					=    logging.get_logger('''transformers-cli/converting''' )

														self._logger.info(F'Loading model {model_type}' )
														_a     					=    model_type
														_a     					=    tf_checkpoint
														_a     					=    pytorch_dump_output
														_a     					=    config
														_a     					=    finetuning_task_name







							def        __lowerCAmelCase      (  self	:  Dict )      ->							Dict:


														"""simple docstring"""


														if self._model_type == "albert":
																					try:
																												from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
																												    convert_tf_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "bert":
																					try:
																												from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
																												    convert_tf_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "funnel":
																					try:
																												from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
																												    convert_tf_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "t5":
																					try:
																												from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "gpt":
																					from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
																					    convert_openai_checkpoint_to_pytorch,
																					)

																					convert_openai_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "transfo_xl":
																					try:
																												from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
																												    convert_transfo_xl_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					if "ckpt" in self._tf_checkpoint.lower():
																												_a     					=    self._tf_checkpoint
																												_a     					=    ''''''
																					else:
																												_a     					=    self._tf_checkpoint
																												_a     					=    ''''''
																					convert_transfo_xl_checkpoint_to_pytorch(
																					    lowerCAmelCase_							,      self._config							,      self._pytorch_dump_output							,      lowerCAmelCase_ )
														elif self._model_type == "gpt2":
																					try:
																												from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
																												    convert_gpta_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "xlnet":
																					try:
																												from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
																												    convert_xlnet_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_xlnet_checkpoint_to_pytorch(
																					    self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output							,      self._finetuning_task_name )
														elif self._model_type == "xlm":
																					from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
																					    convert_xlm_checkpoint_to_pytorch,
																					)

																					convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint							,      self._pytorch_dump_output )
														elif self._model_type == "lxmert":
																					from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
																					    convert_lxmert_checkpoint_to_pytorch,
																					)

																					convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint							,      self._pytorch_dump_output )
														elif self._model_type == "rembert":
																					from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
																					    convert_rembert_tf_checkpoint_to_pytorch,
																					)

																					convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														else:
																					raise ValueError(
																					    '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )

