





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

# General docstring
__A											=						"""UperNetConfig"""






class    _lowerCAmelCase  (	nn.Module			):
       """simple docstring"""




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



         '''simple docstring'''

         super().__init__()
         lowerCAmelCase__     :Optional[int]											=				nn.Convad(
             in_channels=__UpperCAmelCase       ,					out_channels=__UpperCAmelCase       ,					kernel_size=__UpperCAmelCase       ,					padding=__UpperCAmelCase       ,					bias=__UpperCAmelCase       ,					dilation=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :Any											=				nn.BatchNormad(__UpperCAmelCase )
         lowerCAmelCase__     :int											=				nn.ReLU()






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				self.conv(__UpperCAmelCase )
         lowerCAmelCase__     :List[Any]											=				self.batch_norm(__UpperCAmelCase )
         lowerCAmelCase__     :List[str]											=				self.activation(__UpperCAmelCase )

         return output






class    _lowerCAmelCase  (	nn.Module			):
       """simple docstring"""




       def __init__(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase ):



         '''simple docstring'''

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






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				input
         for layer in self.layers:
           lowerCAmelCase__     :Any											=				layer(__UpperCAmelCase )
         return hidden_state






class    _lowerCAmelCase  (	nn.Module			):
       """simple docstring"""




       def __init__(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase ):



         '''simple docstring'''

         super().__init__()
         lowerCAmelCase__     :Union[str, Any]											=				pool_scales
         lowerCAmelCase__     :Union[str, Any]											=				align_corners
         lowerCAmelCase__     :List[str]											=				in_channels
         lowerCAmelCase__     :Any											=				channels
         lowerCAmelCase__     :Optional[int]											=				[]
         for i, pool_scale in enumerate(__UpperCAmelCase ):
           lowerCAmelCase__     :List[Any]											=				UperNetPyramidPoolingBlock(pool_scale=__UpperCAmelCase       ,					in_channels=__UpperCAmelCase       ,					channels=__UpperCAmelCase )
           self.blocks.append(__UpperCAmelCase )
           self.add_module(str(__UpperCAmelCase )       ,					__UpperCAmelCase )






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				[]
         for ppm in self.blocks:
           lowerCAmelCase__     :Optional[Any]											=				ppm(__UpperCAmelCase )
           lowerCAmelCase__     :Union[str, Any]											=				nn.functional.interpolate(
               __UpperCAmelCase       ,					size=x.size()[2:]       ,					mode='bilinear'       ,					align_corners=self.align_corners )
           ppm_outs.append(__UpperCAmelCase )
         return ppm_outs






class    _lowerCAmelCase  (	nn.Module			):
       """simple docstring"""




       def __init__(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase ):



         '''simple docstring'''

         super().__init__()

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

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

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






       def 	snake_case		(       self ):



         '''simple docstring'''

         self.apply(self._init_weights )






       def 	snake_case		(       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 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				inputs[-1]
         lowerCAmelCase__     :Optional[int]											=				[x]
         psp_outs.extend(self.psp_modules(__UpperCAmelCase ) )
         lowerCAmelCase__     :Dict											=				torch.cat(__UpperCAmelCase       ,					dim=1 )
         lowerCAmelCase__     :Dict											=				self.bottleneck(__UpperCAmelCase )

         return output






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

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

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

         return output






class    _lowerCAmelCase  (	nn.Module			):
       """simple docstring"""




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



         '''simple docstring'''

         super().__init__()

         lowerCAmelCase__     :int											=				config
         lowerCAmelCase__     :Tuple											=				config.auxiliary_in_channels
         lowerCAmelCase__     :List[str]											=				config.auxiliary_channels
         lowerCAmelCase__     :Optional[Any]											=				config.auxiliary_num_convs
         lowerCAmelCase__     :Dict											=				config.auxiliary_concat_input
         lowerCAmelCase__     :Union[str, Any]											=				in_index

         lowerCAmelCase__     :str											=				(kernel_size // 2) * dilation
         lowerCAmelCase__     :Dict											=				[]
         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__     :str											=				nn.Identity()
         else:
           lowerCAmelCase__     :Tuple											=				nn.Sequential(*__UpperCAmelCase )
         if self.concat_input:
           lowerCAmelCase__     :Any											=				UperNetConvModule(
               self.in_channels + self.channels       ,					self.channels       ,					kernel_size=__UpperCAmelCase       ,					padding=kernel_size // 2 )

         lowerCAmelCase__     :Dict											=				nn.Convad(self.channels       ,					config.num_labels       ,					kernel_size=1 )






       def 	snake_case		(       self ):



         '''simple docstring'''

         self.apply(self._init_weights )






       def 	snake_case		(       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 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :List[Any]											=				encoder_hidden_states[self.in_index]
         lowerCAmelCase__     :Any											=				self.convs(__UpperCAmelCase )
         if self.concat_input:
           lowerCAmelCase__     :str											=				self.conv_cat(torch.cat([hidden_states, output]       ,					dim=1 ) )
         lowerCAmelCase__     :Tuple											=				self.classifier(__UpperCAmelCase )
         return output






class    _lowerCAmelCase  (	a			):
       """simple docstring"""




       __magic_name__	:Optional[Any]						    =   UperNetConfig
       __magic_name__	:List[str]						    =   """pixel_values"""
       __magic_name__	:Any						    =   True






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

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






       def 	snake_case		(       self ):



         '''simple docstring'''

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






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase=False ):



         '''simple docstring'''

         if isinstance(__UpperCAmelCase       ,					__UpperCAmelCase ):
           lowerCAmelCase__     :Optional[Any]											=				value






__A											=						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											=						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."""    ,  a    ,  )
class    _lowerCAmelCase  (	a			):
       """simple docstring"""




       def __init__(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         super().__init__(__UpperCAmelCase )

         lowerCAmelCase__     :Optional[Any]											=				AutoBackbone.from_config(config.backbone_config )

         # Semantic segmentation head(s)
         lowerCAmelCase__     :Union[str, Any]											=				UperNetHead(__UpperCAmelCase       ,					in_channels=self.backbone.channels )
         lowerCAmelCase__     :Optional[int]											=				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 	snake_case		(       self       ,					__UpperCAmelCase = None       ,					__UpperCAmelCase = None       ,					__UpperCAmelCase = None       ,					__UpperCAmelCase = None       ,					__UpperCAmelCase = None       ,					):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				return_dict if return_dict is not None else self.config.use_return_dict
         lowerCAmelCase__     :str											=				(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         lowerCAmelCase__     :str											=				output_attentions if output_attentions is not None else self.config.output_attentions

         lowerCAmelCase__     :List[Any]											=				self.backbone.forward_with_filtered_kwargs(
             __UpperCAmelCase       ,					output_hidden_states=__UpperCAmelCase       ,					output_attentions=__UpperCAmelCase )
         lowerCAmelCase__     :List[str]											=				outputs.feature_maps

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

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

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

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

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_lowercase  =			{
    """configuration_biogpt""": ["""BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BioGptConfig"""],
    """tokenization_biogpt""": ["""BioGptTokenizer"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowercase  =			[
	    """BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """BioGptForCausalLM""",
	    """BioGptForTokenClassification""",
	    """BioGptForSequenceClassification""",
	    """BioGptModel""",
	    """BioGptPreTrainedModel""",
	]


if TYPE_CHECKING:
	from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig
	from .tokenization_biogpt import BioGptTokenizer

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_biogpt import (
		    BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    BioGptForCausalLM,
		    BioGptForSequenceClassification,
		    BioGptForTokenClassification,
		    BioGptModel,
		    BioGptPreTrainedModel,
		)


else:
	import sys

	_lowercase  =			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

