






'''simple docstring'''







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
							import PIL


UpperCamelCase    :      Any  			=		logging.get_logger(__name__)




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					['pixel_values']






		def __init__(      self   ,_lowerCAmelCase = True   ,_lowerCAmelCase = None   ,_lowerCAmelCase = PILImageResampling.BICUBIC   ,_lowerCAmelCase = True   ,_lowerCAmelCase = 1 / 2_55   ,_lowerCAmelCase = True   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = True   ,**_lowerCAmelCase   ,):
								super().__init__(**_lowerCAmelCase       )
								lowerCamelCase__         =  size if size is not None else {"""height""": 3_84, """width""": 3_84}
								lowerCamelCase__         =  get_size_dict(_lowerCAmelCase   ,default_to_square=_lowerCAmelCase       )

								lowerCamelCase__         =  do_resize
								lowerCamelCase__         =  size
								lowerCamelCase__         =  resample
								lowerCamelCase__         =  do_rescale
								lowerCamelCase__         =  rescale_factor
								lowerCamelCase__         =  do_normalize
								lowerCamelCase__         =  image_mean if image_mean is not None else OPENAI_CLIP_MEAN
								lowerCamelCase__         =  image_std if image_std is not None else OPENAI_CLIP_STD
								lowerCamelCase__         =  do_convert_rgb






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase = PILImageResampling.BICUBIC   ,_lowerCAmelCase = None   ,**_lowerCAmelCase   ,):
								lowerCamelCase__         =  get_size_dict(_lowerCAmelCase   ,default_to_square=_lowerCAmelCase       )
								if "height" not in size or "width" not in size:
														raise ValueError(F'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'''       )
								lowerCamelCase__         =  (size["""height"""], size["""width"""])
								return resize(_lowerCAmelCase   ,size=_lowerCAmelCase   ,resample=_lowerCAmelCase   ,data_format=_lowerCAmelCase   ,**_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase = None   ,**_lowerCAmelCase   ,):
								return rescale(_lowerCAmelCase   ,scale=_lowerCAmelCase   ,data_format=_lowerCAmelCase   ,**_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase = None   ,**_lowerCAmelCase   ,):
								return normalize(_lowerCAmelCase   ,mean=_lowerCAmelCase   ,std=_lowerCAmelCase   ,data_format=_lowerCAmelCase   ,**_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = ChannelDimension.FIRST   ,**_lowerCAmelCase   ,):
								lowerCamelCase__         =  do_resize if do_resize is not None else self.do_resize
								lowerCamelCase__         =  resample if resample is not None else self.resample
								lowerCamelCase__         =  do_rescale if do_rescale is not None else self.do_rescale
								lowerCamelCase__         =  rescale_factor if rescale_factor is not None else self.rescale_factor
								lowerCamelCase__         =  do_normalize if do_normalize is not None else self.do_normalize
								lowerCamelCase__         =  image_mean if image_mean is not None else self.image_mean
								lowerCamelCase__         =  image_std if image_std is not None else self.image_std
								lowerCamelCase__         =  do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

								lowerCamelCase__         =  size if size is not None else self.size
								lowerCamelCase__         =  get_size_dict(_lowerCAmelCase   ,default_to_square=_lowerCAmelCase       )

								lowerCamelCase__         =  make_list_of_images(_lowerCAmelCase       )

								if not valid_images(_lowerCAmelCase       ):
														raise ValueError(
														    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
														    """torch.Tensor, tf.Tensor or jax.ndarray."""       )

								if do_resize and size is None or resample is None:
														raise ValueError("""Size and resample must be specified if do_resize is True."""       )

								if do_rescale and rescale_factor is None:
														raise ValueError("""Rescale factor must be specified if do_rescale is True."""       )

								if do_normalize and (image_mean is None or image_std is None):
														raise ValueError("""Image mean and std must be specified if do_normalize is True."""       )

								# PIL RGBA images are converted to RGB
								if do_convert_rgb:
														lowerCamelCase__         =  [convert_to_rgb(_lowerCAmelCase       ) for image in images]

								# All transformations expect numpy arrays.
								lowerCamelCase__         =  [to_numpy_array(_lowerCAmelCase       ) for image in images]

								if do_resize:
														lowerCamelCase__         =  [self.resize(image=_lowerCAmelCase   ,size=_lowerCAmelCase   ,resample=_lowerCAmelCase       ) for image in images]

								if do_rescale:
														lowerCamelCase__         =  [self.rescale(image=_lowerCAmelCase   ,scale=_lowerCAmelCase       ) for image in images]

								if do_normalize:
														lowerCamelCase__         =  [self.normalize(image=_lowerCAmelCase   ,mean=_lowerCAmelCase   ,std=_lowerCAmelCase       ) for image in images]

								lowerCamelCase__         =  [to_channel_dimension_format(_lowerCAmelCase   ,_lowerCAmelCase       ) for image in images]

								lowerCamelCase__         =  BatchFeature(data={"""pixel_values""": images}   ,tensor_type=_lowerCAmelCase       )

								return encoded_outputs









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




		'''simple docstring'''





		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								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(_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,eager_mode=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result       )
								self.check_results_dict_not_empty(results.memory_inference_result       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sgugger/tiny-distilbert-classification"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,only_pretrain_model=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result       )
								self.check_results_dict_not_empty(results.memory_inference_result       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result       )
								self.check_results_dict_not_empty(results.memory_inference_result       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,eager_mode=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result       )
								self.check_results_dict_not_empty(results.memory_train_result       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """patrickvonplaten/t5-tiny-random"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,use_xla=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result       )
								self.check_results_dict_not_empty(results.memory_inference_result       )






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






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""

								def _check_summary_is_not_empty(_lowerCAmelCase       ):
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""sequential"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""cumulative"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""current"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""total"""       )       )

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

