



"""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 					lowerCAmelCase  (   __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase=None ):
 '''simple docstring'''





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





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

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

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

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

 return weight
def 					lowerCAmelCase  (   __UpperCamelCase							,      __UpperCamelCase ):
 '''simple docstring'''





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

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

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





 os.makedirs(__UpperCamelCase							,      exist_ok=__UpperCamelCase )
 UpperCAmelCase__   :					Optional[Any]   				=   {}
 for name, parameter in state_dict.items():
  UpperCAmelCase__   :					List[Any]   				=   offload_weight(__UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase							,      index=__UpperCamelCase )

 # Update index
 save_offload_index(__UpperCamelCase							,      __UpperCamelCase )







class        __lowercase     (      SCREAMING_SNAKE_CASE_   ):

  def __init__(					self						:     Optional[int]   ,A						:     Optional[Any]   ,A						:     int      ):






   '''simple docstring'''
   UpperCAmelCase__   :					int   				=   dataset
   UpperCAmelCase__   :					Union[str, Any]   				=   prefix

  def __getitem__(					self						:     List[Any]   ,A						:     Dict      ):






   '''simple docstring'''
   return self.dataset[f"{self.prefix}{key}"]

  def __iter__(					self						:     int      ):






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




  def __len__(					self						:     int      ):






   '''simple docstring'''
   return len(self.dataset      )







class        __lowercase     (      SCREAMING_SNAKE_CASE_   ):

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

  def __getitem__(					self						:     Tuple   ,A						:     str      ):






   '''simple docstring'''






   if key in self.state_dict:
    return self.state_dict[key]
   UpperCAmelCase__   :					List[Any]   				=   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__   :					Dict   				=   """cpu""" if self.device is None else self.device
    with safe_open(weight_info["""safetensors_file"""]   ,framework="""pt"""   ,device=SCREAMING_SNAKE_CASE_      ) as f:
     UpperCAmelCase__   :					Optional[Any]   				=   f.get_tensor(weight_info.get("""weight_name"""   ,SCREAMING_SNAKE_CASE_      )      )

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

   UpperCAmelCase__   :					List[Any]   				=   os.path.join(self.save_folder   ,f"{key}.dat"      )
   return load_offloaded_weight(SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_      )

  def __iter__(					self						:     List[Any]      ):






   '''simple docstring'''
   return iter(self.all_keys      )




  def __len__(					self						:     str      ):






   '''simple docstring'''
   return len(self.all_keys      )







def 					lowerCAmelCase  (   __UpperCamelCase							,      __UpperCamelCase ):
 '''simple docstring'''





 UpperCAmelCase__   :					str   				=   {}
 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 __future__ import annotations
def 					lowerCAmelCase  (   __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase ):
 '''simple docstring'''





 UpperCAmelCase__				, UpperCAmelCase__   :					Optional[Any]   				=   m - 1, 0
 while i >= 0 and j < n:
  if key == mat[i][j]:
   print(F"Key {key} found at row- {i + 1} column- {j + 1}" )
   return
  if key < mat[i][j]:
   i -= 1
  else:
   j += 1
 print(F"Key {key} not found" )
def 					lowerCAmelCase  (   ):
 '''simple docstring'''





 UpperCAmelCase__   :					Union[str, Any]   				=   [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]]
 UpperCAmelCase__   :					int   				=   int(input("""Enter the element to be searched:""" ) )
 print(__UpperCamelCase )
 search_in_a_sorted_matrix(__UpperCamelCase							,      len(__UpperCamelCase )							,      len(mat[0] )							,      __UpperCamelCase )


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      main()





