






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_							(				__UpperCAmelCase       ):




						def __init__(    self		,	lowercase		,	)			->						Tuple:
													_a  :    List[str]			=						parent
													_a  :    Dict			=						13
													_a  :    Any			=						7
													_a  :    Union[str, Any]			=						True
													_a  :    Any			=						True
													_a  :    Any			=						True
													_a  :    List[str]			=						True
													_a  :    str			=						True
													_a  :    List[Any]			=						99
													_a  :    Optional[int]			=						32
													_a  :    Any			=						2
													_a  :    str			=						4
													_a  :    int			=						37
													_a  :    str			=						'''gelu'''
													_a  :    Any			=						0.1
													_a  :    Optional[int]			=						0.1
													_a  :    Any			=						512
													_a  :    Any			=						16
													_a  :    Any			=						2
													_a  :    Dict			=						0.02
													_a  :    int			=						3
													_a  :    Tuple			=						4
													_a  :    Union[str, Any]			=						None
													_a  :    int			=						self.vocab_size - 1




						def      snake_case__(    self       )			->						Tuple:
													_a  :    Union[str, Any]			=						ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size       )

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

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

													_a  :    List[str]			=						None
													if self.use_mc_token_ids:
																				_a  :    Optional[int]			=						ids_tensor([self.batch_size, self.num_choices]		,	self.seq_length       )

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

													_a  :    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  :    Union[str, 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      snake_case__(    self		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	*lowercase       )			->						List[Any]:
													_a  :    Dict			=						TFCTRLModel(config=UpperCAmelCase_       )
													_a  :    Any			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													_a  :    List[Any]			=						model(UpperCAmelCase_       )

													_a  :    Tuple			=						[input_ids, None, input_mask]  # None is the input for 'past'
													_a  :    List[str]			=						model(UpperCAmelCase_       )

													_a  :    Dict			=						model(UpperCAmelCase_       )

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




						def      snake_case__(    self		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	*lowercase       )			->						Optional[Any]:
													_a  :    Optional[Any]			=						TFCTRLLMHeadModel(config=UpperCAmelCase_       )
													_a  :    Any			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													_a  :    List[str]			=						model(UpperCAmelCase_       )
													self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.seq_length, self.vocab_size)       )




						def      snake_case__(    self		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	*lowercase       )			->						Union[str, Any]:
													_a  :    Any			=						self.num_labels
													_a  :    str			=						ids_tensor([self.batch_size]		,	self.type_sequence_label_size       )
													_a  :    Optional[int]			=						{
													    '''input_ids''': input_ids,
													    '''token_type_ids''': token_type_ids,
													    '''labels''': sequence_labels,
													}
													_a  :    Any			=						TFCTRLForSequenceClassification(UpperCAmelCase_       )
													_a  :    List[str]			=						model(UpperCAmelCase_       )
													self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.num_labels)       )







						def      snake_case__(    self       )			->						Any:
													_a  :    Optional[Any]			=						self.prepare_config_and_inputs()

													(
													    (
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					(
													    _a  
													)		,					  
													)  :    Dict			=						config_and_inputs

													_a  :    str			=						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
													return config, inputs_dict

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




						def      snake_case__(    self		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase       )			->						Tuple:
													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      snake_case__(    self       )			->						Any:
													_a  :    int			=						TFCTRLModelTester(self       )
													_a  :    Dict			=						ConfigTester(self		,	config_class=UpperCAmelCase_		,	n_embd=37       )




						def      snake_case__(    self       )			->						Optional[Any]:
													self.config_tester.run_common_tests()




						def      snake_case__(    self       )			->						List[str]:
													_a  :    Union[str, Any]			=						self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_       )




						def      snake_case__(    self       )			->						List[Any]:
													_a  :    Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_ctrl_lm_head(*UpperCAmelCase_       )




						def      snake_case__(    self       )			->						List[str]:
													_a  :    Union[str, Any]			=						self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCAmelCase_       )




						def      snake_case__(    self       )			->						List[str]:
													_a		,					_a  :    Union[str, Any]			=						self.model_tester.prepare_config_and_inputs_for_common()
													_a  :    int			=						[TFCTRLLMHeadModel]
													_a  :    str			=						[TFCTRLForSequenceClassification]

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







						@slow
						def      snake_case__(    self       )			->						Any:
													for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				_a  :    List[Any]			=						TFCTRLModel.from_pretrained(UpperCAmelCase_       )
																				self.assertIsNotNone(UpperCAmelCase_       )

@require_tf
class      UpperCamelCase_							(				unittest.TestCase       ):




						@slow
						def      snake_case__(    self       )			->						Optional[int]:
													_a  :    str			=						TFCTRLLMHeadModel.from_pretrained('''ctrl'''       )
													_a  :    Any			=						tf.convert_to_tensor([[11_859, 0, 1_611, 8]]		,	dtype=tf.intaa       )  # Legal the president is
													_a  :    Union[str, Any]			=						[
													    11_859,
													    0,
													    1_611,
													    8,
													    5,
													    150,
													    26_449,
													    2,
													    19,
													    348,
													    469,
													    3,
													    2_595,
													    48,
													    20_740,
													    246_533,
													    246_533,
													    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  :    Optional[Any]			=						model.generate(UpperCAmelCase_		,	do_sample=UpperCAmelCase_       )
													self.assertListEqual(output_ids[0].numpy().tolist()		,	UpperCAmelCase_       )





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCAmelCase_    ={
    """configuration_splinter""": ["""SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SplinterConfig"""],
    """tokenization_splinter""": ["""SplinterTokenizer"""],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase_    =["""SplinterTokenizerFast"""]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase_    =[
		    """SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST""",
		    """SplinterForQuestionAnswering""",
		    """SplinterForPreTraining""",
		    """SplinterLayer""",
		    """SplinterModel""",
		    """SplinterPreTrainedModel""",
		]


if TYPE_CHECKING:
		from .configuration_splinter import SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP, SplinterConfig
		from .tokenization_splinter import SplinterTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_splinter_fast import SplinterTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_splinter import (
				    SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SplinterForPreTraining,
				    SplinterForQuestionAnswering,
				    SplinterLayer,
				    SplinterModel,
				    SplinterPreTrainedModel,
				)


else:
		import sys

		UpperCAmelCase_    =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



