# 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 		_lowercase						(					snake_case_       ):
 lowercase				     =   'microsoft/speecht5_tts'
 lowercase				     =   (
     '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.'
 )
 lowercase				     =   'text_reader'
 lowercase				     =   SpeechTaProcessor
 lowercase				     =   SpeechTaForTextToSpeech
 lowercase				     =   SpeechTaHifiGan

 lowercase				     =   ['text']
 lowercase				     =   ['audio']

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Union[str, Any]			)   ->						int:

  """simple docstring"""





  if self.post_processor is None:
   UpperCamelCase_					:						Dict =					'microsoft/speecht5_hifigan'
  super().setup()

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Dict   ,      snake_case	:							int   ,      snake_case	:							int=None			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						Dict =					self.pre_processor(text=snake_case   ,      return_tensors='pt'   ,      truncation=snake_case			)

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

   UpperCamelCase_					:						str =					load_dataset('Matthijs/cmu-arctic-xvectors'   ,      split='validation'			)
   UpperCamelCase_					:						int =					torch.tensor(embeddings_dataset[7_3_0_5]['xvector']			).unsqueeze(0			)

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

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Tuple   ,      snake_case	:							List[str]			)   ->						Any:

  """simple docstring"""





  with torch.no_grad():
   return self.model.generate_speech(**snake_case			)


 def 						SCREAMING_SNAKE_CASE__   ( self	:							str   ,      snake_case	:							Optional[int]			)   ->						Union[str, Any]:

  """simple docstring"""





  with torch.no_grad():
   return self.post_processor(snake_case			).cpu().detach()




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 						SCREAMING_SNAKE_CASE__   ( self	:							int   ,      snake_case	:							Union[str, Any]			)   ->						Optional[int]:

  """simple docstring"""





  for model_result in results.values():
   for batch_size, sequence_length in zip(model_result['bs']   ,      model_result['ss']			):
    UpperCamelCase_					:						Optional[Any] =					model_result['result'][batch_size][sequence_length]
    self.assertIsNotNone(snake_case			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							int			)   ->						Tuple:

  """simple docstring"""





  UpperCamelCase_					:						Optional[Any] =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						Dict =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      eager_mode=snake_case   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						Tuple =					TensorFlowBenchmark(snake_case			)
  UpperCamelCase_					:						List[str] =					benchmark.run()
  self.check_results_dict_not_empty(results.time_inference_result			)
  self.check_results_dict_not_empty(results.memory_inference_result			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							str			)   ->						Optional[Any]:

  """simple docstring"""





  UpperCamelCase_					:						Dict =					'sgugger/tiny-distilbert-classification'
  UpperCamelCase_					:						int =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      multi_process=snake_case   ,      only_pretrain_model=snake_case   ,      )
  UpperCamelCase_					:						int =					TensorFlowBenchmark(snake_case			)
  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 						SCREAMING_SNAKE_CASE__   ( self	:							Tuple			)   ->						List[str]:

  """simple docstring"""





  UpperCamelCase_					:						str =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						Any =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						List[str] =					TensorFlowBenchmark(snake_case			)
  UpperCamelCase_					:						Optional[int] =					benchmark.run()
  self.check_results_dict_not_empty(results.time_inference_result			)
  self.check_results_dict_not_empty(results.memory_inference_result			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Tuple			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						List[str] =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						str =					AutoConfig.from_pretrained(snake_case			)
  UpperCamelCase_					:						List[str] =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      eager_mode=snake_case   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						int =					TensorFlowBenchmark(snake_case   ,      [config]			)
  UpperCamelCase_					:						Optional[Any] =					benchmark.run()
  self.check_results_dict_not_empty(results.time_inference_result			)
  self.check_results_dict_not_empty(results.memory_inference_result			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[Any]			)   ->						List[str]:

  """simple docstring"""





  UpperCamelCase_					:						List[str] =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						int =					AutoConfig.from_pretrained(snake_case			)
  UpperCamelCase_					:						Any =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						Tuple =					TensorFlowBenchmark(snake_case   ,      [config]			)
  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 						SCREAMING_SNAKE_CASE__   ( self	:							Tuple			)   ->						int:

  """simple docstring"""





  UpperCamelCase_					:						Optional[int] =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						Tuple =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						Optional[int] =					TensorFlowBenchmark(snake_case			)
  UpperCamelCase_					:						str =					benchmark.run()
  self.check_results_dict_not_empty(results.time_train_result			)
  self.check_results_dict_not_empty(results.memory_train_result			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Any			)   ->						int:

  """simple docstring"""





  UpperCamelCase_					:						Tuple =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						List[str] =					AutoConfig.from_pretrained(snake_case			)
  UpperCamelCase_					:						int =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						List[Any] =					TensorFlowBenchmark(snake_case   ,      [config]			)
  UpperCamelCase_					:						int =					benchmark.run()
  self.check_results_dict_not_empty(results.time_train_result			)
  self.check_results_dict_not_empty(results.memory_train_result			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Union[str, Any]			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						Tuple =					'patrickvonplaten/t5-tiny-random'
  UpperCamelCase_					:						List[str] =					AutoConfig.from_pretrained(snake_case			)
  UpperCamelCase_					:						List[str] =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						int =					TensorFlowBenchmark(snake_case   ,      configs=[config]			)
  UpperCamelCase_					:						Optional[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 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[int]			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						int =					'sshleifer/tiny-gpt2'
  UpperCamelCase_					:						Union[str, Any] =					TensorFlowBenchmarkArguments(
      models=[MODEL_ID]   ,      training=snake_case   ,      inference=snake_case   ,      sequence_lengths=[8]   ,      batch_sizes=[1]   ,      use_xla=snake_case   ,      multi_process=snake_case   ,      )
  UpperCamelCase_					:						Union[str, Any] =					TensorFlowBenchmark(snake_case			)
  UpperCamelCase_					:						List[str] =					benchmark.run()
  self.check_results_dict_not_empty(results.time_inference_result			)
  self.check_results_dict_not_empty(results.memory_inference_result			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[str]			)   ->						Optional[int]:

  """simple docstring"""





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


 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[str]			)   ->						str:

  """simple docstring"""





  UpperCamelCase_					:						Optional[Any] =					'sshleifer/tiny-gpt2'

  def _check_summary_is_not_empty(snake_case	:							Union[str, Any]			):
   self.assertTrue(hasattr(snake_case   ,      'sequential'			)			)
   self.assertTrue(hasattr(snake_case   ,      'cumulative'			)			)
   self.assertTrue(hasattr(snake_case   ,      'current'			)			)
   self.assertTrue(hasattr(snake_case   ,      'total'			)			)

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



