

"""simple docstring"""


# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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


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






def     a__    (    __SCREAMING_SNAKE_CASE="no"					, __SCREAMING_SNAKE_CASE = default_json_config_file					, __SCREAMING_SNAKE_CASE = False )	->	Tuple:
		__lowerCAmelCase:       int          =			Path(__SCREAMING_SNAKE_CASE )
		path.parent.mkdir(parents=__SCREAMING_SNAKE_CASE					, exist_ok=__SCREAMING_SNAKE_CASE )
		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
		__lowerCAmelCase:       Union[str, Any]          =			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}" )
		__lowerCAmelCase:       Any          =			{
		    "compute_environment": "LOCAL_MACHINE",
		    "mixed_precision": mixed_precision,
		}
		if torch.cuda.is_available():
				__lowerCAmelCase:       Optional[int]          =			torch.cuda.device_count()
				__lowerCAmelCase:       Union[str, Any]          =			num_gpus
				__lowerCAmelCase:       List[str]          =			False
				if num_gpus > 1:
						__lowerCAmelCase:       List[str]          =			"MULTI_GPU"
				else:
						__lowerCAmelCase:       Optional[int]          =			"NO"
		elif is_xpu_available() and use_xpu:
				__lowerCAmelCase:       Any          =			torch.xpu.device_count()
				__lowerCAmelCase:       List[Any]          =			num_xpus
				__lowerCAmelCase:       Optional[Any]          =			False
				if num_xpus > 1:
						__lowerCAmelCase:       Any          =			"MULTI_XPU"
				else:
						__lowerCAmelCase:       Dict          =			"NO"
		elif is_npu_available():
				__lowerCAmelCase:       Dict          =			torch.npu.device_count()
				__lowerCAmelCase:       str          =			num_npus
				__lowerCAmelCase:       Tuple          =			False
				if num_npus > 1:
						__lowerCAmelCase:       Optional[int]          =			"MULTI_NPU"
				else:
						__lowerCAmelCase:       Any          =			"NO"
		else:
				__lowerCAmelCase:       Tuple          =			0
				__lowerCAmelCase:       Tuple          =			True
				__lowerCAmelCase:       Union[str, Any]          =			1
				__lowerCAmelCase:       Any          =			"NO"
		__lowerCAmelCase:       str          =			ClusterConfig(**__SCREAMING_SNAKE_CASE )
		config.to_json_file(__SCREAMING_SNAKE_CASE )
		return path






def     a__    (    __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE )	->	Optional[Any]:
		__lowerCAmelCase:       Union[str, Any]          =			parser.add_parser("default"					, parents=__SCREAMING_SNAKE_CASE					, help=__SCREAMING_SNAKE_CASE					, formatter_class=__SCREAMING_SNAKE_CASE )
		parser.add_argument(
		    "--config_file"					, default=__SCREAMING_SNAKE_CASE					, 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=__SCREAMING_SNAKE_CASE					, 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=__SCREAMING_SNAKE_CASE )
		return parser






def     a__    (    __SCREAMING_SNAKE_CASE )	->	Tuple:
		__lowerCAmelCase:       Dict          =			write_basic_config(args.mixed_precision					, args.save_location )
		if config_file:
				print(F"accelerate configuration saved at {config_file}" )





"""simple docstring"""


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


__A						   =			logging.get_logger(__name__)

__A						   =			{
    "microsoft/beit-base-patch16-224-pt22k": (
        "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json"
    ),
    # See all BEiT models at https://huggingface.co/models?filter=beit
}





class     snake_case	(				__snake_case  ):
				SCREAMING_SNAKE_CASE_   :      Tuple         =  """beit"""

				def __init__(	self							:				List[Any]     ,  UpperCamelCase__							:				List[str]=8_1_9_2     ,  UpperCamelCase__							:				Dict=7_6_8     ,  UpperCamelCase__							:				List[str]=1_2     ,  UpperCamelCase__							:				Union[str, Any]=1_2     ,  UpperCamelCase__							:				Dict=3_0_7_2     ,  UpperCamelCase__							:				Optional[int]="gelu"     ,  UpperCamelCase__							:				Tuple=0.0     ,  UpperCamelCase__							:				Tuple=0.0     ,  UpperCamelCase__							:				Union[str, Any]=0.02     ,  UpperCamelCase__							:				Optional[Any]=1e-12     ,  UpperCamelCase__							:				str=2_2_4     ,  UpperCamelCase__							:				str=1_6     ,  UpperCamelCase__							:				Union[str, Any]=3     ,  UpperCamelCase__							:				Optional[int]=False     ,  UpperCamelCase__							:				int=False     ,  UpperCamelCase__							:				Optional[Any]=False     ,  UpperCamelCase__							:				Optional[Any]=False     ,  UpperCamelCase__							:				Dict=0.1     ,  UpperCamelCase__							:				List[Any]=0.1     ,  UpperCamelCase__							:				Any=True     ,  UpperCamelCase__							:				Optional[Any]=[3, 5, 7, 1_1]     ,  UpperCamelCase__							:				Optional[Any]=[1, 2, 3, 6]     ,  UpperCamelCase__							:				Optional[Any]=True     ,  UpperCamelCase__							:				Tuple=0.4     ,  UpperCamelCase__							:				Optional[Any]=2_5_6     ,  UpperCamelCase__							:				Optional[int]=1     ,  UpperCamelCase__							:				Optional[int]=False     ,  UpperCamelCase__							:				Optional[Any]=2_5_5     ,  **UpperCamelCase__							:				Optional[int]     ,  )->   int:



						'''simple docstring'''


						super().__init__(**UpperCamelCase__)

						__lowerCAmelCase:       str          =			vocab_size
						__lowerCAmelCase:       List[Any]          =			hidden_size
						__lowerCAmelCase:       str          =			num_hidden_layers
						__lowerCAmelCase:       Tuple          =			num_attention_heads
						__lowerCAmelCase:       Union[str, Any]          =			intermediate_size
						__lowerCAmelCase:       List[Any]          =			hidden_act
						__lowerCAmelCase:       Optional[Any]          =			hidden_dropout_prob
						__lowerCAmelCase:       List[Any]          =			attention_probs_dropout_prob
						__lowerCAmelCase:       str          =			initializer_range
						__lowerCAmelCase:       Optional[Any]          =			layer_norm_eps

						__lowerCAmelCase:       Any          =			image_size
						__lowerCAmelCase:       Any          =			patch_size
						__lowerCAmelCase:       Union[str, Any]          =			num_channels
						__lowerCAmelCase:       Tuple          =			use_mask_token
						__lowerCAmelCase:       Optional[Any]          =			use_absolute_position_embeddings
						__lowerCAmelCase:       List[Any]          =			use_relative_position_bias
						__lowerCAmelCase:       Optional[Any]          =			use_shared_relative_position_bias
						__lowerCAmelCase:       List[str]          =			layer_scale_init_value
						__lowerCAmelCase:       str          =			drop_path_rate
						__lowerCAmelCase:       str          =			use_mean_pooling
						# decode head attributes (semantic segmentation)
						__lowerCAmelCase:       Optional[Any]          =			out_indices
						__lowerCAmelCase:       Union[str, Any]          =			pool_scales
						# auxiliary head attributes (semantic segmentation)
						__lowerCAmelCase:       List[str]          =			use_auxiliary_head
						__lowerCAmelCase:       Union[str, Any]          =			auxiliary_loss_weight
						__lowerCAmelCase:       Optional[int]          =			auxiliary_channels
						__lowerCAmelCase:       Dict          =			auxiliary_num_convs
						__lowerCAmelCase:       List[Any]          =			auxiliary_concat_input
						__lowerCAmelCase:       str          =			semantic_loss_ignore_index





class     snake_case	(				__snake_case  ):
				SCREAMING_SNAKE_CASE_   :      Optional[Any]         =  version.parse("""1.11"""  )

				@property
				def 							lowercase_     (	self							:				str)->   Mapping[str, Mapping[int, str]]:



						'''simple docstring'''


						return OrderedDict(
						    [
						        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
						    ])




				@property
				def 							lowercase_     (	self							:				Any)->   float:



						'''simple docstring'''


						return 1e-4


