


import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
__lowerCAmelCase		       =							sys.version_info >= (3, 10)




def    snake_case_     (  snake_case=None			,							snake_case=None     )  ->				str:
       return field(default_factory=lambda: default			,							metadata=A__     )





@dataclass
class   __a			:
 __lowercase :							Optional[int]        = 42
 __lowercase :							str        = 42
 __lowercase :							Dict        = 42
 __lowercase :							Optional[int]        = 42





@dataclass
class   __a			:
 __lowercase :							Union[str, Any]        = 42
 __lowercase :							Optional[int]        = field(default='toto'       ,       metadata={'help': 'help message'}      )





@dataclass
class   __a			:
 __lowercase :							List[Any]        = False
 __lowercase :							str        = True
 __lowercase :							Any        = None





class   __a			(    _SCREAMING_SNAKE_CASE      ):
 __lowercase :							Optional[Any]        = 'titi'
 __lowercase :							Optional[int]        = 'toto'





class   __a			(    _SCREAMING_SNAKE_CASE      ):
 __lowercase :							Union[str, Any]        = 'titi'
 __lowercase :							List[str]        = 'toto'
 __lowercase :							List[Any]        = 42





@dataclass
class   __a			:
 __lowercase :							Optional[Any]        = 'toto'
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   BasicEnum(self.foo )





@dataclass
class   __a			:
 __lowercase :							Any        = 'toto'
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   MixedTypeEnum(self.foo )





@dataclass
class   __a			:
 __lowercase :							List[str]        = None
 __lowercase :							Union[str, Any]        = field(default=_SCREAMING_SNAKE_CASE       ,       metadata={'help': 'help message'}      )
 __lowercase :							List[Any]        = None
 __lowercase :							Tuple        = list_field(default=[]      )
 __lowercase :							Optional[int]        = list_field(default=[]      )





@dataclass
class   __a			:
 __lowercase :							int        = list_field(default=[]      )
 __lowercase :							Optional[Any]        = list_field(default=[1, 2, 3]      )
 __lowercase :							Optional[int]        = list_field(default=['Hallo', 'Bonjour', 'Hello']      )
 __lowercase :							List[str]        = list_field(default=[0.1, 0.2, 0.3]      )





@dataclass
class   __a			:
 __lowercase :							Optional[Any]        = field()
 __lowercase :							str        = field()
 __lowercase :							int        = field()
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   Tuple  			=   BasicEnum(self.required_enum )





@dataclass
class   __a			:
 __lowercase :							int        = 42
 __lowercase :							Optional[int]        = field()
 __lowercase :							Optional[int]        = None
 __lowercase :							Optional[int]        = field(default='toto'       ,       metadata={'help': 'help message'}      )
 __lowercase :							int        = list_field(default=['Hallo', 'Bonjour', 'Hello']      )


if is_python_no_less_than_3_10:





 @dataclass
 class   __a			:
  __lowercase :							Optional[Any]        = False
  __lowercase :							str        = True
  __lowercase :							Any        = None





 @dataclass
 class   __a			:
  __lowercase :							str        = None
  __lowercase :							Union[str, Any]        = field(default=_SCREAMING_SNAKE_CASE       ,       metadata={'help': 'help message'}      )
  __lowercase :							Any        = None
  __lowercase :							Dict        = list_field(default=[]      )
  __lowercase :							str        = list_field(default=[]      )





class   __a			(    unittest.TestCase      ):
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__ )					->				List[str]:




        '''simple docstring'''
        self.assertEqual(len(a._actions )				, len(b._actions ) )
        for x, y in zip(a._actions				, b._actions ):
               lowercase__:   List[str]  			=   {k: v for k, v in vars(lowerCAmelCase__ ).items() if k != """container"""}
               lowercase__:   Optional[int]  			=   {k: v for k, v in vars(lowerCAmelCase__ ).items() if k != """container"""}

               # Choices with mixed type have custom function as "type"
               # So we need to compare results directly for equality
               if xx.get('choices'				, lowerCAmelCase__ ) and yy.get('choices'				, lowerCAmelCase__ ):
                      for expected_choice in yy["choices"] + xx["choices"]:
                             self.assertEqual(xx['type'](lowerCAmelCase__ )				, yy['type'](lowerCAmelCase__ ) )
                      del xx["type"], yy["type"]

               self.assertEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        lowercase__:   Tuple  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   Optional[Any]  			=   argparse.ArgumentParser()
        expected.add_argument('--foo'				, type=lowerCAmelCase__				, required=lowerCAmelCase__ )
        expected.add_argument('--bar'				, type=lowerCAmelCase__				, required=lowerCAmelCase__ )
        expected.add_argument('--baz'				, type=lowerCAmelCase__				, required=lowerCAmelCase__ )
        expected.add_argument('--flag'				, type=lowerCAmelCase__				, default=lowerCAmelCase__				, const=lowerCAmelCase__				, nargs='?' )
        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )

        lowercase__:   Optional[int]  			=   ["""--foo""", """1""", """--baz""", """quux""", """--bar""", """0.5"""]
        (lowercase__							):   List[Any]  			=   parser.parse_args_into_dataclasses(lowerCAmelCase__				, look_for_args_file=lowerCAmelCase__ )
        self.assertFalse(example.flag )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   Any  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   int  			=   argparse.ArgumentParser()
        expected.add_argument('--foo'				, default=42				, type=lowerCAmelCase__ )
        expected.add_argument('--baz'				, default='toto'				, type=lowerCAmelCase__				, help='help message' )
        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Dict  			=   argparse.ArgumentParser()
        expected.add_argument('--foo'				, type=lowerCAmelCase__				, default=lowerCAmelCase__				, const=lowerCAmelCase__				, nargs='?' )
        expected.add_argument('--baz'				, type=lowerCAmelCase__				, default=lowerCAmelCase__				, const=lowerCAmelCase__				, nargs='?' )
        # A boolean no_* argument always has to come after its "default: True" regular counter-part
        # and its default must be set to False
        expected.add_argument('--no_baz'				, action='store_false'				, default=lowerCAmelCase__				, dest='baz' )
        expected.add_argument('--opt'				, type=lowerCAmelCase__				, default=lowerCAmelCase__ )

        lowercase__:   List[Any]  			=   [WithDefaultBoolExample]
        if is_python_no_less_than_3_10:
               dataclass_types.append(lowerCAmelCase__ )

        for dataclass_type in dataclass_types:
               lowercase__:   Any  			=   HfArgumentParser(lowerCAmelCase__ )
               self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )

               lowercase__:   Dict  			=   parser.parse_args([] )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=lowerCAmelCase__				, baz=lowerCAmelCase__				, opt=lowerCAmelCase__ ) )

               lowercase__:   Dict  			=   parser.parse_args(['--foo', '--no_baz'] )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=lowerCAmelCase__				, baz=lowerCAmelCase__				, opt=lowerCAmelCase__ ) )

               lowercase__:   Optional[Any]  			=   parser.parse_args(['--foo', '--baz'] )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=lowerCAmelCase__				, baz=lowerCAmelCase__				, opt=lowerCAmelCase__ ) )

               lowercase__:   str  			=   parser.parse_args(['--foo', 'True', '--baz', 'True', '--opt', 'True'] )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=lowerCAmelCase__				, baz=lowerCAmelCase__				, opt=lowerCAmelCase__ ) )

               lowercase__:   List[str]  			=   parser.parse_args(['--foo', 'False', '--baz', 'False', '--opt', 'False'] )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=lowerCAmelCase__				, baz=lowerCAmelCase__				, opt=lowerCAmelCase__ ) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Any  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   int  			=   argparse.ArgumentParser()
        expected.add_argument(
            '--foo'				, default='toto'				, choices=['titi', 'toto', 42]				, type=make_choice_type_function(['titi', 'toto', 42] )				, )
        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )

        lowercase__:   Dict  			=   parser.parse_args([] )
        self.assertEqual(args.foo				, 'toto' )
        lowercase__:   int  			=   parser.parse_args_into_dataclasses([] )[0]
        self.assertEqual(enum_ex.foo				, MixedTypeEnum.toto )

        lowercase__:   Optional[Any]  			=   parser.parse_args(['--foo', 'titi'] )
        self.assertEqual(args.foo				, 'titi' )
        lowercase__:   List[Any]  			=   parser.parse_args_into_dataclasses(['--foo', 'titi'] )[0]
        self.assertEqual(enum_ex.foo				, MixedTypeEnum.titi )

        lowercase__:   Any  			=   parser.parse_args(['--foo', '42'] )
        self.assertEqual(args.foo				, 42 )
        lowercase__:   List[Any]  			=   parser.parse_args_into_dataclasses(['--foo', '42'] )[0]
        self.assertEqual(enum_ex.foo				, MixedTypeEnum.fourtytwo )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Any:




        '''simple docstring'''





        @dataclass
        class   __a			:
         __lowercase :							str        = 'toto'





        lowercase__:   Union[str, Any]  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   List[str]  			=   argparse.ArgumentParser()
        expected.add_argument(
            '--foo'				, default='toto'				, choices=('titi', 'toto', 42)				, type=make_choice_type_function(['titi', 'toto', 42] )				, )
        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )

        lowercase__:   Tuple  			=   parser.parse_args([] )
        self.assertEqual(args.foo				, 'toto' )

        lowercase__:   Optional[Any]  			=   parser.parse_args(['--foo', 'titi'] )
        self.assertEqual(args.foo				, 'titi' )

        lowercase__:   Union[str, Any]  			=   parser.parse_args(['--foo', '42'] )
        self.assertEqual(args.foo				, 42 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   Union[str, Any]  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   str  			=   argparse.ArgumentParser()
        expected.add_argument('--foo_int'				, nargs='+'				, default=[]				, type=lowerCAmelCase__ )
        expected.add_argument('--bar_int'				, nargs='+'				, default=[1, 2, 3]				, type=lowerCAmelCase__ )
        expected.add_argument('--foo_str'				, nargs='+'				, default=['Hallo', 'Bonjour', 'Hello']				, type=lowerCAmelCase__ )
        expected.add_argument('--foo_float'				, nargs='+'				, default=[0.1, 0.2, 0.3]				, type=lowerCAmelCase__ )

        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )

        lowercase__:   int  			=   parser.parse_args([] )
        self.assertEqual(
            lowerCAmelCase__				, Namespace(foo_int=[]				, bar_int=[1, 2, 3]				, foo_str=['Hallo', 'Bonjour', 'Hello']				, foo_float=[0.1, 0.2, 0.3] )				, )

        lowercase__:   Dict  			=   parser.parse_args('--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'.split() )
        self.assertEqual(lowerCAmelCase__				, Namespace(foo_int=[1]				, bar_int=[2, 3]				, foo_str=['a', 'b', 'c']				, foo_float=[0.1, 0.7] ) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Tuple  			=   argparse.ArgumentParser()
        expected.add_argument('--foo'				, default=lowerCAmelCase__				, type=lowerCAmelCase__ )
        expected.add_argument('--bar'				, default=lowerCAmelCase__				, type=lowerCAmelCase__				, help='help message' )
        expected.add_argument('--baz'				, default=lowerCAmelCase__				, type=lowerCAmelCase__ )
        expected.add_argument('--ces'				, nargs='+'				, default=[]				, type=lowerCAmelCase__ )
        expected.add_argument('--des'				, nargs='+'				, default=[]				, type=lowerCAmelCase__ )

        lowercase__:   Union[str, Any]  			=   [OptionalExample]
        if is_python_no_less_than_3_10:
               dataclass_types.append(lowerCAmelCase__ )

        for dataclass_type in dataclass_types:
               lowercase__:   str  			=   HfArgumentParser(lowerCAmelCase__ )

               self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )

               lowercase__:   Optional[int]  			=   parser.parse_args([] )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=lowerCAmelCase__				, bar=lowerCAmelCase__				, baz=lowerCAmelCase__				, ces=[]				, des=[] ) )

               lowercase__:   Optional[Any]  			=   parser.parse_args('--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'.split() )
               self.assertEqual(lowerCAmelCase__				, Namespace(foo=12				, bar=3.1_4				, baz='42'				, ces=['a', 'b', 'c']				, des=[1, 2, 3] ) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Union[str, Any]  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   str  			=   argparse.ArgumentParser()
        expected.add_argument('--required_list'				, nargs='+'				, type=lowerCAmelCase__				, required=lowerCAmelCase__ )
        expected.add_argument('--required_str'				, type=lowerCAmelCase__				, required=lowerCAmelCase__ )
        expected.add_argument(
            '--required_enum'				, type=make_choice_type_function(['titi', 'toto'] )				, choices=['titi', 'toto']				, required=lowerCAmelCase__				, )
        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Dict  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   List[str]  			=   argparse.ArgumentParser()
        expected.add_argument('--foo'				, type=lowerCAmelCase__				, required=lowerCAmelCase__ )
        expected.add_argument(
            '--required_enum'				, type=make_choice_type_function(['titi', 'toto'] )				, choices=['titi', 'toto']				, required=lowerCAmelCase__				, )
        expected.add_argument('--opt'				, type=lowerCAmelCase__				, default=lowerCAmelCase__ )
        expected.add_argument('--baz'				, default='toto'				, type=lowerCAmelCase__				, help='help message' )
        expected.add_argument('--foo_str'				, nargs='+'				, default=['Hallo', 'Bonjour', 'Hello']				, type=lowerCAmelCase__ )
        self.argparsersEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   Tuple  			=   {
            """foo""": 12,
            """bar""": 3.1_4,
            """baz""": """42""",
            """flag""": True,
        }

        lowercase__:   List[Any]  			=   parser.parse_dict(lowerCAmelCase__ )[0]
        lowercase__:   str  			=   BasicExample(**lowerCAmelCase__ )
        self.assertEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   int  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   List[Any]  			=   {
            """foo""": 12,
            """bar""": 3.1_4,
            """baz""": """42""",
            """flag""": True,
            """extra""": 42,
        }

        self.assertRaises(lowerCAmelCase__				, parser.parse_dict				, lowerCAmelCase__				, allow_extra_keys=lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   Optional[Any]  			=   {
            """foo""": 12,
            """bar""": 3.1_4,
            """baz""": """42""",
            """flag""": True,
        }
        with tempfile.TemporaryDirectory() as tmp_dir:
               lowercase__:   Union[str, Any]  			=   os.path.join(lowerCAmelCase__				, 'temp_json' )
               os.mkdir(lowerCAmelCase__ )
               with open(temp_local_path + '.json'				, 'w+' ) as f:
                      json.dump(lowerCAmelCase__				, lowerCAmelCase__ )
               lowercase__:   Optional[int]  			=   parser.parse_yaml_file(Path(temp_local_path + '.json' ) )[0]

        lowercase__:   Dict  			=   BasicExample(**lowerCAmelCase__ )
        self.assertEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Tuple:




        '''simple docstring'''
        lowercase__:   str  			=   HfArgumentParser(lowerCAmelCase__ )

        lowercase__:   List[str]  			=   {
            """foo""": 12,
            """bar""": 3.1_4,
            """baz""": """42""",
            """flag""": True,
        }
        with tempfile.TemporaryDirectory() as tmp_dir:
               lowercase__:   List[Any]  			=   os.path.join(lowerCAmelCase__				, 'temp_yaml' )
               os.mkdir(lowerCAmelCase__ )
               with open(temp_local_path + '.yaml'				, 'w+' ) as f:
                      yaml.dump(lowerCAmelCase__				, lowerCAmelCase__ )
               lowercase__:   List[str]  			=   parser.parse_yaml_file(Path(temp_local_path + '.yaml' ) )[0]
        lowercase__:   Dict  			=   BasicExample(**lowerCAmelCase__ )
        self.assertEqual(lowerCAmelCase__				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Any  			=   HfArgumentParser(lowerCAmelCase__ )
        self.assertIsNotNone(lowerCAmelCase__ )






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

# General docstring
__A					:  int											=      "UperNetConfig"






class    __snake_case       (   nn.Module):







    """simple docstring"""



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





    def __lowercase   (   self    :  Tuple    ,						lowerCamelCase    :  torch.Tensor		)   ->  torch.Tensor:
      lowerCAmelCase_			:      Optional[Any]			=   self.conv(lowerCamelCase		)
      lowerCAmelCase_			:      Tuple			=   self.batch_norm(lowerCamelCase		)
      lowerCAmelCase_			:      Union[str, Any]			=   self.activation(lowerCamelCase		)

      return output







class    __snake_case       (   nn.Module):







    """simple docstring"""



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





    def __lowercase   (   self    :  List[str]    ,						lowerCamelCase    :  torch.Tensor		)   ->  torch.Tensor:
      lowerCAmelCase_			:      List[Any]			=   input
      for layer in self.layers:
        lowerCAmelCase_			:      Tuple			=   layer(lowerCamelCase		)
      return hidden_state







class    __snake_case       (   nn.Module):







    """simple docstring"""



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





    def __lowercase   (   self    :  Optional[Any]    ,						lowerCamelCase    :  torch.Tensor		)   ->  List[torch.Tensor]:
      lowerCAmelCase_			:      Any			=   []
      for ppm in self.blocks:
        lowerCAmelCase_			:      Any			=   ppm(lowerCamelCase		)
        lowerCAmelCase_			:      Union[str, Any]			=   nn.functional.interpolate(
            lowerCamelCase    ,						size=x.size()[2:]    ,						mode="""bilinear"""    ,						align_corners=self.align_corners		)
        ppm_outs.append(lowerCamelCase		)
      return ppm_outs







class    __snake_case       (   nn.Module):







    """simple docstring"""



    def __init__(   self    :  int    ,						lowerCamelCase    :  List[str]    ,						lowerCamelCase    :  List[Any]		)   ->  Dict:
      super().__init__()

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

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

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

    def __lowercase   (   self    :  List[Any]		)   ->  Any:
      self.apply(self._init_weights		)

    def __lowercase   (   self    :  Optional[int]    ,						lowerCamelCase    :  str		)   ->  List[Any]:
      if isinstance(lowerCamelCase    ,						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 __lowercase   (   self    :  List[str]    ,						lowerCamelCase    :  Optional[Any]		)   ->  Any:
      lowerCAmelCase_			:      Union[str, Any]			=   inputs[-1]
      lowerCAmelCase_			:      List[str]			=   [x]
      psp_outs.extend(self.psp_modules(lowerCamelCase		)		)
      lowerCAmelCase_			:      str			=   torch.cat(lowerCamelCase    ,						dim=1		)
      lowerCAmelCase_			:      str			=   self.bottleneck(lowerCamelCase		)

      return output





    def __lowercase   (   self    :  Dict    ,						lowerCamelCase    :  torch.Tensor		)   ->  torch.Tensor:
      # build laterals
      lowerCAmelCase_			:      Optional[int]			=   [lateral_conv(encoder_hidden_states[i]		) for i, lateral_conv in enumerate(self.lateral_convs		)]

      laterals.append(self.psp_forward(lowerCamelCase		)		)

      # build top-down path
      lowerCAmelCase_			:      Tuple			=   len(lowerCamelCase		)
      for i in range(used_backbone_levels - 1    ,						0    ,						-1		):
        lowerCAmelCase_			:      Union[str, Any]			=   laterals[i - 1].shape[2:]
        lowerCAmelCase_			:      Optional[Any]			=   laterals[i - 1] + nn.functional.interpolate(
            laterals[i]    ,						size=lowerCamelCase    ,						mode="""bilinear"""    ,						align_corners=self.align_corners		)

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

      return output







class    __snake_case       (   nn.Module):







    """simple docstring"""



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

      lowerCAmelCase_			:      List[Any]			=   config
      lowerCAmelCase_			:      Dict			=   config.auxiliary_in_channels
      lowerCAmelCase_			:      Optional[Any]			=   config.auxiliary_channels
      lowerCAmelCase_			:      Dict			=   config.auxiliary_num_convs
      lowerCAmelCase_			:      int			=   config.auxiliary_concat_input
      lowerCAmelCase_			:      List[Any]			=   in_index

      lowerCAmelCase_			:      List[Any]			=   (kernel_size // 2) * dilation
      lowerCAmelCase_			:      Tuple			=   []
      convs.append(
          UperNetConvModule(
              self.in_channels    ,						self.channels    ,						kernel_size=lowerCamelCase    ,						padding=lowerCamelCase    ,						dilation=lowerCamelCase		)		)
      for i in range(self.num_convs - 1		):
        convs.append(
            UperNetConvModule(
                self.channels    ,						self.channels    ,						kernel_size=lowerCamelCase    ,						padding=lowerCamelCase    ,						dilation=lowerCamelCase		)		)
      if self.num_convs == 0:
        lowerCAmelCase_			:      Optional[Any]			=   nn.Identity()
      else:
        lowerCAmelCase_			:      List[str]			=   nn.Sequential(*lowerCamelCase		)
      if self.concat_input:
        lowerCAmelCase_			:      Union[str, Any]			=   UperNetConvModule(
            self.in_channels + self.channels    ,						self.channels    ,						kernel_size=lowerCamelCase    ,						padding=kernel_size // 2		)

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

    def __lowercase   (   self    :  int		)   ->  List[Any]:
      self.apply(self._init_weights		)

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







class    __snake_case       (   _SCREAMING_SNAKE_CASE):







    """simple docstring"""



    lowercase    =					UperNetConfig
    lowercase    =					'pixel_values'
    lowercase    =					True

    def __lowercase   (   self    :  List[str]    ,						lowerCamelCase    :  Dict		)   ->  Optional[Any]:
      if isinstance(lowerCamelCase    ,						lowerCamelCase		):
        module.backbone.init_weights()
        module.decode_head.init_weights()
        module.auxiliary_head.init_weights()

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





    def __lowercase   (   self    :  Union[str, Any]    ,						lowerCamelCase    :  List[str]    ,						lowerCamelCase    :  Any=False		)   ->  Optional[int]:
      if isinstance(lowerCamelCase    ,						lowerCamelCase		):
        lowerCAmelCase_			:      str			=   value







__A					:  Union[str, Any]											=      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					:  str											=      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.'	,_SCREAMING_SNAKE_CASE	,)
class    __snake_case       (   _SCREAMING_SNAKE_CASE):







    """simple docstring"""



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

      lowerCAmelCase_			:      Union[str, Any]			=   AutoBackbone.from_config(config.backbone_config		)

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

      lowerCAmelCase_			:      Dict			=   self.backbone.forward_with_filtered_kwargs(
          lowerCamelCase    ,						output_hidden_states=lowerCamelCase    ,						output_attentions=lowerCamelCase		)
      lowerCAmelCase_			:      Union[str, Any]			=   outputs.feature_maps

      lowerCAmelCase_			:      Dict			=   self.decode_head(lowerCamelCase		)
      lowerCAmelCase_			:      Optional[Any]			=   nn.functional.interpolate(lowerCamelCase    ,						size=pixel_values.shape[2:]    ,						mode="""bilinear"""    ,						align_corners=lowerCamelCase		)

      lowerCAmelCase_			:      Tuple			=   None
      if self.auxiliary_head is not None:
        lowerCAmelCase_			:      Optional[int]			=   self.auxiliary_head(lowerCamelCase		)
        lowerCAmelCase_			:      Union[str, Any]			=   nn.functional.interpolate(
            lowerCamelCase    ,						size=pixel_values.shape[2:]    ,						mode="""bilinear"""    ,						align_corners=lowerCamelCase		)

      lowerCAmelCase_			:      str			=   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_			:      str			=   CrossEntropyLoss(ignore_index=self.config.loss_ignore_index		)
          lowerCAmelCase_			:      int			=   loss_fct(lowerCamelCase    ,						lowerCamelCase		)
          lowerCAmelCase_			:      List[str]			=   loss_fct(lowerCamelCase    ,						lowerCamelCase		)
          lowerCAmelCase_			:      str			=   main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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

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



