
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      A__       (						lowerCAmelCase__    ):





							def __init__(			self  :       Dict					,			_UpperCAmelCase  :       Tuple					,			)						->		Any:



											"""simple docstring"""


											__lowercase								= parent
											__lowercase								= 13
											__lowercase								= 7
											__lowercase								= True
											__lowercase								= True
											__lowercase								= True
											__lowercase								= True
											__lowercase								= True
											__lowercase								= 99
											__lowercase								= 32
											__lowercase								= 2
											__lowercase								= 4
											__lowercase								= 37
											__lowercase								= 'gelu'
											__lowercase								= 0.1
											__lowercase								= 0.1
											__lowercase								= 5_12
											__lowercase								= 16
											__lowercase								= 2
											__lowercase								= 0.02
											__lowercase								= 3
											__lowercase								= 4
											__lowercase								= None
											__lowercase								= self.vocab_size - 1





							def       a__			(			self  :       Union[str, Any]    )						->		List[str]:



											"""simple docstring"""


											__lowercase								= ids_tensor([self.batch_size, self.seq_length]					,			self.vocab_size    )

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

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

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

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

											__lowercase								= 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					,			)

											__lowercase								= 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  :       Optional[int]					,			_UpperCAmelCase  :       Tuple					,			_UpperCAmelCase  :       Optional[int]					,			_UpperCAmelCase  :       str					,			_UpperCAmelCase  :       Union[str, Any]					,			_UpperCAmelCase  :       str					,			*_UpperCAmelCase  :       Union[str, Any]    )						->		List[str]:



											"""simple docstring"""


											__lowercase								= TFCTRLModel(config=_UpperCAmelCase    )
											__lowercase								= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											__lowercase								= model(_UpperCAmelCase    )

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

											__lowercase								= model(_UpperCAmelCase    )

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





							def       a__			(			self  :       int					,			_UpperCAmelCase  :       List[str]					,			_UpperCAmelCase  :       Optional[Any]					,			_UpperCAmelCase  :       Tuple					,			_UpperCAmelCase  :       Any					,			_UpperCAmelCase  :       Optional[int]					,			*_UpperCAmelCase  :       Optional[Any]    )						->		List[str]:



											"""simple docstring"""


											__lowercase								= TFCTRLLMHeadModel(config=_UpperCAmelCase    )
											__lowercase								= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											__lowercase								= model(_UpperCAmelCase    )
											self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.seq_length, self.vocab_size)    )





							def       a__			(			self  :       Optional[Any]					,			_UpperCAmelCase  :       Any					,			_UpperCAmelCase  :       List[Any]					,			_UpperCAmelCase  :       Dict					,			_UpperCAmelCase  :       str					,			_UpperCAmelCase  :       List[str]					,			*_UpperCAmelCase  :       Optional[Any]    )						->		int:



											"""simple docstring"""


											__lowercase								= self.num_labels
											__lowercase								= ids_tensor([self.batch_size]					,			self.type_sequence_label_size    )
											__lowercase								= {
											    'input_ids': input_ids,
											    'token_type_ids': token_type_ids,
											    'labels': sequence_labels,
											}
											__lowercase								= TFCTRLForSequenceClassification(_UpperCAmelCase    )
											__lowercase								= model(_UpperCAmelCase    )
											self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.num_labels)    )





							def       a__			(			self  :       List[Any]    )						->		Dict:



											"""simple docstring"""


											__lowercase								= self.prepare_config_and_inputs()

											(
											    (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, (
											    __lowercase
											)		, 
											)								= config_and_inputs

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






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





							def       a__			(			self  :       str					,			_UpperCAmelCase  :       Tuple					,			_UpperCAmelCase  :       Optional[int]					,			_UpperCAmelCase  :       Dict					,			_UpperCAmelCase  :       Optional[Any]					,			_UpperCAmelCase  :       int    )						->		int:



											"""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[Any]    )						->		List[str]:



											"""simple docstring"""


											__lowercase								= TFCTRLModelTester(self    )
											__lowercase								= ConfigTester(self					,			config_class=_UpperCAmelCase					,			n_embd=37    )





							def       a__			(			self  :       int    )						->		Optional[Any]:



											"""simple docstring"""


											self.config_tester.run_common_tests()





							def       a__			(			self  :       List[Any]    )						->		str:



											"""simple docstring"""


											__lowercase								= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_ctrl_model(*_UpperCAmelCase    )





							def       a__			(			self  :       Union[str, Any]    )						->		int:



											"""simple docstring"""


											__lowercase								= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_ctrl_lm_head(*_UpperCAmelCase    )





							def       a__			(			self  :       int    )						->		Optional[int]:



											"""simple docstring"""


											__lowercase								= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_ctrl_for_sequence_classification(*_UpperCAmelCase    )





							def       a__			(			self  :       Any    )						->		List[str]:



											"""simple docstring"""


											__lowercase		, __lowercase								= self.model_tester.prepare_config_and_inputs_for_common()
											__lowercase								= [TFCTRLLMHeadModel]
											__lowercase								= [TFCTRLForSequenceClassification]

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





							@slow
							def       a__			(			self  :       Union[str, Any]    )						->		Union[str, Any]:



											"""simple docstring"""


											for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__lowercase								= TFCTRLModel.from_pretrained(_UpperCAmelCase    )
															self.assertIsNotNone(_UpperCAmelCase    )






@require_tf
class      A__       (						unittest.TestCase    ):





							@slow
							def       a__			(			self  :       Dict    )						->		List[Any]:



											"""simple docstring"""


											__lowercase								= TFCTRLLMHeadModel.from_pretrained('ctrl'    )
											__lowercase								= tf.convert_to_tensor([[1_18_59, 0, 16_11, 8]]					,			dtype=tf.intaa    )  # Legal the president is
											__lowercase								= [
											    1_18_59,
											    0,
											    16_11,
											    8,
											    5,
											    1_50,
											    2_64_49,
											    2,
											    19,
											    3_48,
											    4_69,
											    3,
											    25_95,
											    48,
											    2_07_40,
											    24_65_33,
											    24_65_33,
											    19,
											    30,
											    5,
											]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

											__lowercase								= 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_tf_available, is_torch_available


SCREAMING_SNAKE_CASE__							=    {"""configuration_hubert""": ["""HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """HubertConfig"""]}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__							=    [
							    """HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """HubertForCTC""",
							    """HubertForSequenceClassification""",
							    """HubertModel""",
							    """HubertPreTrainedModel""",
							]


try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__							=    [
							    """TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFHubertForCTC""",
							    """TFHubertModel""",
							    """TFHubertPreTrainedModel""",
							]

if TYPE_CHECKING:
							from .configuration_hubert import HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, HubertConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_hubert import (
														    HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    HubertForCTC,
														    HubertForSequenceClassification,
														    HubertModel,
														    HubertPreTrainedModel,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_hubert import (
														    TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFHubertForCTC,
														    TFHubertModel,
														    TFHubertPreTrainedModel,
														)


else:
							import sys

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





