






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       __UpperCAmelCase		:







			def __init__(	self:  int      ,	UpperCAmelCase_:  Optional[Any]      ,	UpperCAmelCase_:  Any=100      ,	UpperCAmelCase_:  Dict=13      ,	UpperCAmelCase_:  Optional[int]=30      ,	UpperCAmelCase_:  Tuple=2      ,	UpperCAmelCase_:  List[Any]=3      ,	UpperCAmelCase_:  List[Any]=True      ,	UpperCAmelCase_:  Optional[Any]=True      ,	UpperCAmelCase_:  Dict=32      ,	UpperCAmelCase_:  Dict=4      ,	UpperCAmelCase_:  List[str]=4      ,	UpperCAmelCase_:  Optional[Any]=37      ,	UpperCAmelCase_:  List[str]="gelu"      ,	UpperCAmelCase_:  Dict=0.1      ,	UpperCAmelCase_:  Tuple=0.1      ,	UpperCAmelCase_:  Optional[int]=10      ,	UpperCAmelCase_:  Union[str, Any]=0.02      ,	UpperCAmelCase_:  List[str]=3      ,	UpperCAmelCase_:  Tuple=None      ,	UpperCAmelCase_:  Any=[0, 1, 2, 3]      ,	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  parent
									_SCREAMING_SNAKE_CASE	      =  100
									_SCREAMING_SNAKE_CASE	      =  batch_size
									_SCREAMING_SNAKE_CASE	      =  image_size
									_SCREAMING_SNAKE_CASE	      =  patch_size
									_SCREAMING_SNAKE_CASE	      =  num_channels
									_SCREAMING_SNAKE_CASE	      =  is_training
									_SCREAMING_SNAKE_CASE	      =  use_labels
									_SCREAMING_SNAKE_CASE	      =  hidden_size
									_SCREAMING_SNAKE_CASE	      =  num_hidden_layers
									_SCREAMING_SNAKE_CASE	      =  num_attention_heads
									_SCREAMING_SNAKE_CASE	      =  intermediate_size
									_SCREAMING_SNAKE_CASE	      =  hidden_act
									_SCREAMING_SNAKE_CASE	      =  hidden_dropout_prob
									_SCREAMING_SNAKE_CASE	      =  attention_probs_dropout_prob
									_SCREAMING_SNAKE_CASE	      =  type_sequence_label_size
									_SCREAMING_SNAKE_CASE	      =  initializer_range
									_SCREAMING_SNAKE_CASE	      =  scope
									_SCREAMING_SNAKE_CASE	      =  out_indices
									_SCREAMING_SNAKE_CASE	      =  num_labels

									# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									_SCREAMING_SNAKE_CASE	      =  (image_size // patch_size) ** 2
									_SCREAMING_SNAKE_CASE	      =  num_patches + 1







			def 							UpperCamelCase      (	self:  Optional[int]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

									_SCREAMING_SNAKE_CASE	      =  None
									_SCREAMING_SNAKE_CASE	      =  None
									if self.use_labels:
															_SCREAMING_SNAKE_CASE	      =  ids_tensor([self.batch_size]      ,	self.type_sequence_label_size     )
															_SCREAMING_SNAKE_CASE	      =  ids_tensor([self.batch_size, self.image_size, self.image_size]      ,	self.num_labels     )

									_SCREAMING_SNAKE_CASE	      =  self.get_config()

									return config, pixel_values, labels, pixel_labels







			def 							UpperCamelCase      (	self:  int     ):







									'''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=__lowerCAmelCase      ,	initializer_range=self.initializer_range      ,	out_indices=self.out_indices      ,	)







			def 							UpperCamelCase      (	self:  Optional[int]      ,	UpperCAmelCase_:  List[str]      ,	UpperCAmelCase_:  Optional[int]      ,	UpperCAmelCase_:  Union[str, Any]      ,	UpperCAmelCase_:  Dict     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitModel(config=__lowerCAmelCase     )
									model.to(__lowerCAmelCase     )
									model.eval()
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCAmelCase     )
									self.parent.assertEqual(result.last_hidden_state.shape      ,	(self.batch_size, self.seq_length, self.hidden_size)     )







			def 							UpperCamelCase      (	self:  int      ,	UpperCAmelCase_:  Optional[Any]      ,	UpperCAmelCase_:  List[str]      ,	UpperCAmelCase_:  List[Any]      ,	UpperCAmelCase_:  Tuple     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitForMaskedImageModeling(config=__lowerCAmelCase     )
									model.to(__lowerCAmelCase     )
									model.eval()
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCAmelCase     )
									self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.seq_length - 1, self.vocab_size)     )







			def 							UpperCamelCase      (	self:  str      ,	UpperCAmelCase_:  int      ,	UpperCAmelCase_:  List[Any]      ,	UpperCAmelCase_:  Optional[Any]      ,	UpperCAmelCase_:  List[str]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.type_sequence_label_size
									_SCREAMING_SNAKE_CASE	      =  BeitForImageClassification(__lowerCAmelCase     )
									model.to(__lowerCAmelCase     )
									model.eval()
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCAmelCase      ,	labels=__lowerCAmelCase     )
									self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.type_sequence_label_size)     )

									# test greyscale images
									_SCREAMING_SNAKE_CASE	      =  1
									_SCREAMING_SNAKE_CASE	      =  BeitForImageClassification(__lowerCAmelCase     )
									model.to(__lowerCAmelCase     )
									model.eval()

									_SCREAMING_SNAKE_CASE	      =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCAmelCase      ,	labels=__lowerCAmelCase     )
									self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.type_sequence_label_size)     )







			def 							UpperCamelCase      (	self:  Optional[Any]      ,	UpperCAmelCase_:  int      ,	UpperCAmelCase_:  Tuple      ,	UpperCAmelCase_:  Any      ,	UpperCAmelCase_:  Optional[Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.num_labels
									_SCREAMING_SNAKE_CASE	      =  BeitForSemanticSegmentation(__lowerCAmelCase     )
									model.to(__lowerCAmelCase     )
									model.eval()
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCAmelCase     )
									self.parent.assertEqual(
									    result.logits.shape      ,	(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCAmelCase      ,	labels=__lowerCAmelCase     )
									self.parent.assertEqual(
									    result.logits.shape      ,	(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )







			def 							UpperCamelCase      (	self:  Dict     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.prepare_config_and_inputs()
									_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE	      =  config_and_inputs
									_SCREAMING_SNAKE_CASE	      =  {"""pixel_values""": pixel_values}
									return config, inputs_dict



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

			__snake_case				:      Any 						=							False
			__snake_case				:      Dict 						=							False
			__snake_case				:      Optional[Any] 						=							False







			def 							UpperCamelCase      (	self:  Any     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitModelTester(self     )
									_SCREAMING_SNAKE_CASE	      =  ConfigTester(self      ,	config_class=__lowerCAmelCase      ,	has_text_modality=__lowerCAmelCase      ,	hidden_size=37     )







			def 							UpperCamelCase      (	self:  str     ):







									'''simple docstring'''




									self.config_tester.run_common_tests()







			@unittest.skip(reason="""BEiT does not use inputs_embeds"""     )
			def 							UpperCamelCase      (	self:  Tuple     ):







									'''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:  str     ):







									'''simple docstring'''




									pass







			def 							UpperCamelCase      (	self:  List[str]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE	      =  model_class(__lowerCAmelCase     )
															self.assertIsInstance(model.get_input_embeddings()      ,	(nn.Module)     )
															_SCREAMING_SNAKE_CASE	      =  model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(__lowerCAmelCase      ,	nn.Linear     )     )







			def 							UpperCamelCase      (	self:  Optional[Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE	      =  model_class(__lowerCAmelCase     )
															_SCREAMING_SNAKE_CASE	      =  inspect.signature(model.forward     )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															_SCREAMING_SNAKE_CASE	      =  [*signature.parameters.keys()]

															_SCREAMING_SNAKE_CASE	      =  ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]      ,	__lowerCAmelCase     )







			def 							UpperCamelCase      (	self:  List[str]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCAmelCase     )







			def 							UpperCamelCase      (	self:  str     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase     )







			def 							UpperCamelCase      (	self:  Union[str, Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase     )







			def 							UpperCamelCase      (	self:  Union[str, Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase     )







			def 							UpperCamelCase      (	self:  Dict     ):







									'''simple docstring'''




									if not self.model_tester.is_training:
															return

									_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs_for_common()
									_SCREAMING_SNAKE_CASE	      =  True

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

															_SCREAMING_SNAKE_CASE	      =  model_class(__lowerCAmelCase     )
															model.to(__lowerCAmelCase     )
															model.train()
															_SCREAMING_SNAKE_CASE	      =  self._prepare_for_class(__lowerCAmelCase      ,	__lowerCAmelCase      ,	return_labels=__lowerCAmelCase     )
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCAmelCase     ).loss
															loss.backward()







			def 							UpperCamelCase      (	self:  List[str]     ):







									'''simple docstring'''




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

									_SCREAMING_SNAKE_CASE	      =  False
									_SCREAMING_SNAKE_CASE	      =  True

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

															_SCREAMING_SNAKE_CASE	      =  model_class(__lowerCAmelCase     )
															model.gradient_checkpointing_enable()
															model.to(__lowerCAmelCase     )
															model.train()
															_SCREAMING_SNAKE_CASE	      =  self._prepare_for_class(__lowerCAmelCase      ,	__lowerCAmelCase      ,	return_labels=__lowerCAmelCase     )
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCAmelCase     ).loss
															loss.backward()







			def 							UpperCamelCase      (	self:  Dict     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs_for_common()

									_SCREAMING_SNAKE_CASE	      =  _config_zero_init(__lowerCAmelCase     )
									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE	      =  model_class(config=__lowerCAmelCase     )
															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:  Union[str, Any]     ):







									'''simple docstring'''




									for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE	      =  BeitModel.from_pretrained(__lowerCAmelCase     )
															self.assertIsNotNone(__lowerCAmelCase     )

def        __lowerCamelCase			(		)							->  List[Any]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE	      =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
						return image




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







			@cached_property
			def 							UpperCamelCase      (	self:  Union[str, Any]     ):







									'''simple docstring'''




									return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""     ) if is_vision_available() else None







			@slow
			def 							UpperCamelCase      (	self:  Union[str, Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""     ).to(__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  self.default_image_processor
									_SCREAMING_SNAKE_CASE	      =  prepare_img()
									_SCREAMING_SNAKE_CASE	      =  image_processor(images=__lowerCAmelCase      ,	return_tensors="""pt"""     ).pixel_values.to(__lowerCAmelCase     )

									# prepare bool_masked_pos
									_SCREAMING_SNAKE_CASE	      =  torch.ones((1, 196)      ,	dtype=torch.bool     ).to(__lowerCAmelCase     )

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE	      =  model(pixel_values=__lowerCAmelCase      ,	bool_masked_pos=__lowerCAmelCase     )
									_SCREAMING_SNAKE_CASE	      =  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE	      =  torch.Size((1, 196, 8_192)     )
									self.assertEqual(logits.shape      ,	__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  torch.tensor(
									    [[-3.24_37, 0.50_72, -13.9_174], [-3.24_56, 0.49_48, -13.9_401], [-3.20_33, 0.51_21, -13.8_550]]     ).to(__lowerCAmelCase     )

									self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]      ,	__lowerCAmelCase      ,	atol=1E-2     )     )







			@slow
			def 							UpperCamelCase      (	self:  Optional[Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""     ).to(__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  self.default_image_processor
									_SCREAMING_SNAKE_CASE	      =  prepare_img()
									_SCREAMING_SNAKE_CASE	      =  image_processor(images=__lowerCAmelCase      ,	return_tensors="""pt"""     ).to(__lowerCAmelCase     )

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCAmelCase     )
									_SCREAMING_SNAKE_CASE	      =  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE	      =  torch.Size((1, 1_000)     )
									self.assertEqual(logits.shape      ,	__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  torch.tensor([-1.23_85, -1.09_87, -1.01_08]     ).to(__lowerCAmelCase     )

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

									_SCREAMING_SNAKE_CASE	      =  281
									self.assertEqual(logits.argmax(-1     ).item()      ,	__lowerCAmelCase     )







			@slow
			def 							UpperCamelCase      (	self:  Tuple     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""     ).to(
									    __lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  self.default_image_processor
									_SCREAMING_SNAKE_CASE	      =  prepare_img()
									_SCREAMING_SNAKE_CASE	      =  image_processor(images=__lowerCAmelCase      ,	return_tensors="""pt"""     ).to(__lowerCAmelCase     )

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCAmelCase     )
									_SCREAMING_SNAKE_CASE	      =  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE	      =  torch.Size((1, 21_841)     )
									self.assertEqual(logits.shape      ,	__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  torch.tensor([1.68_81, -0.27_87, 0.59_01]     ).to(__lowerCAmelCase     )

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

									_SCREAMING_SNAKE_CASE	      =  2_396
									self.assertEqual(logits.argmax(-1     ).item()      ,	__lowerCAmelCase     )







			@slow
			def 							UpperCamelCase      (	self:  Tuple     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""     )
									_SCREAMING_SNAKE_CASE	      =  model.to(__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  BeitImageProcessor(do_resize=__lowerCAmelCase      ,	size=640      ,	do_center_crop=__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  load_dataset("""hf-internal-testing/fixtures_ade20k"""      ,	split="""test"""     )
									_SCREAMING_SNAKE_CASE	      =  Image.open(ds[0]["""file"""]     )
									_SCREAMING_SNAKE_CASE	      =  image_processor(images=__lowerCAmelCase      ,	return_tensors="""pt"""     ).to(__lowerCAmelCase     )

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCAmelCase     )
									_SCREAMING_SNAKE_CASE	      =  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE	      =  torch.Size((1, 150, 160, 160)     )
									self.assertEqual(logits.shape      ,	__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  version.parse(PIL.__version__     ) < version.parse("""9.0.0"""     )

									if is_pillow_less_than_a:
															_SCREAMING_SNAKE_CASE	      =  torch.tensor(
															    [
															        [[-4.92_25, -2.39_54, -3.05_22], [-2.88_22, -1.00_46, -1.75_61], [-2.95_49, -1.32_28, -2.13_47]],
															        [[-5.81_68, -3.41_29, -4.07_78], [-3.86_51, -2.22_14, -3.02_77], [-3.83_56, -2.46_43, -3.35_35]],
															        [[-0.00_78, 3.99_52, 4.07_54], [2.98_56, 4.69_44, 5.00_35], [3.24_13, 4.78_13, 4.99_69]],
															    ]      ,	device=__lowerCAmelCase      ,	)
									else:
															_SCREAMING_SNAKE_CASE	      =  torch.tensor(
															    [
															        [[-4.89_60, -2.36_88, -3.03_55], [-2.84_78, -0.98_36, -1.74_18], [-2.94_49, -1.33_32, -2.14_56]],
															        [[-5.80_81, -3.41_24, -4.10_06], [-3.85_61, -2.20_81, -3.03_23], [-3.83_65, -2.46_01, -3.36_69]],
															        [[-0.03_09, 3.98_68, 4.05_40], [2.96_40, 4.68_77, 4.99_76], [3.20_81, 4.76_90, 4.99_42]],
															    ]      ,	device=__lowerCAmelCase      ,	)

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







			@slow
			def 							UpperCamelCase      (	self:  List[str]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""     )
									_SCREAMING_SNAKE_CASE	      =  model.to(__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  BeitImageProcessor(do_resize=__lowerCAmelCase      ,	size=640      ,	do_center_crop=__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  load_dataset("""hf-internal-testing/fixtures_ade20k"""      ,	split="""test"""     )
									_SCREAMING_SNAKE_CASE	      =  Image.open(ds[0]["""file"""]     )
									_SCREAMING_SNAKE_CASE	      =  image_processor(images=__lowerCAmelCase      ,	return_tensors="""pt"""     ).to(__lowerCAmelCase     )

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  outputs.logits.detach().cpu()

									_SCREAMING_SNAKE_CASE	      =  image_processor.post_process_semantic_segmentation(outputs=__lowerCAmelCase      ,	target_sizes=[(500, 300)]     )
									_SCREAMING_SNAKE_CASE	      =  torch.Size((500, 300)     )
									self.assertEqual(segmentation[0].shape      ,	__lowerCAmelCase     )

									_SCREAMING_SNAKE_CASE	      =  image_processor.post_process_semantic_segmentation(outputs=__lowerCAmelCase     )
									_SCREAMING_SNAKE_CASE	      =  torch.Size((160, 160)     )
									self.assertEqual(segmentation[0].shape      ,	__lowerCAmelCase     )


"""simple docstring"""






from ..utils import DummyObject, requires_backends





class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   List[str]							  =	['onnx']

      def __init__(						self		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(self		,       ["""onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""onnx"""]						)



