



'''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 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			def __init__(							self					:			Optional[Any]							,					lowerCAmelCase__					:			Dict							,					):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		parent
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		1_3
					__SCREAMING_SNAKE_CASE	:		Any   	=		7
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		True
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		True
					__SCREAMING_SNAKE_CASE	:		Any   	=		True
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		True
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		True
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		9_9
					__SCREAMING_SNAKE_CASE	:		Dict   	=		3_2
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		2
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		4
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		3_7
					__SCREAMING_SNAKE_CASE	:		Any   	=		"""gelu"""
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		0.1
					__SCREAMING_SNAKE_CASE	:		str   	=		0.1
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		5_1_2
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		1_6
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		2
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		0.02
					__SCREAMING_SNAKE_CASE	:		Any   	=		3
					__SCREAMING_SNAKE_CASE	:		Any   	=		4
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		None
					__SCREAMING_SNAKE_CASE	:		str   	=		self.vocab_size - 1

			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size						)

					__SCREAMING_SNAKE_CASE	:		str   	=		None
					if self.use_input_mask:
							__SCREAMING_SNAKE_CASE	:		int   	=		random_attention_mask([self.batch_size, self.seq_length]						)

					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		None
					if self.use_token_type_ids:
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		ids_tensor([self.batch_size, self.seq_length]							,					self.type_vocab_size						)

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

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

					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		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	:		int   	=		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					:			Dict							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			Dict							,					lowerCAmelCase__					:			Any							,					lowerCAmelCase__					:			Union[str, Any]							,					lowerCAmelCase__					:			int							,					*lowerCAmelCase__					:			Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		TFCTRLModel(config=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		model(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		[input_ids, None, input_mask]  # None is the input for 'past'
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		model(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		model(lowerCAmelCase__						)

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

			def UpperCamelCase__ (							self					:			Dict							,					lowerCAmelCase__					:			Any							,					lowerCAmelCase__					:			int							,					lowerCAmelCase__					:			Dict							,					lowerCAmelCase__					:			int							,					lowerCAmelCase__					:			int							,					*lowerCAmelCase__					:			Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		TFCTRLLMHeadModel(config=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		model(lowerCAmelCase__						)
					self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.vocab_size)						)

			def UpperCamelCase__ (							self					:			int							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			int							,					lowerCAmelCase__					:			List[Any]							,					lowerCAmelCase__					:			List[str]							,					lowerCAmelCase__					:			List[Any]							,					*lowerCAmelCase__					:			Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.num_labels
					__SCREAMING_SNAKE_CASE	:		Any   	=		ids_tensor([self.batch_size]							,					self.type_sequence_label_size						)
					__SCREAMING_SNAKE_CASE	:		int   	=		{
					    """input_ids""": input_ids,
					    """token_type_ids""": token_type_ids,
					    """labels""": sequence_labels,
					}
					__SCREAMING_SNAKE_CASE	:		Any   	=		TFCTRLForSequenceClassification(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		model(lowerCAmelCase__						)
					self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels)						)



			def UpperCamelCase__ (							self					:			List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		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
					)	,						
					)	:		Any   	=		config_and_inputs

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






@require_tf
class 						_UpperCamelCase  (							lowerCamelCase__   , lowerCamelCase__   , unittest.TestCase   ):


			'''simple docstring'''

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

			def UpperCamelCase__ (							self					:			List[Any]							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			Dict							,					lowerCAmelCase__					:			Tuple							,					lowerCAmelCase__					:			int							,					lowerCAmelCase__					:			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 UpperCamelCase__ (							self					:			Dict						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Any   	=		TFCTRLModelTester(self						)
					__SCREAMING_SNAKE_CASE	:		Any   	=		ConfigTester(self							,					config_class=lowerCAmelCase__							,					n_embd=3_7						)

			def UpperCamelCase__ (							self					:			str						):





					"""simple docstring"""






					self.config_tester.run_common_tests()

			def UpperCamelCase__ (							self					:			Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_model(*lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_lm_head(*lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		self.model_tester.prepare_config_and_inputs_for_common()
					__SCREAMING_SNAKE_CASE	:		str   	=		[TFCTRLLMHeadModel]
					__SCREAMING_SNAKE_CASE	:		int   	=		[TFCTRLForSequenceClassification]

					for model_class in self.all_model_classes:
							__SCREAMING_SNAKE_CASE	:		Tuple   	=		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:
									__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		model.get_output_embeddings()
									assert isinstance(lowerCAmelCase__							,					tf.keras.layers.Layer						)
									__SCREAMING_SNAKE_CASE	:		Any   	=		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:
									__SCREAMING_SNAKE_CASE	:		Tuple   	=		model.get_output_embeddings()
									assert isinstance(lowerCAmelCase__							,					tf.keras.layers.Layer						)
									__SCREAMING_SNAKE_CASE	:		List[str]   	=		model.get_bias()
									assert name is None
							else:
									__SCREAMING_SNAKE_CASE	:		str   	=		model.get_output_embeddings()
									assert x is None
									__SCREAMING_SNAKE_CASE	:		str   	=		model.get_bias()
									assert name is None



			@slow
			def UpperCamelCase__ (							self					:			List[str]						):





					"""simple docstring"""






					for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__SCREAMING_SNAKE_CASE	:		Dict   	=		TFCTRLModel.from_pretrained(lowerCAmelCase__						)
							self.assertIsNotNone(lowerCAmelCase__						)






@require_tf
class 						_UpperCamelCase  (							unittest.TestCase   ):


			'''simple docstring'''

			@slow
			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		str   	=		TFCTRLLMHeadModel.from_pretrained("""ctrl"""						)
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]]							,					dtype=tf.intaa						)  # Legal the president is
					__SCREAMING_SNAKE_CASE	:		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

					__SCREAMING_SNAKE_CASE	:		int   	=		model.generate(lowerCAmelCase__							,					do_sample=lowerCAmelCase__						)
					self.assertListEqual(output_ids[0].numpy().tolist()							,					lowerCAmelCase__						)




'''simple docstring'''




from __future__ import annotations


def     lowerCAmelCase_       (     _lowerCamelCase:       list[int | float] ):
		if len(_lowerCamelCase ) == 0:
				raise ValueError("""find_min() arg is an empty sequence""" )
		__SCREAMING_SNAKE_CASE	:		List[str]   	=		nums[0]
		for num in nums:
				__SCREAMING_SNAKE_CASE	:		int   	=		min(_lowerCamelCase	,				_lowerCamelCase )
		return min_num


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)