'''simple docstring'''


import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

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


if is_torch_available():
		import torch

		from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel






class 						A   :





							def __init__(      self				:							Dict     ,		__a				:							Any     ,		__a				:							str=1_3     ,		__a				:							Optional[int]=7     ,		__a				:							Tuple=True     ,		__a				:							Optional[Any]=True     ,		__a				:							Any=False     ,		__a				:							List[Any]=True     ,		__a				:							int=9_9     ,		__a				:							List[str]=3_2     ,		__a				:							Tuple=5     ,		__a				:							Optional[Any]=4     ,		__a				:							str=3_7     ,		__a				:							Optional[Any]="gelu"     ,		__a				:							List[str]=0.1     ,		__a				:							List[Any]=0.1     ,		__a				:							str=5_1_2     ,		__a				:							Optional[int]=1_6     ,		__a				:							Dict=2     ,		__a				:							Union[str, Any]=0.0_2     ,		__a				:							Any=3     ,		__a				:							Dict=4     ,		__a				:							Any=None     ,		)							->	List[str]:
								__UpperCAmelCase							=			parent
								__UpperCAmelCase							=			batch_size
								__UpperCAmelCase							=			seq_length
								__UpperCAmelCase							=			is_training
								__UpperCAmelCase							=			use_input_mask
								__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





							def       snake_case__						(      self				:							Tuple	)							->	List[Any]:
								__UpperCAmelCase							=			ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size	)

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

								__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							=			self.get_config()

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





							def       snake_case__						(      self				:							int	)							->	Dict:
								return OpenLlamaConfig(
								    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=__A     ,		initializer_range=self.initializer_range     ,		use_stable_embedding=__A     ,		)





							def       snake_case__						(      self				:							str     ,		__a				:							Tuple     ,		__a				:							str     ,		__a				:							Union[str, Any]     ,		__a				:							List[Any]     ,		__a				:							List[str]     ,		__a				:							Optional[int]     ,		__a				:							str	)							->	Any:
								__UpperCAmelCase							=			OpenLlamaModel(config=__A	)
								model.to(__A	)
								model.eval()
								__UpperCAmelCase							=			model(__A     ,		attention_mask=__A	)
								__UpperCAmelCase							=			model(__A	)
								self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)	)





							def       snake_case__						(      self				:							List[Any]     ,		__a				:							Dict     ,		__a				:							int     ,		__a				:							Union[str, Any]     ,		__a				:							Any     ,		__a				:							List[Any]     ,		__a				:							str     ,		__a				:							Union[str, Any]     ,		__a				:							Tuple     ,		__a				:							List[str]     ,		)							->	List[str]:
								__UpperCAmelCase							=			True
								__UpperCAmelCase							=			OpenLlamaModel(__A	)
								model.to(__A	)
								model.eval()
								__UpperCAmelCase							=			model(
								    __A     ,		attention_mask=__A     ,		encoder_hidden_states=__A     ,		encoder_attention_mask=__A     ,		)
								__UpperCAmelCase							=			model(
								    __A     ,		attention_mask=__A     ,		encoder_hidden_states=__A     ,		)
								__UpperCAmelCase							=			model(__A     ,		attention_mask=__A	)
								self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)	)





							def       snake_case__						(      self				:							Optional[int]     ,		__a				:							Optional[Any]     ,		__a				:							str     ,		__a				:							Tuple     ,		__a				:							List[str]     ,		__a				:							int     ,		__a				:							Dict     ,		__a				:							List[str]     ,		__a				:							Optional[int]     ,		__a				:							int     ,		)							->	List[Any]:
								__UpperCAmelCase							=			OpenLlamaForCausalLM(config=__A	)
								model.to(__A	)
								model.eval()
								__UpperCAmelCase							=			model(__A     ,		attention_mask=__A     ,		labels=__A	)
								self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.vocab_size)	)





							def       snake_case__						(      self				:							Any     ,		__a				:							List[str]     ,		__a				:							int     ,		__a				:							int     ,		__a				:							int     ,		__a				:							Dict     ,		__a				:							Optional[Any]     ,		__a				:							Optional[Any]     ,		__a				:							Union[str, Any]     ,		__a				:							List[str]     ,		)							->	str:
								__UpperCAmelCase							=			True
								__UpperCAmelCase							=			True
								__UpperCAmelCase							=			OpenLlamaForCausalLM(config=__A	)
								model.to(__A	)
								model.eval()

								# first forward pass
								__UpperCAmelCase							=			model(
								    __A     ,		attention_mask=__A     ,		encoder_hidden_states=__A     ,		encoder_attention_mask=__A     ,		use_cache=__A     ,		)
								__UpperCAmelCase							=			outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								__UpperCAmelCase							=			ids_tensor((self.batch_size, 3)     ,		config.vocab_size	)
								__UpperCAmelCase							=			ids_tensor((self.batch_size, 3)     ,		vocab_size=2	)

								# append to next input_ids and
								__UpperCAmelCase							=			torch.cat([input_ids, next_tokens]     ,		dim=-1	)
								__UpperCAmelCase							=			torch.cat([input_mask, next_mask]     ,		dim=-1	)

								__UpperCAmelCase							=			model(
								    __A     ,		attention_mask=__A     ,		encoder_hidden_states=__A     ,		encoder_attention_mask=__A     ,		output_hidden_states=__A     ,		)["hidden_states"][0]
								__UpperCAmelCase							=			model(
								    __A     ,		attention_mask=__A     ,		encoder_hidden_states=__A     ,		encoder_attention_mask=__A     ,		past_key_values=__A     ,		output_hidden_states=__A     ,		)["hidden_states"][0]

								# select random slice
								__UpperCAmelCase							=			ids_tensor((1,)     ,		output_from_past.shape[-1]	).item()
								__UpperCAmelCase							=			output_from_no_past[:, -3:, random_slice_idx].detach()
								__UpperCAmelCase							=			output_from_past[:, :, random_slice_idx].detach()

								self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]	)

								# test that outputs are equal for slice
								self.parent.assertTrue(torch.allclose(__A     ,		__A     ,		atol=1e-3	)	)






							def       snake_case__						(      self				:							int	)							->	Dict:
								__UpperCAmelCase							=			self.prepare_config_and_inputs()
								(
								    __UpperCAmelCase		
								)							=			config_and_inputs
								__UpperCAmelCase							=			{"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict






@require_torch
class 						A   (   UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	unittest.TestCase   ):
							a_        =      (
							    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
							)
							a_        =      (OpenLlamaForCausalLM,) if is_torch_available() else ()
							a_        =      (
							    {
							        '''feature-extraction''': OpenLlamaModel,
							        '''text-classification''': OpenLlamaForSequenceClassification,
							        '''text-generation''': OpenLlamaForCausalLM,
							        '''zero-shot''': OpenLlamaForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							a_        =      False
							a_        =      False





							def       snake_case__						(      self				:							int	)							->	Optional[int]:
								__UpperCAmelCase							=			OpenLlamaModelTester(self	)
								__UpperCAmelCase							=			ConfigTester(self     ,		config_class=__A     ,		hidden_size=3_7	)





							def       snake_case__						(      self				:							Union[str, Any]	)							->	int:
								self.config_tester.run_common_tests()





							def       snake_case__						(      self				:							str	)							->	List[Any]:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__A	)





							def       snake_case__						(      self				:							str	)							->	int:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
									__UpperCAmelCase							=			type
									self.model_tester.create_and_check_model(*__A	)





							def       snake_case__						(      self				:							List[Any]	)							->	Tuple:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs_for_common()
								__UpperCAmelCase							=			3
								__UpperCAmelCase							=			input_dict["input_ids"]
								__UpperCAmelCase							=			input_ids.ne(1	).to(__A	)
								__UpperCAmelCase							=			ids_tensor([self.model_tester.batch_size]     ,		self.model_tester.type_sequence_label_size	)
								__UpperCAmelCase							=			OpenLlamaForSequenceClassification(__A	)
								model.to(__A	)
								model.eval()
								__UpperCAmelCase							=			model(__A     ,		attention_mask=__A     ,		labels=__A	)
								self.assertEqual(result.logits.shape     ,		(self.model_tester.batch_size, self.model_tester.num_labels)	)





							def       snake_case__						(      self				:							List[Any]	)							->	Optional[int]:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs_for_common()
								__UpperCAmelCase							=			3
								__UpperCAmelCase							=			"single_label_classification"
								__UpperCAmelCase							=			input_dict["input_ids"]
								__UpperCAmelCase							=			input_ids.ne(1	).to(__A	)
								__UpperCAmelCase							=			ids_tensor([self.model_tester.batch_size]     ,		self.model_tester.type_sequence_label_size	)
								__UpperCAmelCase							=			OpenLlamaForSequenceClassification(__A	)
								model.to(__A	)
								model.eval()
								__UpperCAmelCase							=			model(__A     ,		attention_mask=__A     ,		labels=__A	)
								self.assertEqual(result.logits.shape     ,		(self.model_tester.batch_size, self.model_tester.num_labels)	)





							def       snake_case__						(      self				:							Optional[int]	)							->	Any:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs_for_common()
								__UpperCAmelCase							=			3
								__UpperCAmelCase							=			"multi_label_classification"
								__UpperCAmelCase							=			input_dict["input_ids"]
								__UpperCAmelCase							=			input_ids.ne(1	).to(__A	)
								__UpperCAmelCase							=			ids_tensor(
								    [self.model_tester.batch_size, config.num_labels]     ,		self.model_tester.type_sequence_label_size	).to(torch.float	)
								__UpperCAmelCase							=			OpenLlamaForSequenceClassification(__A	)
								model.to(__A	)
								model.eval()
								__UpperCAmelCase							=			model(__A     ,		attention_mask=__A     ,		labels=__A	)
								self.assertEqual(result.logits.shape     ,		(self.model_tester.batch_size, self.model_tester.num_labels)	)





							@unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test'''	)
							def       snake_case__						(      self				:							List[Any]	)							->	List[Any]:
								pass






							@parameterized.expand([('''linear''',), ('''dynamic''',)]	)
							def       snake_case__						(      self				:							Any     ,		__a				:							Optional[int]	)							->	str:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs_for_common()
								__UpperCAmelCase							=			ids_tensor([1, 1_0]     ,		config.vocab_size	)
								__UpperCAmelCase							=			ids_tensor([1, int(config.max_position_embeddings * 1.5	)]     ,		config.vocab_size	)

								set_seed(4_2	)  # Fixed seed at init time so the two models get the same random weights
								__UpperCAmelCase							=			OpenLlamaModel(__A	)
								original_model.to(__A	)
								original_model.eval()
								__UpperCAmelCase							=			original_model(__A	).last_hidden_state
								__UpperCAmelCase							=			original_model(__A	).last_hidden_state

								set_seed(4_2	)  # Fixed seed at init time so the two models get the same random weights
								__UpperCAmelCase							=			{"type": scaling_type, "factor": 1_0.0}
								__UpperCAmelCase							=			OpenLlamaModel(__A	)
								scaled_model.to(__A	)
								scaled_model.eval()
								__UpperCAmelCase							=			scaled_model(__A	).last_hidden_state
								__UpperCAmelCase							=			scaled_model(__A	).last_hidden_state

								# Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
								# maximum sequence length, so the outputs for the short input should match.
								if scaling_type == "dynamic":
									self.assertTrue(torch.allclose(__A     ,		__A     ,		atol=1e-5	)	)
								else:
									self.assertFalse(torch.allclose(__A     ,		__A     ,		atol=1e-5	)	)

								# The output should be different for long inputs
								self.assertFalse(torch.allclose(__A     ,		__A     ,		atol=1e-5	)	)

'''simple docstring'''


from __future__ import annotations

from statistics import mean





def       lowerCAmelCase					(		UpperCamelCase__				:   list[int]    ,					UpperCamelCase__				:   list[int]    ,					UpperCamelCase__				:   int       ):


	"""simple docstring"""
	__UpperCAmelCase							=			[0] * no_of_processes
	__UpperCAmelCase							=			[0] * no_of_processes

	# Initialize remaining_time to waiting_time.

	for i in range(UpperCamelCase__       ):
		__UpperCAmelCase							=			burst_time[i]
	__UpperCAmelCase							=			[]

	__UpperCAmelCase							=			0
	__UpperCAmelCase							=			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:
		__UpperCAmelCase							=			[]
		__UpperCAmelCase							=			-1

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

		if len(UpperCamelCase__       ) > 0:
			__UpperCAmelCase							=			ready_process[0]
			for i in ready_process:
				if remaining_time[i] < remaining_time[target_process]:
					__UpperCAmelCase							=			i
			total_time += burst_time[target_process]
			completed += 1
			__UpperCAmelCase							=			0
			__UpperCAmelCase							=			(
			    total_time - arrival_time[target_process] - burst_time[target_process]
			)
		else:
			total_time += 1

	return waiting_time





def       lowerCAmelCase					(		UpperCamelCase__				:   list[int]    ,					UpperCamelCase__				:   int    ,					UpperCamelCase__				:   list[int]       ):


	"""simple docstring"""
	__UpperCAmelCase							=			[0] * no_of_processes
	for i in range(UpperCamelCase__       ):
		__UpperCAmelCase							=			burst_time[i] + waiting_time[i]
	return turn_around_time


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

					__lowerCAmelCase	:       List[Any]						       =	4
					__lowerCAmelCase	:       List[Any]						       =	[2, 5, 3, 7]
					__lowerCAmelCase	:       Tuple						       =	[0, 0, 0, 0]
					__lowerCAmelCase	:       Optional[int]						       =	calculate_waitingtime(arrival_time, burst_time, no_of_processes)
					__lowerCAmelCase	:       Dict						       =	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}""")
