






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      =				[
    'openmmlab/upernet-convnext-tiny',
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
__A      =				'UperNetConfig'





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							def __init__(  self:   Tuple		,     __A:   int		,     __A:   int		,     __A:   Union[int, Tuple[int, int]]		,     __A:   Union[int, Tuple[int, int], str] = 0		,     __A:   bool = False		,     __A:   Union[int, Tuple[int, int]] = 1		,     )    ->				None:
												super().__init__()
												_A								= nn.Convad(
												    in_channels=__A		,     out_channels=__A		,     kernel_size=__A		,     padding=__A		,     bias=__A		,     dilation=__A		,     )
												_A								= nn.BatchNormad(__A   )
												_A								= nn.ReLU()



							def       __A (  self:   List[str]		,     __A:   torch.Tensor   )    ->				torch.Tensor:
												_A								= self.conv(__A   )
												_A								= self.batch_norm(__A   )
												_A								= self.activation(__A   )

												return output





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							def __init__(  self:   List[str]		,     __A:   int		,     __A:   int		,     __A:   int   )    ->				None:
												super().__init__()
												_A								= [
												    nn.AdaptiveAvgPoolad(__A   ),
												    UperNetConvModule(__A		,     __A		,     kernel_size=1   ),
												]
												for i, layer in enumerate(self.layers   ):
																	self.add_module(str(__A   )		,     __A   )



							def       __A (  self:   int		,     __A:   torch.Tensor   )    ->				torch.Tensor:
												_A								= input
												for layer in self.layers:
																	_A								= layer(__A   )
												return hidden_state





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							def __init__(  self:   int		,     __A:   Tuple[int, ...]		,     __A:   int		,     __A:   int		,     __A:   bool   )    ->				None:
												super().__init__()
												_A								= pool_scales
												_A								= align_corners
												_A								= in_channels
												_A								= channels
												_A								= []
												for i, pool_scale in enumerate(__A   ):
																	_A								= UperNetPyramidPoolingBlock(pool_scale=__A		,     in_channels=__A		,     channels=__A   )
																	self.blocks.append(__A   )
																	self.add_module(str(__A   )		,     __A   )



							def       __A (  self:   List[Any]		,     __A:   torch.Tensor   )    ->				List[torch.Tensor]:
												_A								= []
												for ppm in self.blocks:
																	_A								= ppm(__A   )
																	_A								= nn.functional.interpolate(
																	    __A		,     size=x.size()[2:]		,     mode='''bilinear'''		,     align_corners=self.align_corners   )
																	ppm_outs.append(__A   )
												return ppm_outs





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							def __init__(  self:   Optional[Any]		,     __A:   int		,     __A:   Optional[int]   )    ->				Optional[int]:
												super().__init__()

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

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

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

							def       __A (  self:   List[str]   )    ->				List[str]:
												self.apply(self._init_weights   )

							def       __A (  self:   List[str]		,     __A:   Union[str, Any]   )    ->				Optional[int]:
												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       __A (  self:   Dict		,     __A:   Tuple   )    ->				Any:
												_A								= inputs[-1]
												_A								= [x]
												psp_outs.extend(self.psp_modules(__A   )   )
												_A								= torch.cat(__A		,     dim=1   )
												_A								= self.bottleneck(__A   )

												return output



							def       __A (  self:   Dict		,     __A:   torch.Tensor   )    ->				torch.Tensor:
												# build laterals
												_A								= [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
												_A								= len(__A   )
												for i in range(used_backbone_levels - 1		,     0		,     -1   ):
																	_A								= laterals[i - 1].shape[2:]
																	_A								= laterals[i - 1] + nn.functional.interpolate(
																	    laterals[i]		,     size=__A		,     mode='''bilinear'''		,     align_corners=self.align_corners   )

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

												return output





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							def __init__(  self:   str		,     __A:   str		,     __A:   int = 2		,     __A:   int = 3		,     __A:   Union[int, Tuple[int, int]] = 1   )    ->				None:
												super().__init__()

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

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

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

							def       __A (  self:   Tuple   )    ->				Optional[Any]:
												self.apply(self._init_weights   )

							def       __A (  self:   Tuple		,     __A:   List[Any]   )    ->				str:
												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       __A (  self:   List[Any]		,     __A:   torch.Tensor   )    ->				torch.Tensor:
												# just take the relevant feature maps
												_A								= encoder_hidden_states[self.in_index]
												_A								= self.convs(__A   )
												if self.concat_input:
																	_A								= self.conv_cat(torch.cat([hidden_states, output]		,     dim=1   )   )
												_A								= self.classifier(__A   )
												return output





class 						SCREAMING_SNAKE_CASE				( snake_case					):





							"""simple docstring"""

							A_						  =					UperNetConfig
							A_						  =					"pixel_values"
							A_						  =					True

							def       __A (  self:   List[Any]		,     __A:   Optional[int]   )    ->				str:
												if isinstance(__A		,     __A   ):
																	module.backbone.init_weights()
																	module.decode_head.init_weights()
																	module.auxiliary_head.init_weights()

							def       __A (  self:   Any   )    ->				int:
												self.backbone.init_weights()
												self.decode_head.init_weights()
												self.auxiliary_head.init_weights()



							def       __A (  self:   int		,     __A:   Tuple		,     __A:   Dict=False   )    ->				Optional[int]:
												if isinstance(__A		,     __A   ):
																	_A								= value



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

__A      =				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."  ,	snake_case  ,	)
class 						SCREAMING_SNAKE_CASE				( snake_case					):





							"""simple docstring"""

							def __init__(  self:   List[Any]		,     __A:   str   )    ->				List[Any]:
												super().__init__(__A   )

												_A								= AutoBackbone.from_config(config.backbone_config   )

												# Semantic segmentation head(s)
												_A								= UperNetHead(__A		,     in_channels=self.backbone.channels   )
												_A								= 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       __A (  self:   List[Any]		,     __A:   Optional[torch.Tensor] = None		,     __A:   Optional[bool] = None		,     __A:   Optional[bool] = None		,     __A:   Optional[torch.Tensor] = None		,     __A:   Optional[bool] = None		,     )    ->				Union[tuple, SemanticSegmenterOutput]:
												_A								= return_dict if return_dict is not None else self.config.use_return_dict
												_A								= (
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												_A								= output_attentions if output_attentions is not None else self.config.output_attentions

												_A								= self.backbone.forward_with_filtered_kwargs(
												    __A		,     output_hidden_states=__A		,     output_attentions=__A   )
												_A								= outputs.feature_maps

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

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

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

												if not return_dict:
																	if output_hidden_states:
																						_A								= (logits,) + outputs[1:]
																	else:
																						_A								= (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 flax.linen as nn
import jax
import jax.numpy as jnp





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							A_						  =					42
							A_						  =					jnp.floataa

							def       __A (  self:   Tuple   )    ->				Tuple:
												_A								= nn.Conv(
												    self.out_channels		,     kernel_size=(3, 3)		,     strides=(1, 1)		,     padding=((1, 1), (1, 1))		,     dtype=self.dtype		,     )



							def __call__(  self:   Dict		,     __A:   Dict   )    ->				Tuple:
												_A							,_A							,_A							,_A								= hidden_states.shape
												_A								= jax.image.resize(
												    __A		,     shape=(batch, height * 2, width * 2, channels)		,     method='''nearest'''		,     )
												_A								= self.conv(__A   )
												return hidden_states





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							A_						  =					42
							A_						  =					jnp.floataa

							def       __A (  self:   List[str]   )    ->				Tuple:
												_A								= nn.Conv(
												    self.out_channels		,     kernel_size=(3, 3)		,     strides=(2, 2)		,     padding=((1, 1), (1, 1))		,     dtype=self.dtype		,     )



							def __call__(  self:   Union[str, Any]		,     __A:   List[Any]   )    ->				Union[str, Any]:
												# pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
												# hidden_states = jnp.pad(hidden_states, pad_width=pad)
												_A								= self.conv(__A   )
												return hidden_states





class 						SCREAMING_SNAKE_CASE				( nn.Module					):





							"""simple docstring"""

							A_						  =					42
							A_						  =					None
							A_						  =					0.0
							A_						  =					None
							A_						  =					jnp.floataa

							def       __A (  self:   Dict   )    ->				Dict:
												_A								= self.in_channels if self.out_channels is None else self.out_channels

												_A								= nn.GroupNorm(num_groups=32		,     epsilon=1e-5   )
												_A								= nn.Conv(
												    __A		,     kernel_size=(3, 3)		,     strides=(1, 1)		,     padding=((1, 1), (1, 1))		,     dtype=self.dtype		,     )

												_A								= nn.Dense(__A		,     dtype=self.dtype   )

												_A								= nn.GroupNorm(num_groups=32		,     epsilon=1e-5   )
												_A								= nn.Dropout(self.dropout_prob   )
												_A								= nn.Conv(
												    __A		,     kernel_size=(3, 3)		,     strides=(1, 1)		,     padding=((1, 1), (1, 1))		,     dtype=self.dtype		,     )

												_A								= self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

												_A								= None
												if use_nin_shortcut:
																	_A								= nn.Conv(
																	    __A		,     kernel_size=(1, 1)		,     strides=(1, 1)		,     padding='''VALID'''		,     dtype=self.dtype		,     )



							def __call__(  self:   Dict		,     __A:   List[Any]		,     __A:   List[Any]		,     __A:   Any=True   )    ->				List[Any]:
												_A								= hidden_states
												_A								= self.norma(__A   )
												_A								= nn.swish(__A   )
												_A								= self.conva(__A   )

												_A								= self.time_emb_proj(nn.swish(__A   )   )
												_A								= jnp.expand_dims(jnp.expand_dims(__A		,     1   )		,     1   )
												_A								= hidden_states + temb

												_A								= self.norma(__A   )
												_A								= nn.swish(__A   )
												_A								= self.dropout(__A		,     __A   )
												_A								= self.conva(__A   )

												if self.conv_shortcut is not None:
																	_A								= self.conv_shortcut(__A   )

												return hidden_states + residual



