


import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE__     					=			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__     					=			{
    "nvidia/segformer-b0-finetuned-ade-512-512": (
        "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}



class 						_UpperCAmelCase      (     lowercase							):
		lowerCamelCase_	:			Any       			=  """segformer"""






		def __init__(						self				:     Optional[Any]   ,  UpperCAmelCase				:     Dict=3   ,  UpperCAmelCase				:     int=4   ,  UpperCAmelCase				:     Tuple=[2, 2, 2, 2]   ,  UpperCAmelCase				:     int=[8, 4, 2, 1]   ,  UpperCAmelCase				:     Optional[int]=[32, 64, 1_60, 2_56]   ,  UpperCAmelCase				:     List[str]=[7, 3, 3, 3]   ,  UpperCAmelCase				:     Optional[Any]=[4, 2, 2, 2]   ,  UpperCAmelCase				:     List[str]=[1, 2, 5, 8]   ,  UpperCAmelCase				:     Optional[int]=[4, 4, 4, 4]   ,  UpperCAmelCase				:     Optional[int]="gelu"   ,  UpperCAmelCase				:     Optional[Any]=0.0   ,  UpperCAmelCase				:     int=0.0   ,  UpperCAmelCase				:     List[Any]=0.1   ,  UpperCAmelCase				:     Dict=0.02   ,  UpperCAmelCase				:     List[str]=0.1   ,  UpperCAmelCase				:     Optional[Any]=1E-6   ,  UpperCAmelCase				:     str=2_56   ,  UpperCAmelCase				:     int=2_55   ,  **UpperCAmelCase				:     List[str]   ,  ):
				super().__init__(**UpperCAmelCase)

				if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
						warnings.warn(
						    "Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be"
						    " removed, as the behaviour will default to that of reshape_last_stage = True."   ,  UpperCAmelCase   ,  )

				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						num_channels
				SCREAMING_SNAKE_CASE_				:Tuple				     =						num_encoder_blocks
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						depths
				SCREAMING_SNAKE_CASE_				:Tuple				     =						sr_ratios
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						hidden_sizes
				SCREAMING_SNAKE_CASE_				:int				     =						patch_sizes
				SCREAMING_SNAKE_CASE_				:List[str]				     =						strides
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						mlp_ratios
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						num_attention_heads
				SCREAMING_SNAKE_CASE_				:Any				     =						hidden_act
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						hidden_dropout_prob
				SCREAMING_SNAKE_CASE_				:int				     =						attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE_				:Any				     =						classifier_dropout_prob
				SCREAMING_SNAKE_CASE_				:str				     =						initializer_range
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						drop_path_rate
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						layer_norm_eps
				SCREAMING_SNAKE_CASE_				:int				     =						decoder_hidden_size
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						kwargs.get("reshape_last_stage"   ,  UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:int				     =						semantic_loss_ignore_index




class 						_UpperCAmelCase      (     lowercase							):
		lowerCamelCase_	:			Union[str, Any]       			=  version.parse("""1.11"""							)






		@property
		def _snake_case		(						self				:     str):
				return OrderedDict(
				    [
				        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
				    ])






		@property
		def _snake_case		(						self				:     List[Any]):
				return 1E-4







		@property
		def _snake_case		(						self				:     List[str]):
				return 12










import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)



class 						_UpperCAmelCase      (     unittest.TestCase							):






		def _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						["a", "b", "c"]

				# Defaults to last layer if both are None
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Dict				     =						get_aligned_output_features_output_indices(UpperCAmelCase   ,  UpperCAmelCase   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["c"])
				self.assertEqual(UpperCAmelCase   ,  [2])

				# Out indices set to match out features
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Tuple				     =						get_aligned_output_features_output_indices(["a", "c"]   ,  UpperCAmelCase   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["a", "c"])
				self.assertEqual(UpperCAmelCase   ,  [0, 2])

				# Out features set to match out indices
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:int				     =						get_aligned_output_features_output_indices(UpperCAmelCase   ,  [0, 2]   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["a", "c"])
				self.assertEqual(UpperCAmelCase   ,  [0, 2])

				# Out features selected from negative indices
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Any				     =						get_aligned_output_features_output_indices(UpperCAmelCase   ,  [-3, -1]   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["a", "c"])
				self.assertEqual(UpperCAmelCase   ,  [-3, -1])






		def _snake_case		(						self				:     str):
				# Stage names must be set
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0, 1)   ,  UpperCAmelCase)

				# Out features must be a list
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(("a", "b")   ,  (0, 1)   ,  ["a", "b"])

				# Out features must be a subset of stage names
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0, 1)   ,  ["a"])

				# Out indices must be a list or tuple
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(UpperCAmelCase   ,  0   ,  ["a", "b"])

				# Out indices must be a subset of stage names
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(UpperCAmelCase   ,  (0, 1)   ,  ["a"])

				# Out features and out indices must be the same length
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0,)   ,  ["a", "b", "c"])

				# Out features should match out indices
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0, 2)   ,  ["a", "b", "c"])

				# Out features and out indices should be in order
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["b", "a"]   ,  (0, 1)   ,  ["a", "b"])

				# Check passes with valid inputs
				verify_out_features_out_indices(["a", "b", "d"]   ,  (0, 1, -1)   ,  ["a", "b", "c", "d"])







		def _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				:int				     =						BackboneMixin()

				SCREAMING_SNAKE_CASE_				:List[str]				     =						["a", "b", "c"]
				SCREAMING_SNAKE_CASE_				:str				     =						["a", "c"]
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						[0, 2]

				# Check that the output features and indices are set correctly
				self.assertEqual(backbone.out_features   ,  ["a", "c"])
				self.assertEqual(backbone.out_indices   ,  [0, 2])

				# Check out features and indices are updated correctly
				SCREAMING_SNAKE_CASE_				:Tuple				     =						["a", "b"]
				self.assertEqual(backbone.out_features   ,  ["a", "b"])
				self.assertEqual(backbone.out_indices   ,  [0, 1])

				SCREAMING_SNAKE_CASE_				:List[str]				     =						[-3, -1]
				self.assertEqual(backbone.out_features   ,  ["a", "c"])
				self.assertEqual(backbone.out_indices   ,  [-3, -1])






