






import os
import string
import sys


lowerCamelCase			:					List[Any]         =	1 << 8

lowerCamelCase			:					int         =	{
    'tab': ord('''\t'''),
    'newline': ord('''\r'''),
    'esc': 27,
    'up': 65 + ARROW_KEY_FLAG,
    'down': 66 + ARROW_KEY_FLAG,
    'right': 67 + ARROW_KEY_FLAG,
    'left': 68 + ARROW_KEY_FLAG,
    'mod_int': 91,
    'undefined': sys.maxsize,
    'interrupt': 3,
    'insert': 50,
    'delete': 51,
    'pg_up': 53,
    'pg_down': 54,
}

lowerCamelCase			:					List[Any]         =	KEYMAP['up']
lowerCamelCase			:					int         =	KEYMAP['left']

if sys.platform == "win32":
 lowerCamelCase			:					str         =	[]
 lowerCamelCase			:					List[str]         =	{
     b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG,
     b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG,
     b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG,
     b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG,
     b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG,
     b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG,
     b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG,
     b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG,
 }

for i in range(10):
 lowerCamelCase			:					Dict         =	ord(str(i))





def       __lowerCAmelCase      (						):




     if os.name == "nt":
          import msvcrt

          __lowerCAmelCase								=					"mbcs"
          # Flush the keyboard buffer
          while msvcrt.kbhit():
               msvcrt.getch()
          if len(_lowerCAmelCase       ) == 0:
               # Read the keystroke
               __lowerCAmelCase								=					msvcrt.getch()

               # If it is a prefix char, get second part
               if ch in (b"\x00", b"\xe0"):
                    __lowerCAmelCase								=					ch + msvcrt.getch()
                    # Translate actual Win chars to bullet char types
                    try:
                         __lowerCAmelCase								=					chr(WIN_KEYMAP[cha]       )
                         WIN_CH_BUFFER.append(chr(KEYMAP["mod_int"]       )       )
                         WIN_CH_BUFFER.append(_lowerCAmelCase       )
                         if ord(_lowerCAmelCase       ) in (
                             KEYMAP["insert"] - 1 << 9,
                             KEYMAP["delete"] - 1 << 9,
                             KEYMAP["pg_up"] - 1 << 9,
                             KEYMAP["pg_down"] - 1 << 9,
                         ):
                              WIN_CH_BUFFER.append(chr(126       )       )
                         __lowerCAmelCase								=					chr(KEYMAP["esc"]       )
                    except KeyError:
                         __lowerCAmelCase								=					cha[1]
               else:
                    __lowerCAmelCase								=					ch.decode(_lowerCAmelCase       )
          else:
               __lowerCAmelCase								=					WIN_CH_BUFFER.pop(0       )
     elif os.name == "posix":
          import termios
          import tty

          __lowerCAmelCase								=					sys.stdin.fileno()
          __lowerCAmelCase								=					termios.tcgetattr(_lowerCAmelCase       )
          try:
               tty.setraw(_lowerCAmelCase       )
               __lowerCAmelCase								=					sys.stdin.read(1       )
          finally:
               termios.tcsetattr(_lowerCAmelCase					, termios.TCSADRAIN					, _lowerCAmelCase       )
     return ch





def       __lowerCAmelCase      (						):
     __lowerCAmelCase								=					get_raw_chars()
     if ord(_lowerCAmelCase       ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
          return char

     elif ord(_lowerCAmelCase       ) == KEYMAP["esc"]:
          __lowerCAmelCase								=					get_raw_chars()
          if ord(_lowerCAmelCase       ) == KEYMAP["mod_int"]:
               __lowerCAmelCase								=					get_raw_chars()
               if ord(_lowerCAmelCase       ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(_lowerCAmelCase       ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
                    return chr(ord(_lowerCAmelCase       ) + ARROW_KEY_FLAG       )
               else:
                    return KEYMAP["undefined"]
          else:
               return get_raw_chars()

     else:
          if char in string.printable:
               return char
          else:
               return KEYMAP["undefined"]











# 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


lowerCamelCase			:					List[str]         =	'''Create a default config file for Accelerate with only a few flags set.'''





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





def       __lowerCAmelCase      (						__snake_case					, __snake_case       ):
     __lowerCAmelCase								=					parser.add_parser("default"					, parents=__snake_case					, help=__snake_case					, formatter_class=__snake_case       )
     parser.add_argument(
         "--config_file"					, default=__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=__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=__snake_case       )
     return parser





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






