



'''simple docstring'''




from __future__ import annotations

from statistics import mean



def      a      (			lowerCamelCase__						,				lowerCamelCase__						,				lowerCamelCase__   ):
			'''simple docstring'''





			A_     :						Optional[int]     =					[0] * no_of_processes
			A_     :						Optional[Any]     =					[0] * no_of_processes

			# Initialize remaining_time to waiting_time.

			for i in range(lowerCamelCase__   ):
						A_     :						Tuple     =					burst_time[i]
			A_     :						list[int]     =					[]

			A_     :						Optional[Any]     =					0
			A_     :						Optional[Any]     =					0

			# When processes are not completed,
			# A process whose arrival time has passed \
			# and has remaining execution time is put into the ready_process.
			# The shortest process in the ready_process, target_process is executed.

			while completed != no_of_processes:
						A_     :						Optional[Any]     =					[]
						A_     :						int     =					-1

						for i in range(lowerCamelCase__   ):
									if (arrival_time[i] <= total_time) and (remaining_time[i] > 0):
												ready_process.append(lowerCamelCase__   )

						if len(lowerCamelCase__   ) > 0:
									A_     :						Dict     =					ready_process[0]
									for i in ready_process:
												if remaining_time[i] < remaining_time[target_process]:
															A_     :						Any     =					i
									total_time += burst_time[target_process]
									completed += 1
									A_     :						List[Any]     =					0
									A_     :						Optional[Any]     =					(
									    total_time - arrival_time[target_process] - burst_time[target_process]
									)
						else:
									total_time += 1

			return waiting_time



def      a      (			lowerCamelCase__						,				lowerCamelCase__						,				lowerCamelCase__   ):
			'''simple docstring'''





			A_     :						int     =					[0] * no_of_processes
			for i in range(lowerCamelCase__   ):
						A_     :						Union[str, Any]     =					burst_time[i] + waiting_time[i]
			return turn_around_time


if __name__ == "__main__":
							print('''[TEST CASE 01]''')

							lowerCamelCase							:Tuple       = 4
							lowerCamelCase							:Tuple       = [2, 5, 3, 7]
							lowerCamelCase							:List[str]       = [0, 0, 0, 0]
							lowerCamelCase							:Any       = calculate_waitingtime(arrival_time, burst_time, no_of_processes)
							lowerCamelCase							:Any       = calculate_turnaroundtime(
							    burst_time, no_of_processes, waiting_time
							)

							# Printing the Result
							print('''PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time''')
							for i, process_id in enumerate(list(range(1, 5))):
														print(
														    F"{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t"
														    F"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}"
														)
							print(F"\nAverage waiting time = {mean(waiting_time):.5f}")
							print(F"Average turnaround time = {mean(turn_around_time):.5f}")




'''simple docstring'''




import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

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


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    NezhaForMaskedLM,
						    NezhaForMultipleChoice,
						    NezhaForNextSentencePrediction,
						    NezhaForPreTraining,
						    NezhaForQuestionAnswering,
						    NezhaForSequenceClassification,
						    NezhaForTokenClassification,
						    NezhaModel,
						)
						from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST







class      _lowerCAmelCase							:


	def __init__(self	,				lowercase	,				lowercase=13	,				lowercase=7	,				lowercase=True	,				lowercase=True	,				lowercase=True	,				lowercase=True	,				lowercase=99	,				lowercase=32	,				lowercase=5	,				lowercase=4	,				lowercase=37	,				lowercase="gelu"	,				lowercase=0.1	,				lowercase=0.1	,				lowercase=128	,				lowercase=32	,				lowercase=16	,				lowercase=2	,				lowercase=0.02	,				lowercase=3	,				lowercase=4	,				lowercase=None	,				):
				A_     :						Union[str, Any]     =					parent
				A_     :						Optional[int]     =					batch_size
				A_     :						Any     =					seq_length
				A_     :						int     =					is_training
				A_     :						List[str]     =					use_input_mask
				A_     :						Any     =					use_token_type_ids
				A_     :						List[Any]     =					use_labels
				A_     :						Dict     =					vocab_size
				A_     :						Optional[int]     =					hidden_size
				A_     :						int     =					num_hidden_layers
				A_     :						List[str]     =					num_attention_heads
				A_     :						Dict     =					intermediate_size
				A_     :						List[str]     =					hidden_act
				A_     :						List[str]     =					hidden_dropout_prob
				A_     :						Union[str, Any]     =					attention_probs_dropout_prob
				A_     :						Optional[Any]     =					max_position_embeddings
				A_     :						Optional[Any]     =					type_vocab_size
				A_     :						List[Any]     =					type_sequence_label_size
				A_     :						Tuple     =					initializer_range
				A_     :						List[Any]     =					num_labels
				A_     :						str     =					num_choices
				A_     :						Tuple     =					scope


	def    _a     (self							):
				A_     :						List[str]     =					ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size							)

				A_     :						Tuple     =					None
				if self.use_input_mask:
							A_     :						str     =					random_attention_mask([self.batch_size, self.seq_length]							)

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

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

				A_     :						Any     =					self.get_config()

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


	def    _a     (self							):
				return NezhaConfig(
				    vocab_size=self.vocab_size	,				hidden_size=self.hidden_size	,				num_hidden_layers=self.num_hidden_layers	,				num_attention_heads=self.num_attention_heads	,				intermediate_size=self.intermediate_size	,				hidden_act=self.hidden_act	,				hidden_dropout_prob=self.hidden_dropout_prob	,				attention_probs_dropout_prob=self.attention_probs_dropout_prob	,				max_position_embeddings=self.max_position_embeddings	,				type_vocab_size=self.type_vocab_size	,				is_decoder=lowercase	,				initializer_range=self.initializer_range	,				)


	def    _a     (self							):
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						Union[str, Any]     =					self.prepare_config_and_inputs()

				A_     :						Union[str, Any]     =					True
				A_     :						List[Any]     =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
				A_     :						Optional[Any]     =					ids_tensor([self.batch_size, self.seq_length]	,				vocab_size=2							)

				return (
				    config,
				    input_ids,
				    token_type_ids,
				    input_mask,
				    sequence_labels,
				    token_labels,
				    choice_labels,
				    encoder_hidden_states,
				    encoder_attention_mask,
				)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Union[str, Any]     =					NezhaModel(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase							)
				A_     :						Optional[Any]     =					model(lowercase	,				token_type_ids=lowercase							)
				A_     :						str     =					model(lowercase							)
				self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)							)
				self.parent.assertEqual(result.pooler_output.shape	,				(self.batch_size, self.hidden_size)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				):
				A_     :						Optional[int]     =					True
				A_     :						Optional[Any]     =					NezhaModel(lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Optional[int]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				encoder_hidden_states=lowercase	,				encoder_attention_mask=lowercase	,				)
				A_     :						str     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				encoder_hidden_states=lowercase	,				)
				A_     :						Tuple     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase							)
				self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)							)
				self.parent.assertEqual(result.pooler_output.shape	,				(self.batch_size, self.hidden_size)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Optional[Any]     =					NezhaForMaskedLM(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						List[str]     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.vocab_size)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Tuple     =					NezhaForNextSentencePrediction(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Union[str, Any]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, 2)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						int     =					NezhaForPreTraining(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						str     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				next_sentence_label=lowercase	,				)
				self.parent.assertEqual(result.prediction_logits.shape	,				(self.batch_size, self.seq_length, self.vocab_size)							)
				self.parent.assertEqual(result.seq_relationship_logits.shape	,				(self.batch_size, 2)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Any     =					NezhaForQuestionAnswering(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Optional[int]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				start_positions=lowercase	,				end_positions=lowercase	,				)
				self.parent.assertEqual(result.start_logits.shape	,				(self.batch_size, self.seq_length)							)
				self.parent.assertEqual(result.end_logits.shape	,				(self.batch_size, self.seq_length)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Optional[Any]     =					self.num_labels
				A_     :						int     =					NezhaForSequenceClassification(lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						List[Any]     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						List[str]     =					self.num_labels
				A_     :						Optional[int]     =					NezhaForTokenClassification(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.num_labels)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						int     =					self.num_choices
				A_     :						int     =					NezhaForMultipleChoice(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					input_ids.unsqueeze(1							).expand(-1	,				self.num_choices	,				-1							).contiguous()
				A_     :						str     =					token_type_ids.unsqueeze(1							).expand(-1	,				self.num_choices	,				-1							).contiguous()
				A_     :						List[str]     =					input_mask.unsqueeze(1							).expand(-1	,				self.num_choices	,				-1							).contiguous()
				A_     :						Optional[int]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_choices)							)


	def    _a     (self							):
				A_     :						Tuple     =					self.prepare_config_and_inputs()
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						int     =					config_and_inputs
				A_     :						Any     =					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
				return config, inputs_dict







@require_torch
class      _lowerCAmelCase							(      __UpperCAmelCase				,      __UpperCAmelCase				,      __UpperCAmelCase				,      unittest.TestCase			):
	__SCREAMING_SNAKE_CASE       :		Optional[Any]		      = (
	    (
	        NezhaModel,
	        NezhaForMaskedLM,
	        NezhaForMultipleChoice,
	        NezhaForNextSentencePrediction,
	        NezhaForPreTraining,
	        NezhaForQuestionAnswering,
	        NezhaForSequenceClassification,
	        NezhaForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	__SCREAMING_SNAKE_CASE       :		str		      = (
	    {
	        'feature-extraction': NezhaModel,
	        'fill-mask': NezhaForMaskedLM,
	        'question-answering': NezhaForQuestionAnswering,
	        'text-classification': NezhaForSequenceClassification,
	        'token-classification': NezhaForTokenClassification,
	        'zero-shot': NezhaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__SCREAMING_SNAKE_CASE       :		List[Any]		      = True


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase=False							):
				A_     :						Optional[Any]     =					super()._prepare_for_class(lowercase	,				lowercase	,				return_labels=lowercase							)

				if return_labels:
							if model_class in get_values(lowercase							):
										A_     :						Optional[Any]     =					torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.seq_length)	,				dtype=torch.long	,				device=lowercase							)
										A_     :						Tuple     =					torch.zeros(
										    self.model_tester.batch_size	,				dtype=torch.long	,				device=lowercase							)
				return inputs_dict


	def    _a     (self							):
				A_     :						Optional[int]     =					NezhaModelTester(self							)
				A_     :						Any     =					ConfigTester(self	,				config_class=lowercase	,				hidden_size=37							)


	def    _a     (self							):
				self.config_tester.run_common_tests()


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase							)


	def    _a     (self							):
				A_     :						str     =					self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_model_as_decoder(*lowercase							)


	def    _a     (self							):
				# This regression test was failing with PyTorch < 1.3
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						List[Any]     =					self.model_tester.prepare_config_and_inputs_for_decoder()

				A_     :						str     =					None

				self.model_tester.create_and_check_model_as_decoder(
				    lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				)


	def    _a     (self							):
				A_     :						str     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*lowercase							)


	def    _a     (self							):
				A_     :						Union[str, Any]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_next_sequence_prediction(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_pretraining(*lowercase							)


	def    _a     (self							):
				A_     :						Tuple     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowercase							)


	def    _a     (self							):
				A_     :						List[str]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowercase							)


	@slow
	def    _a     (self							):
				for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A_     :						Any     =					NezhaModel.from_pretrained(lowercase							)
							self.assertIsNotNone(lowercase							)


	@slow
	@require_torch_gpu
	def    _a     (self							):
				A_,   A_     :						Tuple     =					self.model_tester.prepare_config_and_inputs_for_common()
				for model_class in self.all_model_classes:
							# NezhaForMultipleChoice behaves incorrectly in JIT environments.
							if model_class == NezhaForMultipleChoice:
										return

							A_     :						Optional[int]     =					True
							A_     :						str     =					model_class(config=lowercase							)

							A_     :						str     =					self._prepare_for_class(lowercase	,				lowercase							)
							A_     :						Tuple     =					torch.jit.trace(
							    lowercase	,				(inputs_dict["""input_ids"""].to("""cpu"""							), inputs_dict["""attention_mask"""].to("""cpu"""							))							)

							with tempfile.TemporaryDirectory() as tmp:
										torch.jit.save(lowercase	,				os.path.join(lowercase	,				"""bert.pt"""							)							)
										A_     :						List[str]     =					torch.jit.load(os.path.join(lowercase	,				"""bert.pt"""							)	,				map_location=lowercase							)
										loaded(inputs_dict["""input_ids"""].to(lowercase							)	,				inputs_dict["""attention_mask"""].to(lowercase							)							)







@require_torch
class      _lowerCAmelCase							(      unittest.TestCase			):


	@slow
	def    _a     (self							):
				A_     :						Dict     =					NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""							)
				A_     :						List[Any]     =					torch.tensor([[0, 1, 2, 3, 4, 5]]							)
				A_     :						List[str]     =					torch.tensor([[0, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_     :						Optional[int]     =					model(lowercase	,				attention_mask=lowercase							)[0]
				A_     :						Optional[int]     =					torch.Size((1, 6, 768)							)
				self.assertEqual(output.shape	,				lowercase							)
				A_     :						List[Any]     =					torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]							)

				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,				lowercase	,				atol=1E-4							)							)


	@slow
	def    _a     (self							):
				A_     :						str     =					NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""							)
				A_     :						Union[str, Any]     =					torch.tensor([[0, 1, 2, 3, 4, 5]]							)
				A_     :						str     =					torch.tensor([[1, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_     :						Tuple     =					model(lowercase	,				attention_mask=lowercase							)[0]
				A_     :						str     =					torch.Size((1, 6, 21128)							)
				self.assertEqual(output.shape	,				lowercase							)
				A_     :						List[Any]     =					torch.tensor(
				    [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]							)

				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,				lowercase	,				atol=1E-4							)							)