



from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.ctrl.modeling_tf_ctrl import (
         TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFCTRLForSequenceClassification,
         TFCTRLLMHeadModel,
         TFCTRLModel,
     )



class 		_lowercase		(							UpperCAmelCase__						):

   def __init__(   self      :							Optional[Any]				, __lowerCAmelCase      :							str				, )							->					Optional[Any]:


    """simple docstring"""






    a							=		parent
    a							=		13
    a							=		7
    a							=		True
    a							=		True
    a							=		True
    a							=		True
    a							=		True
    a							=		99
    a							=		32
    a							=		2
    a							=		4
    a							=		37
    a							=		"gelu"
    a							=		0.1
    a							=		0.1
    a							=		512
    a							=		16
    a							=		2
    a							=		0.0_2
    a							=		3
    a							=		4
    a							=		None
    a							=		self.vocab_size - 1

   def 		A				(   self      :							Tuple				)							->					Optional[int]:


    """simple docstring"""






    a							=		ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size				)

    a							=		None
    if self.use_input_mask:
     a							=		random_attention_mask([self.batch_size, self.seq_length]				)

    a							=		None
    if self.use_token_type_ids:
     a							=		ids_tensor([self.batch_size, self.seq_length]				, self.type_vocab_size				)

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

    a							=		None
    a							=		None
    a							=		None
    if self.use_labels:
     a							=		ids_tensor([self.batch_size]				, self.type_sequence_label_size				)
     a							=		ids_tensor([self.batch_size, self.seq_length]				, self.num_labels				)
     a							=		ids_tensor([self.batch_size]				, self.num_choices				)

    a							=		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				, )

    a							=		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      :							List[str]				, __lowerCAmelCase      :							int				, __lowerCAmelCase      :							str				, __lowerCAmelCase      :							str				, __lowerCAmelCase      :							str				, __lowerCAmelCase      :							List[Any]				, *__lowerCAmelCase      :							Optional[int]				)							->					Dict:


    """simple docstring"""






    a							=		TFCTRLModel(config=__lowerCAmelCase				)
    a							=		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
    a							=		model(__lowerCAmelCase				)

    a							=		[input_ids, None, input_mask]  # None is the input for 'past'
    a							=		model(__lowerCAmelCase				)

    a							=		model(__lowerCAmelCase				)

    self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.hidden_size)				)

   def 		A				(   self      :							Optional[Any]				, __lowerCAmelCase      :							int				, __lowerCAmelCase      :							Optional[Any]				, __lowerCAmelCase      :							Dict				, __lowerCAmelCase      :							List[str]				, __lowerCAmelCase      :							Optional[int]				, *__lowerCAmelCase      :							List[str]				)							->					Optional[Any]:


    """simple docstring"""






    a							=		TFCTRLLMHeadModel(config=__lowerCAmelCase				)
    a							=		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
    a							=		model(__lowerCAmelCase				)
    self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size)				)

   def 		A				(   self      :							int				, __lowerCAmelCase      :							List[Any]				, __lowerCAmelCase      :							Optional[int]				, __lowerCAmelCase      :							Optional[Any]				, __lowerCAmelCase      :							List[Any]				, __lowerCAmelCase      :							Dict				, *__lowerCAmelCase      :							Optional[int]				)							->					Tuple:


    """simple docstring"""






    a							=		self.num_labels
    a							=		ids_tensor([self.batch_size]				, self.type_sequence_label_size				)
    a							=		{
        "input_ids": input_ids,
        "token_type_ids": token_type_ids,
        "labels": sequence_labels,
    }
    a							=		TFCTRLForSequenceClassification(__lowerCAmelCase				)
    a							=		model(__lowerCAmelCase				)
    self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_labels)				)




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


    """simple docstring"""






    a							=		self.prepare_config_and_inputs()

    (
        (
        a
    )		,	(
        a
    )		,	(
        a
    )		,	(
        a
    )		,	(
        a
    )		,	(
        a
    )		,	(
        a
    )		,	(
        a
    )		,	(
        a
    )		,	
    )							=		config_and_inputs

    a							=		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
    return config, inputs_dict



@require_tf
class 		_lowercase		(							UpperCAmelCase__, UpperCAmelCase__, unittest.TestCase						):
   _UpperCAmelCase  						=      (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
   _UpperCAmelCase  						=      (TFCTRLLMHeadModel,) if is_tf_available() else ()
   _UpperCAmelCase  						=      (
       {
           '''feature-extraction''': TFCTRLModel,
           '''text-classification''': TFCTRLForSequenceClassification,
           '''text-generation''': TFCTRLLMHeadModel,
           '''zero-shot''': TFCTRLForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   _UpperCAmelCase  						=      False
   _UpperCAmelCase  						=      False

   def 		A				(   self      :							Union[str, Any]				, __lowerCAmelCase      :							Optional[Any]				, __lowerCAmelCase      :							Optional[Any]				, __lowerCAmelCase      :							int				, __lowerCAmelCase      :							str				, __lowerCAmelCase      :							List[str]				)							->					Any:


    """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 		A				(   self      :							str				)							->					Tuple:


    """simple docstring"""






    a							=		TFCTRLModelTester(self				)
    a							=		ConfigTester(self				, config_class=__lowerCAmelCase				, n_embd=37				)

   def 		A				(   self      :							Dict				)							->					List[str]:


    """simple docstring"""






    self.config_tester.run_common_tests()

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


    """simple docstring"""






    a							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_model(*__lowerCAmelCase				)

   def 		A				(   self      :							Optional[int]				)							->					Tuple:


    """simple docstring"""






    a							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_lm_head(*__lowerCAmelCase				)

   def 		A				(   self      :							List[Any]				)							->					Optional[Any]:


    """simple docstring"""






    a							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_for_sequence_classification(*__lowerCAmelCase				)

   def 		A				(   self      :							Any				)							->					Tuple:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    a							=		[TFCTRLLMHeadModel]
    a							=		[TFCTRLForSequenceClassification]

    for model_class in self.all_model_classes:
     a							=		model_class(__lowerCAmelCase				)
     model.build()  # may be needed for the get_bias() call below
     assert isinstance(model.get_input_embeddings()				, tf.keras.layers.Layer				)

     if model_class in list_lm_models:
      a							=		model.get_output_embeddings()
      assert isinstance(__lowerCAmelCase				, tf.keras.layers.Layer				)
      a							=		model.get_bias()
      assert isinstance(__lowerCAmelCase				, __lowerCAmelCase				)
      for k, v in name.items():
       assert isinstance(__lowerCAmelCase				, tf.Variable				)
     elif model_class in list_other_models_with_output_ebd:
      a							=		model.get_output_embeddings()
      assert isinstance(__lowerCAmelCase				, tf.keras.layers.Layer				)
      a							=		model.get_bias()
      assert name is None
     else:
      a							=		model.get_output_embeddings()
      assert x is None
      a							=		model.get_bias()
      assert name is None




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


    """simple docstring"""






    for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     a							=		TFCTRLModel.from_pretrained(__lowerCAmelCase				)
     self.assertIsNotNone(__lowerCAmelCase				)



@require_tf
class 		_lowercase		(							unittest.TestCase						):

   @slow
   def 		A				(   self      :							Optional[int]				)							->					Dict:


    """simple docstring"""






    a							=		TFCTRLLMHeadModel.from_pretrained("ctrl"				)
    a							=		tf.convert_to_tensor([[1_1859, 0, 1611, 8]]				, dtype=tf.intaa				)  # Legal the president is
    a							=		[
        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

    a							=		model.generate(__lowerCAmelCase				, do_sample=__lowerCAmelCase				)
    self.assertListEqual(output_ids[0].numpy().tolist()				, __lowerCAmelCase				)



'''simple docstring'''







class       lowerCAmelCase			:




		def __init__(					self       ,    UpperCamelCase=None   ):
								_SCREAMING_SNAKE_CASE					=			data
								_SCREAMING_SNAKE_CASE					=			None




		def __repr__(					self   ):
								_SCREAMING_SNAKE_CASE					=			""
								_SCREAMING_SNAKE_CASE					=			self
								while temp:
														string_rep += F'<{temp.data}> ---> '
														_SCREAMING_SNAKE_CASE					=			temp.next
								string_rep += "<END>"
								return string_rep



def       _a		(						_SCREAMING_SNAKE_CASE		: Any	):

						# if elements_list is empty
						if not elements_list:
												raise Exception("The Elements List is empty"	)

						# Set first element as Head
						_SCREAMING_SNAKE_CASE					=			Node(elements_list[0]	)
						_SCREAMING_SNAKE_CASE					=			head
						# Loop through elements from position 1
						for data in elements_list[1:]:
												_SCREAMING_SNAKE_CASE					=			Node(_SCREAMING_SNAKE_CASE	)
												_SCREAMING_SNAKE_CASE					=			current.next
						return head


_snake_case					: List[Any]     			=					[1, 3, 5, 32, 44, 12, 43]
print(F"List: {list_data}")
print("""Creating Linked List from List.""")
_snake_case					: List[Any]     			=					make_linked_list(list_data)
print("""Linked List:""")




print(linked_list)