






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        _SCREAMING_SNAKE_CASE			(     _lowercase ):





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





	def 		_A					(						self							:   List[Any]      ):
		UpperCamelCase	:Optional[int]		=      ids_tensor([self.batch_size, self.seq_length]					,    self.vocab_size      )

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

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

		UpperCamelCase	:Optional[Any]		=      None
		if self.use_mc_token_ids:
			UpperCamelCase	:int		=      ids_tensor([self.batch_size, self.num_choices]					,    self.seq_length      )

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

		UpperCamelCase	:Tuple		=      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					,    )

		UpperCamelCase	:str		=      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							:   Dict					,    __lowerCamelCase							:   int					,    __lowerCamelCase							:   Dict					,    __lowerCamelCase							:   Dict					,    __lowerCamelCase							:   List[str]					,    __lowerCamelCase							:   Tuple					,    *__lowerCamelCase							:   Dict      ):
		UpperCamelCase	:Tuple		=      TFCTRLModel(config=__snake_case      )
		UpperCamelCase	:List[str]		=      {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		UpperCamelCase	:List[str]		=      model(__snake_case      )

		UpperCamelCase	:Tuple		=      [input_ids, None, input_mask]  # None is the input for 'past'
		UpperCamelCase	:Optional[Any]		=      model(__snake_case      )

		UpperCamelCase	:Optional[Any]		=      model(__snake_case      )

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





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





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





	def 		_A					(						self							:   List[Any]      ):
		UpperCamelCase	:Dict		=      self.prepare_config_and_inputs()

		(
		    UpperCamelCase		
		)	:Optional[int]		=      config_and_inputs

		UpperCamelCase	:List[str]		=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
		return config, inputs_dict

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





	def 		_A					(						self							:   Any					,    __lowerCamelCase							:   Any					,    __lowerCamelCase							:   str					,    __lowerCamelCase							:   int					,    __lowerCamelCase							:   Union[str, Any]					,    __lowerCamelCase							:   str      ):
		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]      ):
		UpperCamelCase	:List[Any]		=      TFCTRLModelTester(self      )
		UpperCamelCase	:Any		=      ConfigTester(self					,    config_class=__snake_case					,    n_embd=37      )





	def 		_A					(						self							:   Union[str, Any]      ):
		self.config_tester.run_common_tests()





	def 		_A					(						self							:   str      ):
		UpperCamelCase	:Optional[int]		=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_ctrl_model(*__snake_case      )





	def 		_A					(						self							:   Any      ):
		UpperCamelCase	:Optional[int]		=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_ctrl_lm_head(*__snake_case      )





	def 		_A					(						self							:   List[str]      ):
		UpperCamelCase	:Optional[int]		=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_ctrl_for_sequence_classification(*__snake_case      )





	def 		_A					(						self							:   Optional[Any]      ):
		UpperCamelCase	:Optional[Any]		=      self.model_tester.prepare_config_and_inputs_for_common()
		UpperCamelCase	:str		=      [TFCTRLLMHeadModel]
		UpperCamelCase	:Union[str, Any]		=      [TFCTRLForSequenceClassification]

		for model_class in self.all_model_classes:
			UpperCamelCase	:int		=      model_class(__snake_case      )
			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:
				UpperCamelCase	:Tuple		=      model.get_output_embeddings()
				assert isinstance(__snake_case					,    tf.keras.layers.Layer      )
				UpperCamelCase	:Any		=      model.get_bias()
				assert isinstance(__snake_case					,    __snake_case      )
				for k, v in name.items():
					assert isinstance(__snake_case					,    tf.Variable      )
			elif model_class in list_other_models_with_output_ebd:
				UpperCamelCase	:int		=      model.get_output_embeddings()
				assert isinstance(__snake_case					,    tf.keras.layers.Layer      )
				UpperCamelCase	:List[Any]		=      model.get_bias()
				assert name is None
			else:
				UpperCamelCase	:Union[str, Any]		=      model.get_output_embeddings()
				assert x is None
				UpperCamelCase	:Any		=      model.get_bias()
				assert name is None





	@slow
	def 		_A					(						self							:   Optional[int]      ):
		for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			UpperCamelCase	:Optional[int]		=      TFCTRLModel.from_pretrained(__snake_case      )
			self.assertIsNotNone(__snake_case      )

@require_tf
class        _SCREAMING_SNAKE_CASE			(     unittest.TestCase ):





	@slow
	def 		_A					(						self							:   List[str]      ):
		UpperCamelCase	:Optional[Any]		=      TFCTRLLMHeadModel.from_pretrained("""ctrl"""      )
		UpperCamelCase	:Tuple		=      tf.convert_to_tensor([[11_859, 0, 1_611, 8]]					,    dtype=tf.intaa      )  # Legal the president is
		UpperCamelCase	:Optional[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

		UpperCamelCase	:Any		=      model.generate(__snake_case					,    do_sample=__snake_case      )
		self.assertListEqual(output_ids[0].numpy().tolist()					,    __snake_case      )



from typing import TYPE_CHECKING

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


lowerCAmelCase__             =		{"""configuration_vit_hybrid""": ["""VIT_HYBRID_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTHybridConfig"""]}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowerCAmelCase__             =		[
							    """VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """ViTHybridForImageClassification""",
							    """ViTHybridModel""",
							    """ViTHybridPreTrainedModel""",
							]

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowerCAmelCase__             =		["""ViTHybridImageProcessor"""]


if TYPE_CHECKING:
							from .configuration_vit_hybrid import VIT_HYBRID_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTHybridConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_vit_hybrid import (
														    VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST,
														    ViTHybridForImageClassification,
														    ViTHybridModel,
														    ViTHybridPreTrainedModel,
														)

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .image_processing_vit_hybrid import ViTHybridImageProcessor


else:
							import sys

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




