
import unittest

from transformers.testing_utils import require_bsa
from transformers.utils import is_bsa_available

from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin


if is_bsa_available():
	from transformers import MarkupLMFeatureExtractor







class        A_   (			unittest.TestCase   ):




	'''simple docstring'''


	def __init__(       self		,    snake_case       ):
		lowercase       =    parent





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return {}






def 							UpperCAmelCase_		(							):
	lowercase       =    '<HTML>\n\n    <HEAD>\n    <TITLE>sample document</TITLE>\n    </HEAD>\n\n    <BODY BGCOLOR="FFFFFF">\n    <HR>\n    <a href="http://google.com">Goog</a>\n    <H1>This is one header</H1>\n    <H2>This is a another Header</H2>\n    <P>Travel from\n        <P>\n        <B>SFO to JFK</B>\n        <BR>\n        <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>\n        <HR>\n        <div style="color:#0000FF">\n            <h3>Traveler <b> name </b> is\n            <p> John Doe </p>\n        </div>'

	lowercase       =    '\n    <!DOCTYPE html>\n    <html>\n    <body>\n\n    <h1>My First Heading</h1>\n    <p>My first paragraph.</p>\n\n    </body>\n    </html>\n    '

	return [html_string_a, html_string_a]







@require_bsa
class        A_   (			__lowerCamelCase      ,	unittest.TestCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     int							 =       MarkupLMFeatureExtractor if is_bsa_available() else None





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    MarkupLMFeatureExtractionTester(self       )





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return self.feature_extract_tester.prepare_feat_extract_dict()





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		# Initialize feature_extractor
		lowercase       =    self.feature_extraction_class()

		# Test not batched input
		lowercase       =    get_html_strings()[0]
		lowercase       =    feature_extractor(snake_case       )

		# fmt: off
		lowercase       =    [['sample document', 'Goog', 'This is one header', 'This is a another Header', 'Travel from', 'SFO to JFK', 'on May 2, 2015 at 2:00 pm. For details go to confirm.com', 'Traveler', 'name', 'is', 'John Doe']]
		lowercase       =    [['/html/head/title', '/html/body/a', '/html/body/h1', '/html/body/h2', '/html/body/p', '/html/body/p/p/b[1]', '/html/body/p/p/b[2]/i', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/b', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/p']]
		# fmt: on

		self.assertEqual(encoding.nodes		,    snake_case       )
		self.assertEqual(encoding.xpaths		,    snake_case       )

		# Test batched
		lowercase       =    get_html_strings()
		lowercase       =    feature_extractor(snake_case       )

		# fmt: off
		lowercase       =    expected_nodes + [['My First Heading', 'My first paragraph.']]
		lowercase       =    expected_xpaths + [['/html/body/h1', '/html/body/p']]

		self.assertEqual(len(encoding.nodes       )		,    2       )
		self.assertEqual(len(encoding.xpaths       )		,    2       )

		self.assertEqual(encoding.nodes		,    snake_case       )
		self.assertEqual(encoding.xpaths		,    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


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






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE="no" ,							__SCREAMING_SNAKE_CASE = default_json_config_file ,							__SCREAMING_SNAKE_CASE = False							):
	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 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE							):
	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 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE							):
	lowercase       =    write_basic_config(args.mixed_precision ,							args.save_location							)
	if config_file:
		print(F'''accelerate configuration saved at {config_file}'''							)

