





# 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


UpperCamelCase_												=	'Create a default config file for Accelerate with only a few flags set.'






def 				_UpperCAmelCase			(    A="no"				,							A = default_json_config_file				,							A = False     ):




   '''simple docstring'''

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






def 				_UpperCAmelCase			(    A				,							A     ):




   '''simple docstring'''

   UpperCAmelCase__						=parser.add_parser("default"				,							parents=A				,							help=A				,							formatter_class=A     )
   parser.add_argument(
       "--config_file"				,							default=A				,							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=A				,							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=A     )
   return parser






def 				_UpperCAmelCase			(    A     ):




   '''simple docstring'''

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










import os
import zipfile

import requests
from get_ci_error_statistics import download_artifact, get_artifacts_links






def 				_UpperCAmelCase			(    A				,							A=7     ):




   '''simple docstring'''

   UpperCAmelCase__						=None
   if token is not None:
      UpperCAmelCase__						={"Accept": "application/vnd.github+json", "Authorization": F"""Bearer {token}"""}

   # The id of a workflow (not of a workflow run)
   UpperCAmelCase__						="636036"

   UpperCAmelCase__						=F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs"""
   # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results
   url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}"""

   UpperCAmelCase__						=requests.get(A				,							headers=A     ).json()

   return result["workflow_runs"]






def 				_UpperCAmelCase			(    A     ):




   '''simple docstring'''

   UpperCAmelCase__						=get_daily_ci_runs(A     )
   UpperCAmelCase__						=None
   for workflow_run in workflow_runs:
      if workflow_run["status"] == "completed":
         UpperCAmelCase__						=workflow_run["id"]
         break

   return workflow_run_id






def 				_UpperCAmelCase			(    A				,							A				,							A     ):




   '''simple docstring'''

   UpperCAmelCase__						=get_last_daily_ci_runs(A     )
   if workflow_run_id is not None:
      UpperCAmelCase__						=get_artifacts_links(worflow_run_id=A				,							token=A     )
      for artifact_name in artifact_names:
         if artifact_name in artifacts_links:
            UpperCAmelCase__						=artifacts_links[artifact_name]
            download_artifact(
                artifact_name=A				,							artifact_url=A				,							output_dir=A				,							token=A     )






def 				_UpperCAmelCase			(    A				,							A				,							A     ):




   '''simple docstring'''

   get_last_daily_ci_artifacts(A				,							A				,							A     )

   UpperCAmelCase__						={}
   for artifact_name in artifact_names:
      UpperCAmelCase__						=os.path.join(A				,							F"""{artifact_name}.zip"""     )
      if os.path.isfile(A     ):
         UpperCAmelCase__						={}
         with zipfile.ZipFile(A     ) as z:
            for filename in z.namelist():
               if not os.path.isdir(A     ):
                  # read the file
                  with z.open(A     ) as f:
                     UpperCAmelCase__						=f.read().decode("UTF-8"     )

   return results



