



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


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

# General docstring
_lowerCAmelCase    :Tuple     =					'UperNetConfig'
class 							_UpperCAmelCase							( nn.Module      ):





     '''simple docstring'''



     def __init__(					self       ,	A       ,	A       ,	A       ,	A = 0       ,	A = False       ,	A = 1       ,	)							->					None:
         super().__init__()
         _UpperCAmelCase							:  Optional[Any]											=    nn.Convad(
             in_channels=A       ,	out_channels=A       ,	kernel_size=A       ,	padding=A       ,	bias=A       ,	dilation=A       ,	)
         _UpperCAmelCase							:  List[str]											=    nn.BatchNormad(A		)
         _UpperCAmelCase							:  Optional[Any]											=    nn.ReLU()





     def  __lowerCAmelCase	(					self       ,	A		)							->					torch.Tensor:
         _UpperCAmelCase							:  List[Any]											=    self.conv(A		)
         _UpperCAmelCase							:  Optional[Any]											=    self.batch_norm(A		)
         _UpperCAmelCase							:  Union[str, Any]											=    self.activation(A		)

         return output



class 							_UpperCAmelCase							( nn.Module      ):





     '''simple docstring'''



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





     def  __lowerCAmelCase	(					self       ,	A		)							->					torch.Tensor:
         _UpperCAmelCase							:  Optional[int]											=    input
         for layer in self.layers:
             _UpperCAmelCase							:  Any											=    layer(A		)
         return hidden_state



class 							_UpperCAmelCase							( nn.Module      ):





     '''simple docstring'''



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





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



class 							_UpperCAmelCase							( nn.Module      ):





     '''simple docstring'''



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

         _UpperCAmelCase							:  List[Any]											=    config
         _UpperCAmelCase							:  Optional[int]											=    config.pool_scales  # e.g. (1, 2, 3, 6)
         _UpperCAmelCase							:  int											=    in_channels
         _UpperCAmelCase							:  int											=    config.hidden_size
         _UpperCAmelCase							:  int											=    False
         _UpperCAmelCase							:  Optional[int]											=    nn.Convad(self.channels       ,	config.num_labels       ,	kernel_size=1		)

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

         _UpperCAmelCase							:  Optional[Any]											=    UperNetConvModule(
             len(self.in_channels		) * self.channels       ,	self.channels       ,	kernel_size=3       ,	padding=1       ,	)





     def  __lowerCAmelCase	(					self		)							->					Dict:
         self.apply(self._init_weights		)





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

         return output





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

         # build outputs
         _UpperCAmelCase							:  List[Any]											=    [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							:  Optional[int]											=    nn.functional.interpolate(
                 fpn_outs[i]       ,	size=fpn_outs[0].shape[2:]       ,	mode='''bilinear'''       ,	align_corners=self.align_corners		)
         _UpperCAmelCase							:  Tuple											=    torch.cat(A       ,	dim=1		)
         _UpperCAmelCase							:  Tuple											=    self.fpn_bottleneck(A		)
         _UpperCAmelCase							:  Dict											=    self.classifier(A		)

         return output



class 							_UpperCAmelCase							( nn.Module      ):





     '''simple docstring'''



     def __init__(					self       ,	A       ,	A = 2       ,	A = 3       ,	A = 1		)							->					None:
         super().__init__()

         _UpperCAmelCase							:  int											=    config
         _UpperCAmelCase							:  str											=    config.auxiliary_in_channels
         _UpperCAmelCase							:  Optional[int]											=    config.auxiliary_channels
         _UpperCAmelCase							:  List[Any]											=    config.auxiliary_num_convs
         _UpperCAmelCase							:  Optional[int]											=    config.auxiliary_concat_input
         _UpperCAmelCase							:  List[str]											=    in_index

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

         _UpperCAmelCase							:  Optional[Any]											=    nn.Convad(self.channels       ,	config.num_labels       ,	kernel_size=1		)





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





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



class 							_UpperCAmelCase							( a      ):





     '''simple docstring'''



     a__									=UperNetConfig
     a__									='''pixel_values'''
     a__									=True





     def  __lowerCAmelCase	(					self       ,	A		)							->					Optional[int]:
         if isinstance(A       ,	A		):
             module.backbone.init_weights()
             module.decode_head.init_weights()
             module.auxiliary_head.init_weights()





     def  __lowerCAmelCase	(					self		)							->					Optional[int]:
         self.backbone.init_weights()
         self.decode_head.init_weights()
         self.auxiliary_head.init_weights()





     def  __lowerCAmelCase	(					self       ,	A       ,	A=False		)							->					Optional[Any]:
         if isinstance(A       ,	A		):
             _UpperCAmelCase							:  int											=    value



_lowerCAmelCase    :str     =					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'

_lowerCAmelCase    :List[Any]     =					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.'''		,a		,)
class 							_UpperCAmelCase							( a      ):





     '''simple docstring'''



     def __init__(					self       ,	A		)							->					Tuple:
         super().__init__(A		)

         _UpperCAmelCase							:  str											=    AutoBackbone.from_config(config.backbone_config		)

         # Semantic segmentation head(s)
         _UpperCAmelCase							:  List[Any]											=    UperNetHead(A       ,	in_channels=self.backbone.channels		)
         _UpperCAmelCase							:  Optional[int]											=    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  __lowerCAmelCase	(					self       ,	A = None       ,	A = None       ,	A = None       ,	A = None       ,	A = None       ,	)							->					Union[tuple, SemanticSegmenterOutput]:
         _UpperCAmelCase							:  Dict											=    return_dict if return_dict is not None else self.config.use_return_dict
         _UpperCAmelCase							:  Tuple											=    (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         _UpperCAmelCase							:  Optional[int]											=    output_attentions if output_attentions is not None else self.config.output_attentions

         _UpperCAmelCase							:  int											=    self.backbone.forward_with_filtered_kwargs(
             A       ,	output_hidden_states=A       ,	output_attentions=A		)
         _UpperCAmelCase							:  List[str]											=    outputs.feature_maps

         _UpperCAmelCase							:  Dict											=    self.decode_head(A		)
         _UpperCAmelCase							:  Optional[Any]											=    nn.functional.interpolate(A       ,	size=pixel_values.shape[2:]       ,	mode='''bilinear'''       ,	align_corners=A		)

         _UpperCAmelCase							:  Union[str, Any]											=    None
         if self.auxiliary_head is not None:
             _UpperCAmelCase							:  Tuple											=    self.auxiliary_head(A		)
             _UpperCAmelCase							:  str											=    nn.functional.interpolate(
                 A       ,	size=pixel_values.shape[2:]       ,	mode='''bilinear'''       ,	align_corners=A		)

         _UpperCAmelCase							:  int											=    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							:  Any											=    CrossEntropyLoss(ignore_index=self.config.loss_ignore_index		)
                 _UpperCAmelCase							:  str											=    loss_fct(A       ,	A		)
                 _UpperCAmelCase							:  Optional[Any]											=    loss_fct(A       ,	A		)
                 _UpperCAmelCase							:  str											=    main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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







"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCAmelCase    :List[str]     =					logging.get_logger(__name__)

_lowerCAmelCase    :Any     =					{
    'tiiuae/falcon-40b': 'https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json',
    'tiiuae/falcon-7b': 'https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json',
}
class 							_UpperCAmelCase							( a      ):





     '''simple docstring'''



     a__									='''falcon'''
     a__									=['''past_key_values''']





     def __init__(					self       ,	A=6_5_0_2_4       ,	A=4_5_4_4       ,	A=3_2       ,	A=7_1       ,	A=1E-5       ,	A=0.02       ,	A=True       ,	A=0.0       ,	A=0.0       ,	A=None       ,	A=False       ,	A=False       ,	A=True       ,	A=True       ,	A=False       ,	A=1_1       ,	A=1_1       ,	**A       ,	)							->					Any:
         _UpperCAmelCase							:  int											=    vocab_size
         # Backward compatibility with n_embed kwarg
         _UpperCAmelCase							:  Optional[Any]											=    kwargs.pop('''n_embed'''       ,	A		)
         _UpperCAmelCase							:  int											=    hidden_size if n_embed is None else n_embed
         _UpperCAmelCase							:  List[str]											=    num_hidden_layers
         _UpperCAmelCase							:  Tuple											=    num_attention_heads
         _UpperCAmelCase							:  Optional[int]											=    layer_norm_epsilon
         _UpperCAmelCase							:  Tuple											=    initializer_range
         _UpperCAmelCase							:  Optional[int]											=    use_cache
         _UpperCAmelCase							:  Any											=    hidden_dropout
         _UpperCAmelCase							:  Dict											=    attention_dropout

         _UpperCAmelCase							:  Any											=    bos_token_id
         _UpperCAmelCase							:  List[Any]											=    eos_token_id
         _UpperCAmelCase							:  Tuple											=    num_attention_heads if num_kv_heads is None else num_kv_heads
         _UpperCAmelCase							:  Dict											=    alibi
         _UpperCAmelCase							:  Optional[int]											=    new_decoder_architecture
         _UpperCAmelCase							:  str											=    multi_query  # Ignored when new_decoder_architecture is True
         _UpperCAmelCase							:  Optional[int]											=    parallel_attn
         _UpperCAmelCase							:  Optional[int]											=    bias

         super().__init__(bos_token_id=A       ,	eos_token_id=A       ,	**A		)





     @property
     def  __lowerCAmelCase	(					self		)							->					List[str]:
         return self.hidden_size // self.num_attention_heads





     @property
     def  __lowerCAmelCase	(					self		)							->					List[Any]:
         return not self.alibi


