

"""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  lowerCamelCase    :

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   ids_tensor([self.batch_size, self.seq_length]     ,					self.vocab_size							)

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

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

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

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

						SCREAMING_SNAKE_CASE__										=   self.get_config()

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

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

						SCREAMING_SNAKE_CASE__										=   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        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   self.prepare_config_and_inputs()

						(
						    (
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		
						)										=   config_and_inputs

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

						return config, inputs_dict





			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]     ,					__UpperCAmelCase					:    Union[str, Any]     ,					__UpperCAmelCase					:    Optional[int]     ,					__UpperCAmelCase					:    List[str]     ,					__UpperCAmelCase					:    Optional[Any]     ,					*__UpperCAmelCase					:    Any							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   self.num_labels
						SCREAMING_SNAKE_CASE__										=   CTRLForSequenceClassification(__UpperCAmelCase							)
						model.to(__UpperCAmelCase							)
						model.eval()
						SCREAMING_SNAKE_CASE__										=   ids_tensor([self.batch_size]     ,					self.type_sequence_label_size							)
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase     ,					token_type_ids=__UpperCAmelCase     ,					labels=__UpperCAmelCase							)
						self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.num_labels)							)





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

			def        SCREAMING_SNAKE_CASE					(       self					:    Any     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    Optional[Any]     ,					__UpperCAmelCase					:    Union[str, Any]     ,					__UpperCAmelCase					:    Union[str, Any]     ,					__UpperCAmelCase					:    int							) ->     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        SCREAMING_SNAKE_CASE					(       self					:    str							) ->     Optional[int]:
						SCREAMING_SNAKE_CASE__										=   CTRLModelTester(self							)
						SCREAMING_SNAKE_CASE__										=   ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					n_embd=3_7							)

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

			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     str:
						self.config_tester.run_common_tests()

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]							) ->     int:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_ctrl_model(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     int:
						SCREAMING_SNAKE_CASE__										=   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        SCREAMING_SNAKE_CASE					(       self					:    str							) ->     str:
						pass

			@slow
			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     int:
						for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE__										=   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        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     Optional[Any]:
						pass





@require_torch
class  lowerCamelCase    (unittest.TestCase      ):

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





			@slow
			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   CTRLLMHeadModel.from_pretrained("""ctrl"""							)
						model.to(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   torch.tensor(
						    [[1_1_8_5_9, 0, 1_6_1_1, 8]]     ,					dtype=torch.long     ,					device=__UpperCAmelCase							)  # Legal the president is
						SCREAMING_SNAKE_CASE__										=   [
						    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

						SCREAMING_SNAKE_CASE__										=   model.generate(__UpperCAmelCase     ,					do_sample=__UpperCAmelCase							)
						self.assertListEqual(output_ids[0].tolist()     ,					__UpperCAmelCase							)





"""simple docstring"""




from typing import List

from .keymap import KEYMAP, get_character

def 						A							(  snake_case__     ):




			'''simple docstring'''


			def decorator(snake_case__     ):
						SCREAMING_SNAKE_CASE__										=   getattr(snake_case__		,					"""handle_key"""		,					[]     )
						handle += [key]
						setattr(snake_case__		,					"""handle_key"""		,					snake_case__     )
						return func

			return decorator

def 						A							(  *snake_case__     ):




			'''simple docstring'''


			def decorator(snake_case__     ):
						SCREAMING_SNAKE_CASE__										=   getattr(snake_case__		,					"""handle_key"""		,					[]     )
						handle += keys
						setattr(snake_case__		,					"""handle_key"""		,					snake_case__     )
						return func

			return decorator





class  lowerCamelCase    (A__      ):

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

						for value in attrs.values():
									SCREAMING_SNAKE_CASE__										=   getattr(__UpperCAmelCase     ,					"""handle_key"""     ,					[]							)
									for key in handled_keys:
												SCREAMING_SNAKE_CASE__										=   value
						return new_cls





			@staticmethod
			def        SCREAMING_SNAKE_CASE					(       cls					:    Tuple							) ->     Dict:
						SCREAMING_SNAKE_CASE__										=   get_character()
						if char != KEYMAP["undefined"]:
									SCREAMING_SNAKE_CASE__										=   ord(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   cls.key_handler.get(__UpperCAmelCase							)
						if handler:
									SCREAMING_SNAKE_CASE__										=   char
									return handler(cls							)
						else:
									return None

def 						A							(  cls     ):




			'''simple docstring'''


			return KeyHandler(cls.__name__		,					cls.__bases__		,					cls.__dict__.copy()     )


