






"""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 (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    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


__lowerCamelCase :      List[str]										=				logging.get_logger(__name__)


if is_vision_available():
	import PIL



class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase__						):





					"""simple docstring"""






					a_     =		["pixel_values"]



					def __init__( self					:  int							,		__A					:  Dict = True							,		__A					:  Union[str, Any] = None							,		__A					:  Optional[Any] = PILImageResampling.BICUBIC							,		__A					:  int = True							,		__A					:  List[str] = None							,		__A					:  Union[str, Any] = True							,		__A					:  List[str] = 1 / 2_5_5							,		__A					:  Tuple = True							,		__A					:  Optional[Any] = None							,		__A					:  Any = None							,		__A					:  Union[str, Any] = True							,		**__A					:  Tuple							,		):
						super().__init__(**__lowerCamelCase	)
						snake_case__			:				Dict       =  size if size is not None else {"shortest_edge": 2_2_4}
						snake_case__			:				Optional[int]       =  get_size_dict(__lowerCamelCase							,		default_to_square=__lowerCamelCase	)
						snake_case__			:				List[str]       =  crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
						snake_case__			:				Union[str, Any]       =  get_size_dict(__lowerCamelCase							,		default_to_square=__lowerCamelCase							,		param_name="crop_size"	)

						snake_case__			:				str       =  do_resize
						snake_case__			:				int       =  size
						snake_case__			:				int       =  resample
						snake_case__			:				Union[str, Any]       =  do_center_crop
						snake_case__			:				List[str]       =  crop_size
						snake_case__			:				Dict       =  do_rescale
						snake_case__			:				Optional[int]       =  rescale_factor
						snake_case__			:				int       =  do_normalize
						snake_case__			:				Optional[Any]       =  image_mean if image_mean is not None else OPENAI_CLIP_MEAN
						snake_case__			:				Dict       =  image_std if image_std is not None else OPENAI_CLIP_STD
						snake_case__			:				Any       =  do_convert_rgb



					def 	_lowercase					( self					:  List[str]							,		__A					:  Optional[int]							,		__A					:  List[str]							,		__A					:  Dict = PILImageResampling.BICUBIC							,		__A					:  Optional[Any] = None							,		**__A					:  Optional[int]							,		):
						snake_case__			:				int       =  get_size_dict(__lowerCamelCase							,		default_to_square=__lowerCamelCase	)
						if "shortest_edge" not in size:
							raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''	)
						snake_case__			:				Union[str, Any]       =  get_resize_output_image_size(__lowerCamelCase							,		size=size["shortest_edge"]							,		default_to_square=__lowerCamelCase	)
						return resize(__lowerCamelCase							,		size=__lowerCamelCase							,		resample=__lowerCamelCase							,		data_format=__lowerCamelCase							,		**__lowerCamelCase	)



					def 	_lowercase					( self					:  List[Any]							,		__A					:  List[Any]							,		__A					:  List[str]							,		__A					:  Optional[Any] = None							,		**__A					:  Dict							,		):
						snake_case__			:				str       =  get_size_dict(__lowerCamelCase	)
						if "height" not in size or "width" not in size:
							raise ValueError(f'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''	)
						return center_crop(__lowerCamelCase							,		size=(size["height"], size["width"])							,		data_format=__lowerCamelCase							,		**__lowerCamelCase	)



					def 	_lowercase					( self					:  Any							,		__A					:  Any							,		__A					:  Optional[Any]							,		__A					:  Optional[Any] = None							,		**__A					:  Optional[Any]							,		):
						return rescale(__lowerCamelCase							,		scale=__lowerCamelCase							,		data_format=__lowerCamelCase							,		**__lowerCamelCase	)



					def 	_lowercase					( self					:  Optional[int]							,		__A					:  int							,		__A					:  Union[str, Any]							,		__A					:  Optional[Any]							,		__A					:  str = None							,		**__A					:  Tuple							,		):
						return normalize(__lowerCamelCase							,		mean=__lowerCamelCase							,		std=__lowerCamelCase							,		data_format=__lowerCamelCase							,		**__lowerCamelCase	)






					def 	_lowercase					( self					:  Any							,		__A					:  Tuple							,		__A					:  Any = None							,		__A					:  Union[str, Any] = None							,		__A					:  List[Any] = None							,		__A					:  Dict = None							,		__A					:  Any = None							,		__A					:  int = None							,		__A					:  Union[str, Any] = None							,		__A					:  Union[str, Any] = None							,		__A					:  Tuple = None							,		__A					:  List[Any] = None							,		__A					:  Union[str, Any] = None							,		__A					:  Tuple = None							,		__A					:  Any = ChannelDimension.FIRST							,		**__A					:  int							,		):
						snake_case__			:				Tuple       =  do_resize if do_resize is not None else self.do_resize
						snake_case__			:				int       =  size if size is not None else self.size
						snake_case__			:				Any       =  get_size_dict(__lowerCamelCase							,		param_name="size"							,		default_to_square=__lowerCamelCase	)
						snake_case__			:				Optional[Any]       =  resample if resample is not None else self.resample
						snake_case__			:				List[Any]       =  do_center_crop if do_center_crop is not None else self.do_center_crop
						snake_case__			:				int       =  crop_size if crop_size is not None else self.crop_size
						snake_case__			:				int       =  get_size_dict(__lowerCamelCase							,		param_name="crop_size"							,		default_to_square=__lowerCamelCase	)
						snake_case__			:				Dict       =  do_rescale if do_rescale is not None else self.do_rescale
						snake_case__			:				List[Any]       =  rescale_factor if rescale_factor is not None else self.rescale_factor
						snake_case__			:				Optional[int]       =  do_normalize if do_normalize is not None else self.do_normalize
						snake_case__			:				Optional[int]       =  image_mean if image_mean is not None else self.image_mean
						snake_case__			:				Dict       =  image_std if image_std is not None else self.image_std
						snake_case__			:				List[str]       =  do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

						snake_case__			:				Any       =  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:
							raise ValueError("Size must be specified if do_resize is True."	)

						if do_center_crop and crop_size is None:
							raise ValueError("Crop size must be specified if do_center_crop 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:
							snake_case__			:				int       =  [convert_to_rgb(__lowerCamelCase	) for image in images]

						# All transformations expect numpy arrays.
						snake_case__			:				str       =  [to_numpy_array(__lowerCamelCase	) for image in images]

						if do_resize:
							snake_case__			:				List[Any]       =  [self.resize(image=__lowerCamelCase							,		size=__lowerCamelCase							,		resample=__lowerCamelCase	) for image in images]

						if do_center_crop:
							snake_case__			:				int       =  [self.center_crop(image=__lowerCamelCase							,		size=__lowerCamelCase	) for image in images]

						if do_rescale:
							snake_case__			:				int       =  [self.rescale(image=__lowerCamelCase							,		scale=__lowerCamelCase	) for image in images]

						if do_normalize:
							snake_case__			:				Tuple       =  [self.normalize(image=__lowerCamelCase							,		mean=__lowerCamelCase							,		std=__lowerCamelCase	) for image in images]

						snake_case__			:				Union[str, Any]       =  [to_channel_dimension_format(__lowerCamelCase							,		__lowerCamelCase	) for image in images]

						snake_case__			:				List[str]       =  {"pixel_values": images}
						return BatchFeature(data=__lowerCamelCase							,		tensor_type=__lowerCamelCase	)











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

__lowerCamelCase :      List[Any]										=				logging.get_logger(__name__)







def 					SCREAMING_SNAKE_CASE    (			snake_case_ :							bool					,  snake_case_ :							bool       ):
	def run_func(snake_case_ :							str       ):
		@wraps(snake_case_       )
		def run_in_eager_mode(*snake_case_ :							str					,  **snake_case_ :							Union[str, Any]       ):
			return func(*snake_case_					,  **snake_case_       )

		@wraps(snake_case_       )
		@tf.function(experimental_compile=snake_case_       )
		def run_in_graph_mode(*snake_case_ :							List[Any]					,  **snake_case_ :							List[Any]       ):
			return func(*snake_case_					,  **snake_case_       )

		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    (			snake_case_ :							int					,  snake_case_ :							int					,  snake_case_ :							int       ):
	snake_case__			:				Dict       =  random.Random()
	snake_case__			:				List[Any]       =  [rng.randint(0					,  vocab_size - 1       ) for i in range(batch_size * sequence_length       )]
	return tf.constant(snake_case_					,  shape=(batch_size, sequence_length)					,  dtype=tf.intaa       )





class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_						):





					"""simple docstring"""






					a_     =		42
					a_     =		42
					a_     =		"TensorFlow"



					@property
					def 	_lowercase					( self					:  List[str]	):
						return tf.__version__



					def 	_lowercase					( self					:  List[str]							,		__A					:  str							,		__A					:  int							,		__A					:  int	):
						# initialize GPU on separate process
						snake_case__			:				str       =  self.args.strategy
						if strategy is None:
							raise ValueError("A device strategy has to be initialized before using TensorFlow."	)
						snake_case__			:				Dict       =  self._prepare_inference_func(__A							,		__A							,		__A	)
						return self._measure_speed(_inference	)



					def 	_lowercase					( self					:  Tuple							,		__A					:  str							,		__A					:  int							,		__A					:  int	):
						snake_case__			:				Optional[int]       =  self.args.strategy
						if strategy is None:
							raise ValueError("A device strategy has to be initialized before using TensorFlow."	)
						snake_case__			:				Any       =  self._prepare_train_func(__A							,		__A							,		__A	)
						return self._measure_speed(_train	)



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



					def 	_lowercase					( self					:  str							,		__A					:  str							,		__A					:  int							,		__A					:  int	):
						if self.args.is_gpu:
							tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]							,		__A	)
						snake_case__			:				List[Any]       =  self.args.strategy
						if strategy is None:
							raise ValueError("A device strategy has to be initialized before using TensorFlow."	)

						snake_case__			:				int       =  self._prepare_train_func(__A							,		__A							,		__A	)
						return self._measure_memory(_train	)



					def 	_lowercase					( self					:  Union[str, Any]							,		__A					:  str							,		__A					:  int							,		__A					:  int	):
						snake_case__			:				int       =  self.config_dict[model_name]

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

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

						# encoder-decoder has vocab size saved differently
						snake_case__			:				Optional[int]       =  config.vocab_size if hasattr(__A							,		"vocab_size"	) else config.encoder.vocab_size
						snake_case__			:				List[Any]       =  random_input_ids(__A							,		__A							,		__A	)

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

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

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

						return _inference



					def 	_lowercase					( self					:  List[str]							,		__A					:  str							,		__A					:  int							,		__A					:  int	):
						snake_case__			:				Optional[Any]       =  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."	)

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

						# encoder-decoder has vocab size saved differently
						snake_case__			:				Union[str, Any]       =  config.vocab_size if hasattr(__A							,		"vocab_size"	) else config.encoder.vocab_size
						snake_case__			:				List[str]       =  random_input_ids(__A							,		__A							,		__A	)

						@run_with_tf_optimizations(self.args.eager_mode							,		self.args.use_xla	)
						def encoder_decoder_train():
							snake_case__			:				str       =  model(__A							,		decoder_input_ids=__A							,		labels=__A							,		training=__A	)[0]
							snake_case__			:				Dict       =  tf.gradients(__A							,		model.trainable_variables	)
							return gradients

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

						snake_case__			:				int       =  encoder_decoder_train if config.is_encoder_decoder else encoder_train

						return _train



					def 	_lowercase					( self					:  int							,		__A					:  List[Any]	):
						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(__A							,		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
								snake_case__			:				Optional[Any]       =  timeit.repeat(
								    __A							,		repeat=self.args.repeat							,		number=1_0							,		)

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






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

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



