





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)

SCREAMING_SNAKE_CASE_						       =	logging.getLogger()



def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       int:


      '''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


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



@require_torch_tpu
class 		UpperCamelCase__			(				SCREAMING_SNAKE_CASE__       ):



     '''simple docstring'''

     def     SCREAMING_SNAKE_CASE__  (							self						:   Tuple	)					->		Optional[int]:




           '''simple docstring'''


           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(a_						,"""argv"""						,a_	):
                 SCREAMING_SNAKE_CASE		=						time()
                 xla_spawn.main()
                 SCREAMING_SNAKE_CASE		=						time()

                 SCREAMING_SNAKE_CASE		=						get_results(a_	)
                 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     SCREAMING_SNAKE_CASE__  (							self						:   Optional[Any]	)					->		Any:




           '''simple docstring'''


           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(a_						,"""argv"""						,a_	):
                 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


SCREAMING_SNAKE_CASE_						       =	logging.get_logger(__name__)


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



def        __lowercase       (				_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE = None			,						_SCREAMING_SNAKE_CASE = None			,						_SCREAMING_SNAKE_CASE = None			,						_SCREAMING_SNAKE_CASE = None			,						_SCREAMING_SNAKE_CASE = "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(_SCREAMING_SNAKE_CASE     ):
            if not isinstance(_SCREAMING_SNAKE_CASE			,						(Dataset, IterableDataset)     ):
                  if isinstance(_SCREAMING_SNAKE_CASE			,						(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(_SCREAMING_SNAKE_CASE     )}\n"""
                            F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(_SCREAMING_SNAKE_CASE     )     )}']"""     )
                  raise ValueError(
                      F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_SCREAMING_SNAKE_CASE     ).__name__}."""     )
            if i == 0:
                  SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						(
                      (Dataset, IterableDataset) if isinstance(_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     ) else (IterableDataset, Dataset)
                  )
            elif not isinstance(_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     ):
                  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(
                _SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						info=_SCREAMING_SNAKE_CASE			,						split=_SCREAMING_SNAKE_CASE			,						stopping_strategy=_SCREAMING_SNAKE_CASE     )
      else:
            return _interleave_iterable_datasets(
                _SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						info=_SCREAMING_SNAKE_CASE			,						split=_SCREAMING_SNAKE_CASE			,						stopping_strategy=_SCREAMING_SNAKE_CASE     )



def        __lowercase       (				_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE = None			,						_SCREAMING_SNAKE_CASE = None			,						_SCREAMING_SNAKE_CASE = 0			,						)			->       DatasetType:


      '''simple docstring'''


      if not dsets:
            raise ValueError("""Unable to concatenate an empty list of datasets."""     )
      for i, dataset in enumerate(_SCREAMING_SNAKE_CASE     ):
            if not isinstance(_SCREAMING_SNAKE_CASE			,						(Dataset, IterableDataset)     ):
                  if isinstance(_SCREAMING_SNAKE_CASE			,						(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(_SCREAMING_SNAKE_CASE     )}\n"""
                            F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(_SCREAMING_SNAKE_CASE     )     )}']"""     )
                  raise ValueError(
                      F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_SCREAMING_SNAKE_CASE     ).__name__}."""     )
            if i == 0:
                  SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						(
                      (Dataset, IterableDataset) if isinstance(_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     ) else (IterableDataset, Dataset)
                  )
            elif not isinstance(_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     ):
                  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(_SCREAMING_SNAKE_CASE			,						info=_SCREAMING_SNAKE_CASE			,						split=_SCREAMING_SNAKE_CASE			,						axis=_SCREAMING_SNAKE_CASE     )
      else:
            return _concatenate_iterable_datasets(_SCREAMING_SNAKE_CASE			,						info=_SCREAMING_SNAKE_CASE			,						split=_SCREAMING_SNAKE_CASE			,						axis=_SCREAMING_SNAKE_CASE     )


