


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 					snake_case__  ( __A       ):





	def __init__(  self				,    UpperCamelCase_				,    )		->			Dict:
								"""simple docstring"""






								a_       :   Dict			=	parent
								a_       :   Union[str, Any]			=	13
								a_       :   List[str]			=	7
								a_       :   Any			=	True
								a_       :   Any			=	True
								a_       :   List[Any]			=	True
								a_       :   str			=	True
								a_       :   Any			=	True
								a_       :   Dict			=	99
								a_       :   Union[str, Any]			=	32
								a_       :   List[str]			=	2
								a_       :   Any			=	4
								a_       :   Any			=	37
								a_       :   Union[str, Any]			=	"""gelu"""
								a_       :   List[Any]			=	0.1
								a_       :   List[Any]			=	0.1
								a_       :   int			=	512
								a_       :   Dict			=	16
								a_       :   Dict			=	2
								a_       :   str			=	0.02
								a_       :   List[str]			=	3
								a_       :   List[Any]			=	4
								a_       :   Optional[int]			=	None
								a_       :   str			=	self.vocab_size - 1





	def     A				(  self							)		->			List[Any]:
								"""simple docstring"""






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

								a_       :   Union[str, Any]			=	None
								if self.use_input_mask:
															a_       :   Union[str, Any]			=	random_attention_mask([self.batch_size, self.seq_length]							)

								a_       :   Optional[int]			=	None
								if self.use_token_type_ids:
															a_       :   Dict			=	ids_tensor([self.batch_size, self.seq_length]				,    self.type_vocab_size							)

								a_       :   List[str]			=	None
								if self.use_mc_token_ids:
															a_       :   Dict			=	ids_tensor([self.batch_size, self.num_choices]				,    self.seq_length							)

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

								a_       :   List[str]			=	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_       :   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     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    *UpperCamelCase_							)		->			Optional[Any]:
								"""simple docstring"""






								a_       :   List[Any]			=	TFCTRLModel(config=UpperCamelCase_							)
								a_       :   int			=	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								a_       :   Tuple			=	model(UpperCamelCase_							)

								a_       :   int			=	[input_ids, None, input_mask]  # None is the input for 'past'
								a_       :   str			=	model(UpperCamelCase_							)

								a_       :   Tuple			=	model(UpperCamelCase_							)

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





	def     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    *UpperCamelCase_							)		->			Any:
								"""simple docstring"""






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





	def     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    *UpperCamelCase_							)		->			str:
								"""simple docstring"""






								a_       :   Any			=	self.num_labels
								a_       :   Union[str, Any]			=	ids_tensor([self.batch_size]				,    self.type_sequence_label_size							)
								a_       :   Dict			=	{
								    """input_ids""": input_ids,
								    """token_type_ids""": token_type_ids,
								    """labels""": sequence_labels,
								}
								a_       :   Union[str, Any]			=	TFCTRLForSequenceClassification(UpperCamelCase_							)
								a_       :   Tuple			=	model(UpperCamelCase_							)
								self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)							)





	def     A				(  self							)		->			Optional[int]:
								"""simple docstring"""






								a_       :   Any			=	self.prepare_config_and_inputs()

								(
								    (
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				(
								    a_
								)		,				
								)       :   str			=	config_and_inputs

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





@require_tf
class 					snake_case__  ( __A      ,    __A      ,    unittest.TestCase       ):
	UpperCAmelCase   :							Any         = (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
	UpperCAmelCase   :							str         = (TFCTRLLMHeadModel,) if is_tf_available() else ()
	UpperCAmelCase   :							int         = (
	    {
	        """feature-extraction""": TFCTRLModel,
	        """text-classification""": TFCTRLForSequenceClassification,
	        """text-generation""": TFCTRLLMHeadModel,
	        """zero-shot""": TFCTRLForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	UpperCAmelCase   :							Tuple         = False
	UpperCAmelCase   :							Union[str, Any]         = False





	def     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_							)		->			List[str]:
								"""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							)		->			Optional[int]:
								"""simple docstring"""






								a_       :   Any			=	TFCTRLModelTester(self							)
								a_       :   List[Any]			=	ConfigTester(self				,    config_class=UpperCamelCase_				,    n_embd=37							)





	def     A				(  self							)		->			List[str]:
								"""simple docstring"""






								self.config_tester.run_common_tests()





	def     A				(  self							)		->			List[Any]:
								"""simple docstring"""






								a_       :   List[Any]			=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*UpperCamelCase_							)





	def     A				(  self							)		->			Dict:
								"""simple docstring"""






								a_       :   Optional[Any]			=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_lm_head(*UpperCamelCase_							)





	def     A				(  self							)		->			str:
								"""simple docstring"""






								a_       :   Dict			=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCamelCase_							)





	def     A				(  self							)		->			Tuple:
								"""simple docstring"""






								a_		,				a_       :   Any			=	self.model_tester.prepare_config_and_inputs_for_common()
								a_       :   List[Any]			=	[TFCTRLLMHeadModel]
								a_       :   Optional[int]			=	[TFCTRLForSequenceClassification]

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





	@slow
	def     A				(  self							)		->			Optional[Any]:
								"""simple docstring"""






								for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															a_       :   Optional[Any]			=	TFCTRLModel.from_pretrained(UpperCamelCase_							)
															self.assertIsNotNone(UpperCamelCase_							)





@require_tf
class 					snake_case__  ( unittest.TestCase       ):





	@slow
	def     A				(  self							)		->			int:
								"""simple docstring"""






								a_       :   Optional[int]			=	TFCTRLLMHeadModel.from_pretrained("""ctrl"""							)
								a_       :   int			=	tf.convert_to_tensor([[11859, 0, 1611, 8]]				,    dtype=tf.intaa							)  # Legal the president is
								a_       :   Optional[Any]			=	[
								    11859,
								    0,
								    1611,
								    8,
								    5,
								    150,
								    26449,
								    2,
								    19,
								    348,
								    469,
								    3,
								    2595,
								    48,
								    20740,
								    246533,
								    246533,
								    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_       :   Tuple			=	model.generate(UpperCamelCase_				,    do_sample=UpperCamelCase_							)
								self.assertListEqual(output_ids[0].numpy().tolist()				,    UpperCamelCase_							)






from ...utils import is_transformers_available
from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline


if is_transformers_available():
	from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline


