





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    OpenAIGPTConfig,
							    OpenAIGPTDoubleHeadsModel,
							    OpenAIGPTForSequenceClassification,
							    OpenAIGPTLMHeadModel,
							    OpenAIGPTModel,
							)

class  __A:

					def __init__(      self,       A,       A=13,       A=7,       A=True,       A=True,       A=True,       A=99,       A=32,       A=5,       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,       ):





												"""simple docstring"""

												_UpperCamelCase              =						parent
												_UpperCamelCase              =						batch_size
												_UpperCamelCase              =						seq_length
												_UpperCamelCase              =						is_training
												_UpperCamelCase              =						use_token_type_ids
												_UpperCamelCase              =						use_labels
												_UpperCamelCase              =						vocab_size
												_UpperCamelCase              =						hidden_size
												_UpperCamelCase              =						num_hidden_layers
												_UpperCamelCase              =						num_attention_heads
												_UpperCamelCase              =						intermediate_size
												_UpperCamelCase              =						hidden_act
												_UpperCamelCase              =						hidden_dropout_prob
												_UpperCamelCase              =						attention_probs_dropout_prob
												_UpperCamelCase              =						max_position_embeddings
												_UpperCamelCase              =						type_vocab_size
												_UpperCamelCase              =						type_sequence_label_size
												_UpperCamelCase              =						initializer_range
												_UpperCamelCase              =						num_labels
												_UpperCamelCase              =						num_choices
												_UpperCamelCase              =						scope
												_UpperCamelCase              =						self.vocab_size - 1

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						ids_tensor([self.batch_size, self.seq_length],       self.vocab_size      )

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

												_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,
												    head_mask,
												    token_type_ids,
												    sequence_labels,
												    token_labels,
												    choice_labels,
												)

					def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       *A      ):





												"""simple docstring"""

												_UpperCamelCase              =						OpenAIGPTModel(config=_SCREAMING_SNAKE_CASE      )
												model.to(_SCREAMING_SNAKE_CASE      )
												model.eval()

												_UpperCamelCase              =						model(_SCREAMING_SNAKE_CASE,       token_type_ids=_SCREAMING_SNAKE_CASE,       head_mask=_SCREAMING_SNAKE_CASE      )
												_UpperCamelCase              =						model(_SCREAMING_SNAKE_CASE,       token_type_ids=_SCREAMING_SNAKE_CASE      )
												_UpperCamelCase              =						model(_SCREAMING_SNAKE_CASE      )

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

					def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       *A      ):





												"""simple docstring"""

												_UpperCamelCase              =						OpenAIGPTLMHeadModel(_SCREAMING_SNAKE_CASE      )
												model.to(_SCREAMING_SNAKE_CASE      )
												model.eval()

												_UpperCamelCase              =						model(_SCREAMING_SNAKE_CASE,       token_type_ids=_SCREAMING_SNAKE_CASE,       labels=_SCREAMING_SNAKE_CASE      )
												self.parent.assertEqual(result.loss.shape,       ()      )
												self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.seq_length, self.vocab_size)      )

					def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       *A      ):





												"""simple docstring"""

												_UpperCamelCase              =						OpenAIGPTDoubleHeadsModel(_SCREAMING_SNAKE_CASE      )
												model.to(_SCREAMING_SNAKE_CASE      )
												model.eval()

												_UpperCamelCase              =						model(_SCREAMING_SNAKE_CASE,       token_type_ids=_SCREAMING_SNAKE_CASE,       labels=_SCREAMING_SNAKE_CASE      )
												self.parent.assertEqual(result.loss.shape,       ()      )
												self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.seq_length, self.vocab_size)      )

					def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       *A      ):





												"""simple docstring"""

												_UpperCamelCase              =						self.num_labels
												_UpperCamelCase              =						OpenAIGPTForSequenceClassification(_SCREAMING_SNAKE_CASE      )
												model.to(_SCREAMING_SNAKE_CASE      )
												model.eval()

												_UpperCamelCase              =						ids_tensor([self.batch_size],       self.type_sequence_label_size      )
												_UpperCamelCase              =						model(_SCREAMING_SNAKE_CASE,       token_type_ids=_SCREAMING_SNAKE_CASE,       labels=_SCREAMING_SNAKE_CASE      )
												self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.num_labels)      )



					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						self.prepare_config_and_inputs()
												(
												    (
												    _UpperCamelCase      
												)       ,		(
												    _UpperCamelCase      
												)       ,		(
												    _UpperCamelCase      
												)       ,		(
												    _UpperCamelCase      
												)       ,		(
												    _UpperCamelCase      
												)       ,		(
												    _UpperCamelCase      
												)       ,		(
												    _UpperCamelCase      
												)       ,		      
												)              =						config_and_inputs
												_UpperCamelCase              =						{
												    '''input_ids''': input_ids,
												    '''token_type_ids''': token_type_ids,
												    '''head_mask''': head_mask,
												}

												return config, inputs_dict






@require_torch
class  __A(						lowerCAmelCase__						,			lowerCAmelCase__						,			lowerCAmelCase__						,			unittest.TestCase ):
					__A	  =       (
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					__A	  =       (
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					__A	  =       (
					    {
					        "feature-extraction": OpenAIGPTModel,
					        "text-classification": OpenAIGPTForSequenceClassification,
					        "text-generation": OpenAIGPTLMHeadModel,
					        "zero-shot": OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       A      ):





												"""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,       A,       A,       A=False      ):





												"""simple docstring"""

												_UpperCamelCase              =						super()._prepare_for_class(_SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE,       return_labels=_SCREAMING_SNAKE_CASE      )

												if return_labels:
																			if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																										_UpperCamelCase              =						torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length),       dtype=torch.long,       device=_SCREAMING_SNAKE_CASE,       )
																										_UpperCamelCase              =						inputs_dict['''labels''']
																										_UpperCamelCase              =						inputs_dict['''labels''']
																										_UpperCamelCase              =						torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.num_choices),       dtype=torch.long,       device=_SCREAMING_SNAKE_CASE,       )
																										_UpperCamelCase              =						torch.zeros(
																										    self.model_tester.batch_size,       dtype=torch.long,       device=_SCREAMING_SNAKE_CASE      )
												return inputs_dict

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						OpenAIGPTModelTester(self      )
												_UpperCamelCase              =						ConfigTester(self,       config_class=_SCREAMING_SNAKE_CASE,       n_embd=37      )

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												self.config_tester.run_common_tests()

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_model(*_SCREAMING_SNAKE_CASE      )

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_lm_head_model(*_SCREAMING_SNAKE_CASE      )

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_double_lm_head_model(*_SCREAMING_SNAKE_CASE      )

					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_SCREAMING_SNAKE_CASE      )



					@slow
					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			_UpperCamelCase              =						OpenAIGPTModel.from_pretrained(_SCREAMING_SNAKE_CASE      )
																			self.assertIsNotNone(_SCREAMING_SNAKE_CASE      )






@require_torch
class  __A(						unittest.TestCase ):

					@slow
					def 	_UpperCamelCase     (      self      ):





												"""simple docstring"""

												_UpperCamelCase              =						OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''      )
												model.to(_SCREAMING_SNAKE_CASE      )
												_UpperCamelCase              =						torch.tensor([[481, 4735, 544]],       dtype=torch.long,       device=_SCREAMING_SNAKE_CASE      )  # the president is
												_UpperCamelCase              =						[
												    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

												_UpperCamelCase              =						model.generate(_SCREAMING_SNAKE_CASE,       do_sample=_SCREAMING_SNAKE_CASE      )
												self.assertListEqual(output_ids[0].tolist(),       _SCREAMING_SNAKE_CASE      )













import os
import sys
import unittest


lowercase    : Any    	=  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, """utils"""))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
lowercase    : int    	=  os.path.join(git_repo_path, """src""", """transformers""")

lowercase    : str    	=  """
{0} = None
"""

lowercase    : Union[str, Any]    	=  """
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
"""


lowercase    : Optional[Any]    	=  """
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
"""

class  __A(						unittest.TestCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						find_backend('''    _import_structure["models.albert"].append("AlbertTokenizerFast")'''      )
            self.assertIsNone(A      )

            _UpperCamelCase              =						find_backend('''    if not is_tokenizers_available():'''      )
            self.assertEqual(A,       '''tokenizers'''      )

            _UpperCamelCase              =						find_backend('''    if not is_tensorflow_text_available():'''      )
            self.assertEqual(A,       '''tensorflow_text'''      )

            _UpperCamelCase              =						find_backend('''    if not (is_sentencepiece_available() and is_tokenizers_available()):'''      )
            self.assertEqual(A,       '''sentencepiece_and_tokenizers'''      )

            _UpperCamelCase              =						find_backend(
                '''    if not (is_sentencepiece_available() and is_tensorflow_text_available()):'''      )
            self.assertEqual(A,       '''sentencepiece_and_tensorflow_text'''      )

            _UpperCamelCase              =						find_backend(
                '''    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):'''      )
            self.assertEqual(A,       '''sentencepiece_and_tokenizers_and_vision'''      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						read_init()
            # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
            self.assertIn('''torch''',       A      )
            self.assertIn('''tensorflow_text''',       A      )
            self.assertIn('''sentencepiece_and_tokenizers''',       A      )

            # Likewise, we can't assert on the exact content of a key
            self.assertIn('''BertModel''',       objects['''torch''']      )
            self.assertIn('''TFBertModel''',       objects['''tf''']      )
            self.assertIn('''FlaxBertModel''',       objects['''flax''']      )
            self.assertIn('''BertModel''',       objects['''torch''']      )
            self.assertIn('''TFBertTokenizer''',       objects['''tensorflow_text''']      )
            self.assertIn('''convert_slow_tokenizer''',       objects['''sentencepiece_and_tokenizers''']      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						create_dummy_object('''CONSTANT''',       '''\'torch\''''      )
            self.assertEqual(A,       '''\nCONSTANT = None\n'''      )

            _UpperCamelCase              =						create_dummy_object('''function''',       '''\'torch\''''      )
            self.assertEqual(
                A,       '''\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n'''      )

            _UpperCamelCase              =						'''
class FakeClass(metaclass=DummyObject):
    _backends = \'torch\'

    def __init__(self, *args, **kwargs):
        requires_backends(self, \'torch\')
'''
            _UpperCamelCase              =						create_dummy_object('''FakeClass''',       '''\'torch\''''      )
            self.assertEqual(A,       A      )



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						'''# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, ["torch"])


class FakeClass(metaclass=DummyObject):
    _backends = ["torch"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, ["torch"])
'''
            _UpperCamelCase              =						create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']}      )
            self.assertEqual(dummy_files['''torch'''],       A      )






