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
  
  
  SCREAMING_SNAKE_CASE__		:       Union[str, Any]   							=							[nn.LayerNorm]
  
  SCREAMING_SNAKE_CASE__		:       List[Any]   							=							logging.get_logger(__name__)
  
  SCREAMING_SNAKE_CASE__		:       Dict   							=							version.parse(version.parse(torch.__version__).base_version)
  
  SCREAMING_SNAKE_CASE__		:       List[str]   							=							parsed_torch_version_base >= version.parse("""2.0""")
  SCREAMING_SNAKE_CASE__		:       Optional[Any]   							=							parsed_torch_version_base >= version.parse("""1.12""")
  SCREAMING_SNAKE_CASE__		:       Optional[int]   							=							parsed_torch_version_base >= version.parse("""1.11""")
  SCREAMING_SNAKE_CASE__		:       Dict   							=							parsed_torch_version_base < version.parse("""1.11""")
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       self					)   -> Union[str, Any]:
  
       '''simple docstring'''
       from torch import _softmax_backward_data
  
       if is_torch_less_than_1_11:
            return _softmax_backward_data(__lowerCamelCase   ,       __lowerCamelCase   ,       parent.dim   ,       self					)
       else:
            return _softmax_backward_data(__lowerCamelCase   ,       __lowerCamelCase   ,       parent.dim   ,       self.dtype					)
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase = 0					)   -> Any:
  
       '''simple docstring'''
       UpperCAmelCase__			:     Union[str, Any]										= index.to(layer.weight.device					)
       UpperCAmelCase__			:     int										= layer.weight.index_select(__lowerCamelCase   ,       __lowerCamelCase					).clone().detach()
       if layer.bias is not None:
            if dim == 1:
                 UpperCAmelCase__			:     Tuple										= layer.bias.clone().detach()
            else:
                 UpperCAmelCase__			:     Dict										= layer.bias[index].clone().detach()
       UpperCAmelCase__			:     Optional[Any]										= list(layer.weight.size()					)
       UpperCAmelCase__			:     Optional[int]										= len(__lowerCamelCase					)
       UpperCAmelCase__			:     List[str]										= nn.Linear(new_size[1]   ,       new_size[0]   ,       bias=layer.bias is not None					).to(layer.weight.device					)
       UpperCAmelCase__			:     str										= False
       new_layer.weight.copy_(W.contiguous()					)
       UpperCAmelCase__			:     Tuple										= True
       if layer.bias is not None:
            UpperCAmelCase__			:     Tuple										= False
            new_layer.bias.copy_(b.contiguous()					)
            UpperCAmelCase__			:     Tuple										= True
       return new_layer
  
  
  
  
  
  
  
  class 							UpperCAmelCase_     (		nn.Module		):
  
  
  
  
       def __init__(							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            super().__init__()
            UpperCAmelCase__			:     List[str]										= nf
            UpperCAmelCase__			:     Any										= nn.Parameter(torch.empty(lowerCamelCase__  ,	lowerCamelCase__		)		)
            UpperCAmelCase__			:     Union[str, Any]										= nn.Parameter(torch.zeros(lowerCamelCase__		)		)
            nn.init.normal_(self.weight  ,	std=0.0_2		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase		):
            UpperCAmelCase__			:     int										= x.size()[:-1] + (self.nf,)
            UpperCAmelCase__			:     int										= torch.addmm(self.bias  ,	x.view(-1  ,	x.size(-1		)		)  ,	self.weight		)
            UpperCAmelCase__			:     Any										= x.view(lowerCamelCase__		)
            return x
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase = 1					)   -> Union[str, Any]:
  
       '''simple docstring'''
       UpperCAmelCase__			:     List[str]										= index.to(layer.weight.device					)
       UpperCAmelCase__			:     Optional[Any]										= layer.weight.index_select(__lowerCamelCase   ,       __lowerCamelCase					).clone().detach()
       if dim == 0:
            UpperCAmelCase__			:     Union[str, Any]										= layer.bias.clone().detach()
       else:
            UpperCAmelCase__			:     List[str]										= layer.bias[index].clone().detach()
       UpperCAmelCase__			:     Any										= list(layer.weight.size()					)
       UpperCAmelCase__			:     List[str]										= len(__lowerCamelCase					)
       UpperCAmelCase__			:     Union[str, Any]										= ConvaD(new_size[1]   ,       new_size[0]					).to(layer.weight.device					)
       UpperCAmelCase__			:     List[str]										= False
       new_layer.weight.copy_(W.contiguous()					)
       UpperCAmelCase__			:     Optional[Any]										= True
       UpperCAmelCase__			:     str										= False
       new_layer.bias.copy_(b.contiguous()					)
       UpperCAmelCase__			:     List[Any]										= True
       return new_layer
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase = None					)   -> Any:
  
       '''simple docstring'''
       if isinstance(__lowerCamelCase   ,       nn.Linear					):
            return prune_linear_layer(__lowerCamelCase   ,       __lowerCamelCase   ,       dim=0 if dim is None else dim					)
       elif isinstance(__lowerCamelCase   ,       __lowerCamelCase					):
            return prune_convad_layer(__lowerCamelCase   ,       __lowerCamelCase   ,       dim=1 if dim is None else dim					)
       else:
            raise ValueError(F"Can't prune layer of class {layer.__class__}"					)
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       *__lowerCamelCase					)   -> Union[str, Any]:
  
       '''simple docstring'''
       assert len(__lowerCamelCase					) > 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__			:     Optional[Any]										= len(inspect.signature(__lowerCamelCase					).parameters					)
       if num_args_in_forward_chunk_fn != len(__lowerCamelCase					):
            raise ValueError(
                F"forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(__lowerCamelCase					)} input "
                """tensors are given"""					)
  
       if chunk_size > 0:
            UpperCAmelCase__			:     Dict										= 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__			:     List[str]										= input_tensors[0].shape[chunk_dim] // chunk_size
  
            # chunk input tensor into tuples
            UpperCAmelCase__			:     List[Any]										= tuple(input_tensor.chunk(__lowerCamelCase   ,       dim=__lowerCamelCase					) for input_tensor in input_tensors					)
            # apply forward fn to every tuple
            UpperCAmelCase__			:     Optional[Any]										= tuple(forward_fn(*__lowerCamelCase					) for input_tensors_chunk in zip(*__lowerCamelCase					)					)
            # concatenate output at same dimension
            return torch.cat(__lowerCamelCase   ,       dim=__lowerCamelCase					)
  
       return forward_fn(*__lowerCamelCase					)
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase					)   -> Any:
  
       '''simple docstring'''
       UpperCAmelCase__			:     Optional[int]										= torch.ones(__lowerCamelCase   ,       __lowerCamelCase					)
       UpperCAmelCase__			:     int										= set(__lowerCamelCase					) - 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__			:     Any										= head - sum(1 if h < head else 0 for h in already_pruned_heads					)
            UpperCAmelCase__			:     Optional[Any]										= 0
       UpperCAmelCase__			:     Tuple										= mask.view(-1					).contiguous().eq(1					)
       UpperCAmelCase__			:     torch.LongTensor										= torch.arange(len(__lowerCamelCase					)					)[mask].long()
       return heads, index
  
  
  
  
  
  def    _lowerCamelCase	(	*__lowerCamelCase   ,       __lowerCamelCase = None					)   -> Optional[int]:
  
       '''simple docstring'''
       return torch.meshgrid(*__lowerCamelCase   ,       indexing=__lowerCamelCase					)
  
  
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase					)   -> Dict:
  
       '''simple docstring'''
       return tensor.device, storage_ptr(__lowerCamelCase					), storage_size(__lowerCamelCase					)
  
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  
  from ..utils import requires_backends
  
  
  SCREAMING_SNAKE_CASE__		:       int   							=							None
  
  
  
  
  
  
  
  class 							UpperCAmelCase_     :
  
  
  
  
       def __init__(							self  ,	*_lowerCAmelCase  ,	**_lowerCAmelCase		):
            requires_backends(self  ,	["""detectron2"""]		)
  
  
  
  
       @classmethod
       def      __UpperCAmelCase     (							cls  ,	*_lowerCAmelCase  ,	**_lowerCAmelCase		):
            requires_backends(cls  ,	["""detectron2"""]		)
  
  
  
  
  
  
  