






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 		_a					(   a				:Union[str, Any]			,							a				:List[str]			,							a				:Union[str, Any]			,							a				:Dict=None				)							->    Any:
 a							=   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.
  a							=   weight.view(torch.intaa				)
  a							=   '''bfloat16'''
 a							=   weight.cpu().numpy()
 a							=   os.path.join(a			,							F"""{weight_name}.dat"""				)
 if index is not None:
  if dtype is None:
   a							=   str(array.dtype				)
  a							=   {'''dtype''': dtype, '''shape''': list(array.shape				)}
 if array.ndim == 0:
  a							=   array[None]
 a							=   np.memmap(a			,							dtype=array.dtype			,							mode='''w+'''			,							shape=array.shape				)
 a							=   array[:]
 file_array.flush()
 return index
def 		_a					(   a				:Optional[Any]			,							a				:List[Any]				)							->    Optional[Any]:
 a							=   tuple(weight_info['''shape''']				)
 if shape == ():
  # NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
  a							=   (1,)

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

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

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

 return weight
def 		_a					(   a				:Any			,							a				:int				)							->    int:
 if index is None or len(a				) == 0:
  # Nothing to save
  return

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

 with open(a			,							'''w'''			,							encoding='''utf-8'''				) as f:
  json.dump(a			,							a			,							indent=2				)
def 		_a					(   a				:Union[str, os.PathLike]			,							a				:Dict[str, torch.Tensor]				)							->    List[str]:
 os.makedirs(a			,							exist_ok=a				)
 a							=   {}
 for name, parameter in state_dict.items():
  a							=   offload_weight(a			,							a			,							a			,							index=a				)

 # Update index
 save_offload_index(a			,							a				)






class 	lowercase_      (			lowercase     ):






      '''simple docstring'''




      def __init__(   self   :		List[Any]				,      __UpperCAmelCase   :		Mapping				,      __UpperCAmelCase   :		str						)					->Optional[Any]:




       """simple docstring"""


       a							=   dataset
       a							=   prefix







      def __getitem__(   self   :		List[str]				,      __UpperCAmelCase   :		Optional[int]						)					->int:




       """simple docstring"""


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







      def __iter__(   self   :		List[str]						)					->Any:




       """simple docstring"""


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







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




       """simple docstring"""


       return len(self.dataset						)






class 	lowercase_      (			lowercase     ):






      '''simple docstring'''




      def __init__(   self   :		Dict				,      __UpperCAmelCase   :		Dict[str, torch.Tensor] = None				,      __UpperCAmelCase   :		Optional[Union[str, os.PathLike]] = None				,      __UpperCAmelCase   :		Mapping = None				,      __UpperCAmelCase   :		str=None				,      )					->Any:




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

       a							=   {} if state_dict is None else state_dict
       a							=   save_folder
       if index is None and save_folder is not None:
        with open(os.path.join(__UpperCAmelCase				,      '''index.json'''						)						) as f:
         a							=   json.load(__UpperCAmelCase						)
       a							=   {} if index is None else index
       a							=   list(self.state_dict.keys()						)
       self.all_keys.extend([key for key in self.index if key not in self.all_keys]						)
       a							=   device







      def __getitem__(   self   :		Tuple				,      __UpperCAmelCase   :		str						)					->Union[str, Any]:




       """simple docstring"""


       if key in self.state_dict:
        return self.state_dict[key]
       a							=   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

        a							=   '''cpu''' if self.device is None else self.device
        with safe_open(weight_info['''safetensors_file''']				,      framework='''pt'''				,      device=__UpperCAmelCase						) as f:
         a							=   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

       a							=   os.path.join(self.save_folder				,      F"""{key}.dat"""						)
       return load_offloaded_weight(__UpperCAmelCase				,      __UpperCAmelCase						)







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




       """simple docstring"""


       return iter(self.all_keys						)







      def __len__(   self   :		List[str]						)					->Optional[int]:




       """simple docstring"""


       return len(self.all_keys						)
def 		_a					(   a				:Dict[str, torch.Tensor]			,							a				:List[str]				)							->    Dict:
 a							=   {}
 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 numpy as np

UpperCAmelCase__            =	[
    ["a", "b", "c", "d", "e"],
    ["f", "g", "h", "i", "k"],
    ["l", "m", "n", "o", "p"],
    ["q", "r", "s", "t", "u"],
    ["v", "w", "x", "y", "z"],
]






class 	lowercase_      :






      '''simple docstring'''




      def __init__(   self   :		Optional[Any]						)					->None:




       """simple docstring"""


       a							=   np.array(__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		List[str]				,      __UpperCAmelCase   :		str						)					->np.ndarray:




       """simple docstring"""


       a						,  a							=   np.where(letter == self.SQUARE						)
       a							=   np.concatenate([indexa + 1, indexa + 1]						)
       return indexes







      def    __lowerCAmelCase      (   self   :		int				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		int						)					->str:




       """simple docstring"""


       return self.SQUARE[indexa - 1, indexa - 1]







      def    __lowerCAmelCase      (   self   :		Tuple				,      __UpperCAmelCase   :		str						)					->str:




       """simple docstring"""


       a							=   message.lower()
       a							=   message.replace('''j'''				,      '''i'''						)

       a							=   ''''''
       for letter_index in range(len(__UpperCAmelCase						)						):
        if message[letter_index] != " ":
         a							=   self.letter_to_numbers(message[letter_index]						)
         a							=   encoded_message + str(numbers[0]						) + str(numbers[1]						)
        elif message[letter_index] == " ":
         a							=   encoded_message + ''' '''

       return encoded_message







      def    __lowerCAmelCase      (   self   :		Any				,      __UpperCAmelCase   :		str						)					->str:




       """simple docstring"""


       a							=   message.replace(''' '''				,      '''  '''						)
       a							=   ''''''
       for numbers_index in range(int(len(__UpperCAmelCase						) / 2						)						):
        if message[numbers_index * 2] != " ":
         a							=   message[numbers_index * 2]
         a							=   message[numbers_index * 2 + 1]

         a							=   self.numbers_to_letter(int(__UpperCAmelCase						)				,      int(__UpperCAmelCase						)						)
         a							=   decoded_message + letter
        elif message[numbers_index * 2] == " ":
         a							=   decoded_message + ''' '''

       return decoded_message


