




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





def 							A__   (snake_case      :		Optional[Any]		,  snake_case      :		str							)    ->	List[Any]:
     __UpperCamelCase				:  Optional[Any]								=			tuple(weight_info["""shape"""]							)
     if shape == ():
          # NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
          __UpperCamelCase				:  Tuple								=			(1,)

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

     __UpperCamelCase				:  List[str]								=			np.memmap(snake_case		,  dtype=snake_case		,  shape=snake_case		,  mode="""r"""							)

     if len(weight_info["""shape"""]							) == 0:
          __UpperCamelCase				:  int								=			weight[0]
     __UpperCamelCase				:  Any								=			torch.tensor(snake_case							)
     if weight_info["dtype"] == "bfloat16":
          __UpperCamelCase				:  int								=			weight.view(torch.bfloataa							)

     return weight





def 							A__   (snake_case      :		Dict		,  snake_case      :		List[str]							)    ->	Optional[Any]:
     if index is None or len(snake_case							) == 0:
          # Nothing to save
          return

     __UpperCamelCase				:  Dict								=			os.path.join(snake_case		,  """index.json"""							)
     if os.path.isfile(snake_case							):
          with open(snake_case		,  """r"""		,  encoding="""utf-8"""							) as f:
               __UpperCamelCase				:  Any								=			json.load(snake_case							)
     else:
          __UpperCamelCase				:  Union[str, Any]								=			{}
     current_index.update(snake_case							)

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





def 							A__   (snake_case      :		Union[str, os.PathLike]		,  snake_case      :		Dict[str, torch.Tensor]							)    ->	str:
     os.makedirs(snake_case		,  exist_ok=snake_case							)
     __UpperCamelCase				:  Dict								=			{}
     for name, parameter in state_dict.items():
          __UpperCamelCase				:  int								=			offload_weight(snake_case		,  snake_case		,  snake_case		,  index=snake_case							)

     # Update index
     save_offload_index(snake_case		,  snake_case							)


class       SCREAMING_SNAKE_CASE_							(					_UpperCamelCase      ):



 """simple docstring"""


 def __init__(				self		:	Any				,    lowerCAmelCase		:	Mapping				,    lowerCAmelCase		:	str  )  ->					Tuple:







      """simple docstring"""







      __UpperCamelCase				:  Tuple								=			dataset
      __UpperCamelCase				:  List[str]								=			prefix






 def __getitem__(				self		:	List[Any]				,    lowerCAmelCase		:	List[str]  )  ->					Tuple:







      """simple docstring"""







      return self.dataset[F'''{self.prefix}{key}''']






 def __iter__(				self		:	List[Any]  )  ->					List[Any]:







      """simple docstring"""







      return iter([key for key in self.dataset if key.startswith(self.prefix  )]  )






 def __len__(				self		:	List[str]  )  ->					Tuple:







      """simple docstring"""







      return len(self.dataset  )






class       SCREAMING_SNAKE_CASE_							(					_UpperCamelCase      ):



 """simple docstring"""


 def __init__(				self		:	List[str]				,    lowerCAmelCase		:	Dict[str, torch.Tensor] = None				,    lowerCAmelCase		:	Optional[Union[str, os.PathLike]] = None				,    lowerCAmelCase		:	Mapping = None				,    lowerCAmelCase		:	Tuple=None				,    )  ->					Any:







      """simple docstring"""







      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				:  Any								=			{} if state_dict is None else state_dict
      __UpperCamelCase				:  Dict								=			save_folder
      if index is None and save_folder is not None:
           with open(os.path.join(lowerCAmelCase				,    """index.json"""  )  ) as f:
                __UpperCamelCase				:  str								=			json.load(lowerCAmelCase  )
      __UpperCamelCase				:  Optional[Any]								=			{} if index is None else index
      __UpperCamelCase				:  Dict								=			list(self.state_dict.keys()  )
      self.all_keys.extend([key for key in self.index if key not in self.all_keys]  )
      __UpperCamelCase				:  Dict								=			device






 def __getitem__(				self		:	Tuple				,    lowerCAmelCase		:	str  )  ->					List[str]:







      """simple docstring"""







      if key in self.state_dict:
           return self.state_dict[key]
      __UpperCamelCase				:  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

           __UpperCamelCase				:  Optional[int]								=			"""cpu""" if self.device is None else self.device
           with safe_open(weight_info["""safetensors_file"""]				,    framework="""pt"""				,    device=lowerCAmelCase  ) as f:
                __UpperCamelCase				:  Union[str, Any]								=			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				:  int								=			os.path.join(self.save_folder				,    F'''{key}.dat'''  )
      return load_offloaded_weight(lowerCAmelCase				,    lowerCAmelCase  )






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







      """simple docstring"""







      return iter(self.all_keys  )






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







      """simple docstring"""







      return len(self.all_keys  )






def 							A__   (snake_case      :		Dict[str, torch.Tensor]		,  snake_case      :		List[str]							)    ->	Union[str, Any]:
     __UpperCamelCase				:  Dict								=			{}
     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












import os





def 							A__   ()    ->	int:
					__UpperCamelCase				:  int								=			os.path.dirname(os.path.realpath(snake_case							)							)
					__UpperCamelCase				:  List[str]								=			os.path.join(snake_case		,  """triangle.txt"""							)

					with open(snake_case							) as in_file:
										__UpperCamelCase				:  Optional[Any]								=			[[int(snake_case							) for i in line.split()] for line in in_file]

					while len(snake_case							) != 1:
										__UpperCamelCase				:  Union[str, Any]								=			triangle.pop()
										__UpperCamelCase				:  List[str]								=			triangle[-1]
										for j in range(len(snake_case							) - 1							):
															curr_row[j] += max(last_row[j]		,  last_row[j + 1]							)
					return triangle[0][0]


if __name__ == "__main__":
	print(solution())






