


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

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

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

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

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

             a_      :	Dict							=						self.get_config()

             a_      :	int							=						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       : Tuple  )	->    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       : int  ,							SCREAMING_SNAKE_CASE__       : Dict  ,							SCREAMING_SNAKE_CASE__       : Optional[int]  ,							SCREAMING_SNAKE_CASE__       : Any  ,							SCREAMING_SNAKE_CASE__       : Any  ,							SCREAMING_SNAKE_CASE__       : Tuple  ,							*SCREAMING_SNAKE_CASE__       : Any  )	->    Dict:
             a_      :	str							=						CTRLModel(config=SCREAMING_SNAKE_CASE__  )
             model.to(SCREAMING_SNAKE_CASE__  )
             model.eval()

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

             a_      :	Optional[Any]							=						model(SCREAMING_SNAKE_CASE__  ,							token_type_ids=SCREAMING_SNAKE_CASE__  ,							labels=SCREAMING_SNAKE_CASE__  )
             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       : Any  )	->    Optional[Any]:
             a_      :	Optional[Any]							=						self.prepare_config_and_inputs()

             (
                 a_	
             )      :	Optional[int]							=						config_and_inputs

             a_      :	int							=						{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

             return config, inputs_dict

      def  SCREAMING_SNAKE_CASE							( self       : Any  ,							SCREAMING_SNAKE_CASE__       : Optional[Any]  ,							SCREAMING_SNAKE_CASE__       : Optional[int]  ,							SCREAMING_SNAKE_CASE__       : Optional[int]  ,							SCREAMING_SNAKE_CASE__       : List[Any]  ,							*SCREAMING_SNAKE_CASE__       : Optional[int]  )	->    Tuple:
             a_      :	List[str]							=						self.num_labels
             a_      :	List[str]							=						CTRLForSequenceClassification(SCREAMING_SNAKE_CASE__  )
             model.to(SCREAMING_SNAKE_CASE__  )
             model.eval()
             a_      :	Tuple							=						ids_tensor([self.batch_size]  ,							self.type_sequence_label_size  )
             a_      :	str							=						model(SCREAMING_SNAKE_CASE__  ,							token_type_ids=SCREAMING_SNAKE_CASE__  ,							labels=SCREAMING_SNAKE_CASE__  )
             self.parent.assertEqual(result.logits.shape  ,							(self.batch_size, self.num_labels)  )
@require_torch
class 				SCREAMING_SNAKE_CASE__						(       lowercase__		,					lowercase__		,					lowercase__		,					unittest.TestCase    ):
      snake_case__       :  Any       =		(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
      snake_case__       :  Tuple       =		(CTRLLMHeadModel,) if is_torch_available() else ()
      snake_case__       :  List[str]       =		(
          {
              '''feature-extraction''': CTRLModel,
              '''text-classification''': CTRLForSequenceClassification,
              '''text-generation''': CTRLLMHeadModel,
              '''zero-shot''': CTRLForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case__       :  List[str]       =		True
      snake_case__       :  Dict       =		False
      snake_case__       :  Tuple       =		False
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  ,							SCREAMING_SNAKE_CASE__       : Tuple  ,							SCREAMING_SNAKE_CASE__       : int  ,							SCREAMING_SNAKE_CASE__       : Dict  ,							SCREAMING_SNAKE_CASE__       : int  ,							SCREAMING_SNAKE_CASE__       : Union[str, Any]  )	->    List[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       : Any  )	->    List[str]:
             a_      :	int							=						CTRLModelTester(self  )
             a_      :	Any							=						ConfigTester(self  ,							config_class=SCREAMING_SNAKE_CASE__  ,							n_embd=3_7  )
      def  SCREAMING_SNAKE_CASE							( self       : Tuple  )	->    Optional[int]:
             super().tearDown()
             # clean-up as much as possible GPU memory occupied by PyTorch
             gc.collect()
             torch.cuda.empty_cache()
      def  SCREAMING_SNAKE_CASE							( self       : str  )	->    Union[str, Any]:
             self.config_tester.run_common_tests()
      def  SCREAMING_SNAKE_CASE							( self       : Union[str, Any]  )	->    Optional[Any]:
             a_      :	Optional[Any]							=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_ctrl_model(*SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : int  )	->    List[str]:
             a_      :	int							=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE__  )
      @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'  )
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    Optional[int]:
             pass
      @slow
      def  SCREAMING_SNAKE_CASE							( self       : Any  )	->    Union[str, Any]:
             for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_      :	Optional[Any]							=						CTRLModel.from_pretrained(SCREAMING_SNAKE_CASE__  )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__  )

      @unittest.skip('The model doesn\'t support left padding'  )  # and it's not used enough to be worth fixing :)
      def  SCREAMING_SNAKE_CASE							( self       : str  )	->    Any:
             pass
@require_torch
class 				SCREAMING_SNAKE_CASE__						(       unittest.TestCase    ):
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    Union[str, 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       : str  )	->    Any:
             a_      :	Tuple							=						CTRLLMHeadModel.from_pretrained('ctrl'  )
             model.to(SCREAMING_SNAKE_CASE__  )
             a_      :	List[str]							=						torch.tensor(
                 [[1_1_8_5_9, 0, 1_6_1_1, 8]]  ,							dtype=torch.long  ,							device=SCREAMING_SNAKE_CASE__  )  # Legal the president is
             a_      :	Optional[Any]							=						[
                 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

             a_      :	List[Any]							=						model.generate(SCREAMING_SNAKE_CASE__  ,							do_sample=SCREAMING_SNAKE_CASE__  )
             self.assertListEqual(output_ids[0].tolist()  ,							SCREAMING_SNAKE_CASE__  )






def  SCREAMING_SNAKE_CASE_		(    __A				:    str       )			->	list:

       """simple docstring"""

       return [
           txt[:a] + txt[a].upper() + txt[a + 1 :]
           for a in range(len(__A       )       )
           if txt[a].isalpha()
       ]


if __name__ == "__main__":
   __import__('doctest').testmod()


