
'''simple docstring'''






import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import (
					    MODEL_MAPPING,
					    BeitForImageClassification,
					    BeitForMaskedImageModeling,
					    BeitForSemanticSegmentation,
					    BeitModel,
					)
					from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					import PIL
					from PIL import Image

					from transformers import BeitImageProcessor




class     __snake_case		:
					def __init__(     self,    A,    A=100,    A=13,    A=30,    A=2,    A=3,    A=True,    A=True,    A=32,    A=4,    A=4,    A=37,    A="gelu",    A=0.1,    A=0.1,    A=10,    A=0.02,    A=3,    A=None,    A=[0, 1, 2, 3],    ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= parent
									lowerCamelCase					:			str									= 100
									lowerCamelCase					:			Tuple									= batch_size
									lowerCamelCase					:			Tuple									= image_size
									lowerCamelCase					:			Any									= patch_size
									lowerCamelCase					:			Tuple									= num_channels
									lowerCamelCase					:			int									= is_training
									lowerCamelCase					:			List[str]									= use_labels
									lowerCamelCase					:			Optional[int]									= hidden_size
									lowerCamelCase					:			str									= num_hidden_layers
									lowerCamelCase					:			Tuple									= num_attention_heads
									lowerCamelCase					:			Optional[Any]									= intermediate_size
									lowerCamelCase					:			Union[str, Any]									= hidden_act
									lowerCamelCase					:			Optional[int]									= hidden_dropout_prob
									lowerCamelCase					:			str									= attention_probs_dropout_prob
									lowerCamelCase					:			Optional[Any]									= type_sequence_label_size
									lowerCamelCase					:			int									= initializer_range
									lowerCamelCase					:			List[Any]									= scope
									lowerCamelCase					:			Dict									= out_indices
									lowerCamelCase					:			List[str]									= num_labels

									# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									lowerCamelCase					:			Tuple									= (image_size // patch_size) ** 2
									lowerCamelCase					:			List[str]									= num_patches + 1
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			str									= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

									lowerCamelCase					:			Dict									= None
									lowerCamelCase					:			Union[str, Any]									= None
									if self.use_labels:
													lowerCamelCase					:			Optional[int]									= ids_tensor([self.batch_size],    self.type_sequence_label_size      )
													lowerCamelCase					:			Tuple									= ids_tensor([self.batch_size, self.image_size, self.image_size],    self.num_labels      )

									lowerCamelCase					:			str									= self.get_config()

									return config, pixel_values, labels, pixel_labels
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return BeitConfig(
									    vocab_size=self.vocab_size,    image_size=self.image_size,    patch_size=self.patch_size,    num_channels=self.num_channels,    hidden_size=self.hidden_size,    num_hidden_layers=self.num_hidden_layers,    num_attention_heads=self.num_attention_heads,    intermediate_size=self.intermediate_size,    hidden_act=self.hidden_act,    hidden_dropout_prob=self.hidden_dropout_prob,    attention_probs_dropout_prob=self.attention_probs_dropout_prob,    is_decoder=A,    initializer_range=self.initializer_range,    out_indices=self.out_indices,    )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= BeitModel(config=A      )
									model.to(A      )
									model.eval()
									lowerCamelCase					:			Union[str, Any]									= model(A      )
									self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.seq_length, self.hidden_size)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= BeitForMaskedImageModeling(config=A      )
									model.to(A      )
									model.eval()
									lowerCamelCase					:			List[Any]									= model(A      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.seq_length - 1, self.vocab_size)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.type_sequence_label_size
									lowerCamelCase					:			List[Any]									= BeitForImageClassification(A      )
									model.to(A      )
									model.eval()
									lowerCamelCase					:			Any									= model(A,    labels=A      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.type_sequence_label_size)      )

									# test greyscale images
									lowerCamelCase					:			Dict									= 1
									lowerCamelCase					:			List[Any]									= BeitForImageClassification(A      )
									model.to(A      )
									model.eval()

									lowerCamelCase					:			List[Any]									= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
									lowerCamelCase					:			str									= model(A,    labels=A      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.type_sequence_label_size)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= self.num_labels
									lowerCamelCase					:			int									= BeitForSemanticSegmentation(A      )
									model.to(A      )
									model.eval()
									lowerCamelCase					:			str									= model(A      )
									self.parent.assertEqual(
									    result.logits.shape,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
									lowerCamelCase					:			Dict									= model(A,    labels=A      )
									self.parent.assertEqual(
									    result.logits.shape,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )




					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.prepare_config_and_inputs()
									lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase					:			Union[str, Any]									= config_and_inputs
									lowerCamelCase					:			Optional[int]									= {'pixel_values': pixel_values}
									return config, inputs_dict





@require_torch
class     __snake_case		(   a__    ,   a__    ,   unittest.TestCase):
					_lowerCAmelCase			     =	(
					    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
					    if is_torch_available()
					    else ()
					)
					_lowerCAmelCase			     =	(
					    {
					        '''feature-extraction''': BeitModel,
					        '''image-classification''': BeitForImageClassification,
					        '''image-segmentation''': BeitForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					_lowerCAmelCase			     =	False
					_lowerCAmelCase			     =	False
					_lowerCAmelCase			     =	False
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= BeitModelTester(self      )
									lowerCamelCase					:			Dict									= ConfigTester(self,    config_class=A,    has_text_modality=A,    hidden_size=37      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									self.config_tester.run_common_tests()
					@unittest.skip(reason='BEiT does not use inputs_embeds'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass
					@require_torch_multi_gpu
					@unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase			, lowerCamelCase					:			Tuple									= self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													lowerCamelCase					:			Dict									= model_class(A      )
													self.assertIsInstance(model.get_input_embeddings(),    (nn.Module)      )
													lowerCamelCase					:			Any									= model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(A,    nn.Linear      )      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase			, lowerCamelCase					:			int									= self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													lowerCamelCase					:			List[Any]									= model_class(A      )
													lowerCamelCase					:			str									= inspect.signature(model.forward      )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													lowerCamelCase					:			Optional[Any]									= [*signature.parameters.keys()]

													lowerCamelCase					:			Dict									= ['pixel_values']
													self.assertListEqual(arg_names[:1],    A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			str									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			str									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									if not self.model_tester.is_training:
													return

									lowerCamelCase			, lowerCamelCase					:			str									= self.model_tester.prepare_config_and_inputs_for_common()
									lowerCamelCase					:			List[str]									= True

									for model_class in self.all_model_classes:
													# we don't test BeitForMaskedImageModeling
													if model_class in [*get_values(A      ), BeitForMaskedImageModeling]:
																	continue

													lowerCamelCase					:			Optional[int]									= model_class(A      )
													model.to(A      )
													model.train()
													lowerCamelCase					:			Tuple									= self._prepare_for_class(A,    A,    return_labels=A      )
													lowerCamelCase					:			int									= model(**A      ).loss
													loss.backward()
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase			, lowerCamelCase					:			Dict									= self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
													return

									lowerCamelCase					:			Dict									= False
									lowerCamelCase					:			List[str]									= True

									for model_class in self.all_model_classes:
													# we don't test BeitForMaskedImageModeling
													if (
													    model_class in [*get_values(A      ), BeitForMaskedImageModeling]
													    or not model_class.supports_gradient_checkpointing
													):
																	continue

													lowerCamelCase					:			Dict									= model_class(A      )
													model.gradient_checkpointing_enable()
													model.to(A      )
													model.train()
													lowerCamelCase					:			int									= self._prepare_for_class(A,    A,    return_labels=A      )
													lowerCamelCase					:			Optional[int]									= model(**A      ).loss
													loss.backward()
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase			, lowerCamelCase					:			Optional[Any]									= self.model_tester.prepare_config_and_inputs_for_common()

									lowerCamelCase					:			Optional[Any]									= _config_zero_init(A      )
									for model_class in self.all_model_classes:
													lowerCamelCase					:			List[Any]									= model_class(config=A      )
													for name, param in model.named_parameters():
																	# we skip lambda parameters as these require special initial values
																	# determined by config.layer_scale_init_value
																	if "lambda" in name:
																					continue
																	if param.requires_grad:
																					self.assertIn(
																					    ((param.data.mean() * 1e9).round() / 1e9).item(),    [0.0, 1.0],    msg=F'''Parameter {name} of model {model_class} seems not properly initialized''',    )




					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCamelCase					:			int									= BeitModel.from_pretrained(A      )
													self.assertIsNotNone(A      )





def    UpperCAmelCase							(  ):
				lowerCamelCase					:			Dict									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
				return image




@require_torch
@require_vision
class     __snake_case		(   unittest.TestCase):
					@cached_property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'      ) if is_vision_available() else None
					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'      ).to(A      )

									lowerCamelCase					:			Union[str, Any]									= self.default_image_processor
									lowerCamelCase					:			Dict									= prepare_img()
									lowerCamelCase					:			Tuple									= image_processor(images=A,    return_tensors='pt'      ).pixel_values.to(A      )

									# prepare bool_masked_pos
									lowerCamelCase					:			Dict									= torch.ones((1, 196),    dtype=torch.bool      ).to(A      )

									# forward pass
									with torch.no_grad():
													lowerCamelCase					:			str									= model(pixel_values=A,    bool_masked_pos=A      )
									lowerCamelCase					:			Optional[int]									= outputs.logits

									# verify the logits
									lowerCamelCase					:			Optional[int]									= torch.Size((1, 196, 8192)      )
									self.assertEqual(logits.shape,    A      )

									lowerCamelCase					:			Optional[int]									= torch.tensor(
									    [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]      ).to(A      )

									self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3],    A,    atol=1e-2      )      )
					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'      ).to(A      )

									lowerCamelCase					:			str									= self.default_image_processor
									lowerCamelCase					:			Dict									= prepare_img()
									lowerCamelCase					:			List[str]									= image_processor(images=A,    return_tensors='pt'      ).to(A      )

									# forward pass
									with torch.no_grad():
													lowerCamelCase					:			Any									= model(**A      )
									lowerCamelCase					:			Dict									= outputs.logits

									# verify the logits
									lowerCamelCase					:			Tuple									= torch.Size((1, 1000)      )
									self.assertEqual(logits.shape,    A      )

									lowerCamelCase					:			Tuple									= torch.tensor([-1.2385, -1.0987, -1.0108]      ).to(A      )

									self.assertTrue(torch.allclose(logits[0, :3],    A,    atol=1e-4      )      )

									lowerCamelCase					:			List[str]									= 281
									self.assertEqual(logits.argmax(-1      ).item(),    A      )
					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'      ).to(
									    A      )

									lowerCamelCase					:			List[Any]									= self.default_image_processor
									lowerCamelCase					:			Dict									= prepare_img()
									lowerCamelCase					:			str									= image_processor(images=A,    return_tensors='pt'      ).to(A      )

									# forward pass
									with torch.no_grad():
													lowerCamelCase					:			List[str]									= model(**A      )
									lowerCamelCase					:			Optional[Any]									= outputs.logits

									# verify the logits
									lowerCamelCase					:			int									= torch.Size((1, 2_1841)      )
									self.assertEqual(logits.shape,    A      )

									lowerCamelCase					:			List[Any]									= torch.tensor([1.6881, -0.2787, 0.5901]      ).to(A      )

									self.assertTrue(torch.allclose(logits[0, :3],    A,    atol=1e-4      )      )

									lowerCamelCase					:			Union[str, Any]									= 2396
									self.assertEqual(logits.argmax(-1      ).item(),    A      )
					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'      )
									lowerCamelCase					:			List[str]									= model.to(A      )

									lowerCamelCase					:			Dict									= BeitImageProcessor(do_resize=A,    size=640,    do_center_crop=A      )

									lowerCamelCase					:			Optional[Any]									= load_dataset('hf-internal-testing/fixtures_ade20k',    split='test'      )
									lowerCamelCase					:			Any									= Image.open(ds[0]['file']      )
									lowerCamelCase					:			Any									= image_processor(images=A,    return_tensors='pt'      ).to(A      )

									# forward pass
									with torch.no_grad():
													lowerCamelCase					:			Optional[int]									= model(**A      )
									lowerCamelCase					:			List[str]									= outputs.logits

									# verify the logits
									lowerCamelCase					:			int									= torch.Size((1, 150, 160, 160)      )
									self.assertEqual(logits.shape,    A      )

									lowerCamelCase					:			List[str]									= version.parse(PIL.__version__      ) < version.parse('9.0.0'      )

									if is_pillow_less_than_a:
													lowerCamelCase					:			Union[str, Any]									= torch.tensor(
													    [
													        [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]],
													        [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]],
													        [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]],
													    ],    device=A,    )
									else:
													lowerCamelCase					:			Dict									= torch.tensor(
													    [
													        [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]],
													        [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]],
													        [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]],
													    ],    device=A,    )

									self.assertTrue(torch.allclose(logits[0, :3, :3, :3],    A,    atol=1e-4      )      )




					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'      )
									lowerCamelCase					:			Dict									= model.to(A      )

									lowerCamelCase					:			List[str]									= BeitImageProcessor(do_resize=A,    size=640,    do_center_crop=A      )

									lowerCamelCase					:			List[Any]									= load_dataset('hf-internal-testing/fixtures_ade20k',    split='test'      )
									lowerCamelCase					:			str									= Image.open(ds[0]['file']      )
									lowerCamelCase					:			List[str]									= image_processor(images=A,    return_tensors='pt'      ).to(A      )

									# forward pass
									with torch.no_grad():
													lowerCamelCase					:			str									= model(**A      )

									lowerCamelCase					:			List[Any]									= outputs.logits.detach().cpu()

									lowerCamelCase					:			str									= image_processor.post_process_semantic_segmentation(outputs=A,    target_sizes=[(500, 300)]      )
									lowerCamelCase					:			Optional[int]									= torch.Size((500, 300)      )
									self.assertEqual(segmentation[0].shape,    A      )

									lowerCamelCase					:			Any									= image_processor.post_process_semantic_segmentation(outputs=A      )
									lowerCamelCase					:			Optional[int]									= torch.Size((160, 160)      )
									self.assertEqual(segmentation[0].shape,    A      )


'''simple docstring'''






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


A			      =      logging.get_logger(__name__)

A			      =      {
    '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     __snake_case		(   a__):
					_lowerCAmelCase			     =	'''segformer'''
					def __init__(     self,    A=3,    A=4,    A=[2, 2, 2, 2],    A=[8, 4, 2, 1],    A=[32, 64, 160, 256],    A=[7, 3, 3, 3],    A=[4, 2, 2, 2],    A=[1, 2, 5, 8],    A=[4, 4, 4, 4],    A="gelu",    A=0.0,    A=0.0,    A=0.1,    A=0.02,    A=0.1,    A=1e-6,    A=256,    A=255,    **A,    ):

									"""simple docstring"""


									super().__init__(**A      )

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

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





class     __snake_case		(   a__):
					_lowerCAmelCase			     =	version.parse('''1.11''')
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									"""simple docstring"""


									return 1e-4




					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return 12
