




'''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 							snake_case_    (  a__						:      str       ,a__						:      str       ,a__						:      Optional[int]       ,a__						:      List[Any]=None     ):



    """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(a__       ,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(a__       ,dtype=array.dtype       ,mode="""w+"""       ,shape=array.shape     )
    __lowercase     							=		array[:]
    file_array.flush()
    return index
def 							snake_case_    (  a__						:      List[Any]       ,a__						:      Any     ):



    """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(a__       ,dtype=a__       ,shape=a__       ,mode="""r"""     )

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

    return weight
def 							snake_case_    (  a__						:      List[str]       ,a__						:      Optional[int]     ):



    """simple docstring"""




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

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

    with open(a__       ,"""w"""       ,encoding="""utf-8"""     ) as f:
        json.dump(a__       ,a__       ,indent=2     )
def 							snake_case_    (  a__						:      Union[str, os.PathLike]       ,a__						:      Dict[str, torch.Tensor]     ):



    """simple docstring"""




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

    # Update index
    save_offload_index(a__       ,a__     )






class 			SCREAMING_SNAKE_CASE(							__A	):

       def __init__(       self   ,						lowerCamelCase__   ,						lowerCamelCase__	)       ->   Dict:

           """simple docstring"""



           __lowercase     							=		dataset
           __lowercase     							=		prefix

       def __getitem__(       self   ,						lowerCamelCase__	)       ->   Any:

           """simple docstring"""



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

       def __iter__(       self	)       ->   Optional[int]:

           """simple docstring"""



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




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

           """simple docstring"""



           return len(self.dataset	)






class 			SCREAMING_SNAKE_CASE(							__A	):

       def __init__(       self   ,						lowerCamelCase__ = None   ,						lowerCamelCase__ = None   ,						lowerCamelCase__ = None   ,						lowerCamelCase__=None   ,						)       ->   str:

           """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."""	)

           __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(lowerCamelCase__   ,						"""index.json"""	)	) as f:
                   __lowercase     							=		json.load(lowerCamelCase__	)
           __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   ,						lowerCamelCase__	)       ->   List[Any]:

           """simple docstring"""



           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=lowerCamelCase__	) as f:
                   __lowercase     							=		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

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

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

           """simple docstring"""



           return iter(self.all_keys	)




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

           """simple docstring"""



           return len(self.all_keys	)

def 							snake_case_    (  a__						:      Dict[str, torch.Tensor]       ,a__						:      List[str]     ):



    """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










'''simple docstring'''
from math import gcd
def 							snake_case_    (  a__						:      int = 1_20_00     ):



    """simple docstring"""




    __lowercase     							=		0
    for d in range(max_d + 1     ):
        for n in range(d // 3 + 1       ,(d + 1) // 2     ):
            if gcd(a__       ,a__     ) == 1:
                fractions_number += 1
    return fractions_number


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




