



import copy

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


A__  :  int								=					logging.get_logger(__name__)

A__  :  Optional[int]								=					{
    "SenseTime/deformable-detr": "https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json",
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}



class 				__snake_case						(     __lowerCamelCase						):
							_a			      = '''deformable_detr'''
							_a			      = {
							    '''hidden_size''': '''d_model''',
							    '''num_attention_heads''': '''encoder_attention_heads''',
							}




							def __init__(       self     :       List[str]				,				A_     :       Optional[int]=True				,				A_     :       List[Any]=None				,				A_     :       List[str]=3				,				A_     :       str=3_0_0				,				A_     :       List[Any]=1_0_2_4				,				A_     :       int=6				,				A_     :       Union[str, Any]=1_0_2_4				,				A_     :       str=8				,				A_     :       Union[str, Any]=6				,				A_     :       Union[str, Any]=1_0_2_4				,				A_     :       Any=8				,				A_     :       str=0.0				,				A_     :       Any=True				,				A_     :       Dict="relu"				,				A_     :       List[str]=2_5_6				,				A_     :       Any=0.1				,				A_     :       Optional[Any]=0.0				,				A_     :       Optional[Any]=0.0				,				A_     :       str=0.02				,				A_     :       Tuple=1.0				,				A_     :       List[Any]=True				,				A_     :       Any=False				,				A_     :       int="sine"				,				A_     :       Optional[Any]="resnet50"				,				A_     :       Tuple=True				,				A_     :       Dict=False				,				A_     :       Tuple=4				,				A_     :       str=4				,				A_     :       int=4				,				A_     :       Union[str, Any]=False				,				A_     :       Union[str, Any]=3_0_0				,				A_     :       Optional[int]=False				,				A_     :       List[str]=1				,				A_     :       str=5				,				A_     :       str=2				,				A_     :       Optional[Any]=1				,				A_     :       Any=1				,				A_     :       List[str]=5				,				A_     :       List[str]=2				,				A_     :       Any=0.1				,				A_     :       Union[str, Any]=0.25				,				A_     :       List[Any]=False				,				**A_     :       List[str]				,				):
									if backbone_config is not None and use_timm_backbone:
											raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''')

									if not use_timm_backbone:
											if backbone_config is None:
													logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''')
													lowerCAmelCase_			:     List[Any]									= CONFIG_MAPPING["""resnet"""](out_features=['''stage4'''])
											elif isinstance(A_				,				A_):
													lowerCAmelCase_			:     int									= backbone_config.get('''model_type''')
													lowerCAmelCase_			:     Union[str, Any]									= CONFIG_MAPPING[backbone_model_type]
													lowerCAmelCase_			:     str									= config_class.from_dict(A_)
									lowerCAmelCase_			:     Optional[int]									= use_timm_backbone
									lowerCAmelCase_			:     Optional[Any]									= backbone_config
									lowerCAmelCase_			:     Tuple									= num_channels
									lowerCAmelCase_			:     Dict									= num_queries
									lowerCAmelCase_			:     Optional[int]									= max_position_embeddings
									lowerCAmelCase_			:     Any									= d_model
									lowerCAmelCase_			:     Dict									= encoder_ffn_dim
									lowerCAmelCase_			:     Any									= encoder_layers
									lowerCAmelCase_			:     str									= encoder_attention_heads
									lowerCAmelCase_			:     Tuple									= decoder_ffn_dim
									lowerCAmelCase_			:     Optional[Any]									= decoder_layers
									lowerCAmelCase_			:     int									= decoder_attention_heads
									lowerCAmelCase_			:     Tuple									= dropout
									lowerCAmelCase_			:     Tuple									= attention_dropout
									lowerCAmelCase_			:     List[Any]									= activation_dropout
									lowerCAmelCase_			:     Any									= activation_function
									lowerCAmelCase_			:     Dict									= init_std
									lowerCAmelCase_			:     Optional[int]									= init_xavier_std
									lowerCAmelCase_			:     int									= encoder_layerdrop
									lowerCAmelCase_			:     str									= auxiliary_loss
									lowerCAmelCase_			:     List[Any]									= position_embedding_type
									lowerCAmelCase_			:     List[Any]									= backbone
									lowerCAmelCase_			:     Optional[Any]									= use_pretrained_backbone
									lowerCAmelCase_			:     List[str]									= dilation
									# deformable attributes
									lowerCAmelCase_			:     Optional[Any]									= num_feature_levels
									lowerCAmelCase_			:     int									= encoder_n_points
									lowerCAmelCase_			:     Any									= decoder_n_points
									lowerCAmelCase_			:     Optional[Any]									= two_stage
									lowerCAmelCase_			:     Dict									= two_stage_num_proposals
									lowerCAmelCase_			:     Any									= with_box_refine
									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
									lowerCAmelCase_			:     int									= class_cost
									lowerCAmelCase_			:     Any									= bbox_cost
									lowerCAmelCase_			:     Any									= giou_cost
									# Loss coefficients
									lowerCAmelCase_			:     List[Any]									= mask_loss_coefficient
									lowerCAmelCase_			:     Any									= dice_loss_coefficient
									lowerCAmelCase_			:     List[Any]									= bbox_loss_coefficient
									lowerCAmelCase_			:     Union[str, Any]									= giou_loss_coefficient
									lowerCAmelCase_			:     Optional[Any]									= eos_coefficient
									lowerCAmelCase_			:     Optional[int]									= focal_alpha
									lowerCAmelCase_			:     Optional[int]									= disable_custom_kernels
									super().__init__(is_encoder_decoder=A_				,				**A_)




							@property
							def    UpperCAmelCase__			(       self     :       str):
									return self.encoder_attention_heads




							@property
							def    UpperCAmelCase__			(       self     :       List[Any]):
									return self.d_model







							def    UpperCAmelCase__			(       self     :       Optional[int]):
									lowerCAmelCase_			:     Dict									= copy.deepcopy(self.__dict__)
									if self.backbone_config is not None:
											lowerCAmelCase_			:     List[Any]									= self.backbone_config.to_dict()
									lowerCAmelCase_			:     List[Any]									= self.__class__.model_type
									return output












import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
      import tensorflow as tf

      from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments







@require_tf
class     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       def 							UpperCAmelCase__ (  self :    Any							,				A :    Union[str, Any]		):
         for model_result in results.values():
           for batch_size, sequence_length in zip(model_result["""bs"""]							,				model_result["""ss"""]		):
             __snake_case:						Dict   =						model_result["""result"""][batch_size][sequence_length]
             self.assertIsNotNone(A		)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         __snake_case:						List[Any]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				eager_mode=A							,				multi_process=A							,				)
         __snake_case:						Dict   =						TensorFlowBenchmark(A		)
         __snake_case:						List[str]   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Optional[int]   =						"""sgugger/tiny-distilbert-classification"""
         __snake_case:						str   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				only_pretrain_model=A							,				)
         __snake_case:						Any   =						TensorFlowBenchmark(A		)
         __snake_case:						List[Any]   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Dict   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						int   =						TensorFlowBenchmark(A		)
         __snake_case:						str   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         __snake_case:						List[Any]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						int   =						AutoConfig.from_pretrained(A		)
         __snake_case:						Optional[Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				eager_mode=A							,				multi_process=A							,				)
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmark(A							,				[config]		)
         __snake_case:						Tuple   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Optional[int]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Tuple   =						AutoConfig.from_pretrained(A		)
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						str   =						TensorFlowBenchmark(A							,				[config]		)
         __snake_case:						str   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Tuple   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						str   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						Tuple   =						TensorFlowBenchmark(A		)
         __snake_case:						Tuple   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_train_result		)
         self.check_results_dict_not_empty(results.memory_train_result		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Dict   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Union[str, Any]   =						AutoConfig.from_pretrained(A		)
         __snake_case:						Optional[int]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						List[str]   =						TensorFlowBenchmark(A							,				[config]		)
         __snake_case:						Dict   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_train_result		)
         self.check_results_dict_not_empty(results.memory_train_result		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						List[Any]   =						"""patrickvonplaten/t5-tiny-random"""
         __snake_case:						List[str]   =						AutoConfig.from_pretrained(A		)
         __snake_case:						List[str]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						Optional[int]   =						TensorFlowBenchmark(A							,				configs=[config]		)
         __snake_case:						Tuple   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("""GPU"""		)		) == 0							,				"""Cannot do xla on CPU."""		)
       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						Optional[Any]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						List[Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				use_xla=A							,				multi_process=A							,				)
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmark(A		)
         __snake_case:						List[Any]   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						List[str]   =						"""sshleifer/tiny-gpt2"""
         with tempfile.TemporaryDirectory() as tmp_dir:
           __snake_case:						int   =						TensorFlowBenchmarkArguments(
               models=[MODEL_ID]							,				inference=A							,				save_to_csv=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				inference_time_csv_file=os.path.join(A							,				"""inf_time.csv"""		)							,				inference_memory_csv_file=os.path.join(A							,				"""inf_mem.csv"""		)							,				env_info_csv_file=os.path.join(A							,				"""env.csv"""		)							,				multi_process=A							,				)
           __snake_case:						Tuple   =						TensorFlowBenchmark(A		)
           benchmark.run()
           self.assertTrue(Path(os.path.join(A							,				"""inf_time.csv"""		)		).exists()		)
           self.assertTrue(Path(os.path.join(A							,				"""inf_mem.csv"""		)		).exists()		)
           self.assertTrue(Path(os.path.join(A							,				"""env.csv"""		)		).exists()		)






       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Any   =						"""sshleifer/tiny-gpt2"""

         def _check_summary_is_not_empty(A :    Union[str, Any]		):
           self.assertTrue(hasattr(A							,				"""sequential"""		)		)
           self.assertTrue(hasattr(A							,				"""cumulative"""		)		)
           self.assertTrue(hasattr(A							,				"""current"""		)		)
           self.assertTrue(hasattr(A							,				"""total"""		)		)

         with tempfile.TemporaryDirectory() as tmp_dir:
           __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
               models=[MODEL_ID]							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				log_filename=os.path.join(A							,				"""log.txt"""		)							,				log_print=A							,				trace_memory_line_by_line=A							,				eager_mode=A							,				multi_process=A							,				)
           __snake_case:						Dict   =						TensorFlowBenchmark(A		)
           __snake_case:						List[str]   =						benchmark.run()
           _check_summary_is_not_empty(result.inference_summary		)
           self.assertTrue(Path(os.path.join(A							,				"""log.txt"""		)		).exists()		)






