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



 '''simple docstring'''




 def __init__(		self				:   Dict							, lowercase_				:   Optional[Any]							, lowercase_				:   Any							, lowercase_				:   Any							, lowercase_				:   str = 0							, lowercase_				:   str = False							, lowercase_				:   Any = 1							, )						->	List[str]:


   """simple docstring"""







   super().__init__()
   _UpperCamelCase           =    nn.Convad(
       in_channels=A_							, out_channels=A_							, kernel_size=A_							, padding=A_							, bias=A_							, dilation=A_							, )
   _UpperCamelCase           =    nn.BatchNormad(A_)
   _UpperCamelCase           =    nn.ReLU()







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


   """simple docstring"""







   _UpperCamelCase           =    self.conv(A_)
   _UpperCamelCase           =    self.batch_norm(A_)
   _UpperCamelCase           =    self.activation(A_)

   return output




class 					_UpperCAmelCase			(     nn.Module  ):



 '''simple docstring'''




 def __init__(		self				:   Dict							, lowercase_				:   Tuple							, lowercase_				:   Tuple							, lowercase_				:   Dict)						->	Optional[int]:


   """simple docstring"""







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







 def 		__UpperCAmelCase				(		self				:   Tuple							, lowercase_				:   str)						->	Optional[Any]:


   """simple docstring"""







   _UpperCamelCase           =    input
   for layer in self.layers:
     _UpperCamelCase           =    layer(A_)
   return hidden_state




class 					_UpperCAmelCase			(     nn.Module  ):



 '''simple docstring'''




 def __init__(		self				:   str							, lowercase_				:   Dict							, lowercase_				:   Optional[Any]							, lowercase_				:   Optional[int]							, lowercase_				:   Any)						->	Any:


   """simple docstring"""







   super().__init__()
   _UpperCamelCase           =    pool_scales
   _UpperCamelCase           =    align_corners
   _UpperCamelCase           =    in_channels
   _UpperCamelCase           =    channels
   _UpperCamelCase           =    []
   for i, pool_scale in enumerate(A_):
     _UpperCamelCase           =    UperNetPyramidPoolingBlock(pool_scale=A_							, in_channels=A_							, channels=A_)
     self.blocks.append(A_)
     self.add_module(str(A_)							, A_)







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


   """simple docstring"""







   _UpperCamelCase           =    []
   for ppm in self.blocks:
     _UpperCamelCase           =    ppm(A_)
     _UpperCamelCase           =    nn.functional.interpolate(
         A_							, size=x.size()[2:]							, mode="bilinear"							, align_corners=self.align_corners)
     ppm_outs.append(A_)
   return ppm_outs




class 					_UpperCAmelCase			(     nn.Module  ):



 '''simple docstring'''




 def __init__(		self				:   Optional[int]							, lowercase_				:   Optional[Any]							, lowercase_				:   Tuple)						->	Tuple:


   """simple docstring"""







   super().__init__()

   _UpperCamelCase           =    config
   _UpperCamelCase           =    config.pool_scales  # e.g. (1, 2, 3, 6)
   _UpperCamelCase           =    in_channels
   _UpperCamelCase           =    config.hidden_size
   _UpperCamelCase           =    False
   _UpperCamelCase           =    nn.Convad(self.channels							, config.num_labels							, kernel_size=1)

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

   _UpperCamelCase           =    UperNetConvModule(
       len(self.in_channels) * self.channels							, self.channels							, kernel_size=3							, padding=1							, )







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


   """simple docstring"""







   self.apply(self._init_weights)







 def 		__UpperCAmelCase				(		self				:   int							, lowercase_				:   Dict)						->	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 		__UpperCAmelCase				(		self				:   Tuple							, lowercase_				:   Dict)						->	List[Any]:


   """simple docstring"""







   _UpperCamelCase           =    inputs[-1]
   _UpperCamelCase           =    [x]
   psp_outs.extend(self.psp_modules(A_))
   _UpperCamelCase           =    torch.cat(A_							, dim=1)
   _UpperCamelCase           =    self.bottleneck(A_)

   return output







 def 		__UpperCAmelCase				(		self				:   Optional[Any]							, lowercase_				:   List[Any])						->	Optional[Any]:


   """simple docstring"""







   _UpperCamelCase           =    [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           =    len(A_)
   for i in range(used_backbone_levels - 1							, 0							, -1):
     _UpperCamelCase           =    laterals[i - 1].shape[2:]
     _UpperCamelCase           =    laterals[i - 1] + nn.functional.interpolate(
         laterals[i]							, size=A_							, mode="bilinear"							, align_corners=self.align_corners)

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

   return output




class 					_UpperCAmelCase			(     nn.Module  ):



 '''simple docstring'''




 def __init__(		self				:   List[str]							, lowercase_				:   List[str]							, lowercase_				:   int = 2							, lowercase_				:   Any = 3							, lowercase_				:   Tuple = 1)						->	List[str]:


   """simple docstring"""







   super().__init__()

   _UpperCamelCase           =    config
   _UpperCamelCase           =    config.auxiliary_in_channels
   _UpperCamelCase           =    config.auxiliary_channels
   _UpperCamelCase           =    config.auxiliary_num_convs
   _UpperCamelCase           =    config.auxiliary_concat_input
   _UpperCamelCase           =    in_index

   _UpperCamelCase           =    (kernel_size // 2) * dilation
   _UpperCamelCase           =    []
   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           =    nn.Identity()
   else:
     _UpperCamelCase           =    nn.Sequential(*A_)
   if self.concat_input:
     _UpperCamelCase           =    UperNetConvModule(
         self.in_channels + self.channels							, self.channels							, kernel_size=A_							, padding=kernel_size // 2)

   _UpperCamelCase           =    nn.Convad(self.channels							, config.num_labels							, kernel_size=1)







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


   """simple docstring"""







   self.apply(self._init_weights)







 def 		__UpperCAmelCase				(		self				:   str							, lowercase_				:   List[Any])						->	Optional[int]:


   """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 		__UpperCAmelCase				(		self				:   str							, lowercase_				:   Dict)						->	int:


   """simple docstring"""







   _UpperCamelCase           =    encoder_hidden_states[self.in_index]
   _UpperCamelCase           =    self.convs(A_)
   if self.concat_input:
     _UpperCamelCase           =    self.conv_cat(torch.cat([hidden_states, output]							, dim=1))
   _UpperCamelCase           =    self.classifier(A_)
   return output




class 					_UpperCAmelCase			(     lowerCAmelCase  ):



 '''simple docstring'''




 __A			       =					UperNetConfig
 __A			       =					'''pixel_values'''
 __A			       =					True







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


   """simple docstring"""







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







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


   """simple docstring"""







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







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


   """simple docstring"""







   if isinstance(A_							, A_):
     _UpperCamelCase           =    value


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

lowerCamelCase__  =					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 					_UpperCAmelCase			(     lowerCAmelCase  ):



 '''simple docstring'''




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


   """simple docstring"""







   super().__init__(A_)

   _UpperCamelCase           =    AutoBackbone.from_config(config.backbone_config)

   # Semantic segmentation head(s)
   _UpperCamelCase           =    UperNetHead(A_							, in_channels=self.backbone.channels)
   _UpperCamelCase           =    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 		__UpperCAmelCase				(		self				:   Any							, lowercase_				:   int = None							, lowercase_				:   Tuple = None							, lowercase_				:   int = None							, lowercase_				:   Dict = None							, lowercase_				:   Dict = None							, )						->	Any:


   """simple docstring"""







   _UpperCamelCase           =    return_dict if return_dict is not None else self.config.use_return_dict
   _UpperCamelCase           =    (
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   _UpperCamelCase           =    output_attentions if output_attentions is not None else self.config.output_attentions

   _UpperCamelCase           =    self.backbone.forward_with_filtered_kwargs(
       A_							, output_hidden_states=A_							, output_attentions=A_)
   _UpperCamelCase           =    outputs.feature_maps

   _UpperCamelCase           =    self.decode_head(A_)
   _UpperCamelCase           =    nn.functional.interpolate(A_							, size=pixel_values.shape[2:]							, mode="bilinear"							, align_corners=A_)

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

   _UpperCamelCase           =    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           =    CrossEntropyLoss(ignore_index=self.config.loss_ignore_index)
       _UpperCamelCase           =    loss_fct(A_							, A_)
       _UpperCamelCase           =    loss_fct(A_							, A_)
       _UpperCamelCase           =    main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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








from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


SCREAMING_SNAKE_CASE__					:     List[Any]       =     {
    'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json',
    'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json',
}


class 		_SCREAMING_SNAKE_CASE     (						A     ):
     __SCREAMING_SNAKE_CASE     					=    '''ernie_m'''
     __SCREAMING_SNAKE_CASE     					=    {"dropout": "classifier_dropout", "num_classes": "num_labels"}


     def __init__(  self							,					A_ = 25_00_02							,					A_ = 7_68							,					A_ = 12							,					A_ = 12							,					A_ = 30_72							,					A_ = "gelu"							,					A_ = 0.1							,					A_ = 0.1							,					A_ = 5_14							,					A_ = 0.0_2							,					A_ = 1							,					A_ = 1e-05							,					A_=None							,					A_=False							,					A_=0.0							,					**A_							,					):
       super().__init__(pad_token_id=A_							,					**A_    )
       _UpperCAmelCase		:							Tuple							=     vocab_size
       _UpperCAmelCase		:							List[str]							=     hidden_size
       _UpperCAmelCase		:							Optional[int]							=     num_hidden_layers
       _UpperCAmelCase		:							Tuple							=     num_attention_heads
       _UpperCAmelCase		:							Dict							=     intermediate_size
       _UpperCAmelCase		:							List[Any]							=     hidden_act
       _UpperCAmelCase		:							Any							=     hidden_dropout_prob
       _UpperCAmelCase		:							Any							=     attention_probs_dropout_prob
       _UpperCAmelCase		:							Tuple							=     max_position_embeddings
       _UpperCAmelCase		:							Dict							=     initializer_range
       _UpperCAmelCase		:							Optional[Any]							=     layer_norm_eps
       _UpperCAmelCase		:							str							=     classifier_dropout
       _UpperCAmelCase		:							Any							=     is_decoder
       _UpperCAmelCase		:							Union[str, Any]							=     act_dropout





