
'''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				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=1_00				,							__SCREAMING_SNAKE_CASE=13				,							__SCREAMING_SNAKE_CASE=30				,							__SCREAMING_SNAKE_CASE=2				,							__SCREAMING_SNAKE_CASE=3				,							__SCREAMING_SNAKE_CASE=True				,							__SCREAMING_SNAKE_CASE=True				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=4				,							__SCREAMING_SNAKE_CASE=4				,							__SCREAMING_SNAKE_CASE=37				,							__SCREAMING_SNAKE_CASE="gelu"				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=10				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=3				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=[0, 1, 2, 3]				,							):


											"""simple docstring"""





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

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






					def 	_snake_case    (   self		):


											"""simple docstring"""





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

											lowercase_	:     List[str]													=       None
											lowercase_	:     int													=       None
											if self.use_labels:
																	lowercase_	:     str													=       ids_tensor([self.batch_size]				,							self.type_sequence_label_size		)
																	lowercase_	:     Dict													=       ids_tensor([self.batch_size, self.image_size, self.image_size]				,							self.num_labels		)

											lowercase_	:     int													=       self.get_config()

											return config, pixel_values, labels, pixel_labels






					def 	_snake_case    (   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=__SCREAMING_SNAKE_CASE				,							initializer_range=self.initializer_range				,							out_indices=self.out_indices				,							)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     List[str]													=       BeitModel(config=__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()
											lowercase_	:     Any													=       model(__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)		)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     str													=       BeitForMaskedImageModeling(config=__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()
											lowercase_	:     int													=       model(__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length - 1, self.vocab_size)		)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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

											# test greyscale images
											lowercase_	:     Optional[int]													=       1
											lowercase_	:     Optional[Any]													=       BeitForImageClassification(__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()

											lowercase_	:     Dict													=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
											lowercase_	:     Tuple													=       model(__SCREAMING_SNAKE_CASE				,							labels=__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)		)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       self.num_labels
											lowercase_	:     List[str]													=       BeitForSemanticSegmentation(__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()
											lowercase_	:     List[str]													=       model(__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(
											    result.logits.shape				,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
											lowercase_	:     int													=       model(__SCREAMING_SNAKE_CASE				,							labels=__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(
											    result.logits.shape				,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     List[str]													=       self.prepare_config_and_inputs()
											lowercase_						, lowercase_						, lowercase_						, lowercase_	:     Optional[Any]													=       config_and_inputs
											lowercase_	:     Dict													=       {'''pixel_values''': pixel_values}
											return config, inputs_dict







@require_torch
class  lowerCAmelCase__   (  lowerCamelCase_    ,						lowerCamelCase_    ,						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 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Any													=       BeitModelTester(self		)
											lowercase_	:     Dict													=       ConfigTester(self				,							config_class=__SCREAMING_SNAKE_CASE				,							has_text_modality=__SCREAMING_SNAKE_CASE				,							hidden_size=37		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.config_tester.run_common_tests()






					@unittest.skip(reason='''BEiT does not use inputs_embeds'''		)
					def 	_snake_case    (   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 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					def 	_snake_case    (   self		):


											"""simple docstring"""





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

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






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_						, lowercase_	:     str													=       self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	lowercase_	:     List[str]													=       model_class(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Tuple													=       inspect.signature(model.forward		)
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	lowercase_	:     int													=       [*signature.parameters.keys()]

																	lowercase_	:     List[Any]													=       ['''pixel_values''']
																	self.assertListEqual(arg_names[:1]				,							__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     List[Any]													=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     int													=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											if not self.model_tester.is_training:
																	return

											lowercase_						, lowercase_	:     Any													=       self.model_tester.prepare_config_and_inputs_for_common()
											lowercase_	:     List[Any]													=       True

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

																	lowercase_	:     int													=       model_class(__SCREAMING_SNAKE_CASE		)
																	model.to(__SCREAMING_SNAKE_CASE		)
																	model.train()
																	lowercase_	:     str													=       self._prepare_for_class(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							return_labels=__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Dict													=       model(**__SCREAMING_SNAKE_CASE		).loss
																	loss.backward()






					def 	_snake_case    (   self		):


											"""simple docstring"""





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

											lowercase_	:     Any													=       False
											lowercase_	:     List[str]													=       True

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

																	lowercase_	:     int													=       model_class(__SCREAMING_SNAKE_CASE		)
																	model.gradient_checkpointing_enable()
																	model.to(__SCREAMING_SNAKE_CASE		)
																	model.train()
																	lowercase_	:     Dict													=       self._prepare_for_class(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							return_labels=__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Tuple													=       model(**__SCREAMING_SNAKE_CASE		).loss
																	loss.backward()






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_						, lowercase_	:     Dict													=       self.model_tester.prepare_config_and_inputs_for_common()

											lowercase_	:     str													=       _config_zero_init(__SCREAMING_SNAKE_CASE		)
											for model_class in self.all_model_classes:
																	lowercase_	:     Optional[Any]													=       model_class(config=__SCREAMING_SNAKE_CASE		)
																	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 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_	:     Dict													=       BeitModel.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)







def 					snake_case_		(				):



						"""simple docstring"""





						lowercase_	:     Optional[Any]													=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
						return image






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






					@cached_property
					def 	_snake_case    (   self		):


											"""simple docstring"""





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






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





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

											lowercase_	:     int													=       self.default_image_processor
											lowercase_	:     Optional[int]													=       prepare_img()
											lowercase_	:     str													=       image_processor(images=__SCREAMING_SNAKE_CASE				,							return_tensors='''pt'''		).pixel_values.to(__SCREAMING_SNAKE_CASE		)

											# prepare bool_masked_pos
											lowercase_	:     Union[str, Any]													=       torch.ones((1, 1_96)				,							dtype=torch.bool		).to(__SCREAMING_SNAKE_CASE		)

											# forward pass
											with torch.no_grad():
																	lowercase_	:     str													=       model(pixel_values=__SCREAMING_SNAKE_CASE				,							bool_masked_pos=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Any													=       outputs.logits

											# verify the logits
											lowercase_	:     Optional[int]													=       torch.Size((1, 1_96, 81_92)		)
											self.assertEqual(logits.shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[str]													=       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(__SCREAMING_SNAKE_CASE		)

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






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[Any]													=       BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''		).to(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Dict													=       self.default_image_processor
											lowercase_	:     Union[str, Any]													=       prepare_img()
											lowercase_	:     Any													=       image_processor(images=__SCREAMING_SNAKE_CASE				,							return_tensors='''pt'''		).to(__SCREAMING_SNAKE_CASE		)

											# forward pass
											with torch.no_grad():
																	lowercase_	:     List[Any]													=       model(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Dict													=       outputs.logits

											# verify the logits
											lowercase_	:     Any													=       torch.Size((1, 10_00)		)
											self.assertEqual(logits.shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       torch.tensor([-1.2_385, -1.0_987, -1.0_108]		).to(__SCREAMING_SNAKE_CASE		)

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

											lowercase_	:     Any													=       2_81
											self.assertEqual(logits.argmax(-1		).item()				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Dict													=       BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''		).to(
											    __SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[Any]													=       self.default_image_processor
											lowercase_	:     Any													=       prepare_img()
											lowercase_	:     Any													=       image_processor(images=__SCREAMING_SNAKE_CASE				,							return_tensors='''pt'''		).to(__SCREAMING_SNAKE_CASE		)

											# forward pass
											with torch.no_grad():
																	lowercase_	:     List[str]													=       model(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Union[str, Any]													=       outputs.logits

											# verify the logits
											lowercase_	:     Union[str, Any]													=       torch.Size((1, 2_18_41)		)
											self.assertEqual(logits.shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[Any]													=       torch.tensor([1.6_881, -0.2_787, 0.5_901]		).to(__SCREAMING_SNAKE_CASE		)

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

											lowercase_	:     str													=       23_96
											self.assertEqual(logits.argmax(-1		).item()				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
											lowercase_	:     Any													=       model.to(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       BeitImageProcessor(do_resize=__SCREAMING_SNAKE_CASE				,							size=6_40				,							do_center_crop=__SCREAMING_SNAKE_CASE		)

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

											# forward pass
											with torch.no_grad():
																	lowercase_	:     Union[str, Any]													=       model(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       outputs.logits

											# verify the logits
											lowercase_	:     Any													=       torch.Size((1, 1_50, 1_60, 1_60)		)
											self.assertEqual(logits.shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     int													=       version.parse(PIL.__version__		) < version.parse('''9.0.0'''		)

											if is_pillow_less_than_a:
																	lowercase_	:     Dict													=       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=__SCREAMING_SNAKE_CASE				,							)
											else:
																	lowercase_	:     int													=       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=__SCREAMING_SNAKE_CASE				,							)

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






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
											lowercase_	:     str													=       model.to(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[str]													=       BeitImageProcessor(do_resize=__SCREAMING_SNAKE_CASE				,							size=6_40				,							do_center_crop=__SCREAMING_SNAKE_CASE		)

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

											# forward pass
											with torch.no_grad():
																	lowercase_	:     str													=       model(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[int]													=       outputs.logits.detach().cpu()

											lowercase_	:     Optional[int]													=       image_processor.post_process_semantic_segmentation(outputs=__SCREAMING_SNAKE_CASE				,							target_sizes=[(5_00, 3_00)]		)
											lowercase_	:     List[Any]													=       torch.Size((5_00, 3_00)		)
											self.assertEqual(segmentation[0].shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[str]													=       image_processor.post_process_semantic_segmentation(outputs=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Union[str, Any]													=       torch.Size((1_60, 1_60)		)
											self.assertEqual(segmentation[0].shape				,							__SCREAMING_SNAKE_CASE		)





'''simple docstring'''







import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
							import torch_xla.core.xla_model as xm
							import torch_xla.debug.metrics as met






class  lowerCAmelCase__   (  lowerCamelCase_     ):






					def __init__(   self				,							*__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											super().__init__(*__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       eval_examples
											lowercase_	:     Tuple													=       post_process_function






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE = None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE = None				,							__SCREAMING_SNAKE_CASE = "eval"				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       gen_kwargs.copy()
											lowercase_	:     List[str]													=       (
											    gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length'''		) is not None else self.args.generation_max_length
											)
											lowercase_	:     str													=       (
											    gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams'''		) is not None else self.args.generation_num_beams
											)
											lowercase_	:     Dict													=       gen_kwargs

											lowercase_	:     List[Any]													=       self.eval_dataset if eval_dataset is None else eval_dataset
											lowercase_	:     List[str]													=       self.get_eval_dataloader(__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       self.eval_examples if eval_examples is None else eval_examples

											# Temporarily disable metric computation, we will do it in the loop here.
											lowercase_	:     Union[str, Any]													=       self.compute_metrics
											lowercase_	:     Optional[int]													=       None
											lowercase_	:     Tuple													=       time.time()
											lowercase_	:     Tuple													=       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																	lowercase_	:     str													=       eval_loop(
																	    __SCREAMING_SNAKE_CASE				,							description='''Evaluation'''				,							prediction_loss_only=True if compute_metrics is None else None				,							ignore_keys=__SCREAMING_SNAKE_CASE				,							metric_key_prefix=__SCREAMING_SNAKE_CASE				,							)
											finally:
																	lowercase_	:     Any													=       compute_metrics
											lowercase_	:     Any													=       self.args.eval_batch_size * self.args.world_size
											if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
																	start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
											output.metrics.update(
											    speed_metrics(
											        __SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							num_samples=output.num_samples				,							num_steps=math.ceil(output.num_samples / total_batch_size		)				,							)		)

											if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
																	# Only the main node write the results by default
																	lowercase_	:     Optional[Any]													=       self.post_process_function(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Optional[int]													=       self.compute_metrics(__SCREAMING_SNAKE_CASE		)

																	# Prefix all keys with metric_key_prefix + '_'
																	for key in list(metrics.keys()		):
																							if not key.startswith(F'''{metric_key_prefix}_'''		):
																													lowercase_	:     List[Any]													=       metrics.pop(__SCREAMING_SNAKE_CASE		)

																	metrics.update(output.metrics		)
											else:
																	lowercase_	:     List[Any]													=       output.metrics

											if self.args.should_log:
																	# Only the main node log the results by default
																	self.log(__SCREAMING_SNAKE_CASE		)

											if self.args.tpu_metrics_debug or self.args.debug:
																	# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
																	xm.master_print(met.metrics_report()		)

											lowercase_	:     List[Any]													=       self.callback_handler.on_evaluate(self.args				,							self.state				,							self.control				,							__SCREAMING_SNAKE_CASE		)
											return metrics






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE = "test"				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       gen_kwargs.copy()

											lowercase_	:     Tuple													=       self.get_test_dataloader(__SCREAMING_SNAKE_CASE		)

											# Temporarily disable metric computation, we will do it in the loop here.
											lowercase_	:     Optional[Any]													=       self.compute_metrics
											lowercase_	:     Optional[int]													=       None
											lowercase_	:     List[Any]													=       time.time()
											lowercase_	:     int													=       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																	lowercase_	:     Tuple													=       eval_loop(
																	    __SCREAMING_SNAKE_CASE				,							description='''Prediction'''				,							prediction_loss_only=True if compute_metrics is None else None				,							ignore_keys=__SCREAMING_SNAKE_CASE				,							metric_key_prefix=__SCREAMING_SNAKE_CASE				,							)
											finally:
																	lowercase_	:     Any													=       compute_metrics

											lowercase_	:     Tuple													=       self.args.eval_batch_size * self.args.world_size
											if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
																	start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
											output.metrics.update(
											    speed_metrics(
											        __SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							num_samples=output.num_samples				,							num_steps=math.ceil(output.num_samples / total_batch_size		)				,							)		)
											if self.post_process_function is None or self.compute_metrics is None:
																	return output

											lowercase_	:     Any													=       self.post_process_function(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							'''predict'''		)
											lowercase_	:     str													=       self.compute_metrics(__SCREAMING_SNAKE_CASE		)

											# Prefix all keys with metric_key_prefix + '_'
											for key in list(metrics.keys()		):
																	if not key.startswith(F'''{metric_key_prefix}_'''		):
																							lowercase_	:     Optional[int]													=       metrics.pop(__SCREAMING_SNAKE_CASE		)
											metrics.update(output.metrics		)
											return PredictionOutput(predictions=predictions.predictions				,							label_ids=predictions.label_ids				,							metrics=__SCREAMING_SNAKE_CASE		)



