


'''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      lowerCamelCase_  (      nn.Module ):






      def __init__(   self							:       List[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__     :						Union[str, Any]						=							nn.Convad(
              in_channels=_A		,		out_channels=_A		,		kernel_size=_A		,		padding=_A		,		bias=_A		,		dilation=_A		,		)
          UpperCAmelCase__     :						Union[str, Any]						=							nn.BatchNormad(_A					)
          UpperCAmelCase__     :						List[str]						=							nn.ReLU()






      def lowercase_				(   self							:       int		,		_A							:       torch.Tensor					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							self.conv(_A					)
          UpperCAmelCase__     :						Union[str, Any]						=							self.batch_norm(_A					)
          UpperCAmelCase__     :						Tuple						=							self.activation(_A					)

          return output




class      lowerCamelCase_  (      nn.Module ):






      def __init__(   self							:       Tuple		,		_A							:       int		,		_A							:       int		,		_A							:       int					):


          '''simple docstring'''


          super().__init__()
          UpperCAmelCase__     :						Optional[int]						=							[
              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							:       Dict		,		_A							:       torch.Tensor					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							input
          for layer in self.layers:
              UpperCAmelCase__     :						Any						=							layer(_A					)
          return hidden_state




class      lowerCamelCase_  (      nn.Module ):






      def __init__(   self							:       Optional[Any]		,		_A							:       Tuple[int, ...]		,		_A							:       int		,		_A							:       int		,		_A							:       bool					):


          '''simple docstring'''


          super().__init__()
          UpperCAmelCase__     :						Any						=							pool_scales
          UpperCAmelCase__     :						Optional[Any]						=							align_corners
          UpperCAmelCase__     :						Optional[Any]						=							in_channels
          UpperCAmelCase__     :						Tuple						=							channels
          UpperCAmelCase__     :						Union[str, Any]						=							[]
          for i, pool_scale in enumerate(_A					):
              UpperCAmelCase__     :						Optional[Any]						=							UperNetPyramidPoolingBlock(pool_scale=_A		,		in_channels=_A		,		channels=_A					)
              self.blocks.append(_A					)
              self.add_module(str(_A					)		,		_A					)






      def lowercase_				(   self							:       int		,		_A							:       torch.Tensor					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							[]
          for ppm in self.blocks:
              UpperCAmelCase__     :						Tuple						=							ppm(_A					)
              UpperCAmelCase__     :						int						=							nn.functional.interpolate(
                  _A		,		size=x.size()[2:]		,		mode='''bilinear'''		,		align_corners=self.align_corners					)
              ppm_outs.append(_A					)
          return ppm_outs




class      lowerCamelCase_  (      nn.Module ):






      def __init__(   self							:       List[str]		,		_A							:       Union[str, Any]		,		_A							:       Union[str, Any]					):


          '''simple docstring'''


          super().__init__()

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

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

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






      def lowercase_				(   self							:       List[str]					):


          '''simple docstring'''


          self.apply(self._init_weights					)






      def lowercase_				(   self							:       Dict		,		_A							:       str					):


          '''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							:       Any					):


          '''simple docstring'''


          UpperCAmelCase__     :						Tuple						=							inputs[-1]
          UpperCAmelCase__     :						List[str]						=							[x]
          psp_outs.extend(self.psp_modules(_A					)					)
          UpperCAmelCase__     :						Dict						=							torch.cat(_A		,		dim=1					)
          UpperCAmelCase__     :						Tuple						=							self.bottleneck(_A					)

          return output






      def lowercase_				(   self							:       str		,		_A							:       torch.Tensor					):


          '''simple docstring'''


          UpperCAmelCase__     :						Tuple						=							[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__     :						Dict						=							laterals[i - 1].shape[2:]
              UpperCAmelCase__     :						List[str]						=							laterals[i - 1] + nn.functional.interpolate(
                  laterals[i]		,		size=_A		,		mode='''bilinear'''		,		align_corners=self.align_corners					)

          # build outputs
          UpperCAmelCase__     :						List[str]						=							[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__     :						List[str]						=							nn.functional.interpolate(
                  fpn_outs[i]		,		size=fpn_outs[0].shape[2:]		,		mode='''bilinear'''		,		align_corners=self.align_corners					)
          UpperCAmelCase__     :						Optional[Any]						=							torch.cat(_A		,		dim=1					)
          UpperCAmelCase__     :						int						=							self.fpn_bottleneck(_A					)
          UpperCAmelCase__     :						List[str]						=							self.classifier(_A					)

          return output




class      lowerCamelCase_  (      nn.Module ):






      def __init__(   self							:       str		,		_A							:       List[Any]		,		_A							:       int = 2		,		_A							:       int = 3		,		_A							:       Union[int, Tuple[int, int]] = 1					):


          '''simple docstring'''


          super().__init__()

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

          UpperCAmelCase__     :						Union[str, Any]						=							(kernel_size // 2) * dilation
          UpperCAmelCase__     :						Optional[int]						=							[]
          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__     :						Union[str, Any]						=							nn.Identity()
          else:
              UpperCAmelCase__     :						str						=							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__     :						str						=							nn.Convad(self.channels		,		config.num_labels		,		kernel_size=1					)






      def lowercase_				(   self							:       Optional[Any]					):


          '''simple docstring'''


          self.apply(self._init_weights					)






      def lowercase_				(   self							:       Any		,		_A							:       Dict					):


          '''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[str]		,		_A							:       torch.Tensor					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[str]						=							encoder_hidden_states[self.in_index]
          UpperCAmelCase__     :						List[Any]						=							self.convs(_A					)
          if self.concat_input:
              UpperCAmelCase__     :						Dict						=							self.conv_cat(torch.cat([hidden_states, output]		,		dim=1					)					)
          UpperCAmelCase__     :						List[Any]						=							self.classifier(_A					)
          return output




class      lowerCamelCase_  (      __a ):
      lowerCAmelCase__							=	UperNetConfig
      lowerCAmelCase__							=	'pixel_values'
      lowerCAmelCase__							=	True






      def lowercase_				(   self							:       Union[str, Any]		,		_A							:       str					):


          '''simple docstring'''


          if isinstance(_A		,		_A					):
              module.backbone.init_weights()
              module.decode_head.init_weights()
              module.auxiliary_head.init_weights()






      def lowercase_				(   self							:       str					):


          '''simple docstring'''


          self.backbone.init_weights()
          self.decode_head.init_weights()
          self.auxiliary_head.init_weights()






      def lowercase_				(   self							:       Optional[int]		,		_A							:       int		,		_A							:       Union[str, Any]=False					):


          '''simple docstring'''


          if isinstance(_A		,		_A					):
              UpperCAmelCase__     :						Union[str, Any]						=							value

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

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




@add_start_docstrings(
    'UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.'	,			__a	,			)
class      lowerCamelCase_  (      __a ):






      def __init__(   self							:       Tuple		,		_A							:       List[Any]					):


          '''simple docstring'''


          super().__init__(_A					)

          UpperCAmelCase__     :						int						=							AutoBackbone.from_config(config.backbone_config					)

          # Semantic segmentation head(s)
          UpperCAmelCase__     :						List[str]						=							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							:       Union[str, Any]		,		_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__     :						int						=							(
              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__     :						Union[str, Any]						=							self.backbone.forward_with_filtered_kwargs(
              _A		,		output_hidden_states=_A		,		output_attentions=_A					)
          UpperCAmelCase__     :						Optional[Any]						=							outputs.feature_maps

          UpperCAmelCase__     :						Optional[Any]						=							self.decode_head(_A					)
          UpperCAmelCase__     :						Optional[int]						=							nn.functional.interpolate(_A		,		size=pixel_values.shape[2:]		,		mode='''bilinear'''		,		align_corners=_A					)

          UpperCAmelCase__     :						Optional[Any]						=							None
          if self.auxiliary_head is not None:
              UpperCAmelCase__     :						Dict						=							self.auxiliary_head(_A					)
              UpperCAmelCase__     :						List[Any]						=							nn.functional.interpolate(
                  _A		,		size=pixel_values.shape[2:]		,		mode='''bilinear'''		,		align_corners=_A					)

          UpperCAmelCase__     :						Tuple						=							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__     :						str						=							CrossEntropyLoss(ignore_index=self.config.loss_ignore_index					)
                  UpperCAmelCase__     :						Optional[Any]						=							loss_fct(_A		,		_A					)
                  UpperCAmelCase__     :						Tuple						=							loss_fct(_A		,		_A					)
                  UpperCAmelCase__     :						Optional[Any]						=							main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

          if not return_dict:
              if output_hidden_states:
                  UpperCAmelCase__     :						Optional[Any]						=							(logits,) + outputs[1:]
              else:
                  UpperCAmelCase__     :						Union[str, Any]						=							(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		,		)







def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase		,      _UpperCAmelCase					)     ->	Union[str, Any]:
    lowerCamelCase__       : List[Any]   					=						[1]
    for i in range(2		,      _UpperCAmelCase					):
        factorials.append(factorials[-1] * i					)
    assert 0 <= k < factorials[-1] * n, "k out of bounds"

    lowerCamelCase__       : str   					=						[]
    lowerCamelCase__       : Optional[Any]   					=						list(range(_UpperCAmelCase					)					)

    # Find permutation
    while factorials:
        lowerCamelCase__       : List[str]   					=						factorials.pop()
        lowerCamelCase__					,				lowerCamelCase__       : Tuple   					=						divmod(_UpperCAmelCase		,      _UpperCAmelCase					)
        permutation.append(elements[number]					)
        elements.remove(elements[number]					)
    permutation.append(elements[0]					)

    return permutation


if __name__ == "__main__":
    import doctest

    doctest.testmod()





