


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     UpperCamelCase(  _a   ):


					def __init__(						self	:		Tuple						)			->	str:




											'''simple docstring'''

											__snake_case				=		[]


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any     ,    SCREAMING_SNAKE_CASE	:		Any     ,    SCREAMING_SNAKE_CASE	:		int     ,    SCREAMING_SNAKE_CASE	:		Optional[Any]     ,    **SCREAMING_SNAKE_CASE	:		Tuple						)			->	Optional[int]:




											'''simple docstring'''

											self.events.append("on_init_end"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		int     ,    SCREAMING_SNAKE_CASE	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    **SCREAMING_SNAKE_CASE	:		Optional[int]						)			->	Optional[Any]:




											'''simple docstring'''

											self.events.append("on_train_begin"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    **SCREAMING_SNAKE_CASE	:		int						)			->	Optional[int]:




											'''simple docstring'''

											self.events.append("on_train_end"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    **SCREAMING_SNAKE_CASE	:		int						)			->	Any:




											'''simple docstring'''

											self.events.append("on_epoch_begin"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict     ,    SCREAMING_SNAKE_CASE	:		Dict     ,    SCREAMING_SNAKE_CASE	:		List[Any]     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    **SCREAMING_SNAKE_CASE	:		List[str]						)			->	Optional[int]:




											'''simple docstring'''

											self.events.append("on_epoch_end"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		str     ,    SCREAMING_SNAKE_CASE	:		List[str]     ,    **SCREAMING_SNAKE_CASE	:		List[Any]						)			->	Any:




											'''simple docstring'''

											self.events.append("on_step_begin"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		str     ,    **SCREAMING_SNAKE_CASE	:		List[str]						)			->	Union[str, Any]:




											'''simple docstring'''

											self.events.append("on_step_end"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Dict     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    **SCREAMING_SNAKE_CASE	:		List[Any]						)			->	List[str]:




											'''simple docstring'''

											self.events.append("on_evaluate"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		Any     ,    SCREAMING_SNAKE_CASE	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		Any     ,    **SCREAMING_SNAKE_CASE	:		Optional[int]						)			->	int:




											'''simple docstring'''

											self.events.append("on_predict"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		int     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		str     ,    **SCREAMING_SNAKE_CASE	:		Optional[Any]						)			->	List[str]:




											'''simple docstring'''

											self.events.append("on_save"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Any     ,    **SCREAMING_SNAKE_CASE	:		Tuple						)			->	Dict:




											'''simple docstring'''

											self.events.append("on_log"						)






					def 							SCREAMING_SNAKE_CASE_  (						self	:		int     ,    SCREAMING_SNAKE_CASE	:		str     ,    SCREAMING_SNAKE_CASE	:		int     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    **SCREAMING_SNAKE_CASE	:		str						)			->	Dict:




											'''simple docstring'''

											self.events.append("on_prediction_step"						)






@require_torch
class     UpperCamelCase(  unittest.TestCase   ):


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict						)			->	List[Any]:




											'''simple docstring'''

											__snake_case				=		tempfile.mkdtemp()


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	str:




											'''simple docstring'''

											shutil.rmtree(self.output_dir						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		List[str]=0     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]=0     ,    SCREAMING_SNAKE_CASE	:		Optional[int]=6_4     ,    SCREAMING_SNAKE_CASE	:		Tuple=6_4     ,    SCREAMING_SNAKE_CASE	:		int=None     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]=False     ,    **SCREAMING_SNAKE_CASE	:		List[str]						)			->	Optional[Any]:




											'''simple docstring'''

											__snake_case				=		RegressionDataset(length=SCREAMING_SNAKE_CASE						)
											__snake_case				=		RegressionDataset(length=SCREAMING_SNAKE_CASE						)
											__snake_case				=		RegressionModelConfig(a=SCREAMING_SNAKE_CASE     ,    b=SCREAMING_SNAKE_CASE						)
											__snake_case				=		RegressionPreTrainedModel(SCREAMING_SNAKE_CASE						)

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		int     ,    SCREAMING_SNAKE_CASE	:		str     ,    SCREAMING_SNAKE_CASE	:		str						)			->	List[str]:




											'''simple docstring'''

											self.assertEqual(len(SCREAMING_SNAKE_CASE						)     ,    len(SCREAMING_SNAKE_CASE						)						)

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

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Optional[int]						)			->	str:




											'''simple docstring'''

											__snake_case				=		["on_init_end", "on_train_begin"]
											__snake_case				=		0
											__snake_case				=		len(trainer.get_eval_dataloader()						)
											__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(SCREAMING_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 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		self.get_trainer()
											__snake_case				=		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											self.check_callbacks_equality(trainer.callback_handler.callbacks     ,    SCREAMING_SNAKE_CASE						)

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

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[Any]						)			->	Optional[int]:




											'''simple docstring'''

											__snake_case				=		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											__snake_case				=		self.get_trainer()

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

											__snake_case				=		self.get_trainer()
											__snake_case				=		trainer.pop_callback(SCREAMING_SNAKE_CASE						)
											self.assertEqual(cb.__class__     ,    SCREAMING_SNAKE_CASE						)
											self.check_callbacks_equality(trainer.callback_handler.callbacks     ,    SCREAMING_SNAKE_CASE						)

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

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

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

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






					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict						)			->	str:




											'''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=SCREAMING_SNAKE_CASE						)

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

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

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

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

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

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

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









import warnings

from ...utils import logging
from .image_processing_beit import BeitImageProcessor


A							:					Any     						=     logging.get_logger(__name__)






class     UpperCamelCase(  _a   ):


					def __init__(						self	:		Optional[int]     ,    *SCREAMING_SNAKE_CASE	:		Tuple     ,    **SCREAMING_SNAKE_CASE	:		List[Any]						)			->	None:




											'''simple docstring'''

											warnings.warn(
											    "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
											    " use BeitImageProcessor instead."     ,    SCREAMING_SNAKE_CASE     ,    )
											super().__init__(*SCREAMING_SNAKE_CASE     ,    **SCREAMING_SNAKE_CASE						)





