
import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
 from transformers.trainer import DEFAULT_CALLBACKS

 from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel


class 			lowercase_		(      A       ):


       def __init__(    self				)						->    Any:
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=[]


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    List[Any]:
              self.events.append('''on_init_end'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Tuple:
              self.events.append('''on_train_begin'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Union[str, Any]:
              self.events.append('''on_train_end'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    str:
              self.events.append('''on_epoch_begin'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Dict:
              self.events.append('''on_epoch_end'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Union[str, Any]:
              self.events.append('''on_step_begin'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Tuple:
              self.events.append('''on_step_end'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    List[Any]:
              self.events.append('''on_evaluate'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Optional[int]:
              self.events.append('''on_predict'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    List[str]:
              self.events.append('''on_save'''				)


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    Tuple:
              self.events.append('''on_log'''				)





       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       **__A				)						->    List[str]:
              self.events.append('''on_prediction_step'''				)






@require_torch
class 			lowercase_		(      unittest.TestCase       ):


       def 	_snake_case				(    self				)						->    str:
              SCREAMING_SNAKE_CASE_							:				Optional[int]		=tempfile.mkdtemp()


       def 	_snake_case				(    self				)						->    List[str]:
              shutil.rmtree(self.output_dir				)


       def 	_snake_case				(    self						,       __A=0						,       __A=0						,       __A=64						,       __A=64						,       __A=None						,       __A=False						,       **__A				)						->    List[Any]:
              # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
              # its set to False since the tests later on depend on its value.
              SCREAMING_SNAKE_CASE_							:				Optional[Any]		=RegressionDataset(length=__A				)
              SCREAMING_SNAKE_CASE_							:				List[str]		=RegressionDataset(length=__A				)
              SCREAMING_SNAKE_CASE_							:				Tuple		=RegressionModelConfig(a=__A						,       b=__A				)
              SCREAMING_SNAKE_CASE_							:				Dict		=RegressionPreTrainedModel(__A				)

              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=TrainingArguments(self.output_dir						,       disable_tqdm=__A						,       report_to=[]						,       **__A				)
              return Trainer(
                  __A						,       __A						,       train_dataset=__A						,       eval_dataset=__A						,       callbacks=__A						,       )


       def 	_snake_case				(    self						,       __A						,       __A				)						->    Dict:
              self.assertEqual(len(__A				)						,       len(__A				)				)

              # Order doesn't matter
              SCREAMING_SNAKE_CASE_							:				int		=sorted(__A						,       key=lambda __A				: cb.__name__ if isinstance(__A						,       __A				) else cb.__class__.__name__				)
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=sorted(__A						,       key=lambda __A				: cb.__name__ if isinstance(__A						,       __A				) else cb.__class__.__name__				)

              for cba, cba in zip(__A						,       __A				):
                     if isinstance(__A						,       __A				) and isinstance(__A						,       __A				):
                            self.assertEqual(__A						,       __A				)
                     elif isinstance(__A						,       __A				) and not isinstance(__A						,       __A				):
                            self.assertEqual(__A						,       cba.__class__				)
                     elif not isinstance(__A						,       __A				) and isinstance(__A						,       __A				):
                            self.assertEqual(cba.__class__						,       __A				)
                     else:
                            self.assertEqual(__A						,       __A				)


       def 	_snake_case				(    self						,       __A				)						->    Optional[Any]:
              SCREAMING_SNAKE_CASE_							:				str		=['''on_init_end''', '''on_train_begin''']
              SCREAMING_SNAKE_CASE_							:				List[Any]		=0
              SCREAMING_SNAKE_CASE_							:				List[Any]		=len(trainer.get_eval_dataloader()				)
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=['''on_prediction_step'''] * len(trainer.get_eval_dataloader()				) + ['''on_log''', '''on_evaluate''']
              for _ in range(trainer.state.num_train_epochs				):
                     expected_events.append('''on_epoch_begin'''				)
                     for _ in range(__A				):
                            step += 1
                            expected_events += ["on_step_begin", "on_step_end"]
                            if step % trainer.args.logging_steps == 0:
                                   expected_events.append('''on_log'''				)
                            if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
                                   expected_events += evaluation_events.copy()
                            if step % trainer.args.save_steps == 0:
                                   expected_events.append('''on_save'''				)
                     expected_events.append('''on_epoch_end'''				)
                     if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
                            expected_events += evaluation_events.copy()
              expected_events += ["on_log", "on_train_end"]
              return expected_events


       def 	_snake_case				(    self				)						->    str:
              SCREAMING_SNAKE_CASE_							:				Tuple		=self.get_trainer()
              SCREAMING_SNAKE_CASE_							:				List[Any]		=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              # Callbacks passed at init are added to the default callbacks
              SCREAMING_SNAKE_CASE_							:				Dict		=self.get_trainer(callbacks=[MyTestTrainerCallback]				)
              expected_callbacks.append(__A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
              SCREAMING_SNAKE_CASE_							:				Optional[Any]		=self.get_trainer(disable_tqdm=__A				)
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=DEFAULT_CALLBACKS.copy() + [PrinterCallback]
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)


       def 	_snake_case				(    self				)						->    Dict:
              SCREAMING_SNAKE_CASE_							:				str		=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
              SCREAMING_SNAKE_CASE_							:				Tuple		=self.get_trainer()

              # We can add, pop, or remove by class name
              trainer.remove_callback(__A				)
              expected_callbacks.remove(__A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              SCREAMING_SNAKE_CASE_							:				Any		=self.get_trainer()
              SCREAMING_SNAKE_CASE_							:				Dict		=trainer.pop_callback(__A				)
              self.assertEqual(cb.__class__						,       __A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              trainer.add_callback(__A				)
              expected_callbacks.insert(0						,       __A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              # We can also add, pop, or remove by instance
              SCREAMING_SNAKE_CASE_							:				Any		=self.get_trainer()
              SCREAMING_SNAKE_CASE_							:				str		=trainer.callback_handler.callbacks[0]
              trainer.remove_callback(__A				)
              expected_callbacks.remove(__A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              SCREAMING_SNAKE_CASE_							:				Any		=self.get_trainer()
              SCREAMING_SNAKE_CASE_							:				Dict		=trainer.callback_handler.callbacks[0]
              SCREAMING_SNAKE_CASE_							:				List[str]		=trainer.pop_callback(__A				)
              self.assertEqual(__A						,       __A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)

              trainer.add_callback(__A				)
              expected_callbacks.insert(0						,       __A				)
              self.check_callbacks_equality(trainer.callback_handler.callbacks						,       __A				)





       def 	_snake_case				(    self				)						->    str:
              import warnings

              # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
              warnings.simplefilter(action='''ignore'''						,       category=__A				)

              SCREAMING_SNAKE_CASE_							:				int		=self.get_trainer(callbacks=[MyTestTrainerCallback]				)
              trainer.train()
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=trainer.callback_handler.callbacks[-2].events
              self.assertEqual(__A						,       self.get_expected_events(__A				)				)

              # Independent log/save/eval
              SCREAMING_SNAKE_CASE_							:				int		=self.get_trainer(callbacks=[MyTestTrainerCallback]						,       logging_steps=5				)
              trainer.train()
              SCREAMING_SNAKE_CASE_							:				str		=trainer.callback_handler.callbacks[-2].events
              self.assertEqual(__A						,       self.get_expected_events(__A				)				)

              SCREAMING_SNAKE_CASE_							:				Optional[int]		=self.get_trainer(callbacks=[MyTestTrainerCallback]						,       save_steps=5				)
              trainer.train()
              SCREAMING_SNAKE_CASE_							:				Tuple		=trainer.callback_handler.callbacks[-2].events
              self.assertEqual(__A						,       self.get_expected_events(__A				)				)

              SCREAMING_SNAKE_CASE_							:				Any		=self.get_trainer(callbacks=[MyTestTrainerCallback]						,       eval_steps=5						,       evaluation_strategy='''steps'''				)
              trainer.train()
              SCREAMING_SNAKE_CASE_							:				Dict		=trainer.callback_handler.callbacks[-2].events
              self.assertEqual(__A						,       self.get_expected_events(__A				)				)

              SCREAMING_SNAKE_CASE_							:				Optional[int]		=self.get_trainer(callbacks=[MyTestTrainerCallback]						,       evaluation_strategy='''epoch'''				)
              trainer.train()
              SCREAMING_SNAKE_CASE_							:				Dict		=trainer.callback_handler.callbacks[-2].events
              self.assertEqual(__A						,       self.get_expected_events(__A				)				)

              # A bit of everything
              SCREAMING_SNAKE_CASE_							:				Tuple		=self.get_trainer(
                  callbacks=[MyTestTrainerCallback]						,       logging_steps=3						,       save_steps=10						,       eval_steps=5						,       evaluation_strategy='''steps'''						,       )
              trainer.train()
              SCREAMING_SNAKE_CASE_							:				Dict		=trainer.callback_handler.callbacks[-2].events
              self.assertEqual(__A						,       self.get_expected_events(__A				)				)

              # warning should be emitted for duplicated callbacks
              with patch('''transformers.trainer_callback.logger.warning'''				) as warn_mock:
                     SCREAMING_SNAKE_CASE_							:				List[Any]		=self.get_trainer(
                         callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]						,       )
                     assert str(__A				) in warn_mock.call_args[0][0]


import importlib
import os

import fsspec
import pytest
from fsspec import register_implementation
from fsspec.registry import _registry as _fsspec_registry

from datasets.filesystems import COMPRESSION_FILESYSTEMS, HfFileSystem, extract_path_from_uri, is_remote_filesystem

from .utils import require_lza, require_zstandard
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     List[Any]				)			->						Dict:
       assert "mock" in _fsspec_registry
       assert "bz2" in _fsspec_registry
def 							SCREAMING_SNAKE_CASE_	(					)			->						Union[str, Any]:
       assert "mock" not in _fsspec_registry
       assert "bz2" in _fsspec_registry
def 							SCREAMING_SNAKE_CASE_	(					)			->						Dict:
       SCREAMING_SNAKE_CASE_							:				Optional[Any]		='''mock-s3-bucket'''
       SCREAMING_SNAKE_CASE_							:				Any		=f's3://{mock_bucket}'
       SCREAMING_SNAKE_CASE_							:				Dict		=extract_path_from_uri(UpperCAmelCase_				)
       assert dataset_path.startswith('''s3://'''				) is False

       SCREAMING_SNAKE_CASE_							:				Dict		='''./local/path'''
       SCREAMING_SNAKE_CASE_							:				str		=extract_path_from_uri(UpperCAmelCase_				)
       assert dataset_path == new_dataset_path
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     Union[str, Any]				)			->						List[str]:
       SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=is_remote_filesystem(UpperCAmelCase_				)
       assert is_remote is True

       SCREAMING_SNAKE_CASE_							:				Dict		=fsspec.filesystem('''file'''				)

       SCREAMING_SNAKE_CASE_							:				Optional[int]		=is_remote_filesystem(UpperCAmelCase_				)
       assert is_remote is False
@pytest.mark.parametrize('''compression_fs_class'''			,							UpperCAmelCase_				)
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     Union[str, Any]			,							UpperCAmelCase_	:     Any			,							UpperCAmelCase_	:     Optional[Any]			,							UpperCAmelCase_	:     Optional[Any]			,							UpperCAmelCase_	:     int			,							UpperCAmelCase_	:     str			,							UpperCAmelCase_	:     Any				)			->						List[Any]:
       SCREAMING_SNAKE_CASE_							:				str		={'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_file, '''bz2''': bza_file, '''lz4''': lza_file}
       SCREAMING_SNAKE_CASE_							:				List[Any]		=input_paths[compression_fs_class.protocol]
       if input_path is None:
              SCREAMING_SNAKE_CASE_							:				List[str]		=f'for \'{compression_fs_class.protocol}\' compression protocol, '
              if compression_fs_class.protocol == "lz4":
                     reason += require_lza.kwargs["reason"]
              elif compression_fs_class.protocol == "zstd":
                     reason += require_zstandard.kwargs["reason"]
              pytest.skip(UpperCAmelCase_				)
       SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=fsspec.filesystem(compression_fs_class.protocol			,							fo=UpperCAmelCase_				)
       assert isinstance(UpperCAmelCase_			,							UpperCAmelCase_				)
       SCREAMING_SNAKE_CASE_							:				str		=os.path.basename(UpperCAmelCase_				)
       SCREAMING_SNAKE_CASE_							:				Optional[Any]		=expected_filename[: expected_filename.rindex('''.'''				)]
       assert fs.glob('''*'''				) == [expected_filename]
       with fs.open(UpperCAmelCase_			,							'''r'''			,							encoding='''utf-8'''				) as f, open(UpperCAmelCase_			,							encoding='''utf-8'''				) as expected_file:
              assert f.read() == expected_file.read()
@pytest.mark.parametrize('''protocol'''			,							['''zip''', '''gzip''']				)
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     Any			,							UpperCAmelCase_	:     Union[str, Any]			,							UpperCAmelCase_	:     str				)			->						Any:
       SCREAMING_SNAKE_CASE_							:				Tuple		={'''zip''': zip_jsonl_path, '''gzip''': jsonl_gz_path}
       SCREAMING_SNAKE_CASE_							:				List[Any]		=compressed_file_paths[protocol]
       SCREAMING_SNAKE_CASE_							:				str		='''dataset.jsonl'''
       SCREAMING_SNAKE_CASE_							:				Dict		=f'{protocol}://{member_file_path}::{compressed_file_path}'
       SCREAMING_SNAKE_CASE_	,     *SCREAMING_SNAKE_CASE_							:				Dict		=fsspec.get_fs_token_paths(UpperCAmelCase_				)
       assert fs.isfile(UpperCAmelCase_				)
       assert not fs.isfile('''non_existing_''' + member_file_path				)
@pytest.mark.integration
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     List[str]			,							UpperCAmelCase_	:     Optional[int]			,							UpperCAmelCase_	:     Optional[int]			,							UpperCAmelCase_	:     int				)			->						int:
       SCREAMING_SNAKE_CASE_							:				Dict		=hf_api.dataset_info(UpperCAmelCase_			,							token=UpperCAmelCase_				)
       SCREAMING_SNAKE_CASE_							:				Optional[int]		=HfFileSystem(repo_info=UpperCAmelCase_			,							token=UpperCAmelCase_				)
       assert sorted(hffs.glob('''*'''				)				) == [".gitattributes", "data"]
       assert hffs.isdir('''data'''				)
       assert hffs.isfile('''.gitattributes'''				) and hffs.isfile('''data/text_data.txt'''				)
       with open(UpperCAmelCase_				) as f:
              assert hffs.open('''data/text_data.txt'''			,							'''r'''				).read() == f.read()

def 							SCREAMING_SNAKE_CASE_	(					)			->						Union[str, Any]:
       SCREAMING_SNAKE_CASE_							:				Tuple		='''bz2'''

       # Import module
       import datasets.filesystems

       # Overwrite protocol and reload
       register_implementation(UpperCAmelCase_			,							UpperCAmelCase_			,							clobber=UpperCAmelCase_				)
       with pytest.warns(UpperCAmelCase_				) as warning_info:
              importlib.reload(datasets.filesystems				)

       assert len(UpperCAmelCase_				) == 1
       assert (
           str(warning_info[0].message				)
           == f'A filesystem protocol was already set for {protocol} and will be overwritten.'
       )
