






'''simple docstring'''





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						:    Tuple     				=  """Create a default config file for Accelerate with only a few flags set."""



def        __snake_case						(__UpperCAmelCase="no"       ,	__UpperCAmelCase = default_json_config_file       ,	__UpperCAmelCase = False    ):





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



def        __snake_case						(__UpperCAmelCase       ,	__UpperCAmelCase    ):





	"""simple docstring"""
	lowerCamelCase_					:  Any						=    parser.add_parser('''default'''       ,	parents=__UpperCAmelCase       ,	help=__UpperCAmelCase       ,	formatter_class=__UpperCAmelCase    )
	parser.add_argument(
	    '''--config_file'''       ,	default=__UpperCAmelCase       ,	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=__UpperCAmelCase       ,	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=__UpperCAmelCase    )
	return parser




def        __snake_case						(__UpperCAmelCase    ):





	"""simple docstring"""
	lowerCamelCase_					:  Any						=    write_basic_config(args.mixed_precision       ,	args.save_location    )
	if config_file:
		print(F"""accelerate configuration saved at {config_file}"""    )












'''simple docstring'''







from torch import nn







def 						_A      ( UpperCAmelCase						):






   '''simple docstring'''







   if act_fn in ["swish", "silu"]:
      return nn.SiLU()
   elif act_fn == "mish":
      return nn.Mish()
   elif act_fn == "gelu":
      return nn.GELU()
   else:
      raise ValueError(F"""Unsupported activation function: {act_fn}"""						)


