


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


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

# General docstring
a__       :							Union[str, Any]        =  '''UperNetConfig'''




class 				UpperCamelCase_			(				nn.Module):

	"""simple docstring"""





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




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       torch.Tensor     ) ->			torch.Tensor:
						__SCREAMING_SNAKE_CASE		     =       self.conv(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       self.batch_norm(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       self.activation(UpperCAmelCase__     )

						return output




class 				UpperCamelCase_			(				nn.Module):

	"""simple docstring"""





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




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




class 				UpperCamelCase_			(				nn.Module):

	"""simple docstring"""





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




	def 						UpperCAmelCase_   (				self    :       List[str]    ,    UpperCAmelCase__    :       torch.Tensor     ) ->			List[torch.Tensor]:
						__SCREAMING_SNAKE_CASE		     =       []
						for ppm in self.blocks:
											__SCREAMING_SNAKE_CASE		     =       ppm(UpperCAmelCase__     )
											__SCREAMING_SNAKE_CASE		     =       nn.functional.interpolate(
											    UpperCAmelCase__    ,    size=x.size()[2:]    ,    mode="bilinear"    ,    align_corners=self.align_corners     )
											ppm_outs.append(UpperCAmelCase__     )
						return ppm_outs




class 				UpperCamelCase_			(				nn.Module):

	"""simple docstring"""





	def __init__(				self    :       Dict    ,    UpperCAmelCase__    :       Union[str, Any]    ,    UpperCAmelCase__    :       Union[str, Any]     ) ->			Any:
						super().__init__()

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

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

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




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




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       Any     ) ->			Tuple:
						if isinstance(UpperCAmelCase__    ,    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    :       Optional[Any]    ,    UpperCAmelCase__    :       int     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       inputs[-1]
						__SCREAMING_SNAKE_CASE		     =       [x]
						psp_outs.extend(self.psp_modules(UpperCAmelCase__     )     )
						__SCREAMING_SNAKE_CASE		     =       torch.cat(UpperCAmelCase__    ,    dim=1     )
						__SCREAMING_SNAKE_CASE		     =       self.bottleneck(UpperCAmelCase__     )

						return output




	def 						UpperCAmelCase_   (				self    :       str    ,    UpperCAmelCase__    :       torch.Tensor     ) ->			torch.Tensor:
						# build laterals
						__SCREAMING_SNAKE_CASE		     =       [lateral_conv(encoder_hidden_states[i]     ) for i, lateral_conv in enumerate(self.lateral_convs     )]

						laterals.append(self.psp_forward(UpperCAmelCase__     )     )

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

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

						return output




class 				UpperCamelCase_			(				nn.Module):

	"""simple docstring"""





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

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

						__SCREAMING_SNAKE_CASE		     =       (kernel_size // 2) * dilation
						__SCREAMING_SNAKE_CASE		     =       []
						convs.append(
						    UperNetConvModule(
						        self.in_channels    ,    self.channels    ,    kernel_size=UpperCAmelCase__    ,    padding=UpperCAmelCase__    ,    dilation=UpperCAmelCase__     )     )
						for i in range(self.num_convs - 1     ):
											convs.append(
											    UperNetConvModule(
											        self.channels    ,    self.channels    ,    kernel_size=UpperCAmelCase__    ,    padding=UpperCAmelCase__    ,    dilation=UpperCAmelCase__     )     )
						if self.num_convs == 0:
											__SCREAMING_SNAKE_CASE		     =       nn.Identity()
						else:
											__SCREAMING_SNAKE_CASE		     =       nn.Sequential(*UpperCAmelCase__     )
						if self.concat_input:
											__SCREAMING_SNAKE_CASE		     =       UperNetConvModule(
											    self.in_channels + self.channels    ,    self.channels    ,    kernel_size=UpperCAmelCase__    ,    padding=kernel_size // 2     )

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




	def 						UpperCAmelCase_   (				self    :       Any     ) ->			Tuple:
						self.apply(self._init_weights     )




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




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	snake_case__       :						int					      =    UperNetConfig
	snake_case__       :						Dict					      =    "pixel_values"
	snake_case__       :						int					      =    True




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]    ,    UpperCAmelCase__    :       Tuple     ) ->			List[Any]:
						if isinstance(UpperCAmelCase__    ,    UpperCAmelCase__     ):
											module.backbone.init_weights()
											module.decode_head.init_weights()
											module.auxiliary_head.init_weights()




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Tuple:
						self.backbone.init_weights()
						self.decode_head.init_weights()
						self.auxiliary_head.init_weights()




	def 						UpperCAmelCase_   (				self    :       int    ,    UpperCAmelCase__    :       Union[str, Any]    ,    UpperCAmelCase__    :       str=False     ) ->			List[str]:
						if isinstance(UpperCAmelCase__    ,    UpperCAmelCase__     ):
											__SCREAMING_SNAKE_CASE		     =       value


a__       :							Optional[int]        =  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.
'''

a__       :							List[str]        =  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."    ,						UpperCamelCase    ,						)
class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





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

						__SCREAMING_SNAKE_CASE		     =       AutoBackbone.from_config(config.backbone_config     )

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

						__SCREAMING_SNAKE_CASE		     =       self.backbone.forward_with_filtered_kwargs(
						    UpperCAmelCase__    ,    output_hidden_states=UpperCAmelCase__    ,    output_attentions=UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       outputs.feature_maps

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

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

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

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

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




"""simple docstring"""







import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand




def      UpperCAmelCase__ (lowerCAmelCase_     ):




					'''simple docstring'''


					return EnvironmentCommand()




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       ArgumentParser     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       parser.add_parser("env"     )
						download_parser.set_defaults(func=UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       huggingface_hub.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						__SCREAMING_SNAKE_CASE		     =       "NA"
						if is_torch_available():
											import torch

											__SCREAMING_SNAKE_CASE		     =       torch.__version__
											__SCREAMING_SNAKE_CASE		     =       torch.cuda.is_available()

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_transformers_available():
											import transformers

											__SCREAMING_SNAKE_CASE		     =       transformers.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_accelerate_available():
											import accelerate

											__SCREAMING_SNAKE_CASE		     =       accelerate.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_xformers_available():
											import xformers

											__SCREAMING_SNAKE_CASE		     =       xformers.__version__

						__SCREAMING_SNAKE_CASE		     =       {
						    "`diffusers` version": version,
						    "Platform": platform.platform(),
						    "Python version": platform.python_version(),
						    "PyTorch version (GPU?)": F"""{pt_version} ({pt_cuda_available})""",
						    "Huggingface_hub version": hub_version,
						    "Transformers version": transformers_version,
						    "Accelerate version": accelerate_version,
						    "xFormers version": xformers_version,
						    "Using GPU in script?": "<fill in>",
						    "Using distributed or parallel set-up in script?": "<fill in>",
						}

						print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n"     )
						print(self.format_dict(UpperCAmelCase__     )     )

						return info




	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       Optional[Any]     ) ->			str:
						return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]     ) + "\n"
