



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  :




 def __init__(    self						,					__UpperCAmelCase						,					__UpperCAmelCase=14						,					__UpperCAmelCase=7						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=99						,					__UpperCAmelCase=32						,					__UpperCAmelCase=5						,					__UpperCAmelCase=4						,					__UpperCAmelCase=37						,					__UpperCAmelCase="gelu"						,					__UpperCAmelCase=0.1						,					__UpperCAmelCase=0.1						,					__UpperCAmelCase=512						,					__UpperCAmelCase=16						,					__UpperCAmelCase=2						,					__UpperCAmelCase=0.02						,					__UpperCAmelCase=3						,					__UpperCAmelCase=4						,					__UpperCAmelCase=None						,					):



    '''simple docstring'''
    __lowerCamelCase    				=     parent
    __lowerCamelCase    				=     batch_size
    __lowerCamelCase    				=     seq_length
    __lowerCamelCase    				=     is_training
    __lowerCamelCase    				=     use_token_type_ids
    __lowerCamelCase    				=     use_input_mask
    __lowerCamelCase    				=     use_labels
    __lowerCamelCase    				=     use_mc_token_ids
    __lowerCamelCase    				=     vocab_size
    __lowerCamelCase    				=     hidden_size
    __lowerCamelCase    				=     num_hidden_layers
    __lowerCamelCase    				=     num_attention_heads
    __lowerCamelCase    				=     intermediate_size
    __lowerCamelCase    				=     hidden_act
    __lowerCamelCase    				=     hidden_dropout_prob
    __lowerCamelCase    				=     attention_probs_dropout_prob
    __lowerCamelCase    				=     max_position_embeddings
    __lowerCamelCase    				=     type_vocab_size
    __lowerCamelCase    				=     type_sequence_label_size
    __lowerCamelCase    				=     initializer_range
    __lowerCamelCase    				=     num_labels
    __lowerCamelCase    				=     num_choices
    __lowerCamelCase    				=     scope
    __lowerCamelCase    				=     self.vocab_size - 1




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size					)

    __lowerCamelCase    				=     None
    if self.use_input_mask:
       __lowerCamelCase    				=     random_attention_mask([self.batch_size, self.seq_length]					)

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

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

    __lowerCamelCase    				=     None
    __lowerCamelCase    				=     None
    __lowerCamelCase    				=     None
    if self.use_labels:
       __lowerCamelCase    				=     ids_tensor([self.batch_size]						,					self.type_sequence_label_size					)
       __lowerCamelCase    				=     ids_tensor([self.batch_size, self.seq_length]						,					self.num_labels					)
       __lowerCamelCase    				=     ids_tensor([self.batch_size]						,					self.num_choices					)

    __lowerCamelCase    				=     self.get_config()

    __lowerCamelCase    				=     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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    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 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					*__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     CTRLModel(config=__snake_case					)
    model.to(__snake_case					)
    model.eval()

    model(__snake_case						,					token_type_ids=__snake_case						,					head_mask=__snake_case					)
    model(__snake_case						,					token_type_ids=__snake_case					)
    __lowerCamelCase    				=     model(__snake_case					)
    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 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					*__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     CTRLLMHeadModel(__snake_case					)
    model.to(__snake_case					)
    model.eval()

    __lowerCamelCase    				=     model(__snake_case						,					token_type_ids=__snake_case						,					labels=__snake_case					)
    self.parent.assertEqual(result.loss.shape						,					()					)
    self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length, self.vocab_size)					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.prepare_config_and_inputs()

    (
        __lowerCamelCase			
    )    				=     config_and_inputs

    __lowerCamelCase    				=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

    return config, inputs_dict




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					*__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.num_labels
    __lowerCamelCase    				=     CTRLForSequenceClassification(__snake_case					)
    model.to(__snake_case					)
    model.eval()
    __lowerCamelCase    				=     ids_tensor([self.batch_size]						,					self.type_sequence_label_size					)
    __lowerCamelCase    				=     model(__snake_case						,					token_type_ids=__snake_case						,					labels=__snake_case					)
    self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_labels)					)



@require_torch
class 			__lowerCAmelCase  (						SCREAMING_SNAKE_CASE_	,						SCREAMING_SNAKE_CASE_	,						SCREAMING_SNAKE_CASE_	,						unittest.TestCase ):
 lowerCAmelCase__							       =	(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
 lowerCAmelCase__							       =	(CTRLLMHeadModel,) if is_torch_available() else ()
 lowerCAmelCase__							       =	(
     {
         """feature-extraction""": CTRLModel,
         """text-classification""": CTRLForSequenceClassification,
         """text-generation""": CTRLLMHeadModel,
         """zero-shot""": CTRLForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowerCAmelCase__							       =	True
 lowerCAmelCase__							       =	False
 lowerCAmelCase__							       =	False




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase					):



    '''simple docstring'''
    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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     CTRLModelTester(self					)
    __lowerCamelCase    				=     ConfigTester(self						,					config_class=__snake_case						,					n_embd=37					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    super().tearDown()
    # clean-up as much as possible GPU memory occupied by PyTorch
    gc.collect()
    torch.cuda.empty_cache()




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    self.config_tester.run_common_tests()




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_model(*__snake_case					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(*__snake_case					)




 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''					)
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    pass




 @slow
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCamelCase    				=     CTRLModel.from_pretrained(__snake_case					)
       self.assertIsNotNone(__snake_case					)




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



    '''simple docstring'''
    pass



@require_torch
class 			__lowerCAmelCase  (						unittest.TestCase ):




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    super().tearDown()
    # clean-up as much as possible GPU memory occupied by PyTorch
    gc.collect()
    torch.cuda.empty_cache()




 @slow
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     CTRLLMHeadModel.from_pretrained('''ctrl'''					)
    model.to(__snake_case					)
    __lowerCamelCase    				=     torch.tensor(
        [[11859, 0, 1611, 8]]						,					dtype=torch.long						,					device=__snake_case					)  # Legal the president is
    __lowerCamelCase    				=     [
        11859,
        0,
        1611,
        8,
        5,
        150,
        26449,
        2,
        19,
        348,
        469,
        3,
        2595,
        48,
        20740,
        246533,
        246533,
        19,
        30,
        5,
    ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

    __lowerCamelCase    				=     model.generate(__snake_case						,					do_sample=__snake_case					)
    self.assertListEqual(output_ids[0].tolist()						,					__snake_case					)














"""simple docstring"""





from typing import List

from .keymap import KEYMAP, get_character



def   a    (     __UpperCAmelCase      :    str  )    ->							Optional[int]:

						def decorator(__UpperCAmelCase      :    List[str]  ):
												__magic_name__:	Union[str, Any]							=       getattr(__UpperCAmelCase      ,   """handle_key"""      ,   []  )
												handle += [key]
												setattr(__UpperCAmelCase      ,   """handle_key"""      ,   __UpperCAmelCase  )
												return func

						return decorator



def   a    (     *__UpperCAmelCase      :    List[str]  )    ->							Union[str, Any]:

						def decorator(__UpperCAmelCase      :    Tuple  ):
												__magic_name__:	Union[str, Any]							=       getattr(__UpperCAmelCase      ,   """handle_key"""      ,   []  )
												handle += keys
												setattr(__UpperCAmelCase      ,   """handle_key"""      ,   __UpperCAmelCase  )
												return func

						return decorator



class       __A		(   SCREAMING_SNAKE_CASE_			):

					def __new__(			cls  :      Optional[Any]     ,		__snake_case  :      List[str]     ,		__snake_case  :      Optional[Any]     ,		__snake_case  :      Any					)			->							List[Any]:
											__magic_name__:	str							=       super().__new__(cls     ,		__snake_case     ,		__snake_case     ,		__snake_case					)
											if not hasattr(__snake_case     ,		"""key_handler"""					):
																	setattr(__snake_case     ,		"""key_handler"""     ,		{}					)
											setattr(__snake_case     ,		"""handle_input"""     ,		KeyHandler.handle_input					)

											for value in attrs.values():
																	__magic_name__:	List[str]							=       getattr(__snake_case     ,		"""handle_key"""     ,		[]					)
																	for key in handled_keys:
																							__magic_name__:	Any							=       value
											return new_cls






					@staticmethod
					def   lowerCamelCase__   (			cls  :      List[Any]					)			->							Optional[Any]:
											__magic_name__:	List[str]							=       get_character()
											if char != KEYMAP["undefined"]:
																	__magic_name__:	Union[str, Any]							=       ord(__snake_case					)
											__magic_name__:	Dict							=       cls.key_handler.get(__snake_case					)
											if handler:
																	__magic_name__:	List[str]							=       char
																	return handler(cls					)
											else:
																	return None



def   a    (     cls      :    List[Any]  )    ->							int:
						return KeyHandler(cls.__name__      ,   cls.__bases__      ,   cls.__dict__.copy()  )



