



'''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_      (  lowerCAmelCase_ ,     lowerCAmelCase_ ,     lowerCAmelCase_ ,     lowerCAmelCase_=None	)->	Optional[int]:





       '''simple docstring'''


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




def 					snake_case_      (  lowerCAmelCase_ ,     lowerCAmelCase_	)->	Union[str, Any]:





       '''simple docstring'''


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

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

       _UpperCAmelCase						:		int      =				np.memmap(lowerCAmelCase_ ,     dtype=lowerCAmelCase_ ,     shape=lowerCAmelCase_ ,     mode="""r"""	)

       if len(weight_info["""shape"""]	) == 0:
              _UpperCAmelCase						:		Optional[Any]      =				weight[0]
       _UpperCAmelCase						:		List[str]      =				torch.tensor(lowerCAmelCase_	)
       if weight_info["dtype"] == "bfloat16":
              _UpperCAmelCase						:		Tuple      =				weight.view(torch.bfloataa	)

       return weight




def 					snake_case_      (  lowerCAmelCase_ ,     lowerCAmelCase_	)->	List[str]:





       '''simple docstring'''


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

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

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




def 					snake_case_      (  lowerCAmelCase_ ,     lowerCAmelCase_	)->	Union[str, Any]:





       '''simple docstring'''


       os.makedirs(lowerCAmelCase_ ,     exist_ok=lowerCAmelCase_	)
       _UpperCAmelCase						:		List[str]      =				{}
       for name, parameter in state_dict.items():
              _UpperCAmelCase						:		str      =				offload_weight(lowerCAmelCase_ ,     lowerCAmelCase_ ,     lowerCAmelCase_ ,     index=lowerCAmelCase_	)

       # Update index
       save_offload_index(lowerCAmelCase_ ,     lowerCAmelCase_	)



class 							lowercase (						_SCREAMING_SNAKE_CASE       ):


 """simple docstring"""


 def __init__(							self						,a_						,a_							)	->     Optional[Any]:
        _UpperCAmelCase						:		int      =				dataset
        _UpperCAmelCase						:		Optional[Any]      =				prefix







 def __getitem__(							self						,a_							)	->     Optional[Any]:
        return self.dataset[f'''{self.prefix}{key}''']







 def __iter__(							self							)	->     int:
        return iter([key for key in self.dataset if key.startswith(self.prefix							)]							)







 def __len__(							self							)	->     Any:
        return len(self.dataset							)







class 							lowercase (						_SCREAMING_SNAKE_CASE       ):


 """simple docstring"""


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







 def __getitem__(							self						,a_							)	->     int:
        # State dict gets priority
        if key in self.state_dict:
               return self.state_dict[key]
        _UpperCAmelCase						:		Optional[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						:		Optional[int]      =				"""cpu""" if self.device is None else self.device
               with safe_open(weight_info["""safetensors_file"""]						,framework="""pt"""						,device=__A							) as f:
                      _UpperCAmelCase						:		str      =				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						:		Dict      =				os.path.join(self.save_folder						,f'''{key}.dat'''							)
        return load_offloaded_weight(__A						,__A							)







 def __iter__(							self							)	->     Optional[int]:
        return iter(self.all_keys							)







 def __len__(							self							)	->     int:
        return len(self.all_keys							)







def 					snake_case_      (  lowerCAmelCase_ ,     lowerCAmelCase_	)->	Union[str, Any]:





       '''simple docstring'''


       _UpperCAmelCase						:		Dict      =				{}
       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 string import ascii_uppercase




def 					snake_case_      (  lowerCAmelCase_ ,     lowerCAmelCase_ ,     lowerCAmelCase_ = False ,     lowerCAmelCase_ = ascii_uppercase	)->	str:





       '''simple docstring'''


       _UpperCAmelCase						:		List[str]      =				keyword.upper()
       _UpperCAmelCase						:		Optional[int]      =				plaintext.upper()
       _UpperCAmelCase						:		Optional[int]      =				set(lowerCAmelCase_	)

       # create a list of unique characters in the keyword - their order matters
       # it determines how we will map plaintext characters to the ciphertext
       _UpperCAmelCase						:		str      =				[]
       for char in keyword:
              if char in alphabet_set and char not in unique_chars:
                     unique_chars.append(lowerCAmelCase_	)
    # the number of those unique characters will determine the number of rows
       _UpperCAmelCase						:		Optional[Any]      =				len(lowerCAmelCase_	)

       # create a shifted version of the alphabet
       _UpperCAmelCase						:		Optional[Any]      =				unique_chars + [
           char for char in alphabet if char not in unique_chars
       ]

       # create a modified alphabet by splitting the shifted alphabet into rows
       _UpperCAmelCase						:		List[str]      =				[
           shifted_alphabet[k : k + num_unique_chars_in_keyword]
           for k in range(0 ,     26 ,     lowerCAmelCase_	)
       ]

       # map the alphabet characters to the modified alphabet characters
       # going 'vertically' through the modified alphabet - consider columns first
       _UpperCAmelCase						:		int      =				{}
       _UpperCAmelCase						:		int      =				0
       for column in range(lowerCAmelCase_	):
              for row in modified_alphabet:
                     # if current row (the last one) is too short, break out of loop
                     if len(lowerCAmelCase_	) <= column:
                            break

                     # map current letter to letter in modified alphabet
                     _UpperCAmelCase						:		Optional[int]      =				row[column]
                     letter_index += 1

       if verbose:
              print(lowerCAmelCase_	)
       # create the encrypted text by mapping the plaintext to the modified alphabet
       return "".join(mapping[char] if char in mapping else char for char in plaintext	)


if __name__ == "__main__":
   # example use
   print(mixed_keyword("""college""", """UNIVERSITY"""))



