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

    UpperCamelCase													=		True
except ImportError:
    UpperCamelCase													=		False


# Cache this result has it's a C FFI call which can be pretty time-consuming
UpperCamelCase													=		torch.distributed.is_available()

def     lowercase_       (    _lowerCamelCase							: List[str]):
    lowercase__				:     Union[str, Any]					      =       importlib.util.find_spec(UpperCamelCase_) is not None
    if package_exists:
        try:
            lowercase__				:     Union[str, Any]					      =       importlib.metadata.metadata(UpperCamelCase_)
            return True
        except importlib.metadata.PackageNotFoundError:
            return False

def     lowercase_       (    ):
    return _torch_distributed_available

def     lowercase_       (    ):






    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     lowercase_       (    ):
    return importlib.metadata.version("oneccl_bind_pt")

def     lowercase_       (    ):
    return _is_package_available("transformer_engine")

@lru_cache
def     lowercase_       (    _lowerCamelCase							: Tuple=True):






    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__				:     Optional[Any]					      =       xm.xla_device()
            return True
        except RuntimeError:
            return False
    return _tpu_available

def     lowercase_       (    ):
    return _is_package_available("deepspeed")

def     lowercase_       (    _lowerCamelCase							: List[Any]=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     lowercase_       (    ):
    lowercase__				:     Optional[int]					      =       _is_package_available("bitsandbytes")
    if package_exists:
        lowercase__				:     Any					      =       version.parse(importlib.metadata.version("bitsandbytes"))
        return compare_versions(UpperCamelCase_	,			">="	,			"0.39.0")
    return False

def     lowercase_       (    ):
    lowercase__				:     Tuple					      =       _is_package_available("bitsandbytes")
    if package_exists:
        lowercase__				:     str					      =       version.parse(importlib.metadata.version("bitsandbytes"))
        return compare_versions(UpperCamelCase_	,			">="	,			"0.37.2")
    return False

def     lowercase_       (    ):
    return _is_package_available("bitsandbytes")

def     lowercase_       (    ):






    if strtobool(os.environ.get("ACCELERATE_USE_MEGATRON_LM"	,			"False")) == 1:
        lowercase__				:     Dict					      =       importlib.util.find_spec("megatron") is not None
        if package_exists:
            try:
                lowercase__				:     Any					      =       parse(importlib.metadata.version("megatron-lm"))
                return compare_versions(UpperCamelCase_	,			">="	,			"2.2.0")
            except Exception as e:
                warnings.warn(f'''Parse Megatron version failed. Exception:{e}''')
                return False

def     lowercase_       (    ):
    return _is_package_available("safetensors")

def     lowercase_       (    ):
    return _is_package_available("transformers")

def     lowercase_       (    ):
    return _is_package_available("datasets")

def     lowercase_       (    ):
    return _is_package_available("aim")

def     lowercase_       (    ):
    return _is_package_available("tensorboard") or _is_package_available("tensorboardX")

def     lowercase_       (    ):
    return _is_package_available("wandb")

def     lowercase_       (    ):
    return _is_package_available("comet_ml")

def     lowercase_       (    ):
    return _is_package_available("boto3")

def     lowercase_       (    ):






    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"	,			UpperCamelCase_)
        return parse_flag_from_env("ACCELERATE_ENABLE_RICH"	,			UpperCamelCase_)
    return False

def     lowercase_       (    ):
    return _is_package_available("sagemaker")

def     lowercase_       (    ):
    return _is_package_available("tqdm")

def     lowercase_       (    ):
    return _is_package_available("mlflow")

def     lowercase_       (    ):
    return is_torch_version(">="	,			"1.12") and torch.backends.mps.is_available() and torch.backends.mps.is_built()

def     lowercase_       (    ):






    def get_major_and_minor_from_version(_lowerCamelCase							: Dict):
        return str(version.parse(UpperCamelCase_).major) + "." + str(version.parse(UpperCamelCase_).minor)

    lowercase__				:     Tuple					      =       importlib.metadata.version("torch")
    if importlib.util.find_spec("intel_extension_for_pytorch") is None:
        return False
    lowercase__				:     Optional[Any]					      =       "N/A"
    try:
        lowercase__				:     Tuple					      =       importlib.metadata.version("intel_extension_for_pytorch")
    except importlib.metadata.PackageNotFoundError:
        return False
    lowercase__				:     Optional[int]					      =       get_major_and_minor_from_version(_torch_version)
    lowercase__				:     List[str]					      =       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     lowercase_       (    _lowerCamelCase							: Optional[int]=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__				:     str					      =       torch.npu.device_count()
            return torch.npu.is_available()
        except RuntimeError:
            return False
    return hasattr(UpperCamelCase_	,			"npu") and torch.npu.is_available()

@lru_cache
def     lowercase_       (    _lowerCamelCase							: Optional[Any]=False):






    if not parse_flag_from_env("ACCELERATE_USE_XPU"	,			default=UpperCamelCase_):
        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(UpperCamelCase_	,			"xpu") and torch.xpu.is_available()




from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase													=		logging.get_logger(__name__)

UpperCamelCase													=		{
    '''apple/mobilevit-small''': '''https://huggingface.co/apple/mobilevit-small/resolve/main/config.json''',
    '''apple/mobilevit-x-small''': '''https://huggingface.co/apple/mobilevit-x-small/resolve/main/config.json''',
    '''apple/mobilevit-xx-small''': '''https://huggingface.co/apple/mobilevit-xx-small/resolve/main/config.json''',
    '''apple/deeplabv3-mobilevit-small''': (
        '''https://huggingface.co/apple/deeplabv3-mobilevit-small/resolve/main/config.json'''
    ),
    '''apple/deeplabv3-mobilevit-x-small''': (
        '''https://huggingface.co/apple/deeplabv3-mobilevit-x-small/resolve/main/config.json'''
    ),
    '''apple/deeplabv3-mobilevit-xx-small''': (
        '''https://huggingface.co/apple/deeplabv3-mobilevit-xx-small/resolve/main/config.json'''
    ),
    # See all MobileViT models at https://huggingface.co/models?filter=mobilevit
}



class 						snake_case_ (	__A ):
   __A			:	Any        =       "mobilevit"






   def __init__(   self       :  Union[str, Any]			,		lowercase_       :  List[Any]=3			,		lowercase_       :  List[Any]=2_56			,		lowercase_       :  Union[str, Any]=2			,		lowercase_       :  int=[1_44, 1_92, 2_40]			,		lowercase_       :  List[str]=[16, 32, 64, 96, 1_28, 1_60, 6_40]			,		lowercase_       :  Tuple=4			,		lowercase_       :  Optional[int]=2.0			,		lowercase_       :  int=4.0			,		lowercase_       :  str="silu"			,		lowercase_       :  Optional[Any]=3			,		lowercase_       :  Dict=32			,		lowercase_       :  Union[str, Any]=0.1			,		lowercase_       :  str=0.0			,		lowercase_       :  str=0.1			,		lowercase_       :  List[str]=0.02			,		lowercase_       :  Union[str, Any]=1E-5			,		lowercase_       :  Tuple=True			,		lowercase_       :  Optional[Any]=2_56			,		lowercase_       :  List[str]=[6, 12, 18]			,		lowercase_       :  Any=0.1			,		lowercase_       :  List[str]=2_55			,		**lowercase_       :  Tuple			,		)      ->		Optional[Any]:
       super().__init__(**lowercase_						)

       lowercase__				:     List[str]					      =       num_channels
       lowercase__				:     Optional[int]					      =       image_size
       lowercase__				:     Any					      =       patch_size
       lowercase__				:     int					      =       hidden_sizes
       lowercase__				:     str					      =       neck_hidden_sizes
       lowercase__				:     Optional[int]					      =       num_attention_heads
       lowercase__				:     Tuple					      =       mlp_ratio
       lowercase__				:     Tuple					      =       expand_ratio
       lowercase__				:     List[str]					      =       hidden_act
       lowercase__				:     Optional[int]					      =       conv_kernel_size
       lowercase__				:     Union[str, Any]					      =       output_stride
       lowercase__				:     Optional[int]					      =       hidden_dropout_prob
       lowercase__				:     List[str]					      =       attention_probs_dropout_prob
       lowercase__				:     Union[str, Any]					      =       classifier_dropout_prob
       lowercase__				:     Union[str, Any]					      =       initializer_range
       lowercase__				:     List[str]					      =       layer_norm_eps
       lowercase__				:     Dict					      =       qkv_bias

       # decode head attributes for semantic segmentation
       lowercase__				:     List[Any]					      =       aspp_out_channels
       lowercase__				:     List[Any]					      =       atrous_rates
       lowercase__				:     str					      =       aspp_dropout_prob
       lowercase__				:     int					      =       semantic_loss_ignore_index



class 						snake_case_ (	__A ):
   __A			:	int        =       version.parse("1.11" )






   @property
   def    __UpperCamelCase  (   self       :  Union[str, Any]						)      ->		Mapping[str, Mapping[int, str]]:
       return OrderedDict([("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"})]						)






   @property
   def    __UpperCamelCase  (   self       :  str						)      ->		Mapping[str, Mapping[int, str]]:
       if self.task == "image-classification":
           return OrderedDict([("logits", {0: "batch"})]						)
       else:
           return OrderedDict([("last_hidden_state", {0: "batch"}), ("pooler_output", {0: "batch"})]						)







   @property
   def    __UpperCamelCase  (   self       :  Union[str, Any]						)      ->		float:
       return 1E-4



