
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

_SCREAMING_SNAKE_CASE    :      Any          =			logging.get_logger(__name__)

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						bool							,						__UpperCamelCase :						bool       ) ->       Optional[Any]:

				"""simple docstring"""







				def run_func(__UpperCamelCase :						Optional[int]       ):
								@wraps(__UpperCamelCase       )
								def run_in_eager_mode(*__UpperCamelCase :						Tuple							,						**__UpperCamelCase :						Tuple       ):
												return func(*__UpperCamelCase							,						**__UpperCamelCase       )

								@wraps(__UpperCamelCase       )
								@tf.function(experimental_compile=__UpperCamelCase       )
								def run_in_graph_mode(*__UpperCamelCase :						Optional[Any]							,						**__UpperCamelCase :						Optional[int]       ):
												return func(*__UpperCamelCase							,						**__UpperCamelCase       )

								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 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						int							,						__UpperCamelCase :						int							,						__UpperCamelCase :						int       ) ->       ["tf.Tensor"]:

				"""simple docstring"""







				A__  :	Any         =							random.Random()
				A__  :	List[Any]         =							[rng.randint(0							,						vocab_size - 1       ) for i in range(batch_size * sequence_length       )]
				return tf.constant(__UpperCamelCase							,						shape=(batch_size, sequence_length)							,						dtype=tf.intaa       )







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				42
	_lowerCAmelCase							       =				42
	_lowerCAmelCase							       =				"TensorFlow"





	@property
	def   __snake_case				(			self		):
					return tf.__version__





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					# initialize GPU on separate process
					A__  :	Optional[int]         =							self.args.strategy
					if strategy is None:
									raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''		)
					A__  :	str         =							self._prepare_inference_func(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)
					return self._measure_speed(_inference		)





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





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					# initialize GPU on separate process
					if self.args.is_gpu:
									tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]				,       UpperCamelCase__		)
					A__  :	int         =							self.args.strategy
					if strategy is None:
									raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''		)
					A__  :	List[Any]         =							self._prepare_inference_func(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)
					return self._measure_memory(_inference		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					if self.args.is_gpu:
									tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]				,       UpperCamelCase__		)
					A__  :	List[Any]         =							self.args.strategy
					if strategy is None:
									raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''		)

					A__  :	str         =							self._prepare_train_func(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)
					return self._measure_memory(_train		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	Union[str, Any]         =							self.config_dict[model_name]

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

					A__  :	Union[str, Any]         =							(
					    hasattr(UpperCamelCase__				,       '''architectures'''		)
					    and isinstance(config.architectures				,       UpperCamelCase__		)
					    and len(config.architectures		) > 0
					)
					if not self.args.only_pretrain_model and has_model_class_in_config:
									try:
													A__  :	Optional[int]         =							'''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
													A__  :	int         =							__import__('''transformers'''				,       fromlist=[model_class]		)
													A__  :	List[Any]         =							getattr(UpperCamelCase__				,       UpperCamelCase__		)
													A__  :	Any         =							model_cls(UpperCamelCase__		)
									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:
									A__  :	Tuple         =							TF_MODEL_MAPPING[config.__class__](UpperCamelCase__		)

					# encoder-decoder has vocab size saved differently
					A__  :	List[str]         =							config.vocab_size if hasattr(UpperCamelCase__				,       '''vocab_size'''		) else config.encoder.vocab_size
					A__  :	List[str]         =							random_input_ids(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)

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

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

					A__  :	Optional[int]         =							encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

					return _inference





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	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.'''		)

					A__  :	Any         =							(
					    hasattr(UpperCamelCase__				,       '''architectures'''		)
					    and isinstance(config.architectures				,       UpperCamelCase__		)
					    and len(config.architectures		) > 0
					)
					if not self.args.only_pretrain_model and has_model_class_in_config:
									try:
													A__  :	List[Any]         =							'''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
													A__  :	Tuple         =							__import__('''transformers'''				,       fromlist=[model_class]		)
													A__  :	str         =							getattr(UpperCamelCase__				,       UpperCamelCase__		)
													A__  :	Optional[int]         =							model_cls(UpperCamelCase__		)
									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:
									A__  :	Union[str, Any]         =							TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](UpperCamelCase__		)

					# encoder-decoder has vocab size saved differently
					A__  :	Tuple         =							config.vocab_size if hasattr(UpperCamelCase__				,       '''vocab_size'''		) else config.encoder.vocab_size
					A__  :	str         =							random_input_ids(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)

					@run_with_tf_optimizations(self.args.eager_mode				,       self.args.use_xla		)
					def encoder_decoder_train():
									A__  :	Union[str, Any]         =							model(UpperCamelCase__				,       decoder_input_ids=UpperCamelCase__				,       labels=UpperCamelCase__				,       training=UpperCamelCase__		)[0]
									A__  :	Union[str, Any]         =							tf.gradients(UpperCamelCase__				,       model.trainable_variables		)
									return gradients

					@run_with_tf_optimizations(self.args.eager_mode				,       self.args.use_xla		)
					def encoder_train():
									A__  :	List[Any]         =							model(UpperCamelCase__				,       labels=UpperCamelCase__				,       training=UpperCamelCase__		)[0]
									A__  :	Dict         =							tf.gradients(UpperCamelCase__				,       model.trainable_variables		)
									return gradients

					A__  :	Optional[Any]         =							encoder_decoder_train if config.is_encoder_decoder else encoder_train

					return _train





	def   __snake_case				(			self				,       UpperCamelCase__		):
					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(UpperCamelCase__				,       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
													A__  :	Dict         =							timeit.repeat(
													    UpperCamelCase__				,       repeat=self.args.repeat				,       number=10				,       )

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





	def   __snake_case				(			self				,       UpperCamelCase__		):
					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.'''		)
																	A__  :	Optional[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.'''		)
																					A__  :	int         =							'''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()
																					A__  :	List[str]         =							nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx		)
																					A__  :	str         =							nvml.nvmlDeviceGetMemoryInfo(UpperCamelCase__		)
																					A__  :	Any         =							meminfo.used
																					A__  :	int         =							Memory(UpperCamelCase__		)
																					# 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.'''		)
																					A__  :	Any         =							None
																	else:
																					A__  :	Optional[int]         =							measure_peak_memory_cpu(UpperCamelCase__		)
																					A__  :	Dict         =							Memory(UpperCamelCase__		) if isinstance(UpperCamelCase__				,       UpperCamelCase__		) else memory_bytes
													if self.args.trace_memory_line_by_line:
																	A__  :	str         =							stop_memory_tracing(UpperCamelCase__		)
																	if memory is None:
																					A__  :	Tuple         =							summary.total
													else:
																	A__  :	Optional[int]         =							None

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

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor
from ..utils import is_datasets_available
from .base import PipelineTool


if is_datasets_available():
					from datasets import load_dataset







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				"microsoft/speecht5_tts"
	_lowerCAmelCase							       =				(
	    "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the "
	    "text to read (in English) and returns a waveform object containing the sound."
	)
	_lowerCAmelCase							       =				"text_reader"
	_lowerCAmelCase							       =				SpeechTaProcessor
	_lowerCAmelCase							       =				SpeechTaForTextToSpeech
	_lowerCAmelCase							       =				SpeechTaHifiGan

	_lowerCAmelCase							       =				["text"]
	_lowerCAmelCase							       =				["audio"]





	def   __snake_case				(			self		):
					if self.post_processor is None:
									A__  :	int         =							'''microsoft/speecht5_hifigan'''
					super().setup()





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__=None		):
					A__  :	List[Any]         =							self.pre_processor(text=UpperCamelCase__				,       return_tensors='''pt'''				,       truncation=UpperCamelCase__		)

					if speaker_embeddings is None:
									if not is_datasets_available():
													raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.'''		)

									A__  :	List[Any]         =							load_dataset('''Matthijs/cmu-arctic-xvectors'''				,       split='''validation'''		)
									A__  :	Dict         =							torch.tensor(embeddings_dataset[7305]['''xvector''']		).unsqueeze(0		)

					return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}





	def   __snake_case				(			self				,       UpperCamelCase__		):
					with torch.no_grad():
									return self.model.generate_speech(**UpperCamelCase__		)





	def   __snake_case				(			self				,       UpperCamelCase__		):
					with torch.no_grad():
									return self.post_processor(UpperCamelCase__		).cpu().detach()