code: 
  
  
  
  
  
  
  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
  
  
  __A  							=  [nn.LayerNorm]
  
  __A  							=  logging.get_logger(__name__)
  
  __A  							=  version.parse(version.parse(torch.__version__).base_version)
  
  __A  							=  parsed_torch_version_base >= version.parse("2.0")
  __A  							=  parsed_torch_version_base >= version.parse("1.12")
  __A  							=  parsed_torch_version_base >= version.parse("1.11")
  __A  							=  parsed_torch_version_base < version.parse("1.11")
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Tuple     ,			A_:	int     ,			A_:	List[Any]     ,			A_:	Optional[int]     ,			self:	int		)  ->							Union[str, Any]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         from torch import _softmax_backward_data
  
         if is_torch_less_than_1_11:
                return _softmax_backward_data(A_     ,			A_     ,			parent.dim     ,			self		)
         else:
                return _softmax_backward_data(A_     ,			A_     ,			parent.dim     ,			self.dtype		)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	nn.Linear     ,			A_:	torch.LongTensor     ,			A_:	int = 0		)  ->							nn.Linear:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=index.to(layer.weight.device		)
         __UpperCAmelCase    							=layer.weight.index_select(A_     ,			A_		).clone().detach()
         if layer.bias is not None:
                if dim == 1:
                       __UpperCAmelCase    							=layer.bias.clone().detach()
                else:
                       __UpperCAmelCase    							=layer.bias[index].clone().detach()
         __UpperCAmelCase    							=list(layer.weight.size()		)
         __UpperCAmelCase    							=len(A_		)
         __UpperCAmelCase    							=nn.Linear(new_size[1]     ,			new_size[0]     ,			bias=layer.bias is not None		).to(layer.weight.device		)
         __UpperCAmelCase    							=False
         new_layer.weight.copy_(W.contiguous()		)
         __UpperCAmelCase    							=True
         if layer.bias is not None:
                __UpperCAmelCase    							=False
                new_layer.bias.copy_(b.contiguous()		)
                __UpperCAmelCase    							=True
         return new_layer
  
  
  
  class 							_A (    nn.Module							):
      """simple docstring"""
  
      def __init__(							self			:    List[str]   ,    __SCREAMING_SNAKE_CASE			:    int   ,    __SCREAMING_SNAKE_CASE			:    Optional[Any]			)			->       str:
             super().__init__()
             __UpperCAmelCase    							=nf
             __UpperCAmelCase    							=nn.Parameter(torch.empty(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			)			)
             __UpperCAmelCase    							=nn.Parameter(torch.zeros(__SCREAMING_SNAKE_CASE			)			)
             nn.init.normal_(self.weight   ,    std=0.02			)
  
  
  
      def _a       (							self			:    str   ,    __SCREAMING_SNAKE_CASE			:    Dict			)			->       Union[str, Any]:
             __UpperCAmelCase    							=x.size()[:-1] + (self.nf,)
             __UpperCAmelCase    							=torch.addmm(self.bias   ,    x.view(-1   ,    x.size(-1			)			)   ,    self.weight			)
             __UpperCAmelCase    							=x.view(__SCREAMING_SNAKE_CASE			)
             return x
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	ConvaD     ,			A_:	torch.LongTensor     ,			A_:	int = 1		)  ->							ConvaD:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=index.to(layer.weight.device		)
         __UpperCAmelCase    							=layer.weight.index_select(A_     ,			A_		).clone().detach()
         if dim == 0:
                __UpperCAmelCase    							=layer.bias.clone().detach()
         else:
                __UpperCAmelCase    							=layer.bias[index].clone().detach()
         __UpperCAmelCase    							=list(layer.weight.size()		)
         __UpperCAmelCase    							=len(A_		)
         __UpperCAmelCase    							=ConvaD(new_size[1]     ,			new_size[0]		).to(layer.weight.device		)
         __UpperCAmelCase    							=False
         new_layer.weight.copy_(W.contiguous()		)
         __UpperCAmelCase    							=True
         __UpperCAmelCase    							=False
         new_layer.bias.copy_(b.contiguous()		)
         __UpperCAmelCase    							=True
         return new_layer
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Union[nn.Linear, ConvaD]     ,			A_:	torch.LongTensor     ,			A_:	Optional[int] = None		)  ->							Union[nn.Linear, ConvaD]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         if isinstance(A_     ,			nn.Linear		):
                return prune_linear_layer(A_     ,			A_     ,			dim=0 if dim is None else dim		)
         elif isinstance(A_     ,			A_		):
                return prune_convad_layer(A_     ,			A_     ,			dim=1 if dim is None else dim		)
         else:
                raise ValueError(F'''Can\'t prune layer of class {layer.__class__}'''		)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Callable[..., torch.Tensor]     ,			A_:	int     ,			A_:	int     ,			*A_:	Optional[Any]		)  ->							torch.Tensor:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         assert len(A_		) > 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
         __UpperCAmelCase    							=len(inspect.signature(A_		).parameters		)
         if num_args_in_forward_chunk_fn != len(A_		):
                raise ValueError(
                    F'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(A_		)} input '''
                    """tensors are given"""		)
  
         if chunk_size > 0:
                __UpperCAmelCase    							=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}'''		)
  
                __UpperCAmelCase    							=input_tensors[0].shape[chunk_dim] // chunk_size
  
                # chunk input tensor into tuples
                __UpperCAmelCase    							=tuple(input_tensor.chunk(A_     ,			dim=A_		) for input_tensor in input_tensors		)
                # apply forward fn to every tuple
                __UpperCAmelCase    							=tuple(forward_fn(*A_		) for input_tensors_chunk in zip(*A_		)		)
                # concatenate output at same dimension
                return torch.cat(A_     ,			dim=A_		)
  
         return forward_fn(*A_		)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	List[int]     ,			A_:	int     ,			A_:	int     ,			A_:	Set[int]		)  ->							Tuple[Set[int], torch.LongTensor]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=torch.ones(A_     ,			A_		)
         __UpperCAmelCase    							=set(A_		) - 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
                __UpperCAmelCase    							=head - sum(1 if h < head else 0 for h in already_pruned_heads		)
                __UpperCAmelCase    							=0
         __UpperCAmelCase    							=mask.view(-1		).contiguous().eq(1		)
         __UpperCAmelCase    							=torch.arange(len(A_		)		)[mask].long()
         return heads, index
  
  
  
  
  
  
  
  def 			lowercase__  (				*A_:	Union[torch.Tensor, List[torch.Tensor]]     ,			A_:	Optional[str] = None		)  ->							Tuple[torch.Tensor, ...]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         return torch.meshgrid(*A_     ,			indexing=A_		)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	torch.Tensor		)  ->							Tuple[torch.device, int, int]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         return tensor.device, storage_ptr(A_		), storage_size(A_		)
  
  
  
  
  
style_context: 
  
  
  
  
  
  
  from collections import namedtuple
  
  __A  							=  namedtuple("from_to", "from_ to")
  
  __A  							=  {
      "millimeter": "mm",
      "centimeter": "cm",
      "meter": "m",
      "kilometer": "km",
      "inch": "in",
      "inche": "in",  # Trailing 's' has been stripped off
      "feet": "ft",
      "foot": "ft",
      "yard": "yd",
      "mile": "mi",
  }
  
  __A  							=  {
      "mm": from_to(0.001, 10_00),
      "cm": from_to(0.01, 1_00),
      "m": from_to(1, 1),
      "km": from_to(10_00, 0.001),
      "in": from_to(0.0254, 39.3701),
      "ft": from_to(0.3048, 3.2_8084),
      "yd": from_to(0.9144, 1.0_9361),
      "mi": from_to(1609.34, 0.0_0062_1371),
  }
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	float     ,			A_:	str     ,			A_:	str		)  ->							float:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=from_type.lower().rstrip("""s"""		)
         __UpperCAmelCase    							=TYPE_CONVERSION.get(A_     ,			A_		)
         __UpperCAmelCase    							=to_type.lower().rstrip("""s"""		)
         __UpperCAmelCase    							=TYPE_CONVERSION.get(A_     ,			A_		)
         if new_from not in METRIC_CONVERSION:
                __UpperCAmelCase    							=(
                    F'''Invalid \'from_type\' value: {from_type!r}.\n'''
                    F'''Conversion abbreviations are: {", ".join(A_		)}'''
                )
                raise ValueError(A_		)
         if new_to not in METRIC_CONVERSION:
                __UpperCAmelCase    							=(
                    F'''Invalid \'to_type\' value: {to_type!r}.\n'''
                    F'''Conversion abbreviations are: {", ".join(A_		)}'''
                )
                raise ValueError(A_		)
         return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to
  
  
  if __name__ == "__main__":
     import doctest
  
     doctest.testmod()
  
  
  
  
  