


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


				"""simple docstring"""







				def __init__(				self     :    Tuple	,							lowerCamelCase_     :    List[str]	,							):
								_lowerCAmelCase  						=parent
								_lowerCAmelCase  						=13
								_lowerCAmelCase  						=7
								_lowerCAmelCase  						=True
								_lowerCAmelCase  						=True
								_lowerCAmelCase  						=True
								_lowerCAmelCase  						=True
								_lowerCAmelCase  						=True
								_lowerCAmelCase  						=99
								_lowerCAmelCase  						=32
								_lowerCAmelCase  						=2
								_lowerCAmelCase  						=4
								_lowerCAmelCase  						=37
								_lowerCAmelCase  						="""gelu"""
								_lowerCAmelCase  						=0.1
								_lowerCAmelCase  						=0.1
								_lowerCAmelCase  						=512
								_lowerCAmelCase  						=16
								_lowerCAmelCase  						=2
								_lowerCAmelCase  						=0.02
								_lowerCAmelCase  						=3
								_lowerCAmelCase  						=4
								_lowerCAmelCase  						=None
								_lowerCAmelCase  						=self.vocab_size - 1






				def        lowerCAmelCase__			(				self     :    List[Any]					):
								_lowerCAmelCase  						=ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size					)

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

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

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

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

								_lowerCAmelCase  						=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  						=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     :    Optional[Any]	,							lowerCamelCase_     :    Tuple	,							lowerCamelCase_     :    List[str]	,							lowerCamelCase_     :    Optional[int]	,							lowerCamelCase_     :    Any	,							lowerCamelCase_     :    int	,							*lowerCamelCase_     :    List[Any]					):
								_lowerCAmelCase  						=TFCTRLModel(config=lowerCamelCase_					)
								_lowerCAmelCase  						={"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								_lowerCAmelCase  						=model(lowerCamelCase_					)

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

								_lowerCAmelCase  						=model(lowerCamelCase_					)

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






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






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






				def        lowerCAmelCase__			(				self     :    List[Any]					):
								_lowerCAmelCase  						=self.prepare_config_and_inputs()

								(
								    (
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							
								)  						=config_and_inputs

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


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


				"""simple docstring"""







				a_:		Tuple					      =	(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
				a_:		Union[str, 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_:		Optional[int]					      =	False
				a_:		Any					      =	False






				def        lowerCAmelCase__			(				self     :    Dict	,							lowerCamelCase_     :    Tuple	,							lowerCamelCase_     :    List[Any]	,							lowerCamelCase_     :    List[str]	,							lowerCamelCase_     :    Union[str, Any]	,							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        lowerCAmelCase__			(				self     :    List[str]					):
								_lowerCAmelCase  						=TFCTRLModelTester(self					)
								_lowerCAmelCase  						=ConfigTester(self	,							config_class=lowerCamelCase_	,							n_embd=37					)






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






				def        lowerCAmelCase__			(				self     :    Dict					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    List[Any]					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_lm_head(*lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    Tuple					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowerCamelCase_					)






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

								for model_class in self.all_model_classes:
												_lowerCAmelCase  						=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  						=model.get_output_embeddings()
																assert isinstance(lowerCamelCase_	,							tf.keras.layers.Layer					)
																_lowerCAmelCase  						=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  						=model.get_output_embeddings()
																assert isinstance(lowerCamelCase_	,							tf.keras.layers.Layer					)
																_lowerCAmelCase  						=model.get_bias()
																assert name is None
												else:
																_lowerCAmelCase  						=model.get_output_embeddings()
																assert x is None
																_lowerCAmelCase  						=model.get_bias()
																assert name is None






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


@require_tf
class        __lowerCamelCase      (				unittest.TestCase ):


				"""simple docstring"""







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

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





def 		snake_case_   (				lowercase__     :    str     ):






				'''simple docstring'''






				_lowerCAmelCase  						=53_81
				for x in s:
								_lowerCAmelCase  						=((hash_value << 5) + hash_value) + ord(lowercase__     )
				return hash_value & 0Xffff_ffff

