# Copyright 2022 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
import os
import platform

import numpy as np
import psutil
import torch

from accelerate import __version__ as version
from accelerate.commands.config import default_config_file, load_config_from_file

from ..utils import is_npu_available, is_xpu_available




def       _snake_case	(   lowerCAmelCase							:   Dict=None	):
		"""simple docstring"""







		if subparsers is not None:
				SCREAMING_SNAKE_CASE_  :		int				      =    subparsers.add_parser("env"	)
		else:
				SCREAMING_SNAKE_CASE_  :		Dict				      =    argparse.ArgumentParser("Accelerate env command"	)

		parser.add_argument(
		    "--config_file"   , default=lowerCAmelCase   , help="The config file to use for the default values in the launching script."	)

		if subparsers is not None:
				parser.set_defaults(func=lowerCAmelCase	)
		return parser




def       _snake_case	(   lowerCAmelCase							:   List[str]	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Dict				      =    torch.__version__
		SCREAMING_SNAKE_CASE_  :		List[str]				      =    torch.cuda.is_available()
		SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    is_xpu_available()
		SCREAMING_SNAKE_CASE_  :		str				      =    is_npu_available()

		SCREAMING_SNAKE_CASE_  :		int				      =    "Not found"
		# Get the default from the config file.
		if args.config_file is not None or os.path.isfile(lowerCAmelCase	):
				SCREAMING_SNAKE_CASE_  :		List[str]				      =    load_config_from_file(args.config_file	).to_dict()

		SCREAMING_SNAKE_CASE_  :		str				      =    {
		    "`Accelerate` version": version,
		    "Platform": platform.platform(),
		    "Python version": platform.python_version(),
		    "Numpy version": np.__version__,
		    "PyTorch version (GPU?)": f'{pt_version} ({pt_cuda_available})',
		    "PyTorch XPU available": str(lowerCAmelCase	),
		    "PyTorch NPU available": str(lowerCAmelCase	),
		    "System RAM": f'{psutil.virtual_memory().total / 1_0_2_4 ** 3:.2f} GB',
		}
		if pt_cuda_available:
				SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    torch.cuda.get_device_name()

		print("\nCopy-and-paste the text below in your GitHub issue\n"	)
		print("\n".join([f'- {prop}: {val}' for prop, val in info.items()]	)	)

		print("- `Accelerate` default config:" if args.config_file is None else "- `Accelerate` config passed:"	)
		SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    (
		    "\n".join([f'\t- {prop}: {val}' for prop, val in accelerate_config.items()]	)
		    if isinstance(lowerCAmelCase   , lowerCAmelCase	)
		    else f'\t{accelerate_config}'
		)
		print(lowerCAmelCase	)

		SCREAMING_SNAKE_CASE_  :		Dict				      =    accelerate_config

		return info




def       _snake_case	(   ):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Any				      =    env_command_parser()
		SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    parser.parse_args()
		env_command(lowerCAmelCase	)
		return 0


if __name__ == "__main__":
		raise SystemExit(main())

import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases




def       _snake_case	(   lowerCAmelCase							:   str   , lowerCAmelCase							:   Union[str, Any]	):
		"""simple docstring"""







		assert isinstance(lowerCAmelCase   , lowerCAmelCase	)
		assert dataset.num_rows == 4
		assert dataset.num_columns == 1
		assert dataset.column_names == ["text"]
		for feature, expected_dtype in expected_features.items():
				assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize("keep_in_memory"   , [False, True]	)
def       _snake_case	(   lowerCAmelCase							:   Tuple   , lowerCAmelCase							:   Union[str, Any]   , lowerCAmelCase							:   int	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    tmp_path / "cache"
		SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    {"text": "string"}
		with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
				SCREAMING_SNAKE_CASE_  :		List[str]				      =    TextDatasetReader(lowerCAmelCase   , cache_dir=lowerCAmelCase   , keep_in_memory=lowerCAmelCase	).read()
		_check_text_dataset(lowerCAmelCase   , lowerCAmelCase	)




@pytest.mark.parametrize(
    "features"   , [
        None,
        {"text": "string"},
        {"text": "int32"},
        {"text": "float32"},
    ]   , )
def       _snake_case	(   lowerCAmelCase							:   Tuple   , lowerCAmelCase							:   Dict   , lowerCAmelCase							:   List[str]	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Any				      =    tmp_path / "cache"
		SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    {"text": "string"}
		SCREAMING_SNAKE_CASE_  :		Any				      =    features.copy() if features else default_expected_features
		SCREAMING_SNAKE_CASE_  :		List[str]				      =    (
		    Features({feature: Value(lowerCAmelCase	) for feature, dtype in features.items()}	) if features is not None else None
		)
		SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    TextDatasetReader(lowerCAmelCase   , features=lowerCAmelCase   , cache_dir=lowerCAmelCase	).read()
		_check_text_dataset(lowerCAmelCase   , lowerCAmelCase	)




@pytest.mark.parametrize("split"   , [None, NamedSplit("train"	), "train", "test"]	)
def       _snake_case	(   lowerCAmelCase							:   Union[str, Any]   , lowerCAmelCase							:   Optional[Any]   , lowerCAmelCase							:   List[str]	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    tmp_path / "cache"
		SCREAMING_SNAKE_CASE_  :		List[str]				      =    {"text": "string"}
		SCREAMING_SNAKE_CASE_  :		List[str]				      =    TextDatasetReader(lowerCAmelCase   , cache_dir=lowerCAmelCase   , split=lowerCAmelCase	).read()
		_check_text_dataset(lowerCAmelCase   , lowerCAmelCase	)
		assert dataset.split == split if split else "train"




@pytest.mark.parametrize("path_type"   , [str, list]	)
def       _snake_case	(   lowerCAmelCase							:   int   , lowerCAmelCase							:   int   , lowerCAmelCase							:   Optional[int]	):
		"""simple docstring"""







		if issubclass(lowerCAmelCase   , lowerCAmelCase	):
				SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    text_path
		elif issubclass(lowerCAmelCase   , lowerCAmelCase	):
				SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    [text_path]
		SCREAMING_SNAKE_CASE_  :		int				      =    tmp_path / "cache"
		SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    {"text": "string"}
		SCREAMING_SNAKE_CASE_  :		List[str]				      =    TextDatasetReader(lowerCAmelCase   , cache_dir=lowerCAmelCase	).read()
		_check_text_dataset(lowerCAmelCase   , lowerCAmelCase	)




def       _snake_case	(   lowerCAmelCase							:   Dict   , lowerCAmelCase							:   Dict   , lowerCAmelCase							:   List[str]=("train",)	):
		"""simple docstring"""







		assert isinstance(lowerCAmelCase   , lowerCAmelCase	)
		for split in splits:
				SCREAMING_SNAKE_CASE_  :		int				      =    dataset_dict[split]
				assert dataset.num_rows == 4
				assert dataset.num_columns == 1
				assert dataset.column_names == ["text"]
				for feature, expected_dtype in expected_features.items():
						assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize("keep_in_memory"   , [False, True]	)
def       _snake_case	(   lowerCAmelCase							:   List[str]   , lowerCAmelCase							:   str   , lowerCAmelCase							:   Dict	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		List[str]				      =    tmp_path / "cache"
		SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    {"text": "string"}
		with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
				SCREAMING_SNAKE_CASE_  :		List[Any]				      =    TextDatasetReader({"train": text_path}   , cache_dir=lowerCAmelCase   , keep_in_memory=lowerCAmelCase	).read()
		_check_text_datasetdict(lowerCAmelCase   , lowerCAmelCase	)




@pytest.mark.parametrize(
    "features"   , [
        None,
        {"text": "string"},
        {"text": "int32"},
        {"text": "float32"},
    ]   , )
def       _snake_case	(   lowerCAmelCase							:   Union[str, Any]   , lowerCAmelCase							:   List[Any]   , lowerCAmelCase							:   Tuple	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		List[Any]				      =    tmp_path / "cache"
		# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
		SCREAMING_SNAKE_CASE_  :		Tuple				      =    {"text": "string"}
		SCREAMING_SNAKE_CASE_  :		Any				      =    features.copy() if features else default_expected_features
		SCREAMING_SNAKE_CASE_  :		Dict				      =    (
		    Features({feature: Value(lowerCAmelCase	) for feature, dtype in features.items()}	) if features is not None else None
		)
		SCREAMING_SNAKE_CASE_  :		str				      =    TextDatasetReader({"train": text_path}   , features=lowerCAmelCase   , cache_dir=lowerCAmelCase	).read()
		_check_text_datasetdict(lowerCAmelCase   , lowerCAmelCase	)







@pytest.mark.parametrize("split"   , [None, NamedSplit("train"	), "train", "test"]	)
def       _snake_case	(   lowerCAmelCase							:   Optional[Any]   , lowerCAmelCase							:   Any   , lowerCAmelCase							:   Dict	):
		"""simple docstring"""







		if split:
				SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    {split: text_path}
		else:
				SCREAMING_SNAKE_CASE_  :		List[Any]				      =    "train"
				SCREAMING_SNAKE_CASE_  :		Tuple				      =    {"train": text_path, "test": text_path}
		SCREAMING_SNAKE_CASE_  :		Any				      =    tmp_path / "cache"
		SCREAMING_SNAKE_CASE_  :		List[str]				      =    {"text": "string"}
		SCREAMING_SNAKE_CASE_  :		str				      =    TextDatasetReader(lowerCAmelCase   , cache_dir=lowerCAmelCase	).read()
		_check_text_datasetdict(lowerCAmelCase   , lowerCAmelCase   , splits=list(path.keys()	)	)
		assert all(dataset[split].split == split for split in path.keys()	)
