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				,						UpperCAmelCase				,						UpperCAmelCase=1_0_0				,						UpperCAmelCase=1_3				,						UpperCAmelCase=3_0				,						UpperCAmelCase=2				,						UpperCAmelCase=3				,						UpperCAmelCase=True				,						UpperCAmelCase=True				,						UpperCAmelCase=3_2				,						UpperCAmelCase=4				,						UpperCAmelCase=4				,						UpperCAmelCase=3_7				,						UpperCAmelCase="gelu"				,						UpperCAmelCase=0.1				,						UpperCAmelCase=0.1				,						UpperCAmelCase=1_0				,						UpperCAmelCase=0.02				,						UpperCAmelCase=3				,						UpperCAmelCase=None				,						UpperCAmelCase=[0, 1, 2, 3]				,						)			->   List[str]:
								__a		 =       parent
								__a		 =       1_0_0
								__a		 =       batch_size
								__a		 =       image_size
								__a		 =       patch_size
								__a		 =       num_channels
								__a		 =       is_training
								__a		 =       use_labels
								__a		 =       hidden_size
								__a		 =       num_hidden_layers
								__a		 =       num_attention_heads
								__a		 =       intermediate_size
								__a		 =       hidden_act
								__a		 =       hidden_dropout_prob
								__a		 =       attention_probs_dropout_prob
								__a		 =       type_sequence_label_size
								__a		 =       initializer_range
								__a		 =       scope
								__a		 =       out_indices
								__a		 =       num_labels

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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a		 =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

								__a		 =       self.get_config()

								return config, pixel_values, labels, pixel_labels



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
								return BeitConfig(
								    vocab_size=self.vocab_size				,						image_size=self.image_size				,						patch_size=self.patch_size				,						num_channels=self.num_channels				,						hidden_size=self.hidden_size				,						num_hidden_layers=self.num_hidden_layers				,						num_attention_heads=self.num_attention_heads				,						intermediate_size=self.intermediate_size				,						hidden_act=self.hidden_act				,						hidden_dropout_prob=self.hidden_dropout_prob				,						attention_probs_dropout_prob=self.attention_probs_dropout_prob				,						is_decoder=UpperCAmelCase				,						initializer_range=self.initializer_range				,						out_indices=self.out_indices				,						)



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )			->   List[str]:
								__a		 =       BeitModel(config=UpperCAmelCase   )
								model.to(UpperCAmelCase   )
								model.eval()
								__a		 =       model(UpperCAmelCase   )
								self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)   )



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )			->   Any:
								__a		 =       BeitForMaskedImageModeling(config=UpperCAmelCase   )
								model.to(UpperCAmelCase   )
								model.eval()
								__a		 =       model(UpperCAmelCase   )
								self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length - 1, self.vocab_size)   )



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )			->   Any:
								__a		 =       self.type_sequence_label_size
								__a		 =       BeitForImageClassification(UpperCAmelCase   )
								model.to(UpperCAmelCase   )
								model.eval()
								__a		 =       model(UpperCAmelCase				,						labels=UpperCAmelCase   )
								self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.type_sequence_label_size)   )

								# test greyscale images
								__a		 =       1
								__a		 =       BeitForImageClassification(UpperCAmelCase   )
								model.to(UpperCAmelCase   )
								model.eval()

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



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )			->   int:
								__a		 =       self.num_labels
								__a		 =       BeitForSemanticSegmentation(UpperCAmelCase   )
								model.to(UpperCAmelCase   )
								model.eval()
								__a		 =       model(UpperCAmelCase   )
								self.parent.assertEqual(
								    result.logits.shape				,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)   )
								__a		 =       model(UpperCAmelCase				,						labels=UpperCAmelCase   )
								self.parent.assertEqual(
								    result.logits.shape				,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)   )







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       self.prepare_config_and_inputs()
								__a ,				__a ,				__a ,				__a		 =       config_and_inputs
								__a		 =       {'pixel_values': pixel_values}
								return config, inputs_dict






@require_torch
class 						a__							(       __snake_case				,						__snake_case				,						unittest.TestCase		):
			A__    :       Union[str, Any]        =      (
			    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			A__    :       Optional[Any]        =      (
			    {
			        'feature-extraction': BeitModel,
			        'image-classification': BeitForImageClassification,
			        'image-segmentation': BeitForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			A__    :       Optional[int]        =      False
			A__    :       List[str]        =      False
			A__    :       List[Any]        =      False



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       BeitModelTester(self   )
								__a		 =       ConfigTester(self				,						config_class=UpperCAmelCase				,						has_text_modality=UpperCAmelCase				,						hidden_size=3_7   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								self.config_tester.run_common_tests()



			@unittest.skip(reason='BEiT does not use inputs_embeds'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								pass



			@require_torch_multi_gpu
			@unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[Any]:
								pass



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Any:
								__a ,				__a		 =       self.model_tester.prepare_config_and_inputs_for_common()

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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Any:
								__a ,				__a		 =       self.model_tester.prepare_config_and_inputs_for_common()

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

													__a		 =       ['pixel_values']
													self.assertListEqual(arg_names[:1]				,						UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
								__a		 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								if not self.model_tester.is_training:
													return

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

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

													__a		 =       model_class(UpperCAmelCase   )
													model.to(UpperCAmelCase   )
													model.train()
													__a		 =       self._prepare_for_class(UpperCAmelCase				,						UpperCAmelCase				,						return_labels=UpperCAmelCase   )
													__a		 =       model(**UpperCAmelCase   ).loss
													loss.backward()



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a ,				__a		 =       self.model_tester.prepare_config_and_inputs_for_common()
								if not self.model_tester.is_training:
													return

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

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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
								__a ,				__a		 =       self.model_tester.prepare_config_and_inputs_for_common()

								__a		 =       _config_zero_init(UpperCAmelCase   )
								for model_class in self.all_model_classes:
													__a		 =       model_class(config=UpperCAmelCase   )
													for name, param in model.named_parameters():
																		# we skip lambda parameters as these require special initial values
																		# determined by config.layer_scale_init_value
																		if "lambda" in name:
																							continue
																		if param.requires_grad:
																							self.assertIn(
																							    ((param.data.mean() * 1e9).round() / 1e9).item()				,						[0.0, 1.0]				,						msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''				,						)







			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a		 =       BeitModel.from_pretrained(UpperCAmelCase   )
													self.assertIsNotNone(UpperCAmelCase   )






def 							lowerCAmelCase(     ):
					__a		 =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
					return image



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



			@cached_property
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
								return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'   ) if is_vision_available() else None



			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								__a		 =       BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'   ).to(UpperCAmelCase   )

								__a		 =       self.default_image_processor
								__a		 =       prepare_img()
								__a		 =       image_processor(images=UpperCAmelCase				,						return_tensors='pt'   ).pixel_values.to(UpperCAmelCase   )

								# prepare bool_masked_pos
								__a		 =       torch.ones((1, 1_9_6)				,						dtype=torch.bool   ).to(UpperCAmelCase   )

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

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

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

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



			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'   ).to(UpperCAmelCase   )

								__a		 =       self.default_image_processor
								__a		 =       prepare_img()
								__a		 =       image_processor(images=UpperCAmelCase				,						return_tensors='pt'   ).to(UpperCAmelCase   )

								# forward pass
								with torch.no_grad():
													__a		 =       model(**UpperCAmelCase   )
								__a		 =       outputs.logits

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

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

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

								__a		 =       2_8_1
								self.assertEqual(logits.argmax(-1   ).item()				,						UpperCAmelCase   )



			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'   ).to(
								    UpperCAmelCase   )

								__a		 =       self.default_image_processor
								__a		 =       prepare_img()
								__a		 =       image_processor(images=UpperCAmelCase				,						return_tensors='pt'   ).to(UpperCAmelCase   )

								# forward pass
								with torch.no_grad():
													__a		 =       model(**UpperCAmelCase   )
								__a		 =       outputs.logits

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

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

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

								__a		 =       2_3_9_6
								self.assertEqual(logits.argmax(-1   ).item()				,						UpperCAmelCase   )



			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'   )
								__a		 =       model.to(UpperCAmelCase   )

								__a		 =       BeitImageProcessor(do_resize=UpperCAmelCase				,						size=6_4_0				,						do_center_crop=UpperCAmelCase   )

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

								# forward pass
								with torch.no_grad():
													__a		 =       model(**UpperCAmelCase   )
								__a		 =       outputs.logits

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

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

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

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







			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
								__a		 =       BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'   )
								__a		 =       model.to(UpperCAmelCase   )

								__a		 =       BeitImageProcessor(do_resize=UpperCAmelCase				,						size=6_4_0				,						do_center_crop=UpperCAmelCase   )

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

								# forward pass
								with torch.no_grad():
													__a		 =       model(**UpperCAmelCase   )

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

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

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





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 __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase   )			->   Optional[Any]:
								for model_result in results.values():
													for batch_size, sequence_length in zip(model_result['bs']				,						model_result['ss']   ):
																		__a		 =       model_result['result'][batch_size][sequence_length]
																		self.assertIsNotNone(UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						eager_mode=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       'sgugger/tiny-distilbert-classification'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						only_pretrain_model=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       AutoConfig.from_pretrained(UpperCAmelCase   )
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						eager_mode=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase				,						[config]   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result   )
								self.check_results_dict_not_empty(results.memory_train_result   )



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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a		 =       'patrickvonplaten/t5-tiny-random'
								__a		 =       AutoConfig.from_pretrained(UpperCAmelCase   )
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase				,						configs=[config]   )
								__a		 =       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 __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						use_xla=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



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







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       'sshleifer/tiny-gpt2'

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

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




