


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 						__a :
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE=100     ,       _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]     ,       )					->			int:




									"""simple docstring"""





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

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




									"""simple docstring"""





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

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

									_UpperCAmelCase											=     self.get_config()

									return config, pixel_values, labels, pixel_labels
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									return BeitConfig(
									    vocab_size=self.vocab_size     ,       image_size=self.image_size     ,       patch_size=self.patch_size     ,       num_channels=self.num_channels     ,       hidden_size=self.hidden_size     ,       num_hidden_layers=self.num_hidden_layers     ,       num_attention_heads=self.num_attention_heads     ,       intermediate_size=self.intermediate_size     ,       hidden_act=self.hidden_act     ,       hidden_dropout_prob=self.hidden_dropout_prob     ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,       is_decoder=_SCREAMING_SNAKE_CASE     ,       initializer_range=self.initializer_range     ,       out_indices=self.out_indices     ,       )
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			List[str]:




									"""simple docstring"""





									_UpperCAmelCase											=     BeitModel(config=_SCREAMING_SNAKE_CASE		)
									model.to(_SCREAMING_SNAKE_CASE		)
									model.eval()
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     BeitForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE		)
									model.to(_SCREAMING_SNAKE_CASE		)
									model.eval()
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length - 1, self.vocab_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     self.type_sequence_label_size
									_UpperCAmelCase											=     BeitForImageClassification(_SCREAMING_SNAKE_CASE		)
									model.to(_SCREAMING_SNAKE_CASE		)
									model.eval()
									_UpperCAmelCase											=     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
									_UpperCAmelCase											=     1
									_UpperCAmelCase											=     BeitForImageClassification(_SCREAMING_SNAKE_CASE		)
									model.to(_SCREAMING_SNAKE_CASE		)
									model.eval()

									_UpperCAmelCase											=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE     ,       labels=_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.type_sequence_label_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Tuple:




									"""simple docstring"""





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




									"""simple docstring"""





									_UpperCAmelCase											=     self.prepare_config_and_inputs()
									_UpperCAmelCase						,			_UpperCAmelCase						,			_UpperCAmelCase						,			_UpperCAmelCase											=     config_and_inputs
									_UpperCAmelCase											=     {'pixel_values': pixel_values}
									return config, inputs_dict



@require_torch
class 						__a (							UpperCAmelCase       , UpperCAmelCase       , unittest.TestCase     ):
		_a      :    Optional[int]           =      (
		    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)
		_a      :    Any           =      (
		    {
		        'feature-extraction': BeitModel,
		        'image-classification': BeitForImageClassification,
		        'image-segmentation': BeitForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		_a      :    Union[str, Any]           =      False
		_a      :    Optional[Any]           =      False
		_a      :    Union[str, Any]           =      False
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									_UpperCAmelCase											=     BeitModelTester(self		)
									_UpperCAmelCase											=     ConfigTester(self     ,       config_class=_SCREAMING_SNAKE_CASE     ,       has_text_modality=_SCREAMING_SNAKE_CASE     ,       hidden_size=37		)
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									self.config_tester.run_common_tests()
		@unittest.skip(reason='BEiT does not use inputs_embeds'		)
		def       UpperCAmelCase__							(     self		)					->			int:




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




									"""simple docstring"""





									pass
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																self.assertIsInstance(model.get_input_embeddings()     ,       (nn.Module)		)
																_UpperCAmelCase											=     model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE     ,       nn.Linear		)		)
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

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

																_UpperCAmelCase											=     ['pixel_values']
																self.assertListEqual(arg_names[:1]     ,       _SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									if not self.model_tester.is_training:
																return

									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()
									_UpperCAmelCase											=     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

																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																model.to(_SCREAMING_SNAKE_CASE		)
																model.train()
																_UpperCAmelCase											=     self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       return_labels=_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     model(**_SCREAMING_SNAKE_CASE		).loss
																loss.backward()
		def       UpperCAmelCase__							(     self		)					->			Tuple:




									"""simple docstring"""





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

									_UpperCAmelCase											=     False
									_UpperCAmelCase											=     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

																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																model.gradient_checkpointing_enable()
																model.to(_SCREAMING_SNAKE_CASE		)
																model.train()
																_UpperCAmelCase											=     self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       return_labels=_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     model(**_SCREAMING_SNAKE_CASE		).loss
																loss.backward()
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

									_UpperCAmelCase											=     _config_zero_init(_SCREAMING_SNAKE_CASE		)
									for model_class in self.all_model_classes:
																_UpperCAmelCase											=     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       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





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



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


							'''simple docstring'''





							_UpperCAmelCase											=     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
							return image







@require_torch
@require_vision
class 						__a (							unittest.TestCase     ):
		@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		)					->			List[str]:




									"""simple docstring"""





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

									_UpperCAmelCase											=     self.default_image_processor
									_UpperCAmelCase											=     prepare_img()
									_UpperCAmelCase											=     image_processor(images=_SCREAMING_SNAKE_CASE     ,       return_tensors='pt'		).pixel_values.to(_SCREAMING_SNAKE_CASE		)

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

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

									# verify the logits
									_UpperCAmelCase											=     torch.Size((1, 196, 8192)		)
									self.assertEqual(logits.shape     ,       _SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     torch.tensor(
									    [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]		).to(_SCREAMING_SNAKE_CASE		)

									self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]     ,       _SCREAMING_SNAKE_CASE     ,       atol=1e-2		)		)
		@slow
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'		).to(_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     self.default_image_processor
									_UpperCAmelCase											=     prepare_img()
									_UpperCAmelCase											=     image_processor(images=_SCREAMING_SNAKE_CASE     ,       return_tensors='pt'		).to(_SCREAMING_SNAKE_CASE		)

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

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

									_UpperCAmelCase											=     torch.tensor([-1.2385, -1.0987, -1.0108]		).to(_SCREAMING_SNAKE_CASE		)

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

									_UpperCAmelCase											=     281
									self.assertEqual(logits.argmax(-1		).item()     ,       _SCREAMING_SNAKE_CASE		)
		@slow
		def       UpperCAmelCase__							(     self		)					->			List[str]:




									"""simple docstring"""





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

									_UpperCAmelCase											=     self.default_image_processor
									_UpperCAmelCase											=     prepare_img()
									_UpperCAmelCase											=     image_processor(images=_SCREAMING_SNAKE_CASE     ,       return_tensors='pt'		).to(_SCREAMING_SNAKE_CASE		)

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

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

									_UpperCAmelCase											=     torch.tensor([1.6881, -0.2787, 0.5901]		).to(_SCREAMING_SNAKE_CASE		)

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

									_UpperCAmelCase											=     2396
									self.assertEqual(logits.argmax(-1		).item()     ,       _SCREAMING_SNAKE_CASE		)
		@slow
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





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

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

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

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

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

									_UpperCAmelCase											=     version.parse(PIL.__version__		) < version.parse('9.0.0'		)

									if is_pillow_less_than_a:
																_UpperCAmelCase											=     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=_SCREAMING_SNAKE_CASE     ,       )
									else:
																_UpperCAmelCase											=     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=_SCREAMING_SNAKE_CASE     ,       )

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






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




									"""simple docstring"""





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

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

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

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

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

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

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




import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
		import tensorflow as tf

		from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments

@require_tf
class 						__a (							unittest.TestCase     ):
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE		)					->			List[str]:




									"""simple docstring"""





									for model_result in results.values():
																for batch_size, sequence_length in zip(model_result['bs']     ,       model_result['ss']		):
																							_UpperCAmelCase											=     model_result['result'][batch_size][sequence_length]
																							self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     'sgugger/tiny-distilbert-classification'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       only_pretrain_model=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_train_result		)
									self.check_results_dict_not_empty(results.memory_train_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_train_result		)
									self.check_results_dict_not_empty(results.memory_train_result		)
		def       UpperCAmelCase__							(     self		)					->			Any:




									"""simple docstring"""





									_UpperCAmelCase											=     'patrickvonplaten/t5-tiny-random'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       configs=[config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU'		)		) == 0     ,       'Cannot do xla on CPU.'		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       use_xla=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									with tempfile.TemporaryDirectory() as tmp_dir:
																_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
																    models=[MODEL_ID]     ,       inference=_SCREAMING_SNAKE_CASE     ,       save_to_csv=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       inference_time_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_time.csv'		)     ,       inference_memory_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_mem.csv'		)     ,       env_info_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'env.csv'		)     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
																_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
																benchmark.run()
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_time.csv'		)		).exists()		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_mem.csv'		)		).exists()		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'env.csv'		)		).exists()		)






		def       UpperCAmelCase__							(     self		)					->			Tuple:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'

									def _check_summary_is_not_empty(_SCREAMING_SNAKE_CASE		):
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'sequential'		)		)
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'cumulative'		)		)
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'current'		)		)
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'total'		)		)

									with tempfile.TemporaryDirectory() as tmp_dir:
																_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
																    models=[MODEL_ID]     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       log_filename=os.path.join(_SCREAMING_SNAKE_CASE     ,       'log.txt'		)     ,       log_print=_SCREAMING_SNAKE_CASE     ,       trace_memory_line_by_line=_SCREAMING_SNAKE_CASE     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
																_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     benchmark.run()
																_check_summary_is_not_empty(result.inference_summary		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'log.txt'		)		).exists()		)
