

"""simple docstring"""




from typing import List, Optional, Tuple, Union

import torch
from torch import nn
from torch.nn import CrossEntropyLoss

from ... import AutoBackbone
from ...modeling_outputs import SemanticSegmenterOutput
from ...modeling_utils import PreTrainedModel
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from ...utils.backbone_utils import BackboneMixin
from .configuration_upernet import UperNetConfig


__UpperCAmelCase =				[
    'openmmlab/upernet-convnext-tiny',
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
__UpperCAmelCase =				'UperNetConfig'







class      __lowercase			(    nn.Module					):

  def __init__(  self		:			Union[str, Any]					,A		:			int					,A		:			int					,A		:			Union[int, Tuple[int, int]]					,A		:			Union[int, Tuple[int, int], str] = 0					,A		:			bool = False					,A		:			Union[int, Tuple[int, int]] = 1					,):






   '''simple docstring'''
   super().__init__()
   UpperCAmelCase__					:  List[str]					   =					nn.Convad(
       in_channels=A					,out_channels=A					,kernel_size=A					,padding=A					,bias=A					,dilation=A					,)
   UpperCAmelCase__					:  int					   =					nn.BatchNormad(A    )
   UpperCAmelCase__					:  Any					   =					nn.ReLU()




  def    __lowercase       (  self		:			int					,A		:			torch.Tensor    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					self.conv(A    )
   UpperCAmelCase__					:  Tuple					   =					self.batch_norm(A    )
   UpperCAmelCase__					:  Optional[int]					   =					self.activation(A    )

   return output







class      __lowercase			(    nn.Module					):

  def __init__(  self		:			str					,A		:			int					,A		:			int					,A		:			int    ):






   '''simple docstring'''
   super().__init__()
   UpperCAmelCase__					:  Dict					   =					[
       nn.AdaptiveAvgPoolad(A    ),
       UperNetConvModule(A					,A					,kernel_size=1    ),
   ]
   for i, layer in enumerate(self.layers    ):
    self.add_module(str(A    )					,A    )




  def    __lowercase       (  self		:			Any					,A		:			torch.Tensor    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					input
   for layer in self.layers:
    UpperCAmelCase__					:  int					   =					layer(A    )
   return hidden_state







class      __lowercase			(    nn.Module					):

  def __init__(  self		:			List[Any]					,A		:			Tuple[int, ...]					,A		:			int					,A		:			int					,A		:			bool    ):






   '''simple docstring'''
   super().__init__()
   UpperCAmelCase__					:  Union[str, Any]					   =					pool_scales
   UpperCAmelCase__					:  Optional[Any]					   =					align_corners
   UpperCAmelCase__					:  str					   =					in_channels
   UpperCAmelCase__					:  Optional[int]					   =					channels
   UpperCAmelCase__					:  List[str]					   =					[]
   for i, pool_scale in enumerate(A    ):
    UpperCAmelCase__					:  Any					   =					UperNetPyramidPoolingBlock(pool_scale=A					,in_channels=A					,channels=A    )
    self.blocks.append(A    )
    self.add_module(str(A    )					,A    )




  def    __lowercase       (  self		:			Optional[int]					,A		:			torch.Tensor    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[str]					   =					[]
   for ppm in self.blocks:
    UpperCAmelCase__					:  Dict					   =					ppm(A    )
    UpperCAmelCase__					:  str					   =					nn.functional.interpolate(
        A					,size=x.size()[2:]					,mode="""bilinear"""					,align_corners=self.align_corners    )
    ppm_outs.append(A    )
   return ppm_outs







class      __lowercase			(    nn.Module					):

  def __init__(  self		:			List[Any]					,A		:			Optional[Any]					,A		:			List[str]    ):






   '''simple docstring'''
   super().__init__()

   UpperCAmelCase__					:  List[Any]					   =					config
   UpperCAmelCase__					:  List[Any]					   =					config.pool_scales  # e.g. (1, 2, 3, 6)
   UpperCAmelCase__					:  Tuple					   =					in_channels
   UpperCAmelCase__					:  Dict					   =					config.hidden_size
   UpperCAmelCase__					:  Optional[int]					   =					False
   UpperCAmelCase__					:  int					   =					nn.Convad(self.channels					,config.num_labels					,kernel_size=1    )

   # PSP Module
   UpperCAmelCase__					:  Tuple					   =					UperNetPyramidPoolingModule(
       self.pool_scales					,self.in_channels[-1]					,self.channels					,align_corners=self.align_corners					,)
   UpperCAmelCase__					:  Union[str, Any]					   =					UperNetConvModule(
       self.in_channels[-1] + len(self.pool_scales    ) * self.channels					,self.channels					,kernel_size=3					,padding=1					,)
   # FPN Module
   UpperCAmelCase__					:  Any					   =					nn.ModuleList()
   UpperCAmelCase__					:  List[str]					   =					nn.ModuleList()
   for in_channels in self.in_channels[:-1]:  # skip the top layer
    UpperCAmelCase__					:  List[str]					   =					UperNetConvModule(A					,self.channels					,kernel_size=1    )
    UpperCAmelCase__					:  int					   =					UperNetConvModule(self.channels					,self.channels					,kernel_size=3					,padding=1    )
    self.lateral_convs.append(A    )
    self.fpn_convs.append(A    )

   UpperCAmelCase__					:  Union[str, Any]					   =					UperNetConvModule(
       len(self.in_channels    ) * self.channels					,self.channels					,kernel_size=3					,padding=1					,)

  def    __lowercase       (  self		:			Dict    ):






   '''simple docstring'''
   self.apply(self._init_weights    )

  def    __lowercase       (  self		:			str					,A		:			Any    ):






   '''simple docstring'''
   if isinstance(A					,nn.Convad    ):
    module.weight.data.normal_(mean=0.0					,std=self.config.initializer_range    )
    if module.bias is not None:
     module.bias.data.zero_()

  def    __lowercase       (  self		:			Any					,A		:			List[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[Any]					   =					inputs[-1]
   UpperCAmelCase__					:  Optional[Any]					   =					[x]
   psp_outs.extend(self.psp_modules(A    )    )
   UpperCAmelCase__					:  Optional[Any]					   =					torch.cat(A					,dim=1    )
   UpperCAmelCase__					:  int					   =					self.bottleneck(A    )

   return output




  def    __lowercase       (  self		:			str					,A		:			torch.Tensor    ):






   '''simple docstring'''
   # build laterals
   UpperCAmelCase__					:  str					   =					[lateral_conv(encoder_hidden_states[i]    ) for i, lateral_conv in enumerate(self.lateral_convs    )]

   laterals.append(self.psp_forward(A    )    )

   # build top-down path
   UpperCAmelCase__					:  int					   =					len(A    )
   for i in range(used_backbone_levels - 1					,0					,-1    ):
    UpperCAmelCase__					:  Optional[Any]					   =					laterals[i - 1].shape[2:]
    UpperCAmelCase__					:  List[Any]					   =					laterals[i - 1] + nn.functional.interpolate(
        laterals[i]					,size=A					,mode="""bilinear"""					,align_corners=self.align_corners    )

   # build outputs
   UpperCAmelCase__					:  Union[str, Any]					   =					[self.fpn_convs[i](laterals[i]    ) for i in range(used_backbone_levels - 1    )]
   # append psp feature
   fpn_outs.append(laterals[-1]    )

   for i in range(used_backbone_levels - 1					,0					,-1    ):
    UpperCAmelCase__					:  Tuple					   =					nn.functional.interpolate(
        fpn_outs[i]					,size=fpn_outs[0].shape[2:]					,mode="""bilinear"""					,align_corners=self.align_corners    )
   UpperCAmelCase__					:  int					   =					torch.cat(A					,dim=1    )
   UpperCAmelCase__					:  Any					   =					self.fpn_bottleneck(A    )
   UpperCAmelCase__					:  Union[str, Any]					   =					self.classifier(A    )

   return output







class      __lowercase			(    nn.Module					):

  def __init__(  self		:			Optional[int]					,A		:			Dict					,A		:			int = 2					,A		:			int = 3					,A		:			Union[int, Tuple[int, int]] = 1    ):






   '''simple docstring'''
   super().__init__()

   UpperCAmelCase__					:  Union[str, Any]					   =					config
   UpperCAmelCase__					:  Union[str, Any]					   =					config.auxiliary_in_channels
   UpperCAmelCase__					:  Dict					   =					config.auxiliary_channels
   UpperCAmelCase__					:  int					   =					config.auxiliary_num_convs
   UpperCAmelCase__					:  Optional[int]					   =					config.auxiliary_concat_input
   UpperCAmelCase__					:  Optional[Any]					   =					in_index

   UpperCAmelCase__					:  Union[str, Any]					   =					(kernel_size // 2) * dilation
   UpperCAmelCase__					:  Optional[Any]					   =					[]
   convs.append(
       UperNetConvModule(
           self.in_channels					,self.channels					,kernel_size=A					,padding=A					,dilation=A    )    )
   for i in range(self.num_convs - 1    ):
    convs.append(
        UperNetConvModule(
            self.channels					,self.channels					,kernel_size=A					,padding=A					,dilation=A    )    )
   if self.num_convs == 0:
    UpperCAmelCase__					:  int					   =					nn.Identity()
   else:
    UpperCAmelCase__					:  Tuple					   =					nn.Sequential(*A    )
   if self.concat_input:
    UpperCAmelCase__					:  List[Any]					   =					UperNetConvModule(
        self.in_channels + self.channels					,self.channels					,kernel_size=A					,padding=kernel_size // 2    )

   UpperCAmelCase__					:  Tuple					   =					nn.Convad(self.channels					,config.num_labels					,kernel_size=1    )

  def    __lowercase       (  self		:			Union[str, Any]    ):






   '''simple docstring'''
   self.apply(self._init_weights    )

  def    __lowercase       (  self		:			int					,A		:			Tuple    ):






   '''simple docstring'''
   if isinstance(A					,nn.Convad    ):
    module.weight.data.normal_(mean=0.0					,std=self.config.initializer_range    )
    if module.bias is not None:
     module.bias.data.zero_()




  def    __lowercase       (  self		:			List[Any]					,A		:			torch.Tensor    ):






   '''simple docstring'''
   # just take the relevant feature maps
   UpperCAmelCase__					:  int					   =					encoder_hidden_states[self.in_index]
   UpperCAmelCase__					:  Union[str, Any]					   =					self.convs(A    )
   if self.concat_input:
    UpperCAmelCase__					:  Dict					   =					self.conv_cat(torch.cat([hidden_states, output]					,dim=1    )    )
   UpperCAmelCase__					:  Optional[int]					   =					self.classifier(A    )
   return output







class      __lowercase			(    __lowerCamelCase					):
  snake_case_				       =       UperNetConfig
  snake_case_				       =       """pixel_values"""
  snake_case_				       =       True

  def    __lowercase       (  self		:			Tuple					,A		:			Optional[Any]    ):






   '''simple docstring'''
   if isinstance(A					,A    ):
    module.backbone.init_weights()
    module.decode_head.init_weights()
    module.auxiliary_head.init_weights()

  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   self.backbone.init_weights()
   self.decode_head.init_weights()
   self.auxiliary_head.init_weights()




  def    __lowercase       (  self		:			Tuple					,A		:			int					,A		:			Optional[Any]=False    ):






   '''simple docstring'''
   if isinstance(A					,A    ):
    UpperCAmelCase__					:  str					   =					value







__UpperCAmelCase =				r'\n    Parameters:\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n        config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

__UpperCAmelCase =				r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n        output_attentions (`bool`, *optional*):\n            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n            `attentions` under returned tensors for more detail.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n            returned tensors for more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'







@add_start_docstrings(
    """UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes."""	,			__lowerCamelCase	,			)
class      __lowercase			(    __lowerCamelCase					):

  def __init__(  self		:			Dict					,A		:			str    ):






   '''simple docstring'''
   super().__init__(A    )

   UpperCAmelCase__					:  List[str]					   =					AutoBackbone.from_config(config.backbone_config    )

   # Semantic segmentation head(s)
   UpperCAmelCase__					:  Tuple					   =					UperNetHead(A					,in_channels=self.backbone.channels    )
   UpperCAmelCase__					:  int					   =					UperNetFCNHead(A    ) if config.use_auxiliary_head else None

   # Initialize weights and apply final processing
   self.post_init()




  @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length"""    )    )
  @replace_return_docstrings(output_type=A					,config_class=_CONFIG_FOR_DOC    )
  def    __lowercase       (  self		:			int					,A		:			Optional[torch.Tensor] = None					,A		:			Optional[bool] = None					,A		:			Optional[bool] = None					,A		:			Optional[torch.Tensor] = None					,A		:			Optional[bool] = None					,):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					return_dict if return_dict is not None else self.config.use_return_dict
   UpperCAmelCase__					:  Dict					   =					(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__					:  List[Any]					   =					output_attentions if output_attentions is not None else self.config.output_attentions

   UpperCAmelCase__					:  Optional[Any]					   =					self.backbone.forward_with_filtered_kwargs(
       A					,output_hidden_states=A					,output_attentions=A    )
   UpperCAmelCase__					:  Any					   =					outputs.feature_maps

   UpperCAmelCase__					:  Optional[Any]					   =					self.decode_head(A    )
   UpperCAmelCase__					:  str					   =					nn.functional.interpolate(A					,size=pixel_values.shape[2:]					,mode="""bilinear"""					,align_corners=A    )

   UpperCAmelCase__					:  Dict					   =					None
   if self.auxiliary_head is not None:
    UpperCAmelCase__					:  Optional[int]					   =					self.auxiliary_head(A    )
    UpperCAmelCase__					:  Union[str, Any]					   =					nn.functional.interpolate(
        A					,size=pixel_values.shape[2:]					,mode="""bilinear"""					,align_corners=A    )

   UpperCAmelCase__					:  int					   =					None
   if labels is not None:
    if self.config.num_labels == 1:
     raise ValueError("""The number of labels should be greater than one"""    )
    else:
     # compute weighted loss
     UpperCAmelCase__					:  List[str]					   =					CrossEntropyLoss(ignore_index=self.config.loss_ignore_index    )
     UpperCAmelCase__					:  Any					   =					loss_fct(A					,A    )
     UpperCAmelCase__					:  List[str]					   =					loss_fct(A					,A    )
     UpperCAmelCase__					:  List[str]					   =					main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

   if not return_dict:
    if output_hidden_states:
     UpperCAmelCase__					:  Dict					   =					(logits,) + outputs[1:]
    else:
     UpperCAmelCase__					:  Dict					   =					(logits,) + outputs[2:]
    return ((loss,) + output) if loss is not None else output

   return SemanticSegmenterOutput(
       loss=A					,logits=A					,hidden_states=outputs.hidden_states					,attentions=outputs.attentions					,)








"""simple docstring"""




from __future__ import annotations

from typing import Any







class      __lowercase			:

  def __init__(  self		:			Optional[int]					,A		:			int					,A		:			int					,A		:			float = 0    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  str					   =					row, column
   UpperCAmelCase__					:  int					   =					[[default_value for c in range(A    )] for r in range(A    )]

  def __str__(  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					f"Matrix consist of {self.row} rows and {self.column} columns\n"

   # Make string identifier
   UpperCAmelCase__					:  Optional[Any]					   =					0
   for row_vector in self.array:
    for obj in row_vector:
     UpperCAmelCase__					:  Optional[int]					   =					max(A					,len(str(A    )    )    )
   UpperCAmelCase__					:  int					   =					f"%{max_element_length}s"

   # Make string and return
   def single_line(A		:			list[float]    ) -> str:
    nonlocal string_format_identifier
    UpperCAmelCase__					:  List[str]					   =					"""["""
    line += ", ".join(string_format_identifier % (obj,) for obj in row_vector    )
    line += "]"
    return line

   s += "\n".join(single_line(A    ) for row_vector in self.array    )
   return s

  def __repr__(  self		:			Dict    ):






   '''simple docstring'''
   return str(self    )

  def    __lowercase       (  self		:			int					,A		:			tuple[int, int]    ):






   '''simple docstring'''
   if not (isinstance(A					,(list, tuple)    ) and len(A    ) == 2):
    return False
   elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column):
    return False
   else:
    return True

  def __getitem__(  self		:			Optional[int]					,A		:			tuple[int, int]    ):






   '''simple docstring'''
   assert self.validate_indicies(A    )
   return self.array[loc[0]][loc[1]]

  def __setitem__(  self		:			List[Any]					,A		:			tuple[int, int]					,A		:			float    ):






   '''simple docstring'''
   assert self.validate_indicies(A    )
   UpperCAmelCase__					:  str					   =					value

  def __add__(  self		:			Any					,A		:			Matrix    ):






   '''simple docstring'''
   assert isinstance(A					,A    )
   assert self.row == another.row and self.column == another.column

   # Add
   UpperCAmelCase__					:  Optional[int]					   =					Matrix(self.row					,self.column    )
   for r in range(self.row    ):
    for c in range(self.column    ):
     UpperCAmelCase__					:  Union[str, Any]					   =					self[r, c] + another[r, c]
   return result

  def __neg__(  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					Matrix(self.row					,self.column    )
   for r in range(self.row    ):
    for c in range(self.column    ):
     UpperCAmelCase__					:  int					   =					-self[r, c]
   return result

  def __sub__(  self		:			str					,A		:			Matrix    ):






   '''simple docstring'''
   return self + (-another)

  def __mul__(  self		:			Union[str, Any]					,A		:			int | float | Matrix    ):






   '''simple docstring'''

   if isinstance(A					,(int, float)    ):  # Scalar multiplication
    UpperCAmelCase__					:  Dict					   =					Matrix(self.row					,self.column    )
    for r in range(self.row    ):
     for c in range(self.column    ):
      UpperCAmelCase__					:  Any					   =					self[r, c] * another
    return result
   elif isinstance(A					,A    ):  # Matrix multiplication
    assert self.column == another.row
    UpperCAmelCase__					:  Optional[int]					   =					Matrix(self.row					,another.column    )
    for r in range(self.row    ):
     for c in range(another.column    ):
      for i in range(self.column    ):
       result[r, c] += self[r, i] * another[i, c]
    return result
   else:
    UpperCAmelCase__					:  Union[str, Any]					   =					f"Unsupported type given for another ({type(A    )})"
    raise TypeError(A    )

  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					Matrix(self.column					,self.row    )
   for r in range(self.row    ):
    for c in range(self.column    ):
     UpperCAmelCase__					:  Optional[int]					   =					self[r, c]
   return result




  def    __lowercase       (  self		:			Any					,A		:			Matrix					,A		:			Matrix    ):






   '''simple docstring'''
   assert isinstance(A					,A    ) and isinstance(A					,A    )
   assert self.row == self.column == u.row == v.row  # u, v should be column vector
   assert u.column == v.column == 1  # u, v should be column vector

   # Calculate
   UpperCAmelCase__					:  Optional[Any]					   =					v.transpose()
   UpperCAmelCase__					:  Union[str, Any]					   =					(v_t * self * u)[0, 0] + 1
   if numerator_factor == 0:
    return None  # It's not invertable
   return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor))


# Testing
if __name__ == "__main__":
      def   lowerCAmelCase						(					):
       '''simple docstring'''





       UpperCAmelCase__					:  List[str]					   =					Matrix(3							,    3							,    0      )
       for i in range(3      ):
        UpperCAmelCase__					:  List[str]					   =					1
       print(F"a^(-1) is {ainv}"      )
       # u, v
       UpperCAmelCase__					:  List[Any]					   =					Matrix(3							,    1							,    0      )
       UpperCAmelCase__   ,      UpperCAmelCase__   ,      UpperCAmelCase__					:  Optional[int]					   =					1, 2, -3
       UpperCAmelCase__					:  List[str]					   =					Matrix(3							,    1							,    0      )
       UpperCAmelCase__   ,      UpperCAmelCase__   ,      UpperCAmelCase__					:  Tuple					   =					4, -2, 5
       print(F"u is {u}"      )
       print(F"v is {v}"      )
       print(F"uv^T is {u * v.transpose()}"      )
       # Sherman Morrison
       print(F"(a + uv^T)^(-1) is {ainv.sherman_morrison(__UpperCamelCase							,    __UpperCamelCase      )}"      )
      def   lowerCAmelCase						(					):
       '''simple docstring'''





       import doctest

       doctest.testmod()




      testa()





