




'''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       lowerCamelCase_					:
  """simple docstring"""




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




  def 		_lowercase  (				self   :			Union[str, Any]     )      ->      Any:
    __lowerCamelCase : Tuple					 =				ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size     )

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

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

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

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

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

    __lowerCamelCase : 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 		_lowercase  (				self   :			List[Any]    ,			_a   :			int    ,			_a   :			Union[str, Any]    ,			_a   :			Any    ,			_a   :			Optional[int]    ,			_a   :			List[str]    ,			*_a   :			Any     )      ->      List[str]:
    __lowerCamelCase : List[Any]					 =				TFOpenAIGPTModel(config=_snake_case     )
    __lowerCamelCase : Union[str, Any]					 =				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowerCamelCase : List[str]					 =				model(_snake_case     )

    __lowerCamelCase : List[str]					 =				[input_ids, input_mask]
    __lowerCamelCase : Dict					 =				model(_snake_case     )

    __lowerCamelCase : Optional[Any]					 =				model(_snake_case     )

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




  def 		_lowercase  (				self   :			Dict    ,			_a   :			Tuple    ,			_a   :			Union[str, Any]    ,			_a   :			Optional[Any]    ,			_a   :			List[str]    ,			_a   :			Any    ,			*_a   :			Optional[int]     )      ->      Any:
    __lowerCamelCase : Optional[int]					 =				TFOpenAIGPTLMHeadModel(config=_snake_case     )
    __lowerCamelCase : Tuple					 =				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowerCamelCase : Union[str, Any]					 =				model(_snake_case     )
    self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size)     )




  def 		_lowercase  (				self   :			str    ,			_a   :			List[Any]    ,			_a   :			Union[str, Any]    ,			_a   :			Union[str, Any]    ,			_a   :			Any    ,			_a   :			int    ,			_a   :			Union[str, Any]    ,			*_a   :			Dict     )      ->      Optional[int]:
    __lowerCamelCase : List[Any]					 =				TFOpenAIGPTDoubleHeadsModel(config=_snake_case     )

    __lowerCamelCase : List[Any]					 =				tf.tile(tf.expand_dims(_snake_case    ,			1     )    ,			(1, self.num_choices, 1)     )
    __lowerCamelCase : str					 =				tf.tile(tf.expand_dims(_snake_case    ,			1     )    ,			(1, self.num_choices, 1)     )
    __lowerCamelCase : int					 =				tf.tile(tf.expand_dims(_snake_case    ,			1     )    ,			(1, self.num_choices, 1)     )

    __lowerCamelCase : 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,
    }
    __lowerCamelCase : Tuple					 =				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 		_lowercase  (				self   :			Tuple    ,			_a   :			Tuple    ,			_a   :			int    ,			_a   :			List[str]    ,			_a   :			Union[str, Any]    ,			_a   :			Any    ,			*_a   :			List[str]     )      ->      str:
    __lowerCamelCase : List[str]					 =				self.num_labels
    __lowerCamelCase : Dict					 =				ids_tensor([self.batch_size]    ,			self.type_sequence_label_size     )
    __lowerCamelCase : str					 =				{
        '''input_ids''': input_ids,
        '''attention_mask''': input_mask,
        '''token_type_ids''': token_type_ids,
        '''labels''': sequence_labels,
    }
    __lowerCamelCase : Dict					 =				TFOpenAIGPTForSequenceClassification(_snake_case     )
    __lowerCamelCase : str					 =				model(_snake_case     )
    self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels)     )




  def 		_lowercase  (				self   :			int     )      ->      Optional[int]:
    __lowerCamelCase : Tuple					 =				self.prepare_config_and_inputs()

    (
        __lowerCamelCase							
    ) : List[str]					 =				config_and_inputs

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






@require_tf
class       lowerCamelCase_					(       a_     ,     a_     ,     unittest.TestCase  ):
  """simple docstring"""




  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 		_lowercase  (				self   :			Dict    ,			_a   :			int    ,			_a   :			Union[str, Any]    ,			_a   :			str    ,			_a   :			int    ,			_a   :			Dict     )      ->      Optional[int]:
    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 		_lowercase  (				self   :			Any     )      ->      Optional[Any]:
    __lowerCamelCase : Dict					 =				TFOpenAIGPTModelTester(self     )
    __lowerCamelCase : Optional[int]					 =				ConfigTester(self    ,			config_class=_snake_case    ,			n_embd=37     )




  def 		_lowercase  (				self   :			str     )      ->      int:
    self.config_tester.run_common_tests()




  def 		_lowercase  (				self   :			int     )      ->      List[str]:
    __lowerCamelCase : int					 =				self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_model(*_snake_case     )




  def 		_lowercase  (				self   :			Optional[Any]     )      ->      List[Any]:
    __lowerCamelCase : Optional[int]					 =				self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_lm_head(*_snake_case     )




  def 		_lowercase  (				self   :			int     )      ->      int:
    __lowerCamelCase : Dict					 =				self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_double_head(*_snake_case     )




  def 		_lowercase  (				self   :			Dict     )      ->      Optional[int]:
    __lowerCamelCase : Optional[int]					 =				self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_snake_case     )




  @slow
  def 		_lowercase  (				self   :			Optional[int]     )      ->      int:
    for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __lowerCamelCase : int					 =				TFOpenAIGPTModel.from_pretrained(_snake_case     )
      self.assertIsNotNone(_snake_case     )






@require_tf
class       lowerCamelCase_					(       unittest.TestCase  ):
  """simple docstring"""




  @slow
  def 		_lowercase  (				self   :			str     )      ->      List[Any]:
    __lowerCamelCase : Union[str, Any]					 =				TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
    __lowerCamelCase : Optional[Any]					 =				tf.convert_to_tensor([[481, 4735, 544]]    ,			dtype=tf.intaa     )  # the president is
    __lowerCamelCase : Union[str, Any]					 =				[
        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

    __lowerCamelCase : Union[str, Any]					 =				model.generate(_snake_case    ,			do_sample=_snake_case     )
    self.assertListEqual(output_ids[0].numpy().tolist()    ,			_snake_case     )











'''simple docstring'''





from typing import List, Union

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


_UpperCamelCase							       =		logging.get_logger(__name__)

_UpperCamelCase							       =		{
    'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/config.json',
    # See all LED models at https://huggingface.co/models?filter=led
}






class       lowerCamelCase_					(       SCREAMING_SNAKE_CASE_  ):
  """simple docstring"""




  a_							      ="""led"""
  a_							      ={
      """num_attention_heads""": """encoder_attention_heads""",
      """hidden_size""": """d_model""",
      """attention_probs_dropout_prob""": """attention_dropout""",
      """initializer_range""": """init_std""",
  }




  def __init__(				self   :			str    ,			_a   :			Union[str, Any]=5_0265    ,			_a   :			Any=1_6384    ,			_a   :			List[Any]=1024    ,			_a   :			List[Any]=12    ,			_a   :			Any=4096    ,			_a   :			int=16    ,			_a   :			int=12    ,			_a   :			Optional[int]=4096    ,			_a   :			Tuple=16    ,			_a   :			Optional[Any]=0.0    ,			_a   :			Tuple=0.0    ,			_a   :			List[Any]=True    ,			_a   :			Optional[Any]=True    ,			_a   :			Any="gelu"    ,			_a   :			List[Any]=1024    ,			_a   :			List[str]=0.1    ,			_a   :			Dict=0.0    ,			_a   :			Tuple=0.0    ,			_a   :			Any=0.02    ,			_a   :			Optional[int]=2    ,			_a   :			Optional[int]=0.0    ,			_a   :			int=1    ,			_a   :			List[Any]=0    ,			_a   :			str=2    ,			_a   :			Union[List[int], int] = 512    ,			**_a   :			Optional[int]    ,			)      ->      Tuple:
    __lowerCamelCase : Optional[int]					 =				vocab_size
    __lowerCamelCase : List[str]					 =				max_encoder_position_embeddings
    __lowerCamelCase : Any					 =				max_decoder_position_embeddings
    __lowerCamelCase : Optional[Any]					 =				d_model
    __lowerCamelCase : str					 =				encoder_ffn_dim
    __lowerCamelCase : List[Any]					 =				encoder_layers
    __lowerCamelCase : Union[str, Any]					 =				encoder_attention_heads
    __lowerCamelCase : Optional[Any]					 =				decoder_ffn_dim
    __lowerCamelCase : Union[str, Any]					 =				decoder_layers
    __lowerCamelCase : Optional[Any]					 =				decoder_attention_heads
    __lowerCamelCase : Optional[Any]					 =				dropout
    __lowerCamelCase : List[Any]					 =				attention_dropout
    __lowerCamelCase : Optional[int]					 =				activation_dropout
    __lowerCamelCase : Optional[int]					 =				activation_function
    __lowerCamelCase : Union[str, Any]					 =				init_std
    __lowerCamelCase : str					 =				encoder_layerdrop
    __lowerCamelCase : Tuple					 =				decoder_layerdrop
    __lowerCamelCase : Dict					 =				classifier_dropout
    __lowerCamelCase : Any					 =				use_cache
    __lowerCamelCase : Union[str, Any]					 =				encoder_layers
    __lowerCamelCase : Any					 =				attention_window

    super().__init__(
        pad_token_id=_a    ,			bos_token_id=_a    ,			eos_token_id=_a    ,			is_encoder_decoder=_a    ,			decoder_start_token_id=_a    ,			**_a    ,			)






