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__						:

					def __init__(   self				:	Dict,_A				:	Tuple,):







							"""simple docstring"""






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

					def     __UpperCamelCase       (   self				:	int   ):







							"""simple docstring"""






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

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

							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   None
							if self.use_token_type_ids:
									SCREAMING_SNAKE_CASE_  :		List[Any]    				=   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_  :		List[Any]    				=   ids_tensor([self.batch_size, self.num_choices],self.seq_length   )

							SCREAMING_SNAKE_CASE_  :		Tuple    				=   None
							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   None
							SCREAMING_SNAKE_CASE_  :		List[str]    				=   None
							if self.use_labels:
									SCREAMING_SNAKE_CASE_  :		str    				=   ids_tensor([self.batch_size],self.type_sequence_label_size   )
									SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   ids_tensor([self.batch_size, self.seq_length],self.num_labels   )
									SCREAMING_SNAKE_CASE_  :		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_  :		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     __UpperCamelCase       (   self				:	List[str],_A				:	Optional[int],_A				:	int,_A				:	Dict,_A				:	Tuple,_A				:	str,*_A				:	int   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   TFOpenAIGPTModel(config=_A   )
							SCREAMING_SNAKE_CASE_  :		int    				=   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							SCREAMING_SNAKE_CASE_  :		Dict    				=   model(_A   )

							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   [input_ids, input_mask]
							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   model(_A   )

							SCREAMING_SNAKE_CASE_  :		List[str]    				=   model(_A   )

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

					def     __UpperCamelCase       (   self				:	Optional[int],_A				:	Tuple,_A				:	Optional[Any],_A				:	List[Any],_A				:	str,_A				:	Tuple,*_A				:	int   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   TFOpenAIGPTLMHeadModel(config=_A   )
							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   model(_A   )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size)   )

					def     __UpperCamelCase       (   self				:	Optional[Any],_A				:	List[str],_A				:	Tuple,_A				:	str,_A				:	Optional[Any],_A				:	Dict,_A				:	Union[str, Any],*_A				:	Dict   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]    				=   TFOpenAIGPTDoubleHeadsModel(config=_A   )

							SCREAMING_SNAKE_CASE_  :		List[Any]    				=   tf.tile(tf.expand_dims(_A,1   ),(1, self.num_choices, 1)   )
							SCREAMING_SNAKE_CASE_  :		List[Any]    				=   tf.tile(tf.expand_dims(_A,1   ),(1, self.num_choices, 1)   )
							SCREAMING_SNAKE_CASE_  :		Any    				=   tf.tile(tf.expand_dims(_A,1   ),(1, self.num_choices, 1)   )

							SCREAMING_SNAKE_CASE_  :		List[str]    				=   {
							    "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_  :		Tuple    				=   model(_A   )
							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     __UpperCamelCase       (   self				:	Dict,_A				:	Any,_A				:	Union[str, Any],_A				:	Optional[int],_A				:	Union[str, Any],_A				:	Optional[int],*_A				:	Optional[int]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   self.num_labels
							SCREAMING_SNAKE_CASE_  :		List[str]    				=   ids_tensor([self.batch_size],self.type_sequence_label_size   )
							SCREAMING_SNAKE_CASE_  :		Tuple    				=   {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							    "labels": sequence_labels,
							}
							SCREAMING_SNAKE_CASE_  :		List[Any]    				=   TFOpenAIGPTForSequenceClassification(_A   )
							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   model(_A   )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)   )






					def     __UpperCamelCase       (   self				:	Tuple   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]    				=   self.prepare_config_and_inputs()

							(
							    SCREAMING_SNAKE_CASE_  
							)  :		Union[str, Any]    				=   config_and_inputs

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







@require_tf
class 		a__						( snake_case__   ,		snake_case__   ,		unittest.TestCase ):
					A     	=							(
					    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
					    if is_tf_available()
					    else ()
					)
					A     	=							(
					    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					A     	=							(
					    {
					        "feature-extraction": TFOpenAIGPTModel,
					        "text-classification": TFOpenAIGPTForSequenceClassification,
					        "text-generation": TFOpenAIGPTLMHeadModel,
					        "zero-shot": TFOpenAIGPTForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					A     	=							False
					A     	=							False

					def     __UpperCamelCase       (   self				:	Any,_A				:	str,_A				:	str,_A				:	Tuple,_A				:	Dict,_A				:	Any   ):







							"""simple docstring"""






							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     __UpperCamelCase       (   self				:	Any   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]    				=   TFOpenAIGPTModelTester(self   )
							SCREAMING_SNAKE_CASE_  :		List[str]    				=   ConfigTester(self,config_class=_A,n_embd=37   )

					def     __UpperCamelCase       (   self				:	Union[str, Any]   ):







							"""simple docstring"""






							self.config_tester.run_common_tests()

					def     __UpperCamelCase       (   self				:	str   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*_A   )

					def     __UpperCamelCase       (   self				:	Any   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_lm_head(*_A   )

					def     __UpperCamelCase       (   self				:	Tuple   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int    				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_double_head(*_A   )

					def     __UpperCamelCase       (   self				:	Dict   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int    				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_A   )






					@slow
					def     __UpperCamelCase       (   self				:	Dict   ):







							"""simple docstring"""






							for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE_  :		int    				=   TFOpenAIGPTModel.from_pretrained(_A   )
									self.assertIsNotNone(_A   )







@require_tf
class 		a__						( unittest.TestCase ):

					@slow
					def     __UpperCamelCase       (   self				:	Optional[Any]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"   )
							SCREAMING_SNAKE_CASE_  :		List[str]    				=   tf.convert_to_tensor([[481, 4735, 544]],dtype=tf.intaa   )  # the president is
							SCREAMING_SNAKE_CASE_  :		int    				=   [
							    481,
							    4735,
							    544,
							    246,
							    963,
							    870,
							    762,
							    239,
							    244,
							    4_0477,
							    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_  :		Optional[int]    				=   model.generate(_A,do_sample=_A   )
							self.assertListEqual(output_ids[0].numpy().tolist(),_A   )

from ..utils import deprecate
from .attention_processor import (  # noqa: F401
    Attention,
    AttentionProcessor,
    AttnAddedKVProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRALinearLayer,
    LoRAXFormersAttnProcessor,
    SlicedAttnAddedKVProcessor,
    SlicedAttnProcessor,
    XFormersAttnProcessor,
)
from .attention_processor import AttnProcessor as AttnProcessorRename  # noqa: F401


deprecate(
    '''cross_attention''',
    '''0.20.0''',
    '''Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.''',
    standard_warn=False,
)


__lowerCamelCase :     str          =  AttentionProcessor






class 		a__						( A__ ):

					def __init__(   self				:	Tuple,*_A				:	Union[str, Any],**_A				:	Any   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	str,*_A				:	Optional[Any],**_A				:	List[str]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	str,*_A				:	Dict,**_A				:	Optional[int]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	List[Any],*_A				:	List[str],**_A				:	str   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Tuple    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	Optional[int],*_A				:	Any,**_A				:	int   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	Union[str, Any],*_A				:	Union[str, Any],**_A				:	Any   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	Dict,*_A				:	Dict,**_A				:	Optional[Any]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )







class 		a__						( A__ ):

					def __init__(   self				:	int,*_A				:	Dict,**_A				:	Union[str, Any]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		str    				=   F'{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"   )   )} instead.'
							deprecate("cross_attention","0.20.0",_A,standard_warn=_A   )
							super().__init__(*_A,**_A   )
