


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 			a      :






		def __init__(  self	,     __UpperCamelCase	,     )->			int:
						'''simple docstring'''


						A__     :							str   				=parent
						A__     :							List[Any]   				=13
						A__     :							List[str]   				=7
						A__     :							List[str]   				=True
						A__     :							str   				=True
						A__     :							str   				=True
						A__     :							str   				=True
						A__     :							List[Any]   				=True
						A__     :							int   				=99
						A__     :							List[Any]   				=32
						A__     :							List[Any]   				=2
						A__     :							Union[str, Any]   				=4
						A__     :							Optional[int]   				=37
						A__     :							Dict   				='''gelu'''
						A__     :							List[Any]   				=0.1
						A__     :							Optional[Any]   				=0.1
						A__     :							Any   				=5_12
						A__     :							str   				=16
						A__     :							Any   				=2
						A__     :							int   				=0.02
						A__     :							Tuple   				=3
						A__     :							str   				=4
						A__     :							List[str]   				=None
						A__     :							Any   				=self.vocab_size - 1






		def       lowerCAmelCase_  (  self							)->			Dict:
						'''simple docstring'''


						A__     :							str   				=ids_tensor([self.batch_size, self.seq_length]	,     self.vocab_size							)

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

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

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

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

						A__     :							int   				=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__     :							Tuple   				=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	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     *__UpperCamelCase							)->			Optional[Any]:
						'''simple docstring'''


						A__     :							Optional[Any]   				=TFOpenAIGPTModel(config=lowercase_							)
						A__     :							List[Any]   				={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
						A__     :							List[str]   				=model(lowercase_							)

						A__     :							Optional[int]   				=[input_ids, input_mask]
						A__     :							Optional[int]   				=model(lowercase_							)

						A__     :							int   				=model(lowercase_							)

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






		def       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     *__UpperCamelCase							)->			Optional[int]:
						'''simple docstring'''


						A__     :							List[Any]   				=TFOpenAIGPTLMHeadModel(config=lowercase_							)
						A__     :							Any   				={'''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       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     *__UpperCamelCase							)->			List[Any]:
						'''simple docstring'''


						A__     :							str   				=TFOpenAIGPTDoubleHeadsModel(config=lowercase_							)

						A__     :							Tuple   				=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__     :							int   				=tf.tile(tf.expand_dims(lowercase_	,     1							)	,     (1, self.num_choices, 1)							)

						A__     :							List[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,
						}
						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       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     *__UpperCamelCase							)->			List[Any]:
						'''simple docstring'''


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






		def       lowerCAmelCase_  (  self							)->			Union[str, Any]:
						'''simple docstring'''


						A__     :							Union[str, Any]   				=self.prepare_config_and_inputs()

						(
						    (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,  (
						    A__   
						)				,     
						)     :							List[Any]   				=config_and_inputs

						A__     :							Dict   				={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
						return config, inputs_dict







@require_tf
class 			a      (      lowerCamelCase__      ,lowerCamelCase__      ,unittest.TestCase   ):
		__lowercase													=		(
		    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
		    if is_tf_available()
		    else ()
		)
		__lowercase													=		(
		    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
		)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
		__lowercase													=		(
		    {
		        'feature-extraction': TFOpenAIGPTModel,
		        'text-classification': TFOpenAIGPTForSequenceClassification,
		        'text-generation': TFOpenAIGPTLMHeadModel,
		        'zero-shot': TFOpenAIGPTForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		__lowercase													=		False
		__lowercase													=		False






		def       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							)->			int:
						'''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       lowerCAmelCase_  (  self							)->			Optional[int]:
						'''simple docstring'''


						A__     :							Tuple   				=TFOpenAIGPTModelTester(self							)
						A__     :							Optional[Any]   				=ConfigTester(self	,     config_class=lowercase_	,     n_embd=37							)






		def       lowerCAmelCase_  (  self							)->			int:
						'''simple docstring'''


						self.config_tester.run_common_tests()






		def       lowerCAmelCase_  (  self							)->			str:
						'''simple docstring'''


						A__     :							str   				=self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*lowercase_							)






		def       lowerCAmelCase_  (  self							)->			Dict:
						'''simple docstring'''


						A__     :							Tuple   				=self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_lm_head(*lowercase_							)






		def       lowerCAmelCase_  (  self							)->			Any:
						'''simple docstring'''


						A__     :							Optional[Any]   				=self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_double_head(*lowercase_							)






		def       lowerCAmelCase_  (  self							)->			Tuple:
						'''simple docstring'''


						A__     :							Tuple   				=self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase_							)






		@slow
		def       lowerCAmelCase_  (  self							)->			int:
						'''simple docstring'''


						for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A__     :							List[str]   				=TFOpenAIGPTModel.from_pretrained(lowercase_							)
										self.assertIsNotNone(lowercase_							)







@require_tf
class 			a      (      unittest.TestCase   ):






		@slow
		def       lowerCAmelCase_  (  self							)->			List[str]:
						'''simple docstring'''


						A__     :							int   				=TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''							)
						A__     :							Union[str, Any]   				=tf.convert_to_tensor([[4_81, 47_35, 5_44]]	,     dtype=tf.intaa							)  # the president is
						A__     :							Tuple   				=[
						    4_81,
						    47_35,
						    5_44,
						    2_46,
						    9_63,
						    8_70,
						    7_62,
						    2_39,
						    2_44,
						    4_04_77,
						    2_44,
						    2_49,
						    7_19,
						    8_81,
						    4_87,
						    5_44,
						    2_40,
						    2_44,
						    6_03,
						    4_81,
						]  # the president is a very good man. " \n " i\'m sure he is, " said the

						A__     :							Optional[int]   				=model.generate(lowercase_	,     do_sample=lowercase_							)
						self.assertListEqual(output_ids[0].numpy().tolist()	,     lowercase_							)







from __future__ import annotations



def   SCREAMING_SNAKE_CASE__						(							snake_case_	)			->							list[int]:
				A__     :							Optional[int]   				=[0] * len(snake_case_	)
				for i in range(1,	len(snake_case_	)	):
								A__     :							List[Any]   				=duration_times[i - 1] + waiting_times[i - 1]
				return waiting_times



def   SCREAMING_SNAKE_CASE__						(							snake_case_,	snake_case_	)			->							list[int]:
				return [
				    duration_time + waiting_times[i]
				    for i, duration_time in enumerate(snake_case_	)
				]



def   SCREAMING_SNAKE_CASE__						(							snake_case_	)			->							float:
				return sum(snake_case_	) / len(snake_case_	)



def   SCREAMING_SNAKE_CASE__						(							snake_case_	)			->							float:
				return sum(snake_case_	) / len(snake_case_	)


if __name__ == "__main__":
						# process id's
						__lowerCamelCase       :       List[str]				   =			[1, 2, 3]

						# ensure that we actually have processes
						if len(processes) == 0:
												print("Zero amount of processes")
												raise SystemExit(0)

						# duration time of all processes
						__lowerCamelCase       :       Dict				   =			[19, 8, 9]

						# ensure we can match each id to a duration time
						if len(duration_times) != len(processes):
												print("Unable to match all id's with their duration time")
												raise SystemExit(0)

						# get the waiting times and the turnaround times
						__lowerCamelCase       :       int				   =			calculate_waiting_times(duration_times)
						__lowerCamelCase       :       Optional[int]				   =			calculate_turnaround_times(duration_times, waiting_times)

						# get the average times
						__lowerCamelCase       :       List[str]				   =			calculate_average_waiting_time(waiting_times)
						__lowerCamelCase       :       Dict				   =			calculate_average_turnaround_time(turnaround_times)

						# print all the results
						print("Process ID\tDuration Time\tWaiting Time\tTurnaround Time")
						for i, process in enumerate(processes):
												print(
												    F"{process}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t"
												    F"{turnaround_times[i]}"
												)
						print(F"Average waiting time = {average_waiting_time}")
						print(F"Average turn around time = {average_turnaround_time}")



