





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      SCREAMING_SNAKE_CASE      (   lowerCAmelCase     ,							lowerCAmelCase     ,							lowerCAmelCase     ,							lowerCAmelCase=None       ):
       _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(lowerCAmelCase     ,							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(lowerCAmelCase     ,							dtype=array.dtype     ,							mode='''w+'''     ,							shape=array.shape       )
       _UpperCamelCase														=    array[:]
       file_array.flush()
       return index


def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase     ,							lowerCAmelCase       ):
       _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(lowerCAmelCase     ,							dtype=lowerCAmelCase     ,							shape=lowerCAmelCase     ,							mode='''r'''       )

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

       return weight


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

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

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


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

       # Update index
       save_offload_index(lowerCAmelCase     ,							lowerCAmelCase       )

class 			__A(    __UpperCAmelCase			):

     def __init__(   self,							A,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    dataset
            _UpperCamelCase														=    prefix

     def __getitem__(   self,							A   ):





            """simple docstring"""

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

     def __iter__(   self   ):





            """simple docstring"""

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



     def __len__(   self   ):





            """simple docstring"""

            return len(self.dataset   )






class 			__A(    __UpperCAmelCase			):

     def __init__(   self,							A = None,							A = None,							A = None,							A=None,							):





            """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														=    {} 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(A,							'''index.json'''   )   ) as f:
                          _UpperCamelCase														=    json.load(A   )
            _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,							A   ):





            """simple docstring"""

            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=A   ) as f:
                          _UpperCamelCase														=    f.get_tensor(weight_info.get('''weight_name''',							A   )   )

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

            _UpperCamelCase														=    os.path.join(self.save_folder,							F'''{key}.dat'''   )
            return load_offloaded_weight(A,							A   )

     def __iter__(   self   ):





            """simple docstring"""

            return iter(self.all_keys   )



     def __len__(   self   ):





            """simple docstring"""

            return len(self.all_keys   )






def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase     ,							lowerCAmelCase       ):
       _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













import math
from decimal import Decimal, getcontext


def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase = 4_000_000       ):

       try:
              _UpperCamelCase														=    int(lowerCAmelCase       )
       except (TypeError, ValueError):
              raise TypeError('''Parameter n must be int or castable to int.'''       )
       if n <= 0:
              raise ValueError('''Parameter n must be greater than or equal to one.'''       )
       _UpperCamelCase														=    100
       _UpperCamelCase														=    (Decimal(5       ) ** Decimal(0.5       ) + 1) / Decimal(2       )

       _UpperCamelCase														=    (math.floor(math.log(n * (phi + 2)     ,							lowerCAmelCase       ) - 1       ) // 3) * 3 + 2
       _UpperCamelCase														=    Decimal(round(phi ** Decimal(index + 1       )       )       ) / (phi + 2)
       _UpperCamelCase														=    num // 2
       return int(lowerCAmelCase       )


if __name__ == "__main__":
       print(F'''{solution() = }''')






