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

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


UpperCamelCase			:       Union[str, Any]									=				[
    """EAGER""",
    """AOT_EAGER""",
    """INDUCTOR""",
    """NVFUSER""",
    """AOT_NVFUSER""",
    """AOT_CUDAGRAPHS""",
    """OFI""",
    """FX2TRT""",
    """ONNXRT""",
    """IPEX""",
]






def 							A		(       snake_case   :List[Any]       ,      snake_case   :List[str]=None       ,      snake_case   :str=None       ,      snake_case   :Dict=None	)   ->			str:
 __UpperCamelCase       						= True
 while ask_again:
  __UpperCamelCase       						= input(_lowerCAmelCase	)
  try:
   if default is not None and len(_lowerCAmelCase	) == 0:
    return default
   return convert_value(_lowerCAmelCase	) if convert_value is not None else result
  except Exception:
   if error_message is not None:
    print(_lowerCAmelCase	)






def 							A		(       snake_case   :Optional[int]       ,      snake_case   :Dict=[]       ,      snake_case   :Union[str, Any]=None       ,      snake_case   :Dict=0	)   ->			List[Any]:
 __UpperCamelCase       						= BulletMenu(_lowerCAmelCase       ,      _lowerCAmelCase	)
 __UpperCamelCase       						= menu.run(default_choice=_lowerCAmelCase	)
 return convert_value(_lowerCAmelCase	) if convert_value is not None else result






def 							A		(       snake_case   :Tuple	)   ->			Optional[int]:
 __UpperCamelCase       						= int(_lowerCAmelCase	)
 return ComputeEnvironment(['LOCAL_MACHINE', 'AMAZON_SAGEMAKER'][value]	)






def 							A		(       snake_case   :Tuple	)   ->			Union[str, Any]:
 __UpperCamelCase       						= int(_lowerCAmelCase	)
 return DistributedType(['NO', 'MULTI_CPU', 'MULTI_XPU', 'MULTI_GPU', 'MULTI_NPU', 'TPU'][value]	)






def 							A		(       snake_case   :int	)   ->			Any:
 __UpperCamelCase       						= int(_lowerCAmelCase	)
 return DynamoBackend(DYNAMO_BACKENDS[value]	).value






def 							A		(       snake_case   :Optional[int]	)   ->			Dict:
 __UpperCamelCase       						= int(_lowerCAmelCase	)
 return PrecisionType(['no', 'fp16', 'bf16', 'fp8'][value]	)






def 							A		(       snake_case   :Union[str, Any]	)   ->			Optional[Any]:
 __UpperCamelCase       						= int(_lowerCAmelCase	)
 return SageMakerDistributedType(['NO', 'DATA_PARALLEL', 'MODEL_PARALLEL'][value]	)






def 							A		(       snake_case   :Union[str, Any]	)   ->			Any:
 return {"yes": True, "no": False}[value.lower()]





class __lowerCAmelCase	(   argparse.RawDescriptionHelpFormatter    ):





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= super()._format_usage(A_			,  A_			,  A_			,  A_   )
      __UpperCamelCase       						= usage.replace('<command> [<args>] '			,  ''   )
      return usage





"""simple docstring"""

import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp






def 							A		(       )   ->			List[Any]:
 __UpperCamelCase       						= ArgumentParser(
     description=(
         'PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes'
     )	)

 # Optional arguments for the launch helper
 parser.add_argument('--num_cores'       ,      type=snake_case       ,      default=1       ,      help='Number of TPU cores to use (1 or 8).'	)

 # positional
 parser.add_argument(
     'training_script'       ,      type=snake_case       ,      help=(
         'The full path to the single TPU training '
         'program/script to be launched in parallel, '
         'followed by all the arguments for the '
         'training script'
     )       ,      )

 # rest from the training program
 parser.add_argument('training_script_args'       ,      nargs=snake_case	)

 return parser.parse_args()






def 							A		(       )   ->			Optional[int]:
 __UpperCamelCase       						= parse_args()

 # Import training_script as a module.
 __UpperCamelCase       						= Path(args.training_script	)
 sys.path.append(str(script_fpath.parent.resolve()	)	)
 __UpperCamelCase       						= script_fpath.stem
 __UpperCamelCase       						= importlib.import_module(snake_case	)

 # Patch sys.argv
 __UpperCamelCase       						= [args.training_script] + args.training_script_args + ['--tpu_num_cores', str(args.num_cores	)]

 xmp.spawn(mod._mp_fn       ,      args=()       ,      nprocs=args.num_cores	)


if __name__ == "__main__":
      main()



