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

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

       lowerCamelCase		:	Tuple							=      None
       if self.use_token_type_ids:
        lowerCamelCase		:	List[Any]							=      ids_tensor([self.batch_size, self.seq_length]    ,  self.type_vocab_size      )

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

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

       lowerCamelCase		:	Optional[int]							=      self.get_config()

       lowerCamelCase		:	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 						UpperCamelCase__							(      self      ):
       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 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  *__magic_name__      ):
       lowerCamelCase		:	Optional[int]							=      CTRLModel(config=__a      )
       model.to(__a      )
       model.eval()

       model(__a    ,  token_type_ids=__a    ,  head_mask=__a      )
       model(__a    ,  token_type_ids=__a      )
       lowerCamelCase		:	Any							=      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 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  *__magic_name__      ):
       lowerCamelCase		:	Dict							=      CTRLLMHeadModel(__a      )
       model.to(__a      )
       model.eval()

       lowerCamelCase		:	List[str]							=      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 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Union[str, Any]							=      self.prepare_config_and_inputs()

       (
           lowerCamelCase				
       )		:	Any							=      config_and_inputs

       lowerCamelCase		:	List[Any]							=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

       return config, inputs_dict






      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  *__magic_name__      ):
       lowerCamelCase		:	Any							=      self.num_labels
       lowerCamelCase		:	Union[str, Any]							=      CTRLForSequenceClassification(__a      )
       model.to(__a      )
       model.eval()
       lowerCamelCase		:	int							=      ids_tensor([self.batch_size]    ,  self.type_sequence_label_size      )
       lowerCamelCase		:	str							=      model(__a    ,  token_type_ids=__a    ,  labels=__a      )
       self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.num_labels)      )





@require_torch
class 							A__		(	lowerCAmelCase_  ,   lowerCAmelCase_  ,   lowerCAmelCase_  ,   unittest.TestCase):
      _UpperCAmelCase				:    int           =			(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
      _UpperCAmelCase				:    Dict           =			(CTRLLMHeadModel,) if is_torch_available() else ()
      _UpperCAmelCase				:    Tuple           =			(
          {
              """feature-extraction""": CTRLModel,
              """text-classification""": CTRLForSequenceClassification,
              """text-generation""": CTRLLMHeadModel,
              """zero-shot""": CTRLForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _UpperCAmelCase				:    Union[str, Any]           =			True
      _UpperCAmelCase				:    Any           =			False
      _UpperCAmelCase				:    Any           =			False
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):





       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 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Optional[int]							=      CTRLModelTester(self      )
       lowerCamelCase		:	str							=      ConfigTester(self    ,  config_class=__a    ,  n_embd=3_7      )
      def 						UpperCamelCase__							(      self      ):
       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       torch.cuda.empty_cache()
      def 						UpperCamelCase__							(      self      ):
       self.config_tester.run_common_tests()
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	str							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_model(*__a      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Optional[int]							=      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 						UpperCamelCase__							(      self      ):
       pass
      @slow
      def 						UpperCamelCase__							(      self      ):





       for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCamelCase		:	Dict							=      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 						UpperCamelCase__							(      self      ):
       pass





@require_torch
class 							A__		(	unittest.TestCase):
      def 						UpperCamelCase__							(      self      ):
       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       torch.cuda.empty_cache()






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

       lowerCamelCase		:	List[str]							=      model.generate(__a    ,  do_sample=__a      )
       self.assertListEqual(output_ids[0].tolist()    ,  __a      )








'''simple docstring'''



import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append('.')






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   List[Any]   ):
					__a			:       Any					    =  test_file.split(os.path.sep   )
					if components[0:2] != ["tests", "models"]:
										raise ValueError(
										    '`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got '
										    F"""{test_file} instead."""   )
					__a			:       Tuple					    =  components[-1]
					if not test_fn.endswith('py'   ):
										raise ValueError(F"""`test_file` should be a python file. Got {test_fn} instead."""   )
					if not test_fn.startswith('test_modeling_'   ):
										raise ValueError(
										    F"""`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."""   )

					__a			:       List[str]					    =  components[:-1] + [test_fn.replace('.py'						,      ''   )]
					__a			:       Optional[Any]					    =  '.'.join(_SCREAMING_SNAKE_CASE   )

					return test_module_path






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple   ):
					__a			:       List[str]					    =  get_module_path(_SCREAMING_SNAKE_CASE   )
					__a			:       Dict					    =  importlib.import_module(_SCREAMING_SNAKE_CASE   )

					return test_module






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple   ):
					__a			:       List[str]					    =  []
					__a			:       List[str]					    =  get_test_module(_SCREAMING_SNAKE_CASE   )
					for attr in dir(_SCREAMING_SNAKE_CASE   ):
										if attr.endswith('ModelTester'   ):
															tester_classes.append(getattr(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   )   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple   ):
					__a			:       Any					    =  []
					__a			:       str					    =  get_test_module(_SCREAMING_SNAKE_CASE   )
					for attr in dir(_SCREAMING_SNAKE_CASE   ):
										__a			:       int					    =  getattr(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   )
										# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
										# `all_model_classes` is not empty (which also excludes other special classes).
										__a			:       Optional[Any]					    =  getattr(_SCREAMING_SNAKE_CASE						,      'all_model_classes'						,      []   )
										if len(_SCREAMING_SNAKE_CASE   ) > 0:
															test_classes.append(_SCREAMING_SNAKE_CASE   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   int   ):
					__a			:       str					    =  get_test_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       Any					    =  set()
					for test_class in test_classes:
										model_classes.update(test_class.all_model_classes   )

					# sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Optional[Any]   ):
					__a			:       Tuple					    =  test_class()
					if hasattr(_SCREAMING_SNAKE_CASE						,      'setUp'   ):
										test.setUp()

					__a			:       List[Any]					    =  None
					if hasattr(_SCREAMING_SNAKE_CASE						,      'model_tester'   ):
										# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
										if test.model_tester is not None:
															__a			:       List[str]					    =  test.model_tester.__class__

					return model_tester






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   int						,      _SCREAMING_SNAKE_CASE      :   Optional[Any]   ):
					__a			:       str					    =  get_test_classes(_SCREAMING_SNAKE_CASE   )

					__a			:       int					    =  []
					for test_class in test_classes:
										if model_class in test_class.all_model_classes:
															target_test_classes.append(_SCREAMING_SNAKE_CASE   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple						,      _SCREAMING_SNAKE_CASE      :   List[str]   ):
					__a			:       List[Any]					    =  get_test_classes_for_model(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   )

					__a			:       Any					    =  []
					for test_class in test_classes:
										__a			:       Any					    =  get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE   )
										if tester_class is not None:
															tester_classes.append(_SCREAMING_SNAKE_CASE   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Optional[int]   ):
					__a			:       str					    =  get_test_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       int					    =  {test_class: get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE   ) for test_class in test_classes}
					return test_tester_mapping






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   List[str]   ):
					__a			:       Optional[Any]					    =  get_model_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       Optional[int]					    =  {
					    model_class: get_test_classes_for_model(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ) for model_class in model_classes
					}
					return model_test_mapping






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   List[str]   ):
					__a			:       Optional[Any]					    =  get_model_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       str					    =  {
					    model_class: get_tester_classes_for_model(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ) for model_class in model_classes
					}
					return model_to_tester_mapping






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   str   ):
					if isinstance(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ):
										return o
					elif isinstance(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ):
										return o.__name__
					elif isinstance(_SCREAMING_SNAKE_CASE						,      (list, tuple)   ):
										return [to_json(_SCREAMING_SNAKE_CASE   ) for x in o]
					elif isinstance(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ):
										return {to_json(_SCREAMING_SNAKE_CASE   ): to_json(_SCREAMING_SNAKE_CASE   ) for k, v in o.items()}
					else:
										return o



