


'''simple docstring'''

import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin





class   snake_case					(					lowercase    ):





  """simple docstring"""




  _lowerCamelCase						=						"MCTCTFeatureExtractor"
  _lowerCamelCase						=						"AutoTokenizer"






  def __init__(					self    ,						UpperCamelCase    ,						UpperCamelCase  ):







   """simple docstring"""
   super().__init__(UpperCamelCase    ,						UpperCamelCase  )
   lowerCamelCase_  				=						self.feature_extractor
   lowerCamelCase_  				=						False






  def __call__(					self    ,						*UpperCamelCase    ,						**UpperCamelCase  ):







   """simple docstring"""
   # For backward compatibility
   if self._in_target_context_manager:
    return self.current_processor(*UpperCamelCase    ,						**UpperCamelCase  )

   if "raw_speech" in kwargs:
    warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."  )
    lowerCamelCase_  				=						kwargs.pop("raw_speech"  )
   else:
    lowerCamelCase_  				=						kwargs.pop("audio"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						kwargs.pop("sampling_rate"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						kwargs.pop("text"    ,						UpperCamelCase  )
   if len(UpperCamelCase  ) > 0:
    lowerCamelCase_  				=						args[0]
    lowerCamelCase_  				=						args[1:]

   if audio is None and text is None:
    raise ValueError("You need to specify either an `audio` or `text` input to process."  )

   if audio is not None:
    lowerCamelCase_  				=						self.feature_extractor(UpperCamelCase    ,						*UpperCamelCase    ,						sampling_rate=UpperCamelCase    ,						**UpperCamelCase  )
   if text is not None:
    lowerCamelCase_  				=						self.tokenizer(UpperCamelCase    ,						**UpperCamelCase  )

   if text is None:
    return inputs
   elif audio is None:
    return encodings
   else:
    lowerCamelCase_  				=						encodings["input_ids"]
    return inputs






  def 						snake_case	(					self    ,						*UpperCamelCase    ,						**UpperCamelCase  ):







   """simple docstring"""
   return self.tokenizer.batch_decode(*UpperCamelCase    ,						**UpperCamelCase  )






  def 						snake_case	(					self    ,						*UpperCamelCase    ,						**UpperCamelCase  ):







   """simple docstring"""
   # For backward compatibility
   if self._in_target_context_manager:
    return self.current_processor.pad(*UpperCamelCase    ,						**UpperCamelCase  )

   lowerCamelCase_  				=						kwargs.pop("input_features"    ,						UpperCamelCase  )
   lowerCamelCase_  				=						kwargs.pop("labels"    ,						UpperCamelCase  )
   if len(UpperCamelCase  ) > 0:
    lowerCamelCase_  				=						args[0]
    lowerCamelCase_  				=						args[1:]

   if input_features is not None:
    lowerCamelCase_  				=						self.feature_extractor.pad(UpperCamelCase    ,						*UpperCamelCase    ,						**UpperCamelCase  )
   if labels is not None:
    lowerCamelCase_  				=						self.tokenizer.pad(UpperCamelCase    ,						**UpperCamelCase  )

   if labels is None:
    return input_features
   elif input_features is None:
    return labels
   else:
    lowerCamelCase_  				=						labels["input_ids"]
    return input_features






  def 						snake_case	(					self    ,						*UpperCamelCase    ,						**UpperCamelCase  ):







   """simple docstring"""
   return self.tokenizer.decode(*UpperCamelCase    ,						**UpperCamelCase  )







  @contextmanager
  def 						snake_case	(					self  ):







   """simple docstring"""
   warnings.warn(
       "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
       "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
       "your audio inputs, or in a separate call."  )
   lowerCamelCase_  				=						True
   lowerCamelCase_  				=						self.tokenizer
   yield
   lowerCamelCase_  				=						self.feature_extractor
   lowerCamelCase_  				=						False







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





  """simple docstring"""




  def 						snake_case	(					self    ,						UpperCamelCase  ):







   """simple docstring"""
   for model_result in results.values():
    for batch_size, sequence_length in zip(model_result["bs"]    ,						model_result["ss"]  ):
     lowerCamelCase_  				=						model_result["result"][batch_size][sequence_length]
     self.assertIsNotNone(UpperCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						eager_mode=UpperCamelCase    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   lowerCamelCase_  				=						benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result  )
   self.check_results_dict_not_empty(results.memory_inference_result  )






  def 						snake_case	(					self  ):







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






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   lowerCamelCase_  				=						benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result  )
   self.check_results_dict_not_empty(results.memory_inference_result  )






  def 						snake_case	(					self  ):







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






  def 						snake_case	(					self  ):







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






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   lowerCamelCase_  				=						benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result  )
   self.check_results_dict_not_empty(results.memory_train_result  )






  def 						snake_case	(					self  ):







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






  def 						snake_case	(					self  ):







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







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						use_xla=UpperCamelCase    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   lowerCamelCase_  				=						benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result  )
   self.check_results_dict_not_empty(results.memory_inference_result  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   with tempfile.TemporaryDirectory() as tmp_dir:
    lowerCamelCase_  				=						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    ,						)
    lowerCamelCase_  				=						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 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"

   def _check_summary_is_not_empty(UpperCamelCase  ):
    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:
    lowerCamelCase_  				=						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    ,						)
    lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
    lowerCamelCase_  				=						benchmark.run()
    _check_summary_is_not_empty(result.inference_summary  )
    self.assertTrue(Path(os.path.join(UpperCamelCase    ,						"log.txt"  )  ).exists()  )



