


'''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


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

# General docstring
A__:    Tuple					    =       '''UperNetConfig'''







class A__       (						nn.Module      ):






      def __init__( self			:str     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:Union[int, Tuple[int, int]]     ,						SCREAMING_SNAKE_CASE			:Union[int, Tuple[int, int], str] = 0     ,						SCREAMING_SNAKE_CASE			:bool = False     ,						SCREAMING_SNAKE_CASE			:Union[int, Tuple[int, int]] = 1     ,						)       ->					None:




             '''simple docstring'''
             super().__init__()
             _a    : Tuple     					=nn.Convad(
                 in_channels=SCREAMING_SNAKE_CASE     ,						out_channels=SCREAMING_SNAKE_CASE     ,						kernel_size=SCREAMING_SNAKE_CASE     ,						padding=SCREAMING_SNAKE_CASE     ,						bias=SCREAMING_SNAKE_CASE     ,						dilation=SCREAMING_SNAKE_CASE     ,						)
             _a    : Optional[Any]     					=nn.BatchNormad(SCREAMING_SNAKE_CASE	)
             _a    : Tuple     					=nn.ReLU()






      def __UpperCAmelCase       ( self			:int     ,						SCREAMING_SNAKE_CASE			:torch.Tensor	)       ->					torch.Tensor:




             '''simple docstring'''
             _a    : List[str]     					=self.conv(SCREAMING_SNAKE_CASE	)
             _a    : Tuple     					=self.batch_norm(SCREAMING_SNAKE_CASE	)
             _a    : List[Any]     					=self.activation(SCREAMING_SNAKE_CASE	)

             return output







class A__       (						nn.Module      ):






      def __init__( self			:Any     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:int	)       ->					None:




             '''simple docstring'''
             super().__init__()
             _a    : List[Any]     					=[
                 nn.AdaptiveAvgPoolad(SCREAMING_SNAKE_CASE	),
                 UperNetConvModule(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						kernel_size=1	),
             ]
             for i, layer in enumerate(self.layers	):
                    self.add_module(str(SCREAMING_SNAKE_CASE	)     ,						SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:List[Any]     ,						SCREAMING_SNAKE_CASE			:torch.Tensor	)       ->					torch.Tensor:




             '''simple docstring'''
             _a    : List[Any]     					=input
             for layer in self.layers:
                    _a    : Tuple     					=layer(SCREAMING_SNAKE_CASE	)
             return hidden_state







class A__       (						nn.Module      ):






      def __init__( self			:List[str]     ,						SCREAMING_SNAKE_CASE			:Tuple[int, ...]     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:bool	)       ->					None:




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






      def __UpperCAmelCase       ( self			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:torch.Tensor	)       ->					List[torch.Tensor]:




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







class A__       (						nn.Module      ):






      def __init__( self			:Optional[int]     ,						SCREAMING_SNAKE_CASE			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Optional[Any]	)       ->					List[str]:




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

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

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

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






      def __UpperCAmelCase       ( self			:List[str]	)       ->					Any:




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






      def __UpperCAmelCase       ( self			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:List[Any]	)       ->					Union[str, Any]:




             '''simple docstring'''
             if isinstance(SCREAMING_SNAKE_CASE     ,						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			:int     ,						SCREAMING_SNAKE_CASE			:List[Any]	)       ->					List[str]:




             '''simple docstring'''
             _a    : Dict     					=inputs[-1]
             _a    : str     					=[x]
             psp_outs.extend(self.psp_modules(SCREAMING_SNAKE_CASE	)	)
             _a    : str     					=torch.cat(SCREAMING_SNAKE_CASE     ,						dim=1	)
             _a    : str     					=self.bottleneck(SCREAMING_SNAKE_CASE	)

             return output






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:torch.Tensor	)       ->					torch.Tensor:




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

             laterals.append(self.psp_forward(SCREAMING_SNAKE_CASE	)	)

             # build top-down path
             _a    : List[str]     					=len(SCREAMING_SNAKE_CASE	)
             for i in range(used_backbone_levels - 1     ,						0     ,						-1	):
                    _a    : Tuple     					=laterals[i - 1].shape[2:]
                    _a    : List[str]     					=laterals[i - 1] + nn.functional.interpolate(
                        laterals[i]     ,						size=SCREAMING_SNAKE_CASE     ,						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    : Dict     					=nn.functional.interpolate(
                        fpn_outs[i]     ,						size=fpn_outs[0].shape[2:]     ,						mode="""bilinear"""     ,						align_corners=self.align_corners	)
             _a    : int     					=torch.cat(SCREAMING_SNAKE_CASE     ,						dim=1	)
             _a    : List[Any]     					=self.fpn_bottleneck(SCREAMING_SNAKE_CASE	)
             _a    : Any     					=self.classifier(SCREAMING_SNAKE_CASE	)

             return output







class A__       (						nn.Module      ):






      def __init__( self			:Tuple     ,						SCREAMING_SNAKE_CASE			:List[Any]     ,						SCREAMING_SNAKE_CASE			:int = 2     ,						SCREAMING_SNAKE_CASE			:int = 3     ,						SCREAMING_SNAKE_CASE			:Union[int, Tuple[int, int]] = 1	)       ->					None:




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

             _a    : Optional[int]     					=config
             _a    : Any     					=config.auxiliary_in_channels
             _a    : Tuple     					=config.auxiliary_channels
             _a    : List[str]     					=config.auxiliary_num_convs
             _a    : Dict     					=config.auxiliary_concat_input
             _a    : int     					=in_index

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

             _a    : List[Any]     					=nn.Convad(self.channels     ,						config.num_labels     ,						kernel_size=1	)






      def __UpperCAmelCase       ( self			:int	)       ->					List[str]:




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






      def __UpperCAmelCase       ( self			:str     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]	)       ->					Tuple:




             '''simple docstring'''
             if isinstance(SCREAMING_SNAKE_CASE     ,						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     ,						SCREAMING_SNAKE_CASE			:torch.Tensor	)       ->					torch.Tensor:




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







class A__       (						UpperCAmelCase__      ):
      __UpperCamelCase       :  Optional[int]	       =							UperNetConfig
      __UpperCamelCase       :  Any	       =							"pixel_values"
      __UpperCamelCase       :  List[Any]	       =							True






      def __UpperCAmelCase       ( self			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]	)       ->					Union[str, Any]:




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






      def __UpperCAmelCase       ( self			:List[Any]	)       ->					Dict:




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






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Optional[int]     ,						SCREAMING_SNAKE_CASE			:List[Any]=False	)       ->					Optional[Any]:




             '''simple docstring'''
             if isinstance(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	):
                    _a    : List[Any]     					=value

A__:    List[Any]					    =       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.
'''

A__:    Dict					    =       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." ,     UpperCAmelCase__ ,     )
class A__       (						UpperCAmelCase__      ):






      def __init__( self			:Dict     ,						SCREAMING_SNAKE_CASE			:str	)       ->					List[str]:




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

             _a    : Optional[int]     					=AutoBackbone.from_config(config.backbone_config	)

             # Semantic segmentation head(s)
             _a    : Dict     					=UperNetHead(SCREAMING_SNAKE_CASE     ,						in_channels=self.backbone.channels	)
             _a    : Dict     					=UperNetFCNHead(SCREAMING_SNAKE_CASE	) 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=SCREAMING_SNAKE_CASE     ,						config_class=_CONFIG_FOR_DOC	)
      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Tensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[bool] = None     ,						SCREAMING_SNAKE_CASE			:Optional[bool] = None     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Tensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[bool] = None     ,						)       ->					Union[tuple, SemanticSegmenterOutput]:




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

             _a    : Any     					=self.backbone.forward_with_filtered_kwargs(
                 SCREAMING_SNAKE_CASE     ,						output_hidden_states=SCREAMING_SNAKE_CASE     ,						output_attentions=SCREAMING_SNAKE_CASE	)
             _a    : List[Any]     					=outputs.feature_maps

             _a    : int     					=self.decode_head(SCREAMING_SNAKE_CASE	)
             _a    : List[str]     					=nn.functional.interpolate(SCREAMING_SNAKE_CASE     ,						size=pixel_values.shape[2:]     ,						mode="""bilinear"""     ,						align_corners=SCREAMING_SNAKE_CASE	)

             _a    : str     					=None
             if self.auxiliary_head is not None:
                    _a    : str     					=self.auxiliary_head(SCREAMING_SNAKE_CASE	)
                    _a    : Dict     					=nn.functional.interpolate(
                        SCREAMING_SNAKE_CASE     ,						size=pixel_values.shape[2:]     ,						mode="""bilinear"""     ,						align_corners=SCREAMING_SNAKE_CASE	)

             _a    : str     					=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    : Dict     					=CrossEntropyLoss(ignore_index=self.config.loss_ignore_index	)
                           _a    : Union[str, Any]     					=loss_fct(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)
                           _a    : Tuple     					=loss_fct(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)
                           _a    : Any     					=main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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

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






'''simple docstring'''







# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works
from ...utils import deprecate
from ..controlnet.multicontrolnet import MultiControlNetModel  # noqa: F401
from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline  # noqa: F401


deprecate(
    '''stable diffusion controlnet''',
    '''0.22.0''',
    '''Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.''',
    standard_warn=False,
    stacklevel=3,
)


