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



   def __init__(					self : Dict      ,						A__ : int      ,						A__ : int      ,						A__ : List[Any]      ,						A__ : Tuple = 0      ,						A__ : str = False      ,						A__ : Union[str, Any] = 1      ,						)				->  None:




         '''simple docstring'''




         super().__init__()
         snake_case_  :      Optional[Any]  	=							nn.Convad(
             in_channels=snake_case__      ,						out_channels=snake_case__      ,						kernel_size=snake_case__      ,						padding=snake_case__      ,						bias=snake_case__      ,						dilation=snake_case__      ,						)
         snake_case_  :      Union[str, Any]  	=							nn.BatchNormad(snake_case__ )
         snake_case_  :      Union[str, Any]  	=							nn.ReLU()



   def        UpperCAmelCase__							(					self : List[str]      ,						A__ : Tuple )				->  torch.Tensor:




         '''simple docstring'''




         snake_case_  :      Tuple  	=							self.conv(snake_case__ )
         snake_case_  :      Optional[Any]  	=							self.batch_norm(snake_case__ )
         snake_case_  :      Tuple  	=							self.activation(snake_case__ )

         return output



class        snake_case__							(					nn.Module				):



   def __init__(					self : int      ,						A__ : str      ,						A__ : Union[str, Any]      ,						A__ : Optional[int] )				->  None:




         '''simple docstring'''




         super().__init__()
         snake_case_  :      Union[str, Any]  	=							[
             nn.AdaptiveAvgPoolad(snake_case__ ),
             UperNetConvModule(snake_case__      ,						snake_case__      ,						kernel_size=1 ),
         ]
         for i, layer in enumerate(self.layers ):
               self.add_module(str(snake_case__ )      ,						snake_case__ )



   def        UpperCAmelCase__							(					self : Any      ,						A__ : Optional[int] )				->  torch.Tensor:




         '''simple docstring'''




         snake_case_  :      Tuple  	=							input
         for layer in self.layers:
               snake_case_  :      Optional[int]  	=							layer(snake_case__ )
         return hidden_state



class        snake_case__							(					nn.Module				):



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




         '''simple docstring'''




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



   def        UpperCAmelCase__							(					self : List[str]      ,						A__ : Optional[int] )				->  List[torch.Tensor]:




         '''simple docstring'''




         snake_case_  :      List[str]  	=							[]
         for ppm in self.blocks:
               snake_case_  :      Tuple  	=							ppm(snake_case__ )
               snake_case_  :      List[str]  	=							nn.functional.interpolate(
                   snake_case__      ,						size=x.size()[2:]      ,						mode="bilinear"      ,						align_corners=self.align_corners )
               ppm_outs.append(snake_case__ )
         return ppm_outs



class        snake_case__							(					nn.Module				):



   def __init__(					self : Union[str, Any]      ,						A__ : Any      ,						A__ : Dict )				->  Any:




         '''simple docstring'''




         super().__init__()

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

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

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



   def        UpperCAmelCase__							(					self : int )				->  int:




         '''simple docstring'''




         self.apply(self._init_weights )



   def        UpperCAmelCase__							(					self : List[Any]      ,						A__ : Tuple )				->  List[str]:




         '''simple docstring'''




         if isinstance(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 : List[Any]      ,						A__ : Tuple )				->  List[Any]:




         '''simple docstring'''




         snake_case_  :      Tuple  	=							inputs[-1]
         snake_case_  :      List[str]  	=							[x]
         psp_outs.extend(self.psp_modules(snake_case__ ) )
         snake_case_  :      str  	=							torch.cat(snake_case__      ,						dim=1 )
         snake_case_  :      Optional[Any]  	=							self.bottleneck(snake_case__ )

         return output



   def        UpperCAmelCase__							(					self : Tuple      ,						A__ : Dict )				->  torch.Tensor:




         '''simple docstring'''




         snake_case_  :      List[str]  	=							[lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )]

         laterals.append(self.psp_forward(snake_case__ ) )

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

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

         return output



class        snake_case__							(					nn.Module				):



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




         '''simple docstring'''




         super().__init__()

         snake_case_  :      Optional[int]  	=							config
         snake_case_  :      int  	=							config.auxiliary_in_channels
         snake_case_  :      str  	=							config.auxiliary_channels
         snake_case_  :      Dict  	=							config.auxiliary_num_convs
         snake_case_  :      str  	=							config.auxiliary_concat_input
         snake_case_  :      int  	=							in_index

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

         snake_case_  :      Any  	=							nn.Convad(self.channels      ,						config.num_labels      ,						kernel_size=1 )



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




         '''simple docstring'''




         self.apply(self._init_weights )



   def        UpperCAmelCase__							(					self : int      ,						A__ : str )				->  Optional[int]:




         '''simple docstring'''




         if isinstance(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 : Any      ,						A__ : Union[str, Any] )				->  torch.Tensor:




         '''simple docstring'''




         snake_case_  :      Union[str, Any]  	=							encoder_hidden_states[self.in_index]
         snake_case_  :      Union[str, Any]  	=							self.convs(snake_case__ )
         if self.concat_input:
               snake_case_  :      Any  	=							self.conv_cat(torch.cat([hidden_states, output]      ,						dim=1 ) )
         snake_case_  :      Tuple  	=							self.classifier(snake_case__ )
         return output



class        snake_case__							(					lowerCamelCase__				):
   _SCREAMING_SNAKE_CASE						:			Dict         =   UperNetConfig
   _SCREAMING_SNAKE_CASE						:			Union[str, Any]         =   """pixel_values"""
   _SCREAMING_SNAKE_CASE						:			str         =   True



   def        UpperCAmelCase__							(					self : Any      ,						A__ : str )				->  Tuple:




         '''simple docstring'''




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



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




         '''simple docstring'''




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



   def        UpperCAmelCase__							(					self : Union[str, Any]      ,						A__ : List[Any]      ,						A__ : List[Any]=False )				->  Optional[Any]:




         '''simple docstring'''




         if isinstance(snake_case__      ,						snake_case__ ):
               snake_case_  :      Optional[Any]  	=							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        snake_case__							(					lowerCamelCase__				):



   def __init__(					self : Tuple      ,						A__ : List[Any] )				->  Union[str, Any]:




         '''simple docstring'''




         super().__init__(snake_case__ )

         snake_case_  :      Tuple  	=							AutoBackbone.from_config(config.backbone_config )

         # Semantic segmentation head(s)
         snake_case_  :      Any  	=							UperNetHead(snake_case__      ,						in_channels=self.backbone.channels )
         snake_case_  :      Dict  	=							UperNetFCNHead(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=snake_case__      ,						config_class=_CONFIG_FOR_DOC )
   def        UpperCAmelCase__							(					self : Any      ,						A__ : Union[str, Any] = None      ,						A__ : str = None      ,						A__ : Optional[int] = None      ,						A__ : Optional[int] = None      ,						A__ : Optional[Any] = None      ,						)				->  Union[tuple, SemanticSegmenterOutput]:




         '''simple docstring'''




         snake_case_  :      Tuple  	=							return_dict if return_dict is not None else self.config.use_return_dict
         snake_case_  :      Optional[int]  	=							(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         snake_case_  :      str  	=							output_attentions if output_attentions is not None else self.config.output_attentions

         snake_case_  :      Union[str, Any]  	=							self.backbone.forward_with_filtered_kwargs(
             snake_case__      ,						output_hidden_states=snake_case__      ,						output_attentions=snake_case__ )
         snake_case_  :      Tuple  	=							outputs.feature_maps

         snake_case_  :      Tuple  	=							self.decode_head(snake_case__ )
         snake_case_  :      Optional[int]  	=							nn.functional.interpolate(snake_case__      ,						size=pixel_values.shape[2:]      ,						mode="bilinear"      ,						align_corners=snake_case__ )

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

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

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

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










from typing import TYPE_CHECKING

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


UpperCAmelCase_							=				{
    """configuration_m2m_100""": ["""M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP""", """M2M100Config""", """M2M100OnnxConfig"""],
    """tokenization_m2m_100""": ["""M2M100Tokenizer"""],
}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase_							=				[
							    """M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """M2M100ForConditionalGeneration""",
							    """M2M100Model""",
							    """M2M100PreTrainedModel""",
							]


if TYPE_CHECKING:
							from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig
							from .tokenization_mam_aaa import MaMaaaTokenizer

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_mam_aaa import (
														    M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MaMaaaForConditionalGeneration,
														    MaMaaaModel,
														    MaMaaaPreTrainedModel,
														)


else:
							import sys

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