



"""simple docstring"""





from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
  import torch

if is_torch_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm


lowerCamelCase       	=						logging.get_logger(__name__)
@dataclass
class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					[
           '''no_inference''',
           '''no_cuda''',
           '''no_tpu''',
           '''no_speed''',
           '''no_memory''',
           '''no_env_print''',
           '''no_multi_process''',
       ]



       def __init__(							self      :  List[Any]		,    **_UpperCAmelCase      :  Optional[int]   )			->						List[str]:


             '''simple docstring'''




             for deprecated_arg in self.deprecated_args:
                   if deprecated_arg in kwargs:
                         UpperCAmelCase_      			=							deprecated_arg[3:]
                         setattr(self		,    __a		,    not kwargs.pop(__a   )   )
                         logger.warning(
                             F"""{deprecated_arg} is depreciated. Please use --no_{positive_arg} or"""
                             F""" {positive_arg}={kwargs[positive_arg]}"""   )

             UpperCAmelCase_      			=							kwargs.pop("torchscript"		,    self.torchscript   )
             UpperCAmelCase_      			=							kwargs.pop("torch_xla_tpu_print_metrics"		,    self.torch_xla_tpu_print_metrics   )
             UpperCAmelCase_      			=							kwargs.pop("fp16_opt_level"		,    self.fpaa_opt_level   )
             super().__init__(**__a   )

       UpperCamelCase						=					field(default=SCREAMING_SNAKE_CASE    , metadata={'''help''': '''Trace the models using torchscript'''}				)
       UpperCamelCase						=					field(default=SCREAMING_SNAKE_CASE    , metadata={'''help''': '''Print Xla/PyTorch tpu metrics'''}				)
       UpperCamelCase						=					field(
           default='''O1'''    , metadata={
               '''help''': (
                   '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. '''
                   '''See details at https://nvidia.github.io/apex/amp.html'''
               )
           }    , )



       @cached_property
       def 					lowercase__    (							self      :  Any   )			->						Tuple["torch.device", int]:


             '''simple docstring'''




             requires_backends(self		,    ["torch"]   )
             logger.info("PyTorch: setting up devices"   )
             if not self.cuda:
                   UpperCAmelCase_      			=							torch.device("cpu"   )
                   UpperCAmelCase_      			=							0
             elif is_torch_tpu_available():
                   UpperCAmelCase_      			=							xm.xla_device()
                   UpperCAmelCase_      			=							0
             else:
                   UpperCAmelCase_      			=							torch.device("cuda" if torch.cuda.is_available() else "cpu"   )
                   UpperCAmelCase_      			=							torch.cuda.device_count()
             return device, n_gpu



       @property
       def 					lowercase__    (							self      :  Optional[int]   )			->						Optional[Any]:


             '''simple docstring'''




             return is_torch_tpu_available() and self.tpu



       @property
       def 					lowercase__    (							self      :  int   )			->						int:


             '''simple docstring'''




             requires_backends(self		,    ["torch"]   )
             # TODO(PVP): currently only single GPU is supported
             return torch.cuda.current_device()



       @property
       def 					lowercase__    (							self      :  str   )			->						"torch.device":


             '''simple docstring'''




             requires_backends(self		,    ["torch"]   )
             return self._setup_devices[0]



       @property
       def 					lowercase__    (							self      :  Dict   )			->						int:


             '''simple docstring'''




             requires_backends(self		,    ["torch"]   )
             return self._setup_devices[1]







       @property
       def 					lowercase__    (							self      :  List[Any]   )			->						List[str]:


             '''simple docstring'''




             return self.n_gpu > 0









"""simple docstring"""





import warnings

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







       '''simple docstring'''


       UpperCamelCase						=					['''image_processor''', '''tokenizer''']
       UpperCamelCase						=					'''ViTImageProcessor'''
       UpperCamelCase						=					('''CLIPTokenizer''', '''CLIPTokenizerFast''')



       def __init__(							self      :  List[Any]		,    _UpperCAmelCase      :  str=None		,    _UpperCAmelCase      :  Optional[int]=None		,    **_UpperCAmelCase      :  List[str]   )			->						Any:


             '''simple docstring'''




             UpperCAmelCase_      			=							None
             if "feature_extractor" in kwargs:
                   warnings.warn(
                       "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                       " instead."		,    _UpperCAmelCase		,    )
                   UpperCAmelCase_      			=							kwargs.pop("feature_extractor"   )

             UpperCAmelCase_      			=							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__(_UpperCAmelCase		,    _UpperCAmelCase   )



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


             '''simple docstring'''




             if text is None and visual_prompt is None and images is None:
                   raise ValueError("You have to specify either text, visual prompt or images."   )

             if text is not None and visual_prompt is not None:
                   raise ValueError("You have to specify exactly one type of prompt. Either text or visual prompt."   )

             if text is not None:
                   UpperCAmelCase_      			=							self.tokenizer(_UpperCAmelCase		,    return_tensors=_UpperCAmelCase		,    **_UpperCAmelCase   )

             if visual_prompt is not None:
                   UpperCAmelCase_      			=							self.image_processor(_UpperCAmelCase		,    return_tensors=_UpperCAmelCase		,    **_UpperCAmelCase   )

             if images is not None:
                   UpperCAmelCase_      			=							self.image_processor(_UpperCAmelCase		,    return_tensors=_UpperCAmelCase		,    **_UpperCAmelCase   )

             if visual_prompt is not None and images is not None:
                   UpperCAmelCase_      			=							{
                       "pixel_values": image_features.pixel_values,
                       "conditional_pixel_values": prompt_features.pixel_values,
                   }
                   return encoding
             elif text is not None and images is not None:
                   UpperCAmelCase_      			=							image_features.pixel_values
                   return encoding
             elif text is not None:
                   return encoding
             elif visual_prompt is not None:
                   UpperCAmelCase_      			=							{
                       "conditional_pixel_values": prompt_features.pixel_values,
                   }
                   return encoding
             else:
                   return BatchEncoding(data=dict(**_UpperCAmelCase   )		,    tensor_type=_UpperCAmelCase   )



       def 					lowercase__    (							self      :  List[Any]		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  List[Any]   )			->						Any:


             '''simple docstring'''




             return self.tokenizer.batch_decode(*_UpperCAmelCase		,    **_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Dict		,    *_UpperCAmelCase      :  Tuple		,    **_UpperCAmelCase      :  List[Any]   )			->						Union[str, Any]:


             '''simple docstring'''




             return self.tokenizer.decode(*_UpperCAmelCase		,    **_UpperCAmelCase   )



       @property
       def 					lowercase__    (							self      :  int   )			->						Optional[int]:


             '''simple docstring'''




             warnings.warn(
                 "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."		,    _UpperCAmelCase		,    )
             return self.image_processor_class







       @property
       def 					lowercase__    (							self      :  str   )			->						List[Any]:


             '''simple docstring'''




             warnings.warn(
                 "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."		,    _UpperCAmelCase		,    )
             return self.image_processor




