

'''simple docstring'''






import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_torch_available
from transformers.testing_utils import require_torch, torch_device


if is_torch_available():
 from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments






@require_torch
class 			lowercase_	(unittest.TestCase      ):




  """simple docstring"""




  def 					SCREAMING_SNAKE_CASE      (      self   :     int					,lowercase__   :     List[Any]			):
   for model_result in results.values():
    for batch_size, sequence_length in zip(model_result['''bs''']					,model_result['''ss''']			):
     __lowercase							  = model_result['''result'''][batch_size][sequence_length]
     self.assertIsNotNone(lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Dict			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__			)
   __lowercase							  = 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[Any]			):
   __lowercase							  = '''sgugger/tiny-distilbert-classification'''
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,only_pretrain_model=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__			)
   __lowercase							  = 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]			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,torchscript=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__			)
   __lowercase							  = benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result			)
   self.check_results_dict_not_empty(results.memory_inference_result			)






  @unittest.skipIf(torch_device == '''cpu'''					,'''Cant do half precision'''			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,fpaa=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__			)
   __lowercase							  = 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]			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = AutoConfig.from_pretrained(lowercase__			)
   # set architectures equal to `None`
   __lowercase							  = None
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__					,configs=[config]			)
   __lowercase							  = 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   :     int			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__			)
   __lowercase							  = benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result			)
   self.check_results_dict_not_empty(results.memory_train_result			)






  @unittest.skipIf(torch_device == '''cpu'''					,'''Can\'t do half precision'''			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,fpaa=lowercase__					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__			)
   __lowercase							  = 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   :     Tuple			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = AutoConfig.from_pretrained(lowercase__			)
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__					,configs=[config]			)
   __lowercase							  = 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[Any]			):
   __lowercase							  = '''sshleifer/tinier_bart'''
   __lowercase							  = AutoConfig.from_pretrained(lowercase__			)
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__					,configs=[config]			)
   __lowercase							  = 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   :     Dict			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   __lowercase							  = AutoConfig.from_pretrained(lowercase__			)
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__					,configs=[config]			)
   __lowercase							  = 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   :     Optional[Any]			):
   __lowercase							  = '''sshleifer/tinier_bart'''
   __lowercase							  = AutoConfig.from_pretrained(lowercase__			)
   __lowercase							  = PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,multi_process=lowercase__					,)
   __lowercase							  = PyTorchBenchmark(lowercase__					,configs=[config]			)
   __lowercase							  = 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   :     Optional[int]			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''
   with tempfile.TemporaryDirectory() as tmp_dir:
    __lowercase							  = PyTorchBenchmarkArguments(
        models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,save_to_csv=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,inference_time_csv_file=os.path.join(lowercase__					,'''inf_time.csv'''			)					,train_memory_csv_file=os.path.join(lowercase__					,'''train_mem.csv'''			)					,inference_memory_csv_file=os.path.join(lowercase__					,'''inf_mem.csv'''			)					,train_time_csv_file=os.path.join(lowercase__					,'''train_time.csv'''			)					,env_info_csv_file=os.path.join(lowercase__					,'''env.csv'''			)					,multi_process=lowercase__					,)
    __lowercase							  = PyTorchBenchmark(lowercase__			)
    benchmark.run()
    self.assertTrue(Path(os.path.join(lowercase__					,'''inf_time.csv'''			)			).exists()			)
    self.assertTrue(Path(os.path.join(lowercase__					,'''train_time.csv'''			)			).exists()			)
    self.assertTrue(Path(os.path.join(lowercase__					,'''inf_mem.csv'''			)			).exists()			)
    self.assertTrue(Path(os.path.join(lowercase__					,'''train_mem.csv'''			)			).exists()			)
    self.assertTrue(Path(os.path.join(lowercase__					,'''env.csv'''			)			).exists()			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   __lowercase							  = '''sshleifer/tiny-gpt2'''

   def _check_summary_is_not_empty(lowercase__   :     List[Any]			):
    self.assertTrue(hasattr(lowercase__					,'''sequential'''			)			)
    self.assertTrue(hasattr(lowercase__					,'''cumulative'''			)			)
    self.assertTrue(hasattr(lowercase__					,'''current'''			)			)
    self.assertTrue(hasattr(lowercase__					,'''total'''			)			)

   with tempfile.TemporaryDirectory() as tmp_dir:
    __lowercase							  = PyTorchBenchmarkArguments(
        models=[MODEL_ID]					,training=lowercase__					,inference=lowercase__					,sequence_lengths=[8]					,batch_sizes=[1]					,log_filename=os.path.join(lowercase__					,'''log.txt'''			)					,log_print=lowercase__					,trace_memory_line_by_line=lowercase__					,multi_process=lowercase__					,)
    __lowercase							  = PyTorchBenchmark(lowercase__			)
    __lowercase							  = benchmark.run()
    _check_summary_is_not_empty(result.inference_summary			)
    _check_summary_is_not_empty(result.train_summary			)
    self.assertTrue(Path(os.path.join(lowercase__					,'''log.txt'''			)			).exists()			)




'''simple docstring'''






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class 			lowercase_	(lowerCamelCase__      ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE    :  Optional[Any]							=			['image_processor', 'tokenizer']
  SCREAMING_SNAKE_CASE    :  Union[str, Any]							=			'CLIPImageProcessor'
  SCREAMING_SNAKE_CASE    :  Union[str, Any]							=			('CLIPTokenizer', 'CLIPTokenizerFast')






  def __init__(      self   :     List[Any]					,lowercase__   :     Dict=None					,lowercase__   :     Union[str, Any]=None					,**lowercase__   :     Tuple			):
   __lowercase							  = None
   if "feature_extractor" in kwargs:
    warnings.warn(
        '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
        ''' instead.'''					,lowercase__					,)
    __lowercase							  = kwargs.pop('''feature_extractor'''			)

   __lowercase							  = image_processor if image_processor is not None else feature_extractor
   if image_processor is None:
    raise ValueError('''You need to specify an `image_processor`.'''			)
   if tokenizer is None:
    raise ValueError('''You need to specify a `tokenizer`.'''			)

   super().__init__(lowercase__					,lowercase__			)






  def __call__(      self   :     List[Any]					,lowercase__   :     str=None					,lowercase__   :     List[Any]=None					,lowercase__   :     Optional[Any]=None					,**lowercase__   :     int			):

   if text is None and images is None:
    raise ValueError('''You have to specify either text or images. Both cannot be none.'''			)

   if text is not None:
    __lowercase							  = self.tokenizer(lowercase__					,return_tensors=lowercase__					,**lowercase__			)

   if images is not None:
    __lowercase							  = self.image_processor(lowercase__					,return_tensors=lowercase__					,**lowercase__			)

   if text is not None and images is not None:
    __lowercase							  = image_features.pixel_values
    return encoding
   elif text is not None:
    return encoding
   else:
    return BatchEncoding(data=dict(**lowercase__			)					,tensor_type=lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]					,*lowercase__   :     List[str]					,**lowercase__   :     int			):
   return self.tokenizer.batch_decode(*lowercase__					,**lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[Any]					,*lowercase__   :     Optional[int]					,**lowercase__   :     Union[str, Any]			):
   return self.tokenizer.decode(*lowercase__					,**lowercase__			)






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   __lowercase							  = self.tokenizer.model_input_names
   __lowercase							  = self.image_processor.model_input_names
   return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names			)			)






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     List[str]			):
   warnings.warn(
       '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''					,lowercase__					,)
   return self.image_processor_class






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   warnings.warn(
       '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''					,lowercase__					,)
   return self.image_processor

