






import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
							    CTRLForSequenceClassification,
							    CTRLLMHeadModel,
							    CTRLModel,
							)

class        lowerCAmelCase_				:







				"""simple docstring"""




				def __init__( self     :Any  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :List[Any]=14  ,     lowerCamelCase__     :Union[str, Any]=7  ,     lowerCamelCase__     :Dict=True  ,     lowerCamelCase__     :Optional[int]=True  ,     lowerCamelCase__     :int=True  ,     lowerCamelCase__     :Optional[Any]=True  ,     lowerCamelCase__     :Any=True  ,     lowerCamelCase__     :List[str]=99  ,     lowerCamelCase__     :List[Any]=32  ,     lowerCamelCase__     :List[str]=5  ,     lowerCamelCase__     :Tuple=4  ,     lowerCamelCase__     :Dict=37  ,     lowerCamelCase__     :List[Any]="gelu"  ,     lowerCamelCase__     :Optional[Any]=0.1  ,     lowerCamelCase__     :int=0.1  ,     lowerCamelCase__     :Dict=5_12  ,     lowerCamelCase__     :str=16  ,     lowerCamelCase__     :Union[str, Any]=2  ,     lowerCamelCase__     :Union[str, Any]=0.02  ,     lowerCamelCase__     :Optional[int]=3  ,     lowerCamelCase__     :Tuple=4  ,     lowerCamelCase__     :str=None  ,     ):
					UpperCamelCase__ :List[Any]						=      parent
					UpperCamelCase__ :int						=      batch_size
					UpperCamelCase__ :Tuple						=      seq_length
					UpperCamelCase__ :Optional[Any]						=      is_training
					UpperCamelCase__ :int						=      use_token_type_ids
					UpperCamelCase__ :Dict						=      use_input_mask
					UpperCamelCase__ :Optional[int]						=      use_labels
					UpperCamelCase__ :Optional[Any]						=      use_mc_token_ids
					UpperCamelCase__ :Any						=      vocab_size
					UpperCamelCase__ :Union[str, Any]						=      hidden_size
					UpperCamelCase__ :Union[str, Any]						=      num_hidden_layers
					UpperCamelCase__ :Any						=      num_attention_heads
					UpperCamelCase__ :Optional[int]						=      intermediate_size
					UpperCamelCase__ :Tuple						=      hidden_act
					UpperCamelCase__ :List[str]						=      hidden_dropout_prob
					UpperCamelCase__ :Optional[Any]						=      attention_probs_dropout_prob
					UpperCamelCase__ :Tuple						=      max_position_embeddings
					UpperCamelCase__ :Optional[int]						=      type_vocab_size
					UpperCamelCase__ :List[str]						=      type_sequence_label_size
					UpperCamelCase__ :str						=      initializer_range
					UpperCamelCase__ :List[Any]						=      num_labels
					UpperCamelCase__ :Union[str, Any]						=      num_choices
					UpperCamelCase__ :Union[str, Any]						=      scope
					UpperCamelCase__ :int						=      self.vocab_size - 1







				def 				__a   ( self     :List[str]     ):
					UpperCamelCase__ :List[Any]						=      ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size     )

					UpperCamelCase__ :Optional[Any]						=      None
					if self.use_input_mask:
						UpperCamelCase__ :Tuple						=      random_attention_mask([self.batch_size, self.seq_length]     )

					UpperCamelCase__ :Any						=      None
					if self.use_token_type_ids:
						UpperCamelCase__ :Tuple						=      ids_tensor([self.batch_size, self.seq_length]  ,     self.type_vocab_size     )

					UpperCamelCase__ :int						=      None
					if self.use_mc_token_ids:
						UpperCamelCase__ :Optional[int]						=      ids_tensor([self.batch_size, self.num_choices]  ,     self.seq_length     )

					UpperCamelCase__ :int						=      None
					UpperCamelCase__ :str						=      None
					UpperCamelCase__ :Dict						=      None
					if self.use_labels:
						UpperCamelCase__ :List[str]						=      ids_tensor([self.batch_size]  ,     self.type_sequence_label_size     )
						UpperCamelCase__ :Tuple						=      ids_tensor([self.batch_size, self.seq_length]  ,     self.num_labels     )
						UpperCamelCase__ :List[str]						=      ids_tensor([self.batch_size]  ,     self.num_choices     )

					UpperCamelCase__ :List[Any]						=      self.get_config()

					UpperCamelCase__ :Optional[Any]						=      ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,     2     )

					return (
					    config,
					    input_ids,
					    input_mask,
					    head_mask,
					    token_type_ids,
					    mc_token_ids,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					)







				def 				__a   ( self     :Optional[Any]     ):
					return CTRLConfig(
					    vocab_size=self.vocab_size  ,     n_embd=self.hidden_size  ,     n_layer=self.num_hidden_layers  ,     n_head=self.num_attention_heads  ,     n_positions=self.max_position_embeddings  ,     pad_token_id=self.pad_token_id  ,     )







				def 				__a   ( self     :Union[str, Any]  ,     lowerCamelCase__     :Tuple  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Union[str, Any]  ,     lowerCamelCase__     :List[Any]  ,     lowerCamelCase__     :Optional[int]  ,     *lowerCamelCase__     :List[Any]     ):
					UpperCamelCase__ :List[str]						=      CTRLModel(config=lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()

					model(lowerCamelCase__  ,     token_type_ids=lowerCamelCase__  ,     head_mask=lowerCamelCase__     )
					model(lowerCamelCase__  ,     token_type_ids=lowerCamelCase__     )
					UpperCamelCase__ :Tuple						=      model(lowerCamelCase__     )
					self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)     )
					self.parent.assertEqual(len(result.past_key_values     )  ,     config.n_layer     )







				def 				__a   ( self     :Dict  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :List[Any]  ,     lowerCamelCase__     :List[str]  ,     lowerCamelCase__     :Union[str, Any]  ,     *lowerCamelCase__     :List[Any]     ):
					UpperCamelCase__ :Dict						=      CTRLLMHeadModel(lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()

					UpperCamelCase__ :List[Any]						=      model(lowerCamelCase__  ,     token_type_ids=lowerCamelCase__  ,     labels=lowerCamelCase__     )
					self.parent.assertEqual(result.loss.shape  ,     ()     )
					self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)     )







				def 				__a   ( self     :List[Any]     ):
					UpperCamelCase__ :Union[str, Any]						=      self.prepare_config_and_inputs()

					(
					    UpperCamelCase__		
					) :int						=      config_and_inputs

					UpperCamelCase__ :Optional[int]						=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

					return config, inputs_dict







				def 				__a   ( self     :Optional[int]  ,     lowerCamelCase__     :Dict  ,     lowerCamelCase__     :Dict  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Any  ,     *lowerCamelCase__     :Optional[int]     ):
					UpperCamelCase__ :List[str]						=      self.num_labels
					UpperCamelCase__ :Union[str, Any]						=      CTRLForSequenceClassification(lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()
					UpperCamelCase__ :Tuple						=      ids_tensor([self.batch_size]  ,     self.type_sequence_label_size     )
					UpperCamelCase__ :Union[str, Any]						=      model(lowerCamelCase__  ,     token_type_ids=lowerCamelCase__  ,     labels=lowerCamelCase__     )
					self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)     )

@require_torch
class        lowerCAmelCase_				(      lowercase						,      lowercase						,      lowercase						,      unittest.TestCase	):







				"""simple docstring"""




				_snake_case				:   List[str]        =			(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
				_snake_case				:   int        =			(CTRLLMHeadModel,) if is_torch_available() else ()
				_snake_case				:   List[Any]        =			(
				    {
				        'feature-extraction': CTRLModel,
				        'text-classification': CTRLForSequenceClassification,
				        'text-generation': CTRLLMHeadModel,
				        'zero-shot': CTRLForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				_snake_case				:   Dict        =			True
				_snake_case				:   Union[str, Any]        =			False
				_snake_case				:   Union[str, Any]        =			False







				def 				__a   ( self     :Union[str, Any]  ,     lowerCamelCase__     :str  ,     lowerCamelCase__     :str  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :Union[str, Any]  ,     lowerCamelCase__     :List[str]     ):






					if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
						# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
						# `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
						# config could not be created.
						return True

					return False







				def 				__a   ( self     :Dict     ):
					UpperCamelCase__ :List[str]						=      CTRLModelTester(self     )
					UpperCamelCase__ :Optional[Any]						=      ConfigTester(self  ,     config_class=lowerCamelCase__  ,     n_embd=37     )







				def 				__a   ( self     :Tuple     ):
					super().tearDown()
					# clean-up as much as possible GPU memory occupied by PyTorch
					gc.collect()
					torch.cuda.empty_cache()







				def 				__a   ( self     :Optional[int]     ):
					self.config_tester.run_common_tests()







				def 				__a   ( self     :List[str]     ):
					UpperCamelCase__ :Union[str, Any]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_model(*lowerCamelCase__     )







				def 				__a   ( self     :Any     ):
					UpperCamelCase__ :Any						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__     )







				@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""     )
				def 				__a   ( self     :str     ):
					pass







				@slow
				def 				__a   ( self     :Tuple     ):






					for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase__ :Optional[int]						=      CTRLModel.from_pretrained(lowerCamelCase__     )
						self.assertIsNotNone(lowerCamelCase__     )







				@unittest.skip("""The model doesn't support left padding"""     )  # and it's not used enough to be worth fixing :)
				def 				__a   ( self     :Dict     ):
					pass

@require_torch
class        lowerCAmelCase_				(      unittest.TestCase	):







				"""simple docstring"""




				def 				__a   ( self     :List[Any]     ):
					super().tearDown()
					# clean-up as much as possible GPU memory occupied by PyTorch
					gc.collect()
					torch.cuda.empty_cache()







				@slow
				def 				__a   ( self     :Optional[int]     ):
					UpperCamelCase__ :str						=      CTRLLMHeadModel.from_pretrained("""ctrl"""     )
					model.to(lowerCamelCase__     )
					UpperCamelCase__ :List[str]						=      torch.tensor(
					    [[1_18_59, 0, 16_11, 8]]  ,     dtype=torch.long  ,     device=lowerCamelCase__     )  # Legal the president is
					UpperCamelCase__ :Tuple						=      [
					    1_18_59,
					    0,
					    16_11,
					    8,
					    5,
					    1_50,
					    2_64_49,
					    2,
					    19,
					    3_48,
					    4_69,
					    3,
					    25_95,
					    48,
					    2_07_40,
					    24_65_33,
					    24_65_33,
					    19,
					    30,
					    5,
					]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

					UpperCamelCase__ :Dict						=      model.generate(lowerCamelCase__  ,     do_sample=lowerCamelCase__     )
					self.assertListEqual(output_ids[0].tolist()  ,     lowerCamelCase__     )







'''simple docstring'''
import copy

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


UpperCamelCase__			=      logging.get_logger(__name__)

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


class 							_UpperCAmelCase    (		snake_case      ):
    __lowerCamelCase: Union[str, Any]									=     'deta'
    __lowerCamelCase: str									=     {
        'hidden_size': 'd_model',
        'num_attention_heads': 'encoder_attention_heads',
    }




    def __init__(      self	:							List[str]	,  a	:							Union[str, Any]=None	,  a	:							Dict=9_0_0	,  a	:							str=2_0_4_8	,  a	:							Union[str, Any]=6	,  a	:							int=2_0_4_8	,  a	:							Optional[int]=8	,  a	:							Optional[Any]=6	,  a	:							Optional[Any]=1_0_2_4	,  a	:							Any=8	,  a	:							Dict=0.0	,  a	:							Optional[int]=True	,  a	:							Union[str, Any]="relu"	,  a	:							str=2_5_6	,  a	:							str=0.1	,  a	:							Any=0.0	,  a	:							Optional[Any]=0.0	,  a	:							str=0.02	,  a	:							int=1.0	,  a	:							Tuple=True	,  a	:							Optional[int]=False	,  a	:							str="sine"	,  a	:							List[Any]=5	,  a	:							str=4	,  a	:							Any=4	,  a	:							Optional[Any]=True	,  a	:							Union[str, Any]=3_0_0	,  a	:							List[Any]=True	,  a	:							Tuple=True	,  a	:							Any=1	,  a	:							int=5	,  a	:							Optional[int]=2	,  a	:							Tuple=1	,  a	:							List[str]=1	,  a	:							Tuple=5	,  a	:							Tuple=2	,  a	:							List[str]=0.1	,  a	:							Tuple=0.25	,  **a	:							Optional[int]	,  ):






       '''simple docstring'''






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

       lowercase_						:     Optional[int]							     =  backbone_config
       lowercase_						:     Dict							     =  num_queries
       lowercase_						:     Tuple							     =  max_position_embeddings
       lowercase_						:     Union[str, Any]							     =  d_model
       lowercase_						:     Optional[Any]							     =  encoder_ffn_dim
       lowercase_						:     Any							     =  encoder_layers
       lowercase_						:     int							     =  encoder_attention_heads
       lowercase_						:     Tuple							     =  decoder_ffn_dim
       lowercase_						:     Dict							     =  decoder_layers
       lowercase_						:     Dict							     =  decoder_attention_heads
       lowercase_						:     List[Any]							     =  dropout
       lowercase_						:     Union[str, Any]							     =  attention_dropout
       lowercase_						:     List[str]							     =  activation_dropout
       lowercase_						:     Optional[Any]							     =  activation_function
       lowercase_						:     str							     =  init_std
       lowercase_						:     Dict							     =  init_xavier_std
       lowercase_						:     List[Any]							     =  encoder_layerdrop
       lowercase_						:     Optional[int]							     =  auxiliary_loss
       lowercase_						:     str							     =  position_embedding_type
       # deformable attributes
       lowercase_						:     Optional[int]							     =  num_feature_levels
       lowercase_						:     List[Any]							     =  encoder_n_points
       lowercase_						:     Optional[Any]							     =  decoder_n_points
       lowercase_						:     List[Any]							     =  two_stage
       lowercase_						:     Any							     =  two_stage_num_proposals
       lowercase_						:     List[str]							     =  with_box_refine
       lowercase_						:     Optional[int]							     =  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
       lowercase_						:     int							     =  class_cost
       lowercase_						:     Optional[int]							     =  bbox_cost
       lowercase_						:     str							     =  giou_cost
       # Loss coefficients
       lowercase_						:     Optional[int]							     =  mask_loss_coefficient
       lowercase_						:     Union[str, Any]							     =  dice_loss_coefficient
       lowercase_						:     Optional[Any]							     =  bbox_loss_coefficient
       lowercase_						:     Optional[Any]							     =  giou_loss_coefficient
       lowercase_						:     Any							     =  eos_coefficient
       lowercase_						:     str							     =  focal_alpha
       super().__init__(is_encoder_decoder=a	,  **a      )




    @property
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       return self.encoder_attention_heads




    @property
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       return self.d_model





    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  copy.deepcopy(self.__dict__      )
       lowercase_						:     Union[str, Any]							     =  self.backbone_config.to_dict()
       lowercase_						:     str							     =  self.__class__.model_type
       return output




