


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


_lowerCAmelCase:						Optional[int]   				=       logging.get_logger(__name__)

_lowerCAmelCase:						Any   				=       {
    '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       lowercase_					(lowercase__   ):
				snake_case	    ='segformer'





				def __init__(				self       ,  lowercase_=3       ,  lowercase_=4       ,  lowercase_=[2, 2, 2, 2]       ,  lowercase_=[8, 4, 2, 1]       ,  lowercase_=[32, 64, 160, 256]       ,  lowercase_=[7, 3, 3, 3]       ,  lowercase_=[4, 2, 2, 2]       ,  lowercase_=[1, 2, 5, 8]       ,  lowercase_=[4, 4, 4, 4]       ,  lowercase_="gelu"       ,  lowercase_=0.0       ,  lowercase_=0.0       ,  lowercase_=0.1       ,  lowercase_=0.02       ,  lowercase_=0.1       ,  lowercase_=1e-6       ,  lowercase_=256       ,  lowercase_=255       ,  **lowercase_       ,  )  -> str:
											super().__init__(**lowercase_)

											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.'       ,  lowercase_       ,  )

											a__      =num_channels
											a__      =num_encoder_blocks
											a__      =depths
											a__      =sr_ratios
											a__      =hidden_sizes
											a__      =patch_sizes
											a__      =strides
											a__      =mlp_ratios
											a__      =num_attention_heads
											a__      =hidden_act
											a__      =hidden_dropout_prob
											a__      =attention_probs_dropout_prob
											a__      =classifier_dropout_prob
											a__      =initializer_range
											a__      =drop_path_rate
											a__      =layer_norm_eps
											a__      =decoder_hidden_size
											a__      =kwargs.get('reshape_last_stage'       ,  lowercase_)
											a__      =semantic_loss_ignore_index







class       lowercase_					(lowercase__   ):
				snake_case	    =version.parse('1.11'   )





				@property
				def 						__UpperCamelCase    (				self)  -> Mapping[str, Mapping[int, str]]:
											return OrderedDict(
											    [
											        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
											    ])





				@property
				def 						__UpperCamelCase    (				self)  -> float:
											return 1e-4





				@property
				def 						__UpperCamelCase    (				self)  -> int:
											return 12








"""simple docstring"""





import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
       import torch

       from transformers import AutoModelForImageClassification

if is_vision_available():
       from transformers import AutoImageProcessor

@require_torch
@require_vision
class 						lowercase   (   unittest.TestCase   ):





      @slow
      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""					)
            lowerCAmelCase    		=			AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""					)
            model.to(lowercase					)

            from datasets import load_dataset

            lowerCAmelCase    		=			load_dataset("""nielsr/rvlcdip-demo"""					)

            lowerCAmelCase    		=			dataset["""train"""][0]["""image"""].convert("""RGB"""					)

            lowerCAmelCase    		=			image_processor(lowercase ,		return_tensors="""pt"""					).to(lowercase					)

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase    		=			model(**lowercase					)
                  lowerCAmelCase    		=			outputs.logits

            lowerCAmelCase    		=			torch.Size((1, 16)					)
            self.assertEqual(logits.shape ,		lowercase					)

            lowerCAmelCase    		=			torch.tensor(
                [-0.4_158, -0.4_092, -0.4_347] ,		device=lowercase ,		dtype=torch.float ,		)
            self.assertTrue(torch.allclose(logits[0, :3] ,		lowercase ,		atol=1e-4					)					)






