


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				,							a_				,							):
							lowerCamelCase_			:							Tuple      =       parent
							lowerCamelCase_			:							Optional[int]      =       13
							lowerCamelCase_			:							Union[str, Any]      =       7
							lowerCamelCase_			:							Union[str, Any]      =       True
							lowerCamelCase_			:							Optional[Any]      =       True
							lowerCamelCase_			:							Tuple      =       True
							lowerCamelCase_			:							Union[str, Any]      =       True
							lowerCamelCase_			:							Tuple      =       True
							lowerCamelCase_			:							Any      =       99
							lowerCamelCase_			:							Union[str, Any]      =       32
							lowerCamelCase_			:							Union[str, Any]      =       2
							lowerCamelCase_			:							str      =       4
							lowerCamelCase_			:							Optional[Any]      =       37
							lowerCamelCase_			:							Any      =       "gelu"
							lowerCamelCase_			:							str      =       0.1
							lowerCamelCase_			:							List[str]      =       0.1
							lowerCamelCase_			:							str      =       512
							lowerCamelCase_			:							Any      =       16
							lowerCamelCase_			:							Any      =       2
							lowerCamelCase_			:							Optional[Any]      =       0.02
							lowerCamelCase_			:							int      =       3
							lowerCamelCase_			:							Tuple      =       4
							lowerCamelCase_			:							Union[str, Any]      =       None
							lowerCamelCase_			:							Dict      =       self.vocab_size - 1




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Dict      =       ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size     )

							lowerCamelCase_			:							Dict      =       None
							if self.use_input_mask:
								lowerCamelCase_			:							Tuple      =       random_attention_mask([self.batch_size, self.seq_length]     )

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

							lowerCamelCase_			:							Union[str, Any]      =       None
							if self.use_mc_token_ids:
								lowerCamelCase_			:							List[str]      =       ids_tensor([self.batch_size, self.num_choices]				,							self.seq_length     )

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

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

							lowerCamelCase_			:							Dict      =       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				,							a_				,							a_				,							a_				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							Tuple      =       TFCTRLModel(config=_lowerCamelCase     )
							lowerCamelCase_			:							Any      =       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCamelCase_			:							Union[str, Any]      =       model(_lowerCamelCase     )

							lowerCamelCase_			:							List[str]      =       [input_ids, None, input_mask]  # None is the input for 'past'
							lowerCamelCase_			:							Any      =       model(_lowerCamelCase     )

							lowerCamelCase_			:							Any      =       model(_lowerCamelCase     )

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




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




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							List[str]      =       self.num_labels
							lowerCamelCase_			:							str      =       ids_tensor([self.batch_size]				,							self.type_sequence_label_size     )
							lowerCamelCase_			:							List[Any]      =       {
							    "input_ids": input_ids,
							    "token_type_ids": token_type_ids,
							    "labels": sequence_labels,
							}
							lowerCamelCase_			:							Any      =       TFCTRLForSequenceClassification(_lowerCamelCase     )
							lowerCamelCase_			:							List[Any]      =       model(_lowerCamelCase     )
							self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.num_labels)     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Tuple      =       self.prepare_config_and_inputs()

							(
							    (
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,(
							    lowerCamelCase_   
							)   ,   
							)			:							Tuple      =       config_and_inputs

							lowerCamelCase_			:							Optional[Any]      =       {"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"""
						__UpperCAmelCase   :		Optional[int]  					=	(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   :		Union[str, Any]  					=	False
						__UpperCAmelCase   :		Dict  					=	False




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_     ):
							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     ):
							lowerCamelCase_			:							Tuple      =       TFCTRLModelTester(self     )
							lowerCamelCase_			:							List[Any]      =       ConfigTester(self				,							config_class=_lowerCamelCase				,							n_embd=37     )




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




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Union[str, Any]      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_model(*_lowerCamelCase     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							List[str]      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_lm_head(*_lowerCamelCase     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Tuple      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_for_sequence_classification(*_lowerCamelCase     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_   ,lowerCamelCase_			:							Union[str, Any]      =       self.model_tester.prepare_config_and_inputs_for_common()
							lowerCamelCase_			:							Dict      =       [TFCTRLLMHeadModel]
							lowerCamelCase_			:							Union[str, Any]      =       [TFCTRLForSequenceClassification]

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




						@slow
						def 					_UpperCamelCase			(   self     ):
							for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCamelCase_			:							Optional[Any]      =       TFCTRLModel.from_pretrained(_lowerCamelCase     )
								self.assertIsNotNone(_lowerCamelCase     )





@require_tf
class    lowerCAmelCase__				(			unittest.TestCase       ):






						"""simple docstring"""




						@slow
						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							List[Any]      =       TFCTRLLMHeadModel.from_pretrained("ctrl"     )
							lowerCamelCase_			:							Optional[int]      =       tf.convert_to_tensor([[1_1859, 0, 1611, 8]]				,							dtype=tf.intaa     )  # Legal the president is
							lowerCamelCase_			:							Tuple      =       [
							    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_			:							Dict      =       model.generate(_lowerCamelCase				,							do_sample=_lowerCamelCase     )
							self.assertListEqual(output_ids[0].numpy().tolist()				,							_lowerCamelCase     )





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__magic_name__       					=      {
    '''configuration_big_bird''': ['''BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BigBirdConfig''', '''BigBirdOnnxConfig'''],
}

try:
							if not is_sentencepiece_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      ['''BigBirdTokenizer''']

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      ['''BigBirdTokenizerFast''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      [
							    '''BIG_BIRD_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''BigBirdForCausalLM''',
							    '''BigBirdForMaskedLM''',
							    '''BigBirdForMultipleChoice''',
							    '''BigBirdForPreTraining''',
							    '''BigBirdForQuestionAnswering''',
							    '''BigBirdForSequenceClassification''',
							    '''BigBirdForTokenClassification''',
							    '''BigBirdLayer''',
							    '''BigBirdModel''',
							    '''BigBirdPreTrainedModel''',
							    '''load_tf_weights_in_big_bird''',
							]

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      [
							    '''FlaxBigBirdForCausalLM''',
							    '''FlaxBigBirdForMaskedLM''',
							    '''FlaxBigBirdForMultipleChoice''',
							    '''FlaxBigBirdForPreTraining''',
							    '''FlaxBigBirdForQuestionAnswering''',
							    '''FlaxBigBirdForSequenceClassification''',
							    '''FlaxBigBirdForTokenClassification''',
							    '''FlaxBigBirdModel''',
							    '''FlaxBigBirdPreTrainedModel''',
							]

if TYPE_CHECKING:
							from .configuration_big_bird import BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdConfig, BigBirdOnnxConfig

							try:
														if not is_sentencepiece_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_big_bird import BigBirdTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_big_bird_fast import BigBirdTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_big_bird import (
														    BIG_BIRD_PRETRAINED_MODEL_ARCHIVE_LIST,
														    BigBirdForCausalLM,
														    BigBirdForMaskedLM,
														    BigBirdForMultipleChoice,
														    BigBirdForPreTraining,
														    BigBirdForQuestionAnswering,
														    BigBirdForSequenceClassification,
														    BigBirdForTokenClassification,
														    BigBirdLayer,
														    BigBirdModel,
														    BigBirdPreTrainedModel,
														    load_tf_weights_in_big_bird,
														)

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_big_bird import (
														    FlaxBigBirdForCausalLM,
														    FlaxBigBirdForMaskedLM,
														    FlaxBigBirdForMultipleChoice,
														    FlaxBigBirdForPreTraining,
														    FlaxBigBirdForQuestionAnswering,
														    FlaxBigBirdForSequenceClassification,
														    FlaxBigBirdForTokenClassification,
														    FlaxBigBirdModel,
														    FlaxBigBirdPreTrainedModel,
														)

else:
							import sys

							__magic_name__       					=      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

