






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


      def __init__(     self					:						Any				, a					:						Optional[Any]				, ):





            """simple docstring"""







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


      def _UpperCamelCase    (     self					:						List[Any]    ):





            """simple docstring"""







            __snake_case   :     List[Any]	      =ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size    )

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

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

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

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

            __snake_case   :     str	      =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				, )

            __snake_case   :     List[str]	      =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					:						Union[str, Any]				, a					:						str				, a					:						Tuple				, a					:						List[Any]				, a					:						Dict				, a					:						Any				, *a					:						Any    ):





            """simple docstring"""







            __snake_case   :     Dict	      =TFOpenAIGPTModel(config=_a    )
            __snake_case   :     List[str]	      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            __snake_case   :     Union[str, Any]	      =model(_a    )

            __snake_case   :     Optional[int]	      =[input_ids, input_mask]
            __snake_case   :     str	      =model(_a    )

            __snake_case   :     Dict	      =model(_a    )

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


      def _UpperCamelCase    (     self					:						Any				, a					:						Any				, a					:						Dict				, a					:						int				, a					:						List[str]				, a					:						str				, *a					:						Dict    ):





            """simple docstring"""







            __snake_case   :     int	      =TFOpenAIGPTLMHeadModel(config=_a    )
            __snake_case   :     Any	      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            __snake_case   :     str	      =model(_a    )
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size)    )


      def _UpperCamelCase    (     self					:						Dict				, a					:						List[Any]				, a					:						Dict				, a					:						Any				, a					:						Any				, a					:						Tuple				, a					:						Any				, *a					:						Dict    ):





            """simple docstring"""







            __snake_case   :     List[str]	      =TFOpenAIGPTDoubleHeadsModel(config=_a    )

            __snake_case   :     Any	      =tf.tile(tf.expand_dims(_a				, 1    )				, (1, self.num_choices, 1)    )
            __snake_case   :     Optional[Any]	      =tf.tile(tf.expand_dims(_a				, 1    )				, (1, self.num_choices, 1)    )
            __snake_case   :     str	      =tf.tile(tf.expand_dims(_a				, 1    )				, (1, self.num_choices, 1)    )

            __snake_case   :     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,
            }
            __snake_case   :     Optional[int]	      =model(_a    )
            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					:						Any				, a					:						Optional[Any]				, a					:						List[Any]				, a					:						List[Any]				, a					:						Optional[int]				, a					:						Optional[Any]				, *a					:						str    ):





            """simple docstring"""







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







      def _UpperCamelCase    (     self					:						Optional[int]    ):





            """simple docstring"""







            __snake_case   :     Optional[Any]	      =self.prepare_config_and_inputs()

            (
                __snake_case
            )   :     Tuple	      =config_and_inputs

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



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


      def _UpperCamelCase    (     self					:						str				, a					:						List[Any]				, a					:						Tuple				, a					:						Dict				, a					:						Union[str, Any]				, a					:						Any    ):





            """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					:						Dict    ):





            """simple docstring"""







            __snake_case   :     Optional[Any]	      =TFOpenAIGPTModelTester(self    )
            __snake_case   :     List[str]	      =ConfigTester(self				, config_class=_a				, n_embd=3_7    )


      def _UpperCamelCase    (     self					:						str    ):





            """simple docstring"""







            self.config_tester.run_common_tests()


      def _UpperCamelCase    (     self					:						Optional[Any]    ):





            """simple docstring"""







            __snake_case   :     Optional[int]	      =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*_a    )


      def _UpperCamelCase    (     self					:						str    ):





            """simple docstring"""







            __snake_case   :     Optional[Any]	      =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_lm_head(*_a    )


      def _UpperCamelCase    (     self					:						Any    ):





            """simple docstring"""







            __snake_case   :     Any	      =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_double_head(*_a    )


      def _UpperCamelCase    (     self					:						List[str]    ):





            """simple docstring"""







            __snake_case   :     List[Any]	      =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_a    )







      @slow
      def _UpperCamelCase    (     self					:						Union[str, Any]    ):





            """simple docstring"""







            for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  __snake_case   :     Optional[Any]	      =TFOpenAIGPTModel.from_pretrained(_a    )
                  self.assertIsNotNone(_a    )



@require_tf
class   _lowercase			(  unittest.TestCase    ):


      @slow
      def _UpperCamelCase    (     self					:						Union[str, Any]    ):





            """simple docstring"""







            __snake_case   :     str	      =TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''    )
            __snake_case   :     List[str]	      =tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]				, dtype=tf.intaa    )  # the president is
            __snake_case   :     int	      =[
                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

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



'''simple docstring'''




import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
 from .testing_utils import (
     floats_tensor,
     load_hf_numpy,
     load_image,
     load_numpy,
     load_pt,
     nightly,
     parse_flag_from_env,
     print_tensor_test,
     require_torch_a,
     require_torch_gpu,
     skip_mps,
     slow,
     torch_all_close,
     torch_device,
 )
 from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


_A										= get_logger(__name__)

def _UpperCamelCase (							SCREAMING_SNAKE_CASE_				):
     if version.parse(SCREAMING_SNAKE_CASE_				) < version.parse(SCREAMING_SNAKE_CASE_				):
          if "dev" in min_version:
               lowercase_       :							Tuple  					=       (
                   'This example requires a source install from HuggingFace diffusers (see '
                   '`https://huggingface.co/docs/diffusers/installation#install-from-source`),'
               )
          else:
               lowercase_       :							List[Any]  					=       f'''This example requires a minimum version of {min_version},'''
          error_message += f''' but the version found is {__version__}.\n'''
          raise ImportError(SCREAMING_SNAKE_CASE_				)




