"""simple docstring"""






import unittest

from transformers import BigBirdTokenizer, BigBirdTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


__A			   = "▁"

__A			   = get_tests_dir("fixtures/test_sentencepiece.model")


@require_sentencepiece
@require_tokenizers
class     UpperCAmelCase						(_UpperCAmelCase    ,unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Union[str, Any]						    = BigBirdTokenizer
    _UpperCAmelCase	:Tuple						    = BigBirdTokenizerFast
    _UpperCAmelCase	:str						    = True
    _UpperCAmelCase	:Tuple						    = True






    def _snake_case			(   self							):
       super().setUp()

       lowercase__:   Optional[Any]					=      self.tokenizer_class(_UpperCAmelCase    ,			keep_accents=_UpperCAmelCase							)
       tokenizer.save_pretrained(self.tmpdirname							)






    def _snake_case			(   self							):
       lowercase__:   List[str]					=      '''<s>'''
       lowercase__:   Optional[int]					=      1

       self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase							)    ,			_UpperCAmelCase							)
       self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase							)    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       lowercase__:   Union[str, Any]					=      list(self.get_tokenizer().get_vocab().keys()							)

       self.assertEqual(vocab_keys[0]    ,			'''<unk>'''							)
       self.assertEqual(vocab_keys[1]    ,			'''<s>'''							)
       self.assertEqual(vocab_keys[-1]    ,			'''[MASK]'''							)
       self.assertEqual(len(_UpperCAmelCase							)    ,			1004							)






    def _snake_case			(   self							):
       self.assertEqual(self.get_tokenizer().vocab_size    ,			1000							)






    def _snake_case			(   self							):
       if not self.test_rust_tokenizer:
          return

       lowercase__:   Tuple					=      self.get_tokenizer()
       lowercase__:   Tuple					=      self.get_rust_tokenizer()

       lowercase__:   Union[str, Any]					=      '''I was born in 92000, and this is falsé.'''

       lowercase__:   int					=      tokenizer.tokenize(_UpperCAmelCase							)
       lowercase__:   Optional[Any]					=      rust_tokenizer.tokenize(_UpperCAmelCase							)
       self.assertListEqual(_UpperCAmelCase    ,			_UpperCAmelCase							)

       lowercase__:   Tuple					=      tokenizer.encode(_UpperCAmelCase    ,			add_special_tokens=_UpperCAmelCase							)
       lowercase__:   List[str]					=      rust_tokenizer.encode(_UpperCAmelCase    ,			add_special_tokens=_UpperCAmelCase							)
       self.assertListEqual(_UpperCAmelCase    ,			_UpperCAmelCase							)

       lowercase__:   Any					=      self.get_rust_tokenizer()
       lowercase__:   str					=      tokenizer.encode(_UpperCAmelCase							)
       lowercase__:   Union[str, Any]					=      rust_tokenizer.encode(_UpperCAmelCase							)
       self.assertListEqual(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       lowercase__:   Dict					=      BigBirdTokenizer(_UpperCAmelCase    ,			keep_accents=_UpperCAmelCase							)

       lowercase__:   Dict					=      tokenizer.tokenize('''This is a test'''							)
       self.assertListEqual(_UpperCAmelCase    ,			['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']							)

       self.assertListEqual(
           tokenizer.convert_tokens_to_ids(_UpperCAmelCase							)    ,			[285, 46, 10, 170, 382]    ,			)

       lowercase__:   Dict					=      tokenizer.tokenize('''I was born in 92000, and this is falsé.'''							)
       self.assertListEqual(
           _UpperCAmelCase    ,			[
               SPIECE_UNDERLINE + '''I''',
               SPIECE_UNDERLINE + '''was''',
               SPIECE_UNDERLINE + '''b''',
               '''or''',
               '''n''',
               SPIECE_UNDERLINE + '''in''',
               SPIECE_UNDERLINE + '''''',
               '''9''',
               '''2''',
               '''0''',
               '''0''',
               '''0''',
               ''',''',
               SPIECE_UNDERLINE + '''and''',
               SPIECE_UNDERLINE + '''this''',
               SPIECE_UNDERLINE + '''is''',
               SPIECE_UNDERLINE + '''f''',
               '''al''',
               '''s''',
               '''é''',
               '''.''',
           ]    ,			)
       lowercase__:   List[Any]					=      tokenizer.convert_tokens_to_ids(_UpperCAmelCase							)
       self.assertListEqual(
           _UpperCAmelCase    ,			[8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4]    ,			)

       lowercase__:   Any					=      tokenizer.convert_ids_to_tokens(_UpperCAmelCase							)
       self.assertListEqual(
           _UpperCAmelCase    ,			[
               SPIECE_UNDERLINE + '''I''',
               SPIECE_UNDERLINE + '''was''',
               SPIECE_UNDERLINE + '''b''',
               '''or''',
               '''n''',
               SPIECE_UNDERLINE + '''in''',
               SPIECE_UNDERLINE + '''''',
               '''<unk>''',
               '''2''',
               '''0''',
               '''0''',
               '''0''',
               ''',''',
               SPIECE_UNDERLINE + '''and''',
               SPIECE_UNDERLINE + '''this''',
               SPIECE_UNDERLINE + '''is''',
               SPIECE_UNDERLINE + '''f''',
               '''al''',
               '''s''',
               '''<unk>''',
               '''.''',
           ]    ,			)






    @cached_property
    def _snake_case			(   self							):
       return BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base'''							)






    @slow
    def _snake_case			(   self							):
       lowercase__:   Dict					=      '''Hello World!'''
       lowercase__:   List[Any]					=      [65, 18536, 2260, 101, 66]

       self.assertListEqual(_UpperCAmelCase    ,			self.big_tokenizer.encode(_UpperCAmelCase							)							)






    @slow
    def _snake_case			(   self							):
       lowercase__:   str					=      (
           '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will'''
           ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth'''
       )
       # fmt: off
       lowercase__:   Optional[Any]					=      [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66]  # noqa: E231
       # fmt: on
       self.assertListEqual(_UpperCAmelCase    ,			self.big_tokenizer.encode(_UpperCAmelCase							)							)






    @require_torch
    @slow
    def _snake_case			(   self							):
       import torch

       from transformers import BigBirdConfig, BigBirdModel

       # Build sequence
       lowercase__:   Any					=      list(self.big_tokenizer.get_vocab().keys()							)[:10]
       lowercase__:   Dict					=      ''' '''.join(_UpperCAmelCase							)
       lowercase__:   Union[str, Any]					=      self.big_tokenizer.encode_plus(_UpperCAmelCase    ,			return_tensors='''pt'''    ,			return_token_type_ids=_UpperCAmelCase							)
       lowercase__:   str					=      self.big_tokenizer.batch_encode_plus(
           [sequence + ''' ''' + sequence]    ,			return_tensors='''pt'''    ,			return_token_type_ids=_UpperCAmelCase							)

       lowercase__:   List[Any]					=      BigBirdConfig(attention_type='''original_full'''							)
       lowercase__:   Any					=      BigBirdModel(_UpperCAmelCase							)

       assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size

       with torch.no_grad():
          model(**_UpperCAmelCase							)
          model(**_UpperCAmelCase							)






    @slow
    def _snake_case			(   self							):
       lowercase__:   Dict					=      BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base'''							)
       lowercase__:   Dict					=      tokenizer.decode(tokenizer('''Paris is the [MASK].'''							).input_ids							)

       self.assertTrue(decoded_text == '''[CLS] Paris is the[MASK].[SEP]'''							)






    @slow
    def _snake_case			(   self							):
       # fmt: off
       lowercase__:   Any					=      {'''input_ids''': [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
       # fmt: on

       self.tokenizer_integration_test_util(
           expected_encoding=_UpperCAmelCase    ,			model_name='''google/bigbird-roberta-base'''    ,			revision='''215c99f1600e06f83acce68422f2035b2b5c3510'''    ,			)

"""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     UpperCAmelCase						(nn.Module  ):



    """simple docstring"""

    def __init__(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase = 0    ,			_UpperCAmelCase = False    ,			_UpperCAmelCase = 1    ,			):
       super().__init__()
       lowercase__:   List[Any]					=      nn.Convad(
           in_channels=_UpperCAmelCase    ,			out_channels=_UpperCAmelCase    ,			kernel_size=_UpperCAmelCase    ,			padding=_UpperCAmelCase    ,			bias=_UpperCAmelCase    ,			dilation=_UpperCAmelCase    ,			)
       lowercase__:   List[Any]					=      nn.BatchNormad(_UpperCAmelCase							)
       lowercase__:   int					=      nn.ReLU()






    def _snake_case			(   self    ,			_UpperCAmelCase							):
       lowercase__:   Dict					=      self.conv(_UpperCAmelCase							)
       lowercase__:   Optional[int]					=      self.batch_norm(_UpperCAmelCase							)
       lowercase__:   List[Any]					=      self.activation(_UpperCAmelCase							)

       return output



class     UpperCAmelCase						(nn.Module  ):



    """simple docstring"""

    def __init__(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							):
       super().__init__()
       lowercase__:   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							):
       lowercase__:   Any					=      input
       for layer in self.layers:
          lowercase__:   Any					=      layer(_UpperCAmelCase							)
       return hidden_state



class     UpperCAmelCase						(nn.Module  ):



    """simple docstring"""

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






    def _snake_case			(   self    ,			_UpperCAmelCase							):
       lowercase__:   Union[str, Any]					=      []
       for ppm in self.blocks:
          lowercase__:   Tuple					=      ppm(_UpperCAmelCase							)
          lowercase__:   Any					=      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    ,			_UpperCAmelCase    ,			_UpperCAmelCase							):
       super().__init__()

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

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

       lowercase__:   Any					=      UperNetConvModule(
           len(self.in_channels							) * self.channels    ,			self.channels    ,			kernel_size=3    ,			padding=1    ,			)






    def _snake_case			(   self							):
       self.apply(self._init_weights							)






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

       return output






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

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

       return output



class     UpperCAmelCase						(nn.Module  ):



    """simple docstring"""

    def __init__(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase = 2    ,			_UpperCAmelCase = 3    ,			_UpperCAmelCase = 1							):
       super().__init__()

       lowercase__:   Optional[Any]					=      config
       lowercase__:   Optional[Any]					=      config.auxiliary_in_channels
       lowercase__:   List[Any]					=      config.auxiliary_channels
       lowercase__:   Tuple					=      config.auxiliary_num_convs
       lowercase__:   Any					=      config.auxiliary_concat_input
       lowercase__:   Optional[int]					=      in_index

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

       lowercase__:   Union[str, Any]					=      nn.Convad(self.channels    ,			config.num_labels    ,			kernel_size=1							)






    def _snake_case			(   self							):
       self.apply(self._init_weights							)






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



class     UpperCAmelCase						(_UpperCAmelCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Dict						    = UperNetConfig
    _UpperCAmelCase	:int						    = "pixel_values"
    _UpperCAmelCase	:Optional[Any]						    = True






    def _snake_case			(   self    ,			_UpperCAmelCase							):
       if isinstance(_UpperCAmelCase    ,			_UpperCAmelCase							):
          module.backbone.init_weights()
          module.decode_head.init_weights()
          module.auxiliary_head.init_weights()






    def _snake_case			(   self							):
       self.backbone.init_weights()
       self.decode_head.init_weights()
       self.auxiliary_head.init_weights()






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=False							):
       if isinstance(_UpperCAmelCase    ,			_UpperCAmelCase							):
          lowercase__:   Any					=      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."    ,_UpperCAmelCase    ,)
class     UpperCAmelCase						(_UpperCAmelCase  ):



    """simple docstring"""

    def __init__(   self    ,			_UpperCAmelCase							):
       super().__init__(_UpperCAmelCase							)

       lowercase__:   Optional[int]					=      AutoBackbone.from_config(config.backbone_config							)

       # Semantic segmentation head(s)
       lowercase__:   Any					=      UperNetHead(_UpperCAmelCase    ,			in_channels=self.backbone.channels							)
       lowercase__:   Tuple					=      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    ,			):
       lowercase__:   Tuple					=      return_dict if return_dict is not None else self.config.use_return_dict
       lowercase__:   Any					=      (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       lowercase__:   str					=      output_attentions if output_attentions is not None else self.config.output_attentions

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

       lowercase__:   Union[str, Any]					=      self.decode_head(_UpperCAmelCase							)
       lowercase__:   str					=      nn.functional.interpolate(_UpperCAmelCase    ,			size=pixel_values.shape[2:]    ,			mode='''bilinear'''    ,			align_corners=_UpperCAmelCase							)

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

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

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