'''simple docstring'''


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







  '''simple docstring'''






  def __init__(					self ,						snake_case_ ,						):




       '''simple docstring'''

       UpperCAmelCase_				:  Union[str, Any]  = parent
       UpperCAmelCase_				:  Union[str, Any]  = 1_3
       UpperCAmelCase_				:  Dict  = 7
       UpperCAmelCase_				:  Dict  = True
       UpperCAmelCase_				:  int  = True
       UpperCAmelCase_				:  Any  = True
       UpperCAmelCase_				:  Optional[int]  = True
       UpperCAmelCase_				:  Dict  = True
       UpperCAmelCase_				:  Optional[int]  = 9_9
       UpperCAmelCase_				:  Union[str, Any]  = 3_2
       UpperCAmelCase_				:  int  = 2
       UpperCAmelCase_				:  Union[str, Any]  = 4
       UpperCAmelCase_				:  Union[str, Any]  = 3_7
       UpperCAmelCase_				:  Union[str, Any]  = 'gelu'
       UpperCAmelCase_				:  str  = 0.1
       UpperCAmelCase_				:  List[str]  = 0.1
       UpperCAmelCase_				:  int  = 5_1_2
       UpperCAmelCase_				:  Union[str, Any]  = 1_6
       UpperCAmelCase_				:  Dict  = 2
       UpperCAmelCase_				:  List[str]  = 0.02
       UpperCAmelCase_				:  List[Any]  = 3
       UpperCAmelCase_				:  int  = 4
       UpperCAmelCase_				:  Union[str, Any]  = None
       UpperCAmelCase_				:  str  = self.vocab_size - 1


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  str  = ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size		)

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

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

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

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

       UpperCAmelCase_				:  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 ,						)

       UpperCAmelCase_				:  Tuple  = 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 ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						*snake_case_		):




       '''simple docstring'''

       UpperCAmelCase_				:  int  = TFOpenAIGPTModel(config=snake_case_		)
       UpperCAmelCase_				:  Optional[int]  = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
       UpperCAmelCase_				:  Tuple  = model(snake_case_		)

       UpperCAmelCase_				:  Any  = [input_ids, input_mask]
       UpperCAmelCase_				:  str  = model(snake_case_		)

       UpperCAmelCase_				:  Tuple  = model(snake_case_		)

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


  def _UpperCamelCase	(					self ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						*snake_case_		):




       '''simple docstring'''

       UpperCAmelCase_				:  Union[str, Any]  = TFOpenAIGPTLMHeadModel(config=snake_case_		)
       UpperCAmelCase_				:  List[Any]  = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
       UpperCAmelCase_				:  Any  = model(snake_case_		)
       self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size)		)


  def _UpperCamelCase	(					self ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						*snake_case_		):




       '''simple docstring'''

       UpperCAmelCase_				:  int  = TFOpenAIGPTDoubleHeadsModel(config=snake_case_		)

       UpperCAmelCase_				:  Any  = tf.tile(tf.expand_dims(snake_case_ ,						1		) ,						(1, self.num_choices, 1)		)
       UpperCAmelCase_				:  Dict  = tf.tile(tf.expand_dims(snake_case_ ,						1		) ,						(1, self.num_choices, 1)		)
       UpperCAmelCase_				:  Tuple  = tf.tile(tf.expand_dims(snake_case_ ,						1		) ,						(1, self.num_choices, 1)		)

       UpperCAmelCase_				:  Any  = {
           '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,
       }
       UpperCAmelCase_				:  Union[str, Any]  = model(snake_case_		)
       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 ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						*snake_case_		):




       '''simple docstring'''

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






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Union[str, Any]  = self.prepare_config_and_inputs()

       (
           (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     (
           UpperCAmelCase_
       )  ,     
       )				:  int  = config_and_inputs

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







@require_tf
class 	__SCREAMING_SNAKE_CASE		(			lowerCamelCase_		,      lowerCamelCase_		,      unittest.TestCase			):







  '''simple docstring'''






  lowerCamelCase_						:Optional[int]		       =						(
      (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
      if is_tf_available()
      else ()
  )
  lowerCamelCase_						:Dict		       =						(
      (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  lowerCamelCase_						:Any		       =						(
      {
          '''feature-extraction''': TFOpenAIGPTModel,
          '''text-classification''': TFOpenAIGPTForSequenceClassification,
          '''text-generation''': TFOpenAIGPTLMHeadModel,
          '''zero-shot''': TFOpenAIGPTForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  lowerCamelCase_						:List[Any]		       =						False
  lowerCamelCase_						:Optional[Any]		       =						False


  def _UpperCamelCase	(					self ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_ ,						snake_case_		):




       '''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		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[int]  = TFOpenAIGPTModelTester(self		)
       UpperCAmelCase_				:  Any  = ConfigTester(self ,						config_class=snake_case_ ,						n_embd=3_7		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       self.config_tester.run_common_tests()


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*snake_case_		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case_		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[str]  = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_double_head(*snake_case_		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  str  = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case_		)






  @slow
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_				:  List[str]  = TFOpenAIGPTModel.from_pretrained(snake_case_		)
            self.assertIsNotNone(snake_case_		)







@require_tf
class 	__SCREAMING_SNAKE_CASE		(			unittest.TestCase			):







  '''simple docstring'''






  @slow
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  int  = TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'		)
       UpperCAmelCase_				:  List[str]  = tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]] ,						dtype=tf.intaa		)  # the president is
       UpperCAmelCase_				:  List[Any]  = [
           4_8_1,
           4_7_3_5,
           5_4_4,
           2_4_6,
           9_6_3,
           8_7_0,
           7_6_2,
           2_3_9,
           2_4_4,
           4_0_4_7_7,
           2_4_4,
           2_4_9,
           7_1_9,
           8_8_1,
           4_8_7,
           5_4_4,
           2_4_0,
           2_4_4,
           6_0_3,
           4_8_1,
       ]  # the president is a very good man. " \n " i\'m sure he is, " said the

       UpperCAmelCase_				:  Tuple  = model.generate(snake_case_ ,						do_sample=snake_case_		)
       self.assertListEqual(output_ids[0].numpy().tolist() ,						snake_case_		)










'''simple docstring'''


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__     						=  AttentionProcessor






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Union[str, Any]						,	*lowerCamelCase__	:  int						,	**lowerCamelCase__	:  List[str]			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							Dict             =   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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Optional[Any]						,	*lowerCamelCase__	:  Any						,	**lowerCamelCase__	:  Tuple			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  List[Any]						,	*lowerCamelCase__	:  List[Any]						,	**lowerCamelCase__	:  List[str]			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  int						,	*lowerCamelCase__	:  Tuple						,	**lowerCamelCase__	:  Union[str, Any]			) ->int:



					'''simple docstring'''







					_UpperCAmelCase		:							Dict             =   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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  int						,	*lowerCamelCase__	:  List[str]						,	**lowerCamelCase__	:  Tuple			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  List[Any]						,	*lowerCamelCase__	:  int						,	**lowerCamelCase__	:  Optional[int]			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Any						,	*lowerCamelCase__	:  Optional[Any]						,	**lowerCamelCase__	:  Union[str, Any]			) ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Optional[Any]						,	*lowerCamelCase__	:  List[Any]						,	**lowerCamelCase__	:  Dict			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   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"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)



