






from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, 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.openai.modeling_tf_openai import (
				    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFOpenAIGPTDoubleHeadsModel,
				    TFOpenAIGPTForSequenceClassification,
				    TFOpenAIGPTLMHeadModel,
				    TFOpenAIGPTModel,
				)


class 				__a				:





		'''simple docstring'''



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


		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size							)

						SCREAMING_SNAKE_CASE_ :						Any    				= None
						if self.use_input_mask:
										SCREAMING_SNAKE_CASE_ :						Optional[int]    				= random_attention_mask([self.batch_size, self.seq_length]							)

						SCREAMING_SNAKE_CASE_ :						List[str]    				= None
						if self.use_token_type_ids:
										SCREAMING_SNAKE_CASE_ :						Tuple    				= ids_tensor([self.batch_size, self.seq_length]			,     self.type_vocab_size							)

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

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

						SCREAMING_SNAKE_CASE_ :						int    				= OpenAIGPTConfig(
						    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			,     )

						SCREAMING_SNAKE_CASE_ :						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 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     *UpperCamelCase__							):
						SCREAMING_SNAKE_CASE_ :						str    				= TFOpenAIGPTModel(config=UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						int    				= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
						SCREAMING_SNAKE_CASE_ :						List[str]    				= model(UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= [input_ids, input_mask]
						SCREAMING_SNAKE_CASE_ :						List[str]    				= model(UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= model(UpperCamelCase__							)

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


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     *UpperCamelCase__							):
						SCREAMING_SNAKE_CASE_ :						List[str]    				= TFOpenAIGPTLMHeadModel(config=UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Any    				= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
						SCREAMING_SNAKE_CASE_ :						Dict    				= model(UpperCamelCase__							)
						self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)							)


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     *UpperCamelCase__							):
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= TFOpenAIGPTDoubleHeadsModel(config=UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						str    				= tf.tile(tf.expand_dims(UpperCamelCase__			,     1							)			,     (1, self.num_choices, 1)							)
						SCREAMING_SNAKE_CASE_ :						int    				= tf.tile(tf.expand_dims(UpperCamelCase__			,     1							)			,     (1, self.num_choices, 1)							)
						SCREAMING_SNAKE_CASE_ :						int    				= tf.tile(tf.expand_dims(UpperCamelCase__			,     1							)			,     (1, self.num_choices, 1)							)

						SCREAMING_SNAKE_CASE_ :						Tuple    				= {
						    'input_ids': multiple_choice_inputs_ids,
						    'mc_token_ids': mc_token_ids,
						    'attention_mask': multiple_choice_input_mask,
						    'token_type_ids': multiple_choice_token_type_ids,
						}
						SCREAMING_SNAKE_CASE_ :						str    				= model(UpperCamelCase__							)
						self.parent.assertEqual(
						    result.logits.shape			,     (self.batch_size, self.num_choices, self.seq_length, self.vocab_size)							)
						self.parent.assertEqual(result.mc_logits.shape			,     (self.batch_size, self.num_choices)							)


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     *UpperCamelCase__							):
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= self.num_labels
						SCREAMING_SNAKE_CASE_ :						Dict    				= ids_tensor([self.batch_size]			,     self.type_sequence_label_size							)
						SCREAMING_SNAKE_CASE_ :						int    				= {
						    'input_ids': input_ids,
						    'attention_mask': input_mask,
						    'token_type_ids': token_type_ids,
						    'labels': sequence_labels,
						}
						SCREAMING_SNAKE_CASE_ :						Tuple    				= TFOpenAIGPTForSequenceClassification(UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						str    				= model(UpperCamelCase__							)
						self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)							)






		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						Tuple    				= self.prepare_config_and_inputs()

						(
						    (
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							(
						    SCREAMING_SNAKE_CASE_
						)				,							
						) :						Optional[int]    				= config_and_inputs

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






@require_tf
class 				__a				(	__A			,						__A			,						unittest.TestCase    ):





		'''simple docstring'''



		UpperCAmelCase__   :					Tuple					     =    (
		    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
		    if is_tf_available()
		    else ()
		)
		UpperCAmelCase__   :					int					     =    (
		    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
		)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
		UpperCAmelCase__   :					str					     =    (
		    {
		        """feature-extraction""": TFOpenAIGPTModel,
		        """text-classification""": TFOpenAIGPTForSequenceClassification,
		        """text-generation""": TFOpenAIGPTLMHeadModel,
		        """zero-shot""": TFOpenAIGPTForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		UpperCAmelCase__   :					Any					     =    False
		UpperCAmelCase__   :					Tuple					     =    False


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							):
						if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
										# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
										# `OpenAIGPTConfig` 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							):
						SCREAMING_SNAKE_CASE_ :						int    				= TFOpenAIGPTModelTester(self							)
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= ConfigTester(self			,     config_class=UpperCamelCase__			,     n_embd=37							)


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


		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase__							)


		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						Any    				= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCamelCase__							)


		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						int    				= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_double_head(*UpperCamelCase__							)


		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase__							)






		@slow
		def 	__snake_case      (    self							):
						for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										SCREAMING_SNAKE_CASE_ :						int    				= TFOpenAIGPTModel.from_pretrained(UpperCamelCase__							)
										self.assertIsNotNone(UpperCamelCase__							)






@require_tf
class 				__a				(	unittest.TestCase    ):





		'''simple docstring'''



		@slow
		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'							)
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= tf.convert_to_tensor([[481, 4735, 544]]			,     dtype=tf.intaa							)  # the president is
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= [
						    481,
						    4735,
						    544,
						    246,
						    963,
						    870,
						    762,
						    239,
						    244,
						    40477,
						    244,
						    249,
						    719,
						    881,
						    487,
						    544,
						    240,
						    244,
						    603,
						    481,
						]  # the president is a very good man. " \n " i\'m sure he is, " said the

						SCREAMING_SNAKE_CASE_ :						List[str]    				= model.generate(UpperCamelCase__			,     do_sample=UpperCamelCase__							)
						self.assertListEqual(output_ids[0].numpy().tolist()			,     UpperCamelCase__							)







from ...configuration_utils import PretrainedConfig
from ...utils import logging


A										=      logging.get_logger(__name__)

A										=      {
    'ZinengTang/tvlt-base': 'https://huggingface.co/ZinengTang/tvlt-base/blob/main/config.json',
}


class 				__a				(	__A    ):





		'''simple docstring'''



		UpperCAmelCase__   :					str					     =    """tvlt"""


		def __init__(    self			,     UpperCamelCase__=224			,     UpperCamelCase__=2048			,     UpperCamelCase__=128			,     UpperCamelCase__=[16, 16]			,     UpperCamelCase__=[16, 16]			,     UpperCamelCase__=3			,     UpperCamelCase__=1			,     UpperCamelCase__=8			,     UpperCamelCase__=768			,     UpperCamelCase__=12			,     UpperCamelCase__=12			,     UpperCamelCase__=3072			,     UpperCamelCase__="gelu"			,     UpperCamelCase__=0.0			,     UpperCamelCase__=0.0			,     UpperCamelCase__=0.02			,     UpperCamelCase__=1E-6			,     UpperCamelCase__=True			,     UpperCamelCase__=False			,     UpperCamelCase__=16			,     UpperCamelCase__=512			,     UpperCamelCase__=8			,     UpperCamelCase__=2048			,     UpperCamelCase__=0.75			,     UpperCamelCase__=0.15			,     UpperCamelCase__="frame-level"			,     UpperCamelCase__=True			,     UpperCamelCase__=True			,     UpperCamelCase__="classification"			,     **UpperCamelCase__			,     ):
						super().__init__(**UpperCamelCase__							)

						if audio_mask_type not in ("frame-level", "patch_level"):
										raise ValueError(
										    'audio_mask_type must be one of two acceptable strategies - {\'frame_level\', \'patch-level\') '
										    F'''got {audio_mask_type}'''							)

						SCREAMING_SNAKE_CASE_ :						Tuple    				= image_size
						SCREAMING_SNAKE_CASE_ :						int    				= spectrogram_length
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= frequency_length
						SCREAMING_SNAKE_CASE_ :						Any    				= image_patch_size
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= audio_patch_size
						SCREAMING_SNAKE_CASE_ :						int    				= num_image_channels
						SCREAMING_SNAKE_CASE_ :						Any    				= num_audio_channels
						SCREAMING_SNAKE_CASE_ :						Any    				= num_frames

						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= hidden_size
						SCREAMING_SNAKE_CASE_ :						str    				= num_hidden_layers
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= num_attention_heads
						SCREAMING_SNAKE_CASE_ :						str    				= intermediate_size
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= hidden_act
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= hidden_dropout_prob
						SCREAMING_SNAKE_CASE_ :						int    				= attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE_ :						Dict    				= initializer_range
						SCREAMING_SNAKE_CASE_ :						Dict    				= layer_norm_eps
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= qkv_bias
						SCREAMING_SNAKE_CASE_ :						List[str]    				= use_mean_pooling

						SCREAMING_SNAKE_CASE_ :						str    				= decoder_num_attention_heads
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= decoder_hidden_size
						SCREAMING_SNAKE_CASE_ :						Any    				= decoder_num_hidden_layers
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= decoder_intermediate_size
						SCREAMING_SNAKE_CASE_ :						List[str]    				= pixel_mask_ratio
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= audio_mask_ratio
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= audio_mask_type

						SCREAMING_SNAKE_CASE_ :						Any    				= task_matching
						SCREAMING_SNAKE_CASE_ :						Any    				= task_mae
						SCREAMING_SNAKE_CASE_ :						int    				= loss_type