





'''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_											=  [nn.LayerNorm]

UpperCAmelCase_											=  logging.get_logger(__name__)

UpperCAmelCase_											=  version.parse(version.parse(torch.__version__).base_version)

UpperCAmelCase_											=  parsed_torch_version_base >= version.parse("2.0")
UpperCAmelCase_											=  parsed_torch_version_base >= version.parse("1.12")
UpperCAmelCase_											=  parsed_torch_version_base >= version.parse("1.11")
UpperCAmelCase_											=  parsed_torch_version_base < version.parse("1.11")
def     A__					(						SCREAMING_SNAKE_CASE_      :			Optional[int]							,  SCREAMING_SNAKE_CASE_      :			Any							,  SCREAMING_SNAKE_CASE_      :			Union[str, Any]							,  SCREAMING_SNAKE_CASE_      :			Any							,  self      :			List[Any]	)     ->     Any:






   """simple docstring"""







   from torch import _softmax_backward_data

   if is_torch_less_than_1_11:
      return _softmax_backward_data(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_							,  parent.dim							,  self	)
   else:
      return _softmax_backward_data(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_							,  parent.dim							,  self.dtype	)
def     A__					(						SCREAMING_SNAKE_CASE_      :			nn.Linear							,  SCREAMING_SNAKE_CASE_      :			torch.LongTensor							,  SCREAMING_SNAKE_CASE_      :			int = 0	)     ->     nn.Linear:






   """simple docstring"""







   _UpperCAmelCase											=      index.to(layer.weight.device	)
   _UpperCAmelCase											=      layer.weight.index_select(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_	).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(SCREAMING_SNAKE_CASE_	)
   _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  __UpperCamelCase						(       nn.Module						):





     def __init__(    self							,       _UpperCamelCase							,       _UpperCamelCase				):
        super().__init__()
        _UpperCAmelCase											=      nf
        _UpperCAmelCase											=      nn.Parameter(torch.empty(_UpperCamelCase							,       _UpperCamelCase				)				)
        _UpperCAmelCase											=      nn.Parameter(torch.zeros(_UpperCamelCase				)				)
        nn.init.normal_(self.weight							,       std=0.02				)





     def 		UpperCamelCase(    self							,       _UpperCamelCase				):
        _UpperCAmelCase											=      x.size()[:-1] + (self.nf,)
        _UpperCAmelCase											=      torch.addmm(self.bias							,       x.view(-1							,       x.size(-1				)				)							,       self.weight				)
        _UpperCAmelCase											=      x.view(_UpperCamelCase				)
        return x




def     A__					(						SCREAMING_SNAKE_CASE_      :			ConvaD							,  SCREAMING_SNAKE_CASE_      :			torch.LongTensor							,  SCREAMING_SNAKE_CASE_      :			int = 1	)     ->     ConvaD:






   """simple docstring"""







   _UpperCAmelCase											=      index.to(layer.weight.device	)
   _UpperCAmelCase											=      layer.weight.index_select(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_	).clone().detach()
   if dim == 0:
      _UpperCAmelCase											=      layer.bias.clone().detach()
   else:
      _UpperCAmelCase											=      layer.bias[index].clone().detach()
   _UpperCAmelCase											=      list(layer.weight.size()	)
   _UpperCAmelCase											=      len(SCREAMING_SNAKE_CASE_	)
   _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     A__					(						SCREAMING_SNAKE_CASE_      :			Union[nn.Linear, ConvaD]							,  SCREAMING_SNAKE_CASE_      :			torch.LongTensor							,  SCREAMING_SNAKE_CASE_      :			Optional[int] = None	)     ->     Union[nn.Linear, ConvaD]:






   """simple docstring"""







   if isinstance(SCREAMING_SNAKE_CASE_							,  nn.Linear	):
      return prune_linear_layer(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_							,  dim=0 if dim is None else dim	)
   elif isinstance(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_	):
      return prune_convad_layer(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_							,  dim=1 if dim is None else dim	)
   else:
      raise ValueError(F'''Can\'t prune layer of class {layer.__class__}'''	)
def     A__					(						SCREAMING_SNAKE_CASE_      :			Callable[..., torch.Tensor]							,  SCREAMING_SNAKE_CASE_      :			int							,  SCREAMING_SNAKE_CASE_      :			int							,  *SCREAMING_SNAKE_CASE_      :			Optional[int]	)     ->     torch.Tensor:






   """simple docstring"""







   assert len(SCREAMING_SNAKE_CASE_	) > 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(SCREAMING_SNAKE_CASE_	).parameters	)
   if num_args_in_forward_chunk_fn != len(SCREAMING_SNAKE_CASE_	):
      raise ValueError(
          F'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(SCREAMING_SNAKE_CASE_	)} 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(SCREAMING_SNAKE_CASE_							,  dim=SCREAMING_SNAKE_CASE_	) for input_tensor in input_tensors	)
      # apply forward fn to every tuple
      _UpperCAmelCase											=      tuple(forward_fn(*SCREAMING_SNAKE_CASE_	) for input_tensors_chunk in zip(*SCREAMING_SNAKE_CASE_	)	)
      # concatenate output at same dimension
      return torch.cat(SCREAMING_SNAKE_CASE_							,  dim=SCREAMING_SNAKE_CASE_	)

   return forward_fn(*SCREAMING_SNAKE_CASE_	)
def     A__					(						SCREAMING_SNAKE_CASE_      :			List[int]							,  SCREAMING_SNAKE_CASE_      :			int							,  SCREAMING_SNAKE_CASE_      :			int							,  SCREAMING_SNAKE_CASE_      :			Set[int]	)     ->     Tuple[Set[int], torch.LongTensor]:






   """simple docstring"""







   _UpperCAmelCase											=      torch.ones(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_	)
   _UpperCAmelCase											=      set(SCREAMING_SNAKE_CASE_	) - 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(SCREAMING_SNAKE_CASE_	)	)[mask].long()
   return heads, index
def     A__					(						*SCREAMING_SNAKE_CASE_      :			Union[torch.Tensor, List[torch.Tensor]]							,  SCREAMING_SNAKE_CASE_      :			Optional[str] = None	)     ->     Tuple[torch.Tensor, ...]:






   """simple docstring"""







   return torch.meshgrid(*SCREAMING_SNAKE_CASE_							,  indexing=SCREAMING_SNAKE_CASE_	)
def     A__					(						SCREAMING_SNAKE_CASE_      :			torch.Tensor	)     ->     Tuple[torch.device, int, int]:






   """simple docstring"""







   return tensor.device, storage_ptr(SCREAMING_SNAKE_CASE_	), storage_size(SCREAMING_SNAKE_CASE_	)






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCAmelCase_											=  {
    "configuration_oneformer": ["ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "OneFormerConfig"],
    "processing_oneformer": ["OneFormerProcessor"],
}

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase_											=  ["OneFormerImageProcessor"]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase_											=  [
       "ONEFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
       "OneFormerForUniversalSegmentation",
       "OneFormerModel",
       "OneFormerPreTrainedModel",
   ]

if TYPE_CHECKING:
   from .configuration_oneformer import ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, OneFormerConfig
   from .processing_oneformer import OneFormerProcessor

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .image_processing_oneformer import OneFormerImageProcessor
   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_oneformer import (
          ONEFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          OneFormerForUniversalSegmentation,
          OneFormerModel,
          OneFormerPreTrainedModel,
      )


else:
   import sys

   UpperCAmelCase_											=  _LazyModule(__name__, globals()["__file__"], _import_structure)