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


a_							=      logging.get_logger(__name__)


@dataclass
class 			UpperCAmelCase__    (		snake_case					):



  """simple docstring"""






  lowerCAmelCase__		:	Optional[Any]    		= [
      'no_inference',
      'no_cuda',
      'no_tpu',
      'no_speed',
      'no_memory',
      'no_env_print',
      'no_multi_process',
  ]






  def __init__(				self:			Tuple				,				**__lowerCAmelCase:			List[str]   ) -> List[Any]:




    '''simple docstring'''




    for deprecated_arg in self.deprecated_args:
      if deprecated_arg in kwargs:
        __UpperCAmelCase						    =       deprecated_arg[3:]
        setattr(self				,				__lowerCAmelCase				,				not kwargs.pop(__lowerCAmelCase   )   )
        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__(**__lowerCAmelCase   )

  lowerCAmelCase__		:	bool    		= field(default=snake_case		,	metadata={'help': 'Trace the models using torchscript'}					)
  lowerCAmelCase__		:	bool    		= field(default=snake_case		,	metadata={'help': 'Print Xla/PyTorch tpu metrics'}					)
  lowerCAmelCase__		:	str    		= 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 	_UpperCAmelCase				(				self:			Tuple   ) -> 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 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> Union[str, Any]:




    '''simple docstring'''




    return is_torch_tpu_available() and self.tpu






  @property
  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> int:




    '''simple docstring'''




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






  @property
  def 	_UpperCAmelCase				(				self:			int   ) -> "torch.device":




    '''simple docstring'''




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






  @property
  def 	_UpperCAmelCase				(				self:			Tuple   ) -> int:




    '''simple docstring'''




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






  @property
  def 	_UpperCAmelCase				(				self:			List[str]   ) -> Tuple:




    '''simple docstring'''




    return self.n_gpu > 0




import inspect
import unittest


class 			UpperCAmelCase__    (		unittest.TestCase					):



  """simple docstring"""






  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> Dict:




    '''simple docstring'''




    try:
      import diffusers  # noqa: F401
    except ImportError:
      assert False






  def 	_UpperCAmelCase				(				self:			Dict   ) -> Dict:




    '''simple docstring'''




    import diffusers
    from diffusers.dependency_versions_table import deps

    __UpperCAmelCase						    =       inspect.getmembers(__lowerCAmelCase				,				inspect.isclass   )

    for cls_name, cls_module in all_classes:
      if "dummy_" in cls_module.__module__:
        for backend in cls_module._backends:
          if backend == "k_diffusion":
            __UpperCAmelCase						    =       "k-diffusion"
          elif backend == "invisible_watermark":
            __UpperCAmelCase						    =       "invisible-watermark"
          assert backend in deps, F'''{backend} is not in the deps table!'''



