code: 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 				_lowercase						:
  
  
  
  
  
  
      '''simple docstring'''
  
  
  
  
      def __init__(						self					,	lowerCamelCase__					,	):
         lowerCAmelCase_:       List[Any]         =  parent
         lowerCAmelCase_:       Dict         =  13
         lowerCAmelCase_:       Dict         =  7
         lowerCAmelCase_:       Dict         =  True
         lowerCAmelCase_:       List[Any]         =  True
         lowerCAmelCase_:       str         =  True
         lowerCAmelCase_:       Union[str, Any]         =  True
         lowerCAmelCase_:       Optional[Any]         =  True
         lowerCAmelCase_:       Union[str, Any]         =  99
         lowerCAmelCase_:       Any         =  32
         lowerCAmelCase_:       Union[str, Any]         =  2
         lowerCAmelCase_:       Union[str, Any]         =  4
         lowerCAmelCase_:       str         =  37
         lowerCAmelCase_:       int         =  """gelu"""
         lowerCAmelCase_:       Optional[Any]         =  0.1
         lowerCAmelCase_:       List[Any]         =  0.1
         lowerCAmelCase_:       List[str]         =  512
         lowerCAmelCase_:       Optional[Any]         =  16
         lowerCAmelCase_:       str         =  2
         lowerCAmelCase_:       List[str]         =  0.0_2
         lowerCAmelCase_:       Any         =  3
         lowerCAmelCase_:       str         =  4
         lowerCAmelCase_:       Any         =  None
         lowerCAmelCase_:       Any         =  self.vocab_size - 1
  
  
  
  
      def     _a						(						self   ):
         lowerCAmelCase_:       List[str]         =  ids_tensor([self.batch_size, self.seq_length]					,	self.vocab_size   )
  
         lowerCAmelCase_:       Tuple         =  None
         if self.use_input_mask:
            lowerCAmelCase_:       List[str]         =  random_attention_mask([self.batch_size, self.seq_length]   )
  
         lowerCAmelCase_:       Optional[int]         =  None
         if self.use_token_type_ids:
            lowerCAmelCase_:       int         =  ids_tensor([self.batch_size, self.seq_length]					,	self.type_vocab_size   )
  
         lowerCAmelCase_:       int         =  None
         if self.use_mc_token_ids:
            lowerCAmelCase_:       int         =  ids_tensor([self.batch_size, self.num_choices]					,	self.seq_length   )
  
         lowerCAmelCase_:       str         =  None
         lowerCAmelCase_:       Dict         =  None
         lowerCAmelCase_:       Optional[Any]         =  None
         if self.use_labels:
            lowerCAmelCase_:       Union[str, Any]         =  ids_tensor([self.batch_size]					,	self.type_sequence_label_size   )
            lowerCAmelCase_:       Dict         =  ids_tensor([self.batch_size, self.seq_length]					,	self.num_labels   )
            lowerCAmelCase_:       str         =  ids_tensor([self.batch_size]					,	self.num_choices   )
  
         lowerCAmelCase_:       Any         =  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_:       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     _a						(						self					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	*lowerCamelCase__   ):
         lowerCAmelCase_:       Any         =  TFOpenAIGPTModel(config=_lowerCamelCase   )
         lowerCAmelCase_:       Any         =  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         lowerCAmelCase_:       Optional[int]         =  model(_lowerCamelCase   )
  
         lowerCAmelCase_:       List[str]         =  [input_ids, input_mask]
         lowerCAmelCase_:       Dict         =  model(_lowerCamelCase   )
  
         lowerCAmelCase_:       List[str]         =  model(_lowerCamelCase   )
  
         self.parent.assertEqual(result.last_hidden_state.shape					,	(self.batch_size, self.seq_length, self.hidden_size)   )
  
  
  
  
      def     _a						(						self					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	*lowerCamelCase__   ):
         lowerCAmelCase_:       Union[str, Any]         =  TFOpenAIGPTLMHeadModel(config=_lowerCamelCase   )
         lowerCAmelCase_:       Tuple         =  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         lowerCAmelCase_:       str         =  model(_lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.vocab_size)   )
  
  
  
  
      def     _a						(						self					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	*lowerCamelCase__   ):
         lowerCAmelCase_:       int         =  TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase   )
  
         lowerCAmelCase_:       Optional[Any]         =  tf.tile(tf.expand_dims(_lowerCamelCase					,	1   )					,	(1, self.num_choices, 1)   )
         lowerCAmelCase_:       Any         =  tf.tile(tf.expand_dims(_lowerCamelCase					,	1   )					,	(1, self.num_choices, 1)   )
         lowerCAmelCase_:       Any         =  tf.tile(tf.expand_dims(_lowerCamelCase					,	1   )					,	(1, self.num_choices, 1)   )
  
         lowerCAmelCase_:       List[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,
         }
         lowerCAmelCase_:       int         =  model(_lowerCamelCase   )
         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     _a						(						self					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	*lowerCamelCase__   ):
         lowerCAmelCase_:       List[Any]         =  self.num_labels
         lowerCAmelCase_:       int         =  ids_tensor([self.batch_size]					,	self.type_sequence_label_size   )
         lowerCAmelCase_:       Any         =  {
             """input_ids""": input_ids,
             """attention_mask""": input_mask,
             """token_type_ids""": token_type_ids,
             """labels""": sequence_labels,
         }
         lowerCAmelCase_:       Tuple         =  TFOpenAIGPTForSequenceClassification(_lowerCamelCase   )
         lowerCAmelCase_:       Union[str, Any]         =  model(_lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_labels)   )
  
  
  
  
      def     _a						(						self   ):
         lowerCAmelCase_:       int         =  self.prepare_config_and_inputs()
  
         (
             lowerCAmelCase_		
         ):       Optional[Any]         =  config_and_inputs
  
         lowerCAmelCase_:       Tuple         =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
         return config, inputs_dict
  
  
  
  
  
  @require_tf
  class 				_lowercase						(    lowerCAmelCase__   ,     lowerCAmelCase__   ,     unittest.TestCase ):
  
  
  
  
  
  
      '''simple docstring'''
      SCREAMING_SNAKE_CASE:							int        =						(
          (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
          if is_tf_available()
          else ()
      )
      SCREAMING_SNAKE_CASE:							Any        =						(
          (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      SCREAMING_SNAKE_CASE:							Optional[int]        =						(
          {
              'feature-extraction': TFOpenAIGPTModel,
              'text-classification': TFOpenAIGPTForSequenceClassification,
              'text-generation': TFOpenAIGPTLMHeadModel,
              'zero-shot': TFOpenAIGPTForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      SCREAMING_SNAKE_CASE:							Any        =						False
      SCREAMING_SNAKE_CASE:							Dict        =						False
  
  
  
  
      def     _a						(						self					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__   ):
  
         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     _a						(						self   ):
         lowerCAmelCase_:       List[str]         =  TFOpenAIGPTModelTester(self   )
         lowerCAmelCase_:       Optional[int]         =  ConfigTester(self					,	config_class=_lowerCamelCase					,	n_embd=37   )
  
  
  
  
      def     _a						(						self   ):
         self.config_tester.run_common_tests()
  
  
  
  
      def     _a						(						self   ):
         lowerCAmelCase_:       Tuple         =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase   )
  
  
  
  
      def     _a						(						self   ):
         lowerCAmelCase_:       Union[str, Any]         =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase   )
  
  
  
  
      def     _a						(						self   ):
         lowerCAmelCase_:       Dict         =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase   )
  
  
  
  
      def     _a						(						self   ):
         lowerCAmelCase_:       Optional[int]         =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase   )
  
  
  
  
      @slow
      def     _a						(						self   ):
  
         for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            lowerCAmelCase_:       int         =  TFOpenAIGPTModel.from_pretrained(_lowerCamelCase   )
            self.assertIsNotNone(_lowerCamelCase   )
  
  
  
  
  
  @require_tf
  class 				_lowercase						(    unittest.TestCase ):
  
  
  
  
  
  
      '''simple docstring'''
  
  
  
  
      @slow
      def     _a						(						self   ):
         lowerCAmelCase_:       int         =  TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"   )
         lowerCAmelCase_:       Optional[int]         =  tf.convert_to_tensor([[481, 4_735, 544]]					,	dtype=tf.intaa   )  # the president is
         lowerCAmelCase_:       Union[str, Any]         =  [
             481,
             4_735,
             544,
             246,
             963,
             870,
             762,
             239,
             244,
             40_477,
             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_:       Any         =  model.generate(_lowerCamelCase					,	do_sample=_lowerCamelCase   )
         self.assertListEqual(output_ids[0].numpy().tolist()					,	_lowerCamelCase   )
style_context: from ctypes import c_float, sizeof
  from enum import Enum
  from typing import TYPE_CHECKING, Optional, Union
  
  
  if TYPE_CHECKING:
   from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore
  
  class 				_lowercase						(    UpperCAmelCase__ ):
  
  
  
  
  
  
      '''simple docstring'''
      SCREAMING_SNAKE_CASE:							Union[str, Any]        =						c_float
  
  
  
  
      @property
      def     _a						(						self   ):
         return sizeof(self.value   )
  
  
  
  
  
  def 		snake_case__  (		lowercase       ,							lowercase       ,							lowercase = 0				):
     # < 0 is possible if using a dynamic axis
     if dimension <= 0:
        lowerCAmelCase_:       int         =  fixed_dimension
  
     dimension -= num_token_to_add
     return dimension
  def 		snake_case__  (		lowercase       ,							lowercase				):
     return num_parameters * dtype.size
  
  
  
  
  
  
  
  def 		snake_case__  (		lowercase				):
     from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore
  
     try:
        return AutoProcessor.from_pretrained(lowercase				)
     except (ValueError, OSError, KeyError):
        lowerCAmelCase_     ,  lowerCAmelCase_:       int         =  None, None
        try:
           lowerCAmelCase_:       Dict         =  AutoTokenizer.from_pretrained(lowercase				)
        except (OSError, KeyError):
           pass
        try:
           lowerCAmelCase_:       Optional[int]         =  AutoFeatureExtractor.from_pretrained(lowercase				)
        except (OSError, KeyError):
           pass
  
        if tokenizer is not None and feature_extractor is not None:
           raise ValueError(
               F'''Couldn\'t auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor.'''				)
        elif tokenizer is None and feature_extractor is None:
           return None
        elif tokenizer is not None:
           return tokenizer
        else:
           return feature_extractor