"""simple docstring"""
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    __snake_case       :



      def __init__(						self    ,			lowercase    ,			lowercase=14    ,			lowercase=7    ,			lowercase=True    ,			lowercase=True    ,			lowercase=True    ,			lowercase=True    ,			lowercase=True    ,			lowercase=99    ,			lowercase=32    ,			lowercase=5    ,			lowercase=4    ,			lowercase=37    ,			lowercase="gelu"    ,			lowercase=0.1    ,			lowercase=0.1    ,			lowercase=5_12    ,			lowercase=16    ,			lowercase=2    ,			lowercase=0.02    ,			lowercase=3    ,			lowercase=4    ,			lowercase=None    ,			)	->			Optional[int]:





        '''simple docstring'''





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



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        a__:     Dict										=	ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

        a__:     Tuple										=	None
        if self.use_input_mask:
          a__:     Any										=	random_attention_mask([self.batch_size, self.seq_length])

        a__:     List[Any]										=	None
        if self.use_token_type_ids:
          a__:     Tuple										=	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__:     Optional[Any]										=	None
        a__:     Tuple										=	None
        a__:     Tuple										=	None
        if self.use_labels:
          a__:     List[str]										=	ids_tensor([self.batch_size]    ,			self.type_sequence_label_size)
          a__:     Tuple										=	ids_tensor([self.batch_size, self.seq_length]    ,			self.num_labels)
          a__:     List[str]										=	ids_tensor([self.batch_size]    ,			self.num_choices)

        a__:     Optional[int]										=	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   lowerCamelCase_   (						self)	->			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   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			*lowercase)	->			List[str]:





        '''simple docstring'''





        a__:     List[str]										=	CTRLModel(config=lowercase)
        model.to(lowercase)
        model.eval()

        model(lowercase    ,			token_type_ids=lowercase    ,			head_mask=lowercase)
        model(lowercase    ,			token_type_ids=lowercase)
        a__:     str										=	model(lowercase)
        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   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			*lowercase)	->			int:





        '''simple docstring'''





        a__:     Optional[Any]										=	CTRLLMHeadModel(lowercase)
        model.to(lowercase)
        model.eval()

        a__:     List[str]										=	model(lowercase    ,			token_type_ids=lowercase    ,			labels=lowercase)
        self.parent.assertEqual(result.loss.shape    ,			())
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size))



      def   lowerCamelCase_   (						self)	->			int:





        '''simple docstring'''





        a__:     Optional[int]										=	self.prepare_config_and_inputs()

        (
            (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    
        ):     List[Any]										=	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   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			*lowercase)	->			List[str]:





        '''simple docstring'''





        a__:     List[Any]										=	self.num_labels
        a__:     Optional[int]										=	CTRLForSequenceClassification(lowercase)
        model.to(lowercase)
        model.eval()
        a__:     Any										=	ids_tensor([self.batch_size]    ,			self.type_sequence_label_size)
        a__:     Optional[Any]										=	model(lowercase    ,			token_type_ids=lowercase    ,			labels=lowercase)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels))







@require_torch
class    __snake_case       (     __lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase       ,      unittest.TestCase				):
      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   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			Tuple:





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





        '''simple docstring'''





        a__:     List[str]										=	CTRLModelTester(self)
        a__:     Optional[Any]										=	ConfigTester(self    ,			config_class=lowercase    ,			n_embd=37)



      def   lowerCamelCase_   (						self)	->			Optional[int]:





        '''simple docstring'''





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



      def   lowerCamelCase_   (						self)	->			List[Any]:





        '''simple docstring'''





        self.config_tester.run_common_tests()



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        a__:     Optional[Any]										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*lowercase)



      def   lowerCamelCase_   (						self)	->			Optional[int]:





        '''simple docstring'''





        a__:     Dict										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*lowercase)



      @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
      def   lowerCamelCase_   (						self)	->			int:





        '''simple docstring'''





        pass



      @slow
      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__:     int										=	CTRLModel.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)



      @unittest.skip('The model doesn\'t support left padding')  # and it's not used enough to be worth fixing :)
      def   lowerCamelCase_   (						self)	->			Optional[int]:





        '''simple docstring'''





        pass







@require_torch
class    __snake_case       (     unittest.TestCase				):



      def   lowerCamelCase_   (						self)	->			int:





        '''simple docstring'''





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



      @slow
      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        a__:     Dict										=	CTRLLMHeadModel.from_pretrained('ctrl')
        model.to(lowercase)
        a__:     Optional[int]										=	torch.tensor(
            [[1_18_59, 0, 16_11, 8]]    ,			dtype=torch.long    ,			device=lowercase)  # Legal the president is
        a__:     List[Any]										=	[
            1_18_59,
            0,
            16_11,
            8,
            5,
            1_50,
            2_64_49,
            2,
            19,
            3_48,
            4_69,
            3,
            25_95,
            48,
            2_07_40,
            24_65_33,
            24_65_33,
            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__:     List[str]										=	model.generate(lowercase    ,			do_sample=lowercase)
        self.assertListEqual(output_ids[0].tolist()    ,			lowercase)



"""simple docstring"""
class    __snake_case       :



      def __init__(						self    ,			lowercase    ,			lowercase=None    ,			lowercase=None)	->			List[str]:





        '''simple docstring'''





        a__:     Dict										=	data
        a__:     List[Any]										=	previous
        a__:     Any										=	next_node



      def __str__(						self)	->			str:





        '''simple docstring'''





        return f'{self.data}'



      def   lowerCamelCase_   (						self)	->			int:





        '''simple docstring'''





        return self.data



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        return self.next



      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        return self.previous







class    __snake_case       :



      def __init__(						self    ,			lowercase)	->			Dict:





        '''simple docstring'''





        a__:     List[Any]										=	head



      def __iter__(						self)	->			List[Any]:





        '''simple docstring'''





        return self



      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        if not self.current:
          raise StopIteration
        else:
          a__:     Dict										=	self.current.get_data()
          a__:     Optional[Any]										=	self.current.get_next()
          return value







class    __snake_case       :



      def __init__(						self)	->			Dict:





        '''simple docstring'''





        a__:     List[Any]										=	None  # First node in list
        a__:     Optional[int]										=	None  # Last node in list



      def __str__(						self)	->			Optional[Any]:





        '''simple docstring'''





        a__:     Dict										=	self.head
        a__:     Optional[Any]										=	[]
        while current is not None:
          nodes.append(current.get_data())
          a__:     str										=	current.get_next()
        return " ".join(str(lowercase) for node in nodes)



      def __contains__(						self    ,			lowercase)	->			Optional[int]:





        '''simple docstring'''





        a__:     Optional[int]										=	self.head
        while current:
          if current.get_data() == value:
            return True
          a__:     Dict										=	current.get_next()
        return False



      def __iter__(						self)	->			int:





        '''simple docstring'''





        return LinkedListIterator(self.head)



      def   lowerCamelCase_   (						self)	->			List[Any]:





        '''simple docstring'''





        if self.head:
          return self.head.get_data()
        return None



      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        if self.tail:
          return self.tail.get_data()
        return None



      def   lowerCamelCase_   (						self    ,			lowercase)	->			None:





        '''simple docstring'''





        if self.head is None:
          a__:     Optional[Any]										=	node
          a__:     Optional[Any]										=	node
        else:
          self.insert_before_node(self.head    ,			lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase)	->			None:





        '''simple docstring'''





        if self.head is None:
          self.set_head(lowercase)
        else:
          self.insert_after_node(self.tail    ,			lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Tuple										=	Node(lowercase)
        if self.head is None:
          self.set_head(lowercase)
        else:
          self.set_tail(lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Union[str, Any]										=	node
        a__:     Optional[Any]										=	node.previous

        if node.get_previous() is None:
          a__:     Tuple										=	node_to_insert
        else:
          a__:     int										=	node_to_insert

        a__:     Optional[int]										=	node_to_insert



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Optional[int]										=	node
        a__:     Tuple										=	node.next

        if node.get_next() is None:
          a__:     Optional[int]										=	node_to_insert
        else:
          a__:     Any										=	node_to_insert

        a__:     str										=	node_to_insert



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Any										=	1
        a__:     Tuple										=	Node(lowercase)
        a__:     Tuple										=	self.head
        while node:
          if current_position == position:
            self.insert_before_node(lowercase    ,			lowercase)
            return
          current_position += 1
          a__:     List[Any]										=	node.next
        self.insert_after_node(self.tail    ,			lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase)	->			Node:





        '''simple docstring'''





        a__:     Tuple										=	self.head
        while node:
          if node.get_data() == item:
            return node
          a__:     List[str]										=	node.get_next()
        raise Exception('Node not found')



      def   lowerCamelCase_   (						self    ,			lowercase)	->			Any:





        '''simple docstring'''





        if (node := self.get_node(lowercase)) is not None:
          if node == self.head:
            a__:     Any										=	self.head.get_next()

          if node == self.tail:
            a__:     List[Any]										=	self.tail.get_previous()

          self.remove_node_pointers(lowercase)



      @staticmethod
      def   lowerCamelCase_   (						lowercase)	->			None:





        '''simple docstring'''





        if node.get_next():
          a__:     Any										=	node.previous

        if node.get_previous():
          a__:     List[str]										=	node.next

        a__:     int										=	None
        a__:     Union[str, Any]										=	None



      def   lowerCamelCase_   (						self)	->			str:





        '''simple docstring'''





        return self.head is None







def 						__a   (	)	->None:
  pass


if __name__ == "__main__":
 import doctest

 doctest.testmod()


