




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	(       __UpperCamelCase			:						Any       ,     __UpperCamelCase			:						Union[str, Any]       ,     __UpperCamelCase			:						List[str]       ,     __UpperCamelCase			:						Optional[Any]=None      )  ->							List[Any]:
    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(lowercase_       ,     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(lowercase_       ,     dtype=array.dtype       ,     mode='''w+'''       ,     shape=array.shape      )
    UpperCAmelCase_							 =     array[:]
    file_array.flush()
    return index




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						List[str]       ,     __UpperCamelCase			:						Optional[int]      )  ->							List[Any]:
    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(lowercase_       ,     dtype=lowercase_       ,     shape=lowercase_       ,     mode='''r'''      )

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

    return weight




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						List[Any]       ,     __UpperCamelCase			:						Dict      )  ->							Dict:


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

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

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




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						Tuple       ,     __UpperCamelCase			:						str      )  ->							Tuple:
    os.makedirs(lowercase_       ,     exist_ok=lowercase_      )
    UpperCAmelCase_							 =     {}
    for name, parameter in state_dict.items():
        UpperCAmelCase_							 =     offload_weight(lowercase_       ,     lowercase_       ,     lowercase_       ,     index=lowercase_      )

    # Update index
    save_offload_index(lowercase_       ,     lowercase_      )



class 			a     (   _SCREAMING_SNAKE_CASE			):

     '''simple docstring'''





     def __init__(	self   :			int					,    __snake_case   :			Mapping					,    __snake_case   :			str				):
         UpperCAmelCase_							 =     dataset
         UpperCAmelCase_							 =     prefix
     def __getitem__(	self   :			Any					,    __snake_case   :			Optional[Any]				):
         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   :			List[Any]				):
         return len(self.dataset				)



class 			a     (   _SCREAMING_SNAKE_CASE			):

     '''simple docstring'''





     def __init__(	self   :			Optional[int]					,    __snake_case   :			Dict[str, torch.Tensor] = None					,    __snake_case   :			Optional[Union[str, os.PathLike]] = None					,    __snake_case   :			Mapping = None					,    __snake_case   :			Union[str, Any]=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(__snake_case					,    '''index.json'''				)				) as f:
                 UpperCAmelCase_							 =     json.load(__snake_case				)
         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]					,    __snake_case   :			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=__snake_case				) as f:
                 UpperCAmelCase_							 =     f.get_tensor(weight_info.get('''weight_name'''					,    __snake_case				)				)

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

         UpperCAmelCase_							 =     os.path.join(self.save_folder					,    F'{key}.dat'				)
         return load_offloaded_weight(__snake_case					,    __snake_case				)
     def __iter__(	self   :			Optional[Any]				):
         return iter(self.all_keys				)




     def __len__(	self   :			List[Any]				):
         return len(self.all_keys				)




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						str       ,     __UpperCamelCase			:						Any      )  ->							List[Any]:
    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











def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		int:

    """simple docstring"""


    __UpperCamelCase     						=  1.0
    for i in range(1    ,   r + 1					):
        ret *= (n + 1 - i) / i
    return round(lowercase_					)




def     __SCREAMING_SNAKE_CASE      (     lowercase_					)				->		int:

    """simple docstring"""


    return choose(8 + n    ,   lowercase_					) + choose(9 + n    ,   lowercase_					) - 10




def     __SCREAMING_SNAKE_CASE      (     lowercase_					)				->		int:

    """simple docstring"""


    return sum(non_bouncy_exact(lowercase_					) for i in range(1    ,   n + 1					)					)




def     __SCREAMING_SNAKE_CASE      (     lowercase_ = 1_00					)				->		int:

    """simple docstring"""


    return non_bouncy_upto(lowercase_					)


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






