






'''simple docstring'''

from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
							import tensorflow as tf


lowercase_     		= logging.get_logger(__name__)
@dataclass
class   SCREAMING_SNAKE_CASE			(						__lowerCamelCase):







		"""simple docstring"""
		lowercase   :      str       =							[
		    'no_inference',
		    'no_cuda',
		    'no_tpu',
		    'no_speed',
		    'no_memory',
		    'no_env_print',
		    'no_multi_process',
		]






		def __init__(   self		,						**__A				) ->     Tuple:
								for deprecated_arg in self.deprecated_args:
														if deprecated_arg in kwargs:
																				_lowerCAmelCase										=deprecated_arg[3:]
																				_lowerCAmelCase										=not kwargs.pop(UpperCAmelCase_				)
																				logger.warning(
																				    F'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or'''
																				    F''' {positive_arg}={kwargs[positive_arg]}'''				)
								_lowerCAmelCase										=kwargs.pop('tpu_name'		,						self.tpu_name				)
								_lowerCAmelCase										=kwargs.pop('device_idx'		,						self.device_idx				)
								_lowerCAmelCase										=kwargs.pop('eager_mode'		,						self.eager_mode				)
								_lowerCAmelCase										=kwargs.pop('use_xla'		,						self.use_xla				)
								super().__init__(**UpperCAmelCase_				)

		lowercase   :      List[Any]       =							field(
		    default=__lowerCamelCase    ,			metadata={'help': 'Name of TPU'}    ,			)
		lowercase   :      Any       =							field(
		    default=0    ,			metadata={'help': 'CPU / GPU device index. Defaults to 0.'}    ,			)
		lowercase   :      Dict       =							field(default=__lowerCamelCase    ,			metadata={'help': 'Benchmark models in eager model.'})
		lowercase   :      Union[str, Any]       =							field(
		    default=__lowerCamelCase    ,			metadata={
		        'help': 'Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.'
		    }    ,			)






		@cached_property
		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								requires_backends(self		,						['tf']				)
								_lowerCAmelCase										=None
								if self.tpu:
														try:
																				if self.tpu_name:
																										_lowerCAmelCase										=tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name				)
																				else:
																										_lowerCAmelCase										=tf.distribute.cluster_resolver.TPUClusterResolver()
														except ValueError:
																				_lowerCAmelCase										=None
								return tpu






		@cached_property
		def 		UpperCamelCase__       (   self				) ->     Any:
								requires_backends(self		,						['tf']				)
								if self.is_tpu:
														tf.config.experimental_connect_to_cluster(self._setup_tpu				)
														tf.tpu.experimental.initialize_tpu_system(self._setup_tpu				)

														_lowerCAmelCase										=tf.distribute.TPUStrategy(self._setup_tpu				)
								else:
														# currently no multi gpu is allowed
														if self.is_gpu:
																				# TODO: Currently only single GPU is supported
																				tf.config.set_visible_devices(self.gpu_list[self.device_idx]		,						'GPU'				)
																				_lowerCAmelCase										=tf.distribute.OneDeviceStrategy(device=F'''/gpu:{self.device_idx}'''				)
														else:
																				tf.config.set_visible_devices([]		,						'GPU'				)  # disable GPU
																				_lowerCAmelCase										=tf.distribute.OneDeviceStrategy(device=F'''/cpu:{self.device_idx}'''				)

								return strategy






		@property
		def 		UpperCamelCase__       (   self				) ->     int:
								requires_backends(self		,						['tf']				)
								return self._setup_tpu is not None






		@property
		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								requires_backends(self		,						['tf']				)
								return self._setup_strategy






		@property
		def 		UpperCamelCase__       (   self				) ->     Any:
								requires_backends(self		,						['tf']				)
								return tf.config.list_physical_devices('GPU'				)






		@property
		def 		UpperCamelCase__       (   self				) ->     List[str]:
								requires_backends(self		,						['tf']				)
								if self.cuda:
														return len(self.gpu_list				)
								return 0






		@property
		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								return self.n_gpu > 0






'''simple docstring'''

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class   SCREAMING_SNAKE_CASE			(						__lowercase):







		"""simple docstring"""
		lowercase   :      Any       =							['image_processor', 'tokenizer']
		lowercase   :      Any       =							'CLIPImageProcessor'
		lowercase   :      int       =							('CLIPTokenizer', 'CLIPTokenizerFast')






		def __init__(   self		,						__A=None		,						__A=None		,						**__A				) ->     str:
								_lowerCAmelCase										=None
								if "feature_extractor" in kwargs:
														warnings.warn(
														    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
														    ' instead.'		,						__A		,						)
														_lowerCAmelCase										=kwargs.pop('feature_extractor'				)

								_lowerCAmelCase										=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__(__A		,						__A				)






		def __call__(   self		,						__A=None		,						__A=None		,						__A=None		,						**__A				) ->     Optional[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:
														_lowerCAmelCase										=self.tokenizer(__A		,						return_tensors=__A		,						**__A				)

								if images is not None:
														_lowerCAmelCase										=self.image_processor(__A		,						return_tensors=__A		,						**__A				)

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






		def 		UpperCamelCase__       (   self		,						*__A		,						**__A				) ->     Any:
								return self.tokenizer.batch_decode(*__A		,						**__A				)






		def 		UpperCamelCase__       (   self		,						*__A		,						**__A				) ->     Optional[int]:
								return self.tokenizer.decode(*__A		,						**__A				)






		@property
		def 		UpperCamelCase__       (   self				) ->     Tuple:
								_lowerCAmelCase										=self.tokenizer.model_input_names
								_lowerCAmelCase										=self.image_processor.model_input_names
								return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names				)				)






		@property
		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								warnings.warn(
								    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'		,						__A		,						)
								return self.image_processor_class






		@property
		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								warnings.warn(
								    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'		,						__A		,						)
								return self.image_processor





