


import re
import tempfile
from pathlib import Path

import pytest
import yaml

from datasets.utils.readme import ReadMe


# @pytest.fixture
# def example_yaml_structure():

lowerCAmelCase	=							yaml.safe_load(
    '\\nname: ""\nallow_empty: false\nallow_empty_text: true\nsubsections:\n  - name: "Dataset Card for X" # First-level markdown heading\n    allow_empty: false\n    allow_empty_text: true\n    subsections:\n      - name: "Table of Contents"\n        allow_empty: false\n        allow_empty_text: false\n        subsections: null\n      - name: "Dataset Description"\n        allow_empty: false\n        allow_empty_text: false\n        subsections:\n          - name: "Dataset Summary"\n            allow_empty: false\n            allow_empty_text: false\n            subsections: null\n          - name: "Supported Tasks and Leaderboards"\n            allow_empty: true\n            allow_empty_text: true\n            subsections: null\n          - name: Languages\n            allow_empty: false\n            allow_empty_text: true\n            subsections: null\n'
)


lowerCAmelCase	=							{
    'name': 'root',
    'text': '',
    'is_empty_text': True,
    'subsections': [
        {
            'name': 'Dataset Card for My Dataset',
            'text': '',
            'is_empty_text': True,
            'subsections': [
                {'name': 'Table of Contents', 'text': 'Some text here.', 'is_empty_text': False, 'subsections': []},
                {
                    'name': 'Dataset Description',
                    'text': 'Some text here.',
                    'is_empty_text': False,
                    'subsections': [
                        {
                            'name': 'Dataset Summary',
                            'text': 'Some text here.',
                            'is_empty_text': False,
                            'subsections': [],
                        },
                        {
                            'name': 'Supported Tasks and Leaderboards',
                            'text': '',
                            'is_empty_text': True,
                            'subsections': [],
                        },
                        {'name': 'Languages', 'text': 'Language Text', 'is_empty_text': False, 'subsections': []},
                    ],
                },
            ],
        }
    ],
}


lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'


lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n#### Extra Ignored Subsection\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							{
    'name': 'root',
    'text': '',
    'is_empty_text': True,
    'subsections': [
        {
            'name': 'Dataset Card for My Dataset',
            'text': '',
            'is_empty_text': True,
            'subsections': [
                {'name': 'Table of Contents', 'text': 'Some text here.', 'is_empty_text': False, 'subsections': []},
                {
                    'name': 'Dataset Description',
                    'text': 'Some text here.',
                    'is_empty_text': False,
                    'subsections': [
                        {
                            'name': 'Dataset Summary',
                            'text': 'Some text here.',
                            'is_empty_text': False,
                            'subsections': [
                                {
                                    'name': 'Extra Ignored Subsection',
                                    'text': '',
                                    'is_empty_text': True,
                                    'subsections': [],
                                }
                            ],
                        },
                        {
                            'name': 'Supported Tasks and Leaderboards',
                            'text': '',
                            'is_empty_text': True,
                            'subsections': [],
                        },
                        {'name': 'Languages', 'text': 'Language Text', 'is_empty_text': False, 'subsections': []},
                    ],
                },
            ],
        }
    ],
}

lowerCAmelCase	=							'\\n---\n---\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							(
    'The following issues were found for the README at `{path}`:\n-\tEmpty YAML markers are present in the README.'
)

lowerCAmelCase	=							'\\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							(
    'The following issues were found for the README at `{path}`:\n-\tNo YAML markers are present in the README.'
)

lowerCAmelCase	=							'\\n---\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tOnly the start of YAML tags present in the README.'

lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'
lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Summary` but it is empty.\n-\tExpected some text in section `Dataset Summary` but it is empty (text in subsections are ignored).'


lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n'
lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Card for My Dataset` but it is empty.\n-\tSection `Dataset Card for My Dataset` expected the following subsections: `Table of Contents`, `Dataset Description`. Found \'None\'.'

lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tSection `Dataset Description` is missing subsection: `Supported Tasks and Leaderboards`.'


lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\n'

lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Languages` but it is empty.'

lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'
lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README.'

lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n# Dataset Card My Dataset\n'

lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tThe README has several first-level headings: `Dataset Card for My Dataset`, `Dataset Card My Dataset`. Only one heading is expected. Skipping further validation for this README.'

lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tNo first-level heading starting with `Dataset Card for` found in README. Skipping further validation for this README.'

lowerCAmelCase	=							''

lowerCAmelCase	=							'The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README.\n-\tNo YAML markers are present in the README.'

lowerCAmelCase	=							'\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n'

lowerCAmelCase	=							'The following issues were found while parsing the README at `{path}`:\n-\tMultiple sections with the same heading `Dataset Card for My Dataset` have been found. Please keep only one of these sections.'
@pytest.mark.parametrize(
    '''readme_md, expected_dict'''						,    [
        (README_CORRECT, CORRECT_DICT),
        (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		assert ReadMe.from_string(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					).to_dict() == expected_dict
@pytest.mark.parametrize(
    '''readme_md, expected_error'''						,    [
        (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML),
        (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML),
        (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML),
        (README_EMPTY, EXPECTED_ERROR_README_EMPTY),
        (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION),
        (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL),
        (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION),
        (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT),
        (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL),
        (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL),
        (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with pytest.raises(SCREAMING_SNAKE_CASE						,    match=re.escape(expected_error.format(path='''root'''					)					)					):
				lowercase__						=				ReadMe.from_string(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					)
				readme.validate()
@pytest.mark.parametrize(
    '''readme_md, expected_error'''						,    [
        (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with pytest.raises(SCREAMING_SNAKE_CASE						,    match=re.escape(expected_error.format(path='''root'''					)					)					):
				ReadMe.from_string(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					)
@pytest.mark.parametrize(
    '''readme_md,'''						,    [
        (README_MULTIPLE_SAME_HEADING_1),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		ReadMe.from_string(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    suppress_parsing_errors=SCREAMING_SNAKE_CASE					)
@pytest.mark.parametrize(
    '''readme_md, expected_dict'''						,    [
        (README_CORRECT, CORRECT_DICT),
        (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with tempfile.TemporaryDirectory() as tmp_dir:
				lowercase__						=				Path(SCREAMING_SNAKE_CASE					) / '''README.md'''
				with open(SCREAMING_SNAKE_CASE						,    '''w+'''					) as readme_file:
						readme_file.write(SCREAMING_SNAKE_CASE					)
				lowercase__						=				ReadMe.from_readme(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					).to_dict()
				assert out["name"] == path
				assert out["text"] == ""
				assert out["is_empty_text"]
				assert out["subsections"] == expected_dict["subsections"]
@pytest.mark.parametrize(
    '''readme_md, expected_error'''						,    [
        (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML),
        (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML),
        (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML),
        (README_EMPTY, EXPECTED_ERROR_README_EMPTY),
        (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION),
        (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL),
        (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION),
        (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT),
        (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL),
        (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL),
        (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with tempfile.TemporaryDirectory() as tmp_dir:
				lowercase__						=				Path(SCREAMING_SNAKE_CASE					) / '''README.md'''
				with open(SCREAMING_SNAKE_CASE						,    '''w+'''					) as readme_file:
						readme_file.write(SCREAMING_SNAKE_CASE					)
				lowercase__						=				expected_error.format(path=SCREAMING_SNAKE_CASE					)
				with pytest.raises(SCREAMING_SNAKE_CASE						,    match=re.escape(SCREAMING_SNAKE_CASE					)					):
						lowercase__						=				ReadMe.from_readme(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					)
						readme.validate()
@pytest.mark.parametrize(
    '''readme_md, expected_error'''						,    [
        (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with tempfile.TemporaryDirectory() as tmp_dir:
				lowercase__						=				Path(SCREAMING_SNAKE_CASE					) / '''README.md'''
				with open(SCREAMING_SNAKE_CASE						,    '''w+'''					) as readme_file:
						readme_file.write(SCREAMING_SNAKE_CASE					)
				lowercase__						=				expected_error.format(path=SCREAMING_SNAKE_CASE					)
				with pytest.raises(SCREAMING_SNAKE_CASE						,    match=re.escape(SCREAMING_SNAKE_CASE					)					):
						ReadMe.from_readme(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					)
@pytest.mark.parametrize(
    '''readme_md,'''						,    [
        (README_MULTIPLE_SAME_HEADING_1),
    ]						,    )
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with tempfile.TemporaryDirectory() as tmp_dir:
				lowercase__						=				Path(SCREAMING_SNAKE_CASE					) / '''README.md'''
				with open(SCREAMING_SNAKE_CASE						,    '''w+'''					) as readme_file:
						readme_file.write(SCREAMING_SNAKE_CASE					)
				ReadMe.from_readme(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    suppress_parsing_errors=SCREAMING_SNAKE_CASE					)






# 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


lowerCAmelCase	=							'Create a default config file for Accelerate with only a few flags set.'
def       _a			(				SCREAMING_SNAKE_CASE="no"						,    SCREAMING_SNAKE_CASE = default_json_config_file						,    SCREAMING_SNAKE_CASE = False					):


		"""simple docstring"""




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


		"""simple docstring"""




		lowercase__						=				parser.add_parser('''default'''						,    parents=SCREAMING_SNAKE_CASE						,    help=SCREAMING_SNAKE_CASE						,    formatter_class=SCREAMING_SNAKE_CASE					)
		parser.add_argument(
		    '''--config_file'''						,    default=SCREAMING_SNAKE_CASE						,    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=SCREAMING_SNAKE_CASE						,    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=SCREAMING_SNAKE_CASE					)
		return parser
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		lowercase__						=				write_basic_config(args.mixed_precision						,    args.save_location					)
		if config_file:
				print(f'accelerate configuration saved at {config_file}'					)


