






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 			SCREAMING_SNAKE_CASE      :
     '''simple docstring'''


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


     def 					_A							(					self							:						List[Any]   ):
       SCREAMING_SNAKE_CASE							:   Tuple						    =     ids_tensor([self.batch_size, self.seq_length]       ,			self.vocab_size   )

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

       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     None
       if self.use_token_type_ids:
         SCREAMING_SNAKE_CASE							:   List[Any]						    =     ids_tensor([self.batch_size, self.seq_length]       ,			self.type_vocab_size   )

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

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

       SCREAMING_SNAKE_CASE							:   Dict						    =     self.get_config()

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

       SCREAMING_SNAKE_CASE							:   int						    =     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[Any]   ):
       SCREAMING_SNAKE_CASE							:   Tuple						    =     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
       )    ,	
       )							:   Optional[Any]						    =     config_and_inputs

       SCREAMING_SNAKE_CASE							:   Dict						    =     {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

       return config, inputs_dict






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





@require_torch
class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase   ,		lowerCAmelCase   ,		lowerCAmelCase   ,		unittest.TestCase       ):
     '''simple docstring'''


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


     def 					_A							(					self							:						Tuple       ,			UpperCAmelCase_							:						Optional[int]       ,			UpperCAmelCase_							:						Union[str, Any]       ,			UpperCAmelCase_							:						Optional[Any]       ,			UpperCAmelCase_							:						Tuple       ,			UpperCAmelCase_							:						Union[str, 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							:						str   ):
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     CTRLModelTester(self   )
       SCREAMING_SNAKE_CASE							:   Any						    =     ConfigTester(self       ,			config_class=UpperCAmelCase_       ,			n_embd=37   )


     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							:						Optional[Any]   ):
       self.config_tester.run_common_tests()


     def 					_A							(					self							:						Optional[int]   ):
       SCREAMING_SNAKE_CASE							:   Dict						    =     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_   )


     def 					_A							(					self							:						List[Any]   ):
       SCREAMING_SNAKE_CASE							:   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							:						int   ):
       pass


     @slow
     def 					_A							(					self							:						Any   ):
       for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_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							:						Dict   ):
       pass





@require_torch
class 			SCREAMING_SNAKE_CASE      (      unittest.TestCase       ):
     '''simple docstring'''


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






     @slow
     def 					_A							(					self							:						Dict   ):
       SCREAMING_SNAKE_CASE							:   Dict						    =     CTRLLMHeadModel.from_pretrained("ctrl"   )
       model.to(UpperCAmelCase_   )
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     torch.tensor(
           [[1_1859, 0, 1611, 8]]       ,			dtype=torch.long       ,			device=UpperCAmelCase_   )  # Legal the president is
       SCREAMING_SNAKE_CASE							:   List[str]						    =     [
           1_1859,
           0,
           1611,
           8,
           5,
           150,
           2_6449,
           2,
           19,
           348,
           469,
           3,
           2595,
           48,
           2_0740,
           24_6533,
           24_6533,
           19,
           30,
           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							:   str						    =     model.generate(UpperCAmelCase_       ,			do_sample=UpperCAmelCase_   )
       self.assertListEqual(output_ids[0].tolist()       ,			UpperCAmelCase_   )










'''simple docstring'''

def _SCREAMING_SNAKE_CASE (A			)			->			bool:






       """simple docstring"""







       if not isinstance(A    ,					A			):
              raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
       if len(A			) == 0:
              raise ValueError('''Input list must be a non empty list'''			)
       if len(A			) == 1:
              return True
       lowercase__      =					series[1] - series[0]
       for index in range(len(A			) - 1			):
              if series[index + 1] - series[index] != common_diff:
                     return False
       return True

def _SCREAMING_SNAKE_CASE (A			)			->			float:






       """simple docstring"""







       if not isinstance(A    ,					A			):
              raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
       if len(A			) == 0:
              raise ValueError('''Input list must be a non empty list'''			)
       lowercase__      =					0
       for val in series:
              answer += val
       return answer / len(A			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()




