
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 						__magic_name__				(  snake_case      ):





		def __init__(							self      :		List[Any]  ,					lowerCamelCase__      :		str  ,					):
							lowerCAmelCase						:   Optional[Any]							=       parent
							lowerCAmelCase						:   Optional[int]							=       1_3
							lowerCAmelCase						:   Any							=       7
							lowerCAmelCase						:   Dict							=       True
							lowerCAmelCase						:   Any							=       True
							lowerCAmelCase						:   Dict							=       True
							lowerCAmelCase						:   int							=       True
							lowerCAmelCase						:   Dict							=       True
							lowerCAmelCase						:   Any							=       9_9
							lowerCAmelCase						:   List[str]							=       3_2
							lowerCAmelCase						:   Optional[Any]							=       2
							lowerCAmelCase						:   Optional[Any]							=       4
							lowerCAmelCase						:   int							=       3_7
							lowerCAmelCase						:   Union[str, Any]							=       '''gelu'''
							lowerCAmelCase						:   Optional[int]							=       0.1
							lowerCAmelCase						:   List[Any]							=       0.1
							lowerCAmelCase						:   List[Any]							=       5_1_2
							lowerCAmelCase						:   Dict							=       1_6
							lowerCAmelCase						:   List[str]							=       2
							lowerCAmelCase						:   Optional[int]							=       0.0_2
							lowerCAmelCase						:   Optional[int]							=       3
							lowerCAmelCase						:   Union[str, Any]							=       4
							lowerCAmelCase						:   Tuple							=       None
							lowerCAmelCase						:   Optional[Any]							=       self.vocab_size - 1





		def  _A							(							self      :		Optional[int]			):
							lowerCAmelCase						:   str							=       ids_tensor([self.batch_size, self.seq_length]  ,					self.vocab_size			)

							lowerCAmelCase						:   List[str]							=       None
							if self.use_input_mask:
												lowerCAmelCase						:   Tuple							=       random_attention_mask([self.batch_size, self.seq_length]			)

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

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

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

							lowerCAmelCase						:   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  ,					)

							lowerCAmelCase						:   List[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  _A							(							self      :		Tuple  ,					lowerCamelCase__      :		int  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		Any  ,					lowerCamelCase__      :		Tuple  ,					lowerCamelCase__      :		Optional[Any]  ,					*lowerCamelCase__      :		List[str]			):
							lowerCAmelCase						:   Any							=       TFCTRLModel(config=lowerCamelCase__			)
							lowerCAmelCase						:   Dict							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowerCAmelCase						:   Optional[int]							=       model(lowerCamelCase__			)

							lowerCAmelCase						:   int							=       [input_ids, None, input_mask]  # None is the input for 'past'
							lowerCAmelCase						:   Union[str, Any]							=       model(lowerCamelCase__			)

							lowerCAmelCase						:   Dict							=       model(lowerCamelCase__			)

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





		def  _A							(							self      :		Optional[int]  ,					lowerCamelCase__      :		Optional[int]  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		Any  ,					lowerCamelCase__      :		int  ,					lowerCamelCase__      :		List[Any]  ,					*lowerCamelCase__      :		Any			):
							lowerCAmelCase						:   List[Any]							=       TFCTRLLMHeadModel(config=lowerCamelCase__			)
							lowerCAmelCase						:   Union[str, Any]							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowerCAmelCase						:   int							=       model(lowerCamelCase__			)
							self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.seq_length, self.vocab_size)			)





		def  _A							(							self      :		List[str]  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		Dict  ,					lowerCamelCase__      :		int  ,					lowerCamelCase__      :		Any  ,					lowerCamelCase__      :		Tuple  ,					*lowerCamelCase__      :		int			):
							lowerCAmelCase						:   Tuple							=       self.num_labels
							lowerCAmelCase						:   Union[str, Any]							=       ids_tensor([self.batch_size]  ,					self.type_sequence_label_size			)
							lowerCAmelCase						:   int							=       {
							    '''input_ids''': input_ids,
							    '''token_type_ids''': token_type_ids,
							    '''labels''': sequence_labels,
							}
							lowerCAmelCase						:   Optional[int]							=       TFCTRLForSequenceClassification(lowerCamelCase__			)
							lowerCAmelCase						:   Tuple							=       model(lowerCamelCase__			)
							self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.num_labels)			)





		def  _A							(							self      :		Dict			):
							lowerCAmelCase						:   Dict							=       self.prepare_config_and_inputs()

							(
							    (
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							(
							    lowerCAmelCase
							)		,							
							)						:   List[Any]							=       config_and_inputs

							lowerCAmelCase						:   Dict							=       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_tf
class 						__magic_name__				(  snake_case,			snake_case,			unittest.TestCase      ):
		_lowerCAmelCase						=		(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
		_lowerCAmelCase						=		(TFCTRLLMHeadModel,) if is_tf_available() else ()
		_lowerCAmelCase						=		(
		    {
		        "feature-extraction": TFCTRLModel,
		        "text-classification": TFCTRLForSequenceClassification,
		        "text-generation": TFCTRLLMHeadModel,
		        "zero-shot": TFCTRLForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		_lowerCAmelCase						=		False
		_lowerCAmelCase						=		False





		def  _A							(							self      :		Union[str, Any]  ,					lowerCamelCase__      :		Union[str, Any]  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		Dict  ,					lowerCamelCase__      :		Optional[int]  ,					lowerCamelCase__      :		Optional[int]			):
							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]			):
							lowerCAmelCase						:   Union[str, Any]							=       TFCTRLModelTester(self			)
							lowerCAmelCase						:   Optional[int]							=       ConfigTester(self  ,					config_class=lowerCamelCase__  ,					n_embd=3_7			)





		def  _A							(							self      :		List[str]			):
							self.config_tester.run_common_tests()





		def  _A							(							self      :		Optional[int]			):
							lowerCAmelCase						:   Any							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_model(*lowerCamelCase__			)





		def  _A							(							self      :		Any			):
							lowerCAmelCase						:   Dict							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_lm_head(*lowerCamelCase__			)





		def  _A							(							self      :		Any			):
							lowerCAmelCase						:   Tuple							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowerCamelCase__			)





		def  _A							(							self      :		List[str]			):
							lowerCAmelCase		,							lowerCAmelCase						:   int							=       self.model_tester.prepare_config_and_inputs_for_common()
							lowerCAmelCase						:   Optional[int]							=       [TFCTRLLMHeadModel]
							lowerCAmelCase						:   int							=       [TFCTRLForSequenceClassification]

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





		@slow
		def  _A							(							self      :		List[Any]			):
							for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCAmelCase						:   Optional[int]							=       TFCTRLModel.from_pretrained(lowerCamelCase__			)
												self.assertIsNotNone(lowerCamelCase__			)



@require_tf
class 						__magic_name__				(  unittest.TestCase      ):





		@slow
		def  _A							(							self      :		List[str]			):
							lowerCAmelCase						:   Tuple							=       TFCTRLLMHeadModel.from_pretrained('''ctrl'''			)
							lowerCAmelCase						:   Dict							=       tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]]  ,					dtype=tf.intaa			)  # Legal the president is
							lowerCAmelCase						:   Optional[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

							lowerCAmelCase						:   List[str]							=       model.generate(lowerCamelCase__  ,					do_sample=lowerCamelCase__			)
							self.assertListEqual(output_ids[0].numpy().tolist()  ,					lowerCamelCase__			)


import warnings

from ...utils import logging
from .image_processing_conditional_detr import ConditionalDetrImageProcessor


__SCREAMING_SNAKE_CASE     :				List[Any]             =       logging.get_logger(__name__)


class 						__magic_name__				(  snake_case      ):





		def __init__(							self      :		str  ,					*lowerCamelCase__      :		Dict  ,					**lowerCamelCase__      :		int			):
							warnings.warn(
							    '''The class ConditionalDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
							    ''' Please use ConditionalDetrImageProcessor instead.'''  ,					lowerCamelCase__  ,					)
							super().__init__(*lowerCamelCase__  ,					**lowerCamelCase__			)
