






import importlib
import importlib.metadata
import os
import warnings
from distutils.util import strtobool
from functools import lru_cache

import torch
from packaging import version
from packaging.version import parse

from .environment import parse_flag_from_env
from .versions import compare_versions, is_torch_version


try:
 import torch_xla.core.xla_model as xm  # noqa: F401

 a 			=							True
except ImportError:
 a 			=							False


# Cache this result has it's a C FFI call which can be pretty time-consuming
a 			=							torch.distributed.is_available()
def    a__    (	A       ):
   # Check we're not importing a "pkg_name" directory somewhere but the actual library by trying to grab the version
   lowercase__    :						str       =			importlib.util.find_spec(A       ) is not None
   if package_exists:
      try:
         lowercase__    :						Optional[Any]       =			importlib.metadata.metadata(A       )
         return True
      except importlib.metadata.PackageNotFoundError:
         return False
def    a__    (	):
   return _torch_distributed_available
def    a__    (	):
   try:
      pass
   except ImportError:
      print(
          """Intel(R) oneCCL Bindings for PyTorch* is required to run DDP on Intel(R) GPUs, but it is not"""
          """ detected. If you see \"ValueError: Invalid backend: 'ccl'\" error, please install Intel(R) oneCCL"""
          """ Bindings for PyTorch*."""       )
   return (
       importlib.util.find_spec("""torch_ccl"""       ) is not None
       or importlib.util.find_spec("""oneccl_bindings_for_pytorch"""       ) is not None
   )
def    a__    (	):
   return importlib.metadata.version("""oneccl_bind_pt"""       )
def    a__    (	):
   return _is_package_available("""transformer_engine"""       )
@lru_cache
def    a__    (	A=True       ):
   # Due to bugs on the amp series GPUs, we disable torch-xla on them
   if torch.cuda.is_available():
      return False
   if _tpu_available and check_device:
      try:
         # Will raise a RuntimeError if no XLA configuration is found
         lowercase__    :						List[Any]       =			xm.xla_device()
         return True
      except RuntimeError:
         return False
   return _tpu_available
def    a__    (	):
   return _is_package_available("""deepspeed"""       )
def    a__    (	A=False       ):
   if is_tpu_available():
      return not ignore_tpu
   if torch.cuda.is_available():
      return torch.cuda.is_bfaa_supported()
   if is_npu_available():
      return False
   return True
def    a__    (	):
   lowercase__    :						List[Any]       =			_is_package_available("""bitsandbytes"""       )
   if package_exists:
      lowercase__    :						Optional[int]       =			version.parse(importlib.metadata.version("""bitsandbytes"""       )       )
      return compare_versions(A		,					""">="""		,					"""0.39.0"""       )
   return False
def    a__    (	):
   lowercase__    :						Tuple       =			_is_package_available("""bitsandbytes"""       )
   if package_exists:
      lowercase__    :						List[str]       =			version.parse(importlib.metadata.version("""bitsandbytes"""       )       )
      return compare_versions(A		,					""">="""		,					"""0.37.2"""       )
   return False
def    a__    (	):
   return _is_package_available("""bitsandbytes"""       )
def    a__    (	):
   if strtobool(os.environ.get("""ACCELERATE_USE_MEGATRON_LM"""		,					"""False"""       )       ) == 1:
      lowercase__    :						List[str]       =			importlib.util.find_spec("""megatron"""       ) is not None
      if package_exists:
         try:
            lowercase__    :						List[Any]       =			parse(importlib.metadata.version("""megatron-lm"""       )       )
            return compare_versions(A		,					""">="""		,					"""2.2.0"""       )
         except Exception as e:
            warnings.warn(F'''Parse Megatron version failed. Exception:{e}'''       )
            return False
def    a__    (	):
   return _is_package_available("""safetensors"""       )
def    a__    (	):
   return _is_package_available("""transformers"""       )
def    a__    (	):
   return _is_package_available("""datasets"""       )
def    a__    (	):
   return _is_package_available("""aim"""       )
def    a__    (	):
   return _is_package_available("""tensorboard"""       ) or _is_package_available("""tensorboardX"""       )
def    a__    (	):
   return _is_package_available("""wandb"""       )
def    a__    (	):
   return _is_package_available("""comet_ml"""       )
def    a__    (	):
   return _is_package_available("""boto3"""       )
def    a__    (	):
   if _is_package_available("""rich"""       ):
      if "ACCELERATE_DISABLE_RICH" in os.environ:
         warnings.warn(
             """`ACCELERATE_DISABLE_RICH` is deprecated and will be removed in v0.22.0 and deactivated by default. Please use `ACCELERATE_ENABLE_RICH` if you wish to use `rich`."""       )
         return not parse_flag_from_env("""ACCELERATE_DISABLE_RICH"""		,					A       )
      return parse_flag_from_env("""ACCELERATE_ENABLE_RICH"""		,					A       )
   return False
def    a__    (	):
   return _is_package_available("""sagemaker"""       )
def    a__    (	):
   return _is_package_available("""tqdm"""       )
def    a__    (	):
   return _is_package_available("""mlflow"""       )
def    a__    (	):
   return is_torch_version(""">="""		,					"""1.12"""       ) and torch.backends.mps.is_available() and torch.backends.mps.is_built()
def    a__    (	):
   def get_major_and_minor_from_version(A       ):
      return str(version.parse(A       ).major       ) + "." + str(version.parse(A       ).minor       )

   lowercase__    :						Dict       =			importlib.metadata.version("""torch"""       )
   if importlib.util.find_spec("""intel_extension_for_pytorch"""       ) is None:
      return False
   lowercase__    :						Dict       =			"""N/A"""
   try:
      lowercase__    :						str       =			importlib.metadata.version("""intel_extension_for_pytorch"""       )
   except importlib.metadata.PackageNotFoundError:
      return False
   lowercase__    :						Tuple       =			get_major_and_minor_from_version(_torch_version       )
   lowercase__    :						Tuple       =			get_major_and_minor_from_version(_ipex_version       )
   if torch_major_and_minor != ipex_major_and_minor:
      warnings.warn(
          F'''Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,'''
          F''' but PyTorch {_torch_version} is found. Please switch to the matching version and run again.'''       )
      return False
   return True
@lru_cache
def    a__    (	A=False       ):
   if importlib.util.find_spec("""torch"""       ) is None or importlib.util.find_spec("""torch_npu"""       ) is None:
      return False

   import torch
   import torch_npu  # noqa: F401

   if check_device:
      try:
         # Will raise a RuntimeError if no NPU is found
         lowercase__    :						Dict       =			torch.npu.device_count()
         return torch.npu.is_available()
      except RuntimeError:
         return False
   return hasattr(A		,					"""npu"""       ) and torch.npu.is_available()






@lru_cache
def    a__    (	A=False       ):
   if not parse_flag_from_env("""ACCELERATE_USE_XPU"""		,					default=A       ):
      return False
   if is_ipex_available():
      import torch

      if is_torch_version("""<="""		,					"""1.12"""       ):
         return False
   else:
      return False

   import intel_extension_for_pytorch  # noqa: F401

   if check_device:
      try:
         # Will raise a RuntimeError if no XPU  is found
         lowercase__    :						int       =			torch.xpu.device_count()
         return torch.xpu.is_available()
      except RuntimeError:
         return False
   return hasattr(A		,					"""xpu"""       ) and torch.xpu.is_available()










import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union
class 							__snake_case(       enum.Enum   ):
 lowercase			:							List[str]		   =		'''timm'''
 lowercase			:							Any		   =		'''transformers'''
def    a__    (	A		,					A		,					A       ):
   if stage_names is None:
      raise ValueError("""Stage_names must be set for transformers backbones"""       )

   if out_features is not None:
      if not isinstance(A		,					(list,)       ):
         raise ValueError(F'''out_features must be a list {type(A       )}'''       )
      if any(feat not in stage_names for feat in out_features       ):
         raise ValueError(F'''out_features must be a subset of stage_names: {stage_names} got {out_features}'''       )

   if out_indices is not None:
      if not isinstance(A		,					(list, tuple)       ):
         raise ValueError(F'''out_indices must be a list or tuple, got {type(A       )}'''       )
      if any(idx >= len(A       ) for idx in out_indices       ):
         raise ValueError("""out_indices must be valid indices for stage_names {stage_names}, got {out_indices}"""       )

   if out_features is not None and out_indices is not None:
      if len(A       ) != len(A       ):
         raise ValueError("""out_features and out_indices should have the same length if both are set"""       )
      if out_features != [stage_names[idx] for idx in out_indices]:
         raise ValueError("""out_features and out_indices should correspond to the same stages if both are set"""       )
def    a__    (	A		,					A		,					A		,					):
   if out_indices is None and out_features is None:
      lowercase__    :						int       =			[len(A       ) - 1]
      lowercase__    :						Dict       =			[stage_names[-1]]
   elif out_indices is None and out_features is not None:
      lowercase__    :						Any       =			[stage_names.index(A       ) for layer in out_features]
   elif out_features is None and out_indices is not None:
      lowercase__    :						List[str]       =			[stage_names[idx] for idx in out_indices]
   return out_features, out_indices
def    a__    (	A		,					A		,					A		,					):
   verify_out_features_out_indices(out_features=A		,					out_indices=A		,					stage_names=A       )
   lowercase__  ,       lowercase__    :						Dict       =			_align_output_features_output_indices(
       out_features=A		,					out_indices=A		,					stage_names=A       )
   # Verify that the aligned out_features and out_indices are valid
   verify_out_features_out_indices(out_features=A		,					out_indices=A		,					stage_names=A       )
   return output_features, output_indices
class 							__snake_case:
 lowercase			:							Optional[BackboneType]		   =		None



 def     UpperCAmelCase     (						self : Dict			,		UpperCAmelCase__ : int						):

    """simple docstring"""






    if getattr(self			,		"""_backbone"""			,		UpperCAmelCase__						) is None:
       raise ValueError("""self._backbone must be set before calling _init_timm_backbone"""						)

    # These will diagree with the defaults for the transformers models e.g. for resnet50
    # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
    # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
    lowercase__    :						List[str]       =			[stage["""module"""] for stage in self._backbone.feature_info.info]
    lowercase__    :						int       =			[stage["""num_chs"""] for stage in self._backbone.feature_info.info]
    lowercase__    :						Optional[int]       =			self._backbone.feature_info.out_indices
    lowercase__    :						Optional[Any]       =			self._backbone.feature_info.module_name()

    # We verify the out indices and out features are valid
    verify_out_features_out_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)
    lowercase__  ,       lowercase__    :						int       =			out_features, out_indices



 def     UpperCAmelCase     (						self : Optional[Any]			,		UpperCAmelCase__ : str						):

    """simple docstring"""






    lowercase__    :						List[str]       =			getattr(UpperCAmelCase__			,		"""stage_names"""						)
    lowercase__    :						Union[str, Any]       =			getattr(UpperCAmelCase__			,		"""out_features"""			,		UpperCAmelCase__						)
    lowercase__    :						List[str]       =			getattr(UpperCAmelCase__			,		"""out_indices"""			,		UpperCAmelCase__						)

    lowercase__    :						Tuple       =			stage_names
    lowercase__  ,       lowercase__    :						List[str]       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=UpperCAmelCase__						)
    # Number of channels for each stage. This is set in the transformer backbone model init
    lowercase__    :						Optional[int]       =			None



 def     UpperCAmelCase     (						self : Tuple			,		UpperCAmelCase__ : Optional[Any]						):

    """simple docstring"""






    lowercase__    :						List[Any]       =			config

    lowercase__    :						Optional[Any]       =			getattr(UpperCAmelCase__			,		"""use_timm_backbone"""			,		UpperCAmelCase__						)
    lowercase__    :						Optional[int]       =			BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

    if self.backbone_type == BackboneType.TIMM:
       self._init_timm_backbone(UpperCAmelCase__						)
    elif self.backbone_type == BackboneType.TRANSFORMERS:
       self._init_transformers_backbone(UpperCAmelCase__						)
    else:
       raise ValueError(f'''backbone_type {self.backbone_type} not supported.'''						)



 @property
 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    return self._out_features



 @out_features.setter
 def     UpperCAmelCase     (						self : Dict			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    lowercase__  ,       lowercase__    :						Optional[Any]       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)



 @property
 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    return self._out_indices



 @out_indices.setter
 def     UpperCAmelCase     (						self : Optional[Any]			,		UpperCAmelCase__ : Union[Tuple[int], List[int]]						):

    """simple docstring"""






    lowercase__  ,       lowercase__    :						Any       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)



 @property
 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names						)}



 @property
 def     UpperCAmelCase     (						self : Optional[Any]						):

    """simple docstring"""






    return [self.out_feature_channels[name] for name in self.out_features]



 def     UpperCAmelCase     (						self : Optional[Any]			,		*UpperCAmelCase__ : Tuple			,		**UpperCAmelCase__ : Dict						):

    """simple docstring"""






    lowercase__    :						Optional[Any]       =			dict(inspect.signature(self.forward						).parameters						)
    lowercase__    :						Tuple       =			{k: v for k, v in kwargs.items() if k in signature}
    return self(*UpperCAmelCase__			,		**UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : int			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : Optional[bool] = None			,		UpperCAmelCase__ : Optional[bool] = None			,		UpperCAmelCase__ : Optional[bool] = None			,		):

    """simple docstring"""






    raise NotImplementedError("""This method should be implemented by the derived class."""						)







 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






    lowercase__    :						Union[str, Any]       =			super().to_dict()
    lowercase__    :						Tuple       =			output.pop("""_out_features"""						)
    lowercase__    :						Union[str, Any]       =			output.pop("""_out_indices"""						)
    return output
class 							__snake_case:



 @property
 def     UpperCAmelCase     (						self : str						):

    """simple docstring"""






    return self._out_features



 @out_features.setter
 def     UpperCAmelCase     (						self : Union[str, Any]			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    lowercase__  ,       lowercase__    :						Dict       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)



 @property
 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    return self._out_indices



 @out_indices.setter
 def     UpperCAmelCase     (						self : int			,		UpperCAmelCase__ : Union[Tuple[int], List[int]]						):

    """simple docstring"""






    lowercase__  ,       lowercase__    :						Any       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)







 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






    lowercase__    :						Dict       =			super().to_dict()
    lowercase__    :						Union[str, Any]       =			output.pop("""_out_features"""						)
    lowercase__    :						Dict       =			output.pop("""_out_indices"""						)
    return output


