





'''simple docstring'''







import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment






def    __UpperCamelCase	(	a    : Any					,   a    : List[str]=()					,   a    : Tuple=None					,   a    : List[str]="no"					,   a    : Dict="29500"     ) ->int:
 snake_case				=			False
 snake_case				=			False
 if any(key.startswith('''KAGGLE'''     ) for key in os.environ.keys()     ):
  snake_case				=			True
 elif "IPython" in sys.modules:
  snake_case				=			'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()     )

 try:
  snake_case				=			PrecisionType(mixed_precision.lower()     )
 except ValueError:
  raise ValueError(
      f"""Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}."""     )

 if (in_colab or in_kaggle) and (os.environ.get('''TPU_NAME'''					,   a     ) is not None):
  # TPU launch
  import torch_xla.distributed.xla_multiprocessing as xmp

  if len(AcceleratorState._shared_state     ) > 0:
   raise ValueError(
       '''To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside '''
       '''your training function. Restart your notebook and make sure no cells initializes an '''
       '''`Accelerator`.'''     )
  if num_processes is None:
   snake_case				=			8

  snake_case				=			PrepareForLaunch(a					,   distributed_type='''TPU'''     )
  print(f"""Launching a training on {num_processes} TPU cores."""     )
  xmp.spawn(a					,   args=a					,   nprocs=a					,   start_method='''fork'''     )
 elif in_colab:
  # No need for a distributed launch otherwise as it's either CPU or one GPU.
  if torch.cuda.is_available():
   print('''Launching training on one GPU.'''     )
  else:
   print('''Launching training on one CPU.'''     )
  function(*a     )
 else:
  if num_processes is None:
   raise ValueError(
       '''You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.'''     )

  if num_processes > 1:
   # Multi-GPU launch
   from torch.multiprocessing import start_processes
   from torch.multiprocessing.spawn import ProcessRaisedException

   if len(AcceleratorState._shared_state     ) > 0:
    raise ValueError(
        '''To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized '''
        '''inside your training function. Restart your notebook and make sure no cells initializes an '''
        '''`Accelerator`.'''     )

   if torch.cuda.is_initialized():
    raise ValueError(
        '''To launch a multi-GPU training from your notebook, you need to avoid running any instruction '''
        '''using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA '''
        '''function.'''     )

   # torch.distributed will expect a few environment variable to be here. We set the ones common to each
   # process here (the other ones will be set be the launcher).
   with patch_environment(
       world_size=a					,   master_addr='''127.0.01'''					,   master_port=a					,   mixed_precision=a     ):
    snake_case				=			PrepareForLaunch(a					,   distributed_type='''MULTI_GPU'''     )
    print(f"""Launching training on {num_processes} GPUs."""     )
    try:
     start_processes(a					,   args=a					,   nprocs=a					,   start_method='''fork'''     )
    except ProcessRaisedException as e:
     if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
      raise RuntimeError(
          '''CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. '''
          '''This likely stems from an outside import causing issues once the `notebook_launcher()` is called. '''
          '''Please review your imports and test them when running the `notebook_launcher()` to identify '''
          '''which one is problematic.'''     ) from e

  else:
   # No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
   if is_mps_available():
    snake_case				=			'''1'''
    print('''Launching training on MPS.'''     )
   elif torch.cuda.is_available():
    print('''Launching training on one GPU.'''     )
   else:
    print('''Launching training on CPU.'''     )
   function(*a     )






def    __UpperCamelCase	(	a    : Dict					,   a    : Optional[int]=()					,   a    : Dict=2     ) ->int:
 from torch.multiprocessing import start_processes

 with tempfile.NamedTemporaryFile() as tmp_file:
  # torch.distributed will expect a few environment variable to be here. We set the ones common to each
  # process here (the other ones will be set be the launcher).
  with patch_environment(
      world_size=a					,   master_addr='''127.0.01'''					,   master_port='''29500'''					,   accelerate_mixed_precision='''no'''					,   accelerate_debug_rdv_file=tmp_file.name					,   accelerate_use_cpu='''yes'''					,   ):
   snake_case				=			PrepareForLaunch(a					,   debug=a     )
   start_processes(a					,   args=a					,   nprocs=a					,   start_method='''fork'''     )








'''simple docstring'''







from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml
class 			_lowercase						(       yaml.SafeLoader					):







     def     UpperCamelCase			( self    ,   A__    ) ->	List[str]:
      snake_case				=			[self.constructed_objects[key_node] for key_node, _ in node.value]
      snake_case				=			[tuple(A__    ) if isinstance(A__    ,   A__    ) else key for key in keys]
      snake_case				=			Counter(A__    )
      snake_case				=			[key for key in counter if counter[key] > 1]
      if duplicate_keys:
       raise TypeError(F"""Got duplicate yaml keys: {duplicate_keys}"""    )







     def     UpperCamelCase			( self    ,   A__    ,   A__=False    ) ->	List[Any]:
      snake_case				=			super().construct_mapping(A__    ,   deep=A__    )
      self._check_no_duplicates_on_constructed_node(A__    )
      return mapping




def    __UpperCamelCase	(	a    : str     ) ->Tuple[Optional[str], str]:
 snake_case				=			list(readme_content.splitlines()     )
 if full_content and full_content[0] == "---" and "---" in full_content[1:]:
  snake_case				=			full_content[1:].index('''---'''     ) + 1
  snake_case				=			'''\n'''.join(full_content[1:sep_idx]     )
  return yamlblock, "\n".join(full_content[sep_idx + 1 :]     )

 return None, "\n".join(a     )
class 			_lowercase						(       __a					):
     # class attributes
     _UpperCAmelCase										=					{'''train_eval_index'''}  # train-eval-index in the YAML metadata







     @classmethod
     def     UpperCamelCase			( cls    ,   A__    ) ->	"DatasetMetadata":
      with open(A__    ,   encoding='''utf-8'''    ) as readme_file:
       snake_case				,		snake_case				=			_split_yaml_from_readme(readme_file.read()    )
      if yaml_string is not None:
       return cls.from_yaml_string(A__    )
      else:
       return cls()







     def     UpperCamelCase			( self    ,   A__    ) ->	str:
      if path.exists():
       with open(A__    ,   encoding='''utf-8'''    ) as readme_file:
        snake_case				=			readme_file.read()
      else:
       snake_case				=			None
      snake_case				=			self._to_readme(A__    )
      with open(A__    ,   '''w'''    ,   encoding='''utf-8'''    ) as readme_file:
       readme_file.write(A__    )







     def     UpperCamelCase			( self    ,   A__ = None    ) ->	str:
      if readme_content is not None:
       snake_case				,		snake_case				=			_split_yaml_from_readme(A__    )
       snake_case				=			'''---\n''' + self.to_yaml_string() + '''---\n''' + content
      else:
       snake_case				=			'''---\n''' + self.to_yaml_string() + '''---\n'''
      return full_content







     @classmethod
     def     UpperCamelCase			( cls    ,   A__    ) ->	"DatasetMetadata":
      snake_case				=			yaml.load(A__    ,   Loader=_NoDuplicateSafeLoader    ) or {}

      # Convert the YAML keys to DatasetMetadata fields
      snake_case				=			{
          (key.replace('''-'''    ,   '''_'''    ) if key.replace('''-'''    ,   '''_'''    ) in cls._FIELDS_WITH_DASHES else key): value
          for key, value in metadata_dict.items()
      }
      return cls(**A__    )







     def     UpperCamelCase			( self    ) ->	str:
      return yaml.safe_dump(
          {
              (key.replace('''_'''    ,   '''-'''    ) if key in self._FIELDS_WITH_DASHES else key): value
              for key, value in self.items()
          }    ,   sort_keys=A__    ,   allow_unicode=A__    ,   encoding='''utf-8'''    ,   ).decode('''utf-8'''    )




_lowercase       						=			{
    'image-classification': [],
    'translation': [],
    'image-segmentation': [],
    'fill-mask': [],
    'automatic-speech-recognition': [],
    'token-classification': [],
    'sentence-similarity': [],
    'audio-classification': [],
    'question-answering': [],
    'summarization': [],
    'zero-shot-classification': [],
    'table-to-text': [],
    'feature-extraction': [],
    'other': [],
    'multiple-choice': [],
    'text-classification': [],
    'text-to-image': [],
    'text2text-generation': [],
    'zero-shot-image-classification': [],
    'tabular-classification': [],
    'tabular-regression': [],
    'image-to-image': [],
    'tabular-to-text': [],
    'unconditional-image-generation': [],
    'text-retrieval': [],
    'text-to-speech': [],
    'object-detection': [],
    'audio-to-audio': [],
    'text-generation': [],
    'conversational': [],
    'table-question-answering': [],
    'visual-question-answering': [],
    'image-to-text': [],
    'reinforcement-learning': [],
    'voice-activity-detection': [],
    'time-series-forecasting': [],
    'document-question-answering': [],
}


if __name__ == "__main__":
   from argparse import ArgumentParser

   _lowercase       						=			ArgumentParser(usage='Validate the yaml metadata block of a README.md file.')
   ap.add_argument('readme_filepath')
   _lowercase       						=			ap.parse_args()

   _lowercase       						=			Path(args.readme_filepath)
   _lowercase       						=			DatasetMetadata.from_readme(readme_filepath)
   print(dataset_metadata)
   dataset_metadata.to_readme(readme_filepath)

