


from __future__ import annotations



def  _a			(						__UpperCamelCase  ,  __UpperCamelCase = None  ,  __UpperCamelCase = None ):
     if start is None:
          a_    :     Union[str, Any]										=    0

     if end is None:
          a_    :     Union[str, Any]										=    len(__UpperCamelCase ) - 1

     if start >= end:
          return

     a_    :     Union[str, Any]										=    (start + end) // 2

     slowsort(__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase )
     slowsort(__UpperCamelCase  ,  mid + 1  ,  __UpperCamelCase )

     if sequence[end] < sequence[mid]:
          a_    :     List[str]										=    sequence[mid], sequence[end]

     slowsort(__UpperCamelCase  ,  __UpperCamelCase  ,  end - 1 )


if __name__ == "__main__":
       from doctest import testmod

       testmod()







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


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

# General docstring
__lowerCamelCase        =     '''UperNetConfig'''


class   a__	(						nn.Module    ):

       def __init__(   self      :     Any      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     Union[int, Tuple[int, int]]      ,							lowerCamelCase_      :     Union[int, Tuple[int, int], str] = 0      ,							lowerCamelCase_      :     bool = False      ,							lowerCamelCase_      :     Union[int, Tuple[int, int]] = 1      ,							):
            super().__init__()
            a_    :     str										=    nn.Convad(
                in_channels=lowerCamelCase_      ,							out_channels=lowerCamelCase_      ,							kernel_size=lowerCamelCase_      ,							padding=lowerCamelCase_      ,							bias=lowerCamelCase_      ,							dilation=lowerCamelCase_      ,							)
            a_    :     Dict										=    nn.BatchNormad(lowerCamelCase_    )
            a_    :     Union[str, Any]										=    nn.ReLU()

       def 						UpperCAmelCase(   self      :     str      ,							lowerCamelCase_      :     torch.Tensor    ):
            a_    :     Tuple										=    self.conv(lowerCamelCase_    )
            a_    :     int										=    self.batch_norm(lowerCamelCase_    )
            a_    :     Optional[int]										=    self.activation(lowerCamelCase_    )

            return output


class   a__	(						nn.Module    ):

       def __init__(   self      :     List[str]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int    ):
            super().__init__()
            a_    :     Union[str, Any]										=    [
                nn.AdaptiveAvgPoolad(lowerCamelCase_    ),
                UperNetConvModule(lowerCamelCase_      ,							lowerCamelCase_      ,							kernel_size=1    ),
            ]
            for i, layer in enumerate(self.layers    ):
                 self.add_module(str(lowerCamelCase_    )      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     List[Any]      ,							lowerCamelCase_      :     torch.Tensor    ):
            a_    :     Optional[int]										=    input
            for layer in self.layers:
                 a_    :     Dict										=    layer(lowerCamelCase_    )
            return hidden_state


class   a__	(						nn.Module    ):

       def __init__(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Tuple[int, ...]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     bool    ):
            super().__init__()
            a_    :     Optional[int]										=    pool_scales
            a_    :     List[Any]										=    align_corners
            a_    :     Union[str, Any]										=    in_channels
            a_    :     List[str]										=    channels
            a_    :     Optional[int]										=    []
            for i, pool_scale in enumerate(lowerCamelCase_    ):
                 a_    :     Optional[int]										=    UperNetPyramidPoolingBlock(pool_scale=lowerCamelCase_      ,							in_channels=lowerCamelCase_      ,							channels=lowerCamelCase_    )
                 self.blocks.append(lowerCamelCase_    )
                 self.add_module(str(lowerCamelCase_    )      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     torch.Tensor    ):
            a_    :     List[str]										=    []
            for ppm in self.blocks:
                 a_    :     Any										=    ppm(lowerCamelCase_    )
                 a_    :     str										=    nn.functional.interpolate(
                     lowerCamelCase_      ,							size=x.size()[2:]      ,							mode="""bilinear"""      ,							align_corners=self.align_corners    )
                 ppm_outs.append(lowerCamelCase_    )
            return ppm_outs


class   a__	(						nn.Module    ):

       def __init__(   self      :     Any      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     int    ):
            super().__init__()

            a_    :     Tuple										=    config
            a_    :     int										=    config.pool_scales  # e.g. (1, 2, 3, 6)
            a_    :     str										=    in_channels
            a_    :     str										=    config.hidden_size
            a_    :     int										=    False
            a_    :     int										=    nn.Convad(self.channels      ,							config.num_labels      ,							kernel_size=1    )

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

            a_    :     str										=    UperNetConvModule(
                len(self.in_channels    ) * self.channels      ,							self.channels      ,							kernel_size=3      ,							padding=1      ,							)

       def 						UpperCAmelCase(   self      :     str    ):
            self.apply(self._init_weights    )

       def 						UpperCAmelCase(   self      :     Any      ,							lowerCamelCase_      :     Union[str, Any]    ):
            if isinstance(lowerCamelCase_      ,							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 						UpperCAmelCase(   self      :     Tuple      ,							lowerCamelCase_      :     Optional[int]    ):
            a_    :     Optional[int]										=    inputs[-1]
            a_    :     List[str]										=    [x]
            psp_outs.extend(self.psp_modules(lowerCamelCase_    )    )
            a_    :     int										=    torch.cat(lowerCamelCase_      ,							dim=1    )
            a_    :     Tuple										=    self.bottleneck(lowerCamelCase_    )

            return output

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     torch.Tensor    ):
            # build laterals
            a_    :     List[Any]										=    [lateral_conv(encoder_hidden_states[i]    ) for i, lateral_conv in enumerate(self.lateral_convs    )]

            laterals.append(self.psp_forward(lowerCamelCase_    )    )

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

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

            return output


class   a__	(						nn.Module    ):

       def __init__(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Optional[Any]      ,							lowerCamelCase_      :     int = 2      ,							lowerCamelCase_      :     int = 3      ,							lowerCamelCase_      :     Union[int, Tuple[int, int]] = 1    ):
            super().__init__()

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

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

            a_    :     str										=    nn.Convad(self.channels      ,							config.num_labels      ,							kernel_size=1    )

       def 						UpperCAmelCase(   self      :     Optional[int]    ):
            self.apply(self._init_weights    )

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     Dict    ):
            if isinstance(lowerCamelCase_      ,							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 						UpperCAmelCase(   self      :     Any      ,							lowerCamelCase_      :     torch.Tensor    ):
            # just take the relevant feature maps
            a_    :     List[Any]										=    encoder_hidden_states[self.in_index]
            a_    :     Any										=    self.convs(lowerCamelCase_    )
            if self.concat_input:
                 a_    :     Optional[Any]										=    self.conv_cat(torch.cat([hidden_states, output]      ,							dim=1    )    )
            a_    :     Union[str, Any]										=    self.classifier(lowerCamelCase_    )
            return output


class   a__	(						lowerCAmelCase_    ):
       lowerCamelCase__:     Tuple							    =     UperNetConfig
       lowerCamelCase__:     Tuple							    =     """pixel_values"""
       lowerCamelCase__:     Optional[Any]							    =     True

       def 						UpperCAmelCase(   self      :     Optional[int]      ,							lowerCamelCase_      :     str    ):
            if isinstance(lowerCamelCase_      ,							lowerCamelCase_    ):
                 module.backbone.init_weights()
                 module.decode_head.init_weights()
                 module.auxiliary_head.init_weights()

       def 						UpperCAmelCase(   self      :     List[str]    ):
            self.backbone.init_weights()
            self.decode_head.init_weights()
            self.auxiliary_head.init_weights()

       def 						UpperCAmelCase(   self      :     Optional[Any]      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     str=False    ):
            if isinstance(lowerCamelCase_      ,							lowerCamelCase_    ):
                 a_    :     List[Any]										=    value

__lowerCamelCase        =     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.
'''

__lowerCamelCase        =     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."""						,       lowerCAmelCase_						,       )
class   a__	(						lowerCAmelCase_    ):

       def __init__(   self      :     Optional[int]      ,							lowerCamelCase_      :     Optional[Any]    ):
            super().__init__(lowerCamelCase_    )

            a_    :     str										=    AutoBackbone.from_config(config.backbone_config    )

            # Semantic segmentation head(s)
            a_    :     List[Any]										=    UperNetHead(lowerCamelCase_      ,							in_channels=self.backbone.channels    )
            a_    :     List[str]										=    UperNetFCNHead(lowerCamelCase_    ) 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=lowerCamelCase_      ,							config_class=_CONFIG_FOR_DOC    )
       def 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Optional[torch.Tensor] = None      ,							lowerCamelCase_      :     Optional[bool] = None      ,							lowerCamelCase_      :     Optional[bool] = None      ,							lowerCamelCase_      :     Optional[torch.Tensor] = None      ,							lowerCamelCase_      :     Optional[bool] = None      ,							):
            a_    :     List[str]										=    return_dict if return_dict is not None else self.config.use_return_dict
            a_    :     Any										=    (
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            a_    :     Tuple										=    output_attentions if output_attentions is not None else self.config.output_attentions

            a_    :     List[Any]										=    self.backbone.forward_with_filtered_kwargs(
                lowerCamelCase_      ,							output_hidden_states=lowerCamelCase_      ,							output_attentions=lowerCamelCase_    )
            a_    :     str										=    outputs.feature_maps

            a_    :     Tuple										=    self.decode_head(lowerCamelCase_    )
            a_    :     Optional[Any]										=    nn.functional.interpolate(lowerCamelCase_      ,							size=pixel_values.shape[2:]      ,							mode="""bilinear"""      ,							align_corners=lowerCamelCase_    )

            a_    :     List[Any]										=    None
            if self.auxiliary_head is not None:
                 a_    :     Dict										=    self.auxiliary_head(lowerCamelCase_    )
                 a_    :     List[Any]										=    nn.functional.interpolate(
                     lowerCamelCase_      ,							size=pixel_values.shape[2:]      ,							mode="""bilinear"""      ,							align_corners=lowerCamelCase_    )

            a_    :     Dict										=    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
                      a_    :     int										=    CrossEntropyLoss(ignore_index=self.config.loss_ignore_index    )
                      a_    :     List[Any]										=    loss_fct(lowerCamelCase_      ,							lowerCamelCase_    )
                      a_    :     Optional[int]										=    loss_fct(lowerCamelCase_      ,							lowerCamelCase_    )
                      a_    :     Tuple										=    main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

            if not return_dict:
                 if output_hidden_states:
                      a_    :     str										=    (logits,) + outputs[1:]
                 else:
                      a_    :     Optional[int]										=    (logits,) + outputs[2:]
                 return ((loss,) + output) if loss is not None else output

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



