



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


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

# General docstring
snake_case   					=				'''UperNetConfig'''



class     UpperCAmelCase    (	nn.Module  ):



   def __init__(       self    :	int							,       __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							,       ):



     """simple docstring"""
     super().__init__()
     _snake_case   	=      nn.Convad(
         in_channels=__lowerCamelCase							,       out_channels=__lowerCamelCase							,       kernel_size=__lowerCamelCase							,       padding=__lowerCamelCase							,       bias=__lowerCamelCase							,       dilation=__lowerCamelCase							,       )
     _snake_case   	=      nn.BatchNormad(__lowerCamelCase				)
     _snake_case   	=      nn.ReLU()





   def    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	torch.Tensor				):



     """simple docstring"""
     _snake_case   	=      self.conv(__lowerCamelCase				)
     _snake_case   	=      self.batch_norm(__lowerCamelCase				)
     _snake_case   	=      self.activation(__lowerCamelCase				)

     return output



class     UpperCAmelCase    (	nn.Module  ):



   def __init__(       self    :	int							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     super().__init__()
     _snake_case   	=      [
         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    :	Any							,       __lowerCamelCase    :	torch.Tensor				):



     """simple docstring"""
     _snake_case   	=      input
     for layer in self.layers:
       _snake_case   	=      layer(__lowerCamelCase				)
     return hidden_state



class     UpperCAmelCase    (	nn.Module  ):



   def __init__(       self    :	Union[str, Any]							,       __lowerCamelCase    :	Tuple[int, ...]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	bool				):



     """simple docstring"""
     super().__init__()
     _snake_case   	=      pool_scales
     _snake_case   	=      align_corners
     _snake_case   	=      in_channels
     _snake_case   	=      channels
     _snake_case   	=      []
     for i, pool_scale in enumerate(__lowerCamelCase				):
       _snake_case   	=      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				):



     """simple docstring"""
     _snake_case   	=      []
     for ppm in self.blocks:
       _snake_case   	=      ppm(__lowerCamelCase				)
       _snake_case   	=      nn.functional.interpolate(
           __lowerCamelCase							,       size=x.size()[2:]							,       mode='''bilinear'''							,       align_corners=self.align_corners				)
       ppm_outs.append(__lowerCamelCase				)
     return ppm_outs



class     UpperCAmelCase    (	nn.Module  ):



   def __init__(       self    :	Optional[Any]							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Optional[int]				):



     """simple docstring"""
     super().__init__()

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

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

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



   def    __UpperCAmelCase    (       self    :	Dict				):



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



   def    __UpperCAmelCase    (       self    :	Union[str, Any]							,       __lowerCamelCase    :	Optional[Any]				):



     """simple docstring"""
     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    :	str							,       __lowerCamelCase    :	Dict				):



     """simple docstring"""
     _snake_case   	=      inputs[-1]
     _snake_case   	=      [x]
     psp_outs.extend(self.psp_modules(__lowerCamelCase				)				)
     _snake_case   	=      torch.cat(__lowerCamelCase							,       dim=1				)
     _snake_case   	=      self.bottleneck(__lowerCamelCase				)

     return output





   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	torch.Tensor				):



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

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

     return output



class     UpperCAmelCase    (	nn.Module  ):



   def __init__(       self    :	Optional[Any]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	int = 2							,       __lowerCamelCase    :	int = 3							,       __lowerCamelCase    :	Union[int, Tuple[int, int]] = 1				):



     """simple docstring"""
     super().__init__()

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

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

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



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



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



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



     """simple docstring"""
     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    :	List[Any]							,       __lowerCamelCase    :	torch.Tensor				):



     """simple docstring"""
     # just take the relevant feature maps
     _snake_case   	=      encoder_hidden_states[self.in_index]
     _snake_case   	=      self.convs(__lowerCamelCase				)
     if self.concat_input:
       _snake_case   	=      self.conv_cat(torch.cat([hidden_states, output]							,       dim=1				)				)
     _snake_case   	=      self.classifier(__lowerCamelCase				)
     return output



class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):
   A__		:	Optional[int]           =  UperNetConfig
   A__		:	List[str]           =  '''pixel_values'''
   A__		:	List[str]           =  True



   def    __UpperCAmelCase    (       self    :	List[str]							,       __lowerCamelCase    :	Dict				):



     """simple docstring"""
     if isinstance(__lowerCamelCase							,       __lowerCamelCase				):
       module.backbone.init_weights()
       module.decode_head.init_weights()
       module.auxiliary_head.init_weights()



   def    __UpperCAmelCase    (       self    :	int				):



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





   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	List[str]=False				):



     """simple docstring"""
     if isinstance(__lowerCamelCase							,       __lowerCamelCase				):
       _snake_case   	=      value

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

snake_case   					=				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.''',__SCREAMING_SNAKE_CASE,)
class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):



   def __init__(       self    :	List[str]							,       __lowerCamelCase    :	Dict				):



     """simple docstring"""
     super().__init__(__lowerCamelCase				)

     _snake_case   	=      AutoBackbone.from_config(config.backbone_config				)

     # Semantic segmentation head(s)
     _snake_case   	=      UperNetHead(__lowerCamelCase							,       in_channels=self.backbone.channels				)
     _snake_case   	=      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    :	Optional[Any]							,       __lowerCamelCase    :	Optional[torch.Tensor] = None							,       __lowerCamelCase    :	Optional[bool] = None							,       __lowerCamelCase    :	Optional[bool] = None							,       __lowerCamelCase    :	Optional[torch.Tensor] = None							,       __lowerCamelCase    :	Optional[bool] = None							,       ):



     """simple docstring"""
     _snake_case   	=      return_dict if return_dict is not None else self.config.use_return_dict
     _snake_case   	=      (
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     _snake_case   	=      output_attentions if output_attentions is not None else self.config.output_attentions

     _snake_case   	=      self.backbone.forward_with_filtered_kwargs(
         __lowerCamelCase							,       output_hidden_states=__lowerCamelCase							,       output_attentions=__lowerCamelCase				)
     _snake_case   	=      outputs.feature_maps

     _snake_case   	=      self.decode_head(__lowerCamelCase				)
     _snake_case   	=      nn.functional.interpolate(__lowerCamelCase							,       size=pixel_values.shape[2:]							,       mode='''bilinear'''							,       align_corners=__lowerCamelCase				)

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

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

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













"""simple docstring"""
from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCAmelCase       =				logging.get_logger(__name__)

_lowerCAmelCase       =				{
    "huggingface/informer-tourism-monthly": (
        "https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json"
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}



class   SCREAMING_SNAKE_CASE	(					_SCREAMING_SNAKE_CASE    ):





	"""simple docstring"""




	_A							:					Optional[Any] 			=					"""informer"""
	_A							:					Dict 			=					{
	    """hidden_size""": """d_model""",
	    """num_attention_heads""": """encoder_attention_heads""",
	    """num_hidden_layers""": """encoder_layers""",
	}





	def __init__(self  ,  lowerCAmelCase_ = None  ,  lowerCAmelCase_ = None  ,  lowerCAmelCase_ = "student_t"  ,  lowerCAmelCase_ = "nll"  ,  lowerCAmelCase_ = 1  ,  lowerCAmelCase_ = None  ,  lowerCAmelCase_ = "mean"  ,  lowerCAmelCase_ = 0  ,  lowerCAmelCase_ = 0  ,  lowerCAmelCase_ = 0  ,  lowerCAmelCase_ = 0  ,  lowerCAmelCase_ = None  ,  lowerCAmelCase_ = None  ,  lowerCAmelCase_ = 64  ,  lowerCAmelCase_ = 32  ,  lowerCAmelCase_ = 32  ,  lowerCAmelCase_ = 2  ,  lowerCAmelCase_ = 2  ,  lowerCAmelCase_ = 2  ,  lowerCAmelCase_ = 2  ,  lowerCAmelCase_ = True  ,  lowerCAmelCase_ = "gelu"  ,  lowerCAmelCase_ = 0.05  ,  lowerCAmelCase_ = 0.1  ,  lowerCAmelCase_ = 0.1  ,  lowerCAmelCase_ = 0.1  ,  lowerCAmelCase_ = 0.1  ,  lowerCAmelCase_ = 100  ,  lowerCAmelCase_ = 0.02  ,  lowerCAmelCase_=True  ,  lowerCAmelCase_ = "prob"  ,  lowerCAmelCase_ = 5  ,  lowerCAmelCase_ = True  ,  **lowerCAmelCase_  ,  ):
		# time series specific configuration
		A_		:    Optional[Any]						     = prediction_length
		A_		:    Dict						     = context_length or prediction_length
		A_		:    Dict						     = distribution_output
		A_		:    Tuple						     = loss
		A_		:    Dict						     = input_size
		A_		:    Union[str, Any]						     = num_time_features
		A_		:    List[str]						     = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
		A_		:    Optional[int]						     = scaling
		A_		:    Optional[Any]						     = num_dynamic_real_features
		A_		:    Tuple						     = num_static_real_features
		A_		:    Tuple						     = num_static_categorical_features

		# set cardinality
		if cardinality and num_static_categorical_features > 0:
			if len(lowerCAmelCase_				) != num_static_categorical_features:
				raise ValueError(
				    """The cardinality should be a list of the same length as `num_static_categorical_features`"""				)
			A_		:    List[str]						     = cardinality
		else:
			A_		:    List[Any]						     = [0]

		# set embedding_dimension
		if embedding_dimension and num_static_categorical_features > 0:
			if len(lowerCAmelCase_				) != num_static_categorical_features:
				raise ValueError(
				    """The embedding dimension should be a list of the same length as `num_static_categorical_features`"""				)
			A_		:    int						     = embedding_dimension
		else:
			A_		:    Optional[int]						     = [min(50  ,  (cat + 1) // 2				) for cat in self.cardinality]

		A_		:    Optional[int]						     = num_parallel_samples

		# Transformer architecture configuration
		A_		:    Union[str, Any]						     = input_size * len(self.lags_sequence				) + self._number_of_features
		A_		:    Dict						     = d_model
		A_		:    Dict						     = encoder_attention_heads
		A_		:    Dict						     = decoder_attention_heads
		A_		:    Any						     = encoder_ffn_dim
		A_		:    Tuple						     = decoder_ffn_dim
		A_		:    Tuple						     = encoder_layers
		A_		:    Optional[int]						     = decoder_layers

		A_		:    List[str]						     = dropout
		A_		:    List[str]						     = attention_dropout
		A_		:    Any						     = activation_dropout
		A_		:    Any						     = encoder_layerdrop
		A_		:    List[Any]						     = decoder_layerdrop

		A_		:    str						     = activation_function
		A_		:    Optional[Any]						     = init_std

		A_		:    Optional[int]						     = use_cache

		# Informer
		A_		:    Dict						     = attention_type
		A_		:    List[Any]						     = sampling_factor
		A_		:    List[Any]						     = distil

		super().__init__(is_encoder_decoder=lowerCAmelCase_  ,  **lowerCAmelCase_				)





	@property
	def  lowerCamelCase(self				):
		return (
		    sum(self.embedding_dimension				)
		    + self.num_dynamic_real_features
		    + self.num_time_features
		    + self.num_static_real_features
		    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
		)

