


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


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

# General docstring
lowercase_							=							'UperNetConfig'







class    __a						( nn.Module							):







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase = 0      ,	UpperCAmelCase = False      ,	UpperCAmelCase = 1      ,	):

					'''simple docstring'''






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







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.conv(UpperCAmelCase   )
					lowerCAmelCase_      							=			self.batch_norm(UpperCAmelCase   )
					lowerCAmelCase_      							=			self.activation(UpperCAmelCase   )

					return output







class    __a						( nn.Module							):







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






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







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			input
					for layer in self.layers:
						lowerCAmelCase_      							=			layer(UpperCAmelCase   )
					return hidden_state







class    __a						( nn.Module							):







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






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







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






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







class    __a						( nn.Module							):







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					super().__init__()

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

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

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







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					self.apply(self._init_weights   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					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      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






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

					return output







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






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

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

					return output







class    __a						( nn.Module							):







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase = 2      ,	UpperCAmelCase = 3      ,	UpperCAmelCase = 1   ):

					'''simple docstring'''






					super().__init__()

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

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

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







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					self.apply(self._init_weights   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					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      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






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







class    __a						( __snake_case							):
				lowerCamelCase     :       Optional[int]			       =UperNetConfig
				lowerCamelCase     :       Tuple			       ='pixel_values'
				lowerCamelCase     :       Tuple			       =True







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






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







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






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







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase=False   ):

					'''simple docstring'''






					if isinstance(UpperCAmelCase      ,	UpperCAmelCase   ):
						lowerCAmelCase_      							=			value




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

lowercase_							=							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    __a						( __snake_case							):







				def __init__( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					super().__init__(UpperCAmelCase   )

					lowerCAmelCase_      							=			AutoBackbone.from_config(config.backbone_config   )

					# Semantic segmentation head(s)
					lowerCAmelCase_      							=			UperNetHead(UpperCAmelCase      ,	in_channels=self.backbone.channels   )
					lowerCAmelCase_      							=			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      lowerCamelCase_     ( self      ,	UpperCAmelCase = None      ,	UpperCAmelCase = None      ,	UpperCAmelCase = None      ,	UpperCAmelCase = None      ,	UpperCAmelCase = None      ,	):

					'''simple docstring'''






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

					lowerCAmelCase_      							=			self.backbone.forward_with_filtered_kwargs(
					    UpperCAmelCase      ,	output_hidden_states=UpperCAmelCase      ,	output_attentions=UpperCAmelCase   )
					lowerCAmelCase_      							=			outputs.feature_maps

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

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

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

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



from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule
def      UpperCAmelCase     (       _lowercase :   str			,   _lowercase :   int			,   _lowercase :   Optional[int]=1E-12	)						-> List[str]:


	"""simple docstring"""




	lowerCAmelCase_      							=			jnp.divide(emb_a.T			,   jnp.clip(jnp.linalg.norm(_lowercase			,   axis=1	)			,   a_min=_lowercase	)	).T
	lowerCAmelCase_      							=			jnp.divide(emb_a.T			,   jnp.clip(jnp.linalg.norm(_lowercase			,   axis=1	)			,   a_min=_lowercase	)	).T
	return jnp.matmul(_lowercase			,   norm_emb_a.T	)







class    __a						( nn.Module							):
				lowerCamelCase     :       CLIPConfig
				lowerCamelCase     :       jnp.dtype			       =jnp.floataa







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			FlaxCLIPVisionModule(self.config.vision_config   )
					lowerCAmelCase_      							=			nn.Dense(self.config.projection_dim      ,	use_bias=UpperCAmelCase      ,	dtype=self.dtype   )

					lowerCAmelCase_      							=			self.param('''concept_embeds'''      ,	jax.nn.initializers.ones      ,	(17, self.config.projection_dim)   )
					lowerCAmelCase_      							=			self.param(
					    '''special_care_embeds'''      ,	jax.nn.initializers.ones      ,	(3, self.config.projection_dim)   )

					lowerCAmelCase_      							=			self.param('''concept_embeds_weights'''      ,	jax.nn.initializers.ones      ,	(17,)   )
					lowerCAmelCase_      							=			self.param('''special_care_embeds_weights'''      ,	jax.nn.initializers.ones      ,	(3,)   )







				def __call__( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.vision_model(UpperCAmelCase   )[1]
					lowerCAmelCase_      							=			self.visual_projection(UpperCAmelCase   )

					lowerCAmelCase_      							=			jax_cosine_distance(UpperCAmelCase      ,	self.special_care_embeds   )
					lowerCAmelCase_      							=			jax_cosine_distance(UpperCAmelCase      ,	self.concept_embeds   )

					# increase this value to create a stronger `nfsw` filter
					# at the cost of increasing the possibility of filtering benign image inputs
					lowerCAmelCase_      							=			0.0

					lowerCAmelCase_      							=			special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
					lowerCAmelCase_      							=			jnp.round(UpperCAmelCase      ,	3   )
					lowerCAmelCase_      							=			jnp.any(special_scores > 0      ,	axis=1      ,	keepdims=UpperCAmelCase   )
					# Use a lower threshold if an image has any special care concept
					lowerCAmelCase_      							=			is_special_care * 0.0_1

					lowerCAmelCase_      							=			cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
					lowerCAmelCase_      							=			jnp.round(UpperCAmelCase      ,	3   )
					lowerCAmelCase_      							=			jnp.any(concept_scores > 0      ,	axis=1   )

					return has_nsfw_concepts







class    __a						( __snake_case							):
				lowerCamelCase     :       Optional[Any]			       =CLIPConfig
				lowerCamelCase     :       List[str]			       ='clip_input'
				lowerCamelCase     :       int			       =FlaxStableDiffusionSafetyCheckerModule







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase = None      ,	UpperCAmelCase = 0      ,	UpperCAmelCase = jnp.floataa      ,	UpperCAmelCase = True      ,	**UpperCAmelCase      ,	):

					'''simple docstring'''






					if input_shape is None:
						lowerCAmelCase_      							=			(1, 224, 224, 3)
					lowerCAmelCase_      							=			self.module_class(config=UpperCAmelCase      ,	dtype=UpperCAmelCase      ,	**UpperCAmelCase   )
					super().__init__(UpperCAmelCase      ,	UpperCAmelCase      ,	input_shape=UpperCAmelCase      ,	seed=UpperCAmelCase      ,	dtype=UpperCAmelCase      ,	_do_init=_do_init   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase = None   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			jax.random.normal(UpperCAmelCase      ,	UpperCAmelCase   )

					lowerCAmelCase_							,	lowerCAmelCase_      							=			jax.random.split(UpperCAmelCase   )
					lowerCAmelCase_      							=			{'''params''': params_rng, '''dropout''': dropout_rng}

					lowerCAmelCase_      							=			self.module.init(UpperCAmelCase      ,	UpperCAmelCase   )['''params''']

					return random_params







				def __call__( self      ,	UpperCAmelCase      ,	UpperCAmelCase = None      ,	):

					'''simple docstring'''






					lowerCAmelCase_      							=			jnp.transpose(UpperCAmelCase      ,	(0, 2, 3, 1)   )

					return self.module.apply(
					    {'''params''': params or self.params}      ,	jnp.array(UpperCAmelCase      ,	dtype=jnp.floataa   )      ,	rngs={}      ,	)