





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
      from transformers import (
          TFBertModel,
          TFCLIPVisionModel,
          TFDeiTModel,
          TFRobertaModel,
          TFVisionTextDualEncoderModel,
          TFViTModel,
          VisionTextDualEncoderConfig,
      )

if is_vision_available():
      from PIL import Image

      from transformers import VisionTextDualEncoderProcessor



def      __lowercase     (   _SCREAMING_SNAKE_CASE			)				->     int:


      '''simple docstring'''


      if isinstance(_SCREAMING_SNAKE_CASE				,		collections.abc.Iterable			):
            return x
      return (x, x)

@require_tf
class 	UpperCamelCase__				:



     '''simple docstring'''

     def        SCREAMING_SNAKE_CASE__						(							self		:					List[Any]		,lowerCamelCase__		:					Union[str, Any]		,lowerCamelCase__		:					Tuple )  ->	str:




           '''simple docstring'''


           pass






     def        SCREAMING_SNAKE_CASE__						(							self		:					Dict )  ->	List[Any]:




           '''simple docstring'''


           pass






     def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[Any] )  ->	Optional[int]:




           '''simple docstring'''


           pass






     def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[Any]		,lowerCamelCase__		:					Dict		,lowerCamelCase__		:					Any		,lowerCamelCase__		:					Union[str, Any]		,lowerCamelCase__		:					Optional[Any]		,lowerCamelCase__		:					List[str]=None		,**lowerCamelCase__		:					Tuple )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		VisionTextDualEncoderConfig.from_vision_text_configs(lowerCamelCase__		,lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel(lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		model(input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__ )

           self.assertEqual(output["""text_embeds"""].shape		,(input_ids.shape[0], config.projection_dim) )
           self.assertEqual(output["""image_embeds"""].shape		,(pixel_values.shape[0], config.projection_dim) )






     def        SCREAMING_SNAKE_CASE__						(							self		:					Union[str, Any]		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					Dict		,lowerCamelCase__		:					Optional[Any]		,lowerCamelCase__		:					Dict		,lowerCamelCase__		:					str=None		,**lowerCamelCase__		:					int )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		self.get_vision_text_model(lowerCamelCase__		,lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel(vision_model=lowerCamelCase__		,text_model=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		model(input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__ )

           self.assertEqual(output["""text_embeds"""].shape		,(input_ids.shape[0], model.config.projection_dim) )
           self.assertEqual(output["""image_embeds"""].shape		,(pixel_values.shape[0], model.config.projection_dim) )






     def        SCREAMING_SNAKE_CASE__						(							self		:					Any		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					str		,lowerCamelCase__		:					Any		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					int=None		,**lowerCamelCase__		:					Dict )  ->	List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		self.get_vision_text_model(lowerCamelCase__		,lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		{"""vision_model""": vision_model, """text_model""": text_model}
           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		model(input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__ )

           self.assertEqual(output["""text_embeds"""].shape		,(input_ids.shape[0], model.config.projection_dim) )
           self.assertEqual(output["""image_embeds"""].shape		,(pixel_values.shape[0], model.config.projection_dim) )






     def        SCREAMING_SNAKE_CASE__						(							self		:					int		,lowerCamelCase__		:					int		,lowerCamelCase__		:					List[str]		,lowerCamelCase__		:					Any		,lowerCamelCase__		:					Optional[Any]		,lowerCamelCase__		:					Union[str, Any]=None		,**lowerCamelCase__		:					Dict )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		self.get_vision_text_model(lowerCamelCase__		,lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel(vision_model=lowerCamelCase__		,text_model=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		model(input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		output[0].numpy()

           with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_pretrained(lowerCamelCase__ )
                 SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_pretrained(lowerCamelCase__ )

                 SCREAMING_SNAKE_CASE	=		model(input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__ )
                 SCREAMING_SNAKE_CASE	=		after_output[0].numpy()
                 SCREAMING_SNAKE_CASE	=		np.amax(np.abs(out_a - out_a ) )
                 self.assertLessEqual(lowerCamelCase__		,1e-5 )






     def        SCREAMING_SNAKE_CASE__						(							self		:					Union[str, Any]		,lowerCamelCase__		:					int		,lowerCamelCase__		:					Optional[Any]		,lowerCamelCase__		:					Union[str, Any]		,lowerCamelCase__		:					List[str]		,lowerCamelCase__		:					Optional[int]=None		,**lowerCamelCase__		:					str )  ->	Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		self.get_vision_text_model(lowerCamelCase__		,lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel(vision_model=lowerCamelCase__		,text_model=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		model(
               input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__		,output_attentions=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		output.vision_model_output.attentions
           self.assertEqual(len(lowerCamelCase__ )		,vision_config.num_hidden_layers )

           # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
           SCREAMING_SNAKE_CASE	=		to_atuple(vision_model.config.image_size )
           SCREAMING_SNAKE_CASE	=		to_atuple(vision_model.config.patch_size )
           SCREAMING_SNAKE_CASE	=		(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
           SCREAMING_SNAKE_CASE	=		num_patches + 1
           self.assertEqual(vision_attentions[0].shape[-3:]		,(vision_config.num_attention_heads, seq_len, seq_len) )

           SCREAMING_SNAKE_CASE	=		output.text_model_output.attentions
           self.assertEqual(len(lowerCamelCase__ )		,text_config.num_hidden_layers )

           self.assertEqual(
               text_attentions[0].shape[-3:]		,(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])		,)






     def        SCREAMING_SNAKE_CASE__						(							self		:					str		,lowerCamelCase__		:					np.ndarray		,lowerCamelCase__		:					np.ndarray		,lowerCamelCase__		:					float )  ->	Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		np.abs((a - b) ).max()
           self.assertLessEqual(lowerCamelCase__		,lowerCamelCase__		,F"""Difference between torch and flax is {diff} (>= {tol}).""" )






     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple )  ->	Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
           self.check_vision_text_dual_encoder_model(**lowerCamelCase__ )






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
           self.check_model_from_pretrained_configs(**lowerCamelCase__ )






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
           self.check_vision_text_dual_encoder_from_pretrained(**lowerCamelCase__ )






     def        SCREAMING_SNAKE_CASE__						(							self		:					str )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
           self.check_save_load(**lowerCamelCase__ )






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[Any] )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
           self.check_vision_text_output_attention(**lowerCamelCase__ )






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		self.get_pretrained_model_and_inputs()

           SCREAMING_SNAKE_CASE	=		model_a(**lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		outputs[0].numpy()

           with tempfile.TemporaryDirectory() as tmp_dirname:
                 model_a.save_pretrained(lowerCamelCase__ )
                 SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_pretrained(lowerCamelCase__ )

                 SCREAMING_SNAKE_CASE	=		model_a(**lowerCamelCase__ )
                 SCREAMING_SNAKE_CASE	=		after_outputs[0].numpy()
                 SCREAMING_SNAKE_CASE	=		np.amax(np.abs(out_a - out_a ) )
                 self.assertLessEqual(lowerCamelCase__		,1e-5 )

@require_tf
class 	UpperCamelCase__				(   lowerCAmelCase_							,   unittest.TestCase     ):



     '''simple docstring'''

     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_vision_text_pretrained(
               """hf-internal-testing/tiny-random-vit"""		,"""hf-internal-testing/tiny-random-bert""" )
           SCREAMING_SNAKE_CASE	=		13
           SCREAMING_SNAKE_CASE	=		floats_tensor(
               [
                   batch_size,
                   model.vision_model.config.num_channels,
                   model.vision_model.config.image_size,
                   model.vision_model.config.image_size,
               ] )
           SCREAMING_SNAKE_CASE	=		ids_tensor([batch_size, 4]		,model.text_model.config.vocab_size )
           SCREAMING_SNAKE_CASE	=		random_attention_mask([batch_size, 4] )
           SCREAMING_SNAKE_CASE	=		{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

           return model, inputs






     def        SCREAMING_SNAKE_CASE__						(							self		:					str		,lowerCamelCase__		:					List[str]		,lowerCamelCase__		:					Tuple )  ->	Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFViTModel(lowerCamelCase__		,name="""vision_model""" )
           SCREAMING_SNAKE_CASE	=		TFBertModel(lowerCamelCase__		,name="""text_model""" )
           return vision_model, text_model






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFViTModelTester(self )
           SCREAMING_SNAKE_CASE	=		TFBertModelTester(self )
           SCREAMING_SNAKE_CASE	=		vit_model_tester.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE	=		bert_model_tester.prepare_config_and_inputs()

           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		vision_config_and_inputs

           (
               (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      
           )	=		text_config_and_inputs

           return {
               "text_config": text_config,
               "vision_config": vision_config,
               "pixel_values": pixel_values,
               "attention_mask": input_mask,
               "input_ids": input_ids,
               "text_token_type_ids": token_type_ids,
               "text_sequence_labels": sequence_labels,
               "text_token_labels": token_labels,
               "text_choice_labels": choice_labels,
           }

@require_tf
class 	UpperCamelCase__				(   lowerCAmelCase_							,   unittest.TestCase     ):



     '''simple docstring'''

     def        SCREAMING_SNAKE_CASE__						(							self		:					Any )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_vision_text_pretrained(
               """Rocketknight1/tiny-random-deit-tf"""		,"""hf-internal-testing/tiny-random-roberta""" )
           SCREAMING_SNAKE_CASE	=		13
           SCREAMING_SNAKE_CASE	=		floats_tensor(
               [
                   batch_size,
                   model.vision_model.config.num_channels,
                   model.vision_model.config.image_size,
                   model.vision_model.config.image_size,
               ] )
           SCREAMING_SNAKE_CASE	=		ids_tensor([batch_size, 4]		,model.text_model.config.vocab_size )
           SCREAMING_SNAKE_CASE	=		random_attention_mask([batch_size, 4] )
           SCREAMING_SNAKE_CASE	=		{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

           return model, inputs






     def        SCREAMING_SNAKE_CASE__						(							self		:					str		,lowerCamelCase__		:					Union[str, Any]		,lowerCamelCase__		:					Tuple		,lowerCamelCase__		:					Dict		,lowerCamelCase__		:					Optional[Any]		,lowerCamelCase__		:					Optional[int]=None		,**lowerCamelCase__		:					Optional[Any] )  ->	Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		self.get_vision_text_model(lowerCamelCase__		,lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel(vision_model=lowerCamelCase__		,text_model=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		model(
               input_ids=lowerCamelCase__		,pixel_values=lowerCamelCase__		,attention_mask=lowerCamelCase__		,output_attentions=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		output.vision_model_output.attentions
           self.assertEqual(len(lowerCamelCase__ )		,vision_config.num_hidden_layers )

           # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
           SCREAMING_SNAKE_CASE	=		to_atuple(vision_model.config.image_size )
           SCREAMING_SNAKE_CASE	=		to_atuple(vision_model.config.patch_size )
           SCREAMING_SNAKE_CASE	=		(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
           SCREAMING_SNAKE_CASE	=		num_patches + 2
           self.assertEqual(vision_attentions[0].shape[-3:]		,(vision_config.num_attention_heads, seq_len, seq_len) )

           SCREAMING_SNAKE_CASE	=		output.text_model_output.attentions
           self.assertEqual(len(lowerCamelCase__ )		,text_config.num_hidden_layers )

           self.assertEqual(
               text_attentions[0].shape[-3:]		,(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])		,)






     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple		,lowerCamelCase__		:					str		,lowerCamelCase__		:					List[str] )  ->	Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFDeiTModel(lowerCamelCase__		,name="""vision_model""" )
           SCREAMING_SNAKE_CASE	=		TFRobertaModel(lowerCamelCase__		,name="""text_model""" )
           return vision_model, text_model






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFDeiTModelTester(self )
           SCREAMING_SNAKE_CASE	=		TFRobertaModelTester(self )
           SCREAMING_SNAKE_CASE	=		vit_model_tester.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE	=		bert_model_tester.prepare_config_and_inputs()

           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		vision_config_and_inputs

           (
               (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      
           )	=		text_config_and_inputs

           return {
               "text_config": text_config,
               "vision_config": vision_config,
               "pixel_values": pixel_values,
               "attention_mask": input_mask,
               "input_ids": input_ids,
               "text_token_type_ids": token_type_ids,
               "text_sequence_labels": sequence_labels,
               "text_token_labels": token_labels,
               "text_choice_labels": choice_labels,
           }

@require_tf
class 	UpperCamelCase__				(   lowerCAmelCase_							,   unittest.TestCase     ):



     '''simple docstring'''

     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_vision_text_pretrained(
               """Rocketknight1/tiny-random-clip-tf"""		,"""hf-internal-testing/tiny-random-bert""" )
           SCREAMING_SNAKE_CASE	=		13
           SCREAMING_SNAKE_CASE	=		floats_tensor(
               [
                   batch_size,
                   model.vision_model.config.num_channels,
                   model.vision_model.config.image_size,
                   model.vision_model.config.image_size,
               ] )
           SCREAMING_SNAKE_CASE	=		ids_tensor([batch_size, 4]		,model.text_model.config.vocab_size )
           SCREAMING_SNAKE_CASE	=		random_attention_mask([batch_size, 4] )
           SCREAMING_SNAKE_CASE	=		{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

           return model, inputs






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[Any]		,lowerCamelCase__		:					int		,lowerCamelCase__		:					Optional[int] )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFCLIPVisionModel(lowerCamelCase__		,name="""vision_model""" )
           SCREAMING_SNAKE_CASE	=		TFBertModel(lowerCamelCase__		,name="""text_model""" )
           return vision_model, text_model






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFCLIPVisionModelTester(self )
           SCREAMING_SNAKE_CASE	=		TFBertModelTester(self )
           SCREAMING_SNAKE_CASE	=		clip_model_tester.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE	=		bert_model_tester.prepare_config_and_inputs()

           SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		vision_config_and_inputs

           (
               (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      
           )	=		text_config_and_inputs

           return {
               "text_config": text_config,
               "vision_config": vision_config,
               "pixel_values": pixel_values,
               "attention_mask": input_mask,
               "input_ids": input_ids,
               "text_token_type_ids": token_type_ids,
               "text_sequence_labels": sequence_labels,
               "text_token_labels": token_labels,
               "text_choice_labels": choice_labels,
           }

@require_vision
@require_tf
class 	UpperCamelCase__				(   unittest.TestCase     ):



     '''simple docstring'''

     @slow
     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		TFVisionTextDualEncoderModel.from_pretrained(
               """clip-italian/clip-italian"""		,logit_scale_init_value=1.0		,from_pt=lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian""" )

           SCREAMING_SNAKE_CASE	=		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
           SCREAMING_SNAKE_CASE	=		processor(
               text=["""una foto di un gatto""", """una foto di un cane"""]		,images=lowerCamelCase__		,padding=lowerCamelCase__		,return_tensors="""np""" )

           SCREAMING_SNAKE_CASE	=		model(**lowerCamelCase__ )

           # verify the logits
           self.assertEqual(outputs.logits_per_image.shape		,(inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) )
           self.assertEqual(
               outputs.logits_per_text.shape		,(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])		,)

           SCREAMING_SNAKE_CASE	=		np.array([[1.2284727, 0.3104122]] )

           self.assertTrue(np.allclose(outputs.logits_per_image.numpy()		,lowerCamelCase__		,atol=1e-3 ) )









import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


SCREAMING_SNAKE_CASE_		     = logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_		     = {
    """ut/deta""": """https://huggingface.co/ut/deta/resolve/main/config.json""",
}

class 	UpperCamelCase__				(   lowerCAmelCase_     ):



     '''simple docstring'''

     __snake_case					:			str				=    "deta"
     __snake_case					:			int				=    {
         "hidden_size": "d_model",
         "num_attention_heads": "encoder_attention_heads",
     }






     def __init__(							self		:					List[Any]		,lowerCamelCase__		:					Any=None		,lowerCamelCase__		:					Optional[int]=900		,lowerCamelCase__		:					Optional[Any]=2048		,lowerCamelCase__		:					Optional[Any]=6		,lowerCamelCase__		:					Dict=2048		,lowerCamelCase__		:					Optional[int]=8		,lowerCamelCase__		:					List[str]=6		,lowerCamelCase__		:					Union[str, Any]=1024		,lowerCamelCase__		:					str=8		,lowerCamelCase__		:					Union[str, Any]=0.0		,lowerCamelCase__		:					Dict=True		,lowerCamelCase__		:					Dict="relu"		,lowerCamelCase__		:					int=256		,lowerCamelCase__		:					Optional[Any]=0.1		,lowerCamelCase__		:					Dict=0.0		,lowerCamelCase__		:					Dict=0.0		,lowerCamelCase__		:					Dict=0.02		,lowerCamelCase__		:					List[Any]=1.0		,lowerCamelCase__		:					List[str]=True		,lowerCamelCase__		:					int=False		,lowerCamelCase__		:					int="sine"		,lowerCamelCase__		:					Any=5		,lowerCamelCase__		:					Optional[int]=4		,lowerCamelCase__		:					Optional[int]=4		,lowerCamelCase__		:					List[Any]=True		,lowerCamelCase__		:					Optional[int]=300		,lowerCamelCase__		:					Optional[Any]=True		,lowerCamelCase__		:					Optional[Any]=True		,lowerCamelCase__		:					Optional[int]=1		,lowerCamelCase__		:					Tuple=5		,lowerCamelCase__		:					Tuple=2		,lowerCamelCase__		:					Dict=1		,lowerCamelCase__		:					str=1		,lowerCamelCase__		:					List[Any]=5		,lowerCamelCase__		:					Tuple=2		,lowerCamelCase__		:					Union[str, Any]=0.1		,lowerCamelCase__		:					List[str]=0.25		,**lowerCamelCase__		:					int		,)  ->	List[Any]:




           '''simple docstring'''


           if backbone_config is None:
                 logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" )
                 SCREAMING_SNAKE_CASE	=		CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""] )
           else:
                 if isinstance(lowerCamelCase__		,lowerCamelCase__ ):
                       SCREAMING_SNAKE_CASE	=		backbone_config.pop("""model_type""" )
                       SCREAMING_SNAKE_CASE	=		CONFIG_MAPPING[backbone_model_type]
                       SCREAMING_SNAKE_CASE	=		config_class.from_dict(lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		backbone_config
           SCREAMING_SNAKE_CASE	=		num_queries
           SCREAMING_SNAKE_CASE	=		max_position_embeddings
           SCREAMING_SNAKE_CASE	=		d_model
           SCREAMING_SNAKE_CASE	=		encoder_ffn_dim
           SCREAMING_SNAKE_CASE	=		encoder_layers
           SCREAMING_SNAKE_CASE	=		encoder_attention_heads
           SCREAMING_SNAKE_CASE	=		decoder_ffn_dim
           SCREAMING_SNAKE_CASE	=		decoder_layers
           SCREAMING_SNAKE_CASE	=		decoder_attention_heads
           SCREAMING_SNAKE_CASE	=		dropout
           SCREAMING_SNAKE_CASE	=		attention_dropout
           SCREAMING_SNAKE_CASE	=		activation_dropout
           SCREAMING_SNAKE_CASE	=		activation_function
           SCREAMING_SNAKE_CASE	=		init_std
           SCREAMING_SNAKE_CASE	=		init_xavier_std
           SCREAMING_SNAKE_CASE	=		encoder_layerdrop
           SCREAMING_SNAKE_CASE	=		auxiliary_loss
           SCREAMING_SNAKE_CASE	=		position_embedding_type
           # deformable attributes
           SCREAMING_SNAKE_CASE	=		num_feature_levels
           SCREAMING_SNAKE_CASE	=		encoder_n_points
           SCREAMING_SNAKE_CASE	=		decoder_n_points
           SCREAMING_SNAKE_CASE	=		two_stage
           SCREAMING_SNAKE_CASE	=		two_stage_num_proposals
           SCREAMING_SNAKE_CASE	=		with_box_refine
           SCREAMING_SNAKE_CASE	=		assign_first_stage
           if two_stage is True and with_box_refine is False:
                 raise ValueError("""If two_stage is True, with_box_refine must be True.""" )
           # Hungarian matcher
           SCREAMING_SNAKE_CASE	=		class_cost
           SCREAMING_SNAKE_CASE	=		bbox_cost
           SCREAMING_SNAKE_CASE	=		giou_cost
           # Loss coefficients
           SCREAMING_SNAKE_CASE	=		mask_loss_coefficient
           SCREAMING_SNAKE_CASE	=		dice_loss_coefficient
           SCREAMING_SNAKE_CASE	=		bbox_loss_coefficient
           SCREAMING_SNAKE_CASE	=		giou_loss_coefficient
           SCREAMING_SNAKE_CASE	=		eos_coefficient
           SCREAMING_SNAKE_CASE	=		focal_alpha
           super().__init__(is_encoder_decoder=lowerCamelCase__		,**lowerCamelCase__ )






     @property
     def        SCREAMING_SNAKE_CASE__						(							self		:					str )  ->	int:




           '''simple docstring'''


           return self.encoder_attention_heads






     @property
     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	int:




           '''simple docstring'''


           return self.d_model






     def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[Any] )  ->	Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		copy.deepcopy(self.__dict__ )
           SCREAMING_SNAKE_CASE	=		self.backbone_config.to_dict()
           SCREAMING_SNAKE_CASE	=		self.__class__.model_type
           return output


