





"""simple docstring"""
import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


__UpperCamelCase			:		Union[str, Any]		    =   [nn.LayerNorm]

__UpperCamelCase			:		int		    =   logging.get_logger(__name__)

__UpperCamelCase			:		Any		    =   version.parse(version.parse(torch.__version__).base_version)

__UpperCamelCase			:		Union[str, Any]		    =   parsed_torch_version_base >= version.parse('''2.0''')
__UpperCamelCase			:		List[str]		    =   parsed_torch_version_base >= version.parse('''1.12''')
__UpperCamelCase			:		Optional[Any]		    =   parsed_torch_version_base >= version.parse('''1.11''')
__UpperCamelCase			:		Tuple		    =   parsed_torch_version_base < version.parse('''1.11''')

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  Dict   ,			_UpperCAmelCase :  Dict   ,			_UpperCAmelCase :  str   ,			_UpperCAmelCase :  List[str]   ,			self :  List[Any]       ):
 from torch import _softmax_backward_data

 if is_torch_less_than_1_11:
  return _softmax_backward_data(_UpperCAmelCase   ,			_UpperCAmelCase   ,			parent.dim   ,			self       )
 else:
  return _softmax_backward_data(_UpperCAmelCase   ,			_UpperCAmelCase   ,			parent.dim   ,			self.dtype       )

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  nn.Linear   ,			_UpperCAmelCase :  torch.LongTensor   ,			_UpperCAmelCase :  int = 0       ):
 lowerCAmelCase  							=      index.to(layer.weight.device       )
 lowerCAmelCase  							=      layer.weight.index_select(_UpperCAmelCase   ,			_UpperCAmelCase       ).clone().detach()
 if layer.bias is not None:
  if dim == 1:
   lowerCAmelCase  							=      layer.bias.clone().detach()
  else:
   lowerCAmelCase  							=      layer.bias[index].clone().detach()
 lowerCAmelCase  							=      list(layer.weight.size()       )
 lowerCAmelCase  							=      len(_UpperCAmelCase       )
 lowerCAmelCase  							=      nn.Linear(new_size[1]   ,			new_size[0]   ,			bias=layer.bias is not None       ).to(layer.weight.device       )
 lowerCAmelCase  							=      False
 new_layer.weight.copy_(W.contiguous()       )
 lowerCAmelCase  							=      True
 if layer.bias is not None:
  lowerCAmelCase  							=      False
  new_layer.bias.copy_(b.contiguous()       )
  lowerCAmelCase  							=      True
 return new_layer







class 	a				(					nn.Module   ):



 def __init__(			self							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  super().__init__()
  lowerCAmelCase  							=      nf
  lowerCAmelCase  							=      nn.Parameter(torch.empty(_snake_case							,    _snake_case			)			)
  lowerCAmelCase  							=      nn.Parameter(torch.zeros(_snake_case			)			)
  nn.init.normal_(self.weight							,    std=0.02			)



 def 					UpperCamelCase__   (			self							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      x.size()[:-1] + (self.nf,)
  lowerCAmelCase  							=      torch.addmm(self.bias							,    x.view(-1							,    x.size(-1			)			)							,    self.weight			)
  lowerCAmelCase  							=      x.view(_snake_case			)
  return x


def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  ConvaD   ,			_UpperCAmelCase :  torch.LongTensor   ,			_UpperCAmelCase :  int = 1       ):
 lowerCAmelCase  							=      index.to(layer.weight.device       )
 lowerCAmelCase  							=      layer.weight.index_select(_UpperCAmelCase   ,			_UpperCAmelCase       ).clone().detach()
 if dim == 0:
  lowerCAmelCase  							=      layer.bias.clone().detach()
 else:
  lowerCAmelCase  							=      layer.bias[index].clone().detach()
 lowerCAmelCase  							=      list(layer.weight.size()       )
 lowerCAmelCase  							=      len(_UpperCAmelCase       )
 lowerCAmelCase  							=      ConvaD(new_size[1]   ,			new_size[0]       ).to(layer.weight.device       )
 lowerCAmelCase  							=      False
 new_layer.weight.copy_(W.contiguous()       )
 lowerCAmelCase  							=      True
 lowerCAmelCase  							=      False
 new_layer.bias.copy_(b.contiguous()       )
 lowerCAmelCase  							=      True
 return new_layer

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  Union[nn.Linear, ConvaD]   ,			_UpperCAmelCase :  torch.LongTensor   ,			_UpperCAmelCase :  Optional[int] = None       ):
 if isinstance(_UpperCAmelCase   ,			nn.Linear       ):
  return prune_linear_layer(_UpperCAmelCase   ,			_UpperCAmelCase   ,			dim=0 if dim is None else dim       )
 elif isinstance(_UpperCAmelCase   ,			_UpperCAmelCase       ):
  return prune_convad_layer(_UpperCAmelCase   ,			_UpperCAmelCase   ,			dim=1 if dim is None else dim       )
 else:
  raise ValueError(F'Can\'t prune layer of class {layer.__class__}'       )

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  Callable[..., torch.Tensor]   ,			_UpperCAmelCase :  int   ,			_UpperCAmelCase :  int   ,			*_UpperCAmelCase :  Dict       ):
 assert len(_UpperCAmelCase       ) > 0, F'{input_tensors} has to be a tuple/list of tensors'

 # inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
 lowerCAmelCase  							=      len(inspect.signature(_UpperCAmelCase       ).parameters       )
 if num_args_in_forward_chunk_fn != len(_UpperCAmelCase       ):
  raise ValueError(
      F'forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_UpperCAmelCase       )} input '
      'tensors are given'       )

 if chunk_size > 0:
  lowerCAmelCase  							=      input_tensors[0].shape[chunk_dim]
  for input_tensor in input_tensors:
   if input_tensor.shape[chunk_dim] != tensor_shape:
    raise ValueError(
        F'All input tenors have to be of the same shape: {tensor_shape}, '
        F'found shape {input_tensor.shape[chunk_dim]}'       )

  if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
   raise ValueError(
       F'The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk '
       F'size {chunk_size}'       )

  lowerCAmelCase  							=      input_tensors[0].shape[chunk_dim] // chunk_size

  # chunk input tensor into tuples
  lowerCAmelCase  							=      tuple(input_tensor.chunk(_UpperCAmelCase   ,			dim=_UpperCAmelCase       ) for input_tensor in input_tensors       )
  # apply forward fn to every tuple
  lowerCAmelCase  							=      tuple(forward_fn(*_UpperCAmelCase       ) for input_tensors_chunk in zip(*_UpperCAmelCase       )       )
  # concatenate output at same dimension
  return torch.cat(_UpperCAmelCase   ,			dim=_UpperCAmelCase       )

 return forward_fn(*_UpperCAmelCase       )

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  List[int]   ,			_UpperCAmelCase :  int   ,			_UpperCAmelCase :  int   ,			_UpperCAmelCase :  Set[int]       ):
 lowerCAmelCase  							=      torch.ones(_UpperCAmelCase   ,			_UpperCAmelCase       )
 lowerCAmelCase  							=      set(_UpperCAmelCase       ) - already_pruned_heads  # Convert to set and remove already pruned heads
 for head in heads:
  # Compute how many pruned heads are before the head and move the index accordingly
  lowerCAmelCase  							=      head - sum(1 if h < head else 0 for h in already_pruned_heads       )
  lowerCAmelCase  							=      0
 lowerCAmelCase  							=      mask.view(-1       ).contiguous().eq(1       )
 lowerCAmelCase  							=      torch.arange(len(_UpperCAmelCase       )       )[mask].long()
 return heads, index

def 						_SCREAMING_SNAKE_CASE       (*_UpperCAmelCase :  Union[torch.Tensor, List[torch.Tensor]]   ,			_UpperCAmelCase :  Optional[str] = None       ):
 return torch.meshgrid(*_UpperCAmelCase   ,			indexing=_UpperCAmelCase       )




def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  torch.Tensor       ):
 return tensor.device, storage_ptr(_UpperCAmelCase       ), storage_size(_UpperCAmelCase       )












"""simple docstring"""
import os
import sys
import time

from . import transposition_cipher as trans_cipher

def 						_SCREAMING_SNAKE_CASE       ():
 lowerCAmelCase  							=      'Prehistoric Men.txt'
 lowerCAmelCase  							=      'Output.txt'
 lowerCAmelCase  							=      int(input('Enter key: '       )       )
 lowerCAmelCase  							=      input('Encrypt/Decrypt [e/d]: '       )

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

 lowerCAmelCase  							=      time.time()
 if mode.lower().startswith('e'       ):
  with open(_UpperCAmelCase       ) as f:
   lowerCAmelCase  							=      f.read()
  lowerCAmelCase  							=      trans_cipher.encrypt_message(_UpperCAmelCase   ,			_UpperCAmelCase       )
 elif mode.lower().startswith('d'       ):
  with open(_UpperCAmelCase       ) as f:
   lowerCAmelCase  							=      f.read()
  lowerCAmelCase  							=      trans_cipher.decrypt_message(_UpperCAmelCase   ,			_UpperCAmelCase       )

 with open(_UpperCAmelCase   ,			'w'       ) as output_obj:
  output_obj.write(_UpperCAmelCase       )

 lowerCAmelCase  							=      round(time.time() - start_time   ,			2       )
 print(('Done (', total_time, 'seconds )')       )


if __name__ == "__main__":
       main()





