



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





		"""simple docstring"""






		def __init__(       self				,     _lowercase				,     _lowercase=1_0_0				,     _lowercase=1_3				,     _lowercase=3_0				,     _lowercase=2				,     _lowercase=3				,     _lowercase=True				,     _lowercase=True				,     _lowercase=3_2				,     _lowercase=4				,     _lowercase=4				,     _lowercase=3_7				,     _lowercase="gelu"				,     _lowercase=0.1				,     _lowercase=0.1				,     _lowercase=1_0				,     _lowercase=0.02				,     _lowercase=3				,     _lowercase=None				,     _lowercase=[0, 1, 2, 3]				,     )			->    Tuple:







								'''simple docstring'''

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

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





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								snake_case_ :			int            =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

								snake_case_ :			int            =      self.get_config()

								return config, pixel_values, labels, pixel_labels





		def  UpperCAmelCase__				(       self )			->    Union[str, Any]:







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





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Tuple:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Optional[Any]:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Any            =      self.type_sequence_label_size
								snake_case_ :			str            =      BeitForImageClassification(__UpperCAmelCase )
								model.to(__UpperCAmelCase )
								model.eval()
								snake_case_ :			Dict            =      model(__UpperCAmelCase				,     labels=__UpperCAmelCase )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size) )

								# test greyscale images
								snake_case_ :			Optional[int]            =      1
								snake_case_ :			List[Any]            =      BeitForImageClassification(__UpperCAmelCase )
								model.to(__UpperCAmelCase )
								model.eval()

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





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Any            =      self.num_labels
								snake_case_ :			Optional[int]            =      BeitForSemanticSegmentation(__UpperCAmelCase )
								model.to(__UpperCAmelCase )
								model.eval()
								snake_case_ :			Optional[Any]            =      model(__UpperCAmelCase )
								self.parent.assertEqual(
								    result.logits.shape				,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) )
								snake_case_ :			int            =      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  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Dict            =      self.prepare_config_and_inputs()
								snake_case_ :			Dict            =      config_and_inputs
								snake_case_ :			List[Any]            =      {"pixel_values": pixel_values}
								return config, inputs_dict




@require_torch
class  _lowerCAmelCase     (					__UpperCamelCase	,					__UpperCamelCase	,					unittest.TestCase		):





		"""simple docstring"""






		_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 )			->    Optional[Any]:







								'''simple docstring'''

								snake_case_ :			Tuple            =      BeitModelTester(self )
								snake_case_ :			int            =      ConfigTester(self				,     config_class=__UpperCAmelCase				,     has_text_modality=__UpperCAmelCase				,     hidden_size=3_7 )





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								self.config_tester.run_common_tests()





		@unittest.skip(reason="""BEiT does not use inputs_embeds""" )
		def  UpperCAmelCase__				(       self )			->    List[Any]:







								'''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 )			->    Optional[int]:







								'''simple docstring'''

								pass





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Dict            =      self.model_tester.prepare_config_and_inputs_for_common()

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





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			int            =      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														snake_case_ :			int            =      model_class(__UpperCAmelCase )
														snake_case_ :			Optional[Any]            =      inspect.signature(model.forward )
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														snake_case_ :			Union[str, Any]            =      [*signature.parameters.keys()]

														snake_case_ :			Dict            =      ["pixel_values"]
														self.assertListEqual(arg_names[:1]				,     __UpperCAmelCase )





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			int            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__UpperCAmelCase )





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								snake_case_ :			List[str]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase )





		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			int            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase )





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								if not self.model_tester.is_training:
														return

								snake_case_ :			Optional[Any]            =      self.model_tester.prepare_config_and_inputs_for_common()
								snake_case_ :			Dict            =      True

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

														snake_case_ :			Union[str, Any]            =      model_class(__UpperCAmelCase )
														model.to(__UpperCAmelCase )
														model.train()
														snake_case_ :			Union[str, Any]            =      self._prepare_for_class(__UpperCAmelCase				,     __UpperCAmelCase				,     return_labels=__UpperCAmelCase )
														snake_case_ :			int            =      model(**__UpperCAmelCase ).loss
														loss.backward()





		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			List[Any]            =      self.model_tester.prepare_config_and_inputs_for_common()
								if not self.model_tester.is_training:
														return

								snake_case_ :			List[str]            =      False
								snake_case_ :			Dict            =      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

														snake_case_ :			Optional[Any]            =      model_class(__UpperCAmelCase )
														model.gradient_checkpointing_enable()
														model.to(__UpperCAmelCase )
														model.train()
														snake_case_ :			Optional[Any]            =      self._prepare_for_class(__UpperCAmelCase				,     __UpperCAmelCase				,     return_labels=__UpperCAmelCase )
														snake_case_ :			List[str]            =      model(**__UpperCAmelCase ).loss
														loss.backward()





		def  UpperCAmelCase__				(       self )			->    Union[str, Any]:







								'''simple docstring'''

								snake_case_ :			str            =      self.model_tester.prepare_config_and_inputs_for_common()

								snake_case_ :			Union[str, Any]            =      _config_zero_init(__UpperCAmelCase )
								for model_class in self.all_model_classes:
														snake_case_ :			List[str]            =      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  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_ :			Optional[Any]            =      BeitModel.from_pretrained(__UpperCAmelCase )
														self.assertIsNotNone(__UpperCAmelCase )




def 						__lowerCAmelCase    (		):


						'''simple docstring'''






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






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





		"""simple docstring"""






		@cached_property
		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

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





		@slow
		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			Any            =      BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k""" ).to(__UpperCAmelCase )

								snake_case_ :			Any            =      self.default_image_processor
								snake_case_ :			str            =      prepare_img()
								snake_case_ :			Any            =      image_processor(images=__UpperCAmelCase				,     return_tensors="""pt""" ).pixel_values.to(__UpperCAmelCase )

								# prepare bool_masked_pos
								snake_case_ :			Optional[int]            =      torch.ones((1, 1_9_6)				,     dtype=torch.bool ).to(__UpperCAmelCase )

								# forward pass
								with torch.no_grad():
														snake_case_ :			Optional[Any]            =      model(pixel_values=__UpperCAmelCase				,     bool_masked_pos=__UpperCAmelCase )
								snake_case_ :			Any            =      outputs.logits

								# verify the logits
								snake_case_ :			List[Any]            =      torch.Size((1, 1_9_6, 8_1_9_2) )
								self.assertEqual(logits.shape				,     __UpperCAmelCase )

								snake_case_ :			Optional[Any]            =      torch.tensor(
								    [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(__UpperCAmelCase )

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





		@slow
		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			List[Any]            =      BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224""" ).to(__UpperCAmelCase )

								snake_case_ :			Union[str, Any]            =      self.default_image_processor
								snake_case_ :			Optional[int]            =      prepare_img()
								snake_case_ :			Dict            =      image_processor(images=__UpperCAmelCase				,     return_tensors="""pt""" ).to(__UpperCAmelCase )

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

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

								snake_case_ :			List[str]            =      torch.tensor([-1.2385, -1.0987, -1.0108] ).to(__UpperCAmelCase )

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

								snake_case_ :			List[str]            =      2_8_1
								self.assertEqual(logits.argmax(-1 ).item()				,     __UpperCAmelCase )





		@slow
		def  UpperCAmelCase__				(       self )			->    Optional[Any]:







								'''simple docstring'''

								snake_case_ :			List[Any]            =      BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k""" ).to(
								    __UpperCAmelCase )

								snake_case_ :			Optional[int]            =      self.default_image_processor
								snake_case_ :			List[str]            =      prepare_img()
								snake_case_ :			Union[str, Any]            =      image_processor(images=__UpperCAmelCase				,     return_tensors="""pt""" ).to(__UpperCAmelCase )

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

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

								snake_case_ :			List[str]            =      torch.tensor([1.6881, -0.2787, 0.5901] ).to(__UpperCAmelCase )

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

								snake_case_ :			Optional[int]            =      2_3_9_6
								self.assertEqual(logits.argmax(-1 ).item()				,     __UpperCAmelCase )





		@slow
		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Any            =      BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640""" )
								snake_case_ :			Tuple            =      model.to(__UpperCAmelCase )

								snake_case_ :			Any            =      BeitImageProcessor(do_resize=__UpperCAmelCase				,     size=6_4_0				,     do_center_crop=__UpperCAmelCase )

								snake_case_ :			Dict            =      load_dataset("""hf-internal-testing/fixtures_ade20k"""				,     split="""test""" )
								snake_case_ :			List[Any]            =      Image.open(ds[0]["""file"""] )
								snake_case_ :			List[Any]            =      image_processor(images=__UpperCAmelCase				,     return_tensors="""pt""" ).to(__UpperCAmelCase )

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

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

								snake_case_ :			Optional[Any]            =      version.parse(PIL.__version__ ) < version.parse("""9.0.0""" )

								if is_pillow_less_than_a:
														snake_case_ :			Tuple            =      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=__UpperCAmelCase				,     )
								else:
														snake_case_ :			Union[str, Any]            =      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=__UpperCAmelCase				,     )

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





		@slow
		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Any            =      BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640""" )
								snake_case_ :			str            =      model.to(__UpperCAmelCase )

								snake_case_ :			Union[str, Any]            =      BeitImageProcessor(do_resize=__UpperCAmelCase				,     size=6_4_0				,     do_center_crop=__UpperCAmelCase )

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

								# forward pass
								with torch.no_grad():
														snake_case_ :			Optional[int]            =      model(**__UpperCAmelCase )

								snake_case_ :			int            =      outputs.logits.detach().cpu()

								snake_case_ :			str            =      image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase				,     target_sizes=[(5_0_0, 3_0_0)] )
								snake_case_ :			Optional[int]            =      torch.Size((5_0_0, 3_0_0) )
								self.assertEqual(segmentation[0].shape				,     __UpperCAmelCase )

								snake_case_ :			List[str]            =      image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase )
								snake_case_ :			Tuple            =      torch.Size((1_6_0, 1_6_0) )
								self.assertEqual(segmentation[0].shape				,     __UpperCAmelCase )









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 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						def __init__(self:					str ,					*__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					Optional[int]=None ,					__UpperCAmelCase:					Dict=None ,					**__UpperCAmelCase:					Tuple   )    ->     Dict:




											'''simple docstring'''



											super().__init__(*__UpperCAmelCase ,					**__UpperCAmelCase   )
											__a					:						str      =					eval_examples
											__a					:						List[Any]      =					post_process_function



						def        UpperCAmelCase__					(self:					Optional[int] ,					__UpperCAmelCase:					Optional[Dataset] = None ,					__UpperCAmelCase:					Optional[int]=None ,					__UpperCAmelCase:					Optional[List[str]] = None ,					__UpperCAmelCase:					str = "eval" ,					**__UpperCAmelCase:					Any ,					)    ->     Dict[str, float]:




											'''simple docstring'''



											__a					:						Optional[int]      =					gen_kwargs.copy()
											__a					:						Union[str, Any]      =					(
											    gen_kwargs["max_length"] if gen_kwargs.get("max_length"   ) is not None else self.args.generation_max_length
											)
											__a					:						Dict      =					(
											    gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"   ) is not None else self.args.generation_num_beams
											)
											__a					:						Optional[Any]      =					gen_kwargs

											__a					:						Union[str, Any]      =					self.eval_dataset if eval_dataset is None else eval_dataset
											__a					:						Optional[int]      =					self.get_eval_dataloader(__UpperCAmelCase   )
											__a					:						Tuple      =					self.eval_examples if eval_examples is None else eval_examples

											# Temporarily disable metric computation, we will do it in the loop here.
											__a					:						int      =					self.compute_metrics
											__a					:						Dict      =					None
											__a					:						List[Any]      =					time.time()
											__a					:						str      =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																__a					:						str      =					eval_loop(
																    __UpperCAmelCase ,					description="Evaluation" ,					prediction_loss_only=True if compute_metrics is None else None ,					ignore_keys=__UpperCAmelCase ,					metric_key_prefix=__UpperCAmelCase ,					)
											finally:
																__a					:						Union[str, Any]      =					compute_metrics
											__a					:						str      =					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(
											        __UpperCAmelCase ,					__UpperCAmelCase ,					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
																__a					:						Union[str, Any]      =					self.post_process_function(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase   )
																__a					:						Dict      =					self.compute_metrics(__UpperCAmelCase   )

																# Prefix all keys with metric_key_prefix + '_'
																for key in list(metrics.keys()   ):
																					if not key.startswith(f'{metric_key_prefix}_'   ):
																										__a					:						int      =					metrics.pop(__UpperCAmelCase   )

																metrics.update(output.metrics   )
											else:
																__a					:						Optional[int]      =					output.metrics

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

											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()   )

											__a					:						Tuple      =					self.callback_handler.on_evaluate(self.args ,					self.state ,					self.control ,					__UpperCAmelCase   )
											return metrics



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					Union[str, Any]=None ,					__UpperCAmelCase:					str = "test" ,					**__UpperCAmelCase:					List[Any]   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Any      =					gen_kwargs.copy()

											__a					:						Tuple      =					self.get_test_dataloader(__UpperCAmelCase   )

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

											__a					:						Optional[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(
											        __UpperCAmelCase ,					__UpperCAmelCase ,					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

											__a					:						Optional[int]      =					self.post_process_function(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					"predict"   )
											__a					:						Dict      =					self.compute_metrics(__UpperCAmelCase   )

											# Prefix all keys with metric_key_prefix + '_'
											for key in list(metrics.keys()   ):
																if not key.startswith(f'{metric_key_prefix}_'   ):
																					__a					:						List[str]      =					metrics.pop(__UpperCAmelCase   )
											metrics.update(output.metrics   )
											return PredictionOutput(predictions=predictions.predictions ,					label_ids=predictions.label_ids ,					metrics=__UpperCAmelCase   )





