






# 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


snake_case    				=		"""Create a default config file for Accelerate with only a few flags set."""


def   lowerCamelCase__			(     lowercase="no"						,     lowercase = default_json_config_file						,     lowercase = False				):

  """simple docstring"""






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


def   lowerCamelCase__			(     lowercase						,     lowercase				):

  """simple docstring"""






  SCREAMING_SNAKE_CASE       :      Dict							=    parser.add_parser("default"						,     parents=lowercase						,     help=lowercase						,     formatter_class=lowercase				)
  parser.add_argument(
      "--config_file"						,     default=lowercase						,     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=lowercase						,     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=lowercase				)
  return parser


def   lowerCamelCase__			(     lowercase				):

  """simple docstring"""






  SCREAMING_SNAKE_CASE       :      Optional[int]							=    write_basic_config(args.mixed_precision						,     args.save_location				)
  if config_file:
    print(F'''accelerate configuration saved at {config_file}'''				)














import logging
import os
from typing import List, TextIO, Union

from conllu import parse_incr
from utils_ner import InputExample, Split, TokenClassificationTask


snake_case    				=		logging.getLogger(__name__)


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     def __init__(				self       :	str					, UpperCAmelCase_       :	Union[str, Any]=-1      ):
       # in NER datasets, the last column is usually reserved for NER label
       SCREAMING_SNAKE_CASE       :      int							=    label_idx


     def 				_A       (				self       :	Any					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Union[Split, str]      ):
       if isinstance(UpperCAmelCase_					, UpperCAmelCase_      ):
         SCREAMING_SNAKE_CASE       :      Optional[Any]							=    mode.value
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    os.path.join(UpperCAmelCase_					, f'''{mode}.txt'''      )
       SCREAMING_SNAKE_CASE       :      int							=    1
       SCREAMING_SNAKE_CASE       :      Any							=    []
       with open(UpperCAmelCase_					, encoding="utf-8"      ) as f:
         SCREAMING_SNAKE_CASE       :      Optional[int]							=    []
         SCREAMING_SNAKE_CASE       :      List[str]							=    []
         for line in f:
           if line.startswith("-DOCSTART-"      ) or line == "" or line == "\n":
             if words:
               examples.append(InputExample(guid=f'''{mode}-{guid_index}'''					, words=UpperCAmelCase_					, labels=UpperCAmelCase_      )      )
               guid_index += 1
               SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    []
               SCREAMING_SNAKE_CASE       :      Optional[int]							=    []
           else:
             SCREAMING_SNAKE_CASE       :      str							=    line.split(" "      )
             words.append(splits[0]      )
             if len(UpperCAmelCase_      ) > 1:
               labels.append(splits[self.label_idx].replace("\n"					, ""      )      )
             else:
               # Examples could have no label for mode = "test"
               labels.append("O"      )
         if words:
           examples.append(InputExample(guid=f'''{mode}-{guid_index}'''					, words=UpperCAmelCase_					, labels=UpperCAmelCase_      )      )
       return examples


     def 				_A       (				self       :	Tuple					, UpperCAmelCase_       :	TextIO					, UpperCAmelCase_       :	TextIO					, UpperCAmelCase_       :	List      ):
       SCREAMING_SNAKE_CASE       :      List[str]							=    0
       for line in test_input_reader:
         if line.startswith("-DOCSTART-"      ) or line == "" or line == "\n":
           writer.write(UpperCAmelCase_      )
           if not preds_list[example_id]:
             example_id += 1
         elif preds_list[example_id]:
           SCREAMING_SNAKE_CASE       :      Optional[int]							=    line.split()[0] + " " + preds_list[example_id].pop(0      ) + "\n"
           writer.write(UpperCAmelCase_      )
         else:
           logger.warning("Maximum sequence length exceeded: No prediction for '%s'."					, line.split()[0]      )






     def 				_A       (				self       :	Optional[Any]					, UpperCAmelCase_       :	str      ):
       if path:
         with open(UpperCAmelCase_					, "r"      ) as f:
           SCREAMING_SNAKE_CASE       :      List[Any]							=    f.read().splitlines()
         if "O" not in labels:
           SCREAMING_SNAKE_CASE       :      Tuple							=    ["O"] + labels
         return labels
       else:
         return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]





class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     def __init__(				self       :	Union[str, Any]      ):
       # in CONLL2003 dataset chunk column is second-to-last
       super().__init__(label_idx=-2      )






     def 				_A       (				self       :	Optional[int]					, UpperCAmelCase_       :	str      ):
       if path:
         with open(UpperCAmelCase_					, "r"      ) as f:
           SCREAMING_SNAKE_CASE       :      Dict							=    f.read().splitlines()
         if "O" not in labels:
           SCREAMING_SNAKE_CASE       :      str							=    ["O"] + labels
         return labels
       else:
         return [
             "O",
             "B-ADVP",
             "B-INTJ",
             "B-LST",
             "B-PRT",
             "B-NP",
             "B-SBAR",
             "B-VP",
             "B-ADJP",
             "B-CONJP",
             "B-PP",
             "I-ADVP",
             "I-INTJ",
             "I-LST",
             "I-PRT",
             "I-NP",
             "I-SBAR",
             "I-VP",
             "I-ADJP",
             "I-CONJP",
             "I-PP",
         ]





class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     def 				_A       (				self       :	Dict					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Union[Split, str]      ):
       if isinstance(UpperCAmelCase_					, UpperCAmelCase_      ):
         SCREAMING_SNAKE_CASE       :      str							=    mode.value
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    os.path.join(UpperCAmelCase_					, f'''{mode}.txt'''      )
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    1
       SCREAMING_SNAKE_CASE       :      str							=    []

       with open(UpperCAmelCase_					, encoding="utf-8"      ) as f:
         for sentence in parse_incr(UpperCAmelCase_      ):
           SCREAMING_SNAKE_CASE       :      List[Any]							=    []
           SCREAMING_SNAKE_CASE       :      List[str]							=    []
           for token in sentence:
             words.append(token["form"]      )
             labels.append(token["upos"]      )
           assert len(UpperCAmelCase_      ) == len(UpperCAmelCase_      )
           if words:
             examples.append(InputExample(guid=f'''{mode}-{guid_index}'''					, words=UpperCAmelCase_					, labels=UpperCAmelCase_      )      )
             guid_index += 1
       return examples


     def 				_A       (				self       :	str					, UpperCAmelCase_       :	TextIO					, UpperCAmelCase_       :	TextIO					, UpperCAmelCase_       :	List      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    0
       for sentence in parse_incr(UpperCAmelCase_      ):
         SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    preds_list[example_id]
         SCREAMING_SNAKE_CASE       :      Any							=    ""
         for token in sentence:
           out += f'''{token['form']} ({token['upos']}|{s_p.pop(0      )}) '''
         out += "\n"
         writer.write(UpperCAmelCase_      )
         example_id += 1






     def 				_A       (				self       :	Dict					, UpperCAmelCase_       :	str      ):
       if path:
         with open(UpperCAmelCase_					, "r"      ) as f:
           return f.read().splitlines()
       else:
         return [
             "ADJ",
             "ADP",
             "ADV",
             "AUX",
             "CCONJ",
             "DET",
             "INTJ",
             "NOUN",
             "NUM",
             "PART",
             "PRON",
             "PROPN",
             "PUNCT",
             "SCONJ",
             "SYM",
             "VERB",
             "X",
         ]






