






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

	def      __UpperCamelCase							(      self   :						List[str]  ,					UpperCAmelCase_   :						Union[str, Any]):
			for model_result in results.values():
					for batch_size, sequence_length in zip(model_result['bs']  ,					model_result['ss']):
							UpperCamelCase__				:					str					=					model_result['result'][batch_size][sequence_length]
							self.assertIsNotNone(UpperCAmelCase_)

	def      __UpperCamelCase							(      self   :						Tuple):
			UpperCamelCase__				:					Dict					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					Tuple					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					eager_mode=UpperCAmelCase_  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					Optional[int]					=					TensorFlowBenchmark(UpperCAmelCase_)
			UpperCamelCase__				:					Tuple					=					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   :						Optional[Any]):
			UpperCamelCase__				:					List[str]					=					'sgugger/tiny-distilbert-classification'
			UpperCamelCase__				:					Optional[int]					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					multi_process=UpperCAmelCase_  ,					only_pretrain_model=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					Optional[Any]					=					TensorFlowBenchmark(UpperCAmelCase_)
			UpperCamelCase__				:					int					=					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   :						Tuple):
			UpperCamelCase__				:					Optional[Any]					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					Optional[int]					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					Optional[Any]					=					TensorFlowBenchmark(UpperCAmelCase_)
			UpperCamelCase__				:					List[Any]					=					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   :						Optional[Any]):
			UpperCamelCase__				:					int					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					int					=					AutoConfig.from_pretrained(UpperCAmelCase_)
			UpperCamelCase__				:					int					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					eager_mode=UpperCAmelCase_  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					Optional[Any]					=					TensorFlowBenchmark(UpperCAmelCase_  ,					[config])
			UpperCamelCase__				:					Any					=					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   :						Optional[Any]):
			UpperCamelCase__				:					List[str]					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					Union[str, Any]					=					AutoConfig.from_pretrained(UpperCAmelCase_)
			UpperCamelCase__				:					Tuple					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					str					=					TensorFlowBenchmark(UpperCAmelCase_  ,					[config])
			UpperCamelCase__				:					Dict					=					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   :						Union[str, Any]):
			UpperCamelCase__				:					int					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					List[str]					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					List[str]					=					TensorFlowBenchmark(UpperCAmelCase_)
			UpperCamelCase__				:					Dict					=					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):
			UpperCamelCase__				:					Tuple					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					Optional[int]					=					AutoConfig.from_pretrained(UpperCAmelCase_)
			UpperCamelCase__				:					str					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					Optional[int]					=					TensorFlowBenchmark(UpperCAmelCase_  ,					[config])
			UpperCamelCase__				:					Dict					=					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   :						Dict):
			UpperCamelCase__				:					Tuple					=					'patrickvonplaten/t5-tiny-random'
			UpperCamelCase__				:					List[str]					=					AutoConfig.from_pretrained(UpperCAmelCase_)
			UpperCamelCase__				:					str					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					str					=					TensorFlowBenchmark(UpperCAmelCase_  ,					configs=[config])
			UpperCamelCase__				:					Union[str, Any]					=					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):
			UpperCamelCase__				:					str					=					'sshleifer/tiny-gpt2'
			UpperCamelCase__				:					List[Any]					=					TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]  ,					training=UpperCAmelCase_  ,					inference=UpperCAmelCase_  ,					sequence_lengths=[8]  ,					batch_sizes=[1]  ,					use_xla=UpperCAmelCase_  ,					multi_process=UpperCAmelCase_  ,					)
			UpperCamelCase__				:					List[Any]					=					TensorFlowBenchmark(UpperCAmelCase_)
			UpperCamelCase__				:					Any					=					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[str]):
			UpperCamelCase__				:					Any					=					'sshleifer/tiny-gpt2'
			with tempfile.TemporaryDirectory() as tmp_dir:
					UpperCamelCase__				:					str					=					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_  ,					)
					UpperCamelCase__				:					Optional[Any]					=					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      __UpperCamelCase							(      self   :						str):
			UpperCamelCase__				:					List[Any]					=					'sshleifer/tiny-gpt2'

			def _check_summary_is_not_empty(UpperCAmelCase_   :						Union[str, Any]):
					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:
					UpperCamelCase__				:					Tuple					=					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_  ,					)
					UpperCamelCase__				:					Any					=					TensorFlowBenchmark(UpperCAmelCase_)
					UpperCamelCase__				:					Union[str, Any]					=					benchmark.run()
					_check_summary_is_not_empty(result.inference_summary)
					self.assertTrue(Path(os.path.join(UpperCAmelCase_  ,					'log.txt')).exists())












'''simple docstring'''




import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__          =						logging.get_logger(__name__)

lowerCAmelCase__          =						{
    'nvidia/segformer-b0-finetuned-ade-512-512': (
        'https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''segformer'''

	def __init__(      self   :						Tuple  ,					UpperCAmelCase_   :						Optional[Any]=3  ,					UpperCAmelCase_   :						Optional[int]=4  ,					UpperCAmelCase_   :						Tuple=[2, 2, 2, 2]  ,					UpperCAmelCase_   :						List[str]=[8, 4, 2, 1]  ,					UpperCAmelCase_   :						Union[str, Any]=[32, 64, 160, 256]  ,					UpperCAmelCase_   :						Any=[7, 3, 3, 3]  ,					UpperCAmelCase_   :						Any=[4, 2, 2, 2]  ,					UpperCAmelCase_   :						Union[str, Any]=[1, 2, 5, 8]  ,					UpperCAmelCase_   :						Tuple=[4, 4, 4, 4]  ,					UpperCAmelCase_   :						str="gelu"  ,					UpperCAmelCase_   :						List[Any]=0.0  ,					UpperCAmelCase_   :						int=0.0  ,					UpperCAmelCase_   :						int=0.1  ,					UpperCAmelCase_   :						List[str]=0.02  ,					UpperCAmelCase_   :						Dict=0.1  ,					UpperCAmelCase_   :						Dict=1e-6  ,					UpperCAmelCase_   :						int=256  ,					UpperCAmelCase_   :						Optional[int]=255  ,					**UpperCAmelCase_   :						Tuple  ,					):
			super().__init__(**UpperCAmelCase_)

			if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
					warnings.warn(
					    'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
					    ' removed, as the behaviour will default to that of reshape_last_stage = True.'  ,					UpperCAmelCase_  ,					)

			UpperCamelCase__				:					List[Any]					=					num_channels
			UpperCamelCase__				:					Any					=					num_encoder_blocks
			UpperCamelCase__				:					Dict					=					depths
			UpperCamelCase__				:					int					=					sr_ratios
			UpperCamelCase__				:					str					=					hidden_sizes
			UpperCamelCase__				:					List[str]					=					patch_sizes
			UpperCamelCase__				:					Optional[int]					=					strides
			UpperCamelCase__				:					Dict					=					mlp_ratios
			UpperCamelCase__				:					List[str]					=					num_attention_heads
			UpperCamelCase__				:					int					=					hidden_act
			UpperCamelCase__				:					Any					=					hidden_dropout_prob
			UpperCamelCase__				:					str					=					attention_probs_dropout_prob
			UpperCamelCase__				:					List[str]					=					classifier_dropout_prob
			UpperCamelCase__				:					List[Any]					=					initializer_range
			UpperCamelCase__				:					Union[str, Any]					=					drop_path_rate
			UpperCamelCase__				:					int					=					layer_norm_eps
			UpperCamelCase__				:					Dict					=					decoder_hidden_size
			UpperCamelCase__				:					List[Any]					=					kwargs.get('reshape_last_stage'  ,					UpperCAmelCase_)
			UpperCamelCase__				:					List[str]					=					semantic_loss_ignore_index







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  version.parse('''1.11'''    )

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return OrderedDict(
			    [
			        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
			    ])

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return 1e-4





	@property
	def      __UpperCamelCase							(      self   :						Any):
			return 12




