"""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 lowerCamelCase		(_SCREAMING_SNAKE_CASE      ):






					'''simple docstring'''


					def __init__(						self			:				Dict		)							->       Tuple:
									SCREAMING_SNAKE_CASE__		  =				[]



					def 			lowerCAmelCase_					(						self			:				Any   , _snake_case			:				Union[str, Any]   , _snake_case			:				Dict   , _snake_case			:				str   , **_snake_case			:				Any		)							->       Union[str, Any]:
									self.events.append("on_init_end"		)



					def 			lowerCAmelCase_					(						self			:				Dict   , _snake_case			:				List[str]   , _snake_case			:				str   , _snake_case			:				List[str]   , **_snake_case			:				str		)							->       Union[str, Any]:
									self.events.append("on_train_begin"		)



					def 			lowerCAmelCase_					(						self			:				Dict   , _snake_case			:				List[str]   , _snake_case			:				List[Any]   , _snake_case			:				Optional[Any]   , **_snake_case			:				Dict		)							->       Dict:
									self.events.append("on_train_end"		)



					def 			lowerCAmelCase_					(						self			:				int   , _snake_case			:				int   , _snake_case			:				Union[str, Any]   , _snake_case			:				List[str]   , **_snake_case			:				Any		)							->       Tuple:
									self.events.append("on_epoch_begin"		)



					def 			lowerCAmelCase_					(						self			:				Tuple   , _snake_case			:				Tuple   , _snake_case			:				Tuple   , _snake_case			:				List[Any]   , **_snake_case			:				Any		)							->       Union[str, Any]:
									self.events.append("on_epoch_end"		)



					def 			lowerCAmelCase_					(						self			:				Any   , _snake_case			:				Any   , _snake_case			:				Tuple   , _snake_case			:				Optional[int]   , **_snake_case			:				Optional[int]		)							->       List[str]:
									self.events.append("on_step_begin"		)



					def 			lowerCAmelCase_					(						self			:				Any   , _snake_case			:				Dict   , _snake_case			:				Tuple   , _snake_case			:				List[str]   , **_snake_case			:				Any		)							->       Optional[Any]:
									self.events.append("on_step_end"		)



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]   , _snake_case			:				List[str]   , _snake_case			:				List[str]   , _snake_case			:				str   , **_snake_case			:				str		)							->       Optional[Any]:
									self.events.append("on_evaluate"		)



					def 			lowerCAmelCase_					(						self			:				Tuple   , _snake_case			:				List[str]   , _snake_case			:				Union[str, Any]   , _snake_case			:				Tuple   , **_snake_case			:				Any		)							->       Tuple:
									self.events.append("on_predict"		)



					def 			lowerCAmelCase_					(						self			:				List[str]   , _snake_case			:				List[Any]   , _snake_case			:				List[str]   , _snake_case			:				Optional[Any]   , **_snake_case			:				str		)							->       List[Any]:
									self.events.append("on_save"		)



					def 			lowerCAmelCase_					(						self			:				Tuple   , _snake_case			:				Optional[int]   , _snake_case			:				Any   , _snake_case			:				Optional[Any]   , **_snake_case			:				List[Any]		)							->       Optional[int]:
									self.events.append("on_log"		)



					def 			lowerCAmelCase_					(						self			:				Optional[int]   , _snake_case			:				Optional[Any]   , _snake_case			:				int   , _snake_case			:				Tuple   , **_snake_case			:				Optional[int]		)							->       Dict:
									self.events.append("on_prediction_step"		)



@require_torch
class lowerCamelCase		(unittest.TestCase      ):






					'''simple docstring'''


					def 			lowerCAmelCase_					(						self			:				str		)							->       List[Any]:
									SCREAMING_SNAKE_CASE__		  =				tempfile.mkdtemp()



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       Any:
									shutil.rmtree(self.output_dir		)



					def 			lowerCAmelCase_					(						self			:				str   , _snake_case			:				List[str]=0   , _snake_case			:				Dict=0   , _snake_case			:				List[Any]=64   , _snake_case			:				int=64   , _snake_case			:				List[str]=None   , _snake_case			:				Dict=False   , **_snake_case			:				Union[str, Any]		)							->       int:
									# 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__		  =				RegressionDataset(length=_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				RegressionDataset(length=_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				RegressionModelConfig(a=_snake_case   , b=_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				RegressionPreTrainedModel(_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				TrainingArguments(self.output_dir   , disable_tqdm=_snake_case   , report_to=[]   , **_snake_case		)
									return Trainer(
									    _snake_case   , _snake_case   , train_dataset=_snake_case   , eval_dataset=_snake_case   , callbacks=_snake_case   , )



					def 			lowerCAmelCase_					(						self			:				Dict   , _snake_case			:				Optional[int]   , _snake_case			:				int		)							->       List[str]:
									self.assertEqual(len(_snake_case		)   , len(_snake_case		)		)

									# Order doesn't matter
									SCREAMING_SNAKE_CASE__		  =				sorted(_snake_case   , key=lambda _snake_case		: cb.__name__ if isinstance(_snake_case   , _snake_case		) else cb.__class__.__name__		)
									SCREAMING_SNAKE_CASE__		  =				sorted(_snake_case   , key=lambda _snake_case		: cb.__name__ if isinstance(_snake_case   , _snake_case		) else cb.__class__.__name__		)

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



					def 			lowerCAmelCase_					(						self			:				int   , _snake_case			:				Tuple		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				["on_init_end", "on_train_begin"]
									SCREAMING_SNAKE_CASE__		  =				0
									SCREAMING_SNAKE_CASE__		  =				len(trainer.get_eval_dataloader()		)
									SCREAMING_SNAKE_CASE__		  =				["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(_snake_case		):
																	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 			lowerCAmelCase_					(						self			:				List[Any]		)							->       Optional[int]:
									SCREAMING_SNAKE_CASE__		  =				self.get_trainer()
									SCREAMING_SNAKE_CASE__		  =				DEFAULT_CALLBACKS.copy() + [ProgressCallback]
									self.check_callbacks_equality(trainer.callback_handler.callbacks   , _snake_case		)

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

									# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
									SCREAMING_SNAKE_CASE__		  =				self.get_trainer(disable_tqdm=_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				DEFAULT_CALLBACKS.copy() + [PrinterCallback]
									self.check_callbacks_equality(trainer.callback_handler.callbacks   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       List[Any]:
									SCREAMING_SNAKE_CASE__		  =				DEFAULT_CALLBACKS.copy() + [ProgressCallback]
									SCREAMING_SNAKE_CASE__		  =				self.get_trainer()

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

									SCREAMING_SNAKE_CASE__		  =				self.get_trainer()
									SCREAMING_SNAKE_CASE__		  =				trainer.pop_callback(_snake_case		)
									self.assertEqual(cb.__class__   , _snake_case		)
									self.check_callbacks_equality(trainer.callback_handler.callbacks   , _snake_case		)

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

									# We can also add, pop, or remove by instance
									SCREAMING_SNAKE_CASE__		  =				self.get_trainer()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[0]
									trainer.remove_callback(_snake_case		)
									expected_callbacks.remove(_snake_case		)
									self.check_callbacks_equality(trainer.callback_handler.callbacks   , _snake_case		)

									SCREAMING_SNAKE_CASE__		  =				self.get_trainer()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[0]
									SCREAMING_SNAKE_CASE__		  =				trainer.pop_callback(_snake_case		)
									self.assertEqual(_snake_case   , _snake_case		)
									self.check_callbacks_equality(trainer.callback_handler.callbacks   , _snake_case		)

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



					def 			lowerCAmelCase_					(						self			:				Dict		)							->       Optional[int]:
									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=_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				self.get_trainer(callbacks=[MyTestTrainerCallback]		)
									trainer.train()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[-2].events
									self.assertEqual(_snake_case   , self.get_expected_events(_snake_case		)		)

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

									SCREAMING_SNAKE_CASE__		  =				self.get_trainer(callbacks=[MyTestTrainerCallback]   , save_steps=5		)
									trainer.train()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[-2].events
									self.assertEqual(_snake_case   , self.get_expected_events(_snake_case		)		)

									SCREAMING_SNAKE_CASE__		  =				self.get_trainer(callbacks=[MyTestTrainerCallback]   , eval_steps=5   , evaluation_strategy="steps"		)
									trainer.train()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[-2].events
									self.assertEqual(_snake_case   , self.get_expected_events(_snake_case		)		)

									SCREAMING_SNAKE_CASE__		  =				self.get_trainer(callbacks=[MyTestTrainerCallback]   , evaluation_strategy="epoch"		)
									trainer.train()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[-2].events
									self.assertEqual(_snake_case   , self.get_expected_events(_snake_case		)		)

									# A bit of everything
									SCREAMING_SNAKE_CASE__		  =				self.get_trainer(
									    callbacks=[MyTestTrainerCallback]   , logging_steps=3   , save_steps=10   , eval_steps=5   , evaluation_strategy="steps"   , )
									trainer.train()
									SCREAMING_SNAKE_CASE__		  =				trainer.callback_handler.callbacks[-2].events
									self.assertEqual(_snake_case   , self.get_expected_events(_snake_case		)		)

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





"""simple docstring"""






from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream

class lowerCamelCase		(_SCREAMING_SNAKE_CASE      ):






					'''simple docstring'''


					def __init__(						self			:				List[str]   , _snake_case			:				Callable   , _snake_case			:				Optional[Features] = None   , _snake_case			:				str = None   , _snake_case			:				bool = False   , _snake_case			:				bool = False   , _snake_case			:				Optional[dict] = None   , _snake_case			:				Optional[int] = None   , **_snake_case			:				Dict   , )							->       Any:
									super().__init__(
									    features=_snake_case   , cache_dir=_snake_case   , keep_in_memory=_snake_case   , streaming=_snake_case   , num_proc=_snake_case   , **_snake_case   , )
									SCREAMING_SNAKE_CASE__		  =				Generator(
									    cache_dir=_snake_case   , features=_snake_case   , generator=_snake_case   , gen_kwargs=_snake_case   , **_snake_case   , )



					def 			lowerCAmelCase_					(						self			:				List[str]		)							->       Optional[Any]:
									# Build iterable dataset
									if self.streaming:
													SCREAMING_SNAKE_CASE__		  =				self.builder.as_streaming_dataset(split="train"		)
									# Build regular (map-style) dataset
									else:
													SCREAMING_SNAKE_CASE__		  =				None
													SCREAMING_SNAKE_CASE__		  =				None
													SCREAMING_SNAKE_CASE__		  =				None
													SCREAMING_SNAKE_CASE__		  =				None

													self.builder.download_and_prepare(
													    download_config=_snake_case   , download_mode=_snake_case   , verification_mode=_snake_case   , base_path=_snake_case   , num_proc=self.num_proc   , )
													SCREAMING_SNAKE_CASE__		  =				self.builder.as_dataset(
													    split="train"   , verification_mode=_snake_case   , in_memory=self.keep_in_memory		)
									return dataset




