


'''simple docstring'''

import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


a_						:       Any      			=				1e-4

if is_torch_available():
     import torch

     from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
     from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder





@require_torch
class   snake_case					:





  """simple docstring"""




  def __init__(					self    ,						UpperCamelCase    ,						UpperCamelCase=16    ,						UpperCamelCase=13    ,						UpperCamelCase=7    ,						UpperCamelCase=14    ,						UpperCamelCase=10    ,						UpperCamelCase=19    ,						UpperCamelCase=5    ,						UpperCamelCase=4    ,						UpperCamelCase=True    ,						UpperCamelCase=16    ,						UpperCamelCase=2    ,						UpperCamelCase=4    ,						UpperCamelCase=4    ,						UpperCamelCase="gelu"    ,						UpperCamelCase=0.1    ,						UpperCamelCase=0.1    ,						UpperCamelCase=[1, 2, 3, 4, 5]    ,						UpperCamelCase=25    ,						UpperCamelCase=5    ,						):







   """simple docstring"""
   lowerCamelCase_  				=						d_model
   lowerCamelCase_  				=						parent
   lowerCamelCase_  				=						batch_size
   lowerCamelCase_  				=						prediction_length
   lowerCamelCase_  				=						context_length
   lowerCamelCase_  				=						cardinality
   lowerCamelCase_  				=						num_time_features
   lowerCamelCase_  				=						lags_sequence
   lowerCamelCase_  				=						embedding_dimension
   lowerCamelCase_  				=						is_training
   lowerCamelCase_  				=						hidden_size
   lowerCamelCase_  				=						num_hidden_layers
   lowerCamelCase_  				=						num_attention_heads
   lowerCamelCase_  				=						intermediate_size
   lowerCamelCase_  				=						hidden_act
   lowerCamelCase_  				=						hidden_dropout_prob
   lowerCamelCase_  				=						attention_probs_dropout_prob

   lowerCamelCase_  				=						context_length
   lowerCamelCase_  				=						prediction_length + label_length
   lowerCamelCase_  				=						label_length

   lowerCamelCase_  				=						moving_average
   lowerCamelCase_  				=						autocorrelation_factor






  def 						snake_case	(					self  ):







   """simple docstring"""
   return AutoformerConfig(
       d_model=self.d_model    ,						encoder_layers=self.num_hidden_layers    ,						decoder_layers=self.num_hidden_layers    ,						encoder_attention_heads=self.num_attention_heads    ,						decoder_attention_heads=self.num_attention_heads    ,						encoder_ffn_dim=self.intermediate_size    ,						decoder_ffn_dim=self.intermediate_size    ,						dropout=self.hidden_dropout_prob    ,						attention_dropout=self.attention_probs_dropout_prob    ,						prediction_length=self.prediction_length    ,						context_length=self.context_length    ,						label_length=self.label_length    ,						lags_sequence=self.lags_sequence    ,						num_time_features=self.num_time_features    ,						num_static_categorical_features=1    ,						cardinality=[self.cardinality]    ,						embedding_dimension=[self.embedding_dimension]    ,						moving_average=self.moving_average    ,						)






  def 						snake_case	(					self    ,						UpperCamelCase  ):







   """simple docstring"""
   lowerCamelCase_  				=						config.context_length + max(config.lags_sequence  )

   lowerCamelCase_  				=						ids_tensor([self.batch_size, 1]    ,						config.cardinality[0]  )
   lowerCamelCase_  				=						floats_tensor([self.batch_size, _past_length, config.num_time_features]  )
   lowerCamelCase_  				=						floats_tensor([self.batch_size, _past_length]  )
   lowerCamelCase_  				=						floats_tensor([self.batch_size, _past_length]  ) > 0.5

   # decoder inputs
   lowerCamelCase_  				=						floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]  )
   lowerCamelCase_  				=						floats_tensor([self.batch_size, config.prediction_length]  )

   lowerCamelCase_  				=						{
       "past_values": past_values,
       "static_categorical_features": static_categorical_features,
       "past_time_features": past_time_features,
       "past_observed_mask": past_observed_mask,
       "future_time_features": future_time_features,
       "future_values": future_values,
   }
   return inputs_dict






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.get_config()
   lowerCamelCase_  				=						self.prepare_autoformer_inputs_dict(UpperCamelCase  )
   return config, inputs_dict






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_				,lowerCamelCase_  				=						self.prepare_config_and_inputs()
   return config, inputs_dict







  def 						snake_case	(					self    ,						UpperCamelCase    ,						UpperCamelCase  ):







   """simple docstring"""
   lowerCamelCase_  				=						AutoformerModel(config=UpperCamelCase  ).to(UpperCamelCase  ).eval()
   lowerCamelCase_  				=						model(**UpperCamelCase  )

   lowerCamelCase_  				=						outputs.encoder_last_hidden_state
   lowerCamelCase_  				=						outputs.last_hidden_state

   with tempfile.TemporaryDirectory() as tmpdirname:
    lowerCamelCase_  				=						model.get_encoder()
    encoder.save_pretrained(UpperCamelCase  )
    lowerCamelCase_  				=						AutoformerEncoder.from_pretrained(UpperCamelCase  ).to(UpperCamelCase  )

   lowerCamelCase_				,lowerCamelCase_				,lowerCamelCase_				,lowerCamelCase_				,lowerCamelCase_  				=						model.create_network_inputs(**UpperCamelCase  )
   lowerCamelCase_				,lowerCamelCase_  				=						model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]  )

   lowerCamelCase_  				=						torch.cat(
       (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])    ,						dim=-1    ,						)
   lowerCamelCase_  				=						encoder(inputs_embeds=UpperCamelCase  )[0]
   self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3  )

   lowerCamelCase_  				=						(
       torch.mean(transformer_inputs[:, : config.context_length, ...]    ,						dim=1  )
       .unsqueeze(1  )
       .repeat(1    ,						config.prediction_length    ,						1  )
   )
   lowerCamelCase_  				=						torch.zeros(
       [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]    ,						device=enc_input.device    ,						)

   lowerCamelCase_  				=						torch.cat(
       (
           torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)    ,						dim=1  ),
           feature[:, config.context_length - config.label_length :, ...],
       )    ,						dim=-1    ,						)
   lowerCamelCase_  				=						torch.cat(
       (
           torch.cat((trend_input[:, -config.label_length :, ...], mean)    ,						dim=1  ),
           feature[:, config.context_length - config.label_length :, ...],
       )    ,						dim=-1    ,						)

   with tempfile.TemporaryDirectory() as tmpdirname:
    lowerCamelCase_  				=						model.get_decoder()
    decoder.save_pretrained(UpperCamelCase  )
    lowerCamelCase_  				=						AutoformerDecoder.from_pretrained(UpperCamelCase  ).to(UpperCamelCase  )

   lowerCamelCase_  				=						decoder(
       trend=UpperCamelCase    ,						inputs_embeds=UpperCamelCase    ,						encoder_hidden_states=UpperCamelCase    ,						)[0]

   self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3  )





@require_torch
class   snake_case					(					lowercase    ,		lowercase    ,		unittest.TestCase    ):





  """simple docstring"""




  _lowerCamelCase						=						(AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
  _lowerCamelCase						=						(AutoformerForPrediction,) if is_torch_available() else ()
  _lowerCamelCase						=						{"feature-extraction": AutoformerModel} if is_torch_available() else {}
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						AutoformerModelTester(self  )
   lowerCamelCase_  				=						ConfigTester(self    ,						config_class=UpperCamelCase    ,						has_text_modality=UpperCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   self.config_tester.run_common_tests()






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_				,lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs()
   for model_class in self.all_model_classes:
    lowerCamelCase_  				=						model_class(UpperCamelCase  )

    with tempfile.TemporaryDirectory() as tmpdirname:
     model.save_pretrained(UpperCamelCase  )
     lowerCamelCase_				,lowerCamelCase_  				=						model_class.from_pretrained(UpperCamelCase    ,						output_loading_info=UpperCamelCase  )
    self.assertEqual(info["missing_keys"]    ,						[]  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs_for_common()
   self.model_tester.check_encoder_decoder_model_standalone(*UpperCamelCase  )






  @unittest.skip(reason="Model has no tokens embeddings"  )
  def 						snake_case	(					self  ):







   """simple docstring"""
   pass






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						inspect.signature(getattr(UpperCamelCase    ,						"forward"  )  )
   # The main input is the name of the argument after `self`
   lowerCamelCase_  				=						list(model_signature.parameters.keys()  )[1]
   self.assertEqual(AutoformerModel.main_input_name    ,						UpperCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_				,lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    lowerCamelCase_  				=						model_class(UpperCamelCase  )
    lowerCamelCase_  				=						inspect.signature(model.forward  )
    # signature.parameters is an OrderedDict => so arg_names order is deterministic
    lowerCamelCase_  				=						[*signature.parameters.keys()]

    lowerCamelCase_  				=						[
        "past_values",
        "past_time_features",
        "past_observed_mask",
        "static_categorical_features",
        "static_real_features",
        "future_values",
        "future_time_features",
    ]

    if model.__class__.__name__ in ["AutoformerForPrediction"]:
     expected_arg_names.append("future_observed_mask"  )

    expected_arg_names.extend(
        [
            "decoder_attention_mask",
            "head_mask",
            "decoder_head_mask",
            "cross_attn_head_mask",
            "encoder_outputs",
            "past_key_values",
            "output_hidden_states",
            "output_attentions",
            "use_cache",
            "return_dict",
        ]  )

    self.assertListEqual(arg_names[: len(UpperCamelCase  )]    ,						UpperCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_				,lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs_for_common()
   lowerCamelCase_  				=						True

   lowerCamelCase_  				=						getattr(self.model_tester    ,						"seq_length"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						getattr(self.model_tester    ,						"decoder_seq_length"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						getattr(self.model_tester    ,						"encoder_seq_length"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						getattr(self.model_tester    ,						"d_model"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						getattr(self.model_tester    ,						"num_attention_heads"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						d_model // num_attention_heads

   for model_class in self.all_model_classes:
    lowerCamelCase_  				=						True
    lowerCamelCase_  				=						False
    lowerCamelCase_  				=						True
    lowerCamelCase_  				=						model_class(UpperCamelCase  )
    model.to(UpperCamelCase  )
    model.eval()
    with torch.no_grad():
     lowerCamelCase_  				=						model(**self._prepare_for_class(UpperCamelCase    ,						UpperCamelCase  )  )
    lowerCamelCase_  				=						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
    self.assertEqual(len(UpperCamelCase  )    ,						self.model_tester.num_hidden_layers  )

    # check that output_attentions also work using config
    del inputs_dict["output_attentions"]
    lowerCamelCase_  				=						True
    lowerCamelCase_  				=						model_class(UpperCamelCase  )
    model.to(UpperCamelCase  )
    model.eval()
    with torch.no_grad():
     lowerCamelCase_  				=						model(**self._prepare_for_class(UpperCamelCase    ,						UpperCamelCase  )  )
    lowerCamelCase_  				=						outputs.encoder_attentions
    self.assertEqual(len(UpperCamelCase  )    ,						self.model_tester.num_hidden_layers  )

    self.assertListEqual(
        list(attentions[0].shape[-3:]  )    ,						[self.model_tester.num_attention_heads, encoder_seq_length, dim]    ,						)
    lowerCamelCase_  				=						len(UpperCamelCase  )

    lowerCamelCase_  				=						7

    if "last_hidden_state" in outputs:
     correct_outlen += 1

    if "trend" in outputs:
     correct_outlen += 1

    if "past_key_values" in outputs:
     correct_outlen += 1  # past_key_values have been returned

    if "loss" in outputs:
     correct_outlen += 1

    if "params" in outputs:
     correct_outlen += 1

    self.assertEqual(UpperCamelCase    ,						UpperCamelCase  )

    # decoder attentions
    lowerCamelCase_  				=						outputs.decoder_attentions
    self.assertIsInstance(UpperCamelCase    ,						(list, tuple)  )
    self.assertEqual(len(UpperCamelCase  )    ,						self.model_tester.num_hidden_layers  )
    self.assertListEqual(
        list(decoder_attentions[0].shape[-3:]  )    ,						[self.model_tester.num_attention_heads, decoder_seq_length, dim]    ,						)

    # cross attentions
    lowerCamelCase_  				=						outputs.cross_attentions
    self.assertIsInstance(UpperCamelCase    ,						(list, tuple)  )
    self.assertEqual(len(UpperCamelCase  )    ,						self.model_tester.num_hidden_layers  )
    self.assertListEqual(
        list(cross_attentions[0].shape[-3:]  )    ,						[self.model_tester.num_attention_heads, decoder_seq_length, dim]    ,						)

   # Check attention is always last and order is fine
   lowerCamelCase_  				=						True
   lowerCamelCase_  				=						True
   lowerCamelCase_  				=						model_class(UpperCamelCase  )
   model.to(UpperCamelCase  )
   model.eval()
   with torch.no_grad():
    lowerCamelCase_  				=						model(**self._prepare_for_class(UpperCamelCase    ,						UpperCamelCase  )  )

   self.assertEqual(out_len + 2    ,						len(UpperCamelCase  )  )

   lowerCamelCase_  				=						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

   self.assertEqual(len(UpperCamelCase  )    ,						self.model_tester.num_hidden_layers  )
   self.assertListEqual(
       list(self_attentions[0].shape[-3:]  )    ,						[self.model_tester.num_attention_heads, encoder_seq_length, dim]    ,						)







  @is_flaky()
  def 						snake_case	(					self  ):







   """simple docstring"""
   super().test_retain_grad_hidden_states_attentions()





def __snake_case      (   UpperCAmelCase_		:		Union[str, Any]="train-batch.pt"	):
 lowerCamelCase_  				=						hf_hub_download(repo_id="hf-internal-testing/tourism-monthly-batch"      ,       filename=UpperCAmelCase_      ,       repo_type="dataset"	)
 lowerCamelCase_  				=						torch.load(UpperCAmelCase_      ,       map_location=UpperCAmelCase_	)
 return batch







@require_torch
@slow
class   snake_case					(					unittest.TestCase    ):





  """simple docstring"""




  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						AutoformerModel.from_pretrained("huggingface/autoformer-tourism-monthly"  ).to(UpperCamelCase  )
   lowerCamelCase_  				=						prepare_batch()

   with torch.no_grad():
    lowerCamelCase_  				=						model(
        past_values=batch["past_values"]    ,						past_time_features=batch["past_time_features"]    ,						past_observed_mask=batch["past_observed_mask"]    ,						static_categorical_features=batch["static_categorical_features"]    ,						future_values=batch["future_values"]    ,						future_time_features=batch["future_time_features"]    ,						)[0]

   lowerCamelCase_  				=						torch.Size(
       (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)  )
   self.assertEqual(output.shape    ,						UpperCamelCase  )

   lowerCamelCase_  				=						torch.tensor(
       [[0.3_593, -1.3_398, 0.6_330], [0.2_279, 1.5_396, -0.1_792], [0.0_450, 1.3_225, -0.2_335]]    ,						device=UpperCamelCase  )
   self.assertTrue(torch.allclose(output[0, :3, :3]    ,						UpperCamelCase    ,						atol=UpperCamelCase  )  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly"  ).to(UpperCamelCase  )
   lowerCamelCase_  				=						prepare_batch("val-batch.pt"  )
   with torch.no_grad():
    lowerCamelCase_  				=						model(
        past_values=batch["past_values"]    ,						past_time_features=batch["past_time_features"]    ,						past_observed_mask=batch["past_observed_mask"]    ,						static_categorical_features=batch["static_categorical_features"]    ,						).encoder_last_hidden_state
   lowerCamelCase_  				=						torch.Size((64, model.config.context_length, model.config.d_model)  )
   self.assertEqual(output.shape    ,						UpperCamelCase  )

   lowerCamelCase_  				=						torch.tensor(
       [[-0.0_734, -0.9_036, 0.8_358], [4.7_186, 2.4_113, 1.9_581], [1.7_953, 2.3_558, 1.2_970]]    ,						device=UpperCamelCase  )
   self.assertTrue(torch.allclose(output[0, :3, :3]    ,						UpperCamelCase    ,						atol=UpperCamelCase  )  )







  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly"  ).to(UpperCamelCase  )
   lowerCamelCase_  				=						prepare_batch("val-batch.pt"  )
   with torch.no_grad():
    lowerCamelCase_  				=						model.generate(
        static_categorical_features=batch["static_categorical_features"]    ,						past_time_features=batch["past_time_features"]    ,						past_values=batch["past_values"]    ,						future_time_features=batch["future_time_features"]    ,						past_observed_mask=batch["past_observed_mask"]    ,						)
   lowerCamelCase_  				=						torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)  )
   self.assertEqual(outputs.sequences.shape    ,						UpperCamelCase  )

   lowerCamelCase_  				=						torch.tensor([3_130.6_763, 4_056.5_293, 7_053.0_786]    ,						device=UpperCamelCase  )
   lowerCamelCase_  				=						outputs.sequences.mean(dim=1  )
   self.assertTrue(torch.allclose(mean_prediction[0, -3:]    ,						UpperCamelCase    ,						rtol=1e-1  )  )







'''simple docstring'''

from __future__ import annotations

import pandas as pd





def __snake_case      (   UpperCAmelCase_		:		list[int]      ,       UpperCAmelCase_		:		list[int]      ,       UpperCAmelCase_		:		int	):
 lowerCamelCase_  				=						[0] * no_of_processes
 lowerCamelCase_  				=						[0] * no_of_processes
 # Copy the burst time into remaining_time[]
 for i in range(UpperCAmelCase_	):
  lowerCamelCase_  				=						burst_time[i]

 lowerCamelCase_  				=						0
 lowerCamelCase_  				=						0
 lowerCamelCase_  				=						999999999
 lowerCamelCase_  				=						0
 lowerCamelCase_  				=						False

 # Process until all processes are completed
 while complete != no_of_processes:
  for j in range(UpperCAmelCase_	):
   if arrival_time[j] <= increment_time and remaining_time[j] > 0:
    if remaining_time[j] < minm:
     lowerCamelCase_  				=						remaining_time[j]
     lowerCamelCase_  				=						j
     lowerCamelCase_  				=						True

  if not check:
   increment_time += 1
   continue
  remaining_time[short] -= 1

  lowerCamelCase_  				=						remaining_time[short]
  if minm == 0:
   lowerCamelCase_  				=						999999999

  if remaining_time[short] == 0:
   complete += 1
   lowerCamelCase_  				=						False

   # Find finish time of current process
   lowerCamelCase_  				=						increment_time + 1

   # Calculate waiting time
   lowerCamelCase_  				=						finish_time - arrival_time[short]
   lowerCamelCase_  				=						finar - burst_time[short]

   if waiting_time[short] < 0:
    lowerCamelCase_  				=						0

        # Increment time
  increment_time += 1
 return waiting_time





def __snake_case      (   UpperCAmelCase_		:		list[int]      ,       UpperCAmelCase_		:		int      ,       UpperCAmelCase_		:		list[int]	):
 lowerCamelCase_  				=						[0] * no_of_processes
 for i in range(UpperCAmelCase_	):
  lowerCamelCase_  				=						burst_time[i] + waiting_time[i]
 return turn_around_time





def __snake_case      (   UpperCAmelCase_		:		list[int]      ,       UpperCAmelCase_		:		list[int]      ,       UpperCAmelCase_		:		int	):
 lowerCamelCase_  				=						0
 lowerCamelCase_  				=						0
 for i in range(UpperCAmelCase_	):
  lowerCamelCase_  				=						total_waiting_time + waiting_time[i]
  lowerCamelCase_  				=						total_turn_around_time + turn_around_time[i]
 print(F'''Average waiting time = {total_waiting_time / no_of_processes:.5f}'''	)
 print("Average turn around time ="      ,       total_turn_around_time / no_of_processes	)


if __name__ == "__main__":
     print("""Enter how many process you want to analyze""")
     a_						:       Dict      			=				int(input())
     a_						:       Any      			=				[0] * no_of_processes
     a_						:       Optional[int]      			=				[0] * no_of_processes
     a_						:       Tuple      			=				list(range(1, no_of_processes + 1))

     for i in range(no_of_processes):
          print("""Enter the arrival time and burst time for process:--""" + str(i + 1))
          a_			,     a_						:       str      			=				map(int, input().split())

     a_						:       List[Any]      			=				calculate_waitingtime(arrival_time, burst_time, no_of_processes)

     a_						:       int      			=				burst_time
     a_						:       Union[str, Any]      			=				no_of_processes
     a_						:       Optional[int]      			=				waiting_time
     a_						:       Any      			=				calculate_turnaroundtime(bt, n, wt)

     calculate_average_times(waiting_time, turn_around_time, no_of_processes)

     a_						:       Optional[int]      			=				pd.DataFrame(
         list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)),
         columns=[
             """Process""",
             """BurstTime""",
             """ArrivalTime""",
             """WaitingTime""",
             """TurnAroundTime""",
         ],
     )

     # Printing the dataFrame
     pd.set_option("""display.max_rows""", fcfs.shape[0] + 1)
     print(fcfs)



