


'''simple docstring'''






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 						__UpperCamelCase	:





							def __init__(  self		:List[Any]  ,_UpperCamelCase		:str  ,_UpperCamelCase		:Tuple=1_4  ,_UpperCamelCase		:Union[str, Any]=7  ,_UpperCamelCase		:Any=True  ,_UpperCamelCase		:List[Any]=True  ,_UpperCamelCase		:Tuple=True  ,_UpperCamelCase		:Optional[Any]=True  ,_UpperCamelCase		:List[Any]=True  ,_UpperCamelCase		:str=9_9  ,_UpperCamelCase		:List[Any]=3_2  ,_UpperCamelCase		:List[str]=5  ,_UpperCamelCase		:Optional[int]=4  ,_UpperCamelCase		:Optional[int]=3_7  ,_UpperCamelCase		:int="gelu"  ,_UpperCamelCase		:str=0.1  ,_UpperCamelCase		:Optional[int]=0.1  ,_UpperCamelCase		:int=5_1_2  ,_UpperCamelCase		:Union[str, Any]=1_6  ,_UpperCamelCase		:Union[str, Any]=2  ,_UpperCamelCase		:List[Any]=0.02  ,_UpperCamelCase		:Any=3  ,_UpperCamelCase		:Tuple=4  ,_UpperCamelCase		:str=None  ,):
									snake_case_		:							Dict			       =						parent
									snake_case_		:							Optional[int]			       =						batch_size
									snake_case_		:							Union[str, Any]			       =						seq_length
									snake_case_		:							Union[str, Any]			       =						is_training
									snake_case_		:							Tuple			       =						use_token_type_ids
									snake_case_		:							Union[str, Any]			       =						use_input_mask
									snake_case_		:							Dict			       =						use_labels
									snake_case_		:							Optional[int]			       =						use_mc_token_ids
									snake_case_		:							Dict			       =						vocab_size
									snake_case_		:							List[Any]			       =						hidden_size
									snake_case_		:							List[Any]			       =						num_hidden_layers
									snake_case_		:							Union[str, Any]			       =						num_attention_heads
									snake_case_		:							Optional[Any]			       =						intermediate_size
									snake_case_		:							str			       =						hidden_act
									snake_case_		:							int			       =						hidden_dropout_prob
									snake_case_		:							Dict			       =						attention_probs_dropout_prob
									snake_case_		:							Union[str, Any]			       =						max_position_embeddings
									snake_case_		:							Tuple			       =						type_vocab_size
									snake_case_		:							Any			       =						type_sequence_label_size
									snake_case_		:							Dict			       =						initializer_range
									snake_case_		:							Tuple			       =						num_labels
									snake_case_		:							str			       =						num_choices
									snake_case_		:							int			       =						scope
									snake_case_		:							str			       =						self.vocab_size - 1





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							List[Any]			       =						ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size			)

									snake_case_		:							Union[str, Any]			       =						None
									if self.use_input_mask:
											snake_case_		:							Optional[Any]			       =						random_attention_mask([self.batch_size, self.seq_length]			)

									snake_case_		:							Tuple			       =						None
									if self.use_token_type_ids:
											snake_case_		:							Optional[int]			       =						ids_tensor([self.batch_size, self.seq_length]  ,self.type_vocab_size			)

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

									snake_case_		:							Optional[Any]			       =						None
									snake_case_		:							str			       =						None
									snake_case_		:							Optional[Any]			       =						None
									if self.use_labels:
											snake_case_		:							int			       =						ids_tensor([self.batch_size]  ,self.type_sequence_label_size			)
											snake_case_		:							int			       =						ids_tensor([self.batch_size, self.seq_length]  ,self.num_labels			)
											snake_case_		:							Tuple			       =						ids_tensor([self.batch_size]  ,self.num_choices			)

									snake_case_		:							Any			       =						self.get_config()

									snake_case_		:							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		:Tuple			):
									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		:List[Any]  ,_UpperCamelCase		:int  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:int  ,*_UpperCamelCase		:Union[str, Any]			):
									snake_case_		:							Tuple			       =						CTRLModel(config=_UpperCamelCase			)
									model.to(_UpperCamelCase			)
									model.eval()

									model(_UpperCamelCase  ,token_type_ids=_UpperCamelCase  ,head_mask=_UpperCamelCase			)
									model(_UpperCamelCase  ,token_type_ids=_UpperCamelCase			)
									snake_case_		:							Optional[Any]			       =						model(_UpperCamelCase			)
									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		:Optional[Any]  ,_UpperCamelCase		:Tuple  ,_UpperCamelCase		:Dict  ,_UpperCamelCase		:Tuple  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:List[Any]  ,*_UpperCamelCase		:int			):
									snake_case_		:							Union[str, Any]			       =						CTRLLMHeadModel(_UpperCamelCase			)
									model.to(_UpperCamelCase			)
									model.eval()

									snake_case_		:							Tuple			       =						model(_UpperCamelCase  ,token_type_ids=_UpperCamelCase  ,labels=_UpperCamelCase			)
									self.parent.assertEqual(result.loss.shape  ,()			)
									self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)			)





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							Union[str, Any]			       =						self.prepare_config_and_inputs()

									(
									    (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      (
									    snake_case_
									)       ,      
									)		:							List[str]			       =						config_and_inputs

									snake_case_		:							Union[str, Any]			       =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

									return config, inputs_dict





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





@require_torch
class 						__UpperCamelCase	(						lowercase__						,  lowercase__						,  lowercase__						,  unittest.TestCase							):
							lowercase			:		Optional[int]   		=  (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
							lowercase			:		List[str]   		=  (CTRLLMHeadModel,) if is_torch_available() else ()
							lowercase			:		List[str]   		=  (
							    {
							        'feature-extraction': CTRLModel,
							        'text-classification': CTRLForSequenceClassification,
							        'text-generation': CTRLLMHeadModel,
							        'zero-shot': CTRLForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowercase			:		Dict   		=  True
							lowercase			:		Optional[Any]   		=  False
							lowercase			:		int   		=  False





							def 		a__   (  self		:Dict  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:List[Any]  ,_UpperCamelCase		:Dict  ,_UpperCamelCase		:Any  ,_UpperCamelCase		:Optional[Any]			):
									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		:Any			):
									snake_case_		:							int			       =						CTRLModelTester(self			)
									snake_case_		:							Dict			       =						ConfigTester(self  ,config_class=_UpperCamelCase  ,n_embd=3_7			)





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





							def 		a__   (  self		:Tuple			):
									self.config_tester.run_common_tests()





							def 		a__   (  self		:Optional[Any]			):
									snake_case_		:							Tuple			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_ctrl_model(*_UpperCamelCase			)





							def 		a__   (  self		:int			):
									snake_case_		:							Optional[Any]			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*_UpperCamelCase			)





							@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""			)
							def 		a__   (  self		:List[Any]			):
									pass





							@slow
							def 		a__   (  self		:int			):
									for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case_		:							Any			       =						CTRLModel.from_pretrained(_UpperCamelCase			)
											self.assertIsNotNone(_UpperCamelCase			)





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





@require_torch
class 						__UpperCamelCase	(						unittest.TestCase							):





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





							@slow
							def 		a__   (  self		:str			):
									snake_case_		:							Optional[int]			       =						CTRLLMHeadModel.from_pretrained("""ctrl"""			)
									model.to(_UpperCamelCase			)
									snake_case_		:							Optional[Any]			       =						torch.tensor(
									    [[1_1_8_5_9, 0, 1_6_1_1, 8]]  ,dtype=torch.long  ,device=_UpperCamelCase			)  # Legal the president is
									snake_case_		:							List[str]			       =						[
									    1_1_8_5_9,
									    0,
									    1_6_1_1,
									    8,
									    5,
									    1_5_0,
									    2_6_4_4_9,
									    2,
									    1_9,
									    3_4_8,
									    4_6_9,
									    3,
									    2_5_9_5,
									    4_8,
									    2_0_7_4_0,
									    2_4_6_5_3_3,
									    2_4_6_5_3_3,
									    1_9,
									    3_0,
									    5,
									]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

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



'''simple docstring'''






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


__A	:	Optional[int]     =     logging.get_logger(__name__)

__A	:	List[Any]     =     {
    'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json',
    'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json',
}
class 						__UpperCamelCase	(						lowercase__							):
							lowercase			:		Union[str, Any]   		=  'luke'





							def __init__(  self		:List[str]  ,_UpperCamelCase		:Tuple=5_0_2_6_7  ,_UpperCamelCase		:Tuple=5_0_0_0_0_0  ,_UpperCamelCase		:str=7_6_8  ,_UpperCamelCase		:List[Any]=2_5_6  ,_UpperCamelCase		:List[str]=1_2  ,_UpperCamelCase		:str=1_2  ,_UpperCamelCase		:Union[str, Any]=3_0_7_2  ,_UpperCamelCase		:int="gelu"  ,_UpperCamelCase		:int=0.1  ,_UpperCamelCase		:Dict=0.1  ,_UpperCamelCase		:Dict=5_1_2  ,_UpperCamelCase		:Optional[Any]=2  ,_UpperCamelCase		:int=0.02  ,_UpperCamelCase		:Any=1E-1_2  ,_UpperCamelCase		:Tuple=True  ,_UpperCamelCase		:Any=None  ,_UpperCamelCase		:Any=1  ,_UpperCamelCase		:int=0  ,_UpperCamelCase		:List[Any]=2  ,**_UpperCamelCase		:str  ,):
									super().__init__(pad_token_id=_UpperCamelCase  ,bos_token_id=_UpperCamelCase  ,eos_token_id=_UpperCamelCase  ,**_UpperCamelCase			)

									snake_case_		:							Any			       =						vocab_size
									snake_case_		:							List[Any]			       =						entity_vocab_size
									snake_case_		:							Optional[Any]			       =						hidden_size
									snake_case_		:							List[Any]			       =						entity_emb_size
									snake_case_		:							Dict			       =						num_hidden_layers
									snake_case_		:							Union[str, Any]			       =						num_attention_heads
									snake_case_		:							Dict			       =						hidden_act
									snake_case_		:							Dict			       =						intermediate_size
									snake_case_		:							Any			       =						hidden_dropout_prob
									snake_case_		:							List[Any]			       =						attention_probs_dropout_prob
									snake_case_		:							Optional[Any]			       =						max_position_embeddings
									snake_case_		:							Any			       =						type_vocab_size
									snake_case_		:							int			       =						initializer_range
									snake_case_		:							Union[str, Any]			       =						layer_norm_eps
									snake_case_		:							Tuple			       =						use_entity_aware_attention
									snake_case_		:							Tuple			       =						classifier_dropout