



from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


__lowerCamelCase         =			"Create a default config file for Accelerate with only a few flags set."







def    _snake_case (       __snake_case="no"					,  __snake_case = default_json_config_file					,  __snake_case = False    )     ->     List[Any]:






      '''simple docstring'''







      UpperCAmelCase_  :	int				      =		Path(__lowerCAmelCase    )
      path.parent.mkdir(parents=__lowerCAmelCase					,  exist_ok=__lowerCAmelCase    )
      if path.exists():
            print(
                F"""Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`."""    )
            return False
      UpperCAmelCase_  :	Dict				      =		mixed_precision.lower()
      if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
            raise ValueError(
                F"""`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}"""    )
      UpperCAmelCase_  :	Union[str, Any]				      =		{
          """compute_environment""": """LOCAL_MACHINE""",
          """mixed_precision""": mixed_precision,
      }
      if torch.cuda.is_available():
            UpperCAmelCase_  :	str				      =		torch.cuda.device_count()
            UpperCAmelCase_  :	str				      =		num_gpus
            UpperCAmelCase_  :	str				      =		False
            if num_gpus > 1:
                  UpperCAmelCase_  :	str				      =		"""MULTI_GPU"""
            else:
                  UpperCAmelCase_  :	int				      =		"""NO"""
      elif is_xpu_available() and use_xpu:
            UpperCAmelCase_  :	Dict				      =		torch.xpu.device_count()
            UpperCAmelCase_  :	Optional[int]				      =		num_xpus
            UpperCAmelCase_  :	Optional[int]				      =		False
            if num_xpus > 1:
                  UpperCAmelCase_  :	List[Any]				      =		"""MULTI_XPU"""
            else:
                  UpperCAmelCase_  :	Dict				      =		"""NO"""
      elif is_npu_available():
            UpperCAmelCase_  :	Union[str, Any]				      =		torch.npu.device_count()
            UpperCAmelCase_  :	Any				      =		num_npus
            UpperCAmelCase_  :	Optional[int]				      =		False
            if num_npus > 1:
                  UpperCAmelCase_  :	Union[str, Any]				      =		"""MULTI_NPU"""
            else:
                  UpperCAmelCase_  :	int				      =		"""NO"""
      else:
            UpperCAmelCase_  :	List[Any]				      =		0
            UpperCAmelCase_  :	int				      =		True
            UpperCAmelCase_  :	Any				      =		1
            UpperCAmelCase_  :	Optional[Any]				      =		"""NO"""
      UpperCAmelCase_  :	Optional[int]				      =		ClusterConfig(**__lowerCAmelCase    )
      config.to_json_file(__lowerCAmelCase    )
      return path







def    _snake_case (       __snake_case					,  __snake_case    )     ->     Optional[Any]:






      '''simple docstring'''







      UpperCAmelCase_  :	Any				      =		parser.add_parser("default"					,  parents=__lowerCAmelCase					,  help=__lowerCAmelCase					,  formatter_class=__lowerCAmelCase    )
      parser.add_argument(
          "--config_file"					,  default=__lowerCAmelCase					,  help=(
              "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
              "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
              "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
              "with 'huggingface'."
          )					,  dest="save_location"					,  )

      parser.add_argument(
          "--mixed_precision"					,  choices=["no", "fp16", "bf16"]					,  type=__lowerCAmelCase					,  help="Whether or not to use mixed precision training. "
          "Choose between FP16 and BF16 (bfloat16) training. "
          "BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later."					,  default="no"					,  )
      parser.set_defaults(func=__lowerCAmelCase    )
      return parser







def    _snake_case (       __snake_case    )     ->     str:






      '''simple docstring'''







      UpperCAmelCase_  :	str				      =		write_basic_config(args.mixed_precision					,  args.save_location    )
      if config_file:
            print(F"""accelerate configuration saved at {config_file}"""    )





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCamelCase         =			logging.get_logger(__name__)

__lowerCamelCase         =			{
    '''microsoft/focalnet-tiny''': '''https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json''',
}


class   snake_case_ (lowercase__ , lowercase__  ):




       """simple docstring"""






       _lowerCamelCase			    =				"""focalnet"""



       def __init__(     self     ,lowercase=224     ,lowercase=4     ,lowercase=3     ,lowercase=96     ,lowercase=False     ,lowercase=[192, 384, 768, 768]     ,lowercase=[2, 2, 6, 2]     ,lowercase=[2, 2, 2, 2]     ,lowercase=[3, 3, 3, 3]     ,lowercase="gelu"     ,lowercase=4.0     ,lowercase=0.0     ,lowercase=0.1     ,lowercase=False     ,lowercase=1E-4     ,lowercase=False     ,lowercase=False     ,lowercase=False     ,lowercase=0.02     ,lowercase=1E-5     ,lowercase=32     ,lowercase=None     ,lowercase=None     ,**lowercase     ,):

             """simple docstring"""





             super().__init__(**lowercase)

             UpperCAmelCase_  :	int				      =		image_size
             UpperCAmelCase_  :	Optional[Any]				      =		patch_size
             UpperCAmelCase_  :	Dict				      =		num_channels
             UpperCAmelCase_  :	Dict				      =		embed_dim
             UpperCAmelCase_  :	Optional[int]				      =		use_conv_embed
             UpperCAmelCase_  :	int				      =		hidden_sizes
             UpperCAmelCase_  :	Optional[int]				      =		depths
             UpperCAmelCase_  :	Optional[Any]				      =		focal_levels
             UpperCAmelCase_  :	Tuple				      =		focal_windows
             UpperCAmelCase_  :	int				      =		hidden_act
             UpperCAmelCase_  :	Any				      =		mlp_ratio
             UpperCAmelCase_  :	List[Any]				      =		hidden_dropout_prob
             UpperCAmelCase_  :	Tuple				      =		drop_path_rate
             UpperCAmelCase_  :	List[str]				      =		use_layerscale
             UpperCAmelCase_  :	List[str]				      =		layerscale_value
             UpperCAmelCase_  :	List[str]				      =		use_post_layernorm
             UpperCAmelCase_  :	Dict				      =		use_post_layernorm_in_modulation
             UpperCAmelCase_  :	int				      =		normalize_modulator
             UpperCAmelCase_  :	Dict				      =		initializer_range
             UpperCAmelCase_  :	str				      =		layer_norm_eps
             UpperCAmelCase_  :	Union[str, Any]				      =		encoder_stride
             UpperCAmelCase_  :	str				      =		["stem"] + [F"""stage{idx}""" for idx in range(1     ,len(self.depths) + 1)]
             UpperCAmelCase_      ,      UpperCAmelCase_  :	Any				      =		get_aligned_output_features_output_indices(
                 out_features=lowercase     ,out_indices=lowercase     ,stage_names=self.stage_names)
