# 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


a  :      Union[str, Any]            =    """Create a default config file for Accelerate with only a few flags set."""
def 		snake_case__	(		lowercase="no"							,				lowercase = default_json_config_file							,				lowercase = False					):
   lowerCAmelCase_:							Dict       =	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
   lowerCAmelCase_:							List[Any]       =	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_:							Dict       =	{
       "compute_environment": "LOCAL_MACHINE",
       "mixed_precision": mixed_precision,
   }
   if torch.cuda.is_available():
      lowerCAmelCase_:							Union[str, Any]       =	torch.cuda.device_count()
      lowerCAmelCase_:							Optional[Any]       =	num_gpus
      lowerCAmelCase_:							Tuple       =	False
      if num_gpus > 1:
         lowerCAmelCase_:							Union[str, Any]       =	"MULTI_GPU"
      else:
         lowerCAmelCase_:							Optional[int]       =	"NO"
   elif is_xpu_available() and use_xpu:
      lowerCAmelCase_:							Optional[int]       =	torch.xpu.device_count()
      lowerCAmelCase_:							List[str]       =	num_xpus
      lowerCAmelCase_:							Tuple       =	False
      if num_xpus > 1:
         lowerCAmelCase_:							Optional[Any]       =	"MULTI_XPU"
      else:
         lowerCAmelCase_:							int       =	"NO"
   elif is_npu_available():
      lowerCAmelCase_:							List[Any]       =	torch.npu.device_count()
      lowerCAmelCase_:							str       =	num_npus
      lowerCAmelCase_:							Union[str, Any]       =	False
      if num_npus > 1:
         lowerCAmelCase_:							Tuple       =	"MULTI_NPU"
      else:
         lowerCAmelCase_:							List[Any]       =	"NO"
   else:
      lowerCAmelCase_:							List[str]       =	0
      lowerCAmelCase_:							Dict       =	True
      lowerCAmelCase_:							List[str]       =	1
      lowerCAmelCase_:							str       =	"NO"
   lowerCAmelCase_:							Optional[Any]       =	ClusterConfig(**lowercase					)
   config.to_json_file(lowercase					)
   return path
def 		snake_case__	(		lowercase							,				lowercase					):
   lowerCAmelCase_:							int       =	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 		snake_case__	(		lowercase					):
   lowerCAmelCase_:							int       =	write_basic_config(args.mixed_precision							,				args.save_location					)
   if config_file:
      print(F'''accelerate configuration saved at {config_file}'''					)
import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy
def 		snake_case__	(		lowercase							,				lowercase					):
   assert isinstance(lowercase							,				lowercase					)
   assert dataset.num_rows == 4
   assert dataset.num_columns == 3
   assert dataset.column_names == ["col_1", "col_2", "col_3"]
   for feature, expected_dtype in expected_features.items():
      assert dataset.features[feature].dtype == expected_dtype
@require_sqlalchemy
@pytest.mark.parametrize("keep_in_memory"							,				[False, True]					)
def 		snake_case__	(		lowercase							,				lowercase							,				lowercase							,				lowercase					):
   lowerCAmelCase_:							Any       =	tmp_path / "cache"
   lowerCAmelCase_:							int       =	{"col_1": "string", "col_2": "int64", "col_3": "float64"}
   with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
      lowerCAmelCase_:							Optional[int]       =	SqlDatasetReader(
          "dataset"							,				"sqlite:///" + sqlite_path							,				cache_dir=lowercase							,				keep_in_memory=lowercase					).read()
   _check_sql_dataset(lowercase							,				lowercase					)
@require_sqlalchemy
@pytest.mark.parametrize(
    "features"							,				[
        None,
        {"col_1": "string", "col_2": "int64", "col_3": "float64"},
        {"col_1": "string", "col_2": "string", "col_3": "string"},
        {"col_1": "int32", "col_2": "int32", "col_3": "int32"},
        {"col_1": "float32", "col_2": "float32", "col_3": "float32"},
    ]							,				)
def 		snake_case__	(		lowercase							,				lowercase							,				lowercase							,				lowercase					):
   lowerCAmelCase_:							List[str]       =	tmp_path / "cache"
   lowerCAmelCase_:							int       =	{"col_1": "string", "col_2": "int64", "col_3": "float64"}
   lowerCAmelCase_:							List[str]       =	features.copy() if features else default_expected_features
   lowerCAmelCase_:							Any       =	(
       Features({feature: Value(lowercase					) for feature, dtype in features.items()}					) if features is not None else None
   )
   lowerCAmelCase_:							int       =	SqlDatasetReader("dataset"							,				"sqlite:///" + sqlite_path							,				features=lowercase							,				cache_dir=lowercase					).read()
   _check_sql_dataset(lowercase							,				lowercase					)
def 		snake_case__	(		lowercase					):
   with contextlib.closing(sqlitea.connect(lowercase					)					) as con:
      lowerCAmelCase_:							Any       =	con.cursor()
      cur.execute("SELECT * FROM dataset"					)
      for row in cur:
         yield row
@require_sqlalchemy
def 		snake_case__	(		lowercase							,				lowercase							,				lowercase					):
   lowerCAmelCase_:							Optional[int]       =	tmp_path / "cache"
   lowerCAmelCase_:							Optional[Any]       =	os.path.join(lowercase							,				"tmp.sql"					)
   lowerCAmelCase_:							str       =	SqlDatasetReader("dataset"							,				"sqlite:///" + sqlite_path							,				cache_dir=lowercase					).read()
   SqlDatasetWriter(lowercase							,				"dataset"							,				"sqlite:///" + output_sqlite_path							,				num_proc=1					).write()

   lowerCAmelCase_:							Union[str, Any]       =	iter_sql_file(lowercase					)
   lowerCAmelCase_:							str       =	iter_sql_file(lowercase					)

   for rowa, rowa in zip(lowercase							,				lowercase					):
      assert rowa == rowa
@require_sqlalchemy
def 		snake_case__	(		lowercase							,				lowercase							,				lowercase					):
   lowerCAmelCase_:							str       =	tmp_path / "cache"
   lowerCAmelCase_:							Optional[int]       =	os.path.join(lowercase							,				"tmp.sql"					)
   lowerCAmelCase_:							Optional[Any]       =	SqlDatasetReader("dataset"							,				"sqlite:///" + sqlite_path							,				cache_dir=lowercase					).read()
   SqlDatasetWriter(lowercase							,				"dataset"							,				"sqlite:///" + output_sqlite_path							,				num_proc=2					).write()

   lowerCAmelCase_:							Optional[Any]       =	iter_sql_file(lowercase					)
   lowerCAmelCase_:							Optional[int]       =	iter_sql_file(lowercase					)

   for rowa, rowa in zip(lowercase							,				lowercase					):
      assert rowa == rowa







@require_sqlalchemy
def 		snake_case__	(		lowercase							,				lowercase							,				lowercase					):
   lowerCAmelCase_:							Union[str, Any]       =	tmp_path / "cache"
   lowerCAmelCase_:							int       =	os.path.join(lowercase							,				"tmp.sql"					)
   lowerCAmelCase_:							Any       =	SqlDatasetReader("dataset"							,				"sqlite:///" + sqlite_path							,				cache_dir=lowercase					).read()
   with pytest.raises(lowercase					):
      SqlDatasetWriter(lowercase							,				"dataset"							,				"sqlite:///" + output_sqlite_path							,				num_proc=0					).write()