



'''simple docstring'''
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 _A     (			UpperCAmelCase__			):





		def __init__(   self )				->   Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase      :					str   =   []





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Tuple:

					'''simple docstring'''
					self.events.append("""on_init_end""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Optional[Any]:

					'''simple docstring'''
					self.events.append("""on_train_begin""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Any:

					'''simple docstring'''
					self.events.append("""on_train_end""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Dict:

					'''simple docstring'''
					self.events.append("""on_epoch_begin""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Any:

					'''simple docstring'''
					self.events.append("""on_epoch_end""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Union[str, Any]:

					'''simple docstring'''
					self.events.append("""on_step_begin""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   List[str]:

					'''simple docstring'''
					self.events.append("""on_step_end""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Dict:

					'''simple docstring'''
					self.events.append("""on_evaluate""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   List[Any]:

					'''simple docstring'''
					self.events.append("""on_predict""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Tuple:

					'''simple docstring'''
					self.events.append("""on_save""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Optional[int]:

					'''simple docstring'''
					self.events.append("""on_log""" )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				**__UpperCAmelCase )				->   Optional[Any]:

					'''simple docstring'''
					self.events.append("""on_prediction_step""" )



@require_torch
class _A     (			unittest.TestCase			):





		def 							__A				(   self )				->   List[str]:

					'''simple docstring'''
					__UpperCAmelCase      :					Optional[Any]   =   tempfile.mkdtemp()





		def 							__A				(   self )				->   Dict:

					'''simple docstring'''
					shutil.rmtree(self.output_dir )





		def 							__A				(   self       ,				__UpperCAmelCase=0       ,				__UpperCAmelCase=0       ,				__UpperCAmelCase=64       ,				__UpperCAmelCase=64       ,				__UpperCAmelCase=None       ,				__UpperCAmelCase=False       ,				**__UpperCAmelCase )				->   Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					str   =   RegressionDataset(length=lowerCamelCase__ )
					__UpperCAmelCase      :					str   =   RegressionDataset(length=lowerCamelCase__ )
					__UpperCAmelCase      :					Tuple   =   RegressionModelConfig(a=lowerCamelCase__       ,				b=lowerCamelCase__ )
					__UpperCAmelCase      :					Tuple   =   RegressionPreTrainedModel(lowerCamelCase__ )

					__UpperCAmelCase      :					Optional[Any]   =   TrainingArguments(self.output_dir       ,				disable_tqdm=lowerCamelCase__       ,				report_to=[]       ,				**lowerCamelCase__ )
					return Trainer(
					    lowerCamelCase__       ,				lowerCamelCase__       ,				train_dataset=lowerCamelCase__       ,				eval_dataset=lowerCamelCase__       ,				callbacks=lowerCamelCase__       ,				)





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   Tuple:

					'''simple docstring'''
					self.assertEqual(len(lowerCamelCase__ )       ,				len(lowerCamelCase__ ) )

					# Order doesn't matter
					__UpperCAmelCase      :					Tuple   =   sorted(lowerCamelCase__       ,				key=lambda __UpperCAmelCase : cb.__name__ if isinstance(lowerCamelCase__       ,				lowerCamelCase__ ) else cb.__class__.__name__ )
					__UpperCAmelCase      :					Any   =   sorted(lowerCamelCase__       ,				key=lambda __UpperCAmelCase : cb.__name__ if isinstance(lowerCamelCase__       ,				lowerCamelCase__ ) else cb.__class__.__name__ )

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





		def 							__A				(   self       ,				__UpperCAmelCase )				->   Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase      :					Tuple   =   ["on_init_end", "on_train_begin"]
					__UpperCAmelCase      :					Any   =   0
					__UpperCAmelCase      :					int   =   len(trainer.get_eval_dataloader() )
					__UpperCAmelCase      :					int   =   ["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(lowerCamelCase__ ):
											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 							__A				(   self )				->   Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					List[str]   =   self.get_trainer()
					__UpperCAmelCase      :					Any   =   DEFAULT_CALLBACKS.copy() + [ProgressCallback]
					self.check_callbacks_equality(trainer.callback_handler.callbacks       ,				lowerCamelCase__ )

					# Callbacks passed at init are added to the default callbacks
					__UpperCAmelCase      :					List[Any]   =   self.get_trainer(callbacks=[MyTestTrainerCallback] )
					expected_callbacks.append(lowerCamelCase__ )
					self.check_callbacks_equality(trainer.callback_handler.callbacks       ,				lowerCamelCase__ )

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





		def 							__A				(   self )				->   Tuple:

					'''simple docstring'''
					__UpperCAmelCase      :					Union[str, Any]   =   DEFAULT_CALLBACKS.copy() + [ProgressCallback]
					__UpperCAmelCase      :					Dict   =   self.get_trainer()

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

					__UpperCAmelCase      :					Any   =   self.get_trainer()
					__UpperCAmelCase      :					str   =   trainer.pop_callback(lowerCamelCase__ )
					self.assertEqual(cb.__class__       ,				lowerCamelCase__ )
					self.check_callbacks_equality(trainer.callback_handler.callbacks       ,				lowerCamelCase__ )

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

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

					__UpperCAmelCase      :					Tuple   =   self.get_trainer()
					__UpperCAmelCase      :					List[Any]   =   trainer.callback_handler.callbacks[0]
					__UpperCAmelCase      :					Dict   =   trainer.pop_callback(lowerCamelCase__ )
					self.assertEqual(lowerCamelCase__       ,				lowerCamelCase__ )
					self.check_callbacks_equality(trainer.callback_handler.callbacks       ,				lowerCamelCase__ )

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





		def 							__A				(   self )				->   Dict:

					'''simple docstring'''
					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=lowerCamelCase__ )

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

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

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

					__UpperCAmelCase      :					Union[str, Any]   =   self.get_trainer(callbacks=[MyTestTrainerCallback]       ,				eval_steps=5       ,				evaluation_strategy="""steps""" )
					trainer.train()
					__UpperCAmelCase      :					Tuple   =   trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__       ,				self.get_expected_events(lowerCamelCase__ ) )

					__UpperCAmelCase      :					str   =   self.get_trainer(callbacks=[MyTestTrainerCallback]       ,				evaluation_strategy="""epoch""" )
					trainer.train()
					__UpperCAmelCase      :					Any   =   trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__       ,				self.get_expected_events(lowerCamelCase__ ) )

					# A bit of everything
					__UpperCAmelCase      :					Union[str, Any]   =   self.get_trainer(
					    callbacks=[MyTestTrainerCallback]       ,				logging_steps=3       ,				save_steps=10       ,				eval_steps=5       ,				evaluation_strategy="""steps"""       ,				)
					trainer.train()
					__UpperCAmelCase      :					Optional[Any]   =   trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__       ,				self.get_expected_events(lowerCamelCase__ ) )

					# warning should be emitted for duplicated callbacks
					with patch("""transformers.trainer_callback.logger.warning""" ) as warn_mock:
								__UpperCAmelCase      :					int   =   self.get_trainer(
								    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]       ,				)
								assert str(lowerCamelCase__ ) in warn_mock.call_args[0][0]








'''simple docstring'''


import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases






def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase      ):
		assert isinstance(__lowerCAmelCase					,       __lowerCAmelCase      )
		assert dataset.num_rows == 4
		assert dataset.num_columns == 1
		assert dataset.column_names == ["text"]
		for feature, expected_dtype in expected_features.items():
				assert dataset.features[feature].dtype == expected_dtype






@pytest.mark.parametrize("keep_in_memory"					,       [False, True]      )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				Union[str, Any]	    =					tmp_path / "cache"
		_UpperCAmelCase						:				int	    =					{"text": "string"}
		with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
				_UpperCAmelCase						:				Union[str, Any]	    =					TextDatasetReader(__lowerCAmelCase					,       cache_dir=__lowerCAmelCase					,       keep_in_memory=__lowerCAmelCase      ).read()
		_check_text_dataset(__lowerCAmelCase					,       __lowerCAmelCase      )






@pytest.mark.parametrize(
    "features"					,       [
        None,
        {"text": "string"},
        {"text": "int32"},
        {"text": "float32"},
    ]					,       )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				List[Any]	    =					tmp_path / "cache"
		_UpperCAmelCase						:				Any	    =					{"text": "string"}
		_UpperCAmelCase						:				Optional[Any]	    =					features.copy() if features else default_expected_features
		_UpperCAmelCase						:				Union[str, Any]	    =					(
		    Features({feature: Value(__lowerCAmelCase      ) for feature, dtype in features.items()}      ) if features is not None else None
		)
		_UpperCAmelCase						:				Union[str, Any]	    =					TextDatasetReader(__lowerCAmelCase					,       features=__lowerCAmelCase					,       cache_dir=__lowerCAmelCase      ).read()
		_check_text_dataset(__lowerCAmelCase					,       __lowerCAmelCase      )






@pytest.mark.parametrize("split"					,       [None, NamedSplit("train"      ), "train", "test"]      )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				Any	    =					tmp_path / "cache"
		_UpperCAmelCase						:				Dict	    =					{"text": "string"}
		_UpperCAmelCase						:				Union[str, Any]	    =					TextDatasetReader(__lowerCAmelCase					,       cache_dir=__lowerCAmelCase					,       split=__lowerCAmelCase      ).read()
		_check_text_dataset(__lowerCAmelCase					,       __lowerCAmelCase      )
		assert dataset.split == split if split else "train"






@pytest.mark.parametrize("path_type"					,       [str, list]      )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		if issubclass(__lowerCAmelCase					,       __lowerCAmelCase      ):
				_UpperCAmelCase						:				Union[str, Any]	    =					text_path
		elif issubclass(__lowerCAmelCase					,       __lowerCAmelCase      ):
				_UpperCAmelCase						:				int	    =					[text_path]
		_UpperCAmelCase						:				List[Any]	    =					tmp_path / "cache"
		_UpperCAmelCase						:				Union[str, Any]	    =					{"text": "string"}
		_UpperCAmelCase						:				Optional[int]	    =					TextDatasetReader(__lowerCAmelCase					,       cache_dir=__lowerCAmelCase      ).read()
		_check_text_dataset(__lowerCAmelCase					,       __lowerCAmelCase      )






def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase=("train",)      ):
		assert isinstance(__lowerCAmelCase					,       __lowerCAmelCase      )
		for split in splits:
				_UpperCAmelCase						:				List[str]	    =					dataset_dict[split]
				assert dataset.num_rows == 4
				assert dataset.num_columns == 1
				assert dataset.column_names == ["text"]
				for feature, expected_dtype in expected_features.items():
						assert dataset.features[feature].dtype == expected_dtype






@pytest.mark.parametrize("keep_in_memory"					,       [False, True]      )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				List[Any]	    =					tmp_path / "cache"
		_UpperCAmelCase						:				Tuple	    =					{"text": "string"}
		with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
				_UpperCAmelCase						:				Any	    =					TextDatasetReader({"train": text_path}					,       cache_dir=__lowerCAmelCase					,       keep_in_memory=__lowerCAmelCase      ).read()
		_check_text_datasetdict(__lowerCAmelCase					,       __lowerCAmelCase      )






@pytest.mark.parametrize(
    "features"					,       [
        None,
        {"text": "string"},
        {"text": "int32"},
        {"text": "float32"},
    ]					,       )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				List[Any]	    =					tmp_path / "cache"
		# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
		_UpperCAmelCase						:				List[Any]	    =					{"text": "string"}
		_UpperCAmelCase						:				List[str]	    =					features.copy() if features else default_expected_features
		_UpperCAmelCase						:				Optional[int]	    =					(
		    Features({feature: Value(__lowerCAmelCase      ) for feature, dtype in features.items()}      ) if features is not None else None
		)
		_UpperCAmelCase						:				Tuple	    =					TextDatasetReader({"train": text_path}					,       features=__lowerCAmelCase					,       cache_dir=__lowerCAmelCase      ).read()
		_check_text_datasetdict(__lowerCAmelCase					,       __lowerCAmelCase      )






@pytest.mark.parametrize("split"					,       [None, NamedSplit("train"      ), "train", "test"]      )
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		if split:
				_UpperCAmelCase						:				int	    =					{split: text_path}
		else:
				_UpperCAmelCase						:				Tuple	    =					"train"
				_UpperCAmelCase						:				List[str]	    =					{"train": text_path, "test": text_path}
		_UpperCAmelCase						:				Optional[Any]	    =					tmp_path / "cache"
		_UpperCAmelCase						:				Optional[int]	    =					{"text": "string"}
		_UpperCAmelCase						:				int	    =					TextDatasetReader(__lowerCAmelCase					,       cache_dir=__lowerCAmelCase      ).read()
		_check_text_datasetdict(__lowerCAmelCase					,       __lowerCAmelCase					,       splits=list(path.keys()      )      )
		assert all(dataset[split].split == split for split in path.keys()      )



