

"""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    lowerCamelCase						:

			def __init__(					self				:						Tuple    ,				__UpperCAmelCase				:						str    ,				__UpperCAmelCase				:						int=1_0_0    ,				__UpperCAmelCase				:						List[Any]=1_3    ,				__UpperCAmelCase				:						int=3_0    ,				__UpperCAmelCase				:						Dict=2    ,				__UpperCAmelCase				:						List[Any]=3    ,				__UpperCAmelCase				:						Tuple=True    ,				__UpperCAmelCase				:						str=True    ,				__UpperCAmelCase				:						str=3_2    ,				__UpperCAmelCase				:						str=4    ,				__UpperCAmelCase				:						Any=4    ,				__UpperCAmelCase				:						List[Any]=3_7    ,				__UpperCAmelCase				:						Optional[Any]="gelu"    ,				__UpperCAmelCase				:						Any=0.1    ,				__UpperCAmelCase				:						int=0.1    ,				__UpperCAmelCase				:						str=1_0    ,				__UpperCAmelCase				:						int=0.02    ,				__UpperCAmelCase				:						int=3    ,				__UpperCAmelCase				:						Any=None    ,				__UpperCAmelCase				:						Union[str, Any]=[0, 1, 2, 3]    ,				)   ->    Dict:
						SCREAMING_SNAKE_CASE__								=      parent
						SCREAMING_SNAKE_CASE__								=      1_0_0
						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 					SCREAMING_SNAKE_CASE				(					self				:						Tuple       )   ->    int:
						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 					SCREAMING_SNAKE_CASE				(					self				:						str       )   ->    int:
						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=__UpperCAmelCase    ,				initializer_range=self.initializer_range    ,				out_indices=self.out_indices    ,				)

			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						Dict    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						Any       )   ->    Optional[int]:
						SCREAMING_SNAKE_CASE__								=      BeitModel(config=__UpperCAmelCase       )
						model.to(__UpperCAmelCase       )
						model.eval()
						SCREAMING_SNAKE_CASE__								=      model(__UpperCAmelCase       )
						self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size)       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[int]    ,				__UpperCAmelCase				:						List[Any]    ,				__UpperCAmelCase				:						Optional[Any]    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						List[str]       )   ->    List[str]:
						SCREAMING_SNAKE_CASE__								=      BeitForMaskedImageModeling(config=__UpperCAmelCase       )
						model.to(__UpperCAmelCase       )
						model.eval()
						SCREAMING_SNAKE_CASE__								=      model(__UpperCAmelCase       )
						self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length - 1, self.vocab_size)       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						Dict    ,				__UpperCAmelCase				:						Tuple    ,				__UpperCAmelCase				:						Optional[int]    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						List[str]       )   ->    List[Any]:
						SCREAMING_SNAKE_CASE__								=      self.type_sequence_label_size
						SCREAMING_SNAKE_CASE__								=      BeitForImageClassification(__UpperCAmelCase       )
						model.to(__UpperCAmelCase       )
						model.eval()
						SCREAMING_SNAKE_CASE__								=      model(__UpperCAmelCase    ,				labels=__UpperCAmelCase       )
						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(__UpperCAmelCase       )
						model.to(__UpperCAmelCase       )
						model.eval()

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

			def 					SCREAMING_SNAKE_CASE				(					self				:						str    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						Tuple    ,				__UpperCAmelCase				:						List[str]       )   ->    str:
						SCREAMING_SNAKE_CASE__								=      self.num_labels
						SCREAMING_SNAKE_CASE__								=      BeitForSemanticSegmentation(__UpperCAmelCase       )
						model.to(__UpperCAmelCase       )
						model.eval()
						SCREAMING_SNAKE_CASE__								=      model(__UpperCAmelCase       )
						self.parent.assertEqual(
						    result.logits.shape    ,				(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)       )
						SCREAMING_SNAKE_CASE__								=      model(__UpperCAmelCase    ,				labels=__UpperCAmelCase       )
						self.parent.assertEqual(
						    result.logits.shape    ,				(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)       )





			def 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]       )   ->    Union[str, Any]:
						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    lowerCamelCase						(A__			,A__			,unittest.TestCase			):
			lowerCamelCase__   :		str							=							(
			    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__   :		Optional[Any]							=							(
			    {
			        'feature-extraction': BeitModel,
			        'image-classification': BeitForImageClassification,
			        'image-segmentation': BeitForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			lowerCamelCase__   :		Optional[Any]							=							False
			lowerCamelCase__   :		str							=							False
			lowerCamelCase__   :		Any							=							False

			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]       )   ->    Any:
						SCREAMING_SNAKE_CASE__								=      BeitModelTester(self       )
						SCREAMING_SNAKE_CASE__								=      ConfigTester(self    ,				config_class=__UpperCAmelCase    ,				has_text_modality=__UpperCAmelCase    ,				hidden_size=3_7       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						Any       )   ->    int:
						self.config_tester.run_common_tests()

			@unittest.skip(reason="""BEiT does not use inputs_embeds"""       )
			def 					SCREAMING_SNAKE_CASE				(					self				:						List[str]       )   ->    Optional[int]:
						pass

			@require_torch_multi_gpu
			@unittest.skip(reason="""BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""       )
			def 					SCREAMING_SNAKE_CASE				(					self				:						str       )   ->    Tuple:
						pass

			def 					SCREAMING_SNAKE_CASE				(					self				:						List[str]       )   ->    List[Any]:
						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(__UpperCAmelCase       )
									self.assertIsInstance(model.get_input_embeddings()    ,				(nn.Module)       )
									SCREAMING_SNAKE_CASE__								=      model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(__UpperCAmelCase    ,				nn.Linear       )       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[int]       )   ->    Dict:
						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(__UpperCAmelCase       )
									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]    ,				__UpperCAmelCase       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple       )   ->    Any:
						SCREAMING_SNAKE_CASE__								=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCAmelCase       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]       )   ->    int:
						SCREAMING_SNAKE_CASE__								=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]       )   ->    Optional[int]:
						SCREAMING_SNAKE_CASE__								=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[int]       )   ->    Any:
						SCREAMING_SNAKE_CASE__								=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase       )

			def 					SCREAMING_SNAKE_CASE				(					self				:						List[str]       )   ->    int:
						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(__UpperCAmelCase       ), BeitForMaskedImageModeling]:
												continue

									SCREAMING_SNAKE_CASE__								=      model_class(__UpperCAmelCase       )
									model.to(__UpperCAmelCase       )
									model.train()
									SCREAMING_SNAKE_CASE__								=      self._prepare_for_class(__UpperCAmelCase    ,				__UpperCAmelCase    ,				return_labels=__UpperCAmelCase       )
									SCREAMING_SNAKE_CASE__								=      model(**__UpperCAmelCase       ).loss
									loss.backward()

			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]       )   ->    Optional[Any]:
						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(__UpperCAmelCase       ), BeitForMaskedImageModeling]
									    or not model_class.supports_gradient_checkpointing
									):
												continue

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

			def 					SCREAMING_SNAKE_CASE				(					self				:						Union[str, Any]       )   ->    Optional[Any]:
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      self.model_tester.prepare_config_and_inputs_for_common()

						SCREAMING_SNAKE_CASE__								=      _config_zero_init(__UpperCAmelCase       )
						for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE__								=      model_class(config=__UpperCAmelCase       )
									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 					SCREAMING_SNAKE_CASE				(					self				:						Dict       )   ->    List[Any]:
						for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE__								=      BeitModel.from_pretrained(__UpperCAmelCase       )
									self.assertIsNotNone(__UpperCAmelCase       )

def 	A       (     ):




			'''simple docstring'''


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





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

			@cached_property
			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]       )   ->    Dict:
						return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""       ) if is_vision_available() else None

			@slow
			def 					SCREAMING_SNAKE_CASE				(					self				:						Union[str, Any]       )   ->    Tuple:
						SCREAMING_SNAKE_CASE__								=      BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""       ).to(__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      self.default_image_processor
						SCREAMING_SNAKE_CASE__								=      prepare_img()
						SCREAMING_SNAKE_CASE__								=      image_processor(images=__UpperCAmelCase    ,				return_tensors="""pt"""       ).pixel_values.to(__UpperCAmelCase       )

						# prepare bool_masked_pos
						SCREAMING_SNAKE_CASE__								=      torch.ones((1, 1_9_6)    ,				dtype=torch.bool       ).to(__UpperCAmelCase       )

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

						# verify the logits
						SCREAMING_SNAKE_CASE__								=      torch.Size((1, 1_9_6, 8_1_9_2)       )
						self.assertEqual(logits.shape    ,				__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      torch.tensor(
						    [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]]       ).to(__UpperCAmelCase       )

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

			@slow
			def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[int]       )   ->    Tuple:
						SCREAMING_SNAKE_CASE__								=      BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""       ).to(__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      self.default_image_processor
						SCREAMING_SNAKE_CASE__								=      prepare_img()
						SCREAMING_SNAKE_CASE__								=      image_processor(images=__UpperCAmelCase    ,				return_tensors="""pt"""       ).to(__UpperCAmelCase       )

						# forward pass
						with torch.no_grad():
									SCREAMING_SNAKE_CASE__								=      model(**__UpperCAmelCase       )
						SCREAMING_SNAKE_CASE__								=      outputs.logits

						# verify the logits
						SCREAMING_SNAKE_CASE__								=      torch.Size((1, 1_0_0_0)       )
						self.assertEqual(logits.shape    ,				__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      torch.tensor([-1.2_385, -1.0_987, -1.0_108]       ).to(__UpperCAmelCase       )

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

						SCREAMING_SNAKE_CASE__								=      2_8_1
						self.assertEqual(logits.argmax(-1       ).item()    ,				__UpperCAmelCase       )

			@slow
			def 					SCREAMING_SNAKE_CASE				(					self				:						int       )   ->    Optional[Any]:
						SCREAMING_SNAKE_CASE__								=      BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""       ).to(
						    __UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      self.default_image_processor
						SCREAMING_SNAKE_CASE__								=      prepare_img()
						SCREAMING_SNAKE_CASE__								=      image_processor(images=__UpperCAmelCase    ,				return_tensors="""pt"""       ).to(__UpperCAmelCase       )

						# forward pass
						with torch.no_grad():
									SCREAMING_SNAKE_CASE__								=      model(**__UpperCAmelCase       )
						SCREAMING_SNAKE_CASE__								=      outputs.logits

						# verify the logits
						SCREAMING_SNAKE_CASE__								=      torch.Size((1, 2_1_8_4_1)       )
						self.assertEqual(logits.shape    ,				__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      torch.tensor([1.6_881, -0.2_787, 0.5_901]       ).to(__UpperCAmelCase       )

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

						SCREAMING_SNAKE_CASE__								=      2_3_9_6
						self.assertEqual(logits.argmax(-1       ).item()    ,				__UpperCAmelCase       )

			@slow
			def 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]       )   ->    int:
						SCREAMING_SNAKE_CASE__								=      BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""       )
						SCREAMING_SNAKE_CASE__								=      model.to(__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      BeitImageProcessor(do_resize=__UpperCAmelCase    ,				size=6_4_0    ,				do_center_crop=__UpperCAmelCase       )

						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=__UpperCAmelCase    ,				return_tensors="""pt"""       ).to(__UpperCAmelCase       )

						# forward pass
						with torch.no_grad():
									SCREAMING_SNAKE_CASE__								=      model(**__UpperCAmelCase       )
						SCREAMING_SNAKE_CASE__								=      outputs.logits

						# verify the logits
						SCREAMING_SNAKE_CASE__								=      torch.Size((1, 1_5_0, 1_6_0, 1_6_0)       )
						self.assertEqual(logits.shape    ,				__UpperCAmelCase       )

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

						if is_pillow_less_than_a:
									SCREAMING_SNAKE_CASE__								=      torch.tensor(
									    [
									        [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]],
									        [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]],
									        [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]],
									    ]    ,				device=__UpperCAmelCase    ,				)
						else:
									SCREAMING_SNAKE_CASE__								=      torch.tensor(
									    [
									        [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]],
									        [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]],
									        [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]],
									    ]    ,				device=__UpperCAmelCase    ,				)

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





			@slow
			def 					SCREAMING_SNAKE_CASE				(					self				:						Union[str, Any]       )   ->    Dict:
						SCREAMING_SNAKE_CASE__								=      BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""       )
						SCREAMING_SNAKE_CASE__								=      model.to(__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      BeitImageProcessor(do_resize=__UpperCAmelCase    ,				size=6_4_0    ,				do_center_crop=__UpperCAmelCase       )

						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=__UpperCAmelCase    ,				return_tensors="""pt"""       ).to(__UpperCAmelCase       )

						# forward pass
						with torch.no_grad():
									SCREAMING_SNAKE_CASE__								=      model(**__UpperCAmelCase       )

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

						SCREAMING_SNAKE_CASE__								=      image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase    ,				target_sizes=[(5_0_0, 3_0_0)]       )
						SCREAMING_SNAKE_CASE__								=      torch.Size((5_0_0, 3_0_0)       )
						self.assertEqual(segmentation[0].shape    ,				__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase       )
						SCREAMING_SNAKE_CASE__								=      torch.Size((1_6_0, 1_6_0)       )
						self.assertEqual(segmentation[0].shape    ,				__UpperCAmelCase       )





"""simple docstring"""




def 	A       (     snake_case__    ):




			'''simple docstring'''


			if not nums:  # Makes sure that the list is not empty
						raise ValueError("""List is empty"""    )

			SCREAMING_SNAKE_CASE__								=      sum(snake_case__    ) / len(snake_case__    )  # Calculate the average
			return sum(abs(x - average    ) for x in nums    ) / len(snake_case__    )


if __name__ == "__main__":
						import doctest

						doctest.testmod()


