

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



 '''simple docstring'''


 def __init__(self			,			__a			,			__a			,			__a			,			__a = 0			,			__a = False			,			__a = 1			,			)					->   None:






  """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			,			__a					)					->   torch.Tensor:






  """simple docstring"""





  UpperCAmelCase__     							=							self.conv(__a					)
  UpperCAmelCase__     							=							self.batch_norm(__a					)
  UpperCAmelCase__     							=							self.activation(__a					)

  return output







class        lowercase		(			nn.Module   ):



 '''simple docstring'''


 def __init__(self			,			__a			,			__a			,			__a					)					->   None:






  """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			,			__a					)					->   torch.Tensor:






  """simple docstring"""





  UpperCAmelCase__     							=							input
  for layer in self.layers:
   UpperCAmelCase__     							=							layer(__a					)
  return hidden_state







class        lowercase		(			nn.Module   ):



 '''simple docstring'''


 def __init__(self			,			__a			,			__a			,			__a			,			__a					)					->   None:






  """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			,			__a					)					->   List[torch.Tensor]:






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



 '''simple docstring'''


 def __init__(self			,			__a			,			__a					)					->   Any:






  """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					)					->   Optional[Any]:






  """simple docstring"""





  self.apply(self._init_weights					)




 def     UpperCamelCase__				(self			,			__a					)					->   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			,			__a					)					->   str:






  """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			,			__a					)					->   torch.Tensor:






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



 '''simple docstring'''


 def __init__(self			,			__a			,			__a = 2			,			__a = 3			,			__a = 1					)					->   None:






  """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					)					->   Union[str, Any]:






  """simple docstring"""





  self.apply(self._init_weights					)




 def     UpperCamelCase__				(self			,			__a					)					->   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			,			__a					)					->   torch.Tensor:






  """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        lowercase		(			_lowercase   ):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE   				=							UperNetConfig
 __SCREAMING_SNAKE_CASE   				=							'''pixel_values'''
 __SCREAMING_SNAKE_CASE   				=							True




 def     UpperCamelCase__				(self			,			__a					)					->   Any:






  """simple docstring"""





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




 def     UpperCamelCase__				(self					)					->   Dict:






  """simple docstring"""





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




 def     UpperCamelCase__				(self			,			__a			,			__a=False					)					->   Tuple:






  """simple docstring"""





  if isinstance(__a			,			__a					):
   UpperCAmelCase__     							=							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."""					,							_lowercase					,							)
class        lowercase		(			_lowercase   ):



 '''simple docstring'''


 def __init__(self			,			__a					)					->   Optional[int]:






  """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			,			__a = None			,			__a = None			,			__a = None			,			__a = None			,			__a = None			,			)					->   Union[tuple, SemanticSegmenterOutput]:






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








import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
     pass

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


a_      :      Union[str, Any]    =     logging.get_logger(__name__)

a_      :      Optional[Any]    =     {
    'kakaobrain/align-base': 'https://huggingface.co/kakaobrain/align-base/resolve/main/config.json',
}
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    int      						=					'''align_text_model'''

 def __init__(	self		,				SCREAMING_SNAKE_CASE=3_0_5_2_2		,				SCREAMING_SNAKE_CASE=7_6_8		,				SCREAMING_SNAKE_CASE=1_2		,				SCREAMING_SNAKE_CASE=1_2		,				SCREAMING_SNAKE_CASE=3_0_7_2		,				SCREAMING_SNAKE_CASE="gelu"		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=5_1_2		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=0.02		,				SCREAMING_SNAKE_CASE=1e-1_2		,				SCREAMING_SNAKE_CASE=0		,				SCREAMING_SNAKE_CASE="absolute"		,				SCREAMING_SNAKE_CASE=True		,				**SCREAMING_SNAKE_CASE		,				) ->			List[str]:
  super().__init__(**SCREAMING_SNAKE_CASE      )

  a__												= vocab_size
  a__												= hidden_size
  a__												= num_hidden_layers
  a__												= num_attention_heads
  a__												= hidden_act
  a__												= intermediate_size
  a__												= hidden_dropout_prob
  a__												= attention_probs_dropout_prob
  a__												= max_position_embeddings
  a__												= type_vocab_size
  a__												= initializer_range
  a__												= layer_norm_eps
  a__												= position_embedding_type
  a__												= use_cache
  a__												= pad_token_id



 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			"PretrainedConfig":
  cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE      )

  a__							,			a__												= cls.get_config_dict(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  # get the text config dict if we are loading from AlignConfig
  if config_dict.get('''model_type'''      ) == "align":
   a__												= config_dict['''text_config''']

  if "model_type" in config_dict and hasattr(cls		,				'''model_type'''      ) and config_dict["model_type"] != cls.model_type:
   logger.warning(
       f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
       f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."      )

  return cls.from_dict(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )





class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    str      						=					'''align_vision_model'''

 def __init__(	self		,				SCREAMING_SNAKE_CASE = 3		,				SCREAMING_SNAKE_CASE = 6_0_0		,				SCREAMING_SNAKE_CASE = 2.0		,				SCREAMING_SNAKE_CASE = 3.1		,				SCREAMING_SNAKE_CASE = 8		,				SCREAMING_SNAKE_CASE = [3, 3, 5, 3, 5, 5, 3]		,				SCREAMING_SNAKE_CASE = [3_2, 1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2]		,				SCREAMING_SNAKE_CASE = [1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2, 3_2_0]		,				SCREAMING_SNAKE_CASE = []		,				SCREAMING_SNAKE_CASE = [1, 2, 2, 2, 1, 2, 1]		,				SCREAMING_SNAKE_CASE = [1, 2, 2, 3, 3, 4, 1]		,				SCREAMING_SNAKE_CASE = [1, 6, 6, 6, 6, 6, 6]		,				SCREAMING_SNAKE_CASE = 0.25		,				SCREAMING_SNAKE_CASE = "swish"		,				SCREAMING_SNAKE_CASE = 2_5_6_0		,				SCREAMING_SNAKE_CASE = "mean"		,				SCREAMING_SNAKE_CASE = 0.02		,				SCREAMING_SNAKE_CASE = 0.0_01		,				SCREAMING_SNAKE_CASE = 0.99		,				SCREAMING_SNAKE_CASE = 0.2		,				**SCREAMING_SNAKE_CASE		,				) ->			str:
  super().__init__(**SCREAMING_SNAKE_CASE      )

  a__												= num_channels
  a__												= image_size
  a__												= width_coefficient
  a__												= depth_coefficient
  a__												= depth_divisor
  a__												= kernel_sizes
  a__												= in_channels
  a__												= out_channels
  a__												= depthwise_padding
  a__												= strides
  a__												= num_block_repeats
  a__												= expand_ratios
  a__												= squeeze_expansion_ratio
  a__												= hidden_act
  a__												= hidden_dim
  a__												= pooling_type
  a__												= initializer_range
  a__												= batch_norm_eps
  a__												= batch_norm_momentum
  a__												= drop_connect_rate
  a__												= sum(SCREAMING_SNAKE_CASE      ) * 4



 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			"PretrainedConfig":
  cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE      )

  a__							,			a__												= cls.get_config_dict(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  # get the vision config dict if we are loading from AlignConfig
  if config_dict.get('''model_type'''      ) == "align":
   a__												= config_dict['''vision_config''']

  if "model_type" in config_dict and hasattr(cls		,				'''model_type'''      ) and config_dict["model_type"] != cls.model_type:
   logger.warning(
       f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
       f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."      )

  return cls.from_dict(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )





class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    Dict      						=					'''align'''
 _lowercase   :    Optional[Any]      						=					True

 def __init__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=6_4_0		,				SCREAMING_SNAKE_CASE=1.0		,				SCREAMING_SNAKE_CASE=0.02		,				**SCREAMING_SNAKE_CASE		,				) ->			List[str]:
  super().__init__(**SCREAMING_SNAKE_CASE      )

  if text_config is None:
   a__												= {}
   logger.info('''text_config is None. Initializing the AlignTextConfig with default values.'''      )

  if vision_config is None:
   a__												= {}
   logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.'''      )

  a__												= AlignTextConfig(**SCREAMING_SNAKE_CASE      )
  a__												= AlignVisionConfig(**SCREAMING_SNAKE_CASE      )

  a__												= projection_dim
  a__												= temperature_init_value
  a__												= initializer_range

 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			Optional[int]:
  return cls(text_config=text_config.to_dict()		,				vision_config=vision_config.to_dict()		,				**SCREAMING_SNAKE_CASE      )



 def 						_UpperCAmelCase       (	self      ) ->			int:
  a__												= copy.deepcopy(self.__dict__      )
  a__												= self.text_config.to_dict()
  a__												= self.vision_config.to_dict()
  a__												= self.__class__.model_type
  return output





