





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					(       _UpperCamelCase      ):


	'''simple docstring'''





	def __init__(						self:						int			):
			lowercase__					:      int      =		[]

	def    snake_case__(						self:						Tuple,					lowerCamelCase_:						int,					lowerCamelCase_:						Optional[int],					lowerCamelCase_:						str,					**lowerCamelCase_:						Dict			):
			self.events.append('on_init_end'			)

	def    snake_case__(						self:						Any,					lowerCamelCase_:						Dict,					lowerCamelCase_:						Optional[int],					lowerCamelCase_:						str,					**lowerCamelCase_:						Optional[Any]			):
			self.events.append('on_train_begin'			)

	def    snake_case__(						self:						List[Any],					lowerCamelCase_:						Union[str, Any],					lowerCamelCase_:						Dict,					lowerCamelCase_:						Optional[int],					**lowerCamelCase_:						Any			):
			self.events.append('on_train_end'			)

	def    snake_case__(						self:						Optional[int],					lowerCamelCase_:						List[Any],					lowerCamelCase_:						int,					lowerCamelCase_:						Any,					**lowerCamelCase_:						List[str]			):
			self.events.append('on_epoch_begin'			)

	def    snake_case__(						self:						Optional[Any],					lowerCamelCase_:						Optional[Any],					lowerCamelCase_:						Tuple,					lowerCamelCase_:						str,					**lowerCamelCase_:						Tuple			):
			self.events.append('on_epoch_end'			)

	def    snake_case__(						self:						int,					lowerCamelCase_:						List[Any],					lowerCamelCase_:						List[str],					lowerCamelCase_:						List[str],					**lowerCamelCase_:						Union[str, Any]			):
			self.events.append('on_step_begin'			)

	def    snake_case__(						self:						Dict,					lowerCamelCase_:						Tuple,					lowerCamelCase_:						str,					lowerCamelCase_:						Optional[int],					**lowerCamelCase_:						Dict			):
			self.events.append('on_step_end'			)

	def    snake_case__(						self:						Any,					lowerCamelCase_:						List[Any],					lowerCamelCase_:						Optional[int],					lowerCamelCase_:						Dict,					**lowerCamelCase_:						Any			):
			self.events.append('on_evaluate'			)

	def    snake_case__(						self:						str,					lowerCamelCase_:						str,					lowerCamelCase_:						str,					lowerCamelCase_:						Dict,					**lowerCamelCase_:						Any			):
			self.events.append('on_predict'			)

	def    snake_case__(						self:						int,					lowerCamelCase_:						Optional[Any],					lowerCamelCase_:						List[Any],					lowerCamelCase_:						Optional[Any],					**lowerCamelCase_:						int			):
			self.events.append('on_save'			)

	def    snake_case__(						self:						int,					lowerCamelCase_:						str,					lowerCamelCase_:						Optional[int],					lowerCamelCase_:						List[Any],					**lowerCamelCase_:						Any			):
			self.events.append('on_log'			)





	def    snake_case__(						self:						Tuple,					lowerCamelCase_:						Dict,					lowerCamelCase_:						str,					lowerCamelCase_:						Optional[Any],					**lowerCamelCase_:						Dict			):
			self.events.append('on_prediction_step'			)


@require_torch
class       __lowerCAmelCase					(       unittest.TestCase      ):


	'''simple docstring'''





	def    snake_case__(						self:						Optional[int]			):
			lowercase__					:      Any      =		tempfile.mkdtemp()

	def    snake_case__(						self:						int			):
			shutil.rmtree(self.output_dir			)

	def    snake_case__(						self:						List[str],					lowerCamelCase_:						Optional[int]=0,					lowerCamelCase_:						List[Any]=0,					lowerCamelCase_:						int=64,					lowerCamelCase_:						int=64,					lowerCamelCase_:						str=None,					lowerCamelCase_:						List[str]=False,					**lowerCamelCase_:						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.
			lowercase__					:      Dict      =		RegressionDataset(length=lowerCamelCase_			)
			lowercase__					:      Dict      =		RegressionDataset(length=lowerCamelCase_			)
			lowercase__					:      List[Any]      =		RegressionModelConfig(a=lowerCamelCase_,					b=lowerCamelCase_			)
			lowercase__					:      List[str]      =		RegressionPreTrainedModel(lowerCamelCase_			)

			lowercase__					:      Tuple      =		TrainingArguments(self.output_dir,					disable_tqdm=lowerCamelCase_,					report_to=[],					**lowerCamelCase_			)
			return Trainer(
			    lowerCamelCase_,					lowerCamelCase_,					train_dataset=lowerCamelCase_,					eval_dataset=lowerCamelCase_,					callbacks=lowerCamelCase_,					)

	def    snake_case__(						self:						Any,					lowerCamelCase_:						Tuple,					lowerCamelCase_:						List[Any]			):
			self.assertEqual(len(lowerCamelCase_			),					len(lowerCamelCase_			)			)

			# Order doesn't matter
			lowercase__					:      str      =		sorted(lowerCamelCase_,					key=lambda lowerCamelCase_			: cb.__name__ if isinstance(lowerCamelCase_,					lowerCamelCase_			) else cb.__class__.__name__			)
			lowercase__					:      str      =		sorted(lowerCamelCase_,					key=lambda lowerCamelCase_			: 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    snake_case__(						self:						Union[str, Any],					lowerCamelCase_:						int			):
			lowercase__					:      Any      =		['on_init_end', 'on_train_begin']
			lowercase__					:      List[str]      =		0
			lowercase__					:      int      =		len(trainer.get_eval_dataloader()			)
			lowercase__					:      List[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(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    snake_case__(						self:						str			):
			lowercase__					:      Optional[Any]      =		self.get_trainer()
			lowercase__					:      List[str]      =		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
			self.check_callbacks_equality(trainer.callback_handler.callbacks,					lowerCamelCase_			)

			# Callbacks passed at init are added to the default callbacks
			lowercase__					:      List[str]      =		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
			lowercase__					:      Dict      =		self.get_trainer(disable_tqdm=lowerCamelCase_			)
			lowercase__					:      Any      =		DEFAULT_CALLBACKS.copy() + [PrinterCallback]
			self.check_callbacks_equality(trainer.callback_handler.callbacks,					lowerCamelCase_			)

	def    snake_case__(						self:						Any			):
			lowercase__					:      Union[str, Any]      =		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
			lowercase__					:      Optional[int]      =		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_			)

			lowercase__					:      Dict      =		self.get_trainer()
			lowercase__					:      Dict      =		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
			lowercase__					:      Union[str, Any]      =		self.get_trainer()
			lowercase__					:      Dict      =		trainer.callback_handler.callbacks[0]
			trainer.remove_callback(lowerCamelCase_			)
			expected_callbacks.remove(lowerCamelCase_			)
			self.check_callbacks_equality(trainer.callback_handler.callbacks,					lowerCamelCase_			)

			lowercase__					:      Optional[int]      =		self.get_trainer()
			lowercase__					:      List[str]      =		trainer.callback_handler.callbacks[0]
			lowercase__					:      Any      =		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    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=lowerCamelCase_			)

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

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

			lowercase__					:      Union[str, Any]      =		self.get_trainer(callbacks=[MyTestTrainerCallback],					save_steps=5			)
			trainer.train()
			lowercase__					:      Optional[Any]      =		trainer.callback_handler.callbacks[-2].events
			self.assertEqual(lowerCamelCase_,					self.get_expected_events(lowerCamelCase_			)			)

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

			lowercase__					:      List[Any]      =		self.get_trainer(callbacks=[MyTestTrainerCallback],					evaluation_strategy='epoch'			)
			trainer.train()
			lowercase__					:      Tuple      =		trainer.callback_handler.callbacks[-2].events
			self.assertEqual(lowerCamelCase_,					self.get_expected_events(lowerCamelCase_			)			)

			# A bit of everything
			lowercase__					:      Optional[int]      =		self.get_trainer(
			    callbacks=[MyTestTrainerCallback],					logging_steps=3,					save_steps=10,					eval_steps=5,					evaluation_strategy='steps',					)
			trainer.train()
			lowercase__					:      List[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:
					lowercase__					:      int      =		self.get_trainer(
					    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback],					)
					assert str(lowerCamelCase_			) in warn_mock.call_args[0][0]









def       SCREAMING_SNAKE_CASE__  (       _lowercase     : str			)   ->							str:




		'''simple docstring'''
		if not all(char in '01' for char in bin_string			):
				raise ValueError('Non-binary value was passed to the function'			)
		if not bin_string:
				raise ValueError('Empty string was passed to the function'			)
		lowercase__					:      Tuple      =		''
		while len(_lowercase			) % 3 != 0:
				lowercase__					:      Optional[int]      =		'0' + bin_string
		lowercase__					:      List[Any]      =		[
		    bin_string[index : index + 3]
		    for index in range(len(_lowercase			)			)
		    if index % 3 == 0
		]
		for bin_group in bin_string_in_3_list:
				lowercase__					:      int      =		0
				for index, val in enumerate(_lowercase			):
						oct_val += int(2 ** (2 - index) * int(_lowercase			)			)
				oct_string += str(_lowercase			)
		return oct_string


if __name__ == "__main__":
			from doctest import testmod

			testmod()


