

"""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


A__   :					str							=   'Create a default config file for Accelerate with only a few flags set.'




def  _lowerCAmelCase		(		_UpperCamelCase="no" ,				_UpperCamelCase = default_json_config_file ,				_UpperCamelCase = False						):






  """simple docstring"""



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




def  _lowerCAmelCase		(		_UpperCamelCase ,				_UpperCamelCase						):






  """simple docstring"""



  _lowercase:			Optional[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  _lowerCAmelCase		(		_UpperCamelCase						):






  """simple docstring"""



  _lowercase:			Any       =     write_basic_config(args.mixed_precision ,				args.save_location						)
  if config_file:
    print(f'''accelerate configuration saved at {config_file}'''						)


"""simple docstring"""




from __future__ import annotations

import numpy as np




def  _lowerCAmelCase		(		_UpperCamelCase						):






  """simple docstring"""



  _lowercase ,				_lowercase:			List[str]       =     np.shape(_UpperCamelCase						)
  if rows != columns:
    _lowercase:			int       =     (
        '''\'table\' has to be of square shaped array but got a '''
        f'''{rows}x{columns} array:\n{table}'''
    )
    raise ValueError(_UpperCamelCase						)

  _lowercase:			Tuple       =     np.zeros((rows, columns)						)
  _lowercase:			Tuple       =     np.zeros((rows, columns)						)
  for i in range(_UpperCamelCase						):
    for j in range(_UpperCamelCase						):
      _lowercase:			List[Any]       =     sum(lower[i][k] * upper[k][j] for k in range(_UpperCamelCase						)						)
      if upper[j][j] == 0:
        raise ArithmeticError('''No LU decomposition exists'''						)
      _lowercase:			Any       =     (table[i][j] - total) / upper[j][j]
    _lowercase:			Any       =     1
    for j in range(_UpperCamelCase ,				_UpperCamelCase						):
      _lowercase:			Optional[int]       =     sum(lower[i][k] * upper[k][j] for k in range(_UpperCamelCase						)						)
      _lowercase:			List[str]       =     table[i][j] - total
  return lower, upper


if __name__ == "__main__":
  import doctest

  doctest.testmod()
