






import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment

class      snake_case__       (unittest.TestCase	):







						"""simple docstring"""




						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   List[Any]:
									"""simple docstring"""




									a__     :		Tuple		 =						inspect.getfile(accelerate.test_utils						)
									a__     :		List[Any]		 =						os.path.sep.join(mod_file.split(os.path.sep						)[:-1] + ["""scripts""", """test_sync.py"""]						)

						@require_cpu
						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Union[str, Any]:
									"""simple docstring"""




									debug_launcher(test_sync.main      ,     num_processes=1						)

						@require_cpu
						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Tuple:
									"""simple docstring"""




									debug_launcher(test_sync.main						)

						@require_single_gpu
						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   int:
									"""simple docstring"""




									test_sync.main()

						@require_multi_gpu
						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Optional[int]:
									"""simple docstring"""




									print(F'''Found {torch.cuda.device_count()} devices.'''						)
									a__     :		Tuple		 =						["""torchrun""", F'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path]
									with patch_environment(omp_num_threads=1						):
												execute_subprocess_async(__lowercase      ,     env=os.environ.copy()						)









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      ,     __lowercase      ,     )      ->   str:
									"""simple docstring"""




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

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Dict:
									"""simple docstring"""




									a__     :		Tuple		 =						ids_tensor([self.batch_size, self.seq_length]      ,     self.vocab_size						)

									a__     :		int		 =						None
									if self.use_input_mask:
												a__     :		Optional[int]		 =						random_attention_mask([self.batch_size, self.seq_length]						)

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

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

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

									a__     :		Union[str, 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      ,     )

									a__     :		Optional[int]		 =						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 							SCREAMING_SNAKE_CASE__(     self      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     *__lowercase						)      ->   str:
									"""simple docstring"""




									a__     :		Dict		 =						TFOpenAIGPTModel(config=__lowercase						)
									a__     :		Tuple		 =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
									a__     :		int		 =						model(__lowercase						)

									a__     :		List[Any]		 =						[input_ids, input_mask]
									a__     :		int		 =						model(__lowercase						)

									a__     :		Optional[int]		 =						model(__lowercase						)

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

						def 							SCREAMING_SNAKE_CASE__(     self      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     *__lowercase						)      ->   int:
									"""simple docstring"""




									a__     :		List[str]		 =						TFOpenAIGPTLMHeadModel(config=__lowercase						)
									a__     :		List[str]		 =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
									a__     :		Tuple		 =						model(__lowercase						)
									self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.seq_length, self.vocab_size)						)

						def 							SCREAMING_SNAKE_CASE__(     self      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     *__lowercase						)      ->   Tuple:
									"""simple docstring"""




									a__     :		List[Any]		 =						TFOpenAIGPTDoubleHeadsModel(config=__lowercase						)

									a__     :		int		 =						tf.tile(tf.expand_dims(__lowercase      ,     1						)      ,     (1, self.num_choices, 1)						)
									a__     :		List[str]		 =						tf.tile(tf.expand_dims(__lowercase      ,     1						)      ,     (1, self.num_choices, 1)						)
									a__     :		Dict		 =						tf.tile(tf.expand_dims(__lowercase      ,     1						)      ,     (1, self.num_choices, 1)						)

									a__     :		Optional[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,
									}
									a__     :		Optional[Any]		 =						model(__lowercase						)
									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 							SCREAMING_SNAKE_CASE__(     self      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     *__lowercase						)      ->   Dict:
									"""simple docstring"""




									a__     :		Tuple		 =						self.num_labels
									a__     :		Optional[int]		 =						ids_tensor([self.batch_size]      ,     self.type_sequence_label_size						)
									a__     :		Tuple		 =						{
									    """input_ids""": input_ids,
									    """attention_mask""": input_mask,
									    """token_type_ids""": token_type_ids,
									    """labels""": sequence_labels,
									}
									a__     :		Optional[Any]		 =						TFOpenAIGPTForSequenceClassification(__lowercase						)
									a__     :		Union[str, Any]		 =						model(__lowercase						)
									self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.num_labels)						)

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   int:
									"""simple docstring"""




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

									(
									    (
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		(
									    a__
									) ,		
									)     :		Dict		 =						config_and_inputs

									a__     :		Union[str, Any]		 =						{"""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"""




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

						def 							SCREAMING_SNAKE_CASE__(     self      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase      ,     __lowercase						)      ->   Optional[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 							SCREAMING_SNAKE_CASE__(     self						)      ->   int:
									"""simple docstring"""




									a__     :		Optional[Any]		 =						TFOpenAIGPTModelTester(self						)
									a__     :		List[Any]		 =						ConfigTester(self      ,     config_class=__lowercase      ,     n_embd=3_7						)

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Optional[Any]:
									"""simple docstring"""




									self.config_tester.run_common_tests()

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Union[str, Any]:
									"""simple docstring"""




									a__     :		Tuple		 =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*__lowercase						)

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Optional[int]:
									"""simple docstring"""




									a__     :		Optional[Any]		 =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_lm_head(*__lowercase						)

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   List[str]:
									"""simple docstring"""




									a__     :		int		 =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_double_head(*__lowercase						)

						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Dict:
									"""simple docstring"""




									a__     :		Optional[Any]		 =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowercase						)

						@slow
						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Optional[Any]:
									"""simple docstring"""




									for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a__     :		int		 =						TFOpenAIGPTModel.from_pretrained(__lowercase						)
												self.assertIsNotNone(__lowercase						)




@require_tf
class      snake_case__       (unittest.TestCase	):







						"""simple docstring"""




						@slow
						def 							SCREAMING_SNAKE_CASE__(     self						)      ->   Dict:
									"""simple docstring"""




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

									a__     :		Union[str, Any]		 =						model.generate(__lowercase      ,     do_sample=__lowercase						)
									self.assertListEqual(output_ids[0].numpy().tolist()      ,     __lowercase						)




