






"""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      UpperCamelCase_       :



  def __init__(					self       ,						snake_case__       ,						)	->	Tuple:



         """simple docstring"""





         UpperCAmelCase					=	parent
         UpperCAmelCase					=	13
         UpperCAmelCase					=	7
         UpperCAmelCase					=	True
         UpperCAmelCase					=	True
         UpperCAmelCase					=	True
         UpperCAmelCase					=	True
         UpperCAmelCase					=	True
         UpperCAmelCase					=	99
         UpperCAmelCase					=	32
         UpperCAmelCase					=	2
         UpperCAmelCase					=	4
         UpperCAmelCase					=	37
         UpperCAmelCase					=	"""gelu"""
         UpperCAmelCase					=	0.1
         UpperCAmelCase					=	0.1
         UpperCAmelCase					=	5_12
         UpperCAmelCase					=	16
         UpperCAmelCase					=	2
         UpperCAmelCase					=	0.02
         UpperCAmelCase					=	3
         UpperCAmelCase					=	4
         UpperCAmelCase					=	None
         UpperCAmelCase					=	self.vocab_size - 1



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

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

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

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

         UpperCAmelCase					=	None
         UpperCAmelCase					=	None
         UpperCAmelCase					=	None
         if self.use_labels:
                UpperCAmelCase					=	ids_tensor([self.batch_size]       ,						self.type_sequence_label_size )
                UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.num_labels )
                UpperCAmelCase					=	ids_tensor([self.batch_size]       ,						self.num_choices )

         UpperCAmelCase					=	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					=	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__ )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTModel(config=snake_case__ )
         UpperCAmelCase					=	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         UpperCAmelCase					=	model(snake_case__ )

         UpperCAmelCase					=	[input_ids, input_mask]
         UpperCAmelCase					=	model(snake_case__ )

         UpperCAmelCase					=	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__ )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTLMHeadModel(config=snake_case__ )
         UpperCAmelCase					=	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         UpperCAmelCase					=	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__ )	->	int:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTDoubleHeadsModel(config=snake_case__ )

         UpperCAmelCase					=	tf.tile(tf.expand_dims(snake_case__       ,						1 )       ,						(1, self.num_choices, 1) )
         UpperCAmelCase					=	tf.tile(tf.expand_dims(snake_case__       ,						1 )       ,						(1, self.num_choices, 1) )
         UpperCAmelCase					=	tf.tile(tf.expand_dims(snake_case__       ,						1 )       ,						(1, self.num_choices, 1) )

         UpperCAmelCase					=	{
             """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					=	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__ )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	self.num_labels
         UpperCAmelCase					=	ids_tensor([self.batch_size]       ,						self.type_sequence_label_size )
         UpperCAmelCase					=	{
             """input_ids""": input_ids,
             """attention_mask""": input_mask,
             """token_type_ids""": token_type_ids,
             """labels""": sequence_labels,
         }
         UpperCAmelCase					=	TFOpenAIGPTForSequenceClassification(snake_case__ )
         UpperCAmelCase					=	model(snake_case__ )
         self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels) )



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	self.prepare_config_and_inputs()

         (
             (
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				(
             UpperCAmelCase
         )			,				
         )					=	config_and_inputs

         UpperCAmelCase					=	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
         return config, inputs_dict





@require_tf
class      UpperCamelCase_       (				a_      ,    a_      ,    unittest.TestCase     ):
  _A				:    List[str] =		(
      (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
      if is_tf_available()
      else ()
  )
  _A				:    Dict =		(
      (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  _A				:    List[str] =		(
      {
          'feature-extraction': TFOpenAIGPTModel,
          'text-classification': TFOpenAIGPTForSequenceClassification,
          'text-generation': TFOpenAIGPTLMHeadModel,
          'zero-shot': TFOpenAIGPTForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  _A				:    Dict =		False
  _A				:    Tuple =		False



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Dict:



         """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 )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTModelTester(self )
         UpperCAmelCase					=	ConfigTester(self       ,						config_class=snake_case__       ,						n_embd=37 )



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         self.config_tester.run_common_tests()



  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_double_head(*snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case__ )



  @slow
  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase					=	TFOpenAIGPTModel.from_pretrained(snake_case__ )
                self.assertIsNotNone(snake_case__ )





@require_tf
class      UpperCamelCase_       (				unittest.TestCase     ):



  @slow
  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt""" )
         UpperCAmelCase					=	tf.convert_to_tensor([[4_81, 47_35, 5_44]]       ,						dtype=tf.intaa )  # the president is
         UpperCAmelCase					=	[
             4_81,
             47_35,
             5_44,
             2_46,
             9_63,
             8_70,
             7_62,
             2_39,
             2_44,
             4_04_77,
             2_44,
             2_49,
             7_19,
             8_81,
             4_87,
             5_44,
             2_40,
             2_44,
             6_03,
             4_81,
         ]  # the president is a very good man. " \n " i\'m sure he is, " said the

         UpperCAmelCase					=	model.generate(snake_case__       ,						do_sample=snake_case__ )
         self.assertListEqual(output_ids[0].numpy().tolist()       ,						snake_case__ )








"""simple docstring"""







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


lowerCAmelCase_		:			str				   =					logging.get_logger(__name__)

lowerCAmelCase_		:			Optional[int]				   =					{
    '''google/reformer-crime-and-punishment''': (
        '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/config.json'''
    ),
    '''google/reformer-enwik8''': '''https://huggingface.co/google/reformer-enwik8/resolve/main/config.json''',
}





class      UpperCamelCase_       (				a_     ):
  _A				:    int =		'reformer'
  _A				:    Any =		['past_buckets_states']
  _A				:    Optional[Any] =		{}



  def __init__(					self       ,						snake_case__=64       ,						snake_case__=["local", "lsh", "local", "lsh", "local", "lsh"]       ,						snake_case__=1.0       ,						snake_case__=True       ,						snake_case__=[64, 64]       ,						snake_case__=[64, 1_92]       ,						snake_case__=0       ,						snake_case__=2       ,						snake_case__=5_12       ,						snake_case__=None       ,						snake_case__="relu"       ,						snake_case__=0.05       ,						snake_case__=2_56       ,						snake_case__=0.02       ,						snake_case__=False       ,						snake_case__=1e-12       ,						snake_case__=1       ,						snake_case__=0       ,						snake_case__=0.05       ,						snake_case__=64       ,						snake_case__=64       ,						snake_case__=0.0       ,						snake_case__=1       ,						snake_case__=0       ,						snake_case__=40_96       ,						snake_case__=12       ,						snake_case__=None       ,						snake_case__=1       ,						snake_case__=0       ,						snake_case__=3_20       ,						snake_case__=False       ,						snake_case__=True       ,						snake_case__=None       ,						**snake_case__       ,						)	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	hash_seed
         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	attention_head_size
         UpperCAmelCase					=	hidden_size
         UpperCAmelCase					=	num_attention_heads
         UpperCAmelCase					=	num_hashes
         UpperCAmelCase					=	len(snake_case__ )
         UpperCAmelCase					=	tuple(snake_case__ ) if isinstance(snake_case__       ,						snake_case__ ) else num_buckets
         UpperCAmelCase					=	lsh_attn_chunk_length
         UpperCAmelCase					=	local_attn_chunk_length
         UpperCAmelCase					=	lsh_num_chunks_after
         UpperCAmelCase					=	lsh_num_chunks_before
         UpperCAmelCase					=	local_num_chunks_after
         UpperCAmelCase					=	local_num_chunks_before
         UpperCAmelCase					=	hidden_act
         UpperCAmelCase					=	feed_forward_size
         UpperCAmelCase					=	hidden_dropout_prob
         UpperCAmelCase					=	lsh_attention_probs_dropout_prob
         UpperCAmelCase					=	local_attention_probs_dropout_prob
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	initializer_range
         UpperCAmelCase					=	layer_norm_eps
         UpperCAmelCase					=	axial_pos_embds
         UpperCAmelCase					=	tuple(snake_case__ )
         UpperCAmelCase					=	tuple(snake_case__ )
         UpperCAmelCase					=	axial_norm_std
         UpperCAmelCase					=	chunk_size_lm_head
         UpperCAmelCase					=	attn_layers
         UpperCAmelCase					=	use_cache
         UpperCAmelCase					=	classifier_dropout
         super().__init__(
             pad_token_id=snake_case__       ,						eos_token_id=snake_case__       ,						is_decoder=snake_case__       ,						tie_word_embeddings=snake_case__       ,						**snake_case__       ,						)
