





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





     """simple docstring"""



     def __init__(							self				:Optional[int]	,					snake_case				:int	,					snake_case				:int=14	,					snake_case				:str=7	,					snake_case				:Dict=True	,					snake_case				:Union[str, Any]=True	,					snake_case				:Optional[int]=True	,					snake_case				:Any=True	,					snake_case				:List[Any]=True	,					snake_case				:Union[str, Any]=99	,					snake_case				:Any=32	,					snake_case				:Optional[Any]=5	,					snake_case				:Optional[int]=4	,					snake_case				:List[Any]=37	,					snake_case				:Optional[int]="gelu"	,					snake_case				:Union[str, Any]=0.1	,					snake_case				:List[Any]=0.1	,					snake_case				:int=512	,					snake_case				:List[Any]=16	,					snake_case				:Any=2	,					snake_case				:Any=0.02	,					snake_case				:str=3	,					snake_case				:Any=4	,					snake_case				:str=None	,					):





        '''simple docstring'''




        A_    : Dict								=	parent
        A_    : List[str]								=	batch_size
        A_    : int								=	seq_length
        A_    : List[str]								=	is_training
        A_    : List[Any]								=	use_token_type_ids
        A_    : str								=	use_input_mask
        A_    : Dict								=	use_labels
        A_    : Tuple								=	use_mc_token_ids
        A_    : Optional[int]								=	vocab_size
        A_    : Union[str, Any]								=	hidden_size
        A_    : Any								=	num_hidden_layers
        A_    : List[Any]								=	num_attention_heads
        A_    : List[Any]								=	intermediate_size
        A_    : Any								=	hidden_act
        A_    : str								=	hidden_dropout_prob
        A_    : List[Any]								=	attention_probs_dropout_prob
        A_    : str								=	max_position_embeddings
        A_    : Any								=	type_vocab_size
        A_    : Optional[Any]								=	type_sequence_label_size
        A_    : Optional[int]								=	initializer_range
        A_    : Dict								=	num_labels
        A_    : Any								=	num_choices
        A_    : Any								=	scope
        A_    : Optional[Any]								=	self.vocab_size - 1



     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        A_    : Union[str, Any]								=	ids_tensor([self.batch_size, self.seq_length]	,					self.vocab_size     )

        A_    : List[str]								=	None
        if self.use_input_mask:
           A_    : Any								=	random_attention_mask([self.batch_size, self.seq_length]     )

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

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

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

        A_    : Dict								=	self.get_config()

        A_    : 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   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''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   SCREAMING_SNAKE_CASE						(							self				:List[str]	,					snake_case				:str	,					snake_case				:Dict	,					snake_case				:str	,					snake_case				:str	,					snake_case				:Optional[Any]	,					*snake_case				:List[str]     ):





        '''simple docstring'''




        A_    : int								=	CTRLModel(config=_a     )
        model.to(_a     )
        model.eval()

        model(_a	,					token_type_ids=_a	,					head_mask=_a     )
        model(_a	,					token_type_ids=_a     )
        A_    : Tuple								=	model(_a     )
        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				:Optional[Any]	,					snake_case				:List[str]	,					snake_case				:Tuple	,					snake_case				:List[str]	,					snake_case				:List[str]	,					snake_case				:str	,					*snake_case				:List[Any]     ):





        '''simple docstring'''




        A_    : int								=	CTRLLMHeadModel(_a     )
        model.to(_a     )
        model.eval()

        A_    : Tuple								=	model(_a	,					token_type_ids=_a	,					labels=_a     )
        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				:str     ):





        '''simple docstring'''




        A_    : Dict								=	self.prepare_config_and_inputs()

        (
            (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,       
        )    : int								=	config_and_inputs

        A_    : Optional[Any]								=	{"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

        return config, inputs_dict





     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:Tuple	,					snake_case				:str	,					snake_case				:Tuple	,					snake_case				:Union[str, Any]	,					*snake_case				:Any     ):





        '''simple docstring'''




        A_    : Dict								=	self.num_labels
        A_    : Optional[Any]								=	CTRLForSequenceClassification(_a     )
        model.to(_a     )
        model.eval()
        A_    : Tuple								=	ids_tensor([self.batch_size]	,					self.type_sequence_label_size     )
        A_    : Optional[int]								=	model(_a	,					token_type_ids=_a	,					labels=_a     )
        self.parent.assertEqual(result.logits.shape	,					(self.batch_size, self.num_labels)     )




@require_torch
class     __magic_name__     (							__SCREAMING_SNAKE_CASE			, __SCREAMING_SNAKE_CASE			, __SCREAMING_SNAKE_CASE			, unittest.TestCase							):





     """simple docstring"""



     __UpperCamelCase    			=   (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
     __UpperCamelCase    			=   (CTRLLMHeadModel,) if is_torch_available() else ()
     __UpperCamelCase    			=   (
         {
             'feature-extraction': CTRLModel,
             'text-classification': CTRLForSequenceClassification,
             'text-generation': CTRLLMHeadModel,
             'zero-shot': CTRLForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __UpperCamelCase    			=   True
     __UpperCamelCase    			=   False
     __UpperCamelCase    			=   False



     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:Any	,					snake_case				:Any	,					snake_case				:Any	,					snake_case				:List[Any]	,					snake_case				:Optional[Any]     ):





        '''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   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        A_    : Dict								=	CTRLModelTester(self     )
        A_    : Dict								=	ConfigTester(self	,					config_class=_a	,					n_embd=37     )



     def   SCREAMING_SNAKE_CASE						(							self				:Union[str, Any]     ):





        '''simple docstring'''




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



     def   SCREAMING_SNAKE_CASE						(							self				:Dict     ):





        '''simple docstring'''




        self.config_tester.run_common_tests()



     def   SCREAMING_SNAKE_CASE						(							self				:Any     ):





        '''simple docstring'''




        A_    : Tuple								=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*_a     )



     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]     ):





        '''simple docstring'''




        A_    : Tuple								=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*_a     )



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





        '''simple docstring'''




        pass



     @slow
     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]     ):





        '''simple docstring'''




        for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A_    : Union[str, Any]								=	CTRLModel.from_pretrained(_a     )
           self.assertIsNotNone(_a     )





     @unittest.skip("The model doesn\'t support left padding"     )  # and it's not used enough to be worth fixing :)
     def   SCREAMING_SNAKE_CASE						(							self				:Union[str, Any]     ):





        '''simple docstring'''




        pass




@require_torch
class     __magic_name__     (							unittest.TestCase							):





     """simple docstring"""



     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''simple docstring'''




        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]     ):





        '''simple docstring'''




        A_    : int								=	CTRLLMHeadModel.from_pretrained("ctrl"     )
        model.to(_a     )
        A_    : List[Any]								=	torch.tensor(
            [[11_859, 0, 1_611, 8]]	,					dtype=torch.long	,					device=_a     )  # Legal the president is
        A_    : List[Any]								=	[
            11_859,
            0,
            1_611,
            8,
            5,
            150,
            26_449,
            2,
            19,
            348,
            469,
            3,
            2_595,
            48,
            20_740,
            246_533,
            246_533,
            19,
            30,
            5,
        ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

        A_    : Union[str, Any]								=	model.generate(_a	,					do_sample=_a     )
        self.assertListEqual(output_ids[0].tolist()	,					_a     )




"""simple docstring"""




lowercase_			     =  [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
lowercase_			     =  [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
lowercase_			     =  {
    0: "Sunday",
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday",
    6: "Saturday",
}

def        lowercase							(		lowerCAmelCase__ :     int    ,					lowerCAmelCase__ :     int    ,					lowerCAmelCase__ :     int			) ->      str:
		assert len(str(lowerCAmelCase__			)			) > 2, "year should be in YYYY format"
		assert 1 <= month <= 12, "month should be between 1 to 12"
		assert 1 <= day <= 31, "day should be between 1 to 31"

		# Doomsday algorithm:
		__a           =			year // 100
		__a           =			(5 * (century % 4) + 2) % 7
		__a           =			year % 100
		__a           =			centurian % 12
		__a           =			(
		    (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
		) % 7
		__a           =			(
		    DOOMSDAY_NOT_LEAP[month - 1]
		    if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
		    else DOOMSDAY_LEAP[month - 1]
		)
		__a           =			(dooms_day + day - day_anchor) % 7
		return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
				import doctest

				doctest.testmod()






