


"""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        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase=None			):





						"""simple docstring"""

						UpperCAmelCase__												=     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.
												UpperCAmelCase__												=     weight.view(torch.intaa			)
												UpperCAmelCase__												=     '''bfloat16'''
						UpperCAmelCase__												=     weight.cpu().numpy()
						UpperCAmelCase__												=     os.path.join(_UpperCamelCase						,      F"""{weight_name}.dat"""			)
						if index is not None:
												if dtype is None:
																		UpperCAmelCase__												=     str(array.dtype			)
												UpperCAmelCase__												=     {'''dtype''': dtype, '''shape''': list(array.shape			)}
						if array.ndim == 0:
												UpperCAmelCase__												=     array[None]
						UpperCAmelCase__												=     np.memmap(_UpperCamelCase						,      dtype=array.dtype						,      mode='''w+'''						,      shape=array.shape			)
						UpperCAmelCase__												=     array[:]
						file_array.flush()
						return index






def        a__			(							_UpperCamelCase						,      _UpperCamelCase			):





						"""simple docstring"""

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

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

						UpperCAmelCase__												=     np.memmap(_UpperCamelCase						,      dtype=_UpperCamelCase						,      shape=_UpperCamelCase						,      mode='''r'''			)

						if len(weight_info['''shape''']			) == 0:
												UpperCAmelCase__												=     weight[0]
						UpperCAmelCase__												=     torch.tensor(_UpperCamelCase			)
						if weight_info["dtype"] == "bfloat16":
												UpperCAmelCase__												=     weight.view(torch.bfloataa			)

						return weight






def        a__			(							_UpperCamelCase						,      _UpperCamelCase			):





						"""simple docstring"""

						if index is None or len(_UpperCamelCase			) == 0:
												# Nothing to save
												return

						UpperCAmelCase__												=     os.path.join(_UpperCamelCase						,      '''index.json'''			)
						if os.path.isfile(_UpperCamelCase			):
												with open(_UpperCamelCase						,      '''r'''						,      encoding='''utf-8'''			) as f:
																		UpperCAmelCase__												=     json.load(_UpperCamelCase			)
						else:
												UpperCAmelCase__												=     {}
						current_index.update(_UpperCamelCase			)

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






def        a__			(							_UpperCamelCase						,      _UpperCamelCase			):





						"""simple docstring"""

						os.makedirs(_UpperCamelCase						,      exist_ok=_UpperCamelCase			)
						UpperCAmelCase__												=     {}
						for name, parameter in state_dict.items():
												UpperCAmelCase__												=     offload_weight(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      index=_UpperCamelCase			)

						# Update index
						save_offload_index(_UpperCamelCase						,      _UpperCamelCase			)
class 				lowerCamelCase							(      __SCREAMING_SNAKE_CASE			):


							"""simple docstring"""






							def __init__(    self			:  Dict		,			lowerCamelCase_			:  Mapping		,			lowerCamelCase_			:  str					):
													UpperCAmelCase__												=     dataset
													UpperCAmelCase__												=     prefix






							def __getitem__(    self			:  List[Any]		,			lowerCamelCase_			:  str					):
													return self.dataset[f"""{self.prefix}{key}"""]






							def __iter__(    self			:  Tuple					):
													return iter([key for key in self.dataset if key.startswith(self.prefix					)]					)






							def __len__(    self			:  Tuple					):
													return len(self.dataset					)




class 				lowerCamelCase							(      __SCREAMING_SNAKE_CASE			):


							"""simple docstring"""






							def __init__(    self			:  Any		,			lowerCamelCase_			:  Dict[str, torch.Tensor] = None		,			lowerCamelCase_			:  Optional[Union[str, os.PathLike]] = None		,			lowerCamelCase_			:  Mapping = None		,			lowerCamelCase_			:  Tuple=None		,			):
													if state_dict is None and save_folder is None:
																			raise ValueError('''Need either a `state_dict` or a `save_folder` containing offloaded weights.'''					)

													UpperCAmelCase__												=     {} if state_dict is None else state_dict
													UpperCAmelCase__												=     save_folder
													if index is None and save_folder is not None:
																			with open(os.path.join(lowerCamelCase_		,			'''index.json'''					)					) as f:
																									UpperCAmelCase__												=     json.load(lowerCamelCase_					)
													UpperCAmelCase__												=     {} if index is None else index
													UpperCAmelCase__												=     list(self.state_dict.keys()					)
													self.all_keys.extend([key for key in self.index if key not in self.all_keys]					)
													UpperCAmelCase__												=     device






							def __getitem__(    self			:  List[Any]		,			lowerCamelCase_			:  str					):
													# State dict gets priority
													if key in self.state_dict:
																			return self.state_dict[key]
													UpperCAmelCase__												=     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

																			UpperCAmelCase__												=     '''cpu''' if self.device is None else self.device
																			with safe_open(weight_info['''safetensors_file''']		,			framework='''pt'''		,			device=lowerCamelCase_					) as f:
																									UpperCAmelCase__												=     f.get_tensor(weight_info.get('''weight_name'''		,			lowerCamelCase_					)					)

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

													UpperCAmelCase__												=     os.path.join(self.save_folder		,			f"""{key}.dat"""					)
													return load_offloaded_weight(lowerCamelCase_		,			lowerCamelCase_					)






							def __iter__(    self			:  Dict					):
													return iter(self.all_keys					)






							def __len__(    self			:  Tuple					):
													return len(self.all_keys					)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase			):





						"""simple docstring"""

						UpperCAmelCase__												=     {}
						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        a__			(							_UpperCamelCase			):





						"""simple docstring"""

						UpperCAmelCase__												=     sorted(_UpperCamelCase			)
						UpperCAmelCase__												=     len(_UpperCamelCase			)
						UpperCAmelCase__												=     length >> 1
						return (
						    (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
						    if length % 2 == 0
						    else sorted_list[mid_index]
						)






def        a__			(							):





						"""simple docstring"""

						import doctest

						doctest.testmod()


if __name__ == "__main__":
						main()