code: 
  
  """simple docstring"""
  import os
  import random
  from pathlib import Path
  from typing import List
  
  import numpy as np
  import torch
  from torch.cuda.amp import GradScaler
  
  from .utils import (
      MODEL_NAME,
      OPTIMIZER_NAME,
      RNG_STATE_NAME,
      SCALER_NAME,
      SCHEDULER_NAME,
      get_pretty_name,
      is_tpu_available,
      is_xpu_available,
      save,
  )
  
  
  if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm
  
  from .logging import get_logger
  from .state import PartialState
  
  
  _A						=     get_logger(__name__)
  
  
  
  
  def 				a__	(	lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase = None ,  )  ->   List[str]:
  
  
  
  
  
        for i, state in enumerate(__a						):
              UpperCAmelCase__ :       Optional[int]   				=						F"""{MODEL_NAME}.bin""" if i == 0 else F"""{MODEL_NAME}_{i}.bin"""
              UpperCAmelCase__ :       Optional[int]   				=						os.path.join(__a ,  __a						)
              save(__a ,  __a						)
              logger.info(F"""Model weights saved in {output_model_file}"""						)
        # Optimizer states
        for i, opt in enumerate(__a						):
              UpperCAmelCase__ :       Optional[int]   				=						opt.state_dict()
              UpperCAmelCase__ :       Tuple   				=						F"""{OPTIMIZER_NAME}.bin""" if i == 0 else F"""{OPTIMIZER_NAME}_{i}.bin"""
              UpperCAmelCase__ :       Union[str, Any]   				=						os.path.join(__a ,  __a						)
              save(__a ,  __a						)
              logger.info(F"""Optimizer state saved in {output_optimizer_file}"""						)
        # Scheduler states
        for i, scheduler in enumerate(__a						):
              UpperCAmelCase__ :       Dict   				=						scheduler.state_dict()
              UpperCAmelCase__ :       str   				=						F"""{SCHEDULER_NAME}.bin""" if i == 0 else F"""{SCHEDULER_NAME}_{i}.bin"""
              UpperCAmelCase__ :       List[str]   				=						os.path.join(__a ,  __a						)
              save(__a ,  __a						)
              logger.info(F"""Scheduler state saved in {output_scheduler_file}"""						)
        # GradScaler state
        if scaler is not None:
              UpperCAmelCase__ :       Tuple   				=						scaler.state_dict()
              UpperCAmelCase__ :       int   				=						os.path.join(__a ,  __a						)
              torch.save(__a ,  __a						)
              logger.info(F"""Gradient scaler state saved in {output_scaler_file}"""						)
        # Random number generator states
        UpperCAmelCase__ :       Union[str, Any]   				=						{}
        UpperCAmelCase__ :       Any   				=						F"""{RNG_STATE_NAME}_{process_index}.pkl"""
        UpperCAmelCase__ :       Optional[int]   				=						random.getstate()
        UpperCAmelCase__ :       List[Any]   				=						np.random.get_state()
        UpperCAmelCase__ :       Tuple   				=						torch.get_rng_state()
        if is_xpu_available():
              UpperCAmelCase__ :       Dict   				=						torch.xpu.get_rng_state_all()
        else:
              UpperCAmelCase__ :       Optional[int]   				=						torch.cuda.get_rng_state_all()
        if is_tpu_available():
              UpperCAmelCase__ :       List[Any]   				=						xm.get_rng_state()
        UpperCAmelCase__ :       List[Any]   				=						os.path.join(__a ,  __a						)
        torch.save(__a ,  __a						)
        logger.info(F"""Random states saved in {output_states_file}"""						)
        return output_dir
  
  
  
  
  def 				a__	(	lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase=None ,  lowerCAmelCase=None ,  **lowerCAmelCase ,  )  ->   Optional[int]:
  
  
  
  
  
        if map_location not in [None, "cpu", "on_device"]:
              raise TypeError(
                  """Unsupported optimizer map location passed, please choose one of `None`, `\'cpu\'`, or `\'on_device\'`"""						)
        if map_location is None:
              UpperCAmelCase__ :       Dict   				=						'''cpu'''
        elif map_location == "on_device":
              UpperCAmelCase__ :       Tuple   				=						PartialState().device
        # Model states
        for i, model in enumerate(__a						):
              UpperCAmelCase__ :       List[Any]   				=						F"""{MODEL_NAME}.bin""" if i == 0 else F"""{MODEL_NAME}_{i}.bin"""
              UpperCAmelCase__ :       str   				=						os.path.join(__a ,  __a						)
              models[i].load_state_dict(torch.load(__a ,  map_location=__a						) ,  **__a						)
        logger.info("""All model weights loaded successfully"""						)
  
        # Optimizer states
        for i, opt in enumerate(__a						):
              UpperCAmelCase__ :       int   				=						F"""{OPTIMIZER_NAME}.bin""" if i == 0 else F"""{OPTIMIZER_NAME}_{i}.bin"""
              UpperCAmelCase__ :       Tuple   				=						os.path.join(__a ,  __a						)
              UpperCAmelCase__ :       List[Any]   				=						torch.load(__a ,  map_location=__a						)
              optimizers[i].load_state_dict(__a						)
        logger.info("""All optimizer states loaded successfully"""						)
  
        # Scheduler states
        for i, scheduler in enumerate(__a						):
              UpperCAmelCase__ :       Any   				=						F"""{SCHEDULER_NAME}.bin""" if i == 0 else F"""{SCHEDULER_NAME}_{i}.bin"""
              UpperCAmelCase__ :       Tuple   				=						os.path.join(__a ,  __a						)
              scheduler.load_state_dict(torch.load(__a						)						)
        logger.info("""All scheduler states loaded successfully"""						)
  
        # GradScaler state
        if scaler is not None:
              UpperCAmelCase__ :       List[str]   				=						os.path.join(__a ,  __a						)
              scaler.load_state_dict(torch.load(__a						)						)
              logger.info("""GradScaler state loaded successfully"""						)
  
        # Random states
        try:
              UpperCAmelCase__ :       List[Any]   				=						torch.load(os.path.join(__a ,  F"""{RNG_STATE_NAME}_{process_index}.pkl"""						)						)
              random.setstate(states["""random_state"""]						)
              np.random.set_state(states["""numpy_random_seed"""]						)
              torch.set_rng_state(states["""torch_manual_seed"""]						)
              if is_xpu_available():
                    torch.xpu.set_rng_state_all(states["""torch_xpu_manual_seed"""]						)
              else:
                    torch.cuda.set_rng_state_all(states["""torch_cuda_manual_seed"""]						)
              if is_tpu_available():
                    xm.set_rng_state(states["""xm_seed"""]						)
              logger.info("""All random states loaded successfully"""						)
        except Exception:
              logger.info("""Could not load random states"""						)
  
  
  
  
  def 				a__	(	lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase = 0						)  ->   str:
        UpperCAmelCase__ :       str   				=						Path(__a						) / F"""custom_checkpoint_{index}.pkl"""
        logger.info(F"""Saving the state of {get_pretty_name(__a						)} to {save_location}"""						)
        torch.save(obj.state_dict() ,  __a						)
  
  
  
  
  
  def 				a__	(	lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase = 0						)  ->   Optional[Any]:
        UpperCAmelCase__ :       Optional[Any]   				=						F"""{path}/custom_checkpoint_{index}.pkl"""
        logger.info(F"""Loading the state of {get_pretty_name(__a						)} from {load_location}"""						)
        obj.load_state_dict(torch.load(__a ,  map_location="""cpu"""						)						)
  
  
style_context: 
  
  """simple docstring"""
  def 				a__	(	lowerCAmelCase ,  lowerCAmelCase						)  ->   str:
        if not isinstance(lowerCAmelCase ,  lowerCAmelCase						):
              raise TypeError("""plaintext must be a string"""						)
        if not isinstance(lowerCAmelCase ,  lowerCAmelCase						):
              raise TypeError("""key must be a string"""						)
  
        if not plaintext:
              raise ValueError("""plaintext is empty"""						)
        if not key:
              raise ValueError("""key is empty"""						)
  
        key += plaintext
        UpperCAmelCase__ :       Dict						=						plaintext.lower()
        UpperCAmelCase__ :       Tuple						=						key.lower()
        UpperCAmelCase__ :       Optional[int]						=						0
        UpperCAmelCase__ :       Tuple						=						0
        UpperCAmelCase__ :       Optional[int]						=						""""""
        while plaintext_iterator < len(lowerCAmelCase						):
              if (
                  ord(plaintext[plaintext_iterator]						) < 97
                  or ord(plaintext[plaintext_iterator]						) > 1_22
              ):
                    ciphertext += plaintext[plaintext_iterator]
                    plaintext_iterator += 1
              elif ord(key[key_iterator]						) < 97 or ord(key[key_iterator]						) > 1_22:
                    key_iterator += 1
              else:
                    ciphertext += chr(
                        (
                            (ord(plaintext[plaintext_iterator]						) - 97 + ord(key[key_iterator]						))
                            - 97
                        )
                        % 26
                        + 97						)
                    key_iterator += 1
                    plaintext_iterator += 1
        return ciphertext
  
  
  
  
  def 				a__	(	lowerCAmelCase ,  lowerCAmelCase						)  ->   str:
        if not isinstance(lowerCAmelCase ,  lowerCAmelCase						):
              raise TypeError("""ciphertext must be a string"""						)
        if not isinstance(lowerCAmelCase ,  lowerCAmelCase						):
              raise TypeError("""key must be a string"""						)
  
        if not ciphertext:
              raise ValueError("""ciphertext is empty"""						)
        if not key:
              raise ValueError("""key is empty"""						)
  
        UpperCAmelCase__ :       Union[str, Any]						=						key.lower()
        UpperCAmelCase__ :       int						=						0
        UpperCAmelCase__ :       int						=						0
        UpperCAmelCase__ :       int						=						""""""
        while ciphertext_iterator < len(lowerCAmelCase						):
              if (
                  ord(ciphertext[ciphertext_iterator]						) < 97
                  or ord(ciphertext[ciphertext_iterator]						) > 1_22
              ):
                    plaintext += ciphertext[ciphertext_iterator]
              else:
                    plaintext += chr(
                        (ord(ciphertext[ciphertext_iterator]						) - ord(key[key_iterator]						)) % 26
                        + 97						)
                    key += chr(
                        (ord(ciphertext[ciphertext_iterator]						) - ord(key[key_iterator]						)) % 26
                        + 97						)
                    key_iterator += 1
              ciphertext_iterator += 1
        return plaintext
  
  
  if __name__ == "__main__":
      import doctest
  
      doctest.testmod()
      _A						=     int(input("""Type 1 to encrypt or 2 to decrypt:"""))
      if operation == 1:
          _A						=     input("""Typeplaintext to be encrypted:\n""")
          _A						=     input("""Type the key:\n""")
          print(encrypt(plaintext, key))
      elif operation == 2:
          _A						=     input("""Type the ciphertext to be decrypted:\n""")
          _A						=     input("""Type the key:\n""")
          print(decrypt(ciphertext, key))
      decrypt("""jsqqs avvwo""", """coffee""")
  
  