






'''simple docstring'''







import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_MAPPING,
							    SegformerForImageClassification,
							    SegformerForSemanticSegmentation,
							    SegformerModel,
							)
							from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import SegformerImageProcessor




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.config_class(**self.inputs_dict       )
								self.parent.assertTrue(hasattr(_lowerCAmelCase   ,"""hidden_sizes"""       )       )
								self.parent.assertTrue(hasattr(_lowerCAmelCase   ,"""num_attention_heads"""       )       )
								self.parent.assertTrue(hasattr(_lowerCAmelCase   ,"""num_encoder_blocks"""       )       )




class 		UpperCamelCase__    :




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase   ,_lowerCAmelCase=13   ,_lowerCAmelCase=64   ,_lowerCAmelCase=3   ,_lowerCAmelCase=4   ,_lowerCAmelCase=[2, 2, 2, 2]   ,_lowerCAmelCase=[8, 4, 2, 1]   ,_lowerCAmelCase=[16, 32, 64, 1_28]   ,_lowerCAmelCase=[1, 4, 8, 16]   ,_lowerCAmelCase=[1, 2, 4, 8]   ,_lowerCAmelCase=True   ,_lowerCAmelCase=True   ,_lowerCAmelCase="gelu"   ,_lowerCAmelCase=0.1   ,_lowerCAmelCase=0.1   ,_lowerCAmelCase=0.02   ,_lowerCAmelCase=3   ,_lowerCAmelCase=None   ,):
								lowerCamelCase__         =  parent
								lowerCamelCase__         =  batch_size
								lowerCamelCase__         =  image_size
								lowerCamelCase__         =  num_channels
								lowerCamelCase__         =  num_encoder_blocks
								lowerCamelCase__         =  sr_ratios
								lowerCamelCase__         =  depths
								lowerCamelCase__         =  hidden_sizes
								lowerCamelCase__         =  downsampling_rates
								lowerCamelCase__         =  num_attention_heads
								lowerCamelCase__         =  is_training
								lowerCamelCase__         =  use_labels
								lowerCamelCase__         =  hidden_act
								lowerCamelCase__         =  hidden_dropout_prob
								lowerCamelCase__         =  attention_probs_dropout_prob
								lowerCamelCase__         =  initializer_range
								lowerCamelCase__         =  num_labels
								lowerCamelCase__         =  scope






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

								lowerCamelCase__         =  self.get_config()
								return config, pixel_values, labels






		def 			UpperCamelCase_					(      self       ):
								return SegformerConfig(
								    image_size=self.image_size   ,num_channels=self.num_channels   ,num_encoder_blocks=self.num_encoder_blocks   ,depths=self.depths   ,hidden_sizes=self.hidden_sizes   ,num_attention_heads=self.num_attention_heads   ,hidden_act=self.hidden_act   ,hidden_dropout_prob=self.hidden_dropout_prob   ,attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,initializer_range=self.initializer_range   ,)






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  SegformerModel(config=_lowerCAmelCase       )
								model.to(_lowerCAmelCase       )
								model.eval()
								lowerCamelCase__         =  model(_lowerCAmelCase       )
								lowerCamelCase__         =  lowerCamelCase__         =  self.image_size // (self.downsampling_rates[-1] * 2)
								self.parent.assertEqual(
								    result.last_hidden_state.shape   ,(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  self.num_labels
								lowerCamelCase__         =  SegformerForSemanticSegmentation(_lowerCAmelCase       )
								model.to(_lowerCAmelCase       )
								model.eval()
								lowerCamelCase__         =  model(_lowerCAmelCase       )
								self.parent.assertEqual(
								    result.logits.shape   ,(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)       )
								lowerCamelCase__         =  model(_lowerCAmelCase   ,labels=_lowerCAmelCase       )
								self.parent.assertEqual(
								    result.logits.shape   ,(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)       )
								self.parent.assertGreater(result.loss   ,0.0       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  1
								lowerCamelCase__         =  SegformerForSemanticSegmentation(config=_lowerCAmelCase       )
								model.to(_lowerCAmelCase       )
								model.eval()
								lowerCamelCase__         =  torch.randint(0   ,1   ,(self.batch_size, self.image_size, self.image_size)       ).to(_lowerCAmelCase       )
								lowerCamelCase__         =  model(_lowerCAmelCase   ,labels=_lowerCAmelCase       )
								self.parent.assertGreater(result.loss   ,0.0       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.prepare_config_and_inputs()
								lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__         =  config_and_inputs
								lowerCamelCase__         =  {"""pixel_values""": pixel_values}
								return config, inputs_dict




@require_torch
class 		UpperCamelCase__    (a   ,a   ,unittest.TestCase			):




		'''simple docstring'''





		_UpperCamelCase				  =					(
		    (
		        SegformerModel,
		        SegformerForSemanticSegmentation,
		        SegformerForImageClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		_UpperCamelCase				  =					(
		    {
		        'feature-extraction': SegformerModel,
		        'image-classification': SegformerForImageClassification,
		        'image-segmentation': SegformerForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		_UpperCamelCase				  =					True
		_UpperCamelCase				  =					False
		_UpperCamelCase				  =					False
		_UpperCamelCase				  =					False






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  SegformerModelTester(self       )
								lowerCamelCase__         =  SegformerConfigTester(self   ,config_class=_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								self.config_tester.run_common_tests()






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_binary_image_segmentation(*_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_segmentation(*_lowerCAmelCase       )






		@unittest.skip("""SegFormer does not use inputs_embeds"""       )
		def 			UpperCamelCase_					(      self       ):
								pass






		@unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""       )
		def 			UpperCamelCase_					(      self       ):
								pass






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__       ,			lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs_for_common()

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

														lowerCamelCase__         =  ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__       ,			lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase__         =  True

								for model_class in self.all_model_classes:
														lowerCamelCase__         =  True
														lowerCamelCase__         =  False
														lowerCamelCase__         =  True
														lowerCamelCase__         =  model_class(_lowerCAmelCase       )
														model.to(_lowerCAmelCase       )
														model.eval()
														with torch.no_grad():
																				lowerCamelCase__         =  model(**self._prepare_for_class(_lowerCAmelCase   ,_lowerCAmelCase       )       )
														lowerCamelCase__         =  outputs.attentions

														lowerCamelCase__         =  sum(self.model_tester.depths       )
														self.assertEqual(len(_lowerCAmelCase       )   ,_lowerCAmelCase       )

														# check that output_attentions also work using config
														del inputs_dict["output_attentions"]
														lowerCamelCase__         =  True
														lowerCamelCase__         =  model_class(_lowerCAmelCase       )
														model.to(_lowerCAmelCase       )
														model.eval()
														with torch.no_grad():
																				lowerCamelCase__         =  model(**self._prepare_for_class(_lowerCAmelCase   ,_lowerCAmelCase       )       )
														lowerCamelCase__         =  outputs.attentions

														self.assertEqual(len(_lowerCAmelCase       )   ,_lowerCAmelCase       )

														# verify the first attentions (first block, first layer)
														lowerCamelCase__         =  (self.model_tester.image_size // 4) ** 2
														lowerCamelCase__         =  (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
														self.assertListEqual(
														    list(attentions[0].shape[-3:]       )   ,[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]   ,)

														# verify the last attentions (last block, last layer)
														lowerCamelCase__         =  (self.model_tester.image_size // 32) ** 2
														lowerCamelCase__         =  (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
														self.assertListEqual(
														    list(attentions[-1].shape[-3:]       )   ,[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]   ,)
														lowerCamelCase__         =  len(_lowerCAmelCase       )

														# Check attention is always last and order is fine
														lowerCamelCase__         =  True
														lowerCamelCase__         =  True
														lowerCamelCase__         =  model_class(_lowerCAmelCase       )
														model.to(_lowerCAmelCase       )
														model.eval()
														with torch.no_grad():
																				lowerCamelCase__         =  model(**self._prepare_for_class(_lowerCAmelCase   ,_lowerCAmelCase       )       )

														self.assertEqual(out_len + 1   ,len(_lowerCAmelCase       )       )

														lowerCamelCase__         =  outputs.attentions

														self.assertEqual(len(_lowerCAmelCase       )   ,_lowerCAmelCase       )
														# verify the first attentions (first block, first layer)
														lowerCamelCase__         =  (self.model_tester.image_size // 4) ** 2
														lowerCamelCase__         =  (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
														self.assertListEqual(
														    list(self_attentions[0].shape[-3:]       )   ,[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]   ,)






		def 			UpperCamelCase_					(      self       ):
								def check_hidden_states_output(_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
														lowerCamelCase__         =  model_class(_lowerCAmelCase       )
														model.to(_lowerCAmelCase       )
														model.eval()

														with torch.no_grad():
																				lowerCamelCase__         =  model(**self._prepare_for_class(_lowerCAmelCase   ,_lowerCAmelCase       )       )

														lowerCamelCase__         =  outputs.hidden_states

														lowerCamelCase__         =  self.model_tester.num_encoder_blocks
														self.assertEqual(len(_lowerCAmelCase       )   ,_lowerCAmelCase       )

														# verify the first hidden states (first block)
														self.assertListEqual(
														    list(hidden_states[0].shape[-3:]       )   ,[
														        self.model_tester.hidden_sizes[0],
														        self.model_tester.image_size // 4,
														        self.model_tester.image_size // 4,
														    ]   ,)

								lowerCamelCase__       ,			lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														lowerCamelCase__         =  True
														check_hidden_states_output(_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														lowerCamelCase__         =  True

														check_hidden_states_output(_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								if not self.model_tester.is_training:
														return

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

								for model_class in self.all_model_classes:
														if model_class in get_values(_lowerCAmelCase       ):
																				continue

														lowerCamelCase__         =  model_class(_lowerCAmelCase       )
														model.to(_lowerCAmelCase       )
														model.train()
														lowerCamelCase__         =  self._prepare_for_class(_lowerCAmelCase   ,_lowerCAmelCase   ,return_labels=_lowerCAmelCase       )
														lowerCamelCase__         =  model(**_lowerCAmelCase       ).loss
														loss.backward()






		@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
		def 			UpperCamelCase_					(      self       ):
								pass






		@slow
		def 			UpperCamelCase_					(      self       ):
								for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCamelCase__         =  SegformerModel.from_pretrained(_lowerCAmelCase       )
														self.assertIsNotNone(_lowerCAmelCase       )



def 						A__			(				):
						lowerCamelCase__         =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
						return image





@require_torch
class 		UpperCamelCase__    (unittest.TestCase			):




		'''simple docstring'''





		@slow
		def 			UpperCamelCase_					(      self       ):
								# only resize + normalize
								lowerCamelCase__         =  SegformerImageProcessor(
								    image_scale=(5_12, 5_12)   ,keep_ratio=_lowerCAmelCase   ,align=_lowerCAmelCase   ,do_random_crop=_lowerCAmelCase       )
								lowerCamelCase__         =  SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""       ).to(
								    _lowerCAmelCase       )

								lowerCamelCase__         =  prepare_img()
								lowerCamelCase__         =  image_processor(images=_lowerCAmelCase   ,return_tensors="""pt"""       )
								lowerCamelCase__         =  encoded_inputs.pixel_values.to(_lowerCAmelCase       )

								with torch.no_grad():
														lowerCamelCase__         =  model(_lowerCAmelCase       )

								lowerCamelCase__         =  torch.Size((1, model.config.num_labels, 1_28, 1_28)       )
								self.assertEqual(outputs.logits.shape   ,_lowerCAmelCase       )

								lowerCamelCase__         =  torch.tensor(
								    [
								        [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
								        [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
								        [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
								    ]       ).to(_lowerCAmelCase       )
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]   ,_lowerCAmelCase   ,atol=1E-4       )       )






		@slow
		def 			UpperCamelCase_					(      self       ):
								# only resize + normalize
								lowerCamelCase__         =  SegformerImageProcessor(
								    image_scale=(5_12, 5_12)   ,keep_ratio=_lowerCAmelCase   ,align=_lowerCAmelCase   ,do_random_crop=_lowerCAmelCase       )
								lowerCamelCase__         =  SegformerForSemanticSegmentation.from_pretrained(
								    """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""       ).to(_lowerCAmelCase       )

								lowerCamelCase__         =  prepare_img()
								lowerCamelCase__         =  image_processor(images=_lowerCAmelCase   ,return_tensors="""pt"""       )
								lowerCamelCase__         =  encoded_inputs.pixel_values.to(_lowerCAmelCase       )

								with torch.no_grad():
														lowerCamelCase__         =  model(_lowerCAmelCase       )

								lowerCamelCase__         =  torch.Size((1, model.config.num_labels, 1_28, 1_28)       )
								self.assertEqual(outputs.logits.shape   ,_lowerCAmelCase       )

								lowerCamelCase__         =  torch.tensor(
								    [
								        [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
								        [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
								        [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
								    ]       ).to(_lowerCAmelCase       )
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]   ,_lowerCAmelCase   ,atol=1E-1       )       )






		@slow
		def 			UpperCamelCase_					(      self       ):
								# only resize + normalize
								lowerCamelCase__         =  SegformerImageProcessor(
								    image_scale=(5_12, 5_12)   ,keep_ratio=_lowerCAmelCase   ,align=_lowerCAmelCase   ,do_random_crop=_lowerCAmelCase       )
								lowerCamelCase__         =  SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""       ).to(
								    _lowerCAmelCase       )

								lowerCamelCase__         =  prepare_img()
								lowerCamelCase__         =  image_processor(images=_lowerCAmelCase   ,return_tensors="""pt"""       )
								lowerCamelCase__         =  encoded_inputs.pixel_values.to(_lowerCAmelCase       )

								with torch.no_grad():
														lowerCamelCase__         =  model(_lowerCAmelCase       )

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

								lowerCamelCase__         =  image_processor.post_process_semantic_segmentation(outputs=_lowerCAmelCase   ,target_sizes=[(5_00, 3_00)]       )
								lowerCamelCase__         =  torch.Size((5_00, 3_00)       )
								self.assertEqual(segmentation[0].shape   ,_lowerCAmelCase       )

								lowerCamelCase__         =  image_processor.post_process_semantic_segmentation(outputs=_lowerCAmelCase       )
								lowerCamelCase__         =  torch.Size((1_28, 1_28)       )
								self.assertEqual(segmentation[0].shape   ,_lowerCAmelCase       )









'''simple docstring'''







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 		UpperCamelCase__    (unittest.TestCase			):




		'''simple docstring'''





		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								for model_result in results.values():
														for batch_size, sequence_length in zip(model_result["""bs"""]   ,model_result["""ss"""]       ):
																				lowerCamelCase__         =  model_result["""result"""][batch_size][sequence_length]
																				self.assertIsNotNone(_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,eager_mode=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sgugger/tiny-distilbert-classification"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,only_pretrain_model=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,eager_mode=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[config]       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[config]       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[config]       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """patrickvonplaten/t5-tiny-random"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,configs=[config]       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,use_xla=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								with tempfile.TemporaryDirectory() as tmp_dir:
														lowerCamelCase__         =  TensorFlowBenchmarkArguments(
														    models=[MODEL_ID]   ,inference=_lowerCAmelCase   ,save_to_csv=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,inference_time_csv_file=os.path.join(_lowerCAmelCase   ,"""inf_time.csv"""       )   ,inference_memory_csv_file=os.path.join(_lowerCAmelCase   ,"""inf_mem.csv"""       )   ,env_info_csv_file=os.path.join(_lowerCAmelCase   ,"""env.csv"""       )   ,multi_process=_lowerCAmelCase   ,)
														lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
														benchmark.run()
														self.assertTrue(Path(os.path.join(_lowerCAmelCase   ,"""inf_time.csv"""       )       ).exists()       )
														self.assertTrue(Path(os.path.join(_lowerCAmelCase   ,"""inf_mem.csv"""       )       ).exists()       )
														self.assertTrue(Path(os.path.join(_lowerCAmelCase   ,"""env.csv"""       )       ).exists()       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""

								def _check_summary_is_not_empty(_lowerCAmelCase       ):
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""sequential"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""cumulative"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""current"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""total"""       )       )

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

