
import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

__snake_case								=  logging.getLogger()






def 							_lowercase							(     UpperCamelCase_		)		->		Tuple:
					'''simple docstring'''


					SCREAMING_SNAKE_CASE__													= {}
					SCREAMING_SNAKE_CASE__													= os.path.join(UpperCamelCase_	,	'all_results.json'		)
					if os.path.exists(UpperCamelCase_		):
										with open(UpperCamelCase_	,	'r'		) as f:
															SCREAMING_SNAKE_CASE__													= json.load(UpperCamelCase_		)
					else:
										raise ValueError(F'can\'t find {path}'		)
					return results


__snake_case								=  logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




@require_torch_tpu
class 						lowercase__  (						_UpperCAmelCase  ):
	def      A_				(			self							:   str							):
						import xla_spawn

						SCREAMING_SNAKE_CASE__													= self.get_auto_remove_tmp_dir()
						SCREAMING_SNAKE_CASE__													= F'\n            ./examples/pytorch/text-classification/run_glue.py\n            --num_cores=8\n            ./examples/pytorch/text-classification/run_glue.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --do_train\n            --do_eval\n            --debug tpu_metrics_debug\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --max_steps=10\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            '.split()

						with patch.object(UpperCAmelCase_       ,   'argv'       ,   UpperCAmelCase_							):
											SCREAMING_SNAKE_CASE__													= time()
											xla_spawn.main()
											SCREAMING_SNAKE_CASE__													= time()

											SCREAMING_SNAKE_CASE__													= get_results(UpperCAmelCase_							)
											self.assertGreaterEqual(result['eval_accuracy']       ,   0.75							)

											# Assert that the script takes less than 500 seconds to make sure it doesn't hang.
											self.assertLess(end - start       ,   500							)





	def      A_				(			self							:   List[Any]							):
						import xla_spawn

						SCREAMING_SNAKE_CASE__													= '\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            '.split()
						with patch.object(UpperCAmelCase_       ,   'argv'       ,   UpperCAmelCase_							):
											xla_spawn.main()







from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


__snake_case								=  logging.get_logger(__name__)


__snake_case								=  TypeVar("""DatasetType""", Dataset, IterableDataset)






def 							_lowercase							(     UpperCamelCase_	,	UpperCamelCase_ = None	,	UpperCamelCase_ = None	,	UpperCamelCase_ = None	,	UpperCamelCase_ = None	,	UpperCamelCase_ = "first_exhausted"	,	)		->		DatasetType:
					'''simple docstring'''


					from .arrow_dataset import Dataset
					from .iterable_dataset import IterableDataset

					if not datasets:
										raise ValueError('Unable to interleave an empty list of datasets.'		)
					for i, dataset in enumerate(UpperCamelCase_		):
										if not isinstance(UpperCamelCase_	,	(Dataset, IterableDataset)		):
															if isinstance(UpperCamelCase_	,	(DatasetDict, IterableDatasetDict)		):
																				if not dataset:
																									raise ValueError(
																									    F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} '
																									    'is an empty dataset dictionary.'		)
																				raise ValueError(
																				    F'Dataset at position {i} has at least one split: {list(UpperCamelCase_		)}\n'
																				    F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(UpperCamelCase_		)		)}\']'		)
															raise ValueError(
															    F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(UpperCamelCase_		).__name__}.'		)
										if i == 0:
															SCREAMING_SNAKE_CASE__						, SCREAMING_SNAKE_CASE__													= (
															    (Dataset, IterableDataset) if isinstance(UpperCamelCase_	,	UpperCamelCase_		) else (IterableDataset, Dataset)
															)
										elif not isinstance(UpperCamelCase_	,	UpperCamelCase_		):
															raise ValueError(
															    F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.'		)
					if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
										raise ValueError(F'{stopping_strategy} is not supported. Please enter a valid stopping_strategy.'		)
					if dataset_type is Dataset:
										return _interleave_map_style_datasets(
										    UpperCamelCase_	,	UpperCamelCase_	,	UpperCamelCase_	,	info=UpperCamelCase_	,	split=UpperCamelCase_	,	stopping_strategy=UpperCamelCase_		)
					else:
										return _interleave_iterable_datasets(
										    UpperCamelCase_	,	UpperCamelCase_	,	UpperCamelCase_	,	info=UpperCamelCase_	,	split=UpperCamelCase_	,	stopping_strategy=UpperCamelCase_		)






def 							_lowercase							(     UpperCamelCase_	,	UpperCamelCase_ = None	,	UpperCamelCase_ = None	,	UpperCamelCase_ = 0	,	)		->		DatasetType:
					'''simple docstring'''


					if not dsets:
										raise ValueError('Unable to concatenate an empty list of datasets.'		)
					for i, dataset in enumerate(UpperCamelCase_		):
										if not isinstance(UpperCamelCase_	,	(Dataset, IterableDataset)		):
															if isinstance(UpperCamelCase_	,	(DatasetDict, IterableDatasetDict)		):
																				if not dataset:
																									raise ValueError(
																									    F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} '
																									    'is an empty dataset dictionary.'		)
																				raise ValueError(
																				    F'Dataset at position {i} has at least one split: {list(UpperCamelCase_		)}\n'
																				    F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(UpperCamelCase_		)		)}\']'		)
															raise ValueError(
															    F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(UpperCamelCase_		).__name__}.'		)
										if i == 0:
															SCREAMING_SNAKE_CASE__						, SCREAMING_SNAKE_CASE__													= (
															    (Dataset, IterableDataset) if isinstance(UpperCamelCase_	,	UpperCamelCase_		) else (IterableDataset, Dataset)
															)
										elif not isinstance(UpperCamelCase_	,	UpperCamelCase_		):
															raise ValueError(
															    F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.'		)
					if dataset_type is Dataset:
										return _concatenate_map_style_datasets(UpperCamelCase_	,	info=UpperCamelCase_	,	split=UpperCamelCase_	,	axis=UpperCamelCase_		)
					else:
										return _concatenate_iterable_datasets(UpperCamelCase_	,	info=UpperCamelCase_	,	split=UpperCamelCase_	,	axis=UpperCamelCase_		)





