




"""simple docstring"""




import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available







def       __UpperCAmelCase      (				__UpperCamelCase					,							__UpperCamelCase					,							__UpperCamelCase					,							__UpperCamelCase=None					):
			__lowercase	:       Tuple				=   None
			# Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
			if str(weight.dtype					) == "torch.bfloat16":
						# Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
						__lowercase	:       Any				=   weight.view(torch.intaa					)
						__lowercase	:       str				=   '''bfloat16'''
			__lowercase	:       List[Any]				=   weight.cpu().numpy()
			__lowercase	:       Tuple				=   os.path.join(__UpperCamelCase					,							f"""{weight_name}.dat"""					)
			if index is not None:
						if dtype is None:
									__lowercase	:       List[str]				=   str(array.dtype					)
						__lowercase	:       str				=   {'''dtype''': dtype, '''shape''': list(array.shape					)}
			if array.ndim == 0:
						__lowercase	:       List[Any]				=   array[None]
			__lowercase	:       Optional[Any]				=   np.memmap(__UpperCamelCase					,							dtype=array.dtype					,							mode='''w+'''					,							shape=array.shape					)
			__lowercase	:       Tuple				=   array[:]
			file_array.flush()
			return index







def       __UpperCAmelCase      (				__UpperCamelCase					,							__UpperCamelCase					):
			__lowercase	:       List[str]				=   tuple(weight_info['''shape''']					)
			if shape == ():
						# NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
						__lowercase	:       List[str]				=   (1,)

			__lowercase	:       List[Any]				=   weight_info['''dtype''']
			if dtype == "bfloat16":
						# NumPy does not support bfloat16 so this was saved as a int16
						__lowercase	:       Optional[Any]				=   '''int16'''

			__lowercase	:       Dict				=   np.memmap(__UpperCamelCase					,							dtype=__UpperCamelCase					,							shape=__UpperCamelCase					,							mode='''r'''					)

			if len(weight_info['''shape''']					) == 0:
						__lowercase	:       Any				=   weight[0]
			__lowercase	:       Dict				=   torch.tensor(__UpperCamelCase					)
			if weight_info["dtype"] == "bfloat16":
						__lowercase	:       Optional[Any]				=   weight.view(torch.bfloataa					)

			return weight







def       __UpperCAmelCase      (				__UpperCamelCase					,							__UpperCamelCase					):
			if index is None or len(__UpperCamelCase					) == 0:
						# Nothing to save
						return

			__lowercase	:       List[Any]				=   os.path.join(__UpperCamelCase					,							'''index.json'''					)
			if os.path.isfile(__UpperCamelCase					):
						with open(__UpperCamelCase					,							'''r'''					,							encoding='''utf-8'''					) as f:
									__lowercase	:       List[str]				=   json.load(__UpperCamelCase					)
			else:
						__lowercase	:       Optional[int]				=   {}
			current_index.update(__UpperCamelCase					)

			with open(__UpperCamelCase					,							'''w'''					,							encoding='''utf-8'''					) as f:
						json.dump(__UpperCamelCase					,							__UpperCamelCase					,							indent=2					)







def       __UpperCAmelCase      (				__UpperCamelCase					,							__UpperCamelCase					):
			os.makedirs(__UpperCamelCase					,							exist_ok=__UpperCamelCase					)
			__lowercase	:       Tuple				=   {}
			for name, parameter in state_dict.items():
						__lowercase	:       Union[str, Any]				=   offload_weight(__UpperCamelCase					,							__UpperCamelCase					,							__UpperCamelCase					,							index=__UpperCamelCase					)

			# Update index
			save_offload_index(__UpperCamelCase					,							__UpperCamelCase					)

class        UpperCAmelCase_  (	snake_case						):
		def __init__(		self							,		UpperCamelCase_							,		UpperCamelCase_   )		->   Tuple:
					__lowercase	:       int				=   dataset
					__lowercase	:       Any				=   prefix
		def __getitem__(		self							,		UpperCamelCase_   )		->   int:
					return self.dataset[F"""{self.prefix}{key}"""]
		def __iter__(		self   )		->   int:
					return iter([key for key in self.dataset if key.startswith(self.prefix   )]   )
		def __len__(		self   )		->   List[Any]:
					return len(self.dataset   )


class        UpperCAmelCase_  (	snake_case						):
		def __init__(		self							,		UpperCamelCase_ = None							,		UpperCamelCase_ = None							,		UpperCamelCase_ = None							,		UpperCamelCase_=None							,		)		->   Tuple:
					if state_dict is None and save_folder is None:
								raise ValueError('''Need either a `state_dict` or a `save_folder` containing offloaded weights.'''   )

					__lowercase	:       Any				=   {} if state_dict is None else state_dict
					__lowercase	:       Any				=   save_folder
					if index is None and save_folder is not None:
								with open(os.path.join(UpperCamelCase_							,		'''index.json'''   )   ) as f:
											__lowercase	:       str				=   json.load(UpperCamelCase_   )
					__lowercase	:       int				=   {} if index is None else index
					__lowercase	:       Optional[int]				=   list(self.state_dict.keys()   )
					self.all_keys.extend([key for key in self.index if key not in self.all_keys]   )
					__lowercase	:       Optional[Any]				=   device
		def __getitem__(		self							,		UpperCamelCase_   )		->   Dict:
					# State dict gets priority
					if key in self.state_dict:
								return self.state_dict[key]
					__lowercase	:       int				=   self.index[key]
					if weight_info.get('''safetensors_file'''   ) is not None:
								if not is_safetensors_available():
											raise ImportError('''These offloaded weights require the use of safetensors: `pip install safetensors`.'''   )

								from safetensors import safe_open

								__lowercase	:       Dict				=   '''cpu''' if self.device is None else self.device
								with safe_open(weight_info['''safetensors_file''']							,		framework='''pt'''							,		device=UpperCamelCase_   ) as f:
											__lowercase	:       Optional[int]				=   f.get_tensor(weight_info.get('''weight_name'''							,		UpperCamelCase_   )   )

								if "dtype" in weight_info:
											return tensor.to(getattr(UpperCamelCase_							,		weight_info['''dtype''']   )   )
								else:
											return tensor

					__lowercase	:       Optional[int]				=   os.path.join(self.save_folder							,		F"""{key}.dat"""   )
					return load_offloaded_weight(UpperCamelCase_							,		UpperCamelCase_   )
		def __iter__(		self   )		->   List[Any]:
					return iter(self.all_keys   )
		def __len__(		self   )		->   Tuple:
					return len(self.all_keys   )







def       __UpperCAmelCase      (				__UpperCamelCase					,							__UpperCamelCase					):
			__lowercase	:       Tuple				=   {}
			for module_name in submodule_names:
						# We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
						# submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
						result.update(
						    {
						        key: param
						        for key, param in state_dict.items()
						        if key == module_name or key.startswith(module_name + '''.'''					)
						    }					)
			return result









"""simple docstring"""




from __future__ import annotations







def       __UpperCAmelCase      (				__UpperCamelCase					):
			__lowercase	:       List[Any]				=   [0] * len(__UpperCamelCase					)
			for i in range(1					,							len(__UpperCamelCase					)					):
						__lowercase	:       Optional[Any]				=   duration_times[i - 1] + waiting_times[i - 1]
			return waiting_times







def       __UpperCAmelCase      (				__UpperCamelCase					,							__UpperCamelCase					):
			return [
			    duration_time + waiting_times[i]
			    for i, duration_time in enumerate(__UpperCamelCase					)
			]







def       __UpperCAmelCase      (				__UpperCamelCase					):
			return sum(__UpperCamelCase					) / len(__UpperCamelCase					)







def       __UpperCAmelCase      (				__UpperCamelCase					):
			return sum(__UpperCamelCase					) / len(__UpperCamelCase					)


if __name__ == "__main__":
			# process id's
			a_            =   [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
			a_            =   [1_9, 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
			a_            =   calculate_waiting_times(duration_times)
			a_            =   calculate_turnaround_times(duration_times, waiting_times)

			# get the average times
			a_            =   calculate_average_waiting_time(waiting_times)
			a_            =   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}")



