
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       :




 def __init__(  self		,			__UpperCAmelCase		,			):



    '''simple docstring'''
    __lowerCamelCase  							=      parent
    __lowerCamelCase  							=      13
    __lowerCamelCase  							=      7
    __lowerCamelCase  							=      True
    __lowerCamelCase  							=      True
    __lowerCamelCase  							=      True
    __lowerCamelCase  							=      True
    __lowerCamelCase  							=      True
    __lowerCamelCase  							=      99
    __lowerCamelCase  							=      32
    __lowerCamelCase  							=      2
    __lowerCamelCase  							=      4
    __lowerCamelCase  							=      37
    __lowerCamelCase  							=      '''gelu'''
    __lowerCamelCase  							=      0.1
    __lowerCamelCase  							=      0.1
    __lowerCamelCase  							=      512
    __lowerCamelCase  							=      16
    __lowerCamelCase  							=      2
    __lowerCamelCase  							=      0.02
    __lowerCamelCase  							=      3
    __lowerCamelCase  							=      4
    __lowerCamelCase  							=      None
    __lowerCamelCase  							=      self.vocab_size - 1




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size			)

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

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

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

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

    __lowerCamelCase  							=      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  							=      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     lowerCamelCase (  self		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			*__UpperCAmelCase			):



    '''simple docstring'''
    __lowerCamelCase  							=      TFOpenAIGPTModel(config=__snake_case			)
    __lowerCamelCase  							=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowerCamelCase  							=      model(__snake_case			)

    __lowerCamelCase  							=      [input_ids, input_mask]
    __lowerCamelCase  							=      model(__snake_case			)

    __lowerCamelCase  							=      model(__snake_case			)

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




 def     lowerCamelCase (  self		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			*__UpperCAmelCase			):



    '''simple docstring'''
    __lowerCamelCase  							=      TFOpenAIGPTLMHeadModel(config=__snake_case			)
    __lowerCamelCase  							=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowerCamelCase  							=      model(__snake_case			)
    self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size)			)




 def     lowerCamelCase (  self		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			*__UpperCAmelCase			):



    '''simple docstring'''
    __lowerCamelCase  							=      TFOpenAIGPTDoubleHeadsModel(config=__snake_case			)

    __lowerCamelCase  							=      tf.tile(tf.expand_dims(__snake_case		,			1			)		,			(1, self.num_choices, 1)			)
    __lowerCamelCase  							=      tf.tile(tf.expand_dims(__snake_case		,			1			)		,			(1, self.num_choices, 1)			)
    __lowerCamelCase  							=      tf.tile(tf.expand_dims(__snake_case		,			1			)		,			(1, self.num_choices, 1)			)

    __lowerCamelCase  							=      {
        '''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  							=      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     lowerCamelCase (  self		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			*__UpperCAmelCase			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.num_labels
    __lowerCamelCase  							=      ids_tensor([self.batch_size]		,			self.type_sequence_label_size			)
    __lowerCamelCase  							=      {
        '''input_ids''': input_ids,
        '''attention_mask''': input_mask,
        '''token_type_ids''': token_type_ids,
        '''labels''': sequence_labels,
    }
    __lowerCamelCase  							=      TFOpenAIGPTForSequenceClassification(__snake_case			)
    __lowerCamelCase  							=      model(__snake_case			)
    self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_labels)			)




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.prepare_config_and_inputs()

    (
        __lowerCamelCase		
    )  							=      config_and_inputs

    __lowerCamelCase  							=      {'''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				):
 lowerCAmelCase__       =					(
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 lowerCAmelCase__       =					(
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 lowerCAmelCase__       =					(
     {
         """feature-extraction""": TFOpenAIGPTModel,
         """text-classification""": TFOpenAIGPTForSequenceClassification,
         """text-generation""": TFOpenAIGPTLMHeadModel,
         """zero-shot""": TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowerCAmelCase__       =					False
 lowerCAmelCase__       =					False




 def     lowerCamelCase (  self		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase			):



    '''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     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      TFOpenAIGPTModelTester(self			)
    __lowerCamelCase  							=      ConfigTester(self		,			config_class=__snake_case		,			n_embd=37			)




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    self.config_tester.run_common_tests()




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_model(*__snake_case			)




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_lm_head(*__snake_case			)




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_double_head(*__snake_case			)




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case			)




 @slow
 def     lowerCamelCase (  self			):



    '''simple docstring'''
    for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCamelCase  							=      TFOpenAIGPTModel.from_pretrained(__snake_case			)
       self.assertIsNotNone(__snake_case			)



@require_tf
class 	__lowerCAmelCase       (		unittest.TestCase				):




 @slow
 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''			)
    __lowerCamelCase  							=      tf.convert_to_tensor([[481, 4735, 544]]		,			dtype=tf.intaa			)  # the president is
    __lowerCamelCase  							=      [
        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

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











import platform
from argparse import ArgumentParser

import huggingface_hub
import pandas
import pyarrow

from datasets import __version__ as version
from datasets.commands import BaseDatasetsCLICommand





def 						a__     (						_UpperCamelCase     :	Optional[Any]							):
   return EnvironmentCommand()





class 	__lowerCAmelCase       (		lowerCAmelCase__				):




 @staticmethod
 def     lowerCamelCase (  __UpperCAmelCase			):



    '''simple docstring'''
    __lowerCamelCase  							=      parser.add_parser('''env'''		,			help='''Print relevant system environment info.'''			)
    download_parser.set_defaults(func=__UpperCAmelCase			)




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      {
        '''`datasets` version''': version,
        '''Platform''': platform.platform(),
        '''Python version''': platform.python_version(),
        '''Huggingface_hub version''': huggingface_hub.__version__,
        '''PyArrow version''': pyarrow.__version__,
        '''Pandas version''': pandas.__version__,
    }

    print('''\nCopy-and-paste the text below in your GitHub issue.\n'''			)
    print(self.format_dict(__UpperCAmelCase			)			)

    return info




 @staticmethod
 def     lowerCamelCase (  __UpperCAmelCase			):



    '''simple docstring'''
    return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]			) + "\n"






