
import random
import timeit
from functools import wraps
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_tf_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_tf_available():
					import tensorflow as tf
					from tensorflow.python.framework.errors_impl import ResourceExhaustedError

					from .benchmark_args_tf import TensorFlowBenchmarkArguments

if is_pyanvml_available():
					import pyanvml.pyanvml as nvml

lowerCamelCase__          =		logging.get_logger(__name__)
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )				->		str:
			def run_func(SCREAMING_SNAKE_CASE_     ):
						@wraps(SCREAMING_SNAKE_CASE_     )
						def run_in_eager_mode(*SCREAMING_SNAKE_CASE_       ,							**SCREAMING_SNAKE_CASE_     ):
									return func(*SCREAMING_SNAKE_CASE_       ,							**SCREAMING_SNAKE_CASE_     )

						@wraps(SCREAMING_SNAKE_CASE_     )
						@tf.function(experimental_compile=SCREAMING_SNAKE_CASE_     )
						def run_in_graph_mode(*SCREAMING_SNAKE_CASE_       ,							**SCREAMING_SNAKE_CASE_     ):
									return func(*SCREAMING_SNAKE_CASE_       ,							**SCREAMING_SNAKE_CASE_     )

						if do_eager_mode is True:
									if use_xla is not False:
												raise ValueError(
												    'Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.'     )
									return run_in_eager_mode
						else:
									return run_in_graph_mode

			return run_func
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )				->		["tf.Tensor"]:
			lowerCAmelCase__      :      Union[str, Any]         =		random.Random()
			lowerCAmelCase__      :      Dict         =		[rng.randint(0       ,							vocab_size - 1     ) for i in range(batch_size * sequence_length     )]
			return tf.constant(SCREAMING_SNAKE_CASE_       ,							shape=(batch_size, sequence_length)       ,							dtype=tf.intaa     )







class      A__							(     __magic_name__	):
				lowercase			    =  42
				lowercase			    =  42
				lowercase			    =  "TensorFlow"






				@property
				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							return tf.__version__






				def 							_lowerCamelCase		(						self					:      List[Any]    ,     a					:      str    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		self.args.strategy
							if strategy is None:
										raise ValueError('A device strategy has to be initialized before using TensorFlow.'   )
							lowerCAmelCase__      :      Union[str, Any]         =		self._prepare_inference_func(a    ,     a    ,     a   )
							return self._measure_speed(_inference   )






				def 							_lowerCamelCase		(						self					:      Any    ,     a					:      str    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		self.args.strategy
							if strategy is None:
										raise ValueError('A device strategy has to be initialized before using TensorFlow.'   )
							lowerCAmelCase__      :      Any         =		self._prepare_train_func(a    ,     a    ,     a   )
							return self._measure_speed(_train   )






				def 							_lowerCamelCase		(						self					:      str    ,     a					:      str    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							if self.args.is_gpu:
										tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]    ,     a   )
							lowerCAmelCase__      :      Optional[Any]         =		self.args.strategy
							if strategy is None:
										raise ValueError('A device strategy has to be initialized before using TensorFlow.'   )
							lowerCAmelCase__      :      Any         =		self._prepare_inference_func(a    ,     a    ,     a   )
							return self._measure_memory(_inference   )






				def 							_lowerCamelCase		(						self					:      List[str]    ,     a					:      str    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							if self.args.is_gpu:
										tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]    ,     a   )
							lowerCAmelCase__      :      Optional[int]         =		self.args.strategy
							if strategy is None:
										raise ValueError('A device strategy has to be initialized before using TensorFlow.'   )

							lowerCAmelCase__      :      int         =		self._prepare_train_func(a    ,     a    ,     a   )
							return self._measure_memory(_train   )






				def 							_lowerCamelCase		(						self					:      Dict    ,     a					:      str    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      str         =		self.config_dict[model_name]

							if self.args.fpaa:
										raise NotImplementedError('Mixed precision is currently not supported.'   )

							lowerCAmelCase__      :      List[str]         =		(
							    hasattr(a    ,     'architectures'   )
							    and isinstance(config.architectures    ,     a   )
							    and len(config.architectures   ) > 0
							)
							if not self.args.only_pretrain_model and has_model_class_in_config:
										try:
													lowerCAmelCase__      :      str         =		'TF' + config.architectures[0]  # prepend 'TF' for tensorflow model
													lowerCAmelCase__      :      Union[str, Any]         =		__import__('transformers'    ,     fromlist=[model_class]   )
													lowerCAmelCase__      :      Union[str, Any]         =		getattr(a    ,     a   )
													lowerCAmelCase__      :      List[str]         =		model_cls(a   )
										except ImportError:
													raise ImportError(
													    f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
													    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'   )
							else:
										lowerCAmelCase__      :      str         =		TF_MODEL_MAPPING[config.__class__](a   )

							# encoder-decoder has vocab size saved differently
							lowerCAmelCase__      :      Dict         =		config.vocab_size if hasattr(a    ,     'vocab_size'   ) else config.encoder.vocab_size
							lowerCAmelCase__      :      Dict         =		random_input_ids(a    ,     a    ,     a   )

							@run_with_tf_optimizations(self.args.eager_mode    ,     self.args.use_xla   )
							def encoder_decoder_forward():
										return model(a    ,     decoder_input_ids=a    ,     training=a   )

							@run_with_tf_optimizations(self.args.eager_mode    ,     self.args.use_xla   )
							def encoder_forward():
										return model(a    ,     training=a   )

							lowerCAmelCase__      :      int         =		encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

							return _inference






				def 							_lowerCamelCase		(						self					:      Optional[int]    ,     a					:      str    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      str         =		self.config_dict[model_name]

							if self.args.eager_mode is not False:
										raise ValueError('Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.'   )

							if self.args.fpaa:
										raise NotImplementedError('Mixed precision is currently not supported.'   )

							lowerCAmelCase__      :      Tuple         =		(
							    hasattr(a    ,     'architectures'   )
							    and isinstance(config.architectures    ,     a   )
							    and len(config.architectures   ) > 0
							)
							if not self.args.only_pretrain_model and has_model_class_in_config:
										try:
													lowerCAmelCase__      :      int         =		'TF' + config.architectures[0]  # prepend 'TF' for tensorflow model
													lowerCAmelCase__      :      Union[str, Any]         =		__import__('transformers'    ,     fromlist=[model_class]   )
													lowerCAmelCase__      :      Any         =		getattr(a    ,     a   )
													lowerCAmelCase__      :      Dict         =		model_cls(a   )
										except ImportError:
													raise ImportError(
													    f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
													    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'   )
							else:
										lowerCAmelCase__      :      Any         =		TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](a   )

							# encoder-decoder has vocab size saved differently
							lowerCAmelCase__      :      Optional[Any]         =		config.vocab_size if hasattr(a    ,     'vocab_size'   ) else config.encoder.vocab_size
							lowerCAmelCase__      :      Optional[Any]         =		random_input_ids(a    ,     a    ,     a   )

							@run_with_tf_optimizations(self.args.eager_mode    ,     self.args.use_xla   )
							def encoder_decoder_train():
										lowerCAmelCase__      :      Optional[int]         =		model(a    ,     decoder_input_ids=a    ,     labels=a    ,     training=a   )[0]
										lowerCAmelCase__      :      Optional[Any]         =		tf.gradients(a    ,     model.trainable_variables   )
										return gradients

							@run_with_tf_optimizations(self.args.eager_mode    ,     self.args.use_xla   )
							def encoder_train():
										lowerCAmelCase__      :      Union[str, Any]         =		model(a    ,     labels=a    ,     training=a   )[0]
										lowerCAmelCase__      :      Dict         =		tf.gradients(a    ,     model.trainable_variables   )
										return gradients

							lowerCAmelCase__      :      Tuple         =		encoder_decoder_train if config.is_encoder_decoder else encoder_train

							return _train






				def 							_lowerCamelCase		(						self					:      Dict    ,     a					:      List[Any]   ):






							'''simple docstring'''







							with self.args.strategy.scope():
										try:
													if self.args.is_tpu or self.args.use_xla:
																# run additional 10 times to stabilize compilation for tpu
																logger.info('Do inference on TPU. Running model 5 times to stabilize compilation'   )
																timeit.repeat(a    ,     repeat=1    ,     number=5   )

													# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
													lowerCAmelCase__      :      Any         =		timeit.repeat(
													    a    ,     repeat=self.args.repeat    ,     number=10    ,     )

													return min(a   ) / 1_0.0
										except ResourceExhaustedError as e:
													self.print_fn(f'''Doesn\'t fit on GPU. {e}'''   )






				def 							_lowerCamelCase		(						self					:      Tuple    ,     a					:      Callable[[], None]   ):






							'''simple docstring'''







							logger.info(
							    'Note that TensorFlow allocates more memory than '
							    'it might need to speed up computation. '
							    'The memory reported here corresponds to the memory '
							    'reported by `nvidia-smi`, which can vary depending '
							    'on total available memory on the GPU that is used.'   )
							with self.args.strategy.scope():
										try:
													if self.args.trace_memory_line_by_line:
																if not self.args.eager_mode:
																			raise ValueError(
																			    '`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory'
																			    ' consumption line by line.'   )
																lowerCAmelCase__      :      List[Any]         =		start_memory_tracing('transformers'   )

													if self.args.is_tpu:
																# tpu
																raise NotImplementedError(
																    'Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking'
																    ' with `args.memory=False`'   )
													elif self.args.is_gpu:
																# gpu
																if not is_pyanvml_available():
																			logger.warning(
																			    'py3nvml not installed, we won\'t log GPU memory usage. '
																			    'Install py3nvml (pip install py3nvml) to log information about GPU.'   )
																			lowerCAmelCase__      :      Union[str, Any]         =		'N/A'
																else:
																			logger.info(
																			    'Measuring total GPU usage on GPU device. Make sure to not have additional processes'
																			    ' running on the same GPU.'   )
																			# init nvml
																			nvml.nvmlInit()
																			func()
																			lowerCAmelCase__      :      str         =		nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx   )
																			lowerCAmelCase__      :      List[Any]         =		nvml.nvmlDeviceGetMemoryInfo(a   )
																			lowerCAmelCase__      :      Optional[Any]         =		meminfo.used
																			lowerCAmelCase__      :      Optional[int]         =		Memory(a   )
																			# shutdown nvml
																			nvml.nvmlShutdown()
													else:
																# cpu
																if self.args.trace_memory_line_by_line:
																			logger.info(
																			    'When enabling line by line tracing, the max peak memory for CPU is inaccurate in'
																			    ' TensorFlow.'   )
																			lowerCAmelCase__      :      Optional[int]         =		None
																else:
																			lowerCAmelCase__      :      Union[str, Any]         =		measure_peak_memory_cpu(a   )
																			lowerCAmelCase__      :      Dict         =		Memory(a   ) if isinstance(a    ,     a   ) else memory_bytes
													if self.args.trace_memory_line_by_line:
																lowerCAmelCase__      :      Any         =		stop_memory_tracing(a   )
																if memory is None:
																			lowerCAmelCase__      :      List[str]         =		summary.total
													else:
																lowerCAmelCase__      :      Any         =		None

													return memory, summary
										except ResourceExhaustedError as e:
													self.print_fn(f'''Doesn\'t fit on GPU. {e}'''   )
													return "N/A", None

import datetime
import platform
import subprocess
from typing import Optional, Tuple, Union

import numpy as np
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )				->		np.array:
			lowerCAmelCase__      :      Dict         =		F'''{sampling_rate}'''
			lowerCAmelCase__      :      Any         =		'1'
			lowerCAmelCase__      :      Optional[Any]         =		'f32le'
			lowerCAmelCase__      :      Any         =		[
			    'ffmpeg',
			    '-i',
			    'pipe:0',
			    '-ac',
			    ac,
			    '-ar',
			    ar,
			    '-f',
			    format_for_conversion,
			    '-hide_banner',
			    '-loglevel',
			    'quiet',
			    'pipe:1',
			]

			try:
						with subprocess.Popen(SCREAMING_SNAKE_CASE_       ,							stdin=subprocess.PIPE       ,							stdout=subprocess.PIPE     ) as ffmpeg_process:
									lowerCAmelCase__      :      List[Any]         =		ffmpeg_process.communicate(SCREAMING_SNAKE_CASE_     )
			except FileNotFoundError as error:
						raise ValueError('ffmpeg was not found but is required to load audio files from filename'     ) from error
			lowerCAmelCase__      :      List[str]         =		output_stream[0]
			lowerCAmelCase__      :      str         =		np.frombuffer(SCREAMING_SNAKE_CASE_       ,							np.floataa     )
			if audio.shape[0] == 0:
						raise ValueError('Malformed soundfile'     )
			return audio
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_ = "f32le"       ,							)				->		Dict:
			lowerCAmelCase__      :      Optional[Any]         =		F'''{sampling_rate}'''
			lowerCAmelCase__      :      Any         =		'1'
			if format_for_conversion == "s16le":
						lowerCAmelCase__      :      Dict         =		2
			elif format_for_conversion == "f32le":
						lowerCAmelCase__      :      List[str]         =		4
			else:
						raise ValueError(F'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`'''     )

			lowerCAmelCase__      :      Tuple         =		platform.system()
			if system == "Linux":
						lowerCAmelCase__      :      str         =		'alsa'
						lowerCAmelCase__      :      str         =		'default'
			elif system == "Darwin":
						lowerCAmelCase__      :      Any         =		'avfoundation'
						lowerCAmelCase__      :      Tuple         =		':0'
			elif system == "Windows":
						lowerCAmelCase__      :      Any         =		'dshow'
						lowerCAmelCase__      :      int         =		'default'

			lowerCAmelCase__      :      Any         =		[
			    'ffmpeg',
			    '-f',
			    format_,
			    '-i',
			    input_,
			    '-ac',
			    ac,
			    '-ar',
			    ar,
			    '-f',
			    format_for_conversion,
			    '-fflags',
			    'nobuffer',
			    '-hide_banner',
			    '-loglevel',
			    'quiet',
			    'pipe:1',
			]
			lowerCAmelCase__      :      List[str]         =		int(round(sampling_rate * chunk_length_s     )     ) * size_of_sample
			lowerCAmelCase__      :      str         =		_ffmpeg_stream(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )
			for item in iterator:
						yield item
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_ = None       ,							SCREAMING_SNAKE_CASE_ = None       ,							SCREAMING_SNAKE_CASE_ = "f32le"       ,							)				->		str:
			if stream_chunk_s is not None:
						lowerCAmelCase__      :      Union[str, Any]         =		stream_chunk_s
			else:
						lowerCAmelCase__      :      Tuple         =		chunk_length_s

			lowerCAmelCase__      :      Any         =		ffmpeg_microphone(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							format_for_conversion=SCREAMING_SNAKE_CASE_     )
			if format_for_conversion == "s16le":
						lowerCAmelCase__      :      Optional[Any]         =		np.intaa
						lowerCAmelCase__      :      Optional[Any]         =		2
			elif format_for_conversion == "f32le":
						lowerCAmelCase__      :      Optional[Any]         =		np.floataa
						lowerCAmelCase__      :      Optional[Any]         =		4
			else:
						raise ValueError(F'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`'''     )

			if stride_length_s is None:
						lowerCAmelCase__      :      Dict         =		chunk_length_s / 6
			lowerCAmelCase__      :      int         =		int(round(sampling_rate * chunk_length_s     )     ) * size_of_sample
			if isinstance(SCREAMING_SNAKE_CASE_       ,							(int, float)     ):
						lowerCAmelCase__      :      Dict         =		[stride_length_s, stride_length_s]

			lowerCAmelCase__      :      Union[str, Any]         =		int(round(sampling_rate * stride_length_s[0]     )     ) * size_of_sample
			lowerCAmelCase__      :      List[Any]         =		int(round(sampling_rate * stride_length_s[1]     )     ) * size_of_sample
			lowerCAmelCase__      :      Any         =		datetime.datetime.now()
			lowerCAmelCase__      :      Any         =		datetime.timedelta(seconds=SCREAMING_SNAKE_CASE_     )
			for item in chunk_bytes_iter(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							stride=(stride_left, stride_right)       ,							stream=SCREAMING_SNAKE_CASE_     ):
						# Put everything back in numpy scale
						lowerCAmelCase__      :      Any         =		np.frombuffer(item['raw']       ,							dtype=SCREAMING_SNAKE_CASE_     )
						lowerCAmelCase__      :      Optional[Any]         =		(
						    item['stride'][0] // size_of_sample,
						    item['stride'][1] // size_of_sample,
						)
						lowerCAmelCase__      :      Optional[int]         =		sampling_rate
						audio_time += delta
						if datetime.datetime.now() > audio_time + 10 * delta:
									# We're late !! SKIP
									continue
						yield item
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_ = False     )				->		Optional[int]:
			lowerCAmelCase__      :      Union[str, Any]         =		b''
			lowerCAmelCase__      ,				lowerCAmelCase__      :      Union[str, Any]         =		stride
			if stride_left + stride_right >= chunk_len:
						raise ValueError(
						    F'''Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}'''     )
			lowerCAmelCase__      :      List[str]         =		0
			for raw in iterator:
						acc += raw
						if stream and len(SCREAMING_SNAKE_CASE_     ) < chunk_len:
									lowerCAmelCase__      :      Tuple         =		(_stride_left, 0)
									yield {"raw": acc[:chunk_len], "stride": stride, "partial": True}
						else:
									while len(SCREAMING_SNAKE_CASE_     ) >= chunk_len:
												# We are flushing the accumulator
												lowerCAmelCase__      :      Dict         =		(_stride_left, stride_right)
												lowerCAmelCase__      :      Any         =		{'raw': acc[:chunk_len], 'stride': stride}
												if stream:
															lowerCAmelCase__      :      Optional[int]         =		False
												yield item
												lowerCAmelCase__      :      Optional[int]         =		stride_left
												lowerCAmelCase__      :      Optional[int]         =		acc[chunk_len - stride_left - stride_right :]
    # Last chunk
			if len(SCREAMING_SNAKE_CASE_     ) > stride_left:
						lowerCAmelCase__      :      Tuple         =		{'raw': acc, 'stride': (_stride_left, 0)}
						if stream:
									lowerCAmelCase__      :      Any         =		False
						yield item







def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )				->		str:
			lowerCAmelCase__      :      int         =		2**24  # 16Mo
			try:
						with subprocess.Popen(SCREAMING_SNAKE_CASE_       ,							stdout=subprocess.PIPE       ,							bufsize=SCREAMING_SNAKE_CASE_     ) as ffmpeg_process:
									while True:
												lowerCAmelCase__      :      List[str]         =		ffmpeg_process.stdout.read(SCREAMING_SNAKE_CASE_     )
												if raw == b"":
															break
												yield raw
			except FileNotFoundError as error:
						raise ValueError('ffmpeg was not found but is required to stream audio files from filename'     ) from error