

import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
      import torch

      from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin






class 						__magic_name__    :


 def __init__(						self		: Optional[int]			,						lowerCAmelCase__		: List[Any]			,						lowerCAmelCase__		: str=None			,						lowerCAmelCase__		: List[str]=None			,						lowerCAmelCase__		: Optional[int]=None			,						lowerCAmelCase__		: Union[str, Any]="resnet50"			,						lowerCAmelCase__		: Union[str, Any]=3			,						lowerCAmelCase__		: Any=3_2			,						lowerCAmelCase__		: str=3			,						lowerCAmelCase__		: Dict=True			,						lowerCAmelCase__		: Dict=True			,						)    ->      List[str]:
   UpperCAmelCase					      =  parent
   UpperCAmelCase					      =  out_indices if out_indices is not None else [4]
   UpperCAmelCase					      =  stage_names
   UpperCAmelCase					      =  out_features
   UpperCAmelCase					      =  backbone
   UpperCAmelCase					      =  batch_size
   UpperCAmelCase					      =  image_size
   UpperCAmelCase					      =  num_channels
   UpperCAmelCase					      =  use_pretrained_backbone
   UpperCAmelCase					      =  is_training


 def _UpperCamelCase	(						self		: Union[str, Any]    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )
   UpperCAmelCase					      =  self.get_config()

   return config, pixel_values


 def _UpperCamelCase	(						self		: Tuple    )    ->      str:
   return TimmBackboneConfig(
       image_size=self.image_size			,						num_channels=self.num_channels			,						out_features=self.out_features			,						out_indices=self.out_indices			,						stage_names=self.stage_names			,						use_pretrained_backbone=self.use_pretrained_backbone			,						backbone=self.backbone			,						)


 def _UpperCamelCase	(						self		: Dict			,						lowerCAmelCase__		: Optional[Any]			,						lowerCAmelCase__		: Any    )    ->      Any:
   UpperCAmelCase					      =  TimmBackbone(config=lowerCAmelCase__    )
   model.to(lowerCAmelCase__    )
   model.eval()
   with torch.no_grad():
     UpperCAmelCase					      =  model(lowerCAmelCase__    )
   self.parent.assertEqual(
       result.feature_map[-1].shape			,						(self.batch_size, model.channels[-1], 1_4, 1_4)			,						)


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






@require_torch
@require_timm
class 						__magic_name__    (       lowercase__	,						lowercase__	,						lowercase__	,						unittest.TestCase    ):
 UpperCAmelCase							    =						(TimmBackbone,) if is_torch_available() else ()
 UpperCAmelCase							    =						{"""feature-extraction""": TimmBackbone} if is_torch_available() else {}
 UpperCAmelCase							    =						False
 UpperCAmelCase							    =						False
 UpperCAmelCase							    =						False
 UpperCAmelCase							    =						False


 def _UpperCamelCase	(						self		: List[str]    )    ->      Any:
   UpperCAmelCase					      =  TimmBackboneModelTester(self    )
   UpperCAmelCase					      =  ConfigTester(self			,						config_class=lowerCAmelCase__			,						has_text_modality=lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: Any    )    ->      Optional[int]:
   self.config_tester.create_and_test_config_to_json_string()
   self.config_tester.create_and_test_config_to_json_file()
   self.config_tester.create_and_test_config_from_and_save_pretrained()
   self.config_tester.create_and_test_config_with_num_labels()
   self.config_tester.check_config_can_be_init_without_params()
   self.config_tester.check_config_arguments_init()


 def _UpperCamelCase	(						self		: List[Any]    )    ->      List[Any]:
   UpperCAmelCase					      =  "resnet18"
   UpperCAmelCase					      =  "microsoft/resnet-18"

   UpperCAmelCase					      =  AutoBackbone.from_pretrained(lowerCAmelCase__			,						use_timm_backbone=lowerCAmelCase__    )
   UpperCAmelCase					      =  AutoBackbone.from_pretrained(lowerCAmelCase__    )

   self.assertEqual(len(timm_model.out_features    )			,						len(transformers_model.out_features    )    )
   self.assertEqual(len(timm_model.stage_names    )			,						len(transformers_model.stage_names    )    )
   self.assertEqual(timm_model.channels			,						transformers_model.channels    )
   # Out indices are set to the last layer by default. For timm models, we don't know
   # the number of layers in advance, so we set it to (-1,), whereas for transformers
   # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
   self.assertEqual(timm_model.out_indices			,						(-1,)    )
   self.assertEqual(transformers_model.out_indices			,						[len(timm_model.stage_names    ) - 1]    )

   UpperCAmelCase					      =  AutoBackbone.from_pretrained(lowerCAmelCase__			,						use_timm_backbone=lowerCAmelCase__			,						out_indices=[1, 2, 3]    )
   UpperCAmelCase					      =  AutoBackbone.from_pretrained(lowerCAmelCase__			,						out_indices=[1, 2, 3]    )

   self.assertEqual(timm_model.out_indices			,						transformers_model.out_indices    )
   self.assertEqual(len(timm_model.out_features    )			,						len(transformers_model.out_features    )    )
   self.assertEqual(timm_model.channels			,						transformers_model.channels    )


 @unittest.skip("TimmBackbone doesn't support feed forward chunking"    )
 def _UpperCamelCase	(						self		: Dict    )    ->      Optional[int]:
   pass


 @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute"    )
 def _UpperCamelCase	(						self		: Optional[Any]    )    ->      List[Any]:
   pass


 @unittest.skip("TimmBackbone initialization is managed on the timm side"    )
 def _UpperCamelCase	(						self		: Tuple    )    ->      Optional[Any]:
   pass


 @unittest.skip("TimmBackbone models doesn't have inputs_embeds"    )
 def _UpperCamelCase	(						self		: Dict    )    ->      Optional[int]:
   pass


 @unittest.skip("TimmBackbone models doesn't have inputs_embeds"    )
 def _UpperCamelCase	(						self		: Dict    )    ->      str:
   pass


 @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint"    )
 def _UpperCamelCase	(						self		: List[str]    )    ->      Any:
   pass


 @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"    )
 def _UpperCamelCase	(						self		: Optional[int]    )    ->      int:
   pass


 @unittest.skip("model weights aren't tied in TimmBackbone."    )
 def _UpperCamelCase	(						self		: List[str]    )    ->      List[str]:
   pass


 @unittest.skip("model weights aren't tied in TimmBackbone."    )
 def _UpperCamelCase	(						self		: Optional[int]    )    ->      Union[str, Any]:
   pass


 @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"    )
 def _UpperCamelCase	(						self		: int    )    ->      List[Any]:
   pass


 @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"    )
 def _UpperCamelCase	(						self		: List[Any]    )    ->      Optional[int]:
   pass


 @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration."    )
 def _UpperCamelCase	(						self		: Optional[Any]    )    ->      Tuple:
   pass


 @unittest.skip("TimmBackbone doesn't support output_attentions."    )
 def _UpperCamelCase	(						self		: int    )    ->      Tuple:
   pass


 @unittest.skip("Safetensors is not supported by timm."    )
 def _UpperCamelCase	(						self		: int    )    ->      List[Any]:
   pass


 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."    )
 def _UpperCamelCase	(						self		: int    )    ->      Tuple:
   pass


 def _UpperCamelCase	(						self		: Dict    )    ->      Any:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs_for_common()

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

     UpperCAmelCase					      =  ["pixel_values"]
     self.assertListEqual(arg_names[:1]			,						lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: Optional[int]    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs_for_common()
   UpperCAmelCase					      =  True
   UpperCAmelCase					      =  self.has_attentions

   # no need to test all models as different heads yield the same functionality
   UpperCAmelCase					      =  self.all_model_classes[0]
   UpperCAmelCase					      =  model_class(lowerCAmelCase__    )
   model.to(lowerCAmelCase__    )

   UpperCAmelCase					      =  self._prepare_for_class(lowerCAmelCase__			,						lowerCAmelCase__    )
   UpperCAmelCase					      =  model(**lowerCAmelCase__    )
   UpperCAmelCase					      =  outputs[0][-1]

   # Encoder-/Decoder-only models
   UpperCAmelCase					      =  outputs.hidden_states[0]
   hidden_states.retain_grad()

   if self.has_attentions:
     UpperCAmelCase					      =  outputs.attentions[0]
     attentions.retain_grad()

   output.flatten()[0].backward(retain_graph=lowerCAmelCase__    )

   self.assertIsNotNone(hidden_states.grad    )

   if self.has_attentions:
     self.assertIsNotNone(attentions.grad    )


 def _UpperCamelCase	(						self		: Optional[int]    )    ->      List[str]:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     UpperCAmelCase					      =  model_class(lowerCAmelCase__    )
     model.to(lowerCAmelCase__    )
     model.eval()
     UpperCAmelCase					      =  model(**lowerCAmelCase__    )

     self.assertEqual(len(result.feature_maps    )			,						len(config.out_indices    )    )
     self.assertEqual(len(model.channels    )			,						len(config.out_indices    )    )

     # Check output of last stage is taken if out_features=None, out_indices=None
     UpperCAmelCase					      =  copy.deepcopy(lowerCAmelCase__    )
     UpperCAmelCase					      =  None
     UpperCAmelCase					      =  model_class(lowerCAmelCase__    )
     model.to(lowerCAmelCase__    )
     model.eval()
     UpperCAmelCase					      =  model(**lowerCAmelCase__    )

     self.assertEqual(len(result.feature_maps    )			,						1    )
     self.assertEqual(len(model.channels    )			,						1    )

     # Check backbone can be initialized with fresh weights
     UpperCAmelCase					      =  copy.deepcopy(lowerCAmelCase__    )
     UpperCAmelCase					      =  False
     UpperCAmelCase					      =  model_class(lowerCAmelCase__    )
     model.to(lowerCAmelCase__    )
     model.eval()
     UpperCAmelCase					      =  model(**lowerCAmelCase__    )






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""lxmert"""
 UpperCAmelCase							    =						{}


 def __init__(						self		: int			,						lowerCAmelCase__		: Any=3_0_5_2_2			,						lowerCAmelCase__		: List[str]=7_6_8			,						lowerCAmelCase__		: Union[str, Any]=1_2			,						lowerCAmelCase__		: List[Any]=9_5_0_0			,						lowerCAmelCase__		: Any=1_6_0_0			,						lowerCAmelCase__		: Union[str, Any]=4_0_0			,						lowerCAmelCase__		: Tuple=3_0_7_2			,						lowerCAmelCase__		: Dict="gelu"			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: int=5_1_2			,						lowerCAmelCase__		: List[str]=2			,						lowerCAmelCase__		: List[str]=0.02			,						lowerCAmelCase__		: str=1e-1_2			,						lowerCAmelCase__		: str=9			,						lowerCAmelCase__		: int=5			,						lowerCAmelCase__		: Optional[int]=5			,						lowerCAmelCase__		: List[Any]=2_0_4_8			,						lowerCAmelCase__		: Any=4			,						lowerCAmelCase__		: Dict=6.67			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Union[str, Any]=True			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Tuple=True			,						lowerCAmelCase__		: Optional[Any]=True			,						lowerCAmelCase__		: Optional[int]=True			,						lowerCAmelCase__		: Tuple=True			,						**lowerCAmelCase__		: List[Any]			,						)    ->      Dict:
   UpperCAmelCase					      =  vocab_size
   UpperCAmelCase					      =  hidden_size
   UpperCAmelCase					      =  num_attention_heads
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  intermediate_size
   UpperCAmelCase					      =  hidden_dropout_prob
   UpperCAmelCase					      =  attention_probs_dropout_prob
   UpperCAmelCase					      =  max_position_embeddings
   UpperCAmelCase					      =  type_vocab_size
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  num_qa_labels
   UpperCAmelCase					      =  num_object_labels
   UpperCAmelCase					      =  num_attr_labels
   UpperCAmelCase					      =  l_layers
   UpperCAmelCase					      =  x_layers
   UpperCAmelCase					      =  r_layers
   UpperCAmelCase					      =  visual_feat_dim
   UpperCAmelCase					      =  visual_pos_dim
   UpperCAmelCase					      =  visual_loss_normalizer
   UpperCAmelCase					      =  task_matched
   UpperCAmelCase					      =  task_mask_lm
   UpperCAmelCase					      =  task_obj_predict
   UpperCAmelCase					      =  task_qa
   UpperCAmelCase					      =  visual_obj_loss
   UpperCAmelCase					      =  visual_attr_loss
   UpperCAmelCase					      =  visual_feat_loss
   UpperCAmelCase					      =  {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers}
   super().__init__(**lowerCAmelCase__    )



