


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_   (				__snake_case	,		__snake_case	,		__snake_case	,		__snake_case=None		)	->     Dict:







 """simple docstring"""


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







 """simple docstring"""


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

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

 _lowercase				 =np.memmap(__snake_case	,		dtype=__snake_case	,		shape=__snake_case	,		mode='''r'''		)

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

 return weight
def 							UpperCAmelCase_   (				__snake_case	,		__snake_case		)	->     List[str]:







 """simple docstring"""


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

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

 with open(__snake_case	,		'''w'''	,		encoding='''utf-8'''		) as f:
  json.dump(__snake_case	,		__snake_case	,		indent=2		)
def 							UpperCAmelCase_   (				__snake_case	,		__snake_case		)	->     Any:







 """simple docstring"""


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

 # Update index
 save_offload_index(__snake_case	,		__snake_case		)







class  lowerCamelCase__  (			lowerCAmelCase):





     def __init__(self						,	UpperCAmelCase						,	UpperCAmelCase			)	->  int:
      _lowercase				 =dataset
      _lowercase				 =prefix





     def __getitem__(self						,	UpperCAmelCase			)	->  Optional[Any]:
      return self.dataset[f"{self.prefix}{key}"]





     def __iter__(self			)	->  List[str]:
      return iter([key for key in self.dataset if key.startswith(self.prefix			)]			)





     def __len__(self			)	->  Dict:
      return len(self.dataset			)







class  lowerCamelCase__  (			lowerCAmelCase):





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





     def __getitem__(self						,	UpperCAmelCase			)	->  Tuple:
      # State dict gets priority
      if key in self.state_dict:
       return self.state_dict[key]
      _lowercase				 =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				 ='''cpu''' if self.device is None else self.device
       with safe_open(weight_info['''safetensors_file''']						,	framework='''pt'''						,	device=UpperCAmelCase			) as f:
        _lowercase				 =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				 =os.path.join(self.save_folder						,	f"{key}.dat"			)
      return load_offloaded_weight(UpperCAmelCase						,	UpperCAmelCase			)





     def __iter__(self			)	->  int:
      return iter(self.all_keys			)





     def __len__(self			)	->  Any:
      return len(self.all_keys			)



def 							UpperCAmelCase_   (				__snake_case	,		__snake_case		)	->     Tuple:







 """simple docstring"""


 _lowercase				 ={}
 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 math
def 							UpperCAmelCase_   (				__snake_case		)	->     int:







 """simple docstring"""


 return sum(int(__snake_case		) for digit in str(__snake_case		)		)
def 							UpperCAmelCase_   (				__snake_case = 30		)	->     int:







 """simple docstring"""


 _lowercase				 =[]
 for digit in range(2	,		100		):
  for power in range(2	,		100		):
   _lowercase				 =int(math.pow(__snake_case	,		__snake_case		)		)
   if digit == digit_sum(__snake_case		):
    digit_to_powers.append(__snake_case		)

 digit_to_powers.sort()
 return digit_to_powers[n - 1]


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

