


"""simple docstring"""






import unittest

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

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 (
					    ErnieMForInformationExtraction,
					    ErnieMForMultipleChoice,
					    ErnieMForQuestionAnswering,
					    ErnieMForSequenceClassification,
					    ErnieMForTokenClassification,
					    ErnieMModel,
					)
					from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST


class 			SCREAMING_SNAKE_CASE_  :




							'''simple docstring'''





							def __init__(						self	,					lowerCamelCase__	,					lowerCamelCase__=13	,					lowerCamelCase__=7	,					lowerCamelCase__=True	,					lowerCamelCase__=True	,					lowerCamelCase__=True	,					lowerCamelCase__=99	,					lowerCamelCase__=32	,					lowerCamelCase__=5	,					lowerCamelCase__=4	,					lowerCamelCase__=37	,					lowerCamelCase__="gelu"	,					lowerCamelCase__=0.1	,					lowerCamelCase__=0.1	,					lowerCamelCase__=512	,					lowerCamelCase__=16	,					lowerCamelCase__=2	,					lowerCamelCase__=0.02	,					lowerCamelCase__=3	,					lowerCamelCase__=4	,					lowerCamelCase__=None	,					)   ->	List[Any]:







								'''simple docstring'''




								snake_case__  :						str		=      parent
								snake_case__  :						Optional[int]		=      batch_size
								snake_case__  :						Optional[Any]		=      seq_length
								snake_case__  :						Union[str, Any]		=      is_training
								snake_case__  :						Tuple		=      use_input_mask
								snake_case__  :						Optional[int]		=      use_labels
								snake_case__  :						Dict		=      vocab_size
								snake_case__  :						Tuple		=      hidden_size
								snake_case__  :						List[str]		=      num_hidden_layers
								snake_case__  :						Dict		=      num_attention_heads
								snake_case__  :						int		=      intermediate_size
								snake_case__  :						List[Any]		=      hidden_act
								snake_case__  :						Dict		=      hidden_dropout_prob
								snake_case__  :						Optional[Any]		=      attention_probs_dropout_prob
								snake_case__  :						Union[str, Any]		=      max_position_embeddings
								snake_case__  :						List[Any]		=      type_vocab_size
								snake_case__  :						str		=      type_sequence_label_size
								snake_case__  :						str		=      initializer_range
								snake_case__  :						Tuple		=      num_labels
								snake_case__  :						List[str]		=      num_choices
								snake_case__  :						Optional[int]		=      scope




							def        UpperCAmelCase (						self)   ->	Optional[int]:







								'''simple docstring'''




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

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

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

								snake_case__  :						str		=      self.get_config()

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




							def        UpperCAmelCase (						self)   ->	List[str]:







								'''simple docstring'''




								snake_case__  :						Dict		=      ids_tensor([self.batch_size, self.seq_length]	,					self.vocab_size)

								snake_case__  :						List[str]		=      None
								if self.use_input_mask:
									snake_case__  :						Optional[int]		=      random_attention_mask([self.batch_size, self.seq_length])
								snake_case__  :						Tuple		=      self.get_config()

								return config, input_ids, input_mask




							def        UpperCAmelCase (						self)   ->	str:







								'''simple docstring'''




								return ErnieMConfig(
								    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	,					initializer_range=self.initializer_range	,					)




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	List[str]:







								'''simple docstring'''




								snake_case__  :						str		=      ErnieMModel(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								snake_case__  :						Tuple		=      model(_lowerCamelCase	,					return_dict=_lowerCamelCase)
								self.parent.assertEqual(result.last_hidden_state.shape	,					(self.batch_size, self.seq_length, self.hidden_size))




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Optional[Any]:







								'''simple docstring'''




								snake_case__  :						Dict		=      ErnieMForQuestionAnswering(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								snake_case__  :						str		=      model(
								    _lowerCamelCase	,					attention_mask=_lowerCamelCase	,					start_positions=_lowerCamelCase	,					end_positions=_lowerCamelCase	,					)
								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        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Union[str, Any]:







								'''simple docstring'''




								snake_case__  :						Any		=      ErnieMForInformationExtraction(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								snake_case__  :						Union[str, Any]		=      torch.ones_like(_lowerCamelCase	,					dtype=torch.floataa)
								snake_case__  :						Optional[Any]		=      model(
								    _lowerCamelCase	,					attention_mask=_lowerCamelCase	,					start_positions=_lowerCamelCase	,					end_positions=_lowerCamelCase	,					)
								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        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	str:







								'''simple docstring'''




								snake_case__  :						Optional[Any]		=      self.num_labels
								snake_case__  :						Tuple		=      ErnieMForSequenceClassification(_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								snake_case__  :						Union[str, Any]		=      model(_lowerCamelCase	,					attention_mask=_lowerCamelCase	,					labels=_lowerCamelCase)
								self.parent.assertEqual(result.logits.shape	,					(self.batch_size, self.num_labels))




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						Dict		=      self.num_labels
								snake_case__  :						List[Any]		=      ErnieMForTokenClassification(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								input_ids.to(_lowerCamelCase)
								input_mask.to(_lowerCamelCase)
								token_labels.to(_lowerCamelCase)

								snake_case__  :						Any		=      model(_lowerCamelCase	,					attention_mask=_lowerCamelCase	,					labels=_lowerCamelCase)

								self.parent.assertEqual(result.logits.shape	,					(self.batch_size, self.seq_length, self.num_labels))




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Optional[int]:







								'''simple docstring'''




								snake_case__  :						Optional[int]		=      self.num_choices
								snake_case__  :						Dict		=      ErnieMForMultipleChoice(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								snake_case__  :						Dict		=      input_ids.unsqueeze(1).expand(-1	,					self.num_choices	,					-1).contiguous()
								snake_case__  :						Dict		=      input_mask.unsqueeze(1).expand(-1	,					self.num_choices	,					-1).contiguous()
								snake_case__  :						Tuple		=      model(
								    _lowerCamelCase	,					attention_mask=_lowerCamelCase	,					labels=_lowerCamelCase	,					)
								self.parent.assertEqual(result.logits.shape	,					(self.batch_size, self.num_choices))







							def        UpperCAmelCase (						self)   ->	List[str]:







								'''simple docstring'''




								snake_case__  :						Optional[Any]		=      self.prepare_config_and_inputs()
								(
								    snake_case__
								)  :						Union[str, Any]		=      config_and_inputs
								snake_case__  :						Tuple		=      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict


@require_torch
class 			SCREAMING_SNAKE_CASE_  (   a__	,      a__	,      unittest.TestCase):




							'''simple docstring'''





							__magic_name__					:     List[Any]				  =  (
							    (
							        ErnieMModel,
							        ErnieMForMultipleChoice,
							        ErnieMForQuestionAnswering,
							        ErnieMForSequenceClassification,
							        ErnieMForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							__magic_name__					:     Union[str, Any]				  =  ()
							__magic_name__					:     str				  =  (
							    {
							        '''feature-extraction''': ErnieMModel,
							        '''question-answering''': ErnieMForQuestionAnswering,
							        '''text-classification''': ErnieMForSequenceClassification,
							        '''token-classification''': ErnieMForTokenClassification,
							        '''zero-shot''': ErnieMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							__magic_name__					:     Optional[Any]				  =  False




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Optional[int]:







								'''simple docstring'''




								if pipeline_test_casse_name == "QAPipelineTests":
									return True

								return False




							def        UpperCAmelCase (						self)   ->	List[str]:







								'''simple docstring'''




								snake_case__  :						List[str]		=      ErnieMModelTester(self)
								snake_case__  :						Union[str, Any]		=      ConfigTester(self	,					config_class=_lowerCamelCase	,					hidden_size=37)




							def        UpperCAmelCase (						self)   ->	Any:







								'''simple docstring'''




								self.config_tester.run_common_tests()




							def        UpperCAmelCase (						self)   ->	int:







								'''simple docstring'''




								snake_case__  :						Tuple		=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowerCamelCase)




							def        UpperCAmelCase (						self)   ->	Optional[Any]:







								'''simple docstring'''




								snake_case__  :						Tuple		=      self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
									snake_case__  :						Union[str, Any]		=      type
									self.model_tester.create_and_check_model(*_lowerCamelCase)




							def        UpperCAmelCase (						self)   ->	str:







								'''simple docstring'''




								snake_case__  :						List[str]		=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*_lowerCamelCase)




							def        UpperCAmelCase (						self)   ->	Union[str, Any]:







								'''simple docstring'''




								snake_case__  :						str		=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase)




							def        UpperCAmelCase (						self)   ->	Any:







								'''simple docstring'''




								snake_case__  :						List[str]		=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase)




							def        UpperCAmelCase (						self)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						str		=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_information_extraction(*_lowerCamelCase)




							def        UpperCAmelCase (						self)   ->	Tuple:







								'''simple docstring'''




								snake_case__  :						Optional[Any]		=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase)







							@slow
							def        UpperCAmelCase (						self)   ->	List[Any]:







								'''simple docstring'''




								for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									snake_case__  :						Any		=      ErnieMModel.from_pretrained(_lowerCamelCase)
									self.assertIsNotNone(_lowerCamelCase)


@require_torch
class 			SCREAMING_SNAKE_CASE_  (   unittest.TestCase):




							'''simple docstring'''





							@slow
							def        UpperCAmelCase (						self)   ->	Union[str, Any]:







								'''simple docstring'''




								snake_case__  :						Tuple		=      ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch")
								model.eval()
								snake_case__  :						str		=      torch.tensor([[0, 1, 2, 3, 4, 5]])
								snake_case__  :						Tuple		=      model(_lowerCamelCase)[0]

								# TODO Replace vocab size
								snake_case__  :						List[str]		=      768

								snake_case__  :						int		=      torch.Size((1, 6, hidden_size))
								self.assertEqual(output.shape	,					_lowerCamelCase)

								snake_case__  :						str		=      torch.tensor(
								    [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]])

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











'''simple docstring'''




from __future__ import annotations

from statistics import mean




def     snake_case__     (			lowerCamelCase__					: list[int]      ) -> list[int]:
					A_			:  Dict			=				2
					A_			:  List[str]			=				list(lowerCamelCase__      )
					A_			:  int			=				[0] * len(lowerCamelCase__      )
					A_			:  int			=				0
					while True:
										A_			:  List[str]			=				True
										for i, burst_time in enumerate(lowerCamelCase__      ):
															if rem_burst_times[i] > 0:
																				A_			:  Any			=				False
																				if rem_burst_times[i] > quantum:
																									t += quantum
																									rem_burst_times[i] -= quantum
																				else:
																									t += rem_burst_times[i]
																									A_			:  Any			=				t - burst_time
																									A_			:  Optional[int]			=				0
										if done is True:
															return waiting_times




def     snake_case__     (			lowerCamelCase__					: list[int]      ,		lowerCamelCase__					: list[int]      ) -> list[int]:
					return [burst + waiting for burst, waiting in zip(lowerCamelCase__      ,		lowerCamelCase__      )]


if __name__ == "__main__":
							snake_case__ 							=    [3, 5, 7]
							snake_case__ 							=    calculate_waiting_times(burst_times)
							snake_case__ 							=    calculate_turn_around_times(burst_times, waiting_times)
							print("""Process ID \tBurst Time \tWaiting Time \tTurnaround Time""")
							for i, burst_time in enumerate(burst_times):
														print(
														    F'  {i + 1}\t\t  {burst_time}\t\t  {waiting_times[i]}\t\t  '
														    F'{turn_around_times[i]}'
														)
							print(F'\nAverage waiting time = {mean(waiting_times):.5f}')
							print(F'Average turn around time = {mean(turn_around_times):.5f}')



