


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






       def __init__(   self ,    _UpperCamelCase ,    )->			int:
            _A				=       parent
            _A				=       13
            _A				=       7
            _A				=       True
            _A				=       True
            _A				=       True
            _A				=       True
            _A				=       True
            _A				=       99
            _A				=       32
            _A				=       2
            _A				=       4
            _A				=       37
            _A				=       '''gelu'''
            _A				=       0.1
            _A				=       0.1
            _A				=       512
            _A				=       16
            _A				=       2
            _A				=       0.02
            _A				=       3
            _A				=       4
            _A				=       None
            _A				=       self.vocab_size - 1






       def      UpperCamelCase	(   self      )->			Any:
            _A				=       ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size      )

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

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

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

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

            _A				=       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 ,    )

            _A				=       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 ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    *_UpperCamelCase      )->			List[str]:
            _A				=       TFOpenAIGPTModel(config=_snake_case      )
            _A				=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
            _A				=       model(_snake_case      )

            _A				=       [input_ids, input_mask]
            _A				=       model(_snake_case      )

            _A				=       model(_snake_case      )

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






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    *_UpperCamelCase      )->			Any:
            _A				=       TFOpenAIGPTLMHeadModel(config=_snake_case      )
            _A				=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
            _A				=       model(_snake_case      )
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    *_UpperCamelCase      )->			Optional[int]:
            _A				=       TFOpenAIGPTDoubleHeadsModel(config=_snake_case      )

            _A				=       tf.tile(tf.expand_dims(_snake_case ,    1      ) ,    (1, self.num_choices, 1)      )
            _A				=       tf.tile(tf.expand_dims(_snake_case ,    1      ) ,    (1, self.num_choices, 1)      )
            _A				=       tf.tile(tf.expand_dims(_snake_case ,    1      ) ,    (1, self.num_choices, 1)      )

            _A				=       {
                '''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,
            }
            _A				=       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 ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    *_UpperCamelCase      )->			str:
            _A				=       self.num_labels
            _A				=       ids_tensor([self.batch_size] ,    self.type_sequence_label_size      )
            _A				=       {
                '''input_ids''': input_ids,
                '''attention_mask''': input_mask,
                '''token_type_ids''': token_type_ids,
                '''labels''': sequence_labels,
            }
            _A				=       TFOpenAIGPTForSequenceClassification(_snake_case      )
            _A				=       model(_snake_case      )
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_labels)      )






       def      UpperCamelCase	(   self      )->			Optional[int]:
            _A				=       self.prepare_config_and_inputs()

            (
                _A							
            )				=       config_and_inputs

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






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			Optional[int]:
            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]:
            _A				=       TFOpenAIGPTModelTester(self      )
            _A				=       ConfigTester(self ,    config_class=_snake_case ,    n_embd=37      )






       def      UpperCamelCase	(   self      )->			int:
            self.config_tester.run_common_tests()






       def      UpperCamelCase	(   self      )->			List[str]:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*_snake_case      )






       def      UpperCamelCase	(   self      )->			List[Any]:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_lm_head(*_snake_case      )






       def      UpperCamelCase	(   self      )->			int:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_double_head(*_snake_case      )






       def      UpperCamelCase	(   self      )->			Optional[int]:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_snake_case      )






       @slow
       def      UpperCamelCase	(   self      )->			int:
            for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A				=       TFOpenAIGPTModel.from_pretrained(_snake_case      )
                 self.assertIsNotNone(_snake_case      )






@require_tf
class 					lowerCAmelCase_     (    unittest.TestCase  ):






       @slow
       def      UpperCamelCase	(   self      )->			List[Any]:
            _A				=       TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'      )
            _A				=       tf.convert_to_tensor([[481, 4735, 544]] ,    dtype=tf.intaa      )  # the president is
            _A				=       [
                481,
                4735,
                544,
                246,
                963,
                870,
                762,
                239,
                244,
                4_0477,
                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

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









'''simple docstring'''
from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
    import torch

lowerCAmelCase  =				logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
    from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):

    def 				lowerCamelCase_   (   cls				:					Tuple		)				->  Dict:







         """simple docstring"""
         return cls

def 				lowerCamelCase_   (   __UpperCamelCase				:					Union[Tuple, List]       ,						__UpperCamelCase				:					Optional[Union[List["torch.Generator"], "torch.Generator"]] = None       ,						__UpperCamelCase				:					Optional["torch.device"] = None       ,						__UpperCamelCase				:					Optional["torch.dtype"] = None       ,						__UpperCamelCase				:					Optional["torch.layout"] = None       ,						)				->  Tuple:







     """simple docstring"""
     _A				=       device
     _A				=       shape[0]

     _A				=       layout or torch.strided
     _A				=       device or torch.device('cpu'		)

     if generator is not None:
          _A				=       generator.device.type if not isinstance(__UpperCamelCase       ,						__UpperCamelCase		) else generator[0].device.type
          if gen_device_type != device.type and gen_device_type == "cpu":
               _A				=       'cpu'
               if device != "mps":
                    logger.info(
                        F'The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'
                        F' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'
                        F' slighly speed up this function by passing a generator that was created on the {device} device.'		)
          elif gen_device_type != device.type and gen_device_type == "cuda":
               raise ValueError(F'Cannot generate a {device} tensor from a generator of type {gen_device_type}.'		)

     if isinstance(__UpperCamelCase       ,						__UpperCamelCase		):
          _A				=       (1,) + shape[1:]
          _A				=       [
              torch.randn(__UpperCamelCase       ,						generator=generator[i]       ,						device=__UpperCamelCase       ,						dtype=__UpperCamelCase       ,						layout=__UpperCamelCase		)
              for i in range(__UpperCamelCase		)
          ]
          _A				=       torch.cat(__UpperCamelCase       ,						dim=0		).to(__UpperCamelCase		)
     else:
          _A				=       torch.randn(__UpperCamelCase       ,						generator=__UpperCamelCase       ,						device=__UpperCamelCase       ,						dtype=__UpperCamelCase       ,						layout=__UpperCamelCase		).to(__UpperCamelCase		)

     return latents

def 				lowerCamelCase_   (   __UpperCamelCase				:					Any		)				->  Optional[int]:







     """simple docstring"""
     if is_torch_version('<'       ,						'2.0.0'		) or not hasattr(__UpperCamelCase       ,						'_dynamo'		):
          return False
     return isinstance(__UpperCamelCase       ,						torch._dynamo.eval_frame.OptimizedModule		)





