



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 	_A     (   self				,							_A		):







													'''simple docstring'''

													for model_result in results.values():
																				for batch_size, sequence_length in zip(model_result['bs']				,							model_result['ss']		):
																											__SCREAMING_SNAKE_CASE									= model_result['result'][batch_size][sequence_length]
																											self.assertIsNotNone(_A		)






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= 'patrickvonplaten/t5-tiny-random'
													__SCREAMING_SNAKE_CASE									= AutoConfig.from_pretrained(_A		)
													__SCREAMING_SNAKE_CASE									= TensorFlowBenchmarkArguments(
													    models=[MODEL_ID]				,							training=_A				,							inference=_A				,							sequence_lengths=[8]				,							batch_sizes=[1]				,							multi_process=_A				,							)
													__SCREAMING_SNAKE_CASE									= TensorFlowBenchmark(_A				,							configs=[config]		)
													__SCREAMING_SNAKE_CASE									= 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 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= 'sshleifer/tiny-gpt2'

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

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











import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__		:					Dict							=logging.get_logger(__name__)

lowerCAmelCase__		:					Optional[int]							={
    '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''',
    # See all SEW models at https://huggingface.co/models?filter=sew
}

class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						UpperCamelCase__      :			Optional[Any]      =  '''sew'''






						def __init__(   self				,							_A=32				,							_A=768				,							_A=12				,							_A=12				,							_A=3_072				,							_A=2				,							_A="gelu"				,							_A=0.1				,							_A=0.1				,							_A=0.1				,							_A=0.0				,							_A=0.1				,							_A=0.1				,							_A=0.0_2				,							_A=1e-5				,							_A="group"				,							_A="gelu"				,							_A=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)				,							_A=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)				,							_A=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)				,							_A=False				,							_A=128				,							_A=16				,							_A=True				,							_A=0.0_5				,							_A=10				,							_A=2				,							_A=0.0				,							_A=10				,							_A=0				,							_A="mean"				,							_A=False				,							_A=False				,							_A=256				,							_A=0				,							_A=1				,							_A=2				,							**_A				,							):







													'''simple docstring'''

													super().__init__(**_A				,							pad_token_id=_A				,							bos_token_id=_A				,							eos_token_id=_A		)
													__SCREAMING_SNAKE_CASE									= hidden_size
													__SCREAMING_SNAKE_CASE									= feat_extract_norm
													__SCREAMING_SNAKE_CASE									= feat_extract_activation
													__SCREAMING_SNAKE_CASE									= list(_A		)
													__SCREAMING_SNAKE_CASE									= list(_A		)
													__SCREAMING_SNAKE_CASE									= list(_A		)
													__SCREAMING_SNAKE_CASE									= conv_bias
													__SCREAMING_SNAKE_CASE									= num_conv_pos_embeddings
													__SCREAMING_SNAKE_CASE									= num_conv_pos_embedding_groups
													__SCREAMING_SNAKE_CASE									= len(self.conv_dim		)
													__SCREAMING_SNAKE_CASE									= num_hidden_layers
													__SCREAMING_SNAKE_CASE									= intermediate_size
													__SCREAMING_SNAKE_CASE									= squeeze_factor
													__SCREAMING_SNAKE_CASE									= hidden_act
													__SCREAMING_SNAKE_CASE									= num_attention_heads
													__SCREAMING_SNAKE_CASE									= hidden_dropout
													__SCREAMING_SNAKE_CASE									= attention_dropout
													__SCREAMING_SNAKE_CASE									= activation_dropout
													__SCREAMING_SNAKE_CASE									= feat_proj_dropout
													__SCREAMING_SNAKE_CASE									= final_dropout
													__SCREAMING_SNAKE_CASE									= layerdrop
													__SCREAMING_SNAKE_CASE									= layer_norm_eps
													__SCREAMING_SNAKE_CASE									= initializer_range
													__SCREAMING_SNAKE_CASE									= vocab_size

													if (
													    (len(self.conv_stride		) != self.num_feat_extract_layers)
													    or (len(self.conv_kernel		) != self.num_feat_extract_layers)
													    or (len(self.conv_dim		) != self.num_feat_extract_layers)
													):
																				raise ValueError(
																				    'Configuration for convolutional layers is incorrect.'
																				    'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'
																				    f"""but is `len(config.conv_dim) = {len(self.conv_dim		)}`, `len(config.conv_stride)"""
																				    f"""= {len(self.conv_stride		)}`, `len(config.conv_kernel) = {len(self.conv_kernel		)}`."""		)

													# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
													__SCREAMING_SNAKE_CASE									= apply_spec_augment
													__SCREAMING_SNAKE_CASE									= mask_time_prob
													__SCREAMING_SNAKE_CASE									= mask_time_length
													__SCREAMING_SNAKE_CASE									= mask_time_min_masks
													__SCREAMING_SNAKE_CASE									= mask_feature_prob
													__SCREAMING_SNAKE_CASE									= mask_feature_length
													__SCREAMING_SNAKE_CASE									= mask_feature_min_masks

													# ctc loss
													__SCREAMING_SNAKE_CASE									= ctc_loss_reduction
													__SCREAMING_SNAKE_CASE									= ctc_zero_infinity

													# sequence classification
													__SCREAMING_SNAKE_CASE									= use_weighted_layer_sum
													__SCREAMING_SNAKE_CASE									= classifier_proj_size






						@property
						def 	_A     (   self		):







													'''simple docstring'''

													return functools.reduce(operator.mul				,							self.conv_stride				,							1		)






