


"""simple docstring"""






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       lowerCamelCase_		(      lowercase ):





				"""simple docstring"""







				def __init__(    self							,		UpperCAmelCase__							,		):
								SCREAMING_SNAKE_CASE__  				=					parent
								SCREAMING_SNAKE_CASE__  				=					13
								SCREAMING_SNAKE_CASE__  				=					7
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					99
								SCREAMING_SNAKE_CASE__  				=					32
								SCREAMING_SNAKE_CASE__  				=					2
								SCREAMING_SNAKE_CASE__  				=					4
								SCREAMING_SNAKE_CASE__  				=					37
								SCREAMING_SNAKE_CASE__  				=					"gelu"
								SCREAMING_SNAKE_CASE__  				=					0.1
								SCREAMING_SNAKE_CASE__  				=					0.1
								SCREAMING_SNAKE_CASE__  				=					512
								SCREAMING_SNAKE_CASE__  				=					16
								SCREAMING_SNAKE_CASE__  				=					2
								SCREAMING_SNAKE_CASE__  				=					0.02
								SCREAMING_SNAKE_CASE__  				=					3
								SCREAMING_SNAKE_CASE__  				=					4
								SCREAMING_SNAKE_CASE__  				=					None
								SCREAMING_SNAKE_CASE__  				=					self.vocab_size - 1



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size  )

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

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

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

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

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

								SCREAMING_SNAKE_CASE__  				=					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							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					TFCTRLModel(config=UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )

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

								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )

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



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					TFCTRLLMHeadModel(config=UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )
								self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)  )



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					self.num_labels
								SCREAMING_SNAKE_CASE__  				=					ids_tensor([self.batch_size]							,		self.type_sequence_label_size  )
								SCREAMING_SNAKE_CASE__  				=					{
								    "input_ids": input_ids,
								    "token_type_ids": token_type_ids,
								    "labels": sequence_labels,
								}
								SCREAMING_SNAKE_CASE__  				=					TFCTRLForSequenceClassification(UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )
								self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels)  )






				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.prepare_config_and_inputs()

								(
								    (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   
								)  				=					config_and_inputs

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


@require_tf
class       lowerCamelCase_		(      lowercase							,	lowercase							,	unittest.TestCase ):





				"""simple docstring"""







				_lowerCAmelCase   :   Optional[int]   				=       (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
				_lowerCAmelCase   :   int   				=       (TFCTRLLMHeadModel,) if is_tf_available() else ()
				_lowerCAmelCase   :   Optional[int]   				=       (
				    {
				        "feature-extraction": TFCTRLModel,
				        "text-classification": TFCTRLForSequenceClassification,
				        "text-generation": TFCTRLLMHeadModel,
				        "zero-shot": TFCTRLForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				_lowerCAmelCase   :   List[Any]   				=       False
				_lowerCAmelCase   :   List[str]   				=       False



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  ):
								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  ):
								SCREAMING_SNAKE_CASE__  				=					TFCTRLModelTester(self  )
								SCREAMING_SNAKE_CASE__  				=					ConfigTester(self							,		config_class=UpperCAmelCase__							,		n_embd=37  )



				def 	lowerCAmelCase__		(    self  ):
								self.config_tester.run_common_tests()



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_lm_head(*UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__				,   SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs_for_common()
								SCREAMING_SNAKE_CASE__  				=					[TFCTRLLMHeadModel]
								SCREAMING_SNAKE_CASE__  				=					[TFCTRLForSequenceClassification]

								for model_class in self.all_model_classes:
												SCREAMING_SNAKE_CASE__  				=					model_class(UpperCAmelCase__  )
												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:
																SCREAMING_SNAKE_CASE__  				=					model.get_output_embeddings()
																assert isinstance(UpperCAmelCase__							,		tf.keras.layers.Layer  )
																SCREAMING_SNAKE_CASE__  				=					model.get_bias()
																assert isinstance(UpperCAmelCase__							,		UpperCAmelCase__  )
																for k, v in name.items():
																				assert isinstance(UpperCAmelCase__							,		tf.Variable  )
												elif model_class in list_other_models_with_output_ebd:
																SCREAMING_SNAKE_CASE__  				=					model.get_output_embeddings()
																assert isinstance(UpperCAmelCase__							,		tf.keras.layers.Layer  )
																SCREAMING_SNAKE_CASE__  				=					model.get_bias()
																assert name is None
												else:
																SCREAMING_SNAKE_CASE__  				=					model.get_output_embeddings()
																assert x is None
																SCREAMING_SNAKE_CASE__  				=					model.get_bias()
																assert name is None






				@slow
				def 	lowerCAmelCase__		(    self  ):
								for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												SCREAMING_SNAKE_CASE__  				=					TFCTRLModel.from_pretrained(UpperCAmelCase__  )
												self.assertIsNotNone(UpperCAmelCase__  )


@require_tf
class       lowerCamelCase_		(      unittest.TestCase ):





				"""simple docstring"""







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

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




"""simple docstring"""






class       lowerCamelCase_		:





				"""simple docstring"""







				class       lowerCamelCase_		:





								"""simple docstring"""







								_lowerCAmelCase   :   Dict   				=       "_prev", "_data", "_next"



								def __init__(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  ):
												SCREAMING_SNAKE_CASE__  				=					link_p
												SCREAMING_SNAKE_CASE__  				=					element
												SCREAMING_SNAKE_CASE__  				=					link_n






								def 	lowerCAmelCase__		(    self  ):
												return (
												    f''' Prev -> {self._prev is not None}, Next -> {self._next is not None}'''
												)



				def __init__(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self._Node(UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					self._Node(UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					self._trailer
								SCREAMING_SNAKE_CASE__  				=					self._header
								SCREAMING_SNAKE_CASE__  				=					0



				def __len__(    self  ):
								return self._size



				def 	lowerCAmelCase__		(    self  ):
								return self.__len__() == 0



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  ):
								# Create new_node by setting it's prev.link -> header
								# setting it's next.link -> trailer
								SCREAMING_SNAKE_CASE__  				=					self._Node(UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					new_node
								SCREAMING_SNAKE_CASE__  				=					new_node
								self._size += 1
								return self






				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					node._prev
								SCREAMING_SNAKE_CASE__  				=					node._next

								SCREAMING_SNAKE_CASE__  				=					successor
								SCREAMING_SNAKE_CASE__  				=					predecessor
								self._size -= 1
								SCREAMING_SNAKE_CASE__  				=					node._data
								SCREAMING_SNAKE_CASE__  				=					SCREAMING_SNAKE_CASE__  				=					SCREAMING_SNAKE_CASE__  				=					None
								del node
								return temp


class       lowerCamelCase_		(      _DoublyLinkedBase ):





				"""simple docstring"""







				def 	lowerCAmelCase__		(    self  ):
								if self.is_empty():
												raise Exception("List is empty"  )
								return self._header._next._data



				def 	lowerCAmelCase__		(    self  ):
								if self.is_empty():
												raise Exception("List is empty"  )
								return self._trailer._prev._data



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):
								return self._insert(self._header							,		UpperCAmelCase__							,		self._header._next  )



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):
								return self._insert(self._trailer._prev							,		UpperCAmelCase__							,		self._trailer  )



				def 	lowerCAmelCase__		(    self  ):
								if self.is_empty():
												raise IndexError("remove_first from empty list"  )
								return self._delete(self._header._next  )






				def 	lowerCAmelCase__		(    self  ):
								if self.is_empty():
												raise IndexError("remove_first from empty list"  )
								return self._delete(self._trailer._prev  )
