






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




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase=14    ,	_lowerCAmelCase=7    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=99    ,	_lowerCAmelCase=32    ,	_lowerCAmelCase=5    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=37    ,	_lowerCAmelCase="gelu"    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=512    ,	_lowerCAmelCase=16    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=0.02    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=None    ,	):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				parent
          lowerCAmelCase__   :List[Any]      							=				batch_size
          lowerCAmelCase__   :Any      							=				seq_length
          lowerCAmelCase__   :Dict      							=				is_training
          lowerCAmelCase__   :List[str]      							=				use_token_type_ids
          lowerCAmelCase__   :int      							=				use_input_mask
          lowerCAmelCase__   :Any      							=				use_labels
          lowerCAmelCase__   :Optional[Any]      							=				use_mc_token_ids
          lowerCAmelCase__   :str      							=				vocab_size
          lowerCAmelCase__   :List[Any]      							=				hidden_size
          lowerCAmelCase__   :Dict      							=				num_hidden_layers
          lowerCAmelCase__   :Optional[Any]      							=				num_attention_heads
          lowerCAmelCase__   :Tuple      							=				intermediate_size
          lowerCAmelCase__   :List[str]      							=				hidden_act
          lowerCAmelCase__   :List[Any]      							=				hidden_dropout_prob
          lowerCAmelCase__   :Any      							=				attention_probs_dropout_prob
          lowerCAmelCase__   :int      							=				max_position_embeddings
          lowerCAmelCase__   :Dict      							=				type_vocab_size
          lowerCAmelCase__   :int      							=				type_sequence_label_size
          lowerCAmelCase__   :Any      							=				initializer_range
          lowerCAmelCase__   :Optional[Any]      							=				num_labels
          lowerCAmelCase__   :Dict      							=				num_choices
          lowerCAmelCase__   :int      							=				scope
          lowerCAmelCase__   :List[str]      							=				self.vocab_size - 1
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size		)

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

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

          lowerCAmelCase__   :Union[str, Any]      							=				None
          if self.use_mc_token_ids:
              lowerCAmelCase__   :Dict      							=				ids_tensor([self.batch_size, self.num_choices]    ,	self.seq_length		)

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

          lowerCAmelCase__   :Union[str, Any]      							=				self.get_config()

          lowerCAmelCase__   :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 			snake_case_      (   self		):



          '''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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	*_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				CTRLModel(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()

          model(_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase    ,	head_mask=_lowerCAmelCase		)
          model(_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase		)
          lowerCAmelCase__   :Optional[Any]      							=				model(_lowerCAmelCase		)
          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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	*_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				CTRLLMHeadModel(_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()

          lowerCAmelCase__   :Dict      							=				model(_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase    ,	labels=_lowerCAmelCase		)
          self.parent.assertEqual(result.loss.shape    ,	()		)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.vocab_size)		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				self.prepare_config_and_inputs()

          (
              (
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,
          )   :Optional[int]      							=				config_and_inputs

          lowerCAmelCase__   :List[str]      							=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

          return config, inputs_dict



      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	*_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Tuple      							=				self.num_labels
          lowerCAmelCase__   :Dict      							=				CTRLForSequenceClassification(_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :str      							=				ids_tensor([self.batch_size]    ,	self.type_sequence_label_size		)
          lowerCAmelCase__   :Any      							=				model(_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase    ,	labels=_lowerCAmelCase		)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_labels)		)







@require_torch
class 	_UpperCAmelCase  (  _A				,							_A				,							_A				,							unittest.TestCase		):




      """simple docstring"""



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



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



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				CTRLModelTester(self		)
          lowerCAmelCase__   :Union[str, Any]      							=				ConfigTester(self    ,	config_class=_lowerCAmelCase    ,	n_embd=37		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          super().tearDown()
          # clean-up as much as possible GPU memory occupied by PyTorch
          gc.collect()
          torch.cuda.empty_cache()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          self.config_tester.run_common_tests()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_model(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase		)
      @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          pass
      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowerCAmelCase__   :List[str]      							=				CTRLModel.from_pretrained(_lowerCAmelCase		)
              self.assertIsNotNone(_lowerCAmelCase		)



      @unittest.skip("The model doesn't support left padding"		)  # and it's not used enough to be worth fixing :)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          pass







@require_torch
class 	_UpperCAmelCase  (  unittest.TestCase		):




      """simple docstring"""



      def 			snake_case_      (   self		):



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



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				CTRLLMHeadModel.from_pretrained("ctrl"		)
          model.to(_lowerCAmelCase		)
          lowerCAmelCase__   :Optional[int]      							=				torch.tensor(
              [[11_859, 0, 1_611, 8]]    ,	dtype=torch.long    ,	device=_lowerCAmelCase		)  # Legal the president is
          lowerCAmelCase__   :Optional[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

          lowerCAmelCase__   :Optional[Any]      							=				model.generate(_lowerCAmelCase    ,	do_sample=_lowerCAmelCase		)
          self.assertListEqual(output_ids[0].tolist()    ,	_lowerCAmelCase		)











import re


def 				snake_case__							(							UpperCAmelCase							:  str       ):
    return [char.split() for char in re.split(R"[^ a-z A-Z 0-9 \s]"    ,			str_       )]


def 				snake_case__							(							UpperCAmelCase							:  str       ):
    lowerCAmelCase__   :List[Any]      							=				split_input(str_       )
    return "".join(
        ["".join([char.capitalize() for char in sub_str]       ) for sub_str in string_split]       )


def 				snake_case__							(							UpperCAmelCase							:  str    ,			UpperCAmelCase							:  bool    ,			UpperCAmelCase							:  str       ):
    try:
        lowerCAmelCase__   :Dict      							=				split_input(UpperCAmelCase       )
        if upper:
            lowerCAmelCase__   :Dict      							=				"".join(
                [
                    separator.join([char.upper() for char in sub_str]       )
                    for sub_str in string_split
                ]       )
        else:
            lowerCAmelCase__   :Any      							=				"".join(
                [
                    separator.join([char.lower() for char in sub_str]       )
                    for sub_str in string_split
                ]       )
        return res_str
    except IndexError:
        return "not valid string"


def 				snake_case__							(							UpperCAmelCase							:  str       ):
    return to_simple_case(UpperCAmelCase       )


def 				snake_case__							(							UpperCAmelCase							:  str       ):
    try:
        lowerCAmelCase__   :Any      							=				to_simple_case(UpperCAmelCase       )
        return res_str[0].lower() + res_str[1:]
    except IndexError:
        return "not valid string"


def 				snake_case__							(							UpperCAmelCase							:  str    ,			UpperCAmelCase							:  bool       ):
    return to_complex_case(UpperCAmelCase    ,			UpperCAmelCase    ,			"_"       )


def 				snake_case__							(							UpperCAmelCase							:  str    ,			UpperCAmelCase							:  bool       ):
    return to_complex_case(UpperCAmelCase    ,			UpperCAmelCase    ,			"-"       )


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



