





'''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 					snake_case					:




 """simple docstring"""



 def __init__(	self	:							Optional[int]							,     __A	:							int							,     ):
        __UpperCamelCase	=  parent
        __UpperCamelCase	=  1_3
        __UpperCamelCase	=  7
        __UpperCamelCase	=  True
        __UpperCamelCase	=  True
        __UpperCamelCase	=  True
        __UpperCamelCase	=  True
        __UpperCamelCase	=  True
        __UpperCamelCase	=  9_9
        __UpperCamelCase	=  3_2
        __UpperCamelCase	=  2
        __UpperCamelCase	=  4
        __UpperCamelCase	=  3_7
        __UpperCamelCase	=  '''gelu'''
        __UpperCamelCase	=  0.1
        __UpperCamelCase	=  0.1
        __UpperCamelCase	=  5_1_2
        __UpperCamelCase	=  1_6
        __UpperCamelCase	=  2
        __UpperCamelCase	=  0.02
        __UpperCamelCase	=  3
        __UpperCamelCase	=  4
        __UpperCamelCase	=  None
        __UpperCamelCase	=  self.vocab_size - 1






 def 	_lowerCamelCase (	self	:							Optional[int]     ):
        __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 	_lowerCamelCase (	self	:							Union[str, Any]							,     __A	:							List[Any]							,     __A	:							Dict							,     __A	:							Optional[Any]							,     __A	:							str							,     __A	:							Dict							,     *__A	:							Union[str, Any]     ):
        __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 	_lowerCamelCase (	self	:							Optional[Any]							,     __A	:							str							,     __A	:							str							,     __A	:							Union[str, Any]							,     __A	:							Optional[int]							,     __A	:							Tuple							,     *__A	:							List[Any]     ):
        __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 	_lowerCamelCase (	self	:							Union[str, Any]							,     __A	:							Union[str, Any]							,     __A	:							str							,     __A	:							str							,     __A	:							Union[str, Any]							,     __A	:							str							,     __A	:							List[Any]							,     *__A	:							Optional[Any]     ):
        __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 	_lowerCamelCase (	self	:							str							,     __A	:							Dict							,     __A	:							Dict							,     __A	:							Optional[Any]							,     __A	:							Tuple							,     __A	:							Any							,     *__A	:							Optional[int]     ):
        __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 	_lowerCamelCase (	self	:							int     ):
        __UpperCamelCase	=  self.prepare_config_and_inputs()

        (
            __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 					snake_case					(   A__      ,	A__      ,	unittest.TestCase	):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_     : Dict      		=(
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 SCREAMING_SNAKE_CASE_     : Dict      		=(
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 SCREAMING_SNAKE_CASE_     : str      		=(
     {
         "feature-extraction": TFOpenAIGPTModel,
         "text-classification": TFOpenAIGPTForSequenceClassification,
         "text-generation": TFOpenAIGPTLMHeadModel,
         "zero-shot": TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 SCREAMING_SNAKE_CASE_     : int      		=False
 SCREAMING_SNAKE_CASE_     : Optional[Any]      		=False






 def 	_lowerCamelCase (	self	:							Any							,     __A	:							int							,     __A	:							Tuple							,     __A	:							Optional[int]							,     __A	:							List[Any]							,     __A	:							List[Any]     ):
        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	:							Tuple     ):
        __UpperCamelCase	=  TFOpenAIGPTModelTester(self     )
        __UpperCamelCase	=  ConfigTester(self							,     config_class=__snake_case							,     n_embd=3_7     )






 def 	_lowerCamelCase (	self	:							Optional[int]     ):
        self.config_tester.run_common_tests()






 def 	_lowerCamelCase (	self	:							Dict     ):
        __UpperCamelCase	=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*__snake_case     )






 def 	_lowerCamelCase (	self	:							Optional[Any]     ):
        __UpperCamelCase	=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*__snake_case     )






 def 	_lowerCamelCase (	self	:							Dict     ):
        __UpperCamelCase	=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*__snake_case     )






 def 	_lowerCamelCase (	self	:							Optional[int]     ):
        __UpperCamelCase	=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case     )






 @slow
 def 	_lowerCamelCase (	self	:							str     ):
        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 					snake_case					(   unittest.TestCase	):




 """simple docstring"""



 @slow
 def 	_lowerCamelCase (	self	:							Optional[Any]     ):
        __UpperCamelCase	=  TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
        __UpperCamelCase	=  tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]							,     dtype=tf.intaa     )  # the president is
        __UpperCamelCase	=  [
            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

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







'''simple docstring'''
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


a__							:							Optional[int]			=threading.Lock()
a__							:							Optional[logging.Handler]			=None

a__							:							Optional[Any]			={
    '''debug''': logging.DEBUG,
    '''info''': logging.INFO,
    '''warning''': logging.WARNING,
    '''error''': logging.ERROR,
    '''critical''': logging.CRITICAL,
}

a__							:							List[Any]			=logging.WARNING

a__							:							str			=True
def     lowercase__       (       )			->				int:



       """simple docstring"""






       __UpperCamelCase	=  os.getenv('DIFFUSERS_VERBOSITY'    ,  __lowercase					)
       if env_level_str:
              if env_level_str in log_levels:
                     return log_levels[env_level_str]
              else:
                     logging.getLogger().warning(
                         F'''Unknown option DIFFUSERS_VERBOSITY={env_level_str}, '''
                         F'''has to be one of: { ', '.join(log_levels.keys()					) }'''					)
       return _default_log_level
def     lowercase__       (       )			->				str:



       """simple docstring"""






       return __name__.split('.'					)[0]
def     lowercase__       (       )			->				logging.Logger:



       """simple docstring"""






       return logging.getLogger(_get_library_name()					)
def     lowercase__       (       )			->				None:



       """simple docstring"""






       global _default_handler

       with _lock:
              if _default_handler:
                     # This library has already configured the library root logger.
                     return
              __UpperCamelCase	=  logging.StreamHandler()  # Set sys.stderr as stream.
              __UpperCamelCase	=  sys.stderr.flush

              # Apply our default configuration to the library root logger.
              __UpperCamelCase	=  _get_library_root_logger()
              library_root_logger.addHandler(_default_handler					)
              library_root_logger.setLevel(_get_default_logging_level()					)
              __UpperCamelCase	=  False
def     lowercase__       (       )			->				None:



       """simple docstring"""






       global _default_handler

       with _lock:
              if not _default_handler:
                     return

              __UpperCamelCase	=  _get_library_root_logger()
              library_root_logger.removeHandler(_default_handler					)
              library_root_logger.setLevel(logging.NOTSET					)
              __UpperCamelCase	=  None
def     lowercase__       (       )			->				str:



       """simple docstring"""






       return log_levels
def     lowercase__       (       __lowercase						:  Optional[str] = None					)			->				logging.Logger:



       """simple docstring"""






       if name is None:
              __UpperCamelCase	=  _get_library_name()

       _configure_library_root_logger()
       return logging.getLogger(__lowercase					)
def     lowercase__       (       )			->				int:



       """simple docstring"""






       _configure_library_root_logger()
       return _get_library_root_logger().getEffectiveLevel()
def     lowercase__       (       __lowercase						:  int					)			->				None:



       """simple docstring"""






       _configure_library_root_logger()
       _get_library_root_logger().setLevel(__lowercase					)
def     lowercase__       (       )			->				Tuple:



       """simple docstring"""






       return set_verbosity(__lowercase					)
def     lowercase__       (       )			->				Union[str, Any]:



       """simple docstring"""






       return set_verbosity(__lowercase					)
def     lowercase__       (       )			->				Any:



       """simple docstring"""






       return set_verbosity(__lowercase					)
def     lowercase__       (       )			->				int:



       """simple docstring"""






       return set_verbosity(__lowercase					)
def     lowercase__       (       )			->				None:



       """simple docstring"""






       _configure_library_root_logger()

       assert _default_handler is not None
       _get_library_root_logger().removeHandler(_default_handler					)
def     lowercase__       (       )			->				None:



       """simple docstring"""






       _configure_library_root_logger()

       assert _default_handler is not None
       _get_library_root_logger().addHandler(_default_handler					)
def     lowercase__       (       __lowercase						:  logging.Handler					)			->				None:



       """simple docstring"""






       _configure_library_root_logger()

       assert handler is not None
       _get_library_root_logger().addHandler(__lowercase					)
def     lowercase__       (       __lowercase						:  logging.Handler					)			->				None:



       """simple docstring"""






       _configure_library_root_logger()

       assert handler is not None and handler not in _get_library_root_logger().handlers
       _get_library_root_logger().removeHandler(__lowercase					)
def     lowercase__       (       )			->				None:



       """simple docstring"""






       _configure_library_root_logger()
       __UpperCamelCase	=  False
def     lowercase__       (       )			->				None:



       """simple docstring"""






       _configure_library_root_logger()
       __UpperCamelCase	=  True
def     lowercase__       (       )			->				None:



       """simple docstring"""






       __UpperCamelCase	=  _get_library_root_logger().handlers

       for handler in handlers:
              __UpperCamelCase	=  logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'					)
              handler.setFormatter(__lowercase					)
def     lowercase__       (       )			->				None:



       """simple docstring"""






       __UpperCamelCase	=  _get_library_root_logger().handlers

       for handler in handlers:
              handler.setFormatter(__lowercase					)
def     lowercase__       (       self						:  int    ,  *__lowercase						:  List[str]    ,  **__lowercase						:  List[Any]					)			->				Any:



       """simple docstring"""






       __UpperCamelCase	=  os.getenv('DIFFUSERS_NO_ADVISORY_WARNINGS'    ,  __lowercase					)
       if no_advisory_warnings:
              return
       self.warning(*__lowercase    ,  **__lowercase					)


a__							:							Dict			=warning_advice

class 					snake_case					:




 """simple docstring"""



 def __init__(	self	:							Optional[int]							,     *__A	:							int							,     **__A	:							Dict     ):  # pylint: disable=unused-argument
        __UpperCamelCase	=  args[0] if args else None






 def __iter__(	self	:							str     ):
        return iter(self._iterator     )






 def __getattr__(	self	:							Tuple							,     __A	:							Any     ):

        def empty_fn(*__A	:							Any							,     **__A	:							List[str]     ):  # pylint: disable=unused-argument
               return

        return empty_fn






 def __enter__(	self	:							str     ):
        return self






 def __exit__(	self	:							Optional[Any]							,     __A	:							Dict							,     __A	:							str							,     __A	:							Dict     ):
        return






class 					snake_case					:




 """simple docstring"""



 def __call__(	self	:							Union[str, Any]							,     *__A	:							Union[str, Any]							,     **__A	:							Dict     ):
        if _tqdm_active:
               return tqdm_lib.tqdm(*__A							,     **__A     )
        else:
               return EmptyTqdm(*__A							,     **__A     )






 def 	_lowerCamelCase (	self	:							Any							,     *__A	:							str							,     **__A	:							Optional[int]     ):
        __UpperCamelCase	=  None
        if _tqdm_active:
               return tqdm_lib.tqdm.set_lock(*__A							,     **__A     )






 def 	_lowerCamelCase (	self	:							Optional[int]     ):
        if _tqdm_active:
               return tqdm_lib.tqdm.get_lock()






a__							:							List[Any]			=_tqdm_cls()
def     lowercase__       (       )			->				bool:



       """simple docstring"""






       global _tqdm_active
       return bool(_tqdm_active					)
def     lowercase__       (       )			->				List[str]:



       """simple docstring"""






       global _tqdm_active
       __UpperCamelCase	=  True


def     lowercase__       (       )			->				str:



       """simple docstring"""






       global _tqdm_active
       __UpperCamelCase	=  False
