






import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
 import torch

 from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
 import pyanvml.pyanvml as nvml


a    =       logging.get_logger(__name__)
class        __snake_case(					__UpperCAmelCase      ):
 lowercase :       PyTorchBenchmarkArguments
 lowercase :       PretrainedConfig
 lowercase :       str		      =		"PyTorch"



 @property
 def 						UpperCAmelCase    (	self   :   Optional[Any]	):

    """simple docstring"""






    return torch.__version__



 def 						UpperCAmelCase    (	self   :   Union[str, Any] ,		UpperCAmelCase__   :   Any ,		UpperCAmelCase__   :   Any ,		UpperCAmelCase__   :   Union[str, Any]	):

    """simple docstring"""






    lowercase__						:	Union[str, Any]         = self._prepare_inference_func(lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__	)
    return self._measure_speed(_inference	)



 def 						UpperCAmelCase    (	self   :   Optional[int] ,		UpperCAmelCase__   :   List[Any] ,		UpperCAmelCase__   :   List[str] ,		UpperCAmelCase__   :   str	):

    """simple docstring"""






    lowercase__						:	Optional[Any]         = self._prepare_inference_func(lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__	)
    return self._measure_memory(_inference	)



 def 						UpperCAmelCase    (	self   :   Dict ,		UpperCAmelCase__   :   int ,		UpperCAmelCase__   :   Tuple ,		UpperCAmelCase__   :   int	):

    """simple docstring"""






    lowercase__						:	Optional[Any]         = self._prepare_train_func(lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__	)
    return self._measure_speed(_train	)



 def 						UpperCAmelCase    (	self   :   Optional[int] ,		UpperCAmelCase__   :   Optional[Any] ,		UpperCAmelCase__   :   Tuple ,		UpperCAmelCase__   :   Any	):

    """simple docstring"""






    lowercase__						:	List[Any]         = self._prepare_train_func(lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__	)
    return self._measure_memory(_train	)



 def 						UpperCAmelCase    (	self   :   Optional[int] ,		UpperCAmelCase__   :   List[Any] ,		UpperCAmelCase__   :   List[str] ,		UpperCAmelCase__   :   Optional[int]	):

    """simple docstring"""






    lowercase__						:	int         = self.config_dict[model_name]

    if self.args.torchscript:
       lowercase__						:	Any         = True

    lowercase__						:	int         = (
        hasattr(lowerCamelCase__ ,		"""architectures"""	)
        and isinstance(config.architectures ,		lowerCamelCase__	)
        and len(config.architectures	) > 0
    )
    if not self.args.only_pretrain_model and has_model_class_in_config:
       try:
          lowercase__						:	Union[str, Any]         = config.architectures[0]
          lowercase__						:	Optional[Any]         = __import__("""transformers""" ,		fromlist=[model_class]	)
          lowercase__						:	Any         = getattr(lowerCamelCase__ ,		lowerCamelCase__	)
          lowercase__						:	List[Any]         = model_cls(lowerCamelCase__	)
       except ImportError:
          raise ImportError(
              f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
              """ set `--only_pretrain_model` or `args.only_pretrain_model=True`."""	)
    else:
       lowercase__						:	Optional[int]         = MODEL_MAPPING[config.__class__](lowerCamelCase__	)

    model.eval()
    model.to(self.args.device	)

    # encoder-decoder has vocab size saved differently
    lowercase__						:	Union[str, Any]         = config.vocab_size if hasattr(lowerCamelCase__ ,		"""vocab_size"""	) else config.encoder.vocab_size
    lowercase__						:	Optional[int]         = torch.randint(lowerCamelCase__ ,		(batch_size, sequence_length) ,		dtype=torch.long ,		device=self.args.device	)

    if self.args.fpaa:
       logger.info("""Running training in Mixed Precision..."""	)
       if not self.args.is_gpu:
          raise ValueError("""Mixed precision is possible only for GPU."""	)
       # amp seems to have memory leaks so that memory usage
       # is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
       model.half()

    if self.args.torchscript:
       with torch.no_grad():
          lowercase__						:	List[Any]         = torch.jit.trace(lowerCamelCase__ ,		lowerCamelCase__	)
    else:
       lowercase__						:	List[str]         = model

    def encoder_decoder_forward():
       with torch.no_grad():
          lowercase__						:	Optional[Any]         = inference_model(lowerCamelCase__ ,		decoder_input_ids=lowerCamelCase__	)
       return outputs

    def encoder_forward():
       with torch.no_grad():
          lowercase__						:	Union[str, Any]         = inference_model(lowerCamelCase__	)
       return outputs

    lowercase__						:	List[str]         = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
    return _forward



 def 						UpperCAmelCase    (	self   :   List[str] ,		UpperCAmelCase__   :   List[str] ,		UpperCAmelCase__   :   List[Any] ,		UpperCAmelCase__   :   List[Any]	):

    """simple docstring"""






    lowercase__						:	Dict         = self.config_dict[model_name]

    lowercase__						:	List[Any]         = (
        hasattr(lowerCamelCase__ ,		"""architectures"""	)
        and isinstance(config.architectures ,		lowerCamelCase__	)
        and len(config.architectures	) > 0
    )
    if not self.args.only_pretrain_model and has_model_class_in_config:
       try:
          lowercase__						:	Union[str, Any]         = config.architectures[0]
          lowercase__						:	Optional[int]         = __import__("""transformers""" ,		fromlist=[model_class]	)
          lowercase__						:	Any         = getattr(lowerCamelCase__ ,		lowerCamelCase__	)
          lowercase__						:	int         = model_cls(lowerCamelCase__	)
       except ImportError:
          raise ImportError(
              f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
              """ set `--only_pretrain_model` or `args.only_pretrain_model=True`."""	)
    else:
       lowercase__						:	List[Any]         = MODEL_WITH_LM_HEAD_MAPPING[config.__class__](lowerCamelCase__	)

    if self.args.torchscript:
       raise NotImplementedError("""Training for torchscript is currently not implemented"""	)
    else:
       lowercase__						:	Union[str, Any]         = model

    model.train()
    model.to(self.args.device	)

    # encoder-decoder has vocab size saved differently
    lowercase__						:	int         = config.vocab_size if hasattr(lowerCamelCase__ ,		"""vocab_size"""	) else config.encoder.vocab_size
    lowercase__						:	Optional[Any]         = torch.randint(lowerCamelCase__ ,		(batch_size, sequence_length) ,		dtype=torch.long ,		device=self.args.device	)

    if self.args.fpaa:
       logger.info("""Running training in Mixed Precision..."""	)
       if not self.args.is_gpu:
          raise ValueError("""Mixed precision is possible only for GPU."""	)

       # amp seems to have memory leaks so that memory usage
       # is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
       model.half()

    def compute_loss_and_backprob_encoder():
       lowercase__						:	Optional[Any]         = train_model(lowerCamelCase__ ,		labels=lowerCamelCase__	)[0]
       loss.backward()
       return loss

    def compute_loss_and_backprob_encoder_decoder():
       lowercase__						:	List[str]         = train_model(lowerCamelCase__ ,		decoder_input_ids=lowerCamelCase__ ,		labels=lowerCamelCase__	)[0]
       loss.backward()
       return loss

    lowercase__						:	Union[str, Any]         = (
        compute_loss_and_backprob_encoder_decoder
        if config.is_encoder_decoder
        else compute_loss_and_backprob_encoder
    )
    return _train



 def 						UpperCAmelCase    (	self   :   List[Any] ,		UpperCAmelCase__   :   Optional[int]	):

    """simple docstring"""






    try:
       if self.args.is_tpu or self.args.torchscript:
          # run additional 10 times to stabilize compilation for tpu and torchscript
          logger.info("""Do inference on TPU or torchscript. Running model 5 times to stabilize compilation"""	)
          timeit.repeat(
              lowerCamelCase__ ,		repeat=1 ,		number=5 ,		)

       # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
       lowercase__						:	Tuple         = timeit.repeat(
           lowerCamelCase__ ,		repeat=self.args.repeat ,		number=10 ,		)

       if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
          import torch_xla.debug.metrics as met

          self.print_fn(met.metrics_report()	)

       return min(lowerCamelCase__	) / 10.0
    except RuntimeError as e:
       self.print_fn(f'''Doesn\'t fit on GPU. {e}'''	)
       return "N/A"







 def 						UpperCAmelCase    (	self   :   str ,		UpperCAmelCase__   :   Tuple	):

    """simple docstring"""






    try:
       if self.args.trace_memory_line_by_line:
          lowercase__						:	Optional[Any]         = start_memory_tracing("""transformers"""	)

       if self.args.is_tpu:
          # tpu
          raise NotImplementedError(
              """Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with"""
              """ `--no-memory` or `args.memory=False`"""	)
       elif self.args.is_gpu:
          if not is_pyanvml_available():
             logger.warning(
                 """py3nvml not installed, we won't log GPU memory usage. """
                 """Install py3nvml (pip install py3nvml) to log information about GPU."""	)
             lowercase__						:	List[str]         = """N/A"""
          else:
             logger.info(
                 """Measuring total GPU usage on GPU device. Make sure to not have additional processes running"""
                 """ on the same GPU."""	)
             # init nvml
             nvml.nvmlInit()
             func()
             lowercase__						:	List[Any]         = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx	)
             lowercase__						:	int         = nvml.nvmlDeviceGetMemoryInfo(lowerCamelCase__	)
             lowercase__						:	str         = meminfo.used
             lowercase__						:	Dict         = Memory(lowerCamelCase__	)
             # shutdown nvml
             nvml.nvmlShutdown()
       else:
          # cpu
          lowercase__						:	Union[str, Any]         = measure_peak_memory_cpu(lowerCamelCase__	)
          lowercase__						:	Any         = Memory(lowerCamelCase__	) if isinstance(lowerCamelCase__ ,		lowerCamelCase__	) else memory_bytes

       if self.args.trace_memory_line_by_line:
          lowercase__						:	List[Any]         = stop_memory_tracing(lowerCamelCase__	)
       else:
          lowercase__						:	List[str]         = None

       return memory, summary
    except RuntimeError as e:
       self.print_fn(f'''Doesn\'t fit on GPU. {e}'''	)
       return "N/A", None










import os
import sys
import time

from . import transposition_cipher as trans_cipher
def       a__						(  ):
   lowercase__						:	Union[str, Any]         = """Prehistoric Men.txt"""
   lowercase__						:	Optional[Any]         = """Output.txt"""
   lowercase__						:	List[str]         = int(input("""Enter key: """					)					)
   lowercase__						:	Tuple         = input("""Encrypt/Decrypt [e/d]: """					)

   if not os.path.exists(A					):
      print(F'''File {input_file} does not exist. Quitting...'''					)
      sys.exit()
   if os.path.exists(A					):
      print(F'''Overwrite {output_file}? [y/n]'''					)
      lowercase__						:	List[Any]         = input("""> """					)
      if not response.lower().startswith("""y"""					):
         sys.exit()

   lowercase__						:	str         = time.time()
   if mode.lower().startswith("""e"""					):
      with open(A					) as f:
         lowercase__						:	Optional[int]         = f.read()
      lowercase__						:	List[str]         = trans_cipher.encrypt_message(A		,				A					)
   elif mode.lower().startswith("""d"""					):
      with open(A					) as f:
         lowercase__						:	int         = f.read()
      lowercase__						:	int         = trans_cipher.decrypt_message(A		,				A					)

   with open(A		,				"""w"""					) as output_obj:
      output_obj.write(A					)

   lowercase__						:	Optional[Any]         = round(time.time() - start_time		,				2					)
   print(("""Done (""", total_time, """seconds )""")					)


if __name__ == "__main__":
 main()


